From 43678f41a37c077f28517c2e4889cca88cada6ce Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 8 Dec 2009 23:42:51 +0000 Subject: [PATCH] make sure that PHITransAddr keeps its 'InstInputs' list up to date when instsimplify kicks in. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@90901 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Analysis/PHITransAddr.h | 5 ++++ lib/Analysis/PHITransAddr.cpp | 45 ++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/include/llvm/Analysis/PHITransAddr.h b/include/llvm/Analysis/PHITransAddr.h index 1f4f3416da8..8257396a09f 100644 --- a/include/llvm/Analysis/PHITransAddr.h +++ b/include/llvm/Analysis/PHITransAddr.h @@ -98,6 +98,11 @@ private: Value *InsertPHITranslatedSubExpr(Value *InVal, BasicBlock *CurBB, BasicBlock *PredBB, const DominatorTree &DT, SmallVectorImpl &NewInsts); + + /// ReplaceInstWithValue - Remove any instruction inputs in the InstInputs + /// array that are due to the specified instruction that is about to be + /// removed from the address, and add any corresponding to V. This returns V. + Value *ReplaceInstWithValue(Instruction *I, Value *V); }; } // end namespace llvm diff --git a/lib/Analysis/PHITransAddr.cpp b/lib/Analysis/PHITransAddr.cpp index 98cea9bb4bd..edb41f7f4cc 100644 --- a/lib/Analysis/PHITransAddr.cpp +++ b/lib/Analysis/PHITransAddr.cpp @@ -43,6 +43,44 @@ bool PHITransAddr::IsPotentiallyPHITranslatable() const { } +static void RemoveInstInputs(Instruction *I, + SmallVectorImpl &InstInputs) { + // If the instruction is in the InstInputs list, remove it. + SmallVectorImpl::iterator Entry = + std::find(InstInputs.begin(), InstInputs.end(), I); + if (Entry != InstInputs.end()) { + InstInputs.erase(Entry); + return; + } + + // Otherwise, it must have instruction inputs itself. Zap them recursively. + bool HadInstInputs = false; + for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) { + if (Instruction *Op = dyn_cast(I->getOperand(i))) { + RemoveInstInputs(Op, InstInputs); + HadInstInputs = true; + } + } + + // This instruction had to have operands in the instinputs list or it should + // have been in the list itself. If not, the list is broken. + assert(HadInstInputs && "InstInputs list inconsistent!"); +} + +/// ReplaceInstWithValue - Remove any instruction inputs in the InstInputs +/// array that are due to the specified instruction that is about to be +/// removed from the address, and add any corresponding to V. This returns V. +Value *PHITransAddr::ReplaceInstWithValue(Instruction *I, Value *V) { + // Remove the old instruction from InstInputs. + RemoveInstInputs(I, InstInputs); + + // If V is an instruction, it is now an input. + if (Instruction *VI = dyn_cast(V)) + InstInputs.push_back(VI); + return V; +} + + Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB, BasicBlock *PredBB) { // If this is a non-instruction value, it can't require PHI translation. @@ -98,7 +136,8 @@ Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB, // Constants are trivial to find. if (Constant *C = dyn_cast(PHIIn)) - return ConstantExpr::getBitCast(C, BC->getType()); + return ReplaceInstWithValue(BC, ConstantExpr::getBitCast(C, + BC->getType())); // Otherwise we have to see if a bitcasted version of the incoming pointer // is available. If so, we can use it, otherwise we have to fail. @@ -129,7 +168,7 @@ Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB, // Simplify the GEP to handle 'gep x, 0' -> x etc. if (Value *V = SimplifyGEPInst(&GEPOps[0], GEPOps.size(), TD)) - return V; + return ReplaceInstWithValue(GEP, V); // Scan to see if we have this GEP available. Value *APHIOp = GEPOps[0]; @@ -174,7 +213,7 @@ Value *PHITransAddr::PHITranslateSubExpr(Value *V, BasicBlock *CurBB, // See if the add simplifies away. if (Value *Res = SimplifyAddInst(LHS, RHS, isNSW, isNUW, TD)) - return Res; + return ReplaceInstWithValue(Inst, Res); // Otherwise, see if we have this add available somewhere. for (Value::use_iterator UI = LHS->use_begin(), E = LHS->use_end(); -- 2.34.1