X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FVMCore%2FInstructions.cpp;h=c0b3413da33782870832f11d011181ba3ab69485;hb=e6be34a53ecbe8c2ff9f0793b13d847e94c0de91;hp=5179f04f011bdbf22fe4bd5ed947db9a93b4d89c;hpb=50ee9ddc8f0633af6cb0a5693a2c706e98f944da;p=oota-llvm.git diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index 5179f04f011..c0b3413da33 100644 --- a/lib/VMCore/Instructions.cpp +++ b/lib/VMCore/Instructions.cpp @@ -17,7 +17,7 @@ #include "llvm/DerivedTypes.h" #include "llvm/Function.h" #include "llvm/Instructions.h" -#include "llvm/ParameterAttributes.h" +#include "llvm/ParamAttrsList.h" #include "llvm/Support/CallSite.h" #include "llvm/Support/ConstantRange.h" #include "llvm/Support/MathExtras.h" @@ -27,6 +27,10 @@ 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(); @@ -51,12 +55,19 @@ void CallSite::setParamAttrs(const ParamAttrsList *PAL) { else cast(I)->setParamAttrs(PAL); } -bool CallSite::paramHasAttr(uint16_t i, unsigned attr) const { +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->paramHasAttr(i, (ParameterAttributes)attr); + return CI->getParamAlignment(i); else - return cast(I)->paramHasAttr(i, (ParameterAttributes)attr); + return cast(I)->getParamAlignment(i); } + bool CallSite::doesNotAccessMemory() const { if (CallInst *CI = dyn_cast(I)) return CI->doesNotAccessMemory(); @@ -182,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. @@ -194,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 @@ -370,14 +383,22 @@ void CallInst::setParamAttrs(const ParamAttrsList *newAttrs) { ParamAttrs = newAttrs; } -bool CallInst::paramHasAttr(uint16_t i, unsigned attr) const { - if (ParamAttrs && ParamAttrs->paramHasAttr(i, (ParameterAttributes)attr)) +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, (ParameterAttributes)attr); + 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); @@ -404,6 +425,17 @@ bool CallInst::isStructReturn() const { 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) @@ -436,8 +468,8 @@ void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, 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++) { @@ -483,14 +515,21 @@ void InvokeInst::setParamAttrs(const ParamAttrsList *newAttrs) { ParamAttrs = newAttrs; } -bool InvokeInst::paramHasAttr(uint16_t i, unsigned attr) const { - if (ParamAttrs && ParamAttrs->paramHasAttr(i, (ParameterAttributes)attr)) +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, (ParameterAttributes)attr); + 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 { @@ -535,30 +574,66 @@ bool InvokeInst::isStructReturn() const { ReturnInst::ReturnInst(const ReturnInst &RI) : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, RI.getNumOperands()) { - if (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) { - init(retVal); + if (retVal) + init(&retVal, 1); } ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd) : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) { - init(retVal); + 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; + } -void ReturnInst::init(Value *retVal) { - if (retVal && retVal->getType() != Type::VoidTy) { - assert(!isa(retVal) && + 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); } } @@ -566,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!"); } @@ -578,6 +653,10 @@ BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { return 0; } +ReturnInst::~ReturnInst() { + if (NumOperands > 1) + delete [] OperandList; +} //===----------------------------------------------------------------------===// // UnwindInst Implementation @@ -1022,12 +1101,13 @@ const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, if (!isa(Ptr)) return 0; // Type isn't a pointer type! // Handle the special case of the empty set index set... - if (NumIdx == 0) + 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)) { @@ -1895,12 +1975,70 @@ 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 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) { @@ -1909,6 +2047,9 @@ CastInst::getCastOpcode( 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 if (SrcTy->isInteger()) { // Casting from integral @@ -2050,7 +2191,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; @@ -2253,7 +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->isInteger() || isa(Op0Ty) && + assert((Op0Ty->isInteger() || isa(Op0Ty)) && "Invalid operand types for ICmp instruction"); return; } @@ -2357,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!"); @@ -2609,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. @@ -2665,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); }