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