MachineIRBuilder.h 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- llvm/CodeGen/GlobalISel/MachineIRBuilder.h - MIBuilder --*- 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. /// \file
  14. /// This file declares the MachineIRBuilder class.
  15. /// This is a helper class to build MachineInstr.
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
  18. #define LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
  19. #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
  20. #include "llvm/CodeGen/MachineBasicBlock.h"
  21. #include "llvm/CodeGen/MachineInstrBuilder.h"
  22. #include "llvm/CodeGen/MachineRegisterInfo.h"
  23. #include "llvm/CodeGen/TargetOpcodes.h"
  24. #include "llvm/IR/DebugLoc.h"
  25. #include "llvm/IR/Module.h"
  26. namespace llvm {
  27. // Forward declarations.
  28. class APInt;
  29. class BlockAddress;
  30. class Constant;
  31. class ConstantFP;
  32. class ConstantInt;
  33. class DataLayout;
  34. class GISelCSEInfo;
  35. class GlobalValue;
  36. class TargetRegisterClass;
  37. class MachineFunction;
  38. class MachineInstr;
  39. class TargetInstrInfo;
  40. class GISelChangeObserver;
  41. /// Class which stores all the state required in a MachineIRBuilder.
  42. /// Since MachineIRBuilders will only store state in this object, it allows
  43. /// to transfer BuilderState between different kinds of MachineIRBuilders.
  44. struct MachineIRBuilderState {
  45. /// MachineFunction under construction.
  46. MachineFunction *MF = nullptr;
  47. /// Information used to access the description of the opcodes.
  48. const TargetInstrInfo *TII = nullptr;
  49. /// Information used to verify types are consistent and to create virtual registers.
  50. MachineRegisterInfo *MRI = nullptr;
  51. /// Debug location to be set to any instruction we create.
  52. DebugLoc DL;
  53. /// PC sections metadata to be set to any instruction we create.
  54. MDNode *PCSections = nullptr;
  55. /// \name Fields describing the insertion point.
  56. /// @{
  57. MachineBasicBlock *MBB = nullptr;
  58. MachineBasicBlock::iterator II;
  59. /// @}
  60. GISelChangeObserver *Observer = nullptr;
  61. GISelCSEInfo *CSEInfo = nullptr;
  62. };
  63. class DstOp {
  64. union {
  65. LLT LLTTy;
  66. Register Reg;
  67. const TargetRegisterClass *RC;
  68. };
  69. public:
  70. enum class DstType { Ty_LLT, Ty_Reg, Ty_RC };
  71. DstOp(unsigned R) : Reg(R), Ty(DstType::Ty_Reg) {}
  72. DstOp(Register R) : Reg(R), Ty(DstType::Ty_Reg) {}
  73. DstOp(const MachineOperand &Op) : Reg(Op.getReg()), Ty(DstType::Ty_Reg) {}
  74. DstOp(const LLT T) : LLTTy(T), Ty(DstType::Ty_LLT) {}
  75. DstOp(const TargetRegisterClass *TRC) : RC(TRC), Ty(DstType::Ty_RC) {}
  76. void addDefToMIB(MachineRegisterInfo &MRI, MachineInstrBuilder &MIB) const {
  77. switch (Ty) {
  78. case DstType::Ty_Reg:
  79. MIB.addDef(Reg);
  80. break;
  81. case DstType::Ty_LLT:
  82. MIB.addDef(MRI.createGenericVirtualRegister(LLTTy));
  83. break;
  84. case DstType::Ty_RC:
  85. MIB.addDef(MRI.createVirtualRegister(RC));
  86. break;
  87. }
  88. }
  89. LLT getLLTTy(const MachineRegisterInfo &MRI) const {
  90. switch (Ty) {
  91. case DstType::Ty_RC:
  92. return LLT{};
  93. case DstType::Ty_LLT:
  94. return LLTTy;
  95. case DstType::Ty_Reg:
  96. return MRI.getType(Reg);
  97. }
  98. llvm_unreachable("Unrecognised DstOp::DstType enum");
  99. }
  100. Register getReg() const {
  101. assert(Ty == DstType::Ty_Reg && "Not a register");
  102. return Reg;
  103. }
  104. const TargetRegisterClass *getRegClass() const {
  105. switch (Ty) {
  106. case DstType::Ty_RC:
  107. return RC;
  108. default:
  109. llvm_unreachable("Not a RC Operand");
  110. }
  111. }
  112. DstType getDstOpKind() const { return Ty; }
  113. private:
  114. DstType Ty;
  115. };
  116. class SrcOp {
  117. union {
  118. MachineInstrBuilder SrcMIB;
  119. Register Reg;
  120. CmpInst::Predicate Pred;
  121. int64_t Imm;
  122. };
  123. public:
  124. enum class SrcType { Ty_Reg, Ty_MIB, Ty_Predicate, Ty_Imm };
  125. SrcOp(Register R) : Reg(R), Ty(SrcType::Ty_Reg) {}
  126. SrcOp(const MachineOperand &Op) : Reg(Op.getReg()), Ty(SrcType::Ty_Reg) {}
  127. SrcOp(const MachineInstrBuilder &MIB) : SrcMIB(MIB), Ty(SrcType::Ty_MIB) {}
  128. SrcOp(const CmpInst::Predicate P) : Pred(P), Ty(SrcType::Ty_Predicate) {}
  129. /// Use of registers held in unsigned integer variables (or more rarely signed
  130. /// integers) is no longer permitted to avoid ambiguity with upcoming support
  131. /// for immediates.
  132. SrcOp(unsigned) = delete;
  133. SrcOp(int) = delete;
  134. SrcOp(uint64_t V) : Imm(V), Ty(SrcType::Ty_Imm) {}
  135. SrcOp(int64_t V) : Imm(V), Ty(SrcType::Ty_Imm) {}
  136. void addSrcToMIB(MachineInstrBuilder &MIB) const {
  137. switch (Ty) {
  138. case SrcType::Ty_Predicate:
  139. MIB.addPredicate(Pred);
  140. break;
  141. case SrcType::Ty_Reg:
  142. MIB.addUse(Reg);
  143. break;
  144. case SrcType::Ty_MIB:
  145. MIB.addUse(SrcMIB->getOperand(0).getReg());
  146. break;
  147. case SrcType::Ty_Imm:
  148. MIB.addImm(Imm);
  149. break;
  150. }
  151. }
  152. LLT getLLTTy(const MachineRegisterInfo &MRI) const {
  153. switch (Ty) {
  154. case SrcType::Ty_Predicate:
  155. case SrcType::Ty_Imm:
  156. llvm_unreachable("Not a register operand");
  157. case SrcType::Ty_Reg:
  158. return MRI.getType(Reg);
  159. case SrcType::Ty_MIB:
  160. return MRI.getType(SrcMIB->getOperand(0).getReg());
  161. }
  162. llvm_unreachable("Unrecognised SrcOp::SrcType enum");
  163. }
  164. Register getReg() const {
  165. switch (Ty) {
  166. case SrcType::Ty_Predicate:
  167. case SrcType::Ty_Imm:
  168. llvm_unreachable("Not a register operand");
  169. case SrcType::Ty_Reg:
  170. return Reg;
  171. case SrcType::Ty_MIB:
  172. return SrcMIB->getOperand(0).getReg();
  173. }
  174. llvm_unreachable("Unrecognised SrcOp::SrcType enum");
  175. }
  176. CmpInst::Predicate getPredicate() const {
  177. switch (Ty) {
  178. case SrcType::Ty_Predicate:
  179. return Pred;
  180. default:
  181. llvm_unreachable("Not a register operand");
  182. }
  183. }
  184. int64_t getImm() const {
  185. switch (Ty) {
  186. case SrcType::Ty_Imm:
  187. return Imm;
  188. default:
  189. llvm_unreachable("Not an immediate");
  190. }
  191. }
  192. SrcType getSrcOpKind() const { return Ty; }
  193. private:
  194. SrcType Ty;
  195. };
  196. /// Helper class to build MachineInstr.
  197. /// It keeps internally the insertion point and debug location for all
  198. /// the new instructions we want to create.
  199. /// This information can be modified via the related setters.
  200. class MachineIRBuilder {
  201. MachineIRBuilderState State;
  202. unsigned getOpcodeForMerge(const DstOp &DstOp, ArrayRef<SrcOp> SrcOps) const;
  203. protected:
  204. void validateTruncExt(const LLT Dst, const LLT Src, bool IsExtend);
  205. void validateUnaryOp(const LLT Res, const LLT Op0);
  206. void validateBinaryOp(const LLT Res, const LLT Op0, const LLT Op1);
  207. void validateShiftOp(const LLT Res, const LLT Op0, const LLT Op1);
  208. void validateSelectOp(const LLT ResTy, const LLT TstTy, const LLT Op0Ty,
  209. const LLT Op1Ty);
  210. void recordInsertion(MachineInstr *InsertedInstr) const {
  211. if (State.Observer)
  212. State.Observer->createdInstr(*InsertedInstr);
  213. }
  214. public:
  215. /// Some constructors for easy use.
  216. MachineIRBuilder() = default;
  217. MachineIRBuilder(MachineFunction &MF) { setMF(MF); }
  218. MachineIRBuilder(MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt) {
  219. setMF(*MBB.getParent());
  220. setInsertPt(MBB, InsPt);
  221. }
  222. MachineIRBuilder(MachineInstr &MI) :
  223. MachineIRBuilder(*MI.getParent(), MI.getIterator()) {
  224. setInstr(MI);
  225. setDebugLoc(MI.getDebugLoc());
  226. }
  227. MachineIRBuilder(MachineInstr &MI, GISelChangeObserver &Observer) :
  228. MachineIRBuilder(MI) {
  229. setChangeObserver(Observer);
  230. }
  231. virtual ~MachineIRBuilder() = default;
  232. MachineIRBuilder(const MachineIRBuilderState &BState) : State(BState) {}
  233. const TargetInstrInfo &getTII() {
  234. assert(State.TII && "TargetInstrInfo is not set");
  235. return *State.TII;
  236. }
  237. /// Getter for the function we currently build.
  238. MachineFunction &getMF() {
  239. assert(State.MF && "MachineFunction is not set");
  240. return *State.MF;
  241. }
  242. const MachineFunction &getMF() const {
  243. assert(State.MF && "MachineFunction is not set");
  244. return *State.MF;
  245. }
  246. const DataLayout &getDataLayout() const {
  247. return getMF().getFunction().getParent()->getDataLayout();
  248. }
  249. /// Getter for DebugLoc
  250. const DebugLoc &getDL() { return State.DL; }
  251. /// Getter for MRI
  252. MachineRegisterInfo *getMRI() { return State.MRI; }
  253. const MachineRegisterInfo *getMRI() const { return State.MRI; }
  254. /// Getter for the State
  255. MachineIRBuilderState &getState() { return State; }
  256. /// Getter for the basic block we currently build.
  257. const MachineBasicBlock &getMBB() const {
  258. assert(State.MBB && "MachineBasicBlock is not set");
  259. return *State.MBB;
  260. }
  261. MachineBasicBlock &getMBB() {
  262. return const_cast<MachineBasicBlock &>(
  263. const_cast<const MachineIRBuilder *>(this)->getMBB());
  264. }
  265. GISelCSEInfo *getCSEInfo() { return State.CSEInfo; }
  266. const GISelCSEInfo *getCSEInfo() const { return State.CSEInfo; }
  267. /// Current insertion point for new instructions.
  268. MachineBasicBlock::iterator getInsertPt() { return State.II; }
  269. /// Set the insertion point before the specified position.
  270. /// \pre MBB must be in getMF().
  271. /// \pre II must be a valid iterator in MBB.
  272. void setInsertPt(MachineBasicBlock &MBB, MachineBasicBlock::iterator II) {
  273. assert(MBB.getParent() == &getMF() &&
  274. "Basic block is in a different function");
  275. State.MBB = &MBB;
  276. State.II = II;
  277. }
  278. /// @}
  279. void setCSEInfo(GISelCSEInfo *Info) { State.CSEInfo = Info; }
  280. /// \name Setters for the insertion point.
  281. /// @{
  282. /// Set the MachineFunction where to build instructions.
  283. void setMF(MachineFunction &MF);
  284. /// Set the insertion point to the end of \p MBB.
  285. /// \pre \p MBB must be contained by getMF().
  286. void setMBB(MachineBasicBlock &MBB) {
  287. State.MBB = &MBB;
  288. State.II = MBB.end();
  289. assert(&getMF() == MBB.getParent() &&
  290. "Basic block is in a different function");
  291. }
  292. /// Set the insertion point to before MI.
  293. /// \pre MI must be in getMF().
  294. void setInstr(MachineInstr &MI) {
  295. assert(MI.getParent() && "Instruction is not part of a basic block");
  296. setMBB(*MI.getParent());
  297. State.II = MI.getIterator();
  298. setPCSections(MI.getPCSections());
  299. }
  300. /// @}
  301. /// Set the insertion point to before MI, and set the debug loc to MI's loc.
  302. /// \pre MI must be in getMF().
  303. void setInstrAndDebugLoc(MachineInstr &MI) {
  304. setInstr(MI);
  305. setDebugLoc(MI.getDebugLoc());
  306. }
  307. void setChangeObserver(GISelChangeObserver &Observer) {
  308. State.Observer = &Observer;
  309. }
  310. void stopObservingChanges() { State.Observer = nullptr; }
  311. /// @}
  312. /// Set the debug location to \p DL for all the next build instructions.
  313. void setDebugLoc(const DebugLoc &DL) { this->State.DL = DL; }
  314. /// Get the current instruction's debug location.
  315. const DebugLoc &getDebugLoc() { return State.DL; }
  316. /// Set the PC sections metadata to \p MD for all the next build instructions.
  317. void setPCSections(MDNode *MD) { State.PCSections = MD; }
  318. /// Get the current instruction's PC sections metadata.
  319. MDNode *getPCSections() { return State.PCSections; }
  320. /// Build and insert <empty> = \p Opcode <empty>.
  321. /// The insertion point is the one set by the last call of either
  322. /// setBasicBlock or setMI.
  323. ///
  324. /// \pre setBasicBlock or setMI must have been called.
  325. ///
  326. /// \return a MachineInstrBuilder for the newly created instruction.
  327. MachineInstrBuilder buildInstr(unsigned Opcode) {
  328. return insertInstr(buildInstrNoInsert(Opcode));
  329. }
  330. /// Build but don't insert <empty> = \p Opcode <empty>.
  331. ///
  332. /// \pre setMF, setBasicBlock or setMI must have been called.
  333. ///
  334. /// \return a MachineInstrBuilder for the newly created instruction.
  335. MachineInstrBuilder buildInstrNoInsert(unsigned Opcode);
  336. /// Insert an existing instruction at the insertion point.
  337. MachineInstrBuilder insertInstr(MachineInstrBuilder MIB);
  338. /// Build and insert a DBG_VALUE instruction expressing the fact that the
  339. /// associated \p Variable lives in \p Reg (suitably modified by \p Expr).
  340. MachineInstrBuilder buildDirectDbgValue(Register Reg, const MDNode *Variable,
  341. const MDNode *Expr);
  342. /// Build and insert a DBG_VALUE instruction expressing the fact that the
  343. /// associated \p Variable lives in memory at \p Reg (suitably modified by \p
  344. /// Expr).
  345. MachineInstrBuilder buildIndirectDbgValue(Register Reg,
  346. const MDNode *Variable,
  347. const MDNode *Expr);
  348. /// Build and insert a DBG_VALUE instruction expressing the fact that the
  349. /// associated \p Variable lives in the stack slot specified by \p FI
  350. /// (suitably modified by \p Expr).
  351. MachineInstrBuilder buildFIDbgValue(int FI, const MDNode *Variable,
  352. const MDNode *Expr);
  353. /// Build and insert a DBG_VALUE instructions specifying that \p Variable is
  354. /// given by \p C (suitably modified by \p Expr).
  355. MachineInstrBuilder buildConstDbgValue(const Constant &C,
  356. const MDNode *Variable,
  357. const MDNode *Expr);
  358. /// Build and insert a DBG_LABEL instructions specifying that \p Label is
  359. /// given. Convert "llvm.dbg.label Label" to "DBG_LABEL Label".
  360. MachineInstrBuilder buildDbgLabel(const MDNode *Label);
  361. /// Build and insert \p Res = G_DYN_STACKALLOC \p Size, \p Align
  362. ///
  363. /// G_DYN_STACKALLOC does a dynamic stack allocation and writes the address of
  364. /// the allocated memory into \p Res.
  365. /// \pre setBasicBlock or setMI must have been called.
  366. /// \pre \p Res must be a generic virtual register with pointer type.
  367. ///
  368. /// \return a MachineInstrBuilder for the newly created instruction.
  369. MachineInstrBuilder buildDynStackAlloc(const DstOp &Res, const SrcOp &Size,
  370. Align Alignment);
  371. /// Build and insert \p Res = G_FRAME_INDEX \p Idx
  372. ///
  373. /// G_FRAME_INDEX materializes the address of an alloca value or other
  374. /// stack-based object.
  375. ///
  376. /// \pre setBasicBlock or setMI must have been called.
  377. /// \pre \p Res must be a generic virtual register with pointer type.
  378. ///
  379. /// \return a MachineInstrBuilder for the newly created instruction.
  380. MachineInstrBuilder buildFrameIndex(const DstOp &Res, int Idx);
  381. /// Build and insert \p Res = G_GLOBAL_VALUE \p GV
  382. ///
  383. /// G_GLOBAL_VALUE materializes the address of the specified global
  384. /// into \p Res.
  385. ///
  386. /// \pre setBasicBlock or setMI must have been called.
  387. /// \pre \p Res must be a generic virtual register with pointer type
  388. /// in the same address space as \p GV.
  389. ///
  390. /// \return a MachineInstrBuilder for the newly created instruction.
  391. MachineInstrBuilder buildGlobalValue(const DstOp &Res, const GlobalValue *GV);
  392. /// Build and insert \p Res = G_PTR_ADD \p Op0, \p Op1
  393. ///
  394. /// G_PTR_ADD adds \p Op1 addressible units to the pointer specified by \p Op0,
  395. /// storing the resulting pointer in \p Res. Addressible units are typically
  396. /// bytes but this can vary between targets.
  397. ///
  398. /// \pre setBasicBlock or setMI must have been called.
  399. /// \pre \p Res and \p Op0 must be generic virtual registers with pointer
  400. /// type.
  401. /// \pre \p Op1 must be a generic virtual register with scalar type.
  402. ///
  403. /// \return a MachineInstrBuilder for the newly created instruction.
  404. MachineInstrBuilder buildPtrAdd(const DstOp &Res, const SrcOp &Op0,
  405. const SrcOp &Op1);
  406. /// Materialize and insert \p Res = G_PTR_ADD \p Op0, (G_CONSTANT \p Value)
  407. ///
  408. /// G_PTR_ADD adds \p Value bytes to the pointer specified by \p Op0,
  409. /// storing the resulting pointer in \p Res. If \p Value is zero then no
  410. /// G_PTR_ADD or G_CONSTANT will be created and \pre Op0 will be assigned to
  411. /// \p Res.
  412. ///
  413. /// \pre setBasicBlock or setMI must have been called.
  414. /// \pre \p Op0 must be a generic virtual register with pointer type.
  415. /// \pre \p ValueTy must be a scalar type.
  416. /// \pre \p Res must be 0. This is to detect confusion between
  417. /// materializePtrAdd() and buildPtrAdd().
  418. /// \post \p Res will either be a new generic virtual register of the same
  419. /// type as \p Op0 or \p Op0 itself.
  420. ///
  421. /// \return a MachineInstrBuilder for the newly created instruction.
  422. std::optional<MachineInstrBuilder> materializePtrAdd(Register &Res,
  423. Register Op0,
  424. const LLT ValueTy,
  425. uint64_t Value);
  426. /// Build and insert \p Res = G_PTRMASK \p Op0, \p Op1
  427. MachineInstrBuilder buildPtrMask(const DstOp &Res, const SrcOp &Op0,
  428. const SrcOp &Op1) {
  429. return buildInstr(TargetOpcode::G_PTRMASK, {Res}, {Op0, Op1});
  430. }
  431. /// Build and insert \p Res = G_PTRMASK \p Op0, \p G_CONSTANT (1 << NumBits) - 1
  432. ///
  433. /// This clears the low bits of a pointer operand without destroying its
  434. /// pointer properties. This has the effect of rounding the address *down* to
  435. /// a specified alignment in bits.
  436. ///
  437. /// \pre setBasicBlock or setMI must have been called.
  438. /// \pre \p Res and \p Op0 must be generic virtual registers with pointer
  439. /// type.
  440. /// \pre \p NumBits must be an integer representing the number of low bits to
  441. /// be cleared in \p Op0.
  442. ///
  443. /// \return a MachineInstrBuilder for the newly created instruction.
  444. MachineInstrBuilder buildMaskLowPtrBits(const DstOp &Res, const SrcOp &Op0,
  445. uint32_t NumBits);
  446. /// Build and insert
  447. /// a, b, ..., x = G_UNMERGE_VALUES \p Op0
  448. /// \p Res = G_BUILD_VECTOR a, b, ..., x, undef, ..., undef
  449. ///
  450. /// Pad \p Op0 with undef elements to match number of elements in \p Res.
  451. ///
  452. /// \pre setBasicBlock or setMI must have been called.
  453. /// \pre \p Res and \p Op0 must be generic virtual registers with vector type,
  454. /// same vector element type and Op0 must have fewer elements then Res.
  455. ///
  456. /// \return a MachineInstrBuilder for the newly created build vector instr.
  457. MachineInstrBuilder buildPadVectorWithUndefElements(const DstOp &Res,
  458. const SrcOp &Op0);
  459. /// Build and insert
  460. /// a, b, ..., x, y, z = G_UNMERGE_VALUES \p Op0
  461. /// \p Res = G_BUILD_VECTOR a, b, ..., x
  462. ///
  463. /// Delete trailing elements in \p Op0 to match number of elements in \p Res.
  464. ///
  465. /// \pre setBasicBlock or setMI must have been called.
  466. /// \pre \p Res and \p Op0 must be generic virtual registers with vector type,
  467. /// same vector element type and Op0 must have more elements then Res.
  468. ///
  469. /// \return a MachineInstrBuilder for the newly created build vector instr.
  470. MachineInstrBuilder buildDeleteTrailingVectorElements(const DstOp &Res,
  471. const SrcOp &Op0);
  472. /// Build and insert \p Res, \p CarryOut = G_UADDO \p Op0, \p Op1
  473. ///
  474. /// G_UADDO sets \p Res to \p Op0 + \p Op1 (truncated to the bit width) and
  475. /// sets \p CarryOut to 1 if the result overflowed in unsigned arithmetic.
  476. ///
  477. /// \pre setBasicBlock or setMI must have been called.
  478. /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers with the
  479. /// same scalar type.
  480. ////\pre \p CarryOut must be generic virtual register with scalar type
  481. ///(typically s1)
  482. ///
  483. /// \return The newly created instruction.
  484. MachineInstrBuilder buildUAddo(const DstOp &Res, const DstOp &CarryOut,
  485. const SrcOp &Op0, const SrcOp &Op1) {
  486. return buildInstr(TargetOpcode::G_UADDO, {Res, CarryOut}, {Op0, Op1});
  487. }
  488. /// Build and insert \p Res, \p CarryOut = G_USUBO \p Op0, \p Op1
  489. MachineInstrBuilder buildUSubo(const DstOp &Res, const DstOp &CarryOut,
  490. const SrcOp &Op0, const SrcOp &Op1) {
  491. return buildInstr(TargetOpcode::G_USUBO, {Res, CarryOut}, {Op0, Op1});
  492. }
  493. /// Build and insert \p Res, \p CarryOut = G_SADDO \p Op0, \p Op1
  494. MachineInstrBuilder buildSAddo(const DstOp &Res, const DstOp &CarryOut,
  495. const SrcOp &Op0, const SrcOp &Op1) {
  496. return buildInstr(TargetOpcode::G_SADDO, {Res, CarryOut}, {Op0, Op1});
  497. }
  498. /// Build and insert \p Res, \p CarryOut = G_SUBO \p Op0, \p Op1
  499. MachineInstrBuilder buildSSubo(const DstOp &Res, const DstOp &CarryOut,
  500. const SrcOp &Op0, const SrcOp &Op1) {
  501. return buildInstr(TargetOpcode::G_SSUBO, {Res, CarryOut}, {Op0, Op1});
  502. }
  503. /// Build and insert \p Res, \p CarryOut = G_UADDE \p Op0,
  504. /// \p Op1, \p CarryIn
  505. ///
  506. /// G_UADDE sets \p Res to \p Op0 + \p Op1 + \p CarryIn (truncated to the bit
  507. /// width) and sets \p CarryOut to 1 if the result overflowed in unsigned
  508. /// arithmetic.
  509. ///
  510. /// \pre setBasicBlock or setMI must have been called.
  511. /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
  512. /// with the same scalar type.
  513. /// \pre \p CarryOut and \p CarryIn must be generic virtual
  514. /// registers with the same scalar type (typically s1)
  515. ///
  516. /// \return The newly created instruction.
  517. MachineInstrBuilder buildUAdde(const DstOp &Res, const DstOp &CarryOut,
  518. const SrcOp &Op0, const SrcOp &Op1,
  519. const SrcOp &CarryIn) {
  520. return buildInstr(TargetOpcode::G_UADDE, {Res, CarryOut},
  521. {Op0, Op1, CarryIn});
  522. }
  523. /// Build and insert \p Res, \p CarryOut = G_USUBE \p Op0, \p Op1, \p CarryInp
  524. MachineInstrBuilder buildUSube(const DstOp &Res, const DstOp &CarryOut,
  525. const SrcOp &Op0, const SrcOp &Op1,
  526. const SrcOp &CarryIn) {
  527. return buildInstr(TargetOpcode::G_USUBE, {Res, CarryOut},
  528. {Op0, Op1, CarryIn});
  529. }
  530. /// Build and insert \p Res, \p CarryOut = G_SADDE \p Op0, \p Op1, \p CarryInp
  531. MachineInstrBuilder buildSAdde(const DstOp &Res, const DstOp &CarryOut,
  532. const SrcOp &Op0, const SrcOp &Op1,
  533. const SrcOp &CarryIn) {
  534. return buildInstr(TargetOpcode::G_SADDE, {Res, CarryOut},
  535. {Op0, Op1, CarryIn});
  536. }
  537. /// Build and insert \p Res, \p CarryOut = G_SSUBE \p Op0, \p Op1, \p CarryInp
  538. MachineInstrBuilder buildSSube(const DstOp &Res, const DstOp &CarryOut,
  539. const SrcOp &Op0, const SrcOp &Op1,
  540. const SrcOp &CarryIn) {
  541. return buildInstr(TargetOpcode::G_SSUBE, {Res, CarryOut},
  542. {Op0, Op1, CarryIn});
  543. }
  544. /// Build and insert \p Res = G_ANYEXT \p Op0
  545. ///
  546. /// G_ANYEXT produces a register of the specified width, with bits 0 to
  547. /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are unspecified
  548. /// (i.e. this is neither zero nor sign-extension). For a vector register,
  549. /// each element is extended individually.
  550. ///
  551. /// \pre setBasicBlock or setMI must have been called.
  552. /// \pre \p Res must be a generic virtual register with scalar or vector type.
  553. /// \pre \p Op must be a generic virtual register with scalar or vector type.
  554. /// \pre \p Op must be smaller than \p Res
  555. ///
  556. /// \return The newly created instruction.
  557. MachineInstrBuilder buildAnyExt(const DstOp &Res, const SrcOp &Op);
  558. /// Build and insert \p Res = G_SEXT \p Op
  559. ///
  560. /// G_SEXT produces a register of the specified width, with bits 0 to
  561. /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are duplicated from the
  562. /// high bit of \p Op (i.e. 2s-complement sign extended).
  563. ///
  564. /// \pre setBasicBlock or setMI must have been called.
  565. /// \pre \p Res must be a generic virtual register with scalar or vector type.
  566. /// \pre \p Op must be a generic virtual register with scalar or vector type.
  567. /// \pre \p Op must be smaller than \p Res
  568. ///
  569. /// \return The newly created instruction.
  570. MachineInstrBuilder buildSExt(const DstOp &Res, const SrcOp &Op);
  571. /// Build and insert \p Res = G_SEXT_INREG \p Op, ImmOp
  572. MachineInstrBuilder buildSExtInReg(const DstOp &Res, const SrcOp &Op, int64_t ImmOp) {
  573. return buildInstr(TargetOpcode::G_SEXT_INREG, {Res}, {Op, SrcOp(ImmOp)});
  574. }
  575. /// Build and insert \p Res = G_FPEXT \p Op
  576. MachineInstrBuilder buildFPExt(const DstOp &Res, const SrcOp &Op,
  577. std::optional<unsigned> Flags = std::nullopt) {
  578. return buildInstr(TargetOpcode::G_FPEXT, {Res}, {Op}, Flags);
  579. }
  580. /// Build and insert a G_PTRTOINT instruction.
  581. MachineInstrBuilder buildPtrToInt(const DstOp &Dst, const SrcOp &Src) {
  582. return buildInstr(TargetOpcode::G_PTRTOINT, {Dst}, {Src});
  583. }
  584. /// Build and insert a G_INTTOPTR instruction.
  585. MachineInstrBuilder buildIntToPtr(const DstOp &Dst, const SrcOp &Src) {
  586. return buildInstr(TargetOpcode::G_INTTOPTR, {Dst}, {Src});
  587. }
  588. /// Build and insert \p Dst = G_BITCAST \p Src
  589. MachineInstrBuilder buildBitcast(const DstOp &Dst, const SrcOp &Src) {
  590. return buildInstr(TargetOpcode::G_BITCAST, {Dst}, {Src});
  591. }
  592. /// Build and insert \p Dst = G_ADDRSPACE_CAST \p Src
  593. MachineInstrBuilder buildAddrSpaceCast(const DstOp &Dst, const SrcOp &Src) {
  594. return buildInstr(TargetOpcode::G_ADDRSPACE_CAST, {Dst}, {Src});
  595. }
  596. /// \return The opcode of the extension the target wants to use for boolean
  597. /// values.
  598. unsigned getBoolExtOp(bool IsVec, bool IsFP) const;
  599. // Build and insert \p Res = G_ANYEXT \p Op, \p Res = G_SEXT \p Op, or \p Res
  600. // = G_ZEXT \p Op depending on how the target wants to extend boolean values.
  601. MachineInstrBuilder buildBoolExt(const DstOp &Res, const SrcOp &Op,
  602. bool IsFP);
  603. // Build and insert \p Res = G_SEXT_INREG \p Op, 1 or \p Res = G_AND \p Op, 1,
  604. // or COPY depending on how the target wants to extend boolean values, using
  605. // the original register size.
  606. MachineInstrBuilder buildBoolExtInReg(const DstOp &Res, const SrcOp &Op,
  607. bool IsVector,
  608. bool IsFP);
  609. /// Build and insert \p Res = G_ZEXT \p Op
  610. ///
  611. /// G_ZEXT produces a register of the specified width, with bits 0 to
  612. /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are 0. For a vector
  613. /// register, each element is extended individually.
  614. ///
  615. /// \pre setBasicBlock or setMI must have been called.
  616. /// \pre \p Res must be a generic virtual register with scalar or vector type.
  617. /// \pre \p Op must be a generic virtual register with scalar or vector type.
  618. /// \pre \p Op must be smaller than \p Res
  619. ///
  620. /// \return The newly created instruction.
  621. MachineInstrBuilder buildZExt(const DstOp &Res, const SrcOp &Op);
  622. /// Build and insert \p Res = G_SEXT \p Op, \p Res = G_TRUNC \p Op, or
  623. /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
  624. /// ///
  625. /// \pre setBasicBlock or setMI must have been called.
  626. /// \pre \p Res must be a generic virtual register with scalar or vector type.
  627. /// \pre \p Op must be a generic virtual register with scalar or vector type.
  628. ///
  629. /// \return The newly created instruction.
  630. MachineInstrBuilder buildSExtOrTrunc(const DstOp &Res, const SrcOp &Op);
  631. /// Build and insert \p Res = G_ZEXT \p Op, \p Res = G_TRUNC \p Op, or
  632. /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
  633. /// ///
  634. /// \pre setBasicBlock or setMI must have been called.
  635. /// \pre \p Res must be a generic virtual register with scalar or vector type.
  636. /// \pre \p Op must be a generic virtual register with scalar or vector type.
  637. ///
  638. /// \return The newly created instruction.
  639. MachineInstrBuilder buildZExtOrTrunc(const DstOp &Res, const SrcOp &Op);
  640. // Build and insert \p Res = G_ANYEXT \p Op, \p Res = G_TRUNC \p Op, or
  641. /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
  642. /// ///
  643. /// \pre setBasicBlock or setMI must have been called.
  644. /// \pre \p Res must be a generic virtual register with scalar or vector type.
  645. /// \pre \p Op must be a generic virtual register with scalar or vector type.
  646. ///
  647. /// \return The newly created instruction.
  648. MachineInstrBuilder buildAnyExtOrTrunc(const DstOp &Res, const SrcOp &Op);
  649. /// Build and insert \p Res = \p ExtOpc, \p Res = G_TRUNC \p
  650. /// Op, or \p Res = COPY \p Op depending on the differing sizes of \p Res and
  651. /// \p Op.
  652. /// ///
  653. /// \pre setBasicBlock or setMI must have been called.
  654. /// \pre \p Res must be a generic virtual register with scalar or vector type.
  655. /// \pre \p Op must be a generic virtual register with scalar or vector type.
  656. ///
  657. /// \return The newly created instruction.
  658. MachineInstrBuilder buildExtOrTrunc(unsigned ExtOpc, const DstOp &Res,
  659. const SrcOp &Op);
  660. /// Build and inserts \p Res = \p G_AND \p Op, \p LowBitsSet(ImmOp)
  661. /// Since there is no G_ZEXT_INREG like G_SEXT_INREG, the instruction is
  662. /// emulated using G_AND.
  663. MachineInstrBuilder buildZExtInReg(const DstOp &Res, const SrcOp &Op,
  664. int64_t ImmOp);
  665. /// Build and insert an appropriate cast between two registers of equal size.
  666. MachineInstrBuilder buildCast(const DstOp &Dst, const SrcOp &Src);
  667. /// Build and insert G_BR \p Dest
  668. ///
  669. /// G_BR is an unconditional branch to \p Dest.
  670. ///
  671. /// \pre setBasicBlock or setMI must have been called.
  672. ///
  673. /// \return a MachineInstrBuilder for the newly created instruction.
  674. MachineInstrBuilder buildBr(MachineBasicBlock &Dest);
  675. /// Build and insert G_BRCOND \p Tst, \p Dest
  676. ///
  677. /// G_BRCOND is a conditional branch to \p Dest.
  678. ///
  679. /// \pre setBasicBlock or setMI must have been called.
  680. /// \pre \p Tst must be a generic virtual register with scalar
  681. /// type. At the beginning of legalization, this will be a single
  682. /// bit (s1). Targets with interesting flags registers may change
  683. /// this. For a wider type, whether the branch is taken must only
  684. /// depend on bit 0 (for now).
  685. ///
  686. /// \return The newly created instruction.
  687. MachineInstrBuilder buildBrCond(const SrcOp &Tst, MachineBasicBlock &Dest);
  688. /// Build and insert G_BRINDIRECT \p Tgt
  689. ///
  690. /// G_BRINDIRECT is an indirect branch to \p Tgt.
  691. ///
  692. /// \pre setBasicBlock or setMI must have been called.
  693. /// \pre \p Tgt must be a generic virtual register with pointer type.
  694. ///
  695. /// \return a MachineInstrBuilder for the newly created instruction.
  696. MachineInstrBuilder buildBrIndirect(Register Tgt);
  697. /// Build and insert G_BRJT \p TablePtr, \p JTI, \p IndexReg
  698. ///
  699. /// G_BRJT is a jump table branch using a table base pointer \p TablePtr,
  700. /// jump table index \p JTI and index \p IndexReg
  701. ///
  702. /// \pre setBasicBlock or setMI must have been called.
  703. /// \pre \p TablePtr must be a generic virtual register with pointer type.
  704. /// \pre \p JTI must be be a jump table index.
  705. /// \pre \p IndexReg must be a generic virtual register with pointer type.
  706. ///
  707. /// \return a MachineInstrBuilder for the newly created instruction.
  708. MachineInstrBuilder buildBrJT(Register TablePtr, unsigned JTI,
  709. Register IndexReg);
  710. /// Build and insert \p Res = G_CONSTANT \p Val
  711. ///
  712. /// G_CONSTANT is an integer constant with the specified size and value. \p
  713. /// Val will be extended or truncated to the size of \p Reg.
  714. ///
  715. /// \pre setBasicBlock or setMI must have been called.
  716. /// \pre \p Res must be a generic virtual register with scalar or pointer
  717. /// type.
  718. ///
  719. /// \return The newly created instruction.
  720. virtual MachineInstrBuilder buildConstant(const DstOp &Res,
  721. const ConstantInt &Val);
  722. /// Build and insert \p Res = G_CONSTANT \p Val
  723. ///
  724. /// G_CONSTANT is an integer constant with the specified size and value.
  725. ///
  726. /// \pre setBasicBlock or setMI must have been called.
  727. /// \pre \p Res must be a generic virtual register with scalar type.
  728. ///
  729. /// \return The newly created instruction.
  730. MachineInstrBuilder buildConstant(const DstOp &Res, int64_t Val);
  731. MachineInstrBuilder buildConstant(const DstOp &Res, const APInt &Val);
  732. /// Build and insert \p Res = G_FCONSTANT \p Val
  733. ///
  734. /// G_FCONSTANT is a floating-point constant with the specified size and
  735. /// value.
  736. ///
  737. /// \pre setBasicBlock or setMI must have been called.
  738. /// \pre \p Res must be a generic virtual register with scalar type.
  739. ///
  740. /// \return The newly created instruction.
  741. virtual MachineInstrBuilder buildFConstant(const DstOp &Res,
  742. const ConstantFP &Val);
  743. MachineInstrBuilder buildFConstant(const DstOp &Res, double Val);
  744. MachineInstrBuilder buildFConstant(const DstOp &Res, const APFloat &Val);
  745. /// Build and insert \p Res = COPY Op
  746. ///
  747. /// Register-to-register COPY sets \p Res to \p Op.
  748. ///
  749. /// \pre setBasicBlock or setMI must have been called.
  750. ///
  751. /// \return a MachineInstrBuilder for the newly created instruction.
  752. MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op);
  753. /// Build and insert G_ASSERT_SEXT, G_ASSERT_ZEXT, or G_ASSERT_ALIGN
  754. ///
  755. /// \return a MachineInstrBuilder for the newly created instruction.
  756. MachineInstrBuilder buildAssertInstr(unsigned Opc, const DstOp &Res,
  757. const SrcOp &Op, unsigned Val) {
  758. return buildInstr(Opc, Res, Op).addImm(Val);
  759. }
  760. /// Build and insert \p Res = G_ASSERT_ZEXT Op, Size
  761. ///
  762. /// \return a MachineInstrBuilder for the newly created instruction.
  763. MachineInstrBuilder buildAssertZExt(const DstOp &Res, const SrcOp &Op,
  764. unsigned Size) {
  765. return buildAssertInstr(TargetOpcode::G_ASSERT_ZEXT, Res, Op, Size);
  766. }
  767. /// Build and insert \p Res = G_ASSERT_SEXT Op, Size
  768. ///
  769. /// \return a MachineInstrBuilder for the newly created instruction.
  770. MachineInstrBuilder buildAssertSExt(const DstOp &Res, const SrcOp &Op,
  771. unsigned Size) {
  772. return buildAssertInstr(TargetOpcode::G_ASSERT_SEXT, Res, Op, Size);
  773. }
  774. /// Build and insert \p Res = G_ASSERT_ALIGN Op, AlignVal
  775. ///
  776. /// \return a MachineInstrBuilder for the newly created instruction.
  777. MachineInstrBuilder buildAssertAlign(const DstOp &Res, const SrcOp &Op,
  778. Align AlignVal) {
  779. return buildAssertInstr(TargetOpcode::G_ASSERT_ALIGN, Res, Op,
  780. AlignVal.value());
  781. }
  782. /// Build and insert `Res = G_LOAD Addr, MMO`.
  783. ///
  784. /// Loads the value stored at \p Addr. Puts the result in \p Res.
  785. ///
  786. /// \pre setBasicBlock or setMI must have been called.
  787. /// \pre \p Res must be a generic virtual register.
  788. /// \pre \p Addr must be a generic virtual register with pointer type.
  789. ///
  790. /// \return a MachineInstrBuilder for the newly created instruction.
  791. MachineInstrBuilder buildLoad(const DstOp &Res, const SrcOp &Addr,
  792. MachineMemOperand &MMO) {
  793. return buildLoadInstr(TargetOpcode::G_LOAD, Res, Addr, MMO);
  794. }
  795. /// Build and insert a G_LOAD instruction, while constructing the
  796. /// MachineMemOperand.
  797. MachineInstrBuilder
  798. buildLoad(const DstOp &Res, const SrcOp &Addr, MachinePointerInfo PtrInfo,
  799. Align Alignment,
  800. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  801. const AAMDNodes &AAInfo = AAMDNodes());
  802. /// Build and insert `Res = <opcode> Addr, MMO`.
  803. ///
  804. /// Loads the value stored at \p Addr. Puts the result in \p Res.
  805. ///
  806. /// \pre setBasicBlock or setMI must have been called.
  807. /// \pre \p Res must be a generic virtual register.
  808. /// \pre \p Addr must be a generic virtual register with pointer type.
  809. ///
  810. /// \return a MachineInstrBuilder for the newly created instruction.
  811. MachineInstrBuilder buildLoadInstr(unsigned Opcode, const DstOp &Res,
  812. const SrcOp &Addr, MachineMemOperand &MMO);
  813. /// Helper to create a load from a constant offset given a base address. Load
  814. /// the type of \p Dst from \p Offset from the given base address and memory
  815. /// operand.
  816. MachineInstrBuilder buildLoadFromOffset(const DstOp &Dst,
  817. const SrcOp &BasePtr,
  818. MachineMemOperand &BaseMMO,
  819. int64_t Offset);
  820. /// Build and insert `G_STORE Val, Addr, MMO`.
  821. ///
  822. /// Stores the value \p Val to \p Addr.
  823. ///
  824. /// \pre setBasicBlock or setMI must have been called.
  825. /// \pre \p Val must be a generic virtual register.
  826. /// \pre \p Addr must be a generic virtual register with pointer type.
  827. ///
  828. /// \return a MachineInstrBuilder for the newly created instruction.
  829. MachineInstrBuilder buildStore(const SrcOp &Val, const SrcOp &Addr,
  830. MachineMemOperand &MMO);
  831. /// Build and insert a G_STORE instruction, while constructing the
  832. /// MachineMemOperand.
  833. MachineInstrBuilder
  834. buildStore(const SrcOp &Val, const SrcOp &Addr, MachinePointerInfo PtrInfo,
  835. Align Alignment,
  836. MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
  837. const AAMDNodes &AAInfo = AAMDNodes());
  838. /// Build and insert `Res0, ... = G_EXTRACT Src, Idx0`.
  839. ///
  840. /// \pre setBasicBlock or setMI must have been called.
  841. /// \pre \p Res and \p Src must be generic virtual registers.
  842. ///
  843. /// \return a MachineInstrBuilder for the newly created instruction.
  844. MachineInstrBuilder buildExtract(const DstOp &Res, const SrcOp &Src, uint64_t Index);
  845. /// Build and insert \p Res = IMPLICIT_DEF.
  846. MachineInstrBuilder buildUndef(const DstOp &Res);
  847. /// Build and insert \p Res = G_MERGE_VALUES \p Op0, ...
  848. ///
  849. /// G_MERGE_VALUES combines the input elements contiguously into a larger
  850. /// register. It should only be used when the destination register is not a
  851. /// vector.
  852. ///
  853. /// \pre setBasicBlock or setMI must have been called.
  854. /// \pre The entire register \p Res (and no more) must be covered by the input
  855. /// registers.
  856. /// \pre The type of all \p Ops registers must be identical.
  857. ///
  858. /// \return a MachineInstrBuilder for the newly created instruction.
  859. MachineInstrBuilder buildMergeValues(const DstOp &Res,
  860. ArrayRef<Register> Ops);
  861. /// Build and insert \p Res = G_MERGE_VALUES \p Op0, ...
  862. /// or \p Res = G_BUILD_VECTOR \p Op0, ...
  863. /// or \p Res = G_CONCAT_VECTORS \p Op0, ...
  864. ///
  865. /// G_MERGE_VALUES combines the input elements contiguously into a larger
  866. /// register. It is used when the destination register is not a vector.
  867. /// G_BUILD_VECTOR combines scalar inputs into a vector register.
  868. /// G_CONCAT_VECTORS combines vector inputs into a vector register.
  869. ///
  870. /// \pre setBasicBlock or setMI must have been called.
  871. /// \pre The entire register \p Res (and no more) must be covered by the input
  872. /// registers.
  873. /// \pre The type of all \p Ops registers must be identical.
  874. ///
  875. /// \return a MachineInstrBuilder for the newly created instruction. The
  876. /// opcode of the new instruction will depend on the types of both
  877. /// the destination and the sources.
  878. MachineInstrBuilder buildMergeLikeInstr(const DstOp &Res,
  879. ArrayRef<Register> Ops);
  880. MachineInstrBuilder buildMergeLikeInstr(const DstOp &Res,
  881. std::initializer_list<SrcOp> Ops);
  882. /// Build and insert \p Res0, ... = G_UNMERGE_VALUES \p Op
  883. ///
  884. /// G_UNMERGE_VALUES splits contiguous bits of the input into multiple
  885. ///
  886. /// \pre setBasicBlock or setMI must have been called.
  887. /// \pre The entire register \p Res (and no more) must be covered by the input
  888. /// registers.
  889. /// \pre The type of all \p Res registers must be identical.
  890. ///
  891. /// \return a MachineInstrBuilder for the newly created instruction.
  892. MachineInstrBuilder buildUnmerge(ArrayRef<LLT> Res, const SrcOp &Op);
  893. MachineInstrBuilder buildUnmerge(ArrayRef<Register> Res, const SrcOp &Op);
  894. /// Build and insert an unmerge of \p Res sized pieces to cover \p Op
  895. MachineInstrBuilder buildUnmerge(LLT Res, const SrcOp &Op);
  896. /// Build and insert \p Res = G_BUILD_VECTOR \p Op0, ...
  897. ///
  898. /// G_BUILD_VECTOR creates a vector value from multiple scalar registers.
  899. /// \pre setBasicBlock or setMI must have been called.
  900. /// \pre The entire register \p Res (and no more) must be covered by the
  901. /// input scalar registers.
  902. /// \pre The type of all \p Ops registers must be identical.
  903. ///
  904. /// \return a MachineInstrBuilder for the newly created instruction.
  905. MachineInstrBuilder buildBuildVector(const DstOp &Res,
  906. ArrayRef<Register> Ops);
  907. /// Build and insert \p Res = G_BUILD_VECTOR \p Op0, ... where each OpN is
  908. /// built with G_CONSTANT.
  909. MachineInstrBuilder buildBuildVectorConstant(const DstOp &Res,
  910. ArrayRef<APInt> Ops);
  911. /// Build and insert \p Res = G_BUILD_VECTOR with \p Src replicated to fill
  912. /// the number of elements
  913. MachineInstrBuilder buildSplatVector(const DstOp &Res,
  914. const SrcOp &Src);
  915. /// Build and insert \p Res = G_BUILD_VECTOR_TRUNC \p Op0, ...
  916. ///
  917. /// G_BUILD_VECTOR_TRUNC creates a vector value from multiple scalar registers
  918. /// which have types larger than the destination vector element type, and
  919. /// truncates the values to fit.
  920. ///
  921. /// If the operands given are already the same size as the vector elt type,
  922. /// then this method will instead create a G_BUILD_VECTOR instruction.
  923. ///
  924. /// \pre setBasicBlock or setMI must have been called.
  925. /// \pre The type of all \p Ops registers must be identical.
  926. ///
  927. /// \return a MachineInstrBuilder for the newly created instruction.
  928. MachineInstrBuilder buildBuildVectorTrunc(const DstOp &Res,
  929. ArrayRef<Register> Ops);
  930. /// Build and insert a vector splat of a scalar \p Src using a
  931. /// G_INSERT_VECTOR_ELT and G_SHUFFLE_VECTOR idiom.
  932. ///
  933. /// \pre setBasicBlock or setMI must have been called.
  934. /// \pre \p Src must have the same type as the element type of \p Dst
  935. ///
  936. /// \return a MachineInstrBuilder for the newly created instruction.
  937. MachineInstrBuilder buildShuffleSplat(const DstOp &Res, const SrcOp &Src);
  938. /// Build and insert \p Res = G_SHUFFLE_VECTOR \p Src1, \p Src2, \p Mask
  939. ///
  940. /// \pre setBasicBlock or setMI must have been called.
  941. ///
  942. /// \return a MachineInstrBuilder for the newly created instruction.
  943. MachineInstrBuilder buildShuffleVector(const DstOp &Res, const SrcOp &Src1,
  944. const SrcOp &Src2, ArrayRef<int> Mask);
  945. /// Build and insert \p Res = G_CONCAT_VECTORS \p Op0, ...
  946. ///
  947. /// G_CONCAT_VECTORS creates a vector from the concatenation of 2 or more
  948. /// vectors.
  949. ///
  950. /// \pre setBasicBlock or setMI must have been called.
  951. /// \pre The entire register \p Res (and no more) must be covered by the input
  952. /// registers.
  953. /// \pre The type of all source operands must be identical.
  954. ///
  955. /// \return a MachineInstrBuilder for the newly created instruction.
  956. MachineInstrBuilder buildConcatVectors(const DstOp &Res,
  957. ArrayRef<Register> Ops);
  958. MachineInstrBuilder buildInsert(const DstOp &Res, const SrcOp &Src,
  959. const SrcOp &Op, unsigned Index);
  960. /// Build and insert either a G_INTRINSIC (if \p HasSideEffects is false) or
  961. /// G_INTRINSIC_W_SIDE_EFFECTS instruction. Its first operand will be the
  962. /// result register definition unless \p Reg is NoReg (== 0). The second
  963. /// operand will be the intrinsic's ID.
  964. ///
  965. /// Callers are expected to add the required definitions and uses afterwards.
  966. ///
  967. /// \pre setBasicBlock or setMI must have been called.
  968. ///
  969. /// \return a MachineInstrBuilder for the newly created instruction.
  970. MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef<Register> Res,
  971. bool HasSideEffects);
  972. MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef<DstOp> Res,
  973. bool HasSideEffects);
  974. /// Build and insert \p Res = G_FPTRUNC \p Op
  975. ///
  976. /// G_FPTRUNC converts a floating-point value into one with a smaller type.
  977. ///
  978. /// \pre setBasicBlock or setMI must have been called.
  979. /// \pre \p Res must be a generic virtual register with scalar or vector type.
  980. /// \pre \p Op must be a generic virtual register with scalar or vector type.
  981. /// \pre \p Res must be smaller than \p Op
  982. ///
  983. /// \return The newly created instruction.
  984. MachineInstrBuilder
  985. buildFPTrunc(const DstOp &Res, const SrcOp &Op,
  986. std::optional<unsigned> Flags = std::nullopt);
  987. /// Build and insert \p Res = G_TRUNC \p Op
  988. ///
  989. /// G_TRUNC extracts the low bits of a type. For a vector type each element is
  990. /// truncated independently before being packed into the destination.
  991. ///
  992. /// \pre setBasicBlock or setMI must have been called.
  993. /// \pre \p Res must be a generic virtual register with scalar or vector type.
  994. /// \pre \p Op must be a generic virtual register with scalar or vector type.
  995. /// \pre \p Res must be smaller than \p Op
  996. ///
  997. /// \return The newly created instruction.
  998. MachineInstrBuilder buildTrunc(const DstOp &Res, const SrcOp &Op);
  999. /// Build and insert a \p Res = G_ICMP \p Pred, \p Op0, \p Op1
  1000. ///
  1001. /// \pre setBasicBlock or setMI must have been called.
  1002. /// \pre \p Res must be a generic virtual register with scalar or
  1003. /// vector type. Typically this starts as s1 or <N x s1>.
  1004. /// \pre \p Op0 and Op1 must be generic virtual registers with the
  1005. /// same number of elements as \p Res. If \p Res is a scalar,
  1006. /// \p Op0 must be either a scalar or pointer.
  1007. /// \pre \p Pred must be an integer predicate.
  1008. ///
  1009. /// \return a MachineInstrBuilder for the newly created instruction.
  1010. MachineInstrBuilder buildICmp(CmpInst::Predicate Pred, const DstOp &Res,
  1011. const SrcOp &Op0, const SrcOp &Op1);
  1012. /// Build and insert a \p Res = G_FCMP \p Pred\p Op0, \p Op1
  1013. ///
  1014. /// \pre setBasicBlock or setMI must have been called.
  1015. /// \pre \p Res must be a generic virtual register with scalar or
  1016. /// vector type. Typically this starts as s1 or <N x s1>.
  1017. /// \pre \p Op0 and Op1 must be generic virtual registers with the
  1018. /// same number of elements as \p Res (or scalar, if \p Res is
  1019. /// scalar).
  1020. /// \pre \p Pred must be a floating-point predicate.
  1021. ///
  1022. /// \return a MachineInstrBuilder for the newly created instruction.
  1023. MachineInstrBuilder buildFCmp(CmpInst::Predicate Pred, const DstOp &Res,
  1024. const SrcOp &Op0, const SrcOp &Op1,
  1025. std::optional<unsigned> Flags = std::nullopt);
  1026. /// Build and insert a \p Res = G_SELECT \p Tst, \p Op0, \p Op1
  1027. ///
  1028. /// \pre setBasicBlock or setMI must have been called.
  1029. /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
  1030. /// with the same type.
  1031. /// \pre \p Tst must be a generic virtual register with scalar, pointer or
  1032. /// vector type. If vector then it must have the same number of
  1033. /// elements as the other parameters.
  1034. ///
  1035. /// \return a MachineInstrBuilder for the newly created instruction.
  1036. MachineInstrBuilder buildSelect(const DstOp &Res, const SrcOp &Tst,
  1037. const SrcOp &Op0, const SrcOp &Op1,
  1038. std::optional<unsigned> Flags = std::nullopt);
  1039. /// Build and insert \p Res = G_INSERT_VECTOR_ELT \p Val,
  1040. /// \p Elt, \p Idx
  1041. ///
  1042. /// \pre setBasicBlock or setMI must have been called.
  1043. /// \pre \p Res and \p Val must be a generic virtual register
  1044. // with the same vector type.
  1045. /// \pre \p Elt and \p Idx must be a generic virtual register
  1046. /// with scalar type.
  1047. ///
  1048. /// \return The newly created instruction.
  1049. MachineInstrBuilder buildInsertVectorElement(const DstOp &Res,
  1050. const SrcOp &Val,
  1051. const SrcOp &Elt,
  1052. const SrcOp &Idx);
  1053. /// Build and insert \p Res = G_EXTRACT_VECTOR_ELT \p Val, \p Idx
  1054. ///
  1055. /// \pre setBasicBlock or setMI must have been called.
  1056. /// \pre \p Res must be a generic virtual register with scalar type.
  1057. /// \pre \p Val must be a generic virtual register with vector type.
  1058. ///
  1059. /// \return The newly created instruction.
  1060. MachineInstrBuilder buildExtractVectorElementConstant(const DstOp &Res,
  1061. const SrcOp &Val,
  1062. const int Idx) {
  1063. return buildExtractVectorElement(Res, Val,
  1064. buildConstant(LLT::scalar(64), Idx));
  1065. }
  1066. /// Build and insert \p Res = G_EXTRACT_VECTOR_ELT \p Val, \p Idx
  1067. ///
  1068. /// \pre setBasicBlock or setMI must have been called.
  1069. /// \pre \p Res must be a generic virtual register with scalar type.
  1070. /// \pre \p Val must be a generic virtual register with vector type.
  1071. /// \pre \p Idx must be a generic virtual register with scalar type.
  1072. ///
  1073. /// \return The newly created instruction.
  1074. MachineInstrBuilder buildExtractVectorElement(const DstOp &Res,
  1075. const SrcOp &Val,
  1076. const SrcOp &Idx);
  1077. /// Build and insert `OldValRes<def>, SuccessRes<def> =
  1078. /// G_ATOMIC_CMPXCHG_WITH_SUCCESS Addr, CmpVal, NewVal, MMO`.
  1079. ///
  1080. /// Atomically replace the value at \p Addr with \p NewVal if it is currently
  1081. /// \p CmpVal otherwise leaves it unchanged. Puts the original value from \p
  1082. /// Addr in \p Res, along with an s1 indicating whether it was replaced.
  1083. ///
  1084. /// \pre setBasicBlock or setMI must have been called.
  1085. /// \pre \p OldValRes must be a generic virtual register of scalar type.
  1086. /// \pre \p SuccessRes must be a generic virtual register of scalar type. It
  1087. /// will be assigned 0 on failure and 1 on success.
  1088. /// \pre \p Addr must be a generic virtual register with pointer type.
  1089. /// \pre \p OldValRes, \p CmpVal, and \p NewVal must be generic virtual
  1090. /// registers of the same type.
  1091. ///
  1092. /// \return a MachineInstrBuilder for the newly created instruction.
  1093. MachineInstrBuilder
  1094. buildAtomicCmpXchgWithSuccess(Register OldValRes, Register SuccessRes,
  1095. Register Addr, Register CmpVal, Register NewVal,
  1096. MachineMemOperand &MMO);
  1097. /// Build and insert `OldValRes<def> = G_ATOMIC_CMPXCHG Addr, CmpVal, NewVal,
  1098. /// MMO`.
  1099. ///
  1100. /// Atomically replace the value at \p Addr with \p NewVal if it is currently
  1101. /// \p CmpVal otherwise leaves it unchanged. Puts the original value from \p
  1102. /// Addr in \p Res.
  1103. ///
  1104. /// \pre setBasicBlock or setMI must have been called.
  1105. /// \pre \p OldValRes must be a generic virtual register of scalar type.
  1106. /// \pre \p Addr must be a generic virtual register with pointer type.
  1107. /// \pre \p OldValRes, \p CmpVal, and \p NewVal must be generic virtual
  1108. /// registers of the same type.
  1109. ///
  1110. /// \return a MachineInstrBuilder for the newly created instruction.
  1111. MachineInstrBuilder buildAtomicCmpXchg(Register OldValRes, Register Addr,
  1112. Register CmpVal, Register NewVal,
  1113. MachineMemOperand &MMO);
  1114. /// Build and insert `OldValRes<def> = G_ATOMICRMW_<Opcode> Addr, Val, MMO`.
  1115. ///
  1116. /// Atomically read-modify-update the value at \p Addr with \p Val. Puts the
  1117. /// original value from \p Addr in \p OldValRes. The modification is
  1118. /// determined by the opcode.
  1119. ///
  1120. /// \pre setBasicBlock or setMI must have been called.
  1121. /// \pre \p OldValRes must be a generic virtual register.
  1122. /// \pre \p Addr must be a generic virtual register with pointer type.
  1123. /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
  1124. /// same type.
  1125. ///
  1126. /// \return a MachineInstrBuilder for the newly created instruction.
  1127. MachineInstrBuilder buildAtomicRMW(unsigned Opcode, const DstOp &OldValRes,
  1128. const SrcOp &Addr, const SrcOp &Val,
  1129. MachineMemOperand &MMO);
  1130. /// Build and insert `OldValRes<def> = G_ATOMICRMW_XCHG Addr, Val, MMO`.
  1131. ///
  1132. /// Atomically replace the value at \p Addr with \p Val. Puts the original
  1133. /// value from \p Addr in \p OldValRes.
  1134. ///
  1135. /// \pre setBasicBlock or setMI must have been called.
  1136. /// \pre \p OldValRes must be a generic virtual register.
  1137. /// \pre \p Addr must be a generic virtual register with pointer type.
  1138. /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
  1139. /// same type.
  1140. ///
  1141. /// \return a MachineInstrBuilder for the newly created instruction.
  1142. MachineInstrBuilder buildAtomicRMWXchg(Register OldValRes, Register Addr,
  1143. Register Val, MachineMemOperand &MMO);
  1144. /// Build and insert `OldValRes<def> = G_ATOMICRMW_ADD Addr, Val, MMO`.
  1145. ///
  1146. /// Atomically replace the value at \p Addr with the addition of \p Val and
  1147. /// the original value. Puts the original value from \p Addr in \p OldValRes.
  1148. ///
  1149. /// \pre setBasicBlock or setMI must have been called.
  1150. /// \pre \p OldValRes must be a generic virtual register.
  1151. /// \pre \p Addr must be a generic virtual register with pointer type.
  1152. /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
  1153. /// same type.
  1154. ///
  1155. /// \return a MachineInstrBuilder for the newly created instruction.
  1156. MachineInstrBuilder buildAtomicRMWAdd(Register OldValRes, Register Addr,
  1157. Register Val, MachineMemOperand &MMO);
  1158. /// Build and insert `OldValRes<def> = G_ATOMICRMW_SUB Addr, Val, MMO`.
  1159. ///
  1160. /// Atomically replace the value at \p Addr with the subtraction of \p Val and
  1161. /// the original value. Puts the original value from \p Addr in \p OldValRes.
  1162. ///
  1163. /// \pre setBasicBlock or setMI must have been called.
  1164. /// \pre \p OldValRes must be a generic virtual register.
  1165. /// \pre \p Addr must be a generic virtual register with pointer type.
  1166. /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
  1167. /// same type.
  1168. ///
  1169. /// \return a MachineInstrBuilder for the newly created instruction.
  1170. MachineInstrBuilder buildAtomicRMWSub(Register OldValRes, Register Addr,
  1171. Register Val, MachineMemOperand &MMO);
  1172. /// Build and insert `OldValRes<def> = G_ATOMICRMW_AND Addr, Val, MMO`.
  1173. ///
  1174. /// Atomically replace the value at \p Addr with the bitwise and of \p Val and
  1175. /// the original value. Puts the original value from \p Addr in \p OldValRes.
  1176. ///
  1177. /// \pre setBasicBlock or setMI must have been called.
  1178. /// \pre \p OldValRes must be a generic virtual register.
  1179. /// \pre \p Addr must be a generic virtual register with pointer type.
  1180. /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
  1181. /// same type.
  1182. ///
  1183. /// \return a MachineInstrBuilder for the newly created instruction.
  1184. MachineInstrBuilder buildAtomicRMWAnd(Register OldValRes, Register Addr,
  1185. Register Val, MachineMemOperand &MMO);
  1186. /// Build and insert `OldValRes<def> = G_ATOMICRMW_NAND Addr, Val, MMO`.
  1187. ///
  1188. /// Atomically replace the value at \p Addr with the bitwise nand of \p Val
  1189. /// and the original value. Puts the original value from \p Addr in \p
  1190. /// OldValRes.
  1191. ///
  1192. /// \pre setBasicBlock or setMI must have been called.
  1193. /// \pre \p OldValRes must be a generic virtual register.
  1194. /// \pre \p Addr must be a generic virtual register with pointer type.
  1195. /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
  1196. /// same type.
  1197. ///
  1198. /// \return a MachineInstrBuilder for the newly created instruction.
  1199. MachineInstrBuilder buildAtomicRMWNand(Register OldValRes, Register Addr,
  1200. Register Val, MachineMemOperand &MMO);
  1201. /// Build and insert `OldValRes<def> = G_ATOMICRMW_OR Addr, Val, MMO`.
  1202. ///
  1203. /// Atomically replace the value at \p Addr with the bitwise or of \p Val and
  1204. /// the original value. Puts the original value from \p Addr in \p OldValRes.
  1205. ///
  1206. /// \pre setBasicBlock or setMI must have been called.
  1207. /// \pre \p OldValRes must be a generic virtual register.
  1208. /// \pre \p Addr must be a generic virtual register with pointer type.
  1209. /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
  1210. /// same type.
  1211. ///
  1212. /// \return a MachineInstrBuilder for the newly created instruction.
  1213. MachineInstrBuilder buildAtomicRMWOr(Register OldValRes, Register Addr,
  1214. Register Val, MachineMemOperand &MMO);
  1215. /// Build and insert `OldValRes<def> = G_ATOMICRMW_XOR Addr, Val, MMO`.
  1216. ///
  1217. /// Atomically replace the value at \p Addr with the bitwise xor of \p Val and
  1218. /// the original value. Puts the original value from \p Addr in \p OldValRes.
  1219. ///
  1220. /// \pre setBasicBlock or setMI must have been called.
  1221. /// \pre \p OldValRes must be a generic virtual register.
  1222. /// \pre \p Addr must be a generic virtual register with pointer type.
  1223. /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
  1224. /// same type.
  1225. ///
  1226. /// \return a MachineInstrBuilder for the newly created instruction.
  1227. MachineInstrBuilder buildAtomicRMWXor(Register OldValRes, Register Addr,
  1228. Register Val, MachineMemOperand &MMO);
  1229. /// Build and insert `OldValRes<def> = G_ATOMICRMW_MAX Addr, Val, MMO`.
  1230. ///
  1231. /// Atomically replace the value at \p Addr with the signed maximum of \p
  1232. /// Val and the original value. Puts the original value from \p Addr in \p
  1233. /// OldValRes.
  1234. ///
  1235. /// \pre setBasicBlock or setMI must have been called.
  1236. /// \pre \p OldValRes must be a generic virtual register.
  1237. /// \pre \p Addr must be a generic virtual register with pointer type.
  1238. /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
  1239. /// same type.
  1240. ///
  1241. /// \return a MachineInstrBuilder for the newly created instruction.
  1242. MachineInstrBuilder buildAtomicRMWMax(Register OldValRes, Register Addr,
  1243. Register Val, MachineMemOperand &MMO);
  1244. /// Build and insert `OldValRes<def> = G_ATOMICRMW_MIN Addr, Val, MMO`.
  1245. ///
  1246. /// Atomically replace the value at \p Addr with the signed minimum of \p
  1247. /// Val and the original value. Puts the original value from \p Addr in \p
  1248. /// OldValRes.
  1249. ///
  1250. /// \pre setBasicBlock or setMI must have been called.
  1251. /// \pre \p OldValRes must be a generic virtual register.
  1252. /// \pre \p Addr must be a generic virtual register with pointer type.
  1253. /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
  1254. /// same type.
  1255. ///
  1256. /// \return a MachineInstrBuilder for the newly created instruction.
  1257. MachineInstrBuilder buildAtomicRMWMin(Register OldValRes, Register Addr,
  1258. Register Val, MachineMemOperand &MMO);
  1259. /// Build and insert `OldValRes<def> = G_ATOMICRMW_UMAX Addr, Val, MMO`.
  1260. ///
  1261. /// Atomically replace the value at \p Addr with the unsigned maximum of \p
  1262. /// Val and the original value. Puts the original value from \p Addr in \p
  1263. /// OldValRes.
  1264. ///
  1265. /// \pre setBasicBlock or setMI must have been called.
  1266. /// \pre \p OldValRes must be a generic virtual register.
  1267. /// \pre \p Addr must be a generic virtual register with pointer type.
  1268. /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
  1269. /// same type.
  1270. ///
  1271. /// \return a MachineInstrBuilder for the newly created instruction.
  1272. MachineInstrBuilder buildAtomicRMWUmax(Register OldValRes, Register Addr,
  1273. Register Val, MachineMemOperand &MMO);
  1274. /// Build and insert `OldValRes<def> = G_ATOMICRMW_UMIN Addr, Val, MMO`.
  1275. ///
  1276. /// Atomically replace the value at \p Addr with the unsigned minimum of \p
  1277. /// Val and the original value. Puts the original value from \p Addr in \p
  1278. /// OldValRes.
  1279. ///
  1280. /// \pre setBasicBlock or setMI must have been called.
  1281. /// \pre \p OldValRes must be a generic virtual register.
  1282. /// \pre \p Addr must be a generic virtual register with pointer type.
  1283. /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
  1284. /// same type.
  1285. ///
  1286. /// \return a MachineInstrBuilder for the newly created instruction.
  1287. MachineInstrBuilder buildAtomicRMWUmin(Register OldValRes, Register Addr,
  1288. Register Val, MachineMemOperand &MMO);
  1289. /// Build and insert `OldValRes<def> = G_ATOMICRMW_FADD Addr, Val, MMO`.
  1290. MachineInstrBuilder buildAtomicRMWFAdd(
  1291. const DstOp &OldValRes, const SrcOp &Addr, const SrcOp &Val,
  1292. MachineMemOperand &MMO);
  1293. /// Build and insert `OldValRes<def> = G_ATOMICRMW_FSUB Addr, Val, MMO`.
  1294. MachineInstrBuilder buildAtomicRMWFSub(
  1295. const DstOp &OldValRes, const SrcOp &Addr, const SrcOp &Val,
  1296. MachineMemOperand &MMO);
  1297. /// Build and insert `OldValRes<def> = G_ATOMICRMW_FMAX Addr, Val, MMO`.
  1298. ///
  1299. /// Atomically replace the value at \p Addr with the floating point maximum of
  1300. /// \p Val and the original value. Puts the original value from \p Addr in \p
  1301. /// OldValRes.
  1302. ///
  1303. /// \pre setBasicBlock or setMI must have been called.
  1304. /// \pre \p OldValRes must be a generic virtual register.
  1305. /// \pre \p Addr must be a generic virtual register with pointer type.
  1306. /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
  1307. /// same type.
  1308. ///
  1309. /// \return a MachineInstrBuilder for the newly created instruction.
  1310. MachineInstrBuilder buildAtomicRMWFMax(
  1311. const DstOp &OldValRes, const SrcOp &Addr, const SrcOp &Val,
  1312. MachineMemOperand &MMO);
  1313. /// Build and insert `OldValRes<def> = G_ATOMICRMW_FMIN Addr, Val, MMO`.
  1314. ///
  1315. /// Atomically replace the value at \p Addr with the floating point minimum of
  1316. /// \p Val and the original value. Puts the original value from \p Addr in \p
  1317. /// OldValRes.
  1318. ///
  1319. /// \pre setBasicBlock or setMI must have been called.
  1320. /// \pre \p OldValRes must be a generic virtual register.
  1321. /// \pre \p Addr must be a generic virtual register with pointer type.
  1322. /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
  1323. /// same type.
  1324. ///
  1325. /// \return a MachineInstrBuilder for the newly created instruction.
  1326. MachineInstrBuilder buildAtomicRMWFMin(
  1327. const DstOp &OldValRes, const SrcOp &Addr, const SrcOp &Val,
  1328. MachineMemOperand &MMO);
  1329. /// Build and insert `G_FENCE Ordering, Scope`.
  1330. MachineInstrBuilder buildFence(unsigned Ordering, unsigned Scope);
  1331. /// Build and insert \p Dst = G_FREEZE \p Src
  1332. MachineInstrBuilder buildFreeze(const DstOp &Dst, const SrcOp &Src) {
  1333. return buildInstr(TargetOpcode::G_FREEZE, {Dst}, {Src});
  1334. }
  1335. /// Build and insert \p Res = G_BLOCK_ADDR \p BA
  1336. ///
  1337. /// G_BLOCK_ADDR computes the address of a basic block.
  1338. ///
  1339. /// \pre setBasicBlock or setMI must have been called.
  1340. /// \pre \p Res must be a generic virtual register of a pointer type.
  1341. ///
  1342. /// \return The newly created instruction.
  1343. MachineInstrBuilder buildBlockAddress(Register Res, const BlockAddress *BA);
  1344. /// Build and insert \p Res = G_ADD \p Op0, \p Op1
  1345. ///
  1346. /// G_ADD sets \p Res to the sum of integer parameters \p Op0 and \p Op1,
  1347. /// truncated to their width.
  1348. ///
  1349. /// \pre setBasicBlock or setMI must have been called.
  1350. /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
  1351. /// with the same (scalar or vector) type).
  1352. ///
  1353. /// \return a MachineInstrBuilder for the newly created instruction.
  1354. MachineInstrBuilder buildAdd(const DstOp &Dst, const SrcOp &Src0,
  1355. const SrcOp &Src1,
  1356. std::optional<unsigned> Flags = std::nullopt) {
  1357. return buildInstr(TargetOpcode::G_ADD, {Dst}, {Src0, Src1}, Flags);
  1358. }
  1359. /// Build and insert \p Res = G_SUB \p Op0, \p Op1
  1360. ///
  1361. /// G_SUB sets \p Res to the difference of integer parameters \p Op0 and
  1362. /// \p Op1, truncated to their width.
  1363. ///
  1364. /// \pre setBasicBlock or setMI must have been called.
  1365. /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
  1366. /// with the same (scalar or vector) type).
  1367. ///
  1368. /// \return a MachineInstrBuilder for the newly created instruction.
  1369. MachineInstrBuilder buildSub(const DstOp &Dst, const SrcOp &Src0,
  1370. const SrcOp &Src1,
  1371. std::optional<unsigned> Flags = std::nullopt) {
  1372. return buildInstr(TargetOpcode::G_SUB, {Dst}, {Src0, Src1}, Flags);
  1373. }
  1374. /// Build and insert \p Res = G_MUL \p Op0, \p Op1
  1375. ///
  1376. /// G_MUL sets \p Res to the product of integer parameters \p Op0 and \p Op1,
  1377. /// truncated to their width.
  1378. ///
  1379. /// \pre setBasicBlock or setMI must have been called.
  1380. /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
  1381. /// with the same (scalar or vector) type).
  1382. ///
  1383. /// \return a MachineInstrBuilder for the newly created instruction.
  1384. MachineInstrBuilder buildMul(const DstOp &Dst, const SrcOp &Src0,
  1385. const SrcOp &Src1,
  1386. std::optional<unsigned> Flags = std::nullopt) {
  1387. return buildInstr(TargetOpcode::G_MUL, {Dst}, {Src0, Src1}, Flags);
  1388. }
  1389. MachineInstrBuilder buildUMulH(const DstOp &Dst, const SrcOp &Src0,
  1390. const SrcOp &Src1,
  1391. std::optional<unsigned> Flags = std::nullopt) {
  1392. return buildInstr(TargetOpcode::G_UMULH, {Dst}, {Src0, Src1}, Flags);
  1393. }
  1394. MachineInstrBuilder buildSMulH(const DstOp &Dst, const SrcOp &Src0,
  1395. const SrcOp &Src1,
  1396. std::optional<unsigned> Flags = std::nullopt) {
  1397. return buildInstr(TargetOpcode::G_SMULH, {Dst}, {Src0, Src1}, Flags);
  1398. }
  1399. /// Build and insert \p Res = G_UREM \p Op0, \p Op1
  1400. MachineInstrBuilder buildURem(const DstOp &Dst, const SrcOp &Src0,
  1401. const SrcOp &Src1,
  1402. std::optional<unsigned> Flags = std::nullopt) {
  1403. return buildInstr(TargetOpcode::G_UREM, {Dst}, {Src0, Src1}, Flags);
  1404. }
  1405. MachineInstrBuilder buildFMul(const DstOp &Dst, const SrcOp &Src0,
  1406. const SrcOp &Src1,
  1407. std::optional<unsigned> Flags = std::nullopt) {
  1408. return buildInstr(TargetOpcode::G_FMUL, {Dst}, {Src0, Src1}, Flags);
  1409. }
  1410. MachineInstrBuilder
  1411. buildFMinNum(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
  1412. std::optional<unsigned> Flags = std::nullopt) {
  1413. return buildInstr(TargetOpcode::G_FMINNUM, {Dst}, {Src0, Src1}, Flags);
  1414. }
  1415. MachineInstrBuilder
  1416. buildFMaxNum(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
  1417. std::optional<unsigned> Flags = std::nullopt) {
  1418. return buildInstr(TargetOpcode::G_FMAXNUM, {Dst}, {Src0, Src1}, Flags);
  1419. }
  1420. MachineInstrBuilder
  1421. buildFMinNumIEEE(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
  1422. std::optional<unsigned> Flags = std::nullopt) {
  1423. return buildInstr(TargetOpcode::G_FMINNUM_IEEE, {Dst}, {Src0, Src1}, Flags);
  1424. }
  1425. MachineInstrBuilder
  1426. buildFMaxNumIEEE(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
  1427. std::optional<unsigned> Flags = std::nullopt) {
  1428. return buildInstr(TargetOpcode::G_FMAXNUM_IEEE, {Dst}, {Src0, Src1}, Flags);
  1429. }
  1430. MachineInstrBuilder buildShl(const DstOp &Dst, const SrcOp &Src0,
  1431. const SrcOp &Src1,
  1432. std::optional<unsigned> Flags = std::nullopt) {
  1433. return buildInstr(TargetOpcode::G_SHL, {Dst}, {Src0, Src1}, Flags);
  1434. }
  1435. MachineInstrBuilder buildLShr(const DstOp &Dst, const SrcOp &Src0,
  1436. const SrcOp &Src1,
  1437. std::optional<unsigned> Flags = std::nullopt) {
  1438. return buildInstr(TargetOpcode::G_LSHR, {Dst}, {Src0, Src1}, Flags);
  1439. }
  1440. MachineInstrBuilder buildAShr(const DstOp &Dst, const SrcOp &Src0,
  1441. const SrcOp &Src1,
  1442. std::optional<unsigned> Flags = std::nullopt) {
  1443. return buildInstr(TargetOpcode::G_ASHR, {Dst}, {Src0, Src1}, Flags);
  1444. }
  1445. /// Build and insert \p Res = G_AND \p Op0, \p Op1
  1446. ///
  1447. /// G_AND sets \p Res to the bitwise and of integer parameters \p Op0 and \p
  1448. /// Op1.
  1449. ///
  1450. /// \pre setBasicBlock or setMI must have been called.
  1451. /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
  1452. /// with the same (scalar or vector) type).
  1453. ///
  1454. /// \return a MachineInstrBuilder for the newly created instruction.
  1455. MachineInstrBuilder buildAnd(const DstOp &Dst, const SrcOp &Src0,
  1456. const SrcOp &Src1) {
  1457. return buildInstr(TargetOpcode::G_AND, {Dst}, {Src0, Src1});
  1458. }
  1459. /// Build and insert \p Res = G_OR \p Op0, \p Op1
  1460. ///
  1461. /// G_OR sets \p Res to the bitwise or of integer parameters \p Op0 and \p
  1462. /// Op1.
  1463. ///
  1464. /// \pre setBasicBlock or setMI must have been called.
  1465. /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
  1466. /// with the same (scalar or vector) type).
  1467. ///
  1468. /// \return a MachineInstrBuilder for the newly created instruction.
  1469. MachineInstrBuilder buildOr(const DstOp &Dst, const SrcOp &Src0,
  1470. const SrcOp &Src1,
  1471. std::optional<unsigned> Flags = std::nullopt) {
  1472. return buildInstr(TargetOpcode::G_OR, {Dst}, {Src0, Src1}, Flags);
  1473. }
  1474. /// Build and insert \p Res = G_XOR \p Op0, \p Op1
  1475. MachineInstrBuilder buildXor(const DstOp &Dst, const SrcOp &Src0,
  1476. const SrcOp &Src1) {
  1477. return buildInstr(TargetOpcode::G_XOR, {Dst}, {Src0, Src1});
  1478. }
  1479. /// Build and insert a bitwise not,
  1480. /// \p NegOne = G_CONSTANT -1
  1481. /// \p Res = G_OR \p Op0, NegOne
  1482. MachineInstrBuilder buildNot(const DstOp &Dst, const SrcOp &Src0) {
  1483. auto NegOne = buildConstant(Dst.getLLTTy(*getMRI()), -1);
  1484. return buildInstr(TargetOpcode::G_XOR, {Dst}, {Src0, NegOne});
  1485. }
  1486. /// Build and insert integer negation
  1487. /// \p Zero = G_CONSTANT 0
  1488. /// \p Res = G_SUB Zero, \p Op0
  1489. MachineInstrBuilder buildNeg(const DstOp &Dst, const SrcOp &Src0) {
  1490. auto Zero = buildConstant(Dst.getLLTTy(*getMRI()), 0);
  1491. return buildInstr(TargetOpcode::G_SUB, {Dst}, {Zero, Src0});
  1492. }
  1493. /// Build and insert \p Res = G_CTPOP \p Op0, \p Src0
  1494. MachineInstrBuilder buildCTPOP(const DstOp &Dst, const SrcOp &Src0) {
  1495. return buildInstr(TargetOpcode::G_CTPOP, {Dst}, {Src0});
  1496. }
  1497. /// Build and insert \p Res = G_CTLZ \p Op0, \p Src0
  1498. MachineInstrBuilder buildCTLZ(const DstOp &Dst, const SrcOp &Src0) {
  1499. return buildInstr(TargetOpcode::G_CTLZ, {Dst}, {Src0});
  1500. }
  1501. /// Build and insert \p Res = G_CTLZ_ZERO_UNDEF \p Op0, \p Src0
  1502. MachineInstrBuilder buildCTLZ_ZERO_UNDEF(const DstOp &Dst, const SrcOp &Src0) {
  1503. return buildInstr(TargetOpcode::G_CTLZ_ZERO_UNDEF, {Dst}, {Src0});
  1504. }
  1505. /// Build and insert \p Res = G_CTTZ \p Op0, \p Src0
  1506. MachineInstrBuilder buildCTTZ(const DstOp &Dst, const SrcOp &Src0) {
  1507. return buildInstr(TargetOpcode::G_CTTZ, {Dst}, {Src0});
  1508. }
  1509. /// Build and insert \p Res = G_CTTZ_ZERO_UNDEF \p Op0, \p Src0
  1510. MachineInstrBuilder buildCTTZ_ZERO_UNDEF(const DstOp &Dst, const SrcOp &Src0) {
  1511. return buildInstr(TargetOpcode::G_CTTZ_ZERO_UNDEF, {Dst}, {Src0});
  1512. }
  1513. /// Build and insert \p Dst = G_BSWAP \p Src0
  1514. MachineInstrBuilder buildBSwap(const DstOp &Dst, const SrcOp &Src0) {
  1515. return buildInstr(TargetOpcode::G_BSWAP, {Dst}, {Src0});
  1516. }
  1517. /// Build and insert \p Res = G_FADD \p Op0, \p Op1
  1518. MachineInstrBuilder buildFAdd(const DstOp &Dst, const SrcOp &Src0,
  1519. const SrcOp &Src1,
  1520. std::optional<unsigned> Flags = std::nullopt) {
  1521. return buildInstr(TargetOpcode::G_FADD, {Dst}, {Src0, Src1}, Flags);
  1522. }
  1523. /// Build and insert \p Res = G_STRICT_FADD \p Op0, \p Op1
  1524. MachineInstrBuilder
  1525. buildStrictFAdd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
  1526. std::optional<unsigned> Flags = std::nullopt) {
  1527. return buildInstr(TargetOpcode::G_STRICT_FADD, {Dst}, {Src0, Src1}, Flags);
  1528. }
  1529. /// Build and insert \p Res = G_FSUB \p Op0, \p Op1
  1530. MachineInstrBuilder buildFSub(const DstOp &Dst, const SrcOp &Src0,
  1531. const SrcOp &Src1,
  1532. std::optional<unsigned> Flags = std::nullopt) {
  1533. return buildInstr(TargetOpcode::G_FSUB, {Dst}, {Src0, Src1}, Flags);
  1534. }
  1535. /// Build and insert \p Res = G_FDIV \p Op0, \p Op1
  1536. MachineInstrBuilder buildFDiv(const DstOp &Dst, const SrcOp &Src0,
  1537. const SrcOp &Src1,
  1538. std::optional<unsigned> Flags = std::nullopt) {
  1539. return buildInstr(TargetOpcode::G_FDIV, {Dst}, {Src0, Src1}, Flags);
  1540. }
  1541. /// Build and insert \p Res = G_FMA \p Op0, \p Op1, \p Op2
  1542. MachineInstrBuilder buildFMA(const DstOp &Dst, const SrcOp &Src0,
  1543. const SrcOp &Src1, const SrcOp &Src2,
  1544. std::optional<unsigned> Flags = std::nullopt) {
  1545. return buildInstr(TargetOpcode::G_FMA, {Dst}, {Src0, Src1, Src2}, Flags);
  1546. }
  1547. /// Build and insert \p Res = G_FMAD \p Op0, \p Op1, \p Op2
  1548. MachineInstrBuilder buildFMAD(const DstOp &Dst, const SrcOp &Src0,
  1549. const SrcOp &Src1, const SrcOp &Src2,
  1550. std::optional<unsigned> Flags = std::nullopt) {
  1551. return buildInstr(TargetOpcode::G_FMAD, {Dst}, {Src0, Src1, Src2}, Flags);
  1552. }
  1553. /// Build and insert \p Res = G_FNEG \p Op0
  1554. MachineInstrBuilder buildFNeg(const DstOp &Dst, const SrcOp &Src0,
  1555. std::optional<unsigned> Flags = std::nullopt) {
  1556. return buildInstr(TargetOpcode::G_FNEG, {Dst}, {Src0}, Flags);
  1557. }
  1558. /// Build and insert \p Res = G_FABS \p Op0
  1559. MachineInstrBuilder buildFAbs(const DstOp &Dst, const SrcOp &Src0,
  1560. std::optional<unsigned> Flags = std::nullopt) {
  1561. return buildInstr(TargetOpcode::G_FABS, {Dst}, {Src0}, Flags);
  1562. }
  1563. /// Build and insert \p Dst = G_FCANONICALIZE \p Src0
  1564. MachineInstrBuilder
  1565. buildFCanonicalize(const DstOp &Dst, const SrcOp &Src0,
  1566. std::optional<unsigned> Flags = std::nullopt) {
  1567. return buildInstr(TargetOpcode::G_FCANONICALIZE, {Dst}, {Src0}, Flags);
  1568. }
  1569. /// Build and insert \p Dst = G_INTRINSIC_TRUNC \p Src0
  1570. MachineInstrBuilder
  1571. buildIntrinsicTrunc(const DstOp &Dst, const SrcOp &Src0,
  1572. std::optional<unsigned> Flags = std::nullopt) {
  1573. return buildInstr(TargetOpcode::G_INTRINSIC_TRUNC, {Dst}, {Src0}, Flags);
  1574. }
  1575. /// Build and insert \p Res = GFFLOOR \p Op0, \p Op1
  1576. MachineInstrBuilder
  1577. buildFFloor(const DstOp &Dst, const SrcOp &Src0,
  1578. std::optional<unsigned> Flags = std::nullopt) {
  1579. return buildInstr(TargetOpcode::G_FFLOOR, {Dst}, {Src0}, Flags);
  1580. }
  1581. /// Build and insert \p Dst = G_FLOG \p Src
  1582. MachineInstrBuilder buildFLog(const DstOp &Dst, const SrcOp &Src,
  1583. std::optional<unsigned> Flags = std::nullopt) {
  1584. return buildInstr(TargetOpcode::G_FLOG, {Dst}, {Src}, Flags);
  1585. }
  1586. /// Build and insert \p Dst = G_FLOG2 \p Src
  1587. MachineInstrBuilder buildFLog2(const DstOp &Dst, const SrcOp &Src,
  1588. std::optional<unsigned> Flags = std::nullopt) {
  1589. return buildInstr(TargetOpcode::G_FLOG2, {Dst}, {Src}, Flags);
  1590. }
  1591. /// Build and insert \p Dst = G_FEXP2 \p Src
  1592. MachineInstrBuilder buildFExp2(const DstOp &Dst, const SrcOp &Src,
  1593. std::optional<unsigned> Flags = std::nullopt) {
  1594. return buildInstr(TargetOpcode::G_FEXP2, {Dst}, {Src}, Flags);
  1595. }
  1596. /// Build and insert \p Dst = G_FPOW \p Src0, \p Src1
  1597. MachineInstrBuilder buildFPow(const DstOp &Dst, const SrcOp &Src0,
  1598. const SrcOp &Src1,
  1599. std::optional<unsigned> Flags = std::nullopt) {
  1600. return buildInstr(TargetOpcode::G_FPOW, {Dst}, {Src0, Src1}, Flags);
  1601. }
  1602. /// Build and insert \p Res = G_FCOPYSIGN \p Op0, \p Op1
  1603. MachineInstrBuilder buildFCopysign(const DstOp &Dst, const SrcOp &Src0,
  1604. const SrcOp &Src1) {
  1605. return buildInstr(TargetOpcode::G_FCOPYSIGN, {Dst}, {Src0, Src1});
  1606. }
  1607. /// Build and insert \p Res = G_UITOFP \p Src0
  1608. MachineInstrBuilder buildUITOFP(const DstOp &Dst, const SrcOp &Src0) {
  1609. return buildInstr(TargetOpcode::G_UITOFP, {Dst}, {Src0});
  1610. }
  1611. /// Build and insert \p Res = G_SITOFP \p Src0
  1612. MachineInstrBuilder buildSITOFP(const DstOp &Dst, const SrcOp &Src0) {
  1613. return buildInstr(TargetOpcode::G_SITOFP, {Dst}, {Src0});
  1614. }
  1615. /// Build and insert \p Res = G_FPTOUI \p Src0
  1616. MachineInstrBuilder buildFPTOUI(const DstOp &Dst, const SrcOp &Src0) {
  1617. return buildInstr(TargetOpcode::G_FPTOUI, {Dst}, {Src0});
  1618. }
  1619. /// Build and insert \p Res = G_FPTOSI \p Src0
  1620. MachineInstrBuilder buildFPTOSI(const DstOp &Dst, const SrcOp &Src0) {
  1621. return buildInstr(TargetOpcode::G_FPTOSI, {Dst}, {Src0});
  1622. }
  1623. /// Build and insert \p Res = G_SMIN \p Op0, \p Op1
  1624. MachineInstrBuilder buildSMin(const DstOp &Dst, const SrcOp &Src0,
  1625. const SrcOp &Src1) {
  1626. return buildInstr(TargetOpcode::G_SMIN, {Dst}, {Src0, Src1});
  1627. }
  1628. /// Build and insert \p Res = G_SMAX \p Op0, \p Op1
  1629. MachineInstrBuilder buildSMax(const DstOp &Dst, const SrcOp &Src0,
  1630. const SrcOp &Src1) {
  1631. return buildInstr(TargetOpcode::G_SMAX, {Dst}, {Src0, Src1});
  1632. }
  1633. /// Build and insert \p Res = G_UMIN \p Op0, \p Op1
  1634. MachineInstrBuilder buildUMin(const DstOp &Dst, const SrcOp &Src0,
  1635. const SrcOp &Src1) {
  1636. return buildInstr(TargetOpcode::G_UMIN, {Dst}, {Src0, Src1});
  1637. }
  1638. /// Build and insert \p Res = G_UMAX \p Op0, \p Op1
  1639. MachineInstrBuilder buildUMax(const DstOp &Dst, const SrcOp &Src0,
  1640. const SrcOp &Src1) {
  1641. return buildInstr(TargetOpcode::G_UMAX, {Dst}, {Src0, Src1});
  1642. }
  1643. /// Build and insert \p Dst = G_ABS \p Src
  1644. MachineInstrBuilder buildAbs(const DstOp &Dst, const SrcOp &Src) {
  1645. return buildInstr(TargetOpcode::G_ABS, {Dst}, {Src});
  1646. }
  1647. /// Build and insert \p Res = G_JUMP_TABLE \p JTI
  1648. ///
  1649. /// G_JUMP_TABLE sets \p Res to the address of the jump table specified by
  1650. /// the jump table index \p JTI.
  1651. ///
  1652. /// \return a MachineInstrBuilder for the newly created instruction.
  1653. MachineInstrBuilder buildJumpTable(const LLT PtrTy, unsigned JTI);
  1654. /// Build and insert \p Res = G_VECREDUCE_SEQ_FADD \p ScalarIn, \p VecIn
  1655. ///
  1656. /// \p ScalarIn is the scalar accumulator input to start the sequential
  1657. /// reduction operation of \p VecIn.
  1658. MachineInstrBuilder buildVecReduceSeqFAdd(const DstOp &Dst,
  1659. const SrcOp &ScalarIn,
  1660. const SrcOp &VecIn) {
  1661. return buildInstr(TargetOpcode::G_VECREDUCE_SEQ_FADD, {Dst},
  1662. {ScalarIn, {VecIn}});
  1663. }
  1664. /// Build and insert \p Res = G_VECREDUCE_SEQ_FMUL \p ScalarIn, \p VecIn
  1665. ///
  1666. /// \p ScalarIn is the scalar accumulator input to start the sequential
  1667. /// reduction operation of \p VecIn.
  1668. MachineInstrBuilder buildVecReduceSeqFMul(const DstOp &Dst,
  1669. const SrcOp &ScalarIn,
  1670. const SrcOp &VecIn) {
  1671. return buildInstr(TargetOpcode::G_VECREDUCE_SEQ_FMUL, {Dst},
  1672. {ScalarIn, {VecIn}});
  1673. }
  1674. /// Build and insert \p Res = G_VECREDUCE_FADD \p Src
  1675. ///
  1676. /// \p ScalarIn is the scalar accumulator input to the reduction operation of
  1677. /// \p VecIn.
  1678. MachineInstrBuilder buildVecReduceFAdd(const DstOp &Dst,
  1679. const SrcOp &ScalarIn,
  1680. const SrcOp &VecIn) {
  1681. return buildInstr(TargetOpcode::G_VECREDUCE_FADD, {Dst}, {ScalarIn, VecIn});
  1682. }
  1683. /// Build and insert \p Res = G_VECREDUCE_FMUL \p Src
  1684. ///
  1685. /// \p ScalarIn is the scalar accumulator input to the reduction operation of
  1686. /// \p VecIn.
  1687. MachineInstrBuilder buildVecReduceFMul(const DstOp &Dst,
  1688. const SrcOp &ScalarIn,
  1689. const SrcOp &VecIn) {
  1690. return buildInstr(TargetOpcode::G_VECREDUCE_FMUL, {Dst}, {ScalarIn, VecIn});
  1691. }
  1692. /// Build and insert \p Res = G_VECREDUCE_FMAX \p Src
  1693. MachineInstrBuilder buildVecReduceFMax(const DstOp &Dst, const SrcOp &Src) {
  1694. return buildInstr(TargetOpcode::G_VECREDUCE_FMAX, {Dst}, {Src});
  1695. }
  1696. /// Build and insert \p Res = G_VECREDUCE_FMIN \p Src
  1697. MachineInstrBuilder buildVecReduceFMin(const DstOp &Dst, const SrcOp &Src) {
  1698. return buildInstr(TargetOpcode::G_VECREDUCE_FMIN, {Dst}, {Src});
  1699. }
  1700. /// Build and insert \p Res = G_VECREDUCE_ADD \p Src
  1701. MachineInstrBuilder buildVecReduceAdd(const DstOp &Dst, const SrcOp &Src) {
  1702. return buildInstr(TargetOpcode::G_VECREDUCE_ADD, {Dst}, {Src});
  1703. }
  1704. /// Build and insert \p Res = G_VECREDUCE_MUL \p Src
  1705. MachineInstrBuilder buildVecReduceMul(const DstOp &Dst, const SrcOp &Src) {
  1706. return buildInstr(TargetOpcode::G_VECREDUCE_MUL, {Dst}, {Src});
  1707. }
  1708. /// Build and insert \p Res = G_VECREDUCE_AND \p Src
  1709. MachineInstrBuilder buildVecReduceAnd(const DstOp &Dst, const SrcOp &Src) {
  1710. return buildInstr(TargetOpcode::G_VECREDUCE_AND, {Dst}, {Src});
  1711. }
  1712. /// Build and insert \p Res = G_VECREDUCE_OR \p Src
  1713. MachineInstrBuilder buildVecReduceOr(const DstOp &Dst, const SrcOp &Src) {
  1714. return buildInstr(TargetOpcode::G_VECREDUCE_OR, {Dst}, {Src});
  1715. }
  1716. /// Build and insert \p Res = G_VECREDUCE_XOR \p Src
  1717. MachineInstrBuilder buildVecReduceXor(const DstOp &Dst, const SrcOp &Src) {
  1718. return buildInstr(TargetOpcode::G_VECREDUCE_XOR, {Dst}, {Src});
  1719. }
  1720. /// Build and insert \p Res = G_VECREDUCE_SMAX \p Src
  1721. MachineInstrBuilder buildVecReduceSMax(const DstOp &Dst, const SrcOp &Src) {
  1722. return buildInstr(TargetOpcode::G_VECREDUCE_SMAX, {Dst}, {Src});
  1723. }
  1724. /// Build and insert \p Res = G_VECREDUCE_SMIN \p Src
  1725. MachineInstrBuilder buildVecReduceSMin(const DstOp &Dst, const SrcOp &Src) {
  1726. return buildInstr(TargetOpcode::G_VECREDUCE_SMIN, {Dst}, {Src});
  1727. }
  1728. /// Build and insert \p Res = G_VECREDUCE_UMAX \p Src
  1729. MachineInstrBuilder buildVecReduceUMax(const DstOp &Dst, const SrcOp &Src) {
  1730. return buildInstr(TargetOpcode::G_VECREDUCE_UMAX, {Dst}, {Src});
  1731. }
  1732. /// Build and insert \p Res = G_VECREDUCE_UMIN \p Src
  1733. MachineInstrBuilder buildVecReduceUMin(const DstOp &Dst, const SrcOp &Src) {
  1734. return buildInstr(TargetOpcode::G_VECREDUCE_UMIN, {Dst}, {Src});
  1735. }
  1736. /// Build and insert G_MEMCPY or G_MEMMOVE
  1737. MachineInstrBuilder buildMemTransferInst(unsigned Opcode, const SrcOp &DstPtr,
  1738. const SrcOp &SrcPtr,
  1739. const SrcOp &Size,
  1740. MachineMemOperand &DstMMO,
  1741. MachineMemOperand &SrcMMO) {
  1742. auto MIB = buildInstr(
  1743. Opcode, {}, {DstPtr, SrcPtr, Size, SrcOp(INT64_C(0) /*isTailCall*/)});
  1744. MIB.addMemOperand(&DstMMO);
  1745. MIB.addMemOperand(&SrcMMO);
  1746. return MIB;
  1747. }
  1748. MachineInstrBuilder buildMemCpy(const SrcOp &DstPtr, const SrcOp &SrcPtr,
  1749. const SrcOp &Size, MachineMemOperand &DstMMO,
  1750. MachineMemOperand &SrcMMO) {
  1751. return buildMemTransferInst(TargetOpcode::G_MEMCPY, DstPtr, SrcPtr, Size,
  1752. DstMMO, SrcMMO);
  1753. }
  1754. /// Build and insert \p Dst = G_SBFX \p Src, \p LSB, \p Width.
  1755. MachineInstrBuilder buildSbfx(const DstOp &Dst, const SrcOp &Src,
  1756. const SrcOp &LSB, const SrcOp &Width) {
  1757. return buildInstr(TargetOpcode::G_SBFX, {Dst}, {Src, LSB, Width});
  1758. }
  1759. /// Build and insert \p Dst = G_UBFX \p Src, \p LSB, \p Width.
  1760. MachineInstrBuilder buildUbfx(const DstOp &Dst, const SrcOp &Src,
  1761. const SrcOp &LSB, const SrcOp &Width) {
  1762. return buildInstr(TargetOpcode::G_UBFX, {Dst}, {Src, LSB, Width});
  1763. }
  1764. /// Build and insert \p Dst = G_ROTR \p Src, \p Amt
  1765. MachineInstrBuilder buildRotateRight(const DstOp &Dst, const SrcOp &Src,
  1766. const SrcOp &Amt) {
  1767. return buildInstr(TargetOpcode::G_ROTR, {Dst}, {Src, Amt});
  1768. }
  1769. /// Build and insert \p Dst = G_ROTL \p Src, \p Amt
  1770. MachineInstrBuilder buildRotateLeft(const DstOp &Dst, const SrcOp &Src,
  1771. const SrcOp &Amt) {
  1772. return buildInstr(TargetOpcode::G_ROTL, {Dst}, {Src, Amt});
  1773. }
  1774. /// Build and insert \p Dst = G_BITREVERSE \p Src
  1775. MachineInstrBuilder buildBitReverse(const DstOp &Dst, const SrcOp &Src) {
  1776. return buildInstr(TargetOpcode::G_BITREVERSE, {Dst}, {Src});
  1777. }
  1778. virtual MachineInstrBuilder
  1779. buildInstr(unsigned Opc, ArrayRef<DstOp> DstOps, ArrayRef<SrcOp> SrcOps,
  1780. std::optional<unsigned> Flags = std::nullopt);
  1781. };
  1782. } // End namespace llvm.
  1783. #endif // LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
  1784. #ifdef __GNUC__
  1785. #pragma GCC diagnostic pop
  1786. #endif