Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
9.7 KiB

  1. //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- 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 classes that make it really easy to deal with intrinsic
  11. // functions with the isa/dyncast family of functions. In particular, this
  12. // allows you to do things like:
  13. //
  14. // if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst))
  15. // ... MCI->getDest() ... MCI->getSource() ...
  16. //
  17. // All intrinsic function calls are instances of the call instruction, so these
  18. // are all subclasses of the CallInst class. Note that none of these classes
  19. // has state or virtual methods, which is an important part of this gross/neat
  20. // hack working.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #ifndef LLVM_INTRINSICINST_H
  24. #define LLVM_INTRINSICINST_H
  25. #include "llvm/Constants.h"
  26. #include "llvm/Function.h"
  27. #include "llvm/Instructions.h"
  28. #include "llvm/Intrinsics.h"
  29. namespace llvm {
  30. /// IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic
  31. /// functions. This allows the standard isa/dyncast/cast functionality to
  32. /// work with calls to intrinsic functions.
  33. class IntrinsicInst : public CallInst {
  34. IntrinsicInst() LLVM_DELETED_FUNCTION;
  35. IntrinsicInst(const IntrinsicInst&) LLVM_DELETED_FUNCTION;
  36. void operator=(const IntrinsicInst&) LLVM_DELETED_FUNCTION;
  37. public:
  38. /// getIntrinsicID - Return the intrinsic ID of this intrinsic.
  39. ///
  40. Intrinsic::ID getIntrinsicID() const {
  41. return (Intrinsic::ID)getCalledFunction()->getIntrinsicID();
  42. }
  43. // Methods for support type inquiry through isa, cast, and dyn_cast:
  44. static inline bool classof(const IntrinsicInst *) { return true; }
  45. static inline bool classof(const CallInst *I) {
  46. if (const Function *CF = I->getCalledFunction())
  47. return CF->getIntrinsicID() != 0;
  48. return false;
  49. }
  50. static inline bool classof(const Value *V) {
  51. return isa<CallInst>(V) && classof(cast<CallInst>(V));
  52. }
  53. };
  54. /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
  55. ///
  56. class DbgInfoIntrinsic : public IntrinsicInst {
  57. public:
  58. // Methods for support type inquiry through isa, cast, and dyn_cast:
  59. static inline bool classof(const DbgInfoIntrinsic *) { return true; }
  60. static inline bool classof(const IntrinsicInst *I) {
  61. switch (I->getIntrinsicID()) {
  62. case Intrinsic::dbg_declare:
  63. case Intrinsic::dbg_value:
  64. return true;
  65. default: return false;
  66. }
  67. }
  68. static inline bool classof(const Value *V) {
  69. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  70. }
  71. static Value *StripCast(Value *C);
  72. };
  73. /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
  74. ///
  75. class DbgDeclareInst : public DbgInfoIntrinsic {
  76. public:
  77. Value *getAddress() const;
  78. MDNode *getVariable() const { return cast<MDNode>(getArgOperand(1)); }
  79. // Methods for support type inquiry through isa, cast, and dyn_cast:
  80. static inline bool classof(const DbgDeclareInst *) { return true; }
  81. static inline bool classof(const IntrinsicInst *I) {
  82. return I->getIntrinsicID() == Intrinsic::dbg_declare;
  83. }
  84. static inline bool classof(const Value *V) {
  85. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  86. }
  87. };
  88. /// DbgValueInst - This represents the llvm.dbg.value instruction.
  89. ///
  90. class DbgValueInst : public DbgInfoIntrinsic {
  91. public:
  92. const Value *getValue() const;
  93. Value *getValue();
  94. uint64_t getOffset() const {
  95. return cast<ConstantInt>(
  96. const_cast<Value*>(getArgOperand(1)))->getZExtValue();
  97. }
  98. MDNode *getVariable() const { return cast<MDNode>(getArgOperand(2)); }
  99. // Methods for support type inquiry through isa, cast, and dyn_cast:
  100. static inline bool classof(const DbgValueInst *) { return true; }
  101. static inline bool classof(const IntrinsicInst *I) {
  102. return I->getIntrinsicID() == Intrinsic::dbg_value;
  103. }
  104. static inline bool classof(const Value *V) {
  105. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  106. }
  107. };
  108. /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
  109. ///
  110. class MemIntrinsic : public IntrinsicInst {
  111. public:
  112. Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); }
  113. Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); }
  114. ConstantInt *getAlignmentCst() const {
  115. return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3)));
  116. }
  117. unsigned getAlignment() const {
  118. return getAlignmentCst()->getZExtValue();
  119. }
  120. ConstantInt *getVolatileCst() const {
  121. return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4)));
  122. }
  123. bool isVolatile() const {
  124. return !getVolatileCst()->isZero();
  125. }
  126. unsigned getDestAddressSpace() const {
  127. return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
  128. }
  129. /// getDest - This is just like getRawDest, but it strips off any cast
  130. /// instructions that feed it, giving the original input. The returned
  131. /// value is guaranteed to be a pointer.
  132. Value *getDest() const { return getRawDest()->stripPointerCasts(); }
  133. /// set* - Set the specified arguments of the instruction.
  134. ///
  135. void setDest(Value *Ptr) {
  136. assert(getRawDest()->getType() == Ptr->getType() &&
  137. "setDest called with pointer of wrong type!");
  138. setArgOperand(0, Ptr);
  139. }
  140. void setLength(Value *L) {
  141. assert(getLength()->getType() == L->getType() &&
  142. "setLength called with value of wrong type!");
  143. setArgOperand(2, L);
  144. }
  145. void setAlignment(Constant* A) {
  146. setArgOperand(3, A);
  147. }
  148. void setVolatile(Constant* V) {
  149. setArgOperand(4, V);
  150. }
  151. Type *getAlignmentType() const {
  152. return getArgOperand(3)->getType();
  153. }
  154. // Methods for support type inquiry through isa, cast, and dyn_cast:
  155. static inline bool classof(const MemIntrinsic *) { return true; }
  156. static inline bool classof(const IntrinsicInst *I) {
  157. switch (I->getIntrinsicID()) {
  158. case Intrinsic::memcpy:
  159. case Intrinsic::memmove:
  160. case Intrinsic::memset:
  161. return true;
  162. default: return false;
  163. }
  164. }
  165. static inline bool classof(const Value *V) {
  166. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  167. }
  168. };
  169. /// MemSetInst - This class wraps the llvm.memset intrinsic.
  170. ///
  171. class MemSetInst : public MemIntrinsic {
  172. public:
  173. /// get* - Return the arguments to the instruction.
  174. ///
  175. Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); }
  176. void setValue(Value *Val) {
  177. assert(getValue()->getType() == Val->getType() &&
  178. "setValue called with value of wrong type!");
  179. setArgOperand(1, Val);
  180. }
  181. // Methods for support type inquiry through isa, cast, and dyn_cast:
  182. static inline bool classof(const MemSetInst *) { return true; }
  183. static inline bool classof(const IntrinsicInst *I) {
  184. return I->getIntrinsicID() == Intrinsic::memset;
  185. }
  186. static inline bool classof(const Value *V) {
  187. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  188. }
  189. };
  190. /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
  191. ///
  192. class MemTransferInst : public MemIntrinsic {
  193. public:
  194. /// get* - Return the arguments to the instruction.
  195. ///
  196. Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); }
  197. /// getSource - This is just like getRawSource, but it strips off any cast
  198. /// instructions that feed it, giving the original input. The returned
  199. /// value is guaranteed to be a pointer.
  200. Value *getSource() const { return getRawSource()->stripPointerCasts(); }
  201. unsigned getSourceAddressSpace() const {
  202. return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
  203. }
  204. void setSource(Value *Ptr) {
  205. assert(getRawSource()->getType() == Ptr->getType() &&
  206. "setSource called with pointer of wrong type!");
  207. setArgOperand(1, Ptr);
  208. }
  209. // Methods for support type inquiry through isa, cast, and dyn_cast:
  210. static inline bool classof(const MemTransferInst *) { return true; }
  211. static inline bool classof(const IntrinsicInst *I) {
  212. return I->getIntrinsicID() == Intrinsic::memcpy ||
  213. I->getIntrinsicID() == Intrinsic::memmove;
  214. }
  215. static inline bool classof(const Value *V) {
  216. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  217. }
  218. };
  219. /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
  220. ///
  221. class MemCpyInst : public MemTransferInst {
  222. public:
  223. // Methods for support type inquiry through isa, cast, and dyn_cast:
  224. static inline bool classof(const MemCpyInst *) { return true; }
  225. static inline bool classof(const IntrinsicInst *I) {
  226. return I->getIntrinsicID() == Intrinsic::memcpy;
  227. }
  228. static inline bool classof(const Value *V) {
  229. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  230. }
  231. };
  232. /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
  233. ///
  234. class MemMoveInst : public MemTransferInst {
  235. public:
  236. // Methods for support type inquiry through isa, cast, and dyn_cast:
  237. static inline bool classof(const MemMoveInst *) { return true; }
  238. static inline bool classof(const IntrinsicInst *I) {
  239. return I->getIntrinsicID() == Intrinsic::memmove;
  240. }
  241. static inline bool classof(const Value *V) {
  242. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  243. }
  244. };
  245. }
  246. #endif