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 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   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   /// \brief Tests if this call site must be tail call optimized.  Only a
00164   /// CallInst can be tail call optimized.
00165   bool isMustTailCall() const {
00166     return isCall() && cast<CallInst>(getInstruction())->isMustTailCall();
00167   }
00168 
00169   /// \brief Tests if this call site is marked as a tail call.
00170   bool isTailCall() const {
00171     return isCall() && cast<CallInst>(getInstruction())->isTailCall();
00172   }
00173 
00174 #define CALLSITE_DELEGATE_GETTER(METHOD) \
00175   InstrTy *II = getInstruction();    \
00176   return isCall()                        \
00177     ? cast<CallInst>(II)->METHOD         \
00178     : cast<InvokeInst>(II)->METHOD
00179 
00180 #define CALLSITE_DELEGATE_SETTER(METHOD) \
00181   InstrTy *II = getInstruction();    \
00182   if (isCall())                          \
00183     cast<CallInst>(II)->METHOD;          \
00184   else                                   \
00185     cast<InvokeInst>(II)->METHOD
00186 
00187   /// getCallingConv/setCallingConv - get or set the calling convention of the
00188   /// call.
00189   CallingConv::ID getCallingConv() const {
00190     CALLSITE_DELEGATE_GETTER(getCallingConv());
00191   }
00192   void setCallingConv(CallingConv::ID CC) {
00193     CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
00194   }
00195 
00196   /// getAttributes/setAttributes - get or set the parameter attributes of
00197   /// the call.
00198   const AttributeSet &getAttributes() const {
00199     CALLSITE_DELEGATE_GETTER(getAttributes());
00200   }
00201   void setAttributes(const AttributeSet &PAL) {
00202     CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
00203   }
00204 
00205   /// \brief Return true if this function has the given attribute.
00206   bool hasFnAttr(Attribute::AttrKind A) const {
00207     CALLSITE_DELEGATE_GETTER(hasFnAttr(A));
00208   }
00209 
00210   /// \brief Return true if the call or the callee has the given attribute.
00211   bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
00212     CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A));
00213   }
00214 
00215   /// @brief Extract the alignment for a call or parameter (0=unknown).
00216   uint16_t getParamAlignment(uint16_t i) const {
00217     CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
00218   }
00219 
00220   /// @brief Extract the number of dereferenceable bytes for a call or
00221   /// parameter (0=unknown).
00222   uint64_t getDereferenceableBytes(uint16_t i) const {
00223     CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
00224   }
00225 
00226   /// \brief Return true if the call should not be treated as a call to a
00227   /// builtin.
00228   bool isNoBuiltin() const {
00229     CALLSITE_DELEGATE_GETTER(isNoBuiltin());
00230   }
00231 
00232   /// @brief Return true if the call should not be inlined.
00233   bool isNoInline() const {
00234     CALLSITE_DELEGATE_GETTER(isNoInline());
00235   }
00236   void setIsNoInline(bool Value = true) {
00237     CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
00238   }
00239 
00240   /// @brief Determine if the call does not access memory.
00241   bool doesNotAccessMemory() const {
00242     CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
00243   }
00244   void setDoesNotAccessMemory() {
00245     CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
00246   }
00247 
00248   /// @brief Determine if the call does not access or only reads memory.
00249   bool onlyReadsMemory() const {
00250     CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
00251   }
00252   void setOnlyReadsMemory() {
00253     CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
00254   }
00255 
00256   /// @brief Determine if the call cannot return.
00257   bool doesNotReturn() const {
00258     CALLSITE_DELEGATE_GETTER(doesNotReturn());
00259   }
00260   void setDoesNotReturn() {
00261     CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
00262   }
00263 
00264   /// @brief Determine if the call cannot unwind.
00265   bool doesNotThrow() const {
00266     CALLSITE_DELEGATE_GETTER(doesNotThrow());
00267   }
00268   void setDoesNotThrow() {
00269     CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
00270   }
00271 
00272 #undef CALLSITE_DELEGATE_GETTER
00273 #undef CALLSITE_DELEGATE_SETTER
00274 
00275   /// @brief Determine whether this argument is not captured.
00276   bool doesNotCapture(unsigned ArgNo) const {
00277     return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
00278   }
00279 
00280   /// @brief Determine whether this argument is passed by value.
00281   bool isByValArgument(unsigned ArgNo) const {
00282     return paramHasAttr(ArgNo + 1, Attribute::ByVal);
00283   }
00284 
00285   /// @brief Determine whether this argument is passed in an alloca.
00286   bool isInAllocaArgument(unsigned ArgNo) const {
00287     return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
00288   }
00289 
00290   /// @brief Determine whether this argument is passed by value or in an alloca.
00291   bool isByValOrInAllocaArgument(unsigned ArgNo) const {
00292     return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
00293            paramHasAttr(ArgNo + 1, Attribute::InAlloca);
00294   }
00295 
00296   /// @brief Determine if there are is an inalloca argument.  Only the last
00297   /// argument can have the inalloca attribute.
00298   bool hasInAllocaArgument() const {
00299     return paramHasAttr(arg_size(), Attribute::InAlloca);
00300   }
00301 
00302   bool doesNotAccessMemory(unsigned ArgNo) const {
00303     return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
00304   }
00305 
00306   bool onlyReadsMemory(unsigned ArgNo) const {
00307     return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
00308            paramHasAttr(ArgNo + 1, Attribute::ReadNone);
00309   }
00310 
00311   /// @brief Return true if the return value is known to be not null.
00312   /// This may be because it has the nonnull attribute, or because at least
00313   /// one byte is dereferenceable and the pointer is in addrspace(0).
00314   bool isReturnNonNull() const {
00315     if (paramHasAttr(0, Attribute::NonNull))
00316       return true;
00317     else if (getDereferenceableBytes(0) > 0 &&
00318              getType()->getPointerAddressSpace() == 0)
00319       return true;
00320 
00321     return false;
00322   }
00323 
00324   /// hasArgument - Returns true if this CallSite passes the given Value* as an
00325   /// argument to the called function.
00326   bool hasArgument(const Value *Arg) const {
00327     for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
00328          ++AI)
00329       if (AI->get() == Arg)
00330         return true;
00331     return false;
00332   }
00333 
00334 private:
00335   unsigned getArgumentEndOffset() const {
00336     if (isCall())
00337       return 1; // Skip Callee
00338     else
00339       return 3; // Skip BB, BB, Callee
00340   }
00341 
00342   IterTy getCallee() const {
00343     if (isCall()) // Skip Callee
00344       return cast<CallInst>(getInstruction())->op_end() - 1;
00345     else // Skip BB, BB, Callee
00346       return cast<InvokeInst>(getInstruction())->op_end() - 3;
00347   }
00348 };
00349 
00350 class CallSite : public CallSiteBase<Function, Value, User, Instruction,
00351                                      CallInst, InvokeInst, User::op_iterator> {
00352   typedef CallSiteBase<Function, Value, User, Instruction,
00353                        CallInst, InvokeInst, User::op_iterator> Base;
00354 public:
00355   CallSite() {}
00356   CallSite(Base B) : Base(B) {}
00357   CallSite(Value* V) : Base(V) {}
00358   CallSite(CallInst *CI) : Base(CI) {}
00359   CallSite(InvokeInst *II) : Base(II) {}
00360   CallSite(Instruction *II) : Base(II) {}
00361 
00362   bool operator==(const CallSite &CS) const { return I == CS.I; }
00363   bool operator!=(const CallSite &CS) const { return I != CS.I; }
00364   bool operator<(const CallSite &CS) const {
00365     return getInstruction() < CS.getInstruction();
00366   }
00367 
00368 private:
00369   User::op_iterator getCallee() const;
00370 };
00371 
00372 /// ImmutableCallSite - establish a view to a call site for examination
00373 class ImmutableCallSite : public CallSiteBase<> {
00374   typedef CallSiteBase<> Base;
00375 public:
00376   ImmutableCallSite() {}
00377   ImmutableCallSite(const Value* V) : Base(V) {}
00378   ImmutableCallSite(const CallInst *CI) : Base(CI) {}
00379   ImmutableCallSite(const InvokeInst *II) : Base(II) {}
00380   ImmutableCallSite(const Instruction *II) : Base(II) {}
00381   ImmutableCallSite(CallSite CS) : Base(CS.getInstruction()) {}
00382 };
00383 
00384 } // End llvm namespace
00385 
00386 #endif