diff options
author | Chris Lattner <sabre@nondot.org> | 2002-10-09 23:12:25 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2002-10-09 23:12:25 +0000 |
commit | c251f9e0ae46dafec666541b8311af878da27f06 (patch) | |
tree | 9e0f7e4d356a25c49e93e275cbae3f5d351807fb /lib | |
parent | 4bbd839d10b14cd9028896800faa892e9d8ce3a3 (diff) | |
download | external_llvm-c251f9e0ae46dafec666541b8311af878da27f06.zip external_llvm-c251f9e0ae46dafec666541b8311af878da27f06.tar.gz external_llvm-c251f9e0ae46dafec666541b8311af878da27f06.tar.bz2 |
- Add new Constant::replaceUsesOfWithOnConstant which has an end result
similar to User::replaceUsesOfWith but actually does the right thing for
constants.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@4102 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib')
-rw-r--r-- | lib/VMCore/Constants.cpp | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index de61c37..d0413f1 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -327,6 +327,112 @@ bool ConstantFP::isValueValidForType(const Type *Ty, double Val) { }; //===----------------------------------------------------------------------===// +// replaceUsesOfWithOnConstant implementations + +void ConstantArray::replaceUsesOfWithOnConstant(Value *From, Value *To) { + assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); + + std::vector<Constant*> Values; + Values.reserve(getValues().size()); // Build replacement array... + for (unsigned i = 0, e = getValues().size(); i != e; ++i) { + Constant *Val = cast<Constant>(getValues()[i]); + if (Val == From) Val = cast<Constant>(To); + Values.push_back(Val); + } + + ConstantArray *Replacement = ConstantArray::get(getType(), Values); + assert(Replacement != this && "I didn't contain From!"); + + // Everyone using this now uses the replacement... + replaceAllUsesWith(Replacement); + + // Delete the old constant! + destroyConstant(); +} + +void ConstantStruct::replaceUsesOfWithOnConstant(Value *From, Value *To) { + assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); + + std::vector<Constant*> Values; + Values.reserve(getValues().size()); + for (unsigned i = 0, e = getValues().size(); i != e; ++i) { + Constant *Val = cast<Constant>(getValues()[i]); + if (Val == From) Val = cast<Constant>(To); + Values.push_back(Val); + } + + ConstantStruct *Replacement = ConstantStruct::get(getType(), Values); + assert(Replacement != this && "I didn't contain From!"); + + // Everyone using this now uses the replacement... + replaceAllUsesWith(Replacement); + + // Delete the old constant! + destroyConstant(); +} + +void ConstantPointerRef::replaceUsesOfWithOnConstant(Value *From, Value *To) { + if (isa<GlobalValue>(To)) { + assert(From == getOperand(0) && "Doesn't contain from!"); + ConstantPointerRef *Replacement = + ConstantPointerRef::get(cast<GlobalValue>(To)); + + // Everyone using this now uses the replacement... + replaceAllUsesWith(Replacement); + + // Delete the old constant! + destroyConstant(); + } else { + // Just replace ourselves with the To value specified. + replaceAllUsesWith(To); + + // Delete the old constant! + destroyConstant(); + } +} + +void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *To) { + assert(isa<Constant>(To) && "Cannot make Constant refer to non-constant!"); + + ConstantExpr *Replacement = 0; + if (getOpcode() == Instruction::GetElementPtr) { + std::vector<Constant*> Indices; + Constant *Pointer = cast<Constant>(getOperand(0)); + Indices.reserve(getNumOperands()-1); + if (Pointer == From) Pointer = cast<Constant>(To); + + for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { + Constant *Val = cast<Constant>(getOperand(i)); + if (Val == From) Val = cast<Constant>(To); + Indices.push_back(Val); + } + Replacement = ConstantExpr::getGetElementPtr(Pointer, Indices); + } else if (getOpcode() == Instruction::Cast) { + assert(getOperand(0) == From && "Cast only has one use!"); + Replacement = ConstantExpr::getCast(cast<Constant>(To), getType()); + } else if (getNumOperands() == 2) { + Constant *C1 = cast<Constant>(getOperand(0)); + Constant *C2 = cast<Constant>(getOperand(1)); + if (C1 == From) C1 = cast<Constant>(To); + if (C2 == From) C2 = cast<Constant>(To); + Replacement = ConstantExpr::get(getOpcode(), C1, C2); + } else { + assert(0 && "Unknown ConstantExpr type!"); + return; + } + + assert(Replacement != this && "I didn't contain From!"); + + // Everyone using this now uses the replacement... + replaceAllUsesWith(Replacement); + + // Delete the old constant! + destroyConstant(); +} + + + +//===----------------------------------------------------------------------===// // Factory Function Implementation template<class ValType, class ConstantClass> |