#ifndef LLVM_SUPPORT_CALLSITE_H
#define LLVM_SUPPORT_CALLSITE_H
#include "llvm/Attributes.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/BasicBlock.h"
#include "llvm/Instruction.h"
namespace llvm {
class CallInst;
class InvokeInst;
class CallSite {
PointerIntPair<Instruction*, 1, bool> I;
public:
CallSite() : I(0, false) {}
CallSite(CallInst *CI) : I(reinterpret_cast<Instruction*>(CI), true) {}
CallSite(InvokeInst *II) : I(reinterpret_cast<Instruction*>(II), false) {}
CallSite(Instruction *C);
CallSite(const CallSite &CS) : I(CS.I) {}
CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; }
bool operator==(const CallSite &CS) const { return I == CS.I; }
bool operator!=(const CallSite &CS) const { return I != CS.I; }
static CallSite get(Value *V) {
if (Instruction *I = dyn_cast<Instruction>(V)) {
if (I->getOpcode() == Instruction::Call)
return CallSite(reinterpret_cast<CallInst*>(I));
else if (I->getOpcode() == Instruction::Invoke)
return CallSite(reinterpret_cast<InvokeInst*>(I));
}
return CallSite();
}
unsigned getCallingConv() const;
void setCallingConv(unsigned CC);
const AttrListPtr &getAttributes() const;
void setAttributes(const AttrListPtr &PAL);
bool paramHasAttr(uint16_t i, Attributes attr) const;
uint16_t getParamAlignment(uint16_t i) const;
bool doesNotAccessMemory() const;
void setDoesNotAccessMemory(bool doesNotAccessMemory = true);
bool onlyReadsMemory() const;
void setOnlyReadsMemory(bool onlyReadsMemory = true);
bool doesNotReturn() const;
void setDoesNotReturn(bool doesNotReturn = true);
bool doesNotThrow() const;
void setDoesNotThrow(bool doesNotThrow = true);
const Type *getType() const { return getInstruction()->getType(); }
bool isCall() const { return I.getInt(); }
bool isInvoke() const { return getInstruction() && !I.getInt(); }
Instruction *getInstruction() const { return I.getPointer(); }
Function *getCaller() const { return getInstruction()
->getParent()->getParent(); }
Value *getCalledValue() const {
assert(getInstruction() && "Not a call or invoke instruction!");
return getInstruction()->getOperand(0);
}
Function *getCalledFunction() const {
return dyn_cast<Function>(getCalledValue());
}
void setCalledFunction(Value *V) {
assert(getInstruction() && "Not a call or invoke instruction!");
getInstruction()->setOperand(0, V);
}
Value *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(getArgumentOffset() + ArgNo, newVal);
}
unsigned getArgumentNo(unsigned OperandNo) const {
assert(OperandNo >= getArgumentOffset() && "Operand number passed was not "
"a valid argument");
return OperandNo - getArgumentOffset();
}
bool hasArgument(const Value *Arg) const;
typedef User::op_iterator arg_iterator;
arg_iterator arg_begin() const {
assert(getInstruction() && "Not a call or invoke instruction!");
return getInstruction()->op_begin() + getArgumentOffset();
}
arg_iterator arg_end() const { return getInstruction()->op_end(); }
bool arg_empty() const { return arg_end() == arg_begin(); }
unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
bool operator<(const CallSite &CS) const {
return getInstruction() < CS.getInstruction();
}
bool isCallee(Value::use_iterator UI) const {
return getInstruction()->op_begin() == &UI.getUse();
}
private:
unsigned getArgumentOffset() const {
if (isCall())
return 1; else
return 3; }
};
}
#endif