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