IntrinsicInst.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include "llvm/IR/Metadata.h"
  30. #include "llvm/IR/DebugInfoMetadata.h" // HLSL Change
  31. namespace llvm {
  32. /// IntrinsicInst - A useful wrapper class for inspecting calls to intrinsic
  33. /// functions. This allows the standard isa/dyncast/cast functionality to
  34. /// work with calls to intrinsic functions.
  35. class IntrinsicInst : public CallInst {
  36. IntrinsicInst() = delete;
  37. IntrinsicInst(const IntrinsicInst&) = delete;
  38. void operator=(const IntrinsicInst&) = delete;
  39. public:
  40. /// getIntrinsicID - Return the intrinsic ID of this intrinsic.
  41. ///
  42. Intrinsic::ID getIntrinsicID() const {
  43. return getCalledFunction()->getIntrinsicID();
  44. }
  45. // Methods for support type inquiry through isa, cast, and dyn_cast:
  46. static inline bool classof(const CallInst *I) {
  47. if (const Function *CF = I->getCalledFunction())
  48. return CF->isIntrinsic();
  49. return false;
  50. }
  51. static inline bool classof(const Value *V) {
  52. return isa<CallInst>(V) && classof(cast<CallInst>(V));
  53. }
  54. };
  55. /// DbgInfoIntrinsic - This is the common base class for debug info intrinsics
  56. ///
  57. class DbgInfoIntrinsic : public IntrinsicInst {
  58. public:
  59. // Methods for support type inquiry through isa, cast, and dyn_cast:
  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. DILocalVariable *getVariable() const {
  79. return cast<DILocalVariable>(getRawVariable());
  80. }
  81. DIExpression *getExpression() const {
  82. return cast<DIExpression>(getRawExpression());
  83. }
  84. void setVariable(DIVariable *v) { setArgOperand(1, MetadataAsValue::get(getContext(), v)); } // HLSL Change
  85. Metadata *getRawVariable() const {
  86. return cast<MetadataAsValue>(getArgOperand(1))->getMetadata();
  87. }
  88. Metadata *getRawExpression() const {
  89. return cast<MetadataAsValue>(getArgOperand(2))->getMetadata();
  90. }
  91. // Methods for support type inquiry through isa, cast, and dyn_cast:
  92. static inline bool classof(const IntrinsicInst *I) {
  93. return I->getIntrinsicID() == Intrinsic::dbg_declare;
  94. }
  95. static inline bool classof(const Value *V) {
  96. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  97. }
  98. };
  99. /// DbgValueInst - This represents the llvm.dbg.value instruction.
  100. ///
  101. class DbgValueInst : public DbgInfoIntrinsic {
  102. public:
  103. const Value *getValue() const;
  104. Value *getValue();
  105. uint64_t getOffset() const {
  106. return cast<ConstantInt>(
  107. const_cast<Value*>(getArgOperand(1)))->getZExtValue();
  108. }
  109. DILocalVariable *getVariable() const {
  110. return cast<DILocalVariable>(getRawVariable());
  111. }
  112. DIExpression *getExpression() const {
  113. return cast<DIExpression>(getRawExpression());
  114. }
  115. void setVariable(DIVariable *v) { setArgOperand(2, MetadataAsValue::get(getContext(), v)); } // HLSL Change
  116. Metadata *getRawVariable() const {
  117. return cast<MetadataAsValue>(getArgOperand(2))->getMetadata();
  118. }
  119. Metadata *getRawExpression() const {
  120. return cast<MetadataAsValue>(getArgOperand(3))->getMetadata();
  121. }
  122. // Methods for support type inquiry through isa, cast, and dyn_cast:
  123. static inline bool classof(const IntrinsicInst *I) {
  124. return I->getIntrinsicID() == Intrinsic::dbg_value;
  125. }
  126. static inline bool classof(const Value *V) {
  127. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  128. }
  129. };
  130. /// MemIntrinsic - This is the common base class for memset/memcpy/memmove.
  131. ///
  132. class MemIntrinsic : public IntrinsicInst {
  133. public:
  134. Value *getRawDest() const { return const_cast<Value*>(getArgOperand(0)); }
  135. const Use &getRawDestUse() const { return getArgOperandUse(0); }
  136. Use &getRawDestUse() { return getArgOperandUse(0); }
  137. Value *getLength() const { return const_cast<Value*>(getArgOperand(2)); }
  138. const Use &getLengthUse() const { return getArgOperandUse(2); }
  139. Use &getLengthUse() { return getArgOperandUse(2); }
  140. ConstantInt *getAlignmentCst() const {
  141. return cast<ConstantInt>(const_cast<Value*>(getArgOperand(3)));
  142. }
  143. unsigned getAlignment() const {
  144. return getAlignmentCst()->getZExtValue();
  145. }
  146. ConstantInt *getVolatileCst() const {
  147. return cast<ConstantInt>(const_cast<Value*>(getArgOperand(4)));
  148. }
  149. bool isVolatile() const {
  150. return !getVolatileCst()->isZero();
  151. }
  152. unsigned getDestAddressSpace() const {
  153. return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
  154. }
  155. /// getDest - This is just like getRawDest, but it strips off any cast
  156. /// instructions that feed it, giving the original input. The returned
  157. /// value is guaranteed to be a pointer.
  158. Value *getDest() const { return getRawDest()->stripPointerCasts(); }
  159. /// set* - Set the specified arguments of the instruction.
  160. ///
  161. void setDest(Value *Ptr) {
  162. assert(getRawDest()->getType() == Ptr->getType() &&
  163. "setDest called with pointer of wrong type!");
  164. setArgOperand(0, Ptr);
  165. }
  166. void setLength(Value *L) {
  167. assert(getLength()->getType() == L->getType() &&
  168. "setLength called with value of wrong type!");
  169. setArgOperand(2, L);
  170. }
  171. void setAlignment(Constant* A) {
  172. setArgOperand(3, A);
  173. }
  174. void setVolatile(Constant* V) {
  175. setArgOperand(4, V);
  176. }
  177. Type *getAlignmentType() const {
  178. return getArgOperand(3)->getType();
  179. }
  180. // Methods for support type inquiry through isa, cast, and dyn_cast:
  181. static inline bool classof(const IntrinsicInst *I) {
  182. switch (I->getIntrinsicID()) {
  183. case Intrinsic::memcpy:
  184. case Intrinsic::memmove:
  185. case Intrinsic::memset:
  186. return true;
  187. default: return false;
  188. }
  189. }
  190. static inline bool classof(const Value *V) {
  191. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  192. }
  193. };
  194. /// MemSetInst - This class wraps the llvm.memset intrinsic.
  195. ///
  196. class MemSetInst : public MemIntrinsic {
  197. public:
  198. /// get* - Return the arguments to the instruction.
  199. ///
  200. Value *getValue() const { return const_cast<Value*>(getArgOperand(1)); }
  201. const Use &getValueUse() const { return getArgOperandUse(1); }
  202. Use &getValueUse() { return getArgOperandUse(1); }
  203. void setValue(Value *Val) {
  204. assert(getValue()->getType() == Val->getType() &&
  205. "setValue called with value of wrong type!");
  206. setArgOperand(1, Val);
  207. }
  208. // Methods for support type inquiry through isa, cast, and dyn_cast:
  209. static inline bool classof(const IntrinsicInst *I) {
  210. return I->getIntrinsicID() == Intrinsic::memset;
  211. }
  212. static inline bool classof(const Value *V) {
  213. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  214. }
  215. };
  216. /// MemTransferInst - This class wraps the llvm.memcpy/memmove intrinsics.
  217. ///
  218. class MemTransferInst : public MemIntrinsic {
  219. public:
  220. /// get* - Return the arguments to the instruction.
  221. ///
  222. Value *getRawSource() const { return const_cast<Value*>(getArgOperand(1)); }
  223. const Use &getRawSourceUse() const { return getArgOperandUse(1); }
  224. Use &getRawSourceUse() { return getArgOperandUse(1); }
  225. /// getSource - This is just like getRawSource, but it strips off any cast
  226. /// instructions that feed it, giving the original input. The returned
  227. /// value is guaranteed to be a pointer.
  228. Value *getSource() const { return getRawSource()->stripPointerCasts(); }
  229. unsigned getSourceAddressSpace() const {
  230. return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
  231. }
  232. void setSource(Value *Ptr) {
  233. assert(getRawSource()->getType() == Ptr->getType() &&
  234. "setSource called with pointer of wrong type!");
  235. setArgOperand(1, Ptr);
  236. }
  237. // Methods for support type inquiry through isa, cast, and dyn_cast:
  238. static inline bool classof(const IntrinsicInst *I) {
  239. return I->getIntrinsicID() == Intrinsic::memcpy ||
  240. I->getIntrinsicID() == Intrinsic::memmove;
  241. }
  242. static inline bool classof(const Value *V) {
  243. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  244. }
  245. };
  246. /// MemCpyInst - This class wraps the llvm.memcpy intrinsic.
  247. ///
  248. class MemCpyInst : public MemTransferInst {
  249. public:
  250. // Methods for support type inquiry through isa, cast, and dyn_cast:
  251. static inline bool classof(const IntrinsicInst *I) {
  252. return I->getIntrinsicID() == Intrinsic::memcpy;
  253. }
  254. static inline bool classof(const Value *V) {
  255. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  256. }
  257. };
  258. /// MemMoveInst - This class wraps the llvm.memmove intrinsic.
  259. ///
  260. class MemMoveInst : public MemTransferInst {
  261. public:
  262. // Methods for support type inquiry through isa, cast, and dyn_cast:
  263. static inline bool classof(const IntrinsicInst *I) {
  264. return I->getIntrinsicID() == Intrinsic::memmove;
  265. }
  266. static inline bool classof(const Value *V) {
  267. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  268. }
  269. };
  270. /// VAStartInst - This represents the llvm.va_start intrinsic.
  271. ///
  272. class VAStartInst : public IntrinsicInst {
  273. public:
  274. static inline bool classof(const IntrinsicInst *I) {
  275. return I->getIntrinsicID() == Intrinsic::vastart;
  276. }
  277. static inline bool classof(const Value *V) {
  278. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  279. }
  280. Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
  281. };
  282. /// VAEndInst - This represents the llvm.va_end intrinsic.
  283. ///
  284. class VAEndInst : public IntrinsicInst {
  285. public:
  286. static inline bool classof(const IntrinsicInst *I) {
  287. return I->getIntrinsicID() == Intrinsic::vaend;
  288. }
  289. static inline bool classof(const Value *V) {
  290. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  291. }
  292. Value *getArgList() const { return const_cast<Value*>(getArgOperand(0)); }
  293. };
  294. /// VACopyInst - This represents the llvm.va_copy intrinsic.
  295. ///
  296. class VACopyInst : public IntrinsicInst {
  297. public:
  298. static inline bool classof(const IntrinsicInst *I) {
  299. return I->getIntrinsicID() == Intrinsic::vacopy;
  300. }
  301. static inline bool classof(const Value *V) {
  302. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  303. }
  304. Value *getDest() const { return const_cast<Value*>(getArgOperand(0)); }
  305. Value *getSrc() const { return const_cast<Value*>(getArgOperand(1)); }
  306. };
  307. /// This represents the llvm.instrprof_increment intrinsic.
  308. class InstrProfIncrementInst : public IntrinsicInst {
  309. public:
  310. static inline bool classof(const IntrinsicInst *I) {
  311. return I->getIntrinsicID() == Intrinsic::instrprof_increment;
  312. }
  313. static inline bool classof(const Value *V) {
  314. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  315. }
  316. GlobalVariable *getName() const {
  317. return cast<GlobalVariable>(
  318. const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
  319. }
  320. ConstantInt *getHash() const {
  321. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
  322. }
  323. ConstantInt *getNumCounters() const {
  324. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
  325. }
  326. ConstantInt *getIndex() const {
  327. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
  328. }
  329. };
  330. }
  331. #endif