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