LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CallSite.h
Go to the documentation of this file.
1 //===-- llvm/Support/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_SUPPORT_CALLSITE_H
27 #define LLVM_SUPPORT_CALLSITE_H
28 
30 #include "llvm/IR/Attributes.h"
31 #include "llvm/IR/CallingConv.h"
32 #include "llvm/IR/Instructions.h"
33 
34 namespace llvm {
35 
36 class CallInst;
37 class InvokeInst;
38 
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>
46 class CallSiteBase {
47 protected:
49 public:
50  CallSiteBase() : I(0, false) {}
51  CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); }
52  CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); }
53  CallSiteBase(ValTy *II) { *this = get(II); }
54 protected:
55  /// CallSiteBase::get - This static method is sort of like a constructor. It
56  /// will create an appropriate call site for a Call or Invoke instruction, but
57  /// it can also create a null initialized CallSiteBase object for something
58  /// which is NOT a call site.
59  ///
60  static CallSiteBase get(ValTy *V) {
61  if (InstrTy *II = dyn_cast<InstrTy>(V)) {
62  if (II->getOpcode() == Instruction::Call)
63  return CallSiteBase(static_cast<CallTy*>(II));
64  else if (II->getOpcode() == Instruction::Invoke)
65  return CallSiteBase(static_cast<InvokeTy*>(II));
66  }
67  return CallSiteBase();
68  }
69 public:
70  /// isCall - true if a CallInst is enclosed.
71  /// Note that !isCall() does not mean it is an InvokeInst enclosed,
72  /// it also could signify a NULL Instruction pointer.
73  bool isCall() const { return I.getInt(); }
74 
75  /// isInvoke - true if a InvokeInst is enclosed.
76  ///
77  bool isInvoke() const { return getInstruction() && !I.getInt(); }
78 
79  InstrTy *getInstruction() const { return I.getPointer(); }
80  InstrTy *operator->() const { return I.getPointer(); }
81  LLVM_EXPLICIT operator bool() const { return I.getPointer(); }
82 
83  /// getCalledValue - Return the pointer to function that is being called.
84  ///
85  ValTy *getCalledValue() const {
86  assert(getInstruction() && "Not a call or invoke instruction!");
87  return *getCallee();
88  }
89 
90  /// getCalledFunction - Return the function being called if this is a direct
91  /// call, otherwise return null (if it's an indirect call).
92  ///
93  FunTy *getCalledFunction() const {
94  return dyn_cast<FunTy>(getCalledValue());
95  }
96 
97  /// setCalledFunction - Set the callee to the specified value.
98  ///
100  assert(getInstruction() && "Not a call or invoke instruction!");
101  *getCallee() = V;
102  }
103 
104  /// isCallee - Determine whether the passed iterator points to the
105  /// callee operand's Use.
106  ///
108  return getCallee() == &UI.getUse();
109  }
110 
111  ValTy *getArgument(unsigned ArgNo) const {
112  assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
113  return *(arg_begin() + ArgNo);
114  }
115 
116  void setArgument(unsigned ArgNo, Value* newVal) {
117  assert(getInstruction() && "Not a call or invoke instruction!");
118  assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
119  getInstruction()->setOperand(ArgNo, newVal);
120  }
121 
122  /// Given a value use iterator, returns the argument that corresponds to it.
123  /// Iterator must actually correspond to an argument.
125  assert(getInstruction() && "Not a call or invoke instruction!");
126  assert(arg_begin() <= &I.getUse() && &I.getUse() < arg_end()
127  && "Argument # out of range!");
128  return &I.getUse() - arg_begin();
129  }
130 
131  /// arg_iterator - The type of iterator to use when looping over actual
132  /// arguments at this call site.
133  typedef IterTy arg_iterator;
134 
135  /// arg_begin/arg_end - Return iterators corresponding to the actual argument
136  /// list for a call site.
137  IterTy arg_begin() const {
138  assert(getInstruction() && "Not a call or invoke instruction!");
139  // Skip non-arguments
140  return (*this)->op_begin();
141  }
142 
143  IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
144  bool arg_empty() const { return arg_end() == arg_begin(); }
145  unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
146 
147  /// getType - Return the type of the instruction that generated this call site
148  ///
149  Type *getType() const { return (*this)->getType(); }
150 
151  /// getCaller - Return the caller function for this call site
152  ///
153  FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
154 
155 #define CALLSITE_DELEGATE_GETTER(METHOD) \
156  InstrTy *II = getInstruction(); \
157  return isCall() \
158  ? cast<CallInst>(II)->METHOD \
159  : cast<InvokeInst>(II)->METHOD
160 
161 #define CALLSITE_DELEGATE_SETTER(METHOD) \
162  InstrTy *II = getInstruction(); \
163  if (isCall()) \
164  cast<CallInst>(II)->METHOD; \
165  else \
166  cast<InvokeInst>(II)->METHOD
167 
168  /// getCallingConv/setCallingConv - get or set the calling convention of the
169  /// call.
172  }
175  }
176 
177  /// getAttributes/setAttributes - get or set the parameter attributes of
178  /// the call.
179  const AttributeSet &getAttributes() const {
181  }
182  void setAttributes(const AttributeSet &PAL) {
184  }
185 
186  /// \brief Return true if this function has the given attribute.
189  }
190 
191  /// \brief Return true if the call or the callee has the given attribute.
192  bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
194  }
195 
196  /// @brief Extract the alignment for a call or parameter (0=unknown).
197  uint16_t getParamAlignment(uint16_t i) const {
199  }
200 
201  /// \brief Return true if the call should not be treated as a call to a
202  /// builtin.
203  bool isNoBuiltin() const {
205  }
206 
207  /// @brief Return true if the call should not be inlined.
208  bool isNoInline() const {
210  }
211  void setIsNoInline(bool Value = true) {
213  }
214 
215  /// @brief Determine if the call does not access memory.
216  bool doesNotAccessMemory() const {
218  }
221  }
222 
223  /// @brief Determine if the call does not access or only reads memory.
224  bool onlyReadsMemory() const {
226  }
229  }
230 
231  /// @brief Determine if the call cannot return.
232  bool doesNotReturn() const {
234  }
237  }
238 
239  /// @brief Determine if the call cannot unwind.
240  bool doesNotThrow() const {
242  }
245  }
246 
247 #undef CALLSITE_DELEGATE_GETTER
248 #undef CALLSITE_DELEGATE_SETTER
249 
250  /// @brief Determine whether this argument is not captured.
251  bool doesNotCapture(unsigned ArgNo) const {
252  return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
253  }
254 
255  /// @brief Determine whether this argument is passed by value.
256  bool isByValArgument(unsigned ArgNo) const {
257  return paramHasAttr(ArgNo + 1, Attribute::ByVal);
258  }
259 
260  bool doesNotAccessMemory(unsigned ArgNo) const {
261  return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
262  }
263 
264  bool onlyReadsMemory(unsigned ArgNo) const {
265  return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
266  paramHasAttr(ArgNo + 1, Attribute::ReadNone);
267  }
268 
269  /// hasArgument - Returns true if this CallSite passes the given Value* as an
270  /// argument to the called function.
271  bool hasArgument(const Value *Arg) const {
272  for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
273  ++AI)
274  if (AI->get() == Arg)
275  return true;
276  return false;
277  }
278 
279 private:
280  unsigned getArgumentEndOffset() const {
281  if (isCall())
282  return 1; // Skip Callee
283  else
284  return 3; // Skip BB, BB, Callee
285  }
286 
287  IterTy getCallee() const {
288  if (isCall()) // Skip Callee
289  return cast<CallInst>(getInstruction())->op_end() - 1;
290  else // Skip BB, BB, Callee
291  return cast<InvokeInst>(getInstruction())->op_end() - 3;
292  }
293 };
294 
295 class CallSite : public CallSiteBase<Function, Value, User, Instruction,
296  CallInst, InvokeInst, User::op_iterator> {
299 public:
300  CallSite() {}
301  CallSite(Base B) : Base(B) {}
302  CallSite(Value* V) : Base(V) {}
303  CallSite(CallInst *CI) : Base(CI) {}
304  CallSite(InvokeInst *II) : Base(II) {}
305  CallSite(Instruction *II) : Base(II) {}
306 
307  bool operator==(const CallSite &CS) const { return I == CS.I; }
308  bool operator!=(const CallSite &CS) const { return I != CS.I; }
309  bool operator<(const CallSite &CS) const {
310  return getInstruction() < CS.getInstruction();
311  }
312 
313 private:
314  User::op_iterator getCallee() const;
315 };
316 
317 /// ImmutableCallSite - establish a view to a call site for examination
318 class ImmutableCallSite : public CallSiteBase<> {
319  typedef CallSiteBase<> Base;
320 public:
321  ImmutableCallSite(const Value* V) : Base(V) {}
322  ImmutableCallSite(const CallInst *CI) : Base(CI) {}
323  ImmutableCallSite(const InvokeInst *II) : Base(II) {}
324  ImmutableCallSite(const Instruction *II) : Base(II) {}
326 };
327 
328 } // End llvm namespace
329 
330 #endif
bool hasArgument(const Value *Arg) const
Definition: CallSite.h:271
bool isCallee(value_use_iterator< UserTy > UI) const
Definition: CallSite.h:107
ImmutableCallSite(const Value *V)
Definition: CallSite.h:321
ImmutableCallSite(const Instruction *II)
Definition: CallSite.h:324
const AttributeSet & getAttributes() const
Definition: CallSite.h:179
CallSite(Instruction *II)
Definition: CallSite.h:305
IterTy arg_end() const
Definition: CallSite.h:143
unsigned arg_size() const
Definition: CallSite.h:145
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:266
#define CALLSITE_DELEGATE_GETTER(METHOD)
Definition: CallSite.h:155
CallSiteBase(CallTy *CI)
Definition: CallSite.h:51
void setIsNoInline(bool Value=true)
Definition: CallSite.h:211
void setCalledFunction(Value *V)
Definition: CallSite.h:99
void setArgument(unsigned ArgNo, Value *newVal)
Definition: CallSite.h:116
bool doesNotReturn() const
Determine if the call cannot return.
Definition: CallSite.h:232
bool onlyReadsMemory(unsigned ArgNo) const
Definition: CallSite.h:264
CallSite(InvokeInst *II)
Definition: CallSite.h:304
PointerIntPair< InstrTy *, 1, bool > I
Definition: CallSite.h:48
Type * getType() const
Definition: CallSite.h:149
ValTy * getArgument(unsigned ArgNo) const
Definition: CallSite.h:111
InstrTy * operator->() const
Definition: CallSite.h:80
bool operator==(const CallSite &CS) const
Definition: CallSite.h:307
FunTy * getCaller() const
Definition: CallSite.h:153
CallSite(Base B)
Definition: CallSite.h:301
Definition: Use.h:60
CallSite(Value *V)
Definition: CallSite.h:302
This file contains the simple types necessary to represent the attributes associated with functions a...
unsigned getArgumentNo(value_use_iterator< UserTy > I) const
Definition: CallSite.h:124
void setOnlyReadsMemory()
Definition: CallSite.h:227
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
#define false
Definition: ConvertUTF.c:64
Function does not access memory.
Definition: Attributes.h:93
Function creates no aliases of pointer.
Definition: Attributes.h:82
CallingConv::ID getCallingConv() const
Definition: CallSite.h:170
Pass structure by value.
Definition: Attributes.h:73
ValTy * getCalledValue() const
Definition: CallSite.h:85
uint16_t getParamAlignment(uint16_t i) const
Extract the alignment for a call or parameter (0=unknown).
Definition: CallSite.h:197
IterTy arg_iterator
Definition: CallSite.h:133
#define true
Definition: ConvertUTF.c:65
bool isInvoke() const
Definition: CallSite.h:77
InstrTy * getInstruction() const
Definition: CallSite.h:79
CallSite(CallInst *CI)
Definition: CallSite.h:303
IntType getInt() const
bool onlyReadsMemory() const
Determine if the call does not access or only reads memory.
Definition: CallSite.h:224
ImmutableCallSite(CallSite CS)
Definition: CallSite.h:325
bool isNoBuiltin() const
Return true if the call should not be treated as a call to a builtin.
Definition: CallSite.h:203
bool operator!=(const CallSite &CS) const
Definition: CallSite.h:308
PointerTy getPointer() const
bool doesNotThrow() const
Determine if the call cannot unwind.
Definition: CallSite.h:240
bool doesNotAccessMemory() const
Determine if the call does not access memory.
Definition: CallSite.h:216
bool isByValArgument(unsigned ArgNo) const
Determine whether this argument is passed by value.
Definition: CallSite.h:256
ImmutableCallSite(const InvokeInst *II)
Definition: CallSite.h:323
bool arg_empty() const
Definition: CallSite.h:144
bool doesNotCapture(unsigned ArgNo) const
Determine whether this argument is not captured.
Definition: CallSite.h:251
ImmutableCallSite(const CallInst *CI)
Definition: CallSite.h:322
Function only reads from memory.
Definition: Attributes.h:94
void setDoesNotThrow()
Definition: CallSite.h:243
Use & getUse() const
Definition: Use.h:210
void setCallingConv(CallingConv::ID CC)
Definition: CallSite.h:173
#define CALLSITE_DELEGATE_SETTER(METHOD)
Definition: CallSite.h:161
void setDoesNotAccessMemory()
Definition: CallSite.h:219
bool isCall() const
Definition: CallSite.h:73
#define LLVM_EXPLICIT
Expands to explicit on compilers which support explicit conversion operators. Otherwise expands to no...
Definition: Compiler.h:381
bool hasFnAttr(Attribute::AttrKind A) const
Return true if this function has the given attribute.
Definition: CallSite.h:187
ImmutableCallSite - establish a view to a call site for examination.
Definition: CallSite.h:318
void setAttributes(const AttributeSet &PAL)
Definition: CallSite.h:182
bool isNoInline() const
Return true if the call should not be inlined.
Definition: CallSite.h:208
bool paramHasAttr(unsigned i, Attribute::AttrKind A) const
Return true if the call or the callee has the given attribute.
Definition: CallSite.h:192
void setDoesNotReturn()
Definition: CallSite.h:235
CallSiteBase(InvokeTy *II)
Definition: CallSite.h:52
IterTy arg_begin() const
Definition: CallSite.h:137
bool operator<(const CallSite &CS) const
Definition: CallSite.h:309
CallSiteBase(ValTy *II)
Definition: CallSite.h:53
Module * getParent()
Definition: GlobalValue.h:286
LLVM Value Representation.
Definition: Value.h:66
bool doesNotAccessMemory(unsigned ArgNo) const
Definition: CallSite.h:260
FunTy * getCalledFunction() const
Definition: CallSite.h:93
const Use * const_op_iterator
Definition: User.h:114