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" |
Owen Anderson | d672ecb | 2009-07-03 00:17:18 +0000 | [diff] [blame] | 46 | #include "llvm/LLVMContext.h" |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 47 | #include "llvm/Type.h" |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 48 | #include "llvm/Analysis/Dominators.h" |
| 49 | #include "llvm/Analysis/IVUsers.h" |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 50 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 51 | #include "llvm/Analysis/LoopInfo.h" |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 52 | #include "llvm/Analysis/LoopPass.h" |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 53 | #include "llvm/Support/CFG.h" |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 54 | #include "llvm/Support/CommandLine.h" |
Chris Lattner | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 55 | #include "llvm/Support/Debug.h" |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 56 | #include "llvm/Support/raw_ostream.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 57 | #include "llvm/Transforms/Utils/Local.h" |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 58 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 60 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/STLExtras.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 62 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 64 | STATISTIC(NumRemoved , "Number of aux indvars removed"); |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 65 | STATISTIC(NumInserted, "Number of canonical indvars added"); |
| 66 | STATISTIC(NumReplaced, "Number of exit values replaced"); |
| 67 | STATISTIC(NumLFTR , "Number of loop exit tests replaced"); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 68 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 69 | namespace { |
Chris Lattner | 3e8b663 | 2009-09-02 06:11:42 +0000 | [diff] [blame] | 70 | class IndVarSimplify : public LoopPass { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 71 | IVUsers *IU; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 72 | LoopInfo *LI; |
| 73 | ScalarEvolution *SE; |
Dan Gohman | de53dc0 | 2009-06-27 05:16:57 +0000 | [diff] [blame] | 74 | DominatorTree *DT; |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 75 | bool Changed; |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 76 | public: |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 77 | |
Dan Gohman | 5668cf7 | 2009-07-15 01:26:32 +0000 | [diff] [blame] | 78 | static char ID; // Pass identification, replacement for typeid |
| 79 | IndVarSimplify() : LoopPass(&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 80 | |
Dan Gohman | 5668cf7 | 2009-07-15 01:26:32 +0000 | [diff] [blame] | 81 | virtual bool runOnLoop(Loop *L, LPPassManager &LPM); |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 82 | |
Dan Gohman | 5668cf7 | 2009-07-15 01:26:32 +0000 | [diff] [blame] | 83 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 84 | AU.addRequired<DominatorTree>(); |
| 85 | AU.addRequired<LoopInfo>(); |
| 86 | AU.addRequired<ScalarEvolution>(); |
| 87 | AU.addRequiredID(LoopSimplifyID); |
| 88 | AU.addRequiredID(LCSSAID); |
| 89 | AU.addRequired<IVUsers>(); |
| 90 | AU.addPreserved<ScalarEvolution>(); |
| 91 | AU.addPreservedID(LoopSimplifyID); |
| 92 | AU.addPreservedID(LCSSAID); |
| 93 | AU.addPreserved<IVUsers>(); |
| 94 | AU.setPreservesCFG(); |
| 95 | } |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 97 | private: |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 98 | |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 99 | void RewriteNonIntegerIVs(Loop *L); |
| 100 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 101 | ICmpInst *LinearFunctionTestReplace(Loop *L, const SCEV *BackedgeTakenCount, |
Dan Gohman | a575871 | 2009-02-17 15:57:39 +0000 | [diff] [blame] | 102 | Value *IndVar, |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 103 | BasicBlock *ExitingBlock, |
| 104 | BranchInst *BI, |
Dan Gohman | 15cab28 | 2009-02-23 23:20:35 +0000 | [diff] [blame] | 105 | SCEVExpander &Rewriter); |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 106 | void RewriteLoopExitValues(Loop *L, const SCEV *BackedgeTakenCount, |
| 107 | SCEVExpander &Rewriter); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 108 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 109 | void RewriteIVExpressions(Loop *L, const Type *LargestType, |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 110 | 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()) || |
| 151 | SE->isLoopGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) { |
| 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 | |
| 193 | Instruction *OrigCond = cast<Instruction>(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 | const SCEV *BackedgeTakenCount, |
| 219 | SCEVExpander &Rewriter) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 220 | // Verify the input to the pass in already in LCSSA form. |
| 221 | assert(L->isLCSSAForm()); |
| 222 | |
Devang Patel | b7211a2 | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 223 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 224 | L->getUniqueExitBlocks(ExitBlocks); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 226 | // Find all values that are computed inside the loop, but used outside of it. |
| 227 | // Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan |
| 228 | // the exit blocks of the loop to find them. |
| 229 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
| 230 | BasicBlock *ExitBB = ExitBlocks[i]; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 231 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 232 | // If there are no PHI nodes in this exit block, then no values defined |
| 233 | // inside the loop are used on this path, skip it. |
| 234 | PHINode *PN = dyn_cast<PHINode>(ExitBB->begin()); |
| 235 | if (!PN) continue; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 236 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 237 | unsigned NumPreds = PN->getNumIncomingValues(); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 238 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 239 | // Iterate over all of the PHI nodes. |
| 240 | BasicBlock::iterator BBI = ExitBB->begin(); |
| 241 | while ((PN = dyn_cast<PHINode>(BBI++))) { |
Torok Edwin | 3790fb0 | 2009-05-24 19:36:09 +0000 | [diff] [blame] | 242 | if (PN->use_empty()) |
| 243 | continue; // dead use, don't replace it |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 244 | // Iterate over all of the values in all the PHI nodes. |
| 245 | for (unsigned i = 0; i != NumPreds; ++i) { |
| 246 | // If the value being merged in is not integer or is not defined |
| 247 | // in the loop, skip it. |
| 248 | Value *InVal = PN->getIncomingValue(i); |
| 249 | if (!isa<Instruction>(InVal) || |
| 250 | // SCEV only supports integer expressions for now. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 251 | (!isa<IntegerType>(InVal->getType()) && |
| 252 | !isa<PointerType>(InVal->getType()))) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 253 | continue; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 254 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 255 | // If this pred is for a subloop, not L itself, skip it. |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 256 | if (LI->getLoopFor(PN->getIncomingBlock(i)) != L) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 257 | continue; // The Block is in a subloop, skip it. |
| 258 | |
| 259 | // Check that InVal is defined in the loop. |
| 260 | Instruction *Inst = cast<Instruction>(InVal); |
Dan Gohman | 92329c7 | 2009-12-18 01:24:09 +0000 | [diff] [blame] | 261 | if (!L->contains(Inst)) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 262 | continue; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 263 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 264 | // Okay, this instruction has a user outside of the current loop |
| 265 | // and varies predictably *inside* the loop. Evaluate the value it |
| 266 | // contains when the loop exits, if possible. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 267 | const SCEV *ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop()); |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 268 | if (!ExitValue->isLoopInvariant(L)) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 269 | continue; |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 270 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 271 | Changed = true; |
| 272 | ++NumReplaced; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 273 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 274 | Value *ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), Inst); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 275 | |
David Greene | f67ef31 | 2010-01-05 01:27:06 +0000 | [diff] [blame] | 276 | DEBUG(dbgs() << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal << '\n' |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 277 | << " LoopVal = " << *Inst << "\n"); |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 278 | |
| 279 | PN->setIncomingValue(i, ExitVal); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 280 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 281 | // If this instruction is dead now, delete it. |
| 282 | RecursivelyDeleteTriviallyDeadInstructions(Inst); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 283 | |
Dan Gohman | 65d1e2b | 2009-07-14 01:09:02 +0000 | [diff] [blame] | 284 | if (NumPreds == 1) { |
| 285 | // Completely replace a single-pred PHI. This is safe, because the |
| 286 | // NewVal won't be variant in the loop, so we don't need an LCSSA phi |
| 287 | // node anymore. |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 288 | PN->replaceAllUsesWith(ExitVal); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 289 | RecursivelyDeleteTriviallyDeadInstructions(PN); |
Chris Lattner | c9838f2 | 2007-03-03 22:48:48 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
Dan Gohman | 65d1e2b | 2009-07-14 01:09:02 +0000 | [diff] [blame] | 292 | if (NumPreds != 1) { |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 293 | // Clone the PHI and delete the original one. This lets IVUsers and |
| 294 | // any other maps purge the original user from their records. |
Devang Patel | 50b6e33 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 295 | PHINode *NewPN = cast<PHINode>(PN->clone()); |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 296 | NewPN->takeName(PN); |
| 297 | NewPN->insertBefore(PN); |
| 298 | PN->replaceAllUsesWith(NewPN); |
| 299 | PN->eraseFromParent(); |
| 300 | } |
Chris Lattner | c9838f2 | 2007-03-03 22:48:48 +0000 | [diff] [blame] | 301 | } |
| 302 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 303 | } |
| 304 | |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 305 | void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 306 | // First step. Check to see if there are any floating-point recurrences. |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 307 | // If there are, change them into integer recurrences, permitting analysis by |
| 308 | // the SCEV routines. |
| 309 | // |
| 310 | BasicBlock *Header = L->getHeader(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 311 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 312 | SmallVector<WeakVH, 8> PHIs; |
| 313 | for (BasicBlock::iterator I = Header->begin(); |
| 314 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 315 | PHIs.push_back(PN); |
| 316 | |
| 317 | for (unsigned i = 0, e = PHIs.size(); i != e; ++i) |
| 318 | if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i])) |
| 319 | HandleFloatingPointIV(L, PN); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 320 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 321 | // If the loop previously had floating-point IV, ScalarEvolution |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 322 | // may not have been able to compute a trip count. Now that we've done some |
| 323 | // re-writing, the trip count may be computable. |
| 324 | if (Changed) |
Dan Gohman | 4c7279a | 2009-10-31 15:04:55 +0000 | [diff] [blame] | 325 | SE->forgetLoop(L); |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 328 | bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 329 | IU = &getAnalysis<IVUsers>(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 330 | LI = &getAnalysis<LoopInfo>(); |
| 331 | SE = &getAnalysis<ScalarEvolution>(); |
Dan Gohman | de53dc0 | 2009-06-27 05:16:57 +0000 | [diff] [blame] | 332 | DT = &getAnalysis<DominatorTree>(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 333 | Changed = false; |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 334 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 335 | // If there are any floating-point recurrences, attempt to |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 336 | // transform them to use integer recurrences. |
| 337 | RewriteNonIntegerIVs(L); |
| 338 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 339 | BasicBlock *ExitingBlock = L->getExitingBlock(); // may be null |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 340 | const SCEV *BackedgeTakenCount = SE->getBackedgeTakenCount(L); |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 341 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 342 | // Create a rewriter object which we'll use to transform the code with. |
| 343 | SCEVExpander Rewriter(*SE); |
| 344 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 345 | // Check to see if this loop has a computable loop-invariant execution count. |
| 346 | // If so, this means that we can compute the final value of any expressions |
| 347 | // that are recurrent in the loop, and substitute the exit values from the |
| 348 | // loop into any instructions outside of the loop that use the final values of |
| 349 | // the current expressions. |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 350 | // |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 351 | if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 352 | RewriteLoopExitValues(L, BackedgeTakenCount, Rewriter); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 353 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 354 | // Compute the type of the largest recurrence expression, and decide whether |
| 355 | // a canonical induction variable should be inserted. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 356 | const Type *LargestType = 0; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 357 | bool NeedCannIV = false; |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 358 | if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) { |
| 359 | LargestType = BackedgeTakenCount->getType(); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 360 | LargestType = SE->getEffectiveSCEVType(LargestType); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 361 | // If we have a known trip count and a single exit block, we'll be |
| 362 | // rewriting the loop exit test condition below, which requires a |
| 363 | // canonical induction variable. |
| 364 | if (ExitingBlock) |
| 365 | NeedCannIV = true; |
Chris Lattner | f50af08 | 2004-04-17 18:08:33 +0000 | [diff] [blame] | 366 | } |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 367 | for (unsigned i = 0, e = IU->StrideOrder.size(); i != e; ++i) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 368 | const SCEV *Stride = IU->StrideOrder[i]; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 369 | const Type *Ty = SE->getEffectiveSCEVType(Stride->getType()); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 370 | if (!LargestType || |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 371 | SE->getTypeSizeInBits(Ty) > |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 372 | SE->getTypeSizeInBits(LargestType)) |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 373 | LargestType = Ty; |
| 374 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 375 | std::map<const SCEV *, IVUsersOfOneStride *>::iterator SI = |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 376 | IU->IVUsesByStride.find(IU->StrideOrder[i]); |
| 377 | assert(SI != IU->IVUsesByStride.end() && "Stride doesn't exist!"); |
| 378 | |
| 379 | if (!SI->second->Users.empty()) |
| 380 | NeedCannIV = true; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 381 | } |
| 382 | |
Dan Gohman | f451cb8 | 2010-02-10 16:03:48 +0000 | [diff] [blame^] | 383 | // Now that we know the largest of the induction variable expressions |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 384 | // in this loop, insert a canonical induction variable of the largest size. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 385 | Value *IndVar = 0; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 386 | if (NeedCannIV) { |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 387 | // Check to see if the loop already has a canonical-looking induction |
| 388 | // variable. If one is present and it's wider than the planned canonical |
| 389 | // induction variable, temporarily remove it, so that the Rewriter |
| 390 | // doesn't attempt to reuse it. |
| 391 | PHINode *OldCannIV = L->getCanonicalInductionVariable(); |
| 392 | if (OldCannIV) { |
| 393 | if (SE->getTypeSizeInBits(OldCannIV->getType()) > |
| 394 | SE->getTypeSizeInBits(LargestType)) |
| 395 | OldCannIV->removeFromParent(); |
| 396 | else |
| 397 | OldCannIV = 0; |
| 398 | } |
| 399 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 400 | IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L, LargestType); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 401 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 402 | ++NumInserted; |
| 403 | Changed = true; |
David Greene | f67ef31 | 2010-01-05 01:27:06 +0000 | [diff] [blame] | 404 | DEBUG(dbgs() << "INDVARS: New CanIV: " << *IndVar << '\n'); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 405 | |
| 406 | // Now that the official induction variable is established, reinsert |
| 407 | // the old canonical-looking variable after it so that the IR remains |
| 408 | // consistent. It will be deleted as part of the dead-PHI deletion at |
| 409 | // the end of the pass. |
| 410 | if (OldCannIV) |
| 411 | OldCannIV->insertAfter(cast<Instruction>(IndVar)); |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 412 | } |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 413 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 414 | // If we have a trip count expression, rewrite the loop's exit condition |
| 415 | // using it. We can currently only handle loops with a single exit. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 416 | ICmpInst *NewICmp = 0; |
| 417 | if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) && ExitingBlock) { |
| 418 | assert(NeedCannIV && |
| 419 | "LinearFunctionTestReplace requires a canonical induction variable"); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 420 | // Can't rewrite non-branch yet. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 421 | if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator())) |
| 422 | NewICmp = LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar, |
| 423 | ExitingBlock, BI, Rewriter); |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 424 | } |
| 425 | |
Torok Edwin | 3d43138 | 2009-05-24 20:08:21 +0000 | [diff] [blame] | 426 | // Rewrite IV-derived expressions. Clears the rewriter cache. |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 427 | RewriteIVExpressions(L, LargestType, Rewriter); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 428 | |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 429 | // The Rewriter may not be used from this point on. |
Torok Edwin | 3d43138 | 2009-05-24 20:08:21 +0000 | [diff] [blame] | 430 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 431 | // Loop-invariant instructions in the preheader that aren't used in the |
| 432 | // loop may be sunk below the loop to reduce register pressure. |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 433 | SinkUnusedInvariants(L); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 434 | |
| 435 | // For completeness, inform IVUsers of the IV use in the newly-created |
| 436 | // loop exit test instruction. |
| 437 | if (NewICmp) |
| 438 | IU->AddUsersIfInteresting(cast<Instruction>(NewICmp->getOperand(0))); |
| 439 | |
| 440 | // Clean up dead instructions. |
Dan Gohman | 9fff218 | 2010-01-05 16:31:45 +0000 | [diff] [blame] | 441 | Changed |= DeleteDeadPHIs(L->getHeader()); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 442 | // Check a post-condition. |
| 443 | assert(L->isLCSSAForm() && "Indvars did not leave the loop in lcssa form!"); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 444 | return Changed; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 445 | } |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 446 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 447 | void IndVarSimplify::RewriteIVExpressions(Loop *L, const Type *LargestType, |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 448 | SCEVExpander &Rewriter) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 449 | SmallVector<WeakVH, 16> DeadInsts; |
| 450 | |
| 451 | // Rewrite all induction variable expressions in terms of the canonical |
| 452 | // induction variable. |
| 453 | // |
| 454 | // If there were induction variables of other sizes or offsets, manually |
| 455 | // add the offsets to the primary induction variable and cast, avoiding |
| 456 | // the need for the code evaluation methods to insert induction variables |
| 457 | // of different sizes. |
| 458 | for (unsigned i = 0, e = IU->StrideOrder.size(); i != e; ++i) { |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 459 | const SCEV *Stride = IU->StrideOrder[i]; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 460 | |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 461 | std::map<const SCEV *, IVUsersOfOneStride *>::iterator SI = |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 462 | IU->IVUsesByStride.find(IU->StrideOrder[i]); |
| 463 | assert(SI != IU->IVUsesByStride.end() && "Stride doesn't exist!"); |
| 464 | ilist<IVStrideUse> &List = SI->second->Users; |
| 465 | for (ilist<IVStrideUse>::iterator UI = List.begin(), |
| 466 | E = List.end(); UI != E; ++UI) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 467 | Value *Op = UI->getOperandValToReplace(); |
Dan Gohman | 4d8414f | 2009-06-13 16:25:49 +0000 | [diff] [blame] | 468 | const Type *UseTy = Op->getType(); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 469 | Instruction *User = UI->getUser(); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 470 | |
| 471 | // Compute the final addrec to expand into code. |
Dan Gohman | 0bba49c | 2009-07-07 17:06:11 +0000 | [diff] [blame] | 472 | const SCEV *AR = IU->getReplacementExpr(*UI); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 473 | |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 474 | // Evaluate the expression out of the loop, if possible. |
| 475 | if (!L->contains(UI->getUser())) { |
| 476 | const SCEV *ExitVal = SE->getSCEVAtScope(AR, L->getParentLoop()); |
| 477 | if (ExitVal->isLoopInvariant(L)) |
| 478 | AR = ExitVal; |
| 479 | } |
| 480 | |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 481 | // FIXME: It is an extremely bad idea to indvar substitute anything more |
| 482 | // complex than affine induction variables. Doing so will put expensive |
| 483 | // polynomial evaluations inside of the loop, and the str reduction pass |
| 484 | // currently can only reduce affine polynomials. For now just disable |
| 485 | // indvar subst on anything more complex than an affine addrec, unless |
| 486 | // it can be expanded to a trivial value. |
| 487 | if (!AR->isLoopInvariant(L) && !Stride->isLoopInvariant(L)) |
| 488 | continue; |
Dan Gohman | 68c9344 | 2009-06-03 19:11:31 +0000 | [diff] [blame] | 489 | |
Dan Gohman | de53dc0 | 2009-06-27 05:16:57 +0000 | [diff] [blame] | 490 | // Determine the insertion point for this user. By default, insert |
| 491 | // immediately before the user. The SCEVExpander class will automatically |
| 492 | // hoist loop invariants out of the loop. For PHI nodes, there may be |
| 493 | // multiple uses, so compute the nearest common dominator for the |
| 494 | // incoming blocks. |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 495 | Instruction *InsertPt = User; |
| 496 | if (PHINode *PHI = dyn_cast<PHINode>(InsertPt)) |
Dan Gohman | de53dc0 | 2009-06-27 05:16:57 +0000 | [diff] [blame] | 497 | for (unsigned i = 0, e = PHI->getNumIncomingValues(); i != e; ++i) |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 498 | if (PHI->getIncomingValue(i) == Op) { |
Dan Gohman | de53dc0 | 2009-06-27 05:16:57 +0000 | [diff] [blame] | 499 | if (InsertPt == User) |
| 500 | InsertPt = PHI->getIncomingBlock(i)->getTerminator(); |
| 501 | else |
| 502 | InsertPt = |
| 503 | DT->findNearestCommonDominator(InsertPt->getParent(), |
| 504 | PHI->getIncomingBlock(i)) |
| 505 | ->getTerminator(); |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Dan Gohman | 40a5a1b | 2009-06-24 01:18:18 +0000 | [diff] [blame] | 508 | // Now expand it into actual Instructions and patch it into place. |
| 509 | Value *NewVal = Rewriter.expandCodeFor(AR, UseTy, InsertPt); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 510 | |
| 511 | // Patch the new value into place. |
| 512 | if (Op->hasName()) |
| 513 | NewVal->takeName(Op); |
| 514 | User->replaceUsesOfWith(Op, NewVal); |
| 515 | UI->setOperandValToReplace(NewVal); |
David Greene | f67ef31 | 2010-01-05 01:27:06 +0000 | [diff] [blame] | 516 | DEBUG(dbgs() << "INDVARS: Rewrote IV '" << *AR << "' " << *Op << '\n' |
Chris Lattner | bdff548 | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 517 | << " into = " << *NewVal << "\n"); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 518 | ++NumRemoved; |
| 519 | Changed = true; |
| 520 | |
| 521 | // The old value may be dead now. |
| 522 | DeadInsts.push_back(Op); |
| 523 | } |
| 524 | } |
| 525 | |
Torok Edwin | 3d43138 | 2009-05-24 20:08:21 +0000 | [diff] [blame] | 526 | // Clear the rewriter cache, because values that are in the rewriter's cache |
| 527 | // can be deleted in the loop below, causing the AssertingVH in the cache to |
| 528 | // trigger. |
| 529 | Rewriter.clear(); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 530 | // Now that we're done iterating through lists, clean up any instructions |
| 531 | // which are now dead. |
Dan Gohman | a10756e | 2010-01-21 02:09:26 +0000 | [diff] [blame] | 532 | while (!DeadInsts.empty()) |
| 533 | if (Instruction *Inst = |
| 534 | dyn_cast_or_null<Instruction>(DeadInsts.pop_back_val())) |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 535 | RecursivelyDeleteTriviallyDeadInstructions(Inst); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | /// If there's a single exit block, sink any loop-invariant values that |
| 539 | /// were defined in the preheader but not used inside the loop into the |
| 540 | /// exit block to reduce register pressure in the loop. |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 541 | void IndVarSimplify::SinkUnusedInvariants(Loop *L) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 542 | BasicBlock *ExitBlock = L->getExitBlock(); |
| 543 | if (!ExitBlock) return; |
| 544 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 545 | BasicBlock *Preheader = L->getLoopPreheader(); |
Dan Gohman | 03e896b | 2009-11-05 21:11:53 +0000 | [diff] [blame] | 546 | if (!Preheader) return; |
| 547 | |
| 548 | Instruction *InsertPt = ExitBlock->getFirstNonPHI(); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 549 | BasicBlock::iterator I = Preheader->getTerminator(); |
| 550 | while (I != Preheader->begin()) { |
| 551 | --I; |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 552 | // New instructions were inserted at the end of the preheader. |
| 553 | if (isa<PHINode>(I)) |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 554 | break; |
Eli Friedman | 0c77db3 | 2009-07-15 22:48:29 +0000 | [diff] [blame] | 555 | // Don't move instructions which might have side effects, since the side |
| 556 | // effects need to complete before instructions inside the loop. Also |
| 557 | // don't move instructions which might read memory, since the loop may |
| 558 | // modify memory. Note that it's okay if the instruction might have |
| 559 | // undefined behavior: LoopSimplify guarantees that the preheader |
| 560 | // dominates the exit block. |
| 561 | if (I->mayHaveSideEffects() || I->mayReadFromMemory()) |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 562 | continue; |
Dan Gohman | 76f497a | 2009-08-25 17:42:10 +0000 | [diff] [blame] | 563 | // Don't sink static AllocaInsts out of the entry block, which would |
| 564 | // turn them into dynamic allocas! |
| 565 | if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) |
| 566 | if (AI->isStaticAlloca()) |
| 567 | continue; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 568 | // Determine if there is a use in or before the loop (direct or |
| 569 | // otherwise). |
| 570 | bool UsedInLoop = false; |
| 571 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 572 | UI != UE; ++UI) { |
| 573 | BasicBlock *UseBB = cast<Instruction>(UI)->getParent(); |
| 574 | if (PHINode *P = dyn_cast<PHINode>(UI)) { |
| 575 | unsigned i = |
| 576 | PHINode::getIncomingValueNumForOperand(UI.getOperandNo()); |
| 577 | UseBB = P->getIncomingBlock(i); |
| 578 | } |
| 579 | if (UseBB == Preheader || L->contains(UseBB)) { |
| 580 | UsedInLoop = true; |
| 581 | break; |
| 582 | } |
| 583 | } |
| 584 | // If there is, the def must remain in the preheader. |
| 585 | if (UsedInLoop) |
| 586 | continue; |
| 587 | // Otherwise, sink it to the exit block. |
| 588 | Instruction *ToMove = I; |
| 589 | bool Done = false; |
| 590 | if (I != Preheader->begin()) |
| 591 | --I; |
| 592 | else |
| 593 | Done = true; |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 594 | ToMove->moveBefore(InsertPt); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 595 | if (Done) |
| 596 | break; |
Dan Gohman | 667d787 | 2009-06-26 22:53:46 +0000 | [diff] [blame] | 597 | InsertPt = ToMove; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
Devang Patel | 13877bf | 2008-11-18 00:40:02 +0000 | [diff] [blame] | 601 | /// Return true if it is OK to use SIToFPInst for an inducation variable |
| 602 | /// with given inital and exit values. |
| 603 | static bool useSIToFPInst(ConstantFP &InitV, ConstantFP &ExitV, |
| 604 | uint64_t intIV, uint64_t intEV) { |
| 605 | |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 606 | if (InitV.getValueAPF().isNegative() || ExitV.getValueAPF().isNegative()) |
Devang Patel | 13877bf | 2008-11-18 00:40:02 +0000 | [diff] [blame] | 607 | return true; |
| 608 | |
| 609 | // If the iteration range can be handled by SIToFPInst then use it. |
| 610 | APInt Max = APInt::getSignedMaxValue(32); |
Dale Johannesen | bae7d6d | 2009-05-14 16:47:34 +0000 | [diff] [blame] | 611 | if (Max.getZExtValue() > static_cast<uint64_t>(abs64(intEV - intIV))) |
Devang Patel | 13877bf | 2008-11-18 00:40:02 +0000 | [diff] [blame] | 612 | return true; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 613 | |
Devang Patel | 13877bf | 2008-11-18 00:40:02 +0000 | [diff] [blame] | 614 | return false; |
| 615 | } |
| 616 | |
| 617 | /// convertToInt - Convert APF to an integer, if possible. |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 618 | static bool convertToInt(const APFloat &APF, uint64_t *intVal) { |
| 619 | |
| 620 | bool isExact = false; |
Evan Cheng | 794a7db | 2008-11-26 01:11:57 +0000 | [diff] [blame] | 621 | if (&APF.getSemantics() == &APFloat::PPCDoubleDouble) |
| 622 | return false; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 623 | if (APF.convertToInteger(intVal, 32, APF.isNegative(), |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 624 | APFloat::rmTowardZero, &isExact) |
| 625 | != APFloat::opOK) |
| 626 | return false; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 627 | if (!isExact) |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 628 | return false; |
| 629 | return true; |
| 630 | |
| 631 | } |
| 632 | |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 633 | /// HandleFloatingPointIV - If the loop has floating induction variable |
| 634 | /// then insert corresponding integer induction variable if possible. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 635 | /// For example, |
| 636 | /// for(double i = 0; i < 10000; ++i) |
| 637 | /// bar(i) |
| 638 | /// is converted into |
| 639 | /// for(int i = 0; i < 10000; ++i) |
| 640 | /// bar((double)i); |
| 641 | /// |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 642 | void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH) { |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 643 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 644 | unsigned IncomingEdge = L->contains(PH->getIncomingBlock(0)); |
| 645 | unsigned BackEdge = IncomingEdge^1; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 646 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 647 | // Check incoming value. |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 648 | ConstantFP *InitValue = dyn_cast<ConstantFP>(PH->getIncomingValue(IncomingEdge)); |
| 649 | if (!InitValue) return; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 650 | uint64_t newInitValue = |
| 651 | Type::getInt32Ty(PH->getContext())->getPrimitiveSizeInBits(); |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 652 | if (!convertToInt(InitValue->getValueAPF(), &newInitValue)) |
| 653 | return; |
| 654 | |
| 655 | // Check IV increment. Reject this PH if increement operation is not |
| 656 | // an add or increment value can not be represented by an integer. |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 657 | BinaryOperator *Incr = |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 658 | dyn_cast<BinaryOperator>(PH->getIncomingValue(BackEdge)); |
| 659 | if (!Incr) return; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 660 | if (Incr->getOpcode() != Instruction::FAdd) return; |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 661 | ConstantFP *IncrValue = NULL; |
| 662 | unsigned IncrVIndex = 1; |
| 663 | if (Incr->getOperand(1) == PH) |
| 664 | IncrVIndex = 0; |
| 665 | IncrValue = dyn_cast<ConstantFP>(Incr->getOperand(IncrVIndex)); |
| 666 | if (!IncrValue) return; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 667 | uint64_t newIncrValue = |
| 668 | Type::getInt32Ty(PH->getContext())->getPrimitiveSizeInBits(); |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 669 | if (!convertToInt(IncrValue->getValueAPF(), &newIncrValue)) |
| 670 | return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 671 | |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 672 | // Check Incr uses. One user is PH and the other users is exit condition used |
| 673 | // by the conditional terminator. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 674 | Value::use_iterator IncrUse = Incr->use_begin(); |
| 675 | Instruction *U1 = cast<Instruction>(IncrUse++); |
| 676 | if (IncrUse == Incr->use_end()) return; |
| 677 | Instruction *U2 = cast<Instruction>(IncrUse++); |
| 678 | if (IncrUse != Incr->use_end()) return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 679 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 680 | // Find exit condition. |
| 681 | FCmpInst *EC = dyn_cast<FCmpInst>(U1); |
| 682 | if (!EC) |
| 683 | EC = dyn_cast<FCmpInst>(U2); |
| 684 | if (!EC) return; |
| 685 | |
| 686 | if (BranchInst *BI = dyn_cast<BranchInst>(EC->getParent()->getTerminator())) { |
| 687 | if (!BI->isConditional()) return; |
| 688 | if (BI->getCondition() != EC) return; |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 689 | } |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 690 | |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 691 | // Find exit value. If exit value can not be represented as an interger then |
| 692 | // do not handle this floating point PH. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 693 | ConstantFP *EV = NULL; |
| 694 | unsigned EVIndex = 1; |
| 695 | if (EC->getOperand(1) == Incr) |
| 696 | EVIndex = 0; |
| 697 | EV = dyn_cast<ConstantFP>(EC->getOperand(EVIndex)); |
| 698 | if (!EV) return; |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 699 | uint64_t intEV = Type::getInt32Ty(PH->getContext())->getPrimitiveSizeInBits(); |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 700 | if (!convertToInt(EV->getValueAPF(), &intEV)) |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 701 | return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 702 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 703 | // Find new predicate for integer comparison. |
| 704 | CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE; |
| 705 | switch (EC->getPredicate()) { |
| 706 | case CmpInst::FCMP_OEQ: |
| 707 | case CmpInst::FCMP_UEQ: |
| 708 | NewPred = CmpInst::ICMP_EQ; |
| 709 | break; |
| 710 | case CmpInst::FCMP_OGT: |
| 711 | case CmpInst::FCMP_UGT: |
| 712 | NewPred = CmpInst::ICMP_UGT; |
| 713 | break; |
| 714 | case CmpInst::FCMP_OGE: |
| 715 | case CmpInst::FCMP_UGE: |
| 716 | NewPred = CmpInst::ICMP_UGE; |
| 717 | break; |
| 718 | case CmpInst::FCMP_OLT: |
| 719 | case CmpInst::FCMP_ULT: |
| 720 | NewPred = CmpInst::ICMP_ULT; |
| 721 | break; |
| 722 | case CmpInst::FCMP_OLE: |
| 723 | case CmpInst::FCMP_ULE: |
| 724 | NewPred = CmpInst::ICMP_ULE; |
| 725 | break; |
| 726 | default: |
| 727 | break; |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 728 | } |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 729 | if (NewPred == CmpInst::BAD_ICMP_PREDICATE) return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 730 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 731 | // Insert new integer induction variable. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 732 | PHINode *NewPHI = PHINode::Create(Type::getInt32Ty(PH->getContext()), |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 733 | PH->getName()+".int", PH); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 734 | NewPHI->addIncoming(ConstantInt::get(Type::getInt32Ty(PH->getContext()), |
| 735 | newInitValue), |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 736 | PH->getIncomingBlock(IncomingEdge)); |
| 737 | |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 738 | Value *NewAdd = BinaryOperator::CreateAdd(NewPHI, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 739 | ConstantInt::get(Type::getInt32Ty(PH->getContext()), |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 740 | newIncrValue), |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 741 | Incr->getName()+".int", Incr); |
| 742 | NewPHI->addIncoming(NewAdd, PH->getIncomingBlock(BackEdge)); |
| 743 | |
Dale Johannesen | 617d108 | 2009-04-27 21:03:15 +0000 | [diff] [blame] | 744 | // The back edge is edge 1 of newPHI, whatever it may have been in the |
| 745 | // original PHI. |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 746 | ConstantInt *NewEV = ConstantInt::get(Type::getInt32Ty(PH->getContext()), |
| 747 | intEV); |
Dale Johannesen | 617d108 | 2009-04-27 21:03:15 +0000 | [diff] [blame] | 748 | Value *LHS = (EVIndex == 1 ? NewPHI->getIncomingValue(1) : NewEV); |
| 749 | Value *RHS = (EVIndex == 1 ? NewEV : NewPHI->getIncomingValue(1)); |
Owen Anderson | 333c400 | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 750 | ICmpInst *NewEC = new ICmpInst(EC->getParent()->getTerminator(), |
Daniel Dunbar | 460f656 | 2009-07-26 09:48:23 +0000 | [diff] [blame] | 751 | NewPred, LHS, RHS, EC->getName()); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 752 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 753 | // In the following deltions, PH may become dead and may be deleted. |
| 754 | // Use a WeakVH to observe whether this happens. |
| 755 | WeakVH WeakPH = PH; |
| 756 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 757 | // Delete old, floating point, exit comparision instruction. |
Dan Gohman | 14fba29 | 2009-05-24 18:09:01 +0000 | [diff] [blame] | 758 | NewEC->takeName(EC); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 759 | EC->replaceAllUsesWith(NewEC); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 760 | RecursivelyDeleteTriviallyDeadInstructions(EC); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 761 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 762 | // Delete old, floating point, increment instruction. |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 763 | Incr->replaceAllUsesWith(UndefValue::get(Incr->getType())); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 764 | RecursivelyDeleteTriviallyDeadInstructions(Incr); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 765 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 766 | // Replace floating induction variable, if it isn't already deleted. |
| 767 | // Give SIToFPInst preference over UIToFPInst because it is faster on |
| 768 | // platforms that are widely used. |
| 769 | if (WeakPH && !PH->use_empty()) { |
| 770 | if (useSIToFPInst(*InitValue, *EV, newInitValue, intEV)) { |
| 771 | SIToFPInst *Conv = new SIToFPInst(NewPHI, PH->getType(), "indvar.conv", |
| 772 | PH->getParent()->getFirstNonPHI()); |
| 773 | PH->replaceAllUsesWith(Conv); |
| 774 | } else { |
| 775 | UIToFPInst *Conv = new UIToFPInst(NewPHI, PH->getType(), "indvar.conv", |
| 776 | PH->getParent()->getFirstNonPHI()); |
| 777 | PH->replaceAllUsesWith(Conv); |
| 778 | } |
| 779 | RecursivelyDeleteTriviallyDeadInstructions(PH); |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 780 | } |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 781 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 782 | // Add a new IVUsers entry for the newly-created integer PHI. |
| 783 | IU->AddUsersIfInteresting(NewPHI); |
| 784 | } |