From b88a7fbdf6365a6d7a4ad334b8411462ae851924 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 14 Jul 2006 22:20:01 +0000 Subject: [PATCH] Add a new helper, simplify ConstantExpr::getWithOperandReplaced at Gabor's request :) git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@29148 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/Constants.cpp | 79 +++++++++++++++++++++++++++++----------- 1 file changed, 57 insertions(+), 22 deletions(-) diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 8a9bfc787be..80404ead03a 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -504,11 +504,32 @@ Constant *ConstantExpr::getWithOperandReplaced(unsigned OpNo, assert(OpNo < getNumOperands() && "Operand num is out of range!"); assert(Op->getType() == getOperand(OpNo)->getType() && "Replacing operand with value of different type!"); - if (getOperand(OpNo) == Op) return const_cast(this); + if (getOperand(OpNo) == Op) + return const_cast(this); + Constant *Op0, *Op1, *Op2; switch (getOpcode()) { case Instruction::Cast: return ConstantExpr::getCast(Op, getType()); + case Instruction::Select: + Op0 = (OpNo == 0) ? Op : getOperand(0); + Op1 = (OpNo == 1) ? Op : getOperand(1); + Op2 = (OpNo == 2) ? Op : getOperand(2); + return ConstantExpr::getSelect(Op0, Op1, Op2); + case Instruction::InsertElement: + Op0 = (OpNo == 0) ? Op : getOperand(0); + Op1 = (OpNo == 1) ? Op : getOperand(1); + Op2 = (OpNo == 2) ? Op : getOperand(2); + return ConstantExpr::getInsertElement(Op0, Op1, Op2); + case Instruction::ExtractElement: + Op0 = (OpNo == 0) ? Op : getOperand(0); + Op1 = (OpNo == 1) ? Op : getOperand(1); + return ConstantExpr::getExtractElement(Op0, Op1); + case Instruction::ShuffleVector: + Op0 = (OpNo == 0) ? Op : getOperand(0); + Op1 = (OpNo == 1) ? Op : getOperand(1); + Op2 = (OpNo == 2) ? Op : getOperand(2); + return ConstantExpr::getShuffleVector(Op0, Op1, Op2); case Instruction::GetElementPtr: { std::vector Ops; for (unsigned i = 1, e = getNumOperands(); i != e; ++i) @@ -518,33 +539,47 @@ Constant *ConstantExpr::getWithOperandReplaced(unsigned OpNo, Ops[OpNo-1] = Op; return ConstantExpr::getGetElementPtr(getOperand(0), Ops); } + default: + assert(getNumOperands() == 2 && "Must be binary operator?"); + Op0 = (OpNo == 0) ? Op : getOperand(0); + Op1 = (OpNo == 1) ? Op : getOperand(1); + return ConstantExpr::get(getOpcode(), Op0, Op1); + } +} + +/// getWithOperands - This returns the current constant expression with the +/// operands replaced with the specified values. The specified operands must +/// match count and type with the existing ones. +Constant *ConstantExpr:: +getWithOperands(const std::vector &Ops) const { + assert(Ops.size() == getNumOperands() && "Operand count mismatch!"); + bool AnyChange = false; + for (unsigned i = 0, e = Ops.size(); i != e; ++i) { + assert(Ops[i]->getType() == getOperand(i)->getType() && + "Operand type mismatch!"); + AnyChange |= Ops[i] != getOperand(i); + } + if (!AnyChange) // No operands changed, return self. + return const_cast(this); + + switch (getOpcode()) { + case Instruction::Cast: + return ConstantExpr::getCast(Ops[0], getType()); case Instruction::Select: - if (OpNo == 0) - return ConstantExpr::getSelect(Op, getOperand(1), getOperand(2)); - if (OpNo == 1) - return ConstantExpr::getSelect(getOperand(0), Op, getOperand(2)); - return ConstantExpr::getSelect(getOperand(0), getOperand(1), Op); + return ConstantExpr::getSelect(Ops[0], Ops[1], Ops[2]); case Instruction::InsertElement: - if (OpNo == 0) - return ConstantExpr::getInsertElement(Op, getOperand(1), getOperand(2)); - if (OpNo == 1) - return ConstantExpr::getInsertElement(getOperand(0), Op, getOperand(2)); - return ConstantExpr::getInsertElement(getOperand(0), getOperand(1), Op); + return ConstantExpr::getInsertElement(Ops[0], Ops[1], Ops[2]); case Instruction::ExtractElement: - if (OpNo == 0) - return ConstantExpr::getExtractElement(Op, getOperand(1)); - return ConstantExpr::getExtractElement(getOperand(0), Op); + return ConstantExpr::getExtractElement(Ops[0], Ops[1]); case Instruction::ShuffleVector: - if (OpNo == 0) - return ConstantExpr::getShuffleVector(Op, getOperand(1), getOperand(2)); - if (OpNo == 1) - return ConstantExpr::getShuffleVector(getOperand(0), Op, getOperand(2)); - return ConstantExpr::getShuffleVector(getOperand(0), getOperand(1), Op); + return ConstantExpr::getShuffleVector(Ops[0], Ops[1], Ops[2]); + case Instruction::GetElementPtr: { + std::vector ActualOps(Ops.begin()+1, Ops.end()); + return ConstantExpr::getGetElementPtr(Ops[0], ActualOps); + } default: assert(getNumOperands() == 2 && "Must be binary operator?"); - if (OpNo == 0) - return ConstantExpr::get(getOpcode(), Op, getOperand(1)); - return ConstantExpr::get(getOpcode(), getOperand(0), Op); + return ConstantExpr::get(getOpcode(), Ops[0], Ops[1]); } } -- 2.34.1