Add missing dereferenceable_or_null getters
[oota-llvm.git] / include / llvm / IR / CallSite.h
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/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:
48   PointerIntPair<InstrTy*, 1, bool> I;
49
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); }
54
55 private:
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.
60   ///
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));
67     }
68     return CallSiteBase();
69   }
70 public:
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(); }
75
76   /// isInvoke - true if a InvokeInst is enclosed.
77   ///
78   bool isInvoke() const { return getInstruction() && !I.getInt(); }
79
80   InstrTy *getInstruction() const { return I.getPointer(); }
81   InstrTy *operator->() const { return I.getPointer(); }
82   explicit operator bool() const { return I.getPointer(); }
83
84   /// getCalledValue - Return the pointer to function that is being called.
85   ///
86   ValTy *getCalledValue() const {
87     assert(getInstruction() && "Not a call or invoke instruction!");
88     return *getCallee();
89   }
90
91   /// getCalledFunction - Return the function being called if this is a direct
92   /// call, otherwise return null (if it's an indirect call).
93   ///
94   FunTy *getCalledFunction() const {
95     return dyn_cast<FunTy>(getCalledValue());
96   }
97
98   /// setCalledFunction - Set the callee to the specified value.
99   ///
100   void setCalledFunction(Value *V) {
101     assert(getInstruction() && "Not a call or invoke instruction!");
102     *getCallee() = V;
103   }
104
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());
109   }
110
111   /// Determine whether this Use is the callee operand's Use.
112   bool isCallee(const Use *U) const { return getCallee() == U; }
113
114   ValTy *getArgument(unsigned ArgNo) const {
115     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
116     return *(arg_begin() + ArgNo);
117   }
118
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);
123   }
124
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());
129   }
130
131   /// Given a use for an argument, get the argument number that corresponds to
132   /// it.
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();
138   }
139
140   /// arg_iterator - The type of iterator to use when looping over actual
141   /// arguments at this call site.
142   typedef IterTy arg_iterator;
143
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();
150   }
151
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()); }
155
156   /// getType - Return the type of the instruction that generated this call site
157   ///
158   Type *getType() const { return (*this)->getType(); }
159
160   /// getCaller - Return the caller function for this call site
161   ///
162   FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
163
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();
168   }
169
170   /// \brief Tests if this call site is marked as a tail call.
171   bool isTailCall() const {
172     return isCall() && cast<CallInst>(getInstruction())->isTailCall();
173   }
174
175 #define CALLSITE_DELEGATE_GETTER(METHOD) \
176   InstrTy *II = getInstruction();    \
177   return isCall()                        \
178     ? cast<CallInst>(II)->METHOD         \
179     : cast<InvokeInst>(II)->METHOD
180
181 #define CALLSITE_DELEGATE_SETTER(METHOD) \
182   InstrTy *II = getInstruction();    \
183   if (isCall())                          \
184     cast<CallInst>(II)->METHOD;          \
185   else                                   \
186     cast<InvokeInst>(II)->METHOD
187
188   /// getCallingConv/setCallingConv - get or set the calling convention of the
189   /// call.
190   CallingConv::ID getCallingConv() const {
191     CALLSITE_DELEGATE_GETTER(getCallingConv());
192   }
193   void setCallingConv(CallingConv::ID CC) {
194     CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
195   }
196
197   FunctionType *getFunctionType() const {
198     CALLSITE_DELEGATE_GETTER(getFunctionType());
199   }
200
201   void mutateFunctionType(FunctionType *Ty) const {
202     CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty));
203   }
204
205   /// getAttributes/setAttributes - get or set the parameter attributes of
206   /// the call.
207   const AttributeSet &getAttributes() const {
208     CALLSITE_DELEGATE_GETTER(getAttributes());
209   }
210   void setAttributes(const AttributeSet &PAL) {
211     CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
212   }
213
214   /// \brief Return true if this function has the given attribute.
215   bool hasFnAttr(Attribute::AttrKind A) const {
216     CALLSITE_DELEGATE_GETTER(hasFnAttr(A));
217   }
218
219   /// \brief Return true if the call or the callee has the given attribute.
220   bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
221     CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A));
222   }
223
224   /// @brief Extract the alignment for a call or parameter (0=unknown).
225   uint16_t getParamAlignment(uint16_t i) const {
226     CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
227   }
228
229   /// @brief Extract the number of dereferenceable bytes for a call or
230   /// parameter (0=unknown).
231   uint64_t getDereferenceableBytes(uint16_t i) const {
232     CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
233   }
234   
235   /// @brief Extract the number of dereferenceable_or_null bytes for a call or
236   /// parameter (0=unknown).
237   uint64_t getDereferenceableOrNullBytes(uint16_t i) const {
238     CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i));
239   }
240   
241   /// \brief Return true if the call should not be treated as a call to a
242   /// builtin.
243   bool isNoBuiltin() const {
244     CALLSITE_DELEGATE_GETTER(isNoBuiltin());
245   }
246
247   /// @brief Return true if the call should not be inlined.
248   bool isNoInline() const {
249     CALLSITE_DELEGATE_GETTER(isNoInline());
250   }
251   void setIsNoInline(bool Value = true) {
252     CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
253   }
254
255   /// @brief Determine if the call does not access memory.
256   bool doesNotAccessMemory() const {
257     CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
258   }
259   void setDoesNotAccessMemory() {
260     CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
261   }
262
263   /// @brief Determine if the call does not access or only reads memory.
264   bool onlyReadsMemory() const {
265     CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
266   }
267   void setOnlyReadsMemory() {
268     CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
269   }
270
271   /// @brief Determine if the call cannot return.
272   bool doesNotReturn() const {
273     CALLSITE_DELEGATE_GETTER(doesNotReturn());
274   }
275   void setDoesNotReturn() {
276     CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
277   }
278
279   /// @brief Determine if the call cannot unwind.
280   bool doesNotThrow() const {
281     CALLSITE_DELEGATE_GETTER(doesNotThrow());
282   }
283   void setDoesNotThrow() {
284     CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
285   }
286
287 #undef CALLSITE_DELEGATE_GETTER
288 #undef CALLSITE_DELEGATE_SETTER
289
290   /// @brief Determine whether this argument is not captured.
291   bool doesNotCapture(unsigned ArgNo) const {
292     return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
293   }
294
295   /// @brief Determine whether this argument is passed by value.
296   bool isByValArgument(unsigned ArgNo) const {
297     return paramHasAttr(ArgNo + 1, Attribute::ByVal);
298   }
299
300   /// @brief Determine whether this argument is passed in an alloca.
301   bool isInAllocaArgument(unsigned ArgNo) const {
302     return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
303   }
304
305   /// @brief Determine whether this argument is passed by value or in an alloca.
306   bool isByValOrInAllocaArgument(unsigned ArgNo) const {
307     return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
308            paramHasAttr(ArgNo + 1, Attribute::InAlloca);
309   }
310
311   /// @brief Determine if there are is an inalloca argument.  Only the last
312   /// argument can have the inalloca attribute.
313   bool hasInAllocaArgument() const {
314     return paramHasAttr(arg_size(), Attribute::InAlloca);
315   }
316
317   bool doesNotAccessMemory(unsigned ArgNo) const {
318     return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
319   }
320
321   bool onlyReadsMemory(unsigned ArgNo) const {
322     return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
323            paramHasAttr(ArgNo + 1, Attribute::ReadNone);
324   }
325
326   /// @brief Return true if the return value is known to be not null.
327   /// This may be because it has the nonnull attribute, or because at least
328   /// one byte is dereferenceable and the pointer is in addrspace(0).
329   bool isReturnNonNull() const {
330     if (paramHasAttr(0, Attribute::NonNull))
331       return true;
332     else if (getDereferenceableBytes(0) > 0 &&
333              getType()->getPointerAddressSpace() == 0)
334       return true;
335
336     return false;
337   }
338
339   /// hasArgument - Returns true if this CallSite passes the given Value* as an
340   /// argument to the called function.
341   bool hasArgument(const Value *Arg) const {
342     for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
343          ++AI)
344       if (AI->get() == Arg)
345         return true;
346     return false;
347   }
348
349 private:
350   unsigned getArgumentEndOffset() const {
351     if (isCall())
352       return 1; // Skip Callee
353     else
354       return 3; // Skip BB, BB, Callee
355   }
356
357   IterTy getCallee() const {
358     if (isCall()) // Skip Callee
359       return cast<CallInst>(getInstruction())->op_end() - 1;
360     else // Skip BB, BB, Callee
361       return cast<InvokeInst>(getInstruction())->op_end() - 3;
362   }
363 };
364
365 class CallSite : public CallSiteBase<Function, Value, User, Instruction,
366                                      CallInst, InvokeInst, User::op_iterator> {
367 public:
368   CallSite() {}
369   CallSite(CallSiteBase B) : CallSiteBase(B) {}
370   CallSite(CallInst *CI) : CallSiteBase(CI) {}
371   CallSite(InvokeInst *II) : CallSiteBase(II) {}
372   explicit CallSite(Instruction *II) : CallSiteBase(II) {}
373   explicit CallSite(Value *V) : CallSiteBase(V) {}
374
375   bool operator==(const CallSite &CS) const { return I == CS.I; }
376   bool operator!=(const CallSite &CS) const { return I != CS.I; }
377   bool operator<(const CallSite &CS) const {
378     return getInstruction() < CS.getInstruction();
379   }
380
381 private:
382   User::op_iterator getCallee() const;
383 };
384
385 /// ImmutableCallSite - establish a view to a call site for examination
386 class ImmutableCallSite : public CallSiteBase<> {
387 public:
388   ImmutableCallSite() {}
389   ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
390   ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
391   explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
392   explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
393   ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
394 };
395
396 } // End llvm namespace
397
398 #endif