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 |
| 34 | // desired loop transformations have been performed. Additionally, on targets |
| 35 | // where it is profitable, the loop could be transformed to count down to zero |
| 36 | // (the "do loop" optimization). |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 37 | // |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 40 | #define DEBUG_TYPE "indvars" |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 41 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 42 | #include "llvm/BasicBlock.h" |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 43 | #include "llvm/Constants.h" |
Chris Lattner | 18b3c97 | 2003-12-22 05:02:01 +0000 | [diff] [blame] | 44 | #include "llvm/Instructions.h" |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 45 | #include "llvm/Type.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" |
Chris Lattner | a4b9c78 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 52 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 53 | #include "llvm/Target/TargetData.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 54 | #include "llvm/Transforms/Utils/Local.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 55 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/SmallVector.h" |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/SetVector.h" |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 59 | #include "llvm/ADT/Statistic.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 60 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 62 | STATISTIC(NumRemoved , "Number of aux indvars removed"); |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 63 | STATISTIC(NumInserted, "Number of canonical indvars added"); |
| 64 | STATISTIC(NumReplaced, "Number of exit values replaced"); |
| 65 | STATISTIC(NumLFTR , "Number of loop exit tests replaced"); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 66 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 67 | namespace { |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 68 | class VISIBILITY_HIDDEN IndVarSimplify : public LoopPass { |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 69 | LoopInfo *LI; |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 70 | TargetData *TD; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 71 | ScalarEvolution *SE; |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 72 | bool Changed; |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 73 | public: |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 74 | |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 75 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 76 | IndVarSimplify() : LoopPass(&ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 77 | |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 78 | virtual bool runOnLoop(Loop *L, LPPassManager &LPM); |
| 79 | |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 80 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Devang Patel | bc533cd | 2007-09-10 18:08:23 +0000 | [diff] [blame] | 81 | AU.addRequired<ScalarEvolution>(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 82 | AU.addRequiredID(LCSSAID); |
| 83 | AU.addRequiredID(LoopSimplifyID); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 84 | AU.addRequired<LoopInfo>(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 85 | AU.addRequired<TargetData>(); |
Dan Gohman | 474cecf | 2009-02-23 16:29:41 +0000 | [diff] [blame] | 86 | AU.addPreserved<ScalarEvolution>(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 87 | AU.addPreservedID(LoopSimplifyID); |
| 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 | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 96 | void 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 | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 101 | void RewriteLoopExitValues(Loop *L, SCEV *BackedgeTakenCount); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 102 | |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 103 | void DeleteTriviallyDeadInstructions(SmallPtrSet<Instruction*, 16> &Insts); |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 104 | |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 105 | void HandleFloatingPointIV(Loop *L, PHINode *PH, |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 106 | SmallPtrSet<Instruction*, 16> &DeadInsts); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 107 | }; |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 108 | } |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 109 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 110 | char IndVarSimplify::ID = 0; |
| 111 | static RegisterPass<IndVarSimplify> |
| 112 | X("indvars", "Canonicalize Induction Variables"); |
| 113 | |
Daniel Dunbar | 394f044 | 2008-10-22 23:32:42 +0000 | [diff] [blame] | 114 | Pass *llvm::createIndVarSimplifyPass() { |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 115 | return new IndVarSimplify(); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 118 | /// DeleteTriviallyDeadInstructions - If any of the instructions is the |
| 119 | /// specified set are trivially dead, delete them and see if this makes any of |
| 120 | /// their operands subsequently dead. |
| 121 | void IndVarSimplify:: |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 122 | DeleteTriviallyDeadInstructions(SmallPtrSet<Instruction*, 16> &Insts) { |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 123 | while (!Insts.empty()) { |
| 124 | Instruction *I = *Insts.begin(); |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 125 | Insts.erase(I); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 126 | if (isInstructionTriviallyDead(I)) { |
| 127 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 128 | if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i))) |
| 129 | Insts.insert(U); |
Dan Gohman | 5cec4db | 2007-06-19 14:28:31 +0000 | [diff] [blame] | 130 | SE->deleteValueFromRecords(I); |
Chris Lattner | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 131 | DOUT << "INDVARS: Deleting: " << *I; |
Chris Lattner | a4b9c78 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 132 | I->eraseFromParent(); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 133 | Changed = true; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 138 | /// LinearFunctionTestReplace - This method rewrites the exit condition of the |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 139 | /// loop to be a canonical != comparison against the incremented loop induction |
| 140 | /// variable. This pass is able to rewrite the exit tests of any loop where the |
| 141 | /// SCEV analysis can determine a loop-invariant trip count of the loop, which |
| 142 | /// is actually a much broader range than just linear tests. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 143 | void IndVarSimplify::LinearFunctionTestReplace(Loop *L, |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 144 | SCEVHandle BackedgeTakenCount, |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 145 | Value *IndVar, |
| 146 | BasicBlock *ExitingBlock, |
| 147 | BranchInst *BI, |
Dan Gohman | 15cab28 | 2009-02-23 23:20:35 +0000 | [diff] [blame] | 148 | SCEVExpander &Rewriter) { |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 149 | // If the exiting block is not the same as the backedge block, we must compare |
| 150 | // against the preincremented value, otherwise we prefer to compare against |
| 151 | // the post-incremented value. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 152 | Value *CmpIndVar; |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 153 | SCEVHandle RHS = BackedgeTakenCount; |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 154 | if (ExitingBlock == L->getLoopLatch()) { |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 155 | // Add one to the "backedge-taken" count to get the trip count. |
| 156 | // If this addition may overflow, we have to be more pessimistic and |
| 157 | // cast the induction variable before doing the add. |
| 158 | SCEVHandle Zero = SE->getIntegerSCEV(0, BackedgeTakenCount->getType()); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 159 | SCEVHandle N = |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 160 | SE->getAddExpr(BackedgeTakenCount, |
| 161 | SE->getIntegerSCEV(1, BackedgeTakenCount->getType())); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 162 | if ((isa<SCEVConstant>(N) && !N->isZero()) || |
| 163 | SE->isLoopGuardedByCond(L, ICmpInst::ICMP_NE, N, Zero)) { |
| 164 | // No overflow. Cast the sum. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 165 | RHS = SE->getTruncateOrZeroExtend(N, IndVar->getType()); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 166 | } else { |
| 167 | // Potential overflow. Cast before doing the add. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 168 | RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount, |
| 169 | IndVar->getType()); |
| 170 | RHS = SE->getAddExpr(RHS, |
| 171 | SE->getIntegerSCEV(1, IndVar->getType())); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 172 | } |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 173 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 174 | // The BackedgeTaken expression contains the number of times that the |
| 175 | // backedge branches to the loop header. This is one less than the |
| 176 | // number of times the loop executes, so use the incremented indvar. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 177 | CmpIndVar = L->getCanonicalInductionVariableIncrement(); |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 178 | } else { |
| 179 | // We have to use the preincremented value... |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 180 | RHS = SE->getTruncateOrZeroExtend(BackedgeTakenCount, |
| 181 | IndVar->getType()); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 182 | CmpIndVar = IndVar; |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 183 | } |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 184 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 185 | // Expand the code for the iteration count into the preheader of the loop. |
| 186 | BasicBlock *Preheader = L->getLoopPreheader(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 187 | Value *ExitCnt = Rewriter.expandCodeFor(RHS, IndVar->getType(), |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 188 | Preheader->getTerminator()); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 189 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 190 | // Insert a new icmp_ne or icmp_eq instruction before the branch. |
| 191 | ICmpInst::Predicate Opcode; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 192 | if (L->contains(BI->getSuccessor(0))) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 193 | Opcode = ICmpInst::ICMP_NE; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 194 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 195 | Opcode = ICmpInst::ICMP_EQ; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 196 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 197 | DOUT << "INDVARS: Rewriting loop exit condition to:\n" |
| 198 | << " LHS:" << *CmpIndVar // includes a newline |
| 199 | << " op:\t" |
Dan Gohman | f108e2e | 2009-02-14 02:26:50 +0000 | [diff] [blame] | 200 | << (Opcode == ICmpInst::ICMP_NE ? "!=" : "==") << "\n" |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 201 | << " RHS:\t" << *RHS << "\n"; |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 202 | |
| 203 | Value *Cond = new ICmpInst(Opcode, CmpIndVar, ExitCnt, "exitcond", BI); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 204 | BI->setCondition(Cond); |
| 205 | ++NumLFTR; |
| 206 | Changed = true; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 209 | /// RewriteLoopExitValues - Check to see if this loop has a computable |
| 210 | /// loop-invariant execution count. If so, this means that we can compute the |
| 211 | /// final value of any expressions that are recurrent in the loop, and |
| 212 | /// substitute the exit values from the loop into any instructions outside of |
| 213 | /// the loop that use the final values of the current expressions. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 214 | void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEV *BackedgeTakenCount) { |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 215 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 216 | |
| 217 | // Scan all of the instructions in the loop, looking at those that have |
| 218 | // extra-loop users and which are recurrences. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 219 | SCEVExpander Rewriter(*SE, *LI, *TD); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 220 | |
| 221 | // We insert the code into the preheader of the loop if the loop contains |
| 222 | // multiple exit blocks, or in the exit block if there is exactly one. |
| 223 | BasicBlock *BlockToInsertInto; |
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); |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 226 | if (ExitBlocks.size() == 1) |
| 227 | BlockToInsertInto = ExitBlocks[0]; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 228 | else |
| 229 | BlockToInsertInto = Preheader; |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 230 | BasicBlock::iterator InsertPt = BlockToInsertInto->getFirstNonPHI(); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 231 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 232 | bool HasConstantItCount = isa<SCEVConstant>(BackedgeTakenCount); |
Chris Lattner | 20aa098 | 2004-04-17 18:44:09 +0000 | [diff] [blame] | 233 | |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 234 | SmallPtrSet<Instruction*, 16> InstructionsToDelete; |
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++))) { |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 254 | // Iterate over all of the values in all the PHI nodes. |
| 255 | for (unsigned i = 0; i != NumPreds; ++i) { |
| 256 | // If the value being merged in is not integer or is not defined |
| 257 | // in the loop, skip it. |
| 258 | Value *InVal = PN->getIncomingValue(i); |
| 259 | if (!isa<Instruction>(InVal) || |
| 260 | // SCEV only supports integer expressions for now. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 261 | (!isa<IntegerType>(InVal->getType()) && |
| 262 | !isa<PointerType>(InVal->getType()))) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 263 | continue; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 264 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 265 | // If this pred is for a subloop, not L itself, skip it. |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 266 | if (LI->getLoopFor(PN->getIncomingBlock(i)) != L) |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 267 | continue; // The Block is in a subloop, skip it. |
| 268 | |
| 269 | // Check that InVal is defined in the loop. |
| 270 | Instruction *Inst = cast<Instruction>(InVal); |
| 271 | if (!L->contains(Inst->getParent())) |
| 272 | continue; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 273 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 274 | // We require that this value either have a computable evolution or that |
| 275 | // the loop have a constant iteration count. In the case where the loop |
| 276 | // has a constant iteration count, we can sometimes force evaluation of |
| 277 | // the exit value through brute force. |
| 278 | SCEVHandle SH = SE->getSCEV(Inst); |
| 279 | if (!SH->hasComputableLoopEvolution(L) && !HasConstantItCount) |
| 280 | continue; // Cannot get exit evolution for the loop value. |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 281 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 282 | // Okay, this instruction has a user outside of the current loop |
| 283 | // and varies predictably *inside* the loop. Evaluate the value it |
| 284 | // contains when the loop exits, if possible. |
| 285 | SCEVHandle ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop()); |
| 286 | if (isa<SCEVCouldNotCompute>(ExitValue) || |
| 287 | !ExitValue->isLoopInvariant(L)) |
| 288 | continue; |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 289 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 290 | Changed = true; |
| 291 | ++NumReplaced; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 292 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 293 | // See if we already computed the exit value for the instruction, if so, |
| 294 | // just reuse it. |
| 295 | Value *&ExitVal = ExitValues[Inst]; |
| 296 | if (!ExitVal) |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 297 | ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), InsertPt); |
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 | DOUT << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal |
| 300 | << " LoopVal = " << *Inst << "\n"; |
| 301 | |
| 302 | PN->setIncomingValue(i, ExitVal); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 303 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 304 | // If this instruction is dead now, schedule it to be removed. |
| 305 | if (Inst->use_empty()) |
| 306 | InstructionsToDelete.insert(Inst); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 307 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 308 | // See if this is a single-entry LCSSA PHI node. If so, we can (and |
| 309 | // have to) remove |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 310 | // the PHI entirely. This is safe, because the NewVal won't be variant |
| 311 | // 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] | 312 | if (NumPreds == 1) { |
Dan Gohman | 5cec4db | 2007-06-19 14:28:31 +0000 | [diff] [blame] | 313 | SE->deleteValueFromRecords(PN); |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 314 | PN->replaceAllUsesWith(ExitVal); |
| 315 | PN->eraseFromParent(); |
| 316 | break; |
Chris Lattner | c9838f2 | 2007-03-03 22:48:48 +0000 | [diff] [blame] | 317 | } |
| 318 | } |
Chris Lattner | c9838f2 | 2007-03-03 22:48:48 +0000 | [diff] [blame] | 319 | } |
| 320 | } |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 322 | DeleteTriviallyDeadInstructions(InstructionsToDelete); |
| 323 | } |
| 324 | |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 325 | void IndVarSimplify::RewriteNonIntegerIVs(Loop *L) { |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 326 | // First step. Check to see if there are any floating-point recurrences. |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 327 | // If there are, change them into integer recurrences, permitting analysis by |
| 328 | // the SCEV routines. |
| 329 | // |
| 330 | BasicBlock *Header = L->getHeader(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 332 | SmallPtrSet<Instruction*, 16> DeadInsts; |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 333 | for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { |
| 334 | PHINode *PN = cast<PHINode>(I); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 335 | HandleFloatingPointIV(L, PN, DeadInsts); |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 336 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 337 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 338 | // If the loop previously had floating-point IV, ScalarEvolution |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 339 | // may not have been able to compute a trip count. Now that we've done some |
| 340 | // re-writing, the trip count may be computable. |
| 341 | if (Changed) |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 342 | SE->forgetLoopBackedgeTakenCount(L); |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 343 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 344 | if (!DeadInsts.empty()) |
| 345 | DeleteTriviallyDeadInstructions(DeadInsts); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 346 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 347 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 348 | /// getEffectiveIndvarType - Determine the widest type that the |
| 349 | /// induction-variable PHINode Phi is cast to. |
| 350 | /// |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 351 | static const Type *getEffectiveIndvarType(const PHINode *Phi, |
| 352 | const TargetData *TD) { |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 353 | const Type *Ty = Phi->getType(); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 354 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 355 | for (Value::use_const_iterator UI = Phi->use_begin(), UE = Phi->use_end(); |
| 356 | UI != UE; ++UI) { |
| 357 | const Type *CandidateType = NULL; |
| 358 | if (const ZExtInst *ZI = dyn_cast<ZExtInst>(UI)) |
| 359 | CandidateType = ZI->getDestTy(); |
| 360 | else if (const SExtInst *SI = dyn_cast<SExtInst>(UI)) |
| 361 | CandidateType = SI->getDestTy(); |
| 362 | if (CandidateType && |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 363 | TD->getTypeSizeInBits(CandidateType) > TD->getTypeSizeInBits(Ty)) |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 364 | Ty = CandidateType; |
| 365 | } |
| 366 | |
| 367 | return Ty; |
| 368 | } |
| 369 | |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 370 | /// TestOrigIVForWrap - Analyze the original induction variable |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 371 | /// that controls the loop's iteration to determine whether it |
Dan Gohman | f5a309e | 2009-02-18 17:22:41 +0000 | [diff] [blame] | 372 | /// would ever undergo signed or unsigned overflow. Also, check |
| 373 | /// whether an induction variable in the same type that starts |
| 374 | /// at 0 would undergo signed overflow. |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 375 | /// |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 376 | /// In addition to setting the NoSignedWrap and NoUnsignedWrap |
| 377 | /// variables to true when appropriate (they are not set to false here), |
| 378 | /// return the PHI for this induction variable. Also record the initial |
| 379 | /// and final values and the increment; these are not meaningful unless |
| 380 | /// either NoSignedWrap or NoUnsignedWrap is true, and are always meaningful |
| 381 | /// in that case, although the final value may be 0 indicating a nonconstant. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 382 | /// |
| 383 | /// TODO: This duplicates a fair amount of ScalarEvolution logic. |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 384 | /// Perhaps this can be merged with |
| 385 | /// ScalarEvolution::getBackedgeTakenCount |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 386 | /// and/or ScalarEvolution::get{Sign,Zero}ExtendExpr. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 387 | /// |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 388 | static const PHINode *TestOrigIVForWrap(const Loop *L, |
| 389 | const BranchInst *BI, |
| 390 | const Instruction *OrigCond, |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 391 | const TargetData *TD, |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 392 | bool &NoSignedWrap, |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 393 | bool &NoUnsignedWrap, |
| 394 | const ConstantInt* &InitialVal, |
| 395 | const ConstantInt* &IncrVal, |
| 396 | const ConstantInt* &LimitVal) { |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 397 | // Verify that the loop is sane and find the exit condition. |
| 398 | const ICmpInst *Cmp = dyn_cast<ICmpInst>(OrigCond); |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 399 | if (!Cmp) return 0; |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 400 | |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 401 | const Value *CmpLHS = Cmp->getOperand(0); |
| 402 | const Value *CmpRHS = Cmp->getOperand(1); |
| 403 | const BasicBlock *TrueBB = BI->getSuccessor(0); |
| 404 | const BasicBlock *FalseBB = BI->getSuccessor(1); |
| 405 | ICmpInst::Predicate Pred = Cmp->getPredicate(); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 406 | |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 407 | // Canonicalize a constant to the RHS. |
| 408 | if (isa<ConstantInt>(CmpLHS)) { |
| 409 | Pred = ICmpInst::getSwappedPredicate(Pred); |
| 410 | std::swap(CmpLHS, CmpRHS); |
| 411 | } |
| 412 | // Canonicalize SLE to SLT. |
| 413 | if (Pred == ICmpInst::ICMP_SLE) |
| 414 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) |
| 415 | if (!CI->getValue().isMaxSignedValue()) { |
| 416 | CmpRHS = ConstantInt::get(CI->getValue() + 1); |
| 417 | Pred = ICmpInst::ICMP_SLT; |
| 418 | } |
| 419 | // Canonicalize SGT to SGE. |
| 420 | if (Pred == ICmpInst::ICMP_SGT) |
| 421 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) |
| 422 | if (!CI->getValue().isMaxSignedValue()) { |
| 423 | CmpRHS = ConstantInt::get(CI->getValue() + 1); |
| 424 | Pred = ICmpInst::ICMP_SGE; |
| 425 | } |
| 426 | // Canonicalize SGE to SLT. |
| 427 | if (Pred == ICmpInst::ICMP_SGE) { |
| 428 | std::swap(TrueBB, FalseBB); |
| 429 | Pred = ICmpInst::ICMP_SLT; |
| 430 | } |
| 431 | // Canonicalize ULE to ULT. |
| 432 | if (Pred == ICmpInst::ICMP_ULE) |
| 433 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) |
| 434 | if (!CI->getValue().isMaxValue()) { |
| 435 | CmpRHS = ConstantInt::get(CI->getValue() + 1); |
| 436 | Pred = ICmpInst::ICMP_ULT; |
| 437 | } |
| 438 | // Canonicalize UGT to UGE. |
| 439 | if (Pred == ICmpInst::ICMP_UGT) |
| 440 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(CmpRHS)) |
| 441 | if (!CI->getValue().isMaxValue()) { |
| 442 | CmpRHS = ConstantInt::get(CI->getValue() + 1); |
| 443 | Pred = ICmpInst::ICMP_UGE; |
| 444 | } |
| 445 | // Canonicalize UGE to ULT. |
| 446 | if (Pred == ICmpInst::ICMP_UGE) { |
| 447 | std::swap(TrueBB, FalseBB); |
| 448 | Pred = ICmpInst::ICMP_ULT; |
| 449 | } |
| 450 | // For now, analyze only LT loops for signed overflow. |
| 451 | if (Pred != ICmpInst::ICMP_SLT && Pred != ICmpInst::ICMP_ULT) |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 452 | return 0; |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 453 | |
| 454 | bool isSigned = Pred == ICmpInst::ICMP_SLT; |
| 455 | |
| 456 | // Get the increment instruction. Look past casts if we will |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 457 | // be able to prove that the original induction variable doesn't |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 458 | // undergo signed or unsigned overflow, respectively. |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 459 | const Value *IncrInst = CmpLHS; |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 460 | if (isSigned) { |
| 461 | if (const SExtInst *SI = dyn_cast<SExtInst>(CmpLHS)) { |
| 462 | if (!isa<ConstantInt>(CmpRHS) || |
| 463 | !cast<ConstantInt>(CmpRHS)->getValue() |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 464 | .isSignedIntN(TD->getTypeSizeInBits(IncrInst->getType()))) |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 465 | return 0; |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 466 | IncrInst = SI->getOperand(0); |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 467 | } |
| 468 | } else { |
| 469 | if (const ZExtInst *ZI = dyn_cast<ZExtInst>(CmpLHS)) { |
| 470 | if (!isa<ConstantInt>(CmpRHS) || |
| 471 | !cast<ConstantInt>(CmpRHS)->getValue() |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 472 | .isIntN(TD->getTypeSizeInBits(IncrInst->getType()))) |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 473 | return 0; |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 474 | IncrInst = ZI->getOperand(0); |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 475 | } |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | // For now, only analyze induction variables that have simple increments. |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 479 | const BinaryOperator *IncrOp = dyn_cast<BinaryOperator>(IncrInst); |
| 480 | if (!IncrOp || IncrOp->getOpcode() != Instruction::Add) |
| 481 | return 0; |
| 482 | IncrVal = dyn_cast<ConstantInt>(IncrOp->getOperand(1)); |
| 483 | if (!IncrVal) |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 484 | return 0; |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 485 | |
| 486 | // Make sure the PHI looks like a normal IV. |
| 487 | const PHINode *PN = dyn_cast<PHINode>(IncrOp->getOperand(0)); |
| 488 | if (!PN || PN->getNumIncomingValues() != 2) |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 489 | return 0; |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 490 | unsigned IncomingEdge = L->contains(PN->getIncomingBlock(0)); |
| 491 | unsigned BackEdge = !IncomingEdge; |
| 492 | if (!L->contains(PN->getIncomingBlock(BackEdge)) || |
| 493 | PN->getIncomingValue(BackEdge) != IncrOp) |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 494 | return 0; |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 495 | if (!L->contains(TrueBB)) |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 496 | return 0; |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 497 | |
| 498 | // For now, only analyze loops with a constant start value, so that |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 499 | // we can easily determine if the start value is not a maximum value |
| 500 | // which would wrap on the first iteration. |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 501 | InitialVal = dyn_cast<ConstantInt>(PN->getIncomingValue(IncomingEdge)); |
Dan Gohman | cad24c9 | 2009-02-18 16:54:33 +0000 | [diff] [blame] | 502 | if (!InitialVal) |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 503 | return 0; |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 504 | |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 505 | // The upper limit need not be a constant; we'll check later. |
| 506 | LimitVal = dyn_cast<ConstantInt>(CmpRHS); |
| 507 | |
| 508 | // We detect the impossibility of wrapping in two cases, both of |
| 509 | // which require starting with a non-max value: |
| 510 | // - The IV counts up by one, and the loop iterates only while it remains |
| 511 | // less than a limiting value (any) in the same type. |
| 512 | // - The IV counts up by a positive increment other than 1, and the |
| 513 | // constant limiting value + the increment is less than the max value |
| 514 | // (computed as max-increment to avoid overflow) |
Dan Gohman | f5a309e | 2009-02-18 17:22:41 +0000 | [diff] [blame] | 515 | if (isSigned && !InitialVal->getValue().isMaxSignedValue()) { |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 516 | if (IncrVal->equalsInt(1)) |
| 517 | NoSignedWrap = true; // LimitVal need not be constant |
| 518 | else if (LimitVal) { |
| 519 | uint64_t numBits = LimitVal->getValue().getBitWidth(); |
| 520 | if (IncrVal->getValue().sgt(APInt::getNullValue(numBits)) && |
| 521 | (APInt::getSignedMaxValue(numBits) - IncrVal->getValue()) |
| 522 | .sgt(LimitVal->getValue())) |
| 523 | NoSignedWrap = true; |
| 524 | } |
| 525 | } else if (!isSigned && !InitialVal->getValue().isMaxValue()) { |
| 526 | if (IncrVal->equalsInt(1)) |
| 527 | NoUnsignedWrap = true; // LimitVal need not be constant |
| 528 | else if (LimitVal) { |
| 529 | uint64_t numBits = LimitVal->getValue().getBitWidth(); |
| 530 | if (IncrVal->getValue().ugt(APInt::getNullValue(numBits)) && |
| 531 | (APInt::getMaxValue(numBits) - IncrVal->getValue()) |
| 532 | .ugt(LimitVal->getValue())) |
| 533 | NoUnsignedWrap = true; |
| 534 | } |
| 535 | } |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 536 | return PN; |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 539 | static Value *getSignExtendedTruncVar(const SCEVAddRecExpr *AR, |
| 540 | ScalarEvolution *SE, |
| 541 | const Type *LargestType, Loop *L, |
| 542 | const Type *myType, |
| 543 | SCEVExpander &Rewriter, |
| 544 | BasicBlock::iterator InsertPt) { |
| 545 | SCEVHandle ExtendedStart = |
| 546 | SE->getSignExtendExpr(AR->getStart(), LargestType); |
| 547 | SCEVHandle ExtendedStep = |
| 548 | SE->getSignExtendExpr(AR->getStepRecurrence(*SE), LargestType); |
| 549 | SCEVHandle ExtendedAddRec = |
| 550 | SE->getAddRecExpr(ExtendedStart, ExtendedStep, L); |
| 551 | if (LargestType != myType) |
| 552 | ExtendedAddRec = SE->getTruncateExpr(ExtendedAddRec, myType); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 553 | return Rewriter.expandCodeFor(ExtendedAddRec, myType, InsertPt); |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | static Value *getZeroExtendedTruncVar(const SCEVAddRecExpr *AR, |
| 557 | ScalarEvolution *SE, |
| 558 | const Type *LargestType, Loop *L, |
| 559 | const Type *myType, |
| 560 | SCEVExpander &Rewriter, |
| 561 | BasicBlock::iterator InsertPt) { |
| 562 | SCEVHandle ExtendedStart = |
| 563 | SE->getZeroExtendExpr(AR->getStart(), LargestType); |
| 564 | SCEVHandle ExtendedStep = |
| 565 | SE->getZeroExtendExpr(AR->getStepRecurrence(*SE), LargestType); |
| 566 | SCEVHandle ExtendedAddRec = |
| 567 | SE->getAddRecExpr(ExtendedStart, ExtendedStep, L); |
| 568 | if (LargestType != myType) |
| 569 | ExtendedAddRec = SE->getTruncateExpr(ExtendedAddRec, myType); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 570 | return Rewriter.expandCodeFor(ExtendedAddRec, myType, InsertPt); |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 573 | /// allUsesAreSameTyped - See whether all Uses of I are instructions |
| 574 | /// with the same Opcode and the same type. |
| 575 | static bool allUsesAreSameTyped(unsigned int Opcode, Instruction *I) { |
| 576 | const Type* firstType = NULL; |
| 577 | for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); |
| 578 | UI != UE; ++UI) { |
| 579 | Instruction *II = dyn_cast<Instruction>(*UI); |
| 580 | if (!II || II->getOpcode() != Opcode) |
| 581 | return false; |
| 582 | if (!firstType) |
| 583 | firstType = II->getType(); |
| 584 | else if (firstType != II->getType()) |
| 585 | return false; |
| 586 | } |
| 587 | return true; |
| 588 | } |
| 589 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 590 | bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) { |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 591 | LI = &getAnalysis<LoopInfo>(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 592 | TD = &getAnalysis<TargetData>(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 593 | SE = &getAnalysis<ScalarEvolution>(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 594 | Changed = false; |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 595 | |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 596 | // If there are any floating-point recurrences, attempt to |
Dan Gohman | 60f8a63 | 2009-02-17 20:49:49 +0000 | [diff] [blame] | 597 | // transform them to use integer recurrences. |
| 598 | RewriteNonIntegerIVs(L); |
| 599 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 600 | BasicBlock *Header = L->getHeader(); |
| 601 | BasicBlock *ExitingBlock = L->getExitingBlock(); |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 602 | SmallPtrSet<Instruction*, 16> DeadInsts; |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 603 | |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 604 | // Verify the input to the pass in already in LCSSA form. |
| 605 | assert(L->isLCSSAForm()); |
| 606 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 607 | // Check to see if this loop has a computable loop-invariant execution count. |
| 608 | // If so, this means that we can compute the final value of any expressions |
| 609 | // that are recurrent in the loop, and substitute the exit values from the |
| 610 | // loop into any instructions outside of the loop that use the final values of |
| 611 | // the current expressions. |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 612 | // |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 613 | SCEVHandle BackedgeTakenCount = SE->getBackedgeTakenCount(L); |
| 614 | if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) |
| 615 | RewriteLoopExitValues(L, BackedgeTakenCount); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 616 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 617 | // Next, analyze all of the induction variables in the loop, canonicalizing |
| 618 | // auxillary induction variables. |
| 619 | std::vector<std::pair<PHINode*, SCEVHandle> > IndVars; |
| 620 | |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 621 | for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { |
| 622 | PHINode *PN = cast<PHINode>(I); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 623 | if (PN->getType()->isInteger() || isa<PointerType>(PN->getType())) { |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 624 | SCEVHandle SCEV = SE->getSCEV(PN); |
Dan Gohman | cd3eb9b | 2009-02-14 02:25:19 +0000 | [diff] [blame] | 625 | // FIXME: It is an extremely bad idea to indvar substitute anything more |
| 626 | // complex than affine induction variables. Doing so will put expensive |
| 627 | // polynomial evaluations inside of the loop, and the str reduction pass |
| 628 | // currently can only reduce affine polynomials. For now just disable |
| 629 | // indvar subst on anything more complex than an affine addrec. |
| 630 | if (SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(SCEV)) |
| 631 | if (AR->getLoop() == L && AR->isAffine()) |
| 632 | IndVars.push_back(std::make_pair(PN, SCEV)); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 633 | } |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 634 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 635 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 636 | // Compute the type of the largest recurrence expression, and collect |
| 637 | // the set of the types of the other recurrence expressions. |
| 638 | const Type *LargestType = 0; |
| 639 | SmallSetVector<const Type *, 4> SizesToInsert; |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 640 | if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount)) { |
| 641 | LargestType = BackedgeTakenCount->getType(); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 642 | if (isa<PointerType>(LargestType)) |
| 643 | LargestType = TD->getIntPtrType(); |
| 644 | SizesToInsert.insert(LargestType); |
Chris Lattner | f50af08 | 2004-04-17 18:08:33 +0000 | [diff] [blame] | 645 | } |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 646 | for (unsigned i = 0, e = IndVars.size(); i != e; ++i) { |
| 647 | const PHINode *PN = IndVars[i].first; |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 648 | const Type *PNTy = PN->getType(); |
| 649 | if (isa<PointerType>(PNTy)) PNTy = TD->getIntPtrType(); |
| 650 | SizesToInsert.insert(PNTy); |
| 651 | const Type *EffTy = getEffectiveIndvarType(PN, TD); |
| 652 | if (isa<PointerType>(EffTy)) EffTy = TD->getIntPtrType(); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 653 | SizesToInsert.insert(EffTy); |
| 654 | if (!LargestType || |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 655 | TD->getTypeSizeInBits(EffTy) > |
| 656 | TD->getTypeSizeInBits(LargestType)) |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 657 | LargestType = EffTy; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 660 | // Create a rewriter object which we'll use to transform the code with. |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 661 | SCEVExpander Rewriter(*SE, *LI, *TD); |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 663 | // Now that we know the largest of of the induction variables in this loop, |
| 664 | // insert a canonical induction variable of the largest size. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 665 | Value *IndVar = 0; |
| 666 | if (!SizesToInsert.empty()) { |
| 667 | IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L,LargestType); |
| 668 | ++NumInserted; |
| 669 | Changed = true; |
| 670 | DOUT << "INDVARS: New CanIV: " << *IndVar; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 671 | } |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 672 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 673 | // If we have a trip count expression, rewrite the loop's exit condition |
| 674 | // using it. We can currently only handle loops with a single exit. |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 675 | bool NoSignedWrap = false; |
| 676 | bool NoUnsignedWrap = false; |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 677 | const ConstantInt* InitialVal, * IncrVal, * LimitVal; |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 678 | const PHINode *OrigControllingPHI = 0; |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 679 | if (!isa<SCEVCouldNotCompute>(BackedgeTakenCount) && ExitingBlock) |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 680 | // Can't rewrite non-branch yet. |
| 681 | if (BranchInst *BI = dyn_cast<BranchInst>(ExitingBlock->getTerminator())) { |
| 682 | if (Instruction *OrigCond = dyn_cast<Instruction>(BI->getCondition())) { |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 683 | // Determine if the OrigIV will ever undergo overflow. |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 684 | OrigControllingPHI = |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 685 | TestOrigIVForWrap(L, BI, OrigCond, TD, |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 686 | NoSignedWrap, NoUnsignedWrap, |
| 687 | InitialVal, IncrVal, LimitVal); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 688 | |
| 689 | // We'll be replacing the original condition, so it'll be dead. |
| 690 | DeadInsts.insert(OrigCond); |
| 691 | } |
| 692 | |
Dan Gohman | 46bdfb0 | 2009-02-24 18:55:53 +0000 | [diff] [blame] | 693 | LinearFunctionTestReplace(L, BackedgeTakenCount, IndVar, |
Dan Gohman | 15cab28 | 2009-02-23 23:20:35 +0000 | [diff] [blame] | 694 | ExitingBlock, BI, Rewriter); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 695 | } |
| 696 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 697 | // Now that we have a canonical induction variable, we can rewrite any |
| 698 | // recurrences in terms of the induction variable. Start with the auxillary |
| 699 | // induction variables, and recursively rewrite any of their uses. |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 700 | BasicBlock::iterator InsertPt = Header->getFirstNonPHI(); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 5d461d2 | 2004-04-21 22:22:01 +0000 | [diff] [blame] | 702 | // If there were induction variables of other sizes, cast the primary |
| 703 | // induction variable to the right size for them, avoiding the need for the |
| 704 | // code evaluation methods to insert induction variables of different sizes. |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 705 | for (unsigned i = 0, e = SizesToInsert.size(); i != e; ++i) { |
| 706 | const Type *Ty = SizesToInsert[i]; |
| 707 | if (Ty != LargestType) { |
| 708 | Instruction *New = new TruncInst(IndVar, Ty, "indvar", InsertPt); |
| 709 | Rewriter.addInsertedValue(New, SE->getSCEV(New)); |
| 710 | DOUT << "INDVARS: Made trunc IV for type " << *Ty << ": " |
| 711 | << *New << "\n"; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 712 | } |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Chris Lattner | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 715 | // Rewrite all induction variables in terms of the canonical induction |
| 716 | // variable. |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 717 | while (!IndVars.empty()) { |
| 718 | PHINode *PN = IndVars.back().first; |
Dan Gohman | 1a5e936 | 2009-02-17 00:10:53 +0000 | [diff] [blame] | 719 | SCEVAddRecExpr *AR = cast<SCEVAddRecExpr>(IndVars.back().second); |
Dan Gohman | 2d1be87 | 2009-04-16 03:18:22 +0000 | [diff] [blame^] | 720 | Value *NewVal = Rewriter.expandCodeFor(AR, PN->getType(), InsertPt); |
Dan Gohman | 1a5e936 | 2009-02-17 00:10:53 +0000 | [diff] [blame] | 721 | DOUT << "INDVARS: Rewrote IV '" << *AR << "' " << *PN |
Chris Lattner | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 722 | << " into = " << *NewVal << "\n"; |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 723 | NewVal->takeName(PN); |
Chris Lattner | 5d461d2 | 2004-04-21 22:22:01 +0000 | [diff] [blame] | 724 | |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 725 | /// If the new canonical induction variable is wider than the original, |
| 726 | /// and the original has uses that are casts to wider types, see if the |
| 727 | /// truncate and extend can be omitted. |
Dan Gohman | d2067fd | 2009-02-18 00:52:00 +0000 | [diff] [blame] | 728 | if (PN == OrigControllingPHI && PN->getType() != LargestType) |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 729 | for (Value::use_iterator UI = PN->use_begin(), UE = PN->use_end(); |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 730 | UI != UE; ++UI) { |
Dale Johannesen | d3325d28 | 2009-04-15 20:41:02 +0000 | [diff] [blame] | 731 | Instruction *UInst = dyn_cast<Instruction>(*UI); |
| 732 | if (UInst && isa<SExtInst>(UInst) && NoSignedWrap) { |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 733 | Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType, L, |
Dale Johannesen | d3325d28 | 2009-04-15 20:41:02 +0000 | [diff] [blame] | 734 | UInst->getType(), Rewriter, InsertPt); |
| 735 | UInst->replaceAllUsesWith(TruncIndVar); |
| 736 | DeadInsts.insert(UInst); |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 737 | } |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 738 | // See if we can figure out sext(i+constant) doesn't wrap, so we can |
| 739 | // use a larger add. This is common in subscripting. |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 740 | if (UInst && UInst->getOpcode()==Instruction::Add && |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 741 | allUsesAreSameTyped(Instruction::SExt, UInst) && |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 742 | isa<ConstantInt>(UInst->getOperand(1)) && |
Dale Johannesen | d3325d28 | 2009-04-15 20:41:02 +0000 | [diff] [blame] | 743 | NoSignedWrap && LimitVal) { |
| 744 | uint64_t oldBitSize = LimitVal->getValue().getBitWidth(); |
| 745 | uint64_t newBitSize = LargestType->getPrimitiveSizeInBits(); |
| 746 | ConstantInt* AddRHS = dyn_cast<ConstantInt>(UInst->getOperand(1)); |
| 747 | if (((APInt::getSignedMaxValue(oldBitSize) - IncrVal->getValue()) - |
| 748 | AddRHS->getValue()).sgt(LimitVal->getValue())) { |
| 749 | // We've determined this is (i+constant) and it won't overflow. |
| 750 | if (isa<SExtInst>(UInst->use_begin())) { |
| 751 | SExtInst* oldSext = dyn_cast<SExtInst>(UInst->use_begin()); |
| 752 | Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType, |
| 753 | L, oldSext->getType(), Rewriter, |
| 754 | InsertPt); |
| 755 | APInt APcopy = APInt(AddRHS->getValue()); |
| 756 | ConstantInt* newAddRHS =ConstantInt::get(APcopy.sext(newBitSize)); |
| 757 | Value *NewAdd = |
| 758 | BinaryOperator::CreateAdd(TruncIndVar, newAddRHS, |
| 759 | UInst->getName()+".nosex", UInst); |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 760 | for (Value::use_iterator UI2 = UInst->use_begin(), |
| 761 | UE2 = UInst->use_end(); UI2 != UE2; ++UI2) { |
| 762 | Instruction *II = dyn_cast<Instruction>(UI2); |
| 763 | II->replaceAllUsesWith(NewAdd); |
| 764 | DeadInsts.insert(II); |
| 765 | } |
Dale Johannesen | d3325d28 | 2009-04-15 20:41:02 +0000 | [diff] [blame] | 766 | DeadInsts.insert(UInst); |
| 767 | } |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 768 | } |
| 769 | } |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 770 | // Try for sext(i | constant). This is safe as long as the |
| 771 | // high bit of the constant is not set. |
| 772 | if (UInst && UInst->getOpcode()==Instruction::Or && |
| 773 | allUsesAreSameTyped(Instruction::SExt, UInst) && NoSignedWrap && |
| 774 | isa<ConstantInt>(UInst->getOperand(1))) { |
| 775 | ConstantInt* RHS = dyn_cast<ConstantInt>(UInst->getOperand(1)); |
| 776 | if (!RHS->getValue().isNegative()) { |
| 777 | uint64_t newBitSize = LargestType->getPrimitiveSizeInBits(); |
| 778 | SExtInst* oldSext = dyn_cast<SExtInst>(UInst->use_begin()); |
| 779 | Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType, |
| 780 | L, oldSext->getType(), Rewriter, |
| 781 | InsertPt); |
| 782 | APInt APcopy = APInt(RHS->getValue()); |
| 783 | ConstantInt* newRHS =ConstantInt::get(APcopy.sext(newBitSize)); |
| 784 | Value *NewAdd = |
| 785 | BinaryOperator::CreateOr(TruncIndVar, newRHS, |
| 786 | UInst->getName()+".nosex", UInst); |
| 787 | for (Value::use_iterator UI2 = UInst->use_begin(), |
| 788 | UE2 = UInst->use_end(); UI2 != UE2; ++UI2) { |
| 789 | Instruction *II = dyn_cast<Instruction>(UI2); |
| 790 | II->replaceAllUsesWith(NewAdd); |
| 791 | DeadInsts.insert(II); |
| 792 | } |
| 793 | DeadInsts.insert(UInst); |
| 794 | } |
| 795 | } |
| 796 | // A zext of a signed variable known not to overflow is still safe. |
| 797 | if (UInst && isa<ZExtInst>(UInst) && (NoUnsignedWrap || NoSignedWrap)) { |
Dale Johannesen | dd1f9e4 | 2009-04-15 01:10:12 +0000 | [diff] [blame] | 798 | Value *TruncIndVar = getZeroExtendedTruncVar(AR, SE, LargestType, L, |
Dale Johannesen | d3325d28 | 2009-04-15 20:41:02 +0000 | [diff] [blame] | 799 | UInst->getType(), Rewriter, InsertPt); |
| 800 | UInst->replaceAllUsesWith(TruncIndVar); |
| 801 | DeadInsts.insert(UInst); |
| 802 | } |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 803 | // If we have zext(i&constant), it's always safe to use the larger |
| 804 | // variable. This is not common but is a bottleneck in Openssl. |
Dale Johannesen | d3325d28 | 2009-04-15 20:41:02 +0000 | [diff] [blame] | 805 | // (RHS doesn't have to be constant. There should be a better approach |
| 806 | // than bottom-up pattern matching for this...) |
| 807 | if (UInst && UInst->getOpcode()==Instruction::And && |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 808 | allUsesAreSameTyped(Instruction::ZExt, UInst) && |
| 809 | isa<ConstantInt>(UInst->getOperand(1))) { |
Dale Johannesen | d3325d28 | 2009-04-15 20:41:02 +0000 | [diff] [blame] | 810 | uint64_t newBitSize = LargestType->getPrimitiveSizeInBits(); |
| 811 | ConstantInt* AndRHS = dyn_cast<ConstantInt>(UInst->getOperand(1)); |
| 812 | ZExtInst* oldZext = dyn_cast<ZExtInst>(UInst->use_begin()); |
| 813 | Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType, |
| 814 | L, oldZext->getType(), Rewriter, InsertPt); |
| 815 | APInt APcopy = APInt(AndRHS->getValue()); |
| 816 | ConstantInt* newAndRHS = ConstantInt::get(APcopy.zext(newBitSize)); |
| 817 | Value *NewAnd = |
| 818 | BinaryOperator::CreateAnd(TruncIndVar, newAndRHS, |
| 819 | UInst->getName()+".nozex", UInst); |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 820 | for (Value::use_iterator UI2 = UInst->use_begin(), |
| 821 | UE2 = UInst->use_end(); UI2 != UE2; ++UI2) { |
| 822 | Instruction *II = dyn_cast<Instruction>(UI2); |
| 823 | II->replaceAllUsesWith(NewAnd); |
| 824 | DeadInsts.insert(II); |
| 825 | } |
Dale Johannesen | d3325d28 | 2009-04-15 20:41:02 +0000 | [diff] [blame] | 826 | DeadInsts.insert(UInst); |
| 827 | } |
| 828 | // If we have zext((i+constant)&constant), we can use the larger |
| 829 | // variable even if the add does overflow. This works whenever the |
| 830 | // constant being ANDed is the same size as i, which it presumably is. |
| 831 | // We don't need to restrict the expression being and'ed to i+const, |
| 832 | // but we have to promote everything in it, so it's convenient. |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 833 | // zext((i | constant)&constant) is also valid and accepted here. |
| 834 | if (UInst && (UInst->getOpcode()==Instruction::Add || |
| 835 | UInst->getOpcode()==Instruction::Or) && |
Dale Johannesen | d3325d28 | 2009-04-15 20:41:02 +0000 | [diff] [blame] | 836 | UInst->hasOneUse() && |
| 837 | isa<ConstantInt>(UInst->getOperand(1))) { |
| 838 | uint64_t newBitSize = LargestType->getPrimitiveSizeInBits(); |
| 839 | ConstantInt* AddRHS = dyn_cast<ConstantInt>(UInst->getOperand(1)); |
| 840 | Instruction *UInst2 = dyn_cast<Instruction>(UInst->use_begin()); |
| 841 | if (UInst2 && UInst2->getOpcode() == Instruction::And && |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 842 | allUsesAreSameTyped(Instruction::ZExt, UInst2) && |
| 843 | isa<ConstantInt>(UInst2->getOperand(1))) { |
Dale Johannesen | d3325d28 | 2009-04-15 20:41:02 +0000 | [diff] [blame] | 844 | ZExtInst* oldZext = dyn_cast<ZExtInst>(UInst2->use_begin()); |
| 845 | Value *TruncIndVar = getSignExtendedTruncVar(AR, SE, LargestType, |
| 846 | L, oldZext->getType(), Rewriter, InsertPt); |
| 847 | ConstantInt* AndRHS = dyn_cast<ConstantInt>(UInst2->getOperand(1)); |
| 848 | APInt APcopy = APInt(AddRHS->getValue()); |
| 849 | ConstantInt* newAddRHS = ConstantInt::get(APcopy.zext(newBitSize)); |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 850 | Value *NewAdd = ((UInst->getOpcode()==Instruction::Add) ? |
Dale Johannesen | d3325d28 | 2009-04-15 20:41:02 +0000 | [diff] [blame] | 851 | BinaryOperator::CreateAdd(TruncIndVar, newAddRHS, |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 852 | UInst->getName()+".nozex", UInst2) : |
| 853 | BinaryOperator::CreateOr(TruncIndVar, newAddRHS, |
| 854 | UInst->getName()+".nozex", UInst2)); |
Dale Johannesen | d3325d28 | 2009-04-15 20:41:02 +0000 | [diff] [blame] | 855 | APInt APcopy2 = APInt(AndRHS->getValue()); |
| 856 | ConstantInt* newAndRHS = ConstantInt::get(APcopy2.zext(newBitSize)); |
| 857 | Value *NewAnd = |
| 858 | BinaryOperator::CreateAnd(NewAdd, newAndRHS, |
| 859 | UInst->getName()+".nozex", UInst2); |
Dale Johannesen | c671d89 | 2009-04-15 23:31:51 +0000 | [diff] [blame] | 860 | for (Value::use_iterator UI2 = UInst2->use_begin(), |
| 861 | UE2 = UInst2->use_end(); UI2 != UE2; ++UI2) { |
| 862 | Instruction *II = dyn_cast<Instruction>(UI2); |
| 863 | II->replaceAllUsesWith(NewAnd); |
| 864 | DeadInsts.insert(II); |
| 865 | } |
Dale Johannesen | d3325d28 | 2009-04-15 20:41:02 +0000 | [diff] [blame] | 866 | DeadInsts.insert(UInst); |
| 867 | DeadInsts.insert(UInst2); |
| 868 | } |
Dan Gohman | aa03649 | 2009-02-14 02:31:09 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
Dan Gohman | c2390b1 | 2009-02-12 22:19:27 +0000 | [diff] [blame] | 871 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 872 | // Replace the old PHI Node with the inserted computation. |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 873 | PN->replaceAllUsesWith(NewVal); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 874 | DeadInsts.insert(PN); |
| 875 | IndVars.pop_back(); |
| 876 | ++NumRemoved; |
Chris Lattner | 4753bf2 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 877 | Changed = true; |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Chris Lattner | 1363e85 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 880 | DeleteTriviallyDeadInstructions(DeadInsts); |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 881 | assert(L->isLCSSAForm()); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 882 | return Changed; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 883 | } |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 884 | |
Devang Patel | 13877bf | 2008-11-18 00:40:02 +0000 | [diff] [blame] | 885 | /// Return true if it is OK to use SIToFPInst for an inducation variable |
| 886 | /// with given inital and exit values. |
| 887 | static bool useSIToFPInst(ConstantFP &InitV, ConstantFP &ExitV, |
| 888 | uint64_t intIV, uint64_t intEV) { |
| 889 | |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 890 | if (InitV.getValueAPF().isNegative() || ExitV.getValueAPF().isNegative()) |
Devang Patel | 13877bf | 2008-11-18 00:40:02 +0000 | [diff] [blame] | 891 | return true; |
| 892 | |
| 893 | // If the iteration range can be handled by SIToFPInst then use it. |
| 894 | APInt Max = APInt::getSignedMaxValue(32); |
Bill Wendling | 9bef706 | 2008-11-18 10:57:27 +0000 | [diff] [blame] | 895 | if (Max.getZExtValue() > static_cast<uint64_t>(abs(intEV - intIV))) |
Devang Patel | 13877bf | 2008-11-18 00:40:02 +0000 | [diff] [blame] | 896 | return true; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 897 | |
Devang Patel | 13877bf | 2008-11-18 00:40:02 +0000 | [diff] [blame] | 898 | return false; |
| 899 | } |
| 900 | |
| 901 | /// convertToInt - Convert APF to an integer, if possible. |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 902 | static bool convertToInt(const APFloat &APF, uint64_t *intVal) { |
| 903 | |
| 904 | bool isExact = false; |
Evan Cheng | 794a7db | 2008-11-26 01:11:57 +0000 | [diff] [blame] | 905 | if (&APF.getSemantics() == &APFloat::PPCDoubleDouble) |
| 906 | return false; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 907 | if (APF.convertToInteger(intVal, 32, APF.isNegative(), |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 908 | APFloat::rmTowardZero, &isExact) |
| 909 | != APFloat::opOK) |
| 910 | return false; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 911 | if (!isExact) |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 912 | return false; |
| 913 | return true; |
| 914 | |
| 915 | } |
| 916 | |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 917 | /// HandleFloatingPointIV - If the loop has floating induction variable |
| 918 | /// then insert corresponding integer induction variable if possible. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 919 | /// For example, |
| 920 | /// for(double i = 0; i < 10000; ++i) |
| 921 | /// bar(i) |
| 922 | /// is converted into |
| 923 | /// for(int i = 0; i < 10000; ++i) |
| 924 | /// bar((double)i); |
| 925 | /// |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 926 | void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH, |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 927 | SmallPtrSet<Instruction*, 16> &DeadInsts) { |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 928 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 929 | unsigned IncomingEdge = L->contains(PH->getIncomingBlock(0)); |
| 930 | unsigned BackEdge = IncomingEdge^1; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 931 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 932 | // Check incoming value. |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 933 | ConstantFP *InitValue = dyn_cast<ConstantFP>(PH->getIncomingValue(IncomingEdge)); |
| 934 | if (!InitValue) return; |
| 935 | uint64_t newInitValue = Type::Int32Ty->getPrimitiveSizeInBits(); |
| 936 | if (!convertToInt(InitValue->getValueAPF(), &newInitValue)) |
| 937 | return; |
| 938 | |
| 939 | // Check IV increment. Reject this PH if increement operation is not |
| 940 | // an add or increment value can not be represented by an integer. |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 941 | BinaryOperator *Incr = |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 942 | dyn_cast<BinaryOperator>(PH->getIncomingValue(BackEdge)); |
| 943 | if (!Incr) return; |
| 944 | if (Incr->getOpcode() != Instruction::Add) return; |
| 945 | ConstantFP *IncrValue = NULL; |
| 946 | unsigned IncrVIndex = 1; |
| 947 | if (Incr->getOperand(1) == PH) |
| 948 | IncrVIndex = 0; |
| 949 | IncrValue = dyn_cast<ConstantFP>(Incr->getOperand(IncrVIndex)); |
| 950 | if (!IncrValue) return; |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 951 | uint64_t newIncrValue = Type::Int32Ty->getPrimitiveSizeInBits(); |
| 952 | if (!convertToInt(IncrValue->getValueAPF(), &newIncrValue)) |
| 953 | return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 954 | |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 955 | // Check Incr uses. One user is PH and the other users is exit condition used |
| 956 | // by the conditional terminator. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 957 | Value::use_iterator IncrUse = Incr->use_begin(); |
| 958 | Instruction *U1 = cast<Instruction>(IncrUse++); |
| 959 | if (IncrUse == Incr->use_end()) return; |
| 960 | Instruction *U2 = cast<Instruction>(IncrUse++); |
| 961 | if (IncrUse != Incr->use_end()) return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 962 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 963 | // Find exit condition. |
| 964 | FCmpInst *EC = dyn_cast<FCmpInst>(U1); |
| 965 | if (!EC) |
| 966 | EC = dyn_cast<FCmpInst>(U2); |
| 967 | if (!EC) return; |
| 968 | |
| 969 | if (BranchInst *BI = dyn_cast<BranchInst>(EC->getParent()->getTerminator())) { |
| 970 | if (!BI->isConditional()) return; |
| 971 | if (BI->getCondition() != EC) return; |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 972 | } |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 973 | |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 974 | // Find exit value. If exit value can not be represented as an interger then |
| 975 | // do not handle this floating point PH. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 976 | ConstantFP *EV = NULL; |
| 977 | unsigned EVIndex = 1; |
| 978 | if (EC->getOperand(1) == Incr) |
| 979 | EVIndex = 0; |
| 980 | EV = dyn_cast<ConstantFP>(EC->getOperand(EVIndex)); |
| 981 | if (!EV) return; |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 982 | uint64_t intEV = Type::Int32Ty->getPrimitiveSizeInBits(); |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 983 | if (!convertToInt(EV->getValueAPF(), &intEV)) |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 984 | return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 985 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 986 | // Find new predicate for integer comparison. |
| 987 | CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE; |
| 988 | switch (EC->getPredicate()) { |
| 989 | case CmpInst::FCMP_OEQ: |
| 990 | case CmpInst::FCMP_UEQ: |
| 991 | NewPred = CmpInst::ICMP_EQ; |
| 992 | break; |
| 993 | case CmpInst::FCMP_OGT: |
| 994 | case CmpInst::FCMP_UGT: |
| 995 | NewPred = CmpInst::ICMP_UGT; |
| 996 | break; |
| 997 | case CmpInst::FCMP_OGE: |
| 998 | case CmpInst::FCMP_UGE: |
| 999 | NewPred = CmpInst::ICMP_UGE; |
| 1000 | break; |
| 1001 | case CmpInst::FCMP_OLT: |
| 1002 | case CmpInst::FCMP_ULT: |
| 1003 | NewPred = CmpInst::ICMP_ULT; |
| 1004 | break; |
| 1005 | case CmpInst::FCMP_OLE: |
| 1006 | case CmpInst::FCMP_ULE: |
| 1007 | NewPred = CmpInst::ICMP_ULE; |
| 1008 | break; |
| 1009 | default: |
| 1010 | break; |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 1011 | } |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1012 | if (NewPred == CmpInst::BAD_ICMP_PREDICATE) return; |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1013 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1014 | // Insert new integer induction variable. |
| 1015 | PHINode *NewPHI = PHINode::Create(Type::Int32Ty, |
| 1016 | PH->getName()+".int", PH); |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 1017 | NewPHI->addIncoming(ConstantInt::get(Type::Int32Ty, newInitValue), |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1018 | PH->getIncomingBlock(IncomingEdge)); |
| 1019 | |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1020 | Value *NewAdd = BinaryOperator::CreateAdd(NewPHI, |
| 1021 | ConstantInt::get(Type::Int32Ty, |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 1022 | newIncrValue), |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1023 | Incr->getName()+".int", Incr); |
| 1024 | NewPHI->addIncoming(NewAdd, PH->getIncomingBlock(BackEdge)); |
| 1025 | |
| 1026 | ConstantInt *NewEV = ConstantInt::get(Type::Int32Ty, intEV); |
| 1027 | Value *LHS = (EVIndex == 1 ? NewPHI->getIncomingValue(BackEdge) : NewEV); |
| 1028 | Value *RHS = (EVIndex == 1 ? NewEV : NewPHI->getIncomingValue(BackEdge)); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1029 | ICmpInst *NewEC = new ICmpInst(NewPred, LHS, RHS, EC->getNameStart(), |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1030 | EC->getParent()->getTerminator()); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1031 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1032 | // Delete old, floating point, exit comparision instruction. |
| 1033 | EC->replaceAllUsesWith(NewEC); |
| 1034 | DeadInsts.insert(EC); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1035 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1036 | // Delete old, floating point, increment instruction. |
| 1037 | Incr->replaceAllUsesWith(UndefValue::get(Incr->getType())); |
| 1038 | DeadInsts.insert(Incr); |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1039 | |
Devang Patel | 13877bf | 2008-11-18 00:40:02 +0000 | [diff] [blame] | 1040 | // Replace floating induction variable. Give SIToFPInst preference over |
| 1041 | // UIToFPInst because it is faster on platforms that are widely used. |
| 1042 | if (useSIToFPInst(*InitValue, *EV, newInitValue, intEV)) { |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1043 | SIToFPInst *Conv = new SIToFPInst(NewPHI, PH->getType(), "indvar.conv", |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 1044 | PH->getParent()->getFirstNonPHI()); |
| 1045 | PH->replaceAllUsesWith(Conv); |
| 1046 | } else { |
Dan Gohman | cafb813 | 2009-02-17 19:13:57 +0000 | [diff] [blame] | 1047 | UIToFPInst *Conv = new UIToFPInst(NewPHI, PH->getType(), "indvar.conv", |
Devang Patel | cd40233 | 2008-11-17 23:27:13 +0000 | [diff] [blame] | 1048 | PH->getParent()->getFirstNonPHI()); |
| 1049 | PH->replaceAllUsesWith(Conv); |
| 1050 | } |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame] | 1051 | DeadInsts.insert(PH); |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |