CallSite.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. //===- CallSite.h - Abstract Call & Invoke instrs ---------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines the CallSite class, which is a handy wrapper for code that
  11. // wants to treat Call and Invoke instructions in a generic way. When in non-
  12. // mutation context (e.g. an analysis) ImmutableCallSite should be used.
  13. // Finally, when some degree of customization is necessary between these two
  14. // extremes, CallSiteBase<> can be supplied with fine-tuned parameters.
  15. //
  16. // NOTE: These classes are supposed to have "value semantics". So they should be
  17. // passed by value, not by reference; they should not be "new"ed or "delete"d.
  18. // They are efficiently copyable, assignable and constructable, with cost
  19. // equivalent to copying a pointer (notice that they have only a single data
  20. // member). The internal representation carries a flag which indicates which of
  21. // the two variants is enclosed. This allows for cheaper checks when various
  22. // accessors of CallSite are employed.
  23. //
  24. //===----------------------------------------------------------------------===//
  25. #ifndef LLVM_IR_CALLSITE_H
  26. #define LLVM_IR_CALLSITE_H
  27. #include "llvm/ADT/PointerIntPair.h"
  28. #include "llvm/ADT/iterator_range.h"
  29. #include "llvm/IR/Attributes.h"
  30. #include "llvm/IR/CallingConv.h"
  31. #include "llvm/IR/Instructions.h"
  32. namespace llvm {
  33. class CallInst;
  34. class InvokeInst;
  35. template <typename FunTy = const Function,
  36. typename BBTy = const BasicBlock,
  37. typename ValTy = const Value,
  38. typename UserTy = const User,
  39. typename InstrTy = const Instruction,
  40. typename CallTy = const CallInst,
  41. typename InvokeTy = const InvokeInst,
  42. typename IterTy = User::const_op_iterator>
  43. class CallSiteBase {
  44. protected:
  45. PointerIntPair<InstrTy*, 1, bool> I;
  46. CallSiteBase() : I(nullptr, false) {}
  47. CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); }
  48. CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); }
  49. explicit CallSiteBase(ValTy *II) { *this = get(II); }
  50. private:
  51. /// CallSiteBase::get - This static method is sort of like a constructor. It
  52. /// will create an appropriate call site for a Call or Invoke instruction, but
  53. /// it can also create a null initialized CallSiteBase object for something
  54. /// which is NOT a call site.
  55. ///
  56. static CallSiteBase get(ValTy *V) {
  57. if (InstrTy *II = dyn_cast<InstrTy>(V)) {
  58. if (II->getOpcode() == Instruction::Call)
  59. return CallSiteBase(static_cast<CallTy*>(II));
  60. else if (II->getOpcode() == Instruction::Invoke)
  61. return CallSiteBase(static_cast<InvokeTy*>(II));
  62. }
  63. return CallSiteBase();
  64. }
  65. public:
  66. /// isCall - true if a CallInst is enclosed.
  67. /// Note that !isCall() does not mean it is an InvokeInst enclosed,
  68. /// it also could signify a NULL Instruction pointer.
  69. bool isCall() const { return I.getInt(); }
  70. /// isInvoke - true if a InvokeInst is enclosed.
  71. ///
  72. bool isInvoke() const { return getInstruction() && !I.getInt(); }
  73. InstrTy *getInstruction() const { return I.getPointer(); }
  74. InstrTy *operator->() const { return I.getPointer(); }
  75. explicit operator bool() const { return I.getPointer(); }
  76. /// Get the basic block containing the call site
  77. BBTy* getParent() const { return getInstruction()->getParent(); }
  78. /// getCalledValue - Return the pointer to function that is being called.
  79. ///
  80. ValTy *getCalledValue() const {
  81. assert(getInstruction() && "Not a call or invoke instruction!");
  82. return *getCallee();
  83. }
  84. /// getCalledFunction - Return the function being called if this is a direct
  85. /// call, otherwise return null (if it's an indirect call).
  86. ///
  87. FunTy *getCalledFunction() const {
  88. return dyn_cast<FunTy>(getCalledValue());
  89. }
  90. /// setCalledFunction - Set the callee to the specified value.
  91. ///
  92. void setCalledFunction(Value *V) {
  93. assert(getInstruction() && "Not a call or invoke instruction!");
  94. *getCallee() = V;
  95. }
  96. /// isCallee - Determine whether the passed iterator points to the
  97. /// callee operand's Use.
  98. bool isCallee(Value::const_user_iterator UI) const {
  99. return isCallee(&UI.getUse());
  100. }
  101. /// Determine whether this Use is the callee operand's Use.
  102. bool isCallee(const Use *U) const { return getCallee() == U; }
  103. ValTy *getArgument(unsigned ArgNo) const {
  104. assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
  105. return *(arg_begin() + ArgNo);
  106. }
  107. void setArgument(unsigned ArgNo, Value* newVal) {
  108. assert(getInstruction() && "Not a call or invoke instruction!");
  109. assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
  110. getInstruction()->setOperand(ArgNo, newVal);
  111. }
  112. /// Given a value use iterator, returns the argument that corresponds to it.
  113. /// Iterator must actually correspond to an argument.
  114. unsigned getArgumentNo(Value::const_user_iterator I) const {
  115. return getArgumentNo(&I.getUse());
  116. }
  117. /// Given a use for an argument, get the argument number that corresponds to
  118. /// it.
  119. unsigned getArgumentNo(const Use *U) const {
  120. assert(getInstruction() && "Not a call or invoke instruction!");
  121. assert(arg_begin() <= U && U < arg_end()
  122. && "Argument # out of range!");
  123. return U - arg_begin();
  124. }
  125. /// arg_iterator - The type of iterator to use when looping over actual
  126. /// arguments at this call site.
  127. typedef IterTy arg_iterator;
  128. /// arg_begin/arg_end - Return iterators corresponding to the actual argument
  129. /// list for a call site.
  130. IterTy arg_begin() const {
  131. assert(getInstruction() && "Not a call or invoke instruction!");
  132. // Skip non-arguments
  133. return (*this)->op_begin();
  134. }
  135. IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
  136. iterator_range<IterTy> args() const {
  137. return iterator_range<IterTy>(arg_begin(), arg_end());
  138. }
  139. bool arg_empty() const { return arg_end() == arg_begin(); }
  140. unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
  141. /// getType - Return the type of the instruction that generated this call site
  142. ///
  143. Type *getType() const { return (*this)->getType(); }
  144. /// getCaller - Return the caller function for this call site
  145. ///
  146. FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
  147. /// \brief Tests if this call site must be tail call optimized. Only a
  148. /// CallInst can be tail call optimized.
  149. bool isMustTailCall() const {
  150. return isCall() && cast<CallInst>(getInstruction())->isMustTailCall();
  151. }
  152. /// \brief Tests if this call site is marked as a tail call.
  153. bool isTailCall() const {
  154. return isCall() && cast<CallInst>(getInstruction())->isTailCall();
  155. }
  156. #define CALLSITE_DELEGATE_GETTER(METHOD) \
  157. InstrTy *II = getInstruction(); \
  158. return isCall() \
  159. ? cast<CallInst>(II)->METHOD \
  160. : cast<InvokeInst>(II)->METHOD
  161. #define CALLSITE_DELEGATE_SETTER(METHOD) \
  162. InstrTy *II = getInstruction(); \
  163. if (isCall()) \
  164. cast<CallInst>(II)->METHOD; \
  165. else \
  166. cast<InvokeInst>(II)->METHOD
  167. unsigned getNumArgOperands() const {
  168. CALLSITE_DELEGATE_GETTER(getNumArgOperands());
  169. }
  170. ValTy *getArgOperand(unsigned i) const {
  171. CALLSITE_DELEGATE_GETTER(getArgOperand(i));
  172. }
  173. bool isInlineAsm() const {
  174. if (isCall())
  175. return cast<CallInst>(getInstruction())->isInlineAsm();
  176. return false;
  177. }
  178. /// getCallingConv/setCallingConv - get or set the calling convention of the
  179. /// call.
  180. CallingConv::ID getCallingConv() const {
  181. CALLSITE_DELEGATE_GETTER(getCallingConv());
  182. }
  183. void setCallingConv(CallingConv::ID CC) {
  184. CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
  185. }
  186. FunctionType *getFunctionType() const {
  187. CALLSITE_DELEGATE_GETTER(getFunctionType());
  188. }
  189. void mutateFunctionType(FunctionType *Ty) const {
  190. CALLSITE_DELEGATE_SETTER(mutateFunctionType(Ty));
  191. }
  192. /// getAttributes/setAttributes - get or set the parameter attributes of
  193. /// the call.
  194. const AttributeSet &getAttributes() const {
  195. CALLSITE_DELEGATE_GETTER(getAttributes());
  196. }
  197. void setAttributes(const AttributeSet &PAL) {
  198. CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
  199. }
  200. /// \brief Return true if this function has the given attribute.
  201. bool hasFnAttr(Attribute::AttrKind A) const {
  202. CALLSITE_DELEGATE_GETTER(hasFnAttr(A));
  203. }
  204. /// \brief Return true if the call or the callee has the given attribute.
  205. bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
  206. CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A));
  207. }
  208. /// @brief Extract the alignment for a call or parameter (0=unknown).
  209. uint16_t getParamAlignment(uint16_t i) const {
  210. CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
  211. }
  212. /// @brief Extract the number of dereferenceable bytes for a call or
  213. /// parameter (0=unknown).
  214. uint64_t getDereferenceableBytes(uint16_t i) const {
  215. CALLSITE_DELEGATE_GETTER(getDereferenceableBytes(i));
  216. }
  217. /// @brief Extract the number of dereferenceable_or_null bytes for a call or
  218. /// parameter (0=unknown).
  219. uint64_t getDereferenceableOrNullBytes(uint16_t i) const {
  220. CALLSITE_DELEGATE_GETTER(getDereferenceableOrNullBytes(i));
  221. }
  222. /// \brief Return true if the call should not be treated as a call to a
  223. /// builtin.
  224. bool isNoBuiltin() const {
  225. CALLSITE_DELEGATE_GETTER(isNoBuiltin());
  226. }
  227. /// @brief Return true if the call should not be inlined.
  228. bool isNoInline() const {
  229. CALLSITE_DELEGATE_GETTER(isNoInline());
  230. }
  231. void setIsNoInline(bool Value = true) {
  232. CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
  233. }
  234. /// @brief Determine if the call does not access memory.
  235. bool doesNotAccessMemory() const {
  236. CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
  237. }
  238. void setDoesNotAccessMemory() {
  239. CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
  240. }
  241. /// @brief Determine if the call does not access or only reads memory.
  242. bool onlyReadsMemory() const {
  243. CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
  244. }
  245. void setOnlyReadsMemory() {
  246. CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
  247. }
  248. /// @brief Determine if the call can access memmory only using pointers based
  249. /// on its arguments.
  250. bool onlyAccessesArgMemory() const {
  251. CALLSITE_DELEGATE_GETTER(onlyAccessesArgMemory());
  252. }
  253. void setOnlyAccessesArgMemory() {
  254. CALLSITE_DELEGATE_SETTER(setOnlyAccessesArgMemory());
  255. }
  256. /// @brief Determine if the call cannot return.
  257. bool doesNotReturn() const {
  258. CALLSITE_DELEGATE_GETTER(doesNotReturn());
  259. }
  260. void setDoesNotReturn() {
  261. CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
  262. }
  263. /// @brief Determine if the call cannot unwind.
  264. bool doesNotThrow() const {
  265. CALLSITE_DELEGATE_GETTER(doesNotThrow());
  266. }
  267. void setDoesNotThrow() {
  268. CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
  269. }
  270. #undef CALLSITE_DELEGATE_GETTER
  271. #undef CALLSITE_DELEGATE_SETTER
  272. /// @brief Determine whether this argument is not captured.
  273. bool doesNotCapture(unsigned ArgNo) const {
  274. return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
  275. }
  276. /// @brief Determine whether this argument is passed by value.
  277. bool isByValArgument(unsigned ArgNo) const {
  278. return paramHasAttr(ArgNo + 1, Attribute::ByVal);
  279. }
  280. /// @brief Determine whether this argument is passed in an alloca.
  281. bool isInAllocaArgument(unsigned ArgNo) const {
  282. return paramHasAttr(ArgNo + 1, Attribute::InAlloca);
  283. }
  284. /// @brief Determine whether this argument is passed by value or in an alloca.
  285. bool isByValOrInAllocaArgument(unsigned ArgNo) const {
  286. return paramHasAttr(ArgNo + 1, Attribute::ByVal) ||
  287. paramHasAttr(ArgNo + 1, Attribute::InAlloca);
  288. }
  289. /// @brief Determine if there are is an inalloca argument. Only the last
  290. /// argument can have the inalloca attribute.
  291. bool hasInAllocaArgument() const {
  292. return paramHasAttr(arg_size(), Attribute::InAlloca);
  293. }
  294. bool doesNotAccessMemory(unsigned ArgNo) const {
  295. return paramHasAttr(ArgNo + 1, Attribute::ReadNone);
  296. }
  297. bool onlyReadsMemory(unsigned ArgNo) const {
  298. return paramHasAttr(ArgNo + 1, Attribute::ReadOnly) ||
  299. paramHasAttr(ArgNo + 1, Attribute::ReadNone);
  300. }
  301. /// @brief Return true if the return value is known to be not null.
  302. /// This may be because it has the nonnull attribute, or because at least
  303. /// one byte is dereferenceable and the pointer is in addrspace(0).
  304. bool isReturnNonNull() const {
  305. if (paramHasAttr(0, Attribute::NonNull))
  306. return true;
  307. else if (getDereferenceableBytes(0) > 0 &&
  308. getType()->getPointerAddressSpace() == 0)
  309. return true;
  310. return false;
  311. }
  312. /// hasArgument - Returns true if this CallSite passes the given Value* as an
  313. /// argument to the called function.
  314. bool hasArgument(const Value *Arg) const {
  315. for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
  316. ++AI)
  317. if (AI->get() == Arg)
  318. return true;
  319. return false;
  320. }
  321. private:
  322. unsigned getArgumentEndOffset() const {
  323. if (isCall())
  324. return 1; // Skip Callee
  325. else
  326. return 3; // Skip BB, BB, Callee
  327. }
  328. IterTy getCallee() const {
  329. if (isCall()) // Skip Callee
  330. return cast<CallInst>(getInstruction())->op_end() - 1;
  331. else // Skip BB, BB, Callee
  332. return cast<InvokeInst>(getInstruction())->op_end() - 3;
  333. }
  334. };
  335. class CallSite : public CallSiteBase<Function, BasicBlock, Value, User,
  336. Instruction, CallInst, InvokeInst,
  337. User::op_iterator> {
  338. public:
  339. CallSite() {}
  340. CallSite(CallSiteBase B) : CallSiteBase(B) {}
  341. CallSite(CallInst *CI) : CallSiteBase(CI) {}
  342. CallSite(InvokeInst *II) : CallSiteBase(II) {}
  343. explicit CallSite(Instruction *II) : CallSiteBase(II) {}
  344. explicit CallSite(Value *V) : CallSiteBase(V) {}
  345. bool operator==(const CallSite &CS) const { return I == CS.I; }
  346. bool operator!=(const CallSite &CS) const { return I != CS.I; }
  347. bool operator<(const CallSite &CS) const {
  348. return getInstruction() < CS.getInstruction();
  349. }
  350. private:
  351. User::op_iterator getCallee() const;
  352. };
  353. /// ImmutableCallSite - establish a view to a call site for examination
  354. class ImmutableCallSite : public CallSiteBase<> {
  355. public:
  356. ImmutableCallSite() {}
  357. ImmutableCallSite(const CallInst *CI) : CallSiteBase(CI) {}
  358. ImmutableCallSite(const InvokeInst *II) : CallSiteBase(II) {}
  359. explicit ImmutableCallSite(const Instruction *II) : CallSiteBase(II) {}
  360. explicit ImmutableCallSite(const Value *V) : CallSiteBase(V) {}
  361. ImmutableCallSite(CallSite CS) : CallSiteBase(CS.getInstruction()) {}
  362. };
  363. } // End llvm namespace
  364. #endif