Chris Lattner | 476e6df | 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 | b4cfa7f | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 8 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 9 | #include "llvm/Analysis/InductionVariable.h" |
| 10 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 11 | #include "llvm/iPHINode.h" |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 12 | #include "llvm/iOther.h" |
| 13 | #include "llvm/Type.h" |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 14 | #include "llvm/Constants.h" |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 15 | #include "llvm/Support/CFG.h" |
Chris Lattner | 8abcd56 | 2003-08-01 22:15:03 +0000 | [diff] [blame] | 16 | #include "Support/Debug.h" |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 17 | #include "Support/Statistic.h" |
Chris Lattner | 8abcd56 | 2003-08-01 22:15:03 +0000 | [diff] [blame] | 18 | #include "Support/STLExtras.h" |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 19 | |
Chris Lattner | 4184bcc | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 20 | namespace { |
Chris Lattner | bf3a099 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 21 | Statistic<> NumRemoved ("indvars", "Number of aux indvars removed"); |
| 22 | Statistic<> NumInserted("indvars", "Number of cannonical indvars added"); |
Chris Lattner | 4184bcc | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 23 | } |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 24 | |
| 25 | // InsertCast - Cast Val to Ty, setting a useful name on the cast if Val has a |
| 26 | // name... |
| 27 | // |
Chris Lattner | 4184bcc | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 28 | static Instruction *InsertCast(Value *Val, const Type *Ty, |
Chris Lattner | 28a8d24 | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 29 | Instruction *InsertBefore) { |
| 30 | return new CastInst(Val, Ty, Val->getName()+"-casted", InsertBefore); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Chris Lattner | 78dd56f | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 33 | static bool TransformLoop(LoopInfo *Loops, Loop *Loop) { |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 34 | // Transform all subloops before this loop... |
| 35 | bool Changed = reduce_apply_bool(Loop->getSubLoops().begin(), |
| 36 | Loop->getSubLoops().end(), |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 37 | std::bind1st(std::ptr_fun(TransformLoop), Loops)); |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 38 | // 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 Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 40 | // |
| 41 | BasicBlock *Header = Loop->getBlocks().front(); |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 42 | |
| 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 Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 47 | std::vector<InductionVariable> IndVars; // Induction variables for block |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 48 | BasicBlock::iterator AfterPHIIt = Header->begin(); |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 49 | for (; PHINode *PN = dyn_cast<PHINode>(AfterPHIIt); ++AfterPHIIt) |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 50 | IndVars.push_back(InductionVariable(PN, Loops)); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 51 | // AfterPHIIt now points to first nonphi instruction... |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 53 | // If there are no phi nodes in this basic block, there can't be indvars... |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 54 | 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 Lattner | 4184bcc | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 63 | if (IndVars[i].InductionType == InductionVariable::Cannonical && |
| 64 | !isa<PointerType>(IndVars[i].Phi->getType())) |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 65 | 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 Lattner | 28a8d24 | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 76 | // 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 Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 79 | |
| 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 Lattner | 28a8d24 | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 83 | "add1-indvar", AfterPHIIt); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 84 | |
| 85 | // Figure out which block is incoming and which is the backedge for the loop |
| 86 | BasicBlock *Incoming, *BackEdgeBlock; |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 87 | pred_iterator PI = pred_begin(Header); |
| 88 | assert(PI != pred_end(Header) && "Loop headers should have 2 preds!"); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 89 | 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 Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 96 | assert(PI == pred_end(Header) && "Loop headers should have 2 preds!"); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 97 | |
| 98 | // Add incoming values for the PHI node... |
Chris Lattner | 2716b5e | 2002-04-27 02:25:14 +0000 | [diff] [blame] | 99 | PN->addIncoming(Constant::getNullValue(Type::UIntTy), Incoming); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 100 | 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 Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 107 | ++NumInserted; |
Chris Lattner | 6743940 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 108 | Changed = true; |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Anand Shukla | 2bc6419 | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 111 | DEBUG(std::cerr << "Induction variables:\n"); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 112 | |
| 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 Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 121 | for (unsigned i = 0; i < IndVars.size(); ++i) { |
| 122 | InductionVariable *IV = &IndVars[i]; |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 2675007 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 124 | DEBUG(IV->print(std::cerr)); |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 4184bcc | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 126 | // Don't do math with pointers... |
| 127 | const Type *IVTy = IV->Phi->getType(); |
| 128 | if (isa<PointerType>(IVTy)) IVTy = Type::ULongTy; |
| 129 | |
Chris Lattner | d23d752 | 2001-12-04 08:13:06 +0000 | [diff] [blame] | 130 | // Don't modify the cannonical indvar or unrecognized indvars... |
| 131 | if (IV != Cannonical && IV->InductionType != InductionVariable::Unknown) { |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 132 | Instruction *Val = IterCount; |
| 133 | if (!isa<ConstantInt>(IV->Step) || // If the step != 1 |
| 134 | !cast<ConstantInt>(IV->Step)->equalsInt(1)) { |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 135 | |
| 136 | // If the types are not compatible, insert a cast now... |
Chris Lattner | 4184bcc | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 137 | 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 Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 141 | |
Chris Lattner | 4184bcc | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 142 | Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step, |
Chris Lattner | 28a8d24 | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 143 | IV->Phi->getName()+"-scale", AfterPHIIt); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Chris Lattner | 4184bcc | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 146 | // If the start != 0 |
| 147 | if (IV->Start != Constant::getNullValue(IV->Start->getType())) { |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 148 | // If the types are not compatible, insert a cast now... |
Chris Lattner | 4184bcc | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 149 | 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 Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 153 | |
Chris Lattner | 28a8d24 | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 154 | // Insert the instruction after the phi nodes... |
Chris Lattner | 4184bcc | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 155 | Val = BinaryOperator::create(Instruction::Add, Val, IV->Start, |
Chris Lattner | 28a8d24 | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 156 | IV->Phi->getName()+"-offset", AfterPHIIt); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 157 | } |
| 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 Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 161 | Val = InsertCast(Val, IV->Phi->getType(), AfterPHIIt); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 162 | |
| 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 Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 167 | std::string OldName = IV->Phi->getName(); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 168 | IV->Phi->setName(""); |
| 169 | Val->setName(OldName); |
| 170 | |
| 171 | // Delete the old, now unused, phi node... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 172 | Header->getInstList().erase(IV->Phi); |
Chris Lattner | 6743940 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 173 | Changed = true; |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 174 | ++NumRemoved; |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 175 | } |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | return Changed; |
| 179 | } |
| 180 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 181 | namespace { |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 182 | struct InductionVariableSimplify : public FunctionPass { |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 183 | virtual bool runOnFunction(Function &) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 184 | 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 Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 192 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | f0ed55d | 2002-08-08 19:01:30 +0000 | [diff] [blame] | 193 | AU.addRequired<LoopInfo>(); |
Chris Lattner | 820d971 | 2002-10-21 20:00:28 +0000 | [diff] [blame] | 194 | AU.setPreservesCFG(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 195 | } |
| 196 | }; |
Chris Lattner | c8b7092 | 2002-07-26 21:12:46 +0000 | [diff] [blame] | 197 | RegisterOpt<InductionVariableSimplify> X("indvars", |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 198 | "Cannonicalize Induction Variables"); |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 201 | Pass *createIndVarSimplifyPass() { |
| 202 | return new InductionVariableSimplify(); |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 203 | } |