X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;ds=inline;f=lib%2FVMCore%2FInstructions.cpp;h=c0b3413da33782870832f11d011181ba3ab69485;hb=e6be34a53ecbe8c2ff9f0793b13d847e94c0de91;hp=48784099d9c9d08f4e444c74188c23caacc27bf7;hpb=e4d87aa2de6e52952dca73716386db09aad5a8fd;p=oota-llvm.git diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index 48784099d9c..c0b3413da33 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. // //===----------------------------------------------------------------------===// // @@ -17,9 +17,20 @@ #include "llvm/DerivedTypes.h" #include "llvm/Function.h" #include "llvm/Instructions.h" +#include "llvm/ParamAttrsList.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(); @@ -32,24 +43,60 @@ void CallSite::setCallingConv(unsigned CC) { else cast(I)->setCallingConv(CC); } +const ParamAttrsList* CallSite::getParamAttrs() const { + if (CallInst *CI = dyn_cast(I)) + return CI->getParamAttrs(); + else + return cast(I)->getParamAttrs(); +} +void CallSite::setParamAttrs(const ParamAttrsList *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); +} //===----------------------------------------------------------------------===// // TerminatorInst Class //===----------------------------------------------------------------------===// -TerminatorInst::TerminatorInst(Instruction::TermOps iType, - Use *Ops, unsigned NumOps, Instruction *IB) - : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) { -} - -TerminatorInst::TerminatorInst(Instruction::TermOps iType, - Use *Ops, unsigned NumOps, BasicBlock *IAE) - : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) { -} - // Out of line virtual method, so the vtable, etc has a home. TerminatorInst::~TerminatorInst() { } @@ -146,11 +193,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. @@ -158,13 +206,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 @@ -179,7 +228,7 @@ Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const { if (HasUndefInput && !AllowNonDominatingInstruction) if (Instruction *IV = dyn_cast(InVal)) // If it's in the entry block, it dominates everything. - if (IV->getParent() != &IV->getParent()->getParent()->front() || + if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() || isa(IV)) return 0; // Cannot guarantee that InVal dominates this PHINode. @@ -194,20 +243,24 @@ Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const { CallInst::~CallInst() { delete [] OperandList; + if (ParamAttrs) + ParamAttrs->dropRef(); } -void CallInst::init(Value *Func, const std::vector &Params) { - NumOperands = Params.size()+1; - Use *OL = OperandList = new Use[Params.size()+1]; +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); const FunctionType *FTy = cast(cast(Func->getType())->getElementType()); + FTy = FTy; // silence warning. - assert((Params.size() == FTy->getNumParams() || - (FTy->isVarArg() && Params.size() > FTy->getNumParams())) && + assert((NumParams == FTy->getNumParams() || + (FTy->isVarArg() && NumParams > FTy->getNumParams())) && "Calling a function with bad signature!"); - for (unsigned i = 0, e = Params.size(); i != e; ++i) { + for (unsigned i = 0; i != NumParams; ++i) { assert((i >= FTy->getNumParams() || FTy->getParamType(i) == Params[i]->getType()) && "Calling a function with a bad signature!"); @@ -216,6 +269,7 @@ void CallInst::init(Value *Func, const std::vector &Params) { } void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { + ParamAttrs = 0; NumOperands = 3; Use *OL = OperandList = new Use[3]; OL[0].init(Func, this); @@ -224,6 +278,7 @@ void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { const FunctionType *FTy = cast(cast(Func->getType())->getElementType()); + FTy = FTy; // silence warning. assert((FTy->getNumParams() == 2 || (FTy->isVarArg() && FTy->getNumParams() < 2)) && @@ -237,6 +292,7 @@ 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); @@ -244,6 +300,7 @@ void CallInst::init(Value *Func, Value *Actual) { const FunctionType *FTy = cast(cast(Func->getType())->getElementType()); + FTy = FTy; // silence warning. assert((FTy->getNumParams() == 1 || (FTy->isVarArg() && FTy->getNumParams() == 0)) && @@ -254,83 +311,58 @@ 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); - const FunctionType *MTy = + const FunctionType *FTy = cast(cast(Func->getType())->getElementType()); + FTy = FTy; // silence warning. - assert(MTy->getNumParams() == 0 && "Calling a function with bad signature"); -} - -CallInst::CallInst(Value *Func, const std::vector &Params, - const std::string &Name, Instruction *InsertBefore) - : Instruction(cast(cast(Func->getType()) - ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, Name, InsertBefore) { - init(Func, Params); -} - -CallInst::CallInst(Value *Func, const std::vector &Params, - const std::string &Name, BasicBlock *InsertAtEnd) - : Instruction(cast(cast(Func->getType()) - ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, Name, InsertAtEnd) { - init(Func, Params); -} - -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, Name, InsertBefore) { - init(Func, Actual1, Actual2); -} - -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, Name, InsertAtEnd) { - init(Func, Actual1, Actual2); + assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); } CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, - Instruction *InsertBefore) + Instruction *InsertBefore) : Instruction(cast(cast(Func->getType()) ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, Name, InsertBefore) { + Instruction::Call, 0, 0, InsertBefore) { init(Func, Actual); + setName(Name); } CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, BasicBlock *InsertAtEnd) : Instruction(cast(cast(Func->getType()) ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, Name, InsertAtEnd) { + Instruction::Call, 0, 0, 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, Name, InsertBefore) { + Instruction::Call, 0, 0, InsertBefore) { init(Func); + setName(Name); } CallInst::CallInst(Value *Func, const std::string &Name, BasicBlock *InsertAtEnd) : Instruction(cast(cast(Func->getType()) ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, Name, InsertAtEnd) { + Instruction::Call, 0, 0, InsertAtEnd) { init(Func); + setName(Name); } CallInst::CallInst(const CallInst &CI) : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()], - CI.getNumOperands()) { + CI.getNumOperands()), + ParamAttrs(0) { + setParamAttrs(CI.getParamAttrs()); SubclassData = CI.SubclassData; Use *OL = OperandList; Use *InOL = CI.OperandList; @@ -338,6 +370,81 @@ CallInst::CallInst(const CallInst &CI) OL[i].init(InOL[i], this); } +void CallInst::setParamAttrs(const ParamAttrsList *newAttrs) { + if (ParamAttrs == newAttrs) + return; + + if (ParamAttrs) + ParamAttrs->dropRef(); + + if (newAttrs) + newAttrs->addRef(); + + ParamAttrs = newAttrs; +} + +bool CallInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const { + if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr)) + return true; + if (const Function *F = getCalledFunction()) + return F->paramHasAttr(i, attr); + return false; +} + +uint16_t CallInst::getParamAlignment(uint16_t i) const { + if (ParamAttrs && ParamAttrs->getParamAlignment(i)) + return ParamAttrs->getParamAlignment(i); + if (const Function *F = getCalledFunction()) + return F->getParamAlignment(i); + return 0; +} + +/// @brief Determine if the call does not access memory. +bool CallInst::doesNotAccessMemory() const { + return paramHasAttr(0, ParamAttr::ReadNone); +} + +/// @brief Determine if the call does not access or only reads memory. +bool CallInst::onlyReadsMemory() const { + return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly); +} + +/// @brief Determine if the call cannot return. +bool CallInst::doesNotReturn() const { + return paramHasAttr(0, ParamAttr::NoReturn); +} + +/// @brief Determine if the call cannot unwind. +bool CallInst::doesNotThrow() const { + return paramHasAttr(0, ParamAttr::NoUnwind); +} + +/// @brief Determine if the call returns a structure. +bool CallInst::isStructReturn() const { + // Be friendly and also check the callee. + return paramHasAttr(1, ParamAttr::StructRet); +} + +/// @brief Determine if any call argument is an aggregate passed by value. +bool CallInst::hasByValArgument() const { + if (ParamAttrs && ParamAttrs->hasAttrSomewhere(ParamAttr::ByVal)) + return true; + // Be consistent with other methods and check the callee too. + if (const Function *F = getCalledFunction()) + if (const ParamAttrsList *PAL = F->getParamAttrs()) + return PAL->hasAttrSomewhere(ParamAttr::ByVal); + return false; +} + +void CallInst::setDoesNotThrow(bool doesNotThrow) { + const ParamAttrsList *PAL = getParamAttrs(); + if (doesNotThrow) + PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind); + else + PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind); + setParamAttrs(PAL); +} + //===----------------------------------------------------------------------===// // InvokeInst Implementation @@ -345,54 +452,40 @@ CallInst::CallInst(const CallInst &CI) InvokeInst::~InvokeInst() { delete [] OperandList; + if (ParamAttrs) + ParamAttrs->dropRef(); } void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, - const std::vector &Params) { - NumOperands = 3+Params.size(); - Use *OL = OperandList = new Use[3+Params.size()]; + 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); const FunctionType *FTy = cast(cast(Fn->getType())->getElementType()); + FTy = FTy; // silence warning. - assert((Params.size() == FTy->getNumParams()) || - (FTy->isVarArg() && Params.size() > FTy->getNumParams()) && + assert(((NumArgs == FTy->getNumParams()) || + (FTy->isVarArg() && NumArgs > FTy->getNumParams())) && "Calling a function with bad signature"); - for (unsigned i = 0, e = Params.size(); i != e; i++) { + for (unsigned i = 0, e = NumArgs; i != e; i++) { assert((i >= FTy->getNumParams() || - FTy->getParamType(i) == Params[i]->getType()) && + FTy->getParamType(i) == Args[i]->getType()) && "Invoking a function with a bad signature!"); - OL[i+3].init(Params[i], this); + OL[i+3].init(Args[i], this); } } -InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, - BasicBlock *IfException, - const std::vector &Params, - const std::string &Name, Instruction *InsertBefore) - : TerminatorInst(cast(cast(Fn->getType()) - ->getElementType())->getReturnType(), - Instruction::Invoke, 0, 0, Name, InsertBefore) { - init(Fn, IfNormal, IfException, Params); -} - -InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal, - BasicBlock *IfException, - const std::vector &Params, - const std::string &Name, BasicBlock *InsertAtEnd) - : TerminatorInst(cast(cast(Fn->getType()) - ->getElementType())->getReturnType(), - Instruction::Invoke, 0, 0, Name, InsertAtEnd) { - init(Fn, IfNormal, IfException, Params); -} - InvokeInst::InvokeInst(const InvokeInst &II) : TerminatorInst(II.getType(), Instruction::Invoke, - new Use[II.getNumOperands()], II.getNumOperands()) { + new Use[II.getNumOperands()], II.getNumOperands()), + ParamAttrs(0) { + setParamAttrs(II.getParamAttrs()); SubclassData = II.SubclassData; Use *OL = OperandList, *InOL = II.OperandList; for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i) @@ -409,17 +502,138 @@ void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { return setSuccessor(idx, B); } +void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) { + if (ParamAttrs == newAttrs) + return; + + if (ParamAttrs) + ParamAttrs->dropRef(); + + if (newAttrs) + newAttrs->addRef(); + + ParamAttrs = newAttrs; +} + +bool InvokeInst::paramHasAttr(uint16_t i, ParameterAttributes attr) const { + if (ParamAttrs && ParamAttrs->paramHasAttr(i, attr)) + return true; + if (const Function *F = getCalledFunction()) + return F->paramHasAttr(i, attr); + return false; +} + +uint16_t InvokeInst::getParamAlignment(uint16_t i) const { + if (ParamAttrs && ParamAttrs->getParamAlignment(i)) + return ParamAttrs->getParamAlignment(i); + if (const Function *F = getCalledFunction()) + return F->getParamAlignment(i); + return 0; +} + +/// @brief Determine if the call does not access memory. +bool InvokeInst::doesNotAccessMemory() const { + return paramHasAttr(0, ParamAttr::ReadNone); +} + +/// @brief Determine if the call does not access or only reads memory. +bool InvokeInst::onlyReadsMemory() const { + return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly); +} + +/// @brief Determine if the call cannot return. +bool InvokeInst::doesNotReturn() const { + return paramHasAttr(0, ParamAttr::NoReturn); +} + +/// @brief Determine if the call cannot unwind. +bool InvokeInst::doesNotThrow() const { + return paramHasAttr(0, ParamAttr::NoUnwind); +} + +void InvokeInst::setDoesNotThrow(bool doesNotThrow) { + const ParamAttrsList *PAL = getParamAttrs(); + if (doesNotThrow) + PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind); + else + PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind); + setParamAttrs(PAL); +} + +/// @brief Determine if the call returns a structure. +bool InvokeInst::isStructReturn() const { + // Be friendly and also check the callee. + return paramHasAttr(1, ParamAttr::StructRet); +} + //===----------------------------------------------------------------------===// // ReturnInst Implementation //===----------------------------------------------------------------------===// -void ReturnInst::init(Value *retVal) { - if (retVal && retVal->getType() != Type::VoidTy) { - assert(!isa(retVal) && +ReturnInst::ReturnInst(const ReturnInst &RI) + : TerminatorInst(Type::VoidTy, Instruction::Ret, + &RetVal, RI.getNumOperands()) { + unsigned N = RI.getNumOperands(); + if (N == 1) + RetVal.init(RI.RetVal, this); + else if (N) { + Use *OL = OperandList = new Use[N]; + for (unsigned i = 0; i < N; ++i) + OL[i].init(RI.getOperand(i), this); + } +} + +ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore) + : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) { + if (retVal) + init(&retVal, 1); +} +ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd) + : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) { + if (retVal) + init(&retVal, 1); +} +ReturnInst::ReturnInst(BasicBlock *InsertAtEnd) + : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) { +} + +ReturnInst::ReturnInst(Value * const* retVals, unsigned N, + Instruction *InsertBefore) + : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N, InsertBefore) { + if (N != 0) + init(retVals, N); +} +ReturnInst::ReturnInst(Value * const* retVals, unsigned N, + BasicBlock *InsertAtEnd) + : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N, InsertAtEnd) { + if (N != 0) + init(retVals, N); +} +ReturnInst::ReturnInst(Value * const* retVals, unsigned N) + : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, N) { + 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; + RetVal.init(V, this); + return; + } + + Use *OL = OperandList = new Use[NumOperands]; + 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].init(V, this); } } @@ -427,8 +641,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!"); } @@ -439,11 +653,23 @@ BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { return 0; } +ReturnInst::~ReturnInst() { + if (NumOperands > 1) + delete [] OperandList; +} //===----------------------------------------------------------------------===// // UnwindInst Implementation //===----------------------------------------------------------------------===// +UnwindInst::UnwindInst(Instruction *InsertBefore) + : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertBefore) { +} +UnwindInst::UnwindInst(BasicBlock *InsertAtEnd) + : TerminatorInst(Type::VoidTy, Instruction::Unwind, 0, 0, InsertAtEnd) { +} + + unsigned UnwindInst::getNumSuccessorsV() const { return getNumSuccessors(); } @@ -462,6 +688,13 @@ BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const { // UnreachableInst Implementation //===----------------------------------------------------------------------===// +UnreachableInst::UnreachableInst(Instruction *InsertBefore) + : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertBefore) { +} +UnreachableInst::UnreachableInst(BasicBlock *InsertAtEnd) + : TerminatorInst(Type::VoidTy, Instruction::Unreachable, 0, 0, InsertAtEnd) { +} + unsigned UnreachableInst::getNumSuccessorsV() const { return getNumSuccessors(); } @@ -482,12 +715,46 @@ BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { void BranchInst::AssertOK() { if (isConditional()) - assert(getCondition()->getType() == Type::BoolTy && + assert(getCondition()->getType() == Type::Int1Ty && "May only branch on boolean predicates!"); } +BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) + : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertBefore) { + assert(IfTrue != 0 && "Branch destination may not be null!"); + Ops[0].init(reinterpret_cast(IfTrue), this); +} +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); +#ifndef NDEBUG + AssertOK(); +#endif +} + +BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) + : TerminatorInst(Type::VoidTy, Instruction::Br, Ops, 1, InsertAtEnd) { + assert(IfTrue != 0 && "Branch destination may not be null!"); + Ops[0].init(reinterpret_cast(IfTrue), this); +} + +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); +#ifndef NDEBUG + AssertOK(); +#endif +} + + BranchInst::BranchInst(const BranchInst &BI) : - TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) { + TerminatorInst(Type::VoidTy, Instruction::Br, Ops, BI.getNumOperands()) { OperandList[0].init(BI.getOperand(0), this); if (BI.getNumOperands() != 1) { assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); @@ -513,12 +780,12 @@ void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { static Value *getAISize(Value *Amt) { if (!Amt) - Amt = ConstantInt::get(Type::UIntTy, 1); + Amt = ConstantInt::get(Type::Int32Ty, 1); else { assert(!isa(Amt) && - "Passed basic block into allocation size parameter! Ue other ctor"); - assert(Amt->getType() == Type::UIntTy && - "Malloc/Allocation array size != UIntTy!"); + "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!"); } return Amt; } @@ -526,19 +793,21 @@ 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), - Name, InsertBefore), Alignment(Align) { + : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize), + InsertBefore), Alignment(Align) { assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); assert(Ty != Type::VoidTy && "Cannot allocate void!"); + setName(Name); } AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align, const std::string &Name, BasicBlock *InsertAtEnd) - : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize), - Name, InsertAtEnd), Alignment(Align) { + : UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize), + InsertAtEnd), Alignment(Align) { assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); assert(Ty != Type::VoidTy && "Cannot allocate void!"); + setName(Name); } // Out of line virtual method, so the vtable, etc has a home. @@ -546,8 +815,8 @@ AllocationInst::~AllocationInst() { } bool AllocationInst::isArrayAllocation() const { - if (ConstantInt *CUI = dyn_cast(getOperand(0))) - return CUI->getZExtValue() != 1; + if (ConstantInt *CI = dyn_cast(getOperand(0))) + return CI->getZExtValue() != 1; return true; } @@ -575,12 +844,12 @@ void FreeInst::AssertOK() { } FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore) - : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) { + : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertBefore) { AssertOK(); } FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd) - : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) { + : UnaryInstruction(Type::VoidTy, Free, Ptr, InsertAtEnd) { AssertOK(); } @@ -596,35 +865,107 @@ void LoadInst::AssertOK() { LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef) : UnaryInstruction(cast(Ptr->getType())->getElementType(), - Load, Ptr, Name, InsertBef) { + Load, Ptr, InsertBef) { setVolatile(false); + setAlignment(0); AssertOK(); + setName(Name); } LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE) : UnaryInstruction(cast(Ptr->getType())->getElementType(), - Load, Ptr, Name, InsertAE) { + Load, Ptr, InsertAE) { setVolatile(false); + setAlignment(0); AssertOK(); + setName(Name); } LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, Instruction *InsertBef) : UnaryInstruction(cast(Ptr->getType())->getElementType(), - Load, Ptr, Name, InsertBef) { + 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); } LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, BasicBlock *InsertAE) : UnaryInstruction(cast(Ptr->getType())->getElementType(), - Load, Ptr, Name, InsertAE) { + Load, Ptr, InsertAE) { setVolatile(isVolatile); + setAlignment(0); AssertOK(); + setName(Name); } + +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); +} + +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); +} + +LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, + Instruction *InsertBef) +: UnaryInstruction(cast(Ptr->getType())->getElementType(), + Load, Ptr, InsertBef) { + setVolatile(isVolatile); + setAlignment(0); + AssertOK(); + if (Name && Name[0]) setName(Name); +} + +LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, + BasicBlock *InsertAE) + : 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 //===----------------------------------------------------------------------===// @@ -639,68 +980,85 @@ void StoreInst::AssertOK() { StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) - : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) { + : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) { Ops[0].init(val, this); Ops[1].init(addr, this); setVolatile(false); + setAlignment(0); AssertOK(); } StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) - : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) { + : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) { Ops[0].init(val, this); Ops[1].init(addr, this); setVolatile(false); + setAlignment(0); AssertOK(); } StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, Instruction *InsertBefore) - : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) { + : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) { Ops[0].init(val, this); Ops[1].init(addr, this); setVolatile(isVolatile); + setAlignment(0); + AssertOK(); +} + +StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, + unsigned Align, Instruction *InsertBefore) + : Instruction(Type::VoidTy, Store, Ops, 2, InsertBefore) { + Ops[0].init(val, this); + Ops[1].init(addr, this); + setVolatile(isVolatile); + setAlignment(Align); + AssertOK(); +} + +StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, + unsigned Align, BasicBlock *InsertAtEnd) + : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) { + Ops[0].init(val, this); + Ops[1].init(addr, this); + setVolatile(isVolatile); + setAlignment(Align); AssertOK(); } StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, BasicBlock *InsertAtEnd) - : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) { + : Instruction(Type::VoidTy, Store, Ops, 2, InsertAtEnd) { Ops[0].init(val, this); Ops[1].init(addr, this); 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, const std::vector &Idx) { - NumOperands = 1+Idx.size(); +void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx) { + NumOperands = 1+NumIdx; Use *OL = OperandList = new Use[NumOperands]; OL[0].init(Ptr, this); - for (unsigned i = 0, e = Idx.size(); i != e; ++i) + 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); -} - void GetElementPtrInst::init(Value *Ptr, Value *Idx) { NumOperands = 2; Use *OL = OperandList = new Use[2]; @@ -708,50 +1066,22 @@ void GetElementPtrInst::init(Value *Ptr, Value *Idx) { OL[1].init(Idx, this); } -GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector &Idx, - const std::string &Name, Instruction *InBe) - : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), - Idx, true))), - GetElementPtr, 0, 0, Name, InBe) { - init(Ptr, Idx); -} - -GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector &Idx, - const std::string &Name, BasicBlock *IAE) - : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(), - Idx, true))), - GetElementPtr, 0, 0, Name, IAE) { - init(Ptr, Idx); -} - GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &Name, Instruction *InBe) - : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), - GetElementPtr, 0, 0, Name, InBe) { + : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)), + retrieveAddrSpace(Ptr)), + GetElementPtr, 0, 0, InBe) { init(Ptr, Idx); + setName(Name); } GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &Name, BasicBlock *IAE) - : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))), - GetElementPtr, 0, 0, Name, IAE) { + : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx)), + retrieveAddrSpace(Ptr)), + GetElementPtr, 0, 0, IAE) { init(Ptr, Idx); -} - -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, Name, InBe) { - init(Ptr, Idx0, Idx1); -} - -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, Name, IAE) { - init(Ptr, Idx0, Idx1); + setName(Name); } GetElementPtrInst::~GetElementPtrInst() { @@ -765,26 +1095,28 @@ GetElementPtrInst::~GetElementPtrInst() { // pointer type. // const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, - const std::vector &Idx, + Value* const *Idxs, + unsigned NumIdx, bool AllowCompositeLeaf) { if (!isa(Ptr)) return 0; // Type isn't a pointer type! // Handle the special case of the empty set index set... - if (Idx.empty()) + 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 (Idx.size() == CurIdx) { + if (NumIdx == CurIdx) { if (AllowCompositeLeaf || CT->isFirstClassType()) return Ptr; return 0; // Can't load a whole structure or array!?!? } - Value *Index = Idx[CurIdx++]; + Value *Index = Idxs[CurIdx++]; if (isa(CT) && CurIdx != 1) return 0; // Can only index into pointer types at the first index! if (!CT->indexValid(Index)) return 0; @@ -798,37 +1130,46 @@ const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Ptr = Ty; } } - return CurIdx == Idx.size() ? Ptr : 0; + return CurIdx == NumIdx ? Ptr : 0; } -const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, - Value *Idx0, Value *Idx1, - bool AllowCompositeLeaf) { +const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { 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; + if (!PTy->indexValid(Idx)) return 0; - const Type *ElTy = CT->getTypeAtIndex(Idx1); - if (AllowCompositeLeaf || ElTy->isFirstClassType()) - return ElTy; - return 0; + return PTy->getElementType(); } -const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { - const PointerType *PTy = dyn_cast(Ptr); - if (!PTy) return 0; // Type isn't a pointer type! - // Check the pointer index. - if (!PTy->indexValid(Idx)) return 0; +/// hasAllZeroIndices - Return true if all of the indices of this GEP are +/// zeros. If so, the result pointer and the first operand have the same +/// value, just potentially different types. +bool GetElementPtrInst::hasAllZeroIndices() const { + for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { + if (ConstantInt *CI = dyn_cast(getOperand(i))) { + if (!CI->isZero()) return false; + } else { + return false; + } + } + return true; +} - return PTy->getElementType(); +/// 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 //===----------------------------------------------------------------------===// @@ -836,55 +1177,59 @@ const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, const std::string &Name, Instruction *InsertBef) - : Instruction(cast(Val->getType())->getElementType(), - ExtractElement, Ops, 2, Name, InsertBef) { + : Instruction(cast(Val->getType())->getElementType(), + ExtractElement, Ops, 2, InsertBef) { assert(isValidOperands(Val, Index) && "Invalid extractelement instruction operands!"); Ops[0].init(Val, this); Ops[1].init(Index, this); + setName(Name); } ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, const std::string &Name, Instruction *InsertBef) - : Instruction(cast(Val->getType())->getElementType(), - ExtractElement, Ops, 2, Name, InsertBef) { - Constant *Index = ConstantInt::get(Type::UIntTy, IndexV); + : Instruction(cast(Val->getType())->getElementType(), + ExtractElement, Ops, 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); + setName(Name); } ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, const std::string &Name, BasicBlock *InsertAE) - : Instruction(cast(Val->getType())->getElementType(), - ExtractElement, Ops, 2, Name, InsertAE) { + : Instruction(cast(Val->getType())->getElementType(), + ExtractElement, Ops, 2, InsertAE) { assert(isValidOperands(Val, Index) && "Invalid extractelement instruction operands!"); Ops[0].init(Val, this); Ops[1].init(Index, this); + setName(Name); } ExtractElementInst::ExtractElementInst(Value *Val, unsigned IndexV, const std::string &Name, BasicBlock *InsertAE) - : Instruction(cast(Val->getType())->getElementType(), - ExtractElement, Ops, 2, Name, InsertAE) { - Constant *Index = ConstantInt::get(Type::UIntTy, IndexV); + : Instruction(cast(Val->getType())->getElementType(), + ExtractElement, Ops, 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); + setName(Name); } bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { - if (!isa(Val->getType()) || Index->getType() != Type::UIntTy) + if (!isa(Val->getType()) || Index->getType() != Type::Int32Ty) return false; return true; } @@ -903,61 +1248,65 @@ InsertElementInst::InsertElementInst(const InsertElementInst &IE) InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, const std::string &Name, Instruction *InsertBef) - : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) { + : Instruction(Vec->getType(), InsertElement, Ops, 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); + setName(Name); } InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, const std::string &Name, Instruction *InsertBef) - : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertBef) { - Constant *Index = ConstantInt::get(Type::UIntTy, IndexV); + : Instruction(Vec->getType(), InsertElement, Ops, 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); + setName(Name); } InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, const std::string &Name, BasicBlock *InsertAE) - : Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) { + : Instruction(Vec->getType(), InsertElement, Ops, 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); + setName(Name); } InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, unsigned IndexV, const std::string &Name, BasicBlock *InsertAE) -: Instruction(Vec->getType(), InsertElement, Ops, 3, Name, InsertAE) { - Constant *Index = ConstantInt::get(Type::UIntTy, IndexV); +: Instruction(Vec->getType(), InsertElement, Ops, 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); + setName(Name); } bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, const Value *Index) { - if (!isa(Vec->getType())) - return false; // First operand of insertelement must be packed type. + if (!isa(Vec->getType())) + 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. + if (Elt->getType() != cast(Vec->getType())->getElementType()) + return false;// Second operand of insertelement must be vector element type. - if (Index->getType() != Type::UIntTy) + if (Index->getType() != Type::Int32Ty) return false; // Third operand of insertelement must be uint. return true; } @@ -977,34 +1326,36 @@ ShuffleVectorInst::ShuffleVectorInst(const ShuffleVectorInst &SV) ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, const std::string &Name, Instruction *InsertBefore) - : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertBefore) { + : Instruction(V1->getType(), ShuffleVector, Ops, 3, 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); + setName(Name); } ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, const std::string &Name, BasicBlock *InsertAtEnd) - : Instruction(V1->getType(), ShuffleVector, Ops, 3, Name, InsertAtEnd) { + : Instruction(V1->getType(), ShuffleVector, Ops, 3, 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); + setName(Name); } bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, const Value *Mask) { - if (!isa(V1->getType())) return false; + if (!isa(V1->getType())) return false; if (V1->getType() != V2->getType()) return false; - if (!isa(Mask->getType()) || - cast(Mask->getType())->getElementType() != Type::UIntTy || - cast(Mask->getType())->getNumElements() != - cast(V1->getType())->getNumElements()) + if (!isa(Mask->getType()) || + cast(Mask->getType())->getElementType() != Type::Int32Ty || + cast(Mask->getType())->getNumElements() != + cast(V1->getType())->getNumElements()) return false; return true; } @@ -1014,9 +1365,30 @@ bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, // BinaryOperator Class //===----------------------------------------------------------------------===// -void BinaryOperator::init(BinaryOps iType) -{ +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); + init(iType); + setName(Name); +} + +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); + init(iType); + setName(Name); +} + + +void BinaryOperator::init(BinaryOps iType) { Value *LHS = getOperand(0), *RHS = getOperand(1); + LHS = LHS; RHS = RHS; // Silence warnings. assert(LHS->getType() == RHS->getType() && "Binary operator operand types must match!"); #ifndef NDEBUG @@ -1026,46 +1398,54 @@ void BinaryOperator::init(BinaryOps iType) assert(getType() == LHS->getType() && "Arithmetic operation should return same type as operands!"); assert((getType()->isInteger() || getType()->isFloatingPoint() || - isa(getType())) && + isa(getType())) && "Tried to create an arithmetic operation on a non-arithmetic type!"); break; case UDiv: case SDiv: assert(getType() == LHS->getType() && "Arithmetic operation should return same type as operands!"); - assert((getType()->isInteger() || (isa(getType()) && - cast(getType())->getElementType()->isInteger())) && + assert((getType()->isInteger() || (isa(getType()) && + cast(getType())->getElementType()->isInteger())) && "Incorrect operand type (not integer) for S/UDIV"); break; case FDiv: assert(getType() == LHS->getType() && "Arithmetic operation should return same type as operands!"); - assert((getType()->isFloatingPoint() || (isa(getType()) && - cast(getType())->getElementType()->isFloatingPoint())) + assert((getType()->isFloatingPoint() || (isa(getType()) && + cast(getType())->getElementType()->isFloatingPoint())) && "Incorrect operand type (not floating point) for FDIV"); break; case URem: case SRem: assert(getType() == LHS->getType() && "Arithmetic operation should return same type as operands!"); - assert((getType()->isInteger() || (isa(getType()) && - cast(getType())->getElementType()->isInteger())) && + assert((getType()->isInteger() || (isa(getType()) && + cast(getType())->getElementType()->isInteger())) && "Incorrect operand type (not integer) for S/UREM"); break; case FRem: assert(getType() == LHS->getType() && "Arithmetic operation should return same type as operands!"); - assert((getType()->isFloatingPoint() || (isa(getType()) && - cast(getType())->getElementType()->isFloatingPoint())) + assert((getType()->isFloatingPoint() || (isa(getType()) && + cast(getType())->getElementType()->isFloatingPoint())) && "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"); + break; case And: case Or: case Xor: assert(getType() == LHS->getType() && "Logical operation should return same type as operands!"); - assert((getType()->isIntegral() || - (isa(getType()) && - cast(getType())->getElementType()->isIntegral())) && + assert((getType()->isInteger() || + (isa(getType()) && + cast(getType())->getElementType()->isInteger())) && "Tried to create a logical operation on a non-integral type!"); break; default: @@ -1092,36 +1472,28 @@ BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2, BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, Instruction *InsertBefore) { - if (!Op->getType()->isFloatingPoint()) - return new BinaryOperator(Instruction::Sub, - Constant::getNullValue(Op->getType()), Op, - Op->getType(), Name, InsertBefore); - else - return new BinaryOperator(Instruction::Sub, - ConstantFP::get(Op->getType(), -0.0), Op, - Op->getType(), Name, InsertBefore); + Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); + return new BinaryOperator(Instruction::Sub, + zero, Op, + Op->getType(), Name, InsertBefore); } BinaryOperator *BinaryOperator::createNeg(Value *Op, const std::string &Name, BasicBlock *InsertAtEnd) { - if (!Op->getType()->isFloatingPoint()) - return new BinaryOperator(Instruction::Sub, - Constant::getNullValue(Op->getType()), Op, - Op->getType(), Name, InsertAtEnd); - else - return new BinaryOperator(Instruction::Sub, - ConstantFP::get(Op->getType(), -0.0), Op, - Op->getType(), Name, InsertAtEnd); + Value *zero = ConstantExpr::getZeroValueForNegationExpr(Op->getType()); + return new BinaryOperator(Instruction::Sub, + zero, Op, + Op->getType(), Name, InsertAtEnd); } BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, Instruction *InsertBefore) { Constant *C; - if (const PackedType *PTy = dyn_cast(Op->getType())) { - C = ConstantIntegral::getAllOnesValue(PTy->getElementType()); - C = ConstantPacked::get(std::vector(PTy->getNumElements(), C)); + if (const VectorType *PTy = dyn_cast(Op->getType())) { + C = ConstantInt::getAllOnesValue(PTy->getElementType()); + C = ConstantVector::get(std::vector(PTy->getNumElements(), C)); } else { - C = ConstantIntegral::getAllOnesValue(Op->getType()); + C = ConstantInt::getAllOnesValue(Op->getType()); } return new BinaryOperator(Instruction::Xor, Op, C, @@ -1131,13 +1503,13 @@ BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, BinaryOperator *BinaryOperator::createNot(Value *Op, const std::string &Name, BasicBlock *InsertAtEnd) { Constant *AllOnes; - if (const PackedType *PTy = dyn_cast(Op->getType())) { + if (const VectorType *PTy = dyn_cast(Op->getType())) { // Create a vector of all ones values. - Constant *Elt = ConstantIntegral::getAllOnesValue(PTy->getElementType()); + Constant *Elt = ConstantInt::getAllOnesValue(PTy->getElementType()); AllOnes = - ConstantPacked::get(std::vector(PTy->getNumElements(), Elt)); + ConstantVector::get(std::vector(PTy->getNumElements(), Elt)); } else { - AllOnes = ConstantIntegral::getAllOnesValue(Op->getType()); + AllOnes = ConstantInt::getAllOnesValue(Op->getType()); } return new BinaryOperator(Instruction::Xor, Op, AllOnes, @@ -1147,16 +1519,18 @@ 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) { if (const BinaryOperator *Bop = dyn_cast(V)) if (Bop->getOpcode() == Instruction::Sub) - if (!V->getType()->isFloatingPoint()) - return Bop->getOperand(0) == Constant::getNullValue(Bop->getType()); - else - return Bop->getOperand(0) == ConstantFP::get(Bop->getType(), -0.0); + return Bop->getOperand(0) == + ConstantExpr::getZeroValueForNegationExpr(Bop->getType()); return false; } @@ -1218,7 +1592,7 @@ bool CastInst::isIntegerCast() const { case Instruction::Trunc: return true; case Instruction::BitCast: - return getOperand(0)->getType()->isIntegral() && getType()->isIntegral(); + return getOperand(0)->getType()->isInteger() && getType()->isInteger(); } } @@ -1233,23 +1607,9 @@ bool CastInst::isLosslessCast() const { if (SrcTy == DstTy) return true; - // The remaining possibilities are lossless if the typeID of the source type - // matches the type ID of the destination in size and fundamental type. This - // prevents things like int -> ptr, int -> float, packed -> int, mismatched - // packed types of the same size, and etc. - switch (SrcTy->getTypeID()) { - case Type::UByteTyID: return DstTy == Type::SByteTy; - case Type::SByteTyID: return DstTy == Type::UByteTy; - case Type::UShortTyID: return DstTy == Type::ShortTy; - case Type::ShortTyID: return DstTy == Type::UShortTy; - case Type::UIntTyID: return DstTy == Type::IntTy; - case Type::IntTyID: return DstTy == Type::UIntTy; - case Type::ULongTyID: return DstTy == Type::LongTy; - case Type::LongTyID: return DstTy == Type::ULongTy; - case Type::PointerTyID: return isa(DstTy); - default: - break; - } + // Pointer to pointer is always lossless. + if (isa(SrcTy)) + return isa(DstTy); return false; // Other types have no identity values } @@ -1259,7 +1619,7 @@ bool CastInst::isLosslessCast() const { /// 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> +/// # bitcast vector< 2 x int > %x, vector< 4 x short> /// # ptrtoint uint* %x, uint ; on 32-bit plaforms only /// @brief Determine if a cast is a no-op. bool CastInst::isNoopCast(const Type *IntPtrTy) const { @@ -1542,10 +1902,10 @@ CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd) { assert(isa(S->getType()) && "Invalid cast"); - assert((Ty->isIntegral() || Ty->getTypeID() == Type::PointerTyID) && + assert((Ty->isInteger() || isa(Ty)) && "Invalid cast"); - if (Ty->isIntegral()) + if (Ty->isInteger()) return create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); return create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); } @@ -1555,10 +1915,10 @@ CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore) { assert(isa(S->getType()) && "Invalid cast"); - assert((Ty->isIntegral() || Ty->getTypeID() == Type::PointerTyID) && + assert((Ty->isInteger() || isa(Ty)) && "Invalid cast"); - if (Ty->isIntegral()) + if (Ty->isInteger()) return create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); return create(Instruction::BitCast, S, Ty, Name, InsertBefore); } @@ -1566,7 +1926,7 @@ CastInst *CastInst::createPointerCast(Value *S, const Type *Ty, CastInst *CastInst::createIntegerCast(Value *C, const Type *Ty, bool isSigned, const std::string &Name, Instruction *InsertBefore) { - assert(C->getType()->isIntegral() && Ty->isIntegral() && "Invalid cast"); + assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); unsigned DstBits = Ty->getPrimitiveSizeInBits(); Instruction::CastOps opcode = @@ -1579,7 +1939,7 @@ 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()->isIntegral() && Ty->isIntegral() && "Invalid cast"); + assert(C->getType()->isInteger() && Ty->isInteger() && "Invalid cast"); unsigned SrcBits = C->getType()->getPrimitiveSizeInBits(); unsigned DstBits = Ty->getPrimitiveSizeInBits(); Instruction::CastOps opcode = @@ -1615,23 +1975,84 @@ CastInst *CastInst::createFPCast(Value *C, const Type *Ty, 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 // types and size of the operand. This, basically, is a parallel of the -// logic in the checkCast function below. This axiom should hold: -// checkCast( getCastOpcode(Val, Ty), Val, Ty) -// should not assert in checkCast. In other words, this produces a "correct" +// logic in the castIsValid function below. This axiom should hold: +// 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->isIntegral()) { // Casting to integral - if (SrcTy->isIntegral()) { // Casting from integral + if (DestTy->isInteger()) { // Casting to integral + if (SrcTy->isInteger()) { // Casting from integral if (DestBits < SrcBits) return Trunc; // int -> smaller int else if (DestBits > SrcBits) { // its an extension @@ -1647,9 +2068,9 @@ CastInst::getCastOpcode( return FPToSI; // FP -> sint else return FPToUI; // FP -> uint - } else if (const PackedType *PTy = dyn_cast(SrcTy)) { + } 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) && @@ -1657,7 +2078,7 @@ CastInst::getCastOpcode( return PtrToInt; // ptr -> int } } else if (DestTy->isFloatingPoint()) { // Casting to floating pt - if (SrcTy->isIntegral()) { // Casting from integral + if (SrcTy->isInteger()) { // Casting from integral if (SrcIsSigned) return SIToFP; // sint -> FP else @@ -1670,27 +2091,27 @@ CastInst::getCastOpcode( } else { return BitCast; // same size, no-op cast } - } else if (const PackedType *PTy = dyn_cast(SrcTy)) { + } 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"); } - } else if (const PackedType *DestPTy = dyn_cast(DestTy)) { - if (const PackedType *SrcPTy = dyn_cast(SrcTy)) { + } 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)) { return BitCast; // ptr -> ptr - } else if (SrcTy->isIntegral()) { + } else if (SrcTy->isInteger()) { return IntToPtr; // int -> ptr } else { assert(!"Casting pointer to other than pointer or int"); @@ -1713,8 +2134,8 @@ CastInst::getCastOpcode( /// could be broken out into the separate constructors but it is useful to have /// it in one place and to eliminate the redundant code for getting the sizes /// of the types involved. -static bool -checkCast(Instruction::CastOps op, Value *S, const Type *DstTy) { +bool +CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) { // Check for type sanity on the arguments const Type *SrcTy = S->getType(); @@ -1729,11 +2150,11 @@ checkCast(Instruction::CastOps op, Value *S, const Type *DstTy) { switch (op) { default: return false; // This is an input error case Instruction::Trunc: - return SrcTy->isInteger() && DstTy->isIntegral() && SrcBitSize > DstBitSize; + return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize > DstBitSize; case Instruction::ZExt: - return SrcTy->isIntegral() && DstTy->isInteger() && SrcBitSize < DstBitSize; + return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize; case Instruction::SExt: - return SrcTy->isIntegral() && DstTy->isInteger() && SrcBitSize < DstBitSize; + return SrcTy->isInteger() && DstTy->isInteger()&& SrcBitSize < DstBitSize; case Instruction::FPTrunc: return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && SrcBitSize > DstBitSize; @@ -1741,24 +2162,36 @@ checkCast(Instruction::CastOps op, Value *S, const Type *DstTy) { return SrcTy->isFloatingPoint() && DstTy->isFloatingPoint() && SrcBitSize < DstBitSize; case Instruction::UIToFP: - return SrcTy->isIntegral() && DstTy->isFloatingPoint(); case Instruction::SIToFP: - return SrcTy->isIntegral() && DstTy->isFloatingPoint(); + 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->isIntegral(); case Instruction::FPToSI: - return SrcTy->isFloatingPoint() && DstTy->isIntegral(); + 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->isIntegral(); + return isa(SrcTy) && DstTy->isInteger(); case Instruction::IntToPtr: - return SrcTy->isIntegral() && isa(DstTy); + return SrcTy->isInteger() && isa(DstTy); case Instruction::BitCast: // BitCast implies a no-op cast of type only. No bits change. // However, you can't cast pointers to anything but pointers. 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; @@ -1768,144 +2201,144 @@ checkCast(Instruction::CastOps op, Value *S, const Type *DstTy) { TruncInst::TruncInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal Trunc"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); } TruncInst::TruncInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal Trunc"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); } ZExtInst::ZExtInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal ZExt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); } ZExtInst::ZExtInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal ZExt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); } SExtInst::SExtInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, SExt, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal SExt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); } SExtInst::SExtInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal SExt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); } FPTruncInst::FPTruncInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPTrunc"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); } FPTruncInst::FPTruncInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPTrunc"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); } FPExtInst::FPExtInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPExt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); } FPExtInst::FPExtInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPExt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); } UIToFPInst::UIToFPInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal UIToFP"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); } UIToFPInst::UIToFPInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal UIToFP"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); } SIToFPInst::SIToFPInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal SIToFP"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); } SIToFPInst::SIToFPInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal SIToFP"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); } FPToUIInst::FPToUIInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPToUI"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); } FPToUIInst::FPToUIInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPToUI"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); } FPToSIInst::FPToSIInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPToSI"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); } FPToSIInst::FPToSIInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal FPToSI"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); } PtrToIntInst::PtrToIntInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal PtrToInt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); } PtrToIntInst::PtrToIntInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal PtrToInt"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); } IntToPtrInst::IntToPtrInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal IntToPtr"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); } IntToPtrInst::IntToPtrInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal IntToPtr"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); } BitCastInst::BitCastInst( Value *S, const Type *Ty, const std::string &Name, Instruction *InsertBefore ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal BitCast"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); } BitCastInst::BitCastInst( Value *S, const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { - assert(checkCast(getOpcode(), S, Ty) && "Illegal BitCast"); + assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); } //===----------------------------------------------------------------------===// @@ -1914,10 +2347,11 @@ BitCastInst::BitCastInst( CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, const std::string &Name, Instruction *InsertBefore) - : Instruction(Type::BoolTy, op, Ops, 2, Name, InsertBefore) { + : Instruction(Type::Int1Ty, op, Ops, 2, InsertBefore) { Ops[0].init(LHS, this); Ops[1].init(RHS, this); SubclassData = predicate; + setName(Name); if (op == Instruction::ICmp) { assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE && predicate <= ICmpInst::LAST_ICMP_PREDICATE && @@ -1927,9 +2361,7 @@ CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, assert(Op0Ty == Op1Ty && "Both operands to ICmp instruction are not of the same type!"); // Check that the operands are the right type - assert(Op0Ty->isIntegral() || Op0Ty->getTypeID() == Type::PointerTyID || - (isa(Op0Ty) && - cast(Op0Ty)->getElementType()->isIntegral()) && + assert((Op0Ty->isInteger() || isa(Op0Ty)) && "Invalid operand types for ICmp instruction"); return; } @@ -1941,17 +2373,17 @@ CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, 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() || (isa(Op0Ty) && - cast(Op0Ty)->getElementType()->isFloatingPoint()) && + 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::BoolTy, op, Ops, 2, Name, InsertAtEnd) { + : Instruction(Type::Int1Ty, op, Ops, 2, InsertAtEnd) { Ops[0].init(LHS, this); Ops[1].init(RHS, this); SubclassData = predicate; + setName(Name); if (op == Instruction::ICmp) { assert(predicate >= ICmpInst::FIRST_ICMP_PREDICATE && predicate <= ICmpInst::LAST_ICMP_PREDICATE && @@ -1962,9 +2394,7 @@ CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, assert(Op0Ty == Op1Ty && "Both operands to ICmp instruction are not of the same type!"); // Check that the operands are the right type - assert(Op0Ty->isIntegral() || Op0Ty->getTypeID() == Type::PointerTyID || - (isa(Op0Ty) && - cast(Op0Ty)->getElementType()->isIntegral()) && + assert((Op0Ty->isInteger() || isa(Op0Ty)) && "Invalid operand types for ICmp instruction"); return; } @@ -1976,8 +2406,7 @@ CmpInst::CmpInst(OtherOps op, unsigned short predicate, Value *LHS, Value *RHS, 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() || (isa(Op0Ty) && - cast(Op0Ty)->getElementType()->isFloatingPoint()) && + assert(Op0Ty->isFloatingPoint() && "Invalid operand types for FCmp instruction"); } @@ -2069,6 +2498,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!"); @@ -2080,6 +2522,41 @@ bool ICmpInst::isSignedPredicate(Predicate pred) { } } +/// Initialize a set of values that all satisfy the condition with C. +/// +ConstantRange +ICmpInst::makeConstantRange(Predicate pred, const APInt &C) { + APInt Lower(C); + APInt Upper(C); + uint32_t BitWidth = C.getBitWidth(); + switch (pred) { + default: assert(0 && "Invalid ICmp opcode to ConstantRange ctor!"); + case ICmpInst::ICMP_EQ: Upper++; break; + case ICmpInst::ICMP_NE: Lower++; break; + case ICmpInst::ICMP_ULT: Lower = APInt::getMinValue(BitWidth); break; + case ICmpInst::ICMP_SLT: Lower = APInt::getSignedMinValue(BitWidth); break; + case ICmpInst::ICMP_UGT: + Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) + break; + case ICmpInst::ICMP_SGT: + Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) + break; + case ICmpInst::ICMP_ULE: + Lower = APInt::getMinValue(BitWidth); Upper++; + break; + case ICmpInst::ICMP_SLE: + Lower = APInt::getSignedMinValue(BitWidth); Upper++; + break; + case ICmpInst::ICMP_UGE: + Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) + break; + case ICmpInst::ICMP_SGE: + Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) + break; + } + return ConstantRange(Lower, Upper); +} + FCmpInst::Predicate FCmpInst::getInversePredicate(Predicate pred) { switch (pred) { default: @@ -2170,9 +2647,29 @@ void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) { OperandList[1].init(Default, this); } +/// SwitchInst ctor - Create a new switch instruction, specifying a value to +/// switch on and a default destination. The number of additional cases can +/// be specified here to make memory allocation more efficient. This +/// constructor can also autoinsert before another instruction. +SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, + Instruction *InsertBefore) + : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertBefore) { + init(Value, Default, NumCases); +} + +/// SwitchInst ctor - Create a new switch instruction, specifying a value to +/// switch on and a default destination. The number of additional cases can +/// be specified here to make memory allocation more efficient. This +/// constructor also autoinserts at the end of the specified BasicBlock. +SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, + BasicBlock *InsertAtEnd) + : TerminatorInst(Type::VoidTy, Instruction::Switch, 0, 0, InsertAtEnd) { + init(Value, Default, NumCases); +} + SwitchInst::SwitchInst(const SwitchInst &SI) - : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()], - SI.getNumOperands()) { + : TerminatorInst(Type::VoidTy, Instruction::Switch, + new Use[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); @@ -2266,6 +2763,41 @@ void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { setSuccessor(idx, B); } +//===----------------------------------------------------------------------===// +// GetResultInst Implementation +//===----------------------------------------------------------------------===// + +GetResultInst::GetResultInst(Value *Aggregate, unsigned Index, + const std::string &Name, + Instruction *InsertBef) + : Instruction(cast(Aggregate->getType())->getElementType(Index), + GetResult, &Aggr, 1, InsertBef) { + assert(isValidOperands(Aggregate, Index) && "Invalid GetResultInst operands!"); + Aggr.init(Aggregate, this); + Idx = Index; + 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) + 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. @@ -2278,8 +2810,11 @@ BinaryOperator *BinaryOperator::clone() const { return create(getOpcode(), Ops[0], Ops[1]); } -CmpInst* CmpInst::clone() const { - return create(getOpcode(), getPredicate(), Ops[0], Ops[1]); +FCmpInst* FCmpInst::clone() const { + return new FCmpInst(getPredicate(), Ops[0], Ops[1]); +} +ICmpInst* ICmpInst::clone() const { + return new ICmpInst(getPredicate(), Ops[0], Ops[1]); } MallocInst *MallocInst::clone() const { return new MallocInst(*this); } @@ -2300,7 +2835,6 @@ 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); } -ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); } SelectInst *SelectInst::clone() const { return new SelectInst(*this); } VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); } @@ -2320,3 +2854,4 @@ SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); } InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); } UnwindInst *UnwindInst::clone() const { return new UnwindInst(); } UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();} +GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }