IntrinsicInst.h 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552
  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/DebugInfoMetadata.h"
  31. #include "llvm/IR/DerivedTypes.h"
  32. #include "llvm/IR/FPEnv.h"
  33. #include "llvm/IR/Function.h"
  34. #include "llvm/IR/GlobalVariable.h"
  35. #include "llvm/IR/Instructions.h"
  36. #include "llvm/IR/Intrinsics.h"
  37. #include "llvm/IR/Value.h"
  38. #include "llvm/Support/Casting.h"
  39. #include <cassert>
  40. #include <cstdint>
  41. #include <optional>
  42. namespace llvm {
  43. class Metadata;
  44. /// A wrapper class for inspecting calls to intrinsic functions.
  45. /// This allows the standard isa/dyncast/cast functionality to work with calls
  46. /// to intrinsic functions.
  47. class IntrinsicInst : public CallInst {
  48. public:
  49. IntrinsicInst() = delete;
  50. IntrinsicInst(const IntrinsicInst &) = delete;
  51. IntrinsicInst &operator=(const IntrinsicInst &) = delete;
  52. /// Return the intrinsic ID of this intrinsic.
  53. Intrinsic::ID getIntrinsicID() const {
  54. return getCalledFunction()->getIntrinsicID();
  55. }
  56. /// Return true if swapping the first two arguments to the intrinsic produces
  57. /// the same result.
  58. bool isCommutative() const {
  59. switch (getIntrinsicID()) {
  60. case Intrinsic::maxnum:
  61. case Intrinsic::minnum:
  62. case Intrinsic::maximum:
  63. case Intrinsic::minimum:
  64. case Intrinsic::smax:
  65. case Intrinsic::smin:
  66. case Intrinsic::umax:
  67. case Intrinsic::umin:
  68. case Intrinsic::sadd_sat:
  69. case Intrinsic::uadd_sat:
  70. case Intrinsic::sadd_with_overflow:
  71. case Intrinsic::uadd_with_overflow:
  72. case Intrinsic::smul_with_overflow:
  73. case Intrinsic::umul_with_overflow:
  74. case Intrinsic::smul_fix:
  75. case Intrinsic::umul_fix:
  76. case Intrinsic::smul_fix_sat:
  77. case Intrinsic::umul_fix_sat:
  78. case Intrinsic::fma:
  79. case Intrinsic::fmuladd:
  80. return true;
  81. default:
  82. return false;
  83. }
  84. }
  85. /// Checks if the intrinsic is an annotation.
  86. bool isAssumeLikeIntrinsic() const {
  87. switch (getIntrinsicID()) {
  88. default: break;
  89. case Intrinsic::assume:
  90. case Intrinsic::sideeffect:
  91. case Intrinsic::pseudoprobe:
  92. case Intrinsic::dbg_assign:
  93. case Intrinsic::dbg_declare:
  94. case Intrinsic::dbg_value:
  95. case Intrinsic::dbg_label:
  96. case Intrinsic::invariant_start:
  97. case Intrinsic::invariant_end:
  98. case Intrinsic::lifetime_start:
  99. case Intrinsic::lifetime_end:
  100. case Intrinsic::experimental_noalias_scope_decl:
  101. case Intrinsic::objectsize:
  102. case Intrinsic::ptr_annotation:
  103. case Intrinsic::var_annotation:
  104. return true;
  105. }
  106. return false;
  107. }
  108. /// Check if the intrinsic might lower into a regular function call in the
  109. /// course of IR transformations
  110. static bool mayLowerToFunctionCall(Intrinsic::ID IID);
  111. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  112. static bool classof(const CallInst *I) {
  113. if (const Function *CF = I->getCalledFunction())
  114. return CF->isIntrinsic();
  115. return false;
  116. }
  117. static bool classof(const Value *V) {
  118. return isa<CallInst>(V) && classof(cast<CallInst>(V));
  119. }
  120. };
  121. /// Check if \p ID corresponds to a lifetime intrinsic.
  122. static inline bool isLifetimeIntrinsic(Intrinsic::ID ID) {
  123. switch (ID) {
  124. case Intrinsic::lifetime_start:
  125. case Intrinsic::lifetime_end:
  126. return true;
  127. default:
  128. return false;
  129. }
  130. }
  131. /// This is the common base class for lifetime intrinsics.
  132. class LifetimeIntrinsic : public IntrinsicInst {
  133. public:
  134. /// \name Casting methods
  135. /// @{
  136. static bool classof(const IntrinsicInst *I) {
  137. return isLifetimeIntrinsic(I->getIntrinsicID());
  138. }
  139. static bool classof(const Value *V) {
  140. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  141. }
  142. /// @}
  143. };
  144. /// Check if \p ID corresponds to a debug info intrinsic.
  145. static inline bool isDbgInfoIntrinsic(Intrinsic::ID ID) {
  146. switch (ID) {
  147. case Intrinsic::dbg_declare:
  148. case Intrinsic::dbg_value:
  149. case Intrinsic::dbg_addr:
  150. case Intrinsic::dbg_label:
  151. case Intrinsic::dbg_assign:
  152. return true;
  153. default:
  154. return false;
  155. }
  156. }
  157. /// This is the common base class for debug info intrinsics.
  158. class DbgInfoIntrinsic : public IntrinsicInst {
  159. public:
  160. /// \name Casting methods
  161. /// @{
  162. static bool classof(const IntrinsicInst *I) {
  163. return isDbgInfoIntrinsic(I->getIntrinsicID());
  164. }
  165. static bool classof(const Value *V) {
  166. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  167. }
  168. /// @}
  169. };
  170. /// This is the common base class for debug info intrinsics for variables.
  171. class DbgVariableIntrinsic : public DbgInfoIntrinsic {
  172. public:
  173. // Iterator for ValueAsMetadata that internally uses direct pointer iteration
  174. // over either a ValueAsMetadata* or a ValueAsMetadata**, dereferencing to the
  175. // ValueAsMetadata .
  176. class location_op_iterator
  177. : public iterator_facade_base<location_op_iterator,
  178. std::bidirectional_iterator_tag, Value *> {
  179. PointerUnion<ValueAsMetadata *, ValueAsMetadata **> I;
  180. public:
  181. location_op_iterator(ValueAsMetadata *SingleIter) : I(SingleIter) {}
  182. location_op_iterator(ValueAsMetadata **MultiIter) : I(MultiIter) {}
  183. location_op_iterator(const location_op_iterator &R) : I(R.I) {}
  184. location_op_iterator &operator=(const location_op_iterator &R) {
  185. I = R.I;
  186. return *this;
  187. }
  188. bool operator==(const location_op_iterator &RHS) const {
  189. return I == RHS.I;
  190. }
  191. const Value *operator*() const {
  192. ValueAsMetadata *VAM = I.is<ValueAsMetadata *>()
  193. ? I.get<ValueAsMetadata *>()
  194. : *I.get<ValueAsMetadata **>();
  195. return VAM->getValue();
  196. };
  197. Value *operator*() {
  198. ValueAsMetadata *VAM = I.is<ValueAsMetadata *>()
  199. ? I.get<ValueAsMetadata *>()
  200. : *I.get<ValueAsMetadata **>();
  201. return VAM->getValue();
  202. }
  203. location_op_iterator &operator++() {
  204. if (I.is<ValueAsMetadata *>())
  205. I = I.get<ValueAsMetadata *>() + 1;
  206. else
  207. I = I.get<ValueAsMetadata **>() + 1;
  208. return *this;
  209. }
  210. location_op_iterator &operator--() {
  211. if (I.is<ValueAsMetadata *>())
  212. I = I.get<ValueAsMetadata *>() - 1;
  213. else
  214. I = I.get<ValueAsMetadata **>() - 1;
  215. return *this;
  216. }
  217. };
  218. /// Get the locations corresponding to the variable referenced by the debug
  219. /// info intrinsic. Depending on the intrinsic, this could be the
  220. /// variable's value or its address.
  221. iterator_range<location_op_iterator> location_ops() const;
  222. Value *getVariableLocationOp(unsigned OpIdx) const;
  223. void replaceVariableLocationOp(Value *OldValue, Value *NewValue);
  224. void replaceVariableLocationOp(unsigned OpIdx, Value *NewValue);
  225. /// Adding a new location operand will always result in this intrinsic using
  226. /// an ArgList, and must always be accompanied by a new expression that uses
  227. /// the new operand.
  228. void addVariableLocationOps(ArrayRef<Value *> NewValues,
  229. DIExpression *NewExpr);
  230. void setVariable(DILocalVariable *NewVar) {
  231. setArgOperand(1, MetadataAsValue::get(NewVar->getContext(), NewVar));
  232. }
  233. void setExpression(DIExpression *NewExpr) {
  234. setArgOperand(2, MetadataAsValue::get(NewExpr->getContext(), NewExpr));
  235. }
  236. unsigned getNumVariableLocationOps() const {
  237. if (hasArgList())
  238. return cast<DIArgList>(getRawLocation())->getArgs().size();
  239. return 1;
  240. }
  241. bool hasArgList() const { return isa<DIArgList>(getRawLocation()); }
  242. /// Does this describe the address of a local variable. True for dbg.addr and
  243. /// dbg.declare, but not dbg.value, which describes its value, or dbg.assign,
  244. /// which describes a combination of the variable's value and address.
  245. bool isAddressOfVariable() const {
  246. return getIntrinsicID() != Intrinsic::dbg_value &&
  247. getIntrinsicID() != Intrinsic::dbg_assign;
  248. }
  249. void setKillLocation() {
  250. // TODO: When/if we remove duplicate values from DIArgLists, we don't need
  251. // this set anymore.
  252. SmallPtrSet<Value *, 4> RemovedValues;
  253. for (Value *OldValue : location_ops()) {
  254. if (!RemovedValues.insert(OldValue).second)
  255. continue;
  256. Value *Poison = PoisonValue::get(OldValue->getType());
  257. replaceVariableLocationOp(OldValue, Poison);
  258. }
  259. }
  260. bool isKillLocation() const {
  261. return (getNumVariableLocationOps() == 0 &&
  262. !getExpression()->isComplex()) ||
  263. any_of(location_ops(), [](Value *V) { return isa<UndefValue>(V); });
  264. }
  265. DILocalVariable *getVariable() const {
  266. return cast<DILocalVariable>(getRawVariable());
  267. }
  268. DIExpression *getExpression() const {
  269. return cast<DIExpression>(getRawExpression());
  270. }
  271. Metadata *getRawLocation() const {
  272. return cast<MetadataAsValue>(getArgOperand(0))->getMetadata();
  273. }
  274. Metadata *getRawVariable() const {
  275. return cast<MetadataAsValue>(getArgOperand(1))->getMetadata();
  276. }
  277. Metadata *getRawExpression() const {
  278. return cast<MetadataAsValue>(getArgOperand(2))->getMetadata();
  279. }
  280. /// Use of this should generally be avoided; instead,
  281. /// replaceVariableLocationOp and addVariableLocationOps should be used where
  282. /// possible to avoid creating invalid state.
  283. void setRawLocation(Metadata *Location) {
  284. return setArgOperand(0, MetadataAsValue::get(getContext(), Location));
  285. }
  286. /// Get the size (in bits) of the variable, or fragment of the variable that
  287. /// is described.
  288. std::optional<uint64_t> getFragmentSizeInBits() const;
  289. /// Get the FragmentInfo for the variable.
  290. std::optional<DIExpression::FragmentInfo> getFragment() const {
  291. return getExpression()->getFragmentInfo();
  292. }
  293. /// \name Casting methods
  294. /// @{
  295. static bool classof(const IntrinsicInst *I) {
  296. switch (I->getIntrinsicID()) {
  297. case Intrinsic::dbg_declare:
  298. case Intrinsic::dbg_value:
  299. case Intrinsic::dbg_addr:
  300. case Intrinsic::dbg_assign:
  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. /// @}
  310. protected:
  311. void setArgOperand(unsigned i, Value *v) {
  312. DbgInfoIntrinsic::setArgOperand(i, v);
  313. }
  314. void setOperand(unsigned i, Value *v) { DbgInfoIntrinsic::setOperand(i, v); }
  315. };
  316. /// This represents the llvm.dbg.declare instruction.
  317. class DbgDeclareInst : public DbgVariableIntrinsic {
  318. public:
  319. Value *getAddress() const {
  320. assert(getNumVariableLocationOps() == 1 &&
  321. "dbg.declare must have exactly 1 location operand.");
  322. return getVariableLocationOp(0);
  323. }
  324. /// \name Casting methods
  325. /// @{
  326. static bool classof(const IntrinsicInst *I) {
  327. return I->getIntrinsicID() == Intrinsic::dbg_declare;
  328. }
  329. static bool classof(const Value *V) {
  330. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  331. }
  332. /// @}
  333. };
  334. /// This represents the llvm.dbg.addr instruction.
  335. class DbgAddrIntrinsic : public DbgVariableIntrinsic {
  336. public:
  337. Value *getAddress() const {
  338. assert(getNumVariableLocationOps() == 1 &&
  339. "dbg.addr must have exactly 1 location operand.");
  340. return getVariableLocationOp(0);
  341. }
  342. /// \name Casting methods
  343. /// @{
  344. static bool classof(const IntrinsicInst *I) {
  345. return I->getIntrinsicID() == Intrinsic::dbg_addr;
  346. }
  347. static bool classof(const Value *V) {
  348. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  349. }
  350. };
  351. /// This represents the llvm.dbg.value instruction.
  352. class DbgValueInst : public DbgVariableIntrinsic {
  353. public:
  354. // The default argument should only be used in ISel, and the default option
  355. // should be removed once ISel support for multiple location ops is complete.
  356. Value *getValue(unsigned OpIdx = 0) const {
  357. return getVariableLocationOp(OpIdx);
  358. }
  359. iterator_range<location_op_iterator> getValues() const {
  360. return location_ops();
  361. }
  362. /// \name Casting methods
  363. /// @{
  364. static bool classof(const IntrinsicInst *I) {
  365. return I->getIntrinsicID() == Intrinsic::dbg_value ||
  366. I->getIntrinsicID() == Intrinsic::dbg_assign;
  367. }
  368. static bool classof(const Value *V) {
  369. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  370. }
  371. /// @}
  372. };
  373. /// This represents the llvm.dbg.assign instruction.
  374. class DbgAssignIntrinsic : public DbgValueInst {
  375. enum Operands {
  376. OpValue,
  377. OpVar,
  378. OpExpr,
  379. OpAssignID,
  380. OpAddress,
  381. OpAddressExpr,
  382. };
  383. public:
  384. Value *getAddress() const;
  385. Metadata *getRawAddress() const {
  386. return cast<MetadataAsValue>(getArgOperand(OpAddress))->getMetadata();
  387. }
  388. Metadata *getRawAssignID() const {
  389. return cast<MetadataAsValue>(getArgOperand(OpAssignID))->getMetadata();
  390. }
  391. DIAssignID *getAssignID() const { return cast<DIAssignID>(getRawAssignID()); }
  392. Metadata *getRawAddressExpression() const {
  393. return cast<MetadataAsValue>(getArgOperand(OpAddressExpr))->getMetadata();
  394. }
  395. DIExpression *getAddressExpression() const {
  396. return cast<DIExpression>(getRawAddressExpression());
  397. }
  398. void setAddressExpression(DIExpression *NewExpr) {
  399. setArgOperand(OpAddressExpr,
  400. MetadataAsValue::get(NewExpr->getContext(), NewExpr));
  401. }
  402. void setAssignId(DIAssignID *New);
  403. void setAddress(Value *V);
  404. /// Kill the address component.
  405. void setKillAddress();
  406. /// Check whether this kills the address component. This doesn't take into
  407. /// account the position of the intrinsic, therefore a returned value of false
  408. /// does not guarentee the address is a valid location for the variable at the
  409. /// intrinsic's position in IR.
  410. bool isKillAddress() const;
  411. void setValue(Value *V);
  412. /// \name Casting methods
  413. /// @{
  414. static bool classof(const IntrinsicInst *I) {
  415. return I->getIntrinsicID() == Intrinsic::dbg_assign;
  416. }
  417. static bool classof(const Value *V) {
  418. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  419. }
  420. /// @}
  421. };
  422. /// This represents the llvm.dbg.label instruction.
  423. class DbgLabelInst : public DbgInfoIntrinsic {
  424. public:
  425. DILabel *getLabel() const { return cast<DILabel>(getRawLabel()); }
  426. Metadata *getRawLabel() const {
  427. return cast<MetadataAsValue>(getArgOperand(0))->getMetadata();
  428. }
  429. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  430. /// @{
  431. static bool classof(const IntrinsicInst *I) {
  432. return I->getIntrinsicID() == Intrinsic::dbg_label;
  433. }
  434. static bool classof(const Value *V) {
  435. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  436. }
  437. /// @}
  438. };
  439. /// This is the common base class for vector predication intrinsics.
  440. class VPIntrinsic : public IntrinsicInst {
  441. public:
  442. /// \brief Declares a llvm.vp.* intrinsic in \p M that matches the parameters
  443. /// \p Params. Additionally, the load and gather intrinsics require
  444. /// \p ReturnType to be specified.
  445. static Function *getDeclarationForParams(Module *M, Intrinsic::ID,
  446. Type *ReturnType,
  447. ArrayRef<Value *> Params);
  448. static std::optional<unsigned> getMaskParamPos(Intrinsic::ID IntrinsicID);
  449. static std::optional<unsigned> getVectorLengthParamPos(
  450. Intrinsic::ID IntrinsicID);
  451. /// The llvm.vp.* intrinsics for this instruction Opcode
  452. static Intrinsic::ID getForOpcode(unsigned OC);
  453. // Whether \p ID is a VP intrinsic ID.
  454. static bool isVPIntrinsic(Intrinsic::ID);
  455. /// \return The mask parameter or nullptr.
  456. Value *getMaskParam() const;
  457. void setMaskParam(Value *);
  458. /// \return The vector length parameter or nullptr.
  459. Value *getVectorLengthParam() const;
  460. void setVectorLengthParam(Value *);
  461. /// \return Whether the vector length param can be ignored.
  462. bool canIgnoreVectorLengthParam() const;
  463. /// \return The static element count (vector number of elements) the vector
  464. /// length parameter applies to.
  465. ElementCount getStaticVectorLength() const;
  466. /// \return The alignment of the pointer used by this load/store/gather or
  467. /// scatter.
  468. MaybeAlign getPointerAlignment() const;
  469. // MaybeAlign setPointerAlignment(Align NewAlign); // TODO
  470. /// \return The pointer operand of this load,store, gather or scatter.
  471. Value *getMemoryPointerParam() const;
  472. static std::optional<unsigned> getMemoryPointerParamPos(Intrinsic::ID);
  473. /// \return The data (payload) operand of this store or scatter.
  474. Value *getMemoryDataParam() const;
  475. static std::optional<unsigned> getMemoryDataParamPos(Intrinsic::ID);
  476. // Methods for support type inquiry through isa, cast, and dyn_cast:
  477. static bool classof(const IntrinsicInst *I) {
  478. return isVPIntrinsic(I->getIntrinsicID());
  479. }
  480. static bool classof(const Value *V) {
  481. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  482. }
  483. // Equivalent non-predicated opcode
  484. std::optional<unsigned> getFunctionalOpcode() const {
  485. return getFunctionalOpcodeForVP(getIntrinsicID());
  486. }
  487. // Equivalent non-predicated opcode
  488. static std::optional<unsigned> getFunctionalOpcodeForVP(Intrinsic::ID ID);
  489. };
  490. /// This represents vector predication reduction intrinsics.
  491. class VPReductionIntrinsic : public VPIntrinsic {
  492. public:
  493. static bool isVPReduction(Intrinsic::ID ID);
  494. unsigned getStartParamPos() const;
  495. unsigned getVectorParamPos() const;
  496. static std::optional<unsigned> getStartParamPos(Intrinsic::ID ID);
  497. static std::optional<unsigned> getVectorParamPos(Intrinsic::ID ID);
  498. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  499. /// @{
  500. static bool classof(const IntrinsicInst *I) {
  501. return VPReductionIntrinsic::isVPReduction(I->getIntrinsicID());
  502. }
  503. static bool classof(const Value *V) {
  504. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  505. }
  506. /// @}
  507. };
  508. class VPCastIntrinsic : public VPIntrinsic {
  509. public:
  510. static bool isVPCast(Intrinsic::ID ID);
  511. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  512. /// @{
  513. static bool classof(const IntrinsicInst *I) {
  514. return VPCastIntrinsic::isVPCast(I->getIntrinsicID());
  515. }
  516. static bool classof(const Value *V) {
  517. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  518. }
  519. /// @}
  520. };
  521. class VPCmpIntrinsic : public VPIntrinsic {
  522. public:
  523. static bool isVPCmp(Intrinsic::ID ID);
  524. CmpInst::Predicate getPredicate() const;
  525. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  526. /// @{
  527. static bool classof(const IntrinsicInst *I) {
  528. return VPCmpIntrinsic::isVPCmp(I->getIntrinsicID());
  529. }
  530. static bool classof(const Value *V) {
  531. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  532. }
  533. /// @}
  534. };
  535. /// This is the common base class for constrained floating point intrinsics.
  536. class ConstrainedFPIntrinsic : public IntrinsicInst {
  537. public:
  538. bool isUnaryOp() const;
  539. bool isTernaryOp() const;
  540. std::optional<RoundingMode> getRoundingMode() const;
  541. std::optional<fp::ExceptionBehavior> getExceptionBehavior() const;
  542. bool isDefaultFPEnvironment() const;
  543. // Methods for support type inquiry through isa, cast, and dyn_cast:
  544. static bool classof(const IntrinsicInst *I);
  545. static bool classof(const Value *V) {
  546. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  547. }
  548. };
  549. /// Constrained floating point compare intrinsics.
  550. class ConstrainedFPCmpIntrinsic : public ConstrainedFPIntrinsic {
  551. public:
  552. FCmpInst::Predicate getPredicate() const;
  553. bool isSignaling() const {
  554. return getIntrinsicID() == Intrinsic::experimental_constrained_fcmps;
  555. }
  556. // Methods for support type inquiry through isa, cast, and dyn_cast:
  557. static bool classof(const IntrinsicInst *I) {
  558. switch (I->getIntrinsicID()) {
  559. case Intrinsic::experimental_constrained_fcmp:
  560. case Intrinsic::experimental_constrained_fcmps:
  561. return true;
  562. default:
  563. return false;
  564. }
  565. }
  566. static bool classof(const Value *V) {
  567. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  568. }
  569. };
  570. /// This class represents min/max intrinsics.
  571. class MinMaxIntrinsic : public IntrinsicInst {
  572. public:
  573. static bool classof(const IntrinsicInst *I) {
  574. switch (I->getIntrinsicID()) {
  575. case Intrinsic::umin:
  576. case Intrinsic::umax:
  577. case Intrinsic::smin:
  578. case Intrinsic::smax:
  579. return true;
  580. default:
  581. return false;
  582. }
  583. }
  584. static bool classof(const Value *V) {
  585. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  586. }
  587. Value *getLHS() const { return const_cast<Value *>(getArgOperand(0)); }
  588. Value *getRHS() const { return const_cast<Value *>(getArgOperand(1)); }
  589. /// Returns the comparison predicate underlying the intrinsic.
  590. static ICmpInst::Predicate getPredicate(Intrinsic::ID ID) {
  591. switch (ID) {
  592. case Intrinsic::umin:
  593. return ICmpInst::Predicate::ICMP_ULT;
  594. case Intrinsic::umax:
  595. return ICmpInst::Predicate::ICMP_UGT;
  596. case Intrinsic::smin:
  597. return ICmpInst::Predicate::ICMP_SLT;
  598. case Intrinsic::smax:
  599. return ICmpInst::Predicate::ICMP_SGT;
  600. default:
  601. llvm_unreachable("Invalid intrinsic");
  602. }
  603. }
  604. /// Returns the comparison predicate underlying the intrinsic.
  605. ICmpInst::Predicate getPredicate() const {
  606. return getPredicate(getIntrinsicID());
  607. }
  608. /// Whether the intrinsic is signed or unsigned.
  609. static bool isSigned(Intrinsic::ID ID) {
  610. return ICmpInst::isSigned(getPredicate(ID));
  611. };
  612. /// Whether the intrinsic is signed or unsigned.
  613. bool isSigned() const { return isSigned(getIntrinsicID()); };
  614. /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values,
  615. /// so there is a certain threshold value, upon reaching which,
  616. /// their value can no longer change. Return said threshold.
  617. static APInt getSaturationPoint(Intrinsic::ID ID, unsigned numBits) {
  618. switch (ID) {
  619. case Intrinsic::umin:
  620. return APInt::getMinValue(numBits);
  621. case Intrinsic::umax:
  622. return APInt::getMaxValue(numBits);
  623. case Intrinsic::smin:
  624. return APInt::getSignedMinValue(numBits);
  625. case Intrinsic::smax:
  626. return APInt::getSignedMaxValue(numBits);
  627. default:
  628. llvm_unreachable("Invalid intrinsic");
  629. }
  630. }
  631. /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values,
  632. /// so there is a certain threshold value, upon reaching which,
  633. /// their value can no longer change. Return said threshold.
  634. APInt getSaturationPoint(unsigned numBits) const {
  635. return getSaturationPoint(getIntrinsicID(), numBits);
  636. }
  637. /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values,
  638. /// so there is a certain threshold value, upon reaching which,
  639. /// their value can no longer change. Return said threshold.
  640. static Constant *getSaturationPoint(Intrinsic::ID ID, Type *Ty) {
  641. return Constant::getIntegerValue(
  642. Ty, getSaturationPoint(ID, Ty->getScalarSizeInBits()));
  643. }
  644. /// Min/max intrinsics are monotonic, they operate on a fixed-bitwidth values,
  645. /// so there is a certain threshold value, upon reaching which,
  646. /// their value can no longer change. Return said threshold.
  647. Constant *getSaturationPoint(Type *Ty) const {
  648. return getSaturationPoint(getIntrinsicID(), Ty);
  649. }
  650. };
  651. /// This class represents an intrinsic that is based on a binary operation.
  652. /// This includes op.with.overflow and saturating add/sub intrinsics.
  653. class BinaryOpIntrinsic : public IntrinsicInst {
  654. public:
  655. static bool classof(const IntrinsicInst *I) {
  656. switch (I->getIntrinsicID()) {
  657. case Intrinsic::uadd_with_overflow:
  658. case Intrinsic::sadd_with_overflow:
  659. case Intrinsic::usub_with_overflow:
  660. case Intrinsic::ssub_with_overflow:
  661. case Intrinsic::umul_with_overflow:
  662. case Intrinsic::smul_with_overflow:
  663. case Intrinsic::uadd_sat:
  664. case Intrinsic::sadd_sat:
  665. case Intrinsic::usub_sat:
  666. case Intrinsic::ssub_sat:
  667. return true;
  668. default:
  669. return false;
  670. }
  671. }
  672. static bool classof(const Value *V) {
  673. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  674. }
  675. Value *getLHS() const { return const_cast<Value *>(getArgOperand(0)); }
  676. Value *getRHS() const { return const_cast<Value *>(getArgOperand(1)); }
  677. /// Returns the binary operation underlying the intrinsic.
  678. Instruction::BinaryOps getBinaryOp() const;
  679. /// Whether the intrinsic is signed or unsigned.
  680. bool isSigned() const;
  681. /// Returns one of OBO::NoSignedWrap or OBO::NoUnsignedWrap.
  682. unsigned getNoWrapKind() const;
  683. };
  684. /// Represents an op.with.overflow intrinsic.
  685. class WithOverflowInst : public BinaryOpIntrinsic {
  686. public:
  687. static bool classof(const IntrinsicInst *I) {
  688. switch (I->getIntrinsicID()) {
  689. case Intrinsic::uadd_with_overflow:
  690. case Intrinsic::sadd_with_overflow:
  691. case Intrinsic::usub_with_overflow:
  692. case Intrinsic::ssub_with_overflow:
  693. case Intrinsic::umul_with_overflow:
  694. case Intrinsic::smul_with_overflow:
  695. return true;
  696. default:
  697. return false;
  698. }
  699. }
  700. static bool classof(const Value *V) {
  701. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  702. }
  703. };
  704. /// Represents a saturating add/sub intrinsic.
  705. class SaturatingInst : public BinaryOpIntrinsic {
  706. public:
  707. static bool classof(const IntrinsicInst *I) {
  708. switch (I->getIntrinsicID()) {
  709. case Intrinsic::uadd_sat:
  710. case Intrinsic::sadd_sat:
  711. case Intrinsic::usub_sat:
  712. case Intrinsic::ssub_sat:
  713. return true;
  714. default:
  715. return false;
  716. }
  717. }
  718. static bool classof(const Value *V) {
  719. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  720. }
  721. };
  722. /// Common base class for all memory intrinsics. Simply provides
  723. /// common methods.
  724. /// Written as CRTP to avoid a common base class amongst the
  725. /// three atomicity hierarchies.
  726. template <typename Derived> class MemIntrinsicBase : public IntrinsicInst {
  727. private:
  728. enum { ARG_DEST = 0, ARG_LENGTH = 2 };
  729. public:
  730. Value *getRawDest() const {
  731. return const_cast<Value *>(getArgOperand(ARG_DEST));
  732. }
  733. const Use &getRawDestUse() const { return getArgOperandUse(ARG_DEST); }
  734. Use &getRawDestUse() { return getArgOperandUse(ARG_DEST); }
  735. Value *getLength() const {
  736. return const_cast<Value *>(getArgOperand(ARG_LENGTH));
  737. }
  738. const Use &getLengthUse() const { return getArgOperandUse(ARG_LENGTH); }
  739. Use &getLengthUse() { return getArgOperandUse(ARG_LENGTH); }
  740. /// This is just like getRawDest, but it strips off any cast
  741. /// instructions (including addrspacecast) that feed it, giving the
  742. /// original input. The returned value is guaranteed to be a pointer.
  743. Value *getDest() const { return getRawDest()->stripPointerCasts(); }
  744. unsigned getDestAddressSpace() const {
  745. return cast<PointerType>(getRawDest()->getType())->getAddressSpace();
  746. }
  747. /// FIXME: Remove this function once transition to Align is over.
  748. /// Use getDestAlign() instead.
  749. LLVM_DEPRECATED("Use getDestAlign() instead", "getDestAlign")
  750. unsigned getDestAlignment() const {
  751. if (auto MA = getParamAlign(ARG_DEST))
  752. return MA->value();
  753. return 0;
  754. }
  755. MaybeAlign getDestAlign() const { return getParamAlign(ARG_DEST); }
  756. /// Set the specified arguments of the instruction.
  757. void setDest(Value *Ptr) {
  758. assert(getRawDest()->getType() == Ptr->getType() &&
  759. "setDest called with pointer of wrong type!");
  760. setArgOperand(ARG_DEST, Ptr);
  761. }
  762. void setDestAlignment(MaybeAlign Alignment) {
  763. removeParamAttr(ARG_DEST, Attribute::Alignment);
  764. if (Alignment)
  765. addParamAttr(ARG_DEST,
  766. Attribute::getWithAlignment(getContext(), *Alignment));
  767. }
  768. void setDestAlignment(Align Alignment) {
  769. removeParamAttr(ARG_DEST, Attribute::Alignment);
  770. addParamAttr(ARG_DEST,
  771. Attribute::getWithAlignment(getContext(), Alignment));
  772. }
  773. void setLength(Value *L) {
  774. assert(getLength()->getType() == L->getType() &&
  775. "setLength called with value of wrong type!");
  776. setArgOperand(ARG_LENGTH, L);
  777. }
  778. };
  779. /// Common base class for all memory transfer intrinsics. Simply provides
  780. /// common methods.
  781. template <class BaseCL> class MemTransferBase : public BaseCL {
  782. private:
  783. enum { ARG_SOURCE = 1 };
  784. public:
  785. /// Return the arguments to the instruction.
  786. Value *getRawSource() const {
  787. return const_cast<Value *>(BaseCL::getArgOperand(ARG_SOURCE));
  788. }
  789. const Use &getRawSourceUse() const {
  790. return BaseCL::getArgOperandUse(ARG_SOURCE);
  791. }
  792. Use &getRawSourceUse() { return BaseCL::getArgOperandUse(ARG_SOURCE); }
  793. /// This is just like getRawSource, but it strips off any cast
  794. /// instructions that feed it, giving the original input. The returned
  795. /// value is guaranteed to be a pointer.
  796. Value *getSource() const { return getRawSource()->stripPointerCasts(); }
  797. unsigned getSourceAddressSpace() const {
  798. return cast<PointerType>(getRawSource()->getType())->getAddressSpace();
  799. }
  800. /// FIXME: Remove this function once transition to Align is over.
  801. /// Use getSourceAlign() instead.
  802. LLVM_DEPRECATED("Use getSourceAlign() instead", "getSourceAlign")
  803. unsigned getSourceAlignment() const {
  804. if (auto MA = BaseCL::getParamAlign(ARG_SOURCE))
  805. return MA->value();
  806. return 0;
  807. }
  808. MaybeAlign getSourceAlign() const {
  809. return BaseCL::getParamAlign(ARG_SOURCE);
  810. }
  811. void setSource(Value *Ptr) {
  812. assert(getRawSource()->getType() == Ptr->getType() &&
  813. "setSource called with pointer of wrong type!");
  814. BaseCL::setArgOperand(ARG_SOURCE, Ptr);
  815. }
  816. void setSourceAlignment(MaybeAlign Alignment) {
  817. BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment);
  818. if (Alignment)
  819. BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment(
  820. BaseCL::getContext(), *Alignment));
  821. }
  822. void setSourceAlignment(Align Alignment) {
  823. BaseCL::removeParamAttr(ARG_SOURCE, Attribute::Alignment);
  824. BaseCL::addParamAttr(ARG_SOURCE, Attribute::getWithAlignment(
  825. BaseCL::getContext(), Alignment));
  826. }
  827. };
  828. /// Common base class for all memset intrinsics. Simply provides
  829. /// common methods.
  830. template <class BaseCL> class MemSetBase : public BaseCL {
  831. private:
  832. enum { ARG_VALUE = 1 };
  833. public:
  834. Value *getValue() const {
  835. return const_cast<Value *>(BaseCL::getArgOperand(ARG_VALUE));
  836. }
  837. const Use &getValueUse() const { return BaseCL::getArgOperandUse(ARG_VALUE); }
  838. Use &getValueUse() { return BaseCL::getArgOperandUse(ARG_VALUE); }
  839. void setValue(Value *Val) {
  840. assert(getValue()->getType() == Val->getType() &&
  841. "setValue called with value of wrong type!");
  842. BaseCL::setArgOperand(ARG_VALUE, Val);
  843. }
  844. };
  845. // The common base class for the atomic memset/memmove/memcpy intrinsics
  846. // i.e. llvm.element.unordered.atomic.memset/memcpy/memmove
  847. class AtomicMemIntrinsic : public MemIntrinsicBase<AtomicMemIntrinsic> {
  848. private:
  849. enum { ARG_ELEMENTSIZE = 3 };
  850. public:
  851. Value *getRawElementSizeInBytes() const {
  852. return const_cast<Value *>(getArgOperand(ARG_ELEMENTSIZE));
  853. }
  854. ConstantInt *getElementSizeInBytesCst() const {
  855. return cast<ConstantInt>(getRawElementSizeInBytes());
  856. }
  857. uint32_t getElementSizeInBytes() const {
  858. return getElementSizeInBytesCst()->getZExtValue();
  859. }
  860. void setElementSizeInBytes(Constant *V) {
  861. assert(V->getType() == Type::getInt8Ty(getContext()) &&
  862. "setElementSizeInBytes called with value of wrong type!");
  863. setArgOperand(ARG_ELEMENTSIZE, V);
  864. }
  865. static bool classof(const IntrinsicInst *I) {
  866. switch (I->getIntrinsicID()) {
  867. case Intrinsic::memcpy_element_unordered_atomic:
  868. case Intrinsic::memmove_element_unordered_atomic:
  869. case Intrinsic::memset_element_unordered_atomic:
  870. return true;
  871. default:
  872. return false;
  873. }
  874. }
  875. static bool classof(const Value *V) {
  876. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  877. }
  878. };
  879. /// This class represents atomic memset intrinsic
  880. // i.e. llvm.element.unordered.atomic.memset
  881. class AtomicMemSetInst : public MemSetBase<AtomicMemIntrinsic> {
  882. public:
  883. static bool classof(const IntrinsicInst *I) {
  884. return I->getIntrinsicID() == Intrinsic::memset_element_unordered_atomic;
  885. }
  886. static bool classof(const Value *V) {
  887. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  888. }
  889. };
  890. // This class wraps the atomic memcpy/memmove intrinsics
  891. // i.e. llvm.element.unordered.atomic.memcpy/memmove
  892. class AtomicMemTransferInst : public MemTransferBase<AtomicMemIntrinsic> {
  893. public:
  894. static bool classof(const IntrinsicInst *I) {
  895. switch (I->getIntrinsicID()) {
  896. case Intrinsic::memcpy_element_unordered_atomic:
  897. case Intrinsic::memmove_element_unordered_atomic:
  898. return true;
  899. default:
  900. return false;
  901. }
  902. }
  903. static bool classof(const Value *V) {
  904. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  905. }
  906. };
  907. /// This class represents the atomic memcpy intrinsic
  908. /// i.e. llvm.element.unordered.atomic.memcpy
  909. class AtomicMemCpyInst : public AtomicMemTransferInst {
  910. public:
  911. static bool classof(const IntrinsicInst *I) {
  912. return I->getIntrinsicID() == Intrinsic::memcpy_element_unordered_atomic;
  913. }
  914. static bool classof(const Value *V) {
  915. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  916. }
  917. };
  918. /// This class represents the atomic memmove intrinsic
  919. /// i.e. llvm.element.unordered.atomic.memmove
  920. class AtomicMemMoveInst : public AtomicMemTransferInst {
  921. public:
  922. static bool classof(const IntrinsicInst *I) {
  923. return I->getIntrinsicID() == Intrinsic::memmove_element_unordered_atomic;
  924. }
  925. static bool classof(const Value *V) {
  926. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  927. }
  928. };
  929. /// This is the common base class for memset/memcpy/memmove.
  930. class MemIntrinsic : public MemIntrinsicBase<MemIntrinsic> {
  931. private:
  932. enum { ARG_VOLATILE = 3 };
  933. public:
  934. ConstantInt *getVolatileCst() const {
  935. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(ARG_VOLATILE)));
  936. }
  937. bool isVolatile() const { return !getVolatileCst()->isZero(); }
  938. void setVolatile(Constant *V) { setArgOperand(ARG_VOLATILE, V); }
  939. // Methods for support type inquiry through isa, cast, and dyn_cast:
  940. static bool classof(const IntrinsicInst *I) {
  941. switch (I->getIntrinsicID()) {
  942. case Intrinsic::memcpy:
  943. case Intrinsic::memmove:
  944. case Intrinsic::memset:
  945. case Intrinsic::memset_inline:
  946. case Intrinsic::memcpy_inline:
  947. return true;
  948. default:
  949. return false;
  950. }
  951. }
  952. static bool classof(const Value *V) {
  953. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  954. }
  955. };
  956. /// This class wraps the llvm.memset and llvm.memset.inline intrinsics.
  957. class MemSetInst : public MemSetBase<MemIntrinsic> {
  958. public:
  959. // Methods for support type inquiry through isa, cast, and dyn_cast:
  960. static bool classof(const IntrinsicInst *I) {
  961. switch (I->getIntrinsicID()) {
  962. case Intrinsic::memset:
  963. case Intrinsic::memset_inline:
  964. return true;
  965. default:
  966. return false;
  967. }
  968. }
  969. static bool classof(const Value *V) {
  970. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  971. }
  972. };
  973. /// This class wraps the llvm.memset.inline intrinsic.
  974. class MemSetInlineInst : public MemSetInst {
  975. public:
  976. ConstantInt *getLength() const {
  977. return cast<ConstantInt>(MemSetInst::getLength());
  978. }
  979. // Methods for support type inquiry through isa, cast, and dyn_cast:
  980. static bool classof(const IntrinsicInst *I) {
  981. return I->getIntrinsicID() == Intrinsic::memset_inline;
  982. }
  983. static bool classof(const Value *V) {
  984. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  985. }
  986. };
  987. /// This class wraps the llvm.memcpy/memmove intrinsics.
  988. class MemTransferInst : public MemTransferBase<MemIntrinsic> {
  989. public:
  990. // Methods for support type inquiry through isa, cast, and dyn_cast:
  991. static bool classof(const IntrinsicInst *I) {
  992. switch (I->getIntrinsicID()) {
  993. case Intrinsic::memcpy:
  994. case Intrinsic::memmove:
  995. case Intrinsic::memcpy_inline:
  996. return true;
  997. default:
  998. return false;
  999. }
  1000. }
  1001. static bool classof(const Value *V) {
  1002. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1003. }
  1004. };
  1005. /// This class wraps the llvm.memcpy intrinsic.
  1006. class MemCpyInst : public MemTransferInst {
  1007. public:
  1008. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1009. static bool classof(const IntrinsicInst *I) {
  1010. return I->getIntrinsicID() == Intrinsic::memcpy ||
  1011. I->getIntrinsicID() == Intrinsic::memcpy_inline;
  1012. }
  1013. static bool classof(const Value *V) {
  1014. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1015. }
  1016. };
  1017. /// This class wraps the llvm.memmove intrinsic.
  1018. class MemMoveInst : public MemTransferInst {
  1019. public:
  1020. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1021. static bool classof(const IntrinsicInst *I) {
  1022. return I->getIntrinsicID() == Intrinsic::memmove;
  1023. }
  1024. static bool classof(const Value *V) {
  1025. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1026. }
  1027. };
  1028. /// This class wraps the llvm.memcpy.inline intrinsic.
  1029. class MemCpyInlineInst : public MemCpyInst {
  1030. public:
  1031. ConstantInt *getLength() const {
  1032. return cast<ConstantInt>(MemCpyInst::getLength());
  1033. }
  1034. // Methods for support type inquiry through isa, cast, and dyn_cast:
  1035. static bool classof(const IntrinsicInst *I) {
  1036. return I->getIntrinsicID() == Intrinsic::memcpy_inline;
  1037. }
  1038. static bool classof(const Value *V) {
  1039. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1040. }
  1041. };
  1042. // The common base class for any memset/memmove/memcpy intrinsics;
  1043. // whether they be atomic or non-atomic.
  1044. // i.e. llvm.element.unordered.atomic.memset/memcpy/memmove
  1045. // and llvm.memset/memcpy/memmove
  1046. class AnyMemIntrinsic : public MemIntrinsicBase<AnyMemIntrinsic> {
  1047. public:
  1048. bool isVolatile() const {
  1049. // Only the non-atomic intrinsics can be volatile
  1050. if (auto *MI = dyn_cast<MemIntrinsic>(this))
  1051. return MI->isVolatile();
  1052. return false;
  1053. }
  1054. static bool classof(const IntrinsicInst *I) {
  1055. switch (I->getIntrinsicID()) {
  1056. case Intrinsic::memcpy:
  1057. case Intrinsic::memcpy_inline:
  1058. case Intrinsic::memmove:
  1059. case Intrinsic::memset:
  1060. case Intrinsic::memset_inline:
  1061. case Intrinsic::memcpy_element_unordered_atomic:
  1062. case Intrinsic::memmove_element_unordered_atomic:
  1063. case Intrinsic::memset_element_unordered_atomic:
  1064. return true;
  1065. default:
  1066. return false;
  1067. }
  1068. }
  1069. static bool classof(const Value *V) {
  1070. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1071. }
  1072. };
  1073. /// This class represents any memset intrinsic
  1074. // i.e. llvm.element.unordered.atomic.memset
  1075. // and llvm.memset
  1076. class AnyMemSetInst : public MemSetBase<AnyMemIntrinsic> {
  1077. public:
  1078. static bool classof(const IntrinsicInst *I) {
  1079. switch (I->getIntrinsicID()) {
  1080. case Intrinsic::memset:
  1081. case Intrinsic::memset_inline:
  1082. case Intrinsic::memset_element_unordered_atomic:
  1083. return true;
  1084. default:
  1085. return false;
  1086. }
  1087. }
  1088. static bool classof(const Value *V) {
  1089. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1090. }
  1091. };
  1092. // This class wraps any memcpy/memmove intrinsics
  1093. // i.e. llvm.element.unordered.atomic.memcpy/memmove
  1094. // and llvm.memcpy/memmove
  1095. class AnyMemTransferInst : public MemTransferBase<AnyMemIntrinsic> {
  1096. public:
  1097. static bool classof(const IntrinsicInst *I) {
  1098. switch (I->getIntrinsicID()) {
  1099. case Intrinsic::memcpy:
  1100. case Intrinsic::memcpy_inline:
  1101. case Intrinsic::memmove:
  1102. case Intrinsic::memcpy_element_unordered_atomic:
  1103. case Intrinsic::memmove_element_unordered_atomic:
  1104. return true;
  1105. default:
  1106. return false;
  1107. }
  1108. }
  1109. static bool classof(const Value *V) {
  1110. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1111. }
  1112. };
  1113. /// This class represents any memcpy intrinsic
  1114. /// i.e. llvm.element.unordered.atomic.memcpy
  1115. /// and llvm.memcpy
  1116. class AnyMemCpyInst : public AnyMemTransferInst {
  1117. public:
  1118. static bool classof(const IntrinsicInst *I) {
  1119. switch (I->getIntrinsicID()) {
  1120. case Intrinsic::memcpy:
  1121. case Intrinsic::memcpy_inline:
  1122. case Intrinsic::memcpy_element_unordered_atomic:
  1123. return true;
  1124. default:
  1125. return false;
  1126. }
  1127. }
  1128. static bool classof(const Value *V) {
  1129. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1130. }
  1131. };
  1132. /// This class represents any memmove intrinsic
  1133. /// i.e. llvm.element.unordered.atomic.memmove
  1134. /// and llvm.memmove
  1135. class AnyMemMoveInst : public AnyMemTransferInst {
  1136. public:
  1137. static bool classof(const IntrinsicInst *I) {
  1138. switch (I->getIntrinsicID()) {
  1139. case Intrinsic::memmove:
  1140. case Intrinsic::memmove_element_unordered_atomic:
  1141. return true;
  1142. default:
  1143. return false;
  1144. }
  1145. }
  1146. static bool classof(const Value *V) {
  1147. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1148. }
  1149. };
  1150. /// This represents the llvm.va_start intrinsic.
  1151. class VAStartInst : public IntrinsicInst {
  1152. public:
  1153. static bool classof(const IntrinsicInst *I) {
  1154. return I->getIntrinsicID() == Intrinsic::vastart;
  1155. }
  1156. static bool classof(const Value *V) {
  1157. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1158. }
  1159. Value *getArgList() const { return const_cast<Value *>(getArgOperand(0)); }
  1160. };
  1161. /// This represents the llvm.va_end intrinsic.
  1162. class VAEndInst : public IntrinsicInst {
  1163. public:
  1164. static bool classof(const IntrinsicInst *I) {
  1165. return I->getIntrinsicID() == Intrinsic::vaend;
  1166. }
  1167. static bool classof(const Value *V) {
  1168. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1169. }
  1170. Value *getArgList() const { return const_cast<Value *>(getArgOperand(0)); }
  1171. };
  1172. /// This represents the llvm.va_copy intrinsic.
  1173. class VACopyInst : public IntrinsicInst {
  1174. public:
  1175. static bool classof(const IntrinsicInst *I) {
  1176. return I->getIntrinsicID() == Intrinsic::vacopy;
  1177. }
  1178. static bool classof(const Value *V) {
  1179. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1180. }
  1181. Value *getDest() const { return const_cast<Value *>(getArgOperand(0)); }
  1182. Value *getSrc() const { return const_cast<Value *>(getArgOperand(1)); }
  1183. };
  1184. /// A base class for all instrprof intrinsics.
  1185. class InstrProfInstBase : public IntrinsicInst {
  1186. public:
  1187. // The name of the instrumented function.
  1188. GlobalVariable *getName() const {
  1189. return cast<GlobalVariable>(
  1190. const_cast<Value *>(getArgOperand(0))->stripPointerCasts());
  1191. }
  1192. // The hash of the CFG for the instrumented function.
  1193. ConstantInt *getHash() const {
  1194. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
  1195. }
  1196. // The number of counters for the instrumented function.
  1197. ConstantInt *getNumCounters() const;
  1198. // The index of the counter that this instruction acts on.
  1199. ConstantInt *getIndex() const;
  1200. };
  1201. /// This represents the llvm.instrprof.cover intrinsic.
  1202. class InstrProfCoverInst : public InstrProfInstBase {
  1203. public:
  1204. static bool classof(const IntrinsicInst *I) {
  1205. return I->getIntrinsicID() == Intrinsic::instrprof_cover;
  1206. }
  1207. static bool classof(const Value *V) {
  1208. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1209. }
  1210. };
  1211. /// This represents the llvm.instrprof.increment intrinsic.
  1212. class InstrProfIncrementInst : public InstrProfInstBase {
  1213. public:
  1214. static bool classof(const IntrinsicInst *I) {
  1215. return I->getIntrinsicID() == Intrinsic::instrprof_increment ||
  1216. I->getIntrinsicID() == Intrinsic::instrprof_increment_step;
  1217. }
  1218. static bool classof(const Value *V) {
  1219. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1220. }
  1221. Value *getStep() const;
  1222. };
  1223. /// This represents the llvm.instrprof.increment.step intrinsic.
  1224. class InstrProfIncrementInstStep : public InstrProfIncrementInst {
  1225. public:
  1226. static bool classof(const IntrinsicInst *I) {
  1227. return I->getIntrinsicID() == Intrinsic::instrprof_increment_step;
  1228. }
  1229. static bool classof(const Value *V) {
  1230. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1231. }
  1232. };
  1233. /// This represents the llvm.instrprof.value.profile intrinsic.
  1234. class InstrProfValueProfileInst : public InstrProfInstBase {
  1235. public:
  1236. static bool classof(const IntrinsicInst *I) {
  1237. return I->getIntrinsicID() == Intrinsic::instrprof_value_profile;
  1238. }
  1239. static bool classof(const Value *V) {
  1240. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1241. }
  1242. Value *getTargetValue() const {
  1243. return cast<Value>(const_cast<Value *>(getArgOperand(2)));
  1244. }
  1245. ConstantInt *getValueKind() const {
  1246. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
  1247. }
  1248. // Returns the value site index.
  1249. ConstantInt *getIndex() const {
  1250. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(4)));
  1251. }
  1252. };
  1253. class PseudoProbeInst : public IntrinsicInst {
  1254. public:
  1255. static bool classof(const IntrinsicInst *I) {
  1256. return I->getIntrinsicID() == Intrinsic::pseudoprobe;
  1257. }
  1258. static bool classof(const Value *V) {
  1259. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1260. }
  1261. ConstantInt *getFuncGuid() const {
  1262. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(0)));
  1263. }
  1264. ConstantInt *getIndex() const {
  1265. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(1)));
  1266. }
  1267. ConstantInt *getAttributes() const {
  1268. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(2)));
  1269. }
  1270. ConstantInt *getFactor() const {
  1271. return cast<ConstantInt>(const_cast<Value *>(getArgOperand(3)));
  1272. }
  1273. };
  1274. class NoAliasScopeDeclInst : public IntrinsicInst {
  1275. public:
  1276. static bool classof(const IntrinsicInst *I) {
  1277. return I->getIntrinsicID() == Intrinsic::experimental_noalias_scope_decl;
  1278. }
  1279. static bool classof(const Value *V) {
  1280. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1281. }
  1282. MDNode *getScopeList() const {
  1283. auto *MV =
  1284. cast<MetadataAsValue>(getOperand(Intrinsic::NoAliasScopeDeclScopeArg));
  1285. return cast<MDNode>(MV->getMetadata());
  1286. }
  1287. void setScopeList(MDNode *ScopeList) {
  1288. setOperand(Intrinsic::NoAliasScopeDeclScopeArg,
  1289. MetadataAsValue::get(getContext(), ScopeList));
  1290. }
  1291. };
  1292. /// Common base class for representing values projected from a statepoint.
  1293. /// Currently, the only projections available are gc.result and gc.relocate.
  1294. class GCProjectionInst : public IntrinsicInst {
  1295. public:
  1296. static bool classof(const IntrinsicInst *I) {
  1297. return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate ||
  1298. I->getIntrinsicID() == Intrinsic::experimental_gc_result;
  1299. }
  1300. static bool classof(const Value *V) {
  1301. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1302. }
  1303. /// Return true if this relocate is tied to the invoke statepoint.
  1304. /// This includes relocates which are on the unwinding path.
  1305. bool isTiedToInvoke() const {
  1306. const Value *Token = getArgOperand(0);
  1307. return isa<LandingPadInst>(Token) || isa<InvokeInst>(Token);
  1308. }
  1309. /// The statepoint with which this gc.relocate is associated.
  1310. const Value *getStatepoint() const;
  1311. };
  1312. /// Represents calls to the gc.relocate intrinsic.
  1313. class GCRelocateInst : public GCProjectionInst {
  1314. public:
  1315. static bool classof(const IntrinsicInst *I) {
  1316. return I->getIntrinsicID() == Intrinsic::experimental_gc_relocate;
  1317. }
  1318. static bool classof(const Value *V) {
  1319. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1320. }
  1321. /// The index into the associate statepoint's argument list
  1322. /// which contains the base pointer of the pointer whose
  1323. /// relocation this gc.relocate describes.
  1324. unsigned getBasePtrIndex() const {
  1325. return cast<ConstantInt>(getArgOperand(1))->getZExtValue();
  1326. }
  1327. /// The index into the associate statepoint's argument list which
  1328. /// contains the pointer whose relocation this gc.relocate describes.
  1329. unsigned getDerivedPtrIndex() const {
  1330. return cast<ConstantInt>(getArgOperand(2))->getZExtValue();
  1331. }
  1332. Value *getBasePtr() const;
  1333. Value *getDerivedPtr() const;
  1334. };
  1335. /// Represents calls to the gc.result intrinsic.
  1336. class GCResultInst : public GCProjectionInst {
  1337. public:
  1338. static bool classof(const IntrinsicInst *I) {
  1339. return I->getIntrinsicID() == Intrinsic::experimental_gc_result;
  1340. }
  1341. static bool classof(const Value *V) {
  1342. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1343. }
  1344. };
  1345. /// This represents intrinsics that guard a condition
  1346. class CondGuardInst : public IntrinsicInst {
  1347. public:
  1348. static bool classof(const IntrinsicInst *I) {
  1349. return I->getIntrinsicID() == Intrinsic::assume ||
  1350. I->getIntrinsicID() == Intrinsic::experimental_guard;
  1351. }
  1352. static bool classof(const Value *V) {
  1353. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1354. }
  1355. };
  1356. /// This represents the llvm.assume intrinsic.
  1357. class AssumeInst : public CondGuardInst {
  1358. public:
  1359. static bool classof(const IntrinsicInst *I) {
  1360. return I->getIntrinsicID() == Intrinsic::assume;
  1361. }
  1362. static bool classof(const Value *V) {
  1363. return isa<IntrinsicInst>(V) && classof(cast<IntrinsicInst>(V));
  1364. }
  1365. };
  1366. } // end namespace llvm
  1367. #endif // LLVM_IR_INTRINSICINST_H
  1368. #ifdef __GNUC__
  1369. #pragma GCC diagnostic pop
  1370. #endif