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.

316 lines
10 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_IR_INTRINSICINST_H
  24. #define LLVM_IR_INTRINSICINST_H
  25. #include "llvm/IR/Constants.h"
  26. #include "llvm/IR/Function.h"
  27. #include "llvm/IR/Instructions.h"
  28. #include "llvm/IR/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 CallInst *I) {
  45. if (const Function *CF = I->getCalledFunction())
  46. return CF->isIntrinsic();
  47. return false;
  48. }
  49. static inline bool classof(const Value *V) {
  50. return isa<CallInst>(V) && classof(cast<CallInst>(V));
  51. }
  52. };
  53. /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
  54. ///
  55. class DbgInfoIntrinsic : public IntrinsicInst {
  56. public:
  57. // Methods for support type inquiry through isa, cast, and dyn_cast:
  58. static inline bool classof(const IntrinsicInst *I) {
  59. switch (I->getIntrinsicID()) {
  60. case Intrinsic::dbg_declare:
  61. case Intrinsic::dbg_value:
  62. return true;
  63. default: return false;
  64. }
  65. }
  66. static inline bool classof(const Value *V) {
  67. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  68. }
  69. static Value *StripCast(Value *C);
  70. };
  71. /// DbgDeclareInst - This represents the llvm.dbg.declare instruction.
  72. ///
  73. class DbgDeclareInst : public DbgInfoIntrinsic {
  74. public:
  75. Value *getAddress() const;
  76. MDNode *getVariable() const { return cast<MDNode>(getArgOperand(1)); }
  77. // Methods for support type inquiry through isa, cast, and dyn_cast:
  78. static inline bool classof(const IntrinsicInst *I) {
  79. return I->getIntrinsicID() == Intrinsic::dbg_declare;
  80. }
  81. static inline bool classof(const Value *V) {
  82. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  83. }
  84. };
  85. /// DbgValueInst - This represents the llvm.dbg.value instruction.
  86. ///
  87. class DbgValueInst : public DbgInfoIntrinsic {
  88. public:
  89. const Value *getValue() const;
  90. Value *getValue();
  91. uint64_t getOffset() const {
  92. return cast<ConstantInt>(
  93. const_cast<Value*>(getArgOperand(1)))->getZExtValue();
  94. }
  95. MDNode *getVariable() const { return cast<MDNode>(getArgOperand(2)); }
  96. // Methods for support type inquiry through isa, cast, and dyn_cast:
  97. static inline bool classof(const IntrinsicInst *I) {
  98. return I->getIntrinsicID() == Intrinsic::dbg_value;
  99. }
  100. static inline bool classof(const Value *V) {
  101. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  102. }
  103. };
  104. /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
  105. ///
  106. class MemIntrinsic : public IntrinsicInst {
  107. public:
  108. Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); }
  109. Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); }
  110. ConstantInt *getAlignmentCst() const {
  111. return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3)));
  112. }
  113. unsigned getAlignment() const {
  114. return getAlignmentCst()->getZExtValue();
  115. }
  116. ConstantInt *getVolatileCst() const {
  117. return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4)));
  118. }
  119. bool isVolatile() const {
  120. return !getVolatileCst()->isZero();
  121. }
  122. unsigned getDestAddressSpace() const {
  123. return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
  124. }
  125. /// getDest - This is just like getRawDest, but it strips off any cast
  126. /// instructions that feed it, giving the original input. The returned
  127. /// value is guaranteed to be a pointer.
  128. Value *getDest() const { return getRawDest()->stripPointerCasts(); }
  129. /// set* - Set the specified arguments of the instruction.
  130. ///
  131. void setDest(Value *Ptr) {
  132. assert(getRawDest()->getType() == Ptr->getType() &&
  133. "setDest called with pointer of wrong type!");
  134. setArgOperand(0, Ptr);
  135. }
  136. void setLength(Value *L) {
  137. assert(getLength()->getType() == L->getType() &&
  138. "setLength called with value of wrong type!");
  139. setArgOperand(2, L);
  140. }
  141. void setAlignment(Constant* A) {
  142. setArgOperand(3, A);
  143. }
  144. void setVolatile(Constant* V) {
  145. setArgOperand(4, V);
  146. }
  147. Type *getAlignmentType() const {
  148. return getArgOperand(3)->getType();
  149. }
  150. // Methods for support type inquiry through isa, cast, and dyn_cast:
  151. static inline bool classof(const IntrinsicInst *I) {
  152. switch (I->getIntrinsicID()) {
  153. case Intrinsic::memcpy:
  154. case Intrinsic::memmove:
  155. case Intrinsic::memset:
  156. return true;
  157. default: return false;
  158. }
  159. }
  160. static inline bool classof(const Value *V) {
  161. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  162. }
  163. };
  164. /// MemSetInst - This class wraps the llvm.memset intrinsic.
  165. ///
  166. class MemSetInst : public MemIntrinsic {
  167. public:
  168. /// get* - Return the arguments to the instruction.
  169. ///
  170. Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); }
  171. void setValue(Value *Val) {
  172. assert(getValue()->getType() == Val->getType() &&
  173. "setValue called with value of wrong type!");
  174. setArgOperand(1, Val);
  175. }
  176. // Methods for support type inquiry through isa, cast, and dyn_cast:
  177. static inline bool classof(const IntrinsicInst *I) {
  178. return I->getIntrinsicID() == Intrinsic::memset;
  179. }
  180. static inline bool classof(const Value *V) {
  181. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  182. }
  183. };
  184. /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
  185. ///
  186. class MemTransferInst : public MemIntrinsic {
  187. public:
  188. /// get* - Return the arguments to the instruction.
  189. ///
  190. Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); }
  191. /// getSource - This is just like getRawSource, but it strips off any cast
  192. /// instructions that feed it, giving the original input. The returned
  193. /// value is guaranteed to be a pointer.
  194. Value *getSource() const { return getRawSource()->stripPointerCasts(); }
  195. unsigned getSourceAddressSpace() const {
  196. return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
  197. }
  198. void setSource(Value *Ptr) {
  199. assert(getRawSource()->getType() == Ptr->getType() &&
  200. "setSource called with pointer of wrong type!");
  201. setArgOperand(1, Ptr);
  202. }
  203. // Methods for support type inquiry through isa, cast, and dyn_cast:
  204. static inline bool classof(const IntrinsicInst *I) {
  205. return I->getIntrinsicID() == Intrinsic::memcpy ||
  206. I->getIntrinsicID() == Intrinsic::memmove;
  207. }
  208. static inline bool classof(const Value *V) {
  209. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  210. }
  211. };
  212. /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
  213. ///
  214. class MemCpyInst : public MemTransferInst {
  215. public:
  216. // Methods for support type inquiry through isa, cast, and dyn_cast:
  217. static inline bool classof(const IntrinsicInst *I) {
  218. return I->getIntrinsicID() == Intrinsic::memcpy;
  219. }
  220. static inline bool classof(const Value *V) {
  221. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  222. }
  223. };
  224. /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
  225. ///
  226. class MemMoveInst : public MemTransferInst {
  227. public:
  228. // Methods for support type inquiry through isa, cast, and dyn_cast:
  229. static inline bool classof(const IntrinsicInst *I) {
  230. return I->getIntrinsicID() == Intrinsic::memmove;
  231. }
  232. static inline bool classof(const Value *V) {
  233. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  234. }
  235. };
  236. /// VAStartInst - This represents the llvm.va_start intrinsic.
  237. ///
  238. class VAStartInst : public IntrinsicInst {
  239. public:
  240. static inline bool classof(const IntrinsicInst *I) {
  241. return I->getIntrinsicID() == Intrinsic::vastart;
  242. }
  243. static inline bool classof(const Value *V) {
  244. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  245. }
  246. Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
  247. };
  248. /// VAEndInst - This represents the llvm.va_end intrinsic.
  249. ///
  250. class VAEndInst : public IntrinsicInst {
  251. public:
  252. static inline bool classof(const IntrinsicInst *I) {
  253. return I->getIntrinsicID() == Intrinsic::vaend;
  254. }
  255. static inline bool classof(const Value *V) {
  256. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  257. }
  258. Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
  259. };
  260. /// VACopyInst - This represents the llvm.va_copy intrinsic.
  261. ///
  262. class VACopyInst : public IntrinsicInst {
  263. public:
  264. static inline bool classof(const IntrinsicInst *I) {
  265. return I->getIntrinsicID() == Intrinsic::vacopy;
  266. }
  267. static inline bool classof(const Value *V) {
  268. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  269. }
  270. Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); }
  271. Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); }
  272. };
  273. }
  274. #endif