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