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