X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FVMCore%2FInstructions.cpp;h=8d14766c21a6911027f39a0034bdefb68ee2835e;hb=7f6aa2b162e5daaf7b9ccf05d749597d3d7cf460;hp=9fc70ce59b1a14e752fe8ba3b3405ebfa3a4c8d4;hpb=eedff319dc24652431cafc9df8ff84d26f9cdc9d;p=oota-llvm.git diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index 9fc70ce59b1..8d14766c21a 100644 --- a/lib/VMCore/Instructions.cpp +++ b/lib/VMCore/Instructions.cpp @@ -12,7 +12,6 @@ // //===----------------------------------------------------------------------===// -#include "llvm/BasicBlock.h" #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" #include "llvm/Function.h" @@ -26,72 +25,77 @@ using namespace llvm; // CallSite Class //===----------------------------------------------------------------------===// +#define CALLSITE_DELEGATE_GETTER(METHOD) \ + Instruction *II(getInstruction()); \ + return isCall() \ + ? cast(II)->METHOD \ + : cast(II)->METHOD + +#define CALLSITE_DELEGATE_SETTER(METHOD) \ + Instruction *II(getInstruction()); \ + if (isCall()) \ + cast(II)->METHOD; \ + else \ + cast(II)->METHOD + CallSite::CallSite(Instruction *C) { assert((isa(C) || isa(C)) && "Not a call!"); - I = C; + I.setPointer(C); + I.setInt(isa(C)); } unsigned CallSite::getCallingConv() const { - if (CallInst *CI = dyn_cast(I)) - return CI->getCallingConv(); - else - return cast(I)->getCallingConv(); + CALLSITE_DELEGATE_GETTER(getCallingConv()); } void CallSite::setCallingConv(unsigned CC) { - if (CallInst *CI = dyn_cast(I)) - CI->setCallingConv(CC); - else - cast(I)->setCallingConv(CC); + CALLSITE_DELEGATE_SETTER(setCallingConv(CC)); } -const PAListPtr &CallSite::getParamAttrs() const { - if (CallInst *CI = dyn_cast(I)) - return CI->getParamAttrs(); - else - return cast(I)->getParamAttrs(); +const AttrListPtr &CallSite::getAttributes() const { + CALLSITE_DELEGATE_GETTER(getAttributes()); } -void CallSite::setParamAttrs(const PAListPtr &PAL) { - if (CallInst *CI = dyn_cast(I)) - CI->setParamAttrs(PAL); - else - cast(I)->setParamAttrs(PAL); +void CallSite::setAttributes(const AttrListPtr &PAL) { + CALLSITE_DELEGATE_SETTER(setAttributes(PAL)); } -bool CallSite::paramHasAttr(uint16_t i, ParameterAttributes attr) const { - if (CallInst *CI = dyn_cast(I)) - return CI->paramHasAttr(i, attr); - else - return cast(I)->paramHasAttr(i, attr); +bool CallSite::paramHasAttr(uint16_t i, Attributes attr) const { + CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr)); } uint16_t CallSite::getParamAlignment(uint16_t i) const { - if (CallInst *CI = dyn_cast(I)) - return CI->getParamAlignment(i); - else - return cast(I)->getParamAlignment(i); + CALLSITE_DELEGATE_GETTER(getParamAlignment(i)); } - bool CallSite::doesNotAccessMemory() const { - if (CallInst *CI = dyn_cast(I)) - return CI->doesNotAccessMemory(); - else - return cast(I)->doesNotAccessMemory(); + CALLSITE_DELEGATE_GETTER(doesNotAccessMemory()); +} +void CallSite::setDoesNotAccessMemory(bool doesNotAccessMemory) { + CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory)); } bool CallSite::onlyReadsMemory() const { - if (CallInst *CI = dyn_cast(I)) - return CI->onlyReadsMemory(); - else - return cast(I)->onlyReadsMemory(); + CALLSITE_DELEGATE_GETTER(onlyReadsMemory()); +} +void CallSite::setOnlyReadsMemory(bool onlyReadsMemory) { + CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory)); +} +bool CallSite::doesNotReturn() const { + CALLSITE_DELEGATE_GETTER(doesNotReturn()); +} +void CallSite::setDoesNotReturn(bool doesNotReturn) { + CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn)); } bool CallSite::doesNotThrow() const { - if (CallInst *CI = dyn_cast(I)) - return CI->doesNotThrow(); - else - return cast(I)->doesNotThrow(); + CALLSITE_DELEGATE_GETTER(doesNotThrow()); } void CallSite::setDoesNotThrow(bool doesNotThrow) { - if (CallInst *CI = dyn_cast(I)) - CI->setDoesNotThrow(doesNotThrow); - else - cast(I)->setDoesNotThrow(doesNotThrow); + CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow)); } +bool CallSite::hasArgument(const Value *Arg) const { + for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; ++AI) + if (AI->get() == Arg) + return true; + return false; +} + +#undef CALLSITE_DELEGATE_GETTER +#undef CALLSITE_DELEGATE_SETTER + //===----------------------------------------------------------------------===// // TerminatorInst Class //===----------------------------------------------------------------------===// @@ -108,6 +112,33 @@ TerminatorInst::~TerminatorInst() { UnaryInstruction::~UnaryInstruction() { } +//===----------------------------------------------------------------------===// +// SelectInst Class +//===----------------------------------------------------------------------===// + +/// areInvalidOperands - Return a string if the specified operands are invalid +/// for a select operation, otherwise return null. +const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { + if (Op1->getType() != Op2->getType()) + return "both values to select must have same type"; + + if (const VectorType *VT = dyn_cast(Op0->getType())) { + // Vector select. + if (VT->getElementType() != Type::Int1Ty) + return "vector select condition element type must be i1"; + const VectorType *ET = dyn_cast(Op1->getType()); + if (ET == 0) + return "selected values for vector select must be vectors"; + if (ET->getNumElements() != VT->getNumElements()) + return "vector select requires selected vectors to have " + "the same vector length as select condition"; + } else if (Op0->getType() != Type::Int1Ty) { + return "select condition must be i1 or "; + } + return 0; +} + + //===----------------------------------------------------------------------===// // PHINode Class //===----------------------------------------------------------------------===// @@ -118,13 +149,14 @@ PHINode::PHINode(const PHINode &PN) ReservedSpace(PN.getNumOperands()) { Use *OL = OperandList; for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) { - OL[i].init(PN.getOperand(i), this); - OL[i+1].init(PN.getOperand(i+1), this); + OL[i] = PN.getOperand(i); + OL[i+1] = PN.getOperand(i+1); } } PHINode::~PHINode() { - dropHungoffUses(OperandList); + if (OperandList) + dropHungoffUses(OperandList); } // removeIncomingValue - Remove an incoming value. This is useful if a @@ -183,9 +215,7 @@ void PHINode::resizeOperands(unsigned NumOps) { ReservedSpace = NumOps; Use *OldOps = OperandList; Use *NewOps = allocHungoffUses(NumOps); - for (unsigned i = 0; i != e; ++i) { - NewOps[i].init(OldOps[i], this); - } + std::copy(OldOps, OldOps + e, NewOps); OperandList = NewOps; if (OldOps) Use::zap(OldOps, OldOps + e, true); } @@ -249,7 +279,7 @@ CallInst::~CallInst() { void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) { assert(NumOperands == NumParams+1 && "NumOperands not set up?"); Use *OL = OperandList; - OL[0].init(Func, this); + OL[0] = Func; const FunctionType *FTy = cast(cast(Func->getType())->getElementType()); @@ -262,16 +292,16 @@ void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) { assert((i >= FTy->getNumParams() || FTy->getParamType(i) == Params[i]->getType()) && "Calling a function with a bad signature!"); - OL[i+1].init(Params[i], this); + OL[i+1] = Params[i]; } } void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { assert(NumOperands == 3 && "NumOperands not set up?"); Use *OL = OperandList; - OL[0].init(Func, this); - OL[1].init(Actual1, this); - OL[2].init(Actual2, this); + OL[0] = Func; + OL[1] = Actual1; + OL[2] = Actual2; const FunctionType *FTy = cast(cast(Func->getType())->getElementType()); @@ -291,8 +321,8 @@ void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { void CallInst::init(Value *Func, Value *Actual) { assert(NumOperands == 2 && "NumOperands not set up?"); Use *OL = OperandList; - OL[0].init(Func, this); - OL[1].init(Actual, this); + OL[0] = Func; + OL[1] = Actual; const FunctionType *FTy = cast(cast(Func->getType())->getElementType()); @@ -309,7 +339,7 @@ void CallInst::init(Value *Func, Value *Actual) { void CallInst::init(Value *Func) { assert(NumOperands == 1 && "NumOperands not set up?"); Use *OL = OperandList; - OL[0].init(Func, this); + OL[0] = Func; const FunctionType *FTy = cast(cast(Func->getType())->getElementType()); @@ -365,31 +395,34 @@ CallInst::CallInst(const CallInst &CI) : Instruction(CI.getType(), Instruction::Call, OperandTraits::op_end(this) - CI.getNumOperands(), CI.getNumOperands()) { - setParamAttrs(CI.getParamAttrs()); + setAttributes(CI.getAttributes()); SubclassData = CI.SubclassData; Use *OL = OperandList; Use *InOL = CI.OperandList; for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i) - OL[i].init(InOL[i], this); + OL[i] = InOL[i]; } -bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const { - if (ParamAttrs.paramHasAttr(i, attr)) +void CallInst::addAttribute(unsigned i, Attributes attr) { + AttrListPtr PAL = getAttributes(); + PAL = PAL.addAttr(i, attr); + setAttributes(PAL); +} + +void CallInst::removeAttribute(unsigned i, Attributes attr) { + AttrListPtr PAL = getAttributes(); + PAL = PAL.removeAttr(i, attr); + setAttributes(PAL); +} + +bool CallInst::paramHasAttr(unsigned i, Attributes attr) const { + if (AttributeList.paramHasAttr(i, attr)) return true; if (const Function *F = getCalledFunction()) return F->paramHasAttr(i, attr); return false; } -void CallInst::setDoesNotThrow(bool doesNotThrow) { - PAListPtr PAL = getParamAttrs(); - if (doesNotThrow) - PAL = PAL.addAttr(0, ParamAttr::NoUnwind); - else - PAL = PAL.removeAttr(0, ParamAttr::NoUnwind); - setParamAttrs(PAL); -} - //===----------------------------------------------------------------------===// // InvokeInst Implementation @@ -399,9 +432,9 @@ void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, Value* const *Args, unsigned NumArgs) { assert(NumOperands == 3+NumArgs && "NumOperands not set up?"); Use *OL = OperandList; - OL[0].init(Fn, this); - OL[1].init(IfNormal, this); - OL[2].init(IfException, this); + OL[0] = Fn; + OL[1] = IfNormal; + OL[2] = IfException; const FunctionType *FTy = cast(cast(Fn->getType())->getElementType()); FTy = FTy; // silence warning. @@ -415,7 +448,7 @@ void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, FTy->getParamType(i) == Args[i]->getType()) && "Invoking a function with a bad signature!"); - OL[i+3].init(Args[i], this); + OL[i+3] = Args[i]; } } @@ -424,11 +457,11 @@ InvokeInst::InvokeInst(const InvokeInst &II) OperandTraits::op_end(this) - II.getNumOperands(), II.getNumOperands()) { - setParamAttrs(II.getParamAttrs()); + setAttributes(II.getAttributes()); SubclassData = II.SubclassData; Use *OL = OperandList, *InOL = II.OperandList; for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i) - OL[i].init(InOL[i], this); + OL[i] = InOL[i]; } BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { @@ -441,21 +474,24 @@ void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { return setSuccessor(idx, B); } -bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const { - if (ParamAttrs.paramHasAttr(i, attr)) +bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const { + if (AttributeList.paramHasAttr(i, attr)) return true; if (const Function *F = getCalledFunction()) return F->paramHasAttr(i, attr); return false; } -void InvokeInst::setDoesNotThrow(bool doesNotThrow) { - PAListPtr PAL = getParamAttrs(); - if (doesNotThrow) - PAL = PAL.addAttr(0, ParamAttr::NoUnwind); - else - PAL = PAL.removeAttr(0, ParamAttr::NoUnwind); - setParamAttrs(PAL); +void InvokeInst::addAttribute(unsigned i, Attributes attr) { + AttrListPtr PAL = getAttributes(); + PAL = PAL.addAttr(i, attr); + setAttributes(PAL); +} + +void InvokeInst::removeAttribute(unsigned i, Attributes attr) { + AttrListPtr PAL = getAttributes(); + PAL = PAL.removeAttr(i, attr); + setAttributes(PAL); } @@ -465,75 +501,30 @@ void InvokeInst::setDoesNotThrow(bool doesNotThrow) { ReturnInst::ReturnInst(const ReturnInst &RI) : TerminatorInst(Type::VoidTy, Instruction::Ret, - OperandTraits::op_end(this) - - RI.getNumOperands(), + OperandTraits::op_end(this) - + RI.getNumOperands(), RI.getNumOperands()) { - unsigned N = RI.getNumOperands(); - if (N == 1) - Op<0>().init(RI.Op<0>(), this); - else if (N) { - Use *OL = OperandList; - for (unsigned i = 0; i < N; ++i) - OL[i].init(RI.getOperand(i), this); - } + if (RI.getNumOperands()) + Op<0>() = RI.Op<0>(); } ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore) : TerminatorInst(Type::VoidTy, Instruction::Ret, - OperandTraits::op_end(this) - (retVal != 0), - retVal != 0, InsertBefore) { + OperandTraits::op_end(this) - !!retVal, !!retVal, + InsertBefore) { if (retVal) - init(&retVal, 1); + Op<0>() = retVal; } ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd) : TerminatorInst(Type::VoidTy, Instruction::Ret, - OperandTraits::op_end(this) - (retVal != 0), - retVal != 0, InsertAtEnd) { + OperandTraits::op_end(this) - !!retVal, !!retVal, + InsertAtEnd) { if (retVal) - init(&retVal, 1); + Op<0>() = retVal; } ReturnInst::ReturnInst(BasicBlock *InsertAtEnd) : TerminatorInst(Type::VoidTy, Instruction::Ret, - OperandTraits::op_end(this), - 0, InsertAtEnd) { -} - -ReturnInst::ReturnInst(Value * const* retVals, unsigned N, - Instruction *InsertBefore) - : TerminatorInst(Type::VoidTy, Instruction::Ret, - OperandTraits::op_end(this) - N, - N, InsertBefore) { - if (N != 0) - init(retVals, N); -} -ReturnInst::ReturnInst(Value * const* retVals, unsigned N, - BasicBlock *InsertAtEnd) - : TerminatorInst(Type::VoidTy, Instruction::Ret, - OperandTraits::op_end(this) - N, - N, InsertAtEnd) { - if (N != 0) - init(retVals, N); -} - -void ReturnInst::init(Value * const* retVals, unsigned N) { - assert (N > 0 && "Invalid operands numbers in ReturnInst init"); - - NumOperands = N; - if (NumOperands == 1) { - Value *V = *retVals; - if (V->getType() == Type::VoidTy) - return; - Op<0>().init(V, this); - return; - } - - Use *OL = OperandList; - for (unsigned i = 0; i < NumOperands; ++i) { - Value *V = *retVals++; - assert(!isa(V) && - "Cannot return basic block. Probably using the incorrect ctor"); - OL[i].init(V, this); - } + OperandTraits::op_end(this), 0, InsertAtEnd) { } unsigned ReturnInst::getNumSuccessorsV() const { @@ -621,16 +612,16 @@ BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) OperandTraits::op_end(this) - 1, 1, InsertBefore) { assert(IfTrue != 0 && "Branch destination may not be null!"); - Op<0>().init(reinterpret_cast(IfTrue), this); + Op<-1>() = IfTrue; } BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, Instruction *InsertBefore) : TerminatorInst(Type::VoidTy, Instruction::Br, OperandTraits::op_end(this) - 3, 3, InsertBefore) { - Op<0>().init(reinterpret_cast(IfTrue), this); - Op<1>().init(reinterpret_cast(IfFalse), this); - Op<2>().init(Cond, this); + Op<-1>() = IfTrue; + Op<-2>() = IfFalse; + Op<-3>() = Cond; #ifndef NDEBUG AssertOK(); #endif @@ -641,7 +632,7 @@ BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) OperandTraits::op_end(this) - 1, 1, InsertAtEnd) { assert(IfTrue != 0 && "Branch destination may not be null!"); - Op<0>().init(reinterpret_cast(IfTrue), this); + Op<-1>() = IfTrue; } BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, @@ -649,9 +640,9 @@ BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, : TerminatorInst(Type::VoidTy, Instruction::Br, OperandTraits::op_end(this) - 3, 3, InsertAtEnd) { - Op<0>().init(reinterpret_cast(IfTrue), this); - Op<1>().init(reinterpret_cast(IfFalse), this); - Op<2>().init(Cond, this); + Op<-1>() = IfTrue; + Op<-2>() = IfFalse; + Op<-3>() = Cond; #ifndef NDEBUG AssertOK(); #endif @@ -662,14 +653,39 @@ BranchInst::BranchInst(const BranchInst &BI) : TerminatorInst(Type::VoidTy, Instruction::Br, OperandTraits::op_end(this) - BI.getNumOperands(), BI.getNumOperands()) { - OperandList[0].init(BI.getOperand(0), this); + Op<-1>() = BI.Op<-1>(); if (BI.getNumOperands() != 1) { assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); - OperandList[1].init(BI.getOperand(1), this); - OperandList[2].init(BI.getOperand(2), this); + Op<-3>() = BI.Op<-3>(); + Op<-2>() = BI.Op<-2>(); } } + +Use* Use::getPrefix() { + PointerIntPair &PotentialPrefix(this[-1].Prev); + if (PotentialPrefix.getOpaqueValue()) + return 0; + + return reinterpret_cast((char*)&PotentialPrefix + 1); +} + +BranchInst::~BranchInst() { + if (NumOperands == 1) { + if (Use *Prefix = OperandList->getPrefix()) { + Op<-1>() = 0; + // + // mark OperandList to have a special value for scrutiny + // by baseclass destructors and operator delete + OperandList = Prefix; + } else { + NumOperands = 3; + OperandList = op_begin(); + } + } +} + + BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { return getSuccessor(idx); } @@ -742,6 +758,18 @@ AllocaInst::AllocaInst(const AllocaInst &AI) Instruction::Alloca, AI.getAlignment()) { } +/// isStaticAlloca - Return true if this alloca is in the entry block of the +/// function and is a constant size. If so, the code generator will fold it +/// into the prolog/epilog code, so it is basically free. +bool AllocaInst::isStaticAlloca() const { + // Must be constant size. + if (!isa(getArraySize())) return false; + + // Must be in the entry block. + const BasicBlock *Parent = getParent(); + return Parent == &Parent->getParent()->front(); +} + MallocInst::MallocInst(const MallocInst &MI) : AllocationInst(MI.getType()->getElementType(), (Value*)MI.getOperand(0), Instruction::Malloc, MI.getAlignment()) { @@ -884,6 +912,7 @@ void LoadInst::setAlignment(unsigned Align) { //===----------------------------------------------------------------------===// void StoreInst::AssertOK() { + assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); assert(isa(getOperand(1)->getType()) && "Ptr must have pointer type!"); assert(getOperand(0)->getType() == @@ -897,8 +926,8 @@ StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) OperandTraits::op_begin(this), OperandTraits::operands(this), InsertBefore) { - Op<0>().init(val, this); - Op<1>().init(addr, this); + Op<0>() = val; + Op<1>() = addr; setVolatile(false); setAlignment(0); AssertOK(); @@ -909,8 +938,8 @@ StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) OperandTraits::op_begin(this), OperandTraits::operands(this), InsertAtEnd) { - Op<0>().init(val, this); - Op<1>().init(addr, this); + Op<0>() = val; + Op<1>() = addr; setVolatile(false); setAlignment(0); AssertOK(); @@ -922,8 +951,8 @@ StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, OperandTraits::op_begin(this), OperandTraits::operands(this), InsertBefore) { - Op<0>().init(val, this); - Op<1>().init(addr, this); + Op<0>() = val; + Op<1>() = addr; setVolatile(isVolatile); setAlignment(0); AssertOK(); @@ -935,8 +964,8 @@ StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, OperandTraits::op_begin(this), OperandTraits::operands(this), InsertBefore) { - Op<0>().init(val, this); - Op<1>().init(addr, this); + Op<0>() = val; + Op<1>() = addr; setVolatile(isVolatile); setAlignment(Align); AssertOK(); @@ -948,8 +977,8 @@ StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, OperandTraits::op_begin(this), OperandTraits::operands(this), InsertAtEnd) { - Op<0>().init(val, this); - Op<1>().init(addr, this); + Op<0>() = val; + Op<1>() = addr; setVolatile(isVolatile); setAlignment(Align); AssertOK(); @@ -961,8 +990,8 @@ StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, OperandTraits::op_begin(this), OperandTraits::operands(this), InsertAtEnd) { - Op<0>().init(val, this); - Op<1>().init(addr, this); + Op<0>() = val; + Op<1>() = addr; setVolatile(isVolatile); setAlignment(0); AssertOK(); @@ -981,31 +1010,36 @@ static unsigned retrieveAddrSpace(const Value *Val) { return cast(Val->getType())->getAddressSpace(); } -void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) { +void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx, + const std::string &Name) { assert(NumOperands == 1+NumIdx && "NumOperands not initialized?"); Use *OL = OperandList; - OL[0].init(Ptr, this); + OL[0] = Ptr; for (unsigned i = 0; i != NumIdx; ++i) - OL[i+1].init(Idx[i], this); + OL[i+1] = Idx[i]; + + setName(Name); } -void GetElementPtrInst::init(Value *Ptr, Value *Idx) { +void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) { assert(NumOperands == 2 && "NumOperands not initialized?"); Use *OL = OperandList; - OL[0].init(Ptr, this); - OL[1].init(Idx, this); + OL[0] = Ptr; + OL[1] = Idx; + + setName(Name); } GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) - : Instruction(reinterpret_cast(GEPI.getType()), GetElementPtr, + : Instruction(GEPI.getType(), GetElementPtr, OperandTraits::op_end(this) - GEPI.getNumOperands(), GEPI.getNumOperands()) { Use *OL = OperandList; Use *GEPIOL = GEPI.OperandList; for (unsigned i = 0, E = NumOperands; i != E; ++i) - OL[i].init(GEPIOL[i], this); + OL[i] = GEPIOL[i]; } GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, @@ -1015,8 +1049,7 @@ GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, GetElementPtr, OperandTraits::op_end(this) - 2, 2, InBe) { - init(Ptr, Idx); - setName(Name); + init(Ptr, Idx, Name); } GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, @@ -1026,28 +1059,64 @@ GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, GetElementPtr, OperandTraits::op_end(this) - 2, 2, IAE) { - init(Ptr, Idx); - setName(Name); + init(Ptr, Idx, Name); } -// getIndexedType - Returns the type of the element that would be loaded with -// a load instruction with the specified parameters. -// -// A null type is returned if the indices are invalid for the specified -// pointer type. -// -const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, - Value* const *Idxs, - unsigned NumIdx) { +/// getIndexedType - Returns the type of the element that would be accessed with +/// a gep instruction with the specified parameters. +/// +/// The Idxs pointer should point to a continuous piece of memory containing the +/// indices, either as Value* or uint64_t. +/// +/// A null type is returned if the indices are invalid for the specified +/// pointer type. +/// +template +static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs, + unsigned NumIdx) { const PointerType *PTy = dyn_cast(Ptr); if (!PTy) return 0; // Type isn't a pointer type! const Type *Agg = PTy->getElementType(); - // Handle the special case of the empty set index set... + // Handle the special case of the empty set index set, which is always valid. if (NumIdx == 0) return Agg; + + // If there is at least one index, the top level type must be sized, otherwise + // it cannot be 'stepped over'. We explicitly allow abstract types (those + // that contain opaque types) under the assumption that it will be resolved to + // a sane type later. + if (!Agg->isSized() && !Agg->isAbstract()) + return 0; + + unsigned CurIdx = 1; + for (; CurIdx != NumIdx; ++CurIdx) { + const CompositeType *CT = dyn_cast(Agg); + if (!CT || isa(CT)) return 0; + IndexTy Index = Idxs[CurIdx]; + if (!CT->indexValid(Index)) return 0; + Agg = CT->getTypeAtIndex(Index); + + // If the new type forwards to another type, then it is in the middle + // of being refined to another type (and hence, may have dropped all + // references to what it was using before). So, use the new forwarded + // type. + if (const Type *Ty = Agg->getForwardedType()) + Agg = Ty; + } + return CurIdx == NumIdx ? Agg : 0; +} - return ExtractValueInst::getIndexedType(Agg, Idxs+1, Idxs+NumIdx); +const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, + Value* const *Idxs, + unsigned NumIdx) { + return getIndexedTypeInternal(Ptr, Idxs, NumIdx); +} + +const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, + uint64_t const *Idxs, + unsigned NumIdx) { + return getIndexedTypeInternal(Ptr, Idxs, NumIdx); } const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { @@ -1100,8 +1169,8 @@ ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, 2, InsertBef) { assert(isValidOperands(Val, Index) && "Invalid extractelement instruction operands!"); - Op<0>().init(Val, this); - Op<1>().init(Index, this); + Op<0>() = Val; + Op<1>() = Index; setName(Name); } @@ -1115,8 +1184,8 @@ ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); assert(isValidOperands(Val, Index) && "Invalid extractelement instruction operands!"); - Op<0>().init(Val, this); - Op<1>().init(Index, this); + Op<0>() = Val; + Op<1>() = Index; setName(Name); } @@ -1131,8 +1200,8 @@ ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, assert(isValidOperands(Val, Index) && "Invalid extractelement instruction operands!"); - Op<0>().init(Val, this); - Op<1>().init(Index, this); + Op<0>() = Val; + Op<1>() = Index; setName(Name); } @@ -1147,8 +1216,8 @@ ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, assert(isValidOperands(Val, Index) && "Invalid extractelement instruction operands!"); - Op<0>().init(Val, this); - Op<1>().init(Index, this); + Op<0>() = Val; + Op<1>() = Index; setName(Name); } @@ -1167,9 +1236,9 @@ bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { InsertElementInst::InsertElementInst(const InsertElementInst &IE) : Instruction(IE.getType(), InsertElement, OperandTraits::op_begin(this), 3) { - Op<0>().init(IE.Op<0>(), this); - Op<1>().init(IE.Op<1>(), this); - Op<2>().init(IE.Op<2>(), this); + Op<0>() = IE.Op<0>(); + Op<1>() = IE.Op<1>(); + Op<2>() = IE.Op<2>(); } InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, const std::string &Name, @@ -1179,9 +1248,9 @@ InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, 3, InsertBef) { assert(isValidOperands(Vec, Elt, Index) && "Invalid insertelement instruction operands!"); - Op<0>().init(Vec, this); - Op<1>().init(Elt, this); - Op<2>().init(Index, this); + Op<0>() = Vec; + Op<1>() = Elt; + Op<2>() = Index; setName(Name); } @@ -1194,9 +1263,9 @@ InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); assert(isValidOperands(Vec, Elt, Index) && "Invalid insertelement instruction operands!"); - Op<0>().init(Vec, this); - Op<1>().init(Elt, this); - Op<2>().init(Index, this); + Op<0>() = Vec; + Op<1>() = Elt; + Op<2>() = Index; setName(Name); } @@ -1210,9 +1279,9 @@ InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, assert(isValidOperands(Vec, Elt, Index) && "Invalid insertelement instruction operands!"); - Op<0>().init(Vec, this); - Op<1>().init(Elt, this); - Op<2>().init(Index, this); + Op<0>() = Vec; + Op<1>() = Elt; + Op<2>() = Index; setName(Name); } @@ -1226,9 +1295,9 @@ InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, assert(isValidOperands(Vec, Elt, Index) && "Invalid insertelement instruction operands!"); - Op<0>().init(Vec, this); - Op<1>().init(Elt, this); - Op<2>().init(Index, this); + Op<0>() = Vec; + Op<1>() = Elt; + Op<2>() = Index; setName(Name); } @@ -1241,7 +1310,7 @@ bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, return false;// Second operand of insertelement must be vector element type. if (Index->getType() != Type::Int32Ty) - return false; // Third operand of insertelement must be uint. + return false; // Third operand of insertelement must be i32. return true; } @@ -1254,28 +1323,30 @@ ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV) : Instruction(SV.getType(), ShuffleVector, OperandTraits::op_begin(this), OperandTraits::operands(this)) { - Op<0>().init(SV.Op<0>(), this); - Op<1>().init(SV.Op<1>(), this); - Op<2>().init(SV.Op<2>(), this); + Op<0>() = SV.Op<0>(); + Op<1>() = SV.Op<1>(); + Op<2>() = SV.Op<2>(); } ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, const std::string &Name, Instruction *InsertBefore) - : Instruction(V1->getType(), ShuffleVector, - OperandTraits::op_begin(this), - OperandTraits::operands(this), - InsertBefore) { +: Instruction(VectorType::get(cast(V1->getType())->getElementType(), + cast(Mask->getType())->getNumElements()), + ShuffleVector, + OperandTraits::op_begin(this), + OperandTraits::operands(this), + InsertBefore) { assert(isValidOperands(V1, V2, Mask) && "Invalid shuffle vector instruction operands!"); - Op<0>().init(V1, this); - Op<1>().init(V2, this); - Op<2>().init(Mask, this); + Op<0>() = V1; + Op<1>() = V2; + Op<2>() = Mask; setName(Name); } ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, - const std::string &Name, + const std::string &Name, BasicBlock *InsertAtEnd) : Instruction(V1->getType(), ShuffleVector, OperandTraits::op_begin(this), @@ -1284,23 +1355,20 @@ ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, assert(isValidOperands(V1, V2, Mask) && "Invalid shuffle vector instruction operands!"); - Op<0>().init(V1, this); - Op<1>().init(V2, this); - Op<2>().init(Mask, this); + Op<0>() = V1; + Op<1>() = V2; + Op<2>() = Mask; setName(Name); } -bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, +bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, const Value *Mask) { - if (!isa(V1->getType()) || - V1->getType() != V2->getType()) + if (!isa(V1->getType()) || V1->getType() != V2->getType()) return false; const VectorType *MaskTy = dyn_cast(Mask->getType()); if (!isa(Mask) || MaskTy == 0 || - MaskTy->getElementType() != Type::Int32Ty || - MaskTy->getNumElements() != - cast(V1->getType())->getNumElements()) + MaskTy->getElementType() != Type::Int32Ty) return false; return true; } @@ -1320,10 +1388,84 @@ int ShuffleVectorInst::getMaskValue(unsigned i) const { return cast(MaskCV->getOperand(i))->getZExtValue(); } +//===----------------------------------------------------------------------===// +// InsertValueInst Class +//===----------------------------------------------------------------------===// + +void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx, + unsigned NumIdx, const std::string &Name) { + assert(NumOperands == 2 && "NumOperands not initialized?"); + Op<0>() = Agg; + Op<1>() = Val; + + Indices.insert(Indices.end(), Idx, Idx + NumIdx); + setName(Name); +} + +void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx, + const std::string &Name) { + assert(NumOperands == 2 && "NumOperands not initialized?"); + Op<0>() = Agg; + Op<1>() = Val; + + Indices.push_back(Idx); + setName(Name); +} + +InsertValueInst::InsertValueInst(const InsertValueInst &IVI) + : Instruction(IVI.getType(), InsertValue, + OperandTraits::op_begin(this), 2), + Indices(IVI.Indices) { + Op<0>() = IVI.getOperand(0); + Op<1>() = IVI.getOperand(1); +} + +InsertValueInst::InsertValueInst(Value *Agg, + Value *Val, + unsigned Idx, + const std::string &Name, + Instruction *InsertBefore) + : Instruction(Agg->getType(), InsertValue, + OperandTraits::op_begin(this), + 2, InsertBefore) { + init(Agg, Val, Idx, Name); +} + +InsertValueInst::InsertValueInst(Value *Agg, + Value *Val, + unsigned Idx, + const std::string &Name, + BasicBlock *InsertAtEnd) + : Instruction(Agg->getType(), InsertValue, + OperandTraits::op_begin(this), + 2, InsertAtEnd) { + init(Agg, Val, Idx, Name); +} + //===----------------------------------------------------------------------===// // ExtractValueInst Class //===----------------------------------------------------------------------===// +void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx, + const std::string &Name) { + assert(NumOperands == 1 && "NumOperands not initialized?"); + + Indices.insert(Indices.end(), Idx, Idx + NumIdx); + setName(Name); +} + +void ExtractValueInst::init(unsigned Idx, const std::string &Name) { + assert(NumOperands == 1 && "NumOperands not initialized?"); + + Indices.push_back(Idx); + setName(Name); +} + +ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) + : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), + Indices(EVI.Indices) { +} + // getIndexedType - Returns the type of the element that would be extracted // with an extractvalue instruction with the specified parameters. // @@ -1331,13 +1473,13 @@ int ShuffleVectorInst::getMaskValue(unsigned i) const { // pointer type. // const Type* ExtractValueInst::getIndexedType(const Type *Agg, - Value* const *Idxs, + const unsigned *Idxs, unsigned NumIdx) { unsigned CurIdx = 0; for (; CurIdx != NumIdx; ++CurIdx) { const CompositeType *CT = dyn_cast(Agg); - if (!CT || isa(CT)) return 0; - Value *Index = Idxs[CurIdx]; + if (!CT || isa(CT) || isa(CT)) return 0; + unsigned Index = Idxs[CurIdx]; if (!CT->indexValid(Index)) return 0; Agg = CT->getTypeAtIndex(Index); @@ -1351,33 +1493,52 @@ const Type* ExtractValueInst::getIndexedType(const Type *Agg, return CurIdx == NumIdx ? Agg : 0; } +const Type* ExtractValueInst::getIndexedType(const Type *Agg, + unsigned Idx) { + return getIndexedType(Agg, &Idx, 1); +} + //===----------------------------------------------------------------------===// // BinaryOperator Class //===----------------------------------------------------------------------===// +/// AdjustIType - Map Add, Sub, and Mul to FAdd, FSub, and FMul when the +/// type is floating-point, to help provide compatibility with an older API. +/// +static BinaryOperator::BinaryOps AdjustIType(BinaryOperator::BinaryOps iType, + const Type *Ty) { + // API compatibility: Adjust integer opcodes to floating-point opcodes. + if (Ty->isFPOrFPVector()) { + if (iType == BinaryOperator::Add) iType = BinaryOperator::FAdd; + else if (iType == BinaryOperator::Sub) iType = BinaryOperator::FSub; + else if (iType == BinaryOperator::Mul) iType = BinaryOperator::FMul; + } + return iType; +} + BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty, const std::string &Name, Instruction *InsertBefore) - : Instruction(Ty, iType, + : Instruction(Ty, AdjustIType(iType, Ty), OperandTraits::op_begin(this), OperandTraits::operands(this), InsertBefore) { - Op<0>().init(S1, this); - Op<1>().init(S2, this); - init(iType); + Op<0>() = S1; + Op<1>() = S2; + init(AdjustIType(iType, Ty)); setName(Name); } BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd) - : Instruction(Ty, iType, + : Instruction(Ty, AdjustIType(iType, Ty), OperandTraits::op_begin(this), OperandTraits::operands(this), InsertAtEnd) { - Op<0>().init(S1, this); - Op<1>().init(S2, this); - init(iType); + Op<0>() = S1; + Op<1>() = S2; + init(AdjustIType(iType, Ty)); setName(Name); } @@ -1390,12 +1551,19 @@ void BinaryOperator::init(BinaryOps iType) { #ifndef NDEBUG switch (iType) { case Add: case Sub: - case Mul: + case Mul: assert(getType() == LHS->getType() && "Arithmetic operation should return same type as operands!"); - assert((getType()->isInteger() || getType()->isFloatingPoint() || - isa(getType())) && - "Tried to create an arithmetic operation on a non-arithmetic type!"); + assert(getType()->isIntOrIntVector() && + "Tried to create an integer operation on a non-integer type!"); + break; + case FAdd: case FSub: + case FMul: + assert(getType() == LHS->getType() && + "Arithmetic operation should return same type as operands!"); + assert(getType()->isFPOrFPVector() && + "Tried to create a floating-point operation on a " + "non-floating-point type!"); break; case UDiv: case SDiv: @@ -1408,9 +1576,8 @@ void BinaryOperator::init(BinaryOps iType) { case FDiv: assert(getType() == LHS->getType() && "Arithmetic operation should return same type as operands!"); - assert((getType()->isFloatingPoint() || (isa(getType()) && - cast(getType())->getElementType()->isFloatingPoint())) - && "Incorrect operand type (not floating point) for FDIV"); + assert(getType()->isFPOrFPVector() && + "Incorrect operand type (not floating point) for FDIV"); break; case URem: case SRem: @@ -1423,17 +1590,18 @@ void BinaryOperator::init(BinaryOps iType) { case FRem: assert(getType() == LHS->getType() && "Arithmetic operation should return same type as operands!"); - assert((getType()->isFloatingPoint() || (isa(getType()) && - cast(getType())->getElementType()->isFloatingPoint())) - && "Incorrect operand type (not floating point) for FREM"); + assert(getType()->isFPOrFPVector() && + "Incorrect operand type (not floating point) for FREM"); break; case Shl: case LShr: case AShr: assert(getType() == LHS->getType() && "Shift operation should return same type as operands!"); - assert(getType()->isInteger() && - "Shift operation requires integer operands"); + assert((getType()->isInteger() || + (isa(getType()) && + cast(getType())->getElementType()->isInteger())) && + "Tried to create a shift operation on a non-integral type!"); break; case And: case Or: case Xor: @@ -1450,7 +1618,7 @@ void BinaryOperator::init(BinaryOps iType) { #endif } -BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, +BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, const std::string &Name, Instruction *InsertBefore) { assert(S1->getType() == S2->getType() && @@ -1458,15 +1626,15 @@ BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); } -BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, +BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, const std::string &Name, BasicBlock *InsertAtEnd) { - BinaryOperator *Res = create(Op, S1, S2, Name); + BinaryOperator *Res = Create(Op, S1, S2, Name); InsertAtEnd->getInstList().push_back(Res); return Res; } -BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, +BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name, Instruction *InsertBefore) { Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); return new BinaryOperator(Instruction::Sub, @@ -1474,7 +1642,7 @@ BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, Op->getType(), Name, InsertBefore); } -BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, +BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const std::string &Name, BasicBlock *InsertAtEnd) { Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); return new BinaryOperator(Instruction::Sub, @@ -1482,7 +1650,23 @@ BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, Op->getType(), Name, InsertAtEnd); } -BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, +BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const std::string &Name, + Instruction *InsertBefore) { + Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); + return new BinaryOperator(Instruction::FSub, + zero, Op, + Op->getType(), Name, InsertBefore); +} + +BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const std::string &Name, + BasicBlock *InsertAtEnd) { + Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); + return new BinaryOperator(Instruction::FSub, + zero, Op, + Op->getType(), Name, InsertAtEnd); +} + +BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name, Instruction *InsertBefore) { Constant *C; if (const VectorType *PTy = dyn_cast(Op->getType())) { @@ -1496,7 +1680,7 @@ BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, Op->getType(), Name, InsertBefore); } -BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, +BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name, BasicBlock *InsertAtEnd) { Constant *AllOnes; if (const VectorType *PTy = dyn_cast(Op->getType())) { @@ -1530,6 +1714,14 @@ bool BinaryOperator::isNeg(const Value *V) { return false; } +bool BinaryOperator::isFNeg(const Value *V) { + if (const BinaryOperator *Bop = dyn_cast(V)) + if (Bop->getOpcode() == Instruction::FSub) + return Bop->getOperand(0) == + ConstantExpr::getZeroValueForNegationExpr(Bop->getType()); + return false; +} + bool BinaryOperator::isNot(const Value *V) { if (const BinaryOperator *Bop = dyn_cast(V)) return (Bop->getOpcode() == Instruction::Xor && @@ -1547,6 +1739,15 @@ const Value *BinaryOperator::getNegArgument(const Value *BinOp) { return getNegArgument(const_cast(BinOp)); } +Value *BinaryOperator::getFNegArgument(Value *BinOp) { + assert(isFNeg(BinOp) && "getFNegArgument from non-'fneg' instruction!"); + return cast(BinOp)->getOperand(1); +} + +const Value *BinaryOperator::getFNegArgument(const Value *BinOp) { + return getFNegArgument(const_cast(BinOp)); +} + Value *BinaryOperator::getNotArgument(Value *BinOp) { assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); BinaryOperator *BO = cast(BinOp); @@ -1634,11 +1835,11 @@ bool CastInst::isNoopCast(const Type *IntPtrTy) const { case Instruction::BitCast: return true; // BitCast never modifies bits. case Instruction::PtrToInt: - return IntPtrTy->getPrimitiveSizeInBits() == - getType()->getPrimitiveSizeInBits(); + return IntPtrTy->getScalarSizeInBits() == + getType()->getScalarSizeInBits(); case Instruction::IntToPtr: - return IntPtrTy->getPrimitiveSizeInBits() == - getOperand(0)->getType()->getPrimitiveSizeInBits(); + return IntPtrTy->getScalarSizeInBits() == + getOperand(0)->getType()->getScalarSizeInBits(); } } @@ -1677,8 +1878,8 @@ unsigned CastInst::isEliminableCastPair( // BITCONVERT = FirstClass n/a FirstClass n/a // // NOTE: some transforms are safe, but we consider them to be non-profitable. - // For example, we could merge "fptoui double to uint" + "zext uint to ulong", - // into "fptoui double to ulong", but this loses information about the range + // For example, we could merge "fptoui double to i32" + "zext i32 to i64", + // into "fptoui double to i64", but this loses information about the range // of the produced value (we no longer know the top-part is all zeros). // Further this conversion is often much more expensive for typical hardware, // and causes issues when building libgcc. We disallow fptosi+sext for the @@ -1743,8 +1944,8 @@ unsigned CastInst::isEliminableCastPair( return 0; case 7: { // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size - unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); - unsigned MidSize = MidTy->getPrimitiveSizeInBits(); + unsigned PtrSize = IntPtrTy->getScalarSizeInBits(); + unsigned MidSize = MidTy->getScalarSizeInBits(); if (MidSize >= PtrSize) return Instruction::BitCast; return 0; @@ -1753,8 +1954,8 @@ unsigned CastInst::isEliminableCastPair( // ext, trunc -> bitcast, if the SrcTy and DstTy are same size // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) - unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); - unsigned DstSize = DstTy->getPrimitiveSizeInBits(); + unsigned SrcSize = SrcTy->getScalarSizeInBits(); + unsigned DstSize = DstTy->getScalarSizeInBits(); if (SrcSize == DstSize) return Instruction::BitCast; else if (SrcSize < DstSize) @@ -1782,9 +1983,9 @@ unsigned CastInst::isEliminableCastPair( return 0; case 13: { // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize - unsigned PtrSize = IntPtrTy->getPrimitiveSizeInBits(); - unsigned SrcSize = SrcTy->getPrimitiveSizeInBits(); - unsigned DstSize = DstTy->getPrimitiveSizeInBits(); + unsigned PtrSize = IntPtrTy->getScalarSizeInBits(); + unsigned SrcSize = SrcTy->getScalarSizeInBits(); + unsigned DstSize = DstTy->getScalarSizeInBits(); if (SrcSize <= PtrSize && SrcSize == DstSize) return Instruction::BitCast; return 0; @@ -1801,7 +2002,7 @@ unsigned CastInst::isEliminableCastPair( return 0; } -CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, +CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore) { // Construct and return the appropriate CastInst subclass switch (op) { @@ -1823,7 +2024,7 @@ CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, return 0; } -CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, +CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd) { // Construct and return the appropriate CastInst subclass switch (op) { @@ -1845,55 +2046,55 @@ CastInst *CastInst::create(Instruction::CastOps op, Value *S, const Type *Ty, return 0; } -CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty, +CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore) { - if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) - return create(Instruction::BitCast, S, Ty, Name, InsertBefore); - return create(Instruction::ZExt, S, Ty, Name, InsertBefore); + if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) + return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); + return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); } -CastInst *CastInst::createZExtOrBitCast(Value *S, const Type *Ty, +CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd) { - if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) - return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); - return create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); + if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) + return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); + return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); } -CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty, +CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore) { - if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) - return create(Instruction::BitCast, S, Ty, Name, InsertBefore); - return create(Instruction::SExt, S, Ty, Name, InsertBefore); + if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) + return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); + return Create(Instruction::SExt, S, Ty, Name, InsertBefore); } -CastInst *CastInst::createSExtOrBitCast(Value *S, const Type *Ty, +CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd) { - if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) - return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); - return create(Instruction::SExt, S, Ty, Name, InsertAtEnd); + if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) + return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); + return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); } -CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty, +CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore) { - if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) - return create(Instruction::BitCast, S, Ty, Name, InsertBefore); - return create(Instruction::Trunc, S, Ty, Name, InsertBefore); + if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) + return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); + return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); } -CastInst *CastInst::createTruncOrBitCast(Value *S, const Type *Ty, +CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd) { - if (S->getType()->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits()) - return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); - return create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); + if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) + return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); + return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); } -CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, +CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd) { assert(isa(S->getType()) && "Invalid cast"); @@ -1901,12 +2102,12 @@ CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, "Invalid cast"); if (Ty->isInteger()) - return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); - return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); + return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); + return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); } /// @brief Create a BitCast or a PtrToInt cast instruction -CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, +CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore) { assert(isa(S->getType()) && "Invalid cast"); @@ -1914,60 +2115,61 @@ CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, "Invalid cast"); if (Ty->isInteger()) - return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); - return create(Instruction::BitCast, S, Ty, Name, InsertBefore); + return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); + return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); } -CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, +CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty, bool isSigned, const std::string &Name, Instruction *InsertBefore) { assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); - unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); - unsigned DstBits = Ty->getPrimitiveSizeInBits(); + unsigned SrcBits = C->getType()->getScalarSizeInBits(); + unsigned DstBits = Ty->getScalarSizeInBits(); Instruction::CastOps opcode = (SrcBits == DstBits ? Instruction::BitCast : (SrcBits > DstBits ? Instruction::Trunc : (isSigned ? Instruction::SExt : Instruction::ZExt))); - return create(opcode, C, Ty, Name, InsertBefore); + return Create(opcode, C, Ty, Name, InsertBefore); } -CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, +CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty, bool isSigned, const std::string &Name, BasicBlock *InsertAtEnd) { - assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); - unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); - unsigned DstBits = Ty->getPrimitiveSizeInBits(); + assert(C->getType()->isIntOrIntVector() && Ty->isIntOrIntVector() && + "Invalid cast"); + unsigned SrcBits = C->getType()->getScalarSizeInBits(); + unsigned DstBits = Ty->getScalarSizeInBits(); Instruction::CastOps opcode = (SrcBits == DstBits ? Instruction::BitCast : (SrcBits > DstBits ? Instruction::Trunc : (isSigned ? Instruction::SExt : Instruction::ZExt))); - return create(opcode, C, Ty, Name, InsertAtEnd); + return Create(opcode, C, Ty, Name, InsertAtEnd); } -CastInst *CastInst::createFPCast(Value *C, const Type *Ty, +CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty, const std::string &Name, Instruction *InsertBefore) { - assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && + assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() && "Invalid cast"); - unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); - unsigned DstBits = Ty->getPrimitiveSizeInBits(); + unsigned SrcBits = C->getType()->getScalarSizeInBits(); + unsigned DstBits = Ty->getScalarSizeInBits(); Instruction::CastOps opcode = (SrcBits == DstBits ? Instruction::BitCast : (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); - return create(opcode, C, Ty, Name, InsertBefore); + return Create(opcode, C, Ty, Name, InsertBefore); } -CastInst *CastInst::createFPCast(Value *C, const Type *Ty, +CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd) { - assert(C->getType()->isFloatingPoint() && Ty->isFloatingPoint() && + assert(C->getType()->isFPOrFPVector() && Ty->isFPOrFPVector() && "Invalid cast"); - unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); - unsigned DstBits = Ty->getPrimitiveSizeInBits(); + unsigned SrcBits = C->getType()->getScalarSizeInBits(); + unsigned DstBits = Ty->getScalarSizeInBits(); Instruction::CastOps opcode = (SrcBits == DstBits ? Instruction::BitCast : (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); - return create(opcode, C, Ty, Name, InsertAtEnd); + return Create(opcode, C, Ty, Name, InsertAtEnd); } // Check whether it is valid to call getCastOpcode for these types. @@ -1980,8 +2182,8 @@ bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) { return true; // Get the bit sizes, we'll need these - unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector - unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector + unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr + unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr // Run through the possibilities ... if (DestTy->isInteger()) { // Casting to integral @@ -2039,8 +2241,8 @@ CastInst::getCastOpcode( const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) { // Get the bit sizes, we'll need these const Type *SrcTy = Src->getType(); - unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector - unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector + unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr + unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && "Only first class types are castable!"); @@ -2066,6 +2268,7 @@ CastInst::getCastOpcode( } else if (const VectorType *PTy = dyn_cast(SrcTy)) { assert(DestBits == PTy->getBitWidth() && "Casting vector to integer of different width"); + PTy = NULL; return BitCast; // Same size, no-op cast } else { assert(isa(SrcTy) && @@ -2089,7 +2292,8 @@ CastInst::getCastOpcode( } else if (const VectorType *PTy = dyn_cast(SrcTy)) { assert(DestBits == PTy->getBitWidth() && "Casting vector to floating point of different width"); - return BitCast; // same size, no-op cast + PTy = NULL; + return BitCast; // same size, no-op cast } else { assert(0 && "Casting pointer or non-first class to float"); } @@ -2097,6 +2301,7 @@ CastInst::getCastOpcode( if (const VectorType *SrcPTy = dyn_cast(SrcTy)) { assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() && "Casting vector to vector of different widths"); + SrcPTy = NULL; return BitCast; // vector -> vector } else if (DestPTy->getBitWidth() == SrcBits) { return BitCast; // float/int -> vector @@ -2138,44 +2343,49 @@ CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) { return false; // Get the size of the types in bits, we'll need this later - unsigned SrcBitSize = SrcTy->getPrimitiveSizeInBits(); - unsigned DstBitSize = DstTy->getPrimitiveSizeInBits(); + unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); + unsigned DstBitSize = DstTy->getScalarSizeInBits(); // Switch on the opcode provided switch (op) { default: return false; // This is an input error case Instruction::Trunc: - return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize; + return SrcTy->isIntOrIntVector() && + DstTy->isIntOrIntVector()&& SrcBitSize > DstBitSize; case Instruction::ZExt: - return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize; + return SrcTy->isIntOrIntVector() && + DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize; case Instruction::SExt: - return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize; + return SrcTy->isIntOrIntVector() && + DstTy->isIntOrIntVector()&& SrcBitSize < DstBitSize; case Instruction::FPTrunc: - return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && - SrcBitSize > DstBitSize; + return SrcTy->isFPOrFPVector() && + DstTy->isFPOrFPVector() && + SrcBitSize > DstBitSize; case Instruction::FPExt: - return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && - SrcBitSize < DstBitSize; + return SrcTy->isFPOrFPVector() && + DstTy->isFPOrFPVector() && + SrcBitSize < DstBitSize; case Instruction::UIToFP: case Instruction::SIToFP: if (const VectorType *SVTy = dyn_cast(SrcTy)) { if (const VectorType *DVTy = dyn_cast(DstTy)) { - return SVTy->getElementType()->isInteger() && - DVTy->getElementType()->isFloatingPoint() && + return SVTy->getElementType()->isIntOrIntVector() && + DVTy->getElementType()->isFPOrFPVector() && SVTy->getNumElements() == DVTy->getNumElements(); } } - return SrcTy->isInteger() && DstTy->isFloatingPoint(); + return SrcTy->isIntOrIntVector() && DstTy->isFPOrFPVector(); case Instruction::FPToUI: case Instruction::FPToSI: if (const VectorType *SVTy = dyn_cast(SrcTy)) { if (const VectorType *DVTy = dyn_cast(DstTy)) { - return SVTy->getElementType()->isFloatingPoint() && - DVTy->getElementType()->isInteger() && + return SVTy->getElementType()->isFPOrFPVector() && + DVTy->getElementType()->isIntOrIntVector() && SVTy->getNumElements() == DVTy->getNumElements(); } } - return SrcTy->isFloatingPoint() && DstTy->isInteger(); + return SrcTy->isFPOrFPVector() && DstTy->isIntOrIntVector(); case Instruction::PtrToInt: return isa(SrcTy) && DstTy->isInteger(); case Instruction::IntToPtr: @@ -2189,7 +2399,7 @@ CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) { // Now we know we're not dealing with a pointer/non-pointer mismatch. In all // these cases, the cast is okay if the source and destination bit widths // are identical. - return SrcBitSize == DstBitSize; + return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); } } @@ -2347,8 +2557,8 @@ CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate, OperandTraits::op_begin(this), OperandTraits::operands(this), InsertBefore) { - Op<0>().init(LHS, this); - Op<1>().init(RHS, this); + Op<0>() = LHS; + Op<1>() = RHS; SubclassData = predicate; setName(Name); } @@ -2360,48 +2570,32 @@ CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate, OperandTraits::op_begin(this), OperandTraits::operands(this), InsertAtEnd) { - Op<0>().init(LHS, this); - Op<1>().init(RHS, this); + Op<0>() = LHS; + Op<1>() = RHS; SubclassData = predicate; setName(Name); } CmpInst * -CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, +CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, const std::string &Name, Instruction *InsertBefore) { if (Op == Instruction::ICmp) { return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name, InsertBefore); } - if (Op == Instruction::FCmp) { - return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name, - InsertBefore); - } - if (Op == Instruction::VICmp) { - return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name, - InsertBefore); - } - return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name, - InsertBefore); + return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name, + InsertBefore); } CmpInst * -CmpInst::create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, +CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, const std::string &Name, BasicBlock *InsertAtEnd) { if (Op == Instruction::ICmp) { return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name, InsertAtEnd); } - if (Op == Instruction::FCmp) { - return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name, - InsertAtEnd); - } - if (Op == Instruction::VICmp) { - return new VICmpInst(CmpInst::Predicate(predicate), S1, S2, Name, - InsertAtEnd); - } - return new VFCmpInst(CmpInst::Predicate(predicate), S1, S2, Name, - InsertAtEnd); + return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name, + InsertAtEnd); } void CmpInst::swapOperands() { @@ -2424,10 +2618,9 @@ bool CmpInst::isEquality() { } -ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) { +CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { switch (pred) { - default: - assert(!"Unknown icmp predicate!"); + default: assert(!"Unknown cmp predicate!"); case ICMP_EQ: return ICMP_NE; case ICMP_NE: return ICMP_EQ; case ICMP_UGT: return ICMP_ULE; @@ -2438,22 +2631,23 @@ ICmpInst::Predicate ICmpInst::getInversePredicate(Predicate pred) { case ICMP_SLT: return ICMP_SGE; case ICMP_SGE: return ICMP_SLT; case ICMP_SLE: return ICMP_SGT; - } -} -ICmpInst::Predicate ICmpInst::getSwappedPredicate(Predicate pred) { - switch (pred) { - default: assert(! "Unknown icmp predicate!"); - case ICMP_EQ: case ICMP_NE: - return pred; - case ICMP_SGT: return ICMP_SLT; - case ICMP_SLT: return ICMP_SGT; - case ICMP_SGE: return ICMP_SLE; - case ICMP_SLE: return ICMP_SGE; - case ICMP_UGT: return ICMP_ULT; - case ICMP_ULT: return ICMP_UGT; - case ICMP_UGE: return ICMP_ULE; - case ICMP_ULE: return ICMP_UGE; + case FCMP_OEQ: return FCMP_UNE; + case FCMP_ONE: return FCMP_UEQ; + case FCMP_OGT: return FCMP_ULE; + case FCMP_OLT: return FCMP_UGE; + case FCMP_OGE: return FCMP_ULT; + case FCMP_OLE: return FCMP_UGT; + case FCMP_UEQ: return FCMP_ONE; + case FCMP_UNE: return FCMP_OEQ; + case FCMP_UGT: return FCMP_OLE; + case FCMP_ULT: return FCMP_OGE; + case FCMP_UGE: return FCMP_OLT; + case FCMP_ULE: return FCMP_OGT; + case FCMP_ORD: return FCMP_UNO; + case FCMP_UNO: return FCMP_ORD; + case FCMP_TRUE: return FCMP_FALSE; + case FCMP_FALSE: return FCMP_TRUE; } } @@ -2529,32 +2723,20 @@ ICmpInst::makeConstantRange(Predicate pred, const APInt &C) { return ConstantRange(Lower, Upper); } -FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) { - switch (pred) { - default: - assert(!"Unknown icmp predicate!"); - case FCMP_OEQ: return FCMP_UNE; - case FCMP_ONE: return FCMP_UEQ; - case FCMP_OGT: return FCMP_ULE; - case FCMP_OLT: return FCMP_UGE; - case FCMP_OGE: return FCMP_ULT; - case FCMP_OLE: return FCMP_UGT; - case FCMP_UEQ: return FCMP_ONE; - case FCMP_UNE: return FCMP_OEQ; - case FCMP_UGT: return FCMP_OLE; - case FCMP_ULT: return FCMP_OGE; - case FCMP_UGE: return FCMP_OLT; - case FCMP_ULE: return FCMP_OGT; - case FCMP_ORD: return FCMP_UNO; - case FCMP_UNO: return FCMP_ORD; - case FCMP_TRUE: return FCMP_FALSE; - case FCMP_FALSE: return FCMP_TRUE; - } -} - -FCmpInst::Predicate FCmpInst::getSwappedPredicate(Predicate pred) { +CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { switch (pred) { - default: assert(!"Unknown fcmp predicate!"); + default: assert(!"Unknown cmp predicate!"); + case ICMP_EQ: case ICMP_NE: + return pred; + case ICMP_SGT: return ICMP_SLT; + case ICMP_SLT: return ICMP_SGT; + case ICMP_SGE: return ICMP_SLE; + case ICMP_SLE: return ICMP_SGE; + case ICMP_UGT: return ICMP_ULT; + case ICMP_ULT: return ICMP_UGT; + case ICMP_UGE: return ICMP_ULE; + case ICMP_ULE: return ICMP_UGE; + case FCMP_FALSE: case FCMP_TRUE: case FCMP_OEQ: case FCMP_ONE: case FCMP_UEQ: case FCMP_UNE: @@ -2615,8 +2797,8 @@ void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) { NumOperands = 2; OperandList = allocHungoffUses(ReservedSpace); - OperandList[0].init(Value, this); - OperandList[1].init(Default, this); + OperandList[0] = Value; + OperandList[1] = Default; } /// SwitchInst ctor - Create a new switch instruction, specifying a value to @@ -2644,8 +2826,8 @@ SwitchInst::SwitchInst(const SwitchInst &SI) allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) { Use *OL = OperandList, *InOL = SI.OperandList; for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) { - OL[i].init(InOL[i], this); - OL[i+1].init(InOL[i+1], this); + OL[i] = InOL[i]; + OL[i+1] = InOL[i+1]; } } @@ -2663,8 +2845,8 @@ void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { // Initialize some new operands. assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); NumOperands = OpNo+2; - OperandList[OpNo].init(OnVal, this); - OperandList[OpNo+1].init(Dest, this); + OperandList[OpNo] = OnVal; + OperandList[OpNo+1] = Dest; } /// removeCase - This method removes the specified successor from the switch @@ -2718,7 +2900,7 @@ void SwitchInst::resizeOperands(unsigned NumOps) { Use *NewOps = allocHungoffUses(NumOps); Use *OldOps = OperandList; for (unsigned i = 0; i != e; ++i) { - NewOps[i].init(OldOps[i], this); + NewOps[i] = OldOps[i]; } OperandList = NewOps; if (OldOps) Use::zap(OldOps, OldOps + e, true); @@ -2735,43 +2917,6 @@ void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { setSuccessor(idx, B); } -//===----------------------------------------------------------------------===// -// GetResultInst Implementation -//===----------------------------------------------------------------------===// - -GetResultInst::GetResultInst(Value *Aggregate, unsigned Index, - const std::string &Name, - Instruction *InsertBef) - : UnaryInstruction(cast(Aggregate->getType()) - ->getElementType(Index), - GetResult, Aggregate, InsertBef), - Idx(Index) { - assert(isValidOperands(Aggregate, Index) - && "Invalid GetResultInst operands!"); - setName(Name); -} - -bool GetResultInst::isValidOperands(const Value *Aggregate, unsigned Index) { - if (!Aggregate) - return false; - - if (const StructType *STy = dyn_cast(Aggregate->getType())) { - unsigned NumElements = STy->getNumElements(); - if (Index >= NumElements || NumElements == 0) - return false; - - // getresult aggregate value's element types are restricted to - // avoid nested aggregates. - for (unsigned i = 0; i < NumElements; ++i) - if (!STy->getElementType(i)->isFirstClassType()) - return false; - - // Otherwise, Aggregate is valid. - return true; - } - return false; -} - // Define these methods here so vtables don't get emitted into every translation // unit that uses these classes. @@ -2780,7 +2925,7 @@ GetElementPtrInst *GetElementPtrInst::clone() const { } BinaryOperator *BinaryOperator::clone() const { - return create(getOpcode(), Op<0>(), Op<1>()); + return Create(getOpcode(), Op<0>(), Op<1>()); } FCmpInst* FCmpInst::clone() const { @@ -2790,13 +2935,14 @@ ICmpInst* ICmpInst::clone() const { return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); } -VFCmpInst* VFCmpInst::clone() const { - return new VFCmpInst(getPredicate(), Op<0>(), Op<1>()); +ExtractValueInst *ExtractValueInst::clone() const { + return new ExtractValueInst(*this); } -VICmpInst* VICmpInst::clone() const { - return new VICmpInst(getPredicate(), Op<0>(), Op<1>()); +InsertValueInst *InsertValueInst::clone() const { + return new 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)); } @@ -2836,7 +2982,8 @@ ReturnInst *ReturnInst::clone() const { return new(getNumOperands()) ReturnInst(*this); } BranchInst *BranchInst::clone() const { - return new(getNumOperands()) BranchInst(*this); + unsigned Ops(getNumOperands()); + return new(Ops, Ops == 1) BranchInst(*this); } SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); } InvokeInst *InvokeInst::clone() const { @@ -2844,4 +2991,3 @@ InvokeInst *InvokeInst::clone() const { } UnwindInst *UnwindInst::clone() const { return new UnwindInst(); } UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();} -GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }