#ifndef LLVM_IR_CALLSITE_H
#define LLVM_IR_CALLSITE_H
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/CallingConv.h"
#include "llvm/IR/Instructions.h"
namespace llvm {
class CallInst;
class InvokeInst;
template <typename FunTy = const Function,
typename ValTy = const Value,
typename UserTy = const User,
typename InstrTy = const Instruction,
typename CallTy = const CallInst,
typename InvokeTy = const InvokeInst,
typename IterTy = User::const_op_iterator>
class CallSiteBase {
protected:
PointerIntPair<InstrTy*, 1, bool> I;
public:
CallSiteBase() : I(nullptr, false) {}
CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); }
CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); }
CallSiteBase(ValTy *II) { *this = get(II); }
protected:
static CallSiteBase get(ValTy *V) {
if (InstrTy *II = dyn_cast<InstrTy>(V)) {
if (II->getOpcode() == Instruction::Call)
return CallSiteBase(static_cast<CallTy*>(II));
else if (II->getOpcode() == Instruction::Invoke)
return CallSiteBase(static_cast<InvokeTy*>(II));
}
return CallSiteBase();
}
public:
bool isCall() const { return I.getInt(); }
bool isInvoke() const { return getInstruction() && !I.getInt(); }
InstrTy *getInstruction() const { return I.getPointer(); }
InstrTy *operator->() const { return I.getPointer(); }
LLVM_EXPLICIT operator bool() const { return I.getPointer(); }
ValTy *getCalledValue() const {
assert(getInstruction() && "Not a call or invoke instruction!");
return *getCallee();
}
FunTy *getCalledFunction() const {
return dyn_cast<FunTy>(getCalledValue());
}
void setCalledFunction(Value *V) {
assert(getInstruction() && "Not a call or invoke instruction!");
*getCallee() = V;
}
bool isCallee(Value::const_user_iterator UI) const {
return isCallee(&UI.getUse());
}
bool isCallee(const Use *U) const { return getCallee() == U; }
ValTy *getArgument(unsigned ArgNo) const {
assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
return *(arg_begin() + ArgNo);
}
void setArgument(unsigned ArgNo, Value* newVal) {
assert(getInstruction() && "Not a call or invoke instruction!");
assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
getInstruction()->setOperand(ArgNo, newVal);
}
unsigned getArgumentNo(Value::const_user_iterator I) const {
return getArgumentNo(&I.getUse());
}
unsigned getArgumentNo(const Use *U) const {
assert(getInstruction() && "Not a call or invoke instruction!");
assert(arg_begin() <= U && U < arg_end()
&& "Argument # out of range!");
return U - arg_begin();
}
typedef IterTy arg_iterator;
IterTy arg_begin() const {
assert(getInstruction() && "Not a call or invoke instruction!");
return (*this)->op_begin();
}
IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
bool arg_empty() const { return arg_end() == arg_begin(); }
unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
Type *getType() const { return (*this)->getType(); }
FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
bool isMustTailCall() const {
return isCall() && cast<CallInst>(getInstruction())->isMustTailCall();
}
bool isTailCall() const {
return isCall() && cast<CallInst>(getInstruction())->isTailCall();
}
#define CALLSITE_DELEGATE_GETTER(METHOD) \
InstrTy *II = getInstruction(); \
return isCall() \
? cast<CallInst>(II)->METHOD \
: cast<InvokeInst>(II)->METHOD
#define CALLSITE_DELEGATE_SETTER(METHOD) \
InstrTy *II = getInstruction(); \
if (isCall()) \
cast<CallInst>(II)->METHOD; \
else \
cast<InvokeInst>(II)->METHOD
CallingConv::ID getCallingConv() const {
CALLSITE_DELEGATE_GETTER(getCallingConv());
}
void setCallingConv(CallingConv::ID CC) {
CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
}
const AttributeSet &getAttributes() const {
CALLSITE_DELEGATE_GETTER(getAttributes());
}
void setAttributes(const AttributeSet &PAL) {
CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
}
bool hasFnAttr(Attribute::AttrKind A) const {
CALLSITE_DELEGATE_GETTER(hasFnAttr(A));
}
bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A));
}
uint16_t getParamAlignment(uint16_t i) const {
CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
}
uint64_t getDereferenceableBytes(uint16_t i) const {
CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
}
bool isNoBuiltin() const {
CALLSITE_DELEGATE_GETTER(isNoBuiltin());
}
bool isNoInline() const {
CALLSITE_DELEGATE_GETTER(isNoInline());
}
void setIsNoInline(bool Value = true) {
CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
}
bool doesNotAccessMemory() const {
CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
}
void setDoesNotAccessMemory() {
CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
}
bool onlyReadsMemory() const {
CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
}
void setOnlyReadsMemory() {
CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
}
bool doesNotReturn() const {
CALLSITE_DELEGATE_GETTER(doesNotReturn());
}
void setDoesNotReturn() {
CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
}
bool doesNotThrow() const {
CALLSITE_DELEGATE_GETTER(doesNotThrow());
}
void setDoesNotThrow() {
CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
}
#undef CALLSITE_DELEGATE_GETTER
#undef CALLSITE_DELEGATE_SETTER
bool doesNotCapture(unsigned ArgNo) const {
return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
}
bool isByValArgument(unsigned ArgNo) const {
return paramHasAttr(ArgNo + 1, Attribute::ByVal);
}
bool isInAllocaArgument(unsigned ArgNo) const {
return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
}
bool isByValOrInAllocaArgument(unsigned ArgNo) const {
return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
paramHasAttr(ArgNo + 1, Attribute::InAlloca);
}
bool hasInAllocaArgument() const {
return paramHasAttr(arg_size(), Attribute::InAlloca);
}
bool doesNotAccessMemory(unsigned ArgNo) const {
return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
}
bool onlyReadsMemory(unsigned ArgNo) const {
return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
paramHasAttr(ArgNo + 1, Attribute::ReadNone);
}
bool isReturnNonNull() const {
if (paramHasAttr(0, Attribute::NonNull))
return true;
else if (getDereferenceableBytes(0) > 0 &&
getType()->getPointerAddressSpace() == 0)
return true;
return false;
}
bool hasArgument(const Value *Arg) const {
for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
++AI)
if (AI->get() == Arg)
return true;
return false;
}
private:
unsigned getArgumentEndOffset() const {
if (isCall())
return 1; else
return 3; }
IterTy getCallee() const {
if (isCall()) return cast<CallInst>(getInstruction())->op_end() - 1;
else return cast<InvokeInst>(getInstruction())->op_end() - 3;
}
};
class CallSite : public CallSiteBase<Function, Value, User, Instruction,
CallInst, InvokeInst, User::op_iterator> {
typedef CallSiteBase<Function, Value, User, Instruction,
CallInst, InvokeInst, User::op_iterator> Base;
public:
CallSite() {}
CallSite(Base B) : Base(B) {}
CallSite(Value* V) : Base(V) {}
CallSite(CallInst *CI) : Base(CI) {}
CallSite(InvokeInst *II) : Base(II) {}
CallSite(Instruction *II) : Base(II) {}
bool operator==(const CallSite &CS) const { return I == CS.I; }
bool operator!=(const CallSite &CS) const { return I != CS.I; }
bool operator<(const CallSite &CS) const {
return getInstruction() < CS.getInstruction();
}
private:
User::op_iterator getCallee() const;
};
class ImmutableCallSite : public CallSiteBase<> {
typedef CallSiteBase<> Base;
public:
ImmutableCallSite(const Value* V) : Base(V) {}
ImmutableCallSite(const CallInst *CI) : Base(CI) {}
ImmutableCallSite(const InvokeInst *II) : Base(II) {}
ImmutableCallSite(const Instruction *II) : Base(II) {}
ImmutableCallSite(CallSite CS) : Base(CS.getInstruction()) {}
};
}
#endif