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. |
| 20 | // 3. Any pointer arithmetic recurrences are raised to use array subscripts. |
| 21 | // |
| 22 | // If the trip count of a loop is computable, this pass also makes the following |
| 23 | // changes: |
| 24 | // 1. The exit condition for the loop is canonicalized to compare the |
| 25 | // induction value against the exit value. This turns loops like: |
| 26 | // 'for (i = 7; i*i < 1000; ++i)' into 'for (i = 0; i != 25; ++i)' |
| 27 | // 2. Any use outside of the loop of an expression derived from the indvar |
| 28 | // is changed to compute the derived value outside of the loop, eliminating |
| 29 | // the dependence on the exit value of the induction variable. If the only |
| 30 | // purpose of the loop is to compute the exit value of some derived |
| 31 | // expression, this transformation will make the loop dead. |
| 32 | // |
| 33 | // This transformation should be followed by strength reduction after all of the |
Dan Gohman | c2c4cbf | 2009-05-19 20:38:47 +0000 | [diff] [blame] | 34 | // desired loop transformations have been performed. |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 35 | // |
| 36 | //===----------------------------------------------------------------------===// |
| 37 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 38 | #define DEBUG_TYPE "indvars" |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 39 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 40 | #include "llvm/BasicBlock.h" |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 41 | #include "llvm/Constants.h" |
Chris Lattner | 18b3c97 | 2003-12-22 05:02:01 +0000 | [diff] [blame] | 42 | #include "llvm/Instructions.h" |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 43 | #include "llvm/Type.h" |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/Dominators.h" |
| 45 | #include "llvm/Analysis/IVUsers.h" |
Nate Begeman | 36f891b | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 47 | #include "llvm/Analysis/LoopInfo.h" |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 48 | #include "llvm/Analysis/LoopPass.h" |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 49 | #include "llvm/Support/CFG.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 50 | #include "llvm/Support/Compiler.h" |
Chris Lattner | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 51 | #include "llvm/Support/Debug.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 52 | #include "llvm/Transforms/Utils/Local.h" |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 53 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 54 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/Statistic.h" |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/STLExtras.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 58 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 60 | STATISTIC(NumRemoved , "Number of aux indvars removed"); |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 61 | STATISTIC(NumInserted, "Number of canonical indvars added"); |
| 62 | STATISTIC(NumReplaced, "Number of exit values replaced"); |
| 63 | STATISTIC(NumLFTR , "Number of loop exit tests replaced"); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 64 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 65 | namespace { |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 66 | class VISIBILITY_HIDDEN IndVarSimplify : public LoopPass { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 67 | IVUsers *IU; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 68 | LoopInfo *LI; |
| 69 | ScalarEvolution *SE; |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 70 | bool Changed; |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 71 | public: |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 72 | |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 73 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 74 | IndVarSimplify() : LoopPass(&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 75 | |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 76 | virtual bool runOnLoop(Loop *L, LPPassManager &LPM); |
| 77 | |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 78 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 79 | AU.addRequired<DominatorTree>(); |
Devang Patel | bc533cd | 2007-09-10 18:08:23 +0000 | [diff] [blame] | 80 | AU.addRequired<ScalarEvolution>(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 81 | AU.addRequiredID(LCSSAID); |
| 82 | AU.addRequiredID(LoopSimplifyID); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 83 | AU.addRequired<LoopInfo>(); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 84 | AU.addRequired<IVUsers>(); |
Dan Gohman | 474cecf | 2009-02-23 16:29:41 +0000 | [diff] [blame] | 85 | AU.addPreserved<ScalarEvolution>(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 86 | AU.addPreservedID(LoopSimplifyID); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 87 | AU.addPreserved<IVUsers>(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 88 | AU.addPreservedID(LCSSAID); |
| 89 | AU.setPreservesCFG(); |
| 90 | } |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 92 | private: |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 93 | |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 94 | void RewriteNonIntegerIVs(Loop *L); |
| 95 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 96 | ICmpInst *LinearFunctionTestReplace(Loop *L, SCEVHandle BackedgeTakenCount, |
Dan Gohman | a575871 | 2009-02-17 15:57:39 +0000 | [diff] [blame] | 97 | Value *IndVar, |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 98 | BasicBlock *ExitingBlock, |
| 99 | BranchInst *BI, |
Dan Gohman | 15cab28 | 2009-02-23 23:20:35 +0000 | [diff] [blame] | 100 | SCEVExpander &Rewriter); |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 101 | void RewriteLoopExitValues(Loop *L, const SCEV *BackedgeTakenCount); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 102 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 103 | void RewriteIVExpressions(Loop *L, const Type *LargestType, |
| 104 | SCEVExpander &Rewriter); |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 105 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 106 | void SinkUnusedInvariants(Loop *L, SCEVExpander &Rewriter); |
| 107 | |
| 108 | void FixUsesBeforeDefs(Loop *L, SCEVExpander &Rewriter); |
| 109 | |
| 110 | void HandleFloatingPointIV(Loop *L, PHINode *PH); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 111 | }; |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 112 | } |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 113 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 114 | char IndVarSimplify::ID = 0; |
| 115 | static RegisterPass<IndVarSimplify> |
| 116 | X("indvars", "Canonicalize Induction Variables"); |
| 117 | |
Daniel Dunbar | 394f044 | 2008-10-22 23:32:42 +0000 | [diff] [blame] | 118 | Pass *llvm::createIndVarSimplifyPass() { |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 119 | return new IndVarSimplify(); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 122 | /// LinearFunctionTestReplace - This method rewrites the exit condition of the |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 123 | /// loop to be a canonical != comparison against the incremented loop induction |
| 124 | /// variable. This pass is able to rewrite the exit tests of any loop where the |
| 125 | /// SCEV analysis can determine a loop-invariant trip count of the loop, which |
| 126 | /// is actually a much broader range than just linear tests. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 127 | ICmpInst *IndVarSimplify::LinearFunctionTestReplace(Loop *L, |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 128 | SCEVHandle BackedgeTakenCount, |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 129 | Value *IndVar, |
| 130 | BasicBlock *ExitingBlock, |
| 131 | BranchInst *BI, |
Dan Gohman | 15cab28 | 2009-02-23 23:20:35 +0000 | [diff] [blame] | 132 | SCEVExpander &Rewriter) { |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 133 | // If the exiting block is not the same as the backedge block, we must compare |
| 134 | // against the preincremented value, otherwise we prefer to compare against |
| 135 | // the post-incremented value. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 136 | Value *CmpIndVar; |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 137 | SCEVHandle RHS = BackedgeTakenCount; |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 138 | if (ExitingBlock == L->getLoopLatch()) { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 139 | // Add one to the "backedge-taken" count to get the trip count. |
| 140 | // If this addition may overflow, we have to be more pessimistic and |
| 141 | // cast the induction variable before doing the add. |
| 142 | SCEVHandle Zero = SE->getIntegerSCEV(0, BackedgeTakenCount->getType()); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 143 | SCEVHandle N = |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 144 | SE->getAddExpr(BackedgeTakenCount, |
| 145 | SE->getIntegerSCEV(1, BackedgeTakenCount->getType())); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 146 | if ((isa<SCEVConstant>(N) && !N->isZero()) || |
| 147 | SE->isLoopGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) { |
| 148 | // No overflow. Cast the sum. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 149 | RHS = SE->getTruncateOrZeroExtend(N, IndVar->getType()); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 150 | } else { |
| 151 | // Potential overflow. Cast before doing the add. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 152 | RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount, |
| 153 | IndVar->getType()); |
| 154 | RHS = SE->getAddExpr(RHS, |
| 155 | SE->getIntegerSCEV(1, IndVar->getType())); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 156 | } |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 157 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 158 | // The BackedgeTaken expression contains the number of times that the |
| 159 | // backedge branches to the loop header. This is one less than the |
| 160 | // number of times the loop executes, so use the incremented indvar. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 161 | CmpIndVar = L->getCanonicalInductionVariableIncrement(); |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 162 | } else { |
| 163 | // We have to use the preincremented value... |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 164 | RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount, |
| 165 | IndVar->getType()); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 166 | CmpIndVar = IndVar; |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 167 | } |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 169 | // Expand the code for the iteration count into the preheader of the loop. |
| 170 | BasicBlock *Preheader = L->getLoopPreheader(); |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 171 | Value *ExitCnt = Rewriter.expandCodeFor(RHS, CmpIndVar->getType(), |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 172 | Preheader->getTerminator()); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 173 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 174 | // Insert a new icmp_ne or icmp_eq instruction before the branch. |
| 175 | ICmpInst::Predicate Opcode; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 176 | if (L->contains(BI->getSuccessor(0))) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 177 | Opcode = ICmpInst::ICMP_NE; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 178 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 179 | Opcode = ICmpInst::ICMP_EQ; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 180 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 181 | DOUT << "INDVARS: Rewriting loop exit condition to:\n" |
| 182 | << " LHS:" << *CmpIndVar // includes a newline |
| 183 | << " op:\t" |
Dan Gohman | f108e2e | 2009-02-14 02:26:50 +0000 | [diff] [blame] | 184 | << (Opcode == ICmpInst::ICMP_NE ? "!=" : "==") << "\n" |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 185 | << " RHS:\t" << *RHS << "\n"; |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 186 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 187 | ICmpInst *Cond = new ICmpInst(Opcode, CmpIndVar, ExitCnt, "exitcond", BI); |
| 188 | |
| 189 | Instruction *OrigCond = cast<Instruction>(BI->getCondition()); |
Dan Gohman | 95bdbfa | 2009-05-24 19:11:38 +0000 | [diff] [blame] | 190 | // It's tempting to use replaceAllUsesWith here to fully replace the old |
| 191 | // comparison, but that's not immediately safe, since users of the old |
| 192 | // comparison may not be dominated by the new comparison. Instead, just |
| 193 | // update the branch to use the new comparison; in the common case this |
| 194 | // will make old comparison dead. |
| 195 | BI->setCondition(Cond); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 196 | RecursivelyDeleteTriviallyDeadInstructions(OrigCond); |
| 197 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 198 | ++NumLFTR; |
| 199 | Changed = true; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 200 | return Cond; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 203 | /// RewriteLoopExitValues - Check to see if this loop has a computable |
| 204 | /// loop-invariant execution count. If so, this means that we can compute the |
| 205 | /// final value of any expressions that are recurrent in the loop, and |
| 206 | /// substitute the exit values from the loop into any instructions outside of |
| 207 | /// the loop that use the final values of the current expressions. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 208 | /// |
| 209 | /// This is mostly redundant with the regular IndVarSimplify activities that |
| 210 | /// happen later, except that it's more powerful in some cases, because it's |
| 211 | /// able to brute-force evaluate arbitrary instructions as long as they have |
| 212 | /// constant operands at the beginning of the loop. |
Dan Gohman | 890f92b | 2009-04-18 17:56:28 +0000 | [diff] [blame] | 213 | void IndVarSimplify::RewriteLoopExitValues(Loop *L, |
| 214 | const SCEV *BackedgeTakenCount) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 215 | // Verify the input to the pass in already in LCSSA form. |
| 216 | assert(L->isLCSSAForm()); |
| 217 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 218 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 219 | |
| 220 | // Scan all of the instructions in the loop, looking at those that have |
| 221 | // extra-loop users and which are recurrences. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 222 | SCEVExpander Rewriter(*SE); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 223 | |
| 224 | // We insert the code into the preheader of the loop if the loop contains |
| 225 | // multiple exit blocks, or in the exit block if there is exactly one. |
| 226 | BasicBlock *BlockToInsertInto; |
Devang Patel | b7211a2 | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 227 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 228 | L->getUniqueExitBlocks(ExitBlocks); |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 229 | if (ExitBlocks.size() == 1) |
| 230 | BlockToInsertInto = ExitBlocks[0]; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 231 | else |
| 232 | BlockToInsertInto = Preheader; |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 233 | BasicBlock::iterator InsertPt = BlockToInsertInto->getFirstNonPHI(); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 234 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 235 | std::map<Instruction*, Value*> ExitValues; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 236 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 237 | // Find all values that are computed inside the loop, but used outside of it. |
| 238 | // Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan |
| 239 | // the exit blocks of the loop to find them. |
| 240 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
| 241 | BasicBlock *ExitBB = ExitBlocks[i]; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 243 | // If there are no PHI nodes in this exit block, then no values defined |
| 244 | // inside the loop are used on this path, skip it. |
| 245 | PHINode *PN = dyn_cast<PHINode>(ExitBB->begin()); |
| 246 | if (!PN) continue; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 247 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 248 | unsigned NumPreds = PN->getNumIncomingValues(); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 249 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 250 | // Iterate over all of the PHI nodes. |
| 251 | BasicBlock::iterator BBI = ExitBB->begin(); |
| 252 | while ((PN = dyn_cast<PHINode>(BBI++))) { |
Torok Edwin | 3790fb0 | 2009-05-24 19:36:09 +0000 | [diff] [blame] | 253 | if (PN->use_empty()) |
| 254 | continue; // dead use, don't replace it |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 255 | // Iterate over all of the values in all the PHI nodes. |
| 256 | for (unsigned i = 0; i != NumPreds; ++i) { |
| 257 | // If the value being merged in is not integer or is not defined |
| 258 | // in the loop, skip it. |
| 259 | Value *InVal = PN->getIncomingValue(i); |
| 260 | if (!isa<Instruction>(InVal) || |
| 261 | // SCEV only supports integer expressions for now. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 262 | (!isa<IntegerType>(InVal->getType()) && |
| 263 | !isa<PointerType>(InVal->getType()))) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 264 | continue; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 265 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 266 | // If this pred is for a subloop, not L itself, skip it. |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 267 | if (LI->getLoopFor(PN->getIncomingBlock(i)) != L) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 268 | continue; // The Block is in a subloop, skip it. |
| 269 | |
| 270 | // Check that InVal is defined in the loop. |
| 271 | Instruction *Inst = cast<Instruction>(InVal); |
| 272 | if (!L->contains(Inst->getParent())) |
| 273 | continue; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 274 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 275 | // Okay, this instruction has a user outside of the current loop |
| 276 | // and varies predictably *inside* the loop. Evaluate the value it |
| 277 | // contains when the loop exits, if possible. |
Dan Gohman | d594e6f | 2009-05-24 23:25:42 +0000 | [diff] [blame] | 278 | SCEVHandle ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop()); |
| 279 | if (!ExitValue->isLoopInvariant(L)) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 280 | continue; |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 281 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 282 | Changed = true; |
| 283 | ++NumReplaced; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 284 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 285 | // See if we already computed the exit value for the instruction, if so, |
| 286 | // just reuse it. |
| 287 | Value *&ExitVal = ExitValues[Inst]; |
| 288 | if (!ExitVal) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 289 | ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), InsertPt); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 290 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 291 | DOUT << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal |
| 292 | << " LoopVal = " << *Inst << "\n"; |
| 293 | |
| 294 | PN->setIncomingValue(i, ExitVal); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 295 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 296 | // If this instruction is dead now, delete it. |
| 297 | RecursivelyDeleteTriviallyDeadInstructions(Inst); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 298 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 299 | // See if this is a single-entry LCSSA PHI node. If so, we can (and |
| 300 | // have to) remove |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 301 | // the PHI entirely. This is safe, because the NewVal won't be variant |
| 302 | // in the loop, so we don't need an LCSSA phi node anymore. |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 303 | if (NumPreds == 1) { |
| 304 | PN->replaceAllUsesWith(ExitVal); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 305 | RecursivelyDeleteTriviallyDeadInstructions(PN); |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 306 | break; |
Chris Lattner | c9838f2 | 2007-03-03 22:48:48 +0000 | [diff] [blame] | 307 | } |
| 308 | } |
Chris Lattner | c9838f2 | 2007-03-03 22:48:48 +0000 | [diff] [blame] | 309 | } |
| 310 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 313 | void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 314 | // First step. Check to see if there are any floating-point recurrences. |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 315 | // If there are, change them into integer recurrences, permitting analysis by |
| 316 | // the SCEV routines. |
| 317 | // |
| 318 | BasicBlock *Header = L->getHeader(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 319 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 320 | SmallVector<WeakVH, 8> PHIs; |
| 321 | for (BasicBlock::iterator I = Header->begin(); |
| 322 | PHINode *PN = dyn_cast<PHINode>(I); ++I) |
| 323 | PHIs.push_back(PN); |
| 324 | |
| 325 | for (unsigned i = 0, e = PHIs.size(); i != e; ++i) |
| 326 | if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i])) |
| 327 | HandleFloatingPointIV(L, PN); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 328 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 329 | // If the loop previously had floating-point IV, ScalarEvolution |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 330 | // may not have been able to compute a trip count. Now that we've done some |
| 331 | // re-writing, the trip count may be computable. |
| 332 | if (Changed) |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 333 | SE->forgetLoopBackedgeTakenCount(L); |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 336 | bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) { |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 337 | IU = &getAnalysis<IVUsers>(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 338 | LI = &getAnalysis<LoopInfo>(); |
| 339 | SE = &getAnalysis<ScalarEvolution>(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 340 | Changed = false; |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 341 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame] | 342 | // If there are any floating-point recurrences, attempt to |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 343 | // transform them to use integer recurrences. |
| 344 | RewriteNonIntegerIVs(L); |
| 345 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 346 | BasicBlock *Header = L->getHeader(); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 347 | BasicBlock *ExitingBlock = L->getExitingBlock(); // may be null |
| 348 | SCEVHandle BackedgeTakenCount = SE->getBackedgeTakenCount(L); |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 350 | // Check to see if this loop has a computable loop-invariant execution count. |
| 351 | // If so, this means that we can compute the final value of any expressions |
| 352 | // that are recurrent in the loop, and substitute the exit values from the |
| 353 | // loop into any instructions outside of the loop that use the final values of |
| 354 | // the current expressions. |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 355 | // |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 356 | if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) |
| 357 | RewriteLoopExitValues(L, BackedgeTakenCount); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 358 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 359 | // Compute the type of the largest recurrence expression, and decide whether |
| 360 | // a canonical induction variable should be inserted. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 361 | const Type *LargestType = 0; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 362 | bool NeedCannIV = false; |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 363 | if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) { |
| 364 | LargestType = BackedgeTakenCount->getType(); |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 365 | LargestType = SE->getEffectiveSCEVType(LargestType); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 366 | // If we have a known trip count and a single exit block, we'll be |
| 367 | // rewriting the loop exit test condition below, which requires a |
| 368 | // canonical induction variable. |
| 369 | if (ExitingBlock) |
| 370 | NeedCannIV = true; |
Chris Lattner | f50af08 | 2004-04-17 18:08:33 +0000 | [diff] [blame] | 371 | } |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 372 | for (unsigned i = 0, e = IU->StrideOrder.size(); i != e; ++i) { |
| 373 | SCEVHandle Stride = IU->StrideOrder[i]; |
| 374 | const Type *Ty = SE->getEffectiveSCEVType(Stride->getType()); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 375 | if (!LargestType || |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 376 | SE->getTypeSizeInBits(Ty) > |
Dan Gohman | af79fb5 | 2009-04-21 01:07:12 +0000 | [diff] [blame] | 377 | SE->getTypeSizeInBits(LargestType)) |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 378 | LargestType = Ty; |
| 379 | |
| 380 | std::map<SCEVHandle, IVUsersOfOneStride *>::iterator SI = |
| 381 | IU->IVUsesByStride.find(IU->StrideOrder[i]); |
| 382 | assert(SI != IU->IVUsesByStride.end() && "Stride doesn't exist!"); |
| 383 | |
| 384 | if (!SI->second->Users.empty()) |
| 385 | NeedCannIV = true; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 388 | // Create a rewriter object which we'll use to transform the code with. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 389 | SCEVExpander Rewriter(*SE); |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 390 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 391 | // Now that we know the largest of of the induction variable expressions |
| 392 | // in this loop, insert a canonical induction variable of the largest size. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 393 | Value *IndVar = 0; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 394 | if (NeedCannIV) { |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 395 | IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L,LargestType); |
| 396 | ++NumInserted; |
| 397 | Changed = true; |
| 398 | DOUT << "INDVARS: New CanIV: " << *IndVar; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 399 | } |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 400 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 401 | // If we have a trip count expression, rewrite the loop's exit condition |
| 402 | // using it. We can currently only handle loops with a single exit. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 403 | ICmpInst *NewICmp = 0; |
| 404 | if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) && ExitingBlock) { |
| 405 | assert(NeedCannIV && |
| 406 | "LinearFunctionTestReplace requires a canonical induction variable"); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 407 | // Can't rewrite non-branch yet. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 408 | if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator())) |
| 409 | NewICmp = LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar, |
| 410 | ExitingBlock, BI, Rewriter); |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 413 | Rewriter.setInsertionPoint(Header->getFirstNonPHI()); |
Chris Lattner | 5d461d2 | 2004-04-21 22:22:01 +0000 | [diff] [blame] | 414 | |
Torok Edwin | 3d43138 | 2009-05-24 20:08:21 +0000 | [diff] [blame] | 415 | // Rewrite IV-derived expressions. Clears the rewriter cache. |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 416 | RewriteIVExpressions(L, LargestType, Rewriter); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 417 | |
Torok Edwin | 3d43138 | 2009-05-24 20:08:21 +0000 | [diff] [blame] | 418 | // The Rewriter may only be used for isInsertedInstruction queries from this |
| 419 | // point on. |
| 420 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 421 | // Loop-invariant instructions in the preheader that aren't used in the |
| 422 | // loop may be sunk below the loop to reduce register pressure. |
| 423 | SinkUnusedInvariants(L, Rewriter); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 424 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 425 | // Reorder instructions to avoid use-before-def conditions. |
| 426 | FixUsesBeforeDefs(L, Rewriter); |
| 427 | |
| 428 | // For completeness, inform IVUsers of the IV use in the newly-created |
| 429 | // loop exit test instruction. |
| 430 | if (NewICmp) |
| 431 | IU->AddUsersIfInteresting(cast<Instruction>(NewICmp->getOperand(0))); |
| 432 | |
| 433 | // Clean up dead instructions. |
| 434 | DeleteDeadPHIs(L->getHeader()); |
| 435 | // Check a post-condition. |
| 436 | assert(L->isLCSSAForm() && "Indvars did not leave the loop in lcssa form!"); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 437 | return Changed; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 438 | } |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 439 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 440 | void IndVarSimplify::RewriteIVExpressions(Loop *L, const Type *LargestType, |
| 441 | SCEVExpander &Rewriter) { |
| 442 | SmallVector<WeakVH, 16> DeadInsts; |
| 443 | |
| 444 | // Rewrite all induction variable expressions in terms of the canonical |
| 445 | // induction variable. |
| 446 | // |
| 447 | // If there were induction variables of other sizes or offsets, manually |
| 448 | // add the offsets to the primary induction variable and cast, avoiding |
| 449 | // the need for the code evaluation methods to insert induction variables |
| 450 | // of different sizes. |
| 451 | for (unsigned i = 0, e = IU->StrideOrder.size(); i != e; ++i) { |
| 452 | SCEVHandle Stride = IU->StrideOrder[i]; |
| 453 | |
| 454 | std::map<SCEVHandle, IVUsersOfOneStride *>::iterator SI = |
| 455 | IU->IVUsesByStride.find(IU->StrideOrder[i]); |
| 456 | assert(SI != IU->IVUsesByStride.end() && "Stride doesn't exist!"); |
| 457 | ilist<IVStrideUse> &List = SI->second->Users; |
| 458 | for (ilist<IVStrideUse>::iterator UI = List.begin(), |
| 459 | E = List.end(); UI != E; ++UI) { |
| 460 | SCEVHandle Offset = UI->getOffset(); |
| 461 | Value *Op = UI->getOperandValToReplace(); |
| 462 | Instruction *User = UI->getUser(); |
| 463 | bool isSigned = UI->isSigned(); |
| 464 | |
| 465 | // Compute the final addrec to expand into code. |
| 466 | SCEVHandle AR = IU->getReplacementExpr(*UI); |
| 467 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 468 | Value *NewVal = 0; |
| 469 | if (AR->isLoopInvariant(L)) { |
| 470 | BasicBlock::iterator I = Rewriter.getInsertionPoint(); |
| 471 | // Expand loop-invariant values in the loop preheader. They will |
| 472 | // be sunk to the exit block later, if possible. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 473 | NewVal = |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 474 | Rewriter.expandCodeFor(AR, LargestType, |
| 475 | L->getLoopPreheader()->getTerminator()); |
| 476 | Rewriter.setInsertionPoint(I); |
| 477 | ++NumReplaced; |
| 478 | } else { |
Dan Gohman | 68c9344 | 2009-06-03 19:11:31 +0000 | [diff] [blame] | 479 | // FIXME: It is an extremely bad idea to indvar substitute anything more |
| 480 | // complex than affine induction variables. Doing so will put expensive |
| 481 | // polynomial evaluations inside of the loop, and the str reduction pass |
| 482 | // currently can only reduce affine polynomials. For now just disable |
| 483 | // indvar subst on anything more complex than an affine addrec, unless |
| 484 | // it can be expanded to a trivial value. |
| 485 | if (!Stride->isLoopInvariant(L)) |
| 486 | continue; |
| 487 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 488 | const Type *IVTy = Offset->getType(); |
| 489 | const Type *UseTy = Op->getType(); |
| 490 | |
| 491 | // Promote the Offset and Stride up to the canonical induction |
| 492 | // variable's bit width. |
| 493 | SCEVHandle PromotedOffset = Offset; |
| 494 | SCEVHandle PromotedStride = Stride; |
| 495 | if (SE->getTypeSizeInBits(IVTy) != SE->getTypeSizeInBits(LargestType)) { |
| 496 | // It doesn't matter for correctness whether zero or sign extension |
| 497 | // is used here, since the value is truncated away below, but if the |
| 498 | // value is signed, sign extension is more likely to be folded. |
| 499 | if (isSigned) { |
| 500 | PromotedOffset = SE->getSignExtendExpr(PromotedOffset, LargestType); |
| 501 | PromotedStride = SE->getSignExtendExpr(PromotedStride, LargestType); |
| 502 | } else { |
| 503 | PromotedOffset = SE->getZeroExtendExpr(PromotedOffset, LargestType); |
| 504 | // If the stride is obviously negative, use sign extension to |
| 505 | // produce things like x-1 instead of x+255. |
| 506 | if (isa<SCEVConstant>(PromotedStride) && |
| 507 | cast<SCEVConstant>(PromotedStride) |
| 508 | ->getValue()->getValue().isNegative()) |
| 509 | PromotedStride = SE->getSignExtendExpr(PromotedStride, |
| 510 | LargestType); |
| 511 | else |
| 512 | PromotedStride = SE->getZeroExtendExpr(PromotedStride, |
| 513 | LargestType); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | // Create the SCEV representing the offset from the canonical |
| 518 | // induction variable, still in the canonical induction variable's |
| 519 | // type, so that all expanded arithmetic is done in the same type. |
| 520 | SCEVHandle NewAR = SE->getAddRecExpr(SE->getIntegerSCEV(0, LargestType), |
Dan Gohman | 68c9344 | 2009-06-03 19:11:31 +0000 | [diff] [blame] | 521 | PromotedStride, L); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 522 | // Add the PromotedOffset as a separate step, because it may not be |
| 523 | // loop-invariant. |
| 524 | NewAR = SE->getAddExpr(NewAR, PromotedOffset); |
| 525 | |
| 526 | // Expand the addrec into instructions. |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 527 | Value *V = Rewriter.expandCodeFor(NewAR); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 528 | |
| 529 | // Insert an explicit cast if necessary to truncate the value |
| 530 | // down to the original stride type. This is done outside of |
| 531 | // SCEVExpander because in SCEV expressions, a truncate of an |
| 532 | // addrec is always folded. |
| 533 | if (LargestType != IVTy) { |
| 534 | if (SE->getTypeSizeInBits(IVTy) != SE->getTypeSizeInBits(LargestType)) |
| 535 | NewAR = SE->getTruncateExpr(NewAR, IVTy); |
| 536 | if (Rewriter.isInsertedExpression(NewAR)) |
Dan Gohman | 5be18e8 | 2009-05-19 02:15:55 +0000 | [diff] [blame] | 537 | V = Rewriter.expandCodeFor(NewAR); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 538 | else { |
| 539 | V = Rewriter.InsertCastOfTo(CastInst::getCastOpcode(V, false, |
| 540 | IVTy, false), |
| 541 | V, IVTy); |
| 542 | assert(!isa<SExtInst>(V) && !isa<ZExtInst>(V) && |
| 543 | "LargestType wasn't actually the largest type!"); |
| 544 | // Force the rewriter to use this trunc whenever this addrec |
| 545 | // appears so that it doesn't insert new phi nodes or |
| 546 | // arithmetic in a different type. |
| 547 | Rewriter.addInsertedValue(V, NewAR); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | DOUT << "INDVARS: Made offset-and-trunc IV for offset " |
| 552 | << *IVTy << " " << *Offset << ": "; |
| 553 | DEBUG(WriteAsOperand(*DOUT, V, false)); |
| 554 | DOUT << "\n"; |
| 555 | |
| 556 | // Now expand it into actual Instructions and patch it into place. |
| 557 | NewVal = Rewriter.expandCodeFor(AR, UseTy); |
| 558 | } |
| 559 | |
| 560 | // Patch the new value into place. |
| 561 | if (Op->hasName()) |
| 562 | NewVal->takeName(Op); |
| 563 | User->replaceUsesOfWith(Op, NewVal); |
| 564 | UI->setOperandValToReplace(NewVal); |
| 565 | DOUT << "INDVARS: Rewrote IV '" << *AR << "' " << *Op |
| 566 | << " into = " << *NewVal << "\n"; |
| 567 | ++NumRemoved; |
| 568 | Changed = true; |
| 569 | |
| 570 | // The old value may be dead now. |
| 571 | DeadInsts.push_back(Op); |
| 572 | } |
| 573 | } |
| 574 | |
Torok Edwin | 3d43138 | 2009-05-24 20:08:21 +0000 | [diff] [blame] | 575 | // Clear the rewriter cache, because values that are in the rewriter's cache |
| 576 | // can be deleted in the loop below, causing the AssertingVH in the cache to |
| 577 | // trigger. |
| 578 | Rewriter.clear(); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 579 | // Now that we're done iterating through lists, clean up any instructions |
| 580 | // which are now dead. |
| 581 | while (!DeadInsts.empty()) { |
| 582 | Instruction *Inst = dyn_cast_or_null<Instruction>(DeadInsts.pop_back_val()); |
| 583 | if (Inst) |
| 584 | RecursivelyDeleteTriviallyDeadInstructions(Inst); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | /// If there's a single exit block, sink any loop-invariant values that |
| 589 | /// were defined in the preheader but not used inside the loop into the |
| 590 | /// exit block to reduce register pressure in the loop. |
| 591 | void IndVarSimplify::SinkUnusedInvariants(Loop *L, SCEVExpander &Rewriter) { |
| 592 | BasicBlock *ExitBlock = L->getExitBlock(); |
| 593 | if (!ExitBlock) return; |
| 594 | |
| 595 | Instruction *NonPHI = ExitBlock->getFirstNonPHI(); |
| 596 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 597 | BasicBlock::iterator I = Preheader->getTerminator(); |
| 598 | while (I != Preheader->begin()) { |
| 599 | --I; |
| 600 | // New instructions were inserted at the end of the preheader. Only |
| 601 | // consider those new instructions. |
| 602 | if (!Rewriter.isInsertedInstruction(I)) |
| 603 | break; |
| 604 | // Determine if there is a use in or before the loop (direct or |
| 605 | // otherwise). |
| 606 | bool UsedInLoop = false; |
| 607 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 608 | UI != UE; ++UI) { |
| 609 | BasicBlock *UseBB = cast<Instruction>(UI)->getParent(); |
| 610 | if (PHINode *P = dyn_cast<PHINode>(UI)) { |
| 611 | unsigned i = |
| 612 | PHINode::getIncomingValueNumForOperand(UI.getOperandNo()); |
| 613 | UseBB = P->getIncomingBlock(i); |
| 614 | } |
| 615 | if (UseBB == Preheader || L->contains(UseBB)) { |
| 616 | UsedInLoop = true; |
| 617 | break; |
| 618 | } |
| 619 | } |
| 620 | // If there is, the def must remain in the preheader. |
| 621 | if (UsedInLoop) |
| 622 | continue; |
| 623 | // Otherwise, sink it to the exit block. |
| 624 | Instruction *ToMove = I; |
| 625 | bool Done = false; |
| 626 | if (I != Preheader->begin()) |
| 627 | --I; |
| 628 | else |
| 629 | Done = true; |
| 630 | ToMove->moveBefore(NonPHI); |
| 631 | if (Done) |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | |
| 636 | /// Re-schedule the inserted instructions to put defs before uses. This |
| 637 | /// fixes problems that arrise when SCEV expressions contain loop-variant |
| 638 | /// values unrelated to the induction variable which are defined inside the |
| 639 | /// loop. FIXME: It would be better to insert instructions in the right |
| 640 | /// place so that this step isn't needed. |
| 641 | void IndVarSimplify::FixUsesBeforeDefs(Loop *L, SCEVExpander &Rewriter) { |
| 642 | // Visit all the blocks in the loop in pre-order dom-tree dfs order. |
| 643 | DominatorTree *DT = &getAnalysis<DominatorTree>(); |
| 644 | std::map<Instruction *, unsigned> NumPredsLeft; |
| 645 | SmallVector<DomTreeNode *, 16> Worklist; |
| 646 | Worklist.push_back(DT->getNode(L->getHeader())); |
| 647 | do { |
| 648 | DomTreeNode *Node = Worklist.pop_back_val(); |
| 649 | for (DomTreeNode::iterator I = Node->begin(), E = Node->end(); I != E; ++I) |
| 650 | if (L->contains((*I)->getBlock())) |
| 651 | Worklist.push_back(*I); |
| 652 | BasicBlock *BB = Node->getBlock(); |
| 653 | // Visit all the instructions in the block top down. |
| 654 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 655 | // Count the number of operands that aren't properly dominating. |
| 656 | unsigned NumPreds = 0; |
| 657 | if (Rewriter.isInsertedInstruction(I) && !isa<PHINode>(I)) |
| 658 | for (User::op_iterator OI = I->op_begin(), OE = I->op_end(); |
| 659 | OI != OE; ++OI) |
| 660 | if (Instruction *Inst = dyn_cast<Instruction>(OI)) |
| 661 | if (L->contains(Inst->getParent()) && !NumPredsLeft.count(Inst)) |
| 662 | ++NumPreds; |
| 663 | NumPredsLeft[I] = NumPreds; |
| 664 | // Notify uses of the position of this instruction, and move the |
| 665 | // users (and their dependents, recursively) into place after this |
| 666 | // instruction if it is their last outstanding operand. |
| 667 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 668 | UI != UE; ++UI) { |
| 669 | Instruction *Inst = cast<Instruction>(UI); |
| 670 | std::map<Instruction *, unsigned>::iterator Z = NumPredsLeft.find(Inst); |
| 671 | if (Z != NumPredsLeft.end() && Z->second != 0 && --Z->second == 0) { |
| 672 | SmallVector<Instruction *, 4> UseWorkList; |
| 673 | UseWorkList.push_back(Inst); |
Dan Gohman | d6d0294 | 2009-05-22 16:47:11 +0000 | [diff] [blame] | 674 | BasicBlock::iterator InsertPt = I; |
| 675 | if (InvokeInst *II = dyn_cast<InvokeInst>(InsertPt)) |
| 676 | InsertPt = II->getNormalDest()->begin(); |
| 677 | else |
| 678 | ++InsertPt; |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 679 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 680 | do { |
| 681 | Instruction *Use = UseWorkList.pop_back_val(); |
| 682 | Use->moveBefore(InsertPt); |
| 683 | NumPredsLeft.erase(Use); |
| 684 | for (Value::use_iterator IUI = Use->use_begin(), |
| 685 | IUE = Use->use_end(); IUI != IUE; ++IUI) { |
| 686 | Instruction *IUIInst = cast<Instruction>(IUI); |
| 687 | if (L->contains(IUIInst->getParent()) && |
| 688 | Rewriter.isInsertedInstruction(IUIInst) && |
| 689 | !isa<PHINode>(IUIInst)) |
| 690 | UseWorkList.push_back(IUIInst); |
| 691 | } |
| 692 | } while (!UseWorkList.empty()); |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | } while (!Worklist.empty()); |
| 697 | } |
| 698 | |
Devang Patel | 13877bf | 2008-11-18 00:40:02 +0000 | [diff] [blame] | 699 | /// Return true if it is OK to use SIToFPInst for an inducation variable |
| 700 | /// with given inital and exit values. |
| 701 | static bool useSIToFPInst(ConstantFP &InitV, ConstantFP &ExitV, |
| 702 | uint64_t intIV, uint64_t intEV) { |
| 703 | |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 704 | if (InitV.getValueAPF().isNegative() || ExitV.getValueAPF().isNegative()) |
Devang Patel | 13877bf | 2008-11-18 00:40:02 +0000 | [diff] [blame] | 705 | return true; |
| 706 | |
| 707 | // If the iteration range can be handled by SIToFPInst then use it. |
| 708 | APInt Max = APInt::getSignedMaxValue(32); |
Dale Johannesen | bae7d6d | 2009-05-14 16:47:34 +0000 | [diff] [blame] | 709 | if (Max.getZExtValue() > static_cast<uint64_t>(abs64(intEV - intIV))) |
Devang Patel | 13877bf | 2008-11-18 00:40:02 +0000 | [diff] [blame] | 710 | return true; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 711 | |
Devang Patel | 13877bf | 2008-11-18 00:40:02 +0000 | [diff] [blame] | 712 | return false; |
| 713 | } |
| 714 | |
| 715 | /// convertToInt - Convert APF to an integer, if possible. |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 716 | static bool convertToInt(const APFloat &APF, uint64_t *intVal) { |
| 717 | |
| 718 | bool isExact = false; |
Evan Cheng | 794a7db | 2008-11-26 01:11:57 +0000 | [diff] [blame] | 719 | if (&APF.getSemantics() == &APFloat::PPCDoubleDouble) |
| 720 | return false; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 721 | if (APF.convertToInteger(intVal, 32, APF.isNegative(), |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 722 | APFloat::rmTowardZero, &isExact) |
| 723 | != APFloat::opOK) |
| 724 | return false; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 725 | if (!isExact) |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 726 | return false; |
| 727 | return true; |
| 728 | |
| 729 | } |
| 730 | |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 731 | /// HandleFloatingPointIV - If the loop has floating induction variable |
| 732 | /// then insert corresponding integer induction variable if possible. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 733 | /// For example, |
| 734 | /// for(double i = 0; i < 10000; ++i) |
| 735 | /// bar(i) |
| 736 | /// is converted into |
| 737 | /// for(int i = 0; i < 10000; ++i) |
| 738 | /// bar((double)i); |
| 739 | /// |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 740 | void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH) { |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 741 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 742 | unsigned IncomingEdge = L->contains(PH->getIncomingBlock(0)); |
| 743 | unsigned BackEdge = IncomingEdge^1; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 744 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 745 | // Check incoming value. |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 746 | ConstantFP *InitValue = dyn_cast<ConstantFP>(PH->getIncomingValue(IncomingEdge)); |
| 747 | if (!InitValue) return; |
| 748 | uint64_t newInitValue = Type::Int32Ty->getPrimitiveSizeInBits(); |
| 749 | if (!convertToInt(InitValue->getValueAPF(), &newInitValue)) |
| 750 | return; |
| 751 | |
| 752 | // Check IV increment. Reject this PH if increement operation is not |
| 753 | // an add or increment value can not be represented by an integer. |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 754 | BinaryOperator *Incr = |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 755 | dyn_cast<BinaryOperator>(PH->getIncomingValue(BackEdge)); |
| 756 | if (!Incr) return; |
Dan Gohman | ae3a0be | 2009-06-04 22:49:04 +0000 | [diff] [blame^] | 757 | if (Incr->getOpcode() != Instruction::FAdd) return; |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 758 | ConstantFP *IncrValue = NULL; |
| 759 | unsigned IncrVIndex = 1; |
| 760 | if (Incr->getOperand(1) == PH) |
| 761 | IncrVIndex = 0; |
| 762 | IncrValue = dyn_cast<ConstantFP>(Incr->getOperand(IncrVIndex)); |
| 763 | if (!IncrValue) return; |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 764 | uint64_t newIncrValue = Type::Int32Ty->getPrimitiveSizeInBits(); |
| 765 | if (!convertToInt(IncrValue->getValueAPF(), &newIncrValue)) |
| 766 | return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 767 | |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 768 | // Check Incr uses. One user is PH and the other users is exit condition used |
| 769 | // by the conditional terminator. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 770 | Value::use_iterator IncrUse = Incr->use_begin(); |
| 771 | Instruction *U1 = cast<Instruction>(IncrUse++); |
| 772 | if (IncrUse == Incr->use_end()) return; |
| 773 | Instruction *U2 = cast<Instruction>(IncrUse++); |
| 774 | if (IncrUse != Incr->use_end()) return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 775 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 776 | // Find exit condition. |
| 777 | FCmpInst *EC = dyn_cast<FCmpInst>(U1); |
| 778 | if (!EC) |
| 779 | EC = dyn_cast<FCmpInst>(U2); |
| 780 | if (!EC) return; |
| 781 | |
| 782 | if (BranchInst *BI = dyn_cast<BranchInst>(EC->getParent()->getTerminator())) { |
| 783 | if (!BI->isConditional()) return; |
| 784 | if (BI->getCondition() != EC) return; |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 785 | } |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 786 | |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 787 | // Find exit value. If exit value can not be represented as an interger then |
| 788 | // do not handle this floating point PH. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 789 | ConstantFP *EV = NULL; |
| 790 | unsigned EVIndex = 1; |
| 791 | if (EC->getOperand(1) == Incr) |
| 792 | EVIndex = 0; |
| 793 | EV = dyn_cast<ConstantFP>(EC->getOperand(EVIndex)); |
| 794 | if (!EV) return; |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 795 | uint64_t intEV = Type::Int32Ty->getPrimitiveSizeInBits(); |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 796 | if (!convertToInt(EV->getValueAPF(), &intEV)) |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 797 | return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 798 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 799 | // Find new predicate for integer comparison. |
| 800 | CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE; |
| 801 | switch (EC->getPredicate()) { |
| 802 | case CmpInst::FCMP_OEQ: |
| 803 | case CmpInst::FCMP_UEQ: |
| 804 | NewPred = CmpInst::ICMP_EQ; |
| 805 | break; |
| 806 | case CmpInst::FCMP_OGT: |
| 807 | case CmpInst::FCMP_UGT: |
| 808 | NewPred = CmpInst::ICMP_UGT; |
| 809 | break; |
| 810 | case CmpInst::FCMP_OGE: |
| 811 | case CmpInst::FCMP_UGE: |
| 812 | NewPred = CmpInst::ICMP_UGE; |
| 813 | break; |
| 814 | case CmpInst::FCMP_OLT: |
| 815 | case CmpInst::FCMP_ULT: |
| 816 | NewPred = CmpInst::ICMP_ULT; |
| 817 | break; |
| 818 | case CmpInst::FCMP_OLE: |
| 819 | case CmpInst::FCMP_ULE: |
| 820 | NewPred = CmpInst::ICMP_ULE; |
| 821 | break; |
| 822 | default: |
| 823 | break; |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 824 | } |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 825 | if (NewPred == CmpInst::BAD_ICMP_PREDICATE) return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 826 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 827 | // Insert new integer induction variable. |
| 828 | PHINode *NewPHI = PHINode::Create(Type::Int32Ty, |
| 829 | PH->getName()+".int", PH); |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 830 | NewPHI->addIncoming(ConstantInt::get(Type::Int32Ty, newInitValue), |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 831 | PH->getIncomingBlock(IncomingEdge)); |
| 832 | |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 833 | Value *NewAdd = BinaryOperator::CreateAdd(NewPHI, |
| 834 | ConstantInt::get(Type::Int32Ty, |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 835 | newIncrValue), |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 836 | Incr->getName()+".int", Incr); |
| 837 | NewPHI->addIncoming(NewAdd, PH->getIncomingBlock(BackEdge)); |
| 838 | |
Dale Johannesen | 617d108 | 2009-04-27 21:03:15 +0000 | [diff] [blame] | 839 | // The back edge is edge 1 of newPHI, whatever it may have been in the |
| 840 | // original PHI. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 841 | ConstantInt *NewEV = ConstantInt::get(Type::Int32Ty, intEV); |
Dale Johannesen | 617d108 | 2009-04-27 21:03:15 +0000 | [diff] [blame] | 842 | Value *LHS = (EVIndex == 1 ? NewPHI->getIncomingValue(1) : NewEV); |
| 843 | Value *RHS = (EVIndex == 1 ? NewEV : NewPHI->getIncomingValue(1)); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 844 | ICmpInst *NewEC = new ICmpInst(NewPred, LHS, RHS, EC->getNameStart(), |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 845 | EC->getParent()->getTerminator()); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 846 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 847 | // In the following deltions, PH may become dead and may be deleted. |
| 848 | // Use a WeakVH to observe whether this happens. |
| 849 | WeakVH WeakPH = PH; |
| 850 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 851 | // Delete old, floating point, exit comparision instruction. |
Dan Gohman | 14fba29 | 2009-05-24 18:09:01 +0000 | [diff] [blame] | 852 | NewEC->takeName(EC); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 853 | EC->replaceAllUsesWith(NewEC); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 854 | RecursivelyDeleteTriviallyDeadInstructions(EC); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 855 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 856 | // Delete old, floating point, increment instruction. |
| 857 | Incr->replaceAllUsesWith(UndefValue::get(Incr->getType())); |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 858 | RecursivelyDeleteTriviallyDeadInstructions(Incr); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 859 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 860 | // Replace floating induction variable, if it isn't already deleted. |
| 861 | // Give SIToFPInst preference over UIToFPInst because it is faster on |
| 862 | // platforms that are widely used. |
| 863 | if (WeakPH && !PH->use_empty()) { |
| 864 | if (useSIToFPInst(*InitValue, *EV, newInitValue, intEV)) { |
| 865 | SIToFPInst *Conv = new SIToFPInst(NewPHI, PH->getType(), "indvar.conv", |
| 866 | PH->getParent()->getFirstNonPHI()); |
| 867 | PH->replaceAllUsesWith(Conv); |
| 868 | } else { |
| 869 | UIToFPInst *Conv = new UIToFPInst(NewPHI, PH->getType(), "indvar.conv", |
| 870 | PH->getParent()->getFirstNonPHI()); |
| 871 | PH->replaceAllUsesWith(Conv); |
| 872 | } |
| 873 | RecursivelyDeleteTriviallyDeadInstructions(PH); |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 874 | } |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 875 | |
Dan Gohman | 81db61a | 2009-05-12 02:17:14 +0000 | [diff] [blame] | 876 | // Add a new IVUsers entry for the newly-created integer PHI. |
| 877 | IU->AddUsersIfInteresting(NewPHI); |
| 878 | } |