Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 1 | //===- 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 Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 8 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 9 | #include "llvm/Analysis/InductionVariable.h" |
| 10 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 11 | #include "llvm/Analysis/Writer.h" |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 12 | #include "llvm/iPHINode.h" |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 13 | #include "llvm/iOther.h" |
| 14 | #include "llvm/Type.h" |
Chris Lattner | 31bcdb8 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CFG.h" |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 17 | #include "Support/STLExtras.h" |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 18 | #include "Support/StatisticReporter.h" |
| 19 | |
| 20 | static Statistic<> NumRemoved ("indvars\t\t- Number of aux indvars removed"); |
| 21 | static Statistic<> NumInserted("indvars\t\t- Number of cannonical indvars added"); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 23 | |
| 24 | // InsertCast - Cast Val to Ty, setting a useful name on the cast if Val has a |
| 25 | // name... |
| 26 | // |
| 27 | static Instruction *InsertCast(Instruction *Val, const Type *Ty, |
| 28 | BasicBlock::iterator It) { |
| 29 | Instruction *Cast = new CastInst(Val, Ty); |
| 30 | if (Val->hasName()) Cast->setName(Val->getName()+"-casted"); |
| 31 | Val->getParent()->getInstList().insert(It, Cast); |
| 32 | return Cast; |
| 33 | } |
| 34 | |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 35 | static bool TransformLoop(LoopInfo *Loops, Loop *Loop) { |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 36 | // Transform all subloops before this loop... |
| 37 | bool Changed = reduce_apply_bool(Loop->getSubLoops().begin(), |
| 38 | Loop->getSubLoops().end(), |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 39 | std::bind1st(std::ptr_fun(TransformLoop), Loops)); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 40 | // 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 Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 42 | // |
| 43 | BasicBlock *Header = Loop->getBlocks().front(); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 44 | |
| 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 Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 49 | std::vector<InductionVariable> IndVars; // Induction variables for block |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 50 | BasicBlock::iterator AfterPHIIt = Header->begin(); |
| 51 | for (; PHINode *PN = dyn_cast<PHINode>(&*AfterPHIIt); ++AfterPHIIt) |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 52 | IndVars.push_back(InductionVariable(PN, Loops)); |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 53 | // AfterPHIIt now points to first nonphi instruction... |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 55 | // If there are no phi nodes in this basic block, there can't be indvars... |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 56 | 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) { |
| 65 | if (IndVars[i].InductionType == InductionVariable::Cannonical) |
| 66 | Cannonical = &IndVars[i]; |
| 67 | if (IndVars[i].InductionType != InductionVariable::Unknown) |
| 68 | FoundIndVars = true; |
| 69 | } |
| 70 | |
| 71 | // No induction variables, bail early... don't add a cannonnical indvar |
| 72 | if (!FoundIndVars) return Changed; |
| 73 | |
| 74 | // Okay, we want to convert other induction variables to use a cannonical |
| 75 | // indvar. If we don't have one, add one now... |
| 76 | if (!Cannonical) { |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 77 | // Create the PHI node for the new induction variable |
| 78 | PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar"); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 80 | // Insert the phi node at the end of the other phi nodes... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 81 | AfterPHIIt = ++Header->getInstList().insert(AfterPHIIt, PN); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 82 | |
| 83 | // Create the increment instruction to add one to the counter... |
| 84 | Instruction *Add = BinaryOperator::create(Instruction::Add, PN, |
| 85 | ConstantUInt::get(Type::UIntTy,1), |
| 86 | "add1-indvar"); |
| 87 | |
| 88 | // Insert the add instruction after all of the PHI nodes... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 89 | Header->getInstList().insert(AfterPHIIt, Add); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 90 | |
| 91 | // Figure out which block is incoming and which is the backedge for the loop |
| 92 | BasicBlock *Incoming, *BackEdgeBlock; |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 93 | pred_iterator PI = pred_begin(Header); |
| 94 | assert(PI != pred_end(Header) && "Loop headers should have 2 preds!"); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 95 | if (Loop->contains(*PI)) { // First pred is back edge... |
| 96 | BackEdgeBlock = *PI++; |
| 97 | Incoming = *PI++; |
| 98 | } else { |
| 99 | Incoming = *PI++; |
| 100 | BackEdgeBlock = *PI++; |
| 101 | } |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 102 | assert(PI == pred_end(Header) && "Loop headers should have 2 preds!"); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 103 | |
| 104 | // Add incoming values for the PHI node... |
Chris Lattner | 1a18b7c | 2002-04-27 02:25:14 +0000 | [diff] [blame] | 105 | PN->addIncoming(Constant::getNullValue(Type::UIntTy), Incoming); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 106 | PN->addIncoming(Add, BackEdgeBlock); |
| 107 | |
| 108 | // Analyze the new induction variable... |
| 109 | IndVars.push_back(InductionVariable(PN, Loops)); |
| 110 | assert(IndVars.back().InductionType == InductionVariable::Cannonical && |
| 111 | "Just inserted cannonical indvar that is not cannonical!"); |
| 112 | Cannonical = &IndVars.back(); |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 113 | ++NumInserted; |
Chris Lattner | 4753bf2 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 114 | Changed = true; |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Anand Shukla | 5ba99bd | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 117 | DEBUG(std::cerr << "Induction variables:\n"); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 118 | |
| 119 | // Get the current loop iteration count, which is always the value of the |
| 120 | // cannonical phi node... |
| 121 | // |
| 122 | PHINode *IterCount = Cannonical->Phi; |
| 123 | |
| 124 | // Loop through and replace all of the auxillary induction variables with |
| 125 | // references to the primary induction variable... |
| 126 | // |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 127 | for (unsigned i = 0; i < IndVars.size(); ++i) { |
| 128 | InductionVariable *IV = &IndVars[i]; |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 129 | |
Anand Shukla | 5ba99bd | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 130 | DEBUG(std::cerr << IV); |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 3bf915f | 2001-12-04 08:13:06 +0000 | [diff] [blame] | 132 | // Don't modify the cannonical indvar or unrecognized indvars... |
| 133 | if (IV != Cannonical && IV->InductionType != InductionVariable::Unknown) { |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 134 | Instruction *Val = IterCount; |
| 135 | if (!isa<ConstantInt>(IV->Step) || // If the step != 1 |
| 136 | !cast<ConstantInt>(IV->Step)->equalsInt(1)) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 137 | std::string Name; // Create a scale by the step value... |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 138 | if (IV->Phi->hasName()) Name = IV->Phi->getName()+"-scale"; |
| 139 | |
| 140 | // If the types are not compatible, insert a cast now... |
| 141 | if (Val->getType() != IV->Step->getType()) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 142 | Val = InsertCast(Val, IV->Step->getType(), AfterPHIIt); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 143 | |
| 144 | Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step, Name); |
| 145 | // Insert the phi node at the end of the other phi nodes... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 146 | Header->getInstList().insert(AfterPHIIt, Val); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | if (!isa<Constant>(IV->Start) || // If the start != 0 |
| 150 | !cast<Constant>(IV->Start)->isNullValue()) { |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 151 | std::string Name; // Create a offset by the start value... |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 152 | if (IV->Phi->hasName()) Name = IV->Phi->getName()+"-offset"; |
| 153 | |
| 154 | // If the types are not compatible, insert a cast now... |
| 155 | if (Val->getType() != IV->Start->getType()) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 156 | Val = InsertCast(Val, IV->Start->getType(), AfterPHIIt); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 157 | |
| 158 | Val = BinaryOperator::create(Instruction::Add, Val, IV->Start, Name); |
| 159 | // Insert the phi node at the end of the other phi nodes... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 160 | Header->getInstList().insert(AfterPHIIt, Val); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // If the PHI node has a different type than val is, insert a cast now... |
| 164 | if (Val->getType() != IV->Phi->getType()) |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 165 | Val = InsertCast(Val, IV->Phi->getType(), AfterPHIIt); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 166 | |
| 167 | // Replace all uses of the old PHI node with the new computed value... |
| 168 | IV->Phi->replaceAllUsesWith(Val); |
| 169 | |
| 170 | // Move the PHI name to it's new equivalent value... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 171 | std::string OldName = IV->Phi->getName(); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 172 | IV->Phi->setName(""); |
| 173 | Val->setName(OldName); |
| 174 | |
| 175 | // Delete the old, now unused, phi node... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 176 | Header->getInstList().erase(IV->Phi); |
Chris Lattner | 4753bf2 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 177 | Changed = true; |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 178 | ++NumRemoved; |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 179 | } |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | return Changed; |
| 183 | } |
| 184 | |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 185 | namespace { |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 186 | struct InductionVariableSimplify : public FunctionPass { |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 187 | virtual bool runOnFunction(Function &) { |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 188 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 189 | |
| 190 | // Induction Variables live in the header nodes of loops |
| 191 | return reduce_apply_bool(LI.getTopLevelLoops().begin(), |
| 192 | LI.getTopLevelLoops().end(), |
| 193 | std::bind1st(std::ptr_fun(TransformLoop), &LI)); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Chris Lattner | f57b845 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 196 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 1b7f7dc | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 197 | AU.addRequired(LoopInfo::ID); |
Chris Lattner | 97e52e4 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 198 | AU.preservesCFG(); |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 199 | } |
| 200 | }; |
Chris Lattner | f629309 | 2002-07-23 18:06:35 +0000 | [diff] [blame^] | 201 | RegisterPass<InductionVariableSimplify> X("indvars", |
| 202 | "Cannonicalize Induction Variables"); |
Chris Lattner | 793c6b8 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Chris Lattner | bd0ef77 | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 205 | Pass *createIndVarSimplifyPass() { |
| 206 | return new InductionVariableSimplify(); |
Chris Lattner | 793c6b8 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 207 | } |