blob: fbffb308369b451e5ca1ab666b4d053d2e87184e [file] [log] [blame]
Chris Lattner6148c022001-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 Lattner022103b2002-05-07 20:03:00 +00008#include "llvm/Transforms/Scalar.h"
Chris Lattner6148c022001-12-03 17:28:42 +00009#include "llvm/Analysis/InductionVariable.h"
10#include "llvm/Analysis/LoopInfo.h"
Chris Lattner6148c022001-12-03 17:28:42 +000011#include "llvm/iPHINode.h"
Chris Lattner394437f2001-12-04 04:32:29 +000012#include "llvm/iOther.h"
13#include "llvm/Type.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000014#include "llvm/Constants.h"
Chris Lattner455889a2002-02-12 22:39:50 +000015#include "llvm/Support/CFG.h"
Chris Lattner6148c022001-12-03 17:28:42 +000016#include "Support/STLExtras.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000017#include "Support/StatisticReporter.h"
18
Chris Lattner5e761402002-09-10 05:24:05 +000019namespace {
20 Statistic<> NumRemoved ("indvars\t\t- Number of aux indvars removed");
21 Statistic<> NumInserted("indvars\t\t- Number of cannonical indvars added");
22}
Chris Lattner394437f2001-12-04 04:32:29 +000023
24// InsertCast - Cast Val to Ty, setting a useful name on the cast if Val has a
25// name...
26//
Chris Lattner5e761402002-09-10 05:24:05 +000027static Instruction *InsertCast(Value *Val, const Type *Ty,
Chris Lattner394437f2001-12-04 04:32:29 +000028 BasicBlock::iterator It) {
29 Instruction *Cast = new CastInst(Val, Ty);
30 if (Val->hasName()) Cast->setName(Val->getName()+"-casted");
Chris Lattner5e761402002-09-10 05:24:05 +000031 It->getParent()->getInstList().insert(It, Cast);
Chris Lattner394437f2001-12-04 04:32:29 +000032 return Cast;
33}
34
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000035static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
Chris Lattner6148c022001-12-03 17:28:42 +000036 // Transform all subloops before this loop...
37 bool Changed = reduce_apply_bool(Loop->getSubLoops().begin(),
38 Loop->getSubLoops().end(),
Chris Lattner697954c2002-01-20 22:54:45 +000039 std::bind1st(std::ptr_fun(TransformLoop), Loops));
Chris Lattner6148c022001-12-03 17:28:42 +000040 // Get the header node for this loop. All of the phi nodes that could be
41 // induction variables must live in this basic block.
Chris Lattner3dec1f22002-05-10 15:38:35 +000042 //
43 BasicBlock *Header = Loop->getBlocks().front();
Chris Lattner6148c022001-12-03 17:28:42 +000044
45 // Loop over all of the PHI nodes in the basic block, calculating the
46 // induction variables that they represent... stuffing the induction variable
47 // info into a vector...
48 //
Chris Lattner697954c2002-01-20 22:54:45 +000049 std::vector<InductionVariable> IndVars; // Induction variables for block
Chris Lattner7e708292002-06-25 16:13:24 +000050 BasicBlock::iterator AfterPHIIt = Header->begin();
51 for (; PHINode *PN = dyn_cast<PHINode>(&*AfterPHIIt); ++AfterPHIIt)
Chris Lattner6148c022001-12-03 17:28:42 +000052 IndVars.push_back(InductionVariable(PN, Loops));
Chris Lattner7e708292002-06-25 16:13:24 +000053 // AfterPHIIt now points to first nonphi instruction...
Chris Lattner6148c022001-12-03 17:28:42 +000054
Chris Lattner394437f2001-12-04 04:32:29 +000055 // If there are no phi nodes in this basic block, there can't be indvars...
Chris Lattner6148c022001-12-03 17:28:42 +000056 if (IndVars.empty()) return Changed;
57
58 // Loop over the induction variables, looking for a cannonical induction
59 // variable, and checking to make sure they are not all unknown induction
60 // variables.
61 //
62 bool FoundIndVars = false;
63 InductionVariable *Cannonical = 0;
64 for (unsigned i = 0; i < IndVars.size(); ++i) {
Chris Lattner5e761402002-09-10 05:24:05 +000065 if (IndVars[i].InductionType == InductionVariable::Cannonical &&
66 !isa<PointerType>(IndVars[i].Phi->getType()))
Chris Lattner6148c022001-12-03 17:28:42 +000067 Cannonical = &IndVars[i];
68 if (IndVars[i].InductionType != InductionVariable::Unknown)
69 FoundIndVars = true;
70 }
71
72 // No induction variables, bail early... don't add a cannonnical indvar
73 if (!FoundIndVars) return Changed;
74
75 // Okay, we want to convert other induction variables to use a cannonical
76 // indvar. If we don't have one, add one now...
77 if (!Cannonical) {
Chris Lattner394437f2001-12-04 04:32:29 +000078 // Create the PHI node for the new induction variable
79 PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar");
Chris Lattner6148c022001-12-03 17:28:42 +000080
Chris Lattner394437f2001-12-04 04:32:29 +000081 // Insert the phi node at the end of the other phi nodes...
Chris Lattner7e708292002-06-25 16:13:24 +000082 AfterPHIIt = ++Header->getInstList().insert(AfterPHIIt, PN);
Chris Lattner394437f2001-12-04 04:32:29 +000083
84 // Create the increment instruction to add one to the counter...
85 Instruction *Add = BinaryOperator::create(Instruction::Add, PN,
86 ConstantUInt::get(Type::UIntTy,1),
87 "add1-indvar");
88
89 // Insert the add instruction after all of the PHI nodes...
Chris Lattner7e708292002-06-25 16:13:24 +000090 Header->getInstList().insert(AfterPHIIt, Add);
Chris Lattner394437f2001-12-04 04:32:29 +000091
92 // Figure out which block is incoming and which is the backedge for the loop
93 BasicBlock *Incoming, *BackEdgeBlock;
Chris Lattner455889a2002-02-12 22:39:50 +000094 pred_iterator PI = pred_begin(Header);
95 assert(PI != pred_end(Header) && "Loop headers should have 2 preds!");
Chris Lattner394437f2001-12-04 04:32:29 +000096 if (Loop->contains(*PI)) { // First pred is back edge...
97 BackEdgeBlock = *PI++;
98 Incoming = *PI++;
99 } else {
100 Incoming = *PI++;
101 BackEdgeBlock = *PI++;
102 }
Chris Lattner455889a2002-02-12 22:39:50 +0000103 assert(PI == pred_end(Header) && "Loop headers should have 2 preds!");
Chris Lattner394437f2001-12-04 04:32:29 +0000104
105 // Add incoming values for the PHI node...
Chris Lattner1a18b7c2002-04-27 02:25:14 +0000106 PN->addIncoming(Constant::getNullValue(Type::UIntTy), Incoming);
Chris Lattner394437f2001-12-04 04:32:29 +0000107 PN->addIncoming(Add, BackEdgeBlock);
108
109 // Analyze the new induction variable...
110 IndVars.push_back(InductionVariable(PN, Loops));
111 assert(IndVars.back().InductionType == InductionVariable::Cannonical &&
112 "Just inserted cannonical indvar that is not cannonical!");
113 Cannonical = &IndVars.back();
Chris Lattner3dec1f22002-05-10 15:38:35 +0000114 ++NumInserted;
Chris Lattner4753bf22001-12-05 19:41:33 +0000115 Changed = true;
Chris Lattner394437f2001-12-04 04:32:29 +0000116 }
117
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000118 DEBUG(std::cerr << "Induction variables:\n");
Chris Lattner394437f2001-12-04 04:32:29 +0000119
120 // Get the current loop iteration count, which is always the value of the
121 // cannonical phi node...
122 //
123 PHINode *IterCount = Cannonical->Phi;
124
125 // Loop through and replace all of the auxillary induction variables with
126 // references to the primary induction variable...
127 //
Chris Lattner394437f2001-12-04 04:32:29 +0000128 for (unsigned i = 0; i < IndVars.size(); ++i) {
129 InductionVariable *IV = &IndVars[i];
Chris Lattnerf016ea42002-05-22 17:17:27 +0000130
Chris Lattnera59cbb22002-07-27 01:12:17 +0000131 DEBUG(IV->print(std::cerr));
Chris Lattnerf016ea42002-05-22 17:17:27 +0000132
Chris Lattner5e761402002-09-10 05:24:05 +0000133 // Don't do math with pointers...
134 const Type *IVTy = IV->Phi->getType();
135 if (isa<PointerType>(IVTy)) IVTy = Type::ULongTy;
136
Chris Lattner3bf915f2001-12-04 08:13:06 +0000137 // Don't modify the cannonical indvar or unrecognized indvars...
138 if (IV != Cannonical && IV->InductionType != InductionVariable::Unknown) {
Chris Lattner394437f2001-12-04 04:32:29 +0000139 Instruction *Val = IterCount;
140 if (!isa<ConstantInt>(IV->Step) || // If the step != 1
141 !cast<ConstantInt>(IV->Step)->equalsInt(1)) {
Chris Lattner394437f2001-12-04 04:32:29 +0000142
143 // If the types are not compatible, insert a cast now...
Chris Lattner5e761402002-09-10 05:24:05 +0000144 if (Val->getType() != IVTy)
145 Val = InsertCast(Val, IVTy, AfterPHIIt);
146 if (IV->Step->getType() != IVTy)
147 IV->Step = InsertCast(IV->Step, IVTy, AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +0000148
Chris Lattner5e761402002-09-10 05:24:05 +0000149 Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step,
150 IV->Phi->getName()+"-scale");
Chris Lattner394437f2001-12-04 04:32:29 +0000151 // Insert the phi node at the end of the other phi nodes...
Chris Lattner7e708292002-06-25 16:13:24 +0000152 Header->getInstList().insert(AfterPHIIt, Val);
Chris Lattner394437f2001-12-04 04:32:29 +0000153 }
154
Chris Lattner5e761402002-09-10 05:24:05 +0000155 // If the start != 0
156 if (IV->Start != Constant::getNullValue(IV->Start->getType())) {
Chris Lattner394437f2001-12-04 04:32:29 +0000157 // If the types are not compatible, insert a cast now...
Chris Lattner5e761402002-09-10 05:24:05 +0000158 if (Val->getType() != IVTy)
159 Val = InsertCast(Val, IVTy, AfterPHIIt);
160 if (IV->Start->getType() != IVTy)
161 IV->Start = InsertCast(IV->Start, IVTy, AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +0000162
Chris Lattner5e761402002-09-10 05:24:05 +0000163 Val = BinaryOperator::create(Instruction::Add, Val, IV->Start,
164 IV->Phi->getName()+"-offset");
165
Chris Lattner394437f2001-12-04 04:32:29 +0000166 // Insert the phi node at the end of the other phi nodes...
Chris Lattner7e708292002-06-25 16:13:24 +0000167 Header->getInstList().insert(AfterPHIIt, Val);
Chris Lattner394437f2001-12-04 04:32:29 +0000168 }
169
170 // If the PHI node has a different type than val is, insert a cast now...
171 if (Val->getType() != IV->Phi->getType())
Chris Lattner7e708292002-06-25 16:13:24 +0000172 Val = InsertCast(Val, IV->Phi->getType(), AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +0000173
174 // Replace all uses of the old PHI node with the new computed value...
175 IV->Phi->replaceAllUsesWith(Val);
176
177 // Move the PHI name to it's new equivalent value...
Chris Lattner697954c2002-01-20 22:54:45 +0000178 std::string OldName = IV->Phi->getName();
Chris Lattner394437f2001-12-04 04:32:29 +0000179 IV->Phi->setName("");
180 Val->setName(OldName);
181
182 // Delete the old, now unused, phi node...
Chris Lattner7e708292002-06-25 16:13:24 +0000183 Header->getInstList().erase(IV->Phi);
Chris Lattner4753bf22001-12-05 19:41:33 +0000184 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000185 ++NumRemoved;
Chris Lattner394437f2001-12-04 04:32:29 +0000186 }
Chris Lattner6148c022001-12-03 17:28:42 +0000187 }
188
189 return Changed;
190}
191
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000192namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000193 struct InductionVariableSimplify : public FunctionPass {
Chris Lattner7e708292002-06-25 16:13:24 +0000194 virtual bool runOnFunction(Function &) {
Chris Lattner3dec1f22002-05-10 15:38:35 +0000195 LoopInfo &LI = getAnalysis<LoopInfo>();
196
197 // Induction Variables live in the header nodes of loops
198 return reduce_apply_bool(LI.getTopLevelLoops().begin(),
199 LI.getTopLevelLoops().end(),
200 std::bind1st(std::ptr_fun(TransformLoop), &LI));
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000201 }
202
Chris Lattnerf57b8452002-04-27 06:56:12 +0000203 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5f0eb8d2002-08-08 19:01:30 +0000204 AU.addRequired<LoopInfo>();
Chris Lattner97e52e42002-04-28 21:27:06 +0000205 AU.preservesCFG();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000206 }
207 };
Chris Lattnera6275cc2002-07-26 21:12:46 +0000208 RegisterOpt<InductionVariableSimplify> X("indvars",
Chris Lattnerf6293092002-07-23 18:06:35 +0000209 "Cannonicalize Induction Variables");
Chris Lattner793c6b82002-01-31 00:45:11 +0000210}
211
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000212Pass *createIndVarSimplifyPass() {
213 return new InductionVariableSimplify();
Chris Lattner793c6b82002-01-31 00:45:11 +0000214}