Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 1 | //===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 10 | // This transformation analyzes and transforms the induction variables (and |
| 11 | // computations derived from them) into simpler forms suitable for subsequent |
| 12 | // analysis and transformation. |
| 13 | // |
| 14 | // This transformation make the following changes to each loop with an |
| 15 | // identifiable induction variable: |
| 16 | // 1. All loops are transformed to have a SINGLE canonical induction variable |
| 17 | // which starts at zero and steps by one. |
| 18 | // 2. The canonical induction variable is guaranteed to be the first PHI node |
| 19 | // in the loop header block. |
| 20 | // 3. Any pointer arithmetic recurrences are raised to use array subscripts. |
| 21 | // |
| 22 | // If the trip count of a loop is computable, this pass also makes the following |
| 23 | // changes: |
| 24 | // 1. The exit condition for the loop is canonicalized to compare the |
| 25 | // induction value against the exit value. This turns loops like: |
| 26 | // 'for (i = 7; i*i < 1000; ++i)' into 'for (i = 0; i != 25; ++i)' |
| 27 | // 2. Any use outside of the loop of an expression derived from the indvar |
| 28 | // is changed to compute the derived value outside of the loop, eliminating |
| 29 | // the dependence on the exit value of the induction variable. If the only |
| 30 | // purpose of the loop is to compute the exit value of some derived |
| 31 | // expression, this transformation will make the loop dead. |
| 32 | // |
| 33 | // This transformation should be followed by strength reduction after all of the |
| 34 | // desired loop transformations have been performed. Additionally, on targets |
| 35 | // where it is profitable, the loop could be transformed to count down to zero |
| 36 | // (the "do loop" optimization). |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 37 | // |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 40 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 41 | #include "llvm/BasicBlock.h" |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 42 | #include "llvm/Constants.h" |
Chris Lattner | 18b3c97 | 2003-12-22 05:02:01 +0000 | [diff] [blame] | 43 | #include "llvm/Instructions.h" |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 44 | #include "llvm/Type.h" |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 47 | #include "llvm/Support/CFG.h" |
Chris Lattner | a4b9c78 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 48 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 49 | #include "llvm/Transforms/Utils/Local.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 50 | #include "llvm/Support/CommandLine.h" |
| 51 | #include "llvm/ADT/Statistic.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 52 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 54 | namespace { |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 55 | /// SCEVExpander - This class uses information about analyze scalars to |
| 56 | /// rewrite expressions in canonical form. |
| 57 | /// |
| 58 | /// Clients should create an instance of this class when rewriting is needed, |
| 59 | /// and destroying it when finished to allow the release of the associated |
| 60 | /// memory. |
| 61 | struct SCEVExpander : public SCEVVisitor<SCEVExpander, Value*> { |
| 62 | ScalarEvolution &SE; |
| 63 | LoopInfo &LI; |
| 64 | std::map<SCEVHandle, Value*> InsertedExpressions; |
| 65 | std::set<Instruction*> InsertedInstructions; |
| 66 | |
| 67 | Instruction *InsertPt; |
| 68 | |
Chris Lattner | 1fca5ff | 2004-10-27 16:14:51 +0000 | [diff] [blame] | 69 | friend struct SCEVVisitor<SCEVExpander, Value*>; |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 70 | public: |
| 71 | SCEVExpander(ScalarEvolution &se, LoopInfo &li) : SE(se), LI(li) {} |
| 72 | |
| 73 | /// isInsertedInstruction - Return true if the specified instruction was |
| 74 | /// inserted by the code rewriter. If so, the client should not modify the |
| 75 | /// instruction. |
| 76 | bool isInsertedInstruction(Instruction *I) const { |
| 77 | return InsertedInstructions.count(I); |
| 78 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 80 | /// getOrInsertCanonicalInductionVariable - This method returns the |
| 81 | /// canonical induction variable of the specified type for the specified |
| 82 | /// loop (inserting one if there is none). A canonical induction variable |
| 83 | /// starts at zero and steps by one on each iteration. |
| 84 | Value *getOrInsertCanonicalInductionVariable(const Loop *L, const Type *Ty){ |
| 85 | assert((Ty->isInteger() || Ty->isFloatingPoint()) && |
| 86 | "Can only insert integer or floating point induction variables!"); |
| 87 | SCEVHandle H = SCEVAddRecExpr::get(SCEVUnknown::getIntegerSCEV(0, Ty), |
| 88 | SCEVUnknown::getIntegerSCEV(1, Ty), L); |
| 89 | return expand(H); |
| 90 | } |
| 91 | |
| 92 | /// addInsertedValue - Remember the specified instruction as being the |
| 93 | /// canonical form for the specified SCEV. |
| 94 | void addInsertedValue(Instruction *I, SCEV *S) { |
| 95 | InsertedExpressions[S] = (Value*)I; |
| 96 | InsertedInstructions.insert(I); |
| 97 | } |
| 98 | |
| 99 | /// expandCodeFor - Insert code to directly compute the specified SCEV |
| 100 | /// expression into the program. The inserted code is inserted into the |
| 101 | /// specified block. |
| 102 | /// |
| 103 | /// If a particular value sign is required, a type may be specified for the |
| 104 | /// result. |
| 105 | Value *expandCodeFor(SCEVHandle SH, Instruction *IP, const Type *Ty = 0) { |
| 106 | // Expand the code for this SCEV. |
| 107 | this->InsertPt = IP; |
| 108 | return expandInTy(SH, Ty); |
| 109 | } |
| 110 | |
| 111 | protected: |
| 112 | Value *expand(SCEV *S) { |
| 113 | // Check to see if we already expanded this. |
| 114 | std::map<SCEVHandle, Value*>::iterator I = InsertedExpressions.find(S); |
| 115 | if (I != InsertedExpressions.end()) |
| 116 | return I->second; |
| 117 | |
| 118 | Value *V = visit(S); |
| 119 | InsertedExpressions[S] = V; |
| 120 | return V; |
| 121 | } |
| 122 | |
| 123 | Value *expandInTy(SCEV *S, const Type *Ty) { |
| 124 | Value *V = expand(S); |
| 125 | if (Ty && V->getType() != Ty) { |
| 126 | // FIXME: keep track of the cast instruction. |
| 127 | if (Constant *C = dyn_cast<Constant>(V)) |
| 128 | return ConstantExpr::getCast(C, Ty); |
| 129 | else if (Instruction *I = dyn_cast<Instruction>(V)) { |
| 130 | // Check to see if there is already a cast. If there is, use it. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 131 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 132 | UI != E; ++UI) { |
| 133 | if ((*UI)->getType() == Ty) |
| 134 | if (CastInst *CI = dyn_cast<CastInst>(cast<Instruction>(*UI))) { |
| 135 | BasicBlock::iterator It = I; ++It; |
Chris Lattner | f624926 | 2005-02-12 03:26:49 +0000 | [diff] [blame] | 136 | if (isa<InvokeInst>(I)) |
| 137 | It = cast<InvokeInst>(I)->getNormalDest()->begin(); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 138 | while (isa<PHINode>(It)) ++It; |
| 139 | if (It != BasicBlock::iterator(CI)) { |
| 140 | // Splice the cast immediately after the operand in question. |
Chris Lattner | a4b9c78 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 141 | BasicBlock::InstListType &InstList = |
Chris Lattner | 8a7980b | 2005-02-14 20:11:45 +0000 | [diff] [blame] | 142 | It->getParent()->getInstList(); |
Chris Lattner | 989cbd5 | 2004-10-12 01:02:29 +0000 | [diff] [blame] | 143 | InstList.splice(It, CI->getParent()->getInstList(), CI); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 144 | } |
| 145 | return CI; |
| 146 | } |
| 147 | } |
| 148 | BasicBlock::iterator IP = I; ++IP; |
| 149 | if (InvokeInst *II = dyn_cast<InvokeInst>(I)) |
| 150 | IP = II->getNormalDest()->begin(); |
| 151 | while (isa<PHINode>(IP)) ++IP; |
| 152 | return new CastInst(V, Ty, V->getName(), IP); |
| 153 | } else { |
| 154 | // FIXME: check to see if there is already a cast! |
| 155 | return new CastInst(V, Ty, V->getName(), InsertPt); |
| 156 | } |
| 157 | } |
| 158 | return V; |
| 159 | } |
| 160 | |
| 161 | Value *visitConstant(SCEVConstant *S) { |
| 162 | return S->getValue(); |
| 163 | } |
| 164 | |
| 165 | Value *visitTruncateExpr(SCEVTruncateExpr *S) { |
| 166 | Value *V = expand(S->getOperand()); |
| 167 | return new CastInst(V, S->getType(), "tmp.", InsertPt); |
| 168 | } |
| 169 | |
| 170 | Value *visitZeroExtendExpr(SCEVZeroExtendExpr *S) { |
Chris Lattner | 2b994c7 | 2004-06-19 18:15:50 +0000 | [diff] [blame] | 171 | Value *V = expandInTy(S->getOperand(),S->getType()->getUnsignedVersion()); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 172 | return new CastInst(V, S->getType(), "tmp.", InsertPt); |
| 173 | } |
| 174 | |
| 175 | Value *visitAddExpr(SCEVAddExpr *S) { |
| 176 | const Type *Ty = S->getType(); |
| 177 | Value *V = expandInTy(S->getOperand(S->getNumOperands()-1), Ty); |
| 178 | |
| 179 | // Emit a bunch of add instructions |
| 180 | for (int i = S->getNumOperands()-2; i >= 0; --i) |
Chris Lattner | c5c5e6a | 2004-06-20 05:04:01 +0000 | [diff] [blame] | 181 | V = BinaryOperator::createAdd(V, expandInTy(S->getOperand(i), Ty), |
| 182 | "tmp.", InsertPt); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 183 | return V; |
| 184 | } |
| 185 | |
| 186 | Value *visitMulExpr(SCEVMulExpr *S); |
| 187 | |
| 188 | Value *visitUDivExpr(SCEVUDivExpr *S) { |
| 189 | const Type *Ty = S->getType(); |
| 190 | Value *LHS = expandInTy(S->getLHS(), Ty); |
| 191 | Value *RHS = expandInTy(S->getRHS(), Ty); |
Chris Lattner | c5c5e6a | 2004-06-20 05:04:01 +0000 | [diff] [blame] | 192 | return BinaryOperator::createDiv(LHS, RHS, "tmp.", InsertPt); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | Value *visitAddRecExpr(SCEVAddRecExpr *S); |
| 196 | |
| 197 | Value *visitUnknown(SCEVUnknown *S) { |
| 198 | return S->getValue(); |
| 199 | } |
| 200 | }; |
| 201 | } |
| 202 | |
| 203 | Value *SCEVExpander::visitMulExpr(SCEVMulExpr *S) { |
| 204 | const Type *Ty = S->getType(); |
| 205 | int FirstOp = 0; // Set if we should emit a subtract. |
| 206 | if (SCEVConstant *SC = dyn_cast<SCEVConstant>(S->getOperand(0))) |
| 207 | if (SC->getValue()->isAllOnesValue()) |
| 208 | FirstOp = 1; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 209 | |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 210 | int i = S->getNumOperands()-2; |
| 211 | Value *V = expandInTy(S->getOperand(i+1), Ty); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 212 | |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 213 | // Emit a bunch of multiply instructions |
| 214 | for (; i >= FirstOp; --i) |
Chris Lattner | c5c5e6a | 2004-06-20 05:04:01 +0000 | [diff] [blame] | 215 | V = BinaryOperator::createMul(V, expandInTy(S->getOperand(i), Ty), |
| 216 | "tmp.", InsertPt); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 217 | // -1 * ... ---> 0 - ... |
| 218 | if (FirstOp == 1) |
Chris Lattner | c5c5e6a | 2004-06-20 05:04:01 +0000 | [diff] [blame] | 219 | V = BinaryOperator::createNeg(V, "tmp.", InsertPt); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 220 | return V; |
| 221 | } |
| 222 | |
| 223 | Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) { |
| 224 | const Type *Ty = S->getType(); |
| 225 | const Loop *L = S->getLoop(); |
| 226 | // We cannot yet do fp recurrences, e.g. the xform of {X,+,F} --> X+{0,+,F} |
| 227 | assert(Ty->isIntegral() && "Cannot expand fp recurrences yet!"); |
| 228 | |
| 229 | // {X,+,F} --> X + {0,+,F} |
| 230 | if (!isa<SCEVConstant>(S->getStart()) || |
| 231 | !cast<SCEVConstant>(S->getStart())->getValue()->isNullValue()) { |
| 232 | Value *Start = expandInTy(S->getStart(), Ty); |
| 233 | std::vector<SCEVHandle> NewOps(S->op_begin(), S->op_end()); |
| 234 | NewOps[0] = SCEVUnknown::getIntegerSCEV(0, Ty); |
| 235 | Value *Rest = expandInTy(SCEVAddRecExpr::get(NewOps, L), Ty); |
| 236 | |
| 237 | // FIXME: look for an existing add to use. |
Chris Lattner | c5c5e6a | 2004-06-20 05:04:01 +0000 | [diff] [blame] | 238 | return BinaryOperator::createAdd(Rest, Start, "tmp.", InsertPt); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // {0,+,1} --> Insert a canonical induction variable into the loop! |
| 242 | if (S->getNumOperands() == 2 && |
| 243 | S->getOperand(1) == SCEVUnknown::getIntegerSCEV(1, Ty)) { |
| 244 | // Create and insert the PHI node for the induction variable in the |
| 245 | // specified loop. |
| 246 | BasicBlock *Header = L->getHeader(); |
| 247 | PHINode *PN = new PHINode(Ty, "indvar", Header->begin()); |
| 248 | PN->addIncoming(Constant::getNullValue(Ty), L->getLoopPreheader()); |
| 249 | |
| 250 | pred_iterator HPI = pred_begin(Header); |
| 251 | assert(HPI != pred_end(Header) && "Loop with zero preds???"); |
| 252 | if (!L->contains(*HPI)) ++HPI; |
| 253 | assert(HPI != pred_end(Header) && L->contains(*HPI) && |
| 254 | "No backedge in loop?"); |
| 255 | |
| 256 | // Insert a unit add instruction right before the terminator corresponding |
| 257 | // to the back-edge. |
| 258 | Constant *One = Ty->isFloatingPoint() ? (Constant*)ConstantFP::get(Ty, 1.0) |
| 259 | : ConstantInt::get(Ty, 1); |
Chris Lattner | c5c5e6a | 2004-06-20 05:04:01 +0000 | [diff] [blame] | 260 | Instruction *Add = BinaryOperator::createAdd(PN, One, "indvar.next", |
| 261 | (*HPI)->getTerminator()); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 262 | |
| 263 | pred_iterator PI = pred_begin(Header); |
| 264 | if (*PI == L->getLoopPreheader()) |
| 265 | ++PI; |
| 266 | PN->addIncoming(Add, *PI); |
| 267 | return PN; |
| 268 | } |
| 269 | |
| 270 | // Get the canonical induction variable I for this loop. |
| 271 | Value *I = getOrInsertCanonicalInductionVariable(L, Ty); |
| 272 | |
| 273 | if (S->getNumOperands() == 2) { // {0,+,F} --> i*F |
| 274 | Value *F = expandInTy(S->getOperand(1), Ty); |
Chris Lattner | c5c5e6a | 2004-06-20 05:04:01 +0000 | [diff] [blame] | 275 | return BinaryOperator::createMul(I, F, "tmp.", InsertPt); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | // If this is a chain of recurrences, turn it into a closed form, using the |
| 279 | // folders, then expandCodeFor the closed form. This allows the folders to |
| 280 | // simplify the expression without having to build a bunch of special code |
| 281 | // into this folder. |
| 282 | SCEVHandle IH = SCEVUnknown::get(I); // Get I as a "symbolic" SCEV. |
| 283 | |
| 284 | SCEVHandle V = S->evaluateAtIteration(IH); |
| 285 | //std::cerr << "Evaluated: " << *this << "\n to: " << *V << "\n"; |
| 286 | |
| 287 | return expandInTy(V, Ty); |
| 288 | } |
| 289 | |
| 290 | |
| 291 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 292 | Statistic<> NumRemoved ("indvars", "Number of aux indvars removed"); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 293 | Statistic<> NumPointer ("indvars", "Number of pointer indvars promoted"); |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 294 | Statistic<> NumInserted("indvars", "Number of canonical indvars added"); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 295 | Statistic<> NumReplaced("indvars", "Number of exit values replaced"); |
| 296 | Statistic<> NumLFTR ("indvars", "Number of loop exit tests replaced"); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 297 | |
| 298 | class IndVarSimplify : public FunctionPass { |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 299 | LoopInfo *LI; |
| 300 | ScalarEvolution *SE; |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 301 | bool Changed; |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 302 | public: |
| 303 | virtual bool runOnFunction(Function &) { |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 304 | LI = &getAnalysis<LoopInfo>(); |
| 305 | SE = &getAnalysis<ScalarEvolution>(); |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 306 | Changed = false; |
| 307 | |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 308 | // Induction Variables live in the header nodes of loops |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 309 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
Chris Lattner | 329c1c6 | 2004-01-08 00:09:44 +0000 | [diff] [blame] | 310 | runOnLoop(*I); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 311 | return Changed; |
| 312 | } |
| 313 | |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 314 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 315 | AU.addRequiredID(LoopSimplifyID); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 316 | AU.addRequired<ScalarEvolution>(); |
| 317 | AU.addRequired<LoopInfo>(); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 318 | AU.addPreservedID(LoopSimplifyID); |
| 319 | AU.setPreservesCFG(); |
| 320 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 321 | private: |
| 322 | void runOnLoop(Loop *L); |
| 323 | void EliminatePointerRecurrence(PHINode *PN, BasicBlock *Preheader, |
| 324 | std::set<Instruction*> &DeadInsts); |
| 325 | void LinearFunctionTestReplace(Loop *L, SCEV *IterationCount, |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 326 | SCEVExpander &RW); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 327 | void RewriteLoopExitValues(Loop *L); |
| 328 | |
| 329 | void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 330 | }; |
| 331 | RegisterOpt<IndVarSimplify> X("indvars", "Canonicalize Induction Variables"); |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 332 | } |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 333 | |
Chris Lattner | 4b50156 | 2004-09-20 04:43:15 +0000 | [diff] [blame] | 334 | FunctionPass *llvm::createIndVarSimplifyPass() { |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 335 | return new IndVarSimplify(); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 338 | /// DeleteTriviallyDeadInstructions - If any of the instructions is the |
| 339 | /// specified set are trivially dead, delete them and see if this makes any of |
| 340 | /// their operands subsequently dead. |
| 341 | void IndVarSimplify:: |
| 342 | DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) { |
| 343 | while (!Insts.empty()) { |
| 344 | Instruction *I = *Insts.begin(); |
| 345 | Insts.erase(Insts.begin()); |
| 346 | if (isInstructionTriviallyDead(I)) { |
| 347 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 348 | if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i))) |
| 349 | Insts.insert(U); |
| 350 | SE->deleteInstructionFromRecords(I); |
Chris Lattner | a4b9c78 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 351 | I->eraseFromParent(); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 352 | Changed = true; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | |
| 358 | /// EliminatePointerRecurrence - Check to see if this is a trivial GEP pointer |
| 359 | /// recurrence. If so, change it into an integer recurrence, permitting |
| 360 | /// analysis by the SCEV routines. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 361 | void IndVarSimplify::EliminatePointerRecurrence(PHINode *PN, |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 362 | BasicBlock *Preheader, |
| 363 | std::set<Instruction*> &DeadInsts) { |
| 364 | assert(PN->getNumIncomingValues() == 2 && "Noncanonicalized loop!"); |
| 365 | unsigned PreheaderIdx = PN->getBasicBlockIndex(Preheader); |
| 366 | unsigned BackedgeIdx = PreheaderIdx^1; |
| 367 | if (GetElementPtrInst *GEPI = |
| 368 | dyn_cast<GetElementPtrInst>(PN->getIncomingValue(BackedgeIdx))) |
| 369 | if (GEPI->getOperand(0) == PN) { |
| 370 | assert(GEPI->getNumOperands() == 2 && "GEP types must mismatch!"); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 371 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 372 | // Okay, we found a pointer recurrence. Transform this pointer |
| 373 | // recurrence into an integer recurrence. Compute the value that gets |
| 374 | // added to the pointer at every iteration. |
| 375 | Value *AddedVal = GEPI->getOperand(1); |
| 376 | |
| 377 | // Insert a new integer PHI node into the top of the block. |
| 378 | PHINode *NewPhi = new PHINode(AddedVal->getType(), |
| 379 | PN->getName()+".rec", PN); |
Chris Lattner | c5c5e6a | 2004-06-20 05:04:01 +0000 | [diff] [blame] | 380 | NewPhi->addIncoming(Constant::getNullValue(NewPhi->getType()), Preheader); |
| 381 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 382 | // Create the new add instruction. |
Chris Lattner | c5c5e6a | 2004-06-20 05:04:01 +0000 | [diff] [blame] | 383 | Value *NewAdd = BinaryOperator::createAdd(NewPhi, AddedVal, |
| 384 | GEPI->getName()+".rec", GEPI); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 385 | NewPhi->addIncoming(NewAdd, PN->getIncomingBlock(BackedgeIdx)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 386 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 387 | // Update the existing GEP to use the recurrence. |
| 388 | GEPI->setOperand(0, PN->getIncomingValue(PreheaderIdx)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 389 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 390 | // Update the GEP to use the new recurrence we just inserted. |
| 391 | GEPI->setOperand(1, NewAdd); |
| 392 | |
Chris Lattner | a4b9c78 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 393 | // If the incoming value is a constant expr GEP, try peeling out the array |
| 394 | // 0 index if possible to make things simpler. |
| 395 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEPI->getOperand(0))) |
| 396 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 397 | unsigned NumOps = CE->getNumOperands(); |
| 398 | assert(NumOps > 1 && "CE folding didn't work!"); |
| 399 | if (CE->getOperand(NumOps-1)->isNullValue()) { |
| 400 | // Check to make sure the last index really is an array index. |
| 401 | gep_type_iterator GTI = gep_type_begin(GEPI); |
| 402 | for (unsigned i = 1, e = GEPI->getNumOperands()-1; |
| 403 | i != e; ++i, ++GTI) |
| 404 | /*empty*/; |
| 405 | if (isa<SequentialType>(*GTI)) { |
| 406 | // Pull the last index out of the constant expr GEP. |
| 407 | std::vector<Value*> CEIdxs(CE->op_begin()+1, CE->op_end()-1); |
| 408 | Constant *NCE = ConstantExpr::getGetElementPtr(CE->getOperand(0), |
| 409 | CEIdxs); |
| 410 | GetElementPtrInst *NGEPI = |
| 411 | new GetElementPtrInst(NCE, Constant::getNullValue(Type::IntTy), |
| 412 | NewAdd, GEPI->getName(), GEPI); |
| 413 | GEPI->replaceAllUsesWith(NGEPI); |
| 414 | GEPI->eraseFromParent(); |
| 415 | GEPI = NGEPI; |
| 416 | } |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 421 | // Finally, if there are any other users of the PHI node, we must |
| 422 | // insert a new GEP instruction that uses the pre-incremented version |
| 423 | // of the induction amount. |
| 424 | if (!PN->use_empty()) { |
| 425 | BasicBlock::iterator InsertPos = PN; ++InsertPos; |
| 426 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 427 | std::string Name = PN->getName(); PN->setName(""); |
| 428 | Value *PreInc = |
| 429 | new GetElementPtrInst(PN->getIncomingValue(PreheaderIdx), |
| 430 | std::vector<Value*>(1, NewPhi), Name, |
| 431 | InsertPos); |
| 432 | PN->replaceAllUsesWith(PreInc); |
| 433 | } |
| 434 | |
| 435 | // Delete the old PHI for sure, and the GEP if its otherwise unused. |
| 436 | DeadInsts.insert(PN); |
| 437 | |
| 438 | ++NumPointer; |
| 439 | Changed = true; |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | /// LinearFunctionTestReplace - This method rewrites the exit condition of the |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 444 | /// loop to be a canonical != comparison against the incremented loop induction |
| 445 | /// variable. This pass is able to rewrite the exit tests of any loop where the |
| 446 | /// SCEV analysis can determine a loop-invariant trip count of the loop, which |
| 447 | /// is actually a much broader range than just linear tests. |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 448 | void IndVarSimplify::LinearFunctionTestReplace(Loop *L, SCEV *IterationCount, |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 449 | SCEVExpander &RW) { |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 450 | // Find the exit block for the loop. We can currently only handle loops with |
| 451 | // a single exit. |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 452 | std::vector<BasicBlock*> ExitBlocks; |
| 453 | L->getExitBlocks(ExitBlocks); |
| 454 | if (ExitBlocks.size() != 1) return; |
| 455 | BasicBlock *ExitBlock = ExitBlocks[0]; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 456 | |
| 457 | // Make sure there is only one predecessor block in the loop. |
| 458 | BasicBlock *ExitingBlock = 0; |
| 459 | for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock); |
| 460 | PI != PE; ++PI) |
| 461 | if (L->contains(*PI)) { |
| 462 | if (ExitingBlock == 0) |
| 463 | ExitingBlock = *PI; |
| 464 | else |
| 465 | return; // Multiple exits from loop to this block. |
| 466 | } |
| 467 | assert(ExitingBlock && "Loop info is broken"); |
| 468 | |
| 469 | if (!isa<BranchInst>(ExitingBlock->getTerminator())) |
| 470 | return; // Can't rewrite non-branch yet |
| 471 | BranchInst *BI = cast<BranchInst>(ExitingBlock->getTerminator()); |
| 472 | assert(BI->isConditional() && "Must be conditional to be part of loop!"); |
| 473 | |
| 474 | std::set<Instruction*> InstructionsToDelete; |
| 475 | if (Instruction *Cond = dyn_cast<Instruction>(BI->getCondition())) |
| 476 | InstructionsToDelete.insert(Cond); |
| 477 | |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 478 | // If the exiting block is not the same as the backedge block, we must compare |
| 479 | // against the preincremented value, otherwise we prefer to compare against |
| 480 | // the post-incremented value. |
| 481 | BasicBlock *Header = L->getHeader(); |
| 482 | pred_iterator HPI = pred_begin(Header); |
| 483 | assert(HPI != pred_end(Header) && "Loop with zero preds???"); |
| 484 | if (!L->contains(*HPI)) ++HPI; |
| 485 | assert(HPI != pred_end(Header) && L->contains(*HPI) && |
| 486 | "No backedge in loop?"); |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 487 | |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 488 | SCEVHandle TripCount = IterationCount; |
| 489 | Value *IndVar; |
| 490 | if (*HPI == ExitingBlock) { |
| 491 | // The IterationCount expression contains the number of times that the |
| 492 | // backedge actually branches to the loop header. This is one less than the |
| 493 | // number of times the loop executes, so add one to it. |
| 494 | Constant *OneC = ConstantInt::get(IterationCount->getType(), 1); |
| 495 | TripCount = SCEVAddExpr::get(IterationCount, SCEVUnknown::get(OneC)); |
| 496 | IndVar = L->getCanonicalInductionVariableIncrement(); |
| 497 | } else { |
| 498 | // We have to use the preincremented value... |
| 499 | IndVar = L->getCanonicalInductionVariable(); |
| 500 | } |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 501 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 502 | // Expand the code for the iteration count into the preheader of the loop. |
| 503 | BasicBlock *Preheader = L->getLoopPreheader(); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 504 | Value *ExitCnt = RW.expandCodeFor(TripCount, Preheader->getTerminator(), |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 505 | IndVar->getType()); |
| 506 | |
| 507 | // Insert a new setne or seteq instruction before the branch. |
| 508 | Instruction::BinaryOps Opcode; |
| 509 | if (L->contains(BI->getSuccessor(0))) |
| 510 | Opcode = Instruction::SetNE; |
| 511 | else |
| 512 | Opcode = Instruction::SetEQ; |
| 513 | |
| 514 | Value *Cond = new SetCondInst(Opcode, IndVar, ExitCnt, "exitcond", BI); |
| 515 | BI->setCondition(Cond); |
| 516 | ++NumLFTR; |
| 517 | Changed = true; |
| 518 | |
| 519 | DeleteTriviallyDeadInstructions(InstructionsToDelete); |
| 520 | } |
| 521 | |
| 522 | |
| 523 | /// RewriteLoopExitValues - Check to see if this loop has a computable |
| 524 | /// loop-invariant execution count. If so, this means that we can compute the |
| 525 | /// final value of any expressions that are recurrent in the loop, and |
| 526 | /// substitute the exit values from the loop into any instructions outside of |
| 527 | /// the loop that use the final values of the current expressions. |
| 528 | void IndVarSimplify::RewriteLoopExitValues(Loop *L) { |
| 529 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 530 | |
| 531 | // Scan all of the instructions in the loop, looking at those that have |
| 532 | // extra-loop users and which are recurrences. |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 533 | SCEVExpander Rewriter(*SE, *LI); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 534 | |
| 535 | // We insert the code into the preheader of the loop if the loop contains |
| 536 | // multiple exit blocks, or in the exit block if there is exactly one. |
| 537 | BasicBlock *BlockToInsertInto; |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 538 | std::vector<BasicBlock*> ExitBlocks; |
| 539 | L->getExitBlocks(ExitBlocks); |
| 540 | if (ExitBlocks.size() == 1) |
| 541 | BlockToInsertInto = ExitBlocks[0]; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 542 | else |
| 543 | BlockToInsertInto = Preheader; |
| 544 | BasicBlock::iterator InsertPt = BlockToInsertInto->begin(); |
| 545 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 546 | |
Chris Lattner | 20aa098 | 2004-04-17 18:44:09 +0000 | [diff] [blame] | 547 | bool HasConstantItCount = isa<SCEVConstant>(SE->getIterationCount(L)); |
| 548 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 549 | std::set<Instruction*> InstructionsToDelete; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 550 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 551 | for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) |
| 552 | if (LI->getLoopFor(L->getBlocks()[i]) == L) { // Not in a subloop... |
| 553 | BasicBlock *BB = L->getBlocks()[i]; |
Chris Lattner | 4bd09d7 | 2005-06-15 21:29:31 +0000 | [diff] [blame^] | 554 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) { |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 555 | if (I->getType()->isInteger()) { // Is an integer instruction |
| 556 | SCEVHandle SH = SE->getSCEV(I); |
Chris Lattner | 20aa098 | 2004-04-17 18:44:09 +0000 | [diff] [blame] | 557 | if (SH->hasComputableLoopEvolution(L) || // Varies predictably |
| 558 | HasConstantItCount) { |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 559 | // Find out if this predictably varying value is actually used |
| 560 | // outside of the loop. "extra" as opposed to "intra". |
| 561 | std::vector<User*> ExtraLoopUsers; |
| 562 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
| 563 | UI != E; ++UI) |
| 564 | if (!L->contains(cast<Instruction>(*UI)->getParent())) |
| 565 | ExtraLoopUsers.push_back(*UI); |
| 566 | if (!ExtraLoopUsers.empty()) { |
| 567 | // Okay, this instruction has a user outside of the current loop |
| 568 | // and varies predictably in this loop. Evaluate the value it |
| 569 | // contains when the loop exits, and insert code for it. |
Chris Lattner | 20aa098 | 2004-04-17 18:44:09 +0000 | [diff] [blame] | 570 | SCEVHandle ExitValue = SE->getSCEVAtScope(I, L->getParentLoop()); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 571 | if (!isa<SCEVCouldNotCompute>(ExitValue)) { |
| 572 | Changed = true; |
| 573 | ++NumReplaced; |
Chris Lattner | 4bd09d7 | 2005-06-15 21:29:31 +0000 | [diff] [blame^] | 574 | // Remember the next instruction. The rewriter can move code |
| 575 | // around in some cases. |
| 576 | BasicBlock::iterator NextI = I; ++NextI; |
| 577 | |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 578 | Value *NewVal = Rewriter.expandCodeFor(ExitValue, InsertPt, |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 579 | I->getType()); |
| 580 | |
| 581 | // Rewrite any users of the computed value outside of the loop |
| 582 | // with the newly computed value. |
| 583 | for (unsigned i = 0, e = ExtraLoopUsers.size(); i != e; ++i) |
| 584 | ExtraLoopUsers[i]->replaceUsesOfWith(I, NewVal); |
| 585 | |
| 586 | // If this instruction is dead now, schedule it to be removed. |
| 587 | if (I->use_empty()) |
| 588 | InstructionsToDelete.insert(I); |
Chris Lattner | 4bd09d7 | 2005-06-15 21:29:31 +0000 | [diff] [blame^] | 589 | I = NextI; |
| 590 | continue; // Skip the ++I |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | } |
| 594 | } |
Chris Lattner | 4bd09d7 | 2005-06-15 21:29:31 +0000 | [diff] [blame^] | 595 | |
| 596 | // Next instruction. Continue instruction skips this. |
| 597 | ++I; |
| 598 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | DeleteTriviallyDeadInstructions(InstructionsToDelete); |
| 602 | } |
| 603 | |
| 604 | |
| 605 | void IndVarSimplify::runOnLoop(Loop *L) { |
| 606 | // First step. Check to see if there are any trivial GEP pointer recurrences. |
| 607 | // If there are, change them into integer recurrences, permitting analysis by |
| 608 | // the SCEV routines. |
| 609 | // |
| 610 | BasicBlock *Header = L->getHeader(); |
| 611 | BasicBlock *Preheader = L->getLoopPreheader(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 612 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 613 | std::set<Instruction*> DeadInsts; |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 614 | for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { |
| 615 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 616 | if (isa<PointerType>(PN->getType())) |
| 617 | EliminatePointerRecurrence(PN, Preheader, DeadInsts); |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 618 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 619 | |
| 620 | if (!DeadInsts.empty()) |
| 621 | DeleteTriviallyDeadInstructions(DeadInsts); |
| 622 | |
| 623 | |
| 624 | // Next, transform all loops nesting inside of this loop. |
| 625 | for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I) |
Chris Lattner | 329c1c6 | 2004-01-08 00:09:44 +0000 | [diff] [blame] | 626 | runOnLoop(*I); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 627 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 628 | // Check to see if this loop has a computable loop-invariant execution count. |
| 629 | // If so, this means that we can compute the final value of any expressions |
| 630 | // that are recurrent in the loop, and substitute the exit values from the |
| 631 | // loop into any instructions outside of the loop that use the final values of |
| 632 | // the current expressions. |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 633 | // |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 634 | SCEVHandle IterationCount = SE->getIterationCount(L); |
| 635 | if (!isa<SCEVCouldNotCompute>(IterationCount)) |
| 636 | RewriteLoopExitValues(L); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 637 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 638 | // Next, analyze all of the induction variables in the loop, canonicalizing |
| 639 | // auxillary induction variables. |
| 640 | std::vector<std::pair<PHINode*, SCEVHandle> > IndVars; |
| 641 | |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 642 | for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { |
| 643 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 644 | if (PN->getType()->isInteger()) { // FIXME: when we have fast-math, enable! |
| 645 | SCEVHandle SCEV = SE->getSCEV(PN); |
| 646 | if (SCEV->hasComputableLoopEvolution(L)) |
Chris Lattner | a25502a | 2004-06-24 06:49:18 +0000 | [diff] [blame] | 647 | // FIXME: Without a strength reduction pass, it is an extremely bad idea |
| 648 | // to indvar substitute anything more complex than a linear induction |
| 649 | // variable. Doing so will put expensive multiply instructions inside |
| 650 | // of the loop. For now just disable indvar subst on anything more |
| 651 | // complex than a linear addrec. |
Chris Lattner | 595ee7e | 2004-07-26 02:47:12 +0000 | [diff] [blame] | 652 | if (SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(SCEV)) |
| 653 | if (AR->getNumOperands() == 2 && isa<SCEVConstant>(AR->getOperand(1))) |
| 654 | IndVars.push_back(std::make_pair(PN, SCEV)); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 655 | } |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 656 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 657 | |
| 658 | // If there are no induction variables in the loop, there is nothing more to |
| 659 | // do. |
Chris Lattner | f50af08 | 2004-04-17 18:08:33 +0000 | [diff] [blame] | 660 | if (IndVars.empty()) { |
| 661 | // Actually, if we know how many times the loop iterates, lets insert a |
| 662 | // canonical induction variable to help subsequent passes. |
| 663 | if (!isa<SCEVCouldNotCompute>(IterationCount)) { |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 664 | SCEVExpander Rewriter(*SE, *LI); |
| 665 | Rewriter.getOrInsertCanonicalInductionVariable(L, |
Chris Lattner | f50af08 | 2004-04-17 18:08:33 +0000 | [diff] [blame] | 666 | IterationCount->getType()); |
| 667 | LinearFunctionTestReplace(L, IterationCount, Rewriter); |
| 668 | } |
| 669 | return; |
| 670 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 671 | |
| 672 | // Compute the type of the largest recurrence expression. |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 673 | // |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 674 | const Type *LargestType = IndVars[0].first->getType(); |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 675 | bool DifferingSizes = false; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 676 | for (unsigned i = 1, e = IndVars.size(); i != e; ++i) { |
| 677 | const Type *Ty = IndVars[i].first->getType(); |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 678 | DifferingSizes |= Ty->getPrimitiveSize() != LargestType->getPrimitiveSize(); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 679 | if (Ty->getPrimitiveSize() > LargestType->getPrimitiveSize()) |
| 680 | LargestType = Ty; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 683 | // Create a rewriter object which we'll use to transform the code with. |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 684 | SCEVExpander Rewriter(*SE, *LI); |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 685 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 686 | // Now that we know the largest of of the induction variables in this loop, |
| 687 | // insert a canonical induction variable of the largest size. |
Chris Lattner | 006118f | 2004-04-16 06:03:17 +0000 | [diff] [blame] | 688 | LargestType = LargestType->getUnsignedVersion(); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 689 | Value *IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L,LargestType); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 690 | ++NumInserted; |
| 691 | Changed = true; |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 692 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 693 | if (!isa<SCEVCouldNotCompute>(IterationCount)) |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 694 | LinearFunctionTestReplace(L, IterationCount, Rewriter); |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 695 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 696 | // Now that we have a canonical induction variable, we can rewrite any |
| 697 | // recurrences in terms of the induction variable. Start with the auxillary |
| 698 | // induction variables, and recursively rewrite any of their uses. |
| 699 | BasicBlock::iterator InsertPt = Header->begin(); |
| 700 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 5d461d2 | 2004-04-21 22:22:01 +0000 | [diff] [blame] | 702 | // If there were induction variables of other sizes, cast the primary |
| 703 | // induction variable to the right size for them, avoiding the need for the |
| 704 | // code evaluation methods to insert induction variables of different sizes. |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 705 | if (DifferingSizes) { |
| 706 | bool InsertedSizes[17] = { false }; |
| 707 | InsertedSizes[LargestType->getPrimitiveSize()] = true; |
| 708 | for (unsigned i = 0, e = IndVars.size(); i != e; ++i) |
| 709 | if (!InsertedSizes[IndVars[i].first->getType()->getPrimitiveSize()]) { |
| 710 | PHINode *PN = IndVars[i].first; |
| 711 | InsertedSizes[PN->getType()->getPrimitiveSize()] = true; |
| 712 | Instruction *New = new CastInst(IndVar, |
| 713 | PN->getType()->getUnsignedVersion(), |
| 714 | "indvar", InsertPt); |
| 715 | Rewriter.addInsertedValue(New, SE->getSCEV(New)); |
| 716 | } |
| 717 | } |
| 718 | |
| 719 | // If there were induction variables of other sizes, cast the primary |
| 720 | // induction variable to the right size for them, avoiding the need for the |
| 721 | // code evaluation methods to insert induction variables of different sizes. |
Chris Lattner | 5d461d2 | 2004-04-21 22:22:01 +0000 | [diff] [blame] | 722 | std::map<unsigned, Value*> InsertedSizes; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 723 | while (!IndVars.empty()) { |
| 724 | PHINode *PN = IndVars.back().first; |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 725 | Value *NewVal = Rewriter.expandCodeFor(IndVars.back().second, InsertPt, |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 726 | PN->getType()); |
| 727 | std::string Name = PN->getName(); |
| 728 | PN->setName(""); |
| 729 | NewVal->setName(Name); |
Chris Lattner | 5d461d2 | 2004-04-21 22:22:01 +0000 | [diff] [blame] | 730 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 731 | // Replace the old PHI Node with the inserted computation. |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 732 | PN->replaceAllUsesWith(NewVal); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 733 | DeadInsts.insert(PN); |
| 734 | IndVars.pop_back(); |
| 735 | ++NumRemoved; |
Chris Lattner | 4753bf2 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 736 | Changed = true; |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 737 | } |
| 738 | |
Chris Lattner | b4782d1 | 2004-04-22 15:12:36 +0000 | [diff] [blame] | 739 | #if 0 |
Chris Lattner | 1363e85 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 740 | // Now replace all derived expressions in the loop body with simpler |
| 741 | // expressions. |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 742 | for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) |
| 743 | if (LI->getLoopFor(L->getBlocks()[i]) == L) { // Not in a subloop... |
| 744 | BasicBlock *BB = L->getBlocks()[i]; |
| 745 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
| 746 | if (I->getType()->isInteger() && // Is an integer instruction |
Chris Lattner | 1363e85 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 747 | !I->use_empty() && |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 748 | !Rewriter.isInsertedInstruction(I)) { |
| 749 | SCEVHandle SH = SE->getSCEV(I); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 750 | Value *V = Rewriter.expandCodeFor(SH, I, I->getType()); |
Chris Lattner | 1363e85 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 751 | if (V != I) { |
| 752 | if (isa<Instruction>(V)) { |
| 753 | std::string Name = I->getName(); |
| 754 | I->setName(""); |
| 755 | V->setName(Name); |
| 756 | } |
| 757 | I->replaceAllUsesWith(V); |
| 758 | DeadInsts.insert(I); |
| 759 | ++NumRemoved; |
| 760 | Changed = true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 761 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 762 | } |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 763 | } |
Chris Lattner | b4782d1 | 2004-04-22 15:12:36 +0000 | [diff] [blame] | 764 | #endif |
Chris Lattner | 1363e85 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 765 | |
Chris Lattner | 1363e85 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 766 | DeleteTriviallyDeadInstructions(DeadInsts); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 767 | } |