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 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // 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 | // |
Reid Spencer | 47a53ac | 2006-08-18 09:01:07 +0000 | [diff] [blame] | 14 | // This transformation makes the following changes to each loop with an |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 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. |
Dan Gohman | ea73f3c | 2009-06-14 22:38:41 +0000 | [diff] [blame] | 20 | // 3. The canonical induction variable is guaranteed to be in a wide enough |
| 21 | // type so that IV expressions need not be (directly) zero-extended or |
| 22 | // sign-extended. |
| 23 | // 4. Any pointer arithmetic recurrences are raised to use array subscripts. |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 24 | // |
| 25 | // If the trip count of a loop is computable, this pass also makes the following |
| 26 | // changes: |
| 27 | // 1. The exit condition for the loop is canonicalized to compare the |
| 28 | // induction value against the exit value. This turns loops like: |
| 29 | // 'for (i = 7; i*i < 1000; ++i)' into 'for (i = 0; i != 25; ++i)' |
| 30 | // 2. Any use outside of the loop of an expression derived from the indvar |
| 31 | // is changed to compute the derived value outside of the loop, eliminating |
| 32 | // the dependence on the exit value of the induction variable. If the only |
| 33 | // purpose of the loop is to compute the exit value of some derived |
| 34 | // expression, this transformation will make the loop dead. |
| 35 | // |
| 36 | // This transformation should be followed by strength reduction after all of the |
Dan Gohman | c2c4cbf | 2009-05-19 20:38:47 +0000 | [diff] [blame] | 37 | // desired loop transformations have been performed. |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 38 | // |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 41 | #define DEBUG_TYPE "indvars" |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 42 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 43 | #include "llvm/BasicBlock.h" |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 44 | #include "llvm/Constants.h" |
Chris Lattner | 18b3c97 | 2003-12-22 05:02:01 +0000 | [diff] [blame] | 45 | #include "llvm/Instructions.h" |
Devang Patel | 7b9f6b1 | 2010-03-15 22:23:03 +0000 | [diff] [blame] | 46 | #include "llvm/IntrinsicInst.h" |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 47 | #include "llvm/LLVMContext.h" |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 48 | #include "llvm/Type.h" |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 49 | #include "llvm/Analysis/Dominators.h" |
| 50 | #include "llvm/Analysis/IVUsers.h" |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 51 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 52 | #include "llvm/Analysis/LoopInfo.h" |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 53 | #include "llvm/Analysis/LoopPass.h" |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 54 | #include "llvm/Support/CFG.h" |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 55 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 56 | #include "llvm/Support/Debug.h" |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 57 | #include "llvm/Support/raw_ostream.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 58 | #include "llvm/Transforms/Utils/Local.h" |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 59 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 60 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 62 | #include "llvm/ADT/STLExtras.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 63 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 65 | STATISTIC(NumRemoved , "Number of aux indvars removed"); |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 66 | STATISTIC(NumInserted, "Number of canonical indvars added"); |
| 67 | STATISTIC(NumReplaced, "Number of exit values replaced"); |
| 68 | STATISTIC(NumLFTR , "Number of loop exit tests replaced"); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 70 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 71 | class IndVarSimplify : public LoopPass { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 72 | IVUsers *IU; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 73 | LoopInfo *LI; |
| 74 | ScalarEvolution *SE; |
Dan Gohman | de53dc0 | 2009-06-27 05:16:57 +0000 | [diff] [blame] | 75 | DominatorTree *DT; |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 76 | SmallVector<WeakVH, 16> DeadInsts; |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 77 | bool Changed; |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 78 | public: |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 79 | |
Dan Gohman | 5668cf7 | 2009-07-15 01:26:32 +0000 | [diff] [blame] | 80 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 81 | IndVarSimplify() : LoopPass(ID) { |
| 82 | initializeIndVarSimplifyPass(*PassRegistry::getPassRegistry()); |
| 83 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 84 | |
Dan Gohman | 5668cf7 | 2009-07-15 01:26:32 +0000 | [diff] [blame] | 85 | virtual bool runOnLoop(Loop *L, LPPassManager &LPM); |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 86 | |
Dan Gohman | 5668cf7 | 2009-07-15 01:26:32 +0000 | [diff] [blame] | 87 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 88 | AU.addRequired<DominatorTree>(); |
| 89 | AU.addRequired<LoopInfo>(); |
| 90 | AU.addRequired<ScalarEvolution>(); |
| 91 | AU.addRequiredID(LoopSimplifyID); |
| 92 | AU.addRequiredID(LCSSAID); |
| 93 | AU.addRequired<IVUsers>(); |
| 94 | AU.addPreserved<ScalarEvolution>(); |
| 95 | AU.addPreservedID(LoopSimplifyID); |
| 96 | AU.addPreservedID(LCSSAID); |
| 97 | AU.addPreserved<IVUsers>(); |
| 98 | AU.setPreservesCFG(); |
| 99 | } |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 101 | private: |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 102 | bool isValidRewrite(Value *FromVal, Value *ToVal); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 103 | |
Dan Gohman | 931e345 | 2010-04-12 02:21:50 +0000 | [diff] [blame] | 104 | void EliminateIVComparisons(); |
Dan Gohman | a590b79 | 2010-04-13 01:46:36 +0000 | [diff] [blame] | 105 | void EliminateIVRemainders(); |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 106 | void RewriteNonIntegerIVs(Loop *L); |
| 107 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 108 | ICmpInst *LinearFunctionTestReplace(Loop *L, const SCEV *BackedgeTakenCount, |
Dan Gohman | 43ef3fb | 2010-07-20 17:18:52 +0000 | [diff] [blame] | 109 | PHINode *IndVar, |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 110 | BasicBlock *ExitingBlock, |
| 111 | BranchInst *BI, |
Dan Gohman | 15cab28 | 2009-02-23 23:20:35 +0000 | [diff] [blame] | 112 | SCEVExpander &Rewriter); |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 113 | void RewriteLoopExitValues(Loop *L, SCEVExpander &Rewriter); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 114 | |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 115 | void RewriteIVExpressions(Loop *L, SCEVExpander &Rewriter); |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 116 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 117 | void SinkUnusedInvariants(Loop *L); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 118 | |
| 119 | void HandleFloatingPointIV(Loop *L, PHINode *PH); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 120 | }; |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 121 | } |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 122 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 123 | char IndVarSimplify::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 124 | INITIALIZE_PASS_BEGIN(IndVarSimplify, "indvars", |
| 125 | "Canonicalize Induction Variables", false, false) |
| 126 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 127 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 128 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 129 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 130 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
| 131 | INITIALIZE_PASS_DEPENDENCY(IVUsers) |
| 132 | INITIALIZE_PASS_END(IndVarSimplify, "indvars", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 133 | "Canonicalize Induction Variables", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 134 | |
Daniel Dunbar | 394f044 | 2008-10-22 23:32:42 +0000 | [diff] [blame] | 135 | Pass *llvm::createIndVarSimplifyPass() { |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 136 | return new IndVarSimplify(); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 139 | /// isValidRewrite - Return true if the SCEV expansion generated by the |
| 140 | /// rewriter can replace the original value. SCEV guarantees that it |
| 141 | /// produces the same value, but the way it is produced may be illegal IR. |
| 142 | /// Ideally, this function will only be called for verification. |
| 143 | bool IndVarSimplify::isValidRewrite(Value *FromVal, Value *ToVal) { |
| 144 | // If an SCEV expression subsumed multiple pointers, its expansion could |
| 145 | // reassociate the GEP changing the base pointer. This is illegal because the |
| 146 | // final address produced by a GEP chain must be inbounds relative to its |
| 147 | // underlying object. Otherwise basic alias analysis, among other things, |
| 148 | // could fail in a dangerous way. Ultimately, SCEV will be improved to avoid |
| 149 | // producing an expression involving multiple pointers. Until then, we must |
| 150 | // bail out here. |
| 151 | // |
| 152 | // Retrieve the pointer operand of the GEP. Don't use GetUnderlyingObject |
| 153 | // because it understands lcssa phis while SCEV does not. |
| 154 | Value *FromPtr = FromVal; |
| 155 | Value *ToPtr = ToVal; |
| 156 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(FromVal)) { |
| 157 | FromPtr = GEP->getPointerOperand(); |
| 158 | } |
| 159 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(ToVal)) { |
| 160 | ToPtr = GEP->getPointerOperand(); |
| 161 | } |
| 162 | if (FromPtr != FromVal || ToPtr != ToVal) { |
| 163 | // Quickly check the common case |
| 164 | if (FromPtr == ToPtr) |
| 165 | return true; |
| 166 | |
| 167 | // SCEV may have rewritten an expression that produces the GEP's pointer |
| 168 | // operand. That's ok as long as the pointer operand has the same base |
| 169 | // pointer. Unlike GetUnderlyingObject(), getPointerBase() will find the |
| 170 | // base of a recurrence. This handles the case in which SCEV expansion |
| 171 | // converts a pointer type recurrence into a nonrecurrent pointer base |
| 172 | // indexed by an integer recurrence. |
| 173 | const SCEV *FromBase = SE->getPointerBase(SE->getSCEV(FromPtr)); |
| 174 | const SCEV *ToBase = SE->getPointerBase(SE->getSCEV(ToPtr)); |
| 175 | if (FromBase == ToBase) |
| 176 | return true; |
| 177 | |
| 178 | DEBUG(dbgs() << "INDVARS: GEP rewrite bail out " |
| 179 | << *FromBase << " != " << *ToBase << "\n"); |
| 180 | |
| 181 | return false; |
| 182 | } |
| 183 | return true; |
| 184 | } |
| 185 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 186 | /// LinearFunctionTestReplace - This method rewrites the exit condition of the |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 187 | /// loop to be a canonical != comparison against the incremented loop induction |
| 188 | /// variable. This pass is able to rewrite the exit tests of any loop where the |
| 189 | /// SCEV analysis can determine a loop-invariant trip count of the loop, which |
| 190 | /// is actually a much broader range than just linear tests. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 191 | ICmpInst *IndVarSimplify::LinearFunctionTestReplace(Loop *L, |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 192 | const SCEV *BackedgeTakenCount, |
Dan Gohman | 43ef3fb | 2010-07-20 17:18:52 +0000 | [diff] [blame] | 193 | PHINode *IndVar, |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 194 | BasicBlock *ExitingBlock, |
| 195 | BranchInst *BI, |
Dan Gohman | 15cab28 | 2009-02-23 23:20:35 +0000 | [diff] [blame] | 196 | SCEVExpander &Rewriter) { |
Dan Gohman | ca9b703 | 2010-04-12 21:13:43 +0000 | [diff] [blame] | 197 | // Special case: If the backedge-taken count is a UDiv, it's very likely a |
| 198 | // UDiv that ScalarEvolution produced in order to compute a precise |
| 199 | // expression, rather than a UDiv from the user's code. If we can't find a |
| 200 | // UDiv in the code with some simple searching, assume the former and forego |
| 201 | // rewriting the loop. |
| 202 | if (isa<SCEVUDivExpr>(BackedgeTakenCount)) { |
| 203 | ICmpInst *OrigCond = dyn_cast<ICmpInst>(BI->getCondition()); |
| 204 | if (!OrigCond) return 0; |
| 205 | const SCEV *R = SE->getSCEV(OrigCond->getOperand(1)); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 206 | R = SE->getMinusSCEV(R, SE->getConstant(R->getType(), 1)); |
Dan Gohman | ca9b703 | 2010-04-12 21:13:43 +0000 | [diff] [blame] | 207 | if (R != BackedgeTakenCount) { |
| 208 | const SCEV *L = SE->getSCEV(OrigCond->getOperand(0)); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 209 | L = SE->getMinusSCEV(L, SE->getConstant(L->getType(), 1)); |
Dan Gohman | ca9b703 | 2010-04-12 21:13:43 +0000 | [diff] [blame] | 210 | if (L != BackedgeTakenCount) |
| 211 | return 0; |
| 212 | } |
| 213 | } |
| 214 | |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 215 | // If the exiting block is not the same as the backedge block, we must compare |
| 216 | // against the preincremented value, otherwise we prefer to compare against |
| 217 | // the post-incremented value. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 218 | Value *CmpIndVar; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 219 | const SCEV *RHS = BackedgeTakenCount; |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 220 | if (ExitingBlock == L->getLoopLatch()) { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 221 | // Add one to the "backedge-taken" count to get the trip count. |
| 222 | // If this addition may overflow, we have to be more pessimistic and |
| 223 | // cast the induction variable before doing the add. |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 224 | const SCEV *Zero = SE->getConstant(BackedgeTakenCount->getType(), 0); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 225 | const SCEV *N = |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 226 | SE->getAddExpr(BackedgeTakenCount, |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 227 | SE->getConstant(BackedgeTakenCount->getType(), 1)); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 228 | if ((isa<SCEVConstant>(N) && !N->isZero()) || |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 229 | SE->isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) { |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 230 | // No overflow. Cast the sum. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 231 | RHS = SE->getTruncateOrZeroExtend(N, IndVar->getType()); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 232 | } else { |
| 233 | // Potential overflow. Cast before doing the add. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 234 | RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount, |
| 235 | IndVar->getType()); |
| 236 | RHS = SE->getAddExpr(RHS, |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 237 | SE->getConstant(IndVar->getType(), 1)); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 238 | } |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 239 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 240 | // The BackedgeTaken expression contains the number of times that the |
| 241 | // backedge branches to the loop header. This is one less than the |
| 242 | // number of times the loop executes, so use the incremented indvar. |
Dan Gohman | 43ef3fb | 2010-07-20 17:18:52 +0000 | [diff] [blame] | 243 | CmpIndVar = IndVar->getIncomingValueForBlock(ExitingBlock); |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 244 | } else { |
| 245 | // We have to use the preincremented value... |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 246 | RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount, |
| 247 | IndVar->getType()); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 248 | CmpIndVar = IndVar; |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 249 | } |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 250 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 251 | // Expand the code for the iteration count. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 252 | assert(SE->isLoopInvariant(RHS, L) && |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 253 | "Computed iteration count is not loop invariant!"); |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 254 | Value *ExitCnt = Rewriter.expandCodeFor(RHS, IndVar->getType(), BI); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 255 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 256 | // Insert a new icmp_ne or icmp_eq instruction before the branch. |
| 257 | ICmpInst::Predicate Opcode; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 258 | if (L->contains(BI->getSuccessor(0))) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 259 | Opcode = ICmpInst::ICMP_NE; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 260 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 261 | Opcode = ICmpInst::ICMP_EQ; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 262 | |
David Greene | f67ef31 | 2010-01-05 01:27:06 +0000 | [diff] [blame] | 263 | DEBUG(dbgs() << "INDVARS: Rewriting loop exit condition to:\n" |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 264 | << " LHS:" << *CmpIndVar << '\n' |
| 265 | << " op:\t" |
| 266 | << (Opcode == ICmpInst::ICMP_NE ? "!=" : "==") << "\n" |
| 267 | << " RHS:\t" << *RHS << "\n"); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 268 | |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 269 | ICmpInst *Cond = new ICmpInst(BI, Opcode, CmpIndVar, ExitCnt, "exitcond"); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 270 | |
Dan Gohman | 2444080 | 2010-02-22 02:07:36 +0000 | [diff] [blame] | 271 | Value *OrigCond = BI->getCondition(); |
Dan Gohman | 95bdbfa | 2009-05-24 19:11:38 +0000 | [diff] [blame] | 272 | // It's tempting to use replaceAllUsesWith here to fully replace the old |
| 273 | // comparison, but that's not immediately safe, since users of the old |
| 274 | // comparison may not be dominated by the new comparison. Instead, just |
| 275 | // update the branch to use the new comparison; in the common case this |
| 276 | // will make old comparison dead. |
| 277 | BI->setCondition(Cond); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 278 | RecursivelyDeleteTriviallyDeadInstructions(OrigCond); |
| 279 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 280 | ++NumLFTR; |
| 281 | Changed = true; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 282 | return Cond; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 285 | /// RewriteLoopExitValues - Check to see if this loop has a computable |
| 286 | /// loop-invariant execution count. If so, this means that we can compute the |
| 287 | /// final value of any expressions that are recurrent in the loop, and |
| 288 | /// substitute the exit values from the loop into any instructions outside of |
| 289 | /// the loop that use the final values of the current expressions. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 290 | /// |
| 291 | /// This is mostly redundant with the regular IndVarSimplify activities that |
| 292 | /// happen later, except that it's more powerful in some cases, because it's |
| 293 | /// able to brute-force evaluate arbitrary instructions as long as they have |
| 294 | /// constant operands at the beginning of the loop. |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 295 | void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEVExpander &Rewriter) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 296 | // Verify the input to the pass in already in LCSSA form. |
Dan Gohman | bbf81d8 | 2010-03-10 19:38:49 +0000 | [diff] [blame] | 297 | assert(L->isLCSSAForm(*DT)); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 298 | |
Devang Patel | b7211a2 | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 299 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 300 | L->getUniqueExitBlocks(ExitBlocks); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 301 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 302 | // Find all values that are computed inside the loop, but used outside of it. |
| 303 | // Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan |
| 304 | // the exit blocks of the loop to find them. |
| 305 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
| 306 | BasicBlock *ExitBB = ExitBlocks[i]; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 308 | // If there are no PHI nodes in this exit block, then no values defined |
| 309 | // inside the loop are used on this path, skip it. |
| 310 | PHINode *PN = dyn_cast<PHINode>(ExitBB->begin()); |
| 311 | if (!PN) continue; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 312 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 313 | unsigned NumPreds = PN->getNumIncomingValues(); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 314 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 315 | // Iterate over all of the PHI nodes. |
| 316 | BasicBlock::iterator BBI = ExitBB->begin(); |
| 317 | while ((PN = dyn_cast<PHINode>(BBI++))) { |
Torok Edwin | 3790fb0 | 2009-05-24 19:36:09 +0000 | [diff] [blame] | 318 | if (PN->use_empty()) |
| 319 | continue; // dead use, don't replace it |
Dan Gohman | 814f2b2 | 2010-02-18 21:34:02 +0000 | [diff] [blame] | 320 | |
| 321 | // SCEV only supports integer expressions for now. |
| 322 | if (!PN->getType()->isIntegerTy() && !PN->getType()->isPointerTy()) |
| 323 | continue; |
| 324 | |
Dale Johannesen | 45a2d7d | 2010-02-19 07:14:22 +0000 | [diff] [blame] | 325 | // It's necessary to tell ScalarEvolution about this explicitly so that |
| 326 | // it can walk the def-use list and forget all SCEVs, as it may not be |
| 327 | // watching the PHI itself. Once the new exit value is in place, there |
| 328 | // may not be a def-use connection between the loop and every instruction |
| 329 | // which got a SCEVAddRecExpr for that loop. |
| 330 | SE->forgetValue(PN); |
| 331 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 332 | // Iterate over all of the values in all the PHI nodes. |
| 333 | for (unsigned i = 0; i != NumPreds; ++i) { |
| 334 | // If the value being merged in is not integer or is not defined |
| 335 | // in the loop, skip it. |
| 336 | Value *InVal = PN->getIncomingValue(i); |
Dan Gohman | 814f2b2 | 2010-02-18 21:34:02 +0000 | [diff] [blame] | 337 | if (!isa<Instruction>(InVal)) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 338 | continue; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 340 | // If this pred is for a subloop, not L itself, skip it. |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 341 | if (LI->getLoopFor(PN->getIncomingBlock(i)) != L) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 342 | continue; // The Block is in a subloop, skip it. |
| 343 | |
| 344 | // Check that InVal is defined in the loop. |
| 345 | Instruction *Inst = cast<Instruction>(InVal); |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 346 | if (!L->contains(Inst)) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 347 | continue; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 348 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 349 | // Okay, this instruction has a user outside of the current loop |
| 350 | // and varies predictably *inside* the loop. Evaluate the value it |
| 351 | // contains when the loop exits, if possible. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 352 | const SCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop()); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 353 | if (!SE->isLoopInvariant(ExitValue, L)) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 354 | continue; |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 355 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 356 | Value *ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), Inst); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 357 | |
David Greene | f67ef31 | 2010-01-05 01:27:06 +0000 | [diff] [blame] | 358 | DEBUG(dbgs() << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal << '\n' |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 359 | << " LoopVal = " << *Inst << "\n"); |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 360 | |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 361 | if (!isValidRewrite(Inst, ExitVal)) { |
| 362 | DeadInsts.push_back(ExitVal); |
| 363 | continue; |
| 364 | } |
| 365 | Changed = true; |
| 366 | ++NumReplaced; |
| 367 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 368 | PN->setIncomingValue(i, ExitVal); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 369 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 370 | // If this instruction is dead now, delete it. |
| 371 | RecursivelyDeleteTriviallyDeadInstructions(Inst); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 372 | |
Dan Gohman | 65d1e2b | 2009-07-14 01:09:02 +0000 | [diff] [blame] | 373 | if (NumPreds == 1) { |
| 374 | // Completely replace a single-pred PHI. This is safe, because the |
| 375 | // NewVal won't be variant in the loop, so we don't need an LCSSA phi |
| 376 | // node anymore. |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 377 | PN->replaceAllUsesWith(ExitVal); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 378 | RecursivelyDeleteTriviallyDeadInstructions(PN); |
Chris Lattner | c9838f2 | 2007-03-03 22:48:48 +0000 | [diff] [blame] | 379 | } |
| 380 | } |
Dan Gohman | 65d1e2b | 2009-07-14 01:09:02 +0000 | [diff] [blame] | 381 | if (NumPreds != 1) { |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 382 | // Clone the PHI and delete the original one. This lets IVUsers and |
| 383 | // any other maps purge the original user from their records. |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 384 | PHINode *NewPN = cast<PHINode>(PN->clone()); |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 385 | NewPN->takeName(PN); |
| 386 | NewPN->insertBefore(PN); |
| 387 | PN->replaceAllUsesWith(NewPN); |
| 388 | PN->eraseFromParent(); |
| 389 | } |
Chris Lattner | c9838f2 | 2007-03-03 22:48:48 +0000 | [diff] [blame] | 390 | } |
| 391 | } |
Dan Gohman | 472fdf7 | 2010-03-20 03:53:53 +0000 | [diff] [blame] | 392 | |
| 393 | // The insertion point instruction may have been deleted; clear it out |
| 394 | // so that the rewriter doesn't trip over it later. |
| 395 | Rewriter.clearInsertPoint(); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 398 | void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 399 | // First step. Check to see if there are any floating-point recurrences. |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 400 | // If there are, change them into integer recurrences, permitting analysis by |
| 401 | // the SCEV routines. |
| 402 | // |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 403 | BasicBlock *Header = L->getHeader(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 404 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 405 | SmallVector<WeakVH, 8> PHIs; |
| 406 | for (BasicBlock::iterator I = Header->begin(); |
| 407 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 408 | PHIs.push_back(PN); |
| 409 | |
| 410 | for (unsigned i = 0, e = PHIs.size(); i != e; ++i) |
Gabor Greif | ea4894a | 2010-09-18 11:53:39 +0000 | [diff] [blame] | 411 | if (PHINode *PN = dyn_cast_or_null<PHINode>(&*PHIs[i])) |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 412 | HandleFloatingPointIV(L, PN); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 413 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 414 | // If the loop previously had floating-point IV, ScalarEvolution |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 415 | // may not have been able to compute a trip count. Now that we've done some |
| 416 | // re-writing, the trip count may be computable. |
| 417 | if (Changed) |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 418 | SE->forgetLoop(L); |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Dan Gohman | 931e345 | 2010-04-12 02:21:50 +0000 | [diff] [blame] | 421 | void IndVarSimplify::EliminateIVComparisons() { |
| 422 | // Look for ICmp users. |
Dan Gohman | dd842e3 | 2010-04-12 07:29:15 +0000 | [diff] [blame] | 423 | for (IVUsers::iterator I = IU->begin(), E = IU->end(); I != E; ++I) { |
| 424 | IVStrideUse &UI = *I; |
Dan Gohman | 931e345 | 2010-04-12 02:21:50 +0000 | [diff] [blame] | 425 | ICmpInst *ICmp = dyn_cast<ICmpInst>(UI.getUser()); |
| 426 | if (!ICmp) continue; |
| 427 | |
| 428 | bool Swapped = UI.getOperandValToReplace() == ICmp->getOperand(1); |
| 429 | ICmpInst::Predicate Pred = ICmp->getPredicate(); |
| 430 | if (Swapped) Pred = ICmpInst::getSwappedPredicate(Pred); |
| 431 | |
| 432 | // Get the SCEVs for the ICmp operands. |
| 433 | const SCEV *S = IU->getReplacementExpr(UI); |
| 434 | const SCEV *X = SE->getSCEV(ICmp->getOperand(!Swapped)); |
| 435 | |
| 436 | // Simplify unnecessary loops away. |
| 437 | const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent()); |
| 438 | S = SE->getSCEVAtScope(S, ICmpLoop); |
| 439 | X = SE->getSCEVAtScope(X, ICmpLoop); |
| 440 | |
| 441 | // If the condition is always true or always false, replace it with |
| 442 | // a constant value. |
| 443 | if (SE->isKnownPredicate(Pred, S, X)) |
| 444 | ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext())); |
| 445 | else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X)) |
| 446 | ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext())); |
| 447 | else |
| 448 | continue; |
| 449 | |
| 450 | DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n'); |
Dan Gohman | dd842e3 | 2010-04-12 07:29:15 +0000 | [diff] [blame] | 451 | DeadInsts.push_back(ICmp); |
Dan Gohman | 931e345 | 2010-04-12 02:21:50 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Dan Gohman | a590b79 | 2010-04-13 01:46:36 +0000 | [diff] [blame] | 455 | void IndVarSimplify::EliminateIVRemainders() { |
Dan Gohman | a590b79 | 2010-04-13 01:46:36 +0000 | [diff] [blame] | 456 | // Look for SRem and URem users. |
| 457 | for (IVUsers::iterator I = IU->begin(), E = IU->end(); I != E; ++I) { |
| 458 | IVStrideUse &UI = *I; |
| 459 | BinaryOperator *Rem = dyn_cast<BinaryOperator>(UI.getUser()); |
| 460 | if (!Rem) continue; |
| 461 | |
| 462 | bool isSigned = Rem->getOpcode() == Instruction::SRem; |
| 463 | if (!isSigned && Rem->getOpcode() != Instruction::URem) |
| 464 | continue; |
| 465 | |
| 466 | // We're only interested in the case where we know something about |
| 467 | // the numerator. |
| 468 | if (UI.getOperandValToReplace() != Rem->getOperand(0)) |
| 469 | continue; |
| 470 | |
| 471 | // Get the SCEVs for the ICmp operands. |
| 472 | const SCEV *S = SE->getSCEV(Rem->getOperand(0)); |
| 473 | const SCEV *X = SE->getSCEV(Rem->getOperand(1)); |
| 474 | |
| 475 | // Simplify unnecessary loops away. |
| 476 | const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent()); |
| 477 | S = SE->getSCEVAtScope(S, ICmpLoop); |
| 478 | X = SE->getSCEVAtScope(X, ICmpLoop); |
| 479 | |
| 480 | // i % n --> i if i is in [0,n). |
| 481 | if ((!isSigned || SE->isKnownNonNegative(S)) && |
| 482 | SE->isKnownPredicate(isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
| 483 | S, X)) |
| 484 | Rem->replaceAllUsesWith(Rem->getOperand(0)); |
| 485 | else { |
| 486 | // (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n). |
| 487 | const SCEV *LessOne = |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 488 | SE->getMinusSCEV(S, SE->getConstant(S->getType(), 1)); |
Dan Gohman | a590b79 | 2010-04-13 01:46:36 +0000 | [diff] [blame] | 489 | if ((!isSigned || SE->isKnownNonNegative(LessOne)) && |
| 490 | SE->isKnownPredicate(isSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
| 491 | LessOne, X)) { |
| 492 | ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ, |
| 493 | Rem->getOperand(0), Rem->getOperand(1), |
| 494 | "tmp"); |
| 495 | SelectInst *Sel = |
| 496 | SelectInst::Create(ICmp, |
| 497 | ConstantInt::get(Rem->getType(), 0), |
| 498 | Rem->getOperand(0), "tmp", Rem); |
| 499 | Rem->replaceAllUsesWith(Sel); |
| 500 | } else |
| 501 | continue; |
| 502 | } |
| 503 | |
| 504 | // Inform IVUsers about the new users. |
| 505 | if (Instruction *I = dyn_cast<Instruction>(Rem->getOperand(0))) |
| 506 | IU->AddUsersIfInteresting(I); |
| 507 | |
| 508 | DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n'); |
| 509 | DeadInsts.push_back(Rem); |
| 510 | } |
Dan Gohman | a590b79 | 2010-04-13 01:46:36 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 513 | bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) { |
Dan Gohman | a528382 | 2010-06-18 01:35:11 +0000 | [diff] [blame] | 514 | // If LoopSimplify form is not available, stay out of trouble. Some notes: |
| 515 | // - LSR currently only supports LoopSimplify-form loops. Indvars' |
| 516 | // canonicalization can be a pessimization without LSR to "clean up" |
| 517 | // afterwards. |
| 518 | // - We depend on having a preheader; in particular, |
| 519 | // Loop::getCanonicalInductionVariable only supports loops with preheaders, |
| 520 | // and we're in trouble if we can't find the induction variable even when |
| 521 | // we've manually inserted one. |
| 522 | if (!L->isLoopSimplifyForm()) |
| 523 | return false; |
| 524 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 525 | IU = &getAnalysis<IVUsers>(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 526 | LI = &getAnalysis<LoopInfo>(); |
| 527 | SE = &getAnalysis<ScalarEvolution>(); |
Dan Gohman | de53dc0 | 2009-06-27 05:16:57 +0000 | [diff] [blame] | 528 | DT = &getAnalysis<DominatorTree>(); |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 529 | DeadInsts.clear(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 530 | Changed = false; |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 531 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 532 | // If there are any floating-point recurrences, attempt to |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 533 | // transform them to use integer recurrences. |
| 534 | RewriteNonIntegerIVs(L); |
| 535 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 536 | BasicBlock *ExitingBlock = L->getExitingBlock(); // may be null |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 537 | const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L); |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 538 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 539 | // Create a rewriter object which we'll use to transform the code with. |
| 540 | SCEVExpander Rewriter(*SE); |
| 541 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 542 | // Check to see if this loop has a computable loop-invariant execution count. |
| 543 | // If so, this means that we can compute the final value of any expressions |
| 544 | // that are recurrent in the loop, and substitute the exit values from the |
| 545 | // loop into any instructions outside of the loop that use the final values of |
| 546 | // the current expressions. |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 547 | // |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 548 | if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 549 | RewriteLoopExitValues(L, Rewriter); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 550 | |
Dan Gohman | d890f29 | 2010-04-12 07:56:56 +0000 | [diff] [blame] | 551 | // Simplify ICmp IV users. |
| 552 | EliminateIVComparisons(); |
| 553 | |
Dan Gohman | a590b79 | 2010-04-13 01:46:36 +0000 | [diff] [blame] | 554 | // Simplify SRem and URem IV users. |
| 555 | EliminateIVRemainders(); |
| 556 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 557 | // Compute the type of the largest recurrence expression, and decide whether |
| 558 | // a canonical induction variable should be inserted. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 559 | const Type *LargestType = 0; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 560 | bool NeedCannIV = false; |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 561 | if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) { |
| 562 | LargestType = BackedgeTakenCount->getType(); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 563 | LargestType = SE->getEffectiveSCEVType(LargestType); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 564 | // If we have a known trip count and a single exit block, we'll be |
| 565 | // rewriting the loop exit test condition below, which requires a |
| 566 | // canonical induction variable. |
| 567 | if (ExitingBlock) |
| 568 | NeedCannIV = true; |
Chris Lattner | f50af08 | 2004-04-17 18:08:33 +0000 | [diff] [blame] | 569 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 570 | for (IVUsers::const_iterator I = IU->begin(), E = IU->end(); I != E; ++I) { |
| 571 | const Type *Ty = |
| 572 | SE->getEffectiveSCEVType(I->getOperandValToReplace()->getType()); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 573 | if (!LargestType || |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 574 | SE->getTypeSizeInBits(Ty) > |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 575 | SE->getTypeSizeInBits(LargestType)) |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 576 | LargestType = Ty; |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 577 | NeedCannIV = true; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 580 | // Now that we know the largest of the induction variable expressions |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 581 | // in this loop, insert a canonical induction variable of the largest size. |
Dan Gohman | 43ef3fb | 2010-07-20 17:18:52 +0000 | [diff] [blame] | 582 | PHINode *IndVar = 0; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 583 | if (NeedCannIV) { |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 584 | // Check to see if the loop already has any canonical-looking induction |
| 585 | // variables. If any are present and wider than the planned canonical |
| 586 | // induction variable, temporarily remove them, so that the Rewriter |
| 587 | // doesn't attempt to reuse them. |
| 588 | SmallVector<PHINode *, 2> OldCannIVs; |
| 589 | while (PHINode *OldCannIV = L->getCanonicalInductionVariable()) { |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 590 | if (SE->getTypeSizeInBits(OldCannIV->getType()) > |
| 591 | SE->getTypeSizeInBits(LargestType)) |
| 592 | OldCannIV->removeFromParent(); |
| 593 | else |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 594 | break; |
| 595 | OldCannIVs.push_back(OldCannIV); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 598 | IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L, LargestType); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 599 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 600 | ++NumInserted; |
| 601 | Changed = true; |
David Greene | f67ef31 | 2010-01-05 01:27:06 +0000 | [diff] [blame] | 602 | DEBUG(dbgs() << "INDVARS: New CanIV: " << *IndVar << '\n'); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 603 | |
| 604 | // Now that the official induction variable is established, reinsert |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 605 | // any old canonical-looking variables after it so that the IR remains |
| 606 | // consistent. They will be deleted as part of the dead-PHI deletion at |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 607 | // the end of the pass. |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 608 | while (!OldCannIVs.empty()) { |
| 609 | PHINode *OldCannIV = OldCannIVs.pop_back_val(); |
| 610 | OldCannIV->insertBefore(L->getHeader()->getFirstNonPHI()); |
| 611 | } |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 612 | } |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 613 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 614 | // If we have a trip count expression, rewrite the loop's exit condition |
| 615 | // using it. We can currently only handle loops with a single exit. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 616 | ICmpInst *NewICmp = 0; |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 617 | if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) && |
| 618 | !BackedgeTakenCount->isZero() && |
| 619 | ExitingBlock) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 620 | assert(NeedCannIV && |
| 621 | "LinearFunctionTestReplace requires a canonical induction variable"); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 622 | // Can't rewrite non-branch yet. |
Dan Gohman | d890f29 | 2010-04-12 07:56:56 +0000 | [diff] [blame] | 623 | if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator())) |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 624 | NewICmp = LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar, |
| 625 | ExitingBlock, BI, Rewriter); |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 628 | // Rewrite IV-derived expressions. |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 629 | RewriteIVExpressions(L, Rewriter); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 630 | |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 631 | // Clear the rewriter cache, because values that are in the rewriter's cache |
| 632 | // can be deleted in the loop below, causing the AssertingVH in the cache to |
| 633 | // trigger. |
| 634 | Rewriter.clear(); |
| 635 | |
| 636 | // Now that we're done iterating through lists, clean up any instructions |
| 637 | // which are now dead. |
| 638 | while (!DeadInsts.empty()) |
| 639 | if (Instruction *Inst = |
| 640 | dyn_cast_or_null<Instruction>(&*DeadInsts.pop_back_val())) |
| 641 | RecursivelyDeleteTriviallyDeadInstructions(Inst); |
| 642 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 643 | // The Rewriter may not be used from this point on. |
Torok Edwin | 3d43138 | 2009-05-24 20:08:21 +0000 | [diff] [blame] | 644 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 645 | // Loop-invariant instructions in the preheader that aren't used in the |
| 646 | // loop may be sunk below the loop to reduce register pressure. |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 647 | SinkUnusedInvariants(L); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 648 | |
| 649 | // For completeness, inform IVUsers of the IV use in the newly-created |
| 650 | // loop exit test instruction. |
| 651 | if (NewICmp) |
| 652 | IU->AddUsersIfInteresting(cast<Instruction>(NewICmp->getOperand(0))); |
| 653 | |
| 654 | // Clean up dead instructions. |
Dan Gohman | 9fff218 | 2010-01-05 16:31:45 +0000 | [diff] [blame] | 655 | Changed |= DeleteDeadPHIs(L->getHeader()); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 656 | // Check a post-condition. |
Dan Gohman | bbf81d8 | 2010-03-10 19:38:49 +0000 | [diff] [blame] | 657 | assert(L->isLCSSAForm(*DT) && "Indvars did not leave the loop in lcssa form!"); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 658 | return Changed; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 659 | } |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 660 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 661 | // FIXME: It is an extremely bad idea to indvar substitute anything more |
| 662 | // complex than affine induction variables. Doing so will put expensive |
| 663 | // polynomial evaluations inside of the loop, and the str reduction pass |
| 664 | // currently can only reduce affine polynomials. For now just disable |
| 665 | // indvar subst on anything more complex than an affine addrec, unless |
| 666 | // it can be expanded to a trivial value. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 667 | static bool isSafe(const SCEV *S, const Loop *L, ScalarEvolution *SE) { |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 668 | // Loop-invariant values are safe. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 669 | if (SE->isLoopInvariant(S, L)) return true; |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 670 | |
| 671 | // Affine addrecs are safe. Non-affine are not, because LSR doesn't know how |
| 672 | // to transform them into efficient code. |
| 673 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) |
| 674 | return AR->isAffine(); |
| 675 | |
| 676 | // An add is safe it all its operands are safe. |
| 677 | if (const SCEVCommutativeExpr *Commutative = dyn_cast<SCEVCommutativeExpr>(S)) { |
| 678 | for (SCEVCommutativeExpr::op_iterator I = Commutative->op_begin(), |
| 679 | E = Commutative->op_end(); I != E; ++I) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 680 | if (!isSafe(*I, L, SE)) return false; |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 681 | return true; |
| 682 | } |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 683 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 684 | // A cast is safe if its operand is. |
| 685 | if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S)) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 686 | return isSafe(C->getOperand(), L, SE); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 687 | |
| 688 | // A udiv is safe if its operands are. |
| 689 | if (const SCEVUDivExpr *UD = dyn_cast<SCEVUDivExpr>(S)) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 690 | return isSafe(UD->getLHS(), L, SE) && |
| 691 | isSafe(UD->getRHS(), L, SE); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 692 | |
| 693 | // SCEVUnknown is always safe. |
| 694 | if (isa<SCEVUnknown>(S)) |
| 695 | return true; |
| 696 | |
| 697 | // Nothing else is safe. |
| 698 | return false; |
| 699 | } |
| 700 | |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 701 | void IndVarSimplify::RewriteIVExpressions(Loop *L, SCEVExpander &Rewriter) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 702 | // Rewrite all induction variable expressions in terms of the canonical |
| 703 | // induction variable. |
| 704 | // |
| 705 | // If there were induction variables of other sizes or offsets, manually |
| 706 | // add the offsets to the primary induction variable and cast, avoiding |
| 707 | // the need for the code evaluation methods to insert induction variables |
| 708 | // of different sizes. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 709 | for (IVUsers::iterator UI = IU->begin(), E = IU->end(); UI != E; ++UI) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 710 | Value *Op = UI->getOperandValToReplace(); |
| 711 | const Type *UseTy = Op->getType(); |
| 712 | Instruction *User = UI->getUser(); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 713 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 714 | // Compute the final addrec to expand into code. |
| 715 | const SCEV *AR = IU->getReplacementExpr(*UI); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 716 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 717 | // Evaluate the expression out of the loop, if possible. |
| 718 | if (!L->contains(UI->getUser())) { |
| 719 | const SCEV *ExitVal = SE->getSCEVAtScope(AR, L->getParentLoop()); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 720 | if (SE->isLoopInvariant(ExitVal, L)) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 721 | AR = ExitVal; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 722 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 723 | |
| 724 | // FIXME: It is an extremely bad idea to indvar substitute anything more |
| 725 | // complex than affine induction variables. Doing so will put expensive |
| 726 | // polynomial evaluations inside of the loop, and the str reduction pass |
| 727 | // currently can only reduce affine polynomials. For now just disable |
| 728 | // indvar subst on anything more complex than an affine addrec, unless |
| 729 | // it can be expanded to a trivial value. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 730 | if (!isSafe(AR, L, SE)) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 731 | continue; |
| 732 | |
| 733 | // Determine the insertion point for this user. By default, insert |
| 734 | // immediately before the user. The SCEVExpander class will automatically |
| 735 | // hoist loop invariants out of the loop. For PHI nodes, there may be |
| 736 | // multiple uses, so compute the nearest common dominator for the |
| 737 | // incoming blocks. |
| 738 | Instruction *InsertPt = User; |
| 739 | if (PHINode *PHI = dyn_cast<PHINode>(InsertPt)) |
| 740 | for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) |
| 741 | if (PHI->getIncomingValue(i) == Op) { |
| 742 | if (InsertPt == User) |
| 743 | InsertPt = PHI->getIncomingBlock(i)->getTerminator(); |
| 744 | else |
| 745 | InsertPt = |
| 746 | DT->findNearestCommonDominator(InsertPt->getParent(), |
| 747 | PHI->getIncomingBlock(i)) |
| 748 | ->getTerminator(); |
| 749 | } |
| 750 | |
| 751 | // Now expand it into actual Instructions and patch it into place. |
| 752 | Value *NewVal = Rewriter.expandCodeFor(AR, UseTy, InsertPt); |
| 753 | |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 754 | DEBUG(dbgs() << "INDVARS: Rewrote IV '" << *AR << "' " << *Op << '\n' |
| 755 | << " into = " << *NewVal << "\n"); |
| 756 | |
| 757 | if (!isValidRewrite(Op, NewVal)) { |
| 758 | DeadInsts.push_back(NewVal); |
| 759 | continue; |
| 760 | } |
Dan Gohman | d7bfd00 | 2010-04-02 14:48:31 +0000 | [diff] [blame] | 761 | // Inform ScalarEvolution that this value is changing. The change doesn't |
| 762 | // affect its value, but it does potentially affect which use lists the |
| 763 | // value will be on after the replacement, which affects ScalarEvolution's |
| 764 | // ability to walk use lists and drop dangling pointers when a value is |
| 765 | // deleted. |
| 766 | SE->forgetValue(User); |
| 767 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 768 | // Patch the new value into place. |
| 769 | if (Op->hasName()) |
| 770 | NewVal->takeName(Op); |
| 771 | User->replaceUsesOfWith(Op, NewVal); |
| 772 | UI->setOperandValToReplace(NewVal); |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 773 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 774 | ++NumRemoved; |
| 775 | Changed = true; |
| 776 | |
| 777 | // The old value may be dead now. |
| 778 | DeadInsts.push_back(Op); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 779 | } |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | /// If there's a single exit block, sink any loop-invariant values that |
| 783 | /// were defined in the preheader but not used inside the loop into the |
| 784 | /// exit block to reduce register pressure in the loop. |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 785 | void IndVarSimplify::SinkUnusedInvariants(Loop *L) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 786 | BasicBlock *ExitBlock = L->getExitBlock(); |
| 787 | if (!ExitBlock) return; |
| 788 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 789 | BasicBlock *Preheader = L->getLoopPreheader(); |
Dan Gohman | 03e896b | 2009-11-05 21:11:53 +0000 | [diff] [blame] | 790 | if (!Preheader) return; |
| 791 | |
| 792 | Instruction *InsertPt = ExitBlock->getFirstNonPHI(); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 793 | BasicBlock::iterator I = Preheader->getTerminator(); |
| 794 | while (I != Preheader->begin()) { |
| 795 | --I; |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 796 | // New instructions were inserted at the end of the preheader. |
| 797 | if (isa<PHINode>(I)) |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 798 | break; |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 799 | |
Eli Friedman | 0c77db3 | 2009-07-15 22:48:29 +0000 | [diff] [blame] | 800 | // Don't move instructions which might have side effects, since the side |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 801 | // effects need to complete before instructions inside the loop. Also don't |
| 802 | // move instructions which might read memory, since the loop may modify |
| 803 | // memory. Note that it's okay if the instruction might have undefined |
| 804 | // behavior: LoopSimplify guarantees that the preheader dominates the exit |
| 805 | // block. |
Eli Friedman | 0c77db3 | 2009-07-15 22:48:29 +0000 | [diff] [blame] | 806 | if (I->mayHaveSideEffects() || I->mayReadFromMemory()) |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 807 | continue; |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 808 | |
Devang Patel | 7b9f6b1 | 2010-03-15 22:23:03 +0000 | [diff] [blame] | 809 | // Skip debug info intrinsics. |
| 810 | if (isa<DbgInfoIntrinsic>(I)) |
| 811 | continue; |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 812 | |
Dan Gohman | 76f497a | 2009-08-25 17:42:10 +0000 | [diff] [blame] | 813 | // Don't sink static AllocaInsts out of the entry block, which would |
| 814 | // turn them into dynamic allocas! |
| 815 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 816 | if (AI->isStaticAlloca()) |
| 817 | continue; |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 818 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 819 | // Determine if there is a use in or before the loop (direct or |
| 820 | // otherwise). |
| 821 | bool UsedInLoop = false; |
| 822 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 823 | UI != UE; ++UI) { |
Gabor Greif | 7656018 | 2010-07-09 15:40:10 +0000 | [diff] [blame] | 824 | User *U = *UI; |
| 825 | BasicBlock *UseBB = cast<Instruction>(U)->getParent(); |
| 826 | if (PHINode *P = dyn_cast<PHINode>(U)) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 827 | unsigned i = |
| 828 | PHINode::getIncomingValueNumForOperand(UI.getOperandNo()); |
| 829 | UseBB = P->getIncomingBlock(i); |
| 830 | } |
| 831 | if (UseBB == Preheader || L->contains(UseBB)) { |
| 832 | UsedInLoop = true; |
| 833 | break; |
| 834 | } |
| 835 | } |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 836 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 837 | // If there is, the def must remain in the preheader. |
| 838 | if (UsedInLoop) |
| 839 | continue; |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 840 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 841 | // Otherwise, sink it to the exit block. |
| 842 | Instruction *ToMove = I; |
| 843 | bool Done = false; |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 844 | |
| 845 | if (I != Preheader->begin()) { |
| 846 | // Skip debug info intrinsics. |
| 847 | do { |
| 848 | --I; |
| 849 | } while (isa<DbgInfoIntrinsic>(I) && I != Preheader->begin()); |
| 850 | |
| 851 | if (isa<DbgInfoIntrinsic>(I) && I == Preheader->begin()) |
| 852 | Done = true; |
| 853 | } else { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 854 | Done = true; |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 857 | ToMove->moveBefore(InsertPt); |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 858 | if (Done) break; |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 859 | InsertPt = ToMove; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | |
Chris Lattner | bbb9149 | 2010-04-03 06:41:49 +0000 | [diff] [blame] | 863 | /// ConvertToSInt - Convert APF to an integer, if possible. |
| 864 | static bool ConvertToSInt(const APFloat &APF, int64_t &IntVal) { |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 865 | bool isExact = false; |
Evan Cheng | 794a7db | 2008-11-26 01:11:57 +0000 | [diff] [blame] | 866 | if (&APF.getSemantics() == &APFloat::PPCDoubleDouble) |
| 867 | return false; |
Chris Lattner | bbb9149 | 2010-04-03 06:41:49 +0000 | [diff] [blame] | 868 | // See if we can convert this to an int64_t |
| 869 | uint64_t UIntVal; |
| 870 | if (APF.convertToInteger(&UIntVal, 64, true, APFloat::rmTowardZero, |
| 871 | &isExact) != APFloat::opOK || !isExact) |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 872 | return false; |
Chris Lattner | bbb9149 | 2010-04-03 06:41:49 +0000 | [diff] [blame] | 873 | IntVal = UIntVal; |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 874 | return true; |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 877 | /// HandleFloatingPointIV - If the loop has floating induction variable |
| 878 | /// then insert corresponding integer induction variable if possible. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 879 | /// For example, |
| 880 | /// for(double i = 0; i < 10000; ++i) |
| 881 | /// bar(i) |
| 882 | /// is converted into |
| 883 | /// for(int i = 0; i < 10000; ++i) |
| 884 | /// bar((double)i); |
| 885 | /// |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 886 | void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PN) { |
| 887 | unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0)); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 888 | unsigned BackEdge = IncomingEdge^1; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 889 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 890 | // Check incoming value. |
Chris Lattner | c4f7e80 | 2010-04-03 06:05:10 +0000 | [diff] [blame] | 891 | ConstantFP *InitValueVal = |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 892 | dyn_cast<ConstantFP>(PN->getIncomingValue(IncomingEdge)); |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 893 | |
Chris Lattner | bbb9149 | 2010-04-03 06:41:49 +0000 | [diff] [blame] | 894 | int64_t InitValue; |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 895 | if (!InitValueVal || !ConvertToSInt(InitValueVal->getValueAPF(), InitValue)) |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 896 | return; |
| 897 | |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 898 | // Check IV increment. Reject this PN if increment operation is not |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 899 | // an add or increment value can not be represented by an integer. |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 900 | BinaryOperator *Incr = |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 901 | dyn_cast<BinaryOperator>(PN->getIncomingValue(BackEdge)); |
Chris Lattner | 07aa76a | 2010-04-03 05:54:59 +0000 | [diff] [blame] | 902 | if (Incr == 0 || Incr->getOpcode() != Instruction::FAdd) return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 903 | |
Chris Lattner | 07aa76a | 2010-04-03 05:54:59 +0000 | [diff] [blame] | 904 | // If this is not an add of the PHI with a constantfp, or if the constant fp |
| 905 | // is not an integer, bail out. |
Chris Lattner | c4f7e80 | 2010-04-03 06:05:10 +0000 | [diff] [blame] | 906 | ConstantFP *IncValueVal = dyn_cast<ConstantFP>(Incr->getOperand(1)); |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 907 | int64_t IncValue; |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 908 | if (IncValueVal == 0 || Incr->getOperand(0) != PN || |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 909 | !ConvertToSInt(IncValueVal->getValueAPF(), IncValue)) |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 910 | return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 911 | |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 912 | // Check Incr uses. One user is PN and the other user is an exit condition |
Chris Lattner | 07aa76a | 2010-04-03 05:54:59 +0000 | [diff] [blame] | 913 | // used by the conditional terminator. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 914 | Value::use_iterator IncrUse = Incr->use_begin(); |
Gabor Greif | 96f1d8e | 2010-07-22 13:36:47 +0000 | [diff] [blame] | 915 | Instruction *U1 = cast<Instruction>(*IncrUse++); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 916 | if (IncrUse == Incr->use_end()) return; |
Gabor Greif | 96f1d8e | 2010-07-22 13:36:47 +0000 | [diff] [blame] | 917 | Instruction *U2 = cast<Instruction>(*IncrUse++); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 918 | if (IncrUse != Incr->use_end()) return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 919 | |
Chris Lattner | 07aa76a | 2010-04-03 05:54:59 +0000 | [diff] [blame] | 920 | // Find exit condition, which is an fcmp. If it doesn't exist, or if it isn't |
| 921 | // only used by a branch, we can't transform it. |
Chris Lattner | ca703bd | 2010-04-03 06:11:07 +0000 | [diff] [blame] | 922 | FCmpInst *Compare = dyn_cast<FCmpInst>(U1); |
| 923 | if (!Compare) |
| 924 | Compare = dyn_cast<FCmpInst>(U2); |
| 925 | if (Compare == 0 || !Compare->hasOneUse() || |
| 926 | !isa<BranchInst>(Compare->use_back())) |
Chris Lattner | 07aa76a | 2010-04-03 05:54:59 +0000 | [diff] [blame] | 927 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 928 | |
Chris Lattner | ca703bd | 2010-04-03 06:11:07 +0000 | [diff] [blame] | 929 | BranchInst *TheBr = cast<BranchInst>(Compare->use_back()); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 930 | |
Chris Lattner | d52c072 | 2010-04-03 07:21:39 +0000 | [diff] [blame] | 931 | // We need to verify that the branch actually controls the iteration count |
| 932 | // of the loop. If not, the new IV can overflow and no one will notice. |
| 933 | // The branch block must be in the loop and one of the successors must be out |
| 934 | // of the loop. |
| 935 | assert(TheBr->isConditional() && "Can't use fcmp if not conditional"); |
| 936 | if (!L->contains(TheBr->getParent()) || |
| 937 | (L->contains(TheBr->getSuccessor(0)) && |
| 938 | L->contains(TheBr->getSuccessor(1)))) |
| 939 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 940 | |
| 941 | |
Chris Lattner | 07aa76a | 2010-04-03 05:54:59 +0000 | [diff] [blame] | 942 | // If it isn't a comparison with an integer-as-fp (the exit value), we can't |
| 943 | // transform it. |
Chris Lattner | ca703bd | 2010-04-03 06:11:07 +0000 | [diff] [blame] | 944 | ConstantFP *ExitValueVal = dyn_cast<ConstantFP>(Compare->getOperand(1)); |
Chris Lattner | bbb9149 | 2010-04-03 06:41:49 +0000 | [diff] [blame] | 945 | int64_t ExitValue; |
| 946 | if (ExitValueVal == 0 || |
| 947 | !ConvertToSInt(ExitValueVal->getValueAPF(), ExitValue)) |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 948 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 949 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 950 | // Find new predicate for integer comparison. |
| 951 | CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | ca703bd | 2010-04-03 06:11:07 +0000 | [diff] [blame] | 952 | switch (Compare->getPredicate()) { |
Chris Lattner | c4f7e80 | 2010-04-03 06:05:10 +0000 | [diff] [blame] | 953 | default: return; // Unknown comparison. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 954 | case CmpInst::FCMP_OEQ: |
Chris Lattner | c4f7e80 | 2010-04-03 06:05:10 +0000 | [diff] [blame] | 955 | case CmpInst::FCMP_UEQ: NewPred = CmpInst::ICMP_EQ; break; |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 956 | case CmpInst::FCMP_ONE: |
| 957 | case CmpInst::FCMP_UNE: NewPred = CmpInst::ICMP_NE; break; |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 958 | case CmpInst::FCMP_OGT: |
Chris Lattner | a40e4a0 | 2010-04-03 06:25:21 +0000 | [diff] [blame] | 959 | case CmpInst::FCMP_UGT: NewPred = CmpInst::ICMP_SGT; break; |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 960 | case CmpInst::FCMP_OGE: |
Chris Lattner | a40e4a0 | 2010-04-03 06:25:21 +0000 | [diff] [blame] | 961 | case CmpInst::FCMP_UGE: NewPred = CmpInst::ICMP_SGE; break; |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 962 | case CmpInst::FCMP_OLT: |
Chris Lattner | 43b8527 | 2010-04-03 06:30:03 +0000 | [diff] [blame] | 963 | case CmpInst::FCMP_ULT: NewPred = CmpInst::ICMP_SLT; break; |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 964 | case CmpInst::FCMP_OLE: |
Chris Lattner | 43b8527 | 2010-04-03 06:30:03 +0000 | [diff] [blame] | 965 | case CmpInst::FCMP_ULE: NewPred = CmpInst::ICMP_SLE; break; |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 966 | } |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 967 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 968 | // We convert the floating point induction variable to a signed i32 value if |
| 969 | // we can. This is only safe if the comparison will not overflow in a way |
| 970 | // that won't be trapped by the integer equivalent operations. Check for this |
| 971 | // now. |
| 972 | // TODO: We could use i64 if it is native and the range requires it. |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 973 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 974 | // The start/stride/exit values must all fit in signed i32. |
| 975 | if (!isInt<32>(InitValue) || !isInt<32>(IncValue) || !isInt<32>(ExitValue)) |
| 976 | return; |
| 977 | |
| 978 | // If not actually striding (add x, 0.0), avoid touching the code. |
| 979 | if (IncValue == 0) |
| 980 | return; |
| 981 | |
| 982 | // Positive and negative strides have different safety conditions. |
| 983 | if (IncValue > 0) { |
| 984 | // If we have a positive stride, we require the init to be less than the |
| 985 | // exit value and an equality or less than comparison. |
| 986 | if (InitValue >= ExitValue || |
| 987 | NewPred == CmpInst::ICMP_SGT || NewPred == CmpInst::ICMP_SGE) |
| 988 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 989 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 990 | uint32_t Range = uint32_t(ExitValue-InitValue); |
| 991 | if (NewPred == CmpInst::ICMP_SLE) { |
| 992 | // Normalize SLE -> SLT, check for infinite loop. |
| 993 | if (++Range == 0) return; // Range overflows. |
| 994 | } |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 995 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 996 | unsigned Leftover = Range % uint32_t(IncValue); |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 997 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 998 | // If this is an equality comparison, we require that the strided value |
| 999 | // exactly land on the exit value, otherwise the IV condition will wrap |
| 1000 | // around and do things the fp IV wouldn't. |
| 1001 | if ((NewPred == CmpInst::ICMP_EQ || NewPred == CmpInst::ICMP_NE) && |
| 1002 | Leftover != 0) |
| 1003 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1004 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1005 | // If the stride would wrap around the i32 before exiting, we can't |
| 1006 | // transform the IV. |
| 1007 | if (Leftover != 0 && int32_t(ExitValue+IncValue) < ExitValue) |
| 1008 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1009 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1010 | } else { |
| 1011 | // If we have a negative stride, we require the init to be greater than the |
| 1012 | // exit value and an equality or greater than comparison. |
| 1013 | if (InitValue >= ExitValue || |
| 1014 | NewPred == CmpInst::ICMP_SLT || NewPred == CmpInst::ICMP_SLE) |
| 1015 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1017 | uint32_t Range = uint32_t(InitValue-ExitValue); |
| 1018 | if (NewPred == CmpInst::ICMP_SGE) { |
| 1019 | // Normalize SGE -> SGT, check for infinite loop. |
| 1020 | if (++Range == 0) return; // Range overflows. |
| 1021 | } |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1022 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1023 | unsigned Leftover = Range % uint32_t(-IncValue); |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1024 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1025 | // If this is an equality comparison, we require that the strided value |
| 1026 | // exactly land on the exit value, otherwise the IV condition will wrap |
| 1027 | // around and do things the fp IV wouldn't. |
| 1028 | if ((NewPred == CmpInst::ICMP_EQ || NewPred == CmpInst::ICMP_NE) && |
| 1029 | Leftover != 0) |
| 1030 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1031 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1032 | // If the stride would wrap around the i32 before exiting, we can't |
| 1033 | // transform the IV. |
| 1034 | if (Leftover != 0 && int32_t(ExitValue+IncValue) > ExitValue) |
| 1035 | return; |
| 1036 | } |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1037 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1038 | const IntegerType *Int32Ty = Type::getInt32Ty(PN->getContext()); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1039 | |
Chris Lattner | bbb9149 | 2010-04-03 06:41:49 +0000 | [diff] [blame] | 1040 | // Insert new integer induction variable. |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1041 | PHINode *NewPHI = PHINode::Create(Int32Ty, PN->getName()+".int", PN); |
Chris Lattner | c4f7e80 | 2010-04-03 06:05:10 +0000 | [diff] [blame] | 1042 | NewPHI->addIncoming(ConstantInt::get(Int32Ty, InitValue), |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1043 | PN->getIncomingBlock(IncomingEdge)); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1044 | |
Chris Lattner | c4f7e80 | 2010-04-03 06:05:10 +0000 | [diff] [blame] | 1045 | Value *NewAdd = |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1046 | BinaryOperator::CreateAdd(NewPHI, ConstantInt::get(Int32Ty, IncValue), |
Chris Lattner | c4f7e80 | 2010-04-03 06:05:10 +0000 | [diff] [blame] | 1047 | Incr->getName()+".int", Incr); |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1048 | NewPHI->addIncoming(NewAdd, PN->getIncomingBlock(BackEdge)); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1049 | |
Chris Lattner | ca703bd | 2010-04-03 06:11:07 +0000 | [diff] [blame] | 1050 | ICmpInst *NewCompare = new ICmpInst(TheBr, NewPred, NewAdd, |
| 1051 | ConstantInt::get(Int32Ty, ExitValue), |
| 1052 | Compare->getName()); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1053 | |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1054 | // In the following deletions, PN may become dead and may be deleted. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1055 | // Use a WeakVH to observe whether this happens. |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1056 | WeakVH WeakPH = PN; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1057 | |
Chris Lattner | ca703bd | 2010-04-03 06:11:07 +0000 | [diff] [blame] | 1058 | // Delete the old floating point exit comparison. The branch starts using the |
| 1059 | // new comparison. |
| 1060 | NewCompare->takeName(Compare); |
| 1061 | Compare->replaceAllUsesWith(NewCompare); |
| 1062 | RecursivelyDeleteTriviallyDeadInstructions(Compare); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1063 | |
Chris Lattner | ca703bd | 2010-04-03 06:11:07 +0000 | [diff] [blame] | 1064 | // Delete the old floating point increment. |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1065 | Incr->replaceAllUsesWith(UndefValue::get(Incr->getType())); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1066 | RecursivelyDeleteTriviallyDeadInstructions(Incr); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1067 | |
Chris Lattner | 70c0d4f | 2010-04-03 06:16:22 +0000 | [diff] [blame] | 1068 | // If the FP induction variable still has uses, this is because something else |
| 1069 | // in the loop uses its value. In order to canonicalize the induction |
| 1070 | // variable, we chose to eliminate the IV and rewrite it in terms of an |
| 1071 | // int->fp cast. |
| 1072 | // |
| 1073 | // We give preference to sitofp over uitofp because it is faster on most |
| 1074 | // platforms. |
| 1075 | if (WeakPH) { |
Chris Lattner | a40e4a0 | 2010-04-03 06:25:21 +0000 | [diff] [blame] | 1076 | Value *Conv = new SIToFPInst(NewPHI, PN->getType(), "indvar.conv", |
| 1077 | PN->getParent()->getFirstNonPHI()); |
| 1078 | PN->replaceAllUsesWith(Conv); |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1079 | RecursivelyDeleteTriviallyDeadInstructions(PN); |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 1080 | } |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 1081 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1082 | // Add a new IVUsers entry for the newly-created integer PHI. |
| 1083 | IU->AddUsersIfInteresting(NewPHI); |
| 1084 | } |