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. When in non-
12 // mutation context (e.g. an analysis) ImmutableCallSite should be used.
13 // Finally, when some degree of customization is necessary between these two
14 // extremes, CallSiteBase<> can be supplied with fine-tuned parameters.
16 // NOTE: These classes are supposed to have "value semantics". So they should be
17 // passed by value, not by reference; they should not be "new"ed or "delete"d.
18 // They are efficiently copyable, assignable and constructable, with cost
19 // equivalent to copying a pointer (notice that they have only a single data
20 // member). The internal representation carries a flag which indicates which of
21 // the two variants is enclosed. This allows for cheaper checks when various
22 // accessors of CallSite are employed.
24 //===----------------------------------------------------------------------===//
26 #ifndef LLVM_SUPPORT_CALLSITE_H
27 #define LLVM_SUPPORT_CALLSITE_H
29 #include "llvm/Attributes.h"
30 #include "llvm/ADT/PointerIntPair.h"
31 #include "llvm/BasicBlock.h"
32 #include "llvm/CallingConv.h"
33 #include "llvm/Instructions.h"
40 template <typename FunTy = const Function,
41 typename ValTy = const Value,
42 typename UserTy = const User,
43 typename InstrTy = const Instruction,
44 typename CallTy = const CallInst,
45 typename InvokeTy = const InvokeInst,
46 typename IterTy = User::const_op_iterator>
49 PointerIntPair<InstrTy*, 1, bool> I;
51 CallSiteBase() : I(0, false) {}
52 CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); }
53 CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); }
54 CallSiteBase(ValTy *II) { *this = get(II); }
56 /// CallSiteBase::get - This static method is sort of like a constructor. It
57 /// will create an appropriate call site for a Call or Invoke instruction, but
58 /// it can also create a null initialized CallSiteBase object for something
59 /// which is NOT a call site.
61 static CallSiteBase get(ValTy *V) {
62 if (InstrTy *II = dyn_cast<InstrTy>(V)) {
63 if (II->getOpcode() == Instruction::Call)
64 return CallSiteBase(static_cast<CallTy*>(II));
65 else if (II->getOpcode() == Instruction::Invoke)
66 return CallSiteBase(static_cast<InvokeTy*>(II));
68 return CallSiteBase();
71 /// isCall - true if a CallInst is enclosed.
72 /// Note that !isCall() does not mean it is an InvokeInst enclosed,
73 /// it also could signify a NULL Instruction pointer.
74 bool isCall() const { return I.getInt(); }
76 /// isInvoke - true if a InvokeInst is enclosed.
78 bool isInvoke() const { return getInstruction() && !I.getInt(); }
80 InstrTy *getInstruction() const { return I.getPointer(); }
81 InstrTy *operator->() const { return I.getPointer(); }
82 operator bool() const { return I.getPointer(); }
84 /// getCalledValue - Return the pointer to function that is being called.
86 ValTy *getCalledValue() const {
87 assert(getInstruction() && "Not a call or invoke instruction!");
91 /// getCalledFunction - Return the function being called if this is a direct
92 /// call, otherwise return null (if it's an indirect call).
94 FunTy *getCalledFunction() const {
95 return dyn_cast<FunTy>(getCalledValue());
98 /// setCalledFunction - Set the callee to the specified value.
100 void setCalledFunction(Value *V) {
101 assert(getInstruction() && "Not a call or invoke instruction!");
105 /// isCallee - Determine whether the passed iterator points to the
106 /// callee operand's Use.
108 bool isCallee(value_use_iterator<UserTy> UI) const {
109 return getCallee() == &UI.getUse();
112 ValTy *getArgument(unsigned ArgNo) const {
113 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
114 return *(arg_begin() + ArgNo);
117 void setArgument(unsigned ArgNo, Value* newVal) {
118 assert(getInstruction() && "Not a call or invoke instruction!");
119 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
120 getInstruction()->setOperand(ArgNo, newVal);
123 /// Given a value use iterator, returns the argument that corresponds to it.
124 /// Iterator must actually correspond to an argument.
125 unsigned getArgumentNo(value_use_iterator<UserTy> I) const {
126 assert(getInstruction() && "Not a call or invoke instruction!");
127 assert(arg_begin() <= &I.getUse() && &I.getUse() < arg_end()
128 && "Argument # out of range!");
129 return &I.getUse() - arg_begin();
132 /// arg_iterator - The type of iterator to use when looping over actual
133 /// arguments at this call site.
134 typedef IterTy arg_iterator;
136 /// arg_begin/arg_end - Return iterators corresponding to the actual argument
137 /// list for a call site.
138 IterTy arg_begin() const {
139 assert(getInstruction() && "Not a call or invoke instruction!");
140 // Skip non-arguments
141 return (*this)->op_begin();
144 IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
145 bool arg_empty() const { return arg_end() == arg_begin(); }
146 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
148 /// getType - Return the type of the instruction that generated this call site
150 Type *getType() const { return (*this)->getType(); }
152 /// getCaller - Return the caller function for this call site
154 FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
156 #define CALLSITE_DELEGATE_GETTER(METHOD) \
157 InstrTy *II = getInstruction(); \
159 ? cast<CallInst>(II)->METHOD \
160 : cast<InvokeInst>(II)->METHOD
162 #define CALLSITE_DELEGATE_SETTER(METHOD) \
163 InstrTy *II = getInstruction(); \
165 cast<CallInst>(II)->METHOD; \
167 cast<InvokeInst>(II)->METHOD
169 /// getCallingConv/setCallingConv - get or set the calling convention of the
171 CallingConv::ID getCallingConv() const {
172 CALLSITE_DELEGATE_GETTER(getCallingConv());
174 void setCallingConv(CallingConv::ID CC) {
175 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
178 /// getAttributes/setAttributes - get or set the parameter attributes of
180 const AttrListPtr &getAttributes() const {
181 CALLSITE_DELEGATE_GETTER(getAttributes());
183 void setAttributes(const AttrListPtr &PAL) {
184 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
187 /// \brief Return true if this function has the given attribute.
188 bool hasFnAttr(Attributes N) const {
189 CALLSITE_DELEGATE_GETTER(hasFnAttr(N));
192 /// paramHas*Attr - whether the call or the callee has the given attribute.
193 bool paramHasSExtAttr(unsigned i) const {
194 CALLSITE_DELEGATE_GETTER(paramHasSExtAttr(i));
196 bool paramHasZExtAttr(unsigned i) const {
197 CALLSITE_DELEGATE_GETTER(paramHasZExtAttr(i));
199 bool paramHasInRegAttr(unsigned i) const {
200 CALLSITE_DELEGATE_GETTER(paramHasInRegAttr(i));
202 bool paramHasStructRetAttr(unsigned i) const {
203 CALLSITE_DELEGATE_GETTER(paramHasStructRetAttr(i));
205 bool paramHasNestAttr(unsigned i) const {
206 CALLSITE_DELEGATE_GETTER(paramHasNestAttr(i));
208 bool paramHasByValAttr(unsigned i) const {
209 CALLSITE_DELEGATE_GETTER(paramHasByValAttr(i));
211 bool paramHasNoAliasAttr(unsigned i) const {
212 CALLSITE_DELEGATE_GETTER(paramHasNoAliasAttr(i));
215 /// paramHasAttr - whether the call or the callee has the given attribute.
216 bool paramHasAttr(uint16_t i, Attributes attr) const {
217 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, attr));
220 /// @brief Extract the alignment for a call or parameter (0=unknown).
221 uint16_t getParamAlignment(uint16_t i) const {
222 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
225 /// @brief Return true if the call should not be inlined.
226 bool isNoInline() const {
227 CALLSITE_DELEGATE_GETTER(isNoInline());
229 void setIsNoInline(bool Value = true) {
230 CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
233 /// @brief Determine if the call does not access memory.
234 bool doesNotAccessMemory() const {
235 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
237 void setDoesNotAccessMemory(bool doesNotAccessMemory = true) {
238 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory(doesNotAccessMemory));
241 /// @brief Determine if the call does not access or only reads memory.
242 bool onlyReadsMemory() const {
243 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
245 void setOnlyReadsMemory(bool onlyReadsMemory = true) {
246 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory(onlyReadsMemory));
249 /// @brief Determine if the call cannot return.
250 bool doesNotReturn() const {
251 CALLSITE_DELEGATE_GETTER(doesNotReturn());
253 void setDoesNotReturn(bool doesNotReturn = true) {
254 CALLSITE_DELEGATE_SETTER(setDoesNotReturn(doesNotReturn));
257 /// @brief Determine if the call cannot unwind.
258 bool doesNotThrow() const {
259 CALLSITE_DELEGATE_GETTER(doesNotThrow());
261 void setDoesNotThrow(bool doesNotThrow = true) {
262 CALLSITE_DELEGATE_SETTER(setDoesNotThrow(doesNotThrow));
265 #undef CALLSITE_DELEGATE_GETTER
266 #undef CALLSITE_DELEGATE_SETTER
268 /// @brief Determine whether this argument is not captured.
269 bool doesNotCapture(unsigned ArgNo) const {
270 return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
273 /// @brief Determine whether this argument is passed by value.
274 bool isByValArgument(unsigned ArgNo) const {
275 return paramHasAttr(ArgNo + 1, Attribute::ByVal);
278 /// hasArgument - Returns true if this CallSite passes the given Value* as an
279 /// argument to the called function.
280 bool hasArgument(const Value *Arg) const {
281 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
283 if (AI->get() == Arg)
289 unsigned getArgumentEndOffset() const {
291 return 1; // Skip Callee
293 return 3; // Skip BB, BB, Callee
296 IterTy getCallee() const {
297 if (isCall()) // Skip Callee
298 return cast<CallInst>(getInstruction())->op_end() - 1;
299 else // Skip BB, BB, Callee
300 return cast<InvokeInst>(getInstruction())->op_end() - 3;
304 class CallSite : public CallSiteBase<Function, Value, User, Instruction,
305 CallInst, InvokeInst, User::op_iterator> {
306 typedef CallSiteBase<Function, Value, User, Instruction,
307 CallInst, InvokeInst, User::op_iterator> Base;
310 CallSite(Base B) : Base(B) {}
311 CallSite(Value* V) : Base(V) {}
312 CallSite(CallInst *CI) : Base(CI) {}
313 CallSite(InvokeInst *II) : Base(II) {}
314 CallSite(Instruction *II) : Base(II) {}
316 bool operator==(const CallSite &CS) const { return I == CS.I; }
317 bool operator!=(const CallSite &CS) const { return I != CS.I; }
318 bool operator<(const CallSite &CS) const {
319 return getInstruction() < CS.getInstruction();
323 User::op_iterator getCallee() const;
326 /// ImmutableCallSite - establish a view to a call site for examination
327 class ImmutableCallSite : public CallSiteBase<> {
328 typedef CallSiteBase<> Base;
330 ImmutableCallSite(const Value* V) : Base(V) {}
331 ImmutableCallSite(const CallInst *CI) : Base(CI) {}
332 ImmutableCallSite(const InvokeInst *II) : Base(II) {}
333 ImmutableCallSite(const Instruction *II) : Base(II) {}
334 ImmutableCallSite(CallSite CS) : Base(CS.getInstruction()) {}
337 } // End llvm namespace