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" |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/SmallPtrSet.h" |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/Statistic.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 58 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 0e5f499 | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 60 | STATISTIC(NumRemoved , "Number of aux indvars removed"); |
| 61 | STATISTIC(NumPointer , "Number of pointer indvars promoted"); |
| 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 | |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 76 | bool runOnLoop(Loop *L, LPPassManager &LPM); |
| 77 | bool doInitialization(Loop *L, LPPassManager &LPM); |
| 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>(); |
| 83 | AU.addPreservedID(LoopSimplifyID); |
| 84 | AU.addPreservedID(LCSSAID); |
| 85 | AU.setPreservesCFG(); |
| 86 | } |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 87 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 88 | private: |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 90 | void EliminatePointerRecurrence(PHINode *PN, BasicBlock *Preheader, |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 91 | SmallPtrSet<Instruction*, 16> &DeadInsts); |
Chris Lattner | 9ba46c1 | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 92 | Instruction *LinearFunctionTestReplace(Loop *L, SCEV *IterationCount, |
| 93 | SCEVExpander &RW); |
Dan Gohman | 5a6c448 | 2008-08-05 22:34:21 +0000 | [diff] [blame] | 94 | void RewriteLoopExitValues(Loop *L, SCEV *IterationCount); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 96 | void DeleteTriviallyDeadInstructions(SmallPtrSet<Instruction*, 16> &Insts); |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 97 | |
| 98 | void OptimizeCanonicalIVType(Loop *L); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame^] | 99 | void HandleFloatingPointIV(Loop *L, PHINode *PH, |
| 100 | SmallPtrSet<Instruction*, 16> &DeadInsts); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 101 | }; |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 102 | } |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 103 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 104 | char IndVarSimplify::ID = 0; |
| 105 | static RegisterPass<IndVarSimplify> |
| 106 | X("indvars", "Canonicalize Induction Variables"); |
| 107 | |
Daniel Dunbar | 394f044 | 2008-10-22 23:32:42 +0000 | [diff] [blame] | 108 | Pass *llvm::createIndVarSimplifyPass() { |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 109 | return new IndVarSimplify(); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 112 | /// DeleteTriviallyDeadInstructions - If any of the instructions is the |
| 113 | /// specified set are trivially dead, delete them and see if this makes any of |
| 114 | /// their operands subsequently dead. |
| 115 | void IndVarSimplify:: |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 116 | DeleteTriviallyDeadInstructions(SmallPtrSet<Instruction*, 16> &Insts) { |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 117 | while (!Insts.empty()) { |
| 118 | Instruction *I = *Insts.begin(); |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 119 | Insts.erase(I); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 120 | if (isInstructionTriviallyDead(I)) { |
| 121 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 122 | if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i))) |
| 123 | Insts.insert(U); |
Dan Gohman | 5cec4db | 2007-06-19 14:28:31 +0000 | [diff] [blame] | 124 | SE->deleteValueFromRecords(I); |
Chris Lattner | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 125 | DOUT << "INDVARS: Deleting: " << *I; |
Chris Lattner | a4b9c78 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 126 | I->eraseFromParent(); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 127 | Changed = true; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | |
| 133 | /// EliminatePointerRecurrence - Check to see if this is a trivial GEP pointer |
| 134 | /// recurrence. If so, change it into an integer recurrence, permitting |
| 135 | /// analysis by the SCEV routines. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 136 | void IndVarSimplify::EliminatePointerRecurrence(PHINode *PN, |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 137 | BasicBlock *Preheader, |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 138 | SmallPtrSet<Instruction*, 16> &DeadInsts) { |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 139 | assert(PN->getNumIncomingValues() == 2 && "Noncanonicalized loop!"); |
| 140 | unsigned PreheaderIdx = PN->getBasicBlockIndex(Preheader); |
| 141 | unsigned BackedgeIdx = PreheaderIdx^1; |
| 142 | if (GetElementPtrInst *GEPI = |
Chris Lattner | cda9ca5 | 2005-08-10 01:12:06 +0000 | [diff] [blame] | 143 | dyn_cast<GetElementPtrInst>(PN->getIncomingValue(BackedgeIdx))) |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 144 | if (GEPI->getOperand(0) == PN) { |
Chris Lattner | cda9ca5 | 2005-08-10 01:12:06 +0000 | [diff] [blame] | 145 | assert(GEPI->getNumOperands() == 2 && "GEP types must match!"); |
Chris Lattner | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 146 | DOUT << "INDVARS: Eliminating pointer recurrence: " << *GEPI; |
| 147 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 148 | // Okay, we found a pointer recurrence. Transform this pointer |
| 149 | // recurrence into an integer recurrence. Compute the value that gets |
| 150 | // added to the pointer at every iteration. |
| 151 | Value *AddedVal = GEPI->getOperand(1); |
| 152 | |
| 153 | // Insert a new integer PHI node into the top of the block. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 154 | PHINode *NewPhi = PHINode::Create(AddedVal->getType(), |
| 155 | PN->getName()+".rec", PN); |
Chris Lattner | c5c5e6a | 2004-06-20 05:04:01 +0000 | [diff] [blame] | 156 | NewPhi->addIncoming(Constant::getNullValue(NewPhi->getType()), Preheader); |
| 157 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 158 | // Create the new add instruction. |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 159 | Value *NewAdd = BinaryOperator::CreateAdd(NewPhi, AddedVal, |
Chris Lattner | c5c5e6a | 2004-06-20 05:04:01 +0000 | [diff] [blame] | 160 | GEPI->getName()+".rec", GEPI); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 161 | NewPhi->addIncoming(NewAdd, PN->getIncomingBlock(BackedgeIdx)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 162 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 163 | // Update the existing GEP to use the recurrence. |
| 164 | GEPI->setOperand(0, PN->getIncomingValue(PreheaderIdx)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 165 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 166 | // Update the GEP to use the new recurrence we just inserted. |
| 167 | GEPI->setOperand(1, NewAdd); |
| 168 | |
Chris Lattner | a4b9c78 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 169 | // If the incoming value is a constant expr GEP, try peeling out the array |
| 170 | // 0 index if possible to make things simpler. |
| 171 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEPI->getOperand(0))) |
| 172 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 173 | unsigned NumOps = CE->getNumOperands(); |
| 174 | assert(NumOps > 1 && "CE folding didn't work!"); |
| 175 | if (CE->getOperand(NumOps-1)->isNullValue()) { |
| 176 | // Check to make sure the last index really is an array index. |
Chris Lattner | 1730078 | 2005-11-18 18:30:47 +0000 | [diff] [blame] | 177 | gep_type_iterator GTI = gep_type_begin(CE); |
Chris Lattner | ceda605 | 2005-11-17 19:35:42 +0000 | [diff] [blame] | 178 | for (unsigned i = 1, e = CE->getNumOperands()-1; |
Chris Lattner | a4b9c78 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 179 | i != e; ++i, ++GTI) |
| 180 | /*empty*/; |
| 181 | if (isa<SequentialType>(*GTI)) { |
| 182 | // Pull the last index out of the constant expr GEP. |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 183 | SmallVector<Value*, 8> CEIdxs(CE->op_begin()+1, CE->op_end()-1); |
Chris Lattner | a4b9c78 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 184 | Constant *NCE = ConstantExpr::getGetElementPtr(CE->getOperand(0), |
Chris Lattner | 55eb1c4 | 2007-01-31 04:40:53 +0000 | [diff] [blame] | 185 | &CEIdxs[0], |
| 186 | CEIdxs.size()); |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 187 | Value *Idx[2]; |
| 188 | Idx[0] = Constant::getNullValue(Type::Int32Ty); |
| 189 | Idx[1] = NewAdd; |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 190 | GetElementPtrInst *NGEPI = GetElementPtrInst::Create( |
David Greene | b8f7479 | 2007-09-04 15:46:09 +0000 | [diff] [blame] | 191 | NCE, Idx, Idx + 2, |
Reid Spencer | cae5754 | 2007-03-02 00:28:52 +0000 | [diff] [blame] | 192 | GEPI->getName(), GEPI); |
Dan Gohman | 5cec4db | 2007-06-19 14:28:31 +0000 | [diff] [blame] | 193 | SE->deleteValueFromRecords(GEPI); |
Chris Lattner | a4b9c78 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 194 | GEPI->replaceAllUsesWith(NGEPI); |
| 195 | GEPI->eraseFromParent(); |
| 196 | GEPI = NGEPI; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 202 | // Finally, if there are any other users of the PHI node, we must |
| 203 | // insert a new GEP instruction that uses the pre-incremented version |
| 204 | // of the induction amount. |
| 205 | if (!PN->use_empty()) { |
| 206 | BasicBlock::iterator InsertPos = PN; ++InsertPos; |
| 207 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 208 | Value *PreInc = |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 209 | GetElementPtrInst::Create(PN->getIncomingValue(PreheaderIdx), |
| 210 | NewPhi, "", InsertPos); |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 211 | PreInc->takeName(PN); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 212 | PN->replaceAllUsesWith(PreInc); |
| 213 | } |
| 214 | |
| 215 | // Delete the old PHI for sure, and the GEP if its otherwise unused. |
| 216 | DeadInsts.insert(PN); |
| 217 | |
| 218 | ++NumPointer; |
| 219 | Changed = true; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | /// LinearFunctionTestReplace - This method rewrites the exit condition of the |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 224 | /// loop to be a canonical != comparison against the incremented loop induction |
| 225 | /// variable. This pass is able to rewrite the exit tests of any loop where the |
| 226 | /// SCEV analysis can determine a loop-invariant trip count of the loop, which |
| 227 | /// is actually a much broader range than just linear tests. |
Chris Lattner | 9ba46c1 | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 228 | /// |
| 229 | /// This method returns a "potentially dead" instruction whose computation chain |
| 230 | /// should be deleted when convenient. |
| 231 | Instruction *IndVarSimplify::LinearFunctionTestReplace(Loop *L, |
| 232 | SCEV *IterationCount, |
| 233 | SCEVExpander &RW) { |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 234 | // Find the exit block for the loop. We can currently only handle loops with |
| 235 | // a single exit. |
Devang Patel | b7211a2 | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 236 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 237 | L->getExitBlocks(ExitBlocks); |
Chris Lattner | 9ba46c1 | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 238 | if (ExitBlocks.size() != 1) return 0; |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 239 | BasicBlock *ExitBlock = ExitBlocks[0]; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 240 | |
| 241 | // Make sure there is only one predecessor block in the loop. |
| 242 | BasicBlock *ExitingBlock = 0; |
| 243 | for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock); |
| 244 | PI != PE; ++PI) |
| 245 | if (L->contains(*PI)) { |
| 246 | if (ExitingBlock == 0) |
| 247 | ExitingBlock = *PI; |
| 248 | else |
Chris Lattner | 9ba46c1 | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 249 | return 0; // Multiple exits from loop to this block. |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 250 | } |
| 251 | assert(ExitingBlock && "Loop info is broken"); |
| 252 | |
| 253 | if (!isa<BranchInst>(ExitingBlock->getTerminator())) |
Chris Lattner | 9ba46c1 | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 254 | return 0; // Can't rewrite non-branch yet |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 255 | BranchInst *BI = cast<BranchInst>(ExitingBlock->getTerminator()); |
| 256 | assert(BI->isConditional() && "Must be conditional to be part of loop!"); |
| 257 | |
Chris Lattner | 9ba46c1 | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 258 | Instruction *PotentiallyDeadInst = dyn_cast<Instruction>(BI->getCondition()); |
| 259 | |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 260 | // If the exiting block is not the same as the backedge block, we must compare |
| 261 | // against the preincremented value, otherwise we prefer to compare against |
| 262 | // the post-incremented value. |
| 263 | BasicBlock *Header = L->getHeader(); |
| 264 | pred_iterator HPI = pred_begin(Header); |
| 265 | assert(HPI != pred_end(Header) && "Loop with zero preds???"); |
| 266 | if (!L->contains(*HPI)) ++HPI; |
| 267 | assert(HPI != pred_end(Header) && L->contains(*HPI) && |
| 268 | "No backedge in loop?"); |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 269 | |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 270 | SCEVHandle TripCount = IterationCount; |
| 271 | Value *IndVar; |
| 272 | if (*HPI == ExitingBlock) { |
| 273 | // The IterationCount expression contains the number of times that the |
| 274 | // backedge actually branches to the loop header. This is one less than the |
| 275 | // number of times the loop executes, so add one to it. |
Dan Gohman | e19dd87 | 2007-06-15 18:00:55 +0000 | [diff] [blame] | 276 | ConstantInt *OneC = ConstantInt::get(IterationCount->getType(), 1); |
Dan Gohman | 246b256 | 2007-10-22 18:31:58 +0000 | [diff] [blame] | 277 | TripCount = SE->getAddExpr(IterationCount, SE->getConstant(OneC)); |
Chris Lattner | d244057 | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 278 | IndVar = L->getCanonicalInductionVariableIncrement(); |
| 279 | } else { |
| 280 | // We have to use the preincremented value... |
| 281 | IndVar = L->getCanonicalInductionVariable(); |
| 282 | } |
Chris Lattner | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 283 | |
| 284 | DOUT << "INDVARS: LFTR: TripCount = " << *TripCount |
| 285 | << " IndVar = " << *IndVar << "\n"; |
Chris Lattner | 59fdaee | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 287 | // Expand the code for the iteration count into the preheader of the loop. |
| 288 | BasicBlock *Preheader = L->getLoopPreheader(); |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 289 | Value *ExitCnt = RW.expandCodeFor(TripCount, Preheader->getTerminator()); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 290 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 291 | // Insert a new icmp_ne or icmp_eq instruction before the branch. |
| 292 | ICmpInst::Predicate Opcode; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 293 | if (L->contains(BI->getSuccessor(0))) |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 294 | Opcode = ICmpInst::ICMP_NE; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 295 | else |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 296 | Opcode = ICmpInst::ICMP_EQ; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 297 | |
Reid Spencer | e4d87aa | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 298 | Value *Cond = new ICmpInst(Opcode, IndVar, ExitCnt, "exitcond", BI); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 299 | BI->setCondition(Cond); |
| 300 | ++NumLFTR; |
| 301 | Changed = true; |
Chris Lattner | 9ba46c1 | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 302 | return PotentiallyDeadInst; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | |
| 306 | /// RewriteLoopExitValues - Check to see if this loop has a computable |
| 307 | /// loop-invariant execution count. If so, this means that we can compute the |
| 308 | /// final value of any expressions that are recurrent in the loop, and |
| 309 | /// substitute the exit values from the loop into any instructions outside of |
| 310 | /// the loop that use the final values of the current expressions. |
Dan Gohman | 5a6c448 | 2008-08-05 22:34:21 +0000 | [diff] [blame] | 311 | void IndVarSimplify::RewriteLoopExitValues(Loop *L, SCEV *IterationCount) { |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 312 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 313 | |
| 314 | // Scan all of the instructions in the loop, looking at those that have |
| 315 | // extra-loop users and which are recurrences. |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 316 | SCEVExpander Rewriter(*SE, *LI); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 317 | |
| 318 | // We insert the code into the preheader of the loop if the loop contains |
| 319 | // multiple exit blocks, or in the exit block if there is exactly one. |
| 320 | BasicBlock *BlockToInsertInto; |
Devang Patel | b7211a2 | 2007-08-21 00:31:24 +0000 | [diff] [blame] | 321 | SmallVector<BasicBlock*, 8> ExitBlocks; |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 322 | L->getUniqueExitBlocks(ExitBlocks); |
Chris Lattner | f1ab4b4 | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 323 | if (ExitBlocks.size() == 1) |
| 324 | BlockToInsertInto = ExitBlocks[0]; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 325 | else |
| 326 | BlockToInsertInto = Preheader; |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 327 | BasicBlock::iterator InsertPt = BlockToInsertInto->getFirstNonPHI(); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 328 | |
Dan Gohman | 5a6c448 | 2008-08-05 22:34:21 +0000 | [diff] [blame] | 329 | bool HasConstantItCount = isa<SCEVConstant>(IterationCount); |
Chris Lattner | 20aa098 | 2004-04-17 18:44:09 +0000 | [diff] [blame] | 330 | |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 331 | SmallPtrSet<Instruction*, 16> InstructionsToDelete; |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 332 | std::map<Instruction*, Value*> ExitValues; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 333 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 334 | // Find all values that are computed inside the loop, but used outside of it. |
| 335 | // Because of LCSSA, these values will only occur in LCSSA PHI Nodes. Scan |
| 336 | // the exit blocks of the loop to find them. |
| 337 | for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i) { |
| 338 | BasicBlock *ExitBB = ExitBlocks[i]; |
| 339 | |
| 340 | // If there are no PHI nodes in this exit block, then no values defined |
| 341 | // inside the loop are used on this path, skip it. |
| 342 | PHINode *PN = dyn_cast<PHINode>(ExitBB->begin()); |
| 343 | if (!PN) continue; |
| 344 | |
| 345 | unsigned NumPreds = PN->getNumIncomingValues(); |
| 346 | |
| 347 | // Iterate over all of the PHI nodes. |
| 348 | BasicBlock::iterator BBI = ExitBB->begin(); |
| 349 | while ((PN = dyn_cast<PHINode>(BBI++))) { |
Chris Lattner | c9838f2 | 2007-03-03 22:48:48 +0000 | [diff] [blame] | 350 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 351 | // Iterate over all of the values in all the PHI nodes. |
| 352 | for (unsigned i = 0; i != NumPreds; ++i) { |
| 353 | // If the value being merged in is not integer or is not defined |
| 354 | // in the loop, skip it. |
| 355 | Value *InVal = PN->getIncomingValue(i); |
| 356 | if (!isa<Instruction>(InVal) || |
| 357 | // SCEV only supports integer expressions for now. |
| 358 | !isa<IntegerType>(InVal->getType())) |
| 359 | continue; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 361 | // If this pred is for a subloop, not L itself, skip it. |
| 362 | if (LI->getLoopFor(PN->getIncomingBlock(i)) != L) |
| 363 | continue; // The Block is in a subloop, skip it. |
| 364 | |
| 365 | // Check that InVal is defined in the loop. |
| 366 | Instruction *Inst = cast<Instruction>(InVal); |
| 367 | if (!L->contains(Inst->getParent())) |
| 368 | continue; |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 369 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 370 | // We require that this value either have a computable evolution or that |
| 371 | // the loop have a constant iteration count. In the case where the loop |
| 372 | // has a constant iteration count, we can sometimes force evaluation of |
| 373 | // the exit value through brute force. |
| 374 | SCEVHandle SH = SE->getSCEV(Inst); |
| 375 | if (!SH->hasComputableLoopEvolution(L) && !HasConstantItCount) |
| 376 | continue; // Cannot get exit evolution for the loop value. |
| 377 | |
| 378 | // Okay, this instruction has a user outside of the current loop |
| 379 | // and varies predictably *inside* the loop. Evaluate the value it |
| 380 | // contains when the loop exits, if possible. |
| 381 | SCEVHandle ExitValue = SE->getSCEVAtScope(Inst, L->getParentLoop()); |
| 382 | if (isa<SCEVCouldNotCompute>(ExitValue) || |
| 383 | !ExitValue->isLoopInvariant(L)) |
| 384 | continue; |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 385 | |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 386 | Changed = true; |
| 387 | ++NumReplaced; |
| 388 | |
| 389 | // See if we already computed the exit value for the instruction, if so, |
| 390 | // just reuse it. |
| 391 | Value *&ExitVal = ExitValues[Inst]; |
| 392 | if (!ExitVal) |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 393 | ExitVal = Rewriter.expandCodeFor(ExitValue, InsertPt); |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 394 | |
| 395 | DOUT << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal |
| 396 | << " LoopVal = " << *Inst << "\n"; |
| 397 | |
| 398 | PN->setIncomingValue(i, ExitVal); |
| 399 | |
| 400 | // If this instruction is dead now, schedule it to be removed. |
| 401 | if (Inst->use_empty()) |
| 402 | InstructionsToDelete.insert(Inst); |
| 403 | |
| 404 | // See if this is a single-entry LCSSA PHI node. If so, we can (and |
| 405 | // have to) remove |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 406 | // the PHI entirely. This is safe, because the NewVal won't be variant |
| 407 | // 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] | 408 | if (NumPreds == 1) { |
Dan Gohman | 5cec4db | 2007-06-19 14:28:31 +0000 | [diff] [blame] | 409 | SE->deleteValueFromRecords(PN); |
Chris Lattner | 9f3d738 | 2007-03-04 03:43:23 +0000 | [diff] [blame] | 410 | PN->replaceAllUsesWith(ExitVal); |
| 411 | PN->eraseFromParent(); |
| 412 | break; |
Chris Lattner | c9838f2 | 2007-03-03 22:48:48 +0000 | [diff] [blame] | 413 | } |
| 414 | } |
Chris Lattner | c9838f2 | 2007-03-03 22:48:48 +0000 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 418 | DeleteTriviallyDeadInstructions(InstructionsToDelete); |
| 419 | } |
| 420 | |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 421 | bool IndVarSimplify::doInitialization(Loop *L, LPPassManager &LPM) { |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 422 | |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 423 | Changed = false; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 424 | // First step. Check to see if there are any trivial GEP pointer recurrences. |
| 425 | // If there are, change them into integer recurrences, permitting analysis by |
| 426 | // the SCEV routines. |
| 427 | // |
| 428 | BasicBlock *Header = L->getHeader(); |
| 429 | BasicBlock *Preheader = L->getLoopPreheader(); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 430 | SE = &LPM.getAnalysis<ScalarEvolution>(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 431 | |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 432 | SmallPtrSet<Instruction*, 16> DeadInsts; |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 433 | for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { |
| 434 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 435 | if (isa<PointerType>(PN->getType())) |
| 436 | EliminatePointerRecurrence(PN, Preheader, DeadInsts); |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame^] | 437 | else |
| 438 | HandleFloatingPointIV(L, PN, DeadInsts); |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 439 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 440 | |
| 441 | if (!DeadInsts.empty()) |
| 442 | DeleteTriviallyDeadInstructions(DeadInsts); |
| 443 | |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 444 | return Changed; |
| 445 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 446 | |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 447 | bool IndVarSimplify::runOnLoop(Loop *L, LPPassManager &LPM) { |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 448 | |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 449 | |
| 450 | LI = &getAnalysis<LoopInfo>(); |
| 451 | SE = &getAnalysis<ScalarEvolution>(); |
| 452 | |
| 453 | Changed = false; |
| 454 | BasicBlock *Header = L->getHeader(); |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 455 | SmallPtrSet<Instruction*, 16> DeadInsts; |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 456 | |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 457 | // Verify the input to the pass in already in LCSSA form. |
| 458 | assert(L->isLCSSAForm()); |
| 459 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 460 | // Check to see if this loop has a computable loop-invariant execution count. |
| 461 | // If so, this means that we can compute the final value of any expressions |
| 462 | // that are recurrent in the loop, and substitute the exit values from the |
| 463 | // loop into any instructions outside of the loop that use the final values of |
| 464 | // the current expressions. |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 465 | // |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 466 | SCEVHandle IterationCount = SE->getIterationCount(L); |
| 467 | if (!isa<SCEVCouldNotCompute>(IterationCount)) |
Dan Gohman | 5a6c448 | 2008-08-05 22:34:21 +0000 | [diff] [blame] | 468 | RewriteLoopExitValues(L, IterationCount); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 469 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 470 | // Next, analyze all of the induction variables in the loop, canonicalizing |
| 471 | // auxillary induction variables. |
| 472 | std::vector<std::pair<PHINode*, SCEVHandle> > IndVars; |
| 473 | |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 474 | for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { |
| 475 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 476 | if (PN->getType()->isInteger()) { // FIXME: when we have fast-math, enable! |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 477 | SCEVHandle SCEV = SE->getSCEV(PN); |
| 478 | if (SCEV->hasComputableLoopEvolution(L)) |
Chris Lattner | cda9ca5 | 2005-08-10 01:12:06 +0000 | [diff] [blame] | 479 | // FIXME: It is an extremely bad idea to indvar substitute anything more |
| 480 | // complex than affine induction variables. Doing so will put expensive |
| 481 | // polynomial evaluations inside of the loop, and the str reduction pass |
| 482 | // currently can only reduce affine polynomials. For now just disable |
| 483 | // indvar subst on anything more complex than an affine addrec. |
Chris Lattner | 595ee7e | 2004-07-26 02:47:12 +0000 | [diff] [blame] | 484 | if (SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(SCEV)) |
Chris Lattner | cda9ca5 | 2005-08-10 01:12:06 +0000 | [diff] [blame] | 485 | if (AR->isAffine()) |
Chris Lattner | 595ee7e | 2004-07-26 02:47:12 +0000 | [diff] [blame] | 486 | IndVars.push_back(std::make_pair(PN, SCEV)); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 487 | } |
Reid Spencer | 2da5c3d | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 488 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 489 | |
| 490 | // If there are no induction variables in the loop, there is nothing more to |
| 491 | // do. |
Chris Lattner | f50af08 | 2004-04-17 18:08:33 +0000 | [diff] [blame] | 492 | if (IndVars.empty()) { |
| 493 | // Actually, if we know how many times the loop iterates, lets insert a |
| 494 | // canonical induction variable to help subsequent passes. |
| 495 | if (!isa<SCEVCouldNotCompute>(IterationCount)) { |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 496 | SCEVExpander Rewriter(*SE, *LI); |
| 497 | Rewriter.getOrInsertCanonicalInductionVariable(L, |
Chris Lattner | f50af08 | 2004-04-17 18:08:33 +0000 | [diff] [blame] | 498 | IterationCount->getType()); |
Chris Lattner | 9ba46c1 | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 499 | if (Instruction *I = LinearFunctionTestReplace(L, IterationCount, |
| 500 | Rewriter)) { |
Chris Lattner | 1a6111f | 2008-11-16 07:17:51 +0000 | [diff] [blame] | 501 | SmallPtrSet<Instruction*, 16> InstructionsToDelete; |
Chris Lattner | 9ba46c1 | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 502 | InstructionsToDelete.insert(I); |
| 503 | DeleteTriviallyDeadInstructions(InstructionsToDelete); |
| 504 | } |
Chris Lattner | f50af08 | 2004-04-17 18:08:33 +0000 | [diff] [blame] | 505 | } |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 506 | return Changed; |
Chris Lattner | f50af08 | 2004-04-17 18:08:33 +0000 | [diff] [blame] | 507 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 508 | |
| 509 | // Compute the type of the largest recurrence expression. |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 510 | // |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 511 | const Type *LargestType = IndVars[0].first->getType(); |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 512 | bool DifferingSizes = false; |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 513 | for (unsigned i = 1, e = IndVars.size(); i != e; ++i) { |
| 514 | const Type *Ty = IndVars[i].first->getType(); |
Reid Spencer | abaa8ca | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 515 | DifferingSizes |= |
| 516 | Ty->getPrimitiveSizeInBits() != LargestType->getPrimitiveSizeInBits(); |
| 517 | if (Ty->getPrimitiveSizeInBits() > LargestType->getPrimitiveSizeInBits()) |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 518 | LargestType = Ty; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 521 | // Create a rewriter object which we'll use to transform the code with. |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 522 | SCEVExpander Rewriter(*SE, *LI); |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 523 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 524 | // Now that we know the largest of of the induction variables in this loop, |
| 525 | // insert a canonical induction variable of the largest size. |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 526 | Value *IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L,LargestType); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 527 | ++NumInserted; |
| 528 | Changed = true; |
Chris Lattner | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 529 | DOUT << "INDVARS: New CanIV: " << *IndVar; |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 530 | |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 531 | if (!isa<SCEVCouldNotCompute>(IterationCount)) { |
Wojciech Matyjewicz | 9008721 | 2008-06-13 17:02:03 +0000 | [diff] [blame] | 532 | IterationCount = SE->getTruncateOrZeroExtend(IterationCount, LargestType); |
Chris Lattner | 9ba46c1 | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 533 | if (Instruction *DI = LinearFunctionTestReplace(L, IterationCount,Rewriter)) |
| 534 | DeadInsts.insert(DI); |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 535 | } |
Chris Lattner | 15cad75 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 536 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 537 | // Now that we have a canonical induction variable, we can rewrite any |
| 538 | // recurrences in terms of the induction variable. Start with the auxillary |
| 539 | // induction variables, and recursively rewrite any of their uses. |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 540 | BasicBlock::iterator InsertPt = Header->getFirstNonPHI(); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 541 | |
Chris Lattner | 5d461d2 | 2004-04-21 22:22:01 +0000 | [diff] [blame] | 542 | // If there were induction variables of other sizes, cast the primary |
| 543 | // induction variable to the right size for them, avoiding the need for the |
| 544 | // code evaluation methods to insert induction variables of different sizes. |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 545 | if (DifferingSizes) { |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 546 | SmallVector<unsigned,4> InsertedSizes; |
| 547 | InsertedSizes.push_back(LargestType->getPrimitiveSizeInBits()); |
| 548 | for (unsigned i = 0, e = IndVars.size(); i != e; ++i) { |
| 549 | unsigned ithSize = IndVars[i].first->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | ef60b2c | 2007-01-12 22:51:20 +0000 | [diff] [blame] | 550 | if (std::find(InsertedSizes.begin(), InsertedSizes.end(), ithSize) |
| 551 | == InsertedSizes.end()) { |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 552 | PHINode *PN = IndVars[i].first; |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 553 | InsertedSizes.push_back(ithSize); |
Chris Lattner | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 554 | Instruction *New = new TruncInst(IndVar, PN->getType(), "indvar", |
| 555 | InsertPt); |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 556 | Rewriter.addInsertedValue(New, SE->getSCEV(New)); |
Chris Lattner | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 557 | DOUT << "INDVARS: Made trunc IV for " << *PN |
| 558 | << " NewVal = " << *New << "\n"; |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 559 | } |
Reid Spencer | a54b7cb | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 560 | } |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Chris Lattner | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 563 | // Rewrite all induction variables in terms of the canonical induction |
| 564 | // variable. |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 565 | while (!IndVars.empty()) { |
| 566 | PHINode *PN = IndVars.back().first; |
Dan Gohman | d19534a | 2007-06-15 14:38:12 +0000 | [diff] [blame] | 567 | Value *NewVal = Rewriter.expandCodeFor(IndVars.back().second, InsertPt); |
Chris Lattner | ee4f13a | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 568 | DOUT << "INDVARS: Rewrote IV '" << *IndVars.back().second << "' " << *PN |
| 569 | << " into = " << *NewVal << "\n"; |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 570 | NewVal->takeName(PN); |
Chris Lattner | 5d461d2 | 2004-04-21 22:22:01 +0000 | [diff] [blame] | 571 | |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 572 | // Replace the old PHI Node with the inserted computation. |
Chris Lattner | fcb81f5 | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 573 | PN->replaceAllUsesWith(NewVal); |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 574 | DeadInsts.insert(PN); |
| 575 | IndVars.pop_back(); |
| 576 | ++NumRemoved; |
Chris Lattner | 4753bf2 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 577 | Changed = true; |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 578 | } |
| 579 | |
Chris Lattner | b4782d1 | 2004-04-22 15:12:36 +0000 | [diff] [blame] | 580 | #if 0 |
Chris Lattner | 1363e85 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 581 | // Now replace all derived expressions in the loop body with simpler |
| 582 | // expressions. |
Dan Gohman | 9b78763 | 2008-06-22 20:18:58 +0000 | [diff] [blame] | 583 | for (LoopInfo::block_iterator I = L->block_begin(), E = L->block_end(); |
| 584 | I != E; ++I) { |
| 585 | BasicBlock *BB = *I; |
| 586 | if (LI->getLoopFor(BB) == L) { // Not in a subloop... |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 587 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Chris Lattner | 42a7551 | 2007-01-15 02:27:26 +0000 | [diff] [blame] | 588 | if (I->getType()->isInteger() && // Is an integer instruction |
Chris Lattner | 1363e85 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 589 | !I->use_empty() && |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 590 | !Rewriter.isInsertedInstruction(I)) { |
| 591 | SCEVHandle SH = SE->getSCEV(I); |
Chris Lattner | 4a7553e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 592 | Value *V = Rewriter.expandCodeFor(SH, I, I->getType()); |
Chris Lattner | 1363e85 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 593 | if (V != I) { |
Chris Lattner | 6934a04 | 2007-02-11 01:23:03 +0000 | [diff] [blame] | 594 | if (isa<Instruction>(V)) |
| 595 | V->takeName(I); |
Chris Lattner | 1363e85 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 596 | I->replaceAllUsesWith(V); |
| 597 | DeadInsts.insert(I); |
| 598 | ++NumRemoved; |
| 599 | Changed = true; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 600 | } |
Chris Lattner | 40bf8b4 | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 601 | } |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 602 | } |
Dan Gohman | 9b78763 | 2008-06-22 20:18:58 +0000 | [diff] [blame] | 603 | } |
Chris Lattner | b4782d1 | 2004-04-22 15:12:36 +0000 | [diff] [blame] | 604 | #endif |
Chris Lattner | 1363e85 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 605 | |
Chris Lattner | 1363e85 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 606 | DeleteTriviallyDeadInstructions(DeadInsts); |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 607 | OptimizeCanonicalIVType(L); |
Chris Lattner | 9caed54 | 2007-03-04 01:00:28 +0000 | [diff] [blame] | 608 | assert(L->isLCSSAForm()); |
Devang Patel | 5ee9997 | 2007-03-07 06:39:01 +0000 | [diff] [blame] | 609 | return Changed; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 610 | } |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 611 | |
| 612 | /// OptimizeCanonicalIVType - If loop induction variable is always |
Devang Patel | 36a5bf8 | 2008-09-10 14:49:55 +0000 | [diff] [blame] | 613 | /// sign or zero extended then extend the type of the induction |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 614 | /// variable. |
| 615 | void IndVarSimplify::OptimizeCanonicalIVType(Loop *L) { |
| 616 | PHINode *PH = L->getCanonicalInductionVariable(); |
| 617 | if (!PH) return; |
| 618 | |
| 619 | // Check loop iteration count. |
| 620 | SCEVHandle IC = SE->getIterationCount(L); |
| 621 | if (isa<SCEVCouldNotCompute>(IC)) return; |
| 622 | SCEVConstant *IterationCount = dyn_cast<SCEVConstant>(IC); |
| 623 | if (!IterationCount) return; |
| 624 | |
| 625 | unsigned IncomingEdge = L->contains(PH->getIncomingBlock(0)); |
| 626 | unsigned BackEdge = IncomingEdge^1; |
| 627 | |
| 628 | // Check IV uses. If all IV uses are either SEXT or ZEXT (except |
| 629 | // IV increment instruction) then this IV is suitable for this |
Devang Patel | 36a5bf8 | 2008-09-10 14:49:55 +0000 | [diff] [blame] | 630 | // transformation. |
| 631 | bool isSEXT = false; |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 632 | BinaryOperator *Incr = NULL; |
Devang Patel | 36a5bf8 | 2008-09-10 14:49:55 +0000 | [diff] [blame] | 633 | const Type *NewType = NULL; |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 634 | for(Value::use_iterator UI = PH->use_begin(), UE = PH->use_end(); |
| 635 | UI != UE; ++UI) { |
| 636 | const Type *CandidateType = NULL; |
| 637 | if (ZExtInst *ZI = dyn_cast<ZExtInst>(UI)) |
| 638 | CandidateType = ZI->getDestTy(); |
| 639 | else if (SExtInst *SI = dyn_cast<SExtInst>(UI)) { |
| 640 | CandidateType = SI->getDestTy(); |
Devang Patel | 36a5bf8 | 2008-09-10 14:49:55 +0000 | [diff] [blame] | 641 | isSEXT = true; |
Devang Patel | d22a849 | 2008-09-09 21:41:07 +0000 | [diff] [blame] | 642 | } |
| 643 | else if ((Incr = dyn_cast<BinaryOperator>(UI))) { |
| 644 | // Validate IV increment instruction. |
| 645 | if (PH->getIncomingValue(BackEdge) == Incr) |
| 646 | continue; |
| 647 | } |
| 648 | if (!CandidateType) { |
| 649 | NewType = NULL; |
| 650 | break; |
| 651 | } |
| 652 | if (!NewType) |
| 653 | NewType = CandidateType; |
| 654 | else if (NewType != CandidateType) { |
| 655 | NewType = NULL; |
| 656 | break; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | // IV uses are not suitable then avoid this transformation. |
| 661 | if (!NewType || !Incr) |
| 662 | return; |
| 663 | |
| 664 | // IV increment instruction has two uses, one is loop exit condition |
| 665 | // and second is the IV (phi node) itself. |
| 666 | ICmpInst *Exit = NULL; |
| 667 | for(Value::use_iterator II = Incr->use_begin(), IE = Incr->use_end(); |
| 668 | II != IE; ++II) { |
| 669 | if (PH == *II) continue; |
| 670 | Exit = dyn_cast<ICmpInst>(*II); |
| 671 | break; |
| 672 | } |
| 673 | if (!Exit) return; |
| 674 | ConstantInt *EV = dyn_cast<ConstantInt>(Exit->getOperand(0)); |
| 675 | if (!EV) |
| 676 | EV = dyn_cast<ConstantInt>(Exit->getOperand(1)); |
| 677 | if (!EV) return; |
| 678 | |
| 679 | // Check iteration count max value to avoid loops that wrap around IV. |
| 680 | APInt ICount = IterationCount->getValue()->getValue(); |
| 681 | if (ICount.isNegative()) return; |
| 682 | uint32_t BW = PH->getType()->getPrimitiveSizeInBits(); |
| 683 | APInt Max = (isSEXT ? APInt::getSignedMaxValue(BW) : APInt::getMaxValue(BW)); |
| 684 | if (ICount.getZExtValue() > Max.getZExtValue()) return; |
| 685 | |
| 686 | // Extend IV type. |
| 687 | |
| 688 | SCEVExpander Rewriter(*SE, *LI); |
| 689 | Value *NewIV = Rewriter.getOrInsertCanonicalInductionVariable(L,NewType); |
| 690 | PHINode *NewPH = cast<PHINode>(NewIV); |
| 691 | Instruction *NewIncr = cast<Instruction>(NewPH->getIncomingValue(BackEdge)); |
| 692 | |
| 693 | // Replace all SEXT or ZEXT uses. |
| 694 | SmallVector<Instruction *, 4> PHUses; |
| 695 | for(Value::use_iterator UI = PH->use_begin(), UE = PH->use_end(); |
| 696 | UI != UE; ++UI) { |
| 697 | Instruction *I = cast<Instruction>(UI); |
| 698 | PHUses.push_back(I); |
| 699 | } |
| 700 | while (!PHUses.empty()){ |
| 701 | Instruction *Use = PHUses.back(); PHUses.pop_back(); |
| 702 | if (Incr == Use) continue; |
| 703 | |
| 704 | SE->deleteValueFromRecords(Use); |
| 705 | Use->replaceAllUsesWith(NewIV); |
| 706 | Use->eraseFromParent(); |
| 707 | } |
| 708 | |
| 709 | // Replace exit condition. |
| 710 | ConstantInt *NEV = ConstantInt::get(NewType, EV->getZExtValue()); |
| 711 | Instruction *NE = new ICmpInst(Exit->getPredicate(), |
| 712 | NewIncr, NEV, "new.exit", |
| 713 | Exit->getParent()->getTerminator()); |
| 714 | SE->deleteValueFromRecords(Exit); |
| 715 | Exit->replaceAllUsesWith(NE); |
| 716 | Exit->eraseFromParent(); |
| 717 | |
| 718 | // Remove old IV and increment instructions. |
| 719 | SE->deleteValueFromRecords(PH); |
| 720 | PH->removeIncomingValue((unsigned)0); |
| 721 | PH->removeIncomingValue((unsigned)0); |
| 722 | SE->deleteValueFromRecords(Incr); |
| 723 | Incr->eraseFromParent(); |
| 724 | } |
| 725 | |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 726 | /// HandleFloatingPointIV - If the loop has floating induction variable |
| 727 | /// then insert corresponding integer induction variable if possible. |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame^] | 728 | /// For example, |
| 729 | /// for(double i = 0; i < 10000; ++i) |
| 730 | /// bar(i) |
| 731 | /// is converted into |
| 732 | /// for(int i = 0; i < 10000; ++i) |
| 733 | /// bar((double)i); |
| 734 | /// |
| 735 | void IndVarSimplify::HandleFloatingPointIV(Loop *L, PHINode *PH, |
| 736 | SmallPtrSet<Instruction*, 16> &DeadInsts) { |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 737 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame^] | 738 | unsigned IncomingEdge = L->contains(PH->getIncomingBlock(0)); |
| 739 | unsigned BackEdge = IncomingEdge^1; |
| 740 | |
| 741 | // Check incoming value. |
| 742 | ConstantFP *CZ = dyn_cast<ConstantFP>(PH->getIncomingValue(IncomingEdge)); |
| 743 | if (!CZ) return; |
| 744 | APFloat PHInit = CZ->getValueAPF(); |
| 745 | if (!PHInit.isPosZero()) return; |
| 746 | |
| 747 | // Check IV increment. |
| 748 | BinaryOperator *Incr = |
| 749 | dyn_cast<BinaryOperator>(PH->getIncomingValue(BackEdge)); |
| 750 | if (!Incr) return; |
| 751 | if (Incr->getOpcode() != Instruction::Add) return; |
| 752 | ConstantFP *IncrValue = NULL; |
| 753 | unsigned IncrVIndex = 1; |
| 754 | if (Incr->getOperand(1) == PH) |
| 755 | IncrVIndex = 0; |
| 756 | IncrValue = dyn_cast<ConstantFP>(Incr->getOperand(IncrVIndex)); |
| 757 | if (!IncrValue) return; |
| 758 | APFloat IVAPF = IncrValue->getValueAPF(); |
| 759 | APFloat One = APFloat(IVAPF.getSemantics(), 1); |
| 760 | if (!IVAPF.bitwiseIsEqual(One)) return; |
| 761 | |
| 762 | // Check Incr uses. |
| 763 | Value::use_iterator IncrUse = Incr->use_begin(); |
| 764 | Instruction *U1 = cast<Instruction>(IncrUse++); |
| 765 | if (IncrUse == Incr->use_end()) return; |
| 766 | Instruction *U2 = cast<Instruction>(IncrUse++); |
| 767 | if (IncrUse != Incr->use_end()) return; |
| 768 | |
| 769 | // Find exit condition. |
| 770 | FCmpInst *EC = dyn_cast<FCmpInst>(U1); |
| 771 | if (!EC) |
| 772 | EC = dyn_cast<FCmpInst>(U2); |
| 773 | if (!EC) return; |
| 774 | |
| 775 | if (BranchInst *BI = dyn_cast<BranchInst>(EC->getParent()->getTerminator())) { |
| 776 | if (!BI->isConditional()) return; |
| 777 | if (BI->getCondition() != EC) return; |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 778 | } |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 779 | |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame^] | 780 | // Find exit value. |
| 781 | ConstantFP *EV = NULL; |
| 782 | unsigned EVIndex = 1; |
| 783 | if (EC->getOperand(1) == Incr) |
| 784 | EVIndex = 0; |
| 785 | EV = dyn_cast<ConstantFP>(EC->getOperand(EVIndex)); |
| 786 | if (!EV) return; |
| 787 | APFloat EVAPF = EV->getValueAPF(); |
| 788 | if (EVAPF.isNegative()) return; |
| 789 | |
| 790 | // Find corresponding integer exit value. |
| 791 | uint64_t intEV = Type::Int32Ty->getPrimitiveSizeInBits(); |
| 792 | bool isExact = false; |
| 793 | if (EVAPF.convertToInteger(&intEV, 32, false, APFloat::rmTowardZero, &isExact) |
| 794 | != APFloat::opOK) |
| 795 | return; |
| 796 | if (!isExact) return; |
| 797 | |
| 798 | // Find new predicate for integer comparison. |
| 799 | CmpInst::Predicate NewPred = CmpInst::BAD_ICMP_PREDICATE; |
| 800 | switch (EC->getPredicate()) { |
| 801 | case CmpInst::FCMP_OEQ: |
| 802 | case CmpInst::FCMP_UEQ: |
| 803 | NewPred = CmpInst::ICMP_EQ; |
| 804 | break; |
| 805 | case CmpInst::FCMP_OGT: |
| 806 | case CmpInst::FCMP_UGT: |
| 807 | NewPred = CmpInst::ICMP_UGT; |
| 808 | break; |
| 809 | case CmpInst::FCMP_OGE: |
| 810 | case CmpInst::FCMP_UGE: |
| 811 | NewPred = CmpInst::ICMP_UGE; |
| 812 | break; |
| 813 | case CmpInst::FCMP_OLT: |
| 814 | case CmpInst::FCMP_ULT: |
| 815 | NewPred = CmpInst::ICMP_ULT; |
| 816 | break; |
| 817 | case CmpInst::FCMP_OLE: |
| 818 | case CmpInst::FCMP_ULE: |
| 819 | NewPred = CmpInst::ICMP_ULE; |
| 820 | break; |
| 821 | default: |
| 822 | break; |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 823 | } |
Devang Patel | 84e3515 | 2008-11-17 21:32:02 +0000 | [diff] [blame^] | 824 | if (NewPred == CmpInst::BAD_ICMP_PREDICATE) return; |
| 825 | |
| 826 | // Insert new integer induction variable. |
| 827 | PHINode *NewPHI = PHINode::Create(Type::Int32Ty, |
| 828 | PH->getName()+".int", PH); |
| 829 | NewPHI->addIncoming(Constant::getNullValue(NewPHI->getType()), |
| 830 | PH->getIncomingBlock(IncomingEdge)); |
| 831 | |
| 832 | Value *NewAdd = BinaryOperator::CreateAdd(NewPHI, |
| 833 | ConstantInt::get(Type::Int32Ty, 1), |
| 834 | Incr->getName()+".int", Incr); |
| 835 | NewPHI->addIncoming(NewAdd, PH->getIncomingBlock(BackEdge)); |
| 836 | |
| 837 | ConstantInt *NewEV = ConstantInt::get(Type::Int32Ty, intEV); |
| 838 | Value *LHS = (EVIndex == 1 ? NewPHI->getIncomingValue(BackEdge) : NewEV); |
| 839 | Value *RHS = (EVIndex == 1 ? NewEV : NewPHI->getIncomingValue(BackEdge)); |
| 840 | ICmpInst *NewEC = new ICmpInst(NewPred, LHS, RHS, EC->getNameStart(), |
| 841 | EC->getParent()->getTerminator()); |
| 842 | |
| 843 | // Delete old, floating point, exit comparision instruction. |
| 844 | EC->replaceAllUsesWith(NewEC); |
| 845 | DeadInsts.insert(EC); |
| 846 | |
| 847 | // Delete old, floating point, increment instruction. |
| 848 | Incr->replaceAllUsesWith(UndefValue::get(Incr->getType())); |
| 849 | DeadInsts.insert(Incr); |
| 850 | |
| 851 | // Replace floating induction variable. |
| 852 | UIToFPInst *Conv = new UIToFPInst(NewPHI, PH->getType(), "indvar.conv", |
| 853 | PH->getParent()->getFirstNonPHI()); |
| 854 | PH->replaceAllUsesWith(Conv); |
| 855 | DeadInsts.insert(PH); |
Devang Patel | 58d43d4 | 2008-11-03 18:32:19 +0000 | [diff] [blame] | 856 | } |
| 857 | |