#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"
else
cast<InvokeInst>(I)->setCallingConv(CC);
}
-const ParamAttrsList* CallSite::getParamAttrs() const {
+const PAListPtr &CallSite::getParamAttrs() const {
if (CallInst *CI = dyn_cast<CallInst>(I))
return CI->getParamAttrs();
else
return cast<InvokeInst>(I)->getParamAttrs();
}
-void CallSite::setParamAttrs(const ParamAttrsList *PAL) {
+void CallSite::setParamAttrs(const PAListPtr &PAL) {
if (CallInst *CI = dyn_cast<CallInst>(I))
CI->setParamAttrs(PAL);
else
CallInst::~CallInst() {
delete [] OperandList;
- if (ParamAttrs)
- ParamAttrs->dropRef();
}
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);
}
void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
- ParamAttrs = 0;
NumOperands = 3;
Use *OL = OperandList = new Use[3];
OL[0].init(Func, this);
}
void CallInst::init(Value *Func, Value *Actual) {
- ParamAttrs = 0;
NumOperands = 2;
Use *OL = OperandList = new Use[2];
OL[0].init(Func, this);
}
void CallInst::init(Value *Func) {
- ParamAttrs = 0;
NumOperands = 1;
Use *OL = OperandList = new Use[1];
OL[0].init(Func, this);
CallInst::CallInst(const CallInst &CI)
: Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
- CI.getNumOperands()),
- ParamAttrs(0) {
+ CI.getNumOperands()) {
setParamAttrs(CI.getParamAttrs());
SubclassData = CI.SubclassData;
Use *OL = OperandList;
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))
+bool CallInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
+ if (ParamAttrs.paramHasAttr(i, attr))
return true;
if (const Function *F = getCalledFunction())
return F->paramHasAttr(i, attr);
return false;
}
-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();
+ PAListPtr PAL = getParamAttrs();
if (doesNotThrow)
- PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind);
+ PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
else
- PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind);
+ PAL = PAL.removeAttr(0, ParamAttr::NoUnwind);
setParamAttrs(PAL);
}
InvokeInst::~InvokeInst() {
delete [] OperandList;
- if (ParamAttrs)
- ParamAttrs->dropRef();
}
void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
Value* const *Args, unsigned NumArgs) {
- ParamAttrs = 0;
NumOperands = 3+NumArgs;
Use *OL = OperandList = new Use[3+NumArgs];
OL[0].init(Fn, this);
InvokeInst::InvokeInst(const InvokeInst &II)
: TerminatorInst(II.getType(), Instruction::Invoke,
- new Use[II.getNumOperands()], II.getNumOperands()),
- ParamAttrs(0) {
+ new Use[II.getNumOperands()], II.getNumOperands()) {
setParamAttrs(II.getParamAttrs());
SubclassData = II.SubclassData;
Use *OL = OperandList, *InOL = II.OperandList;
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))
+bool InvokeInst::paramHasAttr(unsigned i, ParameterAttributes attr) const {
+ if (ParamAttrs.paramHasAttr(i, attr))
return true;
if (const Function *F = getCalledFunction())
return F->paramHasAttr(i, attr);
return false;
}
-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();
+ PAListPtr PAL = getParamAttrs();
if (doesNotThrow)
- PAL = ParamAttrsList::includeAttrs(PAL, 0, ParamAttr::NoUnwind);
+ PAL = PAL.addAttr(0, ParamAttr::NoUnwind);
else
- PAL = ParamAttrsList::excludeAttrs(PAL, 0, ParamAttr::NoUnwind);
+ PAL = PAL.removeAttr(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 * const* retVals, unsigned N) {
-
assert (N > 0 && "Invalid operands numbers in ReturnInst init");
NumOperands = N;
unsigned Align, const std::string &Name,
Instruction *InsertBefore)
: UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
- InsertBefore), Alignment(Align) {
- assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
+ InsertBefore) {
+ setAlignment(Align);
assert(Ty != Type::VoidTy && "Cannot allocate void!");
setName(Name);
}
unsigned Align, const std::string &Name,
BasicBlock *InsertAtEnd)
: UnaryInstruction(PointerType::getUnqual(Ty), iTy, getAISize(ArraySize),
- InsertAtEnd), Alignment(Align) {
- assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
+ InsertAtEnd) {
+ setAlignment(Align);
assert(Ty != Type::VoidTy && "Cannot allocate void!");
setName(Name);
}
AllocationInst::~AllocationInst() {
}
+void AllocationInst::setAlignment(unsigned Align) {
+ assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
+ SubclassData = Log2_32(Align) + 1;
+ assert(getAlignment() == Align && "Alignment representation error!");
+}
+
bool AllocationInst::isArrayAllocation() const {
if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0)))
return CI->getZExtValue() != 1;
bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2,
const Value *Mask) {
- if (!isa<VectorType>(V1->getType())) return false;
- if (V1->getType() != V2->getType()) return false;
- if (!isa<VectorType>(Mask->getType()) ||
- cast<VectorType>(Mask->getType())->getElementType() != Type::Int32Ty ||
- cast<VectorType>(Mask->getType())->getNumElements() !=
- cast<VectorType>(V1->getType())->getNumElements())
+ if (!isa<VectorType>(V1->getType()) ||
+ V1->getType() != V2->getType())
+ return false;
+
+ const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType());
+ if (!isa<Constant>(Mask) || MaskTy == 0 ||
+ MaskTy->getElementType() != Type::Int32Ty ||
+ MaskTy->getNumElements() !=
+ cast<VectorType>(V1->getType())->getNumElements())
return false;
return true;
}
+/// getMaskValue - Return the index from the shuffle mask for the specified
+/// output result. This is either -1 if the element is undef or a number less
+/// than 2*numelements.
+int ShuffleVectorInst::getMaskValue(unsigned i) const {
+ const Constant *Mask = cast<Constant>(getOperand(2));
+ if (isa<UndefValue>(Mask)) return -1;
+ if (isa<ConstantAggregateZero>(Mask)) return 0;
+ const ConstantVector *MaskCV = cast<ConstantVector>(Mask);
+ assert(i < MaskCV->getNumOperands() && "Index out of range");
+
+ if (isa<UndefValue>(MaskCV->getOperand(i)))
+ return -1;
+ return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue();
+}
+
//===----------------------------------------------------------------------===//
// BinaryOperator Class
// unit that uses these classes.
GetElementPtrInst *GetElementPtrInst::clone() const {
- return new GetElementPtrInst(*this);
+ return new(getNumOperands()) GetElementPtrInst(*this);
}
BinaryOperator *BinaryOperator::clone() const {
CastInst *PtrToIntInst::clone() const { return new PtrToIntInst(*this); }
CastInst *IntToPtrInst::clone() const { return new IntToPtrInst(*this); }
CastInst *BitCastInst::clone() const { return new BitCastInst(*this); }
-CallInst *CallInst::clone() const { return new CallInst(*this); }
-SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
+CallInst *CallInst::clone() const { return new(getNumOperands()) CallInst(*this); }
+SelectInst *SelectInst::clone() const { return new(getNumOperands()) SelectInst(*this); }
VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
ExtractElementInst *ExtractElementInst::clone() const {
return new ExtractElementInst(*this);
}
InsertElementInst *InsertElementInst::clone() const {
- return new InsertElementInst(*this);
+ return InsertElementInst::Create(*this);
}
ShuffleVectorInst *ShuffleVectorInst::clone() const {
return new ShuffleVectorInst(*this);
}
PHINode *PHINode::clone() const { return new PHINode(*this); }
-ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }
-BranchInst *BranchInst::clone() const { return new BranchInst(*this); }
-SwitchInst *SwitchInst::clone() const { return new SwitchInst(*this); }
-InvokeInst *InvokeInst::clone() const { return new InvokeInst(*this); }
+ReturnInst *ReturnInst::clone() const { return new(getNumOperands()) ReturnInst(*this); }
+BranchInst *BranchInst::clone() const { return new(getNumOperands()) BranchInst(*this); }
+SwitchInst *SwitchInst::clone() const { return new(getNumOperands()) SwitchInst(*this); }
+InvokeInst *InvokeInst::clone() const { return new(getNumOperands()) InvokeInst(*this); }
UnwindInst *UnwindInst::clone() const { return new UnwindInst(); }
UnreachableInst *UnreachableInst::clone() const { return new UnreachableInst();}
GetResultInst *GetResultInst::clone() const { return new GetResultInst(*this); }