Home | History | Annotate | Download | only in IR
      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 the call or the callee has the given attribute.
    361   bool paramHasAttr(unsigned i, Attribute::AttrKind Kind) const {
    362     CALLSITE_DELEGATE_GETTER(paramHasAttr(i, Kind));
    363   }
    364 
    365   Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const {
    366     CALLSITE_DELEGATE_GETTER(getAttribute(i, Kind));
    367   }
    368 
    369   Attribute getAttribute(unsigned i, StringRef Kind) const {
    370     CALLSITE_DELEGATE_GETTER(getAttribute(i, Kind));
    371   }
    372 
    373   /// Return true if the data operand at index \p i directly or indirectly has
    374   /// the attribute \p A.
    375   ///
    376   /// Normal call or invoke arguments have per operand attributes, as specified
    377   /// in the attribute set attached to this instruction, while operand bundle
    378   /// operands may have some attributes implied by the type of its containing
    379   /// operand bundle.
    380   bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const {
    381     CALLSITE_DELEGATE_GETTER(dataOperandHasImpliedAttr(i, Kind));
    382   }
    383 
    384   /// Extract the alignment for a call or parameter (0=unknown).
    385   uint16_t getParamAlignment(uint16_t i) const {
    386     CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
    387   }
    388 
    389   /// Extract the number of dereferenceable bytes for a call or parameter
    390   /// (0=unknown).
    391   uint64_t getDereferenceableBytes(uint16_t i) const {
    392     CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
    393   }
    394 
    395   /// Extract the number of dereferenceable_or_null bytes for a call or
    396   /// parameter (0=unknown).
    397   uint64_t getDereferenceableOrNullBytes(uint16_t i) const {
    398     CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i));
    399   }
    400 
    401   /// Determine if the parameter or return value is marked with NoAlias
    402   /// attribute.
    403   /// @param n The parameter to check. 1 is the first parameter, 0 is the return
    404   bool doesNotAlias(unsigned n) const {
    405     CALLSITE_DELEGATE_GETTER(doesNotAlias(n));
    406   }
    407 
    408   /// Return true if the call should not be treated as a call to a builtin.
    409   bool isNoBuiltin() const {
    410     CALLSITE_DELEGATE_GETTER(isNoBuiltin());
    411   }
    412 
    413   /// Return true if the call should not be inlined.
    414   bool isNoInline() const {
    415     CALLSITE_DELEGATE_GETTER(isNoInline());
    416   }
    417   void setIsNoInline(bool Value = true) {
    418     CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
    419   }
    420 
    421   /// Determine if the call does not access memory.
    422   bool doesNotAccessMemory() const {
    423     CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
    424   }
    425   void setDoesNotAccessMemory() {
    426     CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
    427   }
    428 
    429   /// Determine if the call does not access or only reads memory.
    430   bool onlyReadsMemory() const {
    431     CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
    432   }
    433   void setOnlyReadsMemory() {
    434     CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
    435   }
    436 
    437   /// Determine if the call does not access or only writes memory.
    438   bool doesNotReadMemory() const {
    439     CALLSITE_DELEGATE_GETTER(doesNotReadMemory());
    440   }
    441   void setDoesNotReadMemory() {
    442     CALLSITE_DELEGATE_SETTER(setDoesNotReadMemory());
    443   }
    444 
    445   /// Determine if the call can access memmory only using pointers based
    446   /// on its arguments.
    447   bool onlyAccessesArgMemory() const {
    448     CALLSITE_DELEGATE_GETTER(onlyAccessesArgMemory());
    449   }
    450   void setOnlyAccessesArgMemory() {
    451     CALLSITE_DELEGATE_SETTER(setOnlyAccessesArgMemory());
    452   }
    453 
    454   /// Determine if the call cannot return.
    455   bool doesNotReturn() const {
    456     CALLSITE_DELEGATE_GETTER(doesNotReturn());
    457   }
    458   void setDoesNotReturn() {
    459     CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
    460   }
    461 
    462   /// Determine if the call cannot unwind.
    463   bool doesNotThrow() const {
    464     CALLSITE_DELEGATE_GETTER(doesNotThrow());
    465   }
    466   void setDoesNotThrow() {
    467     CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
    468   }
    469 
    470   /// Determine if the call can be duplicated.
    471   bool cannotDuplicate() const {
    472     CALLSITE_DELEGATE_GETTER(cannotDuplicate());
    473   }
    474   void setCannotDuplicate() {
    475     CALLSITE_DELEGATE_GETTER(setCannotDuplicate());
    476   }
    477 
    478   /// Determine if the call is convergent.
    479   bool isConvergent() const {
    480     CALLSITE_DELEGATE_GETTER(isConvergent());
    481   }
    482   void setConvergent() {
    483     CALLSITE_DELEGATE_SETTER(setConvergent());
    484   }
    485   void setNotConvergent() {
    486     CALLSITE_DELEGATE_SETTER(setNotConvergent());
    487   }
    488 
    489   unsigned getNumOperandBundles() const {
    490     CALLSITE_DELEGATE_GETTER(getNumOperandBundles());
    491   }
    492 
    493   bool hasOperandBundles() const {
    494     CALLSITE_DELEGATE_GETTER(hasOperandBundles());
    495   }
    496 
    497   unsigned getBundleOperandsStartIndex() const {
    498     CALLSITE_DELEGATE_GETTER(getBundleOperandsStartIndex());
    499   }
    500 
    501   unsigned getBundleOperandsEndIndex() const {
    502     CALLSITE_DELEGATE_GETTER(getBundleOperandsEndIndex());
    503   }
    504 
    505   unsigned getNumTotalBundleOperands() const {
    506     CALLSITE_DELEGATE_GETTER(getNumTotalBundleOperands());
    507   }
    508 
    509   OperandBundleUse getOperandBundleAt(unsigned Index) const {
    510     CALLSITE_DELEGATE_GETTER(getOperandBundleAt(Index));
    511   }
    512 
    513   Optional<OperandBundleUse> getOperandBundle(StringRef Name) const {
    514     CALLSITE_DELEGATE_GETTER(getOperandBundle(Name));
    515   }
    516 
    517   Optional<OperandBundleUse> getOperandBundle(uint32_t ID) const {
    518     CALLSITE_DELEGATE_GETTER(getOperandBundle(ID));
    519   }
    520 
    521   unsigned countOperandBundlesOfType(uint32_t ID) const {
    522     CALLSITE_DELEGATE_GETTER(countOperandBundlesOfType(ID));
    523   }
    524 
    525   bool isBundleOperand(unsigned Idx) const {
    526     CALLSITE_DELEGATE_GETTER(isBundleOperand(Idx));
    527   }
    528 
    529   IterTy arg_begin() const {
    530     CALLSITE_DELEGATE_GETTER(arg_begin());
    531   }
    532 
    533   IterTy arg_end() const {
    534     CALLSITE_DELEGATE_GETTER(arg_end());
    535   }
    536 
    537 #undef CALLSITE_DELEGATE_GETTER
    538 #undef CALLSITE_DELEGATE_SETTER
    539 
    540   void getOperandBundlesAsDefs(SmallVectorImpl<OperandBundleDef> &Defs) const {
    541     const Instruction *II = getInstruction();
    542     // Since this is actually a getter that "looks like" a setter, don't use the
    543     // above macros to avoid confusion.
    544     if (isCall())
    545       cast<CallInst>(II)->getOperandBundlesAsDefs(Defs);
    546     else
    547       cast<InvokeInst>(II)->getOperandBundlesAsDefs(Defs);
    548   }
    549 
    550   /// Determine whether this data operand is not captured.
    551   bool doesNotCapture(unsigned OpNo) const {
    552     return dataOperandHasImpliedAttr(OpNo + 1, Attribute::NoCapture);
    553   }
    554 
    555   /// Determine whether this argument is passed by value.
    556   bool isByValArgument(unsigned ArgNo) const {
    557     return paramHasAttr(ArgNo + 1, Attribute::ByVal);
    558   }
    559 
    560   /// Determine whether this argument is passed in an alloca.
    561   bool isInAllocaArgument(unsigned ArgNo) const {
    562     return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
    563   }
    564 
    565   /// Determine whether this argument is passed by value or in an alloca.
    566   bool isByValOrInAllocaArgument(unsigned ArgNo) const {
    567     return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
    568            paramHasAttr(ArgNo + 1, Attribute::InAlloca);
    569   }
    570 
    571   /// Determine if there are is an inalloca argument. Only the last argument can
    572   /// have the inalloca attribute.
    573   bool hasInAllocaArgument() const {
    574     return paramHasAttr(arg_size(), Attribute::InAlloca);
    575   }
    576 
    577   bool doesNotAccessMemory(unsigned OpNo) const {
    578     return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
    579   }
    580 
    581   bool onlyReadsMemory(unsigned OpNo) const {
    582     return dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadOnly) ||
    583            dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
    584   }
    585 
    586   bool doesNotReadMemory(unsigned OpNo) const {
    587     return dataOperandHasImpliedAttr(OpNo + 1, Attribute::WriteOnly) ||
    588            dataOperandHasImpliedAttr(OpNo + 1, Attribute::ReadNone);
    589   }
    590 
    591   /// Return true if the return value is known to be not null.
    592   /// This may be because it has the nonnull attribute, or because at least
    593   /// one byte is dereferenceable and the pointer is in addrspace(0).
    594   bool isReturnNonNull() const {
    595     if (paramHasAttr(0, Attribute::NonNull))
    596       return true;
    597     else if (getDereferenceableBytes(0) > 0 &&
    598              getType()->getPointerAddressSpace() == 0)
    599       return true;
    600 
    601     return false;
    602   }
    603 
    604   /// Returns true if this CallSite passes the given Value* as an argument to
    605   /// the called function.
    606   bool hasArgument(const Value *Arg) const {
    607     for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
    608          ++AI)
    609       if (AI->get() == Arg)
    610         return true;
    611     return false;
    612   }
    613 
    614 private:
    615   IterTy getCallee() const {
    616     if (isCall()) // Skip Callee
    617       return cast<CallInst>(getInstruction())->op_end() - 1;
    618     else // Skip BB, BB, Callee
    619       return cast<InvokeInst>(getInstruction())->op_end() - 3;
    620   }
    621 };
    622 
    623 class CallSite : public CallSiteBase<Function, BasicBlock, Value, User, Use,
    624                                      Instruction, CallInst, InvokeInst,
    625                                      User::op_iterator> {
    626 public:
    627   CallSite() = default;
    628   CallSite(CallSiteBase B) : CallSiteBase(B) {}
    629   CallSite(CallInst *CI) : CallSiteBase(CI) {}
    630   CallSite(InvokeInst *II) : CallSiteBase(II) {}
    631   explicit CallSite(Instruction *II) : CallSiteBase(II) {}
    632   explicit CallSite(Value *V) : CallSiteBase(V) {}
    633 
    634   bool operator==(const CallSite &CS) const { return I == CS.I; }
    635   bool operator!=(const CallSite &CS) const { return I != CS.I; }
    636   bool operator<(const CallSite &CS) const {
    637     return getInstruction() < CS.getInstruction();
    638   }
    639 
    640 private:
    641   friend struct DenseMapInfo<CallSite>;
    642 
    643   User::op_iterator getCallee() const;
    644 };
    645 
    646 template <> struct DenseMapInfo<CallSite> {
    647   using BaseInfo = DenseMapInfo<decltype(CallSite::I)>;
    648 
    649   static CallSite getEmptyKey() {
    650     CallSite CS;
    651     CS.I = BaseInfo::getEmptyKey();
    652     return CS;
    653   }
    654 
    655   static CallSite getTombstoneKey() {
    656     CallSite CS;
    657     CS.I = BaseInfo::getTombstoneKey();
    658     return CS;
    659   }
    660 
    661   static unsigned getHashValue(const CallSite &CS) {
    662     return BaseInfo::getHashValue(CS.I);
    663   }
    664 
    665   static bool isEqual(const CallSite &LHS, const CallSite &RHS) {
    666     return LHS == RHS;
    667   }
    668 };
    669 
    670 /// Establish a view to a call site for examination.
    671 class ImmutableCallSite : public CallSiteBase<> {
    672 public:
    673   ImmutableCallSite() = default;
    674   ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
    675   ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
    676   explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
    677   explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
    678   ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
    679 };
    680 
    681 } // end namespace llvm
    682 
    683 #endif // LLVM_IR_CALLSITE_H
    684