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