From 28eca8bb5692dced9f51a99c96078cbd67e230c6 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 9 Oct 2002 23:12:59 +0000 Subject: - Make Value::replaceAllUsesWith work with constants correctly. This fixes bug FuncResolve/2002-08-19-ResolveGlobalVars.ll and gzip looks better. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4103 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/Value.cpp | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) (limited to 'lib/VMCore') diff --git a/lib/VMCore/Value.cpp b/lib/VMCore/Value.cpp index 7587535..f343a6c 100644 --- a/lib/VMCore/Value.cpp +++ b/lib/VMCore/Value.cpp @@ -7,6 +7,7 @@ #include "llvm/InstrTypes.h" #include "llvm/SymbolTable.h" #include "llvm/DerivedTypes.h" +#include "llvm/Constant.h" #include "Support/LeakDetector.h" #include @@ -45,23 +46,23 @@ Value::~Value() { LeakDetector::removeGarbageObject(this); } -void Value::replaceAllUsesWith(Value *D) { - assert(D && "Value::replaceAllUsesWith() is invalid!"); - assert(D != this && "V->replaceAllUsesWith(V) is NOT valid!"); - assert(D->getType() == getType() && + + + +void Value::replaceAllUsesWith(Value *New) { + assert(New && "Value::replaceAllUsesWith() is invalid!"); + assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!"); + assert(New->getType() == getType() && "replaceAllUses of value with new value of different type!"); while (!Uses.empty()) { User *Use = Uses.back(); -#ifndef NDEBUG - unsigned NumUses = Uses.size(); -#endif - Use->replaceUsesOfWith(this, D); - -#ifndef NDEBUG // only in -g mode... - if (Uses.size() == NumUses) - std::cerr << "Use: " << *Use << "replace with: " << *D; -#endif - assert(Uses.size() != NumUses && "Didn't remove definition!"); + // Must handle Constants specially, we cannot call replaceUsesOfWith on a + // constant! + if (Constant *C = dyn_cast(Use)) { + C->replaceUsesOfWithOnConstant(this, New); + } else { + Use->replaceUsesOfWith(this, New); + } } } @@ -105,6 +106,9 @@ User::User(const Type *Ty, ValueTy vty, const std::string &name) void User::replaceUsesOfWith(Value *From, Value *To) { if (From == To) return; // Duh what? + assert(!isa(this) && + "Cannot call User::replaceUsesofWith on a constant!"); + for (unsigned i = 0, E = getNumOperands(); i != E; ++i) if (getOperand(i) == From) { // Is This operand is pointing to oldval? // The side effects of this setOperand call include linking to @@ -113,5 +117,3 @@ void User::replaceUsesOfWith(Value *From, Value *To) { setOperand(i, To); // Fix it now... } } - - -- cgit v1.1