IntrinsicInst.h 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. //===-- llvm/IntrinsicInst.h - Intrinsic Instruction Wrappers ---*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines classes that make it really easy to deal with intrinsic
  10. // functions with the isa/dyncast family of functions. In particular, this
  11. // allows you to do things like:
  12. //
  13. // if (MemCpyInst *MCI = dyn_cast<MemCpyInst>(Inst))
  14. // ... MCI->getDest() ... MCI->getSource() ...
  15. //
  16. // All intrinsic function calls are instances of the call instruction, so these
  17. // are all subclasses of the CallInst class. Note that none of these classes
  18. // has state or virtual methods, which is an important part of this gross/neat
  19. // hack working.
  20. //
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_IR_INTRINSICINST_H
  23. #define LLVM_IR_INTRINSICINST_H
  24. #include "llvm/IR/Constants.h"
  25. #include "llvm/IR/DebugInfoMetadata.h"
  26. #include "llvm/IR/DerivedTypes.h"
  27. #include "llvm/IR/FPEnv.h"
  28. #include "llvm/IR/Function.h"
  29. #include "llvm/IR/GlobalVariable.h"
  30. #include "llvm/IR/Instructions.h"
  31. #include "llvm/IR/Intrinsics.h"
  32. #include "llvm/IR/Metadata.h"
  33. #include "llvm/IR/Value.h"
  34. #include "llvm/Support/Casting.h"
  35. #include <cassert>
  36. #include <cstdint>
  37. namespace llvm {
  38. /// A wrapper class for inspecting calls to intrinsic functions.
  39. /// This allows the standard isa/dyncast/cast functionality to work with calls
  40. /// to intrinsic functions.
  41. class IntrinsicInst : public CallInst {
  42. public:
  43. IntrinsicInst() = delete;
  44. IntrinsicInst(const IntrinsicInst &) = delete;
  45. IntrinsicInst &operator=(const IntrinsicInst &) = delete;
  46. /// Return the intrinsic ID of this intrinsic.
  47. Intrinsic::ID getIntrinsicID() const {
  48. return getCalledFunction()->getIntrinsicID();
  49. }
  50. /// Return true if swapping the first two arguments to the intrinsic produces
  51. /// the same result.
  52. bool isCommutative() const {
  53. switch (getIntrinsicID()) {
  54. case Intrinsic::maxnum:
  55. case Intrinsic::minnum:
  56. case Intrinsic::maximum:
  57. case Intrinsic::minimum:
  58. case Intrinsic::smax:
  59. case Intrinsic::smin:
  60. case Intrinsic::umax:
  61. case Intrinsic::umin:
  62. case Intrinsic::sadd_sat:
  63. case Intrinsic::uadd_sat:
  64. case Intrinsic::sadd_with_overflow:
  65. case Intrinsic::uadd_with_overflow:
  66. case Intrinsic::smul_with_overflow:
  67. case Intrinsic::umul_with_overflow:
  68. case Intrinsic::smul_fix:
  69. case Intrinsic::umul_fix:
  70. case Intrinsic::smul_fix_sat:
  71. case Intrinsic::umul_fix_sat:
  72. case Intrinsic::fma:
  73. case Intrinsic::fmuladd:
  74. return true;
  75. default:
  76. return false;
  77. }
  78. }
  79. // Checks if the intrinsic is an annotation.
  80. bool isAssumeLikeIntrinsic() const {
  81. switch (getIntrinsicID()) {
  82. default: break;
  83. case Intrinsic::assume:
  84. case Intrinsic::sideeffect:
  85. case Intrinsic::pseudoprobe:
  86. case Intrinsic::dbg_declare:
  87. case Intrinsic::dbg_value:
  88. case Intrinsic::dbg_label:
  89. case Intrinsic::invariant_start:
  90. case Intrinsic::invariant_end:
  91. case Intrinsic::lifetime_start:
  92. case Intrinsic::lifetime_end:
  93. case Intrinsic::experimental_noalias_scope_decl:
  94. case Intrinsic::objectsize:
  95. case Intrinsic::ptr_annotation:
  96. case Intrinsic::var_annotation:
  97. return true;
  98. }
  99. return false;
  100. }
  101. // Methods for support type inquiry through isa, cast, and dyn_cast:
  102. static bool classof(const CallInst *I) {
  103. if (const Function *CF = I->getCalledFunction())
  104. return CF->isIntrinsic();
  105. return false;
  106. }
  107. static bool classof(const Value *V) {
  108. return isa<CallInst>(V) && classof(cast<CallInst>(V));
  109. }
  110. };
  111. /// Check if \p ID corresponds to a debug info intrinsic.
  112. static inline bool isDbgInfoIntrinsic(Intrinsic::ID ID) {
  113. switch (ID) {
  114. case Intrinsic::dbg_declare:
  115. case Intrinsic::dbg_value:
  116. case Intrinsic::dbg_addr:
  117. case Intrinsic::dbg_label:
  118. return true;
  119. default:
  120. return false;
  121. }
  122. }
  123. /// This is the common base class for debug info intrinsics.
  124. class DbgInfoIntrinsic : public IntrinsicInst {
  125. public:
  126. /// \name Casting methods
  127. /// @{
  128. static bool classof(const IntrinsicInst *I) {
  129. return isDbgInfoIntrinsic(I->getIntrinsicID());
  130. }
  131. static bool classof(const Value *V) {
  132. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  133. }
  134. /// @}
  135. };
  136. /// This is the common base class for debug info intrinsics for variables.
  137. class DbgVariableIntrinsic : public DbgInfoIntrinsic {
  138. public:
  139. // Iterator for ValueAsMetadata that internally uses direct pointer iteration
  140. // over either a ValueAsMetadata* or a ValueAsMetadata**, dereferencing to the
  141. // ValueAsMetadata .
  142. class location_op_iterator
  143. : public iterator_facade_base<location_op_iterator,
  144. std::bidirectional_iterator_tag, Value *> {
  145. PointerUnion<ValueAsMetadata *, ValueAsMetadata **> I;
  146. public:
  147. location_op_iterator(ValueAsMetadata *SingleIter) : I(SingleIter) {}
  148. location_op_iterator(ValueAsMetadata **MultiIter) : I(MultiIter) {}
  149. location_op_iterator(const location_op_iterator &R) : I(R.I) {}
  150. location_op_iterator &operator=(const location_op_iterator &R) {
  151. I = R.I;
  152. return *this;
  153. }
  154. bool operator==(const location_op_iterator &RHS) const {
  155. return I == RHS.I;
  156. }
  157. const Value *operator*() const {
  158. ValueAsMetadata *VAM = I.is<ValueAsMetadata *>()
  159. ? I.get<ValueAsMetadata *>()
  160. : *I.get<ValueAsMetadata **>();
  161. return VAM->getValue();
  162. };
  163. Value *operator*() {
  164. ValueAsMetadata *VAM = I.is<ValueAsMetadata *>()
  165. ? I.get<ValueAsMetadata *>()
  166. : *I.get<ValueAsMetadata **>();
  167. return VAM->getValue();
  168. }
  169. location_op_iterator &operator++() {
  170. if (I.is<ValueAsMetadata *>())
  171. I = I.get<ValueAsMetadata *>() + 1;
  172. else
  173. I = I.get<ValueAsMetadata **>() + 1;
  174. return *this;
  175. }
  176. location_op_iterator &operator--() {
  177. if (I.is<ValueAsMetadata *>())
  178. I = I.get<ValueAsMetadata *>() - 1;
  179. else
  180. I = I.get<ValueAsMetadata **>() - 1;
  181. return *this;
  182. }
  183. };
  184. /// Get the locations corresponding to the variable referenced by the debug
  185. /// info intrinsic. Depending on the intrinsic, this could be the
  186. /// variable's value or its address.
  187. iterator_range<location_op_iterator> location_ops() const;
  188. Value *getVariableLocationOp(unsigned OpIdx) const;
  189. void replaceVariableLocationOp(Value *OldValue, Value *NewValue);
  190. void replaceVariableLocationOp(unsigned OpIdx, Value *NewValue);
  191. void setVariable(DILocalVariable *NewVar) {
  192. setArgOperand(1, MetadataAsValue::get(NewVar->getContext(), NewVar));
  193. }
  194. void setExpression(DIExpression *NewExpr) {
  195. setArgOperand(2, MetadataAsValue::get(NewExpr->getContext(), NewExpr));
  196. }
  197. unsigned getNumVariableLocationOps() const {
  198. if (hasArgList())
  199. return cast<DIArgList>(getRawLocation())->getArgs().size();
  200. return 1;
  201. }
  202. bool hasArgList() const { return isa<DIArgList>(getRawLocation()); }
  203. /// Does this describe the address of a local variable. True for dbg.addr
  204. /// and dbg.declare, but not dbg.value, which describes its value.
  205. bool isAddressOfVariable() const {
  206. return getIntrinsicID() != Intrinsic::dbg_value;
  207. }
  208. void setUndef() {
  209. // TODO: When/if we remove duplicate values from DIArgLists, we don't need
  210. // this set anymore.
  211. SmallPtrSet<Value *, 4> RemovedValues;
  212. for (Value *OldValue : location_ops()) {
  213. if (!RemovedValues.insert(OldValue).second)
  214. continue;
  215. Value *Undef = UndefValue::get(OldValue->getType());
  216. replaceVariableLocationOp(OldValue, Undef);
  217. }
  218. }
  219. bool isUndef() const {
  220. return (getNumVariableLocationOps() == 0 &&
  221. !getExpression()->isComplex()) ||
  222. any_of(location_ops(), [](Value *V) { return isa<UndefValue>(V); });
  223. }
  224. DILocalVariable *getVariable() const {
  225. return cast<DILocalVariable>(getRawVariable());
  226. }
  227. DIExpression *getExpression() const {
  228. return cast<DIExpression>(getRawExpression());
  229. }
  230. Metadata *getRawLocation() const {
  231. return cast<MetadataAsValue>(getArgOperand(0))->getMetadata();
  232. }
  233. Metadata *getRawVariable() const {
  234. return cast<MetadataAsValue>(getArgOperand(1))->getMetadata();
  235. }
  236. Metadata *getRawExpression() const {
  237. return cast<MetadataAsValue>(getArgOperand(2))->getMetadata();
  238. }
  239. /// Use of this should generally be avoided; instead,
  240. /// replaceVariableLocationOp and addVariableLocationOps should be used where
  241. /// possible to avoid creating invalid state.
  242. void setRawLocation(Metadata *Location) {
  243. return setArgOperand(0, MetadataAsValue::get(getContext(), Location));
  244. }
  245. /// Get the size (in bits) of the variable, or fragment of the variable that
  246. /// is described.
  247. Optional<uint64_t> getFragmentSizeInBits() const;
  248. /// \name Casting methods
  249. /// @{
  250. static bool classof(const IntrinsicInst *I) {
  251. switch (I->getIntrinsicID()) {
  252. case Intrinsic::dbg_declare:
  253. case Intrinsic::dbg_value:
  254. case Intrinsic::dbg_addr:
  255. return true;
  256. default:
  257. return false;
  258. }
  259. }
  260. static bool classof(const Value *V) {
  261. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  262. }
  263. /// @}
  264. private:
  265. void setArgOperand(unsigned i, Value *v) {
  266. DbgInfoIntrinsic::setArgOperand(i, v);
  267. }
  268. void setOperand(unsigned i, Value *v) { DbgInfoIntrinsic::setOperand(i, v); }
  269. };
  270. /// This represents the llvm.dbg.declare instruction.
  271. class DbgDeclareInst : public DbgVariableIntrinsic {
  272. public:
  273. Value *getAddress() const {
  274. assert(getNumVariableLocationOps() == 1 &&
  275. "dbg.declare must have exactly 1 location operand.");
  276. return getVariableLocationOp(0);
  277. }
  278. /// \name Casting methods
  279. /// @{
  280. static bool classof(const IntrinsicInst *I) {
  281. return I->getIntrinsicID() == Intrinsic::dbg_declare;
  282. }
  283. static bool classof(const Value *V) {
  284. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  285. }
  286. /// @}
  287. };
  288. /// This represents the llvm.dbg.addr instruction.
  289. class DbgAddrIntrinsic : public DbgVariableIntrinsic {
  290. public:
  291. Value *getAddress() const {
  292. assert(getNumVariableLocationOps() == 1 &&
  293. "dbg.addr must have exactly 1 location operand.");
  294. return getVariableLocationOp(0);
  295. }
  296. /// \name Casting methods
  297. /// @{
  298. static bool classof(const IntrinsicInst *I) {
  299. return I->getIntrinsicID() == Intrinsic::dbg_addr;
  300. }
  301. static bool classof(const Value *V) {
  302. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  303. }
  304. };
  305. /// This represents the llvm.dbg.value instruction.
  306. class DbgValueInst : public DbgVariableIntrinsic {
  307. public:
  308. // The default argument should only be used in ISel, and the default option
  309. // should be removed once ISel support for multiple location ops is complete.
  310. Value *getValue(unsigned OpIdx = 0) const {
  311. return getVariableLocationOp(OpIdx);
  312. }
  313. iterator_range<location_op_iterator> getValues() const {
  314. return location_ops();
  315. }
  316. /// \name Casting methods
  317. /// @{
  318. static bool classof(const IntrinsicInst *I) {
  319. return I->getIntrinsicID() == Intrinsic::dbg_value;
  320. }
  321. static bool classof(const Value *V) {
  322. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  323. }
  324. /// @}
  325. };
  326. /// This represents the llvm.dbg.label instruction.
  327. class DbgLabelInst : public DbgInfoIntrinsic {
  328. public:
  329. DILabel *getLabel() const { return cast<DILabel>(getRawLabel()); }
  330. Metadata *getRawLabel() const {
  331. return cast<MetadataAsValue>(getArgOperand(0))->getMetadata();
  332. }
  333. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  334. /// @{
  335. static bool classof(const IntrinsicInst *I) {
  336. return I->getIntrinsicID() == Intrinsic::dbg_label;
  337. }
  338. static bool classof(const Value *V) {
  339. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  340. }
  341. /// @}
  342. };
  343. /// This is the common base class for vector predication intrinsics.
  344. class VPIntrinsic : public IntrinsicInst {
  345. public:
  346. static Optional<int> GetMaskParamPos(Intrinsic::ID IntrinsicID);
  347. static Optional<int> GetVectorLengthParamPos(Intrinsic::ID IntrinsicID);
  348. /// The llvm.vp.* intrinsics for this instruction Opcode
  349. static Intrinsic::ID GetForOpcode(unsigned OC);
  350. // Whether \p ID is a VP intrinsic ID.
  351. static bool IsVPIntrinsic(Intrinsic::ID);
  352. /// \return the mask parameter or nullptr.
  353. Value *getMaskParam() const;
  354. void setMaskParam(Value *);
  355. /// \return the vector length parameter or nullptr.
  356. Value *getVectorLengthParam() const;
  357. void setVectorLengthParam(Value *);
  358. /// \return whether the vector length param can be ignored.
  359. bool canIgnoreVectorLengthParam() const;
  360. /// \return the static element count (vector number of elements) the vector
  361. /// length parameter applies to.
  362. ElementCount getStaticVectorLength() const;
  363. // Methods for support type inquiry through isa, cast, and dyn_cast:
  364. static bool classof(const IntrinsicInst *I) {
  365. return IsVPIntrinsic(I->getIntrinsicID());
  366. }
  367. static bool classof(const Value *V) {
  368. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  369. }
  370. // Equivalent non-predicated opcode
  371. Optional<unsigned> getFunctionalOpcode() const {
  372. return GetFunctionalOpcodeForVP(getIntrinsicID());
  373. }
  374. // Equivalent non-predicated opcode
  375. static Optional<unsigned> GetFunctionalOpcodeForVP(Intrinsic::ID ID);
  376. };
  377. /// This is the common base class for constrained floating point intrinsics.
  378. class ConstrainedFPIntrinsic : public IntrinsicInst {
  379. public:
  380. bool isUnaryOp() const;
  381. bool isTernaryOp() const;
  382. Optional<RoundingMode> getRoundingMode() const;
  383. Optional<fp::ExceptionBehavior> getExceptionBehavior() const;
  384. bool isDefaultFPEnvironment() const;
  385. // Methods for support type inquiry through isa, cast, and dyn_cast:
  386. static bool classof(const IntrinsicInst *I);
  387. static bool classof(const Value *V) {
  388. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  389. }
  390. };
  391. /// Constrained floating point compare intrinsics.
  392. class ConstrainedFPCmpIntrinsic : public ConstrainedFPIntrinsic {
  393. public:
  394. FCmpInst::Predicate getPredicate() const;
  395. // Methods for support type inquiry through isa, cast, and dyn_cast:
  396. static bool classof(const IntrinsicInst *I) {
  397. switch (I->getIntrinsicID()) {
  398. case Intrinsic::experimental_constrained_fcmp:
  399. case Intrinsic::experimental_constrained_fcmps:
  400. return true;
  401. default:
  402. return false;
  403. }
  404. }
  405. static bool classof(const Value *V) {
  406. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  407. }
  408. };
  409. /// This class represents min/max intrinsics.
  410. class MinMaxIntrinsic : public IntrinsicInst {
  411. public:
  412. static bool classof(const IntrinsicInst *I) {
  413. switch (I->getIntrinsicID()) {
  414. case Intrinsic::umin:
  415. case Intrinsic::umax:
  416. case Intrinsic::smin:
  417. case Intrinsic::smax:
  418. return true;
  419. default:
  420. return false;
  421. }
  422. }
  423. static bool classof(const Value *V) {
  424. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  425. }
  426. Value *getLHS() const { return const_cast<Value *>(getArgOperand(0)); }
  427. Value *getRHS() const { return const_cast<Value *>(getArgOperand(1)); }
  428. /// Returns the comparison predicate underlying the intrinsic.
  429. ICmpInst::Predicate getPredicate() const {
  430. switch (getIntrinsicID()) {
  431. case Intrinsic::umin:
  432. return ICmpInst::Predicate::ICMP_ULT;
  433. case Intrinsic::umax:
  434. return ICmpInst::Predicate::ICMP_UGT;
  435. case Intrinsic::smin:
  436. return ICmpInst::Predicate::ICMP_SLT;
  437. case Intrinsic::smax:
  438. return ICmpInst::Predicate::ICMP_SGT;
  439. default:
  440. llvm_unreachable("Invalid intrinsic");
  441. }
  442. }
  443. /// Whether the intrinsic is signed or unsigned.
  444. bool isSigned() const { return ICmpInst::isSigned(getPredicate()); };
  445. };
  446. /// This class represents an intrinsic that is based on a binary operation.
  447. /// This includes op.with.overflow and saturating add/sub intrinsics.
  448. class BinaryOpIntrinsic : public IntrinsicInst {
  449. public:
  450. static bool classof(const IntrinsicInst *I) {
  451. switch (I->getIntrinsicID()) {
  452. case Intrinsic::uadd_with_overflow:
  453. case Intrinsic::sadd_with_overflow:
  454. case Intrinsic::usub_with_overflow:
  455. case Intrinsic::ssub_with_overflow:
  456. case Intrinsic::umul_with_overflow:
  457. case Intrinsic::smul_with_overflow:
  458. case Intrinsic::uadd_sat:
  459. case Intrinsic::sadd_sat:
  460. case Intrinsic::usub_sat:
  461. case Intrinsic::ssub_sat:
  462. return true;
  463. default:
  464. return false;
  465. }
  466. }
  467. static bool classof(const Value *V) {
  468. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  469. }
  470. Value *getLHS() const { return const_cast<Value *>(getArgOperand(0)); }
  471. Value *getRHS() const { return const_cast<Value *>(getArgOperand(1)); }
  472. /// Returns the binary operation underlying the intrinsic.
  473. Instruction::BinaryOps getBinaryOp() const;
  474. /// Whether the intrinsic is signed or unsigned.
  475. bool isSigned() const;
  476. /// Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
  477. unsigned getNoWrapKind() const;
  478. };
  479. /// Represents an op.with.overflow intrinsic.
  480. class WithOverflowInst : public BinaryOpIntrinsic {
  481. public:
  482. static bool classof(const IntrinsicInst *I) {
  483. switch (I->getIntrinsicID()) {
  484. case Intrinsic::uadd_with_overflow:
  485. case Intrinsic::sadd_with_overflow:
  486. case Intrinsic::usub_with_overflow:
  487. case Intrinsic::ssub_with_overflow:
  488. case Intrinsic::umul_with_overflow:
  489. case Intrinsic::smul_with_overflow:
  490. return true;
  491. default:
  492. return false;
  493. }
  494. }
  495. static bool classof(const Value *V) {
  496. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  497. }
  498. };
  499. /// Represents a saturating add/sub intrinsic.
  500. class SaturatingInst : public BinaryOpIntrinsic {
  501. public:
  502. static bool classof(const IntrinsicInst *I) {
  503. switch (I->getIntrinsicID()) {
  504. case Intrinsic::uadd_sat:
  505. case Intrinsic::sadd_sat:
  506. case Intrinsic::usub_sat:
  507. case Intrinsic::ssub_sat:
  508. return true;
  509. default:
  510. return false;
  511. }
  512. }
  513. static bool classof(const Value *V) {
  514. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  515. }
  516. };
  517. /// Common base class for all memory intrinsics. Simply provides
  518. /// common methods.
  519. /// Written as CRTP to avoid a common base class amongst the
  520. /// three atomicity hierarchies.
  521. template <typename Derived> class MemIntrinsicBase : public IntrinsicInst {
  522. private:
  523. enum { ARG_DEST = 0, ARG_LENGTH = 2 };
  524. public:
  525. Value *getRawDest() const {
  526. return const_cast<Value *>(getArgOperand(ARG_DEST));
  527. }
  528. const Use &getRawDestUse() const { return getArgOperandUse(ARG_DEST); }
  529. Use &getRawDestUse() { return getArgOperandUse(ARG_DEST); }
  530. Value *getLength() const {
  531. return const_cast<Value *>(getArgOperand(ARG_LENGTH));
  532. }
  533. const Use &getLengthUse() const { return getArgOperandUse(ARG_LENGTH); }
  534. Use &getLengthUse() { return getArgOperandUse(ARG_LENGTH); }
  535. /// This is just like getRawDest, but it strips off any cast
  536. /// instructions (including addrspacecast) that feed it, giving the
  537. /// original input. The returned value is guaranteed to be a pointer.
  538. Value *getDest() const { return getRawDest()->stripPointerCasts(); }
  539. unsigned getDestAddressSpace() const {
  540. return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
  541. }
  542. /// FIXME: Remove this function once transition to Align is over.
  543. /// Use getDestAlign() instead.
  544. unsigned getDestAlignment() const {
  545. if (auto MA = getParamAlign(ARG_DEST))
  546. return MA->value();
  547. return 0;
  548. }
  549. MaybeAlign getDestAlign() const { return getParamAlign(ARG_DEST); }
  550. /// Set the specified arguments of the instruction.
  551. void setDest(Value *Ptr) {
  552. assert(getRawDest()->getType() == Ptr->getType() &&
  553. "setDest called with pointer of wrong type!");
  554. setArgOperand(ARG_DEST, Ptr);
  555. }
  556. /// FIXME: Remove this function once transition to Align is over.
  557. /// Use the version that takes MaybeAlign instead of this one.
  558. void setDestAlignment(unsigned Alignment) {
  559. setDestAlignment(MaybeAlign(Alignment));
  560. }
  561. void setDestAlignment(MaybeAlign Alignment) {
  562. removeParamAttr(ARG_DEST, Attribute::Alignment);
  563. if (Alignment)
  564. addParamAttr(ARG_DEST,
  565. Attribute::getWithAlignment(getContext(), *Alignment));
  566. }
  567. void setDestAlignment(Align Alignment) {
  568. removeParamAttr(ARG_DEST, Attribute::Alignment);
  569. addParamAttr(ARG_DEST,
  570. Attribute::getWithAlignment(getContext(), Alignment));
  571. }
  572. void setLength(Value *L) {
  573. assert(getLength()->getType() == L->getType() &&
  574. "setLength called with value of wrong type!");
  575. setArgOperand(ARG_LENGTH, L);
  576. }
  577. };
  578. /// Common base class for all memory transfer intrinsics. Simply provides
  579. /// common methods.
  580. template <class BaseCL> class MemTransferBase : public BaseCL {
  581. private:
  582. enum { ARG_SOURCE = 1 };
  583. public:
  584. /// Return the arguments to the instruction.
  585. Value *getRawSource() const {
  586. return const_cast<Value *>(BaseCL::getArgOperand(ARG_SOURCE));
  587. }
  588. const Use &getRawSourceUse() const {
  589. return BaseCL::getArgOperandUse(ARG_SOURCE);
  590. }
  591. Use &getRawSourceUse() { return BaseCL::getArgOperandUse(ARG_SOURCE); }
  592. /// This is just like getRawSource, but it strips off any cast
  593. /// instructions that feed it, giving the original input. The returned
  594. /// value is guaranteed to be a pointer.
  595. Value *getSource() const { return getRawSource()->stripPointerCasts(); }
  596. unsigned getSourceAddressSpace() const {
  597. return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
  598. }
  599. /// FIXME: Remove this function once transition to Align is over.
  600. /// Use getSourceAlign() instead.
  601. unsigned getSourceAlignment() const {
  602. if (auto MA = BaseCL::getParamAlign(ARG_SOURCE))
  603. return MA->value();
  604. return 0;
  605. }
  606. MaybeAlign getSourceAlign() const {
  607. return BaseCL::getParamAlign(ARG_SOURCE);
  608. }
  609. void setSource(Value *Ptr) {
  610. assert(getRawSource()->getType() == Ptr->getType() &&
  611. "setSource called with pointer of wrong type!");
  612. BaseCL::setArgOperand(ARG_SOURCE, Ptr);
  613. }
  614. /// FIXME: Remove this function once transition to Align is over.
  615. /// Use the version that takes MaybeAlign instead of this one.
  616. void setSourceAlignment(unsigned Alignment) {
  617. setSourceAlignment(MaybeAlign(Alignment));
  618. }
  619. void setSourceAlignment(MaybeAlign Alignment) {
  620. BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment);
  621. if (Alignment)
  622. BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment(
  623. BaseCL::getContext(), *Alignment));
  624. }
  625. void setSourceAlignment(Align Alignment) {
  626. BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment);
  627. BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment(
  628. BaseCL::getContext(), Alignment));
  629. }
  630. };
  631. /// Common base class for all memset intrinsics. Simply provides
  632. /// common methods.
  633. template <class BaseCL> class MemSetBase : public BaseCL {
  634. private:
  635. enum { ARG_VALUE = 1 };
  636. public:
  637. Value *getValue() const {
  638. return const_cast<Value *>(BaseCL::getArgOperand(ARG_VALUE));
  639. }
  640. const Use &getValueUse() const { return BaseCL::getArgOperandUse(ARG_VALUE); }
  641. Use &getValueUse() { return BaseCL::getArgOperandUse(ARG_VALUE); }
  642. void setValue(Value *Val) {
  643. assert(getValue()->getType() == Val->getType() &&
  644. "setValue called with value of wrong type!");
  645. BaseCL::setArgOperand(ARG_VALUE, Val);
  646. }
  647. };
  648. // The common base class for the atomic memset/memmove/memcpy intrinsics
  649. // i.e. llvm.element.unordered.atomic.memset/memcpy/memmove
  650. class AtomicMemIntrinsic : public MemIntrinsicBase<AtomicMemIntrinsic> {
  651. private:
  652. enum { ARG_ELEMENTSIZE = 3 };
  653. public:
  654. Value *getRawElementSizeInBytes() const {
  655. return const_cast<Value *>(getArgOperand(ARG_ELEMENTSIZE));
  656. }
  657. ConstantInt *getElementSizeInBytesCst() const {
  658. return cast<ConstantInt>(getRawElementSizeInBytes());
  659. }
  660. uint32_t getElementSizeInBytes() const {
  661. return getElementSizeInBytesCst()->getZExtValue();
  662. }
  663. void setElementSizeInBytes(Constant *V) {
  664. assert(V->getType() == Type::getInt8Ty(getContext()) &&
  665. "setElementSizeInBytes called with value of wrong type!");
  666. setArgOperand(ARG_ELEMENTSIZE, V);
  667. }
  668. static bool classof(const IntrinsicInst *I) {
  669. switch (I->getIntrinsicID()) {
  670. case Intrinsic::memcpy_element_unordered_atomic:
  671. case Intrinsic::memmove_element_unordered_atomic:
  672. case Intrinsic::memset_element_unordered_atomic:
  673. return true;
  674. default:
  675. return false;
  676. }
  677. }
  678. static bool classof(const Value *V) {
  679. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  680. }
  681. };
  682. /// This class represents atomic memset intrinsic
  683. // i.e. llvm.element.unordered.atomic.memset
  684. class AtomicMemSetInst : public MemSetBase<AtomicMemIntrinsic> {
  685. public:
  686. static bool classof(const IntrinsicInst *I) {
  687. return I->getIntrinsicID() == Intrinsic::memset_element_unordered_atomic;
  688. }
  689. static bool classof(const Value *V) {
  690. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  691. }
  692. };
  693. // This class wraps the atomic memcpy/memmove intrinsics
  694. // i.e. llvm.element.unordered.atomic.memcpy/memmove
  695. class AtomicMemTransferInst : public MemTransferBase<AtomicMemIntrinsic> {
  696. public:
  697. static bool classof(const IntrinsicInst *I) {
  698. switch (I->getIntrinsicID()) {
  699. case Intrinsic::memcpy_element_unordered_atomic:
  700. case Intrinsic::memmove_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 represents the atomic memcpy intrinsic
  711. /// i.e. llvm.element.unordered.atomic.memcpy
  712. class AtomicMemCpyInst : public AtomicMemTransferInst {
  713. public:
  714. static bool classof(const IntrinsicInst *I) {
  715. return I->getIntrinsicID() == Intrinsic::memcpy_element_unordered_atomic;
  716. }
  717. static bool classof(const Value *V) {
  718. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  719. }
  720. };
  721. /// This class represents the atomic memmove intrinsic
  722. /// i.e. llvm.element.unordered.atomic.memmove
  723. class AtomicMemMoveInst : public AtomicMemTransferInst {
  724. public:
  725. static bool classof(const IntrinsicInst *I) {
  726. return I->getIntrinsicID() == Intrinsic::memmove_element_unordered_atomic;
  727. }
  728. static bool classof(const Value *V) {
  729. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  730. }
  731. };
  732. /// This is the common base class for memset/memcpy/memmove.
  733. class MemIntrinsic : public MemIntrinsicBase<MemIntrinsic> {
  734. private:
  735. enum { ARG_VOLATILE = 3 };
  736. public:
  737. ConstantInt *getVolatileCst() const {
  738. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(ARG_VOLATILE)));
  739. }
  740. bool isVolatile() const { return !getVolatileCst()->isZero(); }
  741. void setVolatile(Constant *V) { setArgOperand(ARG_VOLATILE, V); }
  742. // Methods for support type inquiry through isa, cast, and dyn_cast:
  743. static bool classof(const IntrinsicInst *I) {
  744. switch (I->getIntrinsicID()) {
  745. case Intrinsic::memcpy:
  746. case Intrinsic::memmove:
  747. case Intrinsic::memset:
  748. case Intrinsic::memcpy_inline:
  749. return true;
  750. default:
  751. return false;
  752. }
  753. }
  754. static bool classof(const Value *V) {
  755. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  756. }
  757. };
  758. /// This class wraps the llvm.memset intrinsic.
  759. class MemSetInst : public MemSetBase<MemIntrinsic> {
  760. public:
  761. // Methods for support type inquiry through isa, cast, and dyn_cast:
  762. static bool classof(const IntrinsicInst *I) {
  763. return I->getIntrinsicID() == Intrinsic::memset;
  764. }
  765. static bool classof(const Value *V) {
  766. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  767. }
  768. };
  769. /// This class wraps the llvm.memcpy/memmove intrinsics.
  770. class MemTransferInst : public MemTransferBase<MemIntrinsic> {
  771. public:
  772. // Methods for support type inquiry through isa, cast, and dyn_cast:
  773. static bool classof(const IntrinsicInst *I) {
  774. switch (I->getIntrinsicID()) {
  775. case Intrinsic::memcpy:
  776. case Intrinsic::memmove:
  777. case Intrinsic::memcpy_inline:
  778. return true;
  779. default:
  780. return false;
  781. }
  782. }
  783. static bool classof(const Value *V) {
  784. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  785. }
  786. };
  787. /// This class wraps the llvm.memcpy intrinsic.
  788. class MemCpyInst : public MemTransferInst {
  789. public:
  790. // Methods for support type inquiry through isa, cast, and dyn_cast:
  791. static bool classof(const IntrinsicInst *I) {
  792. return I->getIntrinsicID() == Intrinsic::memcpy;
  793. }
  794. static bool classof(const Value *V) {
  795. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  796. }
  797. };
  798. /// This class wraps the llvm.memmove intrinsic.
  799. class MemMoveInst : public MemTransferInst {
  800. public:
  801. // Methods for support type inquiry through isa, cast, and dyn_cast:
  802. static bool classof(const IntrinsicInst *I) {
  803. return I->getIntrinsicID() == Intrinsic::memmove;
  804. }
  805. static bool classof(const Value *V) {
  806. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  807. }
  808. };
  809. /// This class wraps the llvm.memcpy.inline intrinsic.
  810. class MemCpyInlineInst : public MemTransferInst {
  811. public:
  812. ConstantInt *getLength() const {
  813. return cast<ConstantInt>(MemTransferInst::getLength());
  814. }
  815. // Methods for support type inquiry through isa, cast, and dyn_cast:
  816. static bool classof(const IntrinsicInst *I) {
  817. return I->getIntrinsicID() == Intrinsic::memcpy_inline;
  818. }
  819. static bool classof(const Value *V) {
  820. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  821. }
  822. };
  823. // The common base class for any memset/memmove/memcpy intrinsics;
  824. // whether they be atomic or non-atomic.
  825. // i.e. llvm.element.unordered.atomic.memset/memcpy/memmove
  826. // and llvm.memset/memcpy/memmove
  827. class AnyMemIntrinsic : public MemIntrinsicBase<AnyMemIntrinsic> {
  828. public:
  829. bool isVolatile() const {
  830. // Only the non-atomic intrinsics can be volatile
  831. if (auto *MI = dyn_cast<MemIntrinsic>(this))
  832. return MI->isVolatile();
  833. return false;
  834. }
  835. static bool classof(const IntrinsicInst *I) {
  836. switch (I->getIntrinsicID()) {
  837. case Intrinsic::memcpy:
  838. case Intrinsic::memcpy_inline:
  839. case Intrinsic::memmove:
  840. case Intrinsic::memset:
  841. case Intrinsic::memcpy_element_unordered_atomic:
  842. case Intrinsic::memmove_element_unordered_atomic:
  843. case Intrinsic::memset_element_unordered_atomic:
  844. return true;
  845. default:
  846. return false;
  847. }
  848. }
  849. static bool classof(const Value *V) {
  850. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  851. }
  852. };
  853. /// This class represents any memset intrinsic
  854. // i.e. llvm.element.unordered.atomic.memset
  855. // and llvm.memset
  856. class AnyMemSetInst : public MemSetBase<AnyMemIntrinsic> {
  857. public:
  858. static bool classof(const IntrinsicInst *I) {
  859. switch (I->getIntrinsicID()) {
  860. case Intrinsic::memset:
  861. case Intrinsic::memset_element_unordered_atomic:
  862. return true;
  863. default:
  864. return false;
  865. }
  866. }
  867. static bool classof(const Value *V) {
  868. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  869. }
  870. };
  871. // This class wraps any memcpy/memmove intrinsics
  872. // i.e. llvm.element.unordered.atomic.memcpy/memmove
  873. // and llvm.memcpy/memmove
  874. class AnyMemTransferInst : public MemTransferBase<AnyMemIntrinsic> {
  875. public:
  876. static bool classof(const IntrinsicInst *I) {
  877. switch (I->getIntrinsicID()) {
  878. case Intrinsic::memcpy:
  879. case Intrinsic::memcpy_inline:
  880. case Intrinsic::memmove:
  881. case Intrinsic::memcpy_element_unordered_atomic:
  882. case Intrinsic::memmove_element_unordered_atomic:
  883. return true;
  884. default:
  885. return false;
  886. }
  887. }
  888. static bool classof(const Value *V) {
  889. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  890. }
  891. };
  892. /// This class represents any memcpy intrinsic
  893. /// i.e. llvm.element.unordered.atomic.memcpy
  894. /// and llvm.memcpy
  895. class AnyMemCpyInst : public AnyMemTransferInst {
  896. public:
  897. static bool classof(const IntrinsicInst *I) {
  898. switch (I->getIntrinsicID()) {
  899. case Intrinsic::memcpy:
  900. case Intrinsic::memcpy_inline:
  901. case Intrinsic::memcpy_element_unordered_atomic:
  902. return true;
  903. default:
  904. return false;
  905. }
  906. }
  907. static bool classof(const Value *V) {
  908. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  909. }
  910. };
  911. /// This class represents any memmove intrinsic
  912. /// i.e. llvm.element.unordered.atomic.memmove
  913. /// and llvm.memmove
  914. class AnyMemMoveInst : public AnyMemTransferInst {
  915. public:
  916. static bool classof(const IntrinsicInst *I) {
  917. switch (I->getIntrinsicID()) {
  918. case Intrinsic::memmove:
  919. case Intrinsic::memmove_element_unordered_atomic:
  920. return true;
  921. default:
  922. return false;
  923. }
  924. }
  925. static bool classof(const Value *V) {
  926. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  927. }
  928. };
  929. /// This represents the llvm.va_start intrinsic.
  930. class VAStartInst : public IntrinsicInst {
  931. public:
  932. static bool classof(const IntrinsicInst *I) {
  933. return I->getIntrinsicID() == Intrinsic::vastart;
  934. }
  935. static bool classof(const Value *V) {
  936. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  937. }
  938. Value *getArgList() const { return const_cast<Value *>(getArgOperand(0)); }
  939. };
  940. /// This represents the llvm.va_end intrinsic.
  941. class VAEndInst : public IntrinsicInst {
  942. public:
  943. static bool classof(const IntrinsicInst *I) {
  944. return I->getIntrinsicID() == Intrinsic::vaend;
  945. }
  946. static bool classof(const Value *V) {
  947. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  948. }
  949. Value *getArgList() const { return const_cast<Value *>(getArgOperand(0)); }
  950. };
  951. /// This represents the llvm.va_copy intrinsic.
  952. class VACopyInst : public IntrinsicInst {
  953. public:
  954. static bool classof(const IntrinsicInst *I) {
  955. return I->getIntrinsicID() == Intrinsic::vacopy;
  956. }
  957. static bool classof(const Value *V) {
  958. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  959. }
  960. Value *getDest() const { return const_cast<Value *>(getArgOperand(0)); }
  961. Value *getSrc() const { return const_cast<Value *>(getArgOperand(1)); }
  962. };
  963. /// This represents the llvm.instrprof_increment intrinsic.
  964. class InstrProfIncrementInst : public IntrinsicInst {
  965. public:
  966. static bool classof(const IntrinsicInst *I) {
  967. return I->getIntrinsicID() == Intrinsic::instrprof_increment;
  968. }
  969. static bool classof(const Value *V) {
  970. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  971. }
  972. GlobalVariable *getName() const {
  973. return cast<GlobalVariable>(
  974. const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
  975. }
  976. ConstantInt *getHash() const {
  977. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
  978. }
  979. ConstantInt *getNumCounters() const {
  980. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
  981. }
  982. ConstantInt *getIndex() const {
  983. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
  984. }
  985. Value *getStep() const;
  986. };
  987. class InstrProfIncrementInstStep : public InstrProfIncrementInst {
  988. public:
  989. static bool classof(const IntrinsicInst *I) {
  990. return I->getIntrinsicID() == Intrinsic::instrprof_increment_step;
  991. }
  992. static bool classof(const Value *V) {
  993. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  994. }
  995. };
  996. /// This represents the llvm.instrprof_value_profile intrinsic.
  997. class InstrProfValueProfileInst : public IntrinsicInst {
  998. public:
  999. static bool classof(const IntrinsicInst *I) {
  1000. return I->getIntrinsicID() == Intrinsic::instrprof_value_profile;
  1001. }
  1002. static bool classof(const Value *V) {
  1003. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1004. }
  1005. GlobalVariable *getName() const {
  1006. return cast<GlobalVariable>(
  1007. const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
  1008. }
  1009. ConstantInt *getHash() const {
  1010. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
  1011. }
  1012. Value *getTargetValue() const {
  1013. return cast<Value>(const_cast<Value *>(getArgOperand(2)));
  1014. }
  1015. ConstantInt *getValueKind() const {
  1016. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
  1017. }
  1018. // Returns the value site index.
  1019. ConstantInt *getIndex() const {
  1020. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(4)));
  1021. }
  1022. };
  1023. class PseudoProbeInst : public IntrinsicInst {
  1024. public:
  1025. static bool classof(const IntrinsicInst *I) {
  1026. return I->getIntrinsicID() == Intrinsic::pseudoprobe;
  1027. }
  1028. static bool classof(const Value *V) {
  1029. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1030. }
  1031. ConstantInt *getFuncGuid() const {
  1032. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(0)));
  1033. }
  1034. ConstantInt *getIndex() const {
  1035. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
  1036. }
  1037. ConstantInt *getAttributes() const {
  1038. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
  1039. }
  1040. ConstantInt *getFactor() const {
  1041. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
  1042. }
  1043. };
  1044. class NoAliasScopeDeclInst : public IntrinsicInst {
  1045. public:
  1046. static bool classof(const IntrinsicInst *I) {
  1047. return I->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl;
  1048. }
  1049. static bool classof(const Value *V) {
  1050. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1051. }
  1052. MDNode *getScopeList() const {
  1053. auto *MV =
  1054. cast<MetadataAsValue>(getOperand(Intrinsic::NoAliasScopeDeclScopeArg));
  1055. return cast<MDNode>(MV->getMetadata());
  1056. }
  1057. void setScopeList(MDNode *ScopeList) {
  1058. setOperand(Intrinsic::NoAliasScopeDeclScopeArg,
  1059. MetadataAsValue::get(getContext(), ScopeList));
  1060. }
  1061. };
  1062. // Defined in Statepoint.h -- NOT a subclass of IntrinsicInst
  1063. class GCStatepointInst;
  1064. /// Common base class for representing values projected from a statepoint.
  1065. /// Currently, the only projections available are gc.result and gc.relocate.
  1066. class GCProjectionInst : public IntrinsicInst {
  1067. public:
  1068. static bool classof(const IntrinsicInst *I) {
  1069. return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate ||
  1070. I->getIntrinsicID() == Intrinsic::experimental_gc_result;
  1071. }
  1072. static bool classof(const Value *V) {
  1073. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1074. }
  1075. /// Return true if this relocate is tied to the invoke statepoint.
  1076. /// This includes relocates which are on the unwinding path.
  1077. bool isTiedToInvoke() const {
  1078. const Value *Token = getArgOperand(0);
  1079. return isa<LandingPadInst>(Token) || isa<InvokeInst>(Token);
  1080. }
  1081. /// The statepoint with which this gc.relocate is associated.
  1082. const GCStatepointInst *getStatepoint() const;
  1083. };
  1084. /// Represents calls to the gc.relocate intrinsic.
  1085. class GCRelocateInst : public GCProjectionInst {
  1086. public:
  1087. static bool classof(const IntrinsicInst *I) {
  1088. return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate;
  1089. }
  1090. static bool classof(const Value *V) {
  1091. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1092. }
  1093. /// The index into the associate statepoint's argument list
  1094. /// which contains the base pointer of the pointer whose
  1095. /// relocation this gc.relocate describes.
  1096. unsigned getBasePtrIndex() const {
  1097. return cast<ConstantInt>(getArgOperand(1))->getZExtValue();
  1098. }
  1099. /// The index into the associate statepoint's argument list which
  1100. /// contains the pointer whose relocation this gc.relocate describes.
  1101. unsigned getDerivedPtrIndex() const {
  1102. return cast<ConstantInt>(getArgOperand(2))->getZExtValue();
  1103. }
  1104. Value *getBasePtr() const;
  1105. Value *getDerivedPtr() const;
  1106. };
  1107. /// Represents calls to the gc.result intrinsic.
  1108. class GCResultInst : public GCProjectionInst {
  1109. public:
  1110. static bool classof(const IntrinsicInst *I) {
  1111. return I->getIntrinsicID() == Intrinsic::experimental_gc_result;
  1112. }
  1113. static bool classof(const Value *V) {
  1114. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1115. }
  1116. };
  1117. /// This represents the llvm.assume intrinsic.
  1118. class AssumeInst : public IntrinsicInst {
  1119. public:
  1120. static bool classof(const IntrinsicInst *I) {
  1121. return I->getIntrinsicID() == Intrinsic::assume;
  1122. }
  1123. static bool classof(const Value *V) {
  1124. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1125. }
  1126. };
  1127. } // end namespace llvm
  1128. #endif // LLVM_IR_INTRINSICINST_H