blob: 993e533abc726f33bef660bd106fce2e76a45a6f [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 Lattnera92f6962002-10-01 22:38:41 +000017#include "Support/Statistic.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000018
Chris Lattner5e761402002-09-10 05:24:05 +000019namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000020 Statistic<> NumRemoved ("indvars", "Number of aux indvars removed");
21 Statistic<> NumInserted("indvars", "Number of cannonical indvars added");
Chris Lattner5e761402002-09-10 05:24:05 +000022}
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 Lattner2a7c23e2002-09-10 17:04:02 +000028 Instruction *InsertBefore) {
29 return new CastInst(Val, Ty, Val->getName()+"-casted", InsertBefore);
Chris Lattner394437f2001-12-04 04:32:29 +000030}
31
Chris Lattner1b7f7dc2002-04-28 16:21:30 +000032static bool TransformLoop(LoopInfo *Loops, Loop *Loop) {
Chris Lattner6148c022001-12-03 17:28:42 +000033 // Transform all subloops before this loop...
34 bool Changed = reduce_apply_bool(Loop->getSubLoops().begin(),
35 Loop->getSubLoops().end(),
Chris Lattner697954c2002-01-20 22:54:45 +000036 std::bind1st(std::ptr_fun(TransformLoop), Loops));
Chris Lattner6148c022001-12-03 17:28:42 +000037 // Get the header node for this loop. All of the phi nodes that could be
38 // induction variables must live in this basic block.
Chris Lattner3dec1f22002-05-10 15:38:35 +000039 //
40 BasicBlock *Header = Loop->getBlocks().front();
Chris Lattner6148c022001-12-03 17:28:42 +000041
42 // Loop over all of the PHI nodes in the basic block, calculating the
43 // induction variables that they represent... stuffing the induction variable
44 // info into a vector...
45 //
Chris Lattner697954c2002-01-20 22:54:45 +000046 std::vector<InductionVariable> IndVars; // Induction variables for block
Chris Lattner7e708292002-06-25 16:13:24 +000047 BasicBlock::iterator AfterPHIIt = Header->begin();
Chris Lattnere408e252003-04-23 16:37:45 +000048 for (; PHINode *PN = dyn_cast<PHINode>(AfterPHIIt); ++AfterPHIIt)
Chris Lattner6148c022001-12-03 17:28:42 +000049 IndVars.push_back(InductionVariable(PN, Loops));
Chris Lattner7e708292002-06-25 16:13:24 +000050 // AfterPHIIt now points to first nonphi instruction...
Chris Lattner6148c022001-12-03 17:28:42 +000051
Chris Lattner394437f2001-12-04 04:32:29 +000052 // If there are no phi nodes in this basic block, there can't be indvars...
Chris Lattner6148c022001-12-03 17:28:42 +000053 if (IndVars.empty()) return Changed;
54
55 // Loop over the induction variables, looking for a cannonical induction
56 // variable, and checking to make sure they are not all unknown induction
57 // variables.
58 //
59 bool FoundIndVars = false;
60 InductionVariable *Cannonical = 0;
61 for (unsigned i = 0; i < IndVars.size(); ++i) {
Chris Lattner5e761402002-09-10 05:24:05 +000062 if (IndVars[i].InductionType == InductionVariable::Cannonical &&
63 !isa<PointerType>(IndVars[i].Phi->getType()))
Chris Lattner6148c022001-12-03 17:28:42 +000064 Cannonical = &IndVars[i];
65 if (IndVars[i].InductionType != InductionVariable::Unknown)
66 FoundIndVars = true;
67 }
68
69 // No induction variables, bail early... don't add a cannonnical indvar
70 if (!FoundIndVars) return Changed;
71
72 // Okay, we want to convert other induction variables to use a cannonical
73 // indvar. If we don't have one, add one now...
74 if (!Cannonical) {
Chris Lattner2a7c23e2002-09-10 17:04:02 +000075 // Create the PHI node for the new induction variable, and insert the phi
76 // node at the end of the other phi nodes...
77 PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar", AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +000078
79 // Create the increment instruction to add one to the counter...
80 Instruction *Add = BinaryOperator::create(Instruction::Add, PN,
81 ConstantUInt::get(Type::UIntTy,1),
Chris Lattner2a7c23e2002-09-10 17:04:02 +000082 "add1-indvar", AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +000083
84 // Figure out which block is incoming and which is the backedge for the loop
85 BasicBlock *Incoming, *BackEdgeBlock;
Chris Lattner455889a2002-02-12 22:39:50 +000086 pred_iterator PI = pred_begin(Header);
87 assert(PI != pred_end(Header) && "Loop headers should have 2 preds!");
Chris Lattner394437f2001-12-04 04:32:29 +000088 if (Loop->contains(*PI)) { // First pred is back edge...
89 BackEdgeBlock = *PI++;
90 Incoming = *PI++;
91 } else {
92 Incoming = *PI++;
93 BackEdgeBlock = *PI++;
94 }
Chris Lattner455889a2002-02-12 22:39:50 +000095 assert(PI == pred_end(Header) && "Loop headers should have 2 preds!");
Chris Lattner394437f2001-12-04 04:32:29 +000096
97 // Add incoming values for the PHI node...
Chris Lattner1a18b7c2002-04-27 02:25:14 +000098 PN->addIncoming(Constant::getNullValue(Type::UIntTy), Incoming);
Chris Lattner394437f2001-12-04 04:32:29 +000099 PN->addIncoming(Add, BackEdgeBlock);
100
101 // Analyze the new induction variable...
102 IndVars.push_back(InductionVariable(PN, Loops));
103 assert(IndVars.back().InductionType == InductionVariable::Cannonical &&
104 "Just inserted cannonical indvar that is not cannonical!");
105 Cannonical = &IndVars.back();
Chris Lattner3dec1f22002-05-10 15:38:35 +0000106 ++NumInserted;
Chris Lattner4753bf22001-12-05 19:41:33 +0000107 Changed = true;
Chris Lattner394437f2001-12-04 04:32:29 +0000108 }
109
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000110 DEBUG(std::cerr << "Induction variables:\n");
Chris Lattner394437f2001-12-04 04:32:29 +0000111
112 // Get the current loop iteration count, which is always the value of the
113 // cannonical phi node...
114 //
115 PHINode *IterCount = Cannonical->Phi;
116
117 // Loop through and replace all of the auxillary induction variables with
118 // references to the primary induction variable...
119 //
Chris Lattner394437f2001-12-04 04:32:29 +0000120 for (unsigned i = 0; i < IndVars.size(); ++i) {
121 InductionVariable *IV = &IndVars[i];
Chris Lattnerf016ea42002-05-22 17:17:27 +0000122
Chris Lattnera59cbb22002-07-27 01:12:17 +0000123 DEBUG(IV->print(std::cerr));
Chris Lattnerf016ea42002-05-22 17:17:27 +0000124
Chris Lattner5e761402002-09-10 05:24:05 +0000125 // Don't do math with pointers...
126 const Type *IVTy = IV->Phi->getType();
127 if (isa<PointerType>(IVTy)) IVTy = Type::ULongTy;
128
Chris Lattner3bf915f2001-12-04 08:13:06 +0000129 // Don't modify the cannonical indvar or unrecognized indvars...
130 if (IV != Cannonical && IV->InductionType != InductionVariable::Unknown) {
Chris Lattner394437f2001-12-04 04:32:29 +0000131 Instruction *Val = IterCount;
132 if (!isa<ConstantInt>(IV->Step) || // If the step != 1
133 !cast<ConstantInt>(IV->Step)->equalsInt(1)) {
Chris Lattner394437f2001-12-04 04:32:29 +0000134
135 // If the types are not compatible, insert a cast now...
Chris Lattner5e761402002-09-10 05:24:05 +0000136 if (Val->getType() != IVTy)
137 Val = InsertCast(Val, IVTy, AfterPHIIt);
138 if (IV->Step->getType() != IVTy)
139 IV->Step = InsertCast(IV->Step, IVTy, AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +0000140
Chris Lattner5e761402002-09-10 05:24:05 +0000141 Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000142 IV->Phi->getName()+"-scale", AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +0000143 }
144
Chris Lattner5e761402002-09-10 05:24:05 +0000145 // If the start != 0
146 if (IV->Start != Constant::getNullValue(IV->Start->getType())) {
Chris Lattner394437f2001-12-04 04:32:29 +0000147 // If the types are not compatible, insert a cast now...
Chris Lattner5e761402002-09-10 05:24:05 +0000148 if (Val->getType() != IVTy)
149 Val = InsertCast(Val, IVTy, AfterPHIIt);
150 if (IV->Start->getType() != IVTy)
151 IV->Start = InsertCast(IV->Start, IVTy, AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +0000152
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000153 // Insert the instruction after the phi nodes...
Chris Lattner5e761402002-09-10 05:24:05 +0000154 Val = BinaryOperator::create(Instruction::Add, Val, IV->Start,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000155 IV->Phi->getName()+"-offset", AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +0000156 }
157
158 // If the PHI node has a different type than val is, insert a cast now...
159 if (Val->getType() != IV->Phi->getType())
Chris Lattner7e708292002-06-25 16:13:24 +0000160 Val = InsertCast(Val, IV->Phi->getType(), AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +0000161
162 // Replace all uses of the old PHI node with the new computed value...
163 IV->Phi->replaceAllUsesWith(Val);
164
165 // Move the PHI name to it's new equivalent value...
Chris Lattner697954c2002-01-20 22:54:45 +0000166 std::string OldName = IV->Phi->getName();
Chris Lattner394437f2001-12-04 04:32:29 +0000167 IV->Phi->setName("");
168 Val->setName(OldName);
169
170 // Delete the old, now unused, phi node...
Chris Lattner7e708292002-06-25 16:13:24 +0000171 Header->getInstList().erase(IV->Phi);
Chris Lattner4753bf22001-12-05 19:41:33 +0000172 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000173 ++NumRemoved;
Chris Lattner394437f2001-12-04 04:32:29 +0000174 }
Chris Lattner6148c022001-12-03 17:28:42 +0000175 }
176
177 return Changed;
178}
179
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000180namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000181 struct InductionVariableSimplify : public FunctionPass {
Chris Lattner7e708292002-06-25 16:13:24 +0000182 virtual bool runOnFunction(Function &) {
Chris Lattner3dec1f22002-05-10 15:38:35 +0000183 LoopInfo &LI = getAnalysis<LoopInfo>();
184
185 // Induction Variables live in the header nodes of loops
186 return reduce_apply_bool(LI.getTopLevelLoops().begin(),
187 LI.getTopLevelLoops().end(),
188 std::bind1st(std::ptr_fun(TransformLoop), &LI));
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000189 }
190
Chris Lattnerf57b8452002-04-27 06:56:12 +0000191 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5f0eb8d2002-08-08 19:01:30 +0000192 AU.addRequired<LoopInfo>();
Chris Lattnercb2610e2002-10-21 20:00:28 +0000193 AU.setPreservesCFG();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000194 }
195 };
Chris Lattnera6275cc2002-07-26 21:12:46 +0000196 RegisterOpt<InductionVariableSimplify> X("indvars",
Chris Lattnerf6293092002-07-23 18:06:35 +0000197 "Cannonicalize Induction Variables");
Chris Lattner793c6b82002-01-31 00:45:11 +0000198}
199
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000200Pass *createIndVarSimplifyPass() {
201 return new InductionVariableSimplify();
Chris Lattner793c6b82002-01-31 00:45:11 +0000202}