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 | |
Chris Lattner | 500597a | 2003-12-22 09:53:29 +0000 | [diff] [blame^] | 55 | Value *ComputeAuxIndVarValue(InductionVariable &IV, Value *CIV); |
| 56 | void ReplaceIndVar(InductionVariable &IV, Value *Counter); |
| 57 | |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 58 | bool runOnLoop(Loop *L); |
| 59 | |
| 60 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 61 | AU.addRequired<TargetData>(); // Need pointer size |
| 62 | AU.addRequired<LoopInfo>(); |
| 63 | AU.addRequiredID(LoopSimplifyID); |
| 64 | AU.addPreservedID(LoopSimplifyID); |
| 65 | AU.setPreservesCFG(); |
| 66 | } |
| 67 | }; |
| 68 | RegisterOpt<IndVarSimplify> X("indvars", "Canonicalize Induction Variables"); |
Chris Lattner | 5e76140 | 2002-09-10 05:24:05 +0000 | [diff] [blame] | 69 | } |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 71 | Pass *llvm::createIndVarSimplifyPass() { |
| 72 | return new IndVarSimplify(); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 75 | |
| 76 | bool IndVarSimplify::runOnLoop(Loop *Loop) { |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 77 | // Transform all subloops before this loop... |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 78 | bool Changed = false; |
| 79 | for (unsigned i = 0, e = Loop->getSubLoops().size(); i != e; ++i) |
| 80 | Changed |= runOnLoop(Loop->getSubLoops()[i]); |
| 81 | |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 82 | // Get the header node for this loop. All of the phi nodes that could be |
| 83 | // induction variables must live in this basic block. |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 84 | // |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 85 | BasicBlock *Header = Loop->getHeader(); |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 86 | |
| 87 | // Loop over all of the PHI nodes in the basic block, calculating the |
| 88 | // induction variables that they represent... stuffing the induction variable |
| 89 | // info into a vector... |
| 90 | // |
Chris Lattner | 697954c | 2002-01-20 22:54:45 +0000 | [diff] [blame] | 91 | std::vector<InductionVariable> IndVars; // Induction variables for block |
Chris Lattner | 7e70829 | 2002-06-25 16:13:24 +0000 | [diff] [blame] | 92 | BasicBlock::iterator AfterPHIIt = Header->begin(); |
Chris Lattner | e408e25 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 93 | for (; PHINode *PN = dyn_cast<PHINode>(AfterPHIIt); ++AfterPHIIt) |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 94 | IndVars.push_back(InductionVariable(PN, Loops)); |
Misha Brukman | cf00c4a | 2003-10-10 17:57:28 +0000 | [diff] [blame] | 95 | // AfterPHIIt now points to first non-phi instruction... |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 96 | |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 97 | // 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] | 98 | if (IndVars.empty()) return Changed; |
| 99 | |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 100 | // Loop over the induction variables, looking for a canonical induction |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 101 | // variable, and checking to make sure they are not all unknown induction |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 102 | // variables. Keep track of the largest integer size of the induction |
| 103 | // variable. |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 104 | // |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 105 | InductionVariable *Canonical = 0; |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 106 | unsigned MaxSize = 0; |
| 107 | |
| 108 | for (unsigned i = 0; i != IndVars.size(); ++i) { |
| 109 | InductionVariable &IV = IndVars[i]; |
| 110 | |
| 111 | if (IV.InductionType != InductionVariable::Unknown) { |
| 112 | unsigned IVSize = getTypeSize(IV.Phi->getType()); |
| 113 | |
| 114 | if (IV.InductionType == InductionVariable::Canonical && |
| 115 | !isa<PointerType>(IV.Phi->getType()) && IVSize >= MaxSize) |
| 116 | Canonical = &IV; |
| 117 | |
| 118 | if (IVSize > MaxSize) MaxSize = IVSize; |
| 119 | |
| 120 | // If this variable is larger than the currently identified canonical |
| 121 | // indvar, the canonical indvar is not usable. |
| 122 | if (Canonical && IVSize > getTypeSize(Canonical->Phi->getType())) |
| 123 | Canonical = 0; |
| 124 | } |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 127 | // No induction variables, bail early... don't add a canonical indvar |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 128 | if (MaxSize == 0) return Changed; |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 129 | |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 130 | // Okay, we want to convert other induction variables to use a canonical |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 131 | // indvar. If we don't have one, add one now... |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 132 | if (!Canonical) { |
Chris Lattner | 2a7c23e | 2002-09-10 17:04:02 +0000 | [diff] [blame] | 133 | // 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] | 134 | // node at the start of the PHI nodes... |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 135 | const Type *IVType; |
| 136 | switch (MaxSize) { |
| 137 | default: assert(0 && "Unknown integer type size!"); |
| 138 | case 1: IVType = Type::UByteTy; break; |
| 139 | case 2: IVType = Type::UShortTy; break; |
| 140 | case 4: IVType = Type::UIntTy; break; |
| 141 | case 8: IVType = Type::ULongTy; break; |
| 142 | } |
| 143 | |
| 144 | PHINode *PN = new PHINode(IVType, "cann-indvar", Header->begin()); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 145 | |
| 146 | // Create the increment instruction to add one to the counter... |
| 147 | Instruction *Add = BinaryOperator::create(Instruction::Add, PN, |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 148 | ConstantUInt::get(IVType, 1), |
| 149 | "next-indvar", AfterPHIIt); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 150 | |
| 151 | // Figure out which block is incoming and which is the backedge for the loop |
| 152 | BasicBlock *Incoming, *BackEdgeBlock; |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 153 | pred_iterator PI = pred_begin(Header); |
| 154 | assert(PI != pred_end(Header) && "Loop headers should have 2 preds!"); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 155 | if (Loop->contains(*PI)) { // First pred is back edge... |
| 156 | BackEdgeBlock = *PI++; |
| 157 | Incoming = *PI++; |
| 158 | } else { |
| 159 | Incoming = *PI++; |
| 160 | BackEdgeBlock = *PI++; |
| 161 | } |
Chris Lattner | 455889a | 2002-02-12 22:39:50 +0000 | [diff] [blame] | 162 | assert(PI == pred_end(Header) && "Loop headers should have 2 preds!"); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 163 | |
| 164 | // Add incoming values for the PHI node... |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 165 | PN->addIncoming(Constant::getNullValue(IVType), Incoming); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 166 | PN->addIncoming(Add, BackEdgeBlock); |
| 167 | |
| 168 | // Analyze the new induction variable... |
| 169 | IndVars.push_back(InductionVariable(PN, Loops)); |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 170 | assert(IndVars.back().InductionType == InductionVariable::Canonical && |
| 171 | "Just inserted canonical indvar that is not canonical!"); |
| 172 | Canonical = &IndVars.back(); |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 173 | ++NumInserted; |
Chris Lattner | 4753bf2 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 174 | Changed = true; |
Chris Lattner | 332ae7f | 2003-09-23 20:26:48 +0000 | [diff] [blame] | 175 | } else { |
| 176 | // If we have a canonical induction variable, make sure that it is the first |
| 177 | // one in the basic block. |
| 178 | if (&Header->front() != Canonical->Phi) |
| 179 | Header->getInstList().splice(Header->begin(), Header->getInstList(), |
| 180 | Canonical->Phi); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Anand Shukla | 5ba99bd | 2002-06-25 21:07:58 +0000 | [diff] [blame] | 183 | DEBUG(std::cerr << "Induction variables:\n"); |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 184 | |
| 185 | // 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] | 186 | // canonical phi node... |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 187 | // |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 188 | PHINode *IterCount = Canonical->Phi; |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 189 | |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 190 | // Loop through and replace all of the auxiliary induction variables with |
| 191 | // references to the canonical induction variable... |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 192 | // |
Chris Lattner | 3324e71 | 2003-12-22 03:58:44 +0000 | [diff] [blame] | 193 | for (unsigned i = 0; i != IndVars.size(); ++i) { |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 194 | InductionVariable *IV = &IndVars[i]; |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 195 | |
Chris Lattner | a59cbb2 | 2002-07-27 01:12:17 +0000 | [diff] [blame] | 196 | DEBUG(IV->print(std::cerr)); |
Chris Lattner | f016ea4 | 2002-05-22 17:17:27 +0000 | [diff] [blame] | 197 | |
Chris Lattner | 3adf51d | 2003-09-10 05:24:46 +0000 | [diff] [blame] | 198 | // Don't modify the canonical indvar or unrecognized indvars... |
| 199 | if (IV != Canonical && IV->InductionType != InductionVariable::Unknown) { |
Chris Lattner | 500597a | 2003-12-22 09:53:29 +0000 | [diff] [blame^] | 200 | ReplaceIndVar(*IV, IterCount); |
Chris Lattner | 4753bf2 | 2001-12-05 19:41:33 +0000 | [diff] [blame] | 201 | Changed = true; |
Chris Lattner | 3dec1f2 | 2002-05-10 15:38:35 +0000 | [diff] [blame] | 202 | ++NumRemoved; |
Chris Lattner | 394437f | 2001-12-04 04:32:29 +0000 | [diff] [blame] | 203 | } |
Chris Lattner | 6148c02 | 2001-12-03 17:28:42 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | return Changed; |
| 207 | } |
| 208 | |
Chris Lattner | 500597a | 2003-12-22 09:53:29 +0000 | [diff] [blame^] | 209 | /// ComputeAuxIndVarValue - Given an auxillary induction variable, compute and |
| 210 | /// return a value which will always be equal to the induction variable PHI, but |
| 211 | /// is based off of the canonical induction variable CIV. |
| 212 | /// |
| 213 | Value *IndVarSimplify::ComputeAuxIndVarValue(InductionVariable &IV, Value *CIV){ |
| 214 | Instruction *Phi = IV.Phi; |
| 215 | const Type *IVTy = Phi->getType(); |
| 216 | if (isa<PointerType>(IVTy)) // If indexing into a pointer, make the |
| 217 | IVTy = TD->getIntPtrType(); // index the appropriate type. |
| 218 | |
| 219 | BasicBlock::iterator AfterPHIIt = Phi; |
| 220 | while (isa<PHINode>(AfterPHIIt)) ++AfterPHIIt; |
| 221 | |
| 222 | Value *Val = CIV; |
| 223 | if (Val->getType() != IVTy) |
| 224 | Val = new CastInst(Val, IVTy, Val->getName(), AfterPHIIt); |
| 225 | |
| 226 | if (!isa<ConstantInt>(IV.Step) || // If the step != 1 |
| 227 | !cast<ConstantInt>(IV.Step)->equalsInt(1)) { |
| 228 | |
| 229 | // If the types are not compatible, insert a cast now... |
| 230 | if (IV.Step->getType() != IVTy) |
| 231 | IV.Step = new CastInst(IV.Step, IVTy, IV.Step->getName(), AfterPHIIt); |
| 232 | |
| 233 | Val = BinaryOperator::create(Instruction::Mul, Val, IV.Step, |
| 234 | Phi->getName()+"-scale", AfterPHIIt); |
| 235 | } |
| 236 | |
| 237 | // If this is a pointer indvar... |
| 238 | if (isa<PointerType>(Phi->getType())) { |
| 239 | std::vector<Value*> Idx; |
| 240 | // FIXME: this should not be needed when we fix PR82! |
| 241 | if (Val->getType() != Type::LongTy) |
| 242 | Val = new CastInst(Val, Type::LongTy, Val->getName(), AfterPHIIt); |
| 243 | Idx.push_back(Val); |
| 244 | Val = new GetElementPtrInst(IV.Start, Idx, |
| 245 | Phi->getName()+"-offset", |
| 246 | AfterPHIIt); |
| 247 | |
| 248 | } else if (!isa<Constant>(IV.Start) || // If Start != 0... |
| 249 | !cast<Constant>(IV.Start)->isNullValue()) { |
| 250 | // If the types are not compatible, insert a cast now... |
| 251 | if (IV.Start->getType() != IVTy) |
| 252 | IV.Start = new CastInst(IV.Start, IVTy, IV.Start->getName(), |
| 253 | AfterPHIIt); |
| 254 | |
| 255 | // Insert the instruction after the phi nodes... |
| 256 | Val = BinaryOperator::create(Instruction::Add, Val, IV.Start, |
| 257 | Phi->getName()+"-offset", AfterPHIIt); |
| 258 | } |
| 259 | |
| 260 | // If the PHI node has a different type than val is, insert a cast now... |
| 261 | if (Val->getType() != Phi->getType()) |
| 262 | Val = new CastInst(Val, Phi->getType(), Val->getName(), AfterPHIIt); |
| 263 | |
| 264 | // Move the PHI name to it's new equivalent value... |
| 265 | std::string OldName = Phi->getName(); |
| 266 | Phi->setName(""); |
| 267 | Val->setName(OldName); |
| 268 | |
| 269 | return Val; |
| 270 | } |
| 271 | |
| 272 | |
| 273 | // ReplaceIndVar - Replace all uses of the specified induction variable with |
| 274 | // expressions computed from the specified loop iteration counter variable. |
| 275 | // Return true if instructions were deleted. |
| 276 | void IndVarSimplify::ReplaceIndVar(InductionVariable &IV, Value *CIV) { |
| 277 | Value *IndVarVal = 0; |
| 278 | PHINode *Phi = IV.Phi; |
| 279 | |
| 280 | assert(Phi->getNumOperands() == 4 && |
| 281 | "Only expect induction variables in canonical loops!"); |
| 282 | |
| 283 | // Remember the incoming values used by the PHI node |
| 284 | std::vector<Value*> PHIOps; |
| 285 | PHIOps.reserve(2); |
| 286 | PHIOps.push_back(Phi->getIncomingValue(0)); |
| 287 | PHIOps.push_back(Phi->getIncomingValue(1)); |
| 288 | |
| 289 | // Delete all of the operands of the PHI node... FIXME, this should be more |
| 290 | // intelligent. |
| 291 | Phi->dropAllReferences(); |
| 292 | |
| 293 | // Now that we are rid of unneeded uses of the PHI node, replace any remaining |
| 294 | // ones with the appropriate code using the canonical induction variable. |
| 295 | while (!Phi->use_empty()) { |
| 296 | Instruction *U = cast<Instruction>(Phi->use_back()); |
| 297 | |
| 298 | // TODO: Perform LFTR here if possible |
| 299 | if (0) { |
| 300 | |
| 301 | } else { |
| 302 | // Replace all uses of the old PHI node with the new computed value... |
| 303 | if (IndVarVal == 0) |
| 304 | IndVarVal = ComputeAuxIndVarValue(IV, CIV); |
| 305 | U->replaceUsesOfWith(Phi, IndVarVal); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | // If the PHI is the last user of any instructions for computing PHI nodes |
| 310 | // that are irrelevant now, delete those instructions. |
| 311 | while (!PHIOps.empty()) { |
| 312 | Instruction *MaybeDead = dyn_cast<Instruction>(PHIOps.back()); |
| 313 | PHIOps.pop_back(); |
| 314 | |
| 315 | if (MaybeDead && isInstructionTriviallyDead(MaybeDead) && |
| 316 | (!isa<PHINode>(MaybeDead) || |
| 317 | MaybeDead->getParent() != Phi->getParent())) { |
| 318 | PHIOps.insert(PHIOps.end(), MaybeDead->op_begin(), |
| 319 | MaybeDead->op_end()); |
| 320 | MaybeDead->getParent()->getInstList().erase(MaybeDead); |
| 321 | |
| 322 | // Erase any duplicates entries in the PHIOps list. |
| 323 | std::vector<Value*>::iterator It = |
| 324 | std::find(PHIOps.begin(), PHIOps.end(), MaybeDead); |
| 325 | while (It != PHIOps.end()) { |
| 326 | PHIOps.erase(It); |
| 327 | It = std::find(PHIOps.begin(), PHIOps.end(), MaybeDead); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | // Delete the old, now unused, phi node... |
| 333 | Phi->getParent()->getInstList().erase(Phi); |
| 334 | } |
| 335 | |