X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FVMCore%2FInstructions.cpp;h=3421292930d490f4995e436b2357ce21a35cf386;hb=629c1a3f78494d0dd769fe82bd2bd17df0555843;hp=bfda46d983199935ea485a13c2a4d4973c95b371;hpb=c67bdc288aac130d88630f7fa95ceca6bcf95077;p=oota-llvm.git diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index bfda46d9831..3421292930d 100644 --- a/lib/VMCore/Instructions.cpp +++ b/lib/VMCore/Instructions.cpp @@ -2,8 +2,8 @@ // // The LLVM Compiler Infrastructure // -// This file was developed by the LLVM research group and is distributed under -// the University of Illinois Open Source License. See LICENSE.TXT for details. +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // @@ -12,16 +12,23 @@ // //===----------------------------------------------------------------------===// -#include "llvm/BasicBlock.h" #include "llvm/Constants.h" #include "llvm/DerivedTypes.h" #include "llvm/Function.h" #include "llvm/Instructions.h" -#include "llvm/ParameterAttributes.h" #include "llvm/Support/CallSite.h" #include "llvm/Support/ConstantRange.h" +#include "llvm/Support/MathExtras.h" using namespace llvm; +//===----------------------------------------------------------------------===// +// CallSite Class +//===----------------------------------------------------------------------===// + +CallSite::CallSite(Instruction *C) { + assert((isa(C) || isa(C)) && "Not a call!"); + I = C; +} unsigned CallSite::getCallingConv() const { if (CallInst *CI = dyn_cast(I)) return CI->getCallingConv(); @@ -34,9 +41,62 @@ void CallSite::setCallingConv(unsigned CC) { else cast(I)->setCallingConv(CC); } +const PAListPtr &CallSite::getParamAttrs() const { + if (CallInst *CI = dyn_cast(I)) + return CI->getParamAttrs(); + else + return cast(I)->getParamAttrs(); +} +void CallSite::setParamAttrs(const PAListPtr &PAL) { + if (CallInst *CI = dyn_cast(I)) + CI->setParamAttrs(PAL); + else + cast(I)->setParamAttrs(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); +} +uint16_t CallSite::getParamAlignment(uint16_t i) const { + if (CallInst *CI = dyn_cast(I)) + return CI->getParamAlignment(i); + else + return cast(I)->getParamAlignment(i); +} +bool CallSite::doesNotAccessMemory() const { + if (CallInst *CI = dyn_cast(I)) + return CI->doesNotAccessMemory(); + else + return cast(I)->doesNotAccessMemory(); +} +bool CallSite::onlyReadsMemory() const { + if (CallInst *CI = dyn_cast(I)) + return CI->onlyReadsMemory(); + else + return cast(I)->onlyReadsMemory(); +} +bool CallSite::doesNotThrow() const { + if (CallInst *CI = dyn_cast(I)) + return CI->doesNotThrow(); + else + return cast(I)->doesNotThrow(); +} +void CallSite::setDoesNotThrow(bool doesNotThrow) { + if (CallInst *CI = dyn_cast(I)) + CI->setDoesNotThrow(doesNotThrow); + else + cast(I)->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; +} //===----------------------------------------------------------------------===// // TerminatorInst Class @@ -46,28 +106,32 @@ void CallSite::setCallingConv(unsigned CC) { TerminatorInst::~TerminatorInst() { } +//===----------------------------------------------------------------------===// +// UnaryInstruction Class +//===----------------------------------------------------------------------===// + // Out of line virtual method, so the vtable, etc has a home. UnaryInstruction::~UnaryInstruction() { } - //===----------------------------------------------------------------------===// // PHINode Class //===----------------------------------------------------------------------===// PHINode::PHINode(const PHINode &PN) : Instruction(PN.getType(), Instruction::PHI, - new Use[PN.getNumOperands()], PN.getNumOperands()), + allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()), 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() { - delete [] OperandList; + if (OperandList) + dropHungoffUses(OperandList); } // removeIncomingValue - Remove an incoming value. This is useful if a @@ -110,8 +174,9 @@ Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { /// 3. If NumOps == NumOperands, trim the reserved space. /// void PHINode::resizeOperands(unsigned NumOps) { + unsigned e = getNumOperands(); if (NumOps == 0) { - NumOps = (getNumOperands())*3/2; + NumOps = e*3/2; if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common. } else if (NumOps*2 > NumOperands) { // No resize needed. @@ -123,14 +188,13 @@ void PHINode::resizeOperands(unsigned NumOps) { } ReservedSpace = NumOps; - Use *NewOps = new Use[NumOps]; Use *OldOps = OperandList; - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { - NewOps[i].init(OldOps[i], this); - OldOps[i].set(0); + Use *NewOps = allocHungoffUses(NumOps); + for (unsigned i = 0; i != e; ++i) { + NewOps[i] = OldOps[i]; } - delete [] OldOps; OperandList = NewOps; + if (OldOps) Use::zap(OldOps, OldOps + e, true); } /// hasConstantValue - If the specified PHI node always merges together the same @@ -138,11 +202,12 @@ void PHINode::resizeOperands(unsigned NumOps) { /// Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const { // If the PHI node only has one incoming value, eliminate the PHI node... - if (getNumIncomingValues() == 1) + if (getNumIncomingValues() == 1) { if (getIncomingValue(0) != this) // not X = phi X return getIncomingValue(0); else return UndefValue::get(getType()); // Self cycle is dead. + } // Otherwise if all of the incoming values are the same for the PHI, replace // the PHI node with the incoming value. @@ -150,13 +215,14 @@ Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const { Value *InVal = 0; bool HasUndefInput = false; for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) - if (isa(getIncomingValue(i))) + if (isa(getIncomingValue(i))) { HasUndefInput = true; - else if (getIncomingValue(i) != this) // Not the PHI node itself... + } else if (getIncomingValue(i) != this) { // Not the PHI node itself... if (InVal && getIncomingValue(i) != InVal) return 0; // Not the same, bail out. else InVal = getIncomingValue(i); + } // The only case that could cause InVal to be null is if we have a PHI node // that only has entries for itself. In this case, there is no entry into the @@ -185,15 +251,12 @@ Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const { //===----------------------------------------------------------------------===// CallInst::~CallInst() { - delete [] OperandList; - delete ParamAttrs; // FIXME: ParamAttrsList should be uniqued! } void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) { - ParamAttrs = 0; - NumOperands = NumParams+1; - Use *OL = OperandList = new Use[NumParams+1]; - OL[0].init(Func, this); + assert(NumOperands == NumParams+1 && "NumOperands not set up?"); + Use *OL = OperandList; + OL[0] = Func; const FunctionType *FTy = cast(cast(Func->getType())->getElementType()); @@ -206,17 +269,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) { - ParamAttrs = 0; - NumOperands = 3; - Use *OL = OperandList = new Use[3]; - OL[0].init(Func, this); - OL[1].init(Actual1, this); - OL[2].init(Actual2, this); + assert(NumOperands == 3 && "NumOperands not set up?"); + Use *OL = OperandList; + OL[0] = Func; + OL[1] = Actual1; + OL[2] = Actual2; const FunctionType *FTy = cast(cast(Func->getType())->getElementType()); @@ -234,11 +296,10 @@ void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { } void CallInst::init(Value *Func, Value *Actual) { - ParamAttrs = 0; - NumOperands = 2; - Use *OL = OperandList = new Use[2]; - OL[0].init(Func, this); - OL[1].init(Actual, this); + assert(NumOperands == 2 && "NumOperands not set up?"); + Use *OL = OperandList; + OL[0] = Func; + OL[1] = Actual; const FunctionType *FTy = cast(cast(Func->getType())->getElementType()); @@ -253,10 +314,9 @@ void CallInst::init(Value *Func, Value *Actual) { } void CallInst::init(Value *Func) { - ParamAttrs = 0; - NumOperands = 1; - Use *OL = OperandList = new Use[1]; - OL[0].init(Func, this); + assert(NumOperands == 1 && "NumOperands not set up?"); + Use *OL = OperandList; + OL[0] = Func; const FunctionType *FTy = cast(cast(Func->getType())->getElementType()); @@ -265,46 +325,13 @@ void CallInst::init(Value *Func) { assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); } -CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs, - const std::string &Name, BasicBlock *InsertAtEnd) - : Instruction(cast(cast(Func->getType()) - ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, InsertAtEnd) { - init(Func, Args, NumArgs); - setName(Name); -} -CallInst::CallInst(Value *Func, Value* const *Args, unsigned NumArgs, - const std::string &Name, Instruction *InsertBefore) -: Instruction(cast(cast(Func->getType()) - ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, InsertBefore) { - init(Func, Args, NumArgs); - setName(Name); -} - -CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, - const std::string &Name, Instruction *InsertBefore) - : Instruction(cast(cast(Func->getType()) - ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, InsertBefore) { - init(Func, Actual1, Actual2); - setName(Name); -} - -CallInst::CallInst(Value *Func, Value *Actual1, Value *Actual2, - const std::string &Name, BasicBlock *InsertAtEnd) - : Instruction(cast(cast(Func->getType()) - ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, InsertAtEnd) { - init(Func, Actual1, Actual2); - setName(Name); -} - CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, Instruction *InsertBefore) : Instruction(cast(cast(Func->getType()) ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, InsertBefore) { + Instruction::Call, + OperandTraits::op_end(this) - 2, + 2, InsertBefore) { init(Func, Actual); setName(Name); } @@ -313,16 +340,19 @@ CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, BasicBlock *InsertAtEnd) : Instruction(cast(cast(Func->getType()) ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, InsertAtEnd) { + Instruction::Call, + OperandTraits::op_end(this) - 2, + 2, InsertAtEnd) { init(Func, Actual); setName(Name); } - CallInst::CallInst(Value *Func, const std::string &Name, Instruction *InsertBefore) : Instruction(cast(cast(Func->getType()) ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, InsertBefore) { + Instruction::Call, + OperandTraits::op_end(this) - 1, + 1, InsertBefore) { init(Func); setName(Name); } @@ -331,20 +361,46 @@ CallInst::CallInst(Value *Func, const std::string &Name, BasicBlock *InsertAtEnd) : Instruction(cast(cast(Func->getType()) ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, InsertAtEnd) { + Instruction::Call, + OperandTraits::op_end(this) - 1, + 1, InsertAtEnd) { init(Func); setName(Name); } CallInst::CallInst(const CallInst &CI) - : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()], + : Instruction(CI.getType(), Instruction::Call, + OperandTraits::op_end(this) - CI.getNumOperands(), CI.getNumOperands()) { - ParamAttrs = 0; + setParamAttrs(CI.getParamAttrs()); 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]; +} + +void CallInst::addParamAttr(unsigned i, ParameterAttributes attr) { + PAListPtr PAL = getParamAttrs(); + PAL = PAL.addAttr(i, attr); + setParamAttrs(PAL); +} + +bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const { + if (ParamAttrs.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); } @@ -352,25 +408,19 @@ CallInst::CallInst(const CallInst &CI) // InvokeInst Implementation //===----------------------------------------------------------------------===// -InvokeInst::~InvokeInst() { - delete [] OperandList; - delete ParamAttrs; // FIXME: ParamAttrsList should be uniqued! -} - void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, Value* const *Args, unsigned NumArgs) { - ParamAttrs = 0; - NumOperands = 3+NumArgs; - Use *OL = OperandList = new Use[3+NumArgs]; - OL[0].init(Fn, this); - OL[1].init(IfNormal, this); - OL[2].init(IfException, this); + assert(NumOperands == 3+NumArgs && "NumOperands not set up?"); + Use *OL = OperandList; + OL[0] = Fn; + OL[1] = IfNormal; + OL[2] = IfException; const FunctionType *FTy = cast(cast(Fn->getType())->getElementType()); FTy = FTy; // silence warning. - assert((NumArgs == FTy->getNumParams()) || - (FTy->isVarArg() && NumArgs > FTy->getNumParams()) && + assert(((NumArgs == FTy->getNumParams()) || + (FTy->isVarArg() && NumArgs > FTy->getNumParams())) && "Calling a function with bad signature"); for (unsigned i = 0, e = NumArgs; i != e; i++) { @@ -378,40 +428,20 @@ 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]; } } -InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, - BasicBlock *IfException, - Value* const *Args, unsigned NumArgs, - const std::string &Name, Instruction *InsertBefore) - : TerminatorInst(cast(cast(Fn->getType()) - ->getElementType())->getReturnType(), - Instruction::Invoke, 0, 0, InsertBefore) { - init(Fn, IfNormal, IfException, Args, NumArgs); - setName(Name); -} - -InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, - BasicBlock *IfException, - Value* const *Args, unsigned NumArgs, - const std::string &Name, BasicBlock *InsertAtEnd) - : TerminatorInst(cast(cast(Fn->getType()) - ->getElementType())->getReturnType(), - Instruction::Invoke, 0, 0, InsertAtEnd) { - init(Fn, IfNormal, IfException, Args, NumArgs); - setName(Name); -} - InvokeInst::InvokeInst(const InvokeInst &II) : TerminatorInst(II.getType(), Instruction::Invoke, - new Use[II.getNumOperands()], II.getNumOperands()) { - ParamAttrs = 0; + OperandTraits::op_end(this) + - II.getNumOperands(), + II.getNumOperands()) { + setParamAttrs(II.getParamAttrs()); 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 { @@ -424,6 +454,29 @@ void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { return setSuccessor(idx, B); } +bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const { + if (ParamAttrs.paramHasAttr(i, attr)) + return true; + if (const Function *F = getCalledFunction()) + return F->paramHasAttr(i, attr); + return false; +} + +void InvokeInst::addParamAttr(unsigned i, ParameterAttributes attr) { + PAListPtr PAL = getParamAttrs(); + PAL = PAL.addAttr(i, attr); + setParamAttrs(PAL); +} + +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); +} + //===----------------------------------------------------------------------===// // ReturnInst Implementation @@ -431,31 +484,74 @@ void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { ReturnInst::ReturnInst(const ReturnInst &RI) : TerminatorInst(Type::VoidTy, Instruction::Ret, - &RetVal, RI.getNumOperands()) { - if (RI.getNumOperands()) - RetVal.init(RI.RetVal, this); + OperandTraits::op_end(this) + - RI.getNumOperands(), + RI.getNumOperands()) { + unsigned N = RI.getNumOperands(); + if (N == 1) + Op<0>() = RI.Op<0>(); + else if (N) { + Use *OL = OperandList; + for (unsigned i = 0; i < N; ++i) + OL[i] = RI.getOperand(i); + } } ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore) - : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) { - init(retVal); + : TerminatorInst(Type::VoidTy, Instruction::Ret, + OperandTraits::op_end(this) - (retVal != 0), + retVal != 0, InsertBefore) { + if (retVal) + init(&retVal, 1); } ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd) - : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) { - init(retVal); + : TerminatorInst(Type::VoidTy, Instruction::Ret, + OperandTraits::op_end(this) - (retVal != 0), + retVal != 0, InsertAtEnd) { + if (retVal) + init(&retVal, 1); } ReturnInst::ReturnInst(BasicBlock *InsertAtEnd) - : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, 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>() = V; + return; + } -void ReturnInst::init(Value *retVal) { - if (retVal && retVal->getType() != Type::VoidTy) { - assert(!isa(retVal) && + 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"); - NumOperands = 1; - RetVal.init(retVal, this); + OL[i] = V; } } @@ -463,8 +559,8 @@ unsigned ReturnInst::getNumSuccessorsV() const { return getNumSuccessors(); } -// Out-of-line ReturnInst method, put here so the C++ compiler can choose to -// emit the vtable for the class in this translation unit. +/// Out-of-line ReturnInst method, put here so the C++ compiler can choose to +/// emit the vtable for the class in this translation unit. void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { assert(0 && "ReturnInst has no successors!"); } @@ -475,6 +571,8 @@ BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { return 0; } +ReturnInst::~ReturnInst() { +} //===----------------------------------------------------------------------===// // UnwindInst Implementation @@ -538,33 +636,41 @@ void BranchInst::AssertOK() { } BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) - : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) { + : TerminatorInst(Type::VoidTy, Instruction::Br, + OperandTraits::op_end(this) - 1, + 1, InsertBefore) { assert(IfTrue != 0 && "Branch destination may not be null!"); - Ops[0].init(reinterpret_cast(IfTrue), this); + Op<0>() = IfTrue; } BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, Instruction *InsertBefore) -: TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertBefore) { - Ops[0].init(reinterpret_cast(IfTrue), this); - Ops[1].init(reinterpret_cast(IfFalse), this); - Ops[2].init(Cond, this); + : TerminatorInst(Type::VoidTy, Instruction::Br, + OperandTraits::op_end(this) - 3, + 3, InsertBefore) { + Op<0>() = IfTrue; + Op<1>() = IfFalse; + Op<2>() = Cond; #ifndef NDEBUG AssertOK(); #endif } BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) - : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) { + : TerminatorInst(Type::VoidTy, Instruction::Br, + OperandTraits::op_end(this) - 1, + 1, InsertAtEnd) { assert(IfTrue != 0 && "Branch destination may not be null!"); - Ops[0].init(reinterpret_cast(IfTrue), this); + Op<0>() = IfTrue; } BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, BasicBlock *InsertAtEnd) - : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 3, InsertAtEnd) { - Ops[0].init(reinterpret_cast(IfTrue), this); - Ops[1].init(reinterpret_cast(IfFalse), this); - Ops[2].init(Cond, this); + : TerminatorInst(Type::VoidTy, Instruction::Br, + OperandTraits::op_end(this) - 3, + 3, InsertAtEnd) { + Op<0>() = IfTrue; + Op<1>() = IfFalse; + Op<2>() = Cond; #ifndef NDEBUG AssertOK(); #endif @@ -572,12 +678,14 @@ BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, BranchInst::BranchInst(const BranchInst &BI) : - TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) { - OperandList[0].init(BI.getOperand(0), this); + TerminatorInst(Type::VoidTy, Instruction::Br, + OperandTraits::op_end(this) - BI.getNumOperands(), + BI.getNumOperands()) { + OperandList[0] = BI.getOperand(0); 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); + OperandList[1] = BI.getOperand(1); + OperandList[2] = BI.getOperand(2); } } @@ -601,7 +709,7 @@ static Value *getAISize(Value *Amt) { Amt = ConstantInt::get(Type::Int32Ty, 1); else { assert(!isa(Amt) && - "Passed basic block into allocation size parameter! Ue other ctor"); + "Passed basic block into allocation size parameter! Use other ctor"); assert(Amt->getType() == Type::Int32Ty && "Malloc/Allocation array size is not a 32-bit integer!"); } @@ -611,9 +719,9 @@ static Value *getAISize(Value *Amt) { AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align, const std::string &Name, Instruction *InsertBefore) - : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), - InsertBefore), Alignment(Align) { - assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); + : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize), + InsertBefore) { + setAlignment(Align); assert(Ty != Type::VoidTy && "Cannot allocate void!"); setName(Name); } @@ -621,9 +729,9 @@ AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align, const std::string &Name, BasicBlock *InsertAtEnd) - : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), - InsertAtEnd), Alignment(Align) { - assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); + : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize), + InsertAtEnd) { + setAlignment(Align); assert(Ty != Type::VoidTy && "Cannot allocate void!"); setName(Name); } @@ -632,6 +740,12 @@ AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, AllocationInst::~AllocationInst() { } +void AllocationInst::setAlignment(unsigned Align) { + assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); + SubclassData = Log2_32(Align) + 1; + assert(getAlignment() == Align && "Alignment representation error!"); +} + bool AllocationInst::isArrayAllocation() const { if (ConstantInt *CI = dyn_cast(getOperand(0))) return CI->getZExtValue() != 1; @@ -685,6 +799,7 @@ LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef) : UnaryInstruction(cast(Ptr->getType())->getElementType(), Load, Ptr, InsertBef) { setVolatile(false); + setAlignment(0); AssertOK(); setName(Name); } @@ -693,6 +808,7 @@ LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE) : UnaryInstruction(cast(Ptr->getType())->getElementType(), Load, Ptr, InsertAE) { setVolatile(false); + setAlignment(0); AssertOK(); setName(Name); } @@ -702,6 +818,27 @@ LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, : UnaryInstruction(cast(Ptr->getType())->getElementType(), Load, Ptr, InsertBef) { setVolatile(isVolatile); + setAlignment(0); + AssertOK(); + setName(Name); +} + +LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, + unsigned Align, Instruction *InsertBef) + : UnaryInstruction(cast(Ptr->getType())->getElementType(), + Load, Ptr, InsertBef) { + setVolatile(isVolatile); + setAlignment(Align); + AssertOK(); + setName(Name); +} + +LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, + unsigned Align, BasicBlock *InsertAE) + : UnaryInstruction(cast(Ptr->getType())->getElementType(), + Load, Ptr, InsertAE) { + setVolatile(isVolatile); + setAlignment(Align); AssertOK(); setName(Name); } @@ -711,6 +848,7 @@ LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, : UnaryInstruction(cast(Ptr->getType())->getElementType(), Load, Ptr, InsertAE) { setVolatile(isVolatile); + setAlignment(0); AssertOK(); setName(Name); } @@ -721,6 +859,7 @@ LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) : UnaryInstruction(cast(Ptr->getType())->getElementType(), Load, Ptr, InsertBef) { setVolatile(false); + setAlignment(0); AssertOK(); if (Name && Name[0]) setName(Name); } @@ -729,6 +868,7 @@ LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) : UnaryInstruction(cast(Ptr->getType())->getElementType(), Load, Ptr, InsertAE) { setVolatile(false); + setAlignment(0); AssertOK(); if (Name && Name[0]) setName(Name); } @@ -738,6 +878,7 @@ LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, : UnaryInstruction(cast(Ptr->getType())->getElementType(), Load, Ptr, InsertBef) { setVolatile(isVolatile); + setAlignment(0); AssertOK(); if (Name && Name[0]) setName(Name); } @@ -747,10 +888,15 @@ LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, : UnaryInstruction(cast(Ptr->getType())->getElementType(), Load, Ptr, InsertAE) { setVolatile(isVolatile); + setAlignment(0); AssertOK(); if (Name && Name[0]) setName(Name); } +void LoadInst::setAlignment(unsigned Align) { + assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); + SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1); +} //===----------------------------------------------------------------------===// // StoreInst Implementation @@ -766,132 +912,144 @@ void StoreInst::AssertOK() { StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) - : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) { - Ops[0].init(val, this); - Ops[1].init(addr, this); + : Instruction(Type::VoidTy, Store, + OperandTraits::op_begin(this), + OperandTraits::operands(this), + InsertBefore) { + Op<0>() = val; + Op<1>() = addr; setVolatile(false); + setAlignment(0); AssertOK(); } StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) - : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) { - Ops[0].init(val, this); - Ops[1].init(addr, this); + : Instruction(Type::VoidTy, Store, + OperandTraits::op_begin(this), + OperandTraits::operands(this), + InsertAtEnd) { + Op<0>() = val; + Op<1>() = addr; setVolatile(false); + setAlignment(0); AssertOK(); } StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Instruction *InsertBefore) - : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) { - Ops[0].init(val, this); - Ops[1].init(addr, this); + : Instruction(Type::VoidTy, Store, + OperandTraits::op_begin(this), + OperandTraits::operands(this), + InsertBefore) { + Op<0>() = val; + Op<1>() = addr; + setVolatile(isVolatile); + setAlignment(0); + AssertOK(); +} + +StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, + unsigned Align, Instruction *InsertBefore) + : Instruction(Type::VoidTy, Store, + OperandTraits::op_begin(this), + OperandTraits::operands(this), + InsertBefore) { + Op<0>() = val; + Op<1>() = addr; + setVolatile(isVolatile); + setAlignment(Align); + AssertOK(); +} + +StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, + unsigned Align, BasicBlock *InsertAtEnd) + : Instruction(Type::VoidTy, Store, + OperandTraits::op_begin(this), + OperandTraits::operands(this), + InsertAtEnd) { + Op<0>() = val; + Op<1>() = addr; setVolatile(isVolatile); + setAlignment(Align); AssertOK(); } StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, BasicBlock *InsertAtEnd) - : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) { - Ops[0].init(val, this); - Ops[1].init(addr, this); + : Instruction(Type::VoidTy, Store, + OperandTraits::op_begin(this), + OperandTraits::operands(this), + InsertAtEnd) { + Op<0>() = val; + Op<1>() = addr; setVolatile(isVolatile); + setAlignment(0); AssertOK(); } +void StoreInst::setAlignment(unsigned Align) { + assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); + SubclassData = (SubclassData & 1) | ((Log2_32(Align)+1)<<1); +} + //===----------------------------------------------------------------------===// // GetElementPtrInst Implementation //===----------------------------------------------------------------------===// -// checkType - Simple wrapper function to give a better assertion failure -// message on bad indexes for a gep instruction. -// -static inline const Type *checkType(const Type *Ty) { - assert(Ty && "Invalid GetElementPtrInst indices for type!"); - return Ty; +static unsigned retrieveAddrSpace(const Value *Val) { + return cast(Val->getType())->getAddressSpace(); } -void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) { - NumOperands = 1+NumIdx; - Use *OL = OperandList = new Use[NumOperands]; - OL[0].init(Ptr, this); +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] = Ptr; for (unsigned i = 0; i != NumIdx; ++i) - OL[i+1].init(Idx[i], this); -} - -void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) { - NumOperands = 3; - Use *OL = OperandList = new Use[3]; - OL[0].init(Ptr, this); - OL[1].init(Idx0, this); - OL[2].init(Idx1, this); -} + OL[i+1] = Idx[i]; -void GetElementPtrInst::init(Value *Ptr, Value *Idx) { - NumOperands = 2; - Use *OL = OperandList = new Use[2]; - OL[0].init(Ptr, this); - OL[1].init(Idx, this); + setName(Name); } +void GetElementPtrInst::init(Value *Ptr, Value *Idx, const std::string &Name) { + assert(NumOperands == 2 && "NumOperands not initialized?"); + Use *OL = OperandList; + OL[0] = Ptr; + OL[1] = Idx; -GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx, - unsigned NumIdx, - const std::string &Name, Instruction *InBe) -: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), - Idx, NumIdx, true))), - GetElementPtr, 0, 0, InBe) { - init(Ptr, Idx, NumIdx); setName(Name); } -GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value* const *Idx, - unsigned NumIdx, - const std::string &Name, BasicBlock *IAE) -: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), - Idx, NumIdx, true))), - GetElementPtr, 0, 0, IAE) { - init(Ptr, Idx, NumIdx); - setName(Name); +GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) + : 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] = GEPIOL[i]; } GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &Name, Instruction *InBe) - : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), - GetElementPtr, 0, 0, InBe) { - init(Ptr, Idx); - setName(Name); + : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)), + retrieveAddrSpace(Ptr)), + GetElementPtr, + OperandTraits::op_end(this) - 2, + 2, InBe) { + init(Ptr, Idx, Name); } GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &Name, BasicBlock *IAE) - : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), - GetElementPtr, 0, 0, IAE) { - init(Ptr, Idx); - setName(Name); -} - -GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, - const std::string &Name, Instruction *InBe) - : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), - Idx0, Idx1, true))), - GetElementPtr, 0, 0, InBe) { - init(Ptr, Idx0, Idx1); - setName(Name); -} - -GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1, - const std::string &Name, BasicBlock *IAE) - : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), - Idx0, Idx1, true))), - GetElementPtr, 0, 0, IAE) { - init(Ptr, Idx0, Idx1); - setName(Name); -} - -GetElementPtrInst::~GetElementPtrInst() { - delete[] OperandList; + : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)), + retrieveAddrSpace(Ptr)), + GetElementPtr, + OperandTraits::op_end(this) - 2, + 2, IAE) { + init(Ptr, Idx, Name); } // getIndexedType - Returns the type of the element that would be loaded with @@ -902,58 +1060,31 @@ GetElementPtrInst::~GetElementPtrInst() { // const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value* const *Idxs, - unsigned NumIdx, - bool AllowCompositeLeaf) { - if (!isa(Ptr)) return 0; // Type isn't a pointer type! + 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... if (NumIdx == 0) - if (AllowCompositeLeaf || - cast(Ptr)->getElementType()->isFirstClassType()) - return cast(Ptr)->getElementType(); - else - return 0; - - unsigned CurIdx = 0; - while (const CompositeType *CT = dyn_cast(Ptr)) { - if (NumIdx == CurIdx) { - if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr; - return 0; // Can't load a whole structure or array!?!? - } + return Agg; - Value *Index = Idxs[CurIdx++]; - if (isa(CT) && CurIdx != 1) - return 0; // Can only index into pointer types at the first index! + unsigned CurIdx = 1; + for (; CurIdx != NumIdx; ++CurIdx) { + const CompositeType *CT = dyn_cast(Agg); + if (!CT || isa(CT)) return 0; + Value *Index = Idxs[CurIdx]; if (!CT->indexValid(Index)) return 0; - Ptr = CT->getTypeAtIndex(Index); + 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 = Ptr->getForwardedType()) { - Ptr = Ty; - } + if (const Type *Ty = Agg->getForwardedType()) + Agg = Ty; } - return CurIdx == NumIdx ? Ptr : 0; -} - -const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, - Value *Idx0, Value *Idx1, - bool AllowCompositeLeaf) { - const PointerType *PTy = dyn_cast(Ptr); - if (!PTy) return 0; // Type isn't a pointer type! - - // Check the pointer index. - if (!PTy->indexValid(Idx0)) return 0; - - const CompositeType *CT = dyn_cast(PTy->getElementType()); - if (!CT || !CT->indexValid(Idx1)) return 0; - - const Type *ElTy = CT->getTypeAtIndex(Idx1); - if (AllowCompositeLeaf || ElTy->isFirstClassType()) - return ElTy; - return 0; + return CurIdx == NumIdx ? Agg : 0; } const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { @@ -981,6 +1112,17 @@ bool GetElementPtrInst::hasAllZeroIndices() const { return true; } +/// hasAllConstantIndices - Return true if all of the indices of this GEP are +/// constant integers. If so, the result pointer and the first operand have +/// a constant offset between them. +bool GetElementPtrInst::hasAllConstantIndices() const { + for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { + if (!isa(getOperand(i))) + return false; + } + return true; +} + //===----------------------------------------------------------------------===// // ExtractElementInst Implementation @@ -990,11 +1132,13 @@ ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, const std::string &Name, Instruction *InsertBef) : Instruction(cast(Val->getType())->getElementType(), - ExtractElement, Ops, 2, InsertBef) { + ExtractElement, + OperandTraits::op_begin(this), + 2, InsertBef) { assert(isValidOperands(Val, Index) && "Invalid extractelement instruction operands!"); - Ops[0].init(Val, this); - Ops[1].init(Index, this); + Op<0>() = Val; + Op<1>() = Index; setName(Name); } @@ -1002,12 +1146,14 @@ ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, const std::string &Name, Instruction *InsertBef) : Instruction(cast(Val->getType())->getElementType(), - ExtractElement, Ops, 2, InsertBef) { + ExtractElement, + OperandTraits::op_begin(this), + 2, InsertBef) { Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); assert(isValidOperands(Val, Index) && "Invalid extractelement instruction operands!"); - Ops[0].init(Val, this); - Ops[1].init(Index, this); + Op<0>() = Val; + Op<1>() = Index; setName(Name); } @@ -1016,12 +1162,14 @@ ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, const std::string &Name, BasicBlock *InsertAE) : Instruction(cast(Val->getType())->getElementType(), - ExtractElement, Ops, 2, InsertAE) { + ExtractElement, + OperandTraits::op_begin(this), + 2, InsertAE) { assert(isValidOperands(Val, Index) && "Invalid extractelement instruction operands!"); - Ops[0].init(Val, this); - Ops[1].init(Index, this); + Op<0>() = Val; + Op<1>() = Index; setName(Name); } @@ -1029,13 +1177,15 @@ ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, const std::string &Name, BasicBlock *InsertAE) : Instruction(cast(Val->getType())->getElementType(), - ExtractElement, Ops, 2, InsertAE) { + ExtractElement, + OperandTraits::op_begin(this), + 2, InsertAE) { Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); assert(isValidOperands(Val, Index) && "Invalid extractelement instruction operands!"); - Ops[0].init(Val, this); - Ops[1].init(Index, this); + Op<0>() = Val; + Op<1>() = Index; setName(Name); } @@ -1052,33 +1202,38 @@ bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { //===----------------------------------------------------------------------===// InsertElementInst::InsertElementInst(const InsertElementInst &IE) - : Instruction(IE.getType(), InsertElement, Ops, 3) { - Ops[0].init(IE.Ops[0], this); - Ops[1].init(IE.Ops[1], this); - Ops[2].init(IE.Ops[2], this); + : Instruction(IE.getType(), InsertElement, + OperandTraits::op_begin(this), 3) { + 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, Instruction *InsertBef) - : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) { + : Instruction(Vec->getType(), InsertElement, + OperandTraits::op_begin(this), + 3, InsertBef) { assert(isValidOperands(Vec, Elt, Index) && "Invalid insertelement instruction operands!"); - Ops[0].init(Vec, this); - Ops[1].init(Elt, this); - Ops[2].init(Index, this); + Op<0>() = Vec; + Op<1>() = Elt; + Op<2>() = Index; setName(Name); } InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, const std::string &Name, Instruction *InsertBef) - : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertBef) { + : Instruction(Vec->getType(), InsertElement, + OperandTraits::op_begin(this), + 3, InsertBef) { Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); assert(isValidOperands(Vec, Elt, Index) && "Invalid insertelement instruction operands!"); - Ops[0].init(Vec, this); - Ops[1].init(Elt, this); - Ops[2].init(Index, this); + Op<0>() = Vec; + Op<1>() = Elt; + Op<2>() = Index; setName(Name); } @@ -1086,27 +1241,31 @@ InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, const std::string &Name, BasicBlock *InsertAE) - : Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) { + : Instruction(Vec->getType(), InsertElement, + OperandTraits::op_begin(this), + 3, InsertAE) { assert(isValidOperands(Vec, Elt, Index) && "Invalid insertelement instruction operands!"); - Ops[0].init(Vec, this); - Ops[1].init(Elt, this); - Ops[2].init(Index, this); + Op<0>() = Vec; + Op<1>() = Elt; + Op<2>() = Index; setName(Name); } InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, const std::string &Name, BasicBlock *InsertAE) -: Instruction(Vec->getType(), InsertElement, Ops, 3, InsertAE) { +: Instruction(Vec->getType(), InsertElement, + OperandTraits::op_begin(this), + 3, InsertAE) { Constant *Index = ConstantInt::get(Type::Int32Ty, IndexV); assert(isValidOperands(Vec, Elt, Index) && "Invalid insertelement instruction operands!"); - Ops[0].init(Vec, this); - Ops[1].init(Elt, this); - Ops[2].init(Index, this); + Op<0>() = Vec; + Op<1>() = Elt; + Op<2>() = Index; setName(Name); } @@ -1116,7 +1275,7 @@ bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, return false; // First operand of insertelement must be vector type. if (Elt->getType() != cast(Vec->getType())->getElementType()) - return false;// Second operand of insertelement must be packed element type. + 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. @@ -1129,49 +1288,202 @@ bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, //===----------------------------------------------------------------------===// ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV) - : Instruction(SV.getType(), ShuffleVector, Ops, 3) { - Ops[0].init(SV.Ops[0], this); - Ops[1].init(SV.Ops[1], this); - Ops[2].init(SV.Ops[2], this); + : Instruction(SV.getType(), ShuffleVector, + OperandTraits::op_begin(this), + OperandTraits::operands(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, Ops, 3, InsertBefore) { + : Instruction(V1->getType(), ShuffleVector, + OperandTraits::op_begin(this), + OperandTraits::operands(this), + InsertBefore) { assert(isValidOperands(V1, V2, Mask) && "Invalid shuffle vector instruction operands!"); - Ops[0].init(V1, this); - Ops[1].init(V2, this); - Ops[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, BasicBlock *InsertAtEnd) - : Instruction(V1->getType(), ShuffleVector, Ops, 3, InsertAtEnd) { + : Instruction(V1->getType(), ShuffleVector, + OperandTraits::op_begin(this), + OperandTraits::operands(this), + InsertAtEnd) { assert(isValidOperands(V1, V2, Mask) && "Invalid shuffle vector instruction operands!"); - Ops[0].init(V1, this); - Ops[1].init(V2, this); - Ops[2].init(Mask, this); + Op<0>() = V1; + Op<1>() = V2; + Op<2>() = Mask; setName(Name); } bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, const Value *Mask) { - if (!isa(V1->getType())) return false; - if (V1->getType() != V2->getType()) return false; - if (!isa(Mask->getType()) || - cast(Mask->getType())->getElementType() != Type::Int32Ty || - cast(Mask->getType())->getNumElements() != - cast(V1->getType())->getNumElements()) + 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()) return false; return true; } +/// getMaskValue - Return the index from the shuffle mask for the specified +/// output result. This is either -1 if the element is undef or a number less +/// than 2*numelements. +int ShuffleVectorInst::getMaskValue(unsigned i) const { + const Constant *Mask = cast(getOperand(2)); + if (isa(Mask)) return -1; + if (isa(Mask)) return 0; + const ConstantVector *MaskCV = cast(Mask); + assert(i < MaskCV->getNumOperands() && "Index out of range"); + + if (isa(MaskCV->getOperand(i))) + return -1; + 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. +// +// A null type is returned if the indices are invalid for the specified +// pointer type. +// +const Type* ExtractValueInst::getIndexedType(const Type *Agg, + const unsigned *Idxs, + unsigned NumIdx) { + unsigned CurIdx = 0; + for (; CurIdx != NumIdx; ++CurIdx) { + const CompositeType *CT = dyn_cast(Agg); + if (!CT || isa(CT) || isa(CT)) return 0; + unsigned 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; +} + +const Type* ExtractValueInst::getIndexedType(const Type *Agg, + unsigned Idx) { + return getIndexedType(Agg, &Idx, 1); +} + +ExtractValueInst::ExtractValueInst(Value *Agg, + unsigned Idx, + const std::string &Name, + BasicBlock *InsertAtEnd) + : UnaryInstruction(checkType(getIndexedType(Agg->getType(), &Idx, 1)), + ExtractValue, Agg, InsertAtEnd) { + init(Idx, Name); +} + +ExtractValueInst::ExtractValueInst(Value *Agg, + unsigned Idx, + const std::string &Name, + Instruction *InsertBefore) + : UnaryInstruction(checkType(getIndexedType(Agg->getType(), &Idx, 1)), + ExtractValue, Agg, InsertBefore) { + init(Idx, Name); +} //===----------------------------------------------------------------------===// // BinaryOperator Class @@ -1180,9 +1492,12 @@ bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty, const std::string &Name, Instruction *InsertBefore) - : Instruction(Ty, iType, Ops, 2, InsertBefore) { - Ops[0].init(S1, this); - Ops[1].init(S2, this); + : Instruction(Ty, iType, + OperandTraits::op_begin(this), + OperandTraits::operands(this), + InsertBefore) { + Op<0>() = S1; + Op<1>() = S2; init(iType); setName(Name); } @@ -1190,9 +1505,12 @@ BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd) - : Instruction(Ty, iType, Ops, 2, InsertAtEnd) { - Ops[0].init(S1, this); - Ops[1].init(S2, this); + : Instruction(Ty, iType, + OperandTraits::op_begin(this), + OperandTraits::operands(this), + InsertAtEnd) { + Op<0>() = S1; + Op<1>() = S2; init(iType); setName(Name); } @@ -1266,7 +1584,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() && @@ -1274,15 +1592,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, @@ -1290,7 +1608,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, @@ -1298,7 +1616,7 @@ BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, Op->getType(), Name, InsertAtEnd); } -BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, +BinaryOperator *BinaryOperator::CreateNot(Value *Op, const std::string &Name, Instruction *InsertBefore) { Constant *C; if (const VectorType *PTy = dyn_cast(Op->getType())) { @@ -1312,7 +1630,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())) { @@ -1331,7 +1649,11 @@ BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, // isConstantAllOnes - Helper function for several functions below static inline bool isConstantAllOnes(const Value *V) { - return isa(V) &&cast(V)->isAllOnesValue(); + if (const ConstantInt *CI = dyn_cast(V)) + return CI->isAllOnesValue(); + if (const ConstantVector *CV = dyn_cast(V)) + return CV->isAllOnesValue(); + return false; } bool BinaryOperator::isNeg(const Value *V) { @@ -1383,7 +1705,7 @@ const Value *BinaryOperator::getNotArgument(const Value *BinOp) { bool BinaryOperator::swapOperands() { if (!isCommutative()) return true; // Can't commute operands - std::swap(Ops[0], Ops[1]); + Op<0>().swap(Op<1>()); return false; } @@ -1425,10 +1747,9 @@ bool CastInst::isLosslessCast() const { /// changed in order to effect the cast. Essentially, it identifies cases where /// no code gen is necessary for the cast, hence the name no-op cast. For /// example, the following are all no-op casts: -/// # bitcast uint %X, int -/// # bitcast uint* %x, sbyte* -/// # bitcast packed< 2 x int > %x, packed< 4 x short> -/// # ptrtoint uint* %x, uint ; on 32-bit plaforms only +/// # bitcast i32* %x to i8* +/// # bitcast <2 x i32> %x to <4 x i16> +/// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only /// @brief Determine if a cast is a no-op. bool CastInst::isNoopCast(const Type *IntPtrTy) const { switch (getOpcode()) { @@ -1614,7 +1935,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) { @@ -1636,7 +1957,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) { @@ -1658,55 +1979,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); + 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); + 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); + 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); + 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); + 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); + 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"); @@ -1714,12 +2035,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"); @@ -1727,11 +2048,11 @@ 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"); @@ -1741,10 +2062,10 @@ CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, (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"); @@ -1754,10 +2075,10 @@ CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, (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() && @@ -1767,10 +2088,10 @@ CastInst *CastInst::createFPCast(Value *C, const Type *Ty, 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() && @@ -1780,7 +2101,64 @@ CastInst *CastInst::createFPCast(Value *C, const Type *Ty, 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. +// This routine must be kept in sync with getCastOpcode. +bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) { + if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) + return false; + + if (SrcTy == 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 + + // Run through the possibilities ... + if (DestTy->isInteger()) { // Casting to integral + if (SrcTy->isInteger()) { // Casting from integral + return true; + } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt + return true; + } else if (const VectorType *PTy = dyn_cast(SrcTy)) { + // Casting from vector + return DestBits == PTy->getBitWidth(); + } else { // Casting from something else + return isa(SrcTy); + } + } else if (DestTy->isFloatingPoint()) { // Casting to floating pt + if (SrcTy->isInteger()) { // Casting from integral + return true; + } else if (SrcTy->isFloatingPoint()) { // Casting from floating pt + return true; + } else if (const VectorType *PTy = dyn_cast(SrcTy)) { + // Casting from vector + return DestBits == PTy->getBitWidth(); + } else { // Casting from something else + return false; + } + } else if (const VectorType *DestPTy = dyn_cast(DestTy)) { + // Casting to vector + if (const VectorType *SrcPTy = dyn_cast(SrcTy)) { + // Casting from vector + return DestPTy->getBitWidth() == SrcPTy->getBitWidth(); + } else { // Casting from something else + return DestPTy->getBitWidth() == SrcBits; + } + } else if (isa(DestTy)) { // Casting to pointer + if (isa(SrcTy)) { // Casting from pointer + return true; + } else if (SrcTy->isInteger()) { // Casting from integral + return true; + } else { // Casting from something else + return false; + } + } else { // Casting to something else + return false; + } } // Provide a way to get a "cast" where the cast opcode is inferred from the @@ -1789,13 +2167,17 @@ CastInst *CastInst::createFPCast(Value *C, const Type *Ty, // castIsValid( getCastOpcode(Val, Ty), Val, Ty) // should not assert in castIsValid. In other words, this produces a "correct" // casting opcode for the arguments passed to it. +// This routine must be kept in sync with isCastable. Instruction::CastOps 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/packed - unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/packed + unsigned SrcBits = SrcTy->getPrimitiveSizeInBits(); // 0 for ptr/vector + unsigned DestBits = DestTy->getPrimitiveSizeInBits(); // 0 for ptr/vector + + assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && + "Only first class types are castable!"); // Run through the possibilities ... if (DestTy->isInteger()) { // Casting to integral @@ -1817,7 +2199,7 @@ CastInst::getCastOpcode( return FPToUI; // FP -> uint } else if (const VectorType *PTy = dyn_cast(SrcTy)) { assert(DestBits == PTy->getBitWidth() && - "Casting packed to integer of different width"); + "Casting vector to integer of different width"); return BitCast; // Same size, no-op cast } else { assert(isa(SrcTy) && @@ -1840,7 +2222,7 @@ CastInst::getCastOpcode( } } else if (const VectorType *PTy = dyn_cast(SrcTy)) { assert(DestBits == PTy->getBitWidth() && - "Casting packed to floating point of different width"); + "Casting vector to floating point of different width"); return BitCast; // same size, no-op cast } else { assert(0 && "Casting pointer or non-first class to float"); @@ -1848,12 +2230,12 @@ CastInst::getCastOpcode( } else if (const VectorType *DestPTy = dyn_cast(DestTy)) { if (const VectorType *SrcPTy = dyn_cast(SrcTy)) { assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() && - "Casting packed to packed of different widths"); - return BitCast; // packed -> packed + "Casting vector to vector of different widths"); + return BitCast; // vector -> vector } else if (DestPTy->getBitWidth() == SrcBits) { - return BitCast; // float/int -> packed + return BitCast; // float/int -> vector } else { - assert(!"Illegal cast to packed (wrong type or size)"); + assert(!"Illegal cast to vector (wrong type or size)"); } } else if (isa(DestTy)) { if (isa(SrcTy)) { @@ -1909,12 +2291,24 @@ CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) { return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && SrcBitSize < DstBitSize; case Instruction::UIToFP: - return SrcTy->isInteger() && DstTy->isFloatingPoint(); case Instruction::SIToFP: + if (const VectorType *SVTy = dyn_cast(SrcTy)) { + if (const VectorType *DVTy = dyn_cast(DstTy)) { + return SVTy->getElementType()->isInteger() && + DVTy->getElementType()->isFloatingPoint() && + SVTy->getNumElements() == DVTy->getNumElements(); + } + } return SrcTy->isInteger() && DstTy->isFloatingPoint(); case Instruction::FPToUI: - return SrcTy->isFloatingPoint() && DstTy->isInteger(); case Instruction::FPToSI: + if (const VectorType *SVTy = dyn_cast(SrcTy)) { + if (const VectorType *DVTy = dyn_cast(DstTy)) { + return SVTy->getElementType()->isFloatingPoint() && + DVTy->getElementType()->isInteger() && + SVTy->getNumElements() == DVTy->getNumElements(); + } + } return SrcTy->isFloatingPoint() && DstTy->isInteger(); case Instruction::PtrToInt: return isa(SrcTy) && DstTy->isInteger(); @@ -1926,7 +2320,7 @@ CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) { if (isa(SrcTy) != isa(DstTy)) return false; - // Now we know we're not dealing with a pointer/non-poiner mismatch. In all + // 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; @@ -2080,91 +2474,68 @@ BitCastInst::BitCastInst( // CmpInst Classes //===----------------------------------------------------------------------===// -CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, - const std::string &Name, Instruction *InsertBefore) - : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) { - Ops[0].init(LHS, this); - Ops[1].init(RHS, this); +CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate, + Value *LHS, Value *RHS, const std::string &Name, + Instruction *InsertBefore) + : Instruction(ty, op, + OperandTraits::op_begin(this), + OperandTraits::operands(this), + InsertBefore) { + Op<0>() = LHS; + Op<1>() = RHS; SubclassData = predicate; setName(Name); - if (op == Instruction::ICmp) { - assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE && - predicate <= ICmpInst::LAST_ICMP_PREDICATE && - "Invalid ICmp predicate value"); - const Type* Op0Ty = getOperand(0)->getType(); - const Type* Op1Ty = getOperand(1)->getType(); - assert(Op0Ty == Op1Ty && - "Both operands to ICmp instruction are not of the same type!"); - // Check that the operands are the right type - assert((Op0Ty->isInteger() || isa(Op0Ty)) && - "Invalid operand types for ICmp instruction"); - return; - } - assert(op == Instruction::FCmp && "Invalid CmpInst opcode"); - assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE && - "Invalid FCmp predicate value"); - const Type* Op0Ty = getOperand(0)->getType(); - const Type* Op1Ty = getOperand(1)->getType(); - assert(Op0Ty == Op1Ty && - "Both operands to FCmp instruction are not of the same type!"); - // Check that the operands are the right type - assert(Op0Ty->isFloatingPoint() && - "Invalid operand types for FCmp instruction"); } - -CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, - const std::string &Name, BasicBlock *InsertAtEnd) - : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) { - Ops[0].init(LHS, this); - Ops[1].init(RHS, this); + +CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate, + Value *LHS, Value *RHS, const std::string &Name, + BasicBlock *InsertAtEnd) + : Instruction(ty, op, + OperandTraits::op_begin(this), + OperandTraits::operands(this), + InsertAtEnd) { + Op<0>() = LHS; + Op<1>() = RHS; SubclassData = predicate; setName(Name); - if (op == Instruction::ICmp) { - assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE && - predicate <= ICmpInst::LAST_ICMP_PREDICATE && - "Invalid ICmp predicate value"); - - const Type* Op0Ty = getOperand(0)->getType(); - const Type* Op1Ty = getOperand(1)->getType(); - assert(Op0Ty == Op1Ty && - "Both operands to ICmp instruction are not of the same type!"); - // Check that the operands are the right type - assert(Op0Ty->isInteger() || isa(Op0Ty) && - "Invalid operand types for ICmp instruction"); - return; - } - assert(op == Instruction::FCmp && "Invalid CmpInst opcode"); - assert(predicate <= FCmpInst::LAST_FCMP_PREDICATE && - "Invalid FCmp predicate value"); - const Type* Op0Ty = getOperand(0)->getType(); - const Type* Op1Ty = getOperand(1)->getType(); - assert(Op0Ty == Op1Ty && - "Both operands to FCmp instruction are not of the same type!"); - // Check that the operands are the right type - assert(Op0Ty->isFloatingPoint() && - "Invalid operand types for FCmp instruction"); } 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(ICmpInst::Predicate(predicate), S1, S2, Name, + return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name, + InsertBefore); + } + if (Op == Instruction::FCmp) { + return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name, InsertBefore); } - return new FCmpInst(FCmpInst::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); } 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(ICmpInst::Predicate(predicate), S1, S2, Name, + return new ICmpInst(CmpInst::Predicate(predicate), S1, S2, Name, + InsertAtEnd); + } + if (Op == Instruction::FCmp) { + return new FCmpInst(CmpInst::Predicate(predicate), S1, S2, Name, InsertAtEnd); } - return new FCmpInst(FCmpInst::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); } void CmpInst::swapOperands() { @@ -2187,10 +2558,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; @@ -2201,22 +2571,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; } } @@ -2233,6 +2604,19 @@ ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { } } +ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { + switch (pred) { + default: assert(! "Unknown icmp predicate!"); + case ICMP_EQ: case ICMP_NE: + case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: + return pred; + case ICMP_SGT: return ICMP_UGT; + case ICMP_SLT: return ICMP_ULT; + case ICMP_SGE: return ICMP_UGE; + case ICMP_SLE: return ICMP_ULE; + } +} + bool ICmpInst::isSignedPredicate(Predicate pred) { switch (pred) { default: assert(! "Unknown icmp predicate!"); @@ -2279,32 +2663,20 @@ ICmpInst::makeConstantRange(Predicate pred, const APInt &C) { return ConstantRange(Lower, Upper); } -FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) { +CmpInst::Predicate CmpInst::getSwappedPredicate(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) { - 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: @@ -2363,10 +2735,10 @@ void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) { assert(Value && Default); ReservedSpace = 2+NumCases*2; NumOperands = 2; - OperandList = new Use[ReservedSpace]; + 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 @@ -2391,16 +2763,16 @@ SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, SwitchInst::SwitchInst(const SwitchInst &SI) : TerminatorInst(Type::VoidTy, Instruction::Switch, - new Use[SI.getNumOperands()], SI.getNumOperands()) { + 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]; } } SwitchInst::~SwitchInst() { - delete [] OperandList; + dropHungoffUses(OperandList); } @@ -2413,8 +2785,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 @@ -2447,13 +2819,14 @@ void SwitchInst::removeCase(unsigned idx) { /// resizeOperands - resize operands - This adjusts the length of the operands /// list according to the following behavior: /// 1. If NumOps == 0, grow the operand list in response to a push_back style -/// of operation. This grows the number of ops by 1.5 times. +/// of operation. This grows the number of ops by 3 times. /// 2. If NumOps > NumOperands, reserve space for NumOps operands. /// 3. If NumOps == NumOperands, trim the reserved space. /// void SwitchInst::resizeOperands(unsigned NumOps) { + unsigned e = getNumOperands(); if (NumOps == 0) { - NumOps = getNumOperands()/2*6; + NumOps = e*3; } else if (NumOps*2 > NumOperands) { // No resize needed. if (ReservedSpace >= NumOps) return; @@ -2464,14 +2837,13 @@ void SwitchInst::resizeOperands(unsigned NumOps) { } ReservedSpace = NumOps; - Use *NewOps = new Use[NumOps]; + Use *NewOps = allocHungoffUses(NumOps); Use *OldOps = OperandList; - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { - NewOps[i].init(OldOps[i], this); - OldOps[i].set(0); + for (unsigned i = 0; i != e; ++i) { + NewOps[i] = OldOps[i]; } - delete [] OldOps; OperandList = NewOps; + if (OldOps) Use::zap(OldOps, OldOps + e, true); } @@ -2485,22 +2857,76 @@ 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. GetElementPtrInst *GetElementPtrInst::clone() const { - return new GetElementPtrInst(*this); + return new(getNumOperands()) GetElementPtrInst(*this); } BinaryOperator *BinaryOperator::clone() const { - return create(getOpcode(), Ops[0], Ops[1]); + return Create(getOpcode(), Op<0>(), Op<1>()); } -CmpInst* CmpInst::clone() const { - return create(getOpcode(), getPredicate(), Ops[0], Ops[1]); +FCmpInst* FCmpInst::clone() const { + return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); +} +ICmpInst* ICmpInst::clone() const { + return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); } +VFCmpInst* VFCmpInst::clone() const { + return new VFCmpInst(getPredicate(), Op<0>(), Op<1>()); +} +VICmpInst* VICmpInst::clone() const { + return new VICmpInst(getPredicate(), Op<0>(), Op<1>()); +} + +ExtractValueInst *ExtractValueInst::clone() const { + return new ExtractValueInst(*this); +} +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)); } @@ -2518,23 +2944,34 @@ CastInst *FPToSIInst::clone() const { return new FPToSIInst(*this); } CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); } CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); } CastInst *BitCastInst::clone() const { return new BitCastInst(*this); } -CallInst *CallInst::clone() const { return new CallInst(*this); } -SelectInst *SelectInst::clone() const { return new SelectInst(*this); } +CallInst *CallInst::clone() const { + return new(getNumOperands()) CallInst(*this); +} +SelectInst *SelectInst::clone() const { + return new(getNumOperands()) SelectInst(*this); +} VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); } ExtractElementInst *ExtractElementInst::clone() const { return new ExtractElementInst(*this); } InsertElementInst *InsertElementInst::clone() const { - return new InsertElementInst(*this); + return InsertElementInst::Create(*this); } ShuffleVectorInst *ShuffleVectorInst::clone() const { return new ShuffleVectorInst(*this); } PHINode *PHINode::clone() const { return new PHINode(*this); } -ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); } -BranchInst *BranchInst::clone() const { return new BranchInst(*this); } +ReturnInst *ReturnInst::clone() const { + return new(getNumOperands()) ReturnInst(*this); +} +BranchInst *BranchInst::clone() const { + return new(getNumOperands()) BranchInst(*this); +} SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); } -InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); } +InvokeInst *InvokeInst::clone() const { + return new(getNumOperands()) InvokeInst(*this); +} UnwindInst *UnwindInst::clone() const { return new UnwindInst(); } UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();} +GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }