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. 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_SUPPORT_CALLSITE_H 00027 #define LLVM_SUPPORT_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(0, 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 /// 00107 bool isCallee(value_use_iterator<UserTy> UI) const { 00108 return getCallee() == &UI.getUse(); 00109 } 00110 00111 ValTy *getArgument(unsigned ArgNo) const { 00112 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!"); 00113 return *(arg_begin() + ArgNo); 00114 } 00115 00116 void setArgument(unsigned ArgNo, Value* newVal) { 00117 assert(getInstruction() && "Not a call or invoke instruction!"); 00118 assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!"); 00119 getInstruction()->setOperand(ArgNo, newVal); 00120 } 00121 00122 /// Given a value use iterator, returns the argument that corresponds to it. 00123 /// Iterator must actually correspond to an argument. 00124 unsigned getArgumentNo(value_use_iterator<UserTy> I) const { 00125 assert(getInstruction() && "Not a call or invoke instruction!"); 00126 assert(arg_begin() <= &I.getUse() && &I.getUse() < arg_end() 00127 && "Argument # out of range!"); 00128 return &I.getUse() - arg_begin(); 00129 } 00130 00131 /// arg_iterator - The type of iterator to use when looping over actual 00132 /// arguments at this call site. 00133 typedef IterTy arg_iterator; 00134 00135 /// arg_begin/arg_end - Return iterators corresponding to the actual argument 00136 /// list for a call site. 00137 IterTy arg_begin() const { 00138 assert(getInstruction() && "Not a call or invoke instruction!"); 00139 // Skip non-arguments 00140 return (*this)->op_begin(); 00141 } 00142 00143 IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); } 00144 bool arg_empty() const { return arg_end() == arg_begin(); } 00145 unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); } 00146 00147 /// getType - Return the type of the instruction that generated this call site 00148 /// 00149 Type *getType() const { return (*this)->getType(); } 00150 00151 /// getCaller - Return the caller function for this call site 00152 /// 00153 FunTy *getCaller() const { return (*this)->getParent()->getParent(); } 00154 00155 #define CALLSITE_DELEGATE_GETTER(METHOD) \ 00156 InstrTy *II = getInstruction(); \ 00157 return isCall() \ 00158 ? cast<CallInst>(II)->METHOD \ 00159 : cast<InvokeInst>(II)->METHOD 00160 00161 #define CALLSITE_DELEGATE_SETTER(METHOD) \ 00162 InstrTy *II = getInstruction(); \ 00163 if (isCall()) \ 00164 cast<CallInst>(II)->METHOD; \ 00165 else \ 00166 cast<InvokeInst>(II)->METHOD 00167 00168 /// getCallingConv/setCallingConv - get or set the calling convention of the 00169 /// call. 00170 CallingConv::ID getCallingConv() const { 00171 CALLSITE_DELEGATE_GETTER(getCallingConv()); 00172 } 00173 void setCallingConv(CallingConv::ID CC) { 00174 CALLSITE_DELEGATE_SETTER(setCallingConv(CC)); 00175 } 00176 00177 /// getAttributes/setAttributes - get or set the parameter attributes of 00178 /// the call. 00179 const AttributeSet &getAttributes() const { 00180 CALLSITE_DELEGATE_GETTER(getAttributes()); 00181 } 00182 void setAttributes(const AttributeSet &PAL) { 00183 CALLSITE_DELEGATE_SETTER(setAttributes(PAL)); 00184 } 00185 00186 /// \brief Return true if this function has the given attribute. 00187 bool hasFnAttr(Attribute::AttrKind A) const { 00188 CALLSITE_DELEGATE_GETTER(hasFnAttr(A)); 00189 } 00190 00191 /// \brief Return true if the call or the callee has the given attribute. 00192 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const { 00193 CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A)); 00194 } 00195 00196 /// @brief Extract the alignment for a call or parameter (0=unknown). 00197 uint16_t getParamAlignment(uint16_t i) const { 00198 CALLSITE_DELEGATE_GETTER(getParamAlignment(i)); 00199 } 00200 00201 /// @brief Return true if the call should not be inlined. 00202 bool isNoInline() const { 00203 CALLSITE_DELEGATE_GETTER(isNoInline()); 00204 } 00205 void setIsNoInline(bool Value = true) { 00206 CALLSITE_DELEGATE_SETTER(setIsNoInline(Value)); 00207 } 00208 00209 /// @brief Determine if the call does not access memory. 00210 bool doesNotAccessMemory() const { 00211 CALLSITE_DELEGATE_GETTER(doesNotAccessMemory()); 00212 } 00213 void setDoesNotAccessMemory() { 00214 CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory()); 00215 } 00216 00217 /// @brief Determine if the call does not access or only reads memory. 00218 bool onlyReadsMemory() const { 00219 CALLSITE_DELEGATE_GETTER(onlyReadsMemory()); 00220 } 00221 void setOnlyReadsMemory() { 00222 CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory()); 00223 } 00224 00225 /// @brief Determine if the call cannot return. 00226 bool doesNotReturn() const { 00227 CALLSITE_DELEGATE_GETTER(doesNotReturn()); 00228 } 00229 void setDoesNotReturn() { 00230 CALLSITE_DELEGATE_SETTER(setDoesNotReturn()); 00231 } 00232 00233 /// @brief Determine if the call cannot unwind. 00234 bool doesNotThrow() const { 00235 CALLSITE_DELEGATE_GETTER(doesNotThrow()); 00236 } 00237 void setDoesNotThrow() { 00238 CALLSITE_DELEGATE_SETTER(setDoesNotThrow()); 00239 } 00240 00241 #undef CALLSITE_DELEGATE_GETTER 00242 #undef CALLSITE_DELEGATE_SETTER 00243 00244 /// @brief Determine whether this argument is not captured. 00245 bool doesNotCapture(unsigned ArgNo) const { 00246 return paramHasAttr(ArgNo + 1, Attribute::NoCapture); 00247 } 00248 00249 /// @brief Determine whether this argument is passed by value. 00250 bool isByValArgument(unsigned ArgNo) const { 00251 return paramHasAttr(ArgNo + 1, Attribute::ByVal); 00252 } 00253 00254 /// hasArgument - Returns true if this CallSite passes the given Value* as an 00255 /// argument to the called function. 00256 bool hasArgument(const Value *Arg) const { 00257 for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; 00258 ++AI) 00259 if (AI->get() == Arg) 00260 return true; 00261 return false; 00262 } 00263 00264 private: 00265 unsigned getArgumentEndOffset() const { 00266 if (isCall()) 00267 return 1; // Skip Callee 00268 else 00269 return 3; // Skip BB, BB, Callee 00270 } 00271 00272 IterTy getCallee() const { 00273 if (isCall()) // Skip Callee 00274 return cast<CallInst>(getInstruction())->op_end() - 1; 00275 else // Skip BB, BB, Callee 00276 return cast<InvokeInst>(getInstruction())->op_end() - 3; 00277 } 00278 }; 00279 00280 class CallSite : public CallSiteBase<Function, Value, User, Instruction, 00281 CallInst, InvokeInst, User::op_iterator> { 00282 typedef CallSiteBase<Function, Value, User, Instruction, 00283 CallInst, InvokeInst, User::op_iterator> Base; 00284 public: 00285 CallSite() {} 00286 CallSite(Base B) : Base(B) {} 00287 CallSite(Value* V) : Base(V) {} 00288 CallSite(CallInst *CI) : Base(CI) {} 00289 CallSite(InvokeInst *II) : Base(II) {} 00290 CallSite(Instruction *II) : Base(II) {} 00291 00292 bool operator==(const CallSite &CS) const { return I == CS.I; } 00293 bool operator!=(const CallSite &CS) const { return I != CS.I; } 00294 bool operator<(const CallSite &CS) const { 00295 return getInstruction() < CS.getInstruction(); 00296 } 00297 00298 private: 00299 User::op_iterator getCallee() const; 00300 }; 00301 00302 /// ImmutableCallSite - establish a view to a call site for examination 00303 class ImmutableCallSite : public CallSiteBase<> { 00304 typedef CallSiteBase<> Base; 00305 public: 00306 ImmutableCallSite(const Value* V) : Base(V) {} 00307 ImmutableCallSite(const CallInst *CI) : Base(CI) {} 00308 ImmutableCallSite(const InvokeInst *II) : Base(II) {} 00309 ImmutableCallSite(const Instruction *II) : Base(II) {} 00310 ImmutableCallSite(CallSite CS) : Base(CS.getInstruction()) {} 00311 }; 00312 00313 } // End llvm namespace 00314 00315 #endif