LLVM API Documentation

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 public:
00050   CallSiteBase() : I(nullptr, false) {}
00051   CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); }
00052   CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); }
00053   CallSiteBase(ValTy *II) { *this = get(II); }
00054 protected:
00055   /// CallSiteBase::get - This static method is sort of like a constructor.  It
00056   /// will create an appropriate call site for a Call or Invoke instruction, but
00057   /// it can also create a null initialized CallSiteBase object for something
00058   /// which is NOT a call site.
00059   ///
00060   static CallSiteBase get(ValTy *V) {
00061     if (InstrTy *II = dyn_cast<InstrTy>(V)) {
00062       if (II->getOpcode() == Instruction::Call)
00063         return CallSiteBase(static_cast<CallTy*>(II));
00064       else if (II->getOpcode() == Instruction::Invoke)
00065         return CallSiteBase(static_cast<InvokeTy*>(II));
00066     }
00067     return CallSiteBase();
00068   }
00069 public:
00070   /// isCall - true if a CallInst is enclosed.
00071   /// Note that !isCall() does not mean it is an InvokeInst enclosed,
00072   /// it also could signify a NULL Instruction pointer.
00073   bool isCall() const { return I.getInt(); }
00074 
00075   /// isInvoke - true if a InvokeInst is enclosed.
00076   ///
00077   bool isInvoke() const { return getInstruction() && !I.getInt(); }
00078 
00079   InstrTy *getInstruction() const { return I.getPointer(); }
00080   InstrTy *operator->() const { return I.getPointer(); }
00081   LLVM_EXPLICIT operator bool() const { return I.getPointer(); }
00082 
00083   /// getCalledValue - Return the pointer to function that is being called.
00084   ///
00085   ValTy *getCalledValue() const {
00086     assert(getInstruction() && "Not a call or invoke instruction!");
00087     return *getCallee();
00088   }
00089 
00090   /// getCalledFunction - Return the function being called if this is a direct
00091   /// call, otherwise return null (if it's an indirect call).
00092   ///
00093   FunTy *getCalledFunction() const {
00094     return dyn_cast<FunTy>(getCalledValue());
00095   }
00096 
00097   /// setCalledFunction - Set the callee to the specified value.
00098   ///
00099   void setCalledFunction(Value *V) {
00100     assert(getInstruction() && "Not a call or invoke instruction!");
00101     *getCallee() = V;
00102   }
00103 
00104   /// isCallee - Determine whether the passed iterator points to the
00105   /// callee operand's Use.
00106   bool isCallee(Value::const_user_iterator UI) const {
00107     return isCallee(&UI.getUse());
00108   }
00109 
00110   /// Determine whether this Use is the callee operand's Use.
00111   bool isCallee(const Use *U) const { return getCallee() == U; }
00112 
00113   ValTy *getArgument(unsigned ArgNo) const {
00114     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
00115     return *(arg_begin() + ArgNo);
00116   }
00117 
00118   void setArgument(unsigned ArgNo, Value* newVal) {
00119     assert(getInstruction() && "Not a call or invoke instruction!");
00120     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
00121     getInstruction()->setOperand(ArgNo, newVal);
00122   }
00123 
00124   /// Given a value use iterator, returns the argument that corresponds to it.
00125   /// Iterator must actually correspond to an argument.
00126   unsigned getArgumentNo(Value::const_user_iterator I) const {
00127     return getArgumentNo(&I.getUse());
00128   }
00129 
00130   /// Given a use for an argument, get the argument number that corresponds to
00131   /// it.
00132   unsigned getArgumentNo(const Use *U) const {
00133     assert(getInstruction() && "Not a call or invoke instruction!");
00134     assert(arg_begin() <= U && U < arg_end()
00135            && "Argument # out of range!");
00136     return U - arg_begin();
00137   }
00138 
00139   /// arg_iterator - The type of iterator to use when looping over actual
00140   /// arguments at this call site.
00141   typedef IterTy arg_iterator;
00142 
00143   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
00144   /// list for a call site.
00145   IterTy arg_begin() const {
00146     assert(getInstruction() && "Not a call or invoke instruction!");
00147     // Skip non-arguments
00148     return (*this)->op_begin();
00149   }
00150 
00151   IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
00152   bool arg_empty() const { return arg_end() == arg_begin(); }
00153   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
00154   
00155   /// getType - Return the type of the instruction that generated this call site
00156   ///
00157   Type *getType() const { return (*this)->getType(); }
00158 
00159   /// getCaller - Return the caller function for this call site
00160   ///
00161   FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
00162 
00163 #define CALLSITE_DELEGATE_GETTER(METHOD) \
00164   InstrTy *II = getInstruction();    \
00165   return isCall()                        \
00166     ? cast<CallInst>(II)->METHOD         \
00167     : cast<InvokeInst>(II)->METHOD
00168 
00169 #define CALLSITE_DELEGATE_SETTER(METHOD) \
00170   InstrTy *II = getInstruction();    \
00171   if (isCall())                          \
00172     cast<CallInst>(II)->METHOD;          \
00173   else                                   \
00174     cast<InvokeInst>(II)->METHOD
00175 
00176   /// getCallingConv/setCallingConv - get or set the calling convention of the
00177   /// call.
00178   CallingConv::ID getCallingConv() const {
00179     CALLSITE_DELEGATE_GETTER(getCallingConv());
00180   }
00181   void setCallingConv(CallingConv::ID CC) {
00182     CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
00183   }
00184 
00185   /// getAttributes/setAttributes - get or set the parameter attributes of
00186   /// the call.
00187   const AttributeSet &getAttributes() const {
00188     CALLSITE_DELEGATE_GETTER(getAttributes());
00189   }
00190   void setAttributes(const AttributeSet &PAL) {
00191     CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
00192   }
00193 
00194   /// \brief Return true if this function has the given attribute.
00195   bool hasFnAttr(Attribute::AttrKind A) const {
00196     CALLSITE_DELEGATE_GETTER(hasFnAttr(A));
00197   }
00198 
00199   /// \brief Return true if the call or the callee has the given attribute.
00200   bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
00201     CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A));
00202   }
00203 
00204   /// @brief Extract the alignment for a call or parameter (0=unknown).
00205   uint16_t getParamAlignment(uint16_t i) const {
00206     CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
00207   }
00208 
00209   /// \brief Return true if the call should not be treated as a call to a
00210   /// builtin.
00211   bool isNoBuiltin() const {
00212     CALLSITE_DELEGATE_GETTER(isNoBuiltin());
00213   }
00214 
00215   /// @brief Return true if the call should not be inlined.
00216   bool isNoInline() const {
00217     CALLSITE_DELEGATE_GETTER(isNoInline());
00218   }
00219   void setIsNoInline(bool Value = true) {
00220     CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
00221   }
00222 
00223   /// @brief Determine if the call does not access memory.
00224   bool doesNotAccessMemory() const {
00225     CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
00226   }
00227   void setDoesNotAccessMemory() {
00228     CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
00229   }
00230 
00231   /// @brief Determine if the call does not access or only reads memory.
00232   bool onlyReadsMemory() const {
00233     CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
00234   }
00235   void setOnlyReadsMemory() {
00236     CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
00237   }
00238 
00239   /// @brief Determine if the call cannot return.
00240   bool doesNotReturn() const {
00241     CALLSITE_DELEGATE_GETTER(doesNotReturn());
00242   }
00243   void setDoesNotReturn() {
00244     CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
00245   }
00246 
00247   /// @brief Determine if the call cannot unwind.
00248   bool doesNotThrow() const {
00249     CALLSITE_DELEGATE_GETTER(doesNotThrow());
00250   }
00251   void setDoesNotThrow() {
00252     CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
00253   }
00254 
00255 #undef CALLSITE_DELEGATE_GETTER
00256 #undef CALLSITE_DELEGATE_SETTER
00257 
00258   /// @brief Determine whether this argument is not captured.
00259   bool doesNotCapture(unsigned ArgNo) const {
00260     return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
00261   }
00262 
00263   /// @brief Determine whether this argument is passed by value.
00264   bool isByValArgument(unsigned ArgNo) const {
00265     return paramHasAttr(ArgNo + 1, Attribute::ByVal);
00266   }
00267 
00268   /// @brief Determine whether this argument is passed in an alloca.
00269   bool isInAllocaArgument(unsigned ArgNo) const {
00270     return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
00271   }
00272 
00273   /// @brief Determine whether this argument is passed by value or in an alloca.
00274   bool isByValOrInAllocaArgument(unsigned ArgNo) const {
00275     return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
00276            paramHasAttr(ArgNo + 1, Attribute::InAlloca);
00277   }
00278 
00279   /// @brief Determine if there are is an inalloca argument.  Only the last
00280   /// argument can have the inalloca attribute.
00281   bool hasInAllocaArgument() const {
00282     return paramHasAttr(arg_size(), Attribute::InAlloca);
00283   }
00284 
00285   bool doesNotAccessMemory(unsigned ArgNo) const {
00286     return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
00287   }
00288 
00289   bool onlyReadsMemory(unsigned ArgNo) const {
00290     return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
00291            paramHasAttr(ArgNo + 1, Attribute::ReadNone);
00292   }
00293 
00294   /// hasArgument - Returns true if this CallSite passes the given Value* as an
00295   /// argument to the called function.
00296   bool hasArgument(const Value *Arg) const {
00297     for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
00298          ++AI)
00299       if (AI->get() == Arg)
00300         return true;
00301     return false;
00302   }
00303 
00304 private:
00305   unsigned getArgumentEndOffset() const {
00306     if (isCall())
00307       return 1; // Skip Callee
00308     else
00309       return 3; // Skip BB, BB, Callee
00310   }
00311 
00312   IterTy getCallee() const {
00313     if (isCall()) // Skip Callee
00314       return cast<CallInst>(getInstruction())->op_end() - 1;
00315     else // Skip BB, BB, Callee
00316       return cast<InvokeInst>(getInstruction())->op_end() - 3;
00317   }
00318 };
00319 
00320 class CallSite : public CallSiteBase<Function, Value, User, Instruction,
00321                                      CallInst, InvokeInst, User::op_iterator> {
00322   typedef CallSiteBase<Function, Value, User, Instruction,
00323                        CallInst, InvokeInst, User::op_iterator> Base;
00324 public:
00325   CallSite() {}
00326   CallSite(Base B) : Base(B) {}
00327   CallSite(Value* V) : Base(V) {}
00328   CallSite(CallInst *CI) : Base(CI) {}
00329   CallSite(InvokeInst *II) : Base(II) {}
00330   CallSite(Instruction *II) : Base(II) {}
00331 
00332   bool operator==(const CallSite &CS) const { return I == CS.I; }
00333   bool operator!=(const CallSite &CS) const { return I != CS.I; }
00334   bool operator<(const CallSite &CS) const {
00335     return getInstruction() < CS.getInstruction();
00336   }
00337 
00338 private:
00339   User::op_iterator getCallee() const;
00340 };
00341 
00342 /// ImmutableCallSite - establish a view to a call site for examination
00343 class ImmutableCallSite : public CallSiteBase<> {
00344   typedef CallSiteBase<> Base;
00345 public:
00346   ImmutableCallSite(const Value* V) : Base(V) {}
00347   ImmutableCallSite(const CallInst *CI) : Base(CI) {}
00348   ImmutableCallSite(const InvokeInst *II) : Base(II) {}
00349   ImmutableCallSite(const Instruction *II) : Base(II) {}
00350   ImmutableCallSite(CallSite CS) : Base(CS.getInstruction()) {}
00351 };
00352 
00353 } // End llvm namespace
00354 
00355 #endif