1193323Sed//===-- llvm/Support/CallSite.h - Abstract Call & Invoke instrs -*- C++ -*-===// 2193323Sed// 3193323Sed// The LLVM Compiler Infrastructure 4193323Sed// 5193323Sed// This file is distributed under the University of Illinois Open Source 6193323Sed// License. See LICENSE.TXT for details. 7193323Sed// 8193323Sed//===----------------------------------------------------------------------===// 9193323Sed// 10193323Sed// This file defines the CallSite class, which is a handy wrapper for code that 11206083Srdivacky// wants to treat Call and Invoke instructions in a generic way. When in non- 12206083Srdivacky// mutation context (e.g. an analysis) ImmutableCallSite should be used. 13206083Srdivacky// Finally, when some degree of customization is necessary between these two 14206083Srdivacky// extremes, CallSiteBase<> can be supplied with fine-tuned parameters. 15193323Sed// 16206083Srdivacky// NOTE: These classes are supposed to have "value semantics". So they should be 17206083Srdivacky// passed by value, not by reference; they should not be "new"ed or "delete"d. 18206083Srdivacky// They are efficiently copyable, assignable and constructable, with cost 19206083Srdivacky// equivalent to copying a pointer (notice that they have only a single data 20206083Srdivacky// member). The internal representation carries a flag which indicates which of 21206083Srdivacky// the two variants is enclosed. This allows for cheaper checks when various 22206083Srdivacky// accessors of CallSite are employed. 23193323Sed// 24193323Sed//===----------------------------------------------------------------------===// 25193323Sed 26193323Sed#ifndef LLVM_SUPPORT_CALLSITE_H 27193323Sed#define LLVM_SUPPORT_CALLSITE_H 28193323Sed 29193323Sed#include "llvm/ADT/PointerIntPair.h" 30252723Sdim#include "llvm/IR/Attributes.h" 31252723Sdim#include "llvm/IR/CallingConv.h" 32252723Sdim#include "llvm/IR/Instructions.h" 33193323Sed 34193323Sednamespace llvm { 35193323Sed 36193323Sedclass CallInst; 37193323Sedclass InvokeInst; 38193323Sed 39206083Srdivackytemplate <typename FunTy = const Function, 40206083Srdivacky typename ValTy = const Value, 41206083Srdivacky typename UserTy = const User, 42206083Srdivacky typename InstrTy = const Instruction, 43206083Srdivacky typename CallTy = const CallInst, 44206083Srdivacky typename InvokeTy = const InvokeInst, 45206083Srdivacky typename IterTy = User::const_op_iterator> 46206083Srdivackyclass CallSiteBase { 47206083Srdivackyprotected: 48206083Srdivacky PointerIntPair<InstrTy*, 1, bool> I; 49193323Sedpublic: 50206083Srdivacky CallSiteBase() : I(0, false) {} 51212904Sdim CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); } 52212904Sdim CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); } 53206083Srdivacky CallSiteBase(ValTy *II) { *this = get(II); } 54218893Sdimprotected: 55206083Srdivacky /// CallSiteBase::get - This static method is sort of like a constructor. It 56206083Srdivacky /// will create an appropriate call site for a Call or Invoke instruction, but 57206083Srdivacky /// it can also create a null initialized CallSiteBase object for something 58206083Srdivacky /// which is NOT a call site. 59193323Sed /// 60206083Srdivacky static CallSiteBase get(ValTy *V) { 61206083Srdivacky if (InstrTy *II = dyn_cast<InstrTy>(V)) { 62206083Srdivacky if (II->getOpcode() == Instruction::Call) 63212904Sdim return CallSiteBase(static_cast<CallTy*>(II)); 64206083Srdivacky else if (II->getOpcode() == Instruction::Invoke) 65212904Sdim return CallSiteBase(static_cast<InvokeTy*>(II)); 66193323Sed } 67206083Srdivacky return CallSiteBase(); 68193323Sed } 69218893Sdimpublic: 70193323Sed /// isCall - true if a CallInst is enclosed. 71193323Sed /// Note that !isCall() does not mean it is an InvokeInst enclosed, 72193323Sed /// it also could signify a NULL Instruction pointer. 73193323Sed bool isCall() const { return I.getInt(); } 74193323Sed 75193323Sed /// isInvoke - true if a InvokeInst is enclosed. 76193323Sed /// 77193323Sed bool isInvoke() const { return getInstruction() && !I.getInt(); } 78193323Sed 79206083Srdivacky InstrTy *getInstruction() const { return I.getPointer(); } 80206083Srdivacky InstrTy *operator->() const { return I.getPointer(); } 81263509Sdim LLVM_EXPLICIT operator bool() const { return I.getPointer(); } 82193323Sed 83245431Sdim /// getCalledValue - Return the pointer to function that is being called. 84193323Sed /// 85206083Srdivacky ValTy *getCalledValue() const { 86193323Sed assert(getInstruction() && "Not a call or invoke instruction!"); 87206083Srdivacky return *getCallee(); 88193323Sed } 89193323Sed 90193323Sed /// getCalledFunction - Return the function being called if this is a direct 91193323Sed /// call, otherwise return null (if it's an indirect call). 92193323Sed /// 93206083Srdivacky FunTy *getCalledFunction() const { 94206083Srdivacky return dyn_cast<FunTy>(getCalledValue()); 95193323Sed } 96193323Sed 97245431Sdim /// setCalledFunction - Set the callee to the specified value. 98193323Sed /// 99193323Sed void setCalledFunction(Value *V) { 100193323Sed assert(getInstruction() && "Not a call or invoke instruction!"); 101206083Srdivacky *getCallee() = V; 102193323Sed } 103193323Sed 104206083Srdivacky /// isCallee - Determine whether the passed iterator points to the 105206083Srdivacky /// callee operand's Use. 106206083Srdivacky /// 107206083Srdivacky bool isCallee(value_use_iterator<UserTy> UI) const { 108206083Srdivacky return getCallee() == &UI.getUse(); 109206083Srdivacky } 110206083Srdivacky 111206083Srdivacky ValTy *getArgument(unsigned ArgNo) const { 112193323Sed assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!"); 113212904Sdim return *(arg_begin() + ArgNo); 114193323Sed } 115193323Sed 116193323Sed void setArgument(unsigned ArgNo, Value* newVal) { 117193323Sed assert(getInstruction() && "Not a call or invoke instruction!"); 118193323Sed assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!"); 119212904Sdim getInstruction()->setOperand(ArgNo, newVal); 120193323Sed } 121193323Sed 122206083Srdivacky /// Given a value use iterator, returns the argument that corresponds to it. 123206083Srdivacky /// Iterator must actually correspond to an argument. 124206083Srdivacky unsigned getArgumentNo(value_use_iterator<UserTy> I) const { 125206083Srdivacky assert(getInstruction() && "Not a call or invoke instruction!"); 126206083Srdivacky assert(arg_begin() <= &I.getUse() && &I.getUse() < arg_end() 127206083Srdivacky && "Argument # out of range!"); 128206083Srdivacky return &I.getUse() - arg_begin(); 129193323Sed } 130193323Sed 131193323Sed /// arg_iterator - The type of iterator to use when looping over actual 132245431Sdim /// arguments at this call site. 133206083Srdivacky typedef IterTy arg_iterator; 134193323Sed 135193323Sed /// arg_begin/arg_end - Return iterators corresponding to the actual argument 136193323Sed /// list for a call site. 137206083Srdivacky IterTy arg_begin() const { 138193323Sed assert(getInstruction() && "Not a call or invoke instruction!"); 139193323Sed // Skip non-arguments 140212904Sdim return (*this)->op_begin(); 141193323Sed } 142193323Sed 143206083Srdivacky IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); } 144193323Sed bool arg_empty() const { return arg_end() == arg_begin(); } 145193323Sed unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); } 146206083Srdivacky 147207618Srdivacky /// getType - Return the type of the instruction that generated this call site 148207618Srdivacky /// 149226890Sdim Type *getType() const { return (*this)->getType(); } 150207618Srdivacky 151207618Srdivacky /// getCaller - Return the caller function for this call site 152207618Srdivacky /// 153207618Srdivacky FunTy *getCaller() const { return (*this)->getParent()->getParent(); } 154207618Srdivacky 155207618Srdivacky#define CALLSITE_DELEGATE_GETTER(METHOD) \ 156207618Srdivacky InstrTy *II = getInstruction(); \ 157207618Srdivacky return isCall() \ 158207618Srdivacky ? cast<CallInst>(II)->METHOD \ 159207618Srdivacky : cast<InvokeInst>(II)->METHOD 160207618Srdivacky 161207618Srdivacky#define CALLSITE_DELEGATE_SETTER(METHOD) \ 162207618Srdivacky InstrTy *II = getInstruction(); \ 163207618Srdivacky if (isCall()) \ 164207618Srdivacky cast<CallInst>(II)->METHOD; \ 165207618Srdivacky else \ 166207618Srdivacky cast<InvokeInst>(II)->METHOD 167207618Srdivacky 168207618Srdivacky /// getCallingConv/setCallingConv - get or set the calling convention of the 169207618Srdivacky /// call. 170207618Srdivacky CallingConv::ID getCallingConv() const { 171207618Srdivacky CALLSITE_DELEGATE_GETTER(getCallingConv()); 172207618Srdivacky } 173207618Srdivacky void setCallingConv(CallingConv::ID CC) { 174207618Srdivacky CALLSITE_DELEGATE_SETTER(setCallingConv(CC)); 175207618Srdivacky } 176207618Srdivacky 177207618Srdivacky /// getAttributes/setAttributes - get or set the parameter attributes of 178207618Srdivacky /// the call. 179252723Sdim const AttributeSet &getAttributes() const { 180207618Srdivacky CALLSITE_DELEGATE_GETTER(getAttributes()); 181207618Srdivacky } 182252723Sdim void setAttributes(const AttributeSet &PAL) { 183207618Srdivacky CALLSITE_DELEGATE_SETTER(setAttributes(PAL)); 184207618Srdivacky } 185207618Srdivacky 186245431Sdim /// \brief Return true if this function has the given attribute. 187252723Sdim bool hasFnAttr(Attribute::AttrKind A) const { 188245431Sdim CALLSITE_DELEGATE_GETTER(hasFnAttr(A)); 189207618Srdivacky } 190207618Srdivacky 191245431Sdim /// \brief Return true if the call or the callee has the given attribute. 192252723Sdim bool paramHasAttr(unsigned i, Attribute::AttrKind A) const { 193245431Sdim CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A)); 194245431Sdim } 195245431Sdim 196207618Srdivacky /// @brief Extract the alignment for a call or parameter (0=unknown). 197207618Srdivacky uint16_t getParamAlignment(uint16_t i) const { 198207618Srdivacky CALLSITE_DELEGATE_GETTER(getParamAlignment(i)); 199207618Srdivacky } 200207618Srdivacky 201263509Sdim /// \brief Return true if the call should not be treated as a call to a 202263509Sdim /// builtin. 203263509Sdim bool isNoBuiltin() const { 204263509Sdim CALLSITE_DELEGATE_GETTER(isNoBuiltin()); 205263509Sdim } 206263509Sdim 207207618Srdivacky /// @brief Return true if the call should not be inlined. 208207618Srdivacky bool isNoInline() const { 209207618Srdivacky CALLSITE_DELEGATE_GETTER(isNoInline()); 210207618Srdivacky } 211207618Srdivacky void setIsNoInline(bool Value = true) { 212210299Sed CALLSITE_DELEGATE_SETTER(setIsNoInline(Value)); 213207618Srdivacky } 214210299Sed 215207618Srdivacky /// @brief Determine if the call does not access memory. 216207618Srdivacky bool doesNotAccessMemory() const { 217207618Srdivacky CALLSITE_DELEGATE_GETTER(doesNotAccessMemory()); 218207618Srdivacky } 219245431Sdim void setDoesNotAccessMemory() { 220245431Sdim CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory()); 221207618Srdivacky } 222207618Srdivacky 223207618Srdivacky /// @brief Determine if the call does not access or only reads memory. 224207618Srdivacky bool onlyReadsMemory() const { 225207618Srdivacky CALLSITE_DELEGATE_GETTER(onlyReadsMemory()); 226207618Srdivacky } 227245431Sdim void setOnlyReadsMemory() { 228245431Sdim CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory()); 229207618Srdivacky } 230207618Srdivacky 231207618Srdivacky /// @brief Determine if the call cannot return. 232207618Srdivacky bool doesNotReturn() const { 233207618Srdivacky CALLSITE_DELEGATE_GETTER(doesNotReturn()); 234207618Srdivacky } 235245431Sdim void setDoesNotReturn() { 236245431Sdim CALLSITE_DELEGATE_SETTER(setDoesNotReturn()); 237207618Srdivacky } 238207618Srdivacky 239207618Srdivacky /// @brief Determine if the call cannot unwind. 240207618Srdivacky bool doesNotThrow() const { 241207618Srdivacky CALLSITE_DELEGATE_GETTER(doesNotThrow()); 242207618Srdivacky } 243245431Sdim void setDoesNotThrow() { 244245431Sdim CALLSITE_DELEGATE_SETTER(setDoesNotThrow()); 245207618Srdivacky } 246207618Srdivacky 247207618Srdivacky#undef CALLSITE_DELEGATE_GETTER 248207618Srdivacky#undef CALLSITE_DELEGATE_SETTER 249207618Srdivacky 250235633Sdim /// @brief Determine whether this argument is not captured. 251235633Sdim bool doesNotCapture(unsigned ArgNo) const { 252252723Sdim return paramHasAttr(ArgNo + 1, Attribute::NoCapture); 253235633Sdim } 254235633Sdim 255235633Sdim /// @brief Determine whether this argument is passed by value. 256235633Sdim bool isByValArgument(unsigned ArgNo) const { 257252723Sdim return paramHasAttr(ArgNo + 1, Attribute::ByVal); 258235633Sdim } 259235633Sdim 260263509Sdim bool doesNotAccessMemory(unsigned ArgNo) const { 261263509Sdim return paramHasAttr(ArgNo + 1, Attribute::ReadNone); 262263509Sdim } 263263509Sdim 264263509Sdim bool onlyReadsMemory(unsigned ArgNo) const { 265263509Sdim return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) || 266263509Sdim paramHasAttr(ArgNo + 1, Attribute::ReadNone); 267263509Sdim } 268263509Sdim 269207618Srdivacky /// hasArgument - Returns true if this CallSite passes the given Value* as an 270207618Srdivacky /// argument to the called function. 271207618Srdivacky bool hasArgument(const Value *Arg) const { 272207618Srdivacky for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E; 273207618Srdivacky ++AI) 274207618Srdivacky if (AI->get() == Arg) 275207618Srdivacky return true; 276207618Srdivacky return false; 277207618Srdivacky } 278207618Srdivacky 279206083Srdivackyprivate: 280206083Srdivacky unsigned getArgumentEndOffset() const { 281206083Srdivacky if (isCall()) 282212904Sdim return 1; // Skip Callee 283206083Srdivacky else 284212904Sdim return 3; // Skip BB, BB, Callee 285193323Sed } 286193323Sed 287206083Srdivacky IterTy getCallee() const { 288212904Sdim if (isCall()) // Skip Callee 289212904Sdim return cast<CallInst>(getInstruction())->op_end() - 1; 290212904Sdim else // Skip BB, BB, Callee 291212904Sdim return cast<InvokeInst>(getInstruction())->op_end() - 3; 292193323Sed } 293193323Sed}; 294193323Sed 295206083Srdivackyclass CallSite : public CallSiteBase<Function, Value, User, Instruction, 296206083Srdivacky CallInst, InvokeInst, User::op_iterator> { 297206083Srdivacky typedef CallSiteBase<Function, Value, User, Instruction, 298207618Srdivacky CallInst, InvokeInst, User::op_iterator> Base; 299206083Srdivackypublic: 300206083Srdivacky CallSite() {} 301207618Srdivacky CallSite(Base B) : Base(B) {} 302212904Sdim CallSite(Value* V) : Base(V) {} 303207618Srdivacky CallSite(CallInst *CI) : Base(CI) {} 304207618Srdivacky CallSite(InvokeInst *II) : Base(II) {} 305207618Srdivacky CallSite(Instruction *II) : Base(II) {} 306206083Srdivacky 307206083Srdivacky bool operator==(const CallSite &CS) const { return I == CS.I; } 308206083Srdivacky bool operator!=(const CallSite &CS) const { return I != CS.I; } 309206083Srdivacky bool operator<(const CallSite &CS) const { 310206083Srdivacky return getInstruction() < CS.getInstruction(); 311206083Srdivacky } 312206083Srdivacky 313206083Srdivackyprivate: 314206083Srdivacky User::op_iterator getCallee() const; 315206083Srdivacky}; 316206083Srdivacky 317212904Sdim/// ImmutableCallSite - establish a view to a call site for examination 318212904Sdimclass ImmutableCallSite : public CallSiteBase<> { 319212904Sdim typedef CallSiteBase<> Base; 320212904Sdimpublic: 321212904Sdim ImmutableCallSite(const Value* V) : Base(V) {} 322212904Sdim ImmutableCallSite(const CallInst *CI) : Base(CI) {} 323212904Sdim ImmutableCallSite(const InvokeInst *II) : Base(II) {} 324212904Sdim ImmutableCallSite(const Instruction *II) : Base(II) {} 325212904Sdim ImmutableCallSite(CallSite CS) : Base(CS.getInstruction()) {} 326212904Sdim}; 327212904Sdim 328193323Sed} // End llvm namespace 329193323Sed 330193323Sed#endif 331