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