blob: c96eaf0f52b6e7352e4355a350d7aa0067590c0b [file] [log] [blame]
Chris Lattner6148c022001-12-03 17:28:42 +00001//===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===//
2//
Chris Lattnerc01ccfd2003-09-10 05:10:34 +00003// Guarantees that all loops with identifiable, linear, induction variables will
Chris Lattner3adf51d2003-09-10 05:24:46 +00004// be transformed to have a single, canonical, induction variable. After this
Chris Lattnerc01ccfd2003-09-10 05:10:34 +00005// pass runs, it guarantees the the first PHI node of the header block in the
Chris Lattner3adf51d2003-09-10 05:24:46 +00006// loop is the canonical induction variable if there is one.
Chris Lattner6148c022001-12-03 17:28:42 +00007//
8//===----------------------------------------------------------------------===//
9
Chris Lattner022103b2002-05-07 20:03:00 +000010#include "llvm/Transforms/Scalar.h"
Chris Lattner6148c022001-12-03 17:28:42 +000011#include "llvm/Analysis/InductionVariable.h"
12#include "llvm/Analysis/LoopInfo.h"
Chris Lattner6148c022001-12-03 17:28:42 +000013#include "llvm/iPHINode.h"
Chris Lattner394437f2001-12-04 04:32:29 +000014#include "llvm/iOther.h"
15#include "llvm/Type.h"
Chris Lattner31bcdb82002-04-28 19:55:58 +000016#include "llvm/Constants.h"
Chris Lattner455889a2002-02-12 22:39:50 +000017#include "llvm/Support/CFG.h"
Chris Lattner6806f562003-08-01 22:15:03 +000018#include "Support/Debug.h"
Chris Lattnera92f6962002-10-01 22:38:41 +000019#include "Support/Statistic.h"
Chris Lattner6806f562003-08-01 22:15:03 +000020#include "Support/STLExtras.h"
Chris Lattner3dec1f22002-05-10 15:38:35 +000021
Chris Lattner5e761402002-09-10 05:24:05 +000022namespace {
Chris Lattnera92f6962002-10-01 22:38:41 +000023 Statistic<> NumRemoved ("indvars", "Number of aux indvars removed");
Chris Lattner3adf51d2003-09-10 05:24:46 +000024 Statistic<> NumInserted("indvars", "Number of canonical indvars added");
Chris Lattner5e761402002-09-10 05:24:05 +000025}
Chris Lattner394437f2001-12-04 04:32:29 +000026
27// InsertCast - Cast Val to Ty, setting a useful name on the cast if Val has a
28// name...
29//
Chris Lattner5e761402002-09-10 05:24:05 +000030static Instruction *InsertCast(Value *Val, const Type *Ty,
Chris Lattner2a7c23e2002-09-10 17:04:02 +000031 Instruction *InsertBefore) {
32 return new CastInst(Val, Ty, Val->getName()+"-casted", InsertBefore);
Chris Lattner394437f2001-12-04 04:32:29 +000033}
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 //
Chris Lattner3adf51d2003-09-10 05:24:46 +000043 BasicBlock *Header = Loop->getHeader();
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();
Chris Lattnere408e252003-04-23 16:37:45 +000051 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
Chris Lattner3adf51d2003-09-10 05:24:46 +000058 // Loop over the induction variables, looking for a canonical induction
Chris Lattner6148c022001-12-03 17:28:42 +000059 // variable, and checking to make sure they are not all unknown induction
60 // variables.
61 //
62 bool FoundIndVars = false;
Chris Lattner3adf51d2003-09-10 05:24:46 +000063 InductionVariable *Canonical = 0;
Chris Lattner6148c022001-12-03 17:28:42 +000064 for (unsigned i = 0; i < IndVars.size(); ++i) {
Chris Lattner3adf51d2003-09-10 05:24:46 +000065 if (IndVars[i].InductionType == InductionVariable::Canonical &&
Chris Lattner5e761402002-09-10 05:24:05 +000066 !isa<PointerType>(IndVars[i].Phi->getType()))
Chris Lattner3adf51d2003-09-10 05:24:46 +000067 Canonical = &IndVars[i];
Chris Lattner6148c022001-12-03 17:28:42 +000068 if (IndVars[i].InductionType != InductionVariable::Unknown)
69 FoundIndVars = true;
70 }
71
Chris Lattner3adf51d2003-09-10 05:24:46 +000072 // No induction variables, bail early... don't add a canonical indvar
Chris Lattner6148c022001-12-03 17:28:42 +000073 if (!FoundIndVars) return Changed;
74
Chris Lattner3adf51d2003-09-10 05:24:46 +000075 // Okay, we want to convert other induction variables to use a canonical
Chris Lattner6148c022001-12-03 17:28:42 +000076 // indvar. If we don't have one, add one now...
Chris Lattner3adf51d2003-09-10 05:24:46 +000077 if (!Canonical) {
Chris Lattner2a7c23e2002-09-10 17:04:02 +000078 // Create the PHI node for the new induction variable, and insert the phi
79 // node at the end of the other phi nodes...
80 PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar", AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +000081
82 // Create the increment instruction to add one to the counter...
83 Instruction *Add = BinaryOperator::create(Instruction::Add, PN,
84 ConstantUInt::get(Type::UIntTy,1),
Chris Lattner2a7c23e2002-09-10 17:04:02 +000085 "add1-indvar", AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +000086
87 // Figure out which block is incoming and which is the backedge for the loop
88 BasicBlock *Incoming, *BackEdgeBlock;
Chris Lattner455889a2002-02-12 22:39:50 +000089 pred_iterator PI = pred_begin(Header);
90 assert(PI != pred_end(Header) && "Loop headers should have 2 preds!");
Chris Lattner394437f2001-12-04 04:32:29 +000091 if (Loop->contains(*PI)) { // First pred is back edge...
92 BackEdgeBlock = *PI++;
93 Incoming = *PI++;
94 } else {
95 Incoming = *PI++;
96 BackEdgeBlock = *PI++;
97 }
Chris Lattner455889a2002-02-12 22:39:50 +000098 assert(PI == pred_end(Header) && "Loop headers should have 2 preds!");
Chris Lattner394437f2001-12-04 04:32:29 +000099
100 // Add incoming values for the PHI node...
Chris Lattner1a18b7c2002-04-27 02:25:14 +0000101 PN->addIncoming(Constant::getNullValue(Type::UIntTy), Incoming);
Chris Lattner394437f2001-12-04 04:32:29 +0000102 PN->addIncoming(Add, BackEdgeBlock);
103
104 // Analyze the new induction variable...
105 IndVars.push_back(InductionVariable(PN, Loops));
Chris Lattner3adf51d2003-09-10 05:24:46 +0000106 assert(IndVars.back().InductionType == InductionVariable::Canonical &&
107 "Just inserted canonical indvar that is not canonical!");
108 Canonical = &IndVars.back();
Chris Lattner3dec1f22002-05-10 15:38:35 +0000109 ++NumInserted;
Chris Lattner4753bf22001-12-05 19:41:33 +0000110 Changed = true;
Chris Lattner394437f2001-12-04 04:32:29 +0000111 }
112
Anand Shukla5ba99bd2002-06-25 21:07:58 +0000113 DEBUG(std::cerr << "Induction variables:\n");
Chris Lattner394437f2001-12-04 04:32:29 +0000114
115 // Get the current loop iteration count, which is always the value of the
Chris Lattner3adf51d2003-09-10 05:24:46 +0000116 // canonical phi node...
Chris Lattner394437f2001-12-04 04:32:29 +0000117 //
Chris Lattner3adf51d2003-09-10 05:24:46 +0000118 PHINode *IterCount = Canonical->Phi;
Chris Lattner394437f2001-12-04 04:32:29 +0000119
Chris Lattner3adf51d2003-09-10 05:24:46 +0000120 // Loop through and replace all of the auxiliary induction variables with
121 // references to the canonical induction variable...
Chris Lattner394437f2001-12-04 04:32:29 +0000122 //
Chris Lattner394437f2001-12-04 04:32:29 +0000123 for (unsigned i = 0; i < IndVars.size(); ++i) {
124 InductionVariable *IV = &IndVars[i];
Chris Lattnerf016ea42002-05-22 17:17:27 +0000125
Chris Lattnera59cbb22002-07-27 01:12:17 +0000126 DEBUG(IV->print(std::cerr));
Chris Lattnerf016ea42002-05-22 17:17:27 +0000127
Chris Lattner5e761402002-09-10 05:24:05 +0000128 // Don't do math with pointers...
129 const Type *IVTy = IV->Phi->getType();
130 if (isa<PointerType>(IVTy)) IVTy = Type::ULongTy;
131
Chris Lattner3adf51d2003-09-10 05:24:46 +0000132 // Don't modify the canonical indvar or unrecognized indvars...
133 if (IV != Canonical && IV->InductionType != InductionVariable::Unknown) {
Chris Lattner394437f2001-12-04 04:32:29 +0000134 Instruction *Val = IterCount;
135 if (!isa<ConstantInt>(IV->Step) || // If the step != 1
136 !cast<ConstantInt>(IV->Step)->equalsInt(1)) {
Chris Lattner394437f2001-12-04 04:32:29 +0000137
138 // If the types are not compatible, insert a cast now...
Chris Lattner5e761402002-09-10 05:24:05 +0000139 if (Val->getType() != IVTy)
140 Val = InsertCast(Val, IVTy, AfterPHIIt);
141 if (IV->Step->getType() != IVTy)
142 IV->Step = InsertCast(IV->Step, IVTy, AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +0000143
Chris Lattner5e761402002-09-10 05:24:05 +0000144 Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000145 IV->Phi->getName()+"-scale", AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +0000146 }
147
Chris Lattner5e761402002-09-10 05:24:05 +0000148 // If the start != 0
149 if (IV->Start != Constant::getNullValue(IV->Start->getType())) {
Chris Lattner394437f2001-12-04 04:32:29 +0000150 // If the types are not compatible, insert a cast now...
Chris Lattner5e761402002-09-10 05:24:05 +0000151 if (Val->getType() != IVTy)
152 Val = InsertCast(Val, IVTy, AfterPHIIt);
153 if (IV->Start->getType() != IVTy)
154 IV->Start = InsertCast(IV->Start, IVTy, AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +0000155
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000156 // Insert the instruction after the phi nodes...
Chris Lattner5e761402002-09-10 05:24:05 +0000157 Val = BinaryOperator::create(Instruction::Add, Val, IV->Start,
Chris Lattner2a7c23e2002-09-10 17:04:02 +0000158 IV->Phi->getName()+"-offset", AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +0000159 }
160
161 // If the PHI node has a different type than val is, insert a cast now...
162 if (Val->getType() != IV->Phi->getType())
Chris Lattner7e708292002-06-25 16:13:24 +0000163 Val = InsertCast(Val, IV->Phi->getType(), AfterPHIIt);
Chris Lattner394437f2001-12-04 04:32:29 +0000164
165 // Replace all uses of the old PHI node with the new computed value...
166 IV->Phi->replaceAllUsesWith(Val);
167
168 // Move the PHI name to it's new equivalent value...
Chris Lattner697954c2002-01-20 22:54:45 +0000169 std::string OldName = IV->Phi->getName();
Chris Lattner394437f2001-12-04 04:32:29 +0000170 IV->Phi->setName("");
171 Val->setName(OldName);
172
173 // Delete the old, now unused, phi node...
Chris Lattner7e708292002-06-25 16:13:24 +0000174 Header->getInstList().erase(IV->Phi);
Chris Lattner4753bf22001-12-05 19:41:33 +0000175 Changed = true;
Chris Lattner3dec1f22002-05-10 15:38:35 +0000176 ++NumRemoved;
Chris Lattner394437f2001-12-04 04:32:29 +0000177 }
Chris Lattner6148c022001-12-03 17:28:42 +0000178 }
179
180 return Changed;
181}
182
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000183namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +0000184 struct InductionVariableSimplify : public FunctionPass {
Chris Lattner7e708292002-06-25 16:13:24 +0000185 virtual bool runOnFunction(Function &) {
Chris Lattner3dec1f22002-05-10 15:38:35 +0000186 LoopInfo &LI = getAnalysis<LoopInfo>();
187
188 // Induction Variables live in the header nodes of loops
189 return reduce_apply_bool(LI.getTopLevelLoops().begin(),
190 LI.getTopLevelLoops().end(),
191 std::bind1st(std::ptr_fun(TransformLoop), &LI));
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000192 }
193
Chris Lattnerf57b8452002-04-27 06:56:12 +0000194 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Chris Lattner5f0eb8d2002-08-08 19:01:30 +0000195 AU.addRequired<LoopInfo>();
Chris Lattnercb2610e2002-10-21 20:00:28 +0000196 AU.setPreservesCFG();
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000197 }
198 };
Chris Lattnera6275cc2002-07-26 21:12:46 +0000199 RegisterOpt<InductionVariableSimplify> X("indvars",
Chris Lattner3adf51d2003-09-10 05:24:46 +0000200 "Canonicalize Induction Variables");
Chris Lattner793c6b82002-01-31 00:45:11 +0000201}
202
Chris Lattnerbd0ef772002-02-26 21:46:54 +0000203Pass *createIndVarSimplifyPass() {
204 return new InductionVariableSimplify();
Chris Lattner793c6b82002-01-31 00:45:11 +0000205}