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