diff options
author | James Molloy <james.molloy@arm.com> | 2012-11-17 17:56:30 +0000 |
---|---|---|
committer | James Molloy <james.molloy@arm.com> | 2012-11-17 17:56:30 +0000 |
commit | b9478c2aef060aa6b0ede41c05859c34b1527bf8 (patch) | |
tree | 4c896b2e5249460cc25c158c72a750cc4ad118f5 /lib/VMCore | |
parent | 9e6ee16b1814268897965b81e82a74ef39173ee1 (diff) | |
download | external_llvm-b9478c2aef060aa6b0ede41c05859c34b1527bf8.zip external_llvm-b9478c2aef060aa6b0ede41c05859c34b1527bf8.tar.gz external_llvm-b9478c2aef060aa6b0ede41c05859c34b1527bf8.tar.bz2 |
Add a new function to ConstantExpr - getAsInstruction. This returns its Instruction* corollary, which may be useful if a user
wishes to transform a ConstantExpr so that one of its operands is no longer constant.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168262 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore')
-rw-r--r-- | lib/VMCore/Constants.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 935f6b2..a451430 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -2704,3 +2704,66 @@ void ConstantExpr::replaceUsesOfWithOnConstant(Value *From, Value *ToV, // Delete the old constant! destroyConstant(); } + +Instruction *ConstantExpr::getAsInstruction() { + SmallVector<Value*,4> ValueOperands; + for (op_iterator I = op_begin(), E = op_end(); I != E; ++I) + ValueOperands.push_back(cast<Value>(I)); + + ArrayRef<Value*> Ops(ValueOperands); + + switch (getOpcode()) { + case Instruction::Trunc: + case Instruction::ZExt: + case Instruction::SExt: + case Instruction::FPTrunc: + case Instruction::FPExt: + case Instruction::UIToFP: + case Instruction::SIToFP: + case Instruction::FPToUI: + case Instruction::FPToSI: + case Instruction::PtrToInt: + case Instruction::IntToPtr: + case Instruction::BitCast: + return CastInst::Create((Instruction::CastOps)getOpcode(), + Ops[0], getType()); + case Instruction::Select: + return SelectInst::Create(Ops[0], Ops[1], Ops[2]); + case Instruction::InsertElement: + return InsertElementInst::Create(Ops[0], Ops[1], Ops[2]); + case Instruction::ExtractElement: + return ExtractElementInst::Create(Ops[0], Ops[1]); + case Instruction::InsertValue: + return InsertValueInst::Create(Ops[0], Ops[1], getIndices()); + case Instruction::ExtractValue: + return ExtractValueInst::Create(Ops[0], getIndices()); + case Instruction::ShuffleVector: + return new ShuffleVectorInst(Ops[0], Ops[1], Ops[2]); + + case Instruction::GetElementPtr: + if (cast<GEPOperator>(this)->isInBounds()) + return GetElementPtrInst::CreateInBounds(Ops[0], Ops.slice(1)); + else + return GetElementPtrInst::Create(Ops[0], Ops.slice(1)); + + case Instruction::ICmp: + case Instruction::FCmp: + return CmpInst::Create((Instruction::OtherOps)getOpcode(), + getPredicate(), Ops[0], Ops[1]); + + default: + assert(getNumOperands() == 2 && "Must be binary operator?"); + BinaryOperator *BO = + BinaryOperator::Create((Instruction::BinaryOps)getOpcode(), + Ops[0], Ops[1]); + if (isa<OverflowingBinaryOperator>(BO)) { + BO->setHasNoUnsignedWrap(SubclassOptionalData & + OverflowingBinaryOperator::NoUnsignedWrap); + BO->setHasNoSignedWrap(SubclassOptionalData & + OverflowingBinaryOperator::NoSignedWrap); + } + if (isa<PossiblyExactOperator>(BO)) + BO->setIsExact(SubclassOptionalData & PossiblyExactOperator::IsExact); + return BO; + } +} |