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