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