Counter Strike : Global Offensive Source Code

315 lines
11 KiB

  1. //===-- llvm/Support/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_SUPPORT_CALLSITE_H
  26. #define LLVM_SUPPORT_CALLSITE_H
  27. #include "llvm/ADT/PointerIntPair.h"
  28. #include "llvm/IR/Attributes.h"
  29. #include "llvm/IR/CallingConv.h"
  30. #include "llvm/IR/Instructions.h"
  31. namespace llvm {
  32. class CallInst;
  33. class InvokeInst;
  34. template <typename FunTy = const Function,
  35. typename ValTy = const Value,
  36. typename UserTy = const User,
  37. typename InstrTy = const Instruction,
  38. typename CallTy = const CallInst,
  39. typename InvokeTy = const InvokeInst,
  40. typename IterTy = User::const_op_iterator>
  41. class CallSiteBase {
  42. protected:
  43. PointerIntPair<InstrTy*, 1, bool> I;
  44. public:
  45. CallSiteBase() : I(0, false) {}
  46. CallSiteBase(CallTy *CI) : I(CI, true) { assert(CI); }
  47. CallSiteBase(InvokeTy *II) : I(II, false) { assert(II); }
  48. CallSiteBase(ValTy *II) { *this = get(II); }
  49. protected:
  50. /// CallSiteBase::get - This static method is sort of like a constructor. It
  51. /// will create an appropriate call site for a Call or Invoke instruction, but
  52. /// it can also create a null initialized CallSiteBase object for something
  53. /// which is NOT a call site.
  54. ///
  55. static CallSiteBase get(ValTy *V) {
  56. if (InstrTy *II = dyn_cast<InstrTy>(V)) {
  57. if (II->getOpcode() == Instruction::Call)
  58. return CallSiteBase(static_cast<CallTy*>(II));
  59. else if (II->getOpcode() == Instruction::Invoke)
  60. return CallSiteBase(static_cast<InvokeTy*>(II));
  61. }
  62. return CallSiteBase();
  63. }
  64. public:
  65. /// isCall - true if a CallInst is enclosed.
  66. /// Note that !isCall() does not mean it is an InvokeInst enclosed,
  67. /// it also could signify a NULL Instruction pointer.
  68. bool isCall() const { return I.getInt(); }
  69. /// isInvoke - true if a InvokeInst is enclosed.
  70. ///
  71. bool isInvoke() const { return getInstruction() && !I.getInt(); }
  72. InstrTy *getInstruction() const { return I.getPointer(); }
  73. InstrTy *operator->() const { return I.getPointer(); }
  74. operator bool() const { return I.getPointer(); }
  75. /// getCalledValue - Return the pointer to function that is being called.
  76. ///
  77. ValTy *getCalledValue() const {
  78. assert(getInstruction() && "Not a call or invoke instruction!");
  79. return *getCallee();
  80. }
  81. /// getCalledFunction - Return the function being called if this is a direct
  82. /// call, otherwise return null (if it's an indirect call).
  83. ///
  84. FunTy *getCalledFunction() const {
  85. return dyn_cast<FunTy>(getCalledValue());
  86. }
  87. /// setCalledFunction - Set the callee to the specified value.
  88. ///
  89. void setCalledFunction(Value *V) {
  90. assert(getInstruction() && "Not a call or invoke instruction!");
  91. *getCallee() = V;
  92. }
  93. /// isCallee - Determine whether the passed iterator points to the
  94. /// callee operand's Use.
  95. ///
  96. bool isCallee(value_use_iterator<UserTy> UI) const {
  97. return getCallee() == &UI.getUse();
  98. }
  99. ValTy *getArgument(unsigned ArgNo) const {
  100. assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
  101. return *(arg_begin() + ArgNo);
  102. }
  103. void setArgument(unsigned ArgNo, Value* newVal) {
  104. assert(getInstruction() && "Not a call or invoke instruction!");
  105. assert(arg_begin() + ArgNo < arg_end() && "Argument # out of range!");
  106. getInstruction()->setOperand(ArgNo, newVal);
  107. }
  108. /// Given a value use iterator, returns the argument that corresponds to it.
  109. /// Iterator must actually correspond to an argument.
  110. unsigned getArgumentNo(value_use_iterator<UserTy> I) const {
  111. assert(getInstruction() && "Not a call or invoke instruction!");
  112. assert(arg_begin() <= &I.getUse() && &I.getUse() < arg_end()
  113. && "Argument # out of range!");
  114. return &I.getUse() - arg_begin();
  115. }
  116. /// arg_iterator - The type of iterator to use when looping over actual
  117. /// arguments at this call site.
  118. typedef IterTy arg_iterator;
  119. /// arg_begin/arg_end - Return iterators corresponding to the actual argument
  120. /// list for a call site.
  121. IterTy arg_begin() const {
  122. assert(getInstruction() && "Not a call or invoke instruction!");
  123. // Skip non-arguments
  124. return (*this)->op_begin();
  125. }
  126. IterTy arg_end() const { return (*this)->op_end() - getArgumentEndOffset(); }
  127. bool arg_empty() const { return arg_end() == arg_begin(); }
  128. unsigned arg_size() const { return unsigned(arg_end() - arg_begin()); }
  129. /// getType - Return the type of the instruction that generated this call site
  130. ///
  131. Type *getType() const { return (*this)->getType(); }
  132. /// getCaller - Return the caller function for this call site
  133. ///
  134. FunTy *getCaller() const { return (*this)->getParent()->getParent(); }
  135. #define CALLSITE_DELEGATE_GETTER(METHOD) \
  136. InstrTy *II = getInstruction(); \
  137. return isCall() \
  138. ? cast<CallInst>(II)->METHOD \
  139. : cast<InvokeInst>(II)->METHOD
  140. #define CALLSITE_DELEGATE_SETTER(METHOD) \
  141. InstrTy *II = getInstruction(); \
  142. if (isCall()) \
  143. cast<CallInst>(II)->METHOD; \
  144. else \
  145. cast<InvokeInst>(II)->METHOD
  146. /// getCallingConv/setCallingConv - get or set the calling convention of the
  147. /// call.
  148. CallingConv::ID getCallingConv() const {
  149. CALLSITE_DELEGATE_GETTER(getCallingConv());
  150. }
  151. void setCallingConv(CallingConv::ID CC) {
  152. CALLSITE_DELEGATE_SETTER(setCallingConv(CC));
  153. }
  154. /// getAttributes/setAttributes - get or set the parameter attributes of
  155. /// the call.
  156. const AttributeSet &getAttributes() const {
  157. CALLSITE_DELEGATE_GETTER(getAttributes());
  158. }
  159. void setAttributes(const AttributeSet &PAL) {
  160. CALLSITE_DELEGATE_SETTER(setAttributes(PAL));
  161. }
  162. /// \brief Return true if this function has the given attribute.
  163. bool hasFnAttr(Attribute::AttrKind A) const {
  164. CALLSITE_DELEGATE_GETTER(hasFnAttr(A));
  165. }
  166. /// \brief Return true if the call or the callee has the given attribute.
  167. bool paramHasAttr(unsigned i, Attribute::AttrKind A) const {
  168. CALLSITE_DELEGATE_GETTER(paramHasAttr(i, A));
  169. }
  170. /// @brief Extract the alignment for a call or parameter (0=unknown).
  171. uint16_t getParamAlignment(uint16_t i) const {
  172. CALLSITE_DELEGATE_GETTER(getParamAlignment(i));
  173. }
  174. /// @brief Return true if the call should not be inlined.
  175. bool isNoInline() const {
  176. CALLSITE_DELEGATE_GETTER(isNoInline());
  177. }
  178. void setIsNoInline(bool Value = true) {
  179. CALLSITE_DELEGATE_SETTER(setIsNoInline(Value));
  180. }
  181. /// @brief Determine if the call does not access memory.
  182. bool doesNotAccessMemory() const {
  183. CALLSITE_DELEGATE_GETTER(doesNotAccessMemory());
  184. }
  185. void setDoesNotAccessMemory() {
  186. CALLSITE_DELEGATE_SETTER(setDoesNotAccessMemory());
  187. }
  188. /// @brief Determine if the call does not access or only reads memory.
  189. bool onlyReadsMemory() const {
  190. CALLSITE_DELEGATE_GETTER(onlyReadsMemory());
  191. }
  192. void setOnlyReadsMemory() {
  193. CALLSITE_DELEGATE_SETTER(setOnlyReadsMemory());
  194. }
  195. /// @brief Determine if the call cannot return.
  196. bool doesNotReturn() const {
  197. CALLSITE_DELEGATE_GETTER(doesNotReturn());
  198. }
  199. void setDoesNotReturn() {
  200. CALLSITE_DELEGATE_SETTER(setDoesNotReturn());
  201. }
  202. /// @brief Determine if the call cannot unwind.
  203. bool doesNotThrow() const {
  204. CALLSITE_DELEGATE_GETTER(doesNotThrow());
  205. }
  206. void setDoesNotThrow() {
  207. CALLSITE_DELEGATE_SETTER(setDoesNotThrow());
  208. }
  209. #undef CALLSITE_DELEGATE_GETTER
  210. #undef CALLSITE_DELEGATE_SETTER
  211. /// @brief Determine whether this argument is not captured.
  212. bool doesNotCapture(unsigned ArgNo) const {
  213. return paramHasAttr(ArgNo + 1, Attribute::NoCapture);
  214. }
  215. /// @brief Determine whether this argument is passed by value.
  216. bool isByValArgument(unsigned ArgNo) const {
  217. return paramHasAttr(ArgNo + 1, Attribute::ByVal);
  218. }
  219. /// hasArgument - Returns true if this CallSite passes the given Value* as an
  220. /// argument to the called function.
  221. bool hasArgument(const Value *Arg) const {
  222. for (arg_iterator AI = this->arg_begin(), E = this->arg_end(); AI != E;
  223. ++AI)
  224. if (AI->get() == Arg)
  225. return true;
  226. return false;
  227. }
  228. private:
  229. unsigned getArgumentEndOffset() const {
  230. if (isCall())
  231. return 1; // Skip Callee
  232. else
  233. return 3; // Skip BB, BB, Callee
  234. }
  235. IterTy getCallee() const {
  236. if (isCall()) // Skip Callee
  237. return cast<CallInst>(getInstruction())->op_end() - 1;
  238. else // Skip BB, BB, Callee
  239. return cast<InvokeInst>(getInstruction())->op_end() - 3;
  240. }
  241. };
  242. class CallSite : public CallSiteBase<Function, Value, User, Instruction,
  243. CallInst, InvokeInst, User::op_iterator> {
  244. typedef CallSiteBase<Function, Value, User, Instruction,
  245. CallInst, InvokeInst, User::op_iterator> Base;
  246. public:
  247. CallSite() {}
  248. CallSite(Base B) : Base(B) {}
  249. CallSite(Value* V) : Base(V) {}
  250. CallSite(CallInst *CI) : Base(CI) {}
  251. CallSite(InvokeInst *II) : Base(II) {}
  252. CallSite(Instruction *II) : Base(II) {}
  253. bool operator==(const CallSite &CS) const { return I == CS.I; }
  254. bool operator!=(const CallSite &CS) const { return I != CS.I; }
  255. bool operator<(const CallSite &CS) const {
  256. return getInstruction() < CS.getInstruction();
  257. }
  258. private:
  259. User::op_iterator getCallee() const;
  260. };
  261. /// ImmutableCallSite - establish a view to a call site for examination
  262. class ImmutableCallSite : public CallSiteBase<> {
  263. typedef CallSiteBase<> Base;
  264. public:
  265. ImmutableCallSite(const Value* V) : Base(V) {}
  266. ImmutableCallSite(const CallInst *CI) : Base(CI) {}
  267. ImmutableCallSite(const InvokeInst *II) : Base(II) {}
  268. ImmutableCallSite(const Instruction *II) : Base(II) {}
  269. ImmutableCallSite(CallSite CS) : Base(CS.getInstruction()) {}
  270. };
  271. } // End llvm namespace
  272. #endif