]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/IR/CallSite.h
Merge clang trunk r300422 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / 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/iterator_range.h"
30 #include "llvm/ADT/Optional.h"
31 #include "llvm/ADT/PointerIntPair.h"
32 #include "llvm/IR/Attributes.h"
33 #include "llvm/IR/CallingConv.h"
34 #include "llvm/IR/Function.h"
35 #include "llvm/IR/InstrTypes.h"
36 #include "llvm/IR/Instruction.h"
37 #include "llvm/IR/Instructions.h"
38 #include "llvm/IR/Intrinsics.h"
39 #include "llvm/Support/Casting.h"
40 #include "llvm/IR/Use.h"
41 #include "llvm/IR/User.h"
42 #include "llvm/IR/Value.h"
43 #include <cassert>
44 #include <cstdint>
45 #include <iterator>
46
47 namespace llvm {
48
49 template <typename FunTy = const Function,
50           typename BBTy = const BasicBlock,
51           typename ValTy = const Value,
52           typename UserTy = const User,
53           typename UseTy = const Use,
54           typename InstrTy = const Instruction,
55           typename CallTy = const CallInst,
56           typename InvokeTy = const InvokeInst,
57           typename IterTy = User::const_op_iterator>
58 class CallSiteBase {
59 protected:
60   PointerIntPair<InstrTy*, 1, bool> I;
61
62   CallSiteBase() : I(nullptr, false) {}
63   CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); }
64   CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); }
65   explicit CallSiteBase(ValTy *II) { *this = get(II); }
66
67 private:
68   /// This static method is like a constructor. It will create an appropriate
69   /// call site for a Call or Invoke instruction, but it can also create a null
70   /// initialized CallSiteBase object for something which is NOT a call site.
71   static CallSiteBase get(ValTy *V) {
72     if (InstrTy *II = dyn_cast<InstrTy>(V)) {
73       if (II->getOpcode() == Instruction::Call)
74         return CallSiteBase(static_cast<CallTy*>(II));
75       else if (II->getOpcode() == Instruction::Invoke)
76         return CallSiteBase(static_cast<InvokeTy*>(II));
77     }
78     return CallSiteBase();
79   }
80
81 public:
82   /// Return true if a CallInst is enclosed. Note that !isCall() does not mean
83   /// an InvokeInst is enclosed. It may also signify a NULL instruction pointer.
84   bool isCall() const { return I.getInt(); }
85
86   /// Return true if a InvokeInst is enclosed.
87   bool isInvoke() const { return getInstruction() && !I.getInt(); }
88
89   InstrTy *getInstruction() const { return I.getPointer(); }
90   InstrTy *operator->() const { return I.getPointer(); }
91   explicit operator bool() const { return I.getPointer(); }
92
93   /// Get the basic block containing the call site.
94   BBTy* getParent() const { return getInstruction()->getParent(); }
95
96   /// Return the pointer to function that is being called.
97   ValTy *getCalledValue() const {
98     assert(getInstruction() && "Not a call or invoke instruction!");
99     return *getCallee();
100   }
101
102   /// Return the function being called if this is a direct call, otherwise
103   /// return null (if it's an indirect call).
104   FunTy *getCalledFunction() const {
105     return dyn_cast<FunTy>(getCalledValue());
106   }
107
108   /// Return true if the callsite is an indirect call.
109   bool isIndirectCall() const {
110     Value *V = getCalledValue();
111     if (!V)
112       return false;
113     if (isa<FunTy>(V) || isa<Constant>(V))
114       return false;
115     if (CallInst *CI = dyn_cast<CallInst>(getInstruction())) {
116       if (CI->isInlineAsm())
117         return false;
118     }
119     return true;
120   }
121
122   /// Set the callee to the specified value.
123   void setCalledFunction(Value *V) {
124     assert(getInstruction() && "Not a call or invoke instruction!");
125     *getCallee() = V;
126   }
127
128   /// Return the intrinsic ID of the intrinsic called by this CallSite,
129   /// or Intrinsic::not_intrinsic if the called function is not an
130   /// intrinsic, or if this CallSite is an indirect call.
131   Intrinsic::ID getIntrinsicID() const {
132     if (auto *F = getCalledFunction())
133       return F->getIntrinsicID();
134     // Don't use Intrinsic::not_intrinsic, as it will require pulling
135     // Intrinsics.h into every header that uses CallSite.
136     return static_cast<Intrinsic::ID>(0);
137   }
138
139   /// Determine whether the passed iterator points to the callee operand's Use.
140   bool isCallee(Value::const_user_iterator UI) const {
141     return isCallee(&UI.getUse());
142   }
143
144   /// Determine whether this Use is the callee operand's Use.
145   bool isCallee(const Use *U) const { return getCallee() == U; }
146
147   /// Determine whether the passed iterator points to an argument operand.
148   bool isArgOperand(Value::const_user_iterator UI) const {
149     return isArgOperand(&UI.getUse());
150   }
151
152   /// Determine whether the passed use points to an argument operand.
153   bool isArgOperand(const Use *U) const {
154     assert(getInstruction() == U->getUser());
155     return arg_begin() <= U && U < arg_end();
156   }
157
158   /// Determine whether the passed iterator points to a bundle operand.
159   bool isBundleOperand(Value::const_user_iterator UI) const {
160     return isBundleOperand(&UI.getUse());
161   }
162
163   /// Determine whether the passed use points to a bundle operand.
164   bool isBundleOperand(const Use *U) const {
165     assert(getInstruction() == U->getUser());
166     if (!hasOperandBundles())
167       return false;
168     unsigned OperandNo = U - (*this)->op_begin();
169     return getBundleOperandsStartIndex() <= OperandNo &&
170            OperandNo < getBundleOperandsEndIndex();
171   }
172
173   /// Determine whether the passed iterator points to a data operand.
174   bool isDataOperand(Value::const_user_iterator UI) const {
175     return isDataOperand(&UI.getUse());
176   }
177
178   /// Determine whether the passed use points to a data operand.
179   bool isDataOperand(const Use *U) const {
180     return data_operands_begin() <= U && U < data_operands_end();
181   }
182
183   ValTy *getArgument(unsigned ArgNo) const {
184     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
185     return *(arg_begin() + ArgNo);
186   }
187
188   void setArgument(unsigned ArgNo, Value* newVal) {
189     assert(getInstruction() && "Not a call or invoke instruction!");
190     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
191     getInstruction()->setOperand(ArgNo, newVal);
192   }
193
194   /// Given a value use iterator, returns the argument that corresponds to it.
195   /// Iterator must actually correspond to an argument.
196   unsigned getArgumentNo(Value::const_user_iterator I) const {
197     return getArgumentNo(&I.getUse());
198   }
199
200   /// Given a use for an argument, get the argument number that corresponds to
201   /// it.
202   unsigned getArgumentNo(const Use *U) const {
203     assert(getInstruction() && "Not a call or invoke instruction!");
204     assert(isArgOperand(U) && "Argument # out of range!");
205     return U - arg_begin();
206   }
207
208   /// The type of iterator to use when looping over actual arguments at this
209   /// call site.
210   typedef IterTy arg_iterator;
211
212   iterator_range<IterTy> args() const {
213     return make_range(arg_begin(), arg_end());
214   }
215   bool arg_empty() const { return arg_end() == arg_begin(); }
216   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
217
218   /// Given a value use iterator, return the data operand corresponding to it.
219   /// Iterator must actually correspond to a data operand.
220   unsigned getDataOperandNo(Value::const_user_iterator UI) const {
221     return getDataOperandNo(&UI.getUse());
222   }
223
224   /// Given a use for a data operand, get the data operand number that
225   /// corresponds to it.
226   unsigned getDataOperandNo(const Use *U) const {
227     assert(getInstruction() && "Not a call or invoke instruction!");
228     assert(isDataOperand(U) && "Data operand # out of range!");
229     return U - data_operands_begin();
230   }
231
232   /// Type of iterator to use when looping over data operands at this call site
233   /// (see below).
234   typedef IterTy data_operand_iterator;
235
236   /// data_operands_begin/data_operands_end - Return iterators iterating over
237   /// the call / invoke argument list and bundle operands.  For invokes, this is
238   /// the set of instruction operands except the invoke target and the two
239   /// successor blocks; and for calls this is the set of instruction operands
240   /// except the call target.
241
242   IterTy data_operands_begin() const {
243     assert(getInstruction() && "Not a call or invoke instruction!");
244     return (*this)->op_begin();
245   }
246   IterTy data_operands_end() const {
247     assert(getInstruction() && "Not a call or invoke instruction!");
248     return (*this)->op_end() - (isCall() ? 1 : 3);
249   }
250   iterator_range<IterTy> data_ops() const {
251     return make_range(data_operands_begin(), data_operands_end());
252   }
253   bool data_operands_empty() const {
254     return data_operands_end() == data_operands_begin();
255   }
256   unsigned data_operands_size() const {
257     return std::distance(data_operands_begin(), data_operands_end());
258   }
259
260   /// Return the type of the instruction that generated this call site.
261   Type *getType() const { return (*this)->getType(); }
262
263   /// Return the caller function for this call site.
264   FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
265
266   /// Tests if this call site must be tail call optimized. Only a CallInst can
267   /// be tail call optimized.
268   bool isMustTailCall() const {
269     return isCall() && cast<CallInst>(getInstruction())->isMustTailCall();
270   }
271
272   /// Tests if this call site is marked as a tail call.
273   bool isTailCall() const {
274     return isCall() && cast<CallInst>(getInstruction())->isTailCall();
275   }
276
277 #define CALLSITE_DELEGATE_GETTER(METHOD) \
278   InstrTy *II = getInstruction();    \
279   return isCall()                        \
280     ? cast<CallInst>(II)->METHOD         \
281     : cast<InvokeInst>(II)->METHOD
282
283 #define CALLSITE_DELEGATE_SETTER(METHOD) \
284   InstrTy *II = getInstruction();    \
285   if (isCall())                          \
286     cast<CallInst>(II)->METHOD;          \
287   else                                   \
288     cast<InvokeInst>(II)->METHOD
289
290   unsigned getNumArgOperands() const {
291     CALLSITE_DELEGATE_GETTER(getNumArgOperands());
292   }
293
294   ValTy *getArgOperand(unsigned i) const {
295     CALLSITE_DELEGATE_GETTER(getArgOperand(i));
296   }
297
298   ValTy *getReturnedArgOperand() const {
299     CALLSITE_DELEGATE_GETTER(getReturnedArgOperand());
300   }
301
302   bool isInlineAsm() const {
303     if (isCall())
304       return cast<CallInst>(getInstruction())->isInlineAsm();
305     return false;
306   }
307
308   /// Get the calling convention of the call.
309   CallingConv::ID getCallingConv() const {
310     CALLSITE_DELEGATE_GETTER(getCallingConv());
311   }
312   /// Set the calling convention of the call.
313   void setCallingConv(CallingConv::ID CC) {
314     CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
315   }
316
317   FunctionType *getFunctionType() const {
318     CALLSITE_DELEGATE_GETTER(getFunctionType());
319   }
320
321   void mutateFunctionType(FunctionType *Ty) const {
322     CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty));
323   }
324
325   /// Get the parameter attributes of the call.
326   AttributeList getAttributes() const {
327     CALLSITE_DELEGATE_GETTER(getAttributes());
328   }
329   /// Set the parameter attributes of the call.
330   void setAttributes(AttributeList PAL) {
331     CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
332   }
333
334   void addAttribute(unsigned i, Attribute::AttrKind Kind) {
335     CALLSITE_DELEGATE_SETTER(addAttribute(i, Kind));
336   }
337
338   void addAttribute(unsigned i, Attribute Attr) {
339     CALLSITE_DELEGATE_SETTER(addAttribute(i, Attr));
340   }
341
342   void removeAttribute(unsigned i, Attribute::AttrKind Kind) {
343     CALLSITE_DELEGATE_SETTER(removeAttribute(i, Kind));
344   }
345
346   void removeAttribute(unsigned i, StringRef Kind) {
347     CALLSITE_DELEGATE_SETTER(removeAttribute(i, Kind));
348   }
349
350   /// Return true if this function has the given attribute.
351   bool hasFnAttr(Attribute::AttrKind Kind) const {
352     CALLSITE_DELEGATE_GETTER(hasFnAttr(Kind));
353   }
354
355   /// Return true if this function has the given attribute.
356   bool hasFnAttr(StringRef Kind) const {
357     CALLSITE_DELEGATE_GETTER(hasFnAttr(Kind));
358   }
359
360   /// Return true if this return value has the given attribute.
361   bool hasRetAttr(Attribute::AttrKind Kind) const {
362     CALLSITE_DELEGATE_GETTER(hasRetAttr(Kind));
363   }
364
365   /// Return true if the call or the callee has the given attribute.
366   bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const {
367     CALLSITE_DELEGATE_GETTER(paramHasAttr(ArgNo, Kind));
368   }
369
370   Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
371     CALLSITE_DELEGATE_GETTER(getAttribute(i, Kind));
372   }
373
374   Attribute getAttribute(unsigned i, StringRef Kind) const {
375     CALLSITE_DELEGATE_GETTER(getAttribute(i, Kind));
376   }
377
378   /// Return true if the data operand at index \p i directly or indirectly has
379   /// the attribute \p A.
380   ///
381   /// Normal call or invoke arguments have per operand attributes, as specified
382   /// in the attribute set attached to this instruction, while operand bundle
383   /// operands may have some attributes implied by the type of its containing
384   /// operand bundle.
385   bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const {
386     CALLSITE_DELEGATE_GETTER(dataOperandHasImpliedAttr(i, Kind));
387   }
388
389   /// Extract the alignment for a call or parameter (0=unknown).
390   uint16_t getParamAlignment(uint16_t i) const {
391     CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
392   }
393
394   /// Extract the number of dereferenceable bytes for a call or parameter
395   /// (0=unknown).
396   uint64_t getDereferenceableBytes(uint16_t i) const {
397     CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
398   }
399
400   /// Extract the number of dereferenceable_or_null bytes for a call or
401   /// parameter (0=unknown).
402   uint64_t getDereferenceableOrNullBytes(uint16_t i) const {
403     CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i));
404   }
405
406   /// Determine if the parameter or return value is marked with NoAlias
407   /// attribute.
408   /// @param n The parameter to check. 1 is the first parameter, 0 is the return
409   bool doesNotAlias(unsigned n) const {
410     CALLSITE_DELEGATE_GETTER(doesNotAlias(n));
411   }
412
413   /// Return true if the call should not be treated as a call to a builtin.
414   bool isNoBuiltin() const {
415     CALLSITE_DELEGATE_GETTER(isNoBuiltin());
416   }
417
418   /// Return true if the call should not be inlined.
419   bool isNoInline() const {
420     CALLSITE_DELEGATE_GETTER(isNoInline());
421   }
422   void setIsNoInline(bool Value = true) {
423     CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
424   }
425
426   /// Determine if the call does not access memory.
427   bool doesNotAccessMemory() const {
428     CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
429   }
430   void setDoesNotAccessMemory() {
431     CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
432   }
433
434   /// Determine if the call does not access or only reads memory.
435   bool onlyReadsMemory() const {
436     CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
437   }
438   void setOnlyReadsMemory() {
439     CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
440   }
441
442   /// Determine if the call does not access or only writes memory.
443   bool doesNotReadMemory() const {
444     CALLSITE_DELEGATE_GETTER(doesNotReadMemory());
445   }
446   void setDoesNotReadMemory() {
447     CALLSITE_DELEGATE_SETTER(setDoesNotReadMemory());
448   }
449
450   /// Determine if the call can access memmory only using pointers based
451   /// on its arguments.
452   bool onlyAccessesArgMemory() const {
453     CALLSITE_DELEGATE_GETTER(onlyAccessesArgMemory());
454   }
455   void setOnlyAccessesArgMemory() {
456     CALLSITE_DELEGATE_SETTER(setOnlyAccessesArgMemory());
457   }
458
459   /// Determine if the call cannot return.
460   bool doesNotReturn() const {
461     CALLSITE_DELEGATE_GETTER(doesNotReturn());
462   }
463   void setDoesNotReturn() {
464     CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
465   }
466
467   /// Determine if the call cannot unwind.
468   bool doesNotThrow() const {
469     CALLSITE_DELEGATE_GETTER(doesNotThrow());
470   }
471   void setDoesNotThrow() {
472     CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
473   }
474
475   /// Determine if the call can be duplicated.
476   bool cannotDuplicate() const {
477     CALLSITE_DELEGATE_GETTER(cannotDuplicate());
478   }
479   void setCannotDuplicate() {
480     CALLSITE_DELEGATE_GETTER(setCannotDuplicate());
481   }
482
483   /// Determine if the call is convergent.
484   bool isConvergent() const {
485     CALLSITE_DELEGATE_GETTER(isConvergent());
486   }
487   void setConvergent() {
488     CALLSITE_DELEGATE_SETTER(setConvergent());
489   }
490   void setNotConvergent() {
491     CALLSITE_DELEGATE_SETTER(setNotConvergent());
492   }
493
494   unsigned getNumOperandBundles() const {
495     CALLSITE_DELEGATE_GETTER(getNumOperandBundles());
496   }
497
498   bool hasOperandBundles() const {
499     CALLSITE_DELEGATE_GETTER(hasOperandBundles());
500   }
501
502   unsigned getBundleOperandsStartIndex() const {
503     CALLSITE_DELEGATE_GETTER(getBundleOperandsStartIndex());
504   }
505
506   unsigned getBundleOperandsEndIndex() const {
507     CALLSITE_DELEGATE_GETTER(getBundleOperandsEndIndex());
508   }
509
510   unsigned getNumTotalBundleOperands() const {
511     CALLSITE_DELEGATE_GETTER(getNumTotalBundleOperands());
512   }
513
514   OperandBundleUse getOperandBundleAt(unsigned Index) const {
515     CALLSITE_DELEGATE_GETTER(getOperandBundleAt(Index));
516   }
517
518   Optional<OperandBundleUse> getOperandBundle(StringRef Name) const {
519     CALLSITE_DELEGATE_GETTER(getOperandBundle(Name));
520   }
521
522   Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const {
523     CALLSITE_DELEGATE_GETTER(getOperandBundle(ID));
524   }
525
526   unsigned countOperandBundlesOfType(uint32_t ID) const {
527     CALLSITE_DELEGATE_GETTER(countOperandBundlesOfType(ID));
528   }
529
530   bool isBundleOperand(unsigned Idx) const {
531     CALLSITE_DELEGATE_GETTER(isBundleOperand(Idx));
532   }
533
534   IterTy arg_begin() const {
535     CALLSITE_DELEGATE_GETTER(arg_begin());
536   }
537
538   IterTy arg_end() const {
539     CALLSITE_DELEGATE_GETTER(arg_end());
540   }
541
542 #undef CALLSITE_DELEGATE_GETTER
543 #undef CALLSITE_DELEGATE_SETTER
544
545   void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const {
546     const Instruction *II = getInstruction();
547     // Since this is actually a getter that "looks like" a setter, don't use the
548     // above macros to avoid confusion.
549     if (isCall())
550       cast<CallInst>(II)->getOperandBundlesAsDefs(Defs);
551     else
552       cast<InvokeInst>(II)->getOperandBundlesAsDefs(Defs);
553   }
554
555   /// Determine whether this data operand is not captured.
556   bool doesNotCapture(unsigned OpNo) const {
557     return dataOperandHasImpliedAttr(OpNo + 1, Attribute::NoCapture);
558   }
559
560   /// Determine whether this argument is passed by value.
561   bool isByValArgument(unsigned ArgNo) const {
562     return paramHasAttr(ArgNo, Attribute::ByVal);
563   }
564
565   /// Determine whether this argument is passed in an alloca.
566   bool isInAllocaArgument(unsigned ArgNo) const {
567     return paramHasAttr(ArgNo, Attribute::InAlloca);
568   }
569
570   /// Determine whether this argument is passed by value or in an alloca.
571   bool isByValOrInAllocaArgument(unsigned ArgNo) const {
572     return paramHasAttr(ArgNo, Attribute::ByVal) ||
573            paramHasAttr(ArgNo, Attribute::InAlloca);
574   }
575
576   /// Determine if there are is an inalloca argument. Only the last argument can
577   /// have the inalloca attribute.
578   bool hasInAllocaArgument() const {
579     return !arg_empty() && paramHasAttr(arg_size() - 1, Attribute::InAlloca);
580   }
581
582   bool doesNotAccessMemory(unsigned OpNo) const {
583     return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
584   }
585
586   bool onlyReadsMemory(unsigned OpNo) const {
587     return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadOnly) ||
588            dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
589   }
590
591   bool doesNotReadMemory(unsigned OpNo) const {
592     return dataOperandHasImpliedAttr(OpNo + 1, Attribute::WriteOnly) ||
593            dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
594   }
595
596   /// Return true if the return value is known to be not null.
597   /// This may be because it has the nonnull attribute, or because at least
598   /// one byte is dereferenceable and the pointer is in addrspace(0).
599   bool isReturnNonNull() const {
600     if (hasRetAttr(Attribute::NonNull))
601       return true;
602     else if (getDereferenceableBytes(0) > 0 &&
603              getType()->getPointerAddressSpace() == 0)
604       return true;
605
606     return false;
607   }
608
609   /// Returns true if this CallSite passes the given Value* as an argument to
610   /// the called function.
611   bool hasArgument(const Value *Arg) const {
612     for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
613          ++AI)
614       if (AI->get() == Arg)
615         return true;
616     return false;
617   }
618
619 private:
620   IterTy getCallee() const {
621     if (isCall()) // Skip Callee
622       return cast<CallInst>(getInstruction())->op_end() - 1;
623     else // Skip BB, BB, Callee
624       return cast<InvokeInst>(getInstruction())->op_end() - 3;
625   }
626 };
627
628 class CallSite : public CallSiteBase<Function, BasicBlock, Value, User, Use,
629                                      Instruction, CallInst, InvokeInst,
630                                      User::op_iterator> {
631 public:
632   CallSite() = default;
633   CallSite(CallSiteBase B) : CallSiteBase(B) {}
634   CallSite(CallInst *CI) : CallSiteBase(CI) {}
635   CallSite(InvokeInst *II) : CallSiteBase(II) {}
636   explicit CallSite(Instruction *II) : CallSiteBase(II) {}
637   explicit CallSite(Value *V) : CallSiteBase(V) {}
638
639   bool operator==(const CallSite &CS) const { return I == CS.I; }
640   bool operator!=(const CallSite &CS) const { return I != CS.I; }
641   bool operator<(const CallSite &CS) const {
642     return getInstruction() < CS.getInstruction();
643   }
644
645 private:
646   friend struct DenseMapInfo<CallSite>;
647
648   User::op_iterator getCallee() const;
649 };
650
651 template <> struct DenseMapInfo<CallSite> {
652   using BaseInfo = DenseMapInfo<decltype(CallSite::I)>;
653
654   static CallSite getEmptyKey() {
655     CallSite CS;
656     CS.I = BaseInfo::getEmptyKey();
657     return CS;
658   }
659
660   static CallSite getTombstoneKey() {
661     CallSite CS;
662     CS.I = BaseInfo::getTombstoneKey();
663     return CS;
664   }
665
666   static unsigned getHashValue(const CallSite &CS) {
667     return BaseInfo::getHashValue(CS.I);
668   }
669
670   static bool isEqual(const CallSite &LHS, const CallSite &RHS) {
671     return LHS == RHS;
672   }
673 };
674
675 /// Establish a view to a call site for examination.
676 class ImmutableCallSite : public CallSiteBase<> {
677 public:
678   ImmutableCallSite() = default;
679   ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
680   ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
681   explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
682   explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
683   ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
684 };
685
686 } // end namespace llvm
687
688 #endif // LLVM_IR_CALLSITE_H