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 | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame^] | 11 | #include "llvm/Analysis/Writer.h" |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 12 | #include "llvm/iPHINode.h" |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 13 | #include "llvm/iOther.h" |
| 14 | #include "llvm/Type.h" |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 15 | #include "llvm/Constants.h" |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 16 | #include "llvm/Support/CFG.h" |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 17 | #include "Support/STLExtras.h" |
Chris Lattner | 0b18c1d | 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 | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 22 | |
Chris Lattner | 91daaab | 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 | 78dd56f | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 35 | static bool TransformLoop(LoopInfo *Loops, Loop *Loop) { |
Chris Lattner | 476e6df | 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 | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 39 | std::bind1st(std::ptr_fun(TransformLoop), Loops)); |
Chris Lattner | 476e6df | 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 | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 42 | // |
| 43 | BasicBlock *Header = Loop->getBlocks().front(); |
Chris Lattner | 476e6df | 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 | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 49 | std::vector<InductionVariable> IndVars; // Induction variables for block |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 50 | for (BasicBlock::iterator I = Header->begin(); |
| 51 | PHINode *PN = dyn_cast<PHINode>(*I); ++I) |
| 52 | IndVars.push_back(InductionVariable(PN, Loops)); |
| 53 | |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 54 | // 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] | 55 | if (IndVars.empty()) return Changed; |
| 56 | |
| 57 | // Loop over the induction variables, looking for a cannonical induction |
| 58 | // variable, and checking to make sure they are not all unknown induction |
| 59 | // variables. |
| 60 | // |
| 61 | bool FoundIndVars = false; |
| 62 | InductionVariable *Cannonical = 0; |
| 63 | for (unsigned i = 0; i < IndVars.size(); ++i) { |
| 64 | if (IndVars[i].InductionType == InductionVariable::Cannonical) |
| 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 | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 76 | // Create the PHI node for the new induction variable |
| 77 | PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar"); |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 79 | // Insert the phi node at the end of the other phi nodes... |
| 80 | Header->getInstList().insert(Header->begin()+IndVars.size(), PN); |
| 81 | |
| 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), |
| 85 | "add1-indvar"); |
| 86 | |
| 87 | // Insert the add instruction after all of the PHI nodes... |
| 88 | Header->getInstList().insert(Header->begin()+(IndVars.size()+1), Add); |
| 89 | |
| 90 | // Figure out which block is incoming and which is the backedge for the loop |
| 91 | BasicBlock *Incoming, *BackEdgeBlock; |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 92 | pred_iterator PI = pred_begin(Header); |
| 93 | assert(PI != pred_end(Header) && "Loop headers should have 2 preds!"); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 94 | if (Loop->contains(*PI)) { // First pred is back edge... |
| 95 | BackEdgeBlock = *PI++; |
| 96 | Incoming = *PI++; |
| 97 | } else { |
| 98 | Incoming = *PI++; |
| 99 | BackEdgeBlock = *PI++; |
| 100 | } |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 101 | assert(PI == pred_end(Header) && "Loop headers should have 2 preds!"); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 102 | |
| 103 | // Add incoming values for the PHI node... |
Chris Lattner | 2716b5e | 2002-04-27 02:25:14 +0000 | [diff] [blame] | 104 | PN->addIncoming(Constant::getNullValue(Type::UIntTy), Incoming); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 105 | PN->addIncoming(Add, BackEdgeBlock); |
| 106 | |
| 107 | // Analyze the new induction variable... |
| 108 | IndVars.push_back(InductionVariable(PN, Loops)); |
| 109 | assert(IndVars.back().InductionType == InductionVariable::Cannonical && |
| 110 | "Just inserted cannonical indvar that is not cannonical!"); |
| 111 | Cannonical = &IndVars.back(); |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 112 | ++NumInserted; |
Chris Lattner | 6743940 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 113 | Changed = true; |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame^] | 116 | DEBUG(cerr << "Induction variables:\n"); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 117 | |
| 118 | // Get the current loop iteration count, which is always the value of the |
| 119 | // cannonical phi node... |
| 120 | // |
| 121 | PHINode *IterCount = Cannonical->Phi; |
| 122 | |
| 123 | // Loop through and replace all of the auxillary induction variables with |
| 124 | // references to the primary induction variable... |
| 125 | // |
| 126 | unsigned InsertPos = IndVars.size(); |
| 127 | for (unsigned i = 0; i < IndVars.size(); ++i) { |
| 128 | InductionVariable *IV = &IndVars[i]; |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame^] | 129 | |
| 130 | DEBUG(cerr << IV); |
| 131 | |
Chris Lattner | d23d752 | 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 | 91daaab | 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 | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 137 | std::string Name; // Create a scale by the step value... |
Chris Lattner | 91daaab | 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()) |
| 142 | Val = InsertCast(Val, IV->Step->getType(), |
| 143 | Header->begin()+InsertPos++); |
| 144 | |
| 145 | Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step, Name); |
| 146 | // Insert the phi node at the end of the other phi nodes... |
| 147 | Header->getInstList().insert(Header->begin()+InsertPos++, Val); |
| 148 | } |
| 149 | |
| 150 | if (!isa<Constant>(IV->Start) || // If the start != 0 |
| 151 | !cast<Constant>(IV->Start)->isNullValue()) { |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 152 | std::string Name; // Create a offset by the start value... |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 153 | if (IV->Phi->hasName()) Name = IV->Phi->getName()+"-offset"; |
| 154 | |
| 155 | // If the types are not compatible, insert a cast now... |
| 156 | if (Val->getType() != IV->Start->getType()) |
| 157 | Val = InsertCast(Val, IV->Start->getType(), |
| 158 | Header->begin()+InsertPos++); |
| 159 | |
| 160 | Val = BinaryOperator::create(Instruction::Add, Val, IV->Start, Name); |
| 161 | // Insert the phi node at the end of the other phi nodes... |
| 162 | Header->getInstList().insert(Header->begin()+InsertPos++, Val); |
| 163 | } |
| 164 | |
| 165 | // If the PHI node has a different type than val is, insert a cast now... |
| 166 | if (Val->getType() != IV->Phi->getType()) |
| 167 | Val = InsertCast(Val, IV->Phi->getType(), |
| 168 | Header->begin()+InsertPos++); |
| 169 | |
| 170 | // Replace all uses of the old PHI node with the new computed value... |
| 171 | IV->Phi->replaceAllUsesWith(Val); |
| 172 | |
| 173 | // Move the PHI name to it's new equivalent value... |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 174 | std::string OldName = IV->Phi->getName(); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 175 | IV->Phi->setName(""); |
| 176 | Val->setName(OldName); |
| 177 | |
| 178 | // Delete the old, now unused, phi node... |
| 179 | Header->getInstList().remove(IV->Phi); |
| 180 | delete IV->Phi; |
| 181 | InsertPos--; // Deleted an instr, decrement insert position |
Chris Lattner | 6743940 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 182 | Changed = true; |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 183 | ++NumRemoved; |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 184 | } |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return Changed; |
| 188 | } |
| 189 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 190 | namespace { |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 191 | struct InductionVariableSimplify : public FunctionPass { |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 192 | const char *getPassName() const { |
| 193 | return "Induction Variable Cannonicalize"; |
| 194 | } |
| 195 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 196 | virtual bool runOnFunction(Function *F) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 197 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 198 | |
| 199 | // Induction Variables live in the header nodes of loops |
| 200 | return reduce_apply_bool(LI.getTopLevelLoops().begin(), |
| 201 | LI.getTopLevelLoops().end(), |
| 202 | std::bind1st(std::ptr_fun(TransformLoop), &LI)); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 205 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 78dd56f | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 206 | AU.addRequired(LoopInfo::ID); |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 207 | AU.preservesCFG(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 208 | } |
| 209 | }; |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 212 | Pass *createIndVarSimplifyPass() { |
| 213 | return new InductionVariableSimplify(); |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 214 | } |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 215 | |