LLVM API Documentation

CallSite.h

Go to the documentation of this file.
00001 //===-- llvm/Support/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.
00012 //
00013 // NOTE: This class is supposed to have "value semantics". So it should be
00014 // passed by value, not by reference; it should not be "new"ed or "delete"d. It
00015 // is efficiently copyable, assignable and constructable, with cost equivalent
00016 // to copying a pointer (notice that it has only a single data member).
00017 // The internal representation carries a flag which indicates which of the two
00018 // variants is enclosed. This allows for cheaper checks when various accessors
00019 // of CallSite are employed.
00020 //
00021 //===----------------------------------------------------------------------===//
00022 
00023 #ifndef LLVM_SUPPORT_CALLSITE_H
00024 #define LLVM_SUPPORT_CALLSITE_H
00025 
00026 #include "llvm/Attributes.h"
00027 #include "llvm/ADT/PointerIntPair.h"
00028 #include "llvm/BasicBlock.h"
00029 #include "llvm/Instruction.h"
00030 
00031 namespace llvm {
00032 
00033 class CallInst;
00034 class InvokeInst;
00035 
00036 class CallSite {
00037   PointerIntPair<Instruction*, 1, bool> I;
00038 public:
00039   CallSite() : I(0, false) {}
00040   CallSite(CallInst *CI) : I(reinterpret_cast<Instruction*>(CI), true) {}
00041   CallSite(InvokeInst *II) : I(reinterpret_cast<Instruction*>(II), false) {}
00042   CallSite(Instruction *C);
00043   CallSite(const CallSite &CS) : I(CS.I) {}
00044   CallSite &operator=(const CallSite &CS) { I = CS.I; return *this; }
00045 
00046   bool operator==(const CallSite &CS) const { return I == CS.I; }
00047   bool operator!=(const CallSite &CS) const { return I != CS.I; }
00048 
00049   /// CallSite::get - This static method is sort of like a constructor.  It will
00050   /// create an appropriate call site for a Call or Invoke instruction, but it
00051   /// can also create a null initialized CallSite object for something which is
00052   /// NOT a call site.
00053   ///
00054   static CallSite get(Value *V) {
00055     if (Instruction *I = dyn_cast<Instruction>(V)) {
00056       if (I->getOpcode() == Instruction::Call)
00057         return CallSite(reinterpret_cast<CallInst*>(I));
00058       else if (I->getOpcode() == Instruction::Invoke)
00059         return CallSite(reinterpret_cast<InvokeInst*>(I));
00060     }
00061     return CallSite();
00062   }
00063 
00064   /// getCallingConv/setCallingConv - get or set the calling convention of the
00065   /// call.
00066   unsigned getCallingConv() const;
00067   void setCallingConv(unsigned CC);
00068 
00069   /// getAttributes/setAttributes - get or set the parameter attributes of
00070   /// the call.
00071   const AttrListPtr &getAttributes() const;
00072   void setAttributes(const AttrListPtr &PAL);
00073 
00074   /// paramHasAttr - whether the call or the callee has the given attribute.
00075   bool paramHasAttr(uint16_t i, Attributes attr) const;
00076 
00077   /// @brief Extract the alignment for a call or parameter (0=unknown).
00078   uint16_t getParamAlignment(uint16_t i) const;
00079 
00080   /// @brief Determine if the call does not access memory.
00081   bool doesNotAccessMemory() const;
00082   void setDoesNotAccessMemory(bool doesNotAccessMemory = true);
00083 
00084   /// @brief Determine if the call does not access or only reads memory.
00085   bool onlyReadsMemory() const;
00086   void setOnlyReadsMemory(bool onlyReadsMemory = true);
00087 
00088   /// @brief Determine if the call cannot return.
00089   bool doesNotReturn() const;
00090   void setDoesNotReturn(bool doesNotReturn = true);
00091 
00092   /// @brief Determine if the call cannot unwind.
00093   bool doesNotThrow() const;
00094   void setDoesNotThrow(bool doesNotThrow = true);
00095 
00096   /// getType - Return the type of the instruction that generated this call site
00097   ///
00098   const Type *getType() const { return getInstruction()->getType(); }
00099 
00100   /// isCall - true if a CallInst is enclosed.
00101   /// Note that !isCall() does not mean it is an InvokeInst enclosed,
00102   /// it also could signify a NULL Instruction pointer.
00103   bool isCall() const { return I.getInt(); }
00104 
00105   /// isInvoke - true if a InvokeInst is enclosed.
00106   ///
00107   bool isInvoke() const { return getInstruction() && !I.getInt(); }
00108 
00109   /// getInstruction - Return the instruction this call site corresponds to
00110   ///
00111   Instruction *getInstruction() const { return I.getPointer(); }
00112 
00113   /// getCaller - Return the caller function for this call site
00114   ///
00115   Function *getCaller() const { return getInstruction()
00116                                   ->getParent()->getParent(); }
00117 
00118   /// getCalledValue - Return the pointer to function that is being called...
00119   ///
00120   Value *getCalledValue() const {
00121     assert(getInstruction() && "Not a call or invoke instruction!");
00122     return getInstruction()->getOperand(0);
00123   }
00124 
00125   /// getCalledFunction - Return the function being called if this is a direct
00126   /// call, otherwise return null (if it's an indirect call).
00127   ///
00128   Function *getCalledFunction() const {
00129     return dyn_cast<Function>(getCalledValue());
00130   }
00131 
00132   /// setCalledFunction - Set the callee to the specified value...
00133   ///
00134   void setCalledFunction(Value *V) {
00135     assert(getInstruction() && "Not a call or invoke instruction!");
00136     getInstruction()->setOperand(0, V);
00137   }
00138 
00139   Value *getArgument(unsigned ArgNo) const {
00140     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
00141     return *(arg_begin()+ArgNo);
00142   }
00143 
00144   void setArgument(unsigned ArgNo, Value* newVal) {
00145     assert(getInstruction() && "Not a call or invoke instruction!");
00146     assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
00147     getInstruction()->setOperand(getArgumentOffset() + ArgNo, newVal);
00148   }
00149 
00150   /// Given an operand number, returns the argument that corresponds to it.
00151   /// OperandNo must be a valid operand number that actually corresponds to an
00152   /// argument.
00153   unsigned getArgumentNo(unsigned OperandNo) const {
00154     assert(OperandNo >= getArgumentOffset() && "Operand number passed was not "
00155                                                "a valid argument");
00156     return OperandNo - getArgumentOffset();
00157   }
00158 
00159   /// hasArgument - Returns true if this CallSite passes the given Value* as an
00160   /// argument to the called function.
00161   bool hasArgument(const Value *Arg) const;
00162 
00163   /// arg_iterator - The type of iterator to use when looping over actual
00164   /// arguments at this call site...
00165   typedef User::op_iterator arg_iterator;
00166 
00167   /// arg_begin/arg_end - Return iterators corresponding to the actual argument
00168   /// list for a call site.
00169   arg_iterator arg_begin() const {
00170     assert(getInstruction() && "Not a call or invoke instruction!");
00171     // Skip non-arguments
00172     return getInstruction()->op_begin() + getArgumentOffset();
00173   }
00174 
00175   arg_iterator arg_end() const { return getInstruction()->op_end(); }
00176   bool arg_empty() const { return arg_end() == arg_begin(); }
00177   unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
00178 
00179   bool operator<(const CallSite &CS) const {
00180     return getInstruction() < CS.getInstruction();
00181   }
00182 
00183   bool isCallee(Value::use_iterator UI) const {
00184     return getInstruction()->op_begin() == &UI.getUse();
00185   }
00186 
00187 private:
00188   /// Returns the operand number of the first argument
00189   unsigned getArgumentOffset() const {
00190     if (isCall())
00191       return 1; // Skip Function
00192     else
00193       return 3; // Skip Function, BB, BB
00194   }
00195 };
00196 
00197 } // End llvm namespace
00198 
00199 #endif