1 //===- 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_IR_CALLSITE_H
27 #define LLVM_IR_CALLSITE_H
29 #include "llvm/ADT/PointerIntPair.h"
30 #include "llvm/IR/Attributes.h"
31 #include "llvm/IR/CallingConv.h"
32 #include "llvm/IR/Instructions.h"
39 template <typename FunTy = const Function,
40 typename ValTy = const Value,
41 typename UserTy = const User,
42 typename InstrTy = const Instruction,
43 typename CallTy = const CallInst,
44 typename InvokeTy = const InvokeInst,
45 typename IterTy = User::const_op_iterator>
48 PointerIntPair<InstrTy*, 1, bool> I;
50 CallSiteBase() : I(nullptr, false) {}
51 CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); }
52 CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); }
53 explicit 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 explicit 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.
107 bool isCallee(Value::const_user_iterator UI) const {
108 return isCallee(&UI.getUse());
111 /// Determine whether this Use is the callee operand's Use.
112 bool isCallee(const Use *U) const { return getCallee() == U; }
114 ValTy *getArgument(unsigned ArgNo) const {
115 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
116 return *(arg_begin() + ArgNo);
119 void setArgument(unsigned ArgNo, Value* newVal) {
120 assert(getInstruction() && "Not a call or invoke instruction!");
121 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
122 getInstruction()->setOperand(ArgNo, newVal);
125 /// Given a value use iterator, returns the argument that corresponds to it.
126 /// Iterator must actually correspond to an argument.
127 unsigned getArgumentNo(Value::const_user_iterator I) const {
128 return getArgumentNo(&I.getUse());
131 /// Given a use for an argument, get the argument number that corresponds to
133 unsigned getArgumentNo(const Use *U) const {
134 assert(getInstruction() && "Not a call or invoke instruction!");
135 assert(arg_begin() <= U && U < arg_end()
136 && "Argument # out of range!");
137 return U - arg_begin();
140 /// arg_iterator - The type of iterator to use when looping over actual
141 /// arguments at this call site.
142 typedef IterTy arg_iterator;
144 /// arg_begin/arg_end - Return iterators corresponding to the actual argument
145 /// list for a call site.
146 IterTy arg_begin() const {
147 assert(getInstruction() && "Not a call or invoke instruction!");
148 // Skip non-arguments
149 return (*this)->op_begin();
152 IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
153 bool arg_empty() const { return arg_end() == arg_begin(); }
154 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
156 /// getType - Return the type of the instruction that generated this call site
158 Type *getType() const { return (*this)->getType(); }
160 /// getCaller - Return the caller function for this call site
162 FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
164 /// \brief Tests if this call site must be tail call optimized. Only a
165 /// CallInst can be tail call optimized.
166 bool isMustTailCall() const {
167 return isCall() && cast<CallInst>(getInstruction())->isMustTailCall();
170 /// \brief Tests if this call site is marked as a tail call.
171 bool isTailCall() const {
172 return isCall() && cast<CallInst>(getInstruction())->isTailCall();
175 #define CALLSITE_DELEGATE_GETTER(METHOD) \
176 InstrTy *II = getInstruction(); \
178 ? cast<CallInst>(II)->METHOD \
179 : cast<InvokeInst>(II)->METHOD
181 #define CALLSITE_DELEGATE_SETTER(METHOD) \
182 InstrTy *II = getInstruction(); \
184 cast<CallInst>(II)->METHOD; \
186 cast<InvokeInst>(II)->METHOD
188 /// getCallingConv/setCallingConv - get or set the calling convention of the
190 CallingConv::ID getCallingConv() const {
191 CALLSITE_DELEGATE_GETTER(getCallingConv());
193 void setCallingConv(CallingConv::ID CC) {
194 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
197 FunctionType *getFunctionType() const {
198 CALLSITE_DELEGATE_GETTER(getFunctionType());
201 void mutateFunctionType(FunctionType *Ty) const {
202 CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty));
205 /// getAttributes/setAttributes - get or set the parameter attributes of
207 const AttributeSet &getAttributes() const {
208 CALLSITE_DELEGATE_GETTER(getAttributes());
210 void setAttributes(const AttributeSet &PAL) {
211 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
214 /// \brief Return true if this function has the given attribute.
215 bool hasFnAttr(Attribute::AttrKind A) const {
216 CALLSITE_DELEGATE_GETTER(hasFnAttr(A));
219 /// \brief Return true if the call or the callee has the given attribute.
220 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
221 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A));
224 /// @brief Extract the alignment for a call or parameter (0=unknown).
225 uint16_t getParamAlignment(uint16_t i) const {
226 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
229 /// @brief Extract the number of dereferenceable bytes for a call or
230 /// parameter (0=unknown).
231 uint64_t getDereferenceableBytes(uint16_t i) const {
232 CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
235 /// @brief Extract the number of dereferenceable_or_null bytes for a call or
236 /// parameter (0=unknown).
237 uint64_t getDereferenceableOrNullBytes(uint16_t i) const {
238 CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i));
241 /// \brief Return true if the call should not be treated as a call to a
243 bool isNoBuiltin() const {
244 CALLSITE_DELEGATE_GETTER(isNoBuiltin());
247 /// @brief Return true if the call should not be inlined.
248 bool isNoInline() const {
249 CALLSITE_DELEGATE_GETTER(isNoInline());
251 void setIsNoInline(bool Value = true) {
252 CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
255 /// @brief Determine if the call does not access memory.
256 bool doesNotAccessMemory() const {
257 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
259 void setDoesNotAccessMemory() {
260 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
263 /// @brief Determine if the call does not access or only reads memory.
264 bool onlyReadsMemory() const {
265 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
267 void setOnlyReadsMemory() {
268 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
271 /// @brief Determine if the call cannot return.
272 bool doesNotReturn() const {
273 CALLSITE_DELEGATE_GETTER(doesNotReturn());
275 void setDoesNotReturn() {
276 CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
279 /// @brief Determine if the call cannot unwind.
280 bool doesNotThrow() const {
281 CALLSITE_DELEGATE_GETTER(doesNotThrow());
283 void setDoesNotThrow() {
284 CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
287 #undef CALLSITE_DELEGATE_GETTER
288 #undef CALLSITE_DELEGATE_SETTER
290 /// @brief Determine whether this argument is not captured.
291 bool doesNotCapture(unsigned ArgNo) const {
292 return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
295 /// @brief Determine whether this argument is passed by value.
296 bool isByValArgument(unsigned ArgNo) const {
297 return paramHasAttr(ArgNo + 1, Attribute::ByVal);
300 /// @brief Determine whether this argument is passed in an alloca.
301 bool isInAllocaArgument(unsigned ArgNo) const {
302 return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
305 /// @brief Determine whether this argument is passed by value or in an alloca.
306 bool isByValOrInAllocaArgument(unsigned ArgNo) const {
307 return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
308 paramHasAttr(ArgNo + 1, Attribute::InAlloca);
311 /// @brief Determine if there are is an inalloca argument. Only the last
312 /// argument can have the inalloca attribute.
313 bool hasInAllocaArgument() const {
314 return paramHasAttr(arg_size(), Attribute::InAlloca);
317 bool doesNotAccessMemory(unsigned ArgNo) const {
318 return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
321 bool onlyReadsMemory(unsigned ArgNo) const {
322 return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
323 paramHasAttr(ArgNo + 1, Attribute::ReadNone);
326 /// @brief Return true if the return value is known to be not null.
327 /// This may be because it has the nonnull attribute, or because at least
328 /// one byte is dereferenceable and the pointer is in addrspace(0).
329 bool isReturnNonNull() const {
330 if (paramHasAttr(0, Attribute::NonNull))
332 else if (getDereferenceableBytes(0) > 0 &&
333 getType()->getPointerAddressSpace() == 0)
339 /// hasArgument - Returns true if this CallSite passes the given Value* as an
340 /// argument to the called function.
341 bool hasArgument(const Value *Arg) const {
342 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
344 if (AI->get() == Arg)
350 unsigned getArgumentEndOffset() const {
352 return 1; // Skip Callee
354 return 3; // Skip BB, BB, Callee
357 IterTy getCallee() const {
358 if (isCall()) // Skip Callee
359 return cast<CallInst>(getInstruction())->op_end() - 1;
360 else // Skip BB, BB, Callee
361 return cast<InvokeInst>(getInstruction())->op_end() - 3;
365 class CallSite : public CallSiteBase<Function, Value, User, Instruction,
366 CallInst, InvokeInst, User::op_iterator> {
369 CallSite(CallSiteBase B) : CallSiteBase(B) {}
370 CallSite(CallInst *CI) : CallSiteBase(CI) {}
371 CallSite(InvokeInst *II) : CallSiteBase(II) {}
372 explicit CallSite(Instruction *II) : CallSiteBase(II) {}
373 explicit CallSite(Value *V) : CallSiteBase(V) {}
375 bool operator==(const CallSite &CS) const { return I == CS.I; }
376 bool operator!=(const CallSite &CS) const { return I != CS.I; }
377 bool operator<(const CallSite &CS) const {
378 return getInstruction() < CS.getInstruction();
382 User::op_iterator getCallee() const;
385 /// ImmutableCallSite - establish a view to a call site for examination
386 class ImmutableCallSite : public CallSiteBase<> {
388 ImmutableCallSite() {}
389 ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
390 ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
391 explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
392 explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
393 ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
396 } // End llvm namespace