[Verifier] Follow on to 240836
[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/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
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   ///
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.
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.
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.
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.
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(); }
158   iterator_range<IterTy> args() const {
159     return iterator_range<IterTy>(arg_begin(), arg_end());
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 {
197     CALLSITE_DELEGATE_GETTER(getNumArgOperands());
198   }
199
200   ValTy *getArgOperand(unsigned i) const { 
201     CALLSITE_DELEGATE_GETTER(getArgOperand(i));
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.
212   CallingConv::ID getCallingConv() const {
213     CALLSITE_DELEGATE_GETTER(getCallingConv());
214   }
215   void setCallingConv(CallingConv::ID CC) {
216     CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
217   }
218
219   FunctionType *getFunctionType() const {
220     CALLSITE_DELEGATE_GETTER(getFunctionType());
221   }
222
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.
229   const AttributeSet &getAttributes() const {
230     CALLSITE_DELEGATE_GETTER(getAttributes());
231   }
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.
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.
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).
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).
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).
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.
265   bool isNoBuiltin() const {
266     CALLSITE_DELEGATE_GETTER(isNoBuiltin());
267   }
268
269   /// @brief Return true if the call should not be inlined.
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.
278   bool doesNotAccessMemory() const {
279     CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
280   }
281   void setDoesNotAccessMemory() {
282     CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
283   }
284
285   /// @brief Determine if the call does not access or only reads memory.
286   bool onlyReadsMemory() const {
287     CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
288   }
289   void setOnlyReadsMemory() {
290     CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
291   }
292
293   /// @brief Determine if the call cannot return.
294   bool doesNotReturn() const {
295     CALLSITE_DELEGATE_GETTER(doesNotReturn());
296   }
297   void setDoesNotReturn() {
298     CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
299   }
300
301   /// @brief Determine if the call cannot unwind.
302   bool doesNotThrow() const {
303     CALLSITE_DELEGATE_GETTER(doesNotThrow());
304   }
305   void setDoesNotThrow() {
306     CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
307   }
308
309 #undef CALLSITE_DELEGATE_GETTER
310 #undef CALLSITE_DELEGATE_SETTER
311
312   /// @brief Determine whether this argument is not captured.
313   bool doesNotCapture(unsigned ArgNo) const {
314     return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
315   }
316
317   /// @brief Determine whether this argument is passed by value.
318   bool isByValArgument(unsigned ArgNo) const {
319     return paramHasAttr(ArgNo + 1, Attribute::ByVal);
320   }
321
322   /// @brief Determine whether this argument is passed in an alloca.
323   bool isInAllocaArgument(unsigned ArgNo) const {
324     return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
325   }
326
327   /// @brief Determine whether this argument is passed by value or in an alloca.
328   bool isByValOrInAllocaArgument(unsigned ArgNo) const {
329     return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
330            paramHasAttr(ArgNo + 1, Attribute::InAlloca);
331   }
332
333   /// @brief Determine if there are is an inalloca argument.  Only the last
334   /// argument can have the inalloca attribute.
335   bool hasInAllocaArgument() const {
336     return paramHasAttr(arg_size(), Attribute::InAlloca);
337   }
338
339   bool doesNotAccessMemory(unsigned ArgNo) const {
340     return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
341   }
342
343   bool onlyReadsMemory(unsigned ArgNo) const {
344     return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
345            paramHasAttr(ArgNo + 1, Attribute::ReadNone);
346   }
347
348   /// @brief Return true if the return value is known to be not null.
349   /// This may be because it has the nonnull attribute, or because at least
350   /// one byte is dereferenceable and the pointer is in addrspace(0).
351   bool isReturnNonNull() const {
352     if (paramHasAttr(0, Attribute::NonNull))
353       return true;
354     else if (getDereferenceableBytes(0) > 0 &&
355              getType()->getPointerAddressSpace() == 0)
356       return true;
357
358     return false;
359   }
360
361   /// hasArgument - Returns true if this CallSite passes the given Value* as an
362   /// argument to the called function.
363   bool hasArgument(const Value *Arg) const {
364     for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
365          ++AI)
366       if (AI->get() == Arg)
367         return true;
368     return false;
369   }
370
371 private:
372   unsigned getArgumentEndOffset() const {
373     if (isCall())
374       return 1; // Skip Callee
375     else
376       return 3; // Skip BB, BB, Callee
377   }
378
379   IterTy getCallee() const {
380     if (isCall()) // Skip Callee
381       return cast<CallInst>(getInstruction())->op_end() - 1;
382     else // Skip BB, BB, Callee
383       return cast<InvokeInst>(getInstruction())->op_end() - 3;
384   }
385 };
386
387 class CallSite : public CallSiteBase<Function, BasicBlock, Value, User,
388                                      Instruction, CallInst, InvokeInst,
389                                      User::op_iterator> {
390 public:
391   CallSite() {}
392   CallSite(CallSiteBase B) : CallSiteBase(B) {}
393   CallSite(CallInst *CI) : CallSiteBase(CI) {}
394   CallSite(InvokeInst *II) : CallSiteBase(II) {}
395   explicit CallSite(Instruction *II) : CallSiteBase(II) {}
396   explicit CallSite(Value *V) : CallSiteBase(V) {}
397
398   bool operator==(const CallSite &CS) const { return I == CS.I; }
399   bool operator!=(const CallSite &CS) const { return I != CS.I; }
400   bool operator<(const CallSite &CS) const {
401     return getInstruction() < CS.getInstruction();
402   }
403
404 private:
405   User::op_iterator getCallee() const;
406 };
407
408 /// ImmutableCallSite - establish a view to a call site for examination
409 class ImmutableCallSite : public CallSiteBase<> {
410 public:
411   ImmutableCallSite() {}
412   ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
413   ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
414   explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
415   explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
416   ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
417 };
418
419 } // End llvm namespace
420
421 #endif