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