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 /// getAttributes/setAttributes - get or set the parameter attributes of
199 const AttributeSet &getAttributes() const {
200 CALLSITE_DELEGATE_GETTER(getAttributes());
202 void setAttributes(const AttributeSet &PAL) {
203 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
206 /// \brief Return true if this function has the given attribute.
207 bool hasFnAttr(Attribute::AttrKind A) const {
208 CALLSITE_DELEGATE_GETTER(hasFnAttr(A));
211 /// \brief Return true if the call or the callee has the given attribute.
212 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
213 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A));
216 /// @brief Extract the alignment for a call or parameter (0=unknown).
217 uint16_t getParamAlignment(uint16_t i) const {
218 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
221 /// @brief Extract the number of dereferenceable bytes for a call or
222 /// parameter (0=unknown).
223 uint64_t getDereferenceableBytes(uint16_t i) const {
224 CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
227 /// \brief Return true if the call should not be treated as a call to a
229 bool isNoBuiltin() const {
230 CALLSITE_DELEGATE_GETTER(isNoBuiltin());
233 /// @brief Return true if the call should not be inlined.
234 bool isNoInline() const {
235 CALLSITE_DELEGATE_GETTER(isNoInline());
237 void setIsNoInline(bool Value = true) {
238 CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
241 /// @brief Determine if the call does not access memory.
242 bool doesNotAccessMemory() const {
243 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
245 void setDoesNotAccessMemory() {
246 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
249 /// @brief Determine if the call does not access or only reads memory.
250 bool onlyReadsMemory() const {
251 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
253 void setOnlyReadsMemory() {
254 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
257 /// @brief Determine if the call cannot return.
258 bool doesNotReturn() const {
259 CALLSITE_DELEGATE_GETTER(doesNotReturn());
261 void setDoesNotReturn() {
262 CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
265 /// @brief Determine if the call cannot unwind.
266 bool doesNotThrow() const {
267 CALLSITE_DELEGATE_GETTER(doesNotThrow());
269 void setDoesNotThrow() {
270 CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
273 #undef CALLSITE_DELEGATE_GETTER
274 #undef CALLSITE_DELEGATE_SETTER
276 /// @brief Determine whether this argument is not captured.
277 bool doesNotCapture(unsigned ArgNo) const {
278 return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
281 /// @brief Determine whether this argument is passed by value.
282 bool isByValArgument(unsigned ArgNo) const {
283 return paramHasAttr(ArgNo + 1, Attribute::ByVal);
286 /// @brief Determine whether this argument is passed in an alloca.
287 bool isInAllocaArgument(unsigned ArgNo) const {
288 return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
291 /// @brief Determine whether this argument is passed by value or in an alloca.
292 bool isByValOrInAllocaArgument(unsigned ArgNo) const {
293 return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
294 paramHasAttr(ArgNo + 1, Attribute::InAlloca);
297 /// @brief Determine if there are is an inalloca argument. Only the last
298 /// argument can have the inalloca attribute.
299 bool hasInAllocaArgument() const {
300 return paramHasAttr(arg_size(), Attribute::InAlloca);
303 bool doesNotAccessMemory(unsigned ArgNo) const {
304 return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
307 bool onlyReadsMemory(unsigned ArgNo) const {
308 return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
309 paramHasAttr(ArgNo + 1, Attribute::ReadNone);
312 /// @brief Return true if the return value is known to be not null.
313 /// This may be because it has the nonnull attribute, or because at least
314 /// one byte is dereferenceable and the pointer is in addrspace(0).
315 bool isReturnNonNull() const {
316 if (paramHasAttr(0, Attribute::NonNull))
318 else if (getDereferenceableBytes(0) > 0 &&
319 getType()->getPointerAddressSpace() == 0)
325 /// hasArgument - Returns true if this CallSite passes the given Value* as an
326 /// argument to the called function.
327 bool hasArgument(const Value *Arg) const {
328 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
330 if (AI->get() == Arg)
336 unsigned getArgumentEndOffset() const {
338 return 1; // Skip Callee
340 return 3; // Skip BB, BB, Callee
343 IterTy getCallee() const {
344 if (isCall()) // Skip Callee
345 return cast<CallInst>(getInstruction())->op_end() - 1;
346 else // Skip BB, BB, Callee
347 return cast<InvokeInst>(getInstruction())->op_end() - 3;
351 class CallSite : public CallSiteBase<Function, Value, User, Instruction,
352 CallInst, InvokeInst, User::op_iterator> {
355 CallSite(CallSiteBase B) : CallSiteBase(B) {}
356 CallSite(CallInst *CI) : CallSiteBase(CI) {}
357 CallSite(InvokeInst *II) : CallSiteBase(II) {}
358 explicit CallSite(Instruction *II) : CallSiteBase(II) {}
359 explicit CallSite(Value *V) : CallSiteBase(V) {}
361 bool operator==(const CallSite &CS) const { return I == CS.I; }
362 bool operator!=(const CallSite &CS) const { return I != CS.I; }
363 bool operator<(const CallSite &CS) const {
364 return getInstruction() < CS.getInstruction();
368 User::op_iterator getCallee() const;
371 /// ImmutableCallSite - establish a view to a call site for examination
372 class ImmutableCallSite : public CallSiteBase<> {
374 ImmutableCallSite() {}
375 ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
376 ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
377 explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
378 explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
379 ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
382 } // End llvm namespace