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