[opaque pointer type] Avoid using PointerType::getElementType for a few cases of...
[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 Return true if the call should not be treated as a call to a
236   /// builtin.
237   bool isNoBuiltin() const {
238     CALLSITE_DELEGATE_GETTER(isNoBuiltin());
239   }
240
241   /// @brief Return true if the call should not be inlined.
242   bool isNoInline() const {
243     CALLSITE_DELEGATE_GETTER(isNoInline());
244   }
245   void setIsNoInline(bool Value = true) {
246     CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
247   }
248
249   /// @brief Determine if the call does not access memory.
250   bool doesNotAccessMemory() const {
251     CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
252   }
253   void setDoesNotAccessMemory() {
254     CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
255   }
256
257   /// @brief Determine if the call does not access or only reads memory.
258   bool onlyReadsMemory() const {
259     CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
260   }
261   void setOnlyReadsMemory() {
262     CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
263   }
264
265   /// @brief Determine if the call cannot return.
266   bool doesNotReturn() const {
267     CALLSITE_DELEGATE_GETTER(doesNotReturn());
268   }
269   void setDoesNotReturn() {
270     CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
271   }
272
273   /// @brief Determine if the call cannot unwind.
274   bool doesNotThrow() const {
275     CALLSITE_DELEGATE_GETTER(doesNotThrow());
276   }
277   void setDoesNotThrow() {
278     CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
279   }
280
281 #undef CALLSITE_DELEGATE_GETTER
282 #undef CALLSITE_DELEGATE_SETTER
283
284   /// @brief Determine whether this argument is not captured.
285   bool doesNotCapture(unsigned ArgNo) const {
286     return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
287   }
288
289   /// @brief Determine whether this argument is passed by value.
290   bool isByValArgument(unsigned ArgNo) const {
291     return paramHasAttr(ArgNo + 1, Attribute::ByVal);
292   }
293
294   /// @brief Determine whether this argument is passed in an alloca.
295   bool isInAllocaArgument(unsigned ArgNo) const {
296     return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
297   }
298
299   /// @brief Determine whether this argument is passed by value or in an alloca.
300   bool isByValOrInAllocaArgument(unsigned ArgNo) const {
301     return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
302            paramHasAttr(ArgNo + 1, Attribute::InAlloca);
303   }
304
305   /// @brief Determine if there are is an inalloca argument.  Only the last
306   /// argument can have the inalloca attribute.
307   bool hasInAllocaArgument() const {
308     return paramHasAttr(arg_size(), Attribute::InAlloca);
309   }
310
311   bool doesNotAccessMemory(unsigned ArgNo) const {
312     return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
313   }
314
315   bool onlyReadsMemory(unsigned ArgNo) const {
316     return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
317            paramHasAttr(ArgNo + 1, Attribute::ReadNone);
318   }
319
320   /// @brief Return true if the return value is known to be not null.
321   /// This may be because it has the nonnull attribute, or because at least
322   /// one byte is dereferenceable and the pointer is in addrspace(0).
323   bool isReturnNonNull() const {
324     if (paramHasAttr(0, Attribute::NonNull))
325       return true;
326     else if (getDereferenceableBytes(0) > 0 &&
327              getType()->getPointerAddressSpace() == 0)
328       return true;
329
330     return false;
331   }
332
333   /// hasArgument - Returns true if this CallSite passes the given Value* as an
334   /// argument to the called function.
335   bool hasArgument(const Value *Arg) const {
336     for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
337          ++AI)
338       if (AI->get() == Arg)
339         return true;
340     return false;
341   }
342
343 private:
344   unsigned getArgumentEndOffset() const {
345     if (isCall())
346       return 1; // Skip Callee
347     else
348       return 3; // Skip BB, BB, Callee
349   }
350
351   IterTy getCallee() const {
352     if (isCall()) // Skip Callee
353       return cast<CallInst>(getInstruction())->op_end() - 1;
354     else // Skip BB, BB, Callee
355       return cast<InvokeInst>(getInstruction())->op_end() - 3;
356   }
357 };
358
359 class CallSite : public CallSiteBase<Function, Value, User, Instruction,
360                                      CallInst, InvokeInst, User::op_iterator> {
361 public:
362   CallSite() {}
363   CallSite(CallSiteBase B) : CallSiteBase(B) {}
364   CallSite(CallInst *CI) : CallSiteBase(CI) {}
365   CallSite(InvokeInst *II) : CallSiteBase(II) {}
366   explicit CallSite(Instruction *II) : CallSiteBase(II) {}
367   explicit CallSite(Value *V) : CallSiteBase(V) {}
368
369   bool operator==(const CallSite &CS) const { return I == CS.I; }
370   bool operator!=(const CallSite &CS) const { return I != CS.I; }
371   bool operator<(const CallSite &CS) const {
372     return getInstruction() < CS.getInstruction();
373   }
374
375 private:
376   User::op_iterator getCallee() const;
377 };
378
379 /// ImmutableCallSite - establish a view to a call site for examination
380 class ImmutableCallSite : public CallSiteBase<> {
381 public:
382   ImmutableCallSite() {}
383   ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
384   ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
385   explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
386   explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
387   ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
388 };
389
390 } // End llvm namespace
391
392 #endif