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