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