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