MachineIRBuilder.h 80 KB

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