From 27d672136a57519095a3ef20161697749cffb57c Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 14 Jul 2006 22:21:31 +0000 Subject: [PATCH] eliminate some ugly code, using ConstantExpr::getWithOperands instead. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29149 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Linker/LinkModules.cpp | 55 ++-------------------------- lib/Transforms/Utils/ValueMapper.cpp | 39 ++------------------ 2 files changed, 8 insertions(+), 86 deletions(-) diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index 600b21d3988..3c87177df3d 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -295,57 +295,10 @@ static Value *RemapOperand(const Value *In, Operands[i] = cast(RemapOperand(CP->getOperand(i), ValueMap)); Result = ConstantPacked::get(Operands); } else if (const ConstantExpr *CE = dyn_cast(CPV)) { - if (CE->getOpcode() == Instruction::GetElementPtr) { - Value *Ptr = RemapOperand(CE->getOperand(0), ValueMap); - std::vector Indices; - Indices.reserve(CE->getNumOperands()-1); - for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) - Indices.push_back(cast(RemapOperand(CE->getOperand(i), - ValueMap))); - - Result = ConstantExpr::getGetElementPtr(cast(Ptr), Indices); - } else if (CE->getOpcode() == Instruction::ExtractElement) { - Value *Ptr = RemapOperand(CE->getOperand(0), ValueMap); - Value *Idx = RemapOperand(CE->getOperand(1), ValueMap); - Result = ConstantExpr::getExtractElement(cast(Ptr), - cast(Idx)); - } else if (CE->getOpcode() == Instruction::InsertElement) { - Value *Ptr = RemapOperand(CE->getOperand(0), ValueMap); - Value *Elt = RemapOperand(CE->getOperand(1), ValueMap); - Value *Idx = RemapOperand(CE->getOperand(2), ValueMap); - Result = ConstantExpr::getInsertElement(cast(Ptr), - cast(Elt), - cast(Idx)); - } else if (CE->getOpcode() == Instruction::ShuffleVector) { - Value *V1 = RemapOperand(CE->getOperand(0), ValueMap); - Value *V2 = RemapOperand(CE->getOperand(1), ValueMap); - Result = ConstantExpr::getShuffleVector(cast(V1), - cast(V2), - cast(CE->getOperand(2))); - } else if (CE->getNumOperands() == 1) { - // Cast instruction - assert(CE->getOpcode() == Instruction::Cast); - Value *V = RemapOperand(CE->getOperand(0), ValueMap); - Result = ConstantExpr::getCast(cast(V), CE->getType()); - } else if (CE->getNumOperands() == 3) { - // Select instruction - assert(CE->getOpcode() == Instruction::Select); - Value *V1 = RemapOperand(CE->getOperand(0), ValueMap); - Value *V2 = RemapOperand(CE->getOperand(1), ValueMap); - Value *V3 = RemapOperand(CE->getOperand(2), ValueMap); - Result = ConstantExpr::getSelect(cast(V1), cast(V2), - cast(V3)); - } else if (CE->getNumOperands() == 2) { - // Binary operator... - Value *V1 = RemapOperand(CE->getOperand(0), ValueMap); - Value *V2 = RemapOperand(CE->getOperand(1), ValueMap); - - Result = ConstantExpr::get(CE->getOpcode(), cast(V1), - cast(V2)); - } else { - assert(0 && "Unknown constant expr type!"); - } - + std::vector Ops; + for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) + Ops.push_back(cast(RemapOperand(CE->getOperand(i),ValueMap))); + Result = CE->getWithOperands(Ops); } else { assert(0 && "Unknown type of derived type constant value!"); } diff --git a/lib/Transforms/Utils/ValueMapper.cpp b/lib/Transforms/Utils/ValueMapper.cpp index d4be6e47ef0..f8afabc75f7 100644 --- a/lib/Transforms/Utils/ValueMapper.cpp +++ b/lib/Transforms/Utils/ValueMapper.cpp @@ -71,41 +71,10 @@ Value *llvm::MapValue(const Value *V, std::map &VM) { return VMSlot = C; } else if (ConstantExpr *CE = dyn_cast(C)) { - if (CE->getOpcode() == Instruction::Cast) { - Constant *MV = cast(MapValue(CE->getOperand(0), VM)); - return VMSlot = ConstantExpr::getCast(MV, CE->getType()); - } else if (CE->getOpcode() == Instruction::GetElementPtr) { - std::vector Idx; - Constant *MV = cast(MapValue(CE->getOperand(0), VM)); - for (unsigned i = 1, e = CE->getNumOperands(); i != e; ++i) - Idx.push_back(cast(MapValue(CE->getOperand(i), VM))); - return VMSlot = ConstantExpr::getGetElementPtr(MV, Idx); - } else if (CE->getOpcode() == Instruction::Select) { - Constant *MV1 = cast(MapValue(CE->getOperand(0), VM)); - Constant *MV2 = cast(MapValue(CE->getOperand(1), VM)); - Constant *MV3 = cast(MapValue(CE->getOperand(2), VM)); - return VMSlot = ConstantExpr::getSelect(MV1, MV2, MV3); - } else if (CE->getOpcode() == Instruction::InsertElement) { - Constant *MV1 = cast(MapValue(CE->getOperand(0), VM)); - Constant *MV2 = cast(MapValue(CE->getOperand(1), VM)); - Constant *MV3 = cast(MapValue(CE->getOperand(2), VM)); - return VMSlot = ConstantExpr::getInsertElement(MV1, MV2, MV3); - } else if (CE->getOpcode() == Instruction::ExtractElement) { - Constant *MV1 = cast(MapValue(CE->getOperand(0), VM)); - Constant *MV2 = cast(MapValue(CE->getOperand(1), VM)); - return VMSlot = ConstantExpr::getExtractElement(MV1, MV2); - } else if (CE->getOpcode() == Instruction::ShuffleVector) { - Constant *MV1 = cast(MapValue(CE->getOperand(0), VM)); - Constant *MV2 = cast(MapValue(CE->getOperand(1), VM)); - Constant *MV3 = cast(CE->getOperand(2)); - return VMSlot = ConstantExpr::getShuffleVector(MV1, MV2, MV3); - } else { - assert(CE->getNumOperands() == 2 && "Must be binary operator?"); - Constant *MV1 = cast(MapValue(CE->getOperand(0), VM)); - Constant *MV2 = cast(MapValue(CE->getOperand(1), VM)); - return VMSlot = ConstantExpr::get(CE->getOpcode(), MV1, MV2); - } - + std::vector Ops; + for (unsigned i = 0, e = CE->getNumOperands(); i != e; ++i) + Ops.push_back(cast(MapValue(CE->getOperand(i), VM))); + return VMSlot = CE->getWithOperands(Ops); } else if (ConstantPacked *CP = dyn_cast(C)) { for (unsigned i = 0, e = CP->getNumOperands(); i != e; ++i) { Value *MV = MapValue(CP->getOperand(i), VM);