1 //===-- llvm/Support/CallSite.h - Abstract Call & Invoke instrs -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the CallSite class, which is a handy wrapper for code that
11 // wants to treat Call and Invoke instructions in a generic way.
13 // NOTE: This class is supposed to have "value semantics". So it should be
14 // passed by value, not by reference; it should not be "new"ed or "delete"d. It
15 // is efficiently copyable, assignable and constructable, with cost equivalent
16 // to copying a pointer (notice that it has only a single data member).
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_SUPPORT_CALLSITE_H
21 #define LLVM_SUPPORT_CALLSITE_H
23 #include "llvm/Instruction.h"
24 #include "llvm/BasicBlock.h"
36 CallSite(CallInst *CI) : I(reinterpret_cast<Instruction*>(CI)) {}
37 CallSite(InvokeInst *II) : I(reinterpret_cast<Instruction*>(II)) {}
38 CallSite(Instruction *C);
39 CallSite(const CallSite &CS) : I(CS.I) {}
40 CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; }
42 /// CallSite::get - This static method is sort of like a constructor. It will
43 /// create an appropriate call site for a Call or Invoke instruction, but it
44 /// can also create a null initialized CallSite object for something which is
47 static CallSite get(Value *V) {
48 if (Instruction *I = dyn_cast<Instruction>(V)) {
49 if (I->getOpcode() == Instruction::Call)
50 return CallSite(reinterpret_cast<CallInst*>(I));
51 else if (I->getOpcode() == Instruction::Invoke)
52 return CallSite(reinterpret_cast<InvokeInst*>(I));
57 /// getCallingConv/setCallingConv - get or set the calling convention of the
59 unsigned getCallingConv() const;
60 void setCallingConv(unsigned CC);
62 /// getParamAttrs/setParamAttrs - get or set the parameter attributes of
64 const ParamAttrsList *getParamAttrs() const;
65 void setParamAttrs(const ParamAttrsList *PAL);
67 /// paramHasAttr - whether the call or the callee has the given attribute.
68 bool paramHasAttr(uint16_t i, unsigned attr) const;
70 /// @brief Determine if the call does not access memory.
71 bool doesNotAccessMemory() const;
73 /// @brief Determine if the call does not access or only reads memory.
74 bool onlyReadsMemory() const;
76 /// @brief Determine if the call cannot unwind.
77 bool doesNotThrow() const;
78 void setDoesNotThrow(bool doesNotThrow = true);
80 /// getType - Return the type of the instruction that generated this call site
82 const Type *getType() const { return I->getType(); }
84 /// getInstruction - Return the instruction this call site corresponds to
86 Instruction *getInstruction() const { return I; }
88 /// getCaller - Return the caller function for this call site
90 Function *getCaller() const { return I->getParent()->getParent(); }
92 /// getCalledValue - Return the pointer to function that is being called...
94 Value *getCalledValue() const {
95 assert(I && "Not a call or invoke instruction!");
96 return I->getOperand(0);
99 /// getCalledFunction - Return the function being called if this is a direct
100 /// call, otherwise return null (if it's an indirect call).
102 Function *getCalledFunction() const {
103 return dyn_cast<Function>(getCalledValue());
106 /// setCalledFunction - Set the callee to the specified value...
108 void setCalledFunction(Value *V) {
109 assert(I && "Not a call or invoke instruction!");
113 Value *getArgument(unsigned ArgNo) const {
114 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
115 return *(arg_begin()+ArgNo);
118 void setArgument(unsigned ArgNo, Value* newVal) {
119 assert(I && "Not a call or invoke instruction!");
120 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
121 if (I->getOpcode() == Instruction::Call)
122 I->setOperand(ArgNo+1, newVal); // Skip Function
124 I->setOperand(ArgNo+3, newVal); // Skip Function, BB, BB
127 /// arg_iterator - The type of iterator to use when looping over actual
128 /// arguments at this call site...
129 typedef User::op_iterator arg_iterator;
131 /// arg_begin/arg_end - Return iterators corresponding to the actual argument
132 /// list for a call site.
134 arg_iterator arg_begin() const {
135 assert(I && "Not a call or invoke instruction!");
136 if (I->getOpcode() == Instruction::Call)
137 return I->op_begin()+1; // Skip Function
139 return I->op_begin()+3; // Skip Function, BB, BB
141 arg_iterator arg_end() const { return I->op_end(); }
142 bool arg_empty() const { return arg_end() == arg_begin(); }
143 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
145 bool operator<(const CallSite &CS) const {
146 return getInstruction() < CS.getInstruction();
150 } // End llvm namespace