xref: /freebsd/contrib/llvm-project/llvm/include/llvm/CodeGen/GlobalISel/MachineIRBuilder.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===-- llvm/CodeGen/GlobalISel/MachineIRBuilder.h - MIBuilder --*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This file declares the MachineIRBuilder class.
10 /// This is a helper class to build MachineInstr.
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
14 #define LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
15 
16 #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
17 #include "llvm/CodeGen/MachineBasicBlock.h"
18 #include "llvm/CodeGen/MachineInstrBuilder.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/CodeGen/TargetLowering.h"
21 #include "llvm/CodeGen/TargetOpcodes.h"
22 #include "llvm/IR/DebugLoc.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/Support/Compiler.h"
25 
26 namespace llvm {
27 
28 // Forward declarations.
29 class APInt;
30 class BlockAddress;
31 class Constant;
32 class ConstantFP;
33 class ConstantInt;
34 class DataLayout;
35 class GISelCSEInfo;
36 class GlobalValue;
37 class TargetRegisterClass;
38 class MachineFunction;
39 class MachineInstr;
40 class TargetInstrInfo;
41 class GISelChangeObserver;
42 
43 /// Class which stores all the state required in a MachineIRBuilder.
44 /// Since MachineIRBuilders will only store state in this object, it allows
45 /// to transfer BuilderState between different kinds of MachineIRBuilders.
46 struct MachineIRBuilderState {
47   /// MachineFunction under construction.
48   MachineFunction *MF = nullptr;
49   /// Information used to access the description of the opcodes.
50   const TargetInstrInfo *TII = nullptr;
51   /// Information used to verify types are consistent and to create virtual registers.
52   MachineRegisterInfo *MRI = nullptr;
53   /// Debug location to be set to any instruction we create.
54   DebugLoc DL;
55   /// PC sections metadata to be set to any instruction we create.
56   MDNode *PCSections = nullptr;
57   /// MMRA Metadata to be set on any instruction we create.
58   MDNode *MMRA = nullptr;
59 
60   /// \name Fields describing the insertion point.
61   /// @{
62   MachineBasicBlock *MBB = nullptr;
63   MachineBasicBlock::iterator II;
64   /// @}
65 
66   GISelChangeObserver *Observer = nullptr;
67 
68   GISelCSEInfo *CSEInfo = nullptr;
69 };
70 
71 class DstOp {
72   union {
73     LLT LLTTy;
74     Register Reg;
75     const TargetRegisterClass *RC;
76     MachineRegisterInfo::VRegAttrs Attrs;
77   };
78 
79 public:
80   enum class DstType { Ty_LLT, Ty_Reg, Ty_RC, Ty_VRegAttrs };
DstOp(unsigned R)81   DstOp(unsigned R) : Reg(R), Ty(DstType::Ty_Reg) {}
DstOp(Register R)82   DstOp(Register R) : Reg(R), Ty(DstType::Ty_Reg) {}
DstOp(const MachineOperand & Op)83   DstOp(const MachineOperand &Op) : Reg(Op.getReg()), Ty(DstType::Ty_Reg) {}
DstOp(const LLT T)84   DstOp(const LLT T) : LLTTy(T), Ty(DstType::Ty_LLT) {}
DstOp(const TargetRegisterClass * TRC)85   DstOp(const TargetRegisterClass *TRC) : RC(TRC), Ty(DstType::Ty_RC) {}
DstOp(MachineRegisterInfo::VRegAttrs Attrs)86   DstOp(MachineRegisterInfo::VRegAttrs Attrs)
87       : Attrs(Attrs), Ty(DstType::Ty_VRegAttrs) {}
DstOp(RegClassOrRegBank RCOrRB,LLT Ty)88   DstOp(RegClassOrRegBank RCOrRB, LLT Ty)
89       : Attrs({RCOrRB, Ty}), Ty(DstType::Ty_VRegAttrs) {}
90 
addDefToMIB(MachineRegisterInfo & MRI,MachineInstrBuilder & MIB)91   void addDefToMIB(MachineRegisterInfo &MRI, MachineInstrBuilder &MIB) const {
92     switch (Ty) {
93     case DstType::Ty_Reg:
94       MIB.addDef(Reg);
95       break;
96     case DstType::Ty_LLT:
97       MIB.addDef(MRI.createGenericVirtualRegister(LLTTy));
98       break;
99     case DstType::Ty_RC:
100       MIB.addDef(MRI.createVirtualRegister(RC));
101       break;
102     case DstType::Ty_VRegAttrs:
103       MIB.addDef(MRI.createVirtualRegister(Attrs));
104       break;
105     }
106   }
107 
getLLTTy(const MachineRegisterInfo & MRI)108   LLT getLLTTy(const MachineRegisterInfo &MRI) const {
109     switch (Ty) {
110     case DstType::Ty_RC:
111       return LLT{};
112     case DstType::Ty_LLT:
113       return LLTTy;
114     case DstType::Ty_Reg:
115       return MRI.getType(Reg);
116     case DstType::Ty_VRegAttrs:
117       return Attrs.Ty;
118     }
119     llvm_unreachable("Unrecognised DstOp::DstType enum");
120   }
121 
getReg()122   Register getReg() const {
123     assert(Ty == DstType::Ty_Reg && "Not a register");
124     return Reg;
125   }
126 
getRegClass()127   const TargetRegisterClass *getRegClass() const {
128     assert(Ty == DstType::Ty_RC && "Not a RC Operand");
129     return RC;
130   }
131 
getVRegAttrs()132   MachineRegisterInfo::VRegAttrs getVRegAttrs() const {
133     assert(Ty == DstType::Ty_VRegAttrs && "Not a VRegAttrs Operand");
134     return Attrs;
135   }
136 
getDstOpKind()137   DstType getDstOpKind() const { return Ty; }
138 
139 private:
140   DstType Ty;
141 };
142 
143 class SrcOp {
144   union {
145     MachineInstrBuilder SrcMIB;
146     Register Reg;
147     CmpInst::Predicate Pred;
148     int64_t Imm;
149   };
150 
151 public:
152   enum class SrcType { Ty_Reg, Ty_MIB, Ty_Predicate, Ty_Imm };
SrcOp(Register R)153   SrcOp(Register R) : Reg(R), Ty(SrcType::Ty_Reg) {}
SrcOp(const MachineOperand & Op)154   SrcOp(const MachineOperand &Op) : Reg(Op.getReg()), Ty(SrcType::Ty_Reg) {}
SrcOp(const MachineInstrBuilder & MIB)155   SrcOp(const MachineInstrBuilder &MIB) : SrcMIB(MIB), Ty(SrcType::Ty_MIB) {}
SrcOp(const CmpInst::Predicate P)156   SrcOp(const CmpInst::Predicate P) : Pred(P), Ty(SrcType::Ty_Predicate) {}
157   /// Use of registers held in unsigned integer variables (or more rarely signed
158   /// integers) is no longer permitted to avoid ambiguity with upcoming support
159   /// for immediates.
160   SrcOp(unsigned) = delete;
161   SrcOp(int) = delete;
SrcOp(uint64_t V)162   SrcOp(uint64_t V) : Imm(V), Ty(SrcType::Ty_Imm) {}
SrcOp(int64_t V)163   SrcOp(int64_t V) : Imm(V), Ty(SrcType::Ty_Imm) {}
164 
addSrcToMIB(MachineInstrBuilder & MIB)165   void addSrcToMIB(MachineInstrBuilder &MIB) const {
166     switch (Ty) {
167     case SrcType::Ty_Predicate:
168       MIB.addPredicate(Pred);
169       break;
170     case SrcType::Ty_Reg:
171       MIB.addUse(Reg);
172       break;
173     case SrcType::Ty_MIB:
174       MIB.addUse(SrcMIB->getOperand(0).getReg());
175       break;
176     case SrcType::Ty_Imm:
177       MIB.addImm(Imm);
178       break;
179     }
180   }
181 
getLLTTy(const MachineRegisterInfo & MRI)182   LLT getLLTTy(const MachineRegisterInfo &MRI) const {
183     switch (Ty) {
184     case SrcType::Ty_Predicate:
185     case SrcType::Ty_Imm:
186       llvm_unreachable("Not a register operand");
187     case SrcType::Ty_Reg:
188       return MRI.getType(Reg);
189     case SrcType::Ty_MIB:
190       return MRI.getType(SrcMIB->getOperand(0).getReg());
191     }
192     llvm_unreachable("Unrecognised SrcOp::SrcType enum");
193   }
194 
getReg()195   Register getReg() const {
196     switch (Ty) {
197     case SrcType::Ty_Predicate:
198     case SrcType::Ty_Imm:
199       llvm_unreachable("Not a register operand");
200     case SrcType::Ty_Reg:
201       return Reg;
202     case SrcType::Ty_MIB:
203       return SrcMIB->getOperand(0).getReg();
204     }
205     llvm_unreachable("Unrecognised SrcOp::SrcType enum");
206   }
207 
getPredicate()208   CmpInst::Predicate getPredicate() const {
209     switch (Ty) {
210     case SrcType::Ty_Predicate:
211       return Pred;
212     default:
213       llvm_unreachable("Not a register operand");
214     }
215   }
216 
getImm()217   int64_t getImm() const {
218     switch (Ty) {
219     case SrcType::Ty_Imm:
220       return Imm;
221     default:
222       llvm_unreachable("Not an immediate");
223     }
224   }
225 
getSrcOpKind()226   SrcType getSrcOpKind() const { return Ty; }
227 
228 private:
229   SrcType Ty;
230 };
231 
232 /// Helper class to build MachineInstr.
233 /// It keeps internally the insertion point and debug location for all
234 /// the new instructions we want to create.
235 /// This information can be modified via the related setters.
236 class LLVM_ABI MachineIRBuilder {
237 
238   MachineIRBuilderState State;
239 
240   unsigned getOpcodeForMerge(const DstOp &DstOp, ArrayRef<SrcOp> SrcOps) const;
241 
242 protected:
243   void validateTruncExt(const LLT Dst, const LLT Src, bool IsExtend);
244 
245   void validateUnaryOp(const LLT Res, const LLT Op0);
246   void validateBinaryOp(const LLT Res, const LLT Op0, const LLT Op1);
247   void validateShiftOp(const LLT Res, const LLT Op0, const LLT Op1);
248 
249   void validateSelectOp(const LLT ResTy, const LLT TstTy, const LLT Op0Ty,
250                         const LLT Op1Ty);
251 
recordInsertion(MachineInstr * InsertedInstr)252   void recordInsertion(MachineInstr *InsertedInstr) const {
253     if (State.Observer)
254       State.Observer->createdInstr(*InsertedInstr);
255   }
256 
257 public:
258   /// Some constructors for easy use.
259   MachineIRBuilder() = default;
MachineIRBuilder(MachineFunction & MF)260   MachineIRBuilder(MachineFunction &MF) { setMF(MF); }
261 
MachineIRBuilder(MachineBasicBlock & MBB,MachineBasicBlock::iterator InsPt)262   MachineIRBuilder(MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt) {
263     setMF(*MBB.getParent());
264     setInsertPt(MBB, InsPt);
265   }
266 
MachineIRBuilder(MachineInstr & MI)267   MachineIRBuilder(MachineInstr &MI) :
268     MachineIRBuilder(*MI.getParent(), MI.getIterator()) {
269     setInstr(MI);
270     setDebugLoc(MI.getDebugLoc());
271   }
272 
MachineIRBuilder(MachineInstr & MI,GISelChangeObserver & Observer)273   MachineIRBuilder(MachineInstr &MI, GISelChangeObserver &Observer) :
274     MachineIRBuilder(MI) {
275     setChangeObserver(Observer);
276   }
277 
278   virtual ~MachineIRBuilder() = default;
279 
MachineIRBuilder(const MachineIRBuilderState & BState)280   MachineIRBuilder(const MachineIRBuilderState &BState) : State(BState) {}
281 
getTII()282   const TargetInstrInfo &getTII() {
283     assert(State.TII && "TargetInstrInfo is not set");
284     return *State.TII;
285   }
286 
287   /// Getter for the function we currently build.
getMF()288   MachineFunction &getMF() {
289     assert(State.MF && "MachineFunction is not set");
290     return *State.MF;
291   }
292 
getMF()293   const MachineFunction &getMF() const {
294     assert(State.MF && "MachineFunction is not set");
295     return *State.MF;
296   }
297 
getDataLayout()298   const DataLayout &getDataLayout() const {
299     return getMF().getFunction().getDataLayout();
300   }
301 
getContext()302   LLVMContext &getContext() const {
303     return getMF().getFunction().getContext();
304   }
305 
306   /// Getter for DebugLoc
getDL()307   const DebugLoc &getDL() { return State.DL; }
308 
309   /// Getter for MRI
getMRI()310   MachineRegisterInfo *getMRI() { return State.MRI; }
getMRI()311   const MachineRegisterInfo *getMRI() const { return State.MRI; }
312 
313   /// Getter for the State
getState()314   MachineIRBuilderState &getState() { return State; }
315 
316   /// Setter for the State
setState(const MachineIRBuilderState & NewState)317   void setState(const MachineIRBuilderState &NewState) { State = NewState; }
318 
319   /// Getter for the basic block we currently build.
getMBB()320   const MachineBasicBlock &getMBB() const {
321     assert(State.MBB && "MachineBasicBlock is not set");
322     return *State.MBB;
323   }
324 
getMBB()325   MachineBasicBlock &getMBB() {
326     return const_cast<MachineBasicBlock &>(
327         const_cast<const MachineIRBuilder *>(this)->getMBB());
328   }
329 
getCSEInfo()330   GISelCSEInfo *getCSEInfo() { return State.CSEInfo; }
getCSEInfo()331   const GISelCSEInfo *getCSEInfo() const { return State.CSEInfo; }
332 
333   /// Current insertion point for new instructions.
getInsertPt()334   MachineBasicBlock::iterator getInsertPt() { return State.II; }
335 
336   /// Set the insertion point before the specified position.
337   /// \pre MBB must be in getMF().
338   /// \pre II must be a valid iterator in MBB.
setInsertPt(MachineBasicBlock & MBB,MachineBasicBlock::iterator II)339   void setInsertPt(MachineBasicBlock &MBB, MachineBasicBlock::iterator II) {
340     assert(MBB.getParent() == &getMF() &&
341            "Basic block is in a different function");
342     State.MBB = &MBB;
343     State.II = II;
344   }
345 
346   /// @}
347 
setCSEInfo(GISelCSEInfo * Info)348   void setCSEInfo(GISelCSEInfo *Info) { State.CSEInfo = Info; }
349 
350   /// \name Setters for the insertion point.
351   /// @{
352   /// Set the MachineFunction where to build instructions.
353   void setMF(MachineFunction &MF);
354 
355   /// Set the insertion point to the  end of \p MBB.
356   /// \pre \p MBB must be contained by getMF().
setMBB(MachineBasicBlock & MBB)357   void setMBB(MachineBasicBlock &MBB) {
358     State.MBB = &MBB;
359     State.II = MBB.end();
360     assert(&getMF() == MBB.getParent() &&
361            "Basic block is in a different function");
362   }
363 
364   /// Set the insertion point to before MI.
365   /// \pre MI must be in getMF().
setInstr(MachineInstr & MI)366   void setInstr(MachineInstr &MI) {
367     assert(MI.getParent() && "Instruction is not part of a basic block");
368     setMBB(*MI.getParent());
369     State.II = MI.getIterator();
370     setPCSections(MI.getPCSections());
371     setMMRAMetadata(MI.getMMRAMetadata());
372   }
373   /// @}
374 
375   /// Set the insertion point to before MI, and set the debug loc to MI's loc.
376   /// \pre MI must be in getMF().
setInstrAndDebugLoc(MachineInstr & MI)377   void setInstrAndDebugLoc(MachineInstr &MI) {
378     setInstr(MI);
379     setDebugLoc(MI.getDebugLoc());
380   }
381 
setChangeObserver(GISelChangeObserver & Observer)382   void setChangeObserver(GISelChangeObserver &Observer) {
383     State.Observer = &Observer;
384   }
385 
getObserver()386   GISelChangeObserver *getObserver() { return State.Observer; }
387 
stopObservingChanges()388   void stopObservingChanges() { State.Observer = nullptr; }
389 
isObservingChanges()390   bool isObservingChanges() const { return State.Observer != nullptr; }
391   /// @}
392 
393   /// Set the debug location to \p DL for all the next build instructions.
setDebugLoc(const DebugLoc & DL)394   void setDebugLoc(const DebugLoc &DL) { this->State.DL = DL; }
395 
396   /// Get the current instruction's debug location.
getDebugLoc()397   const DebugLoc &getDebugLoc() { return State.DL; }
398 
399   /// Set the PC sections metadata to \p MD for all the next build instructions.
setPCSections(MDNode * MD)400   void setPCSections(MDNode *MD) { State.PCSections = MD; }
401 
402   /// Get the current instruction's PC sections metadata.
getPCSections()403   MDNode *getPCSections() { return State.PCSections; }
404 
405   /// Set the PC sections metadata to \p MD for all the next build instructions.
setMMRAMetadata(MDNode * MMRA)406   void setMMRAMetadata(MDNode *MMRA) { State.MMRA = MMRA; }
407 
408   /// Get the current instruction's MMRA metadata.
getMMRAMetadata()409   MDNode *getMMRAMetadata() { return State.MMRA; }
410 
411   /// Build and insert <empty> = \p Opcode <empty>.
412   /// The insertion point is the one set by the last call of either
413   /// setBasicBlock or setMI.
414   ///
415   /// \pre setBasicBlock or setMI must have been called.
416   ///
417   /// \return a MachineInstrBuilder for the newly created instruction.
buildInstr(unsigned Opcode)418   MachineInstrBuilder buildInstr(unsigned Opcode) {
419     return insertInstr(buildInstrNoInsert(Opcode));
420   }
421 
422   /// Build but don't insert <empty> = \p Opcode <empty>.
423   ///
424   /// \pre setMF, setBasicBlock or setMI  must have been called.
425   ///
426   /// \return a MachineInstrBuilder for the newly created instruction.
427   MachineInstrBuilder buildInstrNoInsert(unsigned Opcode);
428 
429   /// Insert an existing instruction at the insertion point.
430   MachineInstrBuilder insertInstr(MachineInstrBuilder MIB);
431 
432   /// Build and insert a DBG_VALUE instruction expressing the fact that the
433   /// associated \p Variable lives in \p Reg (suitably modified by \p Expr).
434   MachineInstrBuilder buildDirectDbgValue(Register Reg, const MDNode *Variable,
435                                           const MDNode *Expr);
436 
437   /// Build and insert a DBG_VALUE instruction expressing the fact that the
438   /// associated \p Variable lives in memory at \p Reg (suitably modified by \p
439   /// Expr).
440   MachineInstrBuilder buildIndirectDbgValue(Register Reg,
441                                             const MDNode *Variable,
442                                             const MDNode *Expr);
443 
444   /// Build and insert a DBG_VALUE instruction expressing the fact that the
445   /// associated \p Variable lives in the stack slot specified by \p FI
446   /// (suitably modified by \p Expr).
447   MachineInstrBuilder buildFIDbgValue(int FI, const MDNode *Variable,
448                                       const MDNode *Expr);
449 
450   /// Build and insert a DBG_VALUE instructions specifying that \p Variable is
451   /// given by \p C (suitably modified by \p Expr).
452   MachineInstrBuilder buildConstDbgValue(const Constant &C,
453                                          const MDNode *Variable,
454                                          const MDNode *Expr);
455 
456   /// Build and insert a DBG_LABEL instructions specifying that \p Label is
457   /// given. Convert "llvm.dbg.label Label" to "DBG_LABEL Label".
458   MachineInstrBuilder buildDbgLabel(const MDNode *Label);
459 
460   /// Build and insert \p Res = G_DYN_STACKALLOC \p Size, \p Align
461   ///
462   /// G_DYN_STACKALLOC does a dynamic stack allocation and writes the address of
463   /// the allocated memory into \p Res.
464   /// \pre setBasicBlock or setMI must have been called.
465   /// \pre \p Res must be a generic virtual register with pointer type.
466   ///
467   /// \return a MachineInstrBuilder for the newly created instruction.
468   MachineInstrBuilder buildDynStackAlloc(const DstOp &Res, const SrcOp &Size,
469                                          Align Alignment);
470 
471   /// Build and insert \p Res = G_FRAME_INDEX \p Idx
472   ///
473   /// G_FRAME_INDEX materializes the address of an alloca value or other
474   /// stack-based object.
475   ///
476   /// \pre setBasicBlock or setMI must have been called.
477   /// \pre \p Res must be a generic virtual register with pointer type.
478   ///
479   /// \return a MachineInstrBuilder for the newly created instruction.
480   MachineInstrBuilder buildFrameIndex(const DstOp &Res, int Idx);
481 
482   /// Build and insert \p Res = G_GLOBAL_VALUE \p GV
483   ///
484   /// G_GLOBAL_VALUE materializes the address of the specified global
485   /// into \p Res.
486   ///
487   /// \pre setBasicBlock or setMI must have been called.
488   /// \pre \p Res must be a generic virtual register with pointer type
489   ///      in the same address space as \p GV.
490   ///
491   /// \return a MachineInstrBuilder for the newly created instruction.
492   MachineInstrBuilder buildGlobalValue(const DstOp &Res, const GlobalValue *GV);
493 
494   /// Build and insert \p Res = G_CONSTANT_POOL \p Idx
495   ///
496   /// G_CONSTANT_POOL materializes the address of an object in the constant
497   /// pool.
498   ///
499   /// \pre setBasicBlock or setMI must have been called.
500   /// \pre \p Res must be a generic virtual register with pointer type.
501   ///
502   /// \return a MachineInstrBuilder for the newly created instruction.
503   MachineInstrBuilder buildConstantPool(const DstOp &Res, unsigned Idx);
504 
505   /// Build and insert \p Res = G_PTR_ADD \p Op0, \p Op1
506   ///
507   /// G_PTR_ADD adds \p Op1 addressible units to the pointer specified by \p Op0,
508   /// storing the resulting pointer in \p Res. Addressible units are typically
509   /// bytes but this can vary between targets.
510   ///
511   /// \pre setBasicBlock or setMI must have been called.
512   /// \pre \p Res and \p Op0 must be generic virtual registers with pointer
513   ///      type.
514   /// \pre \p Op1 must be a generic virtual register with scalar type.
515   ///
516   /// \return a MachineInstrBuilder for the newly created instruction.
517   MachineInstrBuilder buildPtrAdd(const DstOp &Res, const SrcOp &Op0,
518                                   const SrcOp &Op1,
519                                   std::optional<unsigned> Flags = std::nullopt);
520 
521   /// Materialize and insert \p Res = G_PTR_ADD \p Op0, (G_CONSTANT \p Value)
522   ///
523   /// G_PTR_ADD adds \p Value bytes to the pointer specified by \p Op0,
524   /// storing the resulting pointer in \p Res. If \p Value is zero then no
525   /// G_PTR_ADD or G_CONSTANT will be created and \pre Op0 will be assigned to
526   /// \p Res.
527   ///
528   /// \pre setBasicBlock or setMI must have been called.
529   /// \pre \p Op0 must be a generic virtual register with pointer type.
530   /// \pre \p ValueTy must be a scalar type.
531   /// \pre \p Res must be 0. This is to detect confusion between
532   ///      materializePtrAdd() and buildPtrAdd().
533   /// \post \p Res will either be a new generic virtual register of the same
534   ///       type as \p Op0 or \p Op0 itself.
535   ///
536   /// \return a MachineInstrBuilder for the newly created instruction.
537   std::optional<MachineInstrBuilder> materializePtrAdd(Register &Res,
538                                                        Register Op0,
539                                                        const LLT ValueTy,
540                                                        uint64_t Value);
541 
542   /// Build and insert \p Res = G_PTRMASK \p Op0, \p Op1
buildPtrMask(const DstOp & Res,const SrcOp & Op0,const SrcOp & Op1)543   MachineInstrBuilder buildPtrMask(const DstOp &Res, const SrcOp &Op0,
544                                    const SrcOp &Op1) {
545     return buildInstr(TargetOpcode::G_PTRMASK, {Res}, {Op0, Op1});
546   }
547 
548   /// Build and insert \p Res = G_PTRMASK \p Op0, \p G_CONSTANT (1 << NumBits) - 1
549   ///
550   /// This clears the low bits of a pointer operand without destroying its
551   /// pointer properties. This has the effect of rounding the address *down* to
552   /// a specified alignment in bits.
553   ///
554   /// \pre setBasicBlock or setMI must have been called.
555   /// \pre \p Res and \p Op0 must be generic virtual registers with pointer
556   ///      type.
557   /// \pre \p NumBits must be an integer representing the number of low bits to
558   ///      be cleared in \p Op0.
559   ///
560   /// \return a MachineInstrBuilder for the newly created instruction.
561   MachineInstrBuilder buildMaskLowPtrBits(const DstOp &Res, const SrcOp &Op0,
562                                           uint32_t NumBits);
563 
564   /// Build and insert
565   /// a, b, ..., x = G_UNMERGE_VALUES \p Op0
566   /// \p Res = G_BUILD_VECTOR a, b, ..., x, undef, ..., undef
567   ///
568   /// Pad \p Op0 with undef elements to match number of elements in \p Res.
569   ///
570   /// \pre setBasicBlock or setMI must have been called.
571   /// \pre \p Res and \p Op0 must be generic virtual registers with vector type,
572   ///      same vector element type and Op0 must have fewer elements then Res.
573   ///
574   /// \return a MachineInstrBuilder for the newly created build vector instr.
575   MachineInstrBuilder buildPadVectorWithUndefElements(const DstOp &Res,
576                                                       const SrcOp &Op0);
577 
578   /// Build and insert
579   /// a, b, ..., x, y, z = G_UNMERGE_VALUES \p Op0
580   /// \p Res = G_BUILD_VECTOR a, b, ..., x
581   ///
582   /// Delete trailing elements in \p Op0 to match number of elements in \p Res.
583   ///
584   /// \pre setBasicBlock or setMI must have been called.
585   /// \pre \p Res and \p Op0 must be generic virtual registers with vector type,
586   ///      same vector element type and Op0 must have more elements then Res.
587   ///
588   /// \return a MachineInstrBuilder for the newly created build vector instr.
589   MachineInstrBuilder buildDeleteTrailingVectorElements(const DstOp &Res,
590                                                         const SrcOp &Op0);
591 
592   /// Build and insert \p Res, \p CarryOut = G_UADDO \p Op0, \p Op1
593   ///
594   /// G_UADDO sets \p Res to \p Op0 + \p Op1 (truncated to the bit width) and
595   /// sets \p CarryOut to 1 if the result overflowed in unsigned arithmetic.
596   ///
597   /// \pre setBasicBlock or setMI must have been called.
598   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers with the
599   /// same scalar type.
600   ////\pre \p CarryOut must be generic virtual register with scalar type
601   ///(typically s1)
602   ///
603   /// \return The newly created instruction.
buildUAddo(const DstOp & Res,const DstOp & CarryOut,const SrcOp & Op0,const SrcOp & Op1)604   MachineInstrBuilder buildUAddo(const DstOp &Res, const DstOp &CarryOut,
605                                  const SrcOp &Op0, const SrcOp &Op1) {
606     return buildInstr(TargetOpcode::G_UADDO, {Res, CarryOut}, {Op0, Op1});
607   }
608 
609   /// Build and insert \p Res, \p CarryOut = G_USUBO \p Op0, \p Op1
buildUSubo(const DstOp & Res,const DstOp & CarryOut,const SrcOp & Op0,const SrcOp & Op1)610   MachineInstrBuilder buildUSubo(const DstOp &Res, const DstOp &CarryOut,
611                                  const SrcOp &Op0, const SrcOp &Op1) {
612     return buildInstr(TargetOpcode::G_USUBO, {Res, CarryOut}, {Op0, Op1});
613   }
614 
615   /// Build and insert \p Res, \p CarryOut = G_SADDO \p Op0, \p Op1
buildSAddo(const DstOp & Res,const DstOp & CarryOut,const SrcOp & Op0,const SrcOp & Op1)616   MachineInstrBuilder buildSAddo(const DstOp &Res, const DstOp &CarryOut,
617                                  const SrcOp &Op0, const SrcOp &Op1) {
618     return buildInstr(TargetOpcode::G_SADDO, {Res, CarryOut}, {Op0, Op1});
619   }
620 
621   /// Build and insert \p Res, \p CarryOut = G_SUBO \p Op0, \p Op1
buildSSubo(const DstOp & Res,const DstOp & CarryOut,const SrcOp & Op0,const SrcOp & Op1)622   MachineInstrBuilder buildSSubo(const DstOp &Res, const DstOp &CarryOut,
623                                  const SrcOp &Op0, const SrcOp &Op1) {
624     return buildInstr(TargetOpcode::G_SSUBO, {Res, CarryOut}, {Op0, Op1});
625   }
626 
627   /// Build and insert \p Res, \p CarryOut = G_UADDE \p Op0,
628   /// \p Op1, \p CarryIn
629   ///
630   /// G_UADDE sets \p Res to \p Op0 + \p Op1 + \p CarryIn (truncated to the bit
631   /// width) and sets \p CarryOut to 1 if the result overflowed in unsigned
632   /// arithmetic.
633   ///
634   /// \pre setBasicBlock or setMI must have been called.
635   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
636   ///      with the same scalar type.
637   /// \pre \p CarryOut and \p CarryIn must be generic virtual
638   ///      registers with the same scalar type (typically s1)
639   ///
640   /// \return The newly created instruction.
buildUAdde(const DstOp & Res,const DstOp & CarryOut,const SrcOp & Op0,const SrcOp & Op1,const SrcOp & CarryIn)641   MachineInstrBuilder buildUAdde(const DstOp &Res, const DstOp &CarryOut,
642                                  const SrcOp &Op0, const SrcOp &Op1,
643                                  const SrcOp &CarryIn) {
644     return buildInstr(TargetOpcode::G_UADDE, {Res, CarryOut},
645                                              {Op0, Op1, CarryIn});
646   }
647 
648   /// Build and insert \p Res, \p CarryOut = G_USUBE \p Op0, \p Op1, \p CarryInp
buildUSube(const DstOp & Res,const DstOp & CarryOut,const SrcOp & Op0,const SrcOp & Op1,const SrcOp & CarryIn)649   MachineInstrBuilder buildUSube(const DstOp &Res, const DstOp &CarryOut,
650                                  const SrcOp &Op0, const SrcOp &Op1,
651                                  const SrcOp &CarryIn) {
652     return buildInstr(TargetOpcode::G_USUBE, {Res, CarryOut},
653                                              {Op0, Op1, CarryIn});
654   }
655 
656   /// Build and insert \p Res, \p CarryOut = G_SADDE \p Op0, \p Op1, \p CarryInp
buildSAdde(const DstOp & Res,const DstOp & CarryOut,const SrcOp & Op0,const SrcOp & Op1,const SrcOp & CarryIn)657   MachineInstrBuilder buildSAdde(const DstOp &Res, const DstOp &CarryOut,
658                                  const SrcOp &Op0, const SrcOp &Op1,
659                                  const SrcOp &CarryIn) {
660     return buildInstr(TargetOpcode::G_SADDE, {Res, CarryOut},
661                                              {Op0, Op1, CarryIn});
662   }
663 
664   /// Build and insert \p Res, \p CarryOut = G_SSUBE \p Op0, \p Op1, \p CarryInp
buildSSube(const DstOp & Res,const DstOp & CarryOut,const SrcOp & Op0,const SrcOp & Op1,const SrcOp & CarryIn)665   MachineInstrBuilder buildSSube(const DstOp &Res, const DstOp &CarryOut,
666                                  const SrcOp &Op0, const SrcOp &Op1,
667                                  const SrcOp &CarryIn) {
668     return buildInstr(TargetOpcode::G_SSUBE, {Res, CarryOut},
669                                              {Op0, Op1, CarryIn});
670   }
671 
672   /// Build and insert \p Res = G_ANYEXT \p Op0
673   ///
674   /// G_ANYEXT produces a register of the specified width, with bits 0 to
675   /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are unspecified
676   /// (i.e. this is neither zero nor sign-extension). For a vector register,
677   /// each element is extended individually.
678   ///
679   /// \pre setBasicBlock or setMI must have been called.
680   /// \pre \p Res must be a generic virtual register with scalar or vector type.
681   /// \pre \p Op must be a generic virtual register with scalar or vector type.
682   /// \pre \p Op must be smaller than \p Res
683   ///
684   /// \return The newly created instruction.
685 
686   MachineInstrBuilder buildAnyExt(const DstOp &Res, const SrcOp &Op);
687 
688   /// Build and insert \p Res = G_SEXT \p Op
689   ///
690   /// G_SEXT produces a register of the specified width, with bits 0 to
691   /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are duplicated from the
692   /// high bit of \p Op (i.e. 2s-complement sign extended).
693   ///
694   /// \pre setBasicBlock or setMI must have been called.
695   /// \pre \p Res must be a generic virtual register with scalar or vector type.
696   /// \pre \p Op must be a generic virtual register with scalar or vector type.
697   /// \pre \p Op must be smaller than \p Res
698   ///
699   /// \return The newly created instruction.
700   MachineInstrBuilder buildSExt(const DstOp &Res, const SrcOp &Op);
701 
702   /// Build and insert \p Res = G_SEXT_INREG \p Op, ImmOp
buildSExtInReg(const DstOp & Res,const SrcOp & Op,int64_t ImmOp)703   MachineInstrBuilder buildSExtInReg(const DstOp &Res, const SrcOp &Op, int64_t ImmOp) {
704     return buildInstr(TargetOpcode::G_SEXT_INREG, {Res}, {Op, SrcOp(ImmOp)});
705   }
706 
707   /// Build and insert \p Res = G_FPEXT \p Op
708   MachineInstrBuilder buildFPExt(const DstOp &Res, const SrcOp &Op,
709                                  std::optional<unsigned> Flags = std::nullopt) {
710     return buildInstr(TargetOpcode::G_FPEXT, {Res}, {Op}, Flags);
711   }
712 
713   /// Build and insert a G_PTRTOINT instruction.
buildPtrToInt(const DstOp & Dst,const SrcOp & Src)714   MachineInstrBuilder buildPtrToInt(const DstOp &Dst, const SrcOp &Src) {
715     return buildInstr(TargetOpcode::G_PTRTOINT, {Dst}, {Src});
716   }
717 
718   /// Build and insert a G_INTTOPTR instruction.
buildIntToPtr(const DstOp & Dst,const SrcOp & Src)719   MachineInstrBuilder buildIntToPtr(const DstOp &Dst, const SrcOp &Src) {
720     return buildInstr(TargetOpcode::G_INTTOPTR, {Dst}, {Src});
721   }
722 
723   /// Build and insert \p Dst = G_BITCAST \p Src
buildBitcast(const DstOp & Dst,const SrcOp & Src)724   MachineInstrBuilder buildBitcast(const DstOp &Dst, const SrcOp &Src) {
725     return buildInstr(TargetOpcode::G_BITCAST, {Dst}, {Src});
726   }
727 
728     /// Build and insert \p Dst = G_ADDRSPACE_CAST \p Src
buildAddrSpaceCast(const DstOp & Dst,const SrcOp & Src)729   MachineInstrBuilder buildAddrSpaceCast(const DstOp &Dst, const SrcOp &Src) {
730     return buildInstr(TargetOpcode::G_ADDRSPACE_CAST, {Dst}, {Src});
731   }
732 
733   /// \return The opcode of the extension the target wants to use for boolean
734   /// values.
735   unsigned getBoolExtOp(bool IsVec, bool IsFP) const;
736 
737   // Build and insert \p Res = G_ANYEXT \p Op, \p Res = G_SEXT \p Op, or \p Res
738   // = G_ZEXT \p Op depending on how the target wants to extend boolean values.
739   MachineInstrBuilder buildBoolExt(const DstOp &Res, const SrcOp &Op,
740                                    bool IsFP);
741 
742   // Build and insert \p Res = G_SEXT_INREG \p Op, 1 or \p Res = G_AND \p Op, 1,
743   // or COPY depending on how the target wants to extend boolean values, using
744   // the original register size.
745   MachineInstrBuilder buildBoolExtInReg(const DstOp &Res, const SrcOp &Op,
746                                         bool IsVector,
747                                         bool IsFP);
748 
749   /// Build and insert \p Res = G_ZEXT \p Op
750   ///
751   /// G_ZEXT produces a register of the specified width, with bits 0 to
752   /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are 0. For a vector
753   /// register, each element is extended individually.
754   ///
755   /// \pre setBasicBlock or setMI must have been called.
756   /// \pre \p Res must be a generic virtual register with scalar or vector type.
757   /// \pre \p Op must be a generic virtual register with scalar or vector type.
758   /// \pre \p Op must be smaller than \p Res
759   ///
760   /// \return The newly created instruction.
761   MachineInstrBuilder buildZExt(const DstOp &Res, const SrcOp &Op,
762                                 std::optional<unsigned> Flags = std::nullopt);
763 
764   /// Build and insert \p Res = G_SEXT \p Op, \p Res = G_TRUNC \p Op, or
765   /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
766   ///
767   /// \pre setBasicBlock or setMI must have been called.
768   /// \pre \p Res must be a generic virtual register with scalar or vector type.
769   /// \pre \p Op must be a generic virtual register with scalar or vector type.
770   ///
771   /// \return The newly created instruction.
772   MachineInstrBuilder buildSExtOrTrunc(const DstOp &Res, const SrcOp &Op);
773 
774   /// Build and insert \p Res = G_ZEXT \p Op, \p Res = G_TRUNC \p Op, or
775   /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
776   ///
777   /// \pre setBasicBlock or setMI must have been called.
778   /// \pre \p Res must be a generic virtual register with scalar or vector type.
779   /// \pre \p Op must be a generic virtual register with scalar or vector type.
780   ///
781   /// \return The newly created instruction.
782   MachineInstrBuilder buildZExtOrTrunc(const DstOp &Res, const SrcOp &Op);
783 
784   // Build and insert \p Res = G_ANYEXT \p Op, \p Res = G_TRUNC \p Op, or
785   /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
786   ///
787   /// \pre setBasicBlock or setMI must have been called.
788   /// \pre \p Res must be a generic virtual register with scalar or vector type.
789   /// \pre \p Op must be a generic virtual register with scalar or vector type.
790   ///
791   /// \return The newly created instruction.
792   MachineInstrBuilder buildAnyExtOrTrunc(const DstOp &Res, const SrcOp &Op);
793 
794   /// Build and insert \p Res = \p ExtOpc, \p Res = G_TRUNC \p
795   /// Op, or \p Res = COPY \p Op depending on the differing sizes of \p Res and
796   /// \p Op.
797   ///
798   /// \pre setBasicBlock or setMI must have been called.
799   /// \pre \p Res must be a generic virtual register with scalar or vector type.
800   /// \pre \p Op must be a generic virtual register with scalar or vector type.
801   ///
802   /// \return The newly created instruction.
803   MachineInstrBuilder buildExtOrTrunc(unsigned ExtOpc, const DstOp &Res,
804                                       const SrcOp &Op);
805 
806   /// Build and inserts \p Res = \p G_AND \p Op, \p LowBitsSet(ImmOp)
807   /// Since there is no G_ZEXT_INREG like G_SEXT_INREG, the instruction is
808   /// emulated using G_AND.
809   MachineInstrBuilder buildZExtInReg(const DstOp &Res, const SrcOp &Op,
810                                      int64_t ImmOp);
811 
812   /// Build and insert \p Res = \p G_TRUNC_SSAT_S \p Op
813   ///
814   /// G_TRUNC_SSAT_S truncates the signed input, \p Op, to a signed result with
815   /// saturation.
816   ///
817   /// \pre setBasicBlock or setMI must have been called.
818   /// \pre \p Res must be a generic virtual register with scalar or vector type.
819   /// \pre \p Op must be a generic virtual register with scalar or vector type.
820   ///
821   /// \return The newly created instruction.
buildTruncSSatS(const DstOp & Res,const SrcOp & Op)822   MachineInstrBuilder buildTruncSSatS(const DstOp &Res, const SrcOp &Op) {
823     return buildInstr(TargetOpcode::G_TRUNC_SSAT_S, {Res}, {Op});
824   }
825 
826   /// Build and insert \p Res = \p G_TRUNC_SSAT_U \p Op
827   ///
828   /// G_TRUNC_SSAT_U truncates the signed input, \p Op, to an unsigned result
829   /// with saturation.
830   ///
831   /// \pre setBasicBlock or setMI must have been called.
832   /// \pre \p Res must be a generic virtual register with scalar or vector type.
833   /// \pre \p Op must be a generic virtual register with scalar or vector type.
834   ///
835   /// \return The newly created instruction.
buildTruncSSatU(const DstOp & Res,const SrcOp & Op)836   MachineInstrBuilder buildTruncSSatU(const DstOp &Res, const SrcOp &Op) {
837     return buildInstr(TargetOpcode::G_TRUNC_SSAT_U, {Res}, {Op});
838   }
839 
840   /// Build and insert \p Res = \p G_TRUNC_USAT_U \p Op
841   ///
842   /// G_TRUNC_USAT_U truncates the unsigned input, \p Op, to an unsigned result
843   /// with saturation.
844   ///
845   /// \pre setBasicBlock or setMI must have been called.
846   /// \pre \p Res must be a generic virtual register with scalar or vector type.
847   /// \pre \p Op must be a generic virtual register with scalar or vector type.
848   ///
849   /// \return The newly created instruction.
buildTruncUSatU(const DstOp & Res,const SrcOp & Op)850   MachineInstrBuilder buildTruncUSatU(const DstOp &Res, const SrcOp &Op) {
851     return buildInstr(TargetOpcode::G_TRUNC_USAT_U, {Res}, {Op});
852   }
853 
854   /// Build and insert an appropriate cast between two registers of equal size.
855   MachineInstrBuilder buildCast(const DstOp &Dst, const SrcOp &Src);
856 
857   /// Build and insert G_BR \p Dest
858   ///
859   /// G_BR is an unconditional branch to \p Dest.
860   ///
861   /// \pre setBasicBlock or setMI must have been called.
862   ///
863   /// \return a MachineInstrBuilder for the newly created instruction.
864   MachineInstrBuilder buildBr(MachineBasicBlock &Dest);
865 
866   /// Build and insert G_BRCOND \p Tst, \p Dest
867   ///
868   /// G_BRCOND is a conditional branch to \p Dest.
869   ///
870   /// \pre setBasicBlock or setMI must have been called.
871   /// \pre \p Tst must be a generic virtual register with scalar
872   ///      type. At the beginning of legalization, this will be a single
873   ///      bit (s1). Targets with interesting flags registers may change
874   ///      this. For a wider type, whether the branch is taken must only
875   ///      depend on bit 0 (for now).
876   ///
877   /// \return The newly created instruction.
878   MachineInstrBuilder buildBrCond(const SrcOp &Tst, MachineBasicBlock &Dest);
879 
880   /// Build and insert G_BRINDIRECT \p Tgt
881   ///
882   /// G_BRINDIRECT is an indirect branch to \p Tgt.
883   ///
884   /// \pre setBasicBlock or setMI must have been called.
885   /// \pre \p Tgt must be a generic virtual register with pointer type.
886   ///
887   /// \return a MachineInstrBuilder for the newly created instruction.
888   MachineInstrBuilder buildBrIndirect(Register Tgt);
889 
890   /// Build and insert G_BRJT \p TablePtr, \p JTI, \p IndexReg
891   ///
892   /// G_BRJT is a jump table branch using a table base pointer \p TablePtr,
893   /// jump table index \p JTI and index \p IndexReg
894   ///
895   /// \pre setBasicBlock or setMI must have been called.
896   /// \pre \p TablePtr must be a generic virtual register with pointer type.
897   /// \pre \p JTI must be a jump table index.
898   /// \pre \p IndexReg must be a generic virtual register with pointer type.
899   ///
900   /// \return a MachineInstrBuilder for the newly created instruction.
901   MachineInstrBuilder buildBrJT(Register TablePtr, unsigned JTI,
902                                 Register IndexReg);
903 
904   /// Build and insert \p Res = G_CONSTANT \p Val
905   ///
906   /// G_CONSTANT is an integer constant with the specified size and value. \p
907   /// Val will be extended or truncated to the size of \p Reg.
908   ///
909   /// \pre setBasicBlock or setMI must have been called.
910   /// \pre \p Res must be a generic virtual register with scalar or pointer
911   ///      type.
912   ///
913   /// \return The newly created instruction.
914   virtual MachineInstrBuilder buildConstant(const DstOp &Res,
915                                             const ConstantInt &Val);
916 
917   /// Build and insert \p Res = G_CONSTANT \p Val
918   ///
919   /// G_CONSTANT is an integer constant with the specified size and value.
920   ///
921   /// \pre setBasicBlock or setMI must have been called.
922   /// \pre \p Res must be a generic virtual register with scalar type.
923   ///
924   /// \return The newly created instruction.
925   MachineInstrBuilder buildConstant(const DstOp &Res, int64_t Val);
926   MachineInstrBuilder buildConstant(const DstOp &Res, const APInt &Val);
927 
928   /// Build and insert \p Res = G_FCONSTANT \p Val
929   ///
930   /// G_FCONSTANT is a floating-point constant with the specified size and
931   /// value.
932   ///
933   /// \pre setBasicBlock or setMI must have been called.
934   /// \pre \p Res must be a generic virtual register with scalar type.
935   ///
936   /// \return The newly created instruction.
937   virtual MachineInstrBuilder buildFConstant(const DstOp &Res,
938                                              const ConstantFP &Val);
939 
940   MachineInstrBuilder buildFConstant(const DstOp &Res, double Val);
941   MachineInstrBuilder buildFConstant(const DstOp &Res, const APFloat &Val);
942 
943   /// Build and insert G_PTRAUTH_GLOBAL_VALUE
944   ///
945   /// \return a MachineInstrBuilder for the newly created instruction.
946   MachineInstrBuilder buildConstantPtrAuth(const DstOp &Res,
947                                            const ConstantPtrAuth *CPA,
948                                            Register Addr, Register AddrDisc);
949 
950   /// Build and insert \p Res = COPY Op
951   ///
952   /// Register-to-register COPY sets \p Res to \p Op.
953   ///
954   /// \pre setBasicBlock or setMI must have been called.
955   ///
956   /// \return a MachineInstrBuilder for the newly created instruction.
957   MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op);
958 
959 
960   /// Build and insert G_ASSERT_SEXT, G_ASSERT_ZEXT, or G_ASSERT_ALIGN
961   ///
962   /// \return a MachineInstrBuilder for the newly created instruction.
buildAssertInstr(unsigned Opc,const DstOp & Res,const SrcOp & Op,unsigned Val)963   MachineInstrBuilder buildAssertInstr(unsigned Opc, const DstOp &Res,
964                                        const SrcOp &Op, unsigned Val) {
965     return buildInstr(Opc, Res, Op).addImm(Val);
966   }
967 
968   /// Build and insert \p Res = G_ASSERT_ZEXT Op, Size
969   ///
970   /// \return a MachineInstrBuilder for the newly created instruction.
buildAssertZExt(const DstOp & Res,const SrcOp & Op,unsigned Size)971   MachineInstrBuilder buildAssertZExt(const DstOp &Res, const SrcOp &Op,
972                                       unsigned Size) {
973     return buildAssertInstr(TargetOpcode::G_ASSERT_ZEXT, Res, Op, Size);
974   }
975 
976   /// Build and insert \p Res = G_ASSERT_SEXT Op, Size
977   ///
978   /// \return a MachineInstrBuilder for the newly created instruction.
buildAssertSExt(const DstOp & Res,const SrcOp & Op,unsigned Size)979   MachineInstrBuilder buildAssertSExt(const DstOp &Res, const SrcOp &Op,
980                                       unsigned Size) {
981     return buildAssertInstr(TargetOpcode::G_ASSERT_SEXT, Res, Op, Size);
982   }
983 
984   /// Build and insert \p Res = G_ASSERT_ALIGN Op, AlignVal
985   ///
986   /// \return a MachineInstrBuilder for the newly created instruction.
buildAssertAlign(const DstOp & Res,const SrcOp & Op,Align AlignVal)987   MachineInstrBuilder buildAssertAlign(const DstOp &Res, const SrcOp &Op,
988 				       Align AlignVal) {
989     return buildAssertInstr(TargetOpcode::G_ASSERT_ALIGN, Res, Op,
990                             AlignVal.value());
991   }
992 
993   /// Build and insert `Res = G_LOAD Addr, MMO`.
994   ///
995   /// Loads the value stored at \p Addr. Puts the result in \p Res.
996   ///
997   /// \pre setBasicBlock or setMI must have been called.
998   /// \pre \p Res must be a generic virtual register.
999   /// \pre \p Addr must be a generic virtual register with pointer type.
1000   ///
1001   /// \return a MachineInstrBuilder for the newly created instruction.
buildLoad(const DstOp & Res,const SrcOp & Addr,MachineMemOperand & MMO)1002   MachineInstrBuilder buildLoad(const DstOp &Res, const SrcOp &Addr,
1003                                 MachineMemOperand &MMO) {
1004     return buildLoadInstr(TargetOpcode::G_LOAD, Res, Addr, MMO);
1005   }
1006 
1007   /// Build and insert a G_LOAD instruction, while constructing the
1008   /// MachineMemOperand.
1009   MachineInstrBuilder
1010   buildLoad(const DstOp &Res, const SrcOp &Addr, MachinePointerInfo PtrInfo,
1011             Align Alignment,
1012             MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1013             const AAMDNodes &AAInfo = AAMDNodes());
1014 
1015   /// Build and insert `Res = <opcode> Addr, MMO`.
1016   ///
1017   /// Loads the value stored at \p Addr. Puts the result in \p Res.
1018   ///
1019   /// \pre setBasicBlock or setMI must have been called.
1020   /// \pre \p Res must be a generic virtual register.
1021   /// \pre \p Addr must be a generic virtual register with pointer type.
1022   ///
1023   /// \return a MachineInstrBuilder for the newly created instruction.
1024   MachineInstrBuilder buildLoadInstr(unsigned Opcode, const DstOp &Res,
1025                                      const SrcOp &Addr, MachineMemOperand &MMO);
1026 
1027   /// Helper to create a load from a constant offset given a base address. Load
1028   /// the type of \p Dst from \p Offset from the given base address and memory
1029   /// operand.
1030   MachineInstrBuilder buildLoadFromOffset(const DstOp &Dst,
1031                                           const SrcOp &BasePtr,
1032                                           MachineMemOperand &BaseMMO,
1033                                           int64_t Offset);
1034 
1035   /// Build and insert `G_STORE Val, Addr, MMO`.
1036   ///
1037   /// Stores the value \p Val to \p Addr.
1038   ///
1039   /// \pre setBasicBlock or setMI must have been called.
1040   /// \pre \p Val must be a generic virtual register.
1041   /// \pre \p Addr must be a generic virtual register with pointer type.
1042   ///
1043   /// \return a MachineInstrBuilder for the newly created instruction.
1044   MachineInstrBuilder buildStore(const SrcOp &Val, const SrcOp &Addr,
1045                                  MachineMemOperand &MMO);
1046 
1047   /// Build and insert a G_STORE instruction, while constructing the
1048   /// MachineMemOperand.
1049   MachineInstrBuilder
1050   buildStore(const SrcOp &Val, const SrcOp &Addr, MachinePointerInfo PtrInfo,
1051              Align Alignment,
1052              MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1053              const AAMDNodes &AAInfo = AAMDNodes());
1054 
1055   /// Build and insert `Res0, ... = G_EXTRACT Src, Idx0`.
1056   ///
1057   /// \pre setBasicBlock or setMI must have been called.
1058   /// \pre \p Res and \p Src must be generic virtual registers.
1059   ///
1060   /// \return a MachineInstrBuilder for the newly created instruction.
1061   MachineInstrBuilder buildExtract(const DstOp &Res, const SrcOp &Src, uint64_t Index);
1062 
1063   /// Build and insert \p Res = IMPLICIT_DEF.
1064   MachineInstrBuilder buildUndef(const DstOp &Res);
1065 
1066   /// Build and insert \p Res = G_MERGE_VALUES \p Op0, ...
1067   ///
1068   /// G_MERGE_VALUES combines the input elements contiguously into a larger
1069   /// register. It should only be used when the destination register is not a
1070   /// vector.
1071   ///
1072   /// \pre setBasicBlock or setMI must have been called.
1073   /// \pre The entire register \p Res (and no more) must be covered by the input
1074   ///      registers.
1075   /// \pre The type of all \p Ops registers must be identical.
1076   ///
1077   /// \return a MachineInstrBuilder for the newly created instruction.
1078   MachineInstrBuilder buildMergeValues(const DstOp &Res,
1079                                        ArrayRef<Register> Ops);
1080 
1081   /// Build and insert \p Res = G_MERGE_VALUES \p Op0, ...
1082   ///               or \p Res = G_BUILD_VECTOR \p Op0, ...
1083   ///               or \p Res = G_CONCAT_VECTORS \p Op0, ...
1084   ///
1085   /// G_MERGE_VALUES combines the input elements contiguously into a larger
1086   /// register. It is used when the destination register is not a vector.
1087   /// G_BUILD_VECTOR combines scalar inputs into a vector register.
1088   /// G_CONCAT_VECTORS combines vector inputs into a vector register.
1089   ///
1090   /// \pre setBasicBlock or setMI must have been called.
1091   /// \pre The entire register \p Res (and no more) must be covered by the input
1092   ///      registers.
1093   /// \pre The type of all \p Ops registers must be identical.
1094   ///
1095   /// \return a MachineInstrBuilder for the newly created instruction. The
1096   ///         opcode of the new instruction will depend on the types of both
1097   ///         the destination and the sources.
1098   MachineInstrBuilder buildMergeLikeInstr(const DstOp &Res,
1099                                           ArrayRef<Register> Ops);
1100   MachineInstrBuilder buildMergeLikeInstr(const DstOp &Res,
1101                                           std::initializer_list<SrcOp> Ops);
1102 
1103   /// Build and insert \p Res0, ... = G_UNMERGE_VALUES \p Op
1104   ///
1105   /// G_UNMERGE_VALUES splits contiguous bits of the input into multiple
1106   ///
1107   /// \pre setBasicBlock or setMI must have been called.
1108   /// \pre The entire register \p Res (and no more) must be covered by the input
1109   ///      registers.
1110   /// \pre The type of all \p Res registers must be identical.
1111   ///
1112   /// \return a MachineInstrBuilder for the newly created instruction.
1113   MachineInstrBuilder buildUnmerge(ArrayRef<LLT> Res, const SrcOp &Op);
1114   MachineInstrBuilder buildUnmerge(ArrayRef<Register> Res, const SrcOp &Op);
1115 
1116   /// Build and insert an unmerge of \p Res sized pieces to cover \p Op
1117   MachineInstrBuilder buildUnmerge(LLT Res, const SrcOp &Op);
1118 
1119   /// Build and insert an unmerge of pieces with \p Attrs register attributes to
1120   /// cover \p Op
1121   MachineInstrBuilder buildUnmerge(MachineRegisterInfo::VRegAttrs Attrs,
1122                                    const SrcOp &Op);
1123 
1124   /// Build and insert \p Res = G_BUILD_VECTOR \p Op0, ...
1125   ///
1126   /// G_BUILD_VECTOR creates a vector value from multiple scalar registers.
1127   /// \pre setBasicBlock or setMI must have been called.
1128   /// \pre The entire register \p Res (and no more) must be covered by the
1129   ///      input scalar registers.
1130   /// \pre The type of all \p Ops registers must be identical.
1131   ///
1132   /// \return a MachineInstrBuilder for the newly created instruction.
1133   MachineInstrBuilder buildBuildVector(const DstOp &Res,
1134                                        ArrayRef<Register> Ops);
1135 
1136   /// Build and insert \p Res = G_BUILD_VECTOR \p Op0, ... where each OpN is
1137   /// built with G_CONSTANT.
1138   MachineInstrBuilder buildBuildVectorConstant(const DstOp &Res,
1139                                                ArrayRef<APInt> Ops);
1140 
1141   /// Build and insert \p Res = G_BUILD_VECTOR with \p Src replicated to fill
1142   /// the number of elements
1143   MachineInstrBuilder buildSplatBuildVector(const DstOp &Res, const SrcOp &Src);
1144 
1145   /// Build and insert \p Res = G_BUILD_VECTOR_TRUNC \p Op0, ...
1146   ///
1147   /// G_BUILD_VECTOR_TRUNC creates a vector value from multiple scalar registers
1148   /// which have types larger than the destination vector element type, and
1149   /// truncates the values to fit.
1150   ///
1151   /// If the operands given are already the same size as the vector elt type,
1152   /// then this method will instead create a G_BUILD_VECTOR instruction.
1153   ///
1154   /// \pre setBasicBlock or setMI must have been called.
1155   /// \pre The type of all \p Ops registers must be identical.
1156   ///
1157   /// \return a MachineInstrBuilder for the newly created instruction.
1158   MachineInstrBuilder buildBuildVectorTrunc(const DstOp &Res,
1159                                             ArrayRef<Register> Ops);
1160 
1161   /// Build and insert a vector splat of a scalar \p Src using a
1162   /// G_INSERT_VECTOR_ELT and G_SHUFFLE_VECTOR idiom.
1163   ///
1164   /// \pre setBasicBlock or setMI must have been called.
1165   /// \pre \p Src must have the same type as the element type of \p Dst
1166   ///
1167   /// \return a MachineInstrBuilder for the newly created instruction.
1168   MachineInstrBuilder buildShuffleSplat(const DstOp &Res, const SrcOp &Src);
1169 
1170   /// Build and insert \p Res = G_SHUFFLE_VECTOR \p Src1, \p Src2, \p Mask
1171   ///
1172   /// \pre setBasicBlock or setMI must have been called.
1173   ///
1174   /// \return a MachineInstrBuilder for the newly created instruction.
1175   MachineInstrBuilder buildShuffleVector(const DstOp &Res, const SrcOp &Src1,
1176                                          const SrcOp &Src2, ArrayRef<int> Mask);
1177 
1178   /// Build and insert \p Res = G_SPLAT_VECTOR \p Val
1179   ///
1180   /// \pre setBasicBlock or setMI must have been called.
1181   /// \pre \p Res must be a generic virtual register with vector type.
1182   /// \pre \p Val must be a generic virtual register with scalar type.
1183   ///
1184   /// \return a MachineInstrBuilder for the newly created instruction.
1185   MachineInstrBuilder buildSplatVector(const DstOp &Res, const SrcOp &Val);
1186 
1187   /// Build and insert \p Res = G_CONCAT_VECTORS \p Op0, ...
1188   ///
1189   /// G_CONCAT_VECTORS creates a vector from the concatenation of 2 or more
1190   /// vectors.
1191   ///
1192   /// \pre setBasicBlock or setMI must have been called.
1193   /// \pre The entire register \p Res (and no more) must be covered by the input
1194   ///      registers.
1195   /// \pre The type of all source operands must be identical.
1196   ///
1197   /// \return a MachineInstrBuilder for the newly created instruction.
1198   MachineInstrBuilder buildConcatVectors(const DstOp &Res,
1199                                          ArrayRef<Register> Ops);
1200 
1201   /// Build and insert `Res = G_INSERT_SUBVECTOR Src0, Src1, Idx`.
1202   ///
1203   /// \pre setBasicBlock or setMI must have been called.
1204   /// \pre \p Res, \p Src0, and \p Src1 must be generic virtual registers with
1205   /// vector type.
1206   ///
1207   /// \return a MachineInstrBuilder for the newly created instruction.
1208   MachineInstrBuilder buildInsertSubvector(const DstOp &Res, const SrcOp &Src0,
1209                                            const SrcOp &Src1, unsigned Index);
1210 
1211   /// Build and insert `Res = G_EXTRACT_SUBVECTOR Src, Idx0`.
1212   ///
1213   /// \pre setBasicBlock or setMI must have been called.
1214   /// \pre \p Res and \p Src must be generic virtual registers with vector type.
1215   ///
1216   /// \return a MachineInstrBuilder for the newly created instruction.
1217   MachineInstrBuilder buildExtractSubvector(const DstOp &Res, const SrcOp &Src,
1218                                             unsigned Index);
1219 
1220   MachineInstrBuilder buildInsert(const DstOp &Res, const SrcOp &Src,
1221                                   const SrcOp &Op, unsigned Index);
1222 
1223   /// Build and insert \p Res = G_STEP_VECTOR \p Step
1224   ///
1225   /// G_STEP_VECTOR returns a scalable vector of linear sequence of step \p Step
1226   /// into \p Res.
1227   ///
1228   /// \pre setBasicBlock or setMI must have been called.
1229   /// \pre \p Res must be a generic virtual register with scalable vector type.
1230   ///
1231   /// \return a MachineInstrBuilder for the newly created instruction.
1232   MachineInstrBuilder buildStepVector(const DstOp &Res, unsigned Step);
1233 
1234   /// Build and insert \p Res = G_VSCALE \p MinElts
1235   ///
1236   /// G_VSCALE puts the value of the runtime vscale multiplied by \p MinElts
1237   /// into \p Res.
1238   ///
1239   /// \pre setBasicBlock or setMI must have been called.
1240   /// \pre \p Res must be a generic virtual register with scalar type.
1241   ///
1242   /// \return a MachineInstrBuilder for the newly created instruction.
1243   MachineInstrBuilder buildVScale(const DstOp &Res, unsigned MinElts);
1244 
1245   /// Build and insert \p Res = G_VSCALE \p MinElts
1246   ///
1247   /// G_VSCALE puts the value of the runtime vscale multiplied by \p MinElts
1248   /// into \p Res.
1249   ///
1250   /// \pre setBasicBlock or setMI must have been called.
1251   /// \pre \p Res must be a generic virtual register with scalar type.
1252   ///
1253   /// \return a MachineInstrBuilder for the newly created instruction.
1254   MachineInstrBuilder buildVScale(const DstOp &Res, const ConstantInt &MinElts);
1255 
1256   /// Build and insert \p Res = G_VSCALE \p MinElts
1257   ///
1258   /// G_VSCALE puts the value of the runtime vscale multiplied by \p MinElts
1259   /// into \p Res.
1260   ///
1261   /// \pre setBasicBlock or setMI must have been called.
1262   /// \pre \p Res must be a generic virtual register with scalar type.
1263   ///
1264   /// \return a MachineInstrBuilder for the newly created instruction.
1265   MachineInstrBuilder buildVScale(const DstOp &Res, const APInt &MinElts);
1266 
1267   /// Build and insert a G_INTRINSIC instruction.
1268   ///
1269   /// There are four different opcodes based on combinations of whether the
1270   /// intrinsic has side effects and whether it is convergent. These properties
1271   /// can be specified as explicit parameters, or else they are retrieved from
1272   /// the MCID for the intrinsic.
1273   ///
1274   /// The parameter \p Res provides the Registers or MOs that will be defined by
1275   /// this instruction.
1276   ///
1277   /// \pre setBasicBlock or setMI must have been called.
1278   ///
1279   /// \return a MachineInstrBuilder for the newly created instruction.
1280   MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef<Register> Res,
1281                                      bool HasSideEffects, bool isConvergent);
1282   MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef<Register> Res);
1283   MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef<DstOp> Res,
1284                                      bool HasSideEffects, bool isConvergent);
1285   MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef<DstOp> Res);
1286 
1287   /// Build and insert \p Res = G_FPTRUNC \p Op
1288   ///
1289   /// G_FPTRUNC converts a floating-point value into one with a smaller type.
1290   ///
1291   /// \pre setBasicBlock or setMI must have been called.
1292   /// \pre \p Res must be a generic virtual register with scalar or vector type.
1293   /// \pre \p Op must be a generic virtual register with scalar or vector type.
1294   /// \pre \p Res must be smaller than \p Op
1295   ///
1296   /// \return The newly created instruction.
1297   MachineInstrBuilder
1298   buildFPTrunc(const DstOp &Res, const SrcOp &Op,
1299                std::optional<unsigned> Flags = std::nullopt);
1300 
1301   /// Build and insert \p Res = G_TRUNC \p Op
1302   ///
1303   /// G_TRUNC extracts the low bits of a type. For a vector type each element is
1304   /// truncated independently before being packed into the destination.
1305   ///
1306   /// \pre setBasicBlock or setMI must have been called.
1307   /// \pre \p Res must be a generic virtual register with scalar or vector type.
1308   /// \pre \p Op must be a generic virtual register with scalar or vector type.
1309   /// \pre \p Res must be smaller than \p Op
1310   ///
1311   /// \return The newly created instruction.
1312   MachineInstrBuilder buildTrunc(const DstOp &Res, const SrcOp &Op,
1313                                  std::optional<unsigned> Flags = std::nullopt);
1314 
1315   /// Build and insert a \p Res = G_ICMP \p Pred, \p Op0, \p Op1
1316   ///
1317   /// \pre setBasicBlock or setMI must have been called.
1318 
1319   /// \pre \p Res must be a generic virtual register with scalar or
1320   ///      vector type. Typically this starts as s1 or <N x s1>.
1321   /// \pre \p Op0 and Op1 must be generic virtual registers with the
1322   ///      same number of elements as \p Res. If \p Res is a scalar,
1323   ///      \p Op0 must be either a scalar or pointer.
1324   /// \pre \p Pred must be an integer predicate.
1325   ///
1326   /// \return a MachineInstrBuilder for the newly created instruction.
1327   MachineInstrBuilder buildICmp(CmpInst::Predicate Pred, const DstOp &Res,
1328                                 const SrcOp &Op0, const SrcOp &Op1,
1329                                 std::optional<unsigned> Flags = std::nullopt);
1330 
1331   /// Build and insert a \p Res = G_FCMP \p Pred\p Op0, \p Op1
1332   ///
1333   /// \pre setBasicBlock or setMI must have been called.
1334 
1335   /// \pre \p Res must be a generic virtual register with scalar or
1336   ///      vector type. Typically this starts as s1 or <N x s1>.
1337   /// \pre \p Op0 and Op1 must be generic virtual registers with the
1338   ///      same number of elements as \p Res (or scalar, if \p Res is
1339   ///      scalar).
1340   /// \pre \p Pred must be a floating-point predicate.
1341   ///
1342   /// \return a MachineInstrBuilder for the newly created instruction.
1343   MachineInstrBuilder buildFCmp(CmpInst::Predicate Pred, const DstOp &Res,
1344                                 const SrcOp &Op0, const SrcOp &Op1,
1345                                 std::optional<unsigned> Flags = std::nullopt);
1346 
1347   /// Build and insert a \p Res = G_SCMP \p Op0, \p Op1
1348   ///
1349   /// \pre setBasicBlock or setMI must have been called.
1350 
1351   /// \pre \p Res must be a generic virtual register with scalar or
1352   ///      vector type. Typically this starts as s2 or <N x s2>.
1353   /// \pre \p Op0 and Op1 must be generic virtual registers with the
1354   ///      same number of elements as \p Res. If \p Res is a scalar,
1355   ///      \p Op0 must be a scalar.
1356   ///
1357   /// \return a MachineInstrBuilder for the newly created instruction.
1358   MachineInstrBuilder buildSCmp(const DstOp &Res, const SrcOp &Op0,
1359                                 const SrcOp &Op1);
1360 
1361   /// Build and insert a \p Res = G_UCMP \p Op0, \p Op1
1362   ///
1363   /// \pre setBasicBlock or setMI must have been called.
1364 
1365   /// \pre \p Res must be a generic virtual register with scalar or
1366   ///      vector type. Typically this starts as s2 or <N x s2>.
1367   /// \pre \p Op0 and Op1 must be generic virtual registers with the
1368   ///      same number of elements as \p Res. If \p Res is a scalar,
1369   ///      \p Op0 must be a scalar.
1370   ///
1371   /// \return a MachineInstrBuilder for the newly created instruction.
1372   MachineInstrBuilder buildUCmp(const DstOp &Res, const SrcOp &Op0,
1373                                 const SrcOp &Op1);
1374 
1375   /// Build and insert a \p Res = G_IS_FPCLASS \p Src, \p Mask
buildIsFPClass(const DstOp & Res,const SrcOp & Src,unsigned Mask)1376   MachineInstrBuilder buildIsFPClass(const DstOp &Res, const SrcOp &Src,
1377                                      unsigned Mask) {
1378     return buildInstr(TargetOpcode::G_IS_FPCLASS, {Res},
1379                       {Src, SrcOp(static_cast<int64_t>(Mask))});
1380   }
1381 
1382   /// Build and insert a \p Res = G_SELECT \p Tst, \p Op0, \p Op1
1383   ///
1384   /// \pre setBasicBlock or setMI must have been called.
1385   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1386   ///      with the same type.
1387   /// \pre \p Tst must be a generic virtual register with scalar, pointer or
1388   ///      vector type. If vector then it must have the same number of
1389   ///      elements as the other parameters.
1390   ///
1391   /// \return a MachineInstrBuilder for the newly created instruction.
1392   MachineInstrBuilder buildSelect(const DstOp &Res, const SrcOp &Tst,
1393                                   const SrcOp &Op0, const SrcOp &Op1,
1394                                   std::optional<unsigned> Flags = std::nullopt);
1395 
1396   /// Build and insert \p Res = G_INSERT_VECTOR_ELT \p Val,
1397   /// \p Elt, \p Idx
1398   ///
1399   /// \pre setBasicBlock or setMI must have been called.
1400   /// \pre \p Res and \p Val must be a generic virtual register
1401   //       with the same vector type.
1402   /// \pre \p Elt and \p Idx must be a generic virtual register
1403   ///      with scalar type.
1404   ///
1405   /// \return The newly created instruction.
1406   MachineInstrBuilder buildInsertVectorElement(const DstOp &Res,
1407                                                const SrcOp &Val,
1408                                                const SrcOp &Elt,
1409                                                const SrcOp &Idx);
1410 
1411   /// Build and insert \p Res = G_EXTRACT_VECTOR_ELT \p Val, \p Idx
1412   ///
1413   /// \pre setBasicBlock or setMI must have been called.
1414   /// \pre \p Res must be a generic virtual register with scalar type.
1415   /// \pre \p Val must be a generic virtual register with vector type.
1416   ///
1417   /// \return The newly created instruction.
buildExtractVectorElementConstant(const DstOp & Res,const SrcOp & Val,const int Idx)1418   MachineInstrBuilder buildExtractVectorElementConstant(const DstOp &Res,
1419                                                         const SrcOp &Val,
1420                                                         const int Idx) {
1421     const TargetLowering *TLI = getMF().getSubtarget().getTargetLowering();
1422     LLT IdxTy = TLI->getVectorIdxLLT(getDataLayout());
1423     return buildExtractVectorElement(Res, Val, buildConstant(IdxTy, Idx));
1424   }
1425 
1426   /// Build and insert \p Res = G_EXTRACT_VECTOR_ELT \p Val, \p Idx
1427   ///
1428   /// \pre setBasicBlock or setMI must have been called.
1429   /// \pre \p Res must be a generic virtual register with scalar type.
1430   /// \pre \p Val must be a generic virtual register with vector type.
1431   /// \pre \p Idx must be a generic virtual register with scalar type.
1432   ///
1433   /// \return The newly created instruction.
1434   MachineInstrBuilder buildExtractVectorElement(const DstOp &Res,
1435                                                 const SrcOp &Val,
1436                                                 const SrcOp &Idx);
1437 
1438   /// Build and insert `OldValRes<def>, SuccessRes<def> =
1439   /// G_ATOMIC_CMPXCHG_WITH_SUCCESS Addr, CmpVal, NewVal, MMO`.
1440   ///
1441   /// Atomically replace the value at \p Addr with \p NewVal if it is currently
1442   /// \p CmpVal otherwise leaves it unchanged. Puts the original value from \p
1443   /// Addr in \p Res, along with an s1 indicating whether it was replaced.
1444   ///
1445   /// \pre setBasicBlock or setMI must have been called.
1446   /// \pre \p OldValRes must be a generic virtual register of scalar type.
1447   /// \pre \p SuccessRes must be a generic virtual register of scalar type. It
1448   ///      will be assigned 0 on failure and 1 on success.
1449   /// \pre \p Addr must be a generic virtual register with pointer type.
1450   /// \pre \p OldValRes, \p CmpVal, and \p NewVal must be generic virtual
1451   ///      registers of the same type.
1452   ///
1453   /// \return a MachineInstrBuilder for the newly created instruction.
1454   MachineInstrBuilder
1455   buildAtomicCmpXchgWithSuccess(const DstOp &OldValRes, const DstOp &SuccessRes,
1456                                 const SrcOp &Addr, const SrcOp &CmpVal,
1457                                 const SrcOp &NewVal, MachineMemOperand &MMO);
1458 
1459   /// Build and insert `OldValRes<def> = G_ATOMIC_CMPXCHG Addr, CmpVal, NewVal,
1460   /// MMO`.
1461   ///
1462   /// Atomically replace the value at \p Addr with \p NewVal if it is currently
1463   /// \p CmpVal otherwise leaves it unchanged. Puts the original value from \p
1464   /// Addr in \p Res.
1465   ///
1466   /// \pre setBasicBlock or setMI must have been called.
1467   /// \pre \p OldValRes must be a generic virtual register of scalar type.
1468   /// \pre \p Addr must be a generic virtual register with pointer type.
1469   /// \pre \p OldValRes, \p CmpVal, and \p NewVal must be generic virtual
1470   ///      registers of the same type.
1471   ///
1472   /// \return a MachineInstrBuilder for the newly created instruction.
1473   MachineInstrBuilder buildAtomicCmpXchg(const DstOp &OldValRes,
1474                                          const SrcOp &Addr, const SrcOp &CmpVal,
1475                                          const SrcOp &NewVal,
1476                                          MachineMemOperand &MMO);
1477 
1478   /// Build and insert `OldValRes<def> = G_ATOMICRMW_<Opcode> Addr, Val, MMO`.
1479   ///
1480   /// Atomically read-modify-update the value at \p Addr with \p Val. Puts the
1481   /// original value from \p Addr in \p OldValRes. The modification is
1482   /// determined by the opcode.
1483   ///
1484   /// \pre setBasicBlock or setMI must have been called.
1485   /// \pre \p OldValRes must be a generic virtual register.
1486   /// \pre \p Addr must be a generic virtual register with pointer type.
1487   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1488   ///      same type.
1489   ///
1490   /// \return a MachineInstrBuilder for the newly created instruction.
1491   MachineInstrBuilder buildAtomicRMW(unsigned Opcode, const DstOp &OldValRes,
1492                                      const SrcOp &Addr, const SrcOp &Val,
1493                                      MachineMemOperand &MMO);
1494 
1495   /// Build and insert `OldValRes<def> = G_ATOMICRMW_XCHG Addr, Val, MMO`.
1496   ///
1497   /// Atomically replace the value at \p Addr with \p Val. Puts the original
1498   /// value from \p Addr in \p OldValRes.
1499   ///
1500   /// \pre setBasicBlock or setMI must have been called.
1501   /// \pre \p OldValRes must be a generic virtual register.
1502   /// \pre \p Addr must be a generic virtual register with pointer type.
1503   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1504   ///      same type.
1505   ///
1506   /// \return a MachineInstrBuilder for the newly created instruction.
1507   MachineInstrBuilder buildAtomicRMWXchg(Register OldValRes, Register Addr,
1508                                          Register Val, MachineMemOperand &MMO);
1509 
1510   /// Build and insert `OldValRes<def> = G_ATOMICRMW_ADD Addr, Val, MMO`.
1511   ///
1512   /// Atomically replace the value at \p Addr with the addition of \p Val and
1513   /// the original value. Puts the original value from \p Addr in \p OldValRes.
1514   ///
1515   /// \pre setBasicBlock or setMI must have been called.
1516   /// \pre \p OldValRes must be a generic virtual register.
1517   /// \pre \p Addr must be a generic virtual register with pointer type.
1518   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1519   ///      same type.
1520   ///
1521   /// \return a MachineInstrBuilder for the newly created instruction.
1522   MachineInstrBuilder buildAtomicRMWAdd(Register OldValRes, Register Addr,
1523                                         Register Val, MachineMemOperand &MMO);
1524 
1525   /// Build and insert `OldValRes<def> = G_ATOMICRMW_SUB Addr, Val, MMO`.
1526   ///
1527   /// Atomically replace the value at \p Addr with the subtraction of \p Val and
1528   /// the original value. Puts the original value from \p Addr in \p OldValRes.
1529   ///
1530   /// \pre setBasicBlock or setMI must have been called.
1531   /// \pre \p OldValRes must be a generic virtual register.
1532   /// \pre \p Addr must be a generic virtual register with pointer type.
1533   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1534   ///      same type.
1535   ///
1536   /// \return a MachineInstrBuilder for the newly created instruction.
1537   MachineInstrBuilder buildAtomicRMWSub(Register OldValRes, Register Addr,
1538                                         Register Val, MachineMemOperand &MMO);
1539 
1540   /// Build and insert `OldValRes<def> = G_ATOMICRMW_AND Addr, Val, MMO`.
1541   ///
1542   /// Atomically replace the value at \p Addr with the bitwise and of \p Val and
1543   /// the original value. Puts the original value from \p Addr in \p OldValRes.
1544   ///
1545   /// \pre setBasicBlock or setMI must have been called.
1546   /// \pre \p OldValRes must be a generic virtual register.
1547   /// \pre \p Addr must be a generic virtual register with pointer type.
1548   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1549   ///      same type.
1550   ///
1551   /// \return a MachineInstrBuilder for the newly created instruction.
1552   MachineInstrBuilder buildAtomicRMWAnd(Register OldValRes, Register Addr,
1553                                         Register Val, MachineMemOperand &MMO);
1554 
1555   /// Build and insert `OldValRes<def> = G_ATOMICRMW_NAND Addr, Val, MMO`.
1556   ///
1557   /// Atomically replace the value at \p Addr with the bitwise nand of \p Val
1558   /// and the original value. Puts the original value from \p Addr in \p
1559   /// OldValRes.
1560   ///
1561   /// \pre setBasicBlock or setMI must have been called.
1562   /// \pre \p OldValRes must be a generic virtual register.
1563   /// \pre \p Addr must be a generic virtual register with pointer type.
1564   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1565   ///      same type.
1566   ///
1567   /// \return a MachineInstrBuilder for the newly created instruction.
1568   MachineInstrBuilder buildAtomicRMWNand(Register OldValRes, Register Addr,
1569                                          Register Val, MachineMemOperand &MMO);
1570 
1571   /// Build and insert `OldValRes<def> = G_ATOMICRMW_OR Addr, Val, MMO`.
1572   ///
1573   /// Atomically replace the value at \p Addr with the bitwise or of \p Val and
1574   /// the original value. Puts the original value from \p Addr in \p OldValRes.
1575   ///
1576   /// \pre setBasicBlock or setMI must have been called.
1577   /// \pre \p OldValRes must be a generic virtual register.
1578   /// \pre \p Addr must be a generic virtual register with pointer type.
1579   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1580   ///      same type.
1581   ///
1582   /// \return a MachineInstrBuilder for the newly created instruction.
1583   MachineInstrBuilder buildAtomicRMWOr(Register OldValRes, Register Addr,
1584                                        Register Val, MachineMemOperand &MMO);
1585 
1586   /// Build and insert `OldValRes<def> = G_ATOMICRMW_XOR Addr, Val, MMO`.
1587   ///
1588   /// Atomically replace the value at \p Addr with the bitwise xor of \p Val and
1589   /// the original value. Puts the original value from \p Addr in \p OldValRes.
1590   ///
1591   /// \pre setBasicBlock or setMI must have been called.
1592   /// \pre \p OldValRes must be a generic virtual register.
1593   /// \pre \p Addr must be a generic virtual register with pointer type.
1594   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1595   ///      same type.
1596   ///
1597   /// \return a MachineInstrBuilder for the newly created instruction.
1598   MachineInstrBuilder buildAtomicRMWXor(Register OldValRes, Register Addr,
1599                                         Register Val, MachineMemOperand &MMO);
1600 
1601   /// Build and insert `OldValRes<def> = G_ATOMICRMW_MAX Addr, Val, MMO`.
1602   ///
1603   /// Atomically replace the value at \p Addr with the signed maximum of \p
1604   /// Val and the original value. Puts the original value from \p Addr in \p
1605   /// OldValRes.
1606   ///
1607   /// \pre setBasicBlock or setMI must have been called.
1608   /// \pre \p OldValRes must be a generic virtual register.
1609   /// \pre \p Addr must be a generic virtual register with pointer type.
1610   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1611   ///      same type.
1612   ///
1613   /// \return a MachineInstrBuilder for the newly created instruction.
1614   MachineInstrBuilder buildAtomicRMWMax(Register OldValRes, Register Addr,
1615                                         Register Val, MachineMemOperand &MMO);
1616 
1617   /// Build and insert `OldValRes<def> = G_ATOMICRMW_MIN Addr, Val, MMO`.
1618   ///
1619   /// Atomically replace the value at \p Addr with the signed minimum of \p
1620   /// Val and the original value. Puts the original value from \p Addr in \p
1621   /// OldValRes.
1622   ///
1623   /// \pre setBasicBlock or setMI must have been called.
1624   /// \pre \p OldValRes must be a generic virtual register.
1625   /// \pre \p Addr must be a generic virtual register with pointer type.
1626   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1627   ///      same type.
1628   ///
1629   /// \return a MachineInstrBuilder for the newly created instruction.
1630   MachineInstrBuilder buildAtomicRMWMin(Register OldValRes, Register Addr,
1631                                         Register Val, MachineMemOperand &MMO);
1632 
1633   /// Build and insert `OldValRes<def> = G_ATOMICRMW_UMAX Addr, Val, MMO`.
1634   ///
1635   /// Atomically replace the value at \p Addr with the unsigned maximum of \p
1636   /// Val and the original value. Puts the original value from \p Addr in \p
1637   /// OldValRes.
1638   ///
1639   /// \pre setBasicBlock or setMI must have been called.
1640   /// \pre \p OldValRes must be a generic virtual register.
1641   /// \pre \p Addr must be a generic virtual register with pointer type.
1642   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1643   ///      same type.
1644   ///
1645   /// \return a MachineInstrBuilder for the newly created instruction.
1646   MachineInstrBuilder buildAtomicRMWUmax(Register OldValRes, Register Addr,
1647                                          Register Val, MachineMemOperand &MMO);
1648 
1649   /// Build and insert `OldValRes<def> = G_ATOMICRMW_UMIN Addr, Val, MMO`.
1650   ///
1651   /// Atomically replace the value at \p Addr with the unsigned minimum of \p
1652   /// Val and the original value. Puts the original value from \p Addr in \p
1653   /// OldValRes.
1654   ///
1655   /// \pre setBasicBlock or setMI must have been called.
1656   /// \pre \p OldValRes must be a generic virtual register.
1657   /// \pre \p Addr must be a generic virtual register with pointer type.
1658   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1659   ///      same type.
1660   ///
1661   /// \return a MachineInstrBuilder for the newly created instruction.
1662   MachineInstrBuilder buildAtomicRMWUmin(Register OldValRes, Register Addr,
1663                                          Register Val, MachineMemOperand &MMO);
1664 
1665   /// Build and insert `OldValRes<def> = G_ATOMICRMW_FADD Addr, Val, MMO`.
1666   MachineInstrBuilder buildAtomicRMWFAdd(
1667     const DstOp &OldValRes, const SrcOp &Addr, const SrcOp &Val,
1668     MachineMemOperand &MMO);
1669 
1670   /// Build and insert `OldValRes<def> = G_ATOMICRMW_FSUB Addr, Val, MMO`.
1671   MachineInstrBuilder buildAtomicRMWFSub(
1672         const DstOp &OldValRes, const SrcOp &Addr, const SrcOp &Val,
1673         MachineMemOperand &MMO);
1674 
1675   /// Build and insert `OldValRes<def> = G_ATOMICRMW_FMAX Addr, Val, MMO`.
1676   ///
1677   /// Atomically replace the value at \p Addr with the floating point maximum of
1678   /// \p Val and the original value. Puts the original value from \p Addr in \p
1679   /// OldValRes.
1680   ///
1681   /// \pre setBasicBlock or setMI must have been called.
1682   /// \pre \p OldValRes must be a generic virtual register.
1683   /// \pre \p Addr must be a generic virtual register with pointer type.
1684   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1685   ///      same type.
1686   ///
1687   /// \return a MachineInstrBuilder for the newly created instruction.
1688   MachineInstrBuilder buildAtomicRMWFMax(
1689         const DstOp &OldValRes, const SrcOp &Addr, const SrcOp &Val,
1690         MachineMemOperand &MMO);
1691 
1692   /// Build and insert `OldValRes<def> = G_ATOMICRMW_FMIN Addr, Val, MMO`.
1693   ///
1694   /// Atomically replace the value at \p Addr with the floating point minimum of
1695   /// \p Val and the original value. Puts the original value from \p Addr in \p
1696   /// OldValRes.
1697   ///
1698   /// \pre setBasicBlock or setMI must have been called.
1699   /// \pre \p OldValRes must be a generic virtual register.
1700   /// \pre \p Addr must be a generic virtual register with pointer type.
1701   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1702   ///      same type.
1703   ///
1704   /// \return a MachineInstrBuilder for the newly created instruction.
1705   MachineInstrBuilder buildAtomicRMWFMin(
1706         const DstOp &OldValRes, const SrcOp &Addr, const SrcOp &Val,
1707         MachineMemOperand &MMO);
1708 
1709   /// Build and insert `OldValRes<def> = G_ATOMICRMW_FMAXIMUM Addr, Val, MMO`.
1710   ///
1711   /// Atomically replace the value at \p Addr with the floating point maximum of
1712   /// \p Val and the original value. Puts the original value from \p Addr in \p
1713   /// OldValRes.
1714   ///
1715   /// \pre setBasicBlock or setMI must have been called.
1716   /// \pre \p OldValRes must be a generic virtual register.
1717   /// \pre \p Addr must be a generic virtual register with pointer type.
1718   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1719   ///      same type.
1720   ///
1721   /// \return a MachineInstrBuilder for the newly created instruction.
1722   MachineInstrBuilder buildAtomicRMWFMaximum(const DstOp &OldValRes,
1723                                              const SrcOp &Addr,
1724                                              const SrcOp &Val,
1725                                              MachineMemOperand &MMO);
1726 
1727   /// Build and insert `OldValRes<def> = G_ATOMICRMW_FMINIMUM Addr, Val, MMO`.
1728   ///
1729   /// Atomically replace the value at \p Addr with the floating point minimum of
1730   /// \p Val and the original value. Puts the original value from \p Addr in \p
1731   /// OldValRes.
1732   ///
1733   /// \pre setBasicBlock or setMI must have been called.
1734   /// \pre \p OldValRes must be a generic virtual register.
1735   /// \pre \p Addr must be a generic virtual register with pointer type.
1736   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1737   ///      same type.
1738   ///
1739   /// \return a MachineInstrBuilder for the newly created instruction.
1740   MachineInstrBuilder buildAtomicRMWFMinimum(const DstOp &OldValRes,
1741                                              const SrcOp &Addr,
1742                                              const SrcOp &Val,
1743                                              MachineMemOperand &MMO);
1744 
1745   /// Build and insert `OldValRes<def> = G_ATOMICRMW_USUB_COND Addr, Val, MMO`.
1746   ///
1747   /// Atomically replace the value at \p Addr with the original value minus \p
1748   /// Val if the original value is greater than or equal to \p Val, or leaves it
1749   /// unchanged otherwise. Puts the original value from \p Addr in \p OldValRes.
1750   ///
1751   /// \pre setBasicBlock or setMI must have been called.
1752   /// \pre \p OldValRes must be a generic virtual register.
1753   /// \pre \p Addr must be a generic virtual register with pointer type.
1754   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1755   ///      same type.
1756   ///
1757   /// \return a MachineInstrBuilder for the newly created instruction.
1758   MachineInstrBuilder buildAtomicRMWUSubCond(const DstOp &OldValRes,
1759                                              const SrcOp &Addr,
1760                                              const SrcOp &Val,
1761                                              MachineMemOperand &MMO);
1762 
1763   /// Build and insert `OldValRes<def> = G_ATOMICRMW_USUB_SAT Addr, Val, MMO`.
1764   ///
1765   /// Atomically replace the value at \p Addr with the original value minus \p
1766   /// Val, with clamping to zero if the unsigned subtraction would overflow.
1767   /// Puts the original value from \p Addr in \p OldValRes.
1768   ///
1769   /// \pre setBasicBlock or setMI must have been called.
1770   /// \pre \p OldValRes must be a generic virtual register.
1771   /// \pre \p Addr must be a generic virtual register with pointer type.
1772   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1773   ///      same type.
1774   ///
1775   /// \return a MachineInstrBuilder for the newly created instruction.
1776   MachineInstrBuilder buildAtomicRMWUSubSat(const DstOp &OldValRes,
1777                                             const SrcOp &Addr, const SrcOp &Val,
1778                                             MachineMemOperand &MMO);
1779 
1780   /// Build and insert `G_FENCE Ordering, Scope`.
1781   MachineInstrBuilder buildFence(unsigned Ordering, unsigned Scope);
1782 
1783   /// Build and insert G_PREFETCH \p Addr, \p RW, \p Locality, \p CacheType
1784   MachineInstrBuilder buildPrefetch(const SrcOp &Addr, unsigned RW,
1785                                     unsigned Locality, unsigned CacheType,
1786                                     MachineMemOperand &MMO);
1787 
1788   /// Build and insert \p Dst = G_FREEZE \p Src
buildFreeze(const DstOp & Dst,const SrcOp & Src)1789   MachineInstrBuilder buildFreeze(const DstOp &Dst, const SrcOp &Src) {
1790     return buildInstr(TargetOpcode::G_FREEZE, {Dst}, {Src});
1791   }
1792 
1793   /// Build and insert \p Res = G_BLOCK_ADDR \p BA
1794   ///
1795   /// G_BLOCK_ADDR computes the address of a basic block.
1796   ///
1797   /// \pre setBasicBlock or setMI must have been called.
1798   /// \pre \p Res must be a generic virtual register of a pointer type.
1799   ///
1800   /// \return The newly created instruction.
1801   MachineInstrBuilder buildBlockAddress(Register Res, const BlockAddress *BA);
1802 
1803   /// Build and insert \p Res = G_ADD \p Op0, \p Op1
1804   ///
1805   /// G_ADD sets \p Res to the sum of integer parameters \p Op0 and \p Op1,
1806   /// truncated to their width.
1807   ///
1808   /// \pre setBasicBlock or setMI must have been called.
1809   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1810   ///      with the same (scalar or vector) type).
1811   ///
1812   /// \return a MachineInstrBuilder for the newly created instruction.
1813 
1814   MachineInstrBuilder buildAdd(const DstOp &Dst, const SrcOp &Src0,
1815                                const SrcOp &Src1,
1816                                std::optional<unsigned> Flags = std::nullopt) {
1817     return buildInstr(TargetOpcode::G_ADD, {Dst}, {Src0, Src1}, Flags);
1818   }
1819 
1820   /// Build and insert \p Res = G_SUB \p Op0, \p Op1
1821   ///
1822   /// G_SUB sets \p Res to the difference of integer parameters \p Op0 and
1823   /// \p Op1, truncated to their width.
1824   ///
1825   /// \pre setBasicBlock or setMI must have been called.
1826   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1827   ///      with the same (scalar or vector) type).
1828   ///
1829   /// \return a MachineInstrBuilder for the newly created instruction.
1830 
1831   MachineInstrBuilder buildSub(const DstOp &Dst, const SrcOp &Src0,
1832                                const SrcOp &Src1,
1833                                std::optional<unsigned> Flags = std::nullopt) {
1834     return buildInstr(TargetOpcode::G_SUB, {Dst}, {Src0, Src1}, Flags);
1835   }
1836 
1837   /// Build and insert \p Res = G_MUL \p Op0, \p Op1
1838   ///
1839   /// G_MUL sets \p Res to the product of integer parameters \p Op0 and \p Op1,
1840   /// truncated to their width.
1841   ///
1842   /// \pre setBasicBlock or setMI must have been called.
1843   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1844   ///      with the same (scalar or vector) type).
1845   ///
1846   /// \return a MachineInstrBuilder for the newly created instruction.
1847   MachineInstrBuilder buildMul(const DstOp &Dst, const SrcOp &Src0,
1848                                const SrcOp &Src1,
1849                                std::optional<unsigned> Flags = std::nullopt) {
1850     return buildInstr(TargetOpcode::G_MUL, {Dst}, {Src0, Src1}, Flags);
1851   }
1852 
1853   /// Build and insert \p Res = G_ABDS \p Op0, \p Op1
1854   ///
1855   /// G_ABDS return the signed absolute difference of \p Op0 and \p Op1.
1856   ///
1857   /// \pre setBasicBlock or setMI must have been called.
1858   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1859   ///      with the same (scalar or vector) type).
1860   ///
1861   /// \return a MachineInstrBuilder for the newly created instruction.
buildAbds(const DstOp & Dst,const SrcOp & Src0,const SrcOp & Src1)1862   MachineInstrBuilder buildAbds(const DstOp &Dst, const SrcOp &Src0,
1863                                 const SrcOp &Src1) {
1864     return buildInstr(TargetOpcode::G_ABDS, {Dst}, {Src0, Src1});
1865   }
1866 
1867   /// Build and insert \p Res = G_ABDU \p Op0, \p Op1
1868   ///
1869   /// G_ABDU return the unsigned absolute difference of \p Op0 and \p Op1.
1870   ///
1871   /// \pre setBasicBlock or setMI must have been called.
1872   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1873   ///      with the same (scalar or vector) type).
1874   ///
1875   /// \return a MachineInstrBuilder for the newly created instruction.
buildAbdu(const DstOp & Dst,const SrcOp & Src0,const SrcOp & Src1)1876   MachineInstrBuilder buildAbdu(const DstOp &Dst, const SrcOp &Src0,
1877                                 const SrcOp &Src1) {
1878     return buildInstr(TargetOpcode::G_ABDU, {Dst}, {Src0, Src1});
1879   }
1880 
1881   MachineInstrBuilder buildUMulH(const DstOp &Dst, const SrcOp &Src0,
1882                                  const SrcOp &Src1,
1883                                  std::optional<unsigned> Flags = std::nullopt) {
1884     return buildInstr(TargetOpcode::G_UMULH, {Dst}, {Src0, Src1}, Flags);
1885   }
1886 
1887   MachineInstrBuilder buildSMulH(const DstOp &Dst, const SrcOp &Src0,
1888                                  const SrcOp &Src1,
1889                                  std::optional<unsigned> Flags = std::nullopt) {
1890     return buildInstr(TargetOpcode::G_SMULH, {Dst}, {Src0, Src1}, Flags);
1891   }
1892 
1893   /// Build and insert \p Res = G_UREM \p Op0, \p Op1
1894   MachineInstrBuilder buildURem(const DstOp &Dst, const SrcOp &Src0,
1895                                 const SrcOp &Src1,
1896                                 std::optional<unsigned> Flags = std::nullopt) {
1897     return buildInstr(TargetOpcode::G_UREM, {Dst}, {Src0, Src1}, Flags);
1898   }
1899 
1900   MachineInstrBuilder buildFMul(const DstOp &Dst, const SrcOp &Src0,
1901                                 const SrcOp &Src1,
1902                                 std::optional<unsigned> Flags = std::nullopt) {
1903     return buildInstr(TargetOpcode::G_FMUL, {Dst}, {Src0, Src1}, Flags);
1904   }
1905 
1906   MachineInstrBuilder
1907   buildFMinNum(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
1908                std::optional<unsigned> Flags = std::nullopt) {
1909     return buildInstr(TargetOpcode::G_FMINNUM, {Dst}, {Src0, Src1}, Flags);
1910   }
1911 
1912   MachineInstrBuilder
1913   buildFMaxNum(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
1914                std::optional<unsigned> Flags = std::nullopt) {
1915     return buildInstr(TargetOpcode::G_FMAXNUM, {Dst}, {Src0, Src1}, Flags);
1916   }
1917 
1918   MachineInstrBuilder
1919   buildFMinNumIEEE(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
1920                    std::optional<unsigned> Flags = std::nullopt) {
1921     return buildInstr(TargetOpcode::G_FMINNUM_IEEE, {Dst}, {Src0, Src1}, Flags);
1922   }
1923 
1924   MachineInstrBuilder
1925   buildFMaxNumIEEE(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
1926                    std::optional<unsigned> Flags = std::nullopt) {
1927     return buildInstr(TargetOpcode::G_FMAXNUM_IEEE, {Dst}, {Src0, Src1}, Flags);
1928   }
1929 
1930   MachineInstrBuilder buildShl(const DstOp &Dst, const SrcOp &Src0,
1931                                const SrcOp &Src1,
1932                                std::optional<unsigned> Flags = std::nullopt) {
1933     return buildInstr(TargetOpcode::G_SHL, {Dst}, {Src0, Src1}, Flags);
1934   }
1935 
1936   MachineInstrBuilder buildLShr(const DstOp &Dst, const SrcOp &Src0,
1937                                 const SrcOp &Src1,
1938                                 std::optional<unsigned> Flags = std::nullopt) {
1939     return buildInstr(TargetOpcode::G_LSHR, {Dst}, {Src0, Src1}, Flags);
1940   }
1941 
1942   MachineInstrBuilder buildAShr(const DstOp &Dst, const SrcOp &Src0,
1943                                 const SrcOp &Src1,
1944                                 std::optional<unsigned> Flags = std::nullopt) {
1945     return buildInstr(TargetOpcode::G_ASHR, {Dst}, {Src0, Src1}, Flags);
1946   }
1947 
1948   /// Build and insert \p Res = G_AND \p Op0, \p Op1
1949   ///
1950   /// G_AND sets \p Res to the bitwise and of integer parameters \p Op0 and \p
1951   /// Op1.
1952   ///
1953   /// \pre setBasicBlock or setMI must have been called.
1954   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1955   ///      with the same (scalar or vector) type).
1956   ///
1957   /// \return a MachineInstrBuilder for the newly created instruction.
1958 
buildAnd(const DstOp & Dst,const SrcOp & Src0,const SrcOp & Src1)1959   MachineInstrBuilder buildAnd(const DstOp &Dst, const SrcOp &Src0,
1960                                const SrcOp &Src1) {
1961     return buildInstr(TargetOpcode::G_AND, {Dst}, {Src0, Src1});
1962   }
1963 
1964   /// Build and insert \p Res = G_OR \p Op0, \p Op1
1965   ///
1966   /// G_OR sets \p Res to the bitwise or of integer parameters \p Op0 and \p
1967   /// Op1.
1968   ///
1969   /// \pre setBasicBlock or setMI must have been called.
1970   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1971   ///      with the same (scalar or vector) type).
1972   ///
1973   /// \return a MachineInstrBuilder for the newly created instruction.
1974   MachineInstrBuilder buildOr(const DstOp &Dst, const SrcOp &Src0,
1975                               const SrcOp &Src1,
1976                               std::optional<unsigned> Flags = std::nullopt) {
1977     return buildInstr(TargetOpcode::G_OR, {Dst}, {Src0, Src1}, Flags);
1978   }
1979 
1980   /// Build and insert \p Res = G_XOR \p Op0, \p Op1
buildXor(const DstOp & Dst,const SrcOp & Src0,const SrcOp & Src1)1981   MachineInstrBuilder buildXor(const DstOp &Dst, const SrcOp &Src0,
1982                                const SrcOp &Src1) {
1983     return buildInstr(TargetOpcode::G_XOR, {Dst}, {Src0, Src1});
1984   }
1985 
1986   /// Build and insert a bitwise not,
1987   /// \p NegOne = G_CONSTANT -1
1988   /// \p Res = G_OR \p Op0, NegOne
buildNot(const DstOp & Dst,const SrcOp & Src0)1989   MachineInstrBuilder buildNot(const DstOp &Dst, const SrcOp &Src0) {
1990     auto NegOne = buildConstant(Dst.getLLTTy(*getMRI()), -1);
1991     return buildInstr(TargetOpcode::G_XOR, {Dst}, {Src0, NegOne});
1992   }
1993 
1994   /// Build and insert integer negation
1995   /// \p Zero = G_CONSTANT 0
1996   /// \p Res = G_SUB Zero, \p Op0
buildNeg(const DstOp & Dst,const SrcOp & Src0)1997   MachineInstrBuilder buildNeg(const DstOp &Dst, const SrcOp &Src0) {
1998     auto Zero = buildConstant(Dst.getLLTTy(*getMRI()), 0);
1999     return buildInstr(TargetOpcode::G_SUB, {Dst}, {Zero, Src0});
2000   }
2001 
2002   /// Build and insert \p Res = G_CTPOP \p Op0, \p Src0
buildCTPOP(const DstOp & Dst,const SrcOp & Src0)2003   MachineInstrBuilder buildCTPOP(const DstOp &Dst, const SrcOp &Src0) {
2004     return buildInstr(TargetOpcode::G_CTPOP, {Dst}, {Src0});
2005   }
2006 
2007   /// Build and insert \p Res = G_CTLZ \p Op0, \p Src0
buildCTLZ(const DstOp & Dst,const SrcOp & Src0)2008   MachineInstrBuilder buildCTLZ(const DstOp &Dst, const SrcOp &Src0) {
2009     return buildInstr(TargetOpcode::G_CTLZ, {Dst}, {Src0});
2010   }
2011 
2012   /// Build and insert \p Res = G_CTLZ_ZERO_UNDEF \p Op0, \p Src0
buildCTLZ_ZERO_UNDEF(const DstOp & Dst,const SrcOp & Src0)2013   MachineInstrBuilder buildCTLZ_ZERO_UNDEF(const DstOp &Dst, const SrcOp &Src0) {
2014     return buildInstr(TargetOpcode::G_CTLZ_ZERO_UNDEF, {Dst}, {Src0});
2015   }
2016 
2017   /// Build and insert \p Res = G_CTTZ \p Op0, \p Src0
buildCTTZ(const DstOp & Dst,const SrcOp & Src0)2018   MachineInstrBuilder buildCTTZ(const DstOp &Dst, const SrcOp &Src0) {
2019     return buildInstr(TargetOpcode::G_CTTZ, {Dst}, {Src0});
2020   }
2021 
2022   /// Build and insert \p Res = G_CTTZ_ZERO_UNDEF \p Op0, \p Src0
buildCTTZ_ZERO_UNDEF(const DstOp & Dst,const SrcOp & Src0)2023   MachineInstrBuilder buildCTTZ_ZERO_UNDEF(const DstOp &Dst, const SrcOp &Src0) {
2024     return buildInstr(TargetOpcode::G_CTTZ_ZERO_UNDEF, {Dst}, {Src0});
2025   }
2026 
2027   /// Build and insert \p Dst = G_BSWAP \p Src0
buildBSwap(const DstOp & Dst,const SrcOp & Src0)2028   MachineInstrBuilder buildBSwap(const DstOp &Dst, const SrcOp &Src0) {
2029     return buildInstr(TargetOpcode::G_BSWAP, {Dst}, {Src0});
2030   }
2031 
2032   /// Build and insert \p Res = G_FADD \p Op0, \p Op1
2033   MachineInstrBuilder buildFAdd(const DstOp &Dst, const SrcOp &Src0,
2034                                 const SrcOp &Src1,
2035                                 std::optional<unsigned> Flags = std::nullopt) {
2036     return buildInstr(TargetOpcode::G_FADD, {Dst}, {Src0, Src1}, Flags);
2037   }
2038 
2039   /// Build and insert \p Res = G_STRICT_FADD \p Op0, \p Op1
2040   MachineInstrBuilder
2041   buildStrictFAdd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
2042                   std::optional<unsigned> Flags = std::nullopt) {
2043     return buildInstr(TargetOpcode::G_STRICT_FADD, {Dst}, {Src0, Src1}, Flags);
2044   }
2045 
2046   /// Build and insert \p Res = G_FSUB \p Op0, \p Op1
2047   MachineInstrBuilder buildFSub(const DstOp &Dst, const SrcOp &Src0,
2048                                 const SrcOp &Src1,
2049                                 std::optional<unsigned> Flags = std::nullopt) {
2050     return buildInstr(TargetOpcode::G_FSUB, {Dst}, {Src0, Src1}, Flags);
2051   }
2052 
2053   /// Build and insert \p Res = G_FDIV \p Op0, \p Op1
2054   MachineInstrBuilder buildFDiv(const DstOp &Dst, const SrcOp &Src0,
2055                                 const SrcOp &Src1,
2056                                 std::optional<unsigned> Flags = std::nullopt) {
2057     return buildInstr(TargetOpcode::G_FDIV, {Dst}, {Src0, Src1}, Flags);
2058   }
2059 
2060   /// Build and insert \p Res = G_FMA \p Op0, \p Op1, \p Op2
2061   MachineInstrBuilder buildFMA(const DstOp &Dst, const SrcOp &Src0,
2062                                const SrcOp &Src1, const SrcOp &Src2,
2063                                std::optional<unsigned> Flags = std::nullopt) {
2064     return buildInstr(TargetOpcode::G_FMA, {Dst}, {Src0, Src1, Src2}, Flags);
2065   }
2066 
2067   /// Build and insert \p Res = G_FMAD \p Op0, \p Op1, \p Op2
2068   MachineInstrBuilder buildFMAD(const DstOp &Dst, const SrcOp &Src0,
2069                                 const SrcOp &Src1, const SrcOp &Src2,
2070                                 std::optional<unsigned> Flags = std::nullopt) {
2071     return buildInstr(TargetOpcode::G_FMAD, {Dst}, {Src0, Src1, Src2}, Flags);
2072   }
2073 
2074   /// Build and insert \p Res = G_FNEG \p Op0
2075   MachineInstrBuilder buildFNeg(const DstOp &Dst, const SrcOp &Src0,
2076                                 std::optional<unsigned> Flags = std::nullopt) {
2077     return buildInstr(TargetOpcode::G_FNEG, {Dst}, {Src0}, Flags);
2078   }
2079 
2080   /// Build and insert \p Res = G_FABS \p Op0
2081   MachineInstrBuilder buildFAbs(const DstOp &Dst, const SrcOp &Src0,
2082                                 std::optional<unsigned> Flags = std::nullopt) {
2083     return buildInstr(TargetOpcode::G_FABS, {Dst}, {Src0}, Flags);
2084   }
2085 
2086   /// Build and insert \p Dst = G_FCANONICALIZE \p Src0
2087   MachineInstrBuilder
2088   buildFCanonicalize(const DstOp &Dst, const SrcOp &Src0,
2089                      std::optional<unsigned> Flags = std::nullopt) {
2090     return buildInstr(TargetOpcode::G_FCANONICALIZE, {Dst}, {Src0}, Flags);
2091   }
2092 
2093   /// Build and insert \p Dst = G_INTRINSIC_TRUNC \p Src0
2094   MachineInstrBuilder
2095   buildIntrinsicTrunc(const DstOp &Dst, const SrcOp &Src0,
2096                       std::optional<unsigned> Flags = std::nullopt) {
2097     return buildInstr(TargetOpcode::G_INTRINSIC_TRUNC, {Dst}, {Src0}, Flags);
2098   }
2099 
2100   /// Build and insert \p Res = GFFLOOR \p Op0, \p Op1
2101   MachineInstrBuilder
2102   buildFFloor(const DstOp &Dst, const SrcOp &Src0,
2103               std::optional<unsigned> Flags = std::nullopt) {
2104     return buildInstr(TargetOpcode::G_FFLOOR, {Dst}, {Src0}, Flags);
2105   }
2106 
2107   /// Build and insert \p Dst = G_FLOG \p Src
2108   MachineInstrBuilder buildFLog(const DstOp &Dst, const SrcOp &Src,
2109                                 std::optional<unsigned> Flags = std::nullopt) {
2110     return buildInstr(TargetOpcode::G_FLOG, {Dst}, {Src}, Flags);
2111   }
2112 
2113   /// Build and insert \p Dst = G_FLOG2 \p Src
2114   MachineInstrBuilder buildFLog2(const DstOp &Dst, const SrcOp &Src,
2115                                  std::optional<unsigned> Flags = std::nullopt) {
2116     return buildInstr(TargetOpcode::G_FLOG2, {Dst}, {Src}, Flags);
2117   }
2118 
2119   /// Build and insert \p Dst = G_FEXP2 \p Src
2120   MachineInstrBuilder buildFExp2(const DstOp &Dst, const SrcOp &Src,
2121                                  std::optional<unsigned> Flags = std::nullopt) {
2122     return buildInstr(TargetOpcode::G_FEXP2, {Dst}, {Src}, Flags);
2123   }
2124 
2125   /// Build and insert \p Dst = G_FPOW \p Src0, \p Src1
2126   MachineInstrBuilder buildFPow(const DstOp &Dst, const SrcOp &Src0,
2127                                 const SrcOp &Src1,
2128                                 std::optional<unsigned> Flags = std::nullopt) {
2129     return buildInstr(TargetOpcode::G_FPOW, {Dst}, {Src0, Src1}, Flags);
2130   }
2131 
2132   /// Build and insert \p Dst = G_FLDEXP \p Src0, \p Src1
2133   MachineInstrBuilder
2134   buildFLdexp(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
2135               std::optional<unsigned> Flags = std::nullopt) {
2136     return buildInstr(TargetOpcode::G_FLDEXP, {Dst}, {Src0, Src1}, Flags);
2137   }
2138 
2139   /// Build and insert \p Fract, \p Exp = G_FFREXP \p Src
2140   MachineInstrBuilder
2141   buildFFrexp(const DstOp &Fract, const DstOp &Exp, const SrcOp &Src,
2142               std::optional<unsigned> Flags = std::nullopt) {
2143     return buildInstr(TargetOpcode::G_FFREXP, {Fract, Exp}, {Src}, Flags);
2144   }
2145 
2146   /// Build and insert \p Sin, \p Cos = G_FSINCOS \p Src
2147   MachineInstrBuilder
2148   buildFSincos(const DstOp &Sin, const DstOp &Cos, const SrcOp &Src,
2149                std::optional<unsigned> Flags = std::nullopt) {
2150     return buildInstr(TargetOpcode::G_FSINCOS, {Sin, Cos}, {Src}, Flags);
2151   }
2152 
2153   /// Build and insert \p Res = G_FCOPYSIGN \p Op0, \p Op1
buildFCopysign(const DstOp & Dst,const SrcOp & Src0,const SrcOp & Src1)2154   MachineInstrBuilder buildFCopysign(const DstOp &Dst, const SrcOp &Src0,
2155                                      const SrcOp &Src1) {
2156     return buildInstr(TargetOpcode::G_FCOPYSIGN, {Dst}, {Src0, Src1});
2157   }
2158 
2159   /// Build and insert \p Res = G_UITOFP \p Src0
buildUITOFP(const DstOp & Dst,const SrcOp & Src0)2160   MachineInstrBuilder buildUITOFP(const DstOp &Dst, const SrcOp &Src0) {
2161     return buildInstr(TargetOpcode::G_UITOFP, {Dst}, {Src0});
2162   }
2163 
2164   /// Build and insert \p Res = G_SITOFP \p Src0
buildSITOFP(const DstOp & Dst,const SrcOp & Src0)2165   MachineInstrBuilder buildSITOFP(const DstOp &Dst, const SrcOp &Src0) {
2166     return buildInstr(TargetOpcode::G_SITOFP, {Dst}, {Src0});
2167   }
2168 
2169   /// Build and insert \p Res = G_FPTOUI \p Src0
buildFPTOUI(const DstOp & Dst,const SrcOp & Src0)2170   MachineInstrBuilder buildFPTOUI(const DstOp &Dst, const SrcOp &Src0) {
2171     return buildInstr(TargetOpcode::G_FPTOUI, {Dst}, {Src0});
2172   }
2173 
2174   /// Build and insert \p Res = G_FPTOSI \p Src0
buildFPTOSI(const DstOp & Dst,const SrcOp & Src0)2175   MachineInstrBuilder buildFPTOSI(const DstOp &Dst, const SrcOp &Src0) {
2176     return buildInstr(TargetOpcode::G_FPTOSI, {Dst}, {Src0});
2177   }
2178 
2179   /// Build and insert \p Res = G_FPTOUI_SAT \p Src0
buildFPTOUI_SAT(const DstOp & Dst,const SrcOp & Src0)2180   MachineInstrBuilder buildFPTOUI_SAT(const DstOp &Dst, const SrcOp &Src0) {
2181     return buildInstr(TargetOpcode::G_FPTOUI_SAT, {Dst}, {Src0});
2182   }
2183 
2184   /// Build and insert \p Res = G_FPTOSI_SAT \p Src0
buildFPTOSI_SAT(const DstOp & Dst,const SrcOp & Src0)2185   MachineInstrBuilder buildFPTOSI_SAT(const DstOp &Dst, const SrcOp &Src0) {
2186     return buildInstr(TargetOpcode::G_FPTOSI_SAT, {Dst}, {Src0});
2187   }
2188 
2189   /// Build and insert \p Dst = G_INTRINSIC_ROUNDEVEN \p Src0, \p Src1
2190   MachineInstrBuilder
2191   buildIntrinsicRoundeven(const DstOp &Dst, const SrcOp &Src0,
2192                           std::optional<unsigned> Flags = std::nullopt) {
2193     return buildInstr(TargetOpcode::G_INTRINSIC_ROUNDEVEN, {Dst}, {Src0},
2194                       Flags);
2195   }
2196 
2197   /// Build and insert \p Res = G_SMIN \p Op0, \p Op1
buildSMin(const DstOp & Dst,const SrcOp & Src0,const SrcOp & Src1)2198   MachineInstrBuilder buildSMin(const DstOp &Dst, const SrcOp &Src0,
2199                                 const SrcOp &Src1) {
2200     return buildInstr(TargetOpcode::G_SMIN, {Dst}, {Src0, Src1});
2201   }
2202 
2203   /// Build and insert \p Res = G_SMAX \p Op0, \p Op1
buildSMax(const DstOp & Dst,const SrcOp & Src0,const SrcOp & Src1)2204   MachineInstrBuilder buildSMax(const DstOp &Dst, const SrcOp &Src0,
2205                                 const SrcOp &Src1) {
2206     return buildInstr(TargetOpcode::G_SMAX, {Dst}, {Src0, Src1});
2207   }
2208 
2209   /// Build and insert \p Res = G_UMIN \p Op0, \p Op1
buildUMin(const DstOp & Dst,const SrcOp & Src0,const SrcOp & Src1)2210   MachineInstrBuilder buildUMin(const DstOp &Dst, const SrcOp &Src0,
2211                                 const SrcOp &Src1) {
2212     return buildInstr(TargetOpcode::G_UMIN, {Dst}, {Src0, Src1});
2213   }
2214 
2215   /// Build and insert \p Res = G_UMAX \p Op0, \p Op1
buildUMax(const DstOp & Dst,const SrcOp & Src0,const SrcOp & Src1)2216   MachineInstrBuilder buildUMax(const DstOp &Dst, const SrcOp &Src0,
2217                                 const SrcOp &Src1) {
2218     return buildInstr(TargetOpcode::G_UMAX, {Dst}, {Src0, Src1});
2219   }
2220 
2221   /// Build and insert \p Dst = G_ABS \p Src
buildAbs(const DstOp & Dst,const SrcOp & Src)2222   MachineInstrBuilder buildAbs(const DstOp &Dst, const SrcOp &Src) {
2223     return buildInstr(TargetOpcode::G_ABS, {Dst}, {Src});
2224   }
2225 
2226   /// Build and insert \p Res = G_JUMP_TABLE \p JTI
2227   ///
2228   /// G_JUMP_TABLE sets \p Res to the address of the jump table specified by
2229   /// the jump table index \p JTI.
2230   ///
2231   /// \return a MachineInstrBuilder for the newly created instruction.
2232   MachineInstrBuilder buildJumpTable(const LLT PtrTy, unsigned JTI);
2233 
2234   /// Build and insert \p Res = G_VECREDUCE_SEQ_FADD \p ScalarIn, \p VecIn
2235   ///
2236   /// \p ScalarIn is the scalar accumulator input to start the sequential
2237   /// reduction operation of \p VecIn.
buildVecReduceSeqFAdd(const DstOp & Dst,const SrcOp & ScalarIn,const SrcOp & VecIn)2238   MachineInstrBuilder buildVecReduceSeqFAdd(const DstOp &Dst,
2239                                             const SrcOp &ScalarIn,
2240                                             const SrcOp &VecIn) {
2241     return buildInstr(TargetOpcode::G_VECREDUCE_SEQ_FADD, {Dst},
2242                       {ScalarIn, {VecIn}});
2243   }
2244 
2245   /// Build and insert \p Res = G_VECREDUCE_SEQ_FMUL \p ScalarIn, \p VecIn
2246   ///
2247   /// \p ScalarIn is the scalar accumulator input to start the sequential
2248   /// reduction operation of \p VecIn.
buildVecReduceSeqFMul(const DstOp & Dst,const SrcOp & ScalarIn,const SrcOp & VecIn)2249   MachineInstrBuilder buildVecReduceSeqFMul(const DstOp &Dst,
2250                                             const SrcOp &ScalarIn,
2251                                             const SrcOp &VecIn) {
2252     return buildInstr(TargetOpcode::G_VECREDUCE_SEQ_FMUL, {Dst},
2253                       {ScalarIn, {VecIn}});
2254   }
2255 
2256   /// Build and insert \p Res = G_VECREDUCE_FADD \p Src
2257   ///
2258   /// \p ScalarIn is the scalar accumulator input to the reduction operation of
2259   /// \p VecIn.
buildVecReduceFAdd(const DstOp & Dst,const SrcOp & ScalarIn,const SrcOp & VecIn)2260   MachineInstrBuilder buildVecReduceFAdd(const DstOp &Dst,
2261                                          const SrcOp &ScalarIn,
2262                                          const SrcOp &VecIn) {
2263     return buildInstr(TargetOpcode::G_VECREDUCE_FADD, {Dst}, {ScalarIn, VecIn});
2264   }
2265 
2266   /// Build and insert \p Res = G_VECREDUCE_FMUL \p Src
2267   ///
2268   /// \p ScalarIn is the scalar accumulator input to the reduction operation of
2269   /// \p VecIn.
buildVecReduceFMul(const DstOp & Dst,const SrcOp & ScalarIn,const SrcOp & VecIn)2270   MachineInstrBuilder buildVecReduceFMul(const DstOp &Dst,
2271                                          const SrcOp &ScalarIn,
2272                                          const SrcOp &VecIn) {
2273     return buildInstr(TargetOpcode::G_VECREDUCE_FMUL, {Dst}, {ScalarIn, VecIn});
2274   }
2275 
2276   /// Build and insert \p Res = G_VECREDUCE_FMAX \p Src
buildVecReduceFMax(const DstOp & Dst,const SrcOp & Src)2277   MachineInstrBuilder buildVecReduceFMax(const DstOp &Dst, const SrcOp &Src) {
2278     return buildInstr(TargetOpcode::G_VECREDUCE_FMAX, {Dst}, {Src});
2279   }
2280 
2281   /// Build and insert \p Res = G_VECREDUCE_FMIN \p Src
buildVecReduceFMin(const DstOp & Dst,const SrcOp & Src)2282   MachineInstrBuilder buildVecReduceFMin(const DstOp &Dst, const SrcOp &Src) {
2283     return buildInstr(TargetOpcode::G_VECREDUCE_FMIN, {Dst}, {Src});
2284   }
2285 
2286   /// Build and insert \p Res = G_VECREDUCE_FMAXIMUM \p Src
buildVecReduceFMaximum(const DstOp & Dst,const SrcOp & Src)2287   MachineInstrBuilder buildVecReduceFMaximum(const DstOp &Dst,
2288                                              const SrcOp &Src) {
2289     return buildInstr(TargetOpcode::G_VECREDUCE_FMAXIMUM, {Dst}, {Src});
2290   }
2291 
2292   /// Build and insert \p Res = G_VECREDUCE_FMINIMUM \p Src
buildVecReduceFMinimum(const DstOp & Dst,const SrcOp & Src)2293   MachineInstrBuilder buildVecReduceFMinimum(const DstOp &Dst,
2294                                              const SrcOp &Src) {
2295     return buildInstr(TargetOpcode::G_VECREDUCE_FMINIMUM, {Dst}, {Src});
2296   }
2297 
2298   /// Build and insert \p Res = G_VECREDUCE_ADD \p Src
buildVecReduceAdd(const DstOp & Dst,const SrcOp & Src)2299   MachineInstrBuilder buildVecReduceAdd(const DstOp &Dst, const SrcOp &Src) {
2300     return buildInstr(TargetOpcode::G_VECREDUCE_ADD, {Dst}, {Src});
2301   }
2302 
2303   /// Build and insert \p Res = G_VECREDUCE_MUL \p Src
buildVecReduceMul(const DstOp & Dst,const SrcOp & Src)2304   MachineInstrBuilder buildVecReduceMul(const DstOp &Dst, const SrcOp &Src) {
2305     return buildInstr(TargetOpcode::G_VECREDUCE_MUL, {Dst}, {Src});
2306   }
2307 
2308   /// Build and insert \p Res = G_VECREDUCE_AND \p Src
buildVecReduceAnd(const DstOp & Dst,const SrcOp & Src)2309   MachineInstrBuilder buildVecReduceAnd(const DstOp &Dst, const SrcOp &Src) {
2310     return buildInstr(TargetOpcode::G_VECREDUCE_AND, {Dst}, {Src});
2311   }
2312 
2313   /// Build and insert \p Res = G_VECREDUCE_OR \p Src
buildVecReduceOr(const DstOp & Dst,const SrcOp & Src)2314   MachineInstrBuilder buildVecReduceOr(const DstOp &Dst, const SrcOp &Src) {
2315     return buildInstr(TargetOpcode::G_VECREDUCE_OR, {Dst}, {Src});
2316   }
2317 
2318   /// Build and insert \p Res = G_VECREDUCE_XOR \p Src
buildVecReduceXor(const DstOp & Dst,const SrcOp & Src)2319   MachineInstrBuilder buildVecReduceXor(const DstOp &Dst, const SrcOp &Src) {
2320     return buildInstr(TargetOpcode::G_VECREDUCE_XOR, {Dst}, {Src});
2321   }
2322 
2323   /// Build and insert \p Res = G_VECREDUCE_SMAX \p Src
buildVecReduceSMax(const DstOp & Dst,const SrcOp & Src)2324   MachineInstrBuilder buildVecReduceSMax(const DstOp &Dst, const SrcOp &Src) {
2325     return buildInstr(TargetOpcode::G_VECREDUCE_SMAX, {Dst}, {Src});
2326   }
2327 
2328   /// Build and insert \p Res = G_VECREDUCE_SMIN \p Src
buildVecReduceSMin(const DstOp & Dst,const SrcOp & Src)2329   MachineInstrBuilder buildVecReduceSMin(const DstOp &Dst, const SrcOp &Src) {
2330     return buildInstr(TargetOpcode::G_VECREDUCE_SMIN, {Dst}, {Src});
2331   }
2332 
2333   /// Build and insert \p Res = G_VECREDUCE_UMAX \p Src
buildVecReduceUMax(const DstOp & Dst,const SrcOp & Src)2334   MachineInstrBuilder buildVecReduceUMax(const DstOp &Dst, const SrcOp &Src) {
2335     return buildInstr(TargetOpcode::G_VECREDUCE_UMAX, {Dst}, {Src});
2336   }
2337 
2338   /// Build and insert \p Res = G_VECREDUCE_UMIN \p Src
buildVecReduceUMin(const DstOp & Dst,const SrcOp & Src)2339   MachineInstrBuilder buildVecReduceUMin(const DstOp &Dst, const SrcOp &Src) {
2340     return buildInstr(TargetOpcode::G_VECREDUCE_UMIN, {Dst}, {Src});
2341   }
2342 
2343   /// Build and insert G_MEMCPY or G_MEMMOVE
buildMemTransferInst(unsigned Opcode,const SrcOp & DstPtr,const SrcOp & SrcPtr,const SrcOp & Size,MachineMemOperand & DstMMO,MachineMemOperand & SrcMMO)2344   MachineInstrBuilder buildMemTransferInst(unsigned Opcode, const SrcOp &DstPtr,
2345                                            const SrcOp &SrcPtr,
2346                                            const SrcOp &Size,
2347                                            MachineMemOperand &DstMMO,
2348                                            MachineMemOperand &SrcMMO) {
2349     auto MIB = buildInstr(
2350         Opcode, {}, {DstPtr, SrcPtr, Size, SrcOp(INT64_C(0) /*isTailCall*/)});
2351     MIB.addMemOperand(&DstMMO);
2352     MIB.addMemOperand(&SrcMMO);
2353     return MIB;
2354   }
2355 
buildMemCpy(const SrcOp & DstPtr,const SrcOp & SrcPtr,const SrcOp & Size,MachineMemOperand & DstMMO,MachineMemOperand & SrcMMO)2356   MachineInstrBuilder buildMemCpy(const SrcOp &DstPtr, const SrcOp &SrcPtr,
2357                                   const SrcOp &Size, MachineMemOperand &DstMMO,
2358                                   MachineMemOperand &SrcMMO) {
2359     return buildMemTransferInst(TargetOpcode::G_MEMCPY, DstPtr, SrcPtr, Size,
2360                                 DstMMO, SrcMMO);
2361   }
2362 
2363   /// Build and insert G_TRAP or G_DEBUGTRAP
2364   MachineInstrBuilder buildTrap(bool Debug = false) {
2365     return buildInstr(Debug ? TargetOpcode::G_DEBUGTRAP : TargetOpcode::G_TRAP);
2366   }
2367 
2368   /// Build and insert \p Dst = G_SBFX \p Src, \p LSB, \p Width.
buildSbfx(const DstOp & Dst,const SrcOp & Src,const SrcOp & LSB,const SrcOp & Width)2369   MachineInstrBuilder buildSbfx(const DstOp &Dst, const SrcOp &Src,
2370                                 const SrcOp &LSB, const SrcOp &Width) {
2371     return buildInstr(TargetOpcode::G_SBFX, {Dst}, {Src, LSB, Width});
2372   }
2373 
2374   /// Build and insert \p Dst = G_UBFX \p Src, \p LSB, \p Width.
buildUbfx(const DstOp & Dst,const SrcOp & Src,const SrcOp & LSB,const SrcOp & Width)2375   MachineInstrBuilder buildUbfx(const DstOp &Dst, const SrcOp &Src,
2376                                 const SrcOp &LSB, const SrcOp &Width) {
2377     return buildInstr(TargetOpcode::G_UBFX, {Dst}, {Src, LSB, Width});
2378   }
2379 
2380   /// Build and insert \p Dst = G_ROTR \p Src, \p Amt
buildRotateRight(const DstOp & Dst,const SrcOp & Src,const SrcOp & Amt)2381   MachineInstrBuilder buildRotateRight(const DstOp &Dst, const SrcOp &Src,
2382                                        const SrcOp &Amt) {
2383     return buildInstr(TargetOpcode::G_ROTR, {Dst}, {Src, Amt});
2384   }
2385 
2386   /// Build and insert \p Dst = G_ROTL \p Src, \p Amt
buildRotateLeft(const DstOp & Dst,const SrcOp & Src,const SrcOp & Amt)2387   MachineInstrBuilder buildRotateLeft(const DstOp &Dst, const SrcOp &Src,
2388                                       const SrcOp &Amt) {
2389     return buildInstr(TargetOpcode::G_ROTL, {Dst}, {Src, Amt});
2390   }
2391 
2392   /// Build and insert \p Dst = G_BITREVERSE \p Src
buildBitReverse(const DstOp & Dst,const SrcOp & Src)2393   MachineInstrBuilder buildBitReverse(const DstOp &Dst, const SrcOp &Src) {
2394     return buildInstr(TargetOpcode::G_BITREVERSE, {Dst}, {Src});
2395   }
2396 
2397   /// Build and insert \p Dst = G_GET_FPENV
buildGetFPEnv(const DstOp & Dst)2398   MachineInstrBuilder buildGetFPEnv(const DstOp &Dst) {
2399     return buildInstr(TargetOpcode::G_GET_FPENV, {Dst}, {});
2400   }
2401 
2402   /// Build and insert G_SET_FPENV \p Src
buildSetFPEnv(const SrcOp & Src)2403   MachineInstrBuilder buildSetFPEnv(const SrcOp &Src) {
2404     return buildInstr(TargetOpcode::G_SET_FPENV, {}, {Src});
2405   }
2406 
2407   /// Build and insert G_RESET_FPENV
buildResetFPEnv()2408   MachineInstrBuilder buildResetFPEnv() {
2409     return buildInstr(TargetOpcode::G_RESET_FPENV, {}, {});
2410   }
2411 
2412   /// Build and insert \p Dst = G_GET_FPMODE
buildGetFPMode(const DstOp & Dst)2413   MachineInstrBuilder buildGetFPMode(const DstOp &Dst) {
2414     return buildInstr(TargetOpcode::G_GET_FPMODE, {Dst}, {});
2415   }
2416 
2417   /// Build and insert G_SET_FPMODE \p Src
buildSetFPMode(const SrcOp & Src)2418   MachineInstrBuilder buildSetFPMode(const SrcOp &Src) {
2419     return buildInstr(TargetOpcode::G_SET_FPMODE, {}, {Src});
2420   }
2421 
2422   /// Build and insert G_RESET_FPMODE
buildResetFPMode()2423   MachineInstrBuilder buildResetFPMode() {
2424     return buildInstr(TargetOpcode::G_RESET_FPMODE, {}, {});
2425   }
2426 
2427   /// Build and insert \p Dst = G_GET_ROUNDING
buildGetRounding(const DstOp & Dst)2428   MachineInstrBuilder buildGetRounding(const DstOp &Dst) {
2429     return buildInstr(TargetOpcode::G_GET_ROUNDING, {Dst}, {});
2430   }
2431 
2432   virtual MachineInstrBuilder
2433   buildInstr(unsigned Opc, ArrayRef<DstOp> DstOps, ArrayRef<SrcOp> SrcOps,
2434              std::optional<unsigned> Flags = std::nullopt);
2435 };
2436 
2437 } // End namespace llvm.
2438 #endif // LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
2439