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