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 Return true if the call should not be treated as a call to a
00236   /// builtin.
00237   bool isNoBuiltin() const {
00238     CALLSITE_DELEGATE_GETTER(isNoBuiltin());
00239   }
00240 
00241   /// @brief Return true if the call should not be inlined.
00242   bool isNoInline() const {
00243     CALLSITE_DELEGATE_GETTER(isNoInline());
00244   }
00245   void setIsNoInline(bool Value = true) {
00246     CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
00247   }
00248 
00249   /// @brief Determine if the call does not access memory.
00250   bool doesNotAccessMemory() const {
00251     CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
00252   }
00253   void setDoesNotAccessMemory() {
00254     CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
00255   }
00256 
00257   /// @brief Determine if the call does not access or only reads memory.
00258   bool onlyReadsMemory() const {
00259     CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
00260   }
00261   void setOnlyReadsMemory() {
00262     CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
00263   }
00264 
00265   /// @brief Determine if the call cannot return.
00266   bool doesNotReturn() const {
00267     CALLSITE_DELEGATE_GETTER(doesNotReturn());
00268   }
00269   void setDoesNotReturn() {
00270     CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
00271   }
00272 
00273   /// @brief Determine if the call cannot unwind.
00274   bool doesNotThrow() const {
00275     CALLSITE_DELEGATE_GETTER(doesNotThrow());
00276   }
00277   void setDoesNotThrow() {
00278     CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
00279   }
00280 
00281 #undef CALLSITE_DELEGATE_GETTER
00282 #undef CALLSITE_DELEGATE_SETTER
00283 
00284   /// @brief Determine whether this argument is not captured.
00285   bool doesNotCapture(unsigned ArgNo) const {
00286     return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
00287   }
00288 
00289   /// @brief Determine whether this argument is passed by value.
00290   bool isByValArgument(unsigned ArgNo) const {
00291     return paramHasAttr(ArgNo + 1, Attribute::ByVal);
00292   }
00293 
00294   /// @brief Determine whether this argument is passed in an alloca.
00295   bool isInAllocaArgument(unsigned ArgNo) const {
00296     return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
00297   }
00298 
00299   /// @brief Determine whether this argument is passed by value or in an alloca.
00300   bool isByValOrInAllocaArgument(unsigned ArgNo) const {
00301     return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
00302            paramHasAttr(ArgNo + 1, Attribute::InAlloca);
00303   }
00304 
00305   /// @brief Determine if there are is an inalloca argument.  Only the last
00306   /// argument can have the inalloca attribute.
00307   bool hasInAllocaArgument() const {
00308     return paramHasAttr(arg_size(), Attribute::InAlloca);
00309   }
00310 
00311   bool doesNotAccessMemory(unsigned ArgNo) const {
00312     return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
00313   }
00314 
00315   bool onlyReadsMemory(unsigned ArgNo) const {
00316     return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
00317            paramHasAttr(ArgNo + 1, Attribute::ReadNone);
00318   }
00319 
00320   /// @brief Return true if the return value is known to be not null.
00321   /// This may be because it has the nonnull attribute, or because at least
00322   /// one byte is dereferenceable and the pointer is in addrspace(0).
00323   bool isReturnNonNull() const {
00324     if (paramHasAttr(0, Attribute::NonNull))
00325       return true;
00326     else if (getDereferenceableBytes(0) > 0 &&
00327              getType()->getPointerAddressSpace() == 0)
00328       return true;
00329 
00330     return false;
00331   }
00332 
00333   /// hasArgument - Returns true if this CallSite passes the given Value* as an
00334   /// argument to the called function.
00335   bool hasArgument(const Value *Arg) const {
00336     for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
00337          ++AI)
00338       if (AI->get() == Arg)
00339         return true;
00340     return false;
00341   }
00342 
00343 private:
00344   unsigned getArgumentEndOffset() const {
00345     if (isCall())
00346       return 1; // Skip Callee
00347     else
00348       return 3; // Skip BB, BB, Callee
00349   }
00350 
00351   IterTy getCallee() const {
00352     if (isCall()) // Skip Callee
00353       return cast<CallInst>(getInstruction())->op_end() - 1;
00354     else // Skip BB, BB, Callee
00355       return cast<InvokeInst>(getInstruction())->op_end() - 3;
00356   }
00357 };
00358 
00359 class CallSite : public CallSiteBase<Function, Value, User, Instruction,
00360                                      CallInst, InvokeInst, User::op_iterator> {
00361 public:
00362   CallSite() {}
00363   CallSite(CallSiteBase B) : CallSiteBase(B) {}
00364   CallSite(CallInst *CI) : CallSiteBase(CI) {}
00365   CallSite(InvokeInst *II) : CallSiteBase(II) {}
00366   explicit CallSite(Instruction *II) : CallSiteBase(II) {}
00367   explicit CallSite(Value *V) : CallSiteBase(V) {}
00368 
00369   bool operator==(const CallSite &CS) const { return I == CS.I; }
00370   bool operator!=(const CallSite &CS) const { return I != CS.I; }
00371   bool operator<(const CallSite &CS) const {
00372     return getInstruction() < CS.getInstruction();
00373   }
00374 
00375 private:
00376   User::op_iterator getCallee() const;
00377 };
00378 
00379 /// ImmutableCallSite - establish a view to a call site for examination
00380 class ImmutableCallSite : public CallSiteBase<> {
00381 public:
00382   ImmutableCallSite() {}
00383   ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
00384   ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
00385   explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
00386   explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
00387   ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
00388 };
00389 
00390 } // End llvm namespace
00391 
00392 #endif