IntrinsicInst.h 12 KB

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