blob: 4203818baaf788989a5595bd497b90dbc7ceb3b8 [file] [log] [blame]
Chris Lattner476e6df2001-12-03 17:28:42 +00001//===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===//
2//
3// InductionVariableSimplify - Transform induction variables in a program
4// to all use a single cannonical induction variable per loop.
5//
6//===----------------------------------------------------------------------===//
7
Chris Lattnerb4cfa7f2002-05-07 20:03:00 +00008#include "llvm/Transforms/Scalar.h"
Chris Lattner476e6df2001-12-03 17:28:42 +00009#include "llvm/Analysis/InductionVariable.h"
10#include "llvm/Analysis/LoopInfo.h"
Chris Lattner476e6df2001-12-03 17:28:42 +000011#include "llvm/iPHINode.h"
Chris Lattner91daaab2001-12-04 04:32:29 +000012#include "llvm/iOther.h"
13#include "llvm/Type.h"
Chris Lattnerca142372002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner83d485b2002-02-12 22:39:50 +000015#include "llvm/Support/CFG.h"
Chris Lattner8abcd562003-08-01 22:15:03 +000016#include "Support/Debug.h"
Chris Lattnerbf3a0992002-10-01 22:38:41 +000017#include "Support/Statistic.h"
Chris Lattner8abcd562003-08-01 22:15:03 +000018#include "Support/STLExtras.h"
Chris Lattner0b18c1d2002-05-10 15:38:35 +000019
Chris Lattner4184bcc2002-09-10 05:24:05 +000020namespace {
Chris Lattnerbf3a0992002-10-01 22:38:41 +000021 Statistic<> NumRemoved ("indvars", "Number of aux indvars removed");
22 Statistic<> NumInserted("indvars", "Number of cannonical indvars added");
Chris Lattner4184bcc2002-09-10 05:24:05 +000023}
Chris Lattner91daaab2001-12-04 04:32:29 +000024
25// InsertCast - Cast Val to Ty, setting a useful name on the cast if Val has a
26// name...
27//
Chris Lattner4184bcc2002-09-10 05:24:05 +000028static Instruction *InsertCast(Value *Val, const Type *Ty,
Chris Lattner28a8d242002-09-10 17:04:02 +000029 Instruction *InsertBefore) {
30 return new CastInst(Val, Ty, Val->getName()+"-casted", InsertBefore);
Chris Lattner91daaab2001-12-04 04:32:29 +000031}
32
Chris Lattner78dd56f2002-04-28 16:21:30 +000033static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
Chris Lattner476e6df2001-12-03 17:28:42 +000034 // Transform all subloops before this loop...
35 bool Changed = reduce_apply_bool(Loop->getSubLoops().begin(),
36 Loop->getSubLoops().end(),
Chris Lattner7f74a562002-01-20 22:54:45 +000037 std::bind1st(std::ptr_fun(TransformLoop), Loops));
Chris Lattner476e6df2001-12-03 17:28:42 +000038 // Get the header node for this loop. All of the phi nodes that could be
39 // induction variables must live in this basic block.
Chris Lattner0b18c1d2002-05-10 15:38:35 +000040 //
41 BasicBlock *Header = Loop->getBlocks().front();
Chris Lattner476e6df2001-12-03 17:28:42 +000042
43 // Loop over all of the PHI nodes in the basic block, calculating the
44 // induction variables that they represent... stuffing the induction variable
45 // info into a vector...
46 //
Chris Lattner7f74a562002-01-20 22:54:45 +000047 std::vector<InductionVariable> IndVars; // Induction variables for block
Chris Lattner113f4f42002-06-25 16:13:24 +000048 BasicBlock::iterator AfterPHIIt = Header->begin();
Chris Lattner889f6202003-04-23 16:37:45 +000049 for (; PHINode *PN = dyn_cast<PHINode>(AfterPHIIt); ++AfterPHIIt)
Chris Lattner476e6df2001-12-03 17:28:42 +000050 IndVars.push_back(InductionVariable(PN, Loops));
Chris Lattner113f4f42002-06-25 16:13:24 +000051 // AfterPHIIt now points to first nonphi instruction...
Chris Lattner476e6df2001-12-03 17:28:42 +000052
Chris Lattner91daaab2001-12-04 04:32:29 +000053 // If there are no phi nodes in this basic block, there can't be indvars...
Chris Lattner476e6df2001-12-03 17:28:42 +000054 if (IndVars.empty()) return Changed;
55
56 // Loop over the induction variables, looking for a cannonical induction
57 // variable, and checking to make sure they are not all unknown induction
58 // variables.
59 //
60 bool FoundIndVars = false;
61 InductionVariable *Cannonical = 0;
62 for (unsigned i = 0; i < IndVars.size(); ++i) {
Chris Lattner4184bcc2002-09-10 05:24:05 +000063 if (IndVars[i].InductionType == InductionVariable::Cannonical &&
64 !isa<PointerType>(IndVars[i].Phi->getType()))
Chris Lattner476e6df2001-12-03 17:28:42 +000065 Cannonical = &IndVars[i];
66 if (IndVars[i].InductionType != InductionVariable::Unknown)
67 FoundIndVars = true;
68 }
69
70 // No induction variables, bail early... don't add a cannonnical indvar
71 if (!FoundIndVars) return Changed;
72
73 // Okay, we want to convert other induction variables to use a cannonical
74 // indvar. If we don't have one, add one now...
75 if (!Cannonical) {
Chris Lattner28a8d242002-09-10 17:04:02 +000076 // Create the PHI node for the new induction variable, and insert the phi
77 // node at the end of the other phi nodes...
78 PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar", AfterPHIIt);
Chris Lattner91daaab2001-12-04 04:32:29 +000079
80 // Create the increment instruction to add one to the counter...
81 Instruction *Add = BinaryOperator::create(Instruction::Add, PN,
82 ConstantUInt::get(Type::UIntTy,1),
Chris Lattner28a8d242002-09-10 17:04:02 +000083 "add1-indvar", AfterPHIIt);
Chris Lattner91daaab2001-12-04 04:32:29 +000084
85 // Figure out which block is incoming and which is the backedge for the loop
86 BasicBlock *Incoming, *BackEdgeBlock;
Chris Lattner83d485b2002-02-12 22:39:50 +000087 pred_iterator PI = pred_begin(Header);
88 assert(PI != pred_end(Header) && "Loop headers should have 2 preds!");
Chris Lattner91daaab2001-12-04 04:32:29 +000089 if (Loop->contains(*PI)) { // First pred is back edge...
90 BackEdgeBlock = *PI++;
91 Incoming = *PI++;
92 } else {
93 Incoming = *PI++;
94 BackEdgeBlock = *PI++;
95 }
Chris Lattner83d485b2002-02-12 22:39:50 +000096 assert(PI == pred_end(Header) && "Loop headers should have 2 preds!");
Chris Lattner91daaab2001-12-04 04:32:29 +000097
98 // Add incoming values for the PHI node...
Chris Lattner2716b5e2002-04-27 02:25:14 +000099 PN->addIncoming(Constant::getNullValue(Type::UIntTy), Incoming);
Chris Lattner91daaab2001-12-04 04:32:29 +0000100 PN->addIncoming(Add, BackEdgeBlock);
101
102 // Analyze the new induction variable...
103 IndVars.push_back(InductionVariable(PN, Loops));
104 assert(IndVars.back().InductionType == InductionVariable::Cannonical &&
105 "Just inserted cannonical indvar that is not cannonical!");
106 Cannonical = &IndVars.back();
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000107 ++NumInserted;
Chris Lattner67439402001-12-05 19:41:33 +0000108 Changed = true;
Chris Lattner91daaab2001-12-04 04:32:29 +0000109 }
110
Anand Shukla2bc64192002-06-25 21:07:58 +0000111 DEBUG(std::cerr << "Induction variables:\n");
Chris Lattner91daaab2001-12-04 04:32:29 +0000112
113 // Get the current loop iteration count, which is always the value of the
114 // cannonical phi node...
115 //
116 PHINode *IterCount = Cannonical->Phi;
117
118 // Loop through and replace all of the auxillary induction variables with
119 // references to the primary induction variable...
120 //
Chris Lattner91daaab2001-12-04 04:32:29 +0000121 for (unsigned i = 0; i < IndVars.size(); ++i) {
122 InductionVariable *IV = &IndVars[i];
Chris Lattner71cbd422002-05-22 17:17:27 +0000123
Chris Lattner26750072002-07-27 01:12:17 +0000124 DEBUG(IV->print(std::cerr));
Chris Lattner71cbd422002-05-22 17:17:27 +0000125
Chris Lattner4184bcc2002-09-10 05:24:05 +0000126 // Don't do math with pointers...
127 const Type *IVTy = IV->Phi->getType();
128 if (isa<PointerType>(IVTy)) IVTy = Type::ULongTy;
129
Chris Lattnerd23d7522001-12-04 08:13:06 +0000130 // Don't modify the cannonical indvar or unrecognized indvars...
131 if (IV != Cannonical && IV->InductionType != InductionVariable::Unknown) {
Chris Lattner91daaab2001-12-04 04:32:29 +0000132 Instruction *Val = IterCount;
133 if (!isa<ConstantInt>(IV->Step) || // If the step != 1
134 !cast<ConstantInt>(IV->Step)->equalsInt(1)) {
Chris Lattner91daaab2001-12-04 04:32:29 +0000135
136 // If the types are not compatible, insert a cast now...
Chris Lattner4184bcc2002-09-10 05:24:05 +0000137 if (Val->getType() != IVTy)
138 Val = InsertCast(Val, IVTy, AfterPHIIt);
139 if (IV->Step->getType() != IVTy)
140 IV->Step = InsertCast(IV->Step, IVTy, AfterPHIIt);
Chris Lattner91daaab2001-12-04 04:32:29 +0000141
Chris Lattner4184bcc2002-09-10 05:24:05 +0000142 Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step,
Chris Lattner28a8d242002-09-10 17:04:02 +0000143 IV->Phi->getName()+"-scale", AfterPHIIt);
Chris Lattner91daaab2001-12-04 04:32:29 +0000144 }
145
Chris Lattner4184bcc2002-09-10 05:24:05 +0000146 // If the start != 0
147 if (IV->Start != Constant::getNullValue(IV->Start->getType())) {
Chris Lattner91daaab2001-12-04 04:32:29 +0000148 // If the types are not compatible, insert a cast now...
Chris Lattner4184bcc2002-09-10 05:24:05 +0000149 if (Val->getType() != IVTy)
150 Val = InsertCast(Val, IVTy, AfterPHIIt);
151 if (IV->Start->getType() != IVTy)
152 IV->Start = InsertCast(IV->Start, IVTy, AfterPHIIt);
Chris Lattner91daaab2001-12-04 04:32:29 +0000153
Chris Lattner28a8d242002-09-10 17:04:02 +0000154 // Insert the instruction after the phi nodes...
Chris Lattner4184bcc2002-09-10 05:24:05 +0000155 Val = BinaryOperator::create(Instruction::Add, Val, IV->Start,
Chris Lattner28a8d242002-09-10 17:04:02 +0000156 IV->Phi->getName()+"-offset", AfterPHIIt);
Chris Lattner91daaab2001-12-04 04:32:29 +0000157 }
158
159 // If the PHI node has a different type than val is, insert a cast now...
160 if (Val->getType() != IV->Phi->getType())
Chris Lattner113f4f42002-06-25 16:13:24 +0000161 Val = InsertCast(Val, IV->Phi->getType(), AfterPHIIt);
Chris Lattner91daaab2001-12-04 04:32:29 +0000162
163 // Replace all uses of the old PHI node with the new computed value...
164 IV->Phi->replaceAllUsesWith(Val);
165
166 // Move the PHI name to it's new equivalent value...
Chris Lattner7f74a562002-01-20 22:54:45 +0000167 std::string OldName = IV->Phi->getName();
Chris Lattner91daaab2001-12-04 04:32:29 +0000168 IV->Phi->setName("");
169 Val->setName(OldName);
170
171 // Delete the old, now unused, phi node...
Chris Lattner113f4f42002-06-25 16:13:24 +0000172 Header->getInstList().erase(IV->Phi);
Chris Lattner67439402001-12-05 19:41:33 +0000173 Changed = true;
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000174 ++NumRemoved;
Chris Lattner91daaab2001-12-04 04:32:29 +0000175 }
Chris Lattner476e6df2001-12-03 17:28:42 +0000176 }
177
178 return Changed;
179}
180
Chris Lattner04805fa2002-02-26 21:46:54 +0000181namespace {
Chris Lattnerc8e66542002-04-27 06:56:12 +0000182 struct InductionVariableSimplify : public FunctionPass {
Chris Lattner113f4f42002-06-25 16:13:24 +0000183 virtual bool runOnFunction(Function &) {
Chris Lattner0b18c1d2002-05-10 15:38:35 +0000184 LoopInfo &LI = getAnalysis<LoopInfo>();
185
186 // Induction Variables live in the header nodes of loops
187 return reduce_apply_bool(LI.getTopLevelLoops().begin(),
188 LI.getTopLevelLoops().end(),
189 std::bind1st(std::ptr_fun(TransformLoop), &LI));
Chris Lattner04805fa2002-02-26 21:46:54 +0000190 }
191
Chris Lattnerc8e66542002-04-27 06:56:12 +0000192 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattnerf0ed55d2002-08-08 19:01:30 +0000193 AU.addRequired<LoopInfo>();
Chris Lattner820d9712002-10-21 20:00:28 +0000194 AU.setPreservesCFG();
Chris Lattner04805fa2002-02-26 21:46:54 +0000195 }
196 };
Chris Lattnerc8b70922002-07-26 21:12:46 +0000197 RegisterOpt<InductionVariableSimplify> X("indvars",
Chris Lattnerb28b6802002-07-23 18:06:35 +0000198 "Cannonicalize Induction Variables");
Chris Lattnerd5d56782002-01-31 00:45:11 +0000199}
200
Chris Lattner04805fa2002-02-26 21:46:54 +0000201Pass *createIndVarSimplifyPass() {
202 return new InductionVariableSimplify();
Chris Lattnerd5d56782002-01-31 00:45:11 +0000203}