IntrinsicInst.h 44 KB

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