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();
74 /// isCall - true if a CallInst is enclosed.
75 /// Note that !isCall() does not mean it is an InvokeInst enclosed,
76 /// it also could signify a NULL Instruction pointer.
77 bool isCall() const { return I.getInt(); }
79 /// isInvoke - true if a InvokeInst is enclosed.
81 bool isInvoke() const { return getInstruction() && !I.getInt(); }
83 InstrTy *getInstruction() const { return I.getPointer(); }
84 InstrTy *operator->() const { return I.getPointer(); }
85 explicit operator bool() const { return I.getPointer(); }
87 /// Get the basic block containing the call site
88 BBTy* getParent() const { return getInstruction()->getParent(); }
90 /// getCalledValue - Return the pointer to function that is being called.
92 ValTy *getCalledValue() const {
93 assert(getInstruction() && "Not a call or invoke instruction!");
97 /// getCalledFunction - Return the function being called if this is a direct
98 /// call, otherwise return null (if it's an indirect call).
100 FunTy *getCalledFunction() const {
101 return dyn_cast<FunTy>(getCalledValue());
104 /// setCalledFunction - Set the callee to the specified value.
106 void setCalledFunction(Value *V) {
107 assert(getInstruction() && "Not a call or invoke instruction!");
111 /// isCallee - Determine whether the passed iterator points to the
112 /// callee operand's Use.
113 bool isCallee(Value::const_user_iterator UI) const {
114 return isCallee(&UI.getUse());
117 /// Determine whether this Use is the callee operand's Use.
118 bool isCallee(const Use *U) const { return getCallee() == U; }
120 ValTy *getArgument(unsigned ArgNo) const {
121 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
122 return *(arg_begin() + ArgNo);
125 void setArgument(unsigned ArgNo, Value* newVal) {
126 assert(getInstruction() && "Not a call or invoke instruction!");
127 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
128 getInstruction()->setOperand(ArgNo, newVal);
131 /// Given a value use iterator, returns the argument that corresponds to it.
132 /// Iterator must actually correspond to an argument.
133 unsigned getArgumentNo(Value::const_user_iterator I) const {
134 return getArgumentNo(&I.getUse());
137 /// Given a use for an argument, get the argument number that corresponds to
139 unsigned getArgumentNo(const Use *U) const {
140 assert(getInstruction() && "Not a call or invoke instruction!");
141 assert(arg_begin() <= U && U < arg_end()
142 && "Argument # out of range!");
143 return U - arg_begin();
146 /// arg_iterator - The type of iterator to use when looping over actual
147 /// arguments at this call site.
148 typedef IterTy arg_iterator;
150 /// arg_begin/arg_end - Return iterators corresponding to the actual argument
151 /// list for a call site.
152 IterTy arg_begin() const {
153 assert(getInstruction() && "Not a call or invoke instruction!");
154 // Skip non-arguments
155 return (*this)->op_begin();
158 IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
159 iterator_range<IterTy> args() const {
160 return iterator_range<IterTy>(arg_begin(), arg_end());
162 bool arg_empty() const { return arg_end() == arg_begin(); }
163 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
165 /// getType - Return the type of the instruction that generated this call site
167 Type *getType() const { return (*this)->getType(); }
169 /// getCaller - Return the caller function for this call site
171 FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
173 /// \brief Tests if this call site must be tail call optimized. Only a
174 /// CallInst can be tail call optimized.
175 bool isMustTailCall() const {
176 return isCall() && cast<CallInst>(getInstruction())->isMustTailCall();
179 /// \brief Tests if this call site is marked as a tail call.
180 bool isTailCall() const {
181 return isCall() && cast<CallInst>(getInstruction())->isTailCall();
184 #define CALLSITE_DELEGATE_GETTER(METHOD) \
185 InstrTy *II = getInstruction(); \
187 ? cast<CallInst>(II)->METHOD \
188 : cast<InvokeInst>(II)->METHOD
190 #define CALLSITE_DELEGATE_SETTER(METHOD) \
191 InstrTy *II = getInstruction(); \
193 cast<CallInst>(II)->METHOD; \
195 cast<InvokeInst>(II)->METHOD
197 unsigned getNumArgOperands() const {
198 CALLSITE_DELEGATE_GETTER(getNumArgOperands());
201 ValTy *getArgOperand(unsigned i) const {
202 CALLSITE_DELEGATE_GETTER(getArgOperand(i));
205 bool isInlineAsm() const {
207 return cast<CallInst>(getInstruction())->isInlineAsm();
211 /// getCallingConv/setCallingConv - get or set the calling convention of the
213 CallingConv::ID getCallingConv() const {
214 CALLSITE_DELEGATE_GETTER(getCallingConv());
216 void setCallingConv(CallingConv::ID CC) {
217 CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
220 FunctionType *getFunctionType() const {
221 CALLSITE_DELEGATE_GETTER(getFunctionType());
224 void mutateFunctionType(FunctionType *Ty) const {
225 CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty));
228 /// getAttributes/setAttributes - get or set the parameter attributes of
230 const AttributeSet &getAttributes() const {
231 CALLSITE_DELEGATE_GETTER(getAttributes());
233 void setAttributes(const AttributeSet &PAL) {
234 CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
237 /// \brief Return true if this function has the given attribute.
238 bool hasFnAttr(Attribute::AttrKind A) const {
239 CALLSITE_DELEGATE_GETTER(hasFnAttr(A));
242 /// \brief Return true if the call or the callee has the given attribute.
243 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
244 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A));
247 /// @brief Extract the alignment for a call or parameter (0=unknown).
248 uint16_t getParamAlignment(uint16_t i) const {
249 CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
252 /// @brief Extract the number of dereferenceable bytes for a call or
253 /// parameter (0=unknown).
254 uint64_t getDereferenceableBytes(uint16_t i) const {
255 CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
258 /// @brief Extract the number of dereferenceable_or_null bytes for a call or
259 /// parameter (0=unknown).
260 uint64_t getDereferenceableOrNullBytes(uint16_t i) const {
261 CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i));
264 /// \brief Return true if the call should not be treated as a call to a
266 bool isNoBuiltin() const {
267 CALLSITE_DELEGATE_GETTER(isNoBuiltin());
270 /// @brief Return true if the call should not be inlined.
271 bool isNoInline() const {
272 CALLSITE_DELEGATE_GETTER(isNoInline());
274 void setIsNoInline(bool Value = true) {
275 CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
278 /// @brief Determine if the call does not access memory.
279 bool doesNotAccessMemory() const {
280 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
282 void setDoesNotAccessMemory() {
283 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
286 /// @brief Determine if the call does not access or only reads memory.
287 bool onlyReadsMemory() const {
288 CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
290 void setOnlyReadsMemory() {
291 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
294 /// @brief Determine if the call can access memmory only using pointers based
295 /// on its arguments.
296 bool onlyAccessesArgMemory() const {
297 CALLSITE_DELEGATE_GETTER(onlyAccessesArgMemory());
299 void setOnlyAccessesArgMemory() {
300 CALLSITE_DELEGATE_SETTER(setOnlyAccessesArgMemory());
303 /// @brief Determine if the call cannot return.
304 bool doesNotReturn() const {
305 CALLSITE_DELEGATE_GETTER(doesNotReturn());
307 void setDoesNotReturn() {
308 CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
311 /// @brief Determine if the call cannot unwind.
312 bool doesNotThrow() const {
313 CALLSITE_DELEGATE_GETTER(doesNotThrow());
315 void setDoesNotThrow() {
316 CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
319 #undef CALLSITE_DELEGATE_GETTER
320 #undef CALLSITE_DELEGATE_SETTER
322 /// @brief Determine whether this argument is not captured.
323 bool doesNotCapture(unsigned ArgNo) const {
324 return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
327 /// @brief Determine whether this argument is passed by value.
328 bool isByValArgument(unsigned ArgNo) const {
329 return paramHasAttr(ArgNo + 1, Attribute::ByVal);
332 /// @brief Determine whether this argument is passed in an alloca.
333 bool isInAllocaArgument(unsigned ArgNo) const {
334 return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
337 /// @brief Determine whether this argument is passed by value or in an alloca.
338 bool isByValOrInAllocaArgument(unsigned ArgNo) const {
339 return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
340 paramHasAttr(ArgNo + 1, Attribute::InAlloca);
343 /// @brief Determine if there are is an inalloca argument. Only the last
344 /// argument can have the inalloca attribute.
345 bool hasInAllocaArgument() const {
346 return paramHasAttr(arg_size(), Attribute::InAlloca);
349 bool doesNotAccessMemory(unsigned ArgNo) const {
350 return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
353 bool onlyReadsMemory(unsigned ArgNo) const {
354 return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
355 paramHasAttr(ArgNo + 1, Attribute::ReadNone);
358 /// @brief Return true if the return value is known to be not null.
359 /// This may be because it has the nonnull attribute, or because at least
360 /// one byte is dereferenceable and the pointer is in addrspace(0).
361 bool isReturnNonNull() const {
362 if (paramHasAttr(0, Attribute::NonNull))
364 else if (getDereferenceableBytes(0) > 0 &&
365 getType()->getPointerAddressSpace() == 0)
371 /// hasArgument - Returns true if this CallSite passes the given Value* as an
372 /// argument to the called function.
373 bool hasArgument(const Value *Arg) const {
374 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
376 if (AI->get() == Arg)
382 unsigned getArgumentEndOffset() const {
384 return 1; // Skip Callee
386 return 3; // Skip BB, BB, Callee
389 IterTy getCallee() const {
390 if (isCall()) // Skip Callee
391 return cast<CallInst>(getInstruction())->op_end() - 1;
392 else // Skip BB, BB, Callee
393 return cast<InvokeInst>(getInstruction())->op_end() - 3;
397 class CallSite : public CallSiteBase<Function, BasicBlock, Value, User,
398 Instruction, CallInst, InvokeInst,
402 CallSite(CallSiteBase B) : CallSiteBase(B) {}
403 CallSite(CallInst *CI) : CallSiteBase(CI) {}
404 CallSite(InvokeInst *II) : CallSiteBase(II) {}
405 explicit CallSite(Instruction *II) : CallSiteBase(II) {}
406 explicit CallSite(Value *V) : CallSiteBase(V) {}
408 bool operator==(const CallSite &CS) const { return I == CS.I; }
409 bool operator!=(const CallSite &CS) const { return I != CS.I; }
410 bool operator<(const CallSite &CS) const {
411 return getInstruction() < CS.getInstruction();
415 User::op_iterator getCallee() const;
418 /// ImmutableCallSite - establish a view to a call site for examination
419 class ImmutableCallSite : public CallSiteBase<> {
421 ImmutableCallSite() {}
422 ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
423 ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
424 explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
425 explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
426 ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
429 } // End llvm namespace