From 9674662036b61b4c569df6c5f13a40a6bf0a4f54 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 23 May 2008 00:36:11 +0000 Subject: [PATCH] Add more IR support for the new extractvalue and insertvalue instructions. llvm-svn: 51461 --- include/llvm/Instructions.h | 72 +++++++++++++++++++++++++++++++++++++ lib/VMCore/Constants.cpp | 23 ++++++------ lib/VMCore/Instructions.cpp | 68 +++++++++++++++++++++++++++++++++++ 3 files changed, 151 insertions(+), 12 deletions(-) diff --git a/include/llvm/Instructions.h b/include/llvm/Instructions.h index c9bd1aa270c..c3540654f39 100644 --- a/include/llvm/Instructions.h +++ b/include/llvm/Instructions.h @@ -1689,8 +1689,42 @@ template <> struct OperandTraits : VariadicOperandTraits<1> { }; +template +ExtractValueInst::ExtractValueInst(Value *Agg, + InputIterator IdxBegin, + InputIterator IdxEnd, + unsigned Values, + const std::string &Name, + Instruction *InsertBefore) + : Instruction(checkType(getIndexedType(Agg->getType(), IdxBegin, IdxEnd)), + ExtractValue, + OperandTraits::op_end(this) - Values, + Values, InsertBefore) { + init(Agg, IdxBegin, IdxEnd, Name, + typename std::iterator_traits::iterator_category()); +} +template +ExtractValueInst::ExtractValueInst(Value *Agg, + InputIterator IdxBegin, + InputIterator IdxEnd, + unsigned Values, + const std::string &Name, + BasicBlock *InsertAtEnd) + : Instruction(PointerType::get(checkType( + getIndexedType(Agg->getType(), + IdxBegin, IdxEnd)), + cast(Agg->getType()) + ->getAddressSpace()), + ExtractValue, + OperandTraits::op_end(this) - Values, + Values, InsertAtEnd) { + init(Agg, IdxBegin, IdxEnd, Name, + typename std::iterator_traits::iterator_category()); +} + DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractValueInst, Value) + //===----------------------------------------------------------------------===// // InsertValueInst Class //===----------------------------------------------------------------------===// @@ -1840,6 +1874,44 @@ template <> struct OperandTraits : VariadicOperandTraits<2> { }; +template +InsertValueInst::InsertValueInst(Value *Agg, + Value *Val, + InputIterator IdxBegin, + InputIterator IdxEnd, + unsigned Values, + const std::string &Name, + Instruction *InsertBefore) + : Instruction(checkType(ExtractValueInst::getIndexedType( + Agg->getType(), + IdxBegin, IdxEnd)), + InsertValue, + OperandTraits::op_end(this) - Values, + Values, InsertBefore) { + init(Agg, Val, IdxBegin, IdxEnd, Name, + typename std::iterator_traits::iterator_category()); +} +template +InsertValueInst::InsertValueInst(Value *Agg, + Value *Val, + InputIterator IdxBegin, + InputIterator IdxEnd, + unsigned Values, + const std::string &Name, + BasicBlock *InsertAtEnd) + : Instruction(PointerType::get(checkType( + ExtractValueInst::getIndexedType( + Val->getType(), + IdxBegin, IdxEnd)), + cast(Val->getType()) + ->getAddressSpace()), + InsertValue, + OperandTraits::op_end(this) - Values, + Values, InsertAtEnd) { + init(Agg, Val, IdxBegin, IdxEnd, Name, + typename std::iterator_traits::iterator_category()); +} + DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value) //===----------------------------------------------------------------------===// diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 75a2b00fb9a..1a79811ee94 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -2308,8 +2308,8 @@ Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg, if (Constant *FC = ConstantFoldInsertValue(Agg, Val, Idxs, NumIdx)) return FC; // Fold a few common cases... - assert(isa(Agg->getType()) && - "Non-pointer type for constant InsertValue expression"); + assert(Agg->getType()->isFirstClassType() && + "Non-first-class type for constant InsertValue expression"); // Look up the constant in the table first to ensure uniqueness std::vector ArgVec; ArgVec.reserve(NumIdx+2); @@ -2323,13 +2323,13 @@ Constant *ConstantExpr::getInsertValueTy(const Type *ReqTy, Constant *Agg, Constant *ConstantExpr::getInsertValue(Constant *Agg, Constant *Val, Constant* const *IdxList, unsigned NumIdx) { - assert((isa(Agg->getType()) || isa(Agg->getType()) || - isa(Agg->getType())) && - "Tried to create insertelement operation on non-aggregate type!"); + assert(Agg->getType()->isFirstClassType() && + "Tried to create insertelement operation on non-first-class type!"); - const Type *ReqTy = + const Type *ReqTy = Agg->getType(); + const Type *ValTy = ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx); - assert(ReqTy && "insertvalue indices invalid!"); + assert(ValTy == Val->getType() && "insertvalue indices invalid!"); return getInsertValueTy(ReqTy, Agg, Val, IdxList, NumIdx); } @@ -2342,8 +2342,8 @@ Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg, if (Constant *FC = ConstantFoldExtractValue(Agg, Idxs, NumIdx)) return FC; // Fold a few common cases... - assert(isa(Agg->getType()) && - "Non-pointer type for constant ExtractValue expression"); + assert(Agg->getType()->isFirstClassType() && + "Non-first-class type for constant extractvalue expression"); // Look up the constant in the table first to ensure uniqueness std::vector ArgVec; ArgVec.reserve(NumIdx+1); @@ -2356,9 +2356,8 @@ Constant *ConstantExpr::getExtractValueTy(const Type *ReqTy, Constant *Agg, Constant *ConstantExpr::getExtractValue(Constant *Agg, Constant* const *IdxList, unsigned NumIdx) { - assert((isa(Agg->getType()) || isa(Agg->getType()) || - isa(Agg->getType())) && - "Tried to create extractelement operation on non-aggregate type!"); + assert(Agg->getType()->isFirstClassType() && + "Tried to create extractelement operation on non-first-class type!"); const Type *ReqTy = ExtractValueInst::getIndexedType(Agg->getType(), IdxList, IdxList+NumIdx); diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index 40eea138350..95e0fc5199e 100644 --- a/lib/VMCore/Instructions.cpp +++ b/lib/VMCore/Instructions.cpp @@ -1332,10 +1332,70 @@ int ShuffleVectorInst::getMaskValue(unsigned i) const { return cast(MaskCV->getOperand(i))->getZExtValue(); } +//===----------------------------------------------------------------------===// +// InsertValueInst Class +//===----------------------------------------------------------------------===// + +void InsertValueInst::init(Value *Agg, Value *Val, Value* const *Idx, unsigned NumIdx) { + assert(NumOperands == 1+NumIdx && "NumOperands not initialized?"); + Use *OL = OperandList; + OL[0].init(Agg, this); + OL[1].init(Val, this); + + for (unsigned i = 0; i != NumIdx; ++i) + OL[i+2].init(Idx[i], this); +} + +void InsertValueInst::init(Value *Agg, Value *Val, Value *Idx) { + assert(NumOperands == 3 && "NumOperands not initialized?"); + Use *OL = OperandList; + OL[0].init(Agg, this); + OL[1].init(Val, this); + OL[2].init(Idx, this); +} + +InsertValueInst::InsertValueInst(const InsertValueInst &IVI) + : Instruction(reinterpret_cast(IVI.getType()), InsertValue, + OperandTraits::op_end(this) + - IVI.getNumOperands(), + IVI.getNumOperands()) { + Use *OL = OperandList; + Use *IVIOL = IVI.OperandList; + for (unsigned i = 0, E = NumOperands; i != E; ++i) + OL[i].init(IVIOL[i], this); +} + //===----------------------------------------------------------------------===// // ExtractValueInst Class //===----------------------------------------------------------------------===// +void ExtractValueInst::init(Value *Agg, Value* const *Idx, unsigned NumIdx) { + assert(NumOperands == 1+NumIdx && "NumOperands not initialized?"); + Use *OL = OperandList; + OL[0].init(Agg, this); + + for (unsigned i = 0; i != NumIdx; ++i) + OL[i+1].init(Idx[i], this); +} + +void ExtractValueInst::init(Value *Agg, Value *Idx) { + assert(NumOperands == 2 && "NumOperands not initialized?"); + Use *OL = OperandList; + OL[0].init(Agg, this); + OL[1].init(Idx, this); +} + +ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) + : Instruction(reinterpret_cast(EVI.getType()), ExtractValue, + OperandTraits::op_end(this) + - EVI.getNumOperands(), + EVI.getNumOperands()) { + Use *OL = OperandList; + Use *EVIOL = EVI.OperandList; + for (unsigned i = 0, E = NumOperands; i != E; ++i) + OL[i].init(EVIOL[i], this); +} + // getIndexedType - Returns the type of the element that would be extracted // with an extractvalue instruction with the specified parameters. // @@ -2809,6 +2869,14 @@ VICmpInst* VICmpInst::clone() const { return new VICmpInst(getPredicate(), Op<0>(), Op<1>()); } +ExtractValueInst *ExtractValueInst::clone() const { + return new(getNumOperands()) ExtractValueInst(*this); +} +InsertValueInst *InsertValueInst::clone() const { + return new(getNumOperands()) InsertValueInst(*this); +} + + MallocInst *MallocInst::clone() const { return new MallocInst(*this); } AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); } FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }