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" |
Anand Shukla | 2bc6419 | 2002-06-25 21:07:58 +0000 | [diff] [blame^] | 19 | #include <iostream> |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 20 | |
| 21 | static Statistic<> NumRemoved ("indvars\t\t- Number of aux indvars removed"); |
| 22 | static Statistic<> NumInserted("indvars\t\t- Number of cannonical indvars added"); |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +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 | // |
| 28 | static Instruction *InsertCast(Instruction *Val, const Type *Ty, |
| 29 | BasicBlock::iterator It) { |
| 30 | Instruction *Cast = new CastInst(Val, Ty); |
| 31 | if (Val->hasName()) Cast->setName(Val->getName()+"-casted"); |
| 32 | Val->getParent()->getInstList().insert(It, Cast); |
| 33 | return Cast; |
| 34 | } |
| 35 | |
Chris Lattner | 78dd56f | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 36 | static bool TransformLoop(LoopInfo *Loops, Loop *Loop) { |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 37 | // Transform all subloops before this loop... |
| 38 | bool Changed = reduce_apply_bool(Loop->getSubLoops().begin(), |
| 39 | Loop->getSubLoops().end(), |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 40 | std::bind1st(std::ptr_fun(TransformLoop), Loops)); |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 41 | // Get the header node for this loop. All of the phi nodes that could be |
| 42 | // induction variables must live in this basic block. |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 43 | // |
| 44 | BasicBlock *Header = Loop->getBlocks().front(); |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 45 | |
| 46 | // Loop over all of the PHI nodes in the basic block, calculating the |
| 47 | // induction variables that they represent... stuffing the induction variable |
| 48 | // info into a vector... |
| 49 | // |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 50 | std::vector<InductionVariable> IndVars; // Induction variables for block |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 51 | BasicBlock::iterator AfterPHIIt = Header->begin(); |
| 52 | for (; PHINode *PN = dyn_cast<PHINode>(&*AfterPHIIt); ++AfterPHIIt) |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 53 | IndVars.push_back(InductionVariable(PN, Loops)); |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 54 | // AfterPHIIt now points to first nonphi instruction... |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 56 | // 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] | 57 | if (IndVars.empty()) return Changed; |
| 58 | |
| 59 | // Loop over the induction variables, looking for a cannonical induction |
| 60 | // variable, and checking to make sure they are not all unknown induction |
| 61 | // variables. |
| 62 | // |
| 63 | bool FoundIndVars = false; |
| 64 | InductionVariable *Cannonical = 0; |
| 65 | for (unsigned i = 0; i < IndVars.size(); ++i) { |
| 66 | if (IndVars[i].InductionType == InductionVariable::Cannonical) |
| 67 | Cannonical = &IndVars[i]; |
| 68 | if (IndVars[i].InductionType != InductionVariable::Unknown) |
| 69 | FoundIndVars = true; |
| 70 | } |
| 71 | |
| 72 | // No induction variables, bail early... don't add a cannonnical indvar |
| 73 | if (!FoundIndVars) return Changed; |
| 74 | |
| 75 | // Okay, we want to convert other induction variables to use a cannonical |
| 76 | // indvar. If we don't have one, add one now... |
| 77 | if (!Cannonical) { |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 78 | // Create the PHI node for the new induction variable |
| 79 | PHINode *PN = new PHINode(Type::UIntTy, "cann-indvar"); |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 80 | |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 81 | // Insert the phi node at the end of the other phi nodes... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 82 | AfterPHIIt = ++Header->getInstList().insert(AfterPHIIt, PN); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 83 | |
| 84 | // Create the increment instruction to add one to the counter... |
| 85 | Instruction *Add = BinaryOperator::create(Instruction::Add, PN, |
| 86 | ConstantUInt::get(Type::UIntTy,1), |
| 87 | "add1-indvar"); |
| 88 | |
| 89 | // Insert the add instruction after all of the PHI nodes... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 90 | Header->getInstList().insert(AfterPHIIt, Add); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 91 | |
| 92 | // Figure out which block is incoming and which is the backedge for the loop |
| 93 | BasicBlock *Incoming, *BackEdgeBlock; |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 94 | pred_iterator PI = pred_begin(Header); |
| 95 | assert(PI != pred_end(Header) && "Loop headers should have 2 preds!"); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 96 | if (Loop->contains(*PI)) { // First pred is back edge... |
| 97 | BackEdgeBlock = *PI++; |
| 98 | Incoming = *PI++; |
| 99 | } else { |
| 100 | Incoming = *PI++; |
| 101 | BackEdgeBlock = *PI++; |
| 102 | } |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 103 | assert(PI == pred_end(Header) && "Loop headers should have 2 preds!"); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 104 | |
| 105 | // Add incoming values for the PHI node... |
Chris Lattner | 2716b5e | 2002-04-27 02:25:14 +0000 | [diff] [blame] | 106 | PN->addIncoming(Constant::getNullValue(Type::UIntTy), Incoming); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 107 | PN->addIncoming(Add, BackEdgeBlock); |
| 108 | |
| 109 | // Analyze the new induction variable... |
| 110 | IndVars.push_back(InductionVariable(PN, Loops)); |
| 111 | assert(IndVars.back().InductionType == InductionVariable::Cannonical && |
| 112 | "Just inserted cannonical indvar that is not cannonical!"); |
| 113 | Cannonical = &IndVars.back(); |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 114 | ++NumInserted; |
Chris Lattner | 6743940 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 115 | Changed = true; |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Anand Shukla | 2bc6419 | 2002-06-25 21:07:58 +0000 | [diff] [blame^] | 118 | DEBUG(std::cerr << "Induction variables:\n"); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 119 | |
| 120 | // Get the current loop iteration count, which is always the value of the |
| 121 | // cannonical phi node... |
| 122 | // |
| 123 | PHINode *IterCount = Cannonical->Phi; |
| 124 | |
| 125 | // Loop through and replace all of the auxillary induction variables with |
| 126 | // references to the primary induction variable... |
| 127 | // |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 128 | for (unsigned i = 0; i < IndVars.size(); ++i) { |
| 129 | InductionVariable *IV = &IndVars[i]; |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 130 | |
Anand Shukla | 2bc6419 | 2002-06-25 21:07:58 +0000 | [diff] [blame^] | 131 | DEBUG(std::cerr << IV); |
Chris Lattner | 71cbd42 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 132 | |
Chris Lattner | d23d752 | 2001-12-04 08:13:06 +0000 | [diff] [blame] | 133 | // Don't modify the cannonical indvar or unrecognized indvars... |
| 134 | if (IV != Cannonical && IV->InductionType != InductionVariable::Unknown) { |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 135 | Instruction *Val = IterCount; |
| 136 | if (!isa<ConstantInt>(IV->Step) || // If the step != 1 |
| 137 | !cast<ConstantInt>(IV->Step)->equalsInt(1)) { |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 138 | std::string Name; // Create a scale by the step value... |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 139 | if (IV->Phi->hasName()) Name = IV->Phi->getName()+"-scale"; |
| 140 | |
| 141 | // If the types are not compatible, insert a cast now... |
| 142 | if (Val->getType() != IV->Step->getType()) |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 143 | Val = InsertCast(Val, IV->Step->getType(), AfterPHIIt); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 144 | |
| 145 | Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step, Name); |
| 146 | // Insert the phi node at the end of the other phi nodes... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 147 | Header->getInstList().insert(AfterPHIIt, Val); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 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()) |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 157 | Val = InsertCast(Val, IV->Start->getType(), AfterPHIIt); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 158 | |
| 159 | Val = BinaryOperator::create(Instruction::Add, Val, IV->Start, Name); |
| 160 | // Insert the phi node at the end of the other phi nodes... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 161 | Header->getInstList().insert(AfterPHIIt, Val); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // If the PHI node has a different type than val is, insert a cast now... |
| 165 | if (Val->getType() != IV->Phi->getType()) |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 166 | Val = InsertCast(Val, IV->Phi->getType(), AfterPHIIt); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 167 | |
| 168 | // Replace all uses of the old PHI node with the new computed value... |
| 169 | IV->Phi->replaceAllUsesWith(Val); |
| 170 | |
| 171 | // Move the PHI name to it's new equivalent value... |
Chris Lattner | 7f74a56 | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 172 | std::string OldName = IV->Phi->getName(); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 173 | IV->Phi->setName(""); |
| 174 | Val->setName(OldName); |
| 175 | |
| 176 | // Delete the old, now unused, phi node... |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 177 | Header->getInstList().erase(IV->Phi); |
Chris Lattner | 6743940 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 178 | Changed = true; |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 179 | ++NumRemoved; |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 180 | } |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | return Changed; |
| 184 | } |
| 185 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 186 | namespace { |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 187 | struct InductionVariableSimplify : public FunctionPass { |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 188 | const char *getPassName() const { |
| 189 | return "Induction Variable Cannonicalize"; |
| 190 | } |
| 191 | |
Chris Lattner | 113f4f4 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 192 | virtual bool runOnFunction(Function &) { |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 193 | LoopInfo &LI = getAnalysis<LoopInfo>(); |
| 194 | |
| 195 | // Induction Variables live in the header nodes of loops |
| 196 | return reduce_apply_bool(LI.getTopLevelLoops().begin(), |
| 197 | LI.getTopLevelLoops().end(), |
| 198 | std::bind1st(std::ptr_fun(TransformLoop), &LI)); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 201 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 78dd56f | 2002-04-28 16:21:30 +0000 | [diff] [blame] | 202 | AU.addRequired(LoopInfo::ID); |
Chris Lattner | f12cc84 | 2002-04-28 21:27:06 +0000 | [diff] [blame] | 203 | AU.preservesCFG(); |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 204 | } |
| 205 | }; |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 208 | Pass *createIndVarSimplifyPass() { |
| 209 | return new InductionVariableSimplify(); |
Chris Lattner | d5d5678 | 2002-01-31 00:45:11 +0000 | [diff] [blame] | 210 | } |
Chris Lattner | 04805fa | 2002-02-26 21:46:54 +0000 | [diff] [blame] | 211 | |