LLVM  3.7.0
CallSite.h
Go to the documentation of this file.
1 //===- CallSite.h - Abstract Call & Invoke instrs ---------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
15 //
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.
23 //
24 //===----------------------------------------------------------------------===//
25 
26 #ifndef LLVM_IR_CALLSITE_H
27 #define LLVM_IR_CALLSITE_H
28 
31 #include "llvm/IR/Attributes.h"
32 #include "llvm/IR/CallingConv.h"
33 #include "llvm/IR/Instructions.h"
34 
35 namespace llvm {
36 
37 class CallInst;
38 class InvokeInst;
39 
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>
48 class CallSiteBase {
49 protected:
51 
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); }
56 
57 private:
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.
62  ///
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));
69  }
70  return CallSiteBase();
71  }
72 public:
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(); }
77 
78  /// isInvoke - true if a InvokeInst is enclosed.
79  ///
80  bool isInvoke() const { return getInstruction() && !I.getInt(); }
81 
82  InstrTy *getInstruction() const { return I.getPointer(); }
83  InstrTy *operator->() const { return I.getPointer(); }
84  explicit operator bool() const { return I.getPointer(); }
85 
86  /// Get the basic block containing the call site
87  BBTy* getParent() const { return getInstruction()->getParent(); }
88 
89  /// getCalledValue - Return the pointer to function that is being called.
90  ///
91  ValTy *getCalledValue() const {
92  assert(getInstruction() && "Not a call or invoke instruction!");
93  return *getCallee();
94  }
95 
96  /// getCalledFunction - Return the function being called if this is a direct
97  /// call, otherwise return null (if it's an indirect call).
98  ///
99  FunTy *getCalledFunction() const {
100  return dyn_cast<FunTy>(getCalledValue());
101  }
102 
103  /// setCalledFunction - Set the callee to the specified value.
104  ///
106  assert(getInstruction() && "Not a call or invoke instruction!");
107  *getCallee() = V;
108  }
109 
110  /// isCallee - Determine whether the passed iterator points to the
111  /// callee operand's Use.
113  return isCallee(&UI.getUse());
114  }
115 
116  /// Determine whether this Use is the callee operand's Use.
117  bool isCallee(const Use *U) const { return getCallee() == U; }
118 
119  ValTy *getArgument(unsigned ArgNo) const {
120  assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
121  return *(arg_begin() + ArgNo);
122  }
123 
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);
128  }
129 
130  /// Given a value use iterator, returns the argument that corresponds to it.
131  /// Iterator must actually correspond to an argument.
133  return getArgumentNo(&I.getUse());
134  }
135 
136  /// Given a use for an argument, get the argument number that corresponds to
137  /// it.
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();
143  }
144 
145  /// arg_iterator - The type of iterator to use when looping over actual
146  /// arguments at this call site.
147  typedef IterTy arg_iterator;
148 
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();
155  }
156 
157  IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
160  }
161  bool arg_empty() const { return arg_end() == arg_begin(); }
162  unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
163 
164  /// getType - Return the type of the instruction that generated this call site
165  ///
166  Type *getType() const { return (*this)->getType(); }
167 
168  /// getCaller - Return the caller function for this call site
169  ///
170  FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
171 
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();
176  }
177 
178  /// \brief Tests if this call site is marked as a tail call.
179  bool isTailCall() const {
180  return isCall() && cast<CallInst>(getInstruction())->isTailCall();
181  }
182 
183 #define CALLSITE_DELEGATE_GETTER(METHOD) \
184  InstrTy *II = getInstruction(); \
185  return isCall() \
186  ? cast<CallInst>(II)->METHOD \
187  : cast<InvokeInst>(II)->METHOD
188 
189 #define CALLSITE_DELEGATE_SETTER(METHOD) \
190  InstrTy *II = getInstruction(); \
191  if (isCall()) \
192  cast<CallInst>(II)->METHOD; \
193  else \
194  cast<InvokeInst>(II)->METHOD
195 
196  unsigned getNumArgOperands() const {
198  }
199 
200  ValTy *getArgOperand(unsigned i) const {
202  }
203 
204  bool isInlineAsm() const {
205  if (isCall())
206  return cast<CallInst>(getInstruction())->isInlineAsm();
207  return false;
208  }
209 
210  /// getCallingConv/setCallingConv - get or set the calling convention of the
211  /// call.
214  }
217  }
218 
221  }
222 
225  }
226 
227  /// getAttributes/setAttributes - get or set the parameter attributes of
228  /// the call.
229  const AttributeSet &getAttributes() const {
231  }
232  void setAttributes(const AttributeSet &PAL) {
234  }
235 
236  /// \brief Return true if this function has the given attribute.
239  }
240 
241  /// \brief Return true if the call or the callee has the given attribute.
242  bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
244  }
245 
246  /// @brief Extract the alignment for a call or parameter (0=unknown).
247  uint16_t getParamAlignment(uint16_t i) const {
249  }
250 
251  /// @brief Extract the number of dereferenceable bytes for a call or
252  /// parameter (0=unknown).
253  uint64_t getDereferenceableBytes(uint16_t i) const {
255  }
256 
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 {
261  }
262 
263  /// \brief Return true if the call should not be treated as a call to a
264  /// builtin.
265  bool isNoBuiltin() const {
267  }
268 
269  /// @brief Return true if the call should not be inlined.
270  bool isNoInline() const {
272  }
273  void setIsNoInline(bool Value = true) {
275  }
276 
277  /// @brief Determine if the call does not access memory.
278  bool doesNotAccessMemory() const {
280  }
283  }
284 
285  /// @brief Determine if the call does not access or only reads memory.
286  bool onlyReadsMemory() const {
288  }
291  }
292 
293  /// @brief Determine if the call can access memmory only using pointers based
294  /// on its arguments.
295  bool onlyAccessesArgMemory() const {
297  }
300  }
301 
302  /// @brief Determine if the call cannot return.
303  bool doesNotReturn() const {
305  }
308  }
309 
310  /// @brief Determine if the call cannot unwind.
311  bool doesNotThrow() const {
313  }
316  }
317 
318 #undef CALLSITE_DELEGATE_GETTER
319 #undef CALLSITE_DELEGATE_SETTER
320 
321  /// @brief Determine whether this argument is not captured.
322  bool doesNotCapture(unsigned ArgNo) const {
323  return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
324  }
325 
326  /// @brief Determine whether this argument is passed by value.
327  bool isByValArgument(unsigned ArgNo) const {
328  return paramHasAttr(ArgNo + 1, Attribute::ByVal);
329  }
330 
331  /// @brief Determine whether this argument is passed in an alloca.
332  bool isInAllocaArgument(unsigned ArgNo) const {
333  return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
334  }
335 
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);
340  }
341 
342  /// @brief Determine if there are is an inalloca argument. Only the last
343  /// argument can have the inalloca attribute.
344  bool hasInAllocaArgument() const {
346  }
347 
348  bool doesNotAccessMemory(unsigned ArgNo) const {
349  return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
350  }
351 
352  bool onlyReadsMemory(unsigned ArgNo) const {
353  return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
354  paramHasAttr(ArgNo + 1, Attribute::ReadNone);
355  }
356 
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 {
362  return true;
363  else if (getDereferenceableBytes(0) > 0 &&
364  getType()->getPointerAddressSpace() == 0)
365  return true;
366 
367  return false;
368  }
369 
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;
374  ++AI)
375  if (AI->get() == Arg)
376  return true;
377  return false;
378  }
379 
380 private:
381  unsigned getArgumentEndOffset() const {
382  if (isCall())
383  return 1; // Skip Callee
384  else
385  return 3; // Skip BB, BB, Callee
386  }
387 
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;
393  }
394 };
395 
396 class CallSite : public CallSiteBase<Function, BasicBlock, Value, User,
397  Instruction, CallInst, InvokeInst,
398  User::op_iterator> {
399 public:
400  CallSite() {}
404  explicit CallSite(Instruction *II) : CallSiteBase(II) {}
405  explicit CallSite(Value *V) : CallSiteBase(V) {}
406 
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();
411  }
412 
413 private:
414  User::op_iterator getCallee() const;
415 };
416 
417 /// ImmutableCallSite - establish a view to a call site for examination
418 class ImmutableCallSite : public CallSiteBase<> {
419 public:
423  explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
424  explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
426 };
427 
428 } // End llvm namespace
429 
430 #endif
void setDoesNotReturn()
Definition: CallSite.h:306
bool hasArgument(const Value *Arg) const
hasArgument - Returns true if this CallSite passes the given Value* as an argument to the called func...
Definition: CallSite.h:372
CallSiteBase(InvokeTy *II)
Definition: CallSite.h:54
bool hasInAllocaArgument() const
Determine if there are is an inalloca argument.
Definition: CallSite.h:344
ImmutableCallSite(const Value *V)
Definition: CallSite.h:424
bool isTailCall() const
Tests if this call site is marked as a tail call.
Definition: CallSite.h:179
ImmutableCallSite(const Instruction *II)
Definition: CallSite.h:423
CallSite(Instruction *II)
Definition: CallSite.h:404
Various leaf nodes.
Definition: ISDOpcodes.h:60
ValTy * getArgument(unsigned ArgNo) const
Definition: CallSite.h:119
unsigned getArgumentNo(const Use *U) const
Given a use for an argument, get the argument number that corresponds to it.
Definition: CallSite.h:138
bool isCallee(const Use *U) const
Determine whether this Use is the callee operand's Use.
Definition: CallSite.h:117
InstrTy * getInstruction() const
Definition: CallSite.h:82
#define CALLSITE_DELEGATE_GETTER(METHOD)
Definition: CallSite.h:183
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
CallInst - This class represents a function call, abstracting a target machine's calling convention...
BBTy * getParent() const
Get the basic block containing the call site.
Definition: CallSite.h:87
void setOnlyReadsMemory()
Definition: CallSite.h:289
void setAttributes(const AttributeSet &PAL)
Definition: CallSite.h:232
FunTy * getCaller() const
getCaller - Return the caller function for this call site
Definition: CallSite.h:170
IterTy arg_iterator
arg_iterator - The type of iterator to use when looping over actual arguments at this call site...
Definition: CallSite.h:147
bool isByValOrInAllocaArgument(unsigned ArgNo) const
Determine whether this argument is passed by value or in an alloca.
Definition: CallSite.h:337
CallSite(InvokeInst *II)
Definition: CallSite.h:403
IterTy arg_end() const
Definition: CallSite.h:157
bool operator==(const CallSite &CS) const
Definition: CallSite.h:407
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
CallSite(Value *V)
Definition: CallSite.h:405
bool doesNotThrow() const
Determine if the call cannot unwind.
Definition: CallSite.h:311
bool doesNotReturn() const
Determine if the call cannot return.
Definition: CallSite.h:303
This file contains the simple types necessary to represent the attributes associated with functions a...
uint64_t getDereferenceableBytes(uint16_t i) const
Extract the number of dereferenceable bytes for a call or parameter (0=unknown).
Definition: CallSite.h:253
bool hasFnAttr(Attribute::AttrKind A) const
Return true if this function has the given attribute.
Definition: CallSite.h:237
#define false
Definition: ConvertUTF.c:65
uint16_t getParamAlignment(uint16_t i) const
Extract the alignment for a call or parameter (0=unknown).
Definition: CallSite.h:247
Function does not access memory.
Definition: Attributes.h:99
Function creates no aliases of pointer.
Definition: Attributes.h:85
void setOnlyAccessesArgMemory()
Definition: CallSite.h:298
iterator_range< IterTy > args() const
Definition: CallSite.h:158
FunctionType - Class to represent function types.
Definition: DerivedTypes.h:96
ValTy * getCalledValue() const
getCalledValue - Return the pointer to function that is being called.
Definition: CallSite.h:91
unsigned getNumArgOperands() const
Definition: CallSite.h:196
bool isInlineAsm() const
Definition: CallSite.h:204
Pass structure by value.
Definition: Attributes.h:73
void setCallingConv(CallingConv::ID CC)
Definition: CallSite.h:215
PointerIntPair< InstrTy *, 1, bool > I
Definition: CallSite.h:50
CallSite(CallSiteBase B)
Definition: CallSite.h:401
bool doesNotAccessMemory() const
Determine if the call does not access memory.
Definition: CallSite.h:278
bool onlyReadsMemory(unsigned ArgNo) const
Definition: CallSite.h:352
bool doesNotAccessMemory(unsigned ArgNo) const
Definition: CallSite.h:348
#define true
Definition: ConvertUTF.c:66
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
unsigned getArgumentNo(Value::const_user_iterator I) const
Given a value use iterator, returns the argument that corresponds to it.
Definition: CallSite.h:132
FunTy * getCalledFunction() const
getCalledFunction - Return the function being called if this is a direct call, otherwise return null ...
Definition: CallSite.h:99
void setIsNoInline(bool Value=true)
Definition: CallSite.h:273
bool isByValArgument(unsigned ArgNo) const
Determine whether this argument is passed by value.
Definition: CallSite.h:327
Pass structure in an alloca.
Definition: Attributes.h:74
CallSite(CallInst *CI)
Definition: CallSite.h:402
void setDoesNotAccessMemory()
Definition: CallSite.h:281
void mutateFunctionType(FunctionType *Ty) const
Definition: CallSite.h:223
uint64_t getDereferenceableOrNullBytes(uint16_t i) const
Extract the number of dereferenceable_or_null bytes for a call or parameter (0=unknown).
Definition: CallSite.h:259
IntType getInt() const
ImmutableCallSite(CallSite CS)
Definition: CallSite.h:425
ValTy * getArgOperand(unsigned i) const
Definition: CallSite.h:200
bool arg_empty() const
Definition: CallSite.h:161
Pointer is known to be not null.
Definition: Attributes.h:91
bool operator!=(const CallSite &CS) const
Definition: CallSite.h:408
bool isMustTailCall() const
Tests if this call site must be tail call optimized.
Definition: CallSite.h:174
PointerTy getPointer() const
InstrTy * operator->() const
Definition: CallSite.h:83
CallSiteBase(CallTy *CI)
Definition: CallSite.h:53
bool paramHasAttr(unsigned i, Attribute::AttrKind A) const
Return true if the call or the callee has the given attribute.
Definition: CallSite.h:242
ImmutableCallSite(const InvokeInst *II)
Definition: CallSite.h:422
bool isCallee(Value::const_user_iterator UI) const
isCallee - Determine whether the passed iterator points to the callee operand's Use.
Definition: CallSite.h:112
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
Definition: CallSite.h:265
unsigned arg_size() const
Definition: CallSite.h:162
bool isInAllocaArgument(unsigned ArgNo) const
Determine whether this argument is passed in an alloca.
Definition: CallSite.h:332
bool isNoInline() const
Return true if the call should not be inlined.
Definition: CallSite.h:270
A range adaptor for a pair of iterators.
ImmutableCallSite(const CallInst *CI)
Definition: CallSite.h:421
Function only reads from memory.
Definition: Attributes.h:100
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
could "use" a pointer
bool isReturnNonNull() const
Return true if the return value is known to be not null.
Definition: CallSite.h:360
user_iterator_impl< const User > const_user_iterator
Definition: Value.h:293
bool doesNotCapture(unsigned ArgNo) const
Determine whether this argument is not captured.
Definition: CallSite.h:322
#define CALLSITE_DELEGATE_SETTER(METHOD)
Definition: CallSite.h:189
void setCalledFunction(Value *V)
setCalledFunction - Set the callee to the specified value.
Definition: CallSite.h:105
ImmutableCallSite - establish a view to a call site for examination.
Definition: CallSite.h:418
CallingConv::ID getCallingConv() const
getCallingConv/setCallingConv - get or set the calling convention of the call.
Definition: CallSite.h:212
const AttributeSet & getAttributes() const
getAttributes/setAttributes - get or set the parameter attributes of the call.
Definition: CallSite.h:229
bool operator<(const CallSite &CS) const
Definition: CallSite.h:409
FunctionType * getFunctionType() const
Definition: CallSite.h:219
Type * getType() const
getType - Return the type of the instruction that generated this call site
Definition: CallSite.h:166
CallSiteBase(ValTy *II)
Definition: CallSite.h:55
void setArgument(unsigned ArgNo, Value *newVal)
Definition: CallSite.h:124
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
LLVM Value Representation.
Definition: Value.h:69
InvokeInst - Invoke instruction.
IterTy arg_begin() const
arg_begin/arg_end - Return iterators corresponding to the actual argument list for a call site...
Definition: CallSite.h:151
bool isInvoke() const
isInvoke - true if a InvokeInst is enclosed.
Definition: CallSite.h:80
void setDoesNotThrow()
Definition: CallSite.h:314
bool isCall() const
isCall - true if a CallInst is enclosed.
Definition: CallSite.h:76
bool onlyReadsMemory() const
Determine if the call does not access or only reads memory.
Definition: CallSite.h:286
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results...
Definition: Attributes.h:64
const Use * const_op_iterator
Definition: User.h:179
bool onlyAccessesArgMemory() const
Determine if the call can access memmory only using pointers based on its arguments.
Definition: CallSite.h:295