Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 1 | //===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | 482202a | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | e61b67d | 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 | 5495fe8 | 2006-08-18 09:01:07 +0000 | [diff] [blame] | 14 | // This transformation makes the following changes to each loop with an |
Chris Lattner | e61b67d | 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 | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 37 | // |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 40 | #define DEBUG_TYPE "indvars" |
Chris Lattner | b4cfa7f | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 41 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 42 | #include "llvm/BasicBlock.h" |
Chris Lattner | 0cec5cb | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 43 | #include "llvm/Constants.h" |
Chris Lattner | 6449dce | 2003-12-22 05:02:01 +0000 | [diff] [blame] | 44 | #include "llvm/Instructions.h" |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 45 | #include "llvm/Type.h" |
Nate Begeman | 2bca4d9 | 2005-07-30 00:12:19 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/ScalarEvolutionExpander.h" |
John Criswell | b22e9b4 | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 47 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | 83d485b | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 48 | #include "llvm/Support/CFG.h" |
Chris Lattner | 0816559 | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 49 | #include "llvm/Support/Debug.h" |
Chris Lattner | 9776f72 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 50 | #include "llvm/Support/GetElementPtrTypeIterator.h" |
John Criswell | b22e9b4 | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 51 | #include "llvm/Transforms/Utils/Local.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 52 | #include "llvm/Support/CommandLine.h" |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 53 | #include "llvm/ADT/SmallVector.h" |
Reid Spencer | 7c16caa | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/Statistic.h" |
John Criswell | b22e9b4 | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 55 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 56 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 57 | STATISTIC(NumRemoved , "Number of aux indvars removed"); |
| 58 | STATISTIC(NumPointer , "Number of pointer indvars promoted"); |
| 59 | STATISTIC(NumInserted, "Number of canonical indvars added"); |
| 60 | STATISTIC(NumReplaced, "Number of exit values replaced"); |
| 61 | STATISTIC(NumLFTR , "Number of loop exit tests replaced"); |
Chris Lattner | d3678bc | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 62 | |
Chris Lattner | 79a42ac | 2006-12-19 21:40:18 +0000 | [diff] [blame] | 63 | namespace { |
Chris Lattner | d3678bc | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 64 | class IndVarSimplify : public FunctionPass { |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 65 | LoopInfo *LI; |
| 66 | ScalarEvolution *SE; |
Chris Lattner | 7e755e4 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 67 | bool Changed; |
Chris Lattner | d3678bc | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 68 | public: |
| 69 | virtual bool runOnFunction(Function &) { |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 70 | LI = &getAnalysis<LoopInfo>(); |
| 71 | SE = &getAnalysis<ScalarEvolution>(); |
Chris Lattner | 7e755e4 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 72 | Changed = false; |
| 73 | |
Chris Lattner | d3678bc | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 74 | // Induction Variables live in the header nodes of loops |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 75 | for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) |
Chris Lattner | 59d2d7f | 2004-01-08 00:09:44 +0000 | [diff] [blame] | 76 | runOnLoop(*I); |
Chris Lattner | d3678bc | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 77 | return Changed; |
| 78 | } |
| 79 | |
Chris Lattner | d3678bc | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 80 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | d3678bc | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 81 | AU.addRequiredID(LoopSimplifyID); |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 82 | AU.addRequired<ScalarEvolution>(); |
| 83 | AU.addRequired<LoopInfo>(); |
Chris Lattner | d3678bc | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 84 | AU.addPreservedID(LoopSimplifyID); |
Owen Anderson | 8cca95c | 2006-08-25 17:41:25 +0000 | [diff] [blame] | 85 | AU.addPreservedID(LCSSAID); |
Chris Lattner | d3678bc | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 86 | AU.setPreservesCFG(); |
| 87 | } |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 88 | private: |
| 89 | void runOnLoop(Loop *L); |
| 90 | void EliminatePointerRecurrence(PHINode *PN, BasicBlock *Preheader, |
| 91 | std::set<Instruction*> &DeadInsts); |
Chris Lattner | 51c95cd | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 92 | Instruction *LinearFunctionTestReplace(Loop *L, SCEV *IterationCount, |
| 93 | SCEVExpander &RW); |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 94 | void RewriteLoopExitValues(Loop *L); |
| 95 | |
| 96 | void DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts); |
Chris Lattner | d3678bc | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 97 | }; |
Chris Lattner | c2d3d31 | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 98 | RegisterPass<IndVarSimplify> X("indvars", "Canonicalize Induction Variables"); |
Chris Lattner | 4184bcc | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 99 | } |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 3e86084 | 2004-09-20 04:43:15 +0000 | [diff] [blame] | 101 | FunctionPass *llvm::createIndVarSimplifyPass() { |
Chris Lattner | d3678bc | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 102 | return new IndVarSimplify(); |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 105 | /// DeleteTriviallyDeadInstructions - If any of the instructions is the |
| 106 | /// specified set are trivially dead, delete them and see if this makes any of |
| 107 | /// their operands subsequently dead. |
| 108 | void IndVarSimplify:: |
| 109 | DeleteTriviallyDeadInstructions(std::set<Instruction*> &Insts) { |
| 110 | while (!Insts.empty()) { |
| 111 | Instruction *I = *Insts.begin(); |
| 112 | Insts.erase(Insts.begin()); |
| 113 | if (isInstructionTriviallyDead(I)) { |
| 114 | for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) |
| 115 | if (Instruction *U = dyn_cast<Instruction>(I->getOperand(i))) |
| 116 | Insts.insert(U); |
| 117 | SE->deleteInstructionFromRecords(I); |
Chris Lattner | 0816559 | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 118 | DOUT << "INDVARS: Deleting: " << *I; |
Chris Lattner | 9776f72 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 119 | I->eraseFromParent(); |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 120 | Changed = true; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | /// EliminatePointerRecurrence - Check to see if this is a trivial GEP pointer |
| 127 | /// recurrence. If so, change it into an integer recurrence, permitting |
| 128 | /// analysis by the SCEV routines. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 129 | void IndVarSimplify::EliminatePointerRecurrence(PHINode *PN, |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 130 | BasicBlock *Preheader, |
| 131 | std::set<Instruction*> &DeadInsts) { |
| 132 | assert(PN->getNumIncomingValues() == 2 && "Noncanonicalized loop!"); |
| 133 | unsigned PreheaderIdx = PN->getBasicBlockIndex(Preheader); |
| 134 | unsigned BackedgeIdx = PreheaderIdx^1; |
| 135 | if (GetElementPtrInst *GEPI = |
Chris Lattner | 677d857 | 2005-08-10 01:12:06 +0000 | [diff] [blame] | 136 | dyn_cast<GetElementPtrInst>(PN->getIncomingValue(BackedgeIdx))) |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 137 | if (GEPI->getOperand(0) == PN) { |
Chris Lattner | 677d857 | 2005-08-10 01:12:06 +0000 | [diff] [blame] | 138 | assert(GEPI->getNumOperands() == 2 && "GEP types must match!"); |
Chris Lattner | 0816559 | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 139 | DOUT << "INDVARS: Eliminating pointer recurrence: " << *GEPI; |
| 140 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 141 | // Okay, we found a pointer recurrence. Transform this pointer |
| 142 | // recurrence into an integer recurrence. Compute the value that gets |
| 143 | // added to the pointer at every iteration. |
| 144 | Value *AddedVal = GEPI->getOperand(1); |
| 145 | |
| 146 | // Insert a new integer PHI node into the top of the block. |
| 147 | PHINode *NewPhi = new PHINode(AddedVal->getType(), |
| 148 | PN->getName()+".rec", PN); |
Chris Lattner | c9e0633 | 2004-06-20 05:04:01 +0000 | [diff] [blame] | 149 | NewPhi->addIncoming(Constant::getNullValue(NewPhi->getType()), Preheader); |
| 150 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 151 | // Create the new add instruction. |
Chris Lattner | c9e0633 | 2004-06-20 05:04:01 +0000 | [diff] [blame] | 152 | Value *NewAdd = BinaryOperator::createAdd(NewPhi, AddedVal, |
| 153 | GEPI->getName()+".rec", GEPI); |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 154 | NewPhi->addIncoming(NewAdd, PN->getIncomingBlock(BackedgeIdx)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 155 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 156 | // Update the existing GEP to use the recurrence. |
| 157 | GEPI->setOperand(0, PN->getIncomingValue(PreheaderIdx)); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 158 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 159 | // Update the GEP to use the new recurrence we just inserted. |
| 160 | GEPI->setOperand(1, NewAdd); |
| 161 | |
Chris Lattner | 9776f72 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 162 | // If the incoming value is a constant expr GEP, try peeling out the array |
| 163 | // 0 index if possible to make things simpler. |
| 164 | if (ConstantExpr *CE = dyn_cast<ConstantExpr>(GEPI->getOperand(0))) |
| 165 | if (CE->getOpcode() == Instruction::GetElementPtr) { |
| 166 | unsigned NumOps = CE->getNumOperands(); |
| 167 | assert(NumOps > 1 && "CE folding didn't work!"); |
| 168 | if (CE->getOperand(NumOps-1)->isNullValue()) { |
| 169 | // Check to make sure the last index really is an array index. |
Chris Lattner | 9c37f23 | 2005-11-18 18:30:47 +0000 | [diff] [blame] | 170 | gep_type_iterator GTI = gep_type_begin(CE); |
Chris Lattner | bca0be8 | 2005-11-17 19:35:42 +0000 | [diff] [blame] | 171 | for (unsigned i = 1, e = CE->getNumOperands()-1; |
Chris Lattner | 9776f72 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 172 | i != e; ++i, ++GTI) |
| 173 | /*empty*/; |
| 174 | if (isa<SequentialType>(*GTI)) { |
| 175 | // Pull the last index out of the constant expr GEP. |
| 176 | std::vector<Value*> CEIdxs(CE->op_begin()+1, CE->op_end()-1); |
| 177 | Constant *NCE = ConstantExpr::getGetElementPtr(CE->getOperand(0), |
| 178 | CEIdxs); |
| 179 | GetElementPtrInst *NGEPI = |
Reid Spencer | c635f47 | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 180 | new GetElementPtrInst(NCE, Constant::getNullValue(Type::Int32Ty), |
Chris Lattner | 9776f72 | 2004-10-11 23:06:50 +0000 | [diff] [blame] | 181 | NewAdd, GEPI->getName(), GEPI); |
| 182 | GEPI->replaceAllUsesWith(NGEPI); |
| 183 | GEPI->eraseFromParent(); |
| 184 | GEPI = NGEPI; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 190 | // Finally, if there are any other users of the PHI node, we must |
| 191 | // insert a new GEP instruction that uses the pre-incremented version |
| 192 | // of the induction amount. |
| 193 | if (!PN->use_empty()) { |
| 194 | BasicBlock::iterator InsertPos = PN; ++InsertPos; |
| 195 | while (isa<PHINode>(InsertPos)) ++InsertPos; |
| 196 | std::string Name = PN->getName(); PN->setName(""); |
| 197 | Value *PreInc = |
| 198 | new GetElementPtrInst(PN->getIncomingValue(PreheaderIdx), |
| 199 | std::vector<Value*>(1, NewPhi), Name, |
| 200 | InsertPos); |
| 201 | PN->replaceAllUsesWith(PreInc); |
| 202 | } |
| 203 | |
| 204 | // Delete the old PHI for sure, and the GEP if its otherwise unused. |
| 205 | DeadInsts.insert(PN); |
| 206 | |
| 207 | ++NumPointer; |
| 208 | Changed = true; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /// LinearFunctionTestReplace - This method rewrites the exit condition of the |
Chris Lattner | 0cec5cb | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 213 | /// loop to be a canonical != comparison against the incremented loop induction |
| 214 | /// variable. This pass is able to rewrite the exit tests of any loop where the |
| 215 | /// SCEV analysis can determine a loop-invariant trip count of the loop, which |
| 216 | /// is actually a much broader range than just linear tests. |
Chris Lattner | 51c95cd | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 217 | /// |
| 218 | /// This method returns a "potentially dead" instruction whose computation chain |
| 219 | /// should be deleted when convenient. |
| 220 | Instruction *IndVarSimplify::LinearFunctionTestReplace(Loop *L, |
| 221 | SCEV *IterationCount, |
| 222 | SCEVExpander &RW) { |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 223 | // Find the exit block for the loop. We can currently only handle loops with |
| 224 | // a single exit. |
Chris Lattner | d72c3eb | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 225 | std::vector<BasicBlock*> ExitBlocks; |
| 226 | L->getExitBlocks(ExitBlocks); |
Chris Lattner | 51c95cd | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 227 | if (ExitBlocks.size() != 1) return 0; |
Chris Lattner | d72c3eb | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 228 | BasicBlock *ExitBlock = ExitBlocks[0]; |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 229 | |
| 230 | // Make sure there is only one predecessor block in the loop. |
| 231 | BasicBlock *ExitingBlock = 0; |
| 232 | for (pred_iterator PI = pred_begin(ExitBlock), PE = pred_end(ExitBlock); |
| 233 | PI != PE; ++PI) |
| 234 | if (L->contains(*PI)) { |
| 235 | if (ExitingBlock == 0) |
| 236 | ExitingBlock = *PI; |
| 237 | else |
Chris Lattner | 51c95cd | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 238 | return 0; // Multiple exits from loop to this block. |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 239 | } |
| 240 | assert(ExitingBlock && "Loop info is broken"); |
| 241 | |
| 242 | if (!isa<BranchInst>(ExitingBlock->getTerminator())) |
Chris Lattner | 51c95cd | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 243 | return 0; // Can't rewrite non-branch yet |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 244 | BranchInst *BI = cast<BranchInst>(ExitingBlock->getTerminator()); |
| 245 | assert(BI->isConditional() && "Must be conditional to be part of loop!"); |
| 246 | |
Chris Lattner | 51c95cd | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 247 | Instruction *PotentiallyDeadInst = dyn_cast<Instruction>(BI->getCondition()); |
| 248 | |
Chris Lattner | d7a559e | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 249 | // If the exiting block is not the same as the backedge block, we must compare |
| 250 | // against the preincremented value, otherwise we prefer to compare against |
| 251 | // the post-incremented value. |
| 252 | BasicBlock *Header = L->getHeader(); |
| 253 | pred_iterator HPI = pred_begin(Header); |
| 254 | assert(HPI != pred_end(Header) && "Loop with zero preds???"); |
| 255 | if (!L->contains(*HPI)) ++HPI; |
| 256 | assert(HPI != pred_end(Header) && L->contains(*HPI) && |
| 257 | "No backedge in loop?"); |
Chris Lattner | 0cec5cb | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 258 | |
Chris Lattner | d7a559e | 2004-04-15 20:26:22 +0000 | [diff] [blame] | 259 | SCEVHandle TripCount = IterationCount; |
| 260 | Value *IndVar; |
| 261 | if (*HPI == ExitingBlock) { |
| 262 | // The IterationCount expression contains the number of times that the |
| 263 | // backedge actually branches to the loop header. This is one less than the |
| 264 | // number of times the loop executes, so add one to it. |
| 265 | Constant *OneC = ConstantInt::get(IterationCount->getType(), 1); |
| 266 | TripCount = SCEVAddExpr::get(IterationCount, SCEVUnknown::get(OneC)); |
| 267 | IndVar = L->getCanonicalInductionVariableIncrement(); |
| 268 | } else { |
| 269 | // We have to use the preincremented value... |
| 270 | IndVar = L->getCanonicalInductionVariable(); |
| 271 | } |
Chris Lattner | 0816559 | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 272 | |
| 273 | DOUT << "INDVARS: LFTR: TripCount = " << *TripCount |
| 274 | << " IndVar = " << *IndVar << "\n"; |
Chris Lattner | 0cec5cb | 2004-04-15 15:21:43 +0000 | [diff] [blame] | 275 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 276 | // Expand the code for the iteration count into the preheader of the loop. |
| 277 | BasicBlock *Preheader = L->getLoopPreheader(); |
Chris Lattner | 83cd87e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 278 | Value *ExitCnt = RW.expandCodeFor(TripCount, Preheader->getTerminator(), |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 279 | IndVar->getType()); |
| 280 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 281 | // Insert a new icmp_ne or icmp_eq instruction before the branch. |
| 282 | ICmpInst::Predicate Opcode; |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 283 | if (L->contains(BI->getSuccessor(0))) |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 284 | Opcode = ICmpInst::ICMP_NE; |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 285 | else |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 286 | Opcode = ICmpInst::ICMP_EQ; |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 287 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 288 | Value *Cond = new ICmpInst(Opcode, IndVar, ExitCnt, "exitcond", BI); |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 289 | BI->setCondition(Cond); |
| 290 | ++NumLFTR; |
| 291 | Changed = true; |
Chris Lattner | 51c95cd | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 292 | return PotentiallyDeadInst; |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | |
| 296 | /// RewriteLoopExitValues - Check to see if this loop has a computable |
| 297 | /// loop-invariant execution count. If so, this means that we can compute the |
| 298 | /// final value of any expressions that are recurrent in the loop, and |
| 299 | /// substitute the exit values from the loop into any instructions outside of |
| 300 | /// the loop that use the final values of the current expressions. |
| 301 | void IndVarSimplify::RewriteLoopExitValues(Loop *L) { |
| 302 | BasicBlock *Preheader = L->getLoopPreheader(); |
| 303 | |
| 304 | // Scan all of the instructions in the loop, looking at those that have |
| 305 | // extra-loop users and which are recurrences. |
Chris Lattner | 83cd87e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 306 | SCEVExpander Rewriter(*SE, *LI); |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 307 | |
| 308 | // We insert the code into the preheader of the loop if the loop contains |
| 309 | // multiple exit blocks, or in the exit block if there is exactly one. |
| 310 | BasicBlock *BlockToInsertInto; |
Chris Lattner | d72c3eb | 2004-04-18 22:14:10 +0000 | [diff] [blame] | 311 | std::vector<BasicBlock*> ExitBlocks; |
| 312 | L->getExitBlocks(ExitBlocks); |
| 313 | if (ExitBlocks.size() == 1) |
| 314 | BlockToInsertInto = ExitBlocks[0]; |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 315 | else |
| 316 | BlockToInsertInto = Preheader; |
| 317 | BasicBlock::iterator InsertPt = BlockToInsertInto->begin(); |
| 318 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
| 319 | |
Chris Lattner | a814080 | 2004-04-17 18:44:09 +0000 | [diff] [blame] | 320 | bool HasConstantItCount = isa<SCEVConstant>(SE->getIterationCount(L)); |
| 321 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 322 | std::set<Instruction*> InstructionsToDelete; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 323 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 324 | for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) |
| 325 | if (LI->getLoopFor(L->getBlocks()[i]) == L) { // Not in a subloop... |
| 326 | BasicBlock *BB = L->getBlocks()[i]; |
Chris Lattner | df81539 | 2005-06-15 21:29:31 +0000 | [diff] [blame] | 327 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;) { |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame^] | 328 | if (I->getType()->isInteger()) { // Is an integer instruction |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 329 | SCEVHandle SH = SE->getSCEV(I); |
Chris Lattner | a814080 | 2004-04-17 18:44:09 +0000 | [diff] [blame] | 330 | if (SH->hasComputableLoopEvolution(L) || // Varies predictably |
| 331 | HasConstantItCount) { |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 332 | // Find out if this predictably varying value is actually used |
| 333 | // outside of the loop. "extra" as opposed to "intra". |
Chris Lattner | 053fb93 | 2006-06-17 01:02:31 +0000 | [diff] [blame] | 334 | std::vector<Instruction*> ExtraLoopUsers; |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 335 | for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); |
Chris Lattner | 053fb93 | 2006-06-17 01:02:31 +0000 | [diff] [blame] | 336 | UI != E; ++UI) { |
| 337 | Instruction *User = cast<Instruction>(*UI); |
Chris Lattner | 9b6c02e | 2006-07-13 19:05:20 +0000 | [diff] [blame] | 338 | if (!L->contains(User->getParent())) { |
| 339 | // If this is a PHI node in the exit block and we're inserting, |
| 340 | // into the exit block, it must have a single entry. In this |
| 341 | // case, we can't insert the code after the PHI and have the PHI |
| 342 | // still use it. Instead, don't insert the the PHI. |
| 343 | if (PHINode *PN = dyn_cast<PHINode>(User)) { |
| 344 | // FIXME: This is a case where LCSSA pessimizes code, this |
| 345 | // should be fixed better. |
| 346 | if (PN->getNumOperands() == 2 && |
| 347 | PN->getParent() == BlockToInsertInto) |
| 348 | continue; |
| 349 | } |
Chris Lattner | 053fb93 | 2006-06-17 01:02:31 +0000 | [diff] [blame] | 350 | ExtraLoopUsers.push_back(User); |
Chris Lattner | 9b6c02e | 2006-07-13 19:05:20 +0000 | [diff] [blame] | 351 | } |
Chris Lattner | 053fb93 | 2006-06-17 01:02:31 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 354 | if (!ExtraLoopUsers.empty()) { |
| 355 | // Okay, this instruction has a user outside of the current loop |
| 356 | // and varies predictably in this loop. Evaluate the value it |
| 357 | // contains when the loop exits, and insert code for it. |
Chris Lattner | a814080 | 2004-04-17 18:44:09 +0000 | [diff] [blame] | 358 | SCEVHandle ExitValue = SE->getSCEVAtScope(I, L->getParentLoop()); |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 359 | if (!isa<SCEVCouldNotCompute>(ExitValue)) { |
| 360 | Changed = true; |
| 361 | ++NumReplaced; |
Chris Lattner | df81539 | 2005-06-15 21:29:31 +0000 | [diff] [blame] | 362 | // Remember the next instruction. The rewriter can move code |
| 363 | // around in some cases. |
| 364 | BasicBlock::iterator NextI = I; ++NextI; |
| 365 | |
Chris Lattner | 83cd87e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 366 | Value *NewVal = Rewriter.expandCodeFor(ExitValue, InsertPt, |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 367 | I->getType()); |
| 368 | |
Chris Lattner | 0816559 | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 369 | DOUT << "INDVARS: RLEV: AfterLoopVal = " << *NewVal |
| 370 | << " LoopVal = " << *I << "\n"; |
| 371 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 372 | // Rewrite any users of the computed value outside of the loop |
| 373 | // with the newly computed value. |
Owen Anderson | bea70ee | 2006-07-14 18:49:15 +0000 | [diff] [blame] | 374 | for (unsigned i = 0, e = ExtraLoopUsers.size(); i != e; ++i) { |
| 375 | PHINode* PN = dyn_cast<PHINode>(ExtraLoopUsers[i]); |
| 376 | if (PN && PN->getNumOperands() == 2 && |
| 377 | !L->contains(PN->getParent())) { |
| 378 | // We're dealing with an LCSSA Phi. Handle it specially. |
| 379 | Instruction* LCSSAInsertPt = BlockToInsertInto->begin(); |
| 380 | |
| 381 | Instruction* NewInstr = dyn_cast<Instruction>(NewVal); |
| 382 | if (NewInstr && !isa<PHINode>(NewInstr) && |
| 383 | !L->contains(NewInstr->getParent())) |
| 384 | for (unsigned j = 0; j < NewInstr->getNumOperands(); ++j){ |
| 385 | Instruction* PredI = |
| 386 | dyn_cast<Instruction>(NewInstr->getOperand(j)); |
| 387 | if (PredI && L->contains(PredI->getParent())) { |
| 388 | PHINode* NewLCSSA = new PHINode(PredI->getType(), |
| 389 | PredI->getName() + ".lcssa", |
| 390 | LCSSAInsertPt); |
| 391 | NewLCSSA->addIncoming(PredI, |
| 392 | BlockToInsertInto->getSinglePredecessor()); |
| 393 | |
| 394 | NewInstr->replaceUsesOfWith(PredI, NewLCSSA); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | PN->replaceAllUsesWith(NewVal); |
| 399 | PN->eraseFromParent(); |
| 400 | } else { |
| 401 | ExtraLoopUsers[i]->replaceUsesOfWith(I, NewVal); |
| 402 | } |
| 403 | } |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 404 | |
| 405 | // If this instruction is dead now, schedule it to be removed. |
| 406 | if (I->use_empty()) |
| 407 | InstructionsToDelete.insert(I); |
Chris Lattner | df81539 | 2005-06-15 21:29:31 +0000 | [diff] [blame] | 408 | I = NextI; |
| 409 | continue; // Skip the ++I |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | } |
| 413 | } |
Chris Lattner | df81539 | 2005-06-15 21:29:31 +0000 | [diff] [blame] | 414 | |
| 415 | // Next instruction. Continue instruction skips this. |
| 416 | ++I; |
| 417 | } |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | DeleteTriviallyDeadInstructions(InstructionsToDelete); |
| 421 | } |
| 422 | |
| 423 | |
| 424 | void IndVarSimplify::runOnLoop(Loop *L) { |
| 425 | // First step. Check to see if there are any trivial GEP pointer recurrences. |
| 426 | // If there are, change them into integer recurrences, permitting analysis by |
| 427 | // the SCEV routines. |
| 428 | // |
| 429 | BasicBlock *Header = L->getHeader(); |
| 430 | BasicBlock *Preheader = L->getLoopPreheader(); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 431 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 432 | std::set<Instruction*> DeadInsts; |
Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 433 | for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { |
| 434 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 435 | if (isa<PointerType>(PN->getType())) |
| 436 | EliminatePointerRecurrence(PN, Preheader, DeadInsts); |
Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 437 | } |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 438 | |
| 439 | if (!DeadInsts.empty()) |
| 440 | DeleteTriviallyDeadInstructions(DeadInsts); |
| 441 | |
| 442 | |
| 443 | // Next, transform all loops nesting inside of this loop. |
| 444 | for (LoopInfo::iterator I = L->begin(), E = L->end(); I != E; ++I) |
Chris Lattner | 59d2d7f | 2004-01-08 00:09:44 +0000 | [diff] [blame] | 445 | runOnLoop(*I); |
Chris Lattner | d3678bc | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 446 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 447 | // Check to see if this loop has a computable loop-invariant execution count. |
| 448 | // If so, this means that we can compute the final value of any expressions |
| 449 | // that are recurrent in the loop, and substitute the exit values from the |
| 450 | // loop into any instructions outside of the loop that use the final values of |
| 451 | // the current expressions. |
Chris Lattner | 0b18c1d | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 452 | // |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 453 | SCEVHandle IterationCount = SE->getIterationCount(L); |
| 454 | if (!isa<SCEVCouldNotCompute>(IterationCount)) |
| 455 | RewriteLoopExitValues(L); |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 456 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 457 | // Next, analyze all of the induction variables in the loop, canonicalizing |
| 458 | // auxillary induction variables. |
| 459 | std::vector<std::pair<PHINode*, SCEVHandle> > IndVars; |
| 460 | |
Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 461 | for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { |
| 462 | PHINode *PN = cast<PHINode>(I); |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame^] | 463 | if (PN->getType()->isInteger()) { // FIXME: when we have fast-math, enable! |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 464 | SCEVHandle SCEV = SE->getSCEV(PN); |
| 465 | if (SCEV->hasComputableLoopEvolution(L)) |
Chris Lattner | 677d857 | 2005-08-10 01:12:06 +0000 | [diff] [blame] | 466 | // FIXME: It is an extremely bad idea to indvar substitute anything more |
| 467 | // complex than affine induction variables. Doing so will put expensive |
| 468 | // polynomial evaluations inside of the loop, and the str reduction pass |
| 469 | // currently can only reduce affine polynomials. For now just disable |
| 470 | // indvar subst on anything more complex than an affine addrec. |
Chris Lattner | e5ad26d | 2004-07-26 02:47:12 +0000 | [diff] [blame] | 471 | if (SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(SCEV)) |
Chris Lattner | 677d857 | 2005-08-10 01:12:06 +0000 | [diff] [blame] | 472 | if (AR->isAffine()) |
Chris Lattner | e5ad26d | 2004-07-26 02:47:12 +0000 | [diff] [blame] | 473 | IndVars.push_back(std::make_pair(PN, SCEV)); |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 474 | } |
Reid Spencer | 6614946 | 2004-09-15 17:06:42 +0000 | [diff] [blame] | 475 | } |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 476 | |
| 477 | // If there are no induction variables in the loop, there is nothing more to |
| 478 | // do. |
Chris Lattner | 885a6eb | 2004-04-17 18:08:33 +0000 | [diff] [blame] | 479 | if (IndVars.empty()) { |
| 480 | // Actually, if we know how many times the loop iterates, lets insert a |
| 481 | // canonical induction variable to help subsequent passes. |
| 482 | if (!isa<SCEVCouldNotCompute>(IterationCount)) { |
Chris Lattner | 83cd87e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 483 | SCEVExpander Rewriter(*SE, *LI); |
| 484 | Rewriter.getOrInsertCanonicalInductionVariable(L, |
Chris Lattner | 885a6eb | 2004-04-17 18:08:33 +0000 | [diff] [blame] | 485 | IterationCount->getType()); |
Chris Lattner | 51c95cd | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 486 | if (Instruction *I = LinearFunctionTestReplace(L, IterationCount, |
| 487 | Rewriter)) { |
| 488 | std::set<Instruction*> InstructionsToDelete; |
| 489 | InstructionsToDelete.insert(I); |
| 490 | DeleteTriviallyDeadInstructions(InstructionsToDelete); |
| 491 | } |
Chris Lattner | 885a6eb | 2004-04-17 18:08:33 +0000 | [diff] [blame] | 492 | } |
| 493 | return; |
| 494 | } |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 495 | |
| 496 | // Compute the type of the largest recurrence expression. |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 497 | // |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 498 | const Type *LargestType = IndVars[0].first->getType(); |
Chris Lattner | c1a682d | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 499 | bool DifferingSizes = false; |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 500 | for (unsigned i = 1, e = IndVars.size(); i != e; ++i) { |
| 501 | const Type *Ty = IndVars[i].first->getType(); |
Reid Spencer | 8f166b0 | 2007-01-08 16:32:00 +0000 | [diff] [blame] | 502 | DifferingSizes |= |
| 503 | Ty->getPrimitiveSizeInBits() != LargestType->getPrimitiveSizeInBits(); |
| 504 | if (Ty->getPrimitiveSizeInBits() > LargestType->getPrimitiveSizeInBits()) |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 505 | LargestType = Ty; |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 506 | } |
| 507 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 508 | // Create a rewriter object which we'll use to transform the code with. |
Chris Lattner | 83cd87e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 509 | SCEVExpander Rewriter(*SE, *LI); |
Chris Lattner | 7e755e4 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 510 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 511 | // Now that we know the largest of of the induction variables in this loop, |
| 512 | // insert a canonical induction variable of the largest size. |
Chris Lattner | 83cd87e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 513 | Value *IndVar = Rewriter.getOrInsertCanonicalInductionVariable(L,LargestType); |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 514 | ++NumInserted; |
| 515 | Changed = true; |
Chris Lattner | 0816559 | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 516 | DOUT << "INDVARS: New CanIV: " << *IndVar; |
Chris Lattner | 7e755e4 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 517 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 518 | if (!isa<SCEVCouldNotCompute>(IterationCount)) |
Chris Lattner | 51c95cd | 2006-09-21 05:12:20 +0000 | [diff] [blame] | 519 | if (Instruction *DI = LinearFunctionTestReplace(L, IterationCount,Rewriter)) |
| 520 | DeadInsts.insert(DI); |
Chris Lattner | 7e755e4 | 2003-12-23 07:47:09 +0000 | [diff] [blame] | 521 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 522 | // Now that we have a canonical induction variable, we can rewrite any |
| 523 | // recurrences in terms of the induction variable. Start with the auxillary |
| 524 | // induction variables, and recursively rewrite any of their uses. |
| 525 | BasicBlock::iterator InsertPt = Header->begin(); |
| 526 | while (isa<PHINode>(InsertPt)) ++InsertPt; |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 527 | |
Chris Lattner | dc7cc35 | 2004-04-21 22:22:01 +0000 | [diff] [blame] | 528 | // If there were induction variables of other sizes, cast the primary |
| 529 | // induction variable to the right size for them, avoiding the need for the |
| 530 | // code evaluation methods to insert induction variables of different sizes. |
Chris Lattner | c1a682d | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 531 | if (DifferingSizes) { |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 532 | SmallVector<unsigned,4> InsertedSizes; |
| 533 | InsertedSizes.push_back(LargestType->getPrimitiveSizeInBits()); |
| 534 | for (unsigned i = 0, e = IndVars.size(); i != e; ++i) { |
| 535 | unsigned ithSize = IndVars[i].first->getType()->getPrimitiveSizeInBits(); |
Chris Lattner | f5e5236 | 2007-01-12 22:51:20 +0000 | [diff] [blame] | 536 | if (std::find(InsertedSizes.begin(), InsertedSizes.end(), ithSize) |
| 537 | == InsertedSizes.end()) { |
Chris Lattner | c1a682d | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 538 | PHINode *PN = IndVars[i].first; |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 539 | InsertedSizes.push_back(ithSize); |
Chris Lattner | 0816559 | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 540 | Instruction *New = new TruncInst(IndVar, PN->getType(), "indvar", |
| 541 | InsertPt); |
Chris Lattner | c1a682d | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 542 | Rewriter.addInsertedValue(New, SE->getSCEV(New)); |
Chris Lattner | 0816559 | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 543 | DOUT << "INDVARS: Made trunc IV for " << *PN |
| 544 | << " NewVal = " << *New << "\n"; |
Chris Lattner | c1a682d | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 545 | } |
Reid Spencer | 7a9c62b | 2007-01-12 07:05:14 +0000 | [diff] [blame] | 546 | } |
Chris Lattner | c1a682d | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Chris Lattner | 0816559 | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 549 | // Rewrite all induction variables in terms of the canonical induction |
| 550 | // variable. |
Chris Lattner | dc7cc35 | 2004-04-21 22:22:01 +0000 | [diff] [blame] | 551 | std::map<unsigned, Value*> InsertedSizes; |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 552 | while (!IndVars.empty()) { |
| 553 | PHINode *PN = IndVars.back().first; |
Chris Lattner | 83cd87e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 554 | Value *NewVal = Rewriter.expandCodeFor(IndVars.back().second, InsertPt, |
Chris Lattner | c1a682d | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 555 | PN->getType()); |
Chris Lattner | 0816559 | 2007-01-07 01:14:12 +0000 | [diff] [blame] | 556 | DOUT << "INDVARS: Rewrote IV '" << *IndVars.back().second << "' " << *PN |
| 557 | << " into = " << *NewVal << "\n"; |
Chris Lattner | c1a682d | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 558 | std::string Name = PN->getName(); |
| 559 | PN->setName(""); |
| 560 | NewVal->setName(Name); |
Chris Lattner | dc7cc35 | 2004-04-21 22:22:01 +0000 | [diff] [blame] | 561 | |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 562 | // Replace the old PHI Node with the inserted computation. |
Chris Lattner | c1a682d | 2004-04-22 14:59:40 +0000 | [diff] [blame] | 563 | PN->replaceAllUsesWith(NewVal); |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 564 | DeadInsts.insert(PN); |
| 565 | IndVars.pop_back(); |
| 566 | ++NumRemoved; |
Chris Lattner | 6743940 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 567 | Changed = true; |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Chris Lattner | c27302c | 2004-04-22 15:12:36 +0000 | [diff] [blame] | 570 | #if 0 |
Chris Lattner | af532f2 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 571 | // Now replace all derived expressions in the loop body with simpler |
| 572 | // expressions. |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 573 | for (unsigned i = 0, e = L->getBlocks().size(); i != e; ++i) |
| 574 | if (LI->getLoopFor(L->getBlocks()[i]) == L) { // Not in a subloop... |
| 575 | BasicBlock *BB = L->getBlocks()[i]; |
| 576 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) |
Chris Lattner | 03c4953 | 2007-01-15 02:27:26 +0000 | [diff] [blame^] | 577 | if (I->getType()->isInteger() && // Is an integer instruction |
Chris Lattner | af532f2 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 578 | !I->use_empty() && |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 579 | !Rewriter.isInsertedInstruction(I)) { |
| 580 | SCEVHandle SH = SE->getSCEV(I); |
Chris Lattner | 83cd87e | 2004-04-23 21:29:48 +0000 | [diff] [blame] | 581 | Value *V = Rewriter.expandCodeFor(SH, I, I->getType()); |
Chris Lattner | af532f2 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 582 | if (V != I) { |
| 583 | if (isa<Instruction>(V)) { |
| 584 | std::string Name = I->getName(); |
| 585 | I->setName(""); |
| 586 | V->setName(Name); |
| 587 | } |
| 588 | I->replaceAllUsesWith(V); |
| 589 | DeadInsts.insert(I); |
| 590 | ++NumRemoved; |
| 591 | Changed = true; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 592 | } |
Chris Lattner | e61b67d | 2004-04-02 20:24:31 +0000 | [diff] [blame] | 593 | } |
Chris Lattner | 91daaab | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 594 | } |
Chris Lattner | c27302c | 2004-04-22 15:12:36 +0000 | [diff] [blame] | 595 | #endif |
Chris Lattner | af532f2 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 596 | |
Chris Lattner | af532f2 | 2004-04-21 23:36:08 +0000 | [diff] [blame] | 597 | DeleteTriviallyDeadInstructions(DeadInsts); |
Owen Anderson | 8e4b029 | 2006-08-25 22:12:36 +0000 | [diff] [blame] | 598 | |
| 599 | if (mustPreserveAnalysisID(LCSSAID)) assert(L->isLCSSAForm()); |
Chris Lattner | 476e6df | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 600 | } |