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