xref: /NextBSD/contrib/llvm/include/llvm/IR/CallSite.h (revision 84d351007654069f9643c8e4b4802a7f5f08ee42)
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 
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"
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:
50   PointerIntPair<InstrTy*, 1, bool> I;
51 
CallSiteBase()52   CallSiteBase() : I(nullptr, false) {}
CallSiteBase(CallTy * CI)53   CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); }
CallSiteBase(InvokeTy * II)54   CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); }
CallSiteBase(ValTy * 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   ///
get(ValTy * V)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.
isCall()76   bool isCall() const { return I.getInt(); }
77 
78   /// isInvoke - true if a InvokeInst is enclosed.
79   ///
isInvoke()80   bool isInvoke() const { return getInstruction() && !I.getInt(); }
81 
getInstruction()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
getParent()87   BBTy* getParent() const { return getInstruction()->getParent(); }
88 
89   /// getCalledValue - Return the pointer to function that is being called.
90   ///
getCalledValue()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   ///
getCalledFunction()99   FunTy *getCalledFunction() const {
100     return dyn_cast<FunTy>(getCalledValue());
101   }
102 
103   /// setCalledFunction - Set the callee to the specified value.
104   ///
setCalledFunction(Value * V)105   void setCalledFunction(Value *V) {
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.
isCallee(Value::const_user_iterator UI)112   bool isCallee(Value::const_user_iterator UI) const {
113     return isCallee(&UI.getUse());
114   }
115 
116   /// Determine whether this Use is the callee operand's Use.
isCallee(const Use * U)117   bool isCallee(const Use *U) const { return getCallee() == U; }
118 
getArgument(unsigned ArgNo)119   ValTy *getArgument(unsigned ArgNo) const {
120     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
121     return *(arg_begin() + ArgNo);
122   }
123 
setArgument(unsigned ArgNo,Value * newVal)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.
getArgumentNo(Value::const_user_iterator I)132   unsigned getArgumentNo(Value::const_user_iterator I) const {
133     return getArgumentNo(&I.getUse());
134   }
135 
136   /// Given a use for an argument, get the argument number that corresponds to
137   /// it.
getArgumentNo(const Use * U)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.
arg_begin()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 
arg_end()157   IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
args()158   iterator_range<IterTy> args() const {
159     return iterator_range<IterTy>(arg_begin(), arg_end());
160   }
arg_empty()161   bool arg_empty() const { return arg_end() == arg_begin(); }
arg_size()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   ///
getType()166   Type *getType() const { return (*this)->getType(); }
167 
168   /// getCaller - Return the caller function for this call site
169   ///
getCaller()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.
isMustTailCall()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.
isTailCall()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 
getNumArgOperands()196   unsigned getNumArgOperands() const {
197     CALLSITE_DELEGATE_GETTER(getNumArgOperands());
198   }
199 
getArgOperand(unsigned i)200   ValTy *getArgOperand(unsigned i) const {
201     CALLSITE_DELEGATE_GETTER(getArgOperand(i));
202   }
203 
isInlineAsm()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.
getCallingConv()212   CallingConv::ID getCallingConv() const {
213     CALLSITE_DELEGATE_GETTER(getCallingConv());
214   }
setCallingConv(CallingConv::ID CC)215   void setCallingConv(CallingConv::ID CC) {
216     CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
217   }
218 
getFunctionType()219   FunctionType *getFunctionType() const {
220     CALLSITE_DELEGATE_GETTER(getFunctionType());
221   }
222 
mutateFunctionType(FunctionType * Ty)223   void mutateFunctionType(FunctionType *Ty) const {
224     CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty));
225   }
226 
227   /// getAttributes/setAttributes - get or set the parameter attributes of
228   /// the call.
getAttributes()229   const AttributeSet &getAttributes() const {
230     CALLSITE_DELEGATE_GETTER(getAttributes());
231   }
setAttributes(const AttributeSet & PAL)232   void setAttributes(const AttributeSet &PAL) {
233     CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
234   }
235 
236   /// \brief Return true if this function has the given attribute.
hasFnAttr(Attribute::AttrKind A)237   bool hasFnAttr(Attribute::AttrKind A) const {
238     CALLSITE_DELEGATE_GETTER(hasFnAttr(A));
239   }
240 
241   /// \brief Return true if the call or the callee has the given attribute.
paramHasAttr(unsigned i,Attribute::AttrKind A)242   bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
243     CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A));
244   }
245 
246   /// @brief Extract the alignment for a call or parameter (0=unknown).
getParamAlignment(uint16_t i)247   uint16_t getParamAlignment(uint16_t i) const {
248     CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
249   }
250 
251   /// @brief Extract the number of dereferenceable bytes for a call or
252   /// parameter (0=unknown).
getDereferenceableBytes(uint16_t i)253   uint64_t getDereferenceableBytes(uint16_t i) const {
254     CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
255   }
256 
257   /// @brief Extract the number of dereferenceable_or_null bytes for a call or
258   /// parameter (0=unknown).
getDereferenceableOrNullBytes(uint16_t i)259   uint64_t getDereferenceableOrNullBytes(uint16_t i) const {
260     CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i));
261   }
262 
263   /// \brief Return true if the call should not be treated as a call to a
264   /// builtin.
isNoBuiltin()265   bool isNoBuiltin() const {
266     CALLSITE_DELEGATE_GETTER(isNoBuiltin());
267   }
268 
269   /// @brief Return true if the call should not be inlined.
isNoInline()270   bool isNoInline() const {
271     CALLSITE_DELEGATE_GETTER(isNoInline());
272   }
273   void setIsNoInline(bool Value = true) {
274     CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
275   }
276 
277   /// @brief Determine if the call does not access memory.
doesNotAccessMemory()278   bool doesNotAccessMemory() const {
279     CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
280   }
setDoesNotAccessMemory()281   void setDoesNotAccessMemory() {
282     CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
283   }
284 
285   /// @brief Determine if the call does not access or only reads memory.
onlyReadsMemory()286   bool onlyReadsMemory() const {
287     CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
288   }
setOnlyReadsMemory()289   void setOnlyReadsMemory() {
290     CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
291   }
292 
293   /// @brief Determine if the call can access memmory only using pointers based
294   /// on its arguments.
onlyAccessesArgMemory()295   bool onlyAccessesArgMemory() const {
296     CALLSITE_DELEGATE_GETTER(onlyAccessesArgMemory());
297   }
setOnlyAccessesArgMemory()298   void setOnlyAccessesArgMemory() {
299     CALLSITE_DELEGATE_SETTER(setOnlyAccessesArgMemory());
300   }
301 
302   /// @brief Determine if the call cannot return.
doesNotReturn()303   bool doesNotReturn() const {
304     CALLSITE_DELEGATE_GETTER(doesNotReturn());
305   }
setDoesNotReturn()306   void setDoesNotReturn() {
307     CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
308   }
309 
310   /// @brief Determine if the call cannot unwind.
doesNotThrow()311   bool doesNotThrow() const {
312     CALLSITE_DELEGATE_GETTER(doesNotThrow());
313   }
setDoesNotThrow()314   void setDoesNotThrow() {
315     CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
316   }
317 
318 #undef CALLSITE_DELEGATE_GETTER
319 #undef CALLSITE_DELEGATE_SETTER
320 
321   /// @brief Determine whether this argument is not captured.
doesNotCapture(unsigned ArgNo)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.
isByValArgument(unsigned ArgNo)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.
isInAllocaArgument(unsigned ArgNo)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.
isByValOrInAllocaArgument(unsigned ArgNo)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.
hasInAllocaArgument()344   bool hasInAllocaArgument() const {
345     return paramHasAttr(arg_size(), Attribute::InAlloca);
346   }
347 
doesNotAccessMemory(unsigned ArgNo)348   bool doesNotAccessMemory(unsigned ArgNo) const {
349     return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
350   }
351 
onlyReadsMemory(unsigned ArgNo)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).
isReturnNonNull()360   bool isReturnNonNull() const {
361     if (paramHasAttr(0, Attribute::NonNull))
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.
hasArgument(const Value * Arg)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:
getArgumentEndOffset()381   unsigned getArgumentEndOffset() const {
382     if (isCall())
383       return 1; // Skip Callee
384     else
385       return 3; // Skip BB, BB, Callee
386   }
387 
getCallee()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:
CallSite()400   CallSite() {}
CallSite(CallSiteBase B)401   CallSite(CallSiteBase B) : CallSiteBase(B) {}
CallSite(CallInst * CI)402   CallSite(CallInst *CI) : CallSiteBase(CI) {}
CallSite(InvokeInst * II)403   CallSite(InvokeInst *II) : CallSiteBase(II) {}
CallSite(Instruction * II)404   explicit CallSite(Instruction *II) : CallSiteBase(II) {}
CallSite(Value * V)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:
ImmutableCallSite()420   ImmutableCallSite() {}
ImmutableCallSite(const CallInst * CI)421   ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
ImmutableCallSite(const InvokeInst * II)422   ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
ImmutableCallSite(const Instruction * II)423   explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
ImmutableCallSite(const Value * V)424   explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
ImmutableCallSite(CallSite CS)425   ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
426 };
427 
428 } // End llvm namespace
429 
430 #endif
431