IntrinsicInst.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines classes that make it really easy to deal with intrinsic
  15. // functions with the isa/dyncast family of functions. In particular, this
  16. // allows you to do things like:
  17. //
  18. // if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst))
  19. // ... MCI->getDest() ... MCI->getSource() ...
  20. //
  21. // All intrinsic function calls are instances of the call instruction, so these
  22. // are all subclasses of the CallInst class. Note that none of these classes
  23. // has state or virtual methods, which is an important part of this gross/neat
  24. // hack working.
  25. //
  26. //===----------------------------------------------------------------------===//
  27. #ifndef LLVM_IR_INTRINSICINST_H
  28. #define LLVM_IR_INTRINSICINST_H
  29. #include "llvm/IR/Constants.h"
  30. #include "llvm/IR/DerivedTypes.h"
  31. #include "llvm/IR/FPEnv.h"
  32. #include "llvm/IR/Function.h"
  33. #include "llvm/IR/GlobalVariable.h"
  34. #include "llvm/IR/Instructions.h"
  35. #include "llvm/IR/Intrinsics.h"
  36. #include "llvm/IR/Metadata.h"
  37. #include "llvm/IR/Value.h"
  38. #include "llvm/Support/Casting.h"
  39. #include <cassert>
  40. #include <cstdint>
  41. namespace llvm {
  42. /// A wrapper class for inspecting calls to intrinsic functions.
  43. /// This allows the standard isa/dyncast/cast functionality to work with calls
  44. /// to intrinsic functions.
  45. class IntrinsicInst : public CallInst {
  46. public:
  47. IntrinsicInst() = delete;
  48. IntrinsicInst(const IntrinsicInst &) = delete;
  49. IntrinsicInst &operator=(const IntrinsicInst &) = delete;
  50. /// Return the intrinsic ID of this intrinsic.
  51. Intrinsic::ID getIntrinsicID() const {
  52. return getCalledFunction()->getIntrinsicID();
  53. }
  54. /// Return true if swapping the first two arguments to the intrinsic produces
  55. /// the same result.
  56. bool isCommutative() const {
  57. switch (getIntrinsicID()) {
  58. case Intrinsic::maxnum:
  59. case Intrinsic::minnum:
  60. case Intrinsic::maximum:
  61. case Intrinsic::minimum:
  62. case Intrinsic::smax:
  63. case Intrinsic::smin:
  64. case Intrinsic::umax:
  65. case Intrinsic::umin:
  66. case Intrinsic::sadd_sat:
  67. case Intrinsic::uadd_sat:
  68. case Intrinsic::sadd_with_overflow:
  69. case Intrinsic::uadd_with_overflow:
  70. case Intrinsic::smul_with_overflow:
  71. case Intrinsic::umul_with_overflow:
  72. case Intrinsic::smul_fix:
  73. case Intrinsic::umul_fix:
  74. case Intrinsic::smul_fix_sat:
  75. case Intrinsic::umul_fix_sat:
  76. case Intrinsic::fma:
  77. case Intrinsic::fmuladd:
  78. return true;
  79. default:
  80. return false;
  81. }
  82. }
  83. // Methods for support type inquiry through isa, cast, and dyn_cast:
  84. static bool classof(const CallInst *I) {
  85. if (const Function *CF = I->getCalledFunction())
  86. return CF->isIntrinsic();
  87. return false;
  88. }
  89. static bool classof(const Value *V) {
  90. return isa<CallInst>(V) && classof(cast<CallInst>(V));
  91. }
  92. };
  93. /// Check if \p ID corresponds to a debug info intrinsic.
  94. static inline bool isDbgInfoIntrinsic(Intrinsic::ID ID) {
  95. switch (ID) {
  96. case Intrinsic::dbg_declare:
  97. case Intrinsic::dbg_value:
  98. case Intrinsic::dbg_addr:
  99. case Intrinsic::dbg_label:
  100. return true;
  101. default:
  102. return false;
  103. }
  104. }
  105. /// This is the common base class for debug info intrinsics.
  106. class DbgInfoIntrinsic : public IntrinsicInst {
  107. public:
  108. /// \name Casting methods
  109. /// @{
  110. static bool classof(const IntrinsicInst *I) {
  111. return isDbgInfoIntrinsic(I->getIntrinsicID());
  112. }
  113. static bool classof(const Value *V) {
  114. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  115. }
  116. /// @}
  117. };
  118. /// This is the common base class for debug info intrinsics for variables.
  119. class DbgVariableIntrinsic : public DbgInfoIntrinsic {
  120. public:
  121. /// Get the location corresponding to the variable referenced by the debug
  122. /// info intrinsic. Depending on the intrinsic, this could be the
  123. /// variable's value or its address.
  124. Value *getVariableLocation(bool AllowNullOp = true) const;
  125. /// Does this describe the address of a local variable. True for dbg.addr
  126. /// and dbg.declare, but not dbg.value, which describes its value.
  127. bool isAddressOfVariable() const {
  128. return getIntrinsicID() != Intrinsic::dbg_value;
  129. }
  130. DILocalVariable *getVariable() const {
  131. return cast<DILocalVariable>(getRawVariable());
  132. }
  133. DIExpression *getExpression() const {
  134. return cast<DIExpression>(getRawExpression());
  135. }
  136. Metadata *getRawVariable() const {
  137. return cast<MetadataAsValue>(getArgOperand(1))->getMetadata();
  138. }
  139. Metadata *getRawExpression() const {
  140. return cast<MetadataAsValue>(getArgOperand(2))->getMetadata();
  141. }
  142. /// Get the size (in bits) of the variable, or fragment of the variable that
  143. /// is described.
  144. Optional<uint64_t> getFragmentSizeInBits() const;
  145. /// \name Casting methods
  146. /// @{
  147. static bool classof(const IntrinsicInst *I) {
  148. switch (I->getIntrinsicID()) {
  149. case Intrinsic::dbg_declare:
  150. case Intrinsic::dbg_value:
  151. case Intrinsic::dbg_addr:
  152. return true;
  153. default:
  154. return false;
  155. }
  156. }
  157. static bool classof(const Value *V) {
  158. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  159. }
  160. /// @}
  161. };
  162. /// This represents the llvm.dbg.declare instruction.
  163. class DbgDeclareInst : public DbgVariableIntrinsic {
  164. public:
  165. Value *getAddress() const { return getVariableLocation(); }
  166. /// \name Casting methods
  167. /// @{
  168. static bool classof(const IntrinsicInst *I) {
  169. return I->getIntrinsicID() == Intrinsic::dbg_declare;
  170. }
  171. static bool classof(const Value *V) {
  172. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  173. }
  174. /// @}
  175. };
  176. /// This represents the llvm.dbg.addr instruction.
  177. class DbgAddrIntrinsic : public DbgVariableIntrinsic {
  178. public:
  179. Value *getAddress() const { return getVariableLocation(); }
  180. /// \name Casting methods
  181. /// @{
  182. static bool classof(const IntrinsicInst *I) {
  183. return I->getIntrinsicID() == Intrinsic::dbg_addr;
  184. }
  185. static bool classof(const Value *V) {
  186. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  187. }
  188. };
  189. /// This represents the llvm.dbg.value instruction.
  190. class DbgValueInst : public DbgVariableIntrinsic {
  191. public:
  192. Value *getValue() const {
  193. return getVariableLocation(/* AllowNullOp = */ false);
  194. }
  195. /// \name Casting methods
  196. /// @{
  197. static bool classof(const IntrinsicInst *I) {
  198. return I->getIntrinsicID() == Intrinsic::dbg_value;
  199. }
  200. static bool classof(const Value *V) {
  201. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  202. }
  203. /// @}
  204. };
  205. /// This represents the llvm.dbg.label instruction.
  206. class DbgLabelInst : public DbgInfoIntrinsic {
  207. public:
  208. DILabel *getLabel() const { return cast<DILabel>(getRawLabel()); }
  209. Metadata *getRawLabel() const {
  210. return cast<MetadataAsValue>(getArgOperand(0))->getMetadata();
  211. }
  212. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  213. /// @{
  214. static bool classof(const IntrinsicInst *I) {
  215. return I->getIntrinsicID() == Intrinsic::dbg_label;
  216. }
  217. static bool classof(const Value *V) {
  218. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  219. }
  220. /// @}
  221. };
  222. /// This is the common base class for vector predication intrinsics.
  223. class VPIntrinsic : public IntrinsicInst {
  224. public:
  225. static Optional<int> GetMaskParamPos(Intrinsic::ID IntrinsicID);
  226. static Optional<int> GetVectorLengthParamPos(Intrinsic::ID IntrinsicID);
  227. /// The llvm.vp.* intrinsics for this instruction Opcode
  228. static Intrinsic::ID GetForOpcode(unsigned OC);
  229. // Whether \p ID is a VP intrinsic ID.
  230. static bool IsVPIntrinsic(Intrinsic::ID);
  231. /// \return the mask parameter or nullptr.
  232. Value *getMaskParam() const;
  233. /// \return the vector length parameter or nullptr.
  234. Value *getVectorLengthParam() const;
  235. /// \return whether the vector length param can be ignored.
  236. bool canIgnoreVectorLengthParam() const;
  237. /// \return the static element count (vector number of elements) the vector
  238. /// length parameter applies to.
  239. ElementCount getStaticVectorLength() const;
  240. // Methods for support type inquiry through isa, cast, and dyn_cast:
  241. static bool classof(const IntrinsicInst *I) {
  242. return IsVPIntrinsic(I->getIntrinsicID());
  243. }
  244. static bool classof(const Value *V) {
  245. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  246. }
  247. // Equivalent non-predicated opcode
  248. unsigned getFunctionalOpcode() const {
  249. return GetFunctionalOpcodeForVP(getIntrinsicID());
  250. }
  251. // Equivalent non-predicated opcode
  252. static unsigned GetFunctionalOpcodeForVP(Intrinsic::ID ID);
  253. };
  254. /// This is the common base class for constrained floating point intrinsics.
  255. class ConstrainedFPIntrinsic : public IntrinsicInst {
  256. public:
  257. bool isUnaryOp() const;
  258. bool isTernaryOp() const;
  259. Optional<RoundingMode> getRoundingMode() const;
  260. Optional<fp::ExceptionBehavior> getExceptionBehavior() const;
  261. // Methods for support type inquiry through isa, cast, and dyn_cast:
  262. static bool classof(const IntrinsicInst *I);
  263. static bool classof(const Value *V) {
  264. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  265. }
  266. };
  267. /// Constrained floating point compare intrinsics.
  268. class ConstrainedFPCmpIntrinsic : public ConstrainedFPIntrinsic {
  269. public:
  270. FCmpInst::Predicate getPredicate() const;
  271. // Methods for support type inquiry through isa, cast, and dyn_cast:
  272. static bool classof(const IntrinsicInst *I) {
  273. switch (I->getIntrinsicID()) {
  274. case Intrinsic::experimental_constrained_fcmp:
  275. case Intrinsic::experimental_constrained_fcmps:
  276. return true;
  277. default:
  278. return false;
  279. }
  280. }
  281. static bool classof(const Value *V) {
  282. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  283. }
  284. };
  285. /// This class represents an intrinsic that is based on a binary operation.
  286. /// This includes op.with.overflow and saturating add/sub intrinsics.
  287. class BinaryOpIntrinsic : public IntrinsicInst {
  288. public:
  289. static bool classof(const IntrinsicInst *I) {
  290. switch (I->getIntrinsicID()) {
  291. case Intrinsic::uadd_with_overflow:
  292. case Intrinsic::sadd_with_overflow:
  293. case Intrinsic::usub_with_overflow:
  294. case Intrinsic::ssub_with_overflow:
  295. case Intrinsic::umul_with_overflow:
  296. case Intrinsic::smul_with_overflow:
  297. case Intrinsic::uadd_sat:
  298. case Intrinsic::sadd_sat:
  299. case Intrinsic::usub_sat:
  300. case Intrinsic::ssub_sat:
  301. return true;
  302. default:
  303. return false;
  304. }
  305. }
  306. static bool classof(const Value *V) {
  307. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  308. }
  309. Value *getLHS() const { return const_cast<Value *>(getArgOperand(0)); }
  310. Value *getRHS() const { return const_cast<Value *>(getArgOperand(1)); }
  311. /// Returns the binary operation underlying the intrinsic.
  312. Instruction::BinaryOps getBinaryOp() const;
  313. /// Whether the intrinsic is signed or unsigned.
  314. bool isSigned() const;
  315. /// Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
  316. unsigned getNoWrapKind() const;
  317. };
  318. /// Represents an op.with.overflow intrinsic.
  319. class WithOverflowInst : public BinaryOpIntrinsic {
  320. public:
  321. static bool classof(const IntrinsicInst *I) {
  322. switch (I->getIntrinsicID()) {
  323. case Intrinsic::uadd_with_overflow:
  324. case Intrinsic::sadd_with_overflow:
  325. case Intrinsic::usub_with_overflow:
  326. case Intrinsic::ssub_with_overflow:
  327. case Intrinsic::umul_with_overflow:
  328. case Intrinsic::smul_with_overflow:
  329. return true;
  330. default:
  331. return false;
  332. }
  333. }
  334. static bool classof(const Value *V) {
  335. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  336. }
  337. };
  338. /// Represents a saturating add/sub intrinsic.
  339. class SaturatingInst : public BinaryOpIntrinsic {
  340. public:
  341. static bool classof(const IntrinsicInst *I) {
  342. switch (I->getIntrinsicID()) {
  343. case Intrinsic::uadd_sat:
  344. case Intrinsic::sadd_sat:
  345. case Intrinsic::usub_sat:
  346. case Intrinsic::ssub_sat:
  347. return true;
  348. default:
  349. return false;
  350. }
  351. }
  352. static bool classof(const Value *V) {
  353. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  354. }
  355. };
  356. /// Common base class for all memory intrinsics. Simply provides
  357. /// common methods.
  358. /// Written as CRTP to avoid a common base class amongst the
  359. /// three atomicity hierarchies.
  360. template <typename Derived> class MemIntrinsicBase : public IntrinsicInst {
  361. private:
  362. enum { ARG_DEST = 0, ARG_LENGTH = 2 };
  363. public:
  364. Value *getRawDest() const {
  365. return const_cast<Value *>(getArgOperand(ARG_DEST));
  366. }
  367. const Use &getRawDestUse() const { return getArgOperandUse(ARG_DEST); }
  368. Use &getRawDestUse() { return getArgOperandUse(ARG_DEST); }
  369. Value *getLength() const {
  370. return const_cast<Value *>(getArgOperand(ARG_LENGTH));
  371. }
  372. const Use &getLengthUse() const { return getArgOperandUse(ARG_LENGTH); }
  373. Use &getLengthUse() { return getArgOperandUse(ARG_LENGTH); }
  374. /// This is just like getRawDest, but it strips off any cast
  375. /// instructions (including addrspacecast) that feed it, giving the
  376. /// original input. The returned value is guaranteed to be a pointer.
  377. Value *getDest() const { return getRawDest()->stripPointerCasts(); }
  378. unsigned getDestAddressSpace() const {
  379. return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
  380. }
  381. /// FIXME: Remove this function once transition to Align is over.
  382. /// Use getDestAlign() instead.
  383. unsigned getDestAlignment() const {
  384. if (auto MA = getParamAlign(ARG_DEST))
  385. return MA->value();
  386. return 0;
  387. }
  388. MaybeAlign getDestAlign() const { return getParamAlign(ARG_DEST); }
  389. /// Set the specified arguments of the instruction.
  390. void setDest(Value *Ptr) {
  391. assert(getRawDest()->getType() == Ptr->getType() &&
  392. "setDest called with pointer of wrong type!");
  393. setArgOperand(ARG_DEST, Ptr);
  394. }
  395. /// FIXME: Remove this function once transition to Align is over.
  396. /// Use the version that takes MaybeAlign instead of this one.
  397. void setDestAlignment(unsigned Alignment) {
  398. setDestAlignment(MaybeAlign(Alignment));
  399. }
  400. void setDestAlignment(MaybeAlign Alignment) {
  401. removeParamAttr(ARG_DEST, Attribute::Alignment);
  402. if (Alignment)
  403. addParamAttr(ARG_DEST,
  404. Attribute::getWithAlignment(getContext(), *Alignment));
  405. }
  406. void setDestAlignment(Align Alignment) {
  407. removeParamAttr(ARG_DEST, Attribute::Alignment);
  408. addParamAttr(ARG_DEST,
  409. Attribute::getWithAlignment(getContext(), Alignment));
  410. }
  411. void setLength(Value *L) {
  412. assert(getLength()->getType() == L->getType() &&
  413. "setLength called with value of wrong type!");
  414. setArgOperand(ARG_LENGTH, L);
  415. }
  416. };
  417. /// Common base class for all memory transfer intrinsics. Simply provides
  418. /// common methods.
  419. template <class BaseCL> class MemTransferBase : public BaseCL {
  420. private:
  421. enum { ARG_SOURCE = 1 };
  422. public:
  423. /// Return the arguments to the instruction.
  424. Value *getRawSource() const {
  425. return const_cast<Value *>(BaseCL::getArgOperand(ARG_SOURCE));
  426. }
  427. const Use &getRawSourceUse() const {
  428. return BaseCL::getArgOperandUse(ARG_SOURCE);
  429. }
  430. Use &getRawSourceUse() { return BaseCL::getArgOperandUse(ARG_SOURCE); }
  431. /// This is just like getRawSource, but it strips off any cast
  432. /// instructions that feed it, giving the original input. The returned
  433. /// value is guaranteed to be a pointer.
  434. Value *getSource() const { return getRawSource()->stripPointerCasts(); }
  435. unsigned getSourceAddressSpace() const {
  436. return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
  437. }
  438. /// FIXME: Remove this function once transition to Align is over.
  439. /// Use getSourceAlign() instead.
  440. unsigned getSourceAlignment() const {
  441. if (auto MA = BaseCL::getParamAlign(ARG_SOURCE))
  442. return MA->value();
  443. return 0;
  444. }
  445. MaybeAlign getSourceAlign() const {
  446. return BaseCL::getParamAlign(ARG_SOURCE);
  447. }
  448. void setSource(Value *Ptr) {
  449. assert(getRawSource()->getType() == Ptr->getType() &&
  450. "setSource called with pointer of wrong type!");
  451. BaseCL::setArgOperand(ARG_SOURCE, Ptr);
  452. }
  453. /// FIXME: Remove this function once transition to Align is over.
  454. /// Use the version that takes MaybeAlign instead of this one.
  455. void setSourceAlignment(unsigned Alignment) {
  456. setSourceAlignment(MaybeAlign(Alignment));
  457. }
  458. void setSourceAlignment(MaybeAlign Alignment) {
  459. BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment);
  460. if (Alignment)
  461. BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment(
  462. BaseCL::getContext(), *Alignment));
  463. }
  464. void setSourceAlignment(Align Alignment) {
  465. BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment);
  466. BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment(
  467. BaseCL::getContext(), Alignment));
  468. }
  469. };
  470. /// Common base class for all memset intrinsics. Simply provides
  471. /// common methods.
  472. template <class BaseCL> class MemSetBase : public BaseCL {
  473. private:
  474. enum { ARG_VALUE = 1 };
  475. public:
  476. Value *getValue() const {
  477. return const_cast<Value *>(BaseCL::getArgOperand(ARG_VALUE));
  478. }
  479. const Use &getValueUse() const { return BaseCL::getArgOperandUse(ARG_VALUE); }
  480. Use &getValueUse() { return BaseCL::getArgOperandUse(ARG_VALUE); }
  481. void setValue(Value *Val) {
  482. assert(getValue()->getType() == Val->getType() &&
  483. "setValue called with value of wrong type!");
  484. BaseCL::setArgOperand(ARG_VALUE, Val);
  485. }
  486. };
  487. // The common base class for the atomic memset/memmove/memcpy intrinsics
  488. // i.e. llvm.element.unordered.atomic.memset/memcpy/memmove
  489. class AtomicMemIntrinsic : public MemIntrinsicBase<AtomicMemIntrinsic> {
  490. private:
  491. enum { ARG_ELEMENTSIZE = 3 };
  492. public:
  493. Value *getRawElementSizeInBytes() const {
  494. return const_cast<Value *>(getArgOperand(ARG_ELEMENTSIZE));
  495. }
  496. ConstantInt *getElementSizeInBytesCst() const {
  497. return cast<ConstantInt>(getRawElementSizeInBytes());
  498. }
  499. uint32_t getElementSizeInBytes() const {
  500. return getElementSizeInBytesCst()->getZExtValue();
  501. }
  502. void setElementSizeInBytes(Constant *V) {
  503. assert(V->getType() == Type::getInt8Ty(getContext()) &&
  504. "setElementSizeInBytes called with value of wrong type!");
  505. setArgOperand(ARG_ELEMENTSIZE, V);
  506. }
  507. static bool classof(const IntrinsicInst *I) {
  508. switch (I->getIntrinsicID()) {
  509. case Intrinsic::memcpy_element_unordered_atomic:
  510. case Intrinsic::memmove_element_unordered_atomic:
  511. case Intrinsic::memset_element_unordered_atomic:
  512. return true;
  513. default:
  514. return false;
  515. }
  516. }
  517. static bool classof(const Value *V) {
  518. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  519. }
  520. };
  521. /// This class represents atomic memset intrinsic
  522. // i.e. llvm.element.unordered.atomic.memset
  523. class AtomicMemSetInst : public MemSetBase<AtomicMemIntrinsic> {
  524. public:
  525. static bool classof(const IntrinsicInst *I) {
  526. return I->getIntrinsicID() == Intrinsic::memset_element_unordered_atomic;
  527. }
  528. static bool classof(const Value *V) {
  529. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  530. }
  531. };
  532. // This class wraps the atomic memcpy/memmove intrinsics
  533. // i.e. llvm.element.unordered.atomic.memcpy/memmove
  534. class AtomicMemTransferInst : public MemTransferBase<AtomicMemIntrinsic> {
  535. public:
  536. static bool classof(const IntrinsicInst *I) {
  537. switch (I->getIntrinsicID()) {
  538. case Intrinsic::memcpy_element_unordered_atomic:
  539. case Intrinsic::memmove_element_unordered_atomic:
  540. return true;
  541. default:
  542. return false;
  543. }
  544. }
  545. static bool classof(const Value *V) {
  546. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  547. }
  548. };
  549. /// This class represents the atomic memcpy intrinsic
  550. /// i.e. llvm.element.unordered.atomic.memcpy
  551. class AtomicMemCpyInst : public AtomicMemTransferInst {
  552. public:
  553. static bool classof(const IntrinsicInst *I) {
  554. return I->getIntrinsicID() == Intrinsic::memcpy_element_unordered_atomic;
  555. }
  556. static bool classof(const Value *V) {
  557. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  558. }
  559. };
  560. /// This class represents the atomic memmove intrinsic
  561. /// i.e. llvm.element.unordered.atomic.memmove
  562. class AtomicMemMoveInst : public AtomicMemTransferInst {
  563. public:
  564. static bool classof(const IntrinsicInst *I) {
  565. return I->getIntrinsicID() == Intrinsic::memmove_element_unordered_atomic;
  566. }
  567. static bool classof(const Value *V) {
  568. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  569. }
  570. };
  571. /// This is the common base class for memset/memcpy/memmove.
  572. class MemIntrinsic : public MemIntrinsicBase<MemIntrinsic> {
  573. private:
  574. enum { ARG_VOLATILE = 3 };
  575. public:
  576. ConstantInt *getVolatileCst() const {
  577. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(ARG_VOLATILE)));
  578. }
  579. bool isVolatile() const { return !getVolatileCst()->isZero(); }
  580. void setVolatile(Constant *V) { setArgOperand(ARG_VOLATILE, V); }
  581. // Methods for support type inquiry through isa, cast, and dyn_cast:
  582. static bool classof(const IntrinsicInst *I) {
  583. switch (I->getIntrinsicID()) {
  584. case Intrinsic::memcpy:
  585. case Intrinsic::memmove:
  586. case Intrinsic::memset:
  587. case Intrinsic::memcpy_inline:
  588. return true;
  589. default:
  590. return false;
  591. }
  592. }
  593. static bool classof(const Value *V) {
  594. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  595. }
  596. };
  597. /// This class wraps the llvm.memset intrinsic.
  598. class MemSetInst : public MemSetBase<MemIntrinsic> {
  599. public:
  600. // Methods for support type inquiry through isa, cast, and dyn_cast:
  601. static bool classof(const IntrinsicInst *I) {
  602. return I->getIntrinsicID() == Intrinsic::memset;
  603. }
  604. static bool classof(const Value *V) {
  605. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  606. }
  607. };
  608. /// This class wraps the llvm.memcpy/memmove intrinsics.
  609. class MemTransferInst : public MemTransferBase<MemIntrinsic> {
  610. public:
  611. // Methods for support type inquiry through isa, cast, and dyn_cast:
  612. static bool classof(const IntrinsicInst *I) {
  613. switch (I->getIntrinsicID()) {
  614. case Intrinsic::memcpy:
  615. case Intrinsic::memmove:
  616. case Intrinsic::memcpy_inline:
  617. return true;
  618. default:
  619. return false;
  620. }
  621. }
  622. static bool classof(const Value *V) {
  623. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  624. }
  625. };
  626. /// This class wraps the llvm.memcpy intrinsic.
  627. class MemCpyInst : public MemTransferInst {
  628. public:
  629. // Methods for support type inquiry through isa, cast, and dyn_cast:
  630. static bool classof(const IntrinsicInst *I) {
  631. return I->getIntrinsicID() == Intrinsic::memcpy;
  632. }
  633. static bool classof(const Value *V) {
  634. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  635. }
  636. };
  637. /// This class wraps the llvm.memmove intrinsic.
  638. class MemMoveInst : public MemTransferInst {
  639. public:
  640. // Methods for support type inquiry through isa, cast, and dyn_cast:
  641. static bool classof(const IntrinsicInst *I) {
  642. return I->getIntrinsicID() == Intrinsic::memmove;
  643. }
  644. static bool classof(const Value *V) {
  645. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  646. }
  647. };
  648. /// This class wraps the llvm.memcpy.inline intrinsic.
  649. class MemCpyInlineInst : public MemTransferInst {
  650. public:
  651. ConstantInt *getLength() const {
  652. return cast<ConstantInt>(MemTransferInst::getLength());
  653. }
  654. // Methods for support type inquiry through isa, cast, and dyn_cast:
  655. static bool classof(const IntrinsicInst *I) {
  656. return I->getIntrinsicID() == Intrinsic::memcpy_inline;
  657. }
  658. static bool classof(const Value *V) {
  659. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  660. }
  661. };
  662. // The common base class for any memset/memmove/memcpy intrinsics;
  663. // whether they be atomic or non-atomic.
  664. // i.e. llvm.element.unordered.atomic.memset/memcpy/memmove
  665. // and llvm.memset/memcpy/memmove
  666. class AnyMemIntrinsic : public MemIntrinsicBase<AnyMemIntrinsic> {
  667. public:
  668. bool isVolatile() const {
  669. // Only the non-atomic intrinsics can be volatile
  670. if (auto *MI = dyn_cast<MemIntrinsic>(this))
  671. return MI->isVolatile();
  672. return false;
  673. }
  674. static bool classof(const IntrinsicInst *I) {
  675. switch (I->getIntrinsicID()) {
  676. case Intrinsic::memcpy:
  677. case Intrinsic::memcpy_inline:
  678. case Intrinsic::memmove:
  679. case Intrinsic::memset:
  680. case Intrinsic::memcpy_element_unordered_atomic:
  681. case Intrinsic::memmove_element_unordered_atomic:
  682. case Intrinsic::memset_element_unordered_atomic:
  683. return true;
  684. default:
  685. return false;
  686. }
  687. }
  688. static bool classof(const Value *V) {
  689. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  690. }
  691. };
  692. /// This class represents any memset intrinsic
  693. // i.e. llvm.element.unordered.atomic.memset
  694. // and llvm.memset
  695. class AnyMemSetInst : public MemSetBase<AnyMemIntrinsic> {
  696. public:
  697. static bool classof(const IntrinsicInst *I) {
  698. switch (I->getIntrinsicID()) {
  699. case Intrinsic::memset:
  700. case Intrinsic::memset_element_unordered_atomic:
  701. return true;
  702. default:
  703. return false;
  704. }
  705. }
  706. static bool classof(const Value *V) {
  707. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  708. }
  709. };
  710. // This class wraps any memcpy/memmove intrinsics
  711. // i.e. llvm.element.unordered.atomic.memcpy/memmove
  712. // and llvm.memcpy/memmove
  713. class AnyMemTransferInst : public MemTransferBase<AnyMemIntrinsic> {
  714. public:
  715. static bool classof(const IntrinsicInst *I) {
  716. switch (I->getIntrinsicID()) {
  717. case Intrinsic::memcpy:
  718. case Intrinsic::memcpy_inline:
  719. case Intrinsic::memmove:
  720. case Intrinsic::memcpy_element_unordered_atomic:
  721. case Intrinsic::memmove_element_unordered_atomic:
  722. return true;
  723. default:
  724. return false;
  725. }
  726. }
  727. static bool classof(const Value *V) {
  728. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  729. }
  730. };
  731. /// This class represents any memcpy intrinsic
  732. /// i.e. llvm.element.unordered.atomic.memcpy
  733. /// and llvm.memcpy
  734. class AnyMemCpyInst : public AnyMemTransferInst {
  735. public:
  736. static bool classof(const IntrinsicInst *I) {
  737. switch (I->getIntrinsicID()) {
  738. case Intrinsic::memcpy:
  739. case Intrinsic::memcpy_inline:
  740. case Intrinsic::memcpy_element_unordered_atomic:
  741. return true;
  742. default:
  743. return false;
  744. }
  745. }
  746. static bool classof(const Value *V) {
  747. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  748. }
  749. };
  750. /// This class represents any memmove intrinsic
  751. /// i.e. llvm.element.unordered.atomic.memmove
  752. /// and llvm.memmove
  753. class AnyMemMoveInst : public AnyMemTransferInst {
  754. public:
  755. static bool classof(const IntrinsicInst *I) {
  756. switch (I->getIntrinsicID()) {
  757. case Intrinsic::memmove:
  758. case Intrinsic::memmove_element_unordered_atomic:
  759. return true;
  760. default:
  761. return false;
  762. }
  763. }
  764. static bool classof(const Value *V) {
  765. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  766. }
  767. };
  768. /// This represents the llvm.va_start intrinsic.
  769. class VAStartInst : public IntrinsicInst {
  770. public:
  771. static bool classof(const IntrinsicInst *I) {
  772. return I->getIntrinsicID() == Intrinsic::vastart;
  773. }
  774. static bool classof(const Value *V) {
  775. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  776. }
  777. Value *getArgList() const { return const_cast<Value *>(getArgOperand(0)); }
  778. };
  779. /// This represents the llvm.va_end intrinsic.
  780. class VAEndInst : public IntrinsicInst {
  781. public:
  782. static bool classof(const IntrinsicInst *I) {
  783. return I->getIntrinsicID() == Intrinsic::vaend;
  784. }
  785. static bool classof(const Value *V) {
  786. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  787. }
  788. Value *getArgList() const { return const_cast<Value *>(getArgOperand(0)); }
  789. };
  790. /// This represents the llvm.va_copy intrinsic.
  791. class VACopyInst : public IntrinsicInst {
  792. public:
  793. static bool classof(const IntrinsicInst *I) {
  794. return I->getIntrinsicID() == Intrinsic::vacopy;
  795. }
  796. static bool classof(const Value *V) {
  797. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  798. }
  799. Value *getDest() const { return const_cast<Value *>(getArgOperand(0)); }
  800. Value *getSrc() const { return const_cast<Value *>(getArgOperand(1)); }
  801. };
  802. /// This represents the llvm.instrprof_increment intrinsic.
  803. class InstrProfIncrementInst : public IntrinsicInst {
  804. public:
  805. static bool classof(const IntrinsicInst *I) {
  806. return I->getIntrinsicID() == Intrinsic::instrprof_increment;
  807. }
  808. static bool classof(const Value *V) {
  809. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  810. }
  811. GlobalVariable *getName() const {
  812. return cast<GlobalVariable>(
  813. const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
  814. }
  815. ConstantInt *getHash() const {
  816. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
  817. }
  818. ConstantInt *getNumCounters() const {
  819. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
  820. }
  821. ConstantInt *getIndex() const {
  822. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
  823. }
  824. Value *getStep() const;
  825. };
  826. class InstrProfIncrementInstStep : public InstrProfIncrementInst {
  827. public:
  828. static bool classof(const IntrinsicInst *I) {
  829. return I->getIntrinsicID() == Intrinsic::instrprof_increment_step;
  830. }
  831. static bool classof(const Value *V) {
  832. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  833. }
  834. };
  835. /// This represents the llvm.instrprof_value_profile intrinsic.
  836. class InstrProfValueProfileInst : public IntrinsicInst {
  837. public:
  838. static bool classof(const IntrinsicInst *I) {
  839. return I->getIntrinsicID() == Intrinsic::instrprof_value_profile;
  840. }
  841. static bool classof(const Value *V) {
  842. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  843. }
  844. GlobalVariable *getName() const {
  845. return cast<GlobalVariable>(
  846. const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
  847. }
  848. ConstantInt *getHash() const {
  849. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
  850. }
  851. Value *getTargetValue() const {
  852. return cast<Value>(const_cast<Value *>(getArgOperand(2)));
  853. }
  854. ConstantInt *getValueKind() const {
  855. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
  856. }
  857. // Returns the value site index.
  858. ConstantInt *getIndex() const {
  859. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(4)));
  860. }
  861. };
  862. class PseudoProbeInst : public IntrinsicInst {
  863. public:
  864. static bool classof(const IntrinsicInst *I) {
  865. return I->getIntrinsicID() == Intrinsic::pseudoprobe;
  866. }
  867. static bool classof(const Value *V) {
  868. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  869. }
  870. ConstantInt *getFuncGuid() const {
  871. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(0)));
  872. }
  873. ConstantInt *getIndex() const {
  874. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
  875. }
  876. ConstantInt *getAttributes() const {
  877. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
  878. }
  879. ConstantInt *getFactor() const {
  880. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
  881. }
  882. };
  883. class NoAliasScopeDeclInst : public IntrinsicInst {
  884. public:
  885. static bool classof(const IntrinsicInst *I) {
  886. return I->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl;
  887. }
  888. static bool classof(const Value *V) {
  889. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  890. }
  891. MDNode *getScopeList() const {
  892. auto *MV =
  893. cast<MetadataAsValue>(getOperand(Intrinsic::NoAliasScopeDeclScopeArg));
  894. return cast<MDNode>(MV->getMetadata());
  895. }
  896. void setScopeList(MDNode *ScopeList) {
  897. setOperand(Intrinsic::NoAliasScopeDeclScopeArg,
  898. MetadataAsValue::get(getContext(), ScopeList));
  899. }
  900. };
  901. } // end namespace llvm
  902. #endif // LLVM_IR_INTRINSICINST_H
  903. #ifdef __GNUC__
  904. #pragma GCC diagnostic pop
  905. #endif