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 | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 55 | #include "llvm/Support/Debug.h" |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 56 | #include "llvm/Support/raw_ostream.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 57 | #include "llvm/Transforms/Utils/Local.h" |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 58 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Andrew Trick | 37da408 | 2011-05-04 02:10:13 +0000 | [diff] [blame] | 59 | #include "llvm/Target/TargetData.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 | |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 65 | STATISTIC(NumRemoved , "Number of aux indvars removed"); |
| 66 | STATISTIC(NumWidened , "Number of indvars widened"); |
| 67 | STATISTIC(NumInserted , "Number of canonical indvars added"); |
| 68 | STATISTIC(NumReplaced , "Number of exit values replaced"); |
| 69 | STATISTIC(NumLFTR , "Number of loop exit tests replaced"); |
| 70 | STATISTIC(NumElimIdentity, "Number of IV identities eliminated"); |
| 71 | STATISTIC(NumElimExt , "Number of IV sign/zero extends eliminated"); |
| 72 | STATISTIC(NumElimRem , "Number of IV remainder operations eliminated"); |
| 73 | STATISTIC(NumElimCmp , "Number of IV comparisons eliminated"); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 74 | |
Andrew Trick | 37da408 | 2011-05-04 02:10:13 +0000 | [diff] [blame] | 75 | // DisableIVRewrite mode currently affects IVUsers, so is defined in libAnalysis |
| 76 | // and referenced here. |
| 77 | namespace llvm { |
| 78 | extern bool DisableIVRewrite; |
| 79 | } |
| 80 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 81 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 82 | class IndVarSimplify : public LoopPass { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 83 | IVUsers *IU; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 84 | LoopInfo *LI; |
| 85 | ScalarEvolution *SE; |
Dan Gohman | de53dc0 | 2009-06-27 05:16:57 +0000 | [diff] [blame] | 86 | DominatorTree *DT; |
Andrew Trick | 37da408 | 2011-05-04 02:10:13 +0000 | [diff] [blame] | 87 | TargetData *TD; |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 88 | |
| 89 | PHINode *CurrIV; // Current IV being simplified. |
| 90 | |
| 91 | // Instructions processed by SimplifyIVUsers for CurrIV. |
| 92 | SmallPtrSet<Instruction*,16> Simplified; |
| 93 | |
| 94 | // Use-def pairs if IVUsers waiting to be processed for CurrIV. |
| 95 | SmallVector<std::pair<Instruction*, Instruction*>, 8> SimpleIVUsers; |
| 96 | |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 97 | SmallVector<WeakVH, 16> DeadInsts; |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 98 | bool Changed; |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 99 | public: |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 100 | |
Dan Gohman | 5668cf7 | 2009-07-15 01:26:32 +0000 | [diff] [blame] | 101 | static char ID; // Pass identification, replacement for typeid |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 102 | IndVarSimplify() : LoopPass(ID), IU(0), LI(0), SE(0), DT(0), TD(0), |
| 103 | CurrIV(0), Changed(false) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 104 | initializeIndVarSimplifyPass(*PassRegistry::getPassRegistry()); |
| 105 | } |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 106 | |
Dan Gohman | 5668cf7 | 2009-07-15 01:26:32 +0000 | [diff] [blame] | 107 | virtual bool runOnLoop(Loop *L, LPPassManager &LPM); |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 108 | |
Dan Gohman | 5668cf7 | 2009-07-15 01:26:32 +0000 | [diff] [blame] | 109 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 110 | AU.addRequired<DominatorTree>(); |
| 111 | AU.addRequired<LoopInfo>(); |
| 112 | AU.addRequired<ScalarEvolution>(); |
| 113 | AU.addRequiredID(LoopSimplifyID); |
| 114 | AU.addRequiredID(LCSSAID); |
| 115 | AU.addRequired<IVUsers>(); |
| 116 | AU.addPreserved<ScalarEvolution>(); |
| 117 | AU.addPreservedID(LoopSimplifyID); |
| 118 | AU.addPreservedID(LCSSAID); |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 119 | if (!DisableIVRewrite) |
| 120 | AU.addPreserved<IVUsers>(); |
Dan Gohman | 5668cf7 | 2009-07-15 01:26:32 +0000 | [diff] [blame] | 121 | AU.setPreservesCFG(); |
| 122 | } |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 123 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 124 | private: |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 125 | bool isValidRewrite(Value *FromVal, Value *ToVal); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 126 | |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 127 | void SimplifyIVUsers(SCEVExpander &Rewriter); |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 128 | void SimplifyIVUsersNoRewrite(Loop *L, SCEVExpander &Rewriter); |
| 129 | |
| 130 | bool EliminateIVUser(Instruction *UseInst, Instruction *IVOperand); |
Andrew Trick | aeee461 | 2011-05-12 00:04:28 +0000 | [diff] [blame] | 131 | void EliminateIVComparison(ICmpInst *ICmp, Value *IVOperand); |
| 132 | void EliminateIVRemainder(BinaryOperator *Rem, |
| 133 | Value *IVOperand, |
Andrew Trick | 4417e53 | 2011-06-21 15:43:52 +0000 | [diff] [blame] | 134 | bool IsSigned); |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 135 | void pushIVUsers(Instruction *Def); |
| 136 | bool isSimpleIVUser(Instruction *I, const Loop *L); |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 137 | void RewriteNonIntegerIVs(Loop *L); |
| 138 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 139 | ICmpInst *LinearFunctionTestReplace(Loop *L, const SCEV *BackedgeTakenCount, |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 140 | PHINode *IndVar, |
| 141 | SCEVExpander &Rewriter); |
Andrew Trick | 37da408 | 2011-05-04 02:10:13 +0000 | [diff] [blame] | 142 | |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 143 | void RewriteLoopExitValues(Loop *L, SCEVExpander &Rewriter); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 144 | |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 145 | void RewriteIVExpressions(Loop *L, SCEVExpander &Rewriter); |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 146 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 147 | void SinkUnusedInvariants(Loop *L); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 148 | |
| 149 | void HandleFloatingPointIV(Loop *L, PHINode *PH); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 150 | }; |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 151 | } |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 152 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 153 | char IndVarSimplify::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 154 | INITIALIZE_PASS_BEGIN(IndVarSimplify, "indvars", |
Andrew Trick | 37da408 | 2011-05-04 02:10:13 +0000 | [diff] [blame] | 155 | "Induction Variable Simplification", false, false) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 156 | INITIALIZE_PASS_DEPENDENCY(DominatorTree) |
| 157 | INITIALIZE_PASS_DEPENDENCY(LoopInfo) |
| 158 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 159 | INITIALIZE_PASS_DEPENDENCY(LoopSimplify) |
| 160 | INITIALIZE_PASS_DEPENDENCY(LCSSA) |
| 161 | INITIALIZE_PASS_DEPENDENCY(IVUsers) |
| 162 | INITIALIZE_PASS_END(IndVarSimplify, "indvars", |
Andrew Trick | 37da408 | 2011-05-04 02:10:13 +0000 | [diff] [blame] | 163 | "Induction Variable Simplification", false, false) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 164 | |
Daniel Dunbar | 394f044 | 2008-10-22 23:32:42 +0000 | [diff] [blame] | 165 | Pass *llvm::createIndVarSimplifyPass() { |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 166 | return new IndVarSimplify(); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 169 | /// isValidRewrite - Return true if the SCEV expansion generated by the |
| 170 | /// rewriter can replace the original value. SCEV guarantees that it |
| 171 | /// produces the same value, but the way it is produced may be illegal IR. |
| 172 | /// Ideally, this function will only be called for verification. |
| 173 | bool IndVarSimplify::isValidRewrite(Value *FromVal, Value *ToVal) { |
| 174 | // If an SCEV expression subsumed multiple pointers, its expansion could |
| 175 | // reassociate the GEP changing the base pointer. This is illegal because the |
| 176 | // final address produced by a GEP chain must be inbounds relative to its |
| 177 | // underlying object. Otherwise basic alias analysis, among other things, |
| 178 | // could fail in a dangerous way. Ultimately, SCEV will be improved to avoid |
| 179 | // producing an expression involving multiple pointers. Until then, we must |
| 180 | // bail out here. |
| 181 | // |
| 182 | // Retrieve the pointer operand of the GEP. Don't use GetUnderlyingObject |
| 183 | // because it understands lcssa phis while SCEV does not. |
| 184 | Value *FromPtr = FromVal; |
| 185 | Value *ToPtr = ToVal; |
| 186 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(FromVal)) { |
| 187 | FromPtr = GEP->getPointerOperand(); |
| 188 | } |
| 189 | if (GEPOperator *GEP = dyn_cast<GEPOperator>(ToVal)) { |
| 190 | ToPtr = GEP->getPointerOperand(); |
| 191 | } |
| 192 | if (FromPtr != FromVal || ToPtr != ToVal) { |
| 193 | // Quickly check the common case |
| 194 | if (FromPtr == ToPtr) |
| 195 | return true; |
| 196 | |
| 197 | // SCEV may have rewritten an expression that produces the GEP's pointer |
| 198 | // operand. That's ok as long as the pointer operand has the same base |
| 199 | // pointer. Unlike GetUnderlyingObject(), getPointerBase() will find the |
| 200 | // base of a recurrence. This handles the case in which SCEV expansion |
| 201 | // converts a pointer type recurrence into a nonrecurrent pointer base |
| 202 | // indexed by an integer recurrence. |
| 203 | const SCEV *FromBase = SE->getPointerBase(SE->getSCEV(FromPtr)); |
| 204 | const SCEV *ToBase = SE->getPointerBase(SE->getSCEV(ToPtr)); |
| 205 | if (FromBase == ToBase) |
| 206 | return true; |
| 207 | |
| 208 | DEBUG(dbgs() << "INDVARS: GEP rewrite bail out " |
| 209 | << *FromBase << " != " << *ToBase << "\n"); |
| 210 | |
| 211 | return false; |
| 212 | } |
| 213 | return true; |
| 214 | } |
| 215 | |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 216 | /// canExpandBackedgeTakenCount - Return true if this loop's backedge taken |
| 217 | /// count expression can be safely and cheaply expanded into an instruction |
| 218 | /// sequence that can be used by LinearFunctionTestReplace. |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 219 | static bool canExpandBackedgeTakenCount(Loop *L, ScalarEvolution *SE) { |
| 220 | const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L); |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 221 | if (isa<SCEVCouldNotCompute>(BackedgeTakenCount) || |
| 222 | BackedgeTakenCount->isZero()) |
| 223 | return false; |
| 224 | |
| 225 | if (!L->getExitingBlock()) |
| 226 | return false; |
| 227 | |
| 228 | // Can't rewrite non-branch yet. |
| 229 | BranchInst *BI = dyn_cast<BranchInst>(L->getExitingBlock()->getTerminator()); |
| 230 | if (!BI) |
| 231 | return false; |
| 232 | |
Dan Gohman | ca9b703 | 2010-04-12 21:13:43 +0000 | [diff] [blame] | 233 | // Special case: If the backedge-taken count is a UDiv, it's very likely a |
| 234 | // UDiv that ScalarEvolution produced in order to compute a precise |
| 235 | // expression, rather than a UDiv from the user's code. If we can't find a |
| 236 | // UDiv in the code with some simple searching, assume the former and forego |
| 237 | // rewriting the loop. |
| 238 | if (isa<SCEVUDivExpr>(BackedgeTakenCount)) { |
| 239 | ICmpInst *OrigCond = dyn_cast<ICmpInst>(BI->getCondition()); |
Andrew Trick | 37da408 | 2011-05-04 02:10:13 +0000 | [diff] [blame] | 240 | if (!OrigCond) return false; |
Dan Gohman | ca9b703 | 2010-04-12 21:13:43 +0000 | [diff] [blame] | 241 | const SCEV *R = SE->getSCEV(OrigCond->getOperand(1)); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 242 | R = SE->getMinusSCEV(R, SE->getConstant(R->getType(), 1)); |
Dan Gohman | ca9b703 | 2010-04-12 21:13:43 +0000 | [diff] [blame] | 243 | if (R != BackedgeTakenCount) { |
| 244 | const SCEV *L = SE->getSCEV(OrigCond->getOperand(0)); |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 245 | L = SE->getMinusSCEV(L, SE->getConstant(L->getType(), 1)); |
Dan Gohman | ca9b703 | 2010-04-12 21:13:43 +0000 | [diff] [blame] | 246 | if (L != BackedgeTakenCount) |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 247 | return false; |
Dan Gohman | ca9b703 | 2010-04-12 21:13:43 +0000 | [diff] [blame] | 248 | } |
| 249 | } |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 250 | return true; |
| 251 | } |
| 252 | |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 253 | /// getBackedgeIVType - Get the widest type used by the loop test after peeking |
| 254 | /// through Truncs. |
| 255 | /// |
| 256 | /// TODO: Unnecessary once LinearFunctionTestReplace is removed. |
| 257 | static const Type *getBackedgeIVType(Loop *L) { |
| 258 | if (!L->getExitingBlock()) |
| 259 | return 0; |
| 260 | |
| 261 | // Can't rewrite non-branch yet. |
| 262 | BranchInst *BI = dyn_cast<BranchInst>(L->getExitingBlock()->getTerminator()); |
| 263 | if (!BI) |
| 264 | return 0; |
| 265 | |
| 266 | ICmpInst *Cond = dyn_cast<ICmpInst>(BI->getCondition()); |
| 267 | if (!Cond) |
| 268 | return 0; |
| 269 | |
| 270 | const Type *Ty = 0; |
| 271 | for(User::op_iterator OI = Cond->op_begin(), OE = Cond->op_end(); |
| 272 | OI != OE; ++OI) { |
| 273 | assert((!Ty || Ty == (*OI)->getType()) && "bad icmp operand types"); |
| 274 | TruncInst *Trunc = dyn_cast<TruncInst>(*OI); |
| 275 | if (!Trunc) |
| 276 | continue; |
| 277 | |
| 278 | return Trunc->getSrcTy(); |
| 279 | } |
| 280 | return Ty; |
| 281 | } |
| 282 | |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 283 | /// LinearFunctionTestReplace - This method rewrites the exit condition of the |
| 284 | /// loop to be a canonical != comparison against the incremented loop induction |
| 285 | /// variable. This pass is able to rewrite the exit tests of any loop where the |
| 286 | /// SCEV analysis can determine a loop-invariant trip count of the loop, which |
| 287 | /// is actually a much broader range than just linear tests. |
| 288 | ICmpInst *IndVarSimplify:: |
| 289 | LinearFunctionTestReplace(Loop *L, |
| 290 | const SCEV *BackedgeTakenCount, |
| 291 | PHINode *IndVar, |
| 292 | SCEVExpander &Rewriter) { |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 293 | assert(canExpandBackedgeTakenCount(L, SE) && "precondition"); |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 294 | BranchInst *BI = cast<BranchInst>(L->getExitingBlock()->getTerminator()); |
Dan Gohman | ca9b703 | 2010-04-12 21:13:43 +0000 | [diff] [blame] | 295 | |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 296 | // If the exiting block is not the same as the backedge block, we must compare |
| 297 | // against the preincremented value, otherwise we prefer to compare against |
| 298 | // the post-incremented value. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 299 | Value *CmpIndVar; |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 300 | const SCEV *RHS = BackedgeTakenCount; |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 301 | if (L->getExitingBlock() == L->getLoopLatch()) { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 302 | // Add one to the "backedge-taken" count to get the trip count. |
| 303 | // If this addition may overflow, we have to be more pessimistic and |
| 304 | // cast the induction variable before doing the add. |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 305 | const SCEV *Zero = SE->getConstant(BackedgeTakenCount->getType(), 0); |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 306 | const SCEV *N = |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 307 | SE->getAddExpr(BackedgeTakenCount, |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 308 | SE->getConstant(BackedgeTakenCount->getType(), 1)); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 309 | if ((isa<SCEVConstant>(N) && !N->isZero()) || |
Dan Gohman | 3948d0b | 2010-04-11 19:27:13 +0000 | [diff] [blame] | 310 | SE->isLoopEntryGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) { |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 311 | // No overflow. Cast the sum. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 312 | RHS = SE->getTruncateOrZeroExtend(N, IndVar->getType()); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 313 | } else { |
| 314 | // Potential overflow. Cast before doing the add. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 315 | RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount, |
| 316 | IndVar->getType()); |
| 317 | RHS = SE->getAddExpr(RHS, |
Dan Gohman | deff621 | 2010-05-03 22:09:21 +0000 | [diff] [blame] | 318 | SE->getConstant(IndVar->getType(), 1)); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 319 | } |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 320 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 321 | // The BackedgeTaken expression contains the number of times that the |
| 322 | // backedge branches to the loop header. This is one less than the |
| 323 | // number of times the loop executes, so use the incremented indvar. |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 324 | CmpIndVar = IndVar->getIncomingValueForBlock(L->getExitingBlock()); |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 325 | } else { |
| 326 | // We have to use the preincremented value... |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 327 | RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount, |
| 328 | IndVar->getType()); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 329 | CmpIndVar = IndVar; |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 330 | } |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 331 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 332 | // Expand the code for the iteration count. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 333 | assert(SE->isLoopInvariant(RHS, L) && |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 334 | "Computed iteration count is not loop invariant!"); |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 335 | Value *ExitCnt = Rewriter.expandCodeFor(RHS, IndVar->getType(), BI); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 336 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 337 | // Insert a new icmp_ne or icmp_eq instruction before the branch. |
| 338 | ICmpInst::Predicate Opcode; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 339 | if (L->contains(BI->getSuccessor(0))) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 340 | Opcode = ICmpInst::ICMP_NE; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 341 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 342 | Opcode = ICmpInst::ICMP_EQ; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 343 | |
David Greene | f67ef31 | 2010-01-05 01:27:06 +0000 | [diff] [blame] | 344 | DEBUG(dbgs() << "INDVARS: Rewriting loop exit condition to:\n" |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 345 | << " LHS:" << *CmpIndVar << '\n' |
| 346 | << " op:\t" |
| 347 | << (Opcode == ICmpInst::ICMP_NE ? "!=" : "==") << "\n" |
| 348 | << " RHS:\t" << *RHS << "\n"); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 349 | |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 350 | ICmpInst *Cond = new ICmpInst(BI, Opcode, CmpIndVar, ExitCnt, "exitcond"); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 351 | |
Dan Gohman | 2444080 | 2010-02-22 02:07:36 +0000 | [diff] [blame] | 352 | Value *OrigCond = BI->getCondition(); |
Dan Gohman | 95bdbfa | 2009-05-24 19:11:38 +0000 | [diff] [blame] | 353 | // It's tempting to use replaceAllUsesWith here to fully replace the old |
| 354 | // comparison, but that's not immediately safe, since users of the old |
| 355 | // comparison may not be dominated by the new comparison. Instead, just |
| 356 | // update the branch to use the new comparison; in the common case this |
| 357 | // will make old comparison dead. |
| 358 | BI->setCondition(Cond); |
Andrew Trick | 88e92cf | 2011-04-28 17:30:04 +0000 | [diff] [blame] | 359 | DeadInsts.push_back(OrigCond); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 361 | ++NumLFTR; |
| 362 | Changed = true; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 363 | return Cond; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 366 | /// RewriteLoopExitValues - Check to see if this loop has a computable |
| 367 | /// loop-invariant execution count. If so, this means that we can compute the |
| 368 | /// final value of any expressions that are recurrent in the loop, and |
| 369 | /// substitute the exit values from the loop into any instructions outside of |
| 370 | /// the loop that use the final values of the current expressions. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 371 | /// |
| 372 | /// This is mostly redundant with the regular IndVarSimplify activities that |
| 373 | /// happen later, except that it's more powerful in some cases, because it's |
| 374 | /// able to brute-force evaluate arbitrary instructions as long as they have |
| 375 | /// constant operands at the beginning of the loop. |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 376 | void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEVExpander &Rewriter) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 377 | // Verify the input to the pass in already in LCSSA form. |
Dan Gohman | bbf81d8 | 2010-03-10 19:38:49 +0000 | [diff] [blame] | 378 | assert(L->isLCSSAForm(*DT)); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 379 | |
Devang Patel | b7211a2 | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 380 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 381 | L->getUniqueExitBlocks(ExitBlocks); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 382 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 383 | // Find all values that are computed inside the loop, but used outside of it. |
| 384 | // Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan |
| 385 | // the exit blocks of the loop to find them. |
| 386 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
| 387 | BasicBlock *ExitBB = ExitBlocks[i]; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 388 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 389 | // If there are no PHI nodes in this exit block, then no values defined |
| 390 | // inside the loop are used on this path, skip it. |
| 391 | PHINode *PN = dyn_cast<PHINode>(ExitBB->begin()); |
| 392 | if (!PN) continue; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 393 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 394 | unsigned NumPreds = PN->getNumIncomingValues(); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 396 | // Iterate over all of the PHI nodes. |
| 397 | BasicBlock::iterator BBI = ExitBB->begin(); |
| 398 | while ((PN = dyn_cast<PHINode>(BBI++))) { |
Torok Edwin | 3790fb0 | 2009-05-24 19:36:09 +0000 | [diff] [blame] | 399 | if (PN->use_empty()) |
| 400 | continue; // dead use, don't replace it |
Dan Gohman | 814f2b2 | 2010-02-18 21:34:02 +0000 | [diff] [blame] | 401 | |
| 402 | // SCEV only supports integer expressions for now. |
| 403 | if (!PN->getType()->isIntegerTy() && !PN->getType()->isPointerTy()) |
| 404 | continue; |
| 405 | |
Dale Johannesen | 45a2d7d | 2010-02-19 07:14:22 +0000 | [diff] [blame] | 406 | // It's necessary to tell ScalarEvolution about this explicitly so that |
| 407 | // it can walk the def-use list and forget all SCEVs, as it may not be |
| 408 | // watching the PHI itself. Once the new exit value is in place, there |
| 409 | // may not be a def-use connection between the loop and every instruction |
| 410 | // which got a SCEVAddRecExpr for that loop. |
| 411 | SE->forgetValue(PN); |
| 412 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 413 | // Iterate over all of the values in all the PHI nodes. |
| 414 | for (unsigned i = 0; i != NumPreds; ++i) { |
| 415 | // If the value being merged in is not integer or is not defined |
| 416 | // in the loop, skip it. |
| 417 | Value *InVal = PN->getIncomingValue(i); |
Dan Gohman | 814f2b2 | 2010-02-18 21:34:02 +0000 | [diff] [blame] | 418 | if (!isa<Instruction>(InVal)) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 419 | continue; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 420 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 421 | // If this pred is for a subloop, not L itself, skip it. |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 422 | if (LI->getLoopFor(PN->getIncomingBlock(i)) != L) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 423 | continue; // The Block is in a subloop, skip it. |
| 424 | |
| 425 | // Check that InVal is defined in the loop. |
| 426 | Instruction *Inst = cast<Instruction>(InVal); |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 427 | if (!L->contains(Inst)) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 428 | continue; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 430 | // Okay, this instruction has a user outside of the current loop |
| 431 | // and varies predictably *inside* the loop. Evaluate the value it |
| 432 | // contains when the loop exits, if possible. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 433 | const SCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop()); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 434 | if (!SE->isLoopInvariant(ExitValue, L)) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 435 | continue; |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 436 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 437 | Value *ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), Inst); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 438 | |
David Greene | f67ef31 | 2010-01-05 01:27:06 +0000 | [diff] [blame] | 439 | DEBUG(dbgs() << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal << '\n' |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 440 | << " LoopVal = " << *Inst << "\n"); |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 441 | |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 442 | if (!isValidRewrite(Inst, ExitVal)) { |
| 443 | DeadInsts.push_back(ExitVal); |
| 444 | continue; |
| 445 | } |
| 446 | Changed = true; |
| 447 | ++NumReplaced; |
| 448 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 449 | PN->setIncomingValue(i, ExitVal); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 450 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 451 | // If this instruction is dead now, delete it. |
| 452 | RecursivelyDeleteTriviallyDeadInstructions(Inst); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 453 | |
Dan Gohman | 65d1e2b | 2009-07-14 01:09:02 +0000 | [diff] [blame] | 454 | if (NumPreds == 1) { |
| 455 | // Completely replace a single-pred PHI. This is safe, because the |
| 456 | // NewVal won't be variant in the loop, so we don't need an LCSSA phi |
| 457 | // node anymore. |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 458 | PN->replaceAllUsesWith(ExitVal); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 459 | RecursivelyDeleteTriviallyDeadInstructions(PN); |
Chris Lattner | c9838f2 | 2007-03-03 22:48:48 +0000 | [diff] [blame] | 460 | } |
| 461 | } |
Dan Gohman | 65d1e2b | 2009-07-14 01:09:02 +0000 | [diff] [blame] | 462 | if (NumPreds != 1) { |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 463 | // Clone the PHI and delete the original one. This lets IVUsers and |
| 464 | // any other maps purge the original user from their records. |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 465 | PHINode *NewPN = cast<PHINode>(PN->clone()); |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 466 | NewPN->takeName(PN); |
| 467 | NewPN->insertBefore(PN); |
| 468 | PN->replaceAllUsesWith(NewPN); |
| 469 | PN->eraseFromParent(); |
| 470 | } |
Chris Lattner | c9838f2 | 2007-03-03 22:48:48 +0000 | [diff] [blame] | 471 | } |
| 472 | } |
Dan Gohman | 472fdf7 | 2010-03-20 03:53:53 +0000 | [diff] [blame] | 473 | |
| 474 | // The insertion point instruction may have been deleted; clear it out |
| 475 | // so that the rewriter doesn't trip over it later. |
| 476 | Rewriter.clearInsertPoint(); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 479 | void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 480 | // First step. Check to see if there are any floating-point recurrences. |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 481 | // If there are, change them into integer recurrences, permitting analysis by |
| 482 | // the SCEV routines. |
| 483 | // |
Chris Lattner | f185989 | 2011-01-09 02:16:18 +0000 | [diff] [blame] | 484 | BasicBlock *Header = L->getHeader(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 485 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 486 | SmallVector<WeakVH, 8> PHIs; |
| 487 | for (BasicBlock::iterator I = Header->begin(); |
| 488 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 489 | PHIs.push_back(PN); |
| 490 | |
| 491 | for (unsigned i = 0, e = PHIs.size(); i != e; ++i) |
Gabor Greif | ea4894a | 2010-09-18 11:53:39 +0000 | [diff] [blame] | 492 | if (PHINode *PN = dyn_cast_or_null<PHINode>(&*PHIs[i])) |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 493 | HandleFloatingPointIV(L, PN); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 494 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 495 | // If the loop previously had floating-point IV, ScalarEvolution |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 496 | // may not have been able to compute a trip count. Now that we've done some |
| 497 | // re-writing, the trip count may be computable. |
| 498 | if (Changed) |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 499 | SE->forgetLoop(L); |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 502 | /// SimplifyIVUsers - Iteratively perform simplification on IVUsers within this |
| 503 | /// loop. IVUsers is treated as a worklist. Each successive simplification may |
| 504 | /// push more users which may themselves be candidates for simplification. |
| 505 | /// |
| 506 | /// This is the old approach to IV simplification to be replaced by |
| 507 | /// SimplifyIVUsersNoRewrite. |
| 508 | /// |
| 509 | void IndVarSimplify::SimplifyIVUsers(SCEVExpander &Rewriter) { |
| 510 | // Each round of simplification involves a round of eliminating operations |
| 511 | // followed by a round of widening IVs. A single IVUsers worklist is used |
| 512 | // across all rounds. The inner loop advances the user. If widening exposes |
| 513 | // more uses, then another pass through the outer loop is triggered. |
| 514 | for (IVUsers::iterator I = IU->begin(); I != IU->end(); ++I) { |
| 515 | Instruction *UseInst = I->getUser(); |
| 516 | Value *IVOperand = I->getOperandValToReplace(); |
| 517 | |
| 518 | if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) { |
| 519 | EliminateIVComparison(ICmp, IVOperand); |
| 520 | continue; |
| 521 | } |
| 522 | if (BinaryOperator *Rem = dyn_cast<BinaryOperator>(UseInst)) { |
| 523 | bool IsSigned = Rem->getOpcode() == Instruction::SRem; |
| 524 | if (IsSigned || Rem->getOpcode() == Instruction::URem) { |
Andrew Trick | 4417e53 | 2011-06-21 15:43:52 +0000 | [diff] [blame] | 525 | EliminateIVRemainder(Rem, IVOperand, IsSigned); |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 526 | continue; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 532 | namespace { |
| 533 | // Collect information about induction variables that are used by sign/zero |
| 534 | // extend operations. This information is recorded by CollectExtend and |
| 535 | // provides the input to WidenIV. |
| 536 | struct WideIVInfo { |
| 537 | const Type *WidestNativeType; // Widest integer type created [sz]ext |
| 538 | bool IsSigned; // Was an sext user seen before a zext? |
| 539 | |
| 540 | WideIVInfo() : WidestNativeType(0), IsSigned(false) {} |
| 541 | }; |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | /// CollectExtend - Update information about the induction variable that is |
| 545 | /// extended by this sign or zero extend operation. This is used to determine |
| 546 | /// the final width of the IV before actually widening it. |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 547 | static void CollectExtend(CastInst *Cast, bool IsSigned, WideIVInfo &WI, |
| 548 | ScalarEvolution *SE, const TargetData *TD) { |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 549 | const Type *Ty = Cast->getType(); |
| 550 | uint64_t Width = SE->getTypeSizeInBits(Ty); |
| 551 | if (TD && !TD->isLegalInteger(Width)) |
| 552 | return; |
| 553 | |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 554 | if (!WI.WidestNativeType) { |
| 555 | WI.WidestNativeType = SE->getEffectiveSCEVType(Ty); |
| 556 | WI.IsSigned = IsSigned; |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 557 | return; |
| 558 | } |
| 559 | |
| 560 | // We extend the IV to satisfy the sign of its first user, arbitrarily. |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 561 | if (WI.IsSigned != IsSigned) |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 562 | return; |
| 563 | |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 564 | if (Width > SE->getTypeSizeInBits(WI.WidestNativeType)) |
| 565 | WI.WidestNativeType = SE->getEffectiveSCEVType(Ty); |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | namespace { |
| 569 | /// WidenIV - The goal of this transform is to remove sign and zero extends |
| 570 | /// without creating any new induction variables. To do this, it creates a new |
| 571 | /// phi of the wider type and redirects all users, either removing extends or |
| 572 | /// inserting truncs whenever we stop propagating the type. |
| 573 | /// |
| 574 | class WidenIV { |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 575 | // Parameters |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 576 | PHINode *OrigPhi; |
| 577 | const Type *WideType; |
| 578 | bool IsSigned; |
| 579 | |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 580 | // Context |
| 581 | LoopInfo *LI; |
| 582 | Loop *L; |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 583 | ScalarEvolution *SE; |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 584 | DominatorTree *DT; |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 585 | |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 586 | // Result |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 587 | PHINode *WidePhi; |
| 588 | Instruction *WideInc; |
| 589 | const SCEV *WideIncExpr; |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 590 | SmallVectorImpl<WeakVH> &DeadInsts; |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 591 | |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 592 | SmallPtrSet<Instruction*,16> Widened; |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 593 | |
| 594 | public: |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 595 | WidenIV(PHINode *PN, const WideIVInfo &WI, LoopInfo *LInfo, |
| 596 | ScalarEvolution *SEv, DominatorTree *DTree, |
Andrew Trick | fcdc9a4 | 2011-05-26 00:46:11 +0000 | [diff] [blame] | 597 | SmallVectorImpl<WeakVH> &DI) : |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 598 | OrigPhi(PN), |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 599 | WideType(WI.WidestNativeType), |
| 600 | IsSigned(WI.IsSigned), |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 601 | LI(LInfo), |
| 602 | L(LI->getLoopFor(OrigPhi->getParent())), |
| 603 | SE(SEv), |
Andrew Trick | fcdc9a4 | 2011-05-26 00:46:11 +0000 | [diff] [blame] | 604 | DT(DTree), |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 605 | WidePhi(0), |
| 606 | WideInc(0), |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 607 | WideIncExpr(0), |
| 608 | DeadInsts(DI) { |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 609 | assert(L->getHeader() == OrigPhi->getParent() && "Phi must be an IV"); |
| 610 | } |
| 611 | |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 612 | PHINode *CreateWideIV(SCEVExpander &Rewriter); |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 613 | |
| 614 | protected: |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 615 | Instruction *CloneIVUser(Instruction *NarrowUse, |
| 616 | Instruction *NarrowDef, |
| 617 | Instruction *WideDef); |
| 618 | |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 619 | const SCEVAddRecExpr *GetWideRecurrence(Instruction *NarrowUse); |
| 620 | |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 621 | Instruction *WidenIVUse(Instruction *NarrowUse, |
| 622 | Instruction *NarrowDef, |
| 623 | Instruction *WideDef); |
| 624 | }; |
| 625 | } // anonymous namespace |
| 626 | |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 627 | static Value *getExtend( Value *NarrowOper, const Type *WideType, |
| 628 | bool IsSigned, IRBuilder<> &Builder) { |
| 629 | return IsSigned ? Builder.CreateSExt(NarrowOper, WideType) : |
| 630 | Builder.CreateZExt(NarrowOper, WideType); |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /// CloneIVUser - Instantiate a wide operation to replace a narrow |
| 634 | /// operation. This only needs to handle operations that can evaluation to |
| 635 | /// SCEVAddRec. It can safely return 0 for any operation we decide not to clone. |
| 636 | Instruction *WidenIV::CloneIVUser(Instruction *NarrowUse, |
| 637 | Instruction *NarrowDef, |
| 638 | Instruction *WideDef) { |
| 639 | unsigned Opcode = NarrowUse->getOpcode(); |
| 640 | switch (Opcode) { |
| 641 | default: |
| 642 | return 0; |
| 643 | case Instruction::Add: |
| 644 | case Instruction::Mul: |
| 645 | case Instruction::UDiv: |
| 646 | case Instruction::Sub: |
| 647 | case Instruction::And: |
| 648 | case Instruction::Or: |
| 649 | case Instruction::Xor: |
| 650 | case Instruction::Shl: |
| 651 | case Instruction::LShr: |
| 652 | case Instruction::AShr: |
| 653 | DEBUG(dbgs() << "Cloning IVUser: " << *NarrowUse << "\n"); |
| 654 | |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 655 | IRBuilder<> Builder(NarrowUse); |
| 656 | |
| 657 | // Replace NarrowDef operands with WideDef. Otherwise, we don't know |
| 658 | // anything about the narrow operand yet so must insert a [sz]ext. It is |
| 659 | // probably loop invariant and will be folded or hoisted. If it actually |
| 660 | // comes from a widened IV, it should be removed during a future call to |
| 661 | // WidenIVUse. |
| 662 | Value *LHS = (NarrowUse->getOperand(0) == NarrowDef) ? WideDef : |
| 663 | getExtend(NarrowUse->getOperand(0), WideType, IsSigned, Builder); |
| 664 | Value *RHS = (NarrowUse->getOperand(1) == NarrowDef) ? WideDef : |
| 665 | getExtend(NarrowUse->getOperand(1), WideType, IsSigned, Builder); |
| 666 | |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 667 | BinaryOperator *NarrowBO = cast<BinaryOperator>(NarrowUse); |
| 668 | BinaryOperator *WideBO = BinaryOperator::Create(NarrowBO->getOpcode(), |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 669 | LHS, RHS, |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 670 | NarrowBO->getName()); |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 671 | Builder.Insert(WideBO); |
| 672 | if (NarrowBO->hasNoUnsignedWrap()) WideBO->setHasNoUnsignedWrap(); |
| 673 | if (NarrowBO->hasNoSignedWrap()) WideBO->setHasNoSignedWrap(); |
| 674 | |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 675 | return WideBO; |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 676 | } |
| 677 | llvm_unreachable(0); |
| 678 | } |
| 679 | |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 680 | // GetWideRecurrence - Is this instruction potentially interesting from IVUsers' |
| 681 | // perspective after widening it's type? In other words, can the extend be |
| 682 | // safely hoisted out of the loop with SCEV reducing the value to a recurrence |
| 683 | // on the same loop. If so, return the sign or zero extended |
| 684 | // recurrence. Otherwise return NULL. |
| 685 | const SCEVAddRecExpr *WidenIV::GetWideRecurrence(Instruction *NarrowUse) { |
| 686 | if (!SE->isSCEVable(NarrowUse->getType())) |
| 687 | return 0; |
| 688 | |
| 689 | const SCEV *NarrowExpr = SE->getSCEV(NarrowUse); |
| 690 | const SCEV *WideExpr = IsSigned ? |
| 691 | SE->getSignExtendExpr(NarrowExpr, WideType) : |
| 692 | SE->getZeroExtendExpr(NarrowExpr, WideType); |
| 693 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(WideExpr); |
| 694 | if (!AddRec || AddRec->getLoop() != L) |
| 695 | return 0; |
| 696 | |
| 697 | return AddRec; |
| 698 | } |
| 699 | |
Andrew Trick | fcdc9a4 | 2011-05-26 00:46:11 +0000 | [diff] [blame] | 700 | /// HoistStep - Attempt to hoist an IV increment above a potential use. |
| 701 | /// |
| 702 | /// To successfully hoist, two criteria must be met: |
| 703 | /// - IncV operands dominate InsertPos and |
| 704 | /// - InsertPos dominates IncV |
| 705 | /// |
| 706 | /// Meeting the second condition means that we don't need to check all of IncV's |
| 707 | /// existing uses (it's moving up in the domtree). |
| 708 | /// |
| 709 | /// This does not yet recursively hoist the operands, although that would |
| 710 | /// not be difficult. |
| 711 | static bool HoistStep(Instruction *IncV, Instruction *InsertPos, |
| 712 | const DominatorTree *DT) |
| 713 | { |
| 714 | if (DT->dominates(IncV, InsertPos)) |
| 715 | return true; |
| 716 | |
| 717 | if (!DT->dominates(InsertPos->getParent(), IncV->getParent())) |
| 718 | return false; |
| 719 | |
| 720 | if (IncV->mayHaveSideEffects()) |
| 721 | return false; |
| 722 | |
| 723 | // Attempt to hoist IncV |
| 724 | for (User::op_iterator OI = IncV->op_begin(), OE = IncV->op_end(); |
| 725 | OI != OE; ++OI) { |
| 726 | Instruction *OInst = dyn_cast<Instruction>(OI); |
| 727 | if (OInst && !DT->dominates(OInst, InsertPos)) |
| 728 | return false; |
| 729 | } |
| 730 | IncV->moveBefore(InsertPos); |
| 731 | return true; |
| 732 | } |
| 733 | |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 734 | /// WidenIVUse - Determine whether an individual user of the narrow IV can be |
| 735 | /// widened. If so, return the wide clone of the user. |
| 736 | Instruction *WidenIV::WidenIVUse(Instruction *NarrowUse, |
| 737 | Instruction *NarrowDef, |
| 738 | Instruction *WideDef) { |
| 739 | // To be consistent with IVUsers, stop traversing the def-use chain at |
| 740 | // inner-loop phis or post-loop phis. |
| 741 | if (isa<PHINode>(NarrowUse) && LI->getLoopFor(NarrowUse->getParent()) != L) |
| 742 | return 0; |
| 743 | |
| 744 | // Handle data flow merges and bizarre phi cycles. |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 745 | if (!Widened.insert(NarrowUse)) |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 746 | return 0; |
| 747 | |
| 748 | // Our raison d'etre! Eliminate sign and zero extension. |
| 749 | if (IsSigned ? isa<SExtInst>(NarrowUse) : isa<ZExtInst>(NarrowUse)) { |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 750 | Value *NewDef = WideDef; |
| 751 | if (NarrowUse->getType() != WideType) { |
| 752 | unsigned CastWidth = SE->getTypeSizeInBits(NarrowUse->getType()); |
| 753 | unsigned IVWidth = SE->getTypeSizeInBits(WideType); |
| 754 | if (CastWidth < IVWidth) { |
| 755 | // The cast isn't as wide as the IV, so insert a Trunc. |
| 756 | IRBuilder<> Builder(NarrowUse); |
| 757 | NewDef = Builder.CreateTrunc(WideDef, NarrowUse->getType()); |
| 758 | } |
| 759 | else { |
| 760 | // A wider extend was hidden behind a narrower one. This may induce |
| 761 | // another round of IV widening in which the intermediate IV becomes |
| 762 | // dead. It should be very rare. |
| 763 | DEBUG(dbgs() << "INDVARS: New IV " << *WidePhi |
| 764 | << " not wide enough to subsume " << *NarrowUse << "\n"); |
| 765 | NarrowUse->replaceUsesOfWith(NarrowDef, WideDef); |
| 766 | NewDef = NarrowUse; |
| 767 | } |
| 768 | } |
| 769 | if (NewDef != NarrowUse) { |
| 770 | DEBUG(dbgs() << "INDVARS: eliminating " << *NarrowUse |
| 771 | << " replaced by " << *WideDef << "\n"); |
| 772 | ++NumElimExt; |
| 773 | NarrowUse->replaceAllUsesWith(NewDef); |
| 774 | DeadInsts.push_back(NarrowUse); |
| 775 | } |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 776 | // Now that the extend is gone, we want to expose it's uses for potential |
| 777 | // further simplification. We don't need to directly inform SimplifyIVUsers |
| 778 | // of the new users, because their parent IV will be processed later as a |
| 779 | // new loop phi. If we preserved IVUsers analysis, we would also want to |
| 780 | // push the uses of WideDef here. |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 781 | |
| 782 | // No further widening is needed. The deceased [sz]ext had done it for us. |
| 783 | return 0; |
| 784 | } |
| 785 | const SCEVAddRecExpr *WideAddRec = GetWideRecurrence(NarrowUse); |
| 786 | if (!WideAddRec) { |
| 787 | // This user does not evaluate to a recurence after widening, so don't |
| 788 | // follow it. Instead insert a Trunc to kill off the original use, |
| 789 | // eventually isolating the original narrow IV so it can be removed. |
| 790 | IRBuilder<> Builder(NarrowUse); |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 791 | Value *Trunc = Builder.CreateTrunc(WideDef, NarrowDef->getType()); |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 792 | NarrowUse->replaceUsesOfWith(NarrowDef, Trunc); |
| 793 | return 0; |
| 794 | } |
Andrew Trick | fcdc9a4 | 2011-05-26 00:46:11 +0000 | [diff] [blame] | 795 | // Reuse the IV increment that SCEVExpander created as long as it dominates |
| 796 | // NarrowUse. |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 797 | Instruction *WideUse = 0; |
Andrew Trick | fcdc9a4 | 2011-05-26 00:46:11 +0000 | [diff] [blame] | 798 | if (WideAddRec == WideIncExpr && HoistStep(WideInc, NarrowUse, DT)) { |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 799 | WideUse = WideInc; |
| 800 | } |
| 801 | else { |
| 802 | WideUse = CloneIVUser(NarrowUse, NarrowDef, WideDef); |
| 803 | if (!WideUse) |
| 804 | return 0; |
| 805 | } |
| 806 | // GetWideRecurrence ensured that the narrow expression could be extended |
| 807 | // outside the loop without overflow. This suggests that the wide use |
| 808 | // evaluates to the same expression as the extended narrow use, but doesn't |
| 809 | // absolutely guarantee it. Hence the following failsafe check. In rare cases |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 810 | // where it fails, we simply throw away the newly created wide use. |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 811 | if (WideAddRec != SE->getSCEV(WideUse)) { |
| 812 | DEBUG(dbgs() << "Wide use expression mismatch: " << *WideUse |
| 813 | << ": " << *SE->getSCEV(WideUse) << " != " << *WideAddRec << "\n"); |
| 814 | DeadInsts.push_back(WideUse); |
| 815 | return 0; |
| 816 | } |
| 817 | |
| 818 | // Returning WideUse pushes it on the worklist. |
| 819 | return WideUse; |
| 820 | } |
| 821 | |
| 822 | /// CreateWideIV - Process a single induction variable. First use the |
| 823 | /// SCEVExpander to create a wide induction variable that evaluates to the same |
| 824 | /// recurrence as the original narrow IV. Then use a worklist to forward |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 825 | /// traverse the narrow IV's def-use chain. After WidenIVUse has processed all |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 826 | /// interesting IV users, the narrow IV will be isolated for removal by |
| 827 | /// DeleteDeadPHIs. |
| 828 | /// |
| 829 | /// It would be simpler to delete uses as they are processed, but we must avoid |
| 830 | /// invalidating SCEV expressions. |
| 831 | /// |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 832 | PHINode *WidenIV::CreateWideIV(SCEVExpander &Rewriter) { |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 833 | // Is this phi an induction variable? |
| 834 | const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(OrigPhi)); |
| 835 | if (!AddRec) |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 836 | return NULL; |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 837 | |
| 838 | // Widen the induction variable expression. |
| 839 | const SCEV *WideIVExpr = IsSigned ? |
| 840 | SE->getSignExtendExpr(AddRec, WideType) : |
| 841 | SE->getZeroExtendExpr(AddRec, WideType); |
| 842 | |
| 843 | assert(SE->getEffectiveSCEVType(WideIVExpr->getType()) == WideType && |
| 844 | "Expect the new IV expression to preserve its type"); |
| 845 | |
| 846 | // Can the IV be extended outside the loop without overflow? |
| 847 | AddRec = dyn_cast<SCEVAddRecExpr>(WideIVExpr); |
| 848 | if (!AddRec || AddRec->getLoop() != L) |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 849 | return NULL; |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 850 | |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 851 | // An AddRec must have loop-invariant operands. Since this AddRec is |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 852 | // materialized by a loop header phi, the expression cannot have any post-loop |
| 853 | // operands, so they must dominate the loop header. |
| 854 | assert(SE->properlyDominates(AddRec->getStart(), L->getHeader()) && |
| 855 | SE->properlyDominates(AddRec->getStepRecurrence(*SE), L->getHeader()) |
| 856 | && "Loop header phi recurrence inputs do not dominate the loop"); |
| 857 | |
| 858 | // The rewriter provides a value for the desired IV expression. This may |
| 859 | // either find an existing phi or materialize a new one. Either way, we |
| 860 | // expect a well-formed cyclic phi-with-increments. i.e. any operand not part |
| 861 | // of the phi-SCC dominates the loop entry. |
| 862 | Instruction *InsertPt = L->getHeader()->begin(); |
| 863 | WidePhi = cast<PHINode>(Rewriter.expandCodeFor(AddRec, WideType, InsertPt)); |
| 864 | |
| 865 | // Remembering the WideIV increment generated by SCEVExpander allows |
| 866 | // WidenIVUse to reuse it when widening the narrow IV's increment. We don't |
| 867 | // employ a general reuse mechanism because the call above is the only call to |
| 868 | // SCEVExpander. Henceforth, we produce 1-to-1 narrow to wide uses. |
Andrew Trick | fcdc9a4 | 2011-05-26 00:46:11 +0000 | [diff] [blame] | 869 | if (BasicBlock *LatchBlock = L->getLoopLatch()) { |
| 870 | WideInc = |
| 871 | cast<Instruction>(WidePhi->getIncomingValueForBlock(LatchBlock)); |
| 872 | WideIncExpr = SE->getSCEV(WideInc); |
| 873 | } |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 874 | |
| 875 | DEBUG(dbgs() << "Wide IV: " << *WidePhi << "\n"); |
| 876 | ++NumWidened; |
| 877 | |
| 878 | // Traverse the def-use chain using a worklist starting at the original IV. |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 879 | assert(Widened.empty() && "expect initial state" ); |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 880 | |
Andrew Trick | fcdc9a4 | 2011-05-26 00:46:11 +0000 | [diff] [blame] | 881 | // Each worklist entry has a Narrow def-use link and Wide def. |
| 882 | SmallVector<std::pair<Use *, Instruction *>, 8> NarrowIVUsers; |
| 883 | for (Value::use_iterator UI = OrigPhi->use_begin(), |
| 884 | UE = OrigPhi->use_end(); UI != UE; ++UI) { |
| 885 | NarrowIVUsers.push_back(std::make_pair(&UI.getUse(), WidePhi)); |
| 886 | } |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 887 | while (!NarrowIVUsers.empty()) { |
Andrew Trick | fcdc9a4 | 2011-05-26 00:46:11 +0000 | [diff] [blame] | 888 | Use *NarrowDefUse; |
| 889 | Instruction *WideDef; |
| 890 | tie(NarrowDefUse, WideDef) = NarrowIVUsers.pop_back_val(); |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 891 | |
Andrew Trick | fcdc9a4 | 2011-05-26 00:46:11 +0000 | [diff] [blame] | 892 | // Process a def-use edge. This may replace the use, so don't hold a |
| 893 | // use_iterator across it. |
| 894 | Instruction *NarrowDef = cast<Instruction>(NarrowDefUse->get()); |
| 895 | Instruction *NarrowUse = cast<Instruction>(NarrowDefUse->getUser()); |
| 896 | Instruction *WideUse = WidenIVUse(NarrowUse, NarrowDef, WideDef); |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 897 | |
Andrew Trick | fcdc9a4 | 2011-05-26 00:46:11 +0000 | [diff] [blame] | 898 | // Follow all def-use edges from the previous narrow use. |
| 899 | if (WideUse) { |
| 900 | for (Value::use_iterator UI = NarrowUse->use_begin(), |
| 901 | UE = NarrowUse->use_end(); UI != UE; ++UI) { |
| 902 | NarrowIVUsers.push_back(std::make_pair(&UI.getUse(), WideUse)); |
| 903 | } |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 904 | } |
Andrew Trick | fcdc9a4 | 2011-05-26 00:46:11 +0000 | [diff] [blame] | 905 | // WidenIVUse may have removed the def-use edge. |
| 906 | if (NarrowDef->use_empty()) |
| 907 | DeadInsts.push_back(NarrowDef); |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 908 | } |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 909 | return WidePhi; |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 910 | } |
| 911 | |
Andrew Trick | aeee461 | 2011-05-12 00:04:28 +0000 | [diff] [blame] | 912 | void IndVarSimplify::EliminateIVComparison(ICmpInst *ICmp, Value *IVOperand) { |
| 913 | unsigned IVOperIdx = 0; |
| 914 | ICmpInst::Predicate Pred = ICmp->getPredicate(); |
| 915 | if (IVOperand != ICmp->getOperand(0)) { |
| 916 | // Swapped |
| 917 | assert(IVOperand == ICmp->getOperand(1) && "Can't find IVOperand"); |
| 918 | IVOperIdx = 1; |
| 919 | Pred = ICmpInst::getSwappedPredicate(Pred); |
Dan Gohman | a590b79 | 2010-04-13 01:46:36 +0000 | [diff] [blame] | 920 | } |
Andrew Trick | aeee461 | 2011-05-12 00:04:28 +0000 | [diff] [blame] | 921 | |
| 922 | // Get the SCEVs for the ICmp operands. |
| 923 | const SCEV *S = SE->getSCEV(ICmp->getOperand(IVOperIdx)); |
| 924 | const SCEV *X = SE->getSCEV(ICmp->getOperand(1 - IVOperIdx)); |
| 925 | |
| 926 | // Simplify unnecessary loops away. |
| 927 | const Loop *ICmpLoop = LI->getLoopFor(ICmp->getParent()); |
| 928 | S = SE->getSCEVAtScope(S, ICmpLoop); |
| 929 | X = SE->getSCEVAtScope(X, ICmpLoop); |
| 930 | |
| 931 | // If the condition is always true or always false, replace it with |
| 932 | // a constant value. |
| 933 | if (SE->isKnownPredicate(Pred, S, X)) |
| 934 | ICmp->replaceAllUsesWith(ConstantInt::getTrue(ICmp->getContext())); |
| 935 | else if (SE->isKnownPredicate(ICmpInst::getInversePredicate(Pred), S, X)) |
| 936 | ICmp->replaceAllUsesWith(ConstantInt::getFalse(ICmp->getContext())); |
| 937 | else |
| 938 | return; |
| 939 | |
| 940 | DEBUG(dbgs() << "INDVARS: Eliminated comparison: " << *ICmp << '\n'); |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 941 | ++NumElimCmp; |
Andrew Trick | 074397d | 2011-05-20 03:37:48 +0000 | [diff] [blame] | 942 | Changed = true; |
Andrew Trick | aeee461 | 2011-05-12 00:04:28 +0000 | [diff] [blame] | 943 | DeadInsts.push_back(ICmp); |
| 944 | } |
| 945 | |
| 946 | void IndVarSimplify::EliminateIVRemainder(BinaryOperator *Rem, |
| 947 | Value *IVOperand, |
Andrew Trick | 4417e53 | 2011-06-21 15:43:52 +0000 | [diff] [blame] | 948 | bool IsSigned) { |
Andrew Trick | aeee461 | 2011-05-12 00:04:28 +0000 | [diff] [blame] | 949 | // We're only interested in the case where we know something about |
| 950 | // the numerator. |
| 951 | if (IVOperand != Rem->getOperand(0)) |
| 952 | return; |
| 953 | |
| 954 | // Get the SCEVs for the ICmp operands. |
| 955 | const SCEV *S = SE->getSCEV(Rem->getOperand(0)); |
| 956 | const SCEV *X = SE->getSCEV(Rem->getOperand(1)); |
| 957 | |
| 958 | // Simplify unnecessary loops away. |
| 959 | const Loop *ICmpLoop = LI->getLoopFor(Rem->getParent()); |
| 960 | S = SE->getSCEVAtScope(S, ICmpLoop); |
| 961 | X = SE->getSCEVAtScope(X, ICmpLoop); |
| 962 | |
| 963 | // i % n --> i if i is in [0,n). |
Andrew Trick | 074397d | 2011-05-20 03:37:48 +0000 | [diff] [blame] | 964 | if ((!IsSigned || SE->isKnownNonNegative(S)) && |
| 965 | SE->isKnownPredicate(IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
Andrew Trick | aeee461 | 2011-05-12 00:04:28 +0000 | [diff] [blame] | 966 | S, X)) |
| 967 | Rem->replaceAllUsesWith(Rem->getOperand(0)); |
| 968 | else { |
| 969 | // (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n). |
| 970 | const SCEV *LessOne = |
| 971 | SE->getMinusSCEV(S, SE->getConstant(S->getType(), 1)); |
Andrew Trick | 074397d | 2011-05-20 03:37:48 +0000 | [diff] [blame] | 972 | if (IsSigned && !SE->isKnownNonNegative(LessOne)) |
Andrew Trick | aeee461 | 2011-05-12 00:04:28 +0000 | [diff] [blame] | 973 | return; |
| 974 | |
Andrew Trick | 074397d | 2011-05-20 03:37:48 +0000 | [diff] [blame] | 975 | if (!SE->isKnownPredicate(IsSigned ? |
Andrew Trick | aeee461 | 2011-05-12 00:04:28 +0000 | [diff] [blame] | 976 | ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, |
| 977 | LessOne, X)) |
| 978 | return; |
| 979 | |
| 980 | ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ, |
| 981 | Rem->getOperand(0), Rem->getOperand(1), |
| 982 | "tmp"); |
| 983 | SelectInst *Sel = |
| 984 | SelectInst::Create(ICmp, |
| 985 | ConstantInt::get(Rem->getType(), 0), |
| 986 | Rem->getOperand(0), "tmp", Rem); |
| 987 | Rem->replaceAllUsesWith(Sel); |
| 988 | } |
| 989 | |
| 990 | // Inform IVUsers about the new users. |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 991 | if (IU) { |
| 992 | if (Instruction *I = dyn_cast<Instruction>(Rem->getOperand(0))) |
Andrew Trick | 4417e53 | 2011-06-21 15:43:52 +0000 | [diff] [blame] | 993 | IU->AddUsersIfInteresting(I); |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 994 | } |
Andrew Trick | aeee461 | 2011-05-12 00:04:28 +0000 | [diff] [blame] | 995 | DEBUG(dbgs() << "INDVARS: Simplified rem: " << *Rem << '\n'); |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 996 | ++NumElimRem; |
Andrew Trick | 074397d | 2011-05-20 03:37:48 +0000 | [diff] [blame] | 997 | Changed = true; |
Andrew Trick | aeee461 | 2011-05-12 00:04:28 +0000 | [diff] [blame] | 998 | DeadInsts.push_back(Rem); |
Dan Gohman | a590b79 | 2010-04-13 01:46:36 +0000 | [diff] [blame] | 999 | } |
| 1000 | |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 1001 | /// EliminateIVUser - Eliminate an operation that consumes a simple IV and has |
| 1002 | /// no observable side-effect given the range of IV values. |
| 1003 | bool IndVarSimplify::EliminateIVUser(Instruction *UseInst, |
| 1004 | Instruction *IVOperand) { |
| 1005 | if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) { |
| 1006 | EliminateIVComparison(ICmp, IVOperand); |
| 1007 | return true; |
| 1008 | } |
| 1009 | if (BinaryOperator *Rem = dyn_cast<BinaryOperator>(UseInst)) { |
| 1010 | bool IsSigned = Rem->getOpcode() == Instruction::SRem; |
| 1011 | if (IsSigned || Rem->getOpcode() == Instruction::URem) { |
Andrew Trick | 4417e53 | 2011-06-21 15:43:52 +0000 | [diff] [blame] | 1012 | EliminateIVRemainder(Rem, IVOperand, IsSigned); |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 1013 | return true; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | // Eliminate any operation that SCEV can prove is an identity function. |
| 1018 | if (!SE->isSCEVable(UseInst->getType()) || |
| 1019 | (SE->getSCEV(UseInst) != SE->getSCEV(IVOperand))) |
| 1020 | return false; |
| 1021 | |
| 1022 | UseInst->replaceAllUsesWith(IVOperand); |
| 1023 | |
| 1024 | DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n'); |
| 1025 | ++NumElimIdentity; |
| 1026 | Changed = true; |
| 1027 | DeadInsts.push_back(UseInst); |
| 1028 | return true; |
| 1029 | } |
| 1030 | |
| 1031 | /// pushIVUsers - Add all uses of Def to the current IV's worklist. |
| 1032 | /// |
| 1033 | void IndVarSimplify::pushIVUsers(Instruction *Def) { |
| 1034 | |
| 1035 | for (Value::use_iterator UI = Def->use_begin(), E = Def->use_end(); |
| 1036 | UI != E; ++UI) { |
| 1037 | Instruction *User = cast<Instruction>(*UI); |
| 1038 | |
| 1039 | // Avoid infinite or exponential worklist processing. |
| 1040 | // Also ensure unique worklist users. |
| 1041 | if (Simplified.insert(User)) |
| 1042 | SimpleIVUsers.push_back(std::make_pair(User, Def)); |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | /// isSimpleIVUser - Return true if this instruction generates a simple SCEV |
| 1047 | /// expression in terms of that IV. |
| 1048 | /// |
| 1049 | /// This is similar to IVUsers' isInsteresting() but processes each instruction |
| 1050 | /// non-recursively when the operand is already known to be a simpleIVUser. |
| 1051 | /// |
| 1052 | bool IndVarSimplify::isSimpleIVUser(Instruction *I, const Loop *L) { |
| 1053 | if (!SE->isSCEVable(I->getType())) |
| 1054 | return false; |
| 1055 | |
| 1056 | // Get the symbolic expression for this instruction. |
| 1057 | const SCEV *S = SE->getSCEV(I); |
| 1058 | |
| 1059 | // Only consider affine recurrences. |
| 1060 | const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S); |
| 1061 | if (AR && AR->getLoop() == L) |
| 1062 | return true; |
| 1063 | |
| 1064 | return false; |
| 1065 | } |
| 1066 | |
| 1067 | /// SimplifyIVUsersNoRewrite - Iteratively perform simplification on a worklist |
| 1068 | /// of IV users. Each successive simplification may push more users which may |
| 1069 | /// themselves be candidates for simplification. |
| 1070 | /// |
| 1071 | /// The "NoRewrite" algorithm does not require IVUsers analysis. Instead, it |
| 1072 | /// simplifies instructions in-place during analysis. Rather than rewriting |
| 1073 | /// induction variables bottom-up from their users, it transforms a chain of |
| 1074 | /// IVUsers top-down, updating the IR only when it encouters a clear |
| 1075 | /// optimization opportunitiy. A SCEVExpander "Rewriter" instance is still |
| 1076 | /// needed, but only used to generate a new IV (phi) of wider type for sign/zero |
| 1077 | /// extend elimination. |
| 1078 | /// |
| 1079 | /// Once DisableIVRewrite is default, LSR will be the only client of IVUsers. |
| 1080 | /// |
| 1081 | void IndVarSimplify::SimplifyIVUsersNoRewrite(Loop *L, SCEVExpander &Rewriter) { |
| 1082 | // Simplification is performed independently for each IV, as represented by a |
| 1083 | // loop header phi. Each round of simplification first iterates through the |
| 1084 | // SimplifyIVUsers worklist, then determines whether the current IV should be |
| 1085 | // widened. Widening adds a new phi to LoopPhis, inducing another round of |
| 1086 | // simplification on the wide IV. |
| 1087 | SmallVector<PHINode*, 8> LoopPhis; |
| 1088 | for (BasicBlock::iterator I = L->getHeader()->begin(); isa<PHINode>(I); ++I) { |
| 1089 | LoopPhis.push_back(cast<PHINode>(I)); |
| 1090 | } |
| 1091 | while (!LoopPhis.empty()) { |
| 1092 | CurrIV = LoopPhis.pop_back_val(); |
| 1093 | Simplified.clear(); |
| 1094 | assert(SimpleIVUsers.empty() && "expect empty IV users list"); |
| 1095 | |
| 1096 | WideIVInfo WI; |
| 1097 | |
| 1098 | pushIVUsers(CurrIV); |
| 1099 | |
| 1100 | while (!SimpleIVUsers.empty()) { |
| 1101 | Instruction *UseInst, *Operand; |
| 1102 | tie(UseInst, Operand) = SimpleIVUsers.pop_back_val(); |
| 1103 | |
| 1104 | if (EliminateIVUser(UseInst, Operand)) { |
| 1105 | pushIVUsers(Operand); |
| 1106 | continue; |
| 1107 | } |
| 1108 | if (CastInst *Cast = dyn_cast<CastInst>(UseInst)) { |
| 1109 | bool IsSigned = Cast->getOpcode() == Instruction::SExt; |
| 1110 | if (IsSigned || Cast->getOpcode() == Instruction::ZExt) { |
| 1111 | CollectExtend(Cast, IsSigned, WI, SE, TD); |
| 1112 | } |
| 1113 | continue; |
| 1114 | } |
| 1115 | if (isSimpleIVUser(UseInst, L)) { |
| 1116 | pushIVUsers(UseInst); |
| 1117 | } |
| 1118 | } |
| 1119 | if (WI.WidestNativeType) { |
| 1120 | WidenIV Widener(CurrIV, WI, LI, SE, DT, DeadInsts); |
| 1121 | if (PHINode *WidePhi = Widener.CreateWideIV(Rewriter)) { |
| 1122 | Changed = true; |
| 1123 | LoopPhis.push_back(WidePhi); |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | } |
| 1128 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 1129 | bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) { |
Dan Gohman | a528382 | 2010-06-18 01:35:11 +0000 | [diff] [blame] | 1130 | // If LoopSimplify form is not available, stay out of trouble. Some notes: |
| 1131 | // - LSR currently only supports LoopSimplify-form loops. Indvars' |
| 1132 | // canonicalization can be a pessimization without LSR to "clean up" |
| 1133 | // afterwards. |
| 1134 | // - We depend on having a preheader; in particular, |
| 1135 | // Loop::getCanonicalInductionVariable only supports loops with preheaders, |
| 1136 | // and we're in trouble if we can't find the induction variable even when |
| 1137 | // we've manually inserted one. |
| 1138 | if (!L->isLoopSimplifyForm()) |
| 1139 | return false; |
| 1140 | |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 1141 | if (!DisableIVRewrite) |
| 1142 | IU = &getAnalysis<IVUsers>(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 1143 | LI = &getAnalysis<LoopInfo>(); |
| 1144 | SE = &getAnalysis<ScalarEvolution>(); |
Dan Gohman | de53dc0 | 2009-06-27 05:16:57 +0000 | [diff] [blame] | 1145 | DT = &getAnalysis<DominatorTree>(); |
Andrew Trick | 37da408 | 2011-05-04 02:10:13 +0000 | [diff] [blame] | 1146 | TD = getAnalysisIfAvailable<TargetData>(); |
| 1147 | |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 1148 | CurrIV = NULL; |
| 1149 | Simplified.clear(); |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 1150 | DeadInsts.clear(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 1151 | Changed = false; |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 1152 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 1153 | // If there are any floating-point recurrences, attempt to |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 1154 | // transform them to use integer recurrences. |
| 1155 | RewriteNonIntegerIVs(L); |
| 1156 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 1157 | const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L); |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 1158 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1159 | // Create a rewriter object which we'll use to transform the code with. |
| 1160 | SCEVExpander Rewriter(*SE); |
Andrew Trick | 37da408 | 2011-05-04 02:10:13 +0000 | [diff] [blame] | 1161 | if (DisableIVRewrite) |
| 1162 | Rewriter.disableCanonicalMode(); |
| 1163 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 1164 | // Check to see if this loop has a computable loop-invariant execution count. |
| 1165 | // If so, this means that we can compute the final value of any expressions |
| 1166 | // that are recurrent in the loop, and substitute the exit values from the |
| 1167 | // loop into any instructions outside of the loop that use the final values of |
| 1168 | // the current expressions. |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 1169 | // |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 1170 | if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1171 | RewriteLoopExitValues(L, Rewriter); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 1172 | |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 1173 | // Eliminate redundant IV users. |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 1174 | if (DisableIVRewrite) |
| 1175 | SimplifyIVUsersNoRewrite(L, Rewriter); |
| 1176 | else |
| 1177 | SimplifyIVUsers(Rewriter); |
Dan Gohman | a590b79 | 2010-04-13 01:46:36 +0000 | [diff] [blame] | 1178 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1179 | // Compute the type of the largest recurrence expression, and decide whether |
| 1180 | // a canonical induction variable should be inserted. |
Andrew Trick | f85092c | 2011-05-20 18:25:42 +0000 | [diff] [blame] | 1181 | const Type *LargestType = 0; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1182 | bool NeedCannIV = false; |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 1183 | bool ExpandBECount = canExpandBackedgeTakenCount(L, SE); |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 1184 | if (ExpandBECount) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1185 | // If we have a known trip count and a single exit block, we'll be |
| 1186 | // rewriting the loop exit test condition below, which requires a |
| 1187 | // canonical induction variable. |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 1188 | NeedCannIV = true; |
| 1189 | const Type *Ty = BackedgeTakenCount->getType(); |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 1190 | if (DisableIVRewrite) { |
| 1191 | // In this mode, SimplifyIVUsers may have already widened the IV used by |
| 1192 | // the backedge test and inserted a Trunc on the compare's operand. Get |
| 1193 | // the wider type to avoid creating a redundant narrow IV only used by the |
| 1194 | // loop test. |
| 1195 | LargestType = getBackedgeIVType(L); |
| 1196 | } |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 1197 | if (!LargestType || |
| 1198 | SE->getTypeSizeInBits(Ty) > |
| 1199 | SE->getTypeSizeInBits(LargestType)) |
| 1200 | LargestType = SE->getEffectiveSCEVType(Ty); |
Chris Lattner | f50af08 | 2004-04-17 18:08:33 +0000 | [diff] [blame] | 1201 | } |
Andrew Trick | 37da408 | 2011-05-04 02:10:13 +0000 | [diff] [blame] | 1202 | if (!DisableIVRewrite) { |
| 1203 | for (IVUsers::const_iterator I = IU->begin(), E = IU->end(); I != E; ++I) { |
| 1204 | NeedCannIV = true; |
| 1205 | const Type *Ty = |
| 1206 | SE->getEffectiveSCEVType(I->getOperandValToReplace()->getType()); |
| 1207 | if (!LargestType || |
| 1208 | SE->getTypeSizeInBits(Ty) > |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 1209 | SE->getTypeSizeInBits(LargestType)) |
Andrew Trick | 37da408 | 2011-05-04 02:10:13 +0000 | [diff] [blame] | 1210 | LargestType = Ty; |
| 1211 | } |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 1212 | } |
| 1213 | |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame] | 1214 | // Now that we know the largest of the induction variable expressions |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1215 | // in this loop, insert a canonical induction variable of the largest size. |
Dan Gohman | 43ef3fb | 2010-07-20 17:18:52 +0000 | [diff] [blame] | 1216 | PHINode *IndVar = 0; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1217 | if (NeedCannIV) { |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 1218 | // Check to see if the loop already has any canonical-looking induction |
| 1219 | // variables. If any are present and wider than the planned canonical |
| 1220 | // induction variable, temporarily remove them, so that the Rewriter |
| 1221 | // doesn't attempt to reuse them. |
| 1222 | SmallVector<PHINode *, 2> OldCannIVs; |
| 1223 | while (PHINode *OldCannIV = L->getCanonicalInductionVariable()) { |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1224 | if (SE->getTypeSizeInBits(OldCannIV->getType()) > |
| 1225 | SE->getTypeSizeInBits(LargestType)) |
| 1226 | OldCannIV->removeFromParent(); |
| 1227 | else |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 1228 | break; |
| 1229 | OldCannIVs.push_back(OldCannIV); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1232 | IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L, LargestType); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1233 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 1234 | ++NumInserted; |
| 1235 | Changed = true; |
David Greene | f67ef31 | 2010-01-05 01:27:06 +0000 | [diff] [blame] | 1236 | DEBUG(dbgs() << "INDVARS: New CanIV: " << *IndVar << '\n'); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 1237 | |
| 1238 | // Now that the official induction variable is established, reinsert |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 1239 | // any old canonical-looking variables after it so that the IR remains |
| 1240 | // 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] | 1241 | // the end of the pass. |
Dan Gohman | 8566963 | 2010-02-25 06:57:05 +0000 | [diff] [blame] | 1242 | while (!OldCannIVs.empty()) { |
| 1243 | PHINode *OldCannIV = OldCannIVs.pop_back_val(); |
| 1244 | OldCannIV->insertBefore(L->getHeader()->getFirstNonPHI()); |
| 1245 | } |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 1246 | } |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 1247 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 1248 | // If we have a trip count expression, rewrite the loop's exit condition |
| 1249 | // using it. We can currently only handle loops with a single exit. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1250 | ICmpInst *NewICmp = 0; |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 1251 | if (ExpandBECount) { |
Andrew Trick | 03d3d3b | 2011-05-25 04:42:22 +0000 | [diff] [blame] | 1252 | assert(canExpandBackedgeTakenCount(L, SE) && |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 1253 | "canonical IV disrupted BackedgeTaken expansion"); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1254 | assert(NeedCannIV && |
| 1255 | "LinearFunctionTestReplace requires a canonical induction variable"); |
Andrew Trick | 4dfdf24 | 2011-05-03 22:24:10 +0000 | [diff] [blame] | 1256 | NewICmp = LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar, |
| 1257 | Rewriter); |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 1258 | } |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 1259 | // Rewrite IV-derived expressions. |
Andrew Trick | 37da408 | 2011-05-04 02:10:13 +0000 | [diff] [blame] | 1260 | if (!DisableIVRewrite) |
| 1261 | RewriteIVExpressions(L, Rewriter); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 1262 | |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 1263 | // Clear the rewriter cache, because values that are in the rewriter's cache |
| 1264 | // can be deleted in the loop below, causing the AssertingVH in the cache to |
| 1265 | // trigger. |
| 1266 | Rewriter.clear(); |
| 1267 | |
| 1268 | // Now that we're done iterating through lists, clean up any instructions |
| 1269 | // which are now dead. |
| 1270 | while (!DeadInsts.empty()) |
| 1271 | if (Instruction *Inst = |
| 1272 | dyn_cast_or_null<Instruction>(&*DeadInsts.pop_back_val())) |
| 1273 | RecursivelyDeleteTriviallyDeadInstructions(Inst); |
| 1274 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1275 | // The Rewriter may not be used from this point on. |
Torok Edwin | 3d43138 | 2009-05-24 20:08:21 +0000 | [diff] [blame] | 1276 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1277 | // Loop-invariant instructions in the preheader that aren't used in the |
| 1278 | // loop may be sunk below the loop to reduce register pressure. |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1279 | SinkUnusedInvariants(L); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1280 | |
| 1281 | // For completeness, inform IVUsers of the IV use in the newly-created |
| 1282 | // loop exit test instruction. |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 1283 | if (NewICmp && IU) |
Andrew Trick | 4417e53 | 2011-06-21 15:43:52 +0000 | [diff] [blame] | 1284 | IU->AddUsersIfInteresting(cast<Instruction>(NewICmp->getOperand(0))); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1285 | |
| 1286 | // Clean up dead instructions. |
Dan Gohman | 9fff218 | 2010-01-05 16:31:45 +0000 | [diff] [blame] | 1287 | Changed |= DeleteDeadPHIs(L->getHeader()); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1288 | // Check a post-condition. |
Dan Gohman | bbf81d8 | 2010-03-10 19:38:49 +0000 | [diff] [blame] | 1289 | 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] | 1290 | return Changed; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 1291 | } |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 1292 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1293 | // FIXME: It is an extremely bad idea to indvar substitute anything more |
| 1294 | // complex than affine induction variables. Doing so will put expensive |
| 1295 | // polynomial evaluations inside of the loop, and the str reduction pass |
| 1296 | // currently can only reduce affine polynomials. For now just disable |
| 1297 | // indvar subst on anything more complex than an affine addrec, unless |
| 1298 | // it can be expanded to a trivial value. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1299 | static bool isSafe(const SCEV *S, const Loop *L, ScalarEvolution *SE) { |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1300 | // Loop-invariant values are safe. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1301 | if (SE->isLoopInvariant(S, L)) return true; |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1302 | |
| 1303 | // Affine addrecs are safe. Non-affine are not, because LSR doesn't know how |
| 1304 | // to transform them into efficient code. |
| 1305 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) |
| 1306 | return AR->isAffine(); |
| 1307 | |
| 1308 | // An add is safe it all its operands are safe. |
| 1309 | if (const SCEVCommutativeExpr *Commutative = dyn_cast<SCEVCommutativeExpr>(S)) { |
| 1310 | for (SCEVCommutativeExpr::op_iterator I = Commutative->op_begin(), |
| 1311 | E = Commutative->op_end(); I != E; ++I) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1312 | if (!isSafe(*I, L, SE)) return false; |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1313 | return true; |
| 1314 | } |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1315 | |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1316 | // A cast is safe if its operand is. |
| 1317 | if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(S)) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1318 | return isSafe(C->getOperand(), L, SE); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1319 | |
| 1320 | // A udiv is safe if its operands are. |
| 1321 | if (const SCEVUDivExpr *UD = dyn_cast<SCEVUDivExpr>(S)) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1322 | return isSafe(UD->getLHS(), L, SE) && |
| 1323 | isSafe(UD->getRHS(), L, SE); |
Dan Gohman | 448db1c | 2010-04-07 22:27:08 +0000 | [diff] [blame] | 1324 | |
| 1325 | // SCEVUnknown is always safe. |
| 1326 | if (isa<SCEVUnknown>(S)) |
| 1327 | return true; |
| 1328 | |
| 1329 | // Nothing else is safe. |
| 1330 | return false; |
| 1331 | } |
| 1332 | |
Dan Gohman | 454d26d | 2010-02-22 04:11:59 +0000 | [diff] [blame] | 1333 | void IndVarSimplify::RewriteIVExpressions(Loop *L, SCEVExpander &Rewriter) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1334 | // Rewrite all induction variable expressions in terms of the canonical |
| 1335 | // induction variable. |
| 1336 | // |
| 1337 | // If there were induction variables of other sizes or offsets, manually |
| 1338 | // add the offsets to the primary induction variable and cast, avoiding |
| 1339 | // the need for the code evaluation methods to insert induction variables |
| 1340 | // of different sizes. |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1341 | for (IVUsers::iterator UI = IU->begin(), E = IU->end(); UI != E; ++UI) { |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1342 | Value *Op = UI->getOperandValToReplace(); |
| 1343 | const Type *UseTy = Op->getType(); |
| 1344 | Instruction *User = UI->getUser(); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1345 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1346 | // Compute the final addrec to expand into code. |
| 1347 | const SCEV *AR = IU->getReplacementExpr(*UI); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1348 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1349 | // Evaluate the expression out of the loop, if possible. |
| 1350 | if (!L->contains(UI->getUser())) { |
| 1351 | const SCEV *ExitVal = SE->getSCEVAtScope(AR, L->getParentLoop()); |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1352 | if (SE->isLoopInvariant(ExitVal, L)) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1353 | AR = ExitVal; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1354 | } |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1355 | |
| 1356 | // FIXME: It is an extremely bad idea to indvar substitute anything more |
| 1357 | // complex than affine induction variables. Doing so will put expensive |
| 1358 | // polynomial evaluations inside of the loop, and the str reduction pass |
| 1359 | // currently can only reduce affine polynomials. For now just disable |
| 1360 | // indvar subst on anything more complex than an affine addrec, unless |
| 1361 | // it can be expanded to a trivial value. |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 1362 | if (!isSafe(AR, L, SE)) |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1363 | continue; |
| 1364 | |
| 1365 | // Determine the insertion point for this user. By default, insert |
| 1366 | // immediately before the user. The SCEVExpander class will automatically |
| 1367 | // hoist loop invariants out of the loop. For PHI nodes, there may be |
| 1368 | // multiple uses, so compute the nearest common dominator for the |
| 1369 | // incoming blocks. |
| 1370 | Instruction *InsertPt = User; |
| 1371 | if (PHINode *PHI = dyn_cast<PHINode>(InsertPt)) |
| 1372 | for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) |
| 1373 | if (PHI->getIncomingValue(i) == Op) { |
| 1374 | if (InsertPt == User) |
| 1375 | InsertPt = PHI->getIncomingBlock(i)->getTerminator(); |
| 1376 | else |
| 1377 | InsertPt = |
| 1378 | DT->findNearestCommonDominator(InsertPt->getParent(), |
| 1379 | PHI->getIncomingBlock(i)) |
| 1380 | ->getTerminator(); |
| 1381 | } |
| 1382 | |
| 1383 | // Now expand it into actual Instructions and patch it into place. |
| 1384 | Value *NewVal = Rewriter.expandCodeFor(AR, UseTy, InsertPt); |
| 1385 | |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 1386 | DEBUG(dbgs() << "INDVARS: Rewrote IV '" << *AR << "' " << *Op << '\n' |
| 1387 | << " into = " << *NewVal << "\n"); |
| 1388 | |
| 1389 | if (!isValidRewrite(Op, NewVal)) { |
| 1390 | DeadInsts.push_back(NewVal); |
| 1391 | continue; |
| 1392 | } |
Dan Gohman | d7bfd00 | 2010-04-02 14:48:31 +0000 | [diff] [blame] | 1393 | // Inform ScalarEvolution that this value is changing. The change doesn't |
| 1394 | // affect its value, but it does potentially affect which use lists the |
| 1395 | // value will be on after the replacement, which affects ScalarEvolution's |
| 1396 | // ability to walk use lists and drop dangling pointers when a value is |
| 1397 | // deleted. |
| 1398 | SE->forgetValue(User); |
| 1399 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1400 | // Patch the new value into place. |
| 1401 | if (Op->hasName()) |
| 1402 | NewVal->takeName(Op); |
Devang Patel | a098bf1 | 2011-06-22 19:52:36 +0000 | [diff] [blame^] | 1403 | if (Instruction *NewValI = dyn_cast<Instruction>(NewVal)) |
| 1404 | NewValI->setDebugLoc(User->getDebugLoc()); |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1405 | User->replaceUsesOfWith(Op, NewVal); |
| 1406 | UI->setOperandValToReplace(NewVal); |
Andrew Trick | b12a754 | 2011-03-17 23:51:11 +0000 | [diff] [blame] | 1407 | |
Dan Gohman | 572645c | 2010-02-12 10:34:29 +0000 | [diff] [blame] | 1408 | ++NumRemoved; |
| 1409 | Changed = true; |
| 1410 | |
| 1411 | // The old value may be dead now. |
| 1412 | DeadInsts.push_back(Op); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1413 | } |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | /// If there's a single exit block, sink any loop-invariant values that |
| 1417 | /// were defined in the preheader but not used inside the loop into the |
| 1418 | /// exit block to reduce register pressure in the loop. |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1419 | void IndVarSimplify::SinkUnusedInvariants(Loop *L) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1420 | BasicBlock *ExitBlock = L->getExitBlock(); |
| 1421 | if (!ExitBlock) return; |
| 1422 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1423 | BasicBlock *Preheader = L->getLoopPreheader(); |
Dan Gohman | 03e896b | 2009-11-05 21:11:53 +0000 | [diff] [blame] | 1424 | if (!Preheader) return; |
| 1425 | |
| 1426 | Instruction *InsertPt = ExitBlock->getFirstNonPHI(); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1427 | BasicBlock::iterator I = Preheader->getTerminator(); |
| 1428 | while (I != Preheader->begin()) { |
| 1429 | --I; |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1430 | // New instructions were inserted at the end of the preheader. |
| 1431 | if (isa<PHINode>(I)) |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1432 | break; |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 1433 | |
Eli Friedman | 0c77db3 | 2009-07-15 22:48:29 +0000 | [diff] [blame] | 1434 | // Don't move instructions which might have side effects, since the side |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 1435 | // effects need to complete before instructions inside the loop. Also don't |
| 1436 | // move instructions which might read memory, since the loop may modify |
| 1437 | // memory. Note that it's okay if the instruction might have undefined |
| 1438 | // behavior: LoopSimplify guarantees that the preheader dominates the exit |
| 1439 | // block. |
Eli Friedman | 0c77db3 | 2009-07-15 22:48:29 +0000 | [diff] [blame] | 1440 | if (I->mayHaveSideEffects() || I->mayReadFromMemory()) |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1441 | continue; |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 1442 | |
Devang Patel | 7b9f6b1 | 2010-03-15 22:23:03 +0000 | [diff] [blame] | 1443 | // Skip debug info intrinsics. |
| 1444 | if (isa<DbgInfoIntrinsic>(I)) |
| 1445 | continue; |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 1446 | |
Dan Gohman | 76f497a | 2009-08-25 17:42:10 +0000 | [diff] [blame] | 1447 | // Don't sink static AllocaInsts out of the entry block, which would |
| 1448 | // turn them into dynamic allocas! |
| 1449 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 1450 | if (AI->isStaticAlloca()) |
| 1451 | continue; |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 1452 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1453 | // Determine if there is a use in or before the loop (direct or |
| 1454 | // otherwise). |
| 1455 | bool UsedInLoop = false; |
| 1456 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 1457 | UI != UE; ++UI) { |
Gabor Greif | 7656018 | 2010-07-09 15:40:10 +0000 | [diff] [blame] | 1458 | User *U = *UI; |
| 1459 | BasicBlock *UseBB = cast<Instruction>(U)->getParent(); |
| 1460 | if (PHINode *P = dyn_cast<PHINode>(U)) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1461 | unsigned i = |
| 1462 | PHINode::getIncomingValueNumForOperand(UI.getOperandNo()); |
| 1463 | UseBB = P->getIncomingBlock(i); |
| 1464 | } |
| 1465 | if (UseBB == Preheader || L->contains(UseBB)) { |
| 1466 | UsedInLoop = true; |
| 1467 | break; |
| 1468 | } |
| 1469 | } |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 1470 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1471 | // If there is, the def must remain in the preheader. |
| 1472 | if (UsedInLoop) |
| 1473 | continue; |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 1474 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1475 | // Otherwise, sink it to the exit block. |
| 1476 | Instruction *ToMove = I; |
| 1477 | bool Done = false; |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 1478 | |
| 1479 | if (I != Preheader->begin()) { |
| 1480 | // Skip debug info intrinsics. |
| 1481 | do { |
| 1482 | --I; |
| 1483 | } while (isa<DbgInfoIntrinsic>(I) && I != Preheader->begin()); |
| 1484 | |
| 1485 | if (isa<DbgInfoIntrinsic>(I) && I == Preheader->begin()) |
| 1486 | Done = true; |
| 1487 | } else { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1488 | Done = true; |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1491 | ToMove->moveBefore(InsertPt); |
Bill Wendling | 87a10f5 | 2010-03-23 21:15:59 +0000 | [diff] [blame] | 1492 | if (Done) break; |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 1493 | InsertPt = ToMove; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1494 | } |
| 1495 | } |
| 1496 | |
Chris Lattner | bbb9149 | 2010-04-03 06:41:49 +0000 | [diff] [blame] | 1497 | /// ConvertToSInt - Convert APF to an integer, if possible. |
| 1498 | static bool ConvertToSInt(const APFloat &APF, int64_t &IntVal) { |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 1499 | bool isExact = false; |
Evan Cheng | 794a7db | 2008-11-26 01:11:57 +0000 | [diff] [blame] | 1500 | if (&APF.getSemantics() == &APFloat::PPCDoubleDouble) |
| 1501 | return false; |
Chris Lattner | bbb9149 | 2010-04-03 06:41:49 +0000 | [diff] [blame] | 1502 | // See if we can convert this to an int64_t |
| 1503 | uint64_t UIntVal; |
| 1504 | if (APF.convertToInteger(&UIntVal, 64, true, APFloat::rmTowardZero, |
| 1505 | &isExact) != APFloat::opOK || !isExact) |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 1506 | return false; |
Chris Lattner | bbb9149 | 2010-04-03 06:41:49 +0000 | [diff] [blame] | 1507 | IntVal = UIntVal; |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 1508 | return true; |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 1511 | /// HandleFloatingPointIV - If the loop has floating induction variable |
| 1512 | /// then insert corresponding integer induction variable if possible. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1513 | /// For example, |
| 1514 | /// for(double i = 0; i < 10000; ++i) |
| 1515 | /// bar(i) |
| 1516 | /// is converted into |
| 1517 | /// for(int i = 0; i < 10000; ++i) |
| 1518 | /// bar((double)i); |
| 1519 | /// |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1520 | void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PN) { |
| 1521 | unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0)); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1522 | unsigned BackEdge = IncomingEdge^1; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1523 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1524 | // Check incoming value. |
Chris Lattner | c4f7e80 | 2010-04-03 06:05:10 +0000 | [diff] [blame] | 1525 | ConstantFP *InitValueVal = |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1526 | dyn_cast<ConstantFP>(PN->getIncomingValue(IncomingEdge)); |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1527 | |
Chris Lattner | bbb9149 | 2010-04-03 06:41:49 +0000 | [diff] [blame] | 1528 | int64_t InitValue; |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1529 | if (!InitValueVal || !ConvertToSInt(InitValueVal->getValueAPF(), InitValue)) |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 1530 | return; |
| 1531 | |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1532 | // Check IV increment. Reject this PN if increment operation is not |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 1533 | // an add or increment value can not be represented by an integer. |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1534 | BinaryOperator *Incr = |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1535 | dyn_cast<BinaryOperator>(PN->getIncomingValue(BackEdge)); |
Chris Lattner | 07aa76a | 2010-04-03 05:54:59 +0000 | [diff] [blame] | 1536 | if (Incr == 0 || Incr->getOpcode() != Instruction::FAdd) return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1537 | |
Chris Lattner | 07aa76a | 2010-04-03 05:54:59 +0000 | [diff] [blame] | 1538 | // If this is not an add of the PHI with a constantfp, or if the constant fp |
| 1539 | // is not an integer, bail out. |
Chris Lattner | c4f7e80 | 2010-04-03 06:05:10 +0000 | [diff] [blame] | 1540 | ConstantFP *IncValueVal = dyn_cast<ConstantFP>(Incr->getOperand(1)); |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1541 | int64_t IncValue; |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1542 | if (IncValueVal == 0 || Incr->getOperand(0) != PN || |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1543 | !ConvertToSInt(IncValueVal->getValueAPF(), IncValue)) |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 1544 | return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1545 | |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1546 | // 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] | 1547 | // used by the conditional terminator. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1548 | Value::use_iterator IncrUse = Incr->use_begin(); |
Gabor Greif | 96f1d8e | 2010-07-22 13:36:47 +0000 | [diff] [blame] | 1549 | Instruction *U1 = cast<Instruction>(*IncrUse++); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1550 | if (IncrUse == Incr->use_end()) return; |
Gabor Greif | 96f1d8e | 2010-07-22 13:36:47 +0000 | [diff] [blame] | 1551 | Instruction *U2 = cast<Instruction>(*IncrUse++); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1552 | if (IncrUse != Incr->use_end()) return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1553 | |
Chris Lattner | 07aa76a | 2010-04-03 05:54:59 +0000 | [diff] [blame] | 1554 | // Find exit condition, which is an fcmp. If it doesn't exist, or if it isn't |
| 1555 | // only used by a branch, we can't transform it. |
Chris Lattner | ca703bd | 2010-04-03 06:11:07 +0000 | [diff] [blame] | 1556 | FCmpInst *Compare = dyn_cast<FCmpInst>(U1); |
| 1557 | if (!Compare) |
| 1558 | Compare = dyn_cast<FCmpInst>(U2); |
| 1559 | if (Compare == 0 || !Compare->hasOneUse() || |
| 1560 | !isa<BranchInst>(Compare->use_back())) |
Chris Lattner | 07aa76a | 2010-04-03 05:54:59 +0000 | [diff] [blame] | 1561 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1562 | |
Chris Lattner | ca703bd | 2010-04-03 06:11:07 +0000 | [diff] [blame] | 1563 | BranchInst *TheBr = cast<BranchInst>(Compare->use_back()); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1564 | |
Chris Lattner | d52c072 | 2010-04-03 07:21:39 +0000 | [diff] [blame] | 1565 | // We need to verify that the branch actually controls the iteration count |
| 1566 | // of the loop. If not, the new IV can overflow and no one will notice. |
| 1567 | // The branch block must be in the loop and one of the successors must be out |
| 1568 | // of the loop. |
| 1569 | assert(TheBr->isConditional() && "Can't use fcmp if not conditional"); |
| 1570 | if (!L->contains(TheBr->getParent()) || |
| 1571 | (L->contains(TheBr->getSuccessor(0)) && |
| 1572 | L->contains(TheBr->getSuccessor(1)))) |
| 1573 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1574 | |
| 1575 | |
Chris Lattner | 07aa76a | 2010-04-03 05:54:59 +0000 | [diff] [blame] | 1576 | // If it isn't a comparison with an integer-as-fp (the exit value), we can't |
| 1577 | // transform it. |
Chris Lattner | ca703bd | 2010-04-03 06:11:07 +0000 | [diff] [blame] | 1578 | ConstantFP *ExitValueVal = dyn_cast<ConstantFP>(Compare->getOperand(1)); |
Chris Lattner | bbb9149 | 2010-04-03 06:41:49 +0000 | [diff] [blame] | 1579 | int64_t ExitValue; |
| 1580 | if (ExitValueVal == 0 || |
| 1581 | !ConvertToSInt(ExitValueVal->getValueAPF(), ExitValue)) |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1582 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1583 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1584 | // Find new predicate for integer comparison. |
| 1585 | CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE; |
Chris Lattner | ca703bd | 2010-04-03 06:11:07 +0000 | [diff] [blame] | 1586 | switch (Compare->getPredicate()) { |
Chris Lattner | c4f7e80 | 2010-04-03 06:05:10 +0000 | [diff] [blame] | 1587 | default: return; // Unknown comparison. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1588 | case CmpInst::FCMP_OEQ: |
Chris Lattner | c4f7e80 | 2010-04-03 06:05:10 +0000 | [diff] [blame] | 1589 | case CmpInst::FCMP_UEQ: NewPred = CmpInst::ICMP_EQ; break; |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1590 | case CmpInst::FCMP_ONE: |
| 1591 | case CmpInst::FCMP_UNE: NewPred = CmpInst::ICMP_NE; break; |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1592 | case CmpInst::FCMP_OGT: |
Chris Lattner | a40e4a0 | 2010-04-03 06:25:21 +0000 | [diff] [blame] | 1593 | case CmpInst::FCMP_UGT: NewPred = CmpInst::ICMP_SGT; break; |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1594 | case CmpInst::FCMP_OGE: |
Chris Lattner | a40e4a0 | 2010-04-03 06:25:21 +0000 | [diff] [blame] | 1595 | case CmpInst::FCMP_UGE: NewPred = CmpInst::ICMP_SGE; break; |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1596 | case CmpInst::FCMP_OLT: |
Chris Lattner | 43b8527 | 2010-04-03 06:30:03 +0000 | [diff] [blame] | 1597 | case CmpInst::FCMP_ULT: NewPred = CmpInst::ICMP_SLT; break; |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1598 | case CmpInst::FCMP_OLE: |
Chris Lattner | 43b8527 | 2010-04-03 06:30:03 +0000 | [diff] [blame] | 1599 | case CmpInst::FCMP_ULE: NewPred = CmpInst::ICMP_SLE; break; |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 1600 | } |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1601 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1602 | // We convert the floating point induction variable to a signed i32 value if |
| 1603 | // we can. This is only safe if the comparison will not overflow in a way |
| 1604 | // that won't be trapped by the integer equivalent operations. Check for this |
| 1605 | // now. |
| 1606 | // 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] | 1607 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1608 | // The start/stride/exit values must all fit in signed i32. |
| 1609 | if (!isInt<32>(InitValue) || !isInt<32>(IncValue) || !isInt<32>(ExitValue)) |
| 1610 | return; |
| 1611 | |
| 1612 | // If not actually striding (add x, 0.0), avoid touching the code. |
| 1613 | if (IncValue == 0) |
| 1614 | return; |
| 1615 | |
| 1616 | // Positive and negative strides have different safety conditions. |
| 1617 | if (IncValue > 0) { |
| 1618 | // If we have a positive stride, we require the init to be less than the |
| 1619 | // exit value and an equality or less than comparison. |
| 1620 | if (InitValue >= ExitValue || |
| 1621 | NewPred == CmpInst::ICMP_SGT || NewPred == CmpInst::ICMP_SGE) |
| 1622 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1623 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1624 | uint32_t Range = uint32_t(ExitValue-InitValue); |
| 1625 | if (NewPred == CmpInst::ICMP_SLE) { |
| 1626 | // Normalize SLE -> SLT, check for infinite loop. |
| 1627 | if (++Range == 0) return; // Range overflows. |
| 1628 | } |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1629 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1630 | unsigned Leftover = Range % uint32_t(IncValue); |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1631 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1632 | // If this is an equality comparison, we require that the strided value |
| 1633 | // exactly land on the exit value, otherwise the IV condition will wrap |
| 1634 | // around and do things the fp IV wouldn't. |
| 1635 | if ((NewPred == CmpInst::ICMP_EQ || NewPred == CmpInst::ICMP_NE) && |
| 1636 | Leftover != 0) |
| 1637 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1638 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1639 | // If the stride would wrap around the i32 before exiting, we can't |
| 1640 | // transform the IV. |
| 1641 | if (Leftover != 0 && int32_t(ExitValue+IncValue) < ExitValue) |
| 1642 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1643 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1644 | } else { |
| 1645 | // If we have a negative stride, we require the init to be greater than the |
| 1646 | // exit value and an equality or greater than comparison. |
| 1647 | if (InitValue >= ExitValue || |
| 1648 | NewPred == CmpInst::ICMP_SLT || NewPred == CmpInst::ICMP_SLE) |
| 1649 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1650 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1651 | uint32_t Range = uint32_t(InitValue-ExitValue); |
| 1652 | if (NewPred == CmpInst::ICMP_SGE) { |
| 1653 | // Normalize SGE -> SGT, check for infinite loop. |
| 1654 | if (++Range == 0) return; // Range overflows. |
| 1655 | } |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1656 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1657 | unsigned Leftover = Range % uint32_t(-IncValue); |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1658 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1659 | // If this is an equality comparison, we require that the strided value |
| 1660 | // exactly land on the exit value, otherwise the IV condition will wrap |
| 1661 | // around and do things the fp IV wouldn't. |
| 1662 | if ((NewPred == CmpInst::ICMP_EQ || NewPred == CmpInst::ICMP_NE) && |
| 1663 | Leftover != 0) |
| 1664 | return; |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1665 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1666 | // If the stride would wrap around the i32 before exiting, we can't |
| 1667 | // transform the IV. |
| 1668 | if (Leftover != 0 && int32_t(ExitValue+IncValue) > ExitValue) |
| 1669 | return; |
| 1670 | } |
Andrew Trick | ead71d5 | 2011-03-17 23:46:48 +0000 | [diff] [blame] | 1671 | |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1672 | const IntegerType *Int32Ty = Type::getInt32Ty(PN->getContext()); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1673 | |
Chris Lattner | bbb9149 | 2010-04-03 06:41:49 +0000 | [diff] [blame] | 1674 | // Insert new integer induction variable. |
Jay Foad | 3ecfc86 | 2011-03-30 11:28:46 +0000 | [diff] [blame] | 1675 | PHINode *NewPHI = PHINode::Create(Int32Ty, 2, PN->getName()+".int", PN); |
Chris Lattner | c4f7e80 | 2010-04-03 06:05:10 +0000 | [diff] [blame] | 1676 | NewPHI->addIncoming(ConstantInt::get(Int32Ty, InitValue), |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1677 | PN->getIncomingBlock(IncomingEdge)); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1678 | |
Chris Lattner | c4f7e80 | 2010-04-03 06:05:10 +0000 | [diff] [blame] | 1679 | Value *NewAdd = |
Chris Lattner | 96fd766 | 2010-04-03 07:18:48 +0000 | [diff] [blame] | 1680 | BinaryOperator::CreateAdd(NewPHI, ConstantInt::get(Int32Ty, IncValue), |
Chris Lattner | c4f7e80 | 2010-04-03 06:05:10 +0000 | [diff] [blame] | 1681 | Incr->getName()+".int", Incr); |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1682 | NewPHI->addIncoming(NewAdd, PN->getIncomingBlock(BackEdge)); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1683 | |
Chris Lattner | ca703bd | 2010-04-03 06:11:07 +0000 | [diff] [blame] | 1684 | ICmpInst *NewCompare = new ICmpInst(TheBr, NewPred, NewAdd, |
| 1685 | ConstantInt::get(Int32Ty, ExitValue), |
| 1686 | Compare->getName()); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1687 | |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1688 | // In the following deletions, PN may become dead and may be deleted. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1689 | // Use a WeakVH to observe whether this happens. |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1690 | WeakVH WeakPH = PN; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1691 | |
Chris Lattner | ca703bd | 2010-04-03 06:11:07 +0000 | [diff] [blame] | 1692 | // Delete the old floating point exit comparison. The branch starts using the |
| 1693 | // new comparison. |
| 1694 | NewCompare->takeName(Compare); |
| 1695 | Compare->replaceAllUsesWith(NewCompare); |
| 1696 | RecursivelyDeleteTriviallyDeadInstructions(Compare); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1697 | |
Chris Lattner | ca703bd | 2010-04-03 06:11:07 +0000 | [diff] [blame] | 1698 | // Delete the old floating point increment. |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 1699 | Incr->replaceAllUsesWith(UndefValue::get(Incr->getType())); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1700 | RecursivelyDeleteTriviallyDeadInstructions(Incr); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1701 | |
Chris Lattner | 70c0d4f | 2010-04-03 06:16:22 +0000 | [diff] [blame] | 1702 | // If the FP induction variable still has uses, this is because something else |
| 1703 | // in the loop uses its value. In order to canonicalize the induction |
| 1704 | // variable, we chose to eliminate the IV and rewrite it in terms of an |
| 1705 | // int->fp cast. |
| 1706 | // |
| 1707 | // We give preference to sitofp over uitofp because it is faster on most |
| 1708 | // platforms. |
| 1709 | if (WeakPH) { |
Chris Lattner | a40e4a0 | 2010-04-03 06:25:21 +0000 | [diff] [blame] | 1710 | Value *Conv = new SIToFPInst(NewPHI, PN->getType(), "indvar.conv", |
| 1711 | PN->getParent()->getFirstNonPHI()); |
| 1712 | PN->replaceAllUsesWith(Conv); |
Chris Lattner | c91961e | 2010-04-03 06:17:08 +0000 | [diff] [blame] | 1713 | RecursivelyDeleteTriviallyDeadInstructions(PN); |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 1714 | } |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 1715 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1716 | // Add a new IVUsers entry for the newly-created integer PHI. |
Andrew Trick | 2fabd46 | 2011-06-21 03:22:38 +0000 | [diff] [blame] | 1717 | if (IU) |
Andrew Trick | 4417e53 | 2011-06-21 15:43:52 +0000 | [diff] [blame] | 1718 | IU->AddUsersIfInteresting(NewPHI); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 1719 | } |