Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 1 | //===- IndVarSimplify.cpp - Induction Variable Elimination ----------------===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 9 | // |
Chris Lattner | c01ccfd | 2003-09-10 05:10:34 +0000 | [diff] [blame] | 10 | // Guarantees that all loops with identifiable, linear, induction variables will |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 11 | // be transformed to have a single, canonical, induction variable. After this |
Chris Lattner | c01ccfd | 2003-09-10 05:10:34 +0000 | [diff] [blame] | 12 | // pass runs, it guarantees the the first PHI node of the header block in the |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 13 | // loop is the canonical induction variable if there is one. |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chris Lattner | 022103b | 2002-05-07 20:03:00 +0000 | [diff] [blame] | 17 | #include "llvm/Transforms/Scalar.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 18 | #include "llvm/Constants.h" |
| 19 | #include "llvm/Type.h" |
Chris Lattner | 18b3c97 | 2003-12-22 05:02:01 +0000 | [diff] [blame] | 20 | #include "llvm/Instructions.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/InductionVariable.h" |
| 22 | #include "llvm/Analysis/LoopInfo.h" |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CFG.h" |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetData.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 6806f56 | 2003-08-01 22:15:03 +0000 | [diff] [blame] | 26 | #include "Support/Debug.h" |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 27 | #include "Support/Statistic.h" |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 30 | namespace { |
Chris Lattner | a92f696 | 2002-10-01 22:38:41 +0000 | [diff] [blame] | 31 | Statistic<> NumRemoved ("indvars", "Number of aux indvars removed"); |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 32 | Statistic<> NumInserted("indvars", "Number of canonical indvars added"); |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 33 | |
| 34 | class IndVarSimplify : public FunctionPass { |
| 35 | LoopInfo *Loops; |
| 36 | TargetData *TD; |
| 37 | public: |
| 38 | virtual bool runOnFunction(Function &) { |
| 39 | Loops = &getAnalysis<LoopInfo>(); |
| 40 | TD = &getAnalysis<TargetData>(); |
| 41 | |
| 42 | // Induction Variables live in the header nodes of loops |
| 43 | bool Changed = false; |
| 44 | for (unsigned i = 0, e = Loops->getTopLevelLoops().size(); i != e; ++i) |
| 45 | Changed |= runOnLoop(Loops->getTopLevelLoops()[i]); |
| 46 | return Changed; |
| 47 | } |
| 48 | |
| 49 | unsigned getTypeSize(const Type *Ty) { |
| 50 | if (unsigned Size = Ty->getPrimitiveSize()) |
| 51 | return Size; |
| 52 | return TD->getTypeSize(Ty); // Must be a pointer |
| 53 | } |
| 54 | |
| 55 | bool runOnLoop(Loop *L); |
| 56 | |
| 57 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 58 | AU.addRequired<TargetData>(); // Need pointer size |
| 59 | AU.addRequired<LoopInfo>(); |
| 60 | AU.addRequiredID(LoopSimplifyID); |
| 61 | AU.addPreservedID(LoopSimplifyID); |
| 62 | AU.setPreservesCFG(); |
| 63 | } |
| 64 | }; |
| 65 | RegisterOpt<IndVarSimplify> X("indvars", "Canonicalize Induction Variables"); |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 66 | } |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 67 | |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 68 | Pass *llvm::createIndVarSimplifyPass() { |
| 69 | return new IndVarSimplify(); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 72 | |
| 73 | bool IndVarSimplify::runOnLoop(Loop *Loop) { |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 74 | // Transform all subloops before this loop... |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 75 | bool Changed = false; |
| 76 | for (unsigned i = 0, e = Loop->getSubLoops().size(); i != e; ++i) |
| 77 | Changed |= runOnLoop(Loop->getSubLoops()[i]); |
| 78 | |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 79 | // Get the header node for this loop. All of the phi nodes that could be |
| 80 | // induction variables must live in this basic block. |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 81 | // |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 82 | BasicBlock *Header = Loop->getHeader(); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 83 | |
| 84 | // Loop over all of the PHI nodes in the basic block, calculating the |
| 85 | // induction variables that they represent... stuffing the induction variable |
| 86 | // info into a vector... |
| 87 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 88 | std::vector<InductionVariable> IndVars; // Induction variables for block |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 89 | BasicBlock::iterator AfterPHIIt = Header->begin(); |
Chris Lattner | e408e25 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 90 | for (; PHINode *PN = dyn_cast<PHINode>(AfterPHIIt); ++AfterPHIIt) |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 91 | IndVars.push_back(InductionVariable(PN, Loops)); |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 92 | // AfterPHIIt now points to first non-phi instruction... |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 93 | |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 94 | // If there are no phi nodes in this basic block, there can't be indvars... |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 95 | if (IndVars.empty()) return Changed; |
| 96 | |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 97 | // Loop over the induction variables, looking for a canonical induction |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 98 | // variable, and checking to make sure they are not all unknown induction |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 99 | // variables. Keep track of the largest integer size of the induction |
| 100 | // variable. |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 101 | // |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 102 | InductionVariable *Canonical = 0; |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 103 | unsigned MaxSize = 0; |
| 104 | |
| 105 | for (unsigned i = 0; i != IndVars.size(); ++i) { |
| 106 | InductionVariable &IV = IndVars[i]; |
| 107 | |
| 108 | if (IV.InductionType != InductionVariable::Unknown) { |
| 109 | unsigned IVSize = getTypeSize(IV.Phi->getType()); |
| 110 | |
| 111 | if (IV.InductionType == InductionVariable::Canonical && |
| 112 | !isa<PointerType>(IV.Phi->getType()) && IVSize >= MaxSize) |
| 113 | Canonical = &IV; |
| 114 | |
| 115 | if (IVSize > MaxSize) MaxSize = IVSize; |
| 116 | |
| 117 | // If this variable is larger than the currently identified canonical |
| 118 | // indvar, the canonical indvar is not usable. |
| 119 | if (Canonical && IVSize > getTypeSize(Canonical->Phi->getType())) |
| 120 | Canonical = 0; |
| 121 | } |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 124 | // No induction variables, bail early... don't add a canonical indvar |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 125 | if (MaxSize == 0) return Changed; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 127 | // Okay, we want to convert other induction variables to use a canonical |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 128 | // indvar. If we don't have one, add one now... |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 129 | if (!Canonical) { |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 130 | // Create the PHI node for the new induction variable, and insert the phi |
Chris Lattner | 332ae7f | 2003-09-23 20:26:48 +0000 | [diff] [blame] | 131 | // node at the start of the PHI nodes... |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 132 | const Type *IVType; |
| 133 | switch (MaxSize) { |
| 134 | default: assert(0 && "Unknown integer type size!"); |
| 135 | case 1: IVType = Type::UByteTy; break; |
| 136 | case 2: IVType = Type::UShortTy; break; |
| 137 | case 4: IVType = Type::UIntTy; break; |
| 138 | case 8: IVType = Type::ULongTy; break; |
| 139 | } |
| 140 | |
| 141 | PHINode *PN = new PHINode(IVType, "cann-indvar", Header->begin()); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 142 | |
| 143 | // Create the increment instruction to add one to the counter... |
| 144 | Instruction *Add = BinaryOperator::create(Instruction::Add, PN, |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 145 | ConstantUInt::get(IVType, 1), |
| 146 | "next-indvar", AfterPHIIt); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 147 | |
| 148 | // Figure out which block is incoming and which is the backedge for the loop |
| 149 | BasicBlock *Incoming, *BackEdgeBlock; |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 150 | pred_iterator PI = pred_begin(Header); |
| 151 | assert(PI != pred_end(Header) && "Loop headers should have 2 preds!"); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 152 | if (Loop->contains(*PI)) { // First pred is back edge... |
| 153 | BackEdgeBlock = *PI++; |
| 154 | Incoming = *PI++; |
| 155 | } else { |
| 156 | Incoming = *PI++; |
| 157 | BackEdgeBlock = *PI++; |
| 158 | } |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 159 | assert(PI == pred_end(Header) && "Loop headers should have 2 preds!"); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 160 | |
| 161 | // Add incoming values for the PHI node... |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 162 | PN->addIncoming(Constant::getNullValue(IVType), Incoming); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 163 | PN->addIncoming(Add, BackEdgeBlock); |
| 164 | |
| 165 | // Analyze the new induction variable... |
| 166 | IndVars.push_back(InductionVariable(PN, Loops)); |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 167 | assert(IndVars.back().InductionType == InductionVariable::Canonical && |
| 168 | "Just inserted canonical indvar that is not canonical!"); |
| 169 | Canonical = &IndVars.back(); |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 170 | ++NumInserted; |
Chris Lattner | 4753bf2 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 171 | Changed = true; |
Chris Lattner | 332ae7f | 2003-09-23 20:26:48 +0000 | [diff] [blame] | 172 | } else { |
| 173 | // If we have a canonical induction variable, make sure that it is the first |
| 174 | // one in the basic block. |
| 175 | if (&Header->front() != Canonical->Phi) |
| 176 | Header->getInstList().splice(Header->begin(), Header->getInstList(), |
| 177 | Canonical->Phi); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Anand Shukla | 5ba99bd | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 180 | DEBUG(std::cerr << "Induction variables:\n"); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 181 | |
| 182 | // Get the current loop iteration count, which is always the value of the |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 183 | // canonical phi node... |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 184 | // |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 185 | PHINode *IterCount = Canonical->Phi; |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 187 | // Loop through and replace all of the auxiliary induction variables with |
| 188 | // references to the canonical induction variable... |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 189 | // |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 190 | for (unsigned i = 0; i != IndVars.size(); ++i) { |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 191 | InductionVariable *IV = &IndVars[i]; |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 192 | |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 193 | DEBUG(IV->print(std::cerr)); |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 194 | |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 195 | while (isa<PHINode>(AfterPHIIt)) ++AfterPHIIt; |
| 196 | |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 197 | // Don't modify the canonical indvar or unrecognized indvars... |
| 198 | if (IV != Canonical && IV->InductionType != InductionVariable::Unknown) { |
Chris Lattner | 18b3c97 | 2003-12-22 05:02:01 +0000 | [diff] [blame] | 199 | const Type *IVTy = IV->Phi->getType(); |
| 200 | if (isa<PointerType>(IVTy)) // If indexing into a pointer, make the |
| 201 | IVTy = TD->getIntPtrType(); // index the appropriate type. |
| 202 | |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 203 | Instruction *Val = IterCount; |
| 204 | if (!isa<ConstantInt>(IV->Step) || // If the step != 1 |
| 205 | !cast<ConstantInt>(IV->Step)->equalsInt(1)) { |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 206 | |
| 207 | // If the types are not compatible, insert a cast now... |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 208 | if (Val->getType() != IVTy) |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 209 | Val = new CastInst(Val, IVTy, Val->getName(), AfterPHIIt); |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 210 | if (IV->Step->getType() != IVTy) |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 211 | IV->Step = new CastInst(IV->Step, IVTy, IV->Step->getName(), |
| 212 | AfterPHIIt); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 213 | |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 214 | Val = BinaryOperator::create(Instruction::Mul, Val, IV->Step, |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 215 | IV->Phi->getName()+"-scale", AfterPHIIt); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Chris Lattner | 18b3c97 | 2003-12-22 05:02:01 +0000 | [diff] [blame] | 218 | // If this is a pointer indvar... |
| 219 | if (isa<PointerType>(IV->Phi->getType())) { |
| 220 | std::vector<Value*> Idx; |
| 221 | // FIXME: this should not be needed when we fix PR82! |
| 222 | if (Val->getType() != Type::LongTy) |
| 223 | Val = new CastInst(Val, Type::LongTy, Val->getName(), AfterPHIIt); |
| 224 | Idx.push_back(Val); |
| 225 | Val = new GetElementPtrInst(IV->Start, Idx, |
| 226 | IV->Phi->getName()+"-offset", |
| 227 | AfterPHIIt); |
| 228 | |
| 229 | } else if (!isa<Constant>(IV->Start) || // If Start != 0... |
| 230 | !cast<Constant>(IV->Start)->isNullValue()) { |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 231 | // If the types are not compatible, insert a cast now... |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 232 | if (Val->getType() != IVTy) |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 233 | Val = new CastInst(Val, IVTy, Val->getName(), AfterPHIIt); |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 234 | if (IV->Start->getType() != IVTy) |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 235 | IV->Start = new CastInst(IV->Start, IVTy, IV->Start->getName(), |
| 236 | AfterPHIIt); |
Chris Lattner | 18b3c97 | 2003-12-22 05:02:01 +0000 | [diff] [blame] | 237 | |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 238 | // Insert the instruction after the phi nodes... |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 239 | Val = BinaryOperator::create(Instruction::Add, Val, IV->Start, |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 240 | IV->Phi->getName()+"-offset", AfterPHIIt); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | // If the PHI node has a different type than val is, insert a cast now... |
| 244 | if (Val->getType() != IV->Phi->getType()) |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 245 | Val = new CastInst(Val, IV->Phi->getType(), Val->getName(), AfterPHIIt); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 246 | |
| 247 | // Replace all uses of the old PHI node with the new computed value... |
| 248 | IV->Phi->replaceAllUsesWith(Val); |
| 249 | |
| 250 | // Move the PHI name to it's new equivalent value... |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 251 | std::string OldName = IV->Phi->getName(); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 252 | IV->Phi->setName(""); |
| 253 | Val->setName(OldName); |
| 254 | |
John Criswell | 47df12d | 2003-12-18 17:19:19 +0000 | [diff] [blame] | 255 | // Get the incoming values used by the PHI node |
| 256 | std::vector<Value*> PHIOps; |
| 257 | PHIOps.reserve(IV->Phi->getNumIncomingValues()); |
| 258 | for (unsigned i = 0, e = IV->Phi->getNumIncomingValues(); i != e; ++i) |
| 259 | PHIOps.push_back(IV->Phi->getIncomingValue(i)); |
| 260 | |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 261 | // Delete the old, now unused, phi node... |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 262 | Header->getInstList().erase(IV->Phi); |
Chris Lattner | ba4f3f6 | 2003-12-10 18:06:47 +0000 | [diff] [blame] | 263 | |
| 264 | // If the PHI is the last user of any instructions for computing PHI nodes |
| 265 | // that are irrelevant now, delete those instructions. |
| 266 | while (!PHIOps.empty()) { |
| 267 | Instruction *MaybeDead = dyn_cast<Instruction>(PHIOps.back()); |
| 268 | PHIOps.pop_back(); |
| 269 | |
| 270 | if (MaybeDead && isInstructionTriviallyDead(MaybeDead)) { |
| 271 | PHIOps.insert(PHIOps.end(), MaybeDead->op_begin(), |
| 272 | MaybeDead->op_end()); |
| 273 | MaybeDead->getParent()->getInstList().erase(MaybeDead); |
Chris Lattner | 9e45d2e | 2003-12-15 17:34:02 +0000 | [diff] [blame] | 274 | |
| 275 | // Erase any duplicates entries in the PHIOps list. |
| 276 | std::vector<Value*>::iterator It = |
| 277 | std::find(PHIOps.begin(), PHIOps.end(), MaybeDead); |
| 278 | while (It != PHIOps.end()) { |
| 279 | PHIOps.erase(It); |
| 280 | It = std::find(PHIOps.begin(), PHIOps.end(), MaybeDead); |
| 281 | } |
Chris Lattner | 88369d2 | 2003-12-10 20:43:04 +0000 | [diff] [blame] | 282 | |
| 283 | // Erasing the instruction could invalidate the AfterPHI iterator! |
| 284 | AfterPHIIt = Header->begin(); |
Chris Lattner | ba4f3f6 | 2003-12-10 18:06:47 +0000 | [diff] [blame] | 285 | } |
| 286 | } |
| 287 | |
Chris Lattner | 4753bf2 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 288 | Changed = true; |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 289 | ++NumRemoved; |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 290 | } |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | return Changed; |
| 294 | } |
| 295 | |