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 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(nullptr, false) {}
52 CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); }
53 CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); }
54 explicit CallSiteBase(ValTy *II) { *this = get(II); }
57 /// CallSiteBase::get - This static method is sort of like a constructor. It
58 /// will create an appropriate call site for a Call or Invoke instruction, but
59 /// it can also create a null initialized CallSiteBase object for something
60 /// which is NOT a call site.
62 static CallSiteBase get(ValTy *V) {
63 if (InstrTy *II = dyn_cast<InstrTy>(V)) {
64 if (II->getOpcode() == Instruction::Call)
65 return CallSiteBase(static_cast<CallTy*>(II));
66 else if (II->getOpcode() == Instruction::Invoke)
67 return CallSiteBase(static_cast<InvokeTy*>(II));
69 return CallSiteBase();
72 /// isCall - true if a CallInst is enclosed.
73 /// Note that !isCall() does not mean it is an InvokeInst enclosed,
74 /// it also could signify a NULL Instruction pointer.
75 bool isCall() const { return I.getInt(); }
77 /// isInvoke - true if a InvokeInst is enclosed.
79 bool isInvoke() const { return getInstruction() && !I.getInt(); }
81 InstrTy *getInstruction() const { return I.getPointer(); }
82 InstrTy *operator->() const { return I.getPointer(); }
83 explicit operator bool() const { return I.getPointer(); }
85 /// getCalledValue - Return the pointer to function that is being called.
87 ValTy *getCalledValue() const {
88 assert(getInstruction() && "Not a call or invoke instruction!");
92 /// getCalledFunction - Return the function being called if this is a direct
93 /// call, otherwise return null (if it's an indirect call).
95 FunTy *getCalledFunction() const {
96 return dyn_cast<FunTy>(getCalledValue());
99 /// setCalledFunction - Set the callee to the specified value.
101 void setCalledFunction(Value *V) {
102 assert(getInstruction() && "Not a call or invoke instruction!");
106 /// isCallee - Determine whether the passed iterator points to the
107 /// callee operand's Use.
108 bool isCallee(Value::const_user_iterator UI) const {
109 return isCallee(&UI.getUse());
112 /// Determine whether this Use is the callee operand's Use.
113 bool isCallee(const Use *U) const { return getCallee() == U; }
115 ValTy *getArgument(unsigned ArgNo) const {
116 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
117 return *(arg_begin() + ArgNo);
120 void setArgument(unsigned ArgNo, Value* newVal) {
121 assert(getInstruction() && "Not a call or invoke instruction!");
122 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
123 getInstruction()->setOperand(ArgNo, newVal);
126 /// Given a value use iterator, returns the argument that corresponds to it.
127 /// Iterator must actually correspond to an argument.
128 unsigned getArgumentNo(Value::const_user_iterator I) const {
129 return getArgumentNo(&I.getUse());
132 /// Given a use for an argument, get the argument number that corresponds to
134 unsigned getArgumentNo(const Use *U) const {
135 assert(getInstruction() && "Not a call or invoke instruction!");
136 assert(arg_begin() <= U && U < arg_end()
137 && "Argument # out of range!");
138 return U - arg_begin();
141 /// arg_iterator - The type of iterator to use when looping over actual
142 /// arguments at this call site.
143 typedef IterTy arg_iterator;
145 /// arg_begin/arg_end - Return iterators corresponding to the actual argument
146 /// list for a call site.
147 IterTy arg_begin() const {
148 assert(getInstruction() && "Not a call or invoke instruction!");
149 // Skip non-arguments
150 return (*this)->op_begin();
153 IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
154 iterator_range<IterTy> args() const {
155 return iterator_range<IterTy>(arg_begin(), arg_end());
157 bool arg_empty() const { return arg_end() == arg_begin(); }
158 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
160 /// getType - Return the type of the instruction that generated this call site
162 Type *getType() const { return (*this)->getType(); }
164 /// getCaller - Return the caller function for this call site
166 FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
168 /// \brief Tests if this call site must be tail call optimized. Only a
169 /// CallInst can be tail call optimized.
170 bool isMustTailCall() const {
171 return isCall() && cast<CallInst>(getInstruction())->isMustTailCall();
174 /// \brief Tests if this call site is marked as a tail call.
175 bool isTailCall() const {
176 return isCall() && cast<CallInst>(getInstruction())->isTailCall();
179 #define CALLSITE_DELEGATE_GETTER(METHOD) \
180 InstrTy *II = getInstruction(); \
182 ? cast<CallInst>(II)->METHOD \
183 : cast<InvokeInst>(II)->METHOD
185 #define CALLSITE_DELEGATE_SETTER(METHOD) \
186 InstrTy *II = getInstruction(); \
188 cast<CallInst>(II)->METHOD; \
190 cast<InvokeInst>(II)->METHOD
192 /// getCallingConv/setCallingConv - get or set the calling convention of the
194 CallingConv::ID getCallingConv() const {
195 CALLSITE_DELEGATE_GETTER(getCallingConv());
197 void setCallingConv(CallingConv::ID CC) {
198 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
201 FunctionType *getFunctionType() const {
202 CALLSITE_DELEGATE_GETTER(getFunctionType());
205 void mutateFunctionType(FunctionType *Ty) const {
206 CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty));
209 /// getAttributes/setAttributes - get or set the parameter attributes of
211 const AttributeSet &getAttributes() const {
212 CALLSITE_DELEGATE_GETTER(getAttributes());
214 void setAttributes(const AttributeSet &PAL) {
215 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
218 /// \brief Return true if this function has the given attribute.
219 bool hasFnAttr(Attribute::AttrKind A) const {
220 CALLSITE_DELEGATE_GETTER(hasFnAttr(A));
223 /// \brief Return true if the call or the callee has the given attribute.
224 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
225 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A));
228 /// @brief Extract the alignment for a call or parameter (0=unknown).
229 uint16_t getParamAlignment(uint16_t i) const {
230 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
233 /// @brief Extract the number of dereferenceable bytes for a call or
234 /// parameter (0=unknown).
235 uint64_t getDereferenceableBytes(uint16_t i) const {
236 CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
239 /// @brief Extract the number of dereferenceable_or_null bytes for a call or
240 /// parameter (0=unknown).
241 uint64_t getDereferenceableOrNullBytes(uint16_t i) const {
242 CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i));
245 /// \brief Return true if the call should not be treated as a call to a
247 bool isNoBuiltin() const {
248 CALLSITE_DELEGATE_GETTER(isNoBuiltin());
251 /// @brief Return true if the call should not be inlined.
252 bool isNoInline() const {
253 CALLSITE_DELEGATE_GETTER(isNoInline());
255 void setIsNoInline(bool Value = true) {
256 CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
259 /// @brief Determine if the call does not access memory.
260 bool doesNotAccessMemory() const {
261 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
263 void setDoesNotAccessMemory() {
264 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
267 /// @brief Determine if the call does not access or only reads memory.
268 bool onlyReadsMemory() const {
269 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
271 void setOnlyReadsMemory() {
272 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
275 /// @brief Determine if the call cannot return.
276 bool doesNotReturn() const {
277 CALLSITE_DELEGATE_GETTER(doesNotReturn());
279 void setDoesNotReturn() {
280 CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
283 /// @brief Determine if the call cannot unwind.
284 bool doesNotThrow() const {
285 CALLSITE_DELEGATE_GETTER(doesNotThrow());
287 void setDoesNotThrow() {
288 CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
291 #undef CALLSITE_DELEGATE_GETTER
292 #undef CALLSITE_DELEGATE_SETTER
294 /// @brief Determine whether this argument is not captured.
295 bool doesNotCapture(unsigned ArgNo) const {
296 return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
299 /// @brief Determine whether this argument is passed by value.
300 bool isByValArgument(unsigned ArgNo) const {
301 return paramHasAttr(ArgNo + 1, Attribute::ByVal);
304 /// @brief Determine whether this argument is passed in an alloca.
305 bool isInAllocaArgument(unsigned ArgNo) const {
306 return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
309 /// @brief Determine whether this argument is passed by value or in an alloca.
310 bool isByValOrInAllocaArgument(unsigned ArgNo) const {
311 return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
312 paramHasAttr(ArgNo + 1, Attribute::InAlloca);
315 /// @brief Determine if there are is an inalloca argument. Only the last
316 /// argument can have the inalloca attribute.
317 bool hasInAllocaArgument() const {
318 return paramHasAttr(arg_size(), Attribute::InAlloca);
321 bool doesNotAccessMemory(unsigned ArgNo) const {
322 return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
325 bool onlyReadsMemory(unsigned ArgNo) const {
326 return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
327 paramHasAttr(ArgNo + 1, Attribute::ReadNone);
330 /// @brief Return true if the return value is known to be not null.
331 /// This may be because it has the nonnull attribute, or because at least
332 /// one byte is dereferenceable and the pointer is in addrspace(0).
333 bool isReturnNonNull() const {
334 if (paramHasAttr(0, Attribute::NonNull))
336 else if (getDereferenceableBytes(0) > 0 &&
337 getType()->getPointerAddressSpace() == 0)
343 /// hasArgument - Returns true if this CallSite passes the given Value* as an
344 /// argument to the called function.
345 bool hasArgument(const Value *Arg) const {
346 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
348 if (AI->get() == Arg)
354 unsigned getArgumentEndOffset() const {
356 return 1; // Skip Callee
358 return 3; // Skip BB, BB, Callee
361 IterTy getCallee() const {
362 if (isCall()) // Skip Callee
363 return cast<CallInst>(getInstruction())->op_end() - 1;
364 else // Skip BB, BB, Callee
365 return cast<InvokeInst>(getInstruction())->op_end() - 3;
369 class CallSite : public CallSiteBase<Function, Value, User, Instruction,
370 CallInst, InvokeInst, User::op_iterator> {
373 CallSite(CallSiteBase B) : CallSiteBase(B) {}
374 CallSite(CallInst *CI) : CallSiteBase(CI) {}
375 CallSite(InvokeInst *II) : CallSiteBase(II) {}
376 explicit CallSite(Instruction *II) : CallSiteBase(II) {}
377 explicit CallSite(Value *V) : CallSiteBase(V) {}
379 bool operator==(const CallSite &CS) const { return I == CS.I; }
380 bool operator!=(const CallSite &CS) const { return I != CS.I; }
381 bool operator<(const CallSite &CS) const {
382 return getInstruction() < CS.getInstruction();
386 User::op_iterator getCallee() const;
389 /// ImmutableCallSite - establish a view to a call site for examination
390 class ImmutableCallSite : public CallSiteBase<> {
392 ImmutableCallSite() {}
393 ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
394 ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
395 explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
396 explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
397 ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
400 } // End llvm namespace