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/ADT/iterator_range.h"
31 #include "llvm/IR/Attributes.h"
32 #include "llvm/IR/CallingConv.h"
33 #include "llvm/IR/Instructions.h"
40 template <typename FunTy = const Function,
41 typename BBTy = const BasicBlock,
42 typename ValTy = const Value,
43 typename UserTy = const User,
44 typename InstrTy = const Instruction,
45 typename CallTy = const CallInst,
46 typename InvokeTy = const InvokeInst,
47 typename IterTy = User::const_op_iterator>
50 PointerIntPair<InstrTy*, 1, bool> I;
52 CallSiteBase() : I(nullptr, false) {}
53 CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); }
54 CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); }
55 explicit CallSiteBase(ValTy *II) { *this = get(II); }
58 /// CallSiteBase::get - This static method is sort of like a constructor. It
59 /// will create an appropriate call site for a Call or Invoke instruction, but
60 /// it can also create a null initialized CallSiteBase object for something
61 /// which is NOT a call site.
63 static CallSiteBase get(ValTy *V) {
64 if (InstrTy *II = dyn_cast<InstrTy>(V)) {
65 if (II->getOpcode() == Instruction::Call)
66 return CallSiteBase(static_cast<CallTy*>(II));
67 else if (II->getOpcode() == Instruction::Invoke)
68 return CallSiteBase(static_cast<InvokeTy*>(II));
70 return CallSiteBase();
73 /// isCall - true if a CallInst is enclosed.
74 /// Note that !isCall() does not mean it is an InvokeInst enclosed,
75 /// it also could signify a NULL Instruction pointer.
76 bool isCall() const { return I.getInt(); }
78 /// isInvoke - true if a InvokeInst is enclosed.
80 bool isInvoke() const { return getInstruction() && !I.getInt(); }
82 InstrTy *getInstruction() const { return I.getPointer(); }
83 InstrTy *operator->() const { return I.getPointer(); }
84 explicit operator bool() const { return I.getPointer(); }
86 /// Get the basic block containing the call site
87 BBTy* getParent() const { return getInstruction()->getParent(); }
89 /// getCalledValue - Return the pointer to function that is being called.
91 ValTy *getCalledValue() const {
92 assert(getInstruction() && "Not a call or invoke instruction!");
96 /// getCalledFunction - Return the function being called if this is a direct
97 /// call, otherwise return null (if it's an indirect call).
99 FunTy *getCalledFunction() const {
100 return dyn_cast<FunTy>(getCalledValue());
103 /// setCalledFunction - Set the callee to the specified value.
105 void setCalledFunction(Value *V) {
106 assert(getInstruction() && "Not a call or invoke instruction!");
110 /// isCallee - Determine whether the passed iterator points to the
111 /// callee operand's Use.
112 bool isCallee(Value::const_user_iterator UI) const {
113 return isCallee(&UI.getUse());
116 /// Determine whether this Use is the callee operand's Use.
117 bool isCallee(const Use *U) const { return getCallee() == U; }
119 ValTy *getArgument(unsigned ArgNo) const {
120 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
121 return *(arg_begin() + ArgNo);
124 void setArgument(unsigned ArgNo, Value* newVal) {
125 assert(getInstruction() && "Not a call or invoke instruction!");
126 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
127 getInstruction()->setOperand(ArgNo, newVal);
130 /// Given a value use iterator, returns the argument that corresponds to it.
131 /// Iterator must actually correspond to an argument.
132 unsigned getArgumentNo(Value::const_user_iterator I) const {
133 return getArgumentNo(&I.getUse());
136 /// Given a use for an argument, get the argument number that corresponds to
138 unsigned getArgumentNo(const Use *U) const {
139 assert(getInstruction() && "Not a call or invoke instruction!");
140 assert(arg_begin() <= U && U < arg_end()
141 && "Argument # out of range!");
142 return U - arg_begin();
145 /// arg_iterator - The type of iterator to use when looping over actual
146 /// arguments at this call site.
147 typedef IterTy arg_iterator;
149 /// arg_begin/arg_end - Return iterators corresponding to the actual argument
150 /// list for a call site.
151 IterTy arg_begin() const {
152 assert(getInstruction() && "Not a call or invoke instruction!");
153 // Skip non-arguments
154 return (*this)->op_begin();
157 IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
158 iterator_range<IterTy> args() const {
159 return iterator_range<IterTy>(arg_begin(), arg_end());
161 bool arg_empty() const { return arg_end() == arg_begin(); }
162 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
164 /// getType - Return the type of the instruction that generated this call site
166 Type *getType() const { return (*this)->getType(); }
168 /// getCaller - Return the caller function for this call site
170 FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
172 /// \brief Tests if this call site must be tail call optimized. Only a
173 /// CallInst can be tail call optimized.
174 bool isMustTailCall() const {
175 return isCall() && cast<CallInst>(getInstruction())->isMustTailCall();
178 /// \brief Tests if this call site is marked as a tail call.
179 bool isTailCall() const {
180 return isCall() && cast<CallInst>(getInstruction())->isTailCall();
183 #define CALLSITE_DELEGATE_GETTER(METHOD) \
184 InstrTy *II = getInstruction(); \
186 ? cast<CallInst>(II)->METHOD \
187 : cast<InvokeInst>(II)->METHOD
189 #define CALLSITE_DELEGATE_SETTER(METHOD) \
190 InstrTy *II = getInstruction(); \
192 cast<CallInst>(II)->METHOD; \
194 cast<InvokeInst>(II)->METHOD
196 unsigned getNumArgOperands() const {
197 CALLSITE_DELEGATE_GETTER(getNumArgOperands());
200 ValTy *getArgOperand(unsigned i) const {
201 CALLSITE_DELEGATE_GETTER(getArgOperand(i));
204 bool isInlineAsm() const {
206 return cast<CallInst>(getInstruction())->isInlineAsm();
210 /// getCallingConv/setCallingConv - get or set the calling convention of the
212 CallingConv::ID getCallingConv() const {
213 CALLSITE_DELEGATE_GETTER(getCallingConv());
215 void setCallingConv(CallingConv::ID CC) {
216 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
219 FunctionType *getFunctionType() const {
220 CALLSITE_DELEGATE_GETTER(getFunctionType());
223 void mutateFunctionType(FunctionType *Ty) const {
224 CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty));
227 /// getAttributes/setAttributes - get or set the parameter attributes of
229 const AttributeSet &getAttributes() const {
230 CALLSITE_DELEGATE_GETTER(getAttributes());
232 void setAttributes(const AttributeSet &PAL) {
233 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
236 /// \brief Return true if this function has the given attribute.
237 bool hasFnAttr(Attribute::AttrKind A) const {
238 CALLSITE_DELEGATE_GETTER(hasFnAttr(A));
241 /// \brief Return true if the call or the callee has the given attribute.
242 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
243 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A));
246 /// @brief Extract the alignment for a call or parameter (0=unknown).
247 uint16_t getParamAlignment(uint16_t i) const {
248 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
251 /// @brief Extract the number of dereferenceable bytes for a call or
252 /// parameter (0=unknown).
253 uint64_t getDereferenceableBytes(uint16_t i) const {
254 CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
257 /// @brief Extract the number of dereferenceable_or_null bytes for a call or
258 /// parameter (0=unknown).
259 uint64_t getDereferenceableOrNullBytes(uint16_t i) const {
260 CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i));
263 /// \brief Return true if the call should not be treated as a call to a
265 bool isNoBuiltin() const {
266 CALLSITE_DELEGATE_GETTER(isNoBuiltin());
269 /// @brief Return true if the call should not be inlined.
270 bool isNoInline() const {
271 CALLSITE_DELEGATE_GETTER(isNoInline());
273 void setIsNoInline(bool Value = true) {
274 CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
277 /// @brief Determine if the call does not access memory.
278 bool doesNotAccessMemory() const {
279 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
281 void setDoesNotAccessMemory() {
282 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
285 /// @brief Determine if the call does not access or only reads memory.
286 bool onlyReadsMemory() const {
287 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
289 void setOnlyReadsMemory() {
290 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
293 /// @brief Determine if the call can access memmory only using pointers based
294 /// on its arguments.
295 bool onlyAccessesArgMemory() const {
296 CALLSITE_DELEGATE_GETTER(onlyAccessesArgMemory());
298 void setOnlyAccessesArgMemory() {
299 CALLSITE_DELEGATE_SETTER(setOnlyAccessesArgMemory());
302 /// @brief Determine if the call cannot return.
303 bool doesNotReturn() const {
304 CALLSITE_DELEGATE_GETTER(doesNotReturn());
306 void setDoesNotReturn() {
307 CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
310 /// @brief Determine if the call cannot unwind.
311 bool doesNotThrow() const {
312 CALLSITE_DELEGATE_GETTER(doesNotThrow());
314 void setDoesNotThrow() {
315 CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
318 #undef CALLSITE_DELEGATE_GETTER
319 #undef CALLSITE_DELEGATE_SETTER
321 /// @brief Determine whether this argument is not captured.
322 bool doesNotCapture(unsigned ArgNo) const {
323 return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
326 /// @brief Determine whether this argument is passed by value.
327 bool isByValArgument(unsigned ArgNo) const {
328 return paramHasAttr(ArgNo + 1, Attribute::ByVal);
331 /// @brief Determine whether this argument is passed in an alloca.
332 bool isInAllocaArgument(unsigned ArgNo) const {
333 return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
336 /// @brief Determine whether this argument is passed by value or in an alloca.
337 bool isByValOrInAllocaArgument(unsigned ArgNo) const {
338 return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
339 paramHasAttr(ArgNo + 1, Attribute::InAlloca);
342 /// @brief Determine if there are is an inalloca argument. Only the last
343 /// argument can have the inalloca attribute.
344 bool hasInAllocaArgument() const {
345 return paramHasAttr(arg_size(), Attribute::InAlloca);
348 bool doesNotAccessMemory(unsigned ArgNo) const {
349 return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
352 bool onlyReadsMemory(unsigned ArgNo) const {
353 return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
354 paramHasAttr(ArgNo + 1, Attribute::ReadNone);
357 /// @brief Return true if the return value is known to be not null.
358 /// This may be because it has the nonnull attribute, or because at least
359 /// one byte is dereferenceable and the pointer is in addrspace(0).
360 bool isReturnNonNull() const {
361 if (paramHasAttr(0, Attribute::NonNull))
363 else if (getDereferenceableBytes(0) > 0 &&
364 getType()->getPointerAddressSpace() == 0)
370 /// hasArgument - Returns true if this CallSite passes the given Value* as an
371 /// argument to the called function.
372 bool hasArgument(const Value *Arg) const {
373 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
375 if (AI->get() == Arg)
381 unsigned getArgumentEndOffset() const {
383 return 1; // Skip Callee
385 return 3; // Skip BB, BB, Callee
388 IterTy getCallee() const {
389 if (isCall()) // Skip Callee
390 return cast<CallInst>(getInstruction())->op_end() - 1;
391 else // Skip BB, BB, Callee
392 return cast<InvokeInst>(getInstruction())->op_end() - 3;
396 class CallSite : public CallSiteBase<Function, BasicBlock, Value, User,
397 Instruction, CallInst, InvokeInst,
401 CallSite(CallSiteBase B) : CallSiteBase(B) {}
402 CallSite(CallInst *CI) : CallSiteBase(CI) {}
403 CallSite(InvokeInst *II) : CallSiteBase(II) {}
404 explicit CallSite(Instruction *II) : CallSiteBase(II) {}
405 explicit CallSite(Value *V) : CallSiteBase(V) {}
407 bool operator==(const CallSite &CS) const { return I == CS.I; }
408 bool operator!=(const CallSite &CS) const { return I != CS.I; }
409 bool operator<(const CallSite &CS) const {
410 return getInstruction() < CS.getInstruction();
414 User::op_iterator getCallee() const;
417 /// ImmutableCallSite - establish a view to a call site for examination
418 class ImmutableCallSite : public CallSiteBase<> {
420 ImmutableCallSite() {}
421 ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
422 ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
423 explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
424 explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
425 ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
428 } // End llvm namespace