LLVM  mainline
CallSite.h
Go to the documentation of this file.
00001 //===- CallSite.h - Abstract Call & Invoke instrs ---------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines the CallSite class, which is a handy wrapper for code that
00011 // wants to treat Call and Invoke instructions in a generic way. When in non-
00012 // mutation context (e.g. an analysis) ImmutableCallSite should be used.
00013 // Finally, when some degree of customization is necessary between these two
00014 // extremes, CallSiteBase<> can be supplied with fine-tuned parameters.
00015 //
00016 // NOTE: These classes are supposed to have "value semantics". So they should be
00017 // passed by value, not by reference; they should not be "new"ed or "delete"d.
00018 // They are efficiently copyable, assignable and constructable, with cost
00019 // equivalent to copying a pointer (notice that they have only a single data
00020 // member). The internal representation carries a flag which indicates which of
00021 // the two variants is enclosed. This allows for cheaper checks when various
00022 // accessors of CallSite are employed.
00023 //
00024 //===----------------------------------------------------------------------===//
00025 
00026 #ifndef LLVM_IR_CALLSITE_H
00027 #define LLVM_IR_CALLSITE_H
00028 
00029 #include "llvm/ADT/PointerIntPair.h"
00030 #include "llvm/IR/Attributes.h"
00031 #include "llvm/IR/CallingConv.h"
00032 #include "llvm/IR/Instructions.h"
00033 
00034 namespace llvm {
00035 
00036 class CallInst;
00037 class InvokeInst;
00038 
00039 template <typename FunTy = const Function,
00040           typename ValTy = const Value,
00041           typename UserTy = const User,
00042           typename InstrTy = const Instruction,
00043           typename CallTy = const CallInst,
00044           typename InvokeTy = const InvokeInst,
00045           typename IterTy = User::const_op_iterator>
00046 class CallSiteBase {
00047 protected:
00048   PointerIntPair<InstrTy*, 1, bool> I;
00049 
00050   CallSiteBase() : I(nullptr, false) {}
00051   CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); }
00052   CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); }
00053   explicit CallSiteBase(ValTy *II) { *this = get(II); }
00054 
00055 private:
00056   /// CallSiteBase::get - This static method is sort of like a constructor.  It
00057   /// will create an appropriate call site for a Call or Invoke instruction, but
00058   /// it can also create a null initialized CallSiteBase object for something
00059   /// which is NOT a call site.
00060   ///
00061   static CallSiteBase get(ValTy *V) {
00062     if (InstrTy *II = dyn_cast<InstrTy>(V)) {
00063       if (II->getOpcode() == Instruction::Call)
00064         return CallSiteBase(static_cast<CallTy*>(II));
00065       else if (II->getOpcode() == Instruction::Invoke)
00066         return CallSiteBase(static_cast<InvokeTy*>(II));
00067     }
00068     return CallSiteBase();
00069   }
00070 public:
00071   /// isCall - true if a CallInst is enclosed.
00072   /// Note that !isCall() does not mean it is an InvokeInst enclosed,
00073   /// it also could signify a NULL Instruction pointer.
00074   bool isCall() const { return I.getInt(); }
00075 
00076   /// isInvoke - true if a InvokeInst is enclosed.
00077   ///
00078   bool isInvoke() const { return getInstruction() && !I.getInt(); }
00079 
00080   InstrTy *getInstruction() const { return I.getPointer(); }
00081   InstrTy *operator->() const { return I.getPointer(); }
00082   explicit operator bool() const { return I.getPointer(); }
00083 
00084   /// getCalledValue - Return the pointer to function that is being called.
00085   ///
00086   ValTy *getCalledValue() const {
00087     assert(getInstruction() && "Not a call or invoke instruction!");
00088     return *getCallee();
00089   }
00090 
00091   /// getCalledFunction - Return the function being called if this is a direct
00092   /// call, otherwise return null (if it's an indirect call).
00093   ///
00094   FunTy *getCalledFunction() const {
00095     return dyn_cast<FunTy>(getCalledValue());
00096   }
00097 
00098   /// setCalledFunction - Set the callee to the specified value.
00099   ///
00100   void setCalledFunction(Value *V) {
00101     assert(getInstruction() && "Not a call or invoke instruction!");
00102     *getCallee() = V;
00103   }
00104 
00105   /// isCallee - Determine whether the passed iterator points to the
00106   /// callee operand's Use.
00107   bool isCallee(Value::const_user_iterator UI) const {
00108     return isCallee(&UI.getUse());
00109   }
00110 
00111   /// Determine whether this Use is the callee operand's Use.
00112   bool isCallee(const Use *U) const { return getCallee() == U; }
00113 
00114   ValTy *getArgument(unsigned ArgNo) const {
00115     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
00116     return *(arg_begin() + ArgNo);
00117   }
00118 
00119   void setArgument(unsigned ArgNo, Value* newVal) {
00120     assert(getInstruction() && "Not a call or invoke instruction!");
00121     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
00122     getInstruction()->setOperand(ArgNo, newVal);
00123   }
00124 
00125   /// Given a value use iterator, returns the argument that corresponds to it.
00126   /// Iterator must actually correspond to an argument.
00127   unsigned getArgumentNo(Value::const_user_iterator I) const {
00128     return getArgumentNo(&I.getUse());
00129   }
00130 
00131   /// Given a use for an argument, get the argument number that corresponds to
00132   /// it.
00133   unsigned getArgumentNo(const Use *U) const {
00134     assert(getInstruction() && "Not a call or invoke instruction!");
00135     assert(arg_begin() <= U && U < arg_end()
00136            && "Argument # out of range!");
00137     return U - arg_begin();
00138   }
00139 
00140   /// arg_iterator - The type of iterator to use when looping over actual
00141   /// arguments at this call site.
00142   typedef IterTy arg_iterator;
00143 
00144   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
00145   /// list for a call site.
00146   IterTy arg_begin() const {
00147     assert(getInstruction() && "Not a call or invoke instruction!");
00148     // Skip non-arguments
00149     return (*this)->op_begin();
00150   }
00151 
00152   IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
00153   bool arg_empty() const { return arg_end() == arg_begin(); }
00154   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
00155 
00156   /// getType - Return the type of the instruction that generated this call site
00157   ///
00158   Type *getType() const { return (*this)->getType(); }
00159 
00160   /// getCaller - Return the caller function for this call site
00161   ///
00162   FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
00163 
00164   /// \brief Tests if this call site must be tail call optimized.  Only a
00165   /// CallInst can be tail call optimized.
00166   bool isMustTailCall() const {
00167     return isCall() && cast<CallInst>(getInstruction())->isMustTailCall();
00168   }
00169 
00170   /// \brief Tests if this call site is marked as a tail call.
00171   bool isTailCall() const {
00172     return isCall() && cast<CallInst>(getInstruction())->isTailCall();
00173   }
00174 
00175 #define CALLSITE_DELEGATE_GETTER(METHOD) \
00176   InstrTy *II = getInstruction();    \
00177   return isCall()                        \
00178     ? cast<CallInst>(II)->METHOD         \
00179     : cast<InvokeInst>(II)->METHOD
00180 
00181 #define CALLSITE_DELEGATE_SETTER(METHOD) \
00182   InstrTy *II = getInstruction();    \
00183   if (isCall())                          \
00184     cast<CallInst>(II)->METHOD;          \
00185   else                                   \
00186     cast<InvokeInst>(II)->METHOD
00187 
00188   /// getCallingConv/setCallingConv - get or set the calling convention of the
00189   /// call.
00190   CallingConv::ID getCallingConv() const {
00191     CALLSITE_DELEGATE_GETTER(getCallingConv());
00192   }
00193   void setCallingConv(CallingConv::ID CC) {
00194     CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
00195   }
00196 
00197   FunctionType *getFunctionType() const {
00198     CALLSITE_DELEGATE_GETTER(getFunctionType());
00199   }
00200 
00201   void mutateFunctionType(FunctionType *Ty) const {
00202     CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty));
00203   }
00204 
00205   /// getAttributes/setAttributes - get or set the parameter attributes of
00206   /// the call.
00207   const AttributeSet &getAttributes() const {
00208     CALLSITE_DELEGATE_GETTER(getAttributes());
00209   }
00210   void setAttributes(const AttributeSet &PAL) {
00211     CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
00212   }
00213 
00214   /// \brief Return true if this function has the given attribute.
00215   bool hasFnAttr(Attribute::AttrKind A) const {
00216     CALLSITE_DELEGATE_GETTER(hasFnAttr(A));
00217   }
00218 
00219   /// \brief Return true if the call or the callee has the given attribute.
00220   bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
00221     CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A));
00222   }
00223 
00224   /// @brief Extract the alignment for a call or parameter (0=unknown).
00225   uint16_t getParamAlignment(uint16_t i) const {
00226     CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
00227   }
00228 
00229   /// @brief Extract the number of dereferenceable bytes for a call or
00230   /// parameter (0=unknown).
00231   uint64_t getDereferenceableBytes(uint16_t i) const {
00232     CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
00233   }
00234   
00235   /// @brief Extract the number of dereferenceable_or_null bytes for a call or
00236   /// parameter (0=unknown).
00237   uint64_t getDereferenceableOrNullBytes(uint16_t i) const {
00238     CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i));
00239   }
00240   
00241   /// \brief Return true if the call should not be treated as a call to a
00242   /// builtin.
00243   bool isNoBuiltin() const {
00244     CALLSITE_DELEGATE_GETTER(isNoBuiltin());
00245   }
00246 
00247   /// @brief Return true if the call should not be inlined.
00248   bool isNoInline() const {
00249     CALLSITE_DELEGATE_GETTER(isNoInline());
00250   }
00251   void setIsNoInline(bool Value = true) {
00252     CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
00253   }
00254 
00255   /// @brief Determine if the call does not access memory.
00256   bool doesNotAccessMemory() const {
00257     CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
00258   }
00259   void setDoesNotAccessMemory() {
00260     CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
00261   }
00262 
00263   /// @brief Determine if the call does not access or only reads memory.
00264   bool onlyReadsMemory() const {
00265     CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
00266   }
00267   void setOnlyReadsMemory() {
00268     CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
00269   }
00270 
00271   /// @brief Determine if the call cannot return.
00272   bool doesNotReturn() const {
00273     CALLSITE_DELEGATE_GETTER(doesNotReturn());
00274   }
00275   void setDoesNotReturn() {
00276     CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
00277   }
00278 
00279   /// @brief Determine if the call cannot unwind.
00280   bool doesNotThrow() const {
00281     CALLSITE_DELEGATE_GETTER(doesNotThrow());
00282   }
00283   void setDoesNotThrow() {
00284     CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
00285   }
00286 
00287 #undef CALLSITE_DELEGATE_GETTER
00288 #undef CALLSITE_DELEGATE_SETTER
00289 
00290   /// @brief Determine whether this argument is not captured.
00291   bool doesNotCapture(unsigned ArgNo) const {
00292     return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
00293   }
00294 
00295   /// @brief Determine whether this argument is passed by value.
00296   bool isByValArgument(unsigned ArgNo) const {
00297     return paramHasAttr(ArgNo + 1, Attribute::ByVal);
00298   }
00299 
00300   /// @brief Determine whether this argument is passed in an alloca.
00301   bool isInAllocaArgument(unsigned ArgNo) const {
00302     return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
00303   }
00304 
00305   /// @brief Determine whether this argument is passed by value or in an alloca.
00306   bool isByValOrInAllocaArgument(unsigned ArgNo) const {
00307     return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
00308            paramHasAttr(ArgNo + 1, Attribute::InAlloca);
00309   }
00310 
00311   /// @brief Determine if there are is an inalloca argument.  Only the last
00312   /// argument can have the inalloca attribute.
00313   bool hasInAllocaArgument() const {
00314     return paramHasAttr(arg_size(), Attribute::InAlloca);
00315   }
00316 
00317   bool doesNotAccessMemory(unsigned ArgNo) const {
00318     return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
00319   }
00320 
00321   bool onlyReadsMemory(unsigned ArgNo) const {
00322     return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
00323            paramHasAttr(ArgNo + 1, Attribute::ReadNone);
00324   }
00325 
00326   /// @brief Return true if the return value is known to be not null.
00327   /// This may be because it has the nonnull attribute, or because at least
00328   /// one byte is dereferenceable and the pointer is in addrspace(0).
00329   bool isReturnNonNull() const {
00330     if (paramHasAttr(0, Attribute::NonNull))
00331       return true;
00332     else if (getDereferenceableBytes(0) > 0 &&
00333              getType()->getPointerAddressSpace() == 0)
00334       return true;
00335 
00336     return false;
00337   }
00338 
00339   /// hasArgument - Returns true if this CallSite passes the given Value* as an
00340   /// argument to the called function.
00341   bool hasArgument(const Value *Arg) const {
00342     for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
00343          ++AI)
00344       if (AI->get() == Arg)
00345         return true;
00346     return false;
00347   }
00348 
00349 private:
00350   unsigned getArgumentEndOffset() const {
00351     if (isCall())
00352       return 1; // Skip Callee
00353     else
00354       return 3; // Skip BB, BB, Callee
00355   }
00356 
00357   IterTy getCallee() const {
00358     if (isCall()) // Skip Callee
00359       return cast<CallInst>(getInstruction())->op_end() - 1;
00360     else // Skip BB, BB, Callee
00361       return cast<InvokeInst>(getInstruction())->op_end() - 3;
00362   }
00363 };
00364 
00365 class CallSite : public CallSiteBase<Function, Value, User, Instruction,
00366                                      CallInst, InvokeInst, User::op_iterator> {
00367 public:
00368   CallSite() {}
00369   CallSite(CallSiteBase B) : CallSiteBase(B) {}
00370   CallSite(CallInst *CI) : CallSiteBase(CI) {}
00371   CallSite(InvokeInst *II) : CallSiteBase(II) {}
00372   explicit CallSite(Instruction *II) : CallSiteBase(II) {}
00373   explicit CallSite(Value *V) : CallSiteBase(V) {}
00374 
00375   bool operator==(const CallSite &CS) const { return I == CS.I; }
00376   bool operator!=(const CallSite &CS) const { return I != CS.I; }
00377   bool operator<(const CallSite &CS) const {
00378     return getInstruction() < CS.getInstruction();
00379   }
00380 
00381 private:
00382   User::op_iterator getCallee() const;
00383 };
00384 
00385 /// ImmutableCallSite - establish a view to a call site for examination
00386 class ImmutableCallSite : public CallSiteBase<> {
00387 public:
00388   ImmutableCallSite() {}
00389   ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
00390   ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
00391   explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
00392   explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
00393   ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
00394 };
00395 
00396 } // End llvm namespace
00397 
00398 #endif