Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:25

0001 //===-- llvm/CodeGen/GlobalISel/MachineIRBuilder.h - MIBuilder --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 /// \file
0009 /// This file declares the MachineIRBuilder class.
0010 /// This is a helper class to build MachineInstr.
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
0014 #define LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
0015 
0016 #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
0017 #include "llvm/CodeGen/MachineBasicBlock.h"
0018 #include "llvm/CodeGen/MachineInstrBuilder.h"
0019 #include "llvm/CodeGen/MachineRegisterInfo.h"
0020 #include "llvm/CodeGen/TargetLowering.h"
0021 #include "llvm/CodeGen/TargetOpcodes.h"
0022 #include "llvm/IR/DebugLoc.h"
0023 #include "llvm/IR/Module.h"
0024 
0025 namespace llvm {
0026 
0027 // Forward declarations.
0028 class APInt;
0029 class BlockAddress;
0030 class Constant;
0031 class ConstantFP;
0032 class ConstantInt;
0033 class DataLayout;
0034 class GISelCSEInfo;
0035 class GlobalValue;
0036 class TargetRegisterClass;
0037 class MachineFunction;
0038 class MachineInstr;
0039 class TargetInstrInfo;
0040 class GISelChangeObserver;
0041 
0042 /// Class which stores all the state required in a MachineIRBuilder.
0043 /// Since MachineIRBuilders will only store state in this object, it allows
0044 /// to transfer BuilderState between different kinds of MachineIRBuilders.
0045 struct MachineIRBuilderState {
0046   /// MachineFunction under construction.
0047   MachineFunction *MF = nullptr;
0048   /// Information used to access the description of the opcodes.
0049   const TargetInstrInfo *TII = nullptr;
0050   /// Information used to verify types are consistent and to create virtual registers.
0051   MachineRegisterInfo *MRI = nullptr;
0052   /// Debug location to be set to any instruction we create.
0053   DebugLoc DL;
0054   /// PC sections metadata to be set to any instruction we create.
0055   MDNode *PCSections = nullptr;
0056   /// MMRA Metadata to be set on any instruction we create.
0057   MDNode *MMRA = nullptr;
0058 
0059   /// \name Fields describing the insertion point.
0060   /// @{
0061   MachineBasicBlock *MBB = nullptr;
0062   MachineBasicBlock::iterator II;
0063   /// @}
0064 
0065   GISelChangeObserver *Observer = nullptr;
0066 
0067   GISelCSEInfo *CSEInfo = nullptr;
0068 };
0069 
0070 class DstOp {
0071   union {
0072     LLT LLTTy;
0073     Register Reg;
0074     const TargetRegisterClass *RC;
0075     MachineRegisterInfo::VRegAttrs Attrs;
0076   };
0077 
0078 public:
0079   enum class DstType { Ty_LLT, Ty_Reg, Ty_RC, Ty_VRegAttrs };
0080   DstOp(unsigned R) : Reg(R), Ty(DstType::Ty_Reg) {}
0081   DstOp(Register R) : Reg(R), Ty(DstType::Ty_Reg) {}
0082   DstOp(const MachineOperand &Op) : Reg(Op.getReg()), Ty(DstType::Ty_Reg) {}
0083   DstOp(const LLT T) : LLTTy(T), Ty(DstType::Ty_LLT) {}
0084   DstOp(const TargetRegisterClass *TRC) : RC(TRC), Ty(DstType::Ty_RC) {}
0085   DstOp(MachineRegisterInfo::VRegAttrs Attrs)
0086       : Attrs(Attrs), Ty(DstType::Ty_VRegAttrs) {}
0087   DstOp(RegClassOrRegBank RCOrRB, LLT Ty)
0088       : Attrs({RCOrRB, Ty}), Ty(DstType::Ty_VRegAttrs) {}
0089 
0090   void addDefToMIB(MachineRegisterInfo &MRI, MachineInstrBuilder &MIB) const {
0091     switch (Ty) {
0092     case DstType::Ty_Reg:
0093       MIB.addDef(Reg);
0094       break;
0095     case DstType::Ty_LLT:
0096       MIB.addDef(MRI.createGenericVirtualRegister(LLTTy));
0097       break;
0098     case DstType::Ty_RC:
0099       MIB.addDef(MRI.createVirtualRegister(RC));
0100       break;
0101     case DstType::Ty_VRegAttrs:
0102       MIB.addDef(MRI.createVirtualRegister(Attrs));
0103       break;
0104     }
0105   }
0106 
0107   LLT getLLTTy(const MachineRegisterInfo &MRI) const {
0108     switch (Ty) {
0109     case DstType::Ty_RC:
0110       return LLT{};
0111     case DstType::Ty_LLT:
0112       return LLTTy;
0113     case DstType::Ty_Reg:
0114       return MRI.getType(Reg);
0115     case DstType::Ty_VRegAttrs:
0116       return Attrs.Ty;
0117     }
0118     llvm_unreachable("Unrecognised DstOp::DstType enum");
0119   }
0120 
0121   Register getReg() const {
0122     assert(Ty == DstType::Ty_Reg && "Not a register");
0123     return Reg;
0124   }
0125 
0126   const TargetRegisterClass *getRegClass() const {
0127     assert(Ty == DstType::Ty_RC && "Not a RC Operand");
0128     return RC;
0129   }
0130 
0131   MachineRegisterInfo::VRegAttrs getVRegAttrs() const {
0132     assert(Ty == DstType::Ty_VRegAttrs && "Not a VRegAttrs Operand");
0133     return Attrs;
0134   }
0135 
0136   DstType getDstOpKind() const { return Ty; }
0137 
0138 private:
0139   DstType Ty;
0140 };
0141 
0142 class SrcOp {
0143   union {
0144     MachineInstrBuilder SrcMIB;
0145     Register Reg;
0146     CmpInst::Predicate Pred;
0147     int64_t Imm;
0148   };
0149 
0150 public:
0151   enum class SrcType { Ty_Reg, Ty_MIB, Ty_Predicate, Ty_Imm };
0152   SrcOp(Register R) : Reg(R), Ty(SrcType::Ty_Reg) {}
0153   SrcOp(const MachineOperand &Op) : Reg(Op.getReg()), Ty(SrcType::Ty_Reg) {}
0154   SrcOp(const MachineInstrBuilder &MIB) : SrcMIB(MIB), Ty(SrcType::Ty_MIB) {}
0155   SrcOp(const CmpInst::Predicate P) : Pred(P), Ty(SrcType::Ty_Predicate) {}
0156   /// Use of registers held in unsigned integer variables (or more rarely signed
0157   /// integers) is no longer permitted to avoid ambiguity with upcoming support
0158   /// for immediates.
0159   SrcOp(unsigned) = delete;
0160   SrcOp(int) = delete;
0161   SrcOp(uint64_t V) : Imm(V), Ty(SrcType::Ty_Imm) {}
0162   SrcOp(int64_t V) : Imm(V), Ty(SrcType::Ty_Imm) {}
0163 
0164   void addSrcToMIB(MachineInstrBuilder &MIB) const {
0165     switch (Ty) {
0166     case SrcType::Ty_Predicate:
0167       MIB.addPredicate(Pred);
0168       break;
0169     case SrcType::Ty_Reg:
0170       MIB.addUse(Reg);
0171       break;
0172     case SrcType::Ty_MIB:
0173       MIB.addUse(SrcMIB->getOperand(0).getReg());
0174       break;
0175     case SrcType::Ty_Imm:
0176       MIB.addImm(Imm);
0177       break;
0178     }
0179   }
0180 
0181   LLT getLLTTy(const MachineRegisterInfo &MRI) const {
0182     switch (Ty) {
0183     case SrcType::Ty_Predicate:
0184     case SrcType::Ty_Imm:
0185       llvm_unreachable("Not a register operand");
0186     case SrcType::Ty_Reg:
0187       return MRI.getType(Reg);
0188     case SrcType::Ty_MIB:
0189       return MRI.getType(SrcMIB->getOperand(0).getReg());
0190     }
0191     llvm_unreachable("Unrecognised SrcOp::SrcType enum");
0192   }
0193 
0194   Register getReg() const {
0195     switch (Ty) {
0196     case SrcType::Ty_Predicate:
0197     case SrcType::Ty_Imm:
0198       llvm_unreachable("Not a register operand");
0199     case SrcType::Ty_Reg:
0200       return Reg;
0201     case SrcType::Ty_MIB:
0202       return SrcMIB->getOperand(0).getReg();
0203     }
0204     llvm_unreachable("Unrecognised SrcOp::SrcType enum");
0205   }
0206 
0207   CmpInst::Predicate getPredicate() const {
0208     switch (Ty) {
0209     case SrcType::Ty_Predicate:
0210       return Pred;
0211     default:
0212       llvm_unreachable("Not a register operand");
0213     }
0214   }
0215 
0216   int64_t getImm() const {
0217     switch (Ty) {
0218     case SrcType::Ty_Imm:
0219       return Imm;
0220     default:
0221       llvm_unreachable("Not an immediate");
0222     }
0223   }
0224 
0225   SrcType getSrcOpKind() const { return Ty; }
0226 
0227 private:
0228   SrcType Ty;
0229 };
0230 
0231 /// Helper class to build MachineInstr.
0232 /// It keeps internally the insertion point and debug location for all
0233 /// the new instructions we want to create.
0234 /// This information can be modified via the related setters.
0235 class MachineIRBuilder {
0236 
0237   MachineIRBuilderState State;
0238 
0239   unsigned getOpcodeForMerge(const DstOp &DstOp, ArrayRef<SrcOp> SrcOps) const;
0240 
0241 protected:
0242   void validateTruncExt(const LLT Dst, const LLT Src, bool IsExtend);
0243 
0244   void validateUnaryOp(const LLT Res, const LLT Op0);
0245   void validateBinaryOp(const LLT Res, const LLT Op0, const LLT Op1);
0246   void validateShiftOp(const LLT Res, const LLT Op0, const LLT Op1);
0247 
0248   void validateSelectOp(const LLT ResTy, const LLT TstTy, const LLT Op0Ty,
0249                         const LLT Op1Ty);
0250 
0251   void recordInsertion(MachineInstr *InsertedInstr) const {
0252     if (State.Observer)
0253       State.Observer->createdInstr(*InsertedInstr);
0254   }
0255 
0256 public:
0257   /// Some constructors for easy use.
0258   MachineIRBuilder() = default;
0259   MachineIRBuilder(MachineFunction &MF) { setMF(MF); }
0260 
0261   MachineIRBuilder(MachineBasicBlock &MBB, MachineBasicBlock::iterator InsPt) {
0262     setMF(*MBB.getParent());
0263     setInsertPt(MBB, InsPt);
0264   }
0265 
0266   MachineIRBuilder(MachineInstr &MI) :
0267     MachineIRBuilder(*MI.getParent(), MI.getIterator()) {
0268     setInstr(MI);
0269     setDebugLoc(MI.getDebugLoc());
0270   }
0271 
0272   MachineIRBuilder(MachineInstr &MI, GISelChangeObserver &Observer) :
0273     MachineIRBuilder(MI) {
0274     setChangeObserver(Observer);
0275   }
0276 
0277   virtual ~MachineIRBuilder() = default;
0278 
0279   MachineIRBuilder(const MachineIRBuilderState &BState) : State(BState) {}
0280 
0281   const TargetInstrInfo &getTII() {
0282     assert(State.TII && "TargetInstrInfo is not set");
0283     return *State.TII;
0284   }
0285 
0286   /// Getter for the function we currently build.
0287   MachineFunction &getMF() {
0288     assert(State.MF && "MachineFunction is not set");
0289     return *State.MF;
0290   }
0291 
0292   const MachineFunction &getMF() const {
0293     assert(State.MF && "MachineFunction is not set");
0294     return *State.MF;
0295   }
0296 
0297   const DataLayout &getDataLayout() const {
0298     return getMF().getFunction().getDataLayout();
0299   }
0300 
0301   LLVMContext &getContext() const {
0302     return getMF().getFunction().getContext();
0303   }
0304 
0305   /// Getter for DebugLoc
0306   const DebugLoc &getDL() { return State.DL; }
0307 
0308   /// Getter for MRI
0309   MachineRegisterInfo *getMRI() { return State.MRI; }
0310   const MachineRegisterInfo *getMRI() const { return State.MRI; }
0311 
0312   /// Getter for the State
0313   MachineIRBuilderState &getState() { return State; }
0314 
0315   /// Setter for the State
0316   void setState(const MachineIRBuilderState &NewState) { State = NewState; }
0317 
0318   /// Getter for the basic block we currently build.
0319   const MachineBasicBlock &getMBB() const {
0320     assert(State.MBB && "MachineBasicBlock is not set");
0321     return *State.MBB;
0322   }
0323 
0324   MachineBasicBlock &getMBB() {
0325     return const_cast<MachineBasicBlock &>(
0326         const_cast<const MachineIRBuilder *>(this)->getMBB());
0327   }
0328 
0329   GISelCSEInfo *getCSEInfo() { return State.CSEInfo; }
0330   const GISelCSEInfo *getCSEInfo() const { return State.CSEInfo; }
0331 
0332   /// Current insertion point for new instructions.
0333   MachineBasicBlock::iterator getInsertPt() { return State.II; }
0334 
0335   /// Set the insertion point before the specified position.
0336   /// \pre MBB must be in getMF().
0337   /// \pre II must be a valid iterator in MBB.
0338   void setInsertPt(MachineBasicBlock &MBB, MachineBasicBlock::iterator II) {
0339     assert(MBB.getParent() == &getMF() &&
0340            "Basic block is in a different function");
0341     State.MBB = &MBB;
0342     State.II = II;
0343   }
0344 
0345   /// @}
0346 
0347   void setCSEInfo(GISelCSEInfo *Info) { State.CSEInfo = Info; }
0348 
0349   /// \name Setters for the insertion point.
0350   /// @{
0351   /// Set the MachineFunction where to build instructions.
0352   void setMF(MachineFunction &MF);
0353 
0354   /// Set the insertion point to the  end of \p MBB.
0355   /// \pre \p MBB must be contained by getMF().
0356   void setMBB(MachineBasicBlock &MBB) {
0357     State.MBB = &MBB;
0358     State.II = MBB.end();
0359     assert(&getMF() == MBB.getParent() &&
0360            "Basic block is in a different function");
0361   }
0362 
0363   /// Set the insertion point to before MI.
0364   /// \pre MI must be in getMF().
0365   void setInstr(MachineInstr &MI) {
0366     assert(MI.getParent() && "Instruction is not part of a basic block");
0367     setMBB(*MI.getParent());
0368     State.II = MI.getIterator();
0369     setPCSections(MI.getPCSections());
0370     setMMRAMetadata(MI.getMMRAMetadata());
0371   }
0372   /// @}
0373 
0374   /// Set the insertion point to before MI, and set the debug loc to MI's loc.
0375   /// \pre MI must be in getMF().
0376   void setInstrAndDebugLoc(MachineInstr &MI) {
0377     setInstr(MI);
0378     setDebugLoc(MI.getDebugLoc());
0379   }
0380 
0381   void setChangeObserver(GISelChangeObserver &Observer) {
0382     State.Observer = &Observer;
0383   }
0384 
0385   GISelChangeObserver *getObserver() { return State.Observer; }
0386 
0387   void stopObservingChanges() { State.Observer = nullptr; }
0388 
0389   bool isObservingChanges() const { return State.Observer != nullptr; }
0390   /// @}
0391 
0392   /// Set the debug location to \p DL for all the next build instructions.
0393   void setDebugLoc(const DebugLoc &DL) { this->State.DL = DL; }
0394 
0395   /// Get the current instruction's debug location.
0396   const DebugLoc &getDebugLoc() { return State.DL; }
0397 
0398   /// Set the PC sections metadata to \p MD for all the next build instructions.
0399   void setPCSections(MDNode *MD) { State.PCSections = MD; }
0400 
0401   /// Get the current instruction's PC sections metadata.
0402   MDNode *getPCSections() { return State.PCSections; }
0403 
0404   /// Set the PC sections metadata to \p MD for all the next build instructions.
0405   void setMMRAMetadata(MDNode *MMRA) { State.MMRA = MMRA; }
0406 
0407   /// Get the current instruction's MMRA metadata.
0408   MDNode *getMMRAMetadata() { return State.MMRA; }
0409 
0410   /// Build and insert <empty> = \p Opcode <empty>.
0411   /// The insertion point is the one set by the last call of either
0412   /// setBasicBlock or setMI.
0413   ///
0414   /// \pre setBasicBlock or setMI must have been called.
0415   ///
0416   /// \return a MachineInstrBuilder for the newly created instruction.
0417   MachineInstrBuilder buildInstr(unsigned Opcode) {
0418     return insertInstr(buildInstrNoInsert(Opcode));
0419   }
0420 
0421   /// Build but don't insert <empty> = \p Opcode <empty>.
0422   ///
0423   /// \pre setMF, setBasicBlock or setMI  must have been called.
0424   ///
0425   /// \return a MachineInstrBuilder for the newly created instruction.
0426   MachineInstrBuilder buildInstrNoInsert(unsigned Opcode);
0427 
0428   /// Insert an existing instruction at the insertion point.
0429   MachineInstrBuilder insertInstr(MachineInstrBuilder MIB);
0430 
0431   /// Build and insert a DBG_VALUE instruction expressing the fact that the
0432   /// associated \p Variable lives in \p Reg (suitably modified by \p Expr).
0433   MachineInstrBuilder buildDirectDbgValue(Register Reg, const MDNode *Variable,
0434                                           const MDNode *Expr);
0435 
0436   /// Build and insert a DBG_VALUE instruction expressing the fact that the
0437   /// associated \p Variable lives in memory at \p Reg (suitably modified by \p
0438   /// Expr).
0439   MachineInstrBuilder buildIndirectDbgValue(Register Reg,
0440                                             const MDNode *Variable,
0441                                             const MDNode *Expr);
0442 
0443   /// Build and insert a DBG_VALUE instruction expressing the fact that the
0444   /// associated \p Variable lives in the stack slot specified by \p FI
0445   /// (suitably modified by \p Expr).
0446   MachineInstrBuilder buildFIDbgValue(int FI, const MDNode *Variable,
0447                                       const MDNode *Expr);
0448 
0449   /// Build and insert a DBG_VALUE instructions specifying that \p Variable is
0450   /// given by \p C (suitably modified by \p Expr).
0451   MachineInstrBuilder buildConstDbgValue(const Constant &C,
0452                                          const MDNode *Variable,
0453                                          const MDNode *Expr);
0454 
0455   /// Build and insert a DBG_LABEL instructions specifying that \p Label is
0456   /// given. Convert "llvm.dbg.label Label" to "DBG_LABEL Label".
0457   MachineInstrBuilder buildDbgLabel(const MDNode *Label);
0458 
0459   /// Build and insert \p Res = G_DYN_STACKALLOC \p Size, \p Align
0460   ///
0461   /// G_DYN_STACKALLOC does a dynamic stack allocation and writes the address of
0462   /// the allocated memory into \p Res.
0463   /// \pre setBasicBlock or setMI must have been called.
0464   /// \pre \p Res must be a generic virtual register with pointer type.
0465   ///
0466   /// \return a MachineInstrBuilder for the newly created instruction.
0467   MachineInstrBuilder buildDynStackAlloc(const DstOp &Res, const SrcOp &Size,
0468                                          Align Alignment);
0469 
0470   /// Build and insert \p Res = G_FRAME_INDEX \p Idx
0471   ///
0472   /// G_FRAME_INDEX materializes the address of an alloca value or other
0473   /// stack-based object.
0474   ///
0475   /// \pre setBasicBlock or setMI must have been called.
0476   /// \pre \p Res must be a generic virtual register with pointer type.
0477   ///
0478   /// \return a MachineInstrBuilder for the newly created instruction.
0479   MachineInstrBuilder buildFrameIndex(const DstOp &Res, int Idx);
0480 
0481   /// Build and insert \p Res = G_GLOBAL_VALUE \p GV
0482   ///
0483   /// G_GLOBAL_VALUE materializes the address of the specified global
0484   /// into \p Res.
0485   ///
0486   /// \pre setBasicBlock or setMI must have been called.
0487   /// \pre \p Res must be a generic virtual register with pointer type
0488   ///      in the same address space as \p GV.
0489   ///
0490   /// \return a MachineInstrBuilder for the newly created instruction.
0491   MachineInstrBuilder buildGlobalValue(const DstOp &Res, const GlobalValue *GV);
0492 
0493   /// Build and insert \p Res = G_CONSTANT_POOL \p Idx
0494   ///
0495   /// G_CONSTANT_POOL materializes the address of an object in the constant
0496   /// pool.
0497   ///
0498   /// \pre setBasicBlock or setMI must have been called.
0499   /// \pre \p Res must be a generic virtual register with pointer type.
0500   ///
0501   /// \return a MachineInstrBuilder for the newly created instruction.
0502   MachineInstrBuilder buildConstantPool(const DstOp &Res, unsigned Idx);
0503 
0504   /// Build and insert \p Res = G_PTR_ADD \p Op0, \p Op1
0505   ///
0506   /// G_PTR_ADD adds \p Op1 addressible units to the pointer specified by \p Op0,
0507   /// storing the resulting pointer in \p Res. Addressible units are typically
0508   /// bytes but this can vary between targets.
0509   ///
0510   /// \pre setBasicBlock or setMI must have been called.
0511   /// \pre \p Res and \p Op0 must be generic virtual registers with pointer
0512   ///      type.
0513   /// \pre \p Op1 must be a generic virtual register with scalar type.
0514   ///
0515   /// \return a MachineInstrBuilder for the newly created instruction.
0516   MachineInstrBuilder buildPtrAdd(const DstOp &Res, const SrcOp &Op0,
0517                                   const SrcOp &Op1,
0518                                   std::optional<unsigned> Flags = std::nullopt);
0519 
0520   /// Materialize and insert \p Res = G_PTR_ADD \p Op0, (G_CONSTANT \p Value)
0521   ///
0522   /// G_PTR_ADD adds \p Value bytes to the pointer specified by \p Op0,
0523   /// storing the resulting pointer in \p Res. If \p Value is zero then no
0524   /// G_PTR_ADD or G_CONSTANT will be created and \pre Op0 will be assigned to
0525   /// \p Res.
0526   ///
0527   /// \pre setBasicBlock or setMI must have been called.
0528   /// \pre \p Op0 must be a generic virtual register with pointer type.
0529   /// \pre \p ValueTy must be a scalar type.
0530   /// \pre \p Res must be 0. This is to detect confusion between
0531   ///      materializePtrAdd() and buildPtrAdd().
0532   /// \post \p Res will either be a new generic virtual register of the same
0533   ///       type as \p Op0 or \p Op0 itself.
0534   ///
0535   /// \return a MachineInstrBuilder for the newly created instruction.
0536   std::optional<MachineInstrBuilder> materializePtrAdd(Register &Res,
0537                                                        Register Op0,
0538                                                        const LLT ValueTy,
0539                                                        uint64_t Value);
0540 
0541   /// Build and insert \p Res = G_PTRMASK \p Op0, \p Op1
0542   MachineInstrBuilder buildPtrMask(const DstOp &Res, const SrcOp &Op0,
0543                                    const SrcOp &Op1) {
0544     return buildInstr(TargetOpcode::G_PTRMASK, {Res}, {Op0, Op1});
0545   }
0546 
0547   /// Build and insert \p Res = G_PTRMASK \p Op0, \p G_CONSTANT (1 << NumBits) - 1
0548   ///
0549   /// This clears the low bits of a pointer operand without destroying its
0550   /// pointer properties. This has the effect of rounding the address *down* to
0551   /// a specified alignment in bits.
0552   ///
0553   /// \pre setBasicBlock or setMI must have been called.
0554   /// \pre \p Res and \p Op0 must be generic virtual registers with pointer
0555   ///      type.
0556   /// \pre \p NumBits must be an integer representing the number of low bits to
0557   ///      be cleared in \p Op0.
0558   ///
0559   /// \return a MachineInstrBuilder for the newly created instruction.
0560   MachineInstrBuilder buildMaskLowPtrBits(const DstOp &Res, const SrcOp &Op0,
0561                                           uint32_t NumBits);
0562 
0563   /// Build and insert
0564   /// a, b, ..., x = G_UNMERGE_VALUES \p Op0
0565   /// \p Res = G_BUILD_VECTOR a, b, ..., x, undef, ..., undef
0566   ///
0567   /// Pad \p Op0 with undef elements to match number of elements in \p Res.
0568   ///
0569   /// \pre setBasicBlock or setMI must have been called.
0570   /// \pre \p Res and \p Op0 must be generic virtual registers with vector type,
0571   ///      same vector element type and Op0 must have fewer elements then Res.
0572   ///
0573   /// \return a MachineInstrBuilder for the newly created build vector instr.
0574   MachineInstrBuilder buildPadVectorWithUndefElements(const DstOp &Res,
0575                                                       const SrcOp &Op0);
0576 
0577   /// Build and insert
0578   /// a, b, ..., x, y, z = G_UNMERGE_VALUES \p Op0
0579   /// \p Res = G_BUILD_VECTOR a, b, ..., x
0580   ///
0581   /// Delete trailing elements in \p Op0 to match number of elements in \p Res.
0582   ///
0583   /// \pre setBasicBlock or setMI must have been called.
0584   /// \pre \p Res and \p Op0 must be generic virtual registers with vector type,
0585   ///      same vector element type and Op0 must have more elements then Res.
0586   ///
0587   /// \return a MachineInstrBuilder for the newly created build vector instr.
0588   MachineInstrBuilder buildDeleteTrailingVectorElements(const DstOp &Res,
0589                                                         const SrcOp &Op0);
0590 
0591   /// Build and insert \p Res, \p CarryOut = G_UADDO \p Op0, \p Op1
0592   ///
0593   /// G_UADDO sets \p Res to \p Op0 + \p Op1 (truncated to the bit width) and
0594   /// sets \p CarryOut to 1 if the result overflowed in unsigned arithmetic.
0595   ///
0596   /// \pre setBasicBlock or setMI must have been called.
0597   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers with the
0598   /// same scalar type.
0599   ////\pre \p CarryOut must be generic virtual register with scalar type
0600   ///(typically s1)
0601   ///
0602   /// \return The newly created instruction.
0603   MachineInstrBuilder buildUAddo(const DstOp &Res, const DstOp &CarryOut,
0604                                  const SrcOp &Op0, const SrcOp &Op1) {
0605     return buildInstr(TargetOpcode::G_UADDO, {Res, CarryOut}, {Op0, Op1});
0606   }
0607 
0608   /// Build and insert \p Res, \p CarryOut = G_USUBO \p Op0, \p Op1
0609   MachineInstrBuilder buildUSubo(const DstOp &Res, const DstOp &CarryOut,
0610                                  const SrcOp &Op0, const SrcOp &Op1) {
0611     return buildInstr(TargetOpcode::G_USUBO, {Res, CarryOut}, {Op0, Op1});
0612   }
0613 
0614   /// Build and insert \p Res, \p CarryOut = G_SADDO \p Op0, \p Op1
0615   MachineInstrBuilder buildSAddo(const DstOp &Res, const DstOp &CarryOut,
0616                                  const SrcOp &Op0, const SrcOp &Op1) {
0617     return buildInstr(TargetOpcode::G_SADDO, {Res, CarryOut}, {Op0, Op1});
0618   }
0619 
0620   /// Build and insert \p Res, \p CarryOut = G_SUBO \p Op0, \p Op1
0621   MachineInstrBuilder buildSSubo(const DstOp &Res, const DstOp &CarryOut,
0622                                  const SrcOp &Op0, const SrcOp &Op1) {
0623     return buildInstr(TargetOpcode::G_SSUBO, {Res, CarryOut}, {Op0, Op1});
0624   }
0625 
0626   /// Build and insert \p Res, \p CarryOut = G_UADDE \p Op0,
0627   /// \p Op1, \p CarryIn
0628   ///
0629   /// G_UADDE sets \p Res to \p Op0 + \p Op1 + \p CarryIn (truncated to the bit
0630   /// width) and sets \p CarryOut to 1 if the result overflowed in unsigned
0631   /// arithmetic.
0632   ///
0633   /// \pre setBasicBlock or setMI must have been called.
0634   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
0635   ///      with the same scalar type.
0636   /// \pre \p CarryOut and \p CarryIn must be generic virtual
0637   ///      registers with the same scalar type (typically s1)
0638   ///
0639   /// \return The newly created instruction.
0640   MachineInstrBuilder buildUAdde(const DstOp &Res, const DstOp &CarryOut,
0641                                  const SrcOp &Op0, const SrcOp &Op1,
0642                                  const SrcOp &CarryIn) {
0643     return buildInstr(TargetOpcode::G_UADDE, {Res, CarryOut},
0644                                              {Op0, Op1, CarryIn});
0645   }
0646 
0647   /// Build and insert \p Res, \p CarryOut = G_USUBE \p Op0, \p Op1, \p CarryInp
0648   MachineInstrBuilder buildUSube(const DstOp &Res, const DstOp &CarryOut,
0649                                  const SrcOp &Op0, const SrcOp &Op1,
0650                                  const SrcOp &CarryIn) {
0651     return buildInstr(TargetOpcode::G_USUBE, {Res, CarryOut},
0652                                              {Op0, Op1, CarryIn});
0653   }
0654 
0655   /// Build and insert \p Res, \p CarryOut = G_SADDE \p Op0, \p Op1, \p CarryInp
0656   MachineInstrBuilder buildSAdde(const DstOp &Res, const DstOp &CarryOut,
0657                                  const SrcOp &Op0, const SrcOp &Op1,
0658                                  const SrcOp &CarryIn) {
0659     return buildInstr(TargetOpcode::G_SADDE, {Res, CarryOut},
0660                                              {Op0, Op1, CarryIn});
0661   }
0662 
0663   /// Build and insert \p Res, \p CarryOut = G_SSUBE \p Op0, \p Op1, \p CarryInp
0664   MachineInstrBuilder buildSSube(const DstOp &Res, const DstOp &CarryOut,
0665                                  const SrcOp &Op0, const SrcOp &Op1,
0666                                  const SrcOp &CarryIn) {
0667     return buildInstr(TargetOpcode::G_SSUBE, {Res, CarryOut},
0668                                              {Op0, Op1, CarryIn});
0669   }
0670 
0671   /// Build and insert \p Res = G_ANYEXT \p Op0
0672   ///
0673   /// G_ANYEXT produces a register of the specified width, with bits 0 to
0674   /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are unspecified
0675   /// (i.e. this is neither zero nor sign-extension). For a vector register,
0676   /// each element is extended individually.
0677   ///
0678   /// \pre setBasicBlock or setMI must have been called.
0679   /// \pre \p Res must be a generic virtual register with scalar or vector type.
0680   /// \pre \p Op must be a generic virtual register with scalar or vector type.
0681   /// \pre \p Op must be smaller than \p Res
0682   ///
0683   /// \return The newly created instruction.
0684 
0685   MachineInstrBuilder buildAnyExt(const DstOp &Res, const SrcOp &Op);
0686 
0687   /// Build and insert \p Res = G_SEXT \p Op
0688   ///
0689   /// G_SEXT produces a register of the specified width, with bits 0 to
0690   /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are duplicated from the
0691   /// high bit of \p Op (i.e. 2s-complement sign extended).
0692   ///
0693   /// \pre setBasicBlock or setMI must have been called.
0694   /// \pre \p Res must be a generic virtual register with scalar or vector type.
0695   /// \pre \p Op must be a generic virtual register with scalar or vector type.
0696   /// \pre \p Op must be smaller than \p Res
0697   ///
0698   /// \return The newly created instruction.
0699   MachineInstrBuilder buildSExt(const DstOp &Res, const SrcOp &Op);
0700 
0701   /// Build and insert \p Res = G_SEXT_INREG \p Op, ImmOp
0702   MachineInstrBuilder buildSExtInReg(const DstOp &Res, const SrcOp &Op, int64_t ImmOp) {
0703     return buildInstr(TargetOpcode::G_SEXT_INREG, {Res}, {Op, SrcOp(ImmOp)});
0704   }
0705 
0706   /// Build and insert \p Res = G_FPEXT \p Op
0707   MachineInstrBuilder buildFPExt(const DstOp &Res, const SrcOp &Op,
0708                                  std::optional<unsigned> Flags = std::nullopt) {
0709     return buildInstr(TargetOpcode::G_FPEXT, {Res}, {Op}, Flags);
0710   }
0711 
0712   /// Build and insert a G_PTRTOINT instruction.
0713   MachineInstrBuilder buildPtrToInt(const DstOp &Dst, const SrcOp &Src) {
0714     return buildInstr(TargetOpcode::G_PTRTOINT, {Dst}, {Src});
0715   }
0716 
0717   /// Build and insert a G_INTTOPTR instruction.
0718   MachineInstrBuilder buildIntToPtr(const DstOp &Dst, const SrcOp &Src) {
0719     return buildInstr(TargetOpcode::G_INTTOPTR, {Dst}, {Src});
0720   }
0721 
0722   /// Build and insert \p Dst = G_BITCAST \p Src
0723   MachineInstrBuilder buildBitcast(const DstOp &Dst, const SrcOp &Src) {
0724     return buildInstr(TargetOpcode::G_BITCAST, {Dst}, {Src});
0725   }
0726 
0727     /// Build and insert \p Dst = G_ADDRSPACE_CAST \p Src
0728   MachineInstrBuilder buildAddrSpaceCast(const DstOp &Dst, const SrcOp &Src) {
0729     return buildInstr(TargetOpcode::G_ADDRSPACE_CAST, {Dst}, {Src});
0730   }
0731 
0732   /// \return The opcode of the extension the target wants to use for boolean
0733   /// values.
0734   unsigned getBoolExtOp(bool IsVec, bool IsFP) const;
0735 
0736   // Build and insert \p Res = G_ANYEXT \p Op, \p Res = G_SEXT \p Op, or \p Res
0737   // = G_ZEXT \p Op depending on how the target wants to extend boolean values.
0738   MachineInstrBuilder buildBoolExt(const DstOp &Res, const SrcOp &Op,
0739                                    bool IsFP);
0740 
0741   // Build and insert \p Res = G_SEXT_INREG \p Op, 1 or \p Res = G_AND \p Op, 1,
0742   // or COPY depending on how the target wants to extend boolean values, using
0743   // the original register size.
0744   MachineInstrBuilder buildBoolExtInReg(const DstOp &Res, const SrcOp &Op,
0745                                         bool IsVector,
0746                                         bool IsFP);
0747 
0748   /// Build and insert \p Res = G_ZEXT \p Op
0749   ///
0750   /// G_ZEXT produces a register of the specified width, with bits 0 to
0751   /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are 0. For a vector
0752   /// register, each element is extended individually.
0753   ///
0754   /// \pre setBasicBlock or setMI must have been called.
0755   /// \pre \p Res must be a generic virtual register with scalar or vector type.
0756   /// \pre \p Op must be a generic virtual register with scalar or vector type.
0757   /// \pre \p Op must be smaller than \p Res
0758   ///
0759   /// \return The newly created instruction.
0760   MachineInstrBuilder buildZExt(const DstOp &Res, const SrcOp &Op,
0761                                 std::optional<unsigned> Flags = std::nullopt);
0762 
0763   /// Build and insert \p Res = G_SEXT \p Op, \p Res = G_TRUNC \p Op, or
0764   /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
0765   ///  ///
0766   /// \pre setBasicBlock or setMI must have been called.
0767   /// \pre \p Res must be a generic virtual register with scalar or vector type.
0768   /// \pre \p Op must be a generic virtual register with scalar or vector type.
0769   ///
0770   /// \return The newly created instruction.
0771   MachineInstrBuilder buildSExtOrTrunc(const DstOp &Res, const SrcOp &Op);
0772 
0773   /// Build and insert \p Res = G_ZEXT \p Op, \p Res = G_TRUNC \p Op, or
0774   /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
0775   ///  ///
0776   /// \pre setBasicBlock or setMI must have been called.
0777   /// \pre \p Res must be a generic virtual register with scalar or vector type.
0778   /// \pre \p Op must be a generic virtual register with scalar or vector type.
0779   ///
0780   /// \return The newly created instruction.
0781   MachineInstrBuilder buildZExtOrTrunc(const DstOp &Res, const SrcOp &Op);
0782 
0783   // Build and insert \p Res = G_ANYEXT \p Op, \p Res = G_TRUNC \p Op, or
0784   /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
0785   ///  ///
0786   /// \pre setBasicBlock or setMI must have been called.
0787   /// \pre \p Res must be a generic virtual register with scalar or vector type.
0788   /// \pre \p Op must be a generic virtual register with scalar or vector type.
0789   ///
0790   /// \return The newly created instruction.
0791   MachineInstrBuilder buildAnyExtOrTrunc(const DstOp &Res, const SrcOp &Op);
0792 
0793   /// Build and insert \p Res = \p ExtOpc, \p Res = G_TRUNC \p
0794   /// Op, or \p Res = COPY \p Op depending on the differing sizes of \p Res and
0795   /// \p Op.
0796   ///  ///
0797   /// \pre setBasicBlock or setMI must have been called.
0798   /// \pre \p Res must be a generic virtual register with scalar or vector type.
0799   /// \pre \p Op must be a generic virtual register with scalar or vector type.
0800   ///
0801   /// \return The newly created instruction.
0802   MachineInstrBuilder buildExtOrTrunc(unsigned ExtOpc, const DstOp &Res,
0803                                       const SrcOp &Op);
0804 
0805   /// Build and inserts \p Res = \p G_AND \p Op, \p LowBitsSet(ImmOp)
0806   /// Since there is no G_ZEXT_INREG like G_SEXT_INREG, the instruction is
0807   /// emulated using G_AND.
0808   MachineInstrBuilder buildZExtInReg(const DstOp &Res, const SrcOp &Op,
0809                                      int64_t ImmOp);
0810 
0811   /// Build and insert an appropriate cast between two registers of equal size.
0812   MachineInstrBuilder buildCast(const DstOp &Dst, const SrcOp &Src);
0813 
0814   /// Build and insert G_BR \p Dest
0815   ///
0816   /// G_BR is an unconditional branch to \p Dest.
0817   ///
0818   /// \pre setBasicBlock or setMI must have been called.
0819   ///
0820   /// \return a MachineInstrBuilder for the newly created instruction.
0821   MachineInstrBuilder buildBr(MachineBasicBlock &Dest);
0822 
0823   /// Build and insert G_BRCOND \p Tst, \p Dest
0824   ///
0825   /// G_BRCOND is a conditional branch to \p Dest.
0826   ///
0827   /// \pre setBasicBlock or setMI must have been called.
0828   /// \pre \p Tst must be a generic virtual register with scalar
0829   ///      type. At the beginning of legalization, this will be a single
0830   ///      bit (s1). Targets with interesting flags registers may change
0831   ///      this. For a wider type, whether the branch is taken must only
0832   ///      depend on bit 0 (for now).
0833   ///
0834   /// \return The newly created instruction.
0835   MachineInstrBuilder buildBrCond(const SrcOp &Tst, MachineBasicBlock &Dest);
0836 
0837   /// Build and insert G_BRINDIRECT \p Tgt
0838   ///
0839   /// G_BRINDIRECT is an indirect branch to \p Tgt.
0840   ///
0841   /// \pre setBasicBlock or setMI must have been called.
0842   /// \pre \p Tgt must be a generic virtual register with pointer type.
0843   ///
0844   /// \return a MachineInstrBuilder for the newly created instruction.
0845   MachineInstrBuilder buildBrIndirect(Register Tgt);
0846 
0847   /// Build and insert G_BRJT \p TablePtr, \p JTI, \p IndexReg
0848   ///
0849   /// G_BRJT is a jump table branch using a table base pointer \p TablePtr,
0850   /// jump table index \p JTI and index \p IndexReg
0851   ///
0852   /// \pre setBasicBlock or setMI must have been called.
0853   /// \pre \p TablePtr must be a generic virtual register with pointer type.
0854   /// \pre \p JTI must be a jump table index.
0855   /// \pre \p IndexReg must be a generic virtual register with pointer type.
0856   ///
0857   /// \return a MachineInstrBuilder for the newly created instruction.
0858   MachineInstrBuilder buildBrJT(Register TablePtr, unsigned JTI,
0859                                 Register IndexReg);
0860 
0861   /// Build and insert \p Res = G_CONSTANT \p Val
0862   ///
0863   /// G_CONSTANT is an integer constant with the specified size and value. \p
0864   /// Val will be extended or truncated to the size of \p Reg.
0865   ///
0866   /// \pre setBasicBlock or setMI must have been called.
0867   /// \pre \p Res must be a generic virtual register with scalar or pointer
0868   ///      type.
0869   ///
0870   /// \return The newly created instruction.
0871   virtual MachineInstrBuilder buildConstant(const DstOp &Res,
0872                                             const ConstantInt &Val);
0873 
0874   /// Build and insert \p Res = G_CONSTANT \p Val
0875   ///
0876   /// G_CONSTANT is an integer constant with the specified size and value.
0877   ///
0878   /// \pre setBasicBlock or setMI must have been called.
0879   /// \pre \p Res must be a generic virtual register with scalar type.
0880   ///
0881   /// \return The newly created instruction.
0882   MachineInstrBuilder buildConstant(const DstOp &Res, int64_t Val);
0883   MachineInstrBuilder buildConstant(const DstOp &Res, const APInt &Val);
0884 
0885   /// Build and insert \p Res = G_FCONSTANT \p Val
0886   ///
0887   /// G_FCONSTANT is a floating-point constant with the specified size and
0888   /// value.
0889   ///
0890   /// \pre setBasicBlock or setMI must have been called.
0891   /// \pre \p Res must be a generic virtual register with scalar type.
0892   ///
0893   /// \return The newly created instruction.
0894   virtual MachineInstrBuilder buildFConstant(const DstOp &Res,
0895                                              const ConstantFP &Val);
0896 
0897   MachineInstrBuilder buildFConstant(const DstOp &Res, double Val);
0898   MachineInstrBuilder buildFConstant(const DstOp &Res, const APFloat &Val);
0899 
0900   /// Build and insert G_PTRAUTH_GLOBAL_VALUE
0901   ///
0902   /// \return a MachineInstrBuilder for the newly created instruction.
0903   MachineInstrBuilder buildConstantPtrAuth(const DstOp &Res,
0904                                            const ConstantPtrAuth *CPA,
0905                                            Register Addr, Register AddrDisc);
0906 
0907   /// Build and insert \p Res = COPY Op
0908   ///
0909   /// Register-to-register COPY sets \p Res to \p Op.
0910   ///
0911   /// \pre setBasicBlock or setMI must have been called.
0912   ///
0913   /// \return a MachineInstrBuilder for the newly created instruction.
0914   MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op);
0915 
0916 
0917   /// Build and insert G_ASSERT_SEXT, G_ASSERT_ZEXT, or G_ASSERT_ALIGN
0918   ///
0919   /// \return a MachineInstrBuilder for the newly created instruction.
0920   MachineInstrBuilder buildAssertInstr(unsigned Opc, const DstOp &Res,
0921                                        const SrcOp &Op, unsigned Val) {
0922     return buildInstr(Opc, Res, Op).addImm(Val);
0923   }
0924 
0925   /// Build and insert \p Res = G_ASSERT_ZEXT Op, Size
0926   ///
0927   /// \return a MachineInstrBuilder for the newly created instruction.
0928   MachineInstrBuilder buildAssertZExt(const DstOp &Res, const SrcOp &Op,
0929                                       unsigned Size) {
0930     return buildAssertInstr(TargetOpcode::G_ASSERT_ZEXT, Res, Op, Size);
0931   }
0932 
0933   /// Build and insert \p Res = G_ASSERT_SEXT Op, Size
0934   ///
0935   /// \return a MachineInstrBuilder for the newly created instruction.
0936   MachineInstrBuilder buildAssertSExt(const DstOp &Res, const SrcOp &Op,
0937                                       unsigned Size) {
0938     return buildAssertInstr(TargetOpcode::G_ASSERT_SEXT, Res, Op, Size);
0939   }
0940 
0941   /// Build and insert \p Res = G_ASSERT_ALIGN Op, AlignVal
0942   ///
0943   /// \return a MachineInstrBuilder for the newly created instruction.
0944   MachineInstrBuilder buildAssertAlign(const DstOp &Res, const SrcOp &Op,
0945                        Align AlignVal) {
0946     return buildAssertInstr(TargetOpcode::G_ASSERT_ALIGN, Res, Op,
0947                             AlignVal.value());
0948   }
0949 
0950   /// Build and insert `Res = G_LOAD Addr, MMO`.
0951   ///
0952   /// Loads the value stored at \p Addr. Puts the result in \p Res.
0953   ///
0954   /// \pre setBasicBlock or setMI must have been called.
0955   /// \pre \p Res must be a generic virtual register.
0956   /// \pre \p Addr must be a generic virtual register with pointer type.
0957   ///
0958   /// \return a MachineInstrBuilder for the newly created instruction.
0959   MachineInstrBuilder buildLoad(const DstOp &Res, const SrcOp &Addr,
0960                                 MachineMemOperand &MMO) {
0961     return buildLoadInstr(TargetOpcode::G_LOAD, Res, Addr, MMO);
0962   }
0963 
0964   /// Build and insert a G_LOAD instruction, while constructing the
0965   /// MachineMemOperand.
0966   MachineInstrBuilder
0967   buildLoad(const DstOp &Res, const SrcOp &Addr, MachinePointerInfo PtrInfo,
0968             Align Alignment,
0969             MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
0970             const AAMDNodes &AAInfo = AAMDNodes());
0971 
0972   /// Build and insert `Res = <opcode> Addr, MMO`.
0973   ///
0974   /// Loads the value stored at \p Addr. Puts the result in \p Res.
0975   ///
0976   /// \pre setBasicBlock or setMI must have been called.
0977   /// \pre \p Res must be a generic virtual register.
0978   /// \pre \p Addr must be a generic virtual register with pointer type.
0979   ///
0980   /// \return a MachineInstrBuilder for the newly created instruction.
0981   MachineInstrBuilder buildLoadInstr(unsigned Opcode, const DstOp &Res,
0982                                      const SrcOp &Addr, MachineMemOperand &MMO);
0983 
0984   /// Helper to create a load from a constant offset given a base address. Load
0985   /// the type of \p Dst from \p Offset from the given base address and memory
0986   /// operand.
0987   MachineInstrBuilder buildLoadFromOffset(const DstOp &Dst,
0988                                           const SrcOp &BasePtr,
0989                                           MachineMemOperand &BaseMMO,
0990                                           int64_t Offset);
0991 
0992   /// Build and insert `G_STORE Val, Addr, MMO`.
0993   ///
0994   /// Stores the value \p Val to \p Addr.
0995   ///
0996   /// \pre setBasicBlock or setMI must have been called.
0997   /// \pre \p Val must be a generic virtual register.
0998   /// \pre \p Addr must be a generic virtual register with pointer type.
0999   ///
1000   /// \return a MachineInstrBuilder for the newly created instruction.
1001   MachineInstrBuilder buildStore(const SrcOp &Val, const SrcOp &Addr,
1002                                  MachineMemOperand &MMO);
1003 
1004   /// Build and insert a G_STORE instruction, while constructing the
1005   /// MachineMemOperand.
1006   MachineInstrBuilder
1007   buildStore(const SrcOp &Val, const SrcOp &Addr, MachinePointerInfo PtrInfo,
1008              Align Alignment,
1009              MachineMemOperand::Flags MMOFlags = MachineMemOperand::MONone,
1010              const AAMDNodes &AAInfo = AAMDNodes());
1011 
1012   /// Build and insert `Res0, ... = G_EXTRACT Src, Idx0`.
1013   ///
1014   /// \pre setBasicBlock or setMI must have been called.
1015   /// \pre \p Res and \p Src must be generic virtual registers.
1016   ///
1017   /// \return a MachineInstrBuilder for the newly created instruction.
1018   MachineInstrBuilder buildExtract(const DstOp &Res, const SrcOp &Src, uint64_t Index);
1019 
1020   /// Build and insert \p Res = IMPLICIT_DEF.
1021   MachineInstrBuilder buildUndef(const DstOp &Res);
1022 
1023   /// Build and insert \p Res = G_MERGE_VALUES \p Op0, ...
1024   ///
1025   /// G_MERGE_VALUES combines the input elements contiguously into a larger
1026   /// register. It should only be used when the destination register is not a
1027   /// vector.
1028   ///
1029   /// \pre setBasicBlock or setMI must have been called.
1030   /// \pre The entire register \p Res (and no more) must be covered by the input
1031   ///      registers.
1032   /// \pre The type of all \p Ops registers must be identical.
1033   ///
1034   /// \return a MachineInstrBuilder for the newly created instruction.
1035   MachineInstrBuilder buildMergeValues(const DstOp &Res,
1036                                        ArrayRef<Register> Ops);
1037 
1038   /// Build and insert \p Res = G_MERGE_VALUES \p Op0, ...
1039   ///               or \p Res = G_BUILD_VECTOR \p Op0, ...
1040   ///               or \p Res = G_CONCAT_VECTORS \p Op0, ...
1041   ///
1042   /// G_MERGE_VALUES combines the input elements contiguously into a larger
1043   /// register. It is used when the destination register is not a vector.
1044   /// G_BUILD_VECTOR combines scalar inputs into a vector register.
1045   /// G_CONCAT_VECTORS combines vector inputs into a vector register.
1046   ///
1047   /// \pre setBasicBlock or setMI must have been called.
1048   /// \pre The entire register \p Res (and no more) must be covered by the input
1049   ///      registers.
1050   /// \pre The type of all \p Ops registers must be identical.
1051   ///
1052   /// \return a MachineInstrBuilder for the newly created instruction. The
1053   ///         opcode of the new instruction will depend on the types of both
1054   ///         the destination and the sources.
1055   MachineInstrBuilder buildMergeLikeInstr(const DstOp &Res,
1056                                           ArrayRef<Register> Ops);
1057   MachineInstrBuilder buildMergeLikeInstr(const DstOp &Res,
1058                                           std::initializer_list<SrcOp> Ops);
1059 
1060   /// Build and insert \p Res0, ... = G_UNMERGE_VALUES \p Op
1061   ///
1062   /// G_UNMERGE_VALUES splits contiguous bits of the input into multiple
1063   ///
1064   /// \pre setBasicBlock or setMI must have been called.
1065   /// \pre The entire register \p Res (and no more) must be covered by the input
1066   ///      registers.
1067   /// \pre The type of all \p Res registers must be identical.
1068   ///
1069   /// \return a MachineInstrBuilder for the newly created instruction.
1070   MachineInstrBuilder buildUnmerge(ArrayRef<LLT> Res, const SrcOp &Op);
1071   MachineInstrBuilder buildUnmerge(ArrayRef<Register> Res, const SrcOp &Op);
1072 
1073   /// Build and insert an unmerge of \p Res sized pieces to cover \p Op
1074   MachineInstrBuilder buildUnmerge(LLT Res, const SrcOp &Op);
1075 
1076   /// Build and insert an unmerge of pieces with \p Attrs register attributes to
1077   /// cover \p Op
1078   MachineInstrBuilder buildUnmerge(MachineRegisterInfo::VRegAttrs Attrs,
1079                                    const SrcOp &Op);
1080 
1081   /// Build and insert \p Res = G_BUILD_VECTOR \p Op0, ...
1082   ///
1083   /// G_BUILD_VECTOR creates a vector value from multiple scalar registers.
1084   /// \pre setBasicBlock or setMI must have been called.
1085   /// \pre The entire register \p Res (and no more) must be covered by the
1086   ///      input scalar registers.
1087   /// \pre The type of all \p Ops registers must be identical.
1088   ///
1089   /// \return a MachineInstrBuilder for the newly created instruction.
1090   MachineInstrBuilder buildBuildVector(const DstOp &Res,
1091                                        ArrayRef<Register> Ops);
1092 
1093   /// Build and insert \p Res = G_BUILD_VECTOR \p Op0, ... where each OpN is
1094   /// built with G_CONSTANT.
1095   MachineInstrBuilder buildBuildVectorConstant(const DstOp &Res,
1096                                                ArrayRef<APInt> Ops);
1097 
1098   /// Build and insert \p Res = G_BUILD_VECTOR with \p Src replicated to fill
1099   /// the number of elements
1100   MachineInstrBuilder buildSplatBuildVector(const DstOp &Res, const SrcOp &Src);
1101 
1102   /// Build and insert \p Res = G_BUILD_VECTOR_TRUNC \p Op0, ...
1103   ///
1104   /// G_BUILD_VECTOR_TRUNC creates a vector value from multiple scalar registers
1105   /// which have types larger than the destination vector element type, and
1106   /// truncates the values to fit.
1107   ///
1108   /// If the operands given are already the same size as the vector elt type,
1109   /// then this method will instead create a G_BUILD_VECTOR instruction.
1110   ///
1111   /// \pre setBasicBlock or setMI must have been called.
1112   /// \pre The type of all \p Ops registers must be identical.
1113   ///
1114   /// \return a MachineInstrBuilder for the newly created instruction.
1115   MachineInstrBuilder buildBuildVectorTrunc(const DstOp &Res,
1116                                             ArrayRef<Register> Ops);
1117 
1118   /// Build and insert a vector splat of a scalar \p Src using a
1119   /// G_INSERT_VECTOR_ELT and G_SHUFFLE_VECTOR idiom.
1120   ///
1121   /// \pre setBasicBlock or setMI must have been called.
1122   /// \pre \p Src must have the same type as the element type of \p Dst
1123   ///
1124   /// \return a MachineInstrBuilder for the newly created instruction.
1125   MachineInstrBuilder buildShuffleSplat(const DstOp &Res, const SrcOp &Src);
1126 
1127   /// Build and insert \p Res = G_SHUFFLE_VECTOR \p Src1, \p Src2, \p Mask
1128   ///
1129   /// \pre setBasicBlock or setMI must have been called.
1130   ///
1131   /// \return a MachineInstrBuilder for the newly created instruction.
1132   MachineInstrBuilder buildShuffleVector(const DstOp &Res, const SrcOp &Src1,
1133                                          const SrcOp &Src2, ArrayRef<int> Mask);
1134 
1135   /// Build and insert \p Res = G_SPLAT_VECTOR \p Val
1136   ///
1137   /// \pre setBasicBlock or setMI must have been called.
1138   /// \pre \p Res must be a generic virtual register with vector type.
1139   /// \pre \p Val must be a generic virtual register with scalar type.
1140   ///
1141   /// \return a MachineInstrBuilder for the newly created instruction.
1142   MachineInstrBuilder buildSplatVector(const DstOp &Res, const SrcOp &Val);
1143 
1144   /// Build and insert \p Res = G_CONCAT_VECTORS \p Op0, ...
1145   ///
1146   /// G_CONCAT_VECTORS creates a vector from the concatenation of 2 or more
1147   /// vectors.
1148   ///
1149   /// \pre setBasicBlock or setMI must have been called.
1150   /// \pre The entire register \p Res (and no more) must be covered by the input
1151   ///      registers.
1152   /// \pre The type of all source operands must be identical.
1153   ///
1154   /// \return a MachineInstrBuilder for the newly created instruction.
1155   MachineInstrBuilder buildConcatVectors(const DstOp &Res,
1156                                          ArrayRef<Register> Ops);
1157 
1158   /// Build and insert `Res = G_INSERT_SUBVECTOR Src0, Src1, Idx`.
1159   ///
1160   /// \pre setBasicBlock or setMI must have been called.
1161   /// \pre \p Res, \p Src0, and \p Src1 must be generic virtual registers with
1162   /// vector type.
1163   ///
1164   /// \return a MachineInstrBuilder for the newly created instruction.
1165   MachineInstrBuilder buildInsertSubvector(const DstOp &Res, const SrcOp &Src0,
1166                                            const SrcOp &Src1, unsigned Index);
1167 
1168   /// Build and insert `Res = G_EXTRACT_SUBVECTOR Src, Idx0`.
1169   ///
1170   /// \pre setBasicBlock or setMI must have been called.
1171   /// \pre \p Res and \p Src must be generic virtual registers with vector type.
1172   ///
1173   /// \return a MachineInstrBuilder for the newly created instruction.
1174   MachineInstrBuilder buildExtractSubvector(const DstOp &Res, const SrcOp &Src,
1175                                             unsigned Index);
1176 
1177   MachineInstrBuilder buildInsert(const DstOp &Res, const SrcOp &Src,
1178                                   const SrcOp &Op, unsigned Index);
1179 
1180   /// Build and insert \p Res = G_STEP_VECTOR \p Step
1181   ///
1182   /// G_STEP_VECTOR returns a scalable vector of linear sequence of step \p Step
1183   /// into \p Res.
1184   ///
1185   /// \pre setBasicBlock or setMI must have been called.
1186   /// \pre \p Res must be a generic virtual register with scalable vector type.
1187   ///
1188   /// \return a MachineInstrBuilder for the newly created instruction.
1189   MachineInstrBuilder buildStepVector(const DstOp &Res, unsigned Step);
1190 
1191   /// Build and insert \p Res = G_VSCALE \p MinElts
1192   ///
1193   /// G_VSCALE puts the value of the runtime vscale multiplied by \p MinElts
1194   /// into \p Res.
1195   ///
1196   /// \pre setBasicBlock or setMI must have been called.
1197   /// \pre \p Res must be a generic virtual register with scalar type.
1198   ///
1199   /// \return a MachineInstrBuilder for the newly created instruction.
1200   MachineInstrBuilder buildVScale(const DstOp &Res, unsigned MinElts);
1201 
1202   /// Build and insert \p Res = G_VSCALE \p MinElts
1203   ///
1204   /// G_VSCALE puts the value of the runtime vscale multiplied by \p MinElts
1205   /// into \p Res.
1206   ///
1207   /// \pre setBasicBlock or setMI must have been called.
1208   /// \pre \p Res must be a generic virtual register with scalar type.
1209   ///
1210   /// \return a MachineInstrBuilder for the newly created instruction.
1211   MachineInstrBuilder buildVScale(const DstOp &Res, const ConstantInt &MinElts);
1212 
1213   /// Build and insert \p Res = G_VSCALE \p MinElts
1214   ///
1215   /// G_VSCALE puts the value of the runtime vscale multiplied by \p MinElts
1216   /// into \p Res.
1217   ///
1218   /// \pre setBasicBlock or setMI must have been called.
1219   /// \pre \p Res must be a generic virtual register with scalar type.
1220   ///
1221   /// \return a MachineInstrBuilder for the newly created instruction.
1222   MachineInstrBuilder buildVScale(const DstOp &Res, const APInt &MinElts);
1223 
1224   /// Build and insert a G_INTRINSIC instruction.
1225   ///
1226   /// There are four different opcodes based on combinations of whether the
1227   /// intrinsic has side effects and whether it is convergent. These properties
1228   /// can be specified as explicit parameters, or else they are retrieved from
1229   /// the MCID for the intrinsic.
1230   ///
1231   /// The parameter \p Res provides the Registers or MOs that will be defined by
1232   /// this instruction.
1233   ///
1234   /// \pre setBasicBlock or setMI must have been called.
1235   ///
1236   /// \return a MachineInstrBuilder for the newly created instruction.
1237   MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef<Register> Res,
1238                                      bool HasSideEffects, bool isConvergent);
1239   MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef<Register> Res);
1240   MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef<DstOp> Res,
1241                                      bool HasSideEffects, bool isConvergent);
1242   MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef<DstOp> Res);
1243 
1244   /// Build and insert \p Res = G_FPTRUNC \p Op
1245   ///
1246   /// G_FPTRUNC converts a floating-point value into one with a smaller type.
1247   ///
1248   /// \pre setBasicBlock or setMI must have been called.
1249   /// \pre \p Res must be a generic virtual register with scalar or vector type.
1250   /// \pre \p Op must be a generic virtual register with scalar or vector type.
1251   /// \pre \p Res must be smaller than \p Op
1252   ///
1253   /// \return The newly created instruction.
1254   MachineInstrBuilder
1255   buildFPTrunc(const DstOp &Res, const SrcOp &Op,
1256                std::optional<unsigned> Flags = std::nullopt);
1257 
1258   /// Build and insert \p Res = G_TRUNC \p Op
1259   ///
1260   /// G_TRUNC extracts the low bits of a type. For a vector type each element is
1261   /// truncated independently before being packed into the destination.
1262   ///
1263   /// \pre setBasicBlock or setMI must have been called.
1264   /// \pre \p Res must be a generic virtual register with scalar or vector type.
1265   /// \pre \p Op must be a generic virtual register with scalar or vector type.
1266   /// \pre \p Res must be smaller than \p Op
1267   ///
1268   /// \return The newly created instruction.
1269   MachineInstrBuilder buildTrunc(const DstOp &Res, const SrcOp &Op,
1270                                  std::optional<unsigned> Flags = std::nullopt);
1271 
1272   /// Build and insert a \p Res = G_ICMP \p Pred, \p Op0, \p Op1
1273   ///
1274   /// \pre setBasicBlock or setMI must have been called.
1275 
1276   /// \pre \p Res must be a generic virtual register with scalar or
1277   ///      vector type. Typically this starts as s1 or <N x s1>.
1278   /// \pre \p Op0 and Op1 must be generic virtual registers with the
1279   ///      same number of elements as \p Res. If \p Res is a scalar,
1280   ///      \p Op0 must be either a scalar or pointer.
1281   /// \pre \p Pred must be an integer predicate.
1282   ///
1283   /// \return a MachineInstrBuilder for the newly created instruction.
1284   MachineInstrBuilder buildICmp(CmpInst::Predicate Pred, const DstOp &Res,
1285                                 const SrcOp &Op0, const SrcOp &Op1,
1286                                 std::optional<unsigned> Flags = std::nullopt);
1287 
1288   /// Build and insert a \p Res = G_FCMP \p Pred\p Op0, \p Op1
1289   ///
1290   /// \pre setBasicBlock or setMI must have been called.
1291 
1292   /// \pre \p Res must be a generic virtual register with scalar or
1293   ///      vector type. Typically this starts as s1 or <N x s1>.
1294   /// \pre \p Op0 and Op1 must be generic virtual registers with the
1295   ///      same number of elements as \p Res (or scalar, if \p Res is
1296   ///      scalar).
1297   /// \pre \p Pred must be a floating-point predicate.
1298   ///
1299   /// \return a MachineInstrBuilder for the newly created instruction.
1300   MachineInstrBuilder buildFCmp(CmpInst::Predicate Pred, const DstOp &Res,
1301                                 const SrcOp &Op0, const SrcOp &Op1,
1302                                 std::optional<unsigned> Flags = std::nullopt);
1303 
1304   /// Build and insert a \p Res = G_SCMP \p Op0, \p Op1
1305   ///
1306   /// \pre setBasicBlock or setMI must have been called.
1307 
1308   /// \pre \p Res must be a generic virtual register with scalar or
1309   ///      vector type. Typically this starts as s2 or <N x s2>.
1310   /// \pre \p Op0 and Op1 must be generic virtual registers with the
1311   ///      same number of elements as \p Res. If \p Res is a scalar,
1312   ///      \p Op0 must be a scalar.
1313   ///
1314   /// \return a MachineInstrBuilder for the newly created instruction.
1315   MachineInstrBuilder buildSCmp(const DstOp &Res, const SrcOp &Op0,
1316                                 const SrcOp &Op1);
1317 
1318   /// Build and insert a \p Res = G_UCMP \p Op0, \p Op1
1319   ///
1320   /// \pre setBasicBlock or setMI must have been called.
1321 
1322   /// \pre \p Res must be a generic virtual register with scalar or
1323   ///      vector type. Typically this starts as s2 or <N x s2>.
1324   /// \pre \p Op0 and Op1 must be generic virtual registers with the
1325   ///      same number of elements as \p Res. If \p Res is a scalar,
1326   ///      \p Op0 must be a scalar.
1327   ///
1328   /// \return a MachineInstrBuilder for the newly created instruction.
1329   MachineInstrBuilder buildUCmp(const DstOp &Res, const SrcOp &Op0,
1330                                 const SrcOp &Op1);
1331 
1332   /// Build and insert a \p Res = G_IS_FPCLASS \p Src, \p Mask
1333   MachineInstrBuilder buildIsFPClass(const DstOp &Res, const SrcOp &Src,
1334                                      unsigned Mask) {
1335     return buildInstr(TargetOpcode::G_IS_FPCLASS, {Res},
1336                       {Src, SrcOp(static_cast<int64_t>(Mask))});
1337   }
1338 
1339   /// Build and insert a \p Res = G_SELECT \p Tst, \p Op0, \p Op1
1340   ///
1341   /// \pre setBasicBlock or setMI must have been called.
1342   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1343   ///      with the same type.
1344   /// \pre \p Tst must be a generic virtual register with scalar, pointer or
1345   ///      vector type. If vector then it must have the same number of
1346   ///      elements as the other parameters.
1347   ///
1348   /// \return a MachineInstrBuilder for the newly created instruction.
1349   MachineInstrBuilder buildSelect(const DstOp &Res, const SrcOp &Tst,
1350                                   const SrcOp &Op0, const SrcOp &Op1,
1351                                   std::optional<unsigned> Flags = std::nullopt);
1352 
1353   /// Build and insert \p Res = G_INSERT_VECTOR_ELT \p Val,
1354   /// \p Elt, \p Idx
1355   ///
1356   /// \pre setBasicBlock or setMI must have been called.
1357   /// \pre \p Res and \p Val must be a generic virtual register
1358   //       with the same vector type.
1359   /// \pre \p Elt and \p Idx must be a generic virtual register
1360   ///      with scalar type.
1361   ///
1362   /// \return The newly created instruction.
1363   MachineInstrBuilder buildInsertVectorElement(const DstOp &Res,
1364                                                const SrcOp &Val,
1365                                                const SrcOp &Elt,
1366                                                const SrcOp &Idx);
1367 
1368   /// Build and insert \p Res = G_EXTRACT_VECTOR_ELT \p Val, \p Idx
1369   ///
1370   /// \pre setBasicBlock or setMI must have been called.
1371   /// \pre \p Res must be a generic virtual register with scalar type.
1372   /// \pre \p Val must be a generic virtual register with vector type.
1373   ///
1374   /// \return The newly created instruction.
1375   MachineInstrBuilder buildExtractVectorElementConstant(const DstOp &Res,
1376                                                         const SrcOp &Val,
1377                                                         const int Idx) {
1378     auto TLI = getMF().getSubtarget().getTargetLowering();
1379     unsigned VecIdxWidth = TLI->getVectorIdxTy(getDataLayout()).getSizeInBits();
1380     return buildExtractVectorElement(
1381         Res, Val, buildConstant(LLT::scalar(VecIdxWidth), Idx));
1382   }
1383 
1384   /// Build and insert \p Res = G_EXTRACT_VECTOR_ELT \p Val, \p Idx
1385   ///
1386   /// \pre setBasicBlock or setMI must have been called.
1387   /// \pre \p Res must be a generic virtual register with scalar type.
1388   /// \pre \p Val must be a generic virtual register with vector type.
1389   /// \pre \p Idx must be a generic virtual register with scalar type.
1390   ///
1391   /// \return The newly created instruction.
1392   MachineInstrBuilder buildExtractVectorElement(const DstOp &Res,
1393                                                 const SrcOp &Val,
1394                                                 const SrcOp &Idx);
1395 
1396   /// Build and insert `OldValRes<def>, SuccessRes<def> =
1397   /// G_ATOMIC_CMPXCHG_WITH_SUCCESS Addr, CmpVal, NewVal, MMO`.
1398   ///
1399   /// Atomically replace the value at \p Addr with \p NewVal if it is currently
1400   /// \p CmpVal otherwise leaves it unchanged. Puts the original value from \p
1401   /// Addr in \p Res, along with an s1 indicating whether it was replaced.
1402   ///
1403   /// \pre setBasicBlock or setMI must have been called.
1404   /// \pre \p OldValRes must be a generic virtual register of scalar type.
1405   /// \pre \p SuccessRes must be a generic virtual register of scalar type. It
1406   ///      will be assigned 0 on failure and 1 on success.
1407   /// \pre \p Addr must be a generic virtual register with pointer type.
1408   /// \pre \p OldValRes, \p CmpVal, and \p NewVal must be generic virtual
1409   ///      registers of the same type.
1410   ///
1411   /// \return a MachineInstrBuilder for the newly created instruction.
1412   MachineInstrBuilder
1413   buildAtomicCmpXchgWithSuccess(const DstOp &OldValRes, const DstOp &SuccessRes,
1414                                 const SrcOp &Addr, const SrcOp &CmpVal,
1415                                 const SrcOp &NewVal, MachineMemOperand &MMO);
1416 
1417   /// Build and insert `OldValRes<def> = G_ATOMIC_CMPXCHG Addr, CmpVal, NewVal,
1418   /// MMO`.
1419   ///
1420   /// Atomically replace the value at \p Addr with \p NewVal if it is currently
1421   /// \p CmpVal otherwise leaves it unchanged. Puts the original value from \p
1422   /// Addr in \p Res.
1423   ///
1424   /// \pre setBasicBlock or setMI must have been called.
1425   /// \pre \p OldValRes must be a generic virtual register of scalar type.
1426   /// \pre \p Addr must be a generic virtual register with pointer type.
1427   /// \pre \p OldValRes, \p CmpVal, and \p NewVal must be generic virtual
1428   ///      registers of the same type.
1429   ///
1430   /// \return a MachineInstrBuilder for the newly created instruction.
1431   MachineInstrBuilder buildAtomicCmpXchg(const DstOp &OldValRes,
1432                                          const SrcOp &Addr, const SrcOp &CmpVal,
1433                                          const SrcOp &NewVal,
1434                                          MachineMemOperand &MMO);
1435 
1436   /// Build and insert `OldValRes<def> = G_ATOMICRMW_<Opcode> Addr, Val, MMO`.
1437   ///
1438   /// Atomically read-modify-update the value at \p Addr with \p Val. Puts the
1439   /// original value from \p Addr in \p OldValRes. The modification is
1440   /// determined by the opcode.
1441   ///
1442   /// \pre setBasicBlock or setMI must have been called.
1443   /// \pre \p OldValRes must be a generic virtual register.
1444   /// \pre \p Addr must be a generic virtual register with pointer type.
1445   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1446   ///      same type.
1447   ///
1448   /// \return a MachineInstrBuilder for the newly created instruction.
1449   MachineInstrBuilder buildAtomicRMW(unsigned Opcode, const DstOp &OldValRes,
1450                                      const SrcOp &Addr, const SrcOp &Val,
1451                                      MachineMemOperand &MMO);
1452 
1453   /// Build and insert `OldValRes<def> = G_ATOMICRMW_XCHG Addr, Val, MMO`.
1454   ///
1455   /// Atomically replace the value at \p Addr with \p Val. Puts the original
1456   /// value from \p Addr in \p OldValRes.
1457   ///
1458   /// \pre setBasicBlock or setMI must have been called.
1459   /// \pre \p OldValRes must be a generic virtual register.
1460   /// \pre \p Addr must be a generic virtual register with pointer type.
1461   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1462   ///      same type.
1463   ///
1464   /// \return a MachineInstrBuilder for the newly created instruction.
1465   MachineInstrBuilder buildAtomicRMWXchg(Register OldValRes, Register Addr,
1466                                          Register Val, MachineMemOperand &MMO);
1467 
1468   /// Build and insert `OldValRes<def> = G_ATOMICRMW_ADD Addr, Val, MMO`.
1469   ///
1470   /// Atomically replace the value at \p Addr with the addition of \p Val and
1471   /// the original value. Puts the original value from \p Addr in \p OldValRes.
1472   ///
1473   /// \pre setBasicBlock or setMI must have been called.
1474   /// \pre \p OldValRes must be a generic virtual register.
1475   /// \pre \p Addr must be a generic virtual register with pointer type.
1476   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1477   ///      same type.
1478   ///
1479   /// \return a MachineInstrBuilder for the newly created instruction.
1480   MachineInstrBuilder buildAtomicRMWAdd(Register OldValRes, Register Addr,
1481                                         Register Val, MachineMemOperand &MMO);
1482 
1483   /// Build and insert `OldValRes<def> = G_ATOMICRMW_SUB Addr, Val, MMO`.
1484   ///
1485   /// Atomically replace the value at \p Addr with the subtraction of \p Val and
1486   /// the original value. Puts the original value from \p Addr in \p OldValRes.
1487   ///
1488   /// \pre setBasicBlock or setMI must have been called.
1489   /// \pre \p OldValRes must be a generic virtual register.
1490   /// \pre \p Addr must be a generic virtual register with pointer type.
1491   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1492   ///      same type.
1493   ///
1494   /// \return a MachineInstrBuilder for the newly created instruction.
1495   MachineInstrBuilder buildAtomicRMWSub(Register OldValRes, Register Addr,
1496                                         Register Val, MachineMemOperand &MMO);
1497 
1498   /// Build and insert `OldValRes<def> = G_ATOMICRMW_AND Addr, Val, MMO`.
1499   ///
1500   /// Atomically replace the value at \p Addr with the bitwise and of \p Val and
1501   /// the original value. Puts the original value from \p Addr in \p OldValRes.
1502   ///
1503   /// \pre setBasicBlock or setMI must have been called.
1504   /// \pre \p OldValRes must be a generic virtual register.
1505   /// \pre \p Addr must be a generic virtual register with pointer type.
1506   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1507   ///      same type.
1508   ///
1509   /// \return a MachineInstrBuilder for the newly created instruction.
1510   MachineInstrBuilder buildAtomicRMWAnd(Register OldValRes, Register Addr,
1511                                         Register Val, MachineMemOperand &MMO);
1512 
1513   /// Build and insert `OldValRes<def> = G_ATOMICRMW_NAND Addr, Val, MMO`.
1514   ///
1515   /// Atomically replace the value at \p Addr with the bitwise nand of \p Val
1516   /// and the original value. Puts the original value from \p Addr in \p
1517   /// OldValRes.
1518   ///
1519   /// \pre setBasicBlock or setMI must have been called.
1520   /// \pre \p OldValRes must be a generic virtual register.
1521   /// \pre \p Addr must be a generic virtual register with pointer type.
1522   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1523   ///      same type.
1524   ///
1525   /// \return a MachineInstrBuilder for the newly created instruction.
1526   MachineInstrBuilder buildAtomicRMWNand(Register OldValRes, Register Addr,
1527                                          Register Val, MachineMemOperand &MMO);
1528 
1529   /// Build and insert `OldValRes<def> = G_ATOMICRMW_OR Addr, Val, MMO`.
1530   ///
1531   /// Atomically replace the value at \p Addr with the bitwise or of \p Val and
1532   /// the original value. Puts the original value from \p Addr in \p OldValRes.
1533   ///
1534   /// \pre setBasicBlock or setMI must have been called.
1535   /// \pre \p OldValRes must be a generic virtual register.
1536   /// \pre \p Addr must be a generic virtual register with pointer type.
1537   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1538   ///      same type.
1539   ///
1540   /// \return a MachineInstrBuilder for the newly created instruction.
1541   MachineInstrBuilder buildAtomicRMWOr(Register OldValRes, Register Addr,
1542                                        Register Val, MachineMemOperand &MMO);
1543 
1544   /// Build and insert `OldValRes<def> = G_ATOMICRMW_XOR Addr, Val, MMO`.
1545   ///
1546   /// Atomically replace the value at \p Addr with the bitwise xor of \p Val and
1547   /// the original value. Puts the original value from \p Addr in \p OldValRes.
1548   ///
1549   /// \pre setBasicBlock or setMI must have been called.
1550   /// \pre \p OldValRes must be a generic virtual register.
1551   /// \pre \p Addr must be a generic virtual register with pointer type.
1552   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1553   ///      same type.
1554   ///
1555   /// \return a MachineInstrBuilder for the newly created instruction.
1556   MachineInstrBuilder buildAtomicRMWXor(Register OldValRes, Register Addr,
1557                                         Register Val, MachineMemOperand &MMO);
1558 
1559   /// Build and insert `OldValRes<def> = G_ATOMICRMW_MAX Addr, Val, MMO`.
1560   ///
1561   /// Atomically replace the value at \p Addr with the signed maximum of \p
1562   /// Val and the original value. Puts the original value from \p Addr in \p
1563   /// OldValRes.
1564   ///
1565   /// \pre setBasicBlock or setMI must have been called.
1566   /// \pre \p OldValRes must be a generic virtual register.
1567   /// \pre \p Addr must be a generic virtual register with pointer type.
1568   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1569   ///      same type.
1570   ///
1571   /// \return a MachineInstrBuilder for the newly created instruction.
1572   MachineInstrBuilder buildAtomicRMWMax(Register OldValRes, Register Addr,
1573                                         Register Val, MachineMemOperand &MMO);
1574 
1575   /// Build and insert `OldValRes<def> = G_ATOMICRMW_MIN Addr, Val, MMO`.
1576   ///
1577   /// Atomically replace the value at \p Addr with the signed minimum of \p
1578   /// Val and the original value. Puts the original value from \p Addr in \p
1579   /// OldValRes.
1580   ///
1581   /// \pre setBasicBlock or setMI must have been called.
1582   /// \pre \p OldValRes must be a generic virtual register.
1583   /// \pre \p Addr must be a generic virtual register with pointer type.
1584   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1585   ///      same type.
1586   ///
1587   /// \return a MachineInstrBuilder for the newly created instruction.
1588   MachineInstrBuilder buildAtomicRMWMin(Register OldValRes, Register Addr,
1589                                         Register Val, MachineMemOperand &MMO);
1590 
1591   /// Build and insert `OldValRes<def> = G_ATOMICRMW_UMAX Addr, Val, MMO`.
1592   ///
1593   /// Atomically replace the value at \p Addr with the unsigned maximum of \p
1594   /// Val and the original value. Puts the original value from \p Addr in \p
1595   /// OldValRes.
1596   ///
1597   /// \pre setBasicBlock or setMI must have been called.
1598   /// \pre \p OldValRes must be a generic virtual register.
1599   /// \pre \p Addr must be a generic virtual register with pointer type.
1600   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1601   ///      same type.
1602   ///
1603   /// \return a MachineInstrBuilder for the newly created instruction.
1604   MachineInstrBuilder buildAtomicRMWUmax(Register OldValRes, Register Addr,
1605                                          Register Val, MachineMemOperand &MMO);
1606 
1607   /// Build and insert `OldValRes<def> = G_ATOMICRMW_UMIN Addr, Val, MMO`.
1608   ///
1609   /// Atomically replace the value at \p Addr with the unsigned minimum of \p
1610   /// Val and the original value. Puts the original value from \p Addr in \p
1611   /// OldValRes.
1612   ///
1613   /// \pre setBasicBlock or setMI must have been called.
1614   /// \pre \p OldValRes must be a generic virtual register.
1615   /// \pre \p Addr must be a generic virtual register with pointer type.
1616   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1617   ///      same type.
1618   ///
1619   /// \return a MachineInstrBuilder for the newly created instruction.
1620   MachineInstrBuilder buildAtomicRMWUmin(Register OldValRes, Register Addr,
1621                                          Register Val, MachineMemOperand &MMO);
1622 
1623   /// Build and insert `OldValRes<def> = G_ATOMICRMW_FADD Addr, Val, MMO`.
1624   MachineInstrBuilder buildAtomicRMWFAdd(
1625     const DstOp &OldValRes, const SrcOp &Addr, const SrcOp &Val,
1626     MachineMemOperand &MMO);
1627 
1628   /// Build and insert `OldValRes<def> = G_ATOMICRMW_FSUB Addr, Val, MMO`.
1629   MachineInstrBuilder buildAtomicRMWFSub(
1630         const DstOp &OldValRes, const SrcOp &Addr, const SrcOp &Val,
1631         MachineMemOperand &MMO);
1632 
1633   /// Build and insert `OldValRes<def> = G_ATOMICRMW_FMAX Addr, Val, MMO`.
1634   ///
1635   /// Atomically replace the value at \p Addr with the floating point maximum of
1636   /// \p 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 buildAtomicRMWFMax(
1647         const DstOp &OldValRes, const SrcOp &Addr, const SrcOp &Val,
1648         MachineMemOperand &MMO);
1649 
1650   /// Build and insert `OldValRes<def> = G_ATOMICRMW_FMIN Addr, Val, MMO`.
1651   ///
1652   /// Atomically replace the value at \p Addr with the floating point minimum of
1653   /// \p Val and the original value. Puts the original value from \p Addr in \p
1654   /// OldValRes.
1655   ///
1656   /// \pre setBasicBlock or setMI must have been called.
1657   /// \pre \p OldValRes must be a generic virtual register.
1658   /// \pre \p Addr must be a generic virtual register with pointer type.
1659   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1660   ///      same type.
1661   ///
1662   /// \return a MachineInstrBuilder for the newly created instruction.
1663   MachineInstrBuilder buildAtomicRMWFMin(
1664         const DstOp &OldValRes, const SrcOp &Addr, const SrcOp &Val,
1665         MachineMemOperand &MMO);
1666 
1667   /// Build and insert `OldValRes<def> = G_ATOMICRMW_USUB_COND Addr, Val, MMO`.
1668   ///
1669   /// Atomically replace the value at \p Addr with the original value minus \p
1670   /// Val if the original value is greater than or equal to \p Val, or leaves it
1671   /// unchanged otherwise. Puts the original value from \p Addr in \p OldValRes.
1672   ///
1673   /// \pre setBasicBlock or setMI must have been called.
1674   /// \pre \p OldValRes must be a generic virtual register.
1675   /// \pre \p Addr must be a generic virtual register with pointer type.
1676   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1677   ///      same type.
1678   ///
1679   /// \return a MachineInstrBuilder for the newly created instruction.
1680   MachineInstrBuilder buildAtomicRMWUSubCond(const DstOp &OldValRes,
1681                                              const SrcOp &Addr,
1682                                              const SrcOp &Val,
1683                                              MachineMemOperand &MMO);
1684 
1685   /// Build and insert `OldValRes<def> = G_ATOMICRMW_USUB_SAT Addr, Val, MMO`.
1686   ///
1687   /// Atomically replace the value at \p Addr with the original value minus \p
1688   /// Val, with clamping to zero if the unsigned subtraction would overflow.
1689   /// Puts the original value from \p Addr in \p OldValRes.
1690   ///
1691   /// \pre setBasicBlock or setMI must have been called.
1692   /// \pre \p OldValRes must be a generic virtual register.
1693   /// \pre \p Addr must be a generic virtual register with pointer type.
1694   /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1695   ///      same type.
1696   ///
1697   /// \return a MachineInstrBuilder for the newly created instruction.
1698   MachineInstrBuilder buildAtomicRMWUSubSat(const DstOp &OldValRes,
1699                                             const SrcOp &Addr, const SrcOp &Val,
1700                                             MachineMemOperand &MMO);
1701 
1702   /// Build and insert `G_FENCE Ordering, Scope`.
1703   MachineInstrBuilder buildFence(unsigned Ordering, unsigned Scope);
1704 
1705   /// Build and insert G_PREFETCH \p Addr, \p RW, \p Locality, \p CacheType
1706   MachineInstrBuilder buildPrefetch(const SrcOp &Addr, unsigned RW,
1707                                     unsigned Locality, unsigned CacheType,
1708                                     MachineMemOperand &MMO);
1709 
1710   /// Build and insert \p Dst = G_FREEZE \p Src
1711   MachineInstrBuilder buildFreeze(const DstOp &Dst, const SrcOp &Src) {
1712     return buildInstr(TargetOpcode::G_FREEZE, {Dst}, {Src});
1713   }
1714 
1715   /// Build and insert \p Res = G_BLOCK_ADDR \p BA
1716   ///
1717   /// G_BLOCK_ADDR computes the address of a basic block.
1718   ///
1719   /// \pre setBasicBlock or setMI must have been called.
1720   /// \pre \p Res must be a generic virtual register of a pointer type.
1721   ///
1722   /// \return The newly created instruction.
1723   MachineInstrBuilder buildBlockAddress(Register Res, const BlockAddress *BA);
1724 
1725   /// Build and insert \p Res = G_ADD \p Op0, \p Op1
1726   ///
1727   /// G_ADD sets \p Res to the sum of integer parameters \p Op0 and \p Op1,
1728   /// truncated to their width.
1729   ///
1730   /// \pre setBasicBlock or setMI must have been called.
1731   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1732   ///      with the same (scalar or vector) type).
1733   ///
1734   /// \return a MachineInstrBuilder for the newly created instruction.
1735 
1736   MachineInstrBuilder buildAdd(const DstOp &Dst, const SrcOp &Src0,
1737                                const SrcOp &Src1,
1738                                std::optional<unsigned> Flags = std::nullopt) {
1739     return buildInstr(TargetOpcode::G_ADD, {Dst}, {Src0, Src1}, Flags);
1740   }
1741 
1742   /// Build and insert \p Res = G_SUB \p Op0, \p Op1
1743   ///
1744   /// G_SUB sets \p Res to the difference of integer parameters \p Op0 and
1745   /// \p Op1, truncated to their width.
1746   ///
1747   /// \pre setBasicBlock or setMI must have been called.
1748   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1749   ///      with the same (scalar or vector) type).
1750   ///
1751   /// \return a MachineInstrBuilder for the newly created instruction.
1752 
1753   MachineInstrBuilder buildSub(const DstOp &Dst, const SrcOp &Src0,
1754                                const SrcOp &Src1,
1755                                std::optional<unsigned> Flags = std::nullopt) {
1756     return buildInstr(TargetOpcode::G_SUB, {Dst}, {Src0, Src1}, Flags);
1757   }
1758 
1759   /// Build and insert \p Res = G_MUL \p Op0, \p Op1
1760   ///
1761   /// G_MUL sets \p Res to the product of integer parameters \p Op0 and \p Op1,
1762   /// truncated to their width.
1763   ///
1764   /// \pre setBasicBlock or setMI must have been called.
1765   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1766   ///      with the same (scalar or vector) type).
1767   ///
1768   /// \return a MachineInstrBuilder for the newly created instruction.
1769   MachineInstrBuilder buildMul(const DstOp &Dst, const SrcOp &Src0,
1770                                const SrcOp &Src1,
1771                                std::optional<unsigned> Flags = std::nullopt) {
1772     return buildInstr(TargetOpcode::G_MUL, {Dst}, {Src0, Src1}, Flags);
1773   }
1774 
1775   /// Build and insert \p Res = G_ABDS \p Op0, \p Op1
1776   ///
1777   /// G_ABDS return the signed absolute difference of \p Op0 and \p Op1.
1778   ///
1779   /// \pre setBasicBlock or setMI must have been called.
1780   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1781   ///      with the same (scalar or vector) type).
1782   ///
1783   /// \return a MachineInstrBuilder for the newly created instruction.
1784   MachineInstrBuilder buildAbds(const DstOp &Dst, const SrcOp &Src0,
1785                                 const SrcOp &Src1) {
1786     return buildInstr(TargetOpcode::G_ABDS, {Dst}, {Src0, Src1});
1787   }
1788 
1789   /// Build and insert \p Res = G_ABDU \p Op0, \p Op1
1790   ///
1791   /// G_ABDU return the unsigned absolute difference of \p Op0 and \p Op1.
1792   ///
1793   /// \pre setBasicBlock or setMI must have been called.
1794   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1795   ///      with the same (scalar or vector) type).
1796   ///
1797   /// \return a MachineInstrBuilder for the newly created instruction.
1798   MachineInstrBuilder buildAbdu(const DstOp &Dst, const SrcOp &Src0,
1799                                 const SrcOp &Src1) {
1800     return buildInstr(TargetOpcode::G_ABDU, {Dst}, {Src0, Src1});
1801   }
1802 
1803   MachineInstrBuilder buildUMulH(const DstOp &Dst, const SrcOp &Src0,
1804                                  const SrcOp &Src1,
1805                                  std::optional<unsigned> Flags = std::nullopt) {
1806     return buildInstr(TargetOpcode::G_UMULH, {Dst}, {Src0, Src1}, Flags);
1807   }
1808 
1809   MachineInstrBuilder buildSMulH(const DstOp &Dst, const SrcOp &Src0,
1810                                  const SrcOp &Src1,
1811                                  std::optional<unsigned> Flags = std::nullopt) {
1812     return buildInstr(TargetOpcode::G_SMULH, {Dst}, {Src0, Src1}, Flags);
1813   }
1814 
1815   /// Build and insert \p Res = G_UREM \p Op0, \p Op1
1816   MachineInstrBuilder buildURem(const DstOp &Dst, const SrcOp &Src0,
1817                                 const SrcOp &Src1,
1818                                 std::optional<unsigned> Flags = std::nullopt) {
1819     return buildInstr(TargetOpcode::G_UREM, {Dst}, {Src0, Src1}, Flags);
1820   }
1821 
1822   MachineInstrBuilder buildFMul(const DstOp &Dst, const SrcOp &Src0,
1823                                 const SrcOp &Src1,
1824                                 std::optional<unsigned> Flags = std::nullopt) {
1825     return buildInstr(TargetOpcode::G_FMUL, {Dst}, {Src0, Src1}, Flags);
1826   }
1827 
1828   MachineInstrBuilder
1829   buildFMinNum(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
1830                std::optional<unsigned> Flags = std::nullopt) {
1831     return buildInstr(TargetOpcode::G_FMINNUM, {Dst}, {Src0, Src1}, Flags);
1832   }
1833 
1834   MachineInstrBuilder
1835   buildFMaxNum(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
1836                std::optional<unsigned> Flags = std::nullopt) {
1837     return buildInstr(TargetOpcode::G_FMAXNUM, {Dst}, {Src0, Src1}, Flags);
1838   }
1839 
1840   MachineInstrBuilder
1841   buildFMinNumIEEE(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
1842                    std::optional<unsigned> Flags = std::nullopt) {
1843     return buildInstr(TargetOpcode::G_FMINNUM_IEEE, {Dst}, {Src0, Src1}, Flags);
1844   }
1845 
1846   MachineInstrBuilder
1847   buildFMaxNumIEEE(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
1848                    std::optional<unsigned> Flags = std::nullopt) {
1849     return buildInstr(TargetOpcode::G_FMAXNUM_IEEE, {Dst}, {Src0, Src1}, Flags);
1850   }
1851 
1852   MachineInstrBuilder buildShl(const DstOp &Dst, const SrcOp &Src0,
1853                                const SrcOp &Src1,
1854                                std::optional<unsigned> Flags = std::nullopt) {
1855     return buildInstr(TargetOpcode::G_SHL, {Dst}, {Src0, Src1}, Flags);
1856   }
1857 
1858   MachineInstrBuilder buildLShr(const DstOp &Dst, const SrcOp &Src0,
1859                                 const SrcOp &Src1,
1860                                 std::optional<unsigned> Flags = std::nullopt) {
1861     return buildInstr(TargetOpcode::G_LSHR, {Dst}, {Src0, Src1}, Flags);
1862   }
1863 
1864   MachineInstrBuilder buildAShr(const DstOp &Dst, const SrcOp &Src0,
1865                                 const SrcOp &Src1,
1866                                 std::optional<unsigned> Flags = std::nullopt) {
1867     return buildInstr(TargetOpcode::G_ASHR, {Dst}, {Src0, Src1}, Flags);
1868   }
1869 
1870   /// Build and insert \p Res = G_AND \p Op0, \p Op1
1871   ///
1872   /// G_AND sets \p Res to the bitwise and of integer parameters \p Op0 and \p
1873   /// Op1.
1874   ///
1875   /// \pre setBasicBlock or setMI must have been called.
1876   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1877   ///      with the same (scalar or vector) type).
1878   ///
1879   /// \return a MachineInstrBuilder for the newly created instruction.
1880 
1881   MachineInstrBuilder buildAnd(const DstOp &Dst, const SrcOp &Src0,
1882                                const SrcOp &Src1) {
1883     return buildInstr(TargetOpcode::G_AND, {Dst}, {Src0, Src1});
1884   }
1885 
1886   /// Build and insert \p Res = G_OR \p Op0, \p Op1
1887   ///
1888   /// G_OR sets \p Res to the bitwise or of integer parameters \p Op0 and \p
1889   /// Op1.
1890   ///
1891   /// \pre setBasicBlock or setMI must have been called.
1892   /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1893   ///      with the same (scalar or vector) type).
1894   ///
1895   /// \return a MachineInstrBuilder for the newly created instruction.
1896   MachineInstrBuilder buildOr(const DstOp &Dst, const SrcOp &Src0,
1897                               const SrcOp &Src1,
1898                               std::optional<unsigned> Flags = std::nullopt) {
1899     return buildInstr(TargetOpcode::G_OR, {Dst}, {Src0, Src1}, Flags);
1900   }
1901 
1902   /// Build and insert \p Res = G_XOR \p Op0, \p Op1
1903   MachineInstrBuilder buildXor(const DstOp &Dst, const SrcOp &Src0,
1904                                const SrcOp &Src1) {
1905     return buildInstr(TargetOpcode::G_XOR, {Dst}, {Src0, Src1});
1906   }
1907 
1908   /// Build and insert a bitwise not,
1909   /// \p NegOne = G_CONSTANT -1
1910   /// \p Res = G_OR \p Op0, NegOne
1911   MachineInstrBuilder buildNot(const DstOp &Dst, const SrcOp &Src0) {
1912     auto NegOne = buildConstant(Dst.getLLTTy(*getMRI()), -1);
1913     return buildInstr(TargetOpcode::G_XOR, {Dst}, {Src0, NegOne});
1914   }
1915 
1916   /// Build and insert integer negation
1917   /// \p Zero = G_CONSTANT 0
1918   /// \p Res = G_SUB Zero, \p Op0
1919   MachineInstrBuilder buildNeg(const DstOp &Dst, const SrcOp &Src0) {
1920     auto Zero = buildConstant(Dst.getLLTTy(*getMRI()), 0);
1921     return buildInstr(TargetOpcode::G_SUB, {Dst}, {Zero, Src0});
1922   }
1923 
1924   /// Build and insert \p Res = G_CTPOP \p Op0, \p Src0
1925   MachineInstrBuilder buildCTPOP(const DstOp &Dst, const SrcOp &Src0) {
1926     return buildInstr(TargetOpcode::G_CTPOP, {Dst}, {Src0});
1927   }
1928 
1929   /// Build and insert \p Res = G_CTLZ \p Op0, \p Src0
1930   MachineInstrBuilder buildCTLZ(const DstOp &Dst, const SrcOp &Src0) {
1931     return buildInstr(TargetOpcode::G_CTLZ, {Dst}, {Src0});
1932   }
1933 
1934   /// Build and insert \p Res = G_CTLZ_ZERO_UNDEF \p Op0, \p Src0
1935   MachineInstrBuilder buildCTLZ_ZERO_UNDEF(const DstOp &Dst, const SrcOp &Src0) {
1936     return buildInstr(TargetOpcode::G_CTLZ_ZERO_UNDEF, {Dst}, {Src0});
1937   }
1938 
1939   /// Build and insert \p Res = G_CTTZ \p Op0, \p Src0
1940   MachineInstrBuilder buildCTTZ(const DstOp &Dst, const SrcOp &Src0) {
1941     return buildInstr(TargetOpcode::G_CTTZ, {Dst}, {Src0});
1942   }
1943 
1944   /// Build and insert \p Res = G_CTTZ_ZERO_UNDEF \p Op0, \p Src0
1945   MachineInstrBuilder buildCTTZ_ZERO_UNDEF(const DstOp &Dst, const SrcOp &Src0) {
1946     return buildInstr(TargetOpcode::G_CTTZ_ZERO_UNDEF, {Dst}, {Src0});
1947   }
1948 
1949   /// Build and insert \p Dst = G_BSWAP \p Src0
1950   MachineInstrBuilder buildBSwap(const DstOp &Dst, const SrcOp &Src0) {
1951     return buildInstr(TargetOpcode::G_BSWAP, {Dst}, {Src0});
1952   }
1953 
1954   /// Build and insert \p Res = G_FADD \p Op0, \p Op1
1955   MachineInstrBuilder buildFAdd(const DstOp &Dst, const SrcOp &Src0,
1956                                 const SrcOp &Src1,
1957                                 std::optional<unsigned> Flags = std::nullopt) {
1958     return buildInstr(TargetOpcode::G_FADD, {Dst}, {Src0, Src1}, Flags);
1959   }
1960 
1961   /// Build and insert \p Res = G_STRICT_FADD \p Op0, \p Op1
1962   MachineInstrBuilder
1963   buildStrictFAdd(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
1964                   std::optional<unsigned> Flags = std::nullopt) {
1965     return buildInstr(TargetOpcode::G_STRICT_FADD, {Dst}, {Src0, Src1}, Flags);
1966   }
1967 
1968   /// Build and insert \p Res = G_FSUB \p Op0, \p Op1
1969   MachineInstrBuilder buildFSub(const DstOp &Dst, const SrcOp &Src0,
1970                                 const SrcOp &Src1,
1971                                 std::optional<unsigned> Flags = std::nullopt) {
1972     return buildInstr(TargetOpcode::G_FSUB, {Dst}, {Src0, Src1}, Flags);
1973   }
1974 
1975   /// Build and insert \p Res = G_FDIV \p Op0, \p Op1
1976   MachineInstrBuilder buildFDiv(const DstOp &Dst, const SrcOp &Src0,
1977                                 const SrcOp &Src1,
1978                                 std::optional<unsigned> Flags = std::nullopt) {
1979     return buildInstr(TargetOpcode::G_FDIV, {Dst}, {Src0, Src1}, Flags);
1980   }
1981 
1982   /// Build and insert \p Res = G_FMA \p Op0, \p Op1, \p Op2
1983   MachineInstrBuilder buildFMA(const DstOp &Dst, const SrcOp &Src0,
1984                                const SrcOp &Src1, const SrcOp &Src2,
1985                                std::optional<unsigned> Flags = std::nullopt) {
1986     return buildInstr(TargetOpcode::G_FMA, {Dst}, {Src0, Src1, Src2}, Flags);
1987   }
1988 
1989   /// Build and insert \p Res = G_FMAD \p Op0, \p Op1, \p Op2
1990   MachineInstrBuilder buildFMAD(const DstOp &Dst, const SrcOp &Src0,
1991                                 const SrcOp &Src1, const SrcOp &Src2,
1992                                 std::optional<unsigned> Flags = std::nullopt) {
1993     return buildInstr(TargetOpcode::G_FMAD, {Dst}, {Src0, Src1, Src2}, Flags);
1994   }
1995 
1996   /// Build and insert \p Res = G_FNEG \p Op0
1997   MachineInstrBuilder buildFNeg(const DstOp &Dst, const SrcOp &Src0,
1998                                 std::optional<unsigned> Flags = std::nullopt) {
1999     return buildInstr(TargetOpcode::G_FNEG, {Dst}, {Src0}, Flags);
2000   }
2001 
2002   /// Build and insert \p Res = G_FABS \p Op0
2003   MachineInstrBuilder buildFAbs(const DstOp &Dst, const SrcOp &Src0,
2004                                 std::optional<unsigned> Flags = std::nullopt) {
2005     return buildInstr(TargetOpcode::G_FABS, {Dst}, {Src0}, Flags);
2006   }
2007 
2008   /// Build and insert \p Dst = G_FCANONICALIZE \p Src0
2009   MachineInstrBuilder
2010   buildFCanonicalize(const DstOp &Dst, const SrcOp &Src0,
2011                      std::optional<unsigned> Flags = std::nullopt) {
2012     return buildInstr(TargetOpcode::G_FCANONICALIZE, {Dst}, {Src0}, Flags);
2013   }
2014 
2015   /// Build and insert \p Dst = G_INTRINSIC_TRUNC \p Src0
2016   MachineInstrBuilder
2017   buildIntrinsicTrunc(const DstOp &Dst, const SrcOp &Src0,
2018                       std::optional<unsigned> Flags = std::nullopt) {
2019     return buildInstr(TargetOpcode::G_INTRINSIC_TRUNC, {Dst}, {Src0}, Flags);
2020   }
2021 
2022   /// Build and insert \p Res = GFFLOOR \p Op0, \p Op1
2023   MachineInstrBuilder
2024   buildFFloor(const DstOp &Dst, const SrcOp &Src0,
2025               std::optional<unsigned> Flags = std::nullopt) {
2026     return buildInstr(TargetOpcode::G_FFLOOR, {Dst}, {Src0}, Flags);
2027   }
2028 
2029   /// Build and insert \p Dst = G_FLOG \p Src
2030   MachineInstrBuilder buildFLog(const DstOp &Dst, const SrcOp &Src,
2031                                 std::optional<unsigned> Flags = std::nullopt) {
2032     return buildInstr(TargetOpcode::G_FLOG, {Dst}, {Src}, Flags);
2033   }
2034 
2035   /// Build and insert \p Dst = G_FLOG2 \p Src
2036   MachineInstrBuilder buildFLog2(const DstOp &Dst, const SrcOp &Src,
2037                                  std::optional<unsigned> Flags = std::nullopt) {
2038     return buildInstr(TargetOpcode::G_FLOG2, {Dst}, {Src}, Flags);
2039   }
2040 
2041   /// Build and insert \p Dst = G_FEXP2 \p Src
2042   MachineInstrBuilder buildFExp2(const DstOp &Dst, const SrcOp &Src,
2043                                  std::optional<unsigned> Flags = std::nullopt) {
2044     return buildInstr(TargetOpcode::G_FEXP2, {Dst}, {Src}, Flags);
2045   }
2046 
2047   /// Build and insert \p Dst = G_FPOW \p Src0, \p Src1
2048   MachineInstrBuilder buildFPow(const DstOp &Dst, const SrcOp &Src0,
2049                                 const SrcOp &Src1,
2050                                 std::optional<unsigned> Flags = std::nullopt) {
2051     return buildInstr(TargetOpcode::G_FPOW, {Dst}, {Src0, Src1}, Flags);
2052   }
2053 
2054   /// Build and insert \p Dst = G_FLDEXP \p Src0, \p Src1
2055   MachineInstrBuilder
2056   buildFLdexp(const DstOp &Dst, const SrcOp &Src0, const SrcOp &Src1,
2057               std::optional<unsigned> Flags = std::nullopt) {
2058     return buildInstr(TargetOpcode::G_FLDEXP, {Dst}, {Src0, Src1}, Flags);
2059   }
2060 
2061   /// Build and insert \p Fract, \p Exp = G_FFREXP \p Src
2062   MachineInstrBuilder
2063   buildFFrexp(const DstOp &Fract, const DstOp &Exp, const SrcOp &Src,
2064               std::optional<unsigned> Flags = std::nullopt) {
2065     return buildInstr(TargetOpcode::G_FFREXP, {Fract, Exp}, {Src}, Flags);
2066   }
2067 
2068   /// Build and insert \p Sin, \p Cos = G_FSINCOS \p Src
2069   MachineInstrBuilder
2070   buildFSincos(const DstOp &Sin, const DstOp &Cos, const SrcOp &Src,
2071                std::optional<unsigned> Flags = std::nullopt) {
2072     return buildInstr(TargetOpcode::G_FSINCOS, {Sin, Cos}, {Src}, Flags);
2073   }
2074 
2075   /// Build and insert \p Res = G_FCOPYSIGN \p Op0, \p Op1
2076   MachineInstrBuilder buildFCopysign(const DstOp &Dst, const SrcOp &Src0,
2077                                      const SrcOp &Src1) {
2078     return buildInstr(TargetOpcode::G_FCOPYSIGN, {Dst}, {Src0, Src1});
2079   }
2080 
2081   /// Build and insert \p Res = G_UITOFP \p Src0
2082   MachineInstrBuilder buildUITOFP(const DstOp &Dst, const SrcOp &Src0) {
2083     return buildInstr(TargetOpcode::G_UITOFP, {Dst}, {Src0});
2084   }
2085 
2086   /// Build and insert \p Res = G_SITOFP \p Src0
2087   MachineInstrBuilder buildSITOFP(const DstOp &Dst, const SrcOp &Src0) {
2088     return buildInstr(TargetOpcode::G_SITOFP, {Dst}, {Src0});
2089   }
2090 
2091   /// Build and insert \p Res = G_FPTOUI \p Src0
2092   MachineInstrBuilder buildFPTOUI(const DstOp &Dst, const SrcOp &Src0) {
2093     return buildInstr(TargetOpcode::G_FPTOUI, {Dst}, {Src0});
2094   }
2095 
2096   /// Build and insert \p Res = G_FPTOSI \p Src0
2097   MachineInstrBuilder buildFPTOSI(const DstOp &Dst, const SrcOp &Src0) {
2098     return buildInstr(TargetOpcode::G_FPTOSI, {Dst}, {Src0});
2099   }
2100 
2101   /// Build and insert \p Res = G_FPTOUI_SAT \p Src0
2102   MachineInstrBuilder buildFPTOUI_SAT(const DstOp &Dst, const SrcOp &Src0) {
2103     return buildInstr(TargetOpcode::G_FPTOUI_SAT, {Dst}, {Src0});
2104   }
2105 
2106   /// Build and insert \p Res = G_FPTOSI_SAT \p Src0
2107   MachineInstrBuilder buildFPTOSI_SAT(const DstOp &Dst, const SrcOp &Src0) {
2108     return buildInstr(TargetOpcode::G_FPTOSI_SAT, {Dst}, {Src0});
2109   }
2110 
2111   /// Build and insert \p Dst = G_INTRINSIC_ROUNDEVEN \p Src0, \p Src1
2112   MachineInstrBuilder
2113   buildIntrinsicRoundeven(const DstOp &Dst, const SrcOp &Src0,
2114                           std::optional<unsigned> Flags = std::nullopt) {
2115     return buildInstr(TargetOpcode::G_INTRINSIC_ROUNDEVEN, {Dst}, {Src0},
2116                       Flags);
2117   }
2118 
2119   /// Build and insert \p Res = G_SMIN \p Op0, \p Op1
2120   MachineInstrBuilder buildSMin(const DstOp &Dst, const SrcOp &Src0,
2121                                 const SrcOp &Src1) {
2122     return buildInstr(TargetOpcode::G_SMIN, {Dst}, {Src0, Src1});
2123   }
2124 
2125   /// Build and insert \p Res = G_SMAX \p Op0, \p Op1
2126   MachineInstrBuilder buildSMax(const DstOp &Dst, const SrcOp &Src0,
2127                                 const SrcOp &Src1) {
2128     return buildInstr(TargetOpcode::G_SMAX, {Dst}, {Src0, Src1});
2129   }
2130 
2131   /// Build and insert \p Res = G_UMIN \p Op0, \p Op1
2132   MachineInstrBuilder buildUMin(const DstOp &Dst, const SrcOp &Src0,
2133                                 const SrcOp &Src1) {
2134     return buildInstr(TargetOpcode::G_UMIN, {Dst}, {Src0, Src1});
2135   }
2136 
2137   /// Build and insert \p Res = G_UMAX \p Op0, \p Op1
2138   MachineInstrBuilder buildUMax(const DstOp &Dst, const SrcOp &Src0,
2139                                 const SrcOp &Src1) {
2140     return buildInstr(TargetOpcode::G_UMAX, {Dst}, {Src0, Src1});
2141   }
2142 
2143   /// Build and insert \p Dst = G_ABS \p Src
2144   MachineInstrBuilder buildAbs(const DstOp &Dst, const SrcOp &Src) {
2145     return buildInstr(TargetOpcode::G_ABS, {Dst}, {Src});
2146   }
2147 
2148   /// Build and insert \p Res = G_JUMP_TABLE \p JTI
2149   ///
2150   /// G_JUMP_TABLE sets \p Res to the address of the jump table specified by
2151   /// the jump table index \p JTI.
2152   ///
2153   /// \return a MachineInstrBuilder for the newly created instruction.
2154   MachineInstrBuilder buildJumpTable(const LLT PtrTy, unsigned JTI);
2155 
2156   /// Build and insert \p Res = G_VECREDUCE_SEQ_FADD \p ScalarIn, \p VecIn
2157   ///
2158   /// \p ScalarIn is the scalar accumulator input to start the sequential
2159   /// reduction operation of \p VecIn.
2160   MachineInstrBuilder buildVecReduceSeqFAdd(const DstOp &Dst,
2161                                             const SrcOp &ScalarIn,
2162                                             const SrcOp &VecIn) {
2163     return buildInstr(TargetOpcode::G_VECREDUCE_SEQ_FADD, {Dst},
2164                       {ScalarIn, {VecIn}});
2165   }
2166 
2167   /// Build and insert \p Res = G_VECREDUCE_SEQ_FMUL \p ScalarIn, \p VecIn
2168   ///
2169   /// \p ScalarIn is the scalar accumulator input to start the sequential
2170   /// reduction operation of \p VecIn.
2171   MachineInstrBuilder buildVecReduceSeqFMul(const DstOp &Dst,
2172                                             const SrcOp &ScalarIn,
2173                                             const SrcOp &VecIn) {
2174     return buildInstr(TargetOpcode::G_VECREDUCE_SEQ_FMUL, {Dst},
2175                       {ScalarIn, {VecIn}});
2176   }
2177 
2178   /// Build and insert \p Res = G_VECREDUCE_FADD \p Src
2179   ///
2180   /// \p ScalarIn is the scalar accumulator input to the reduction operation of
2181   /// \p VecIn.
2182   MachineInstrBuilder buildVecReduceFAdd(const DstOp &Dst,
2183                                          const SrcOp &ScalarIn,
2184                                          const SrcOp &VecIn) {
2185     return buildInstr(TargetOpcode::G_VECREDUCE_FADD, {Dst}, {ScalarIn, VecIn});
2186   }
2187 
2188   /// Build and insert \p Res = G_VECREDUCE_FMUL \p Src
2189   ///
2190   /// \p ScalarIn is the scalar accumulator input to the reduction operation of
2191   /// \p VecIn.
2192   MachineInstrBuilder buildVecReduceFMul(const DstOp &Dst,
2193                                          const SrcOp &ScalarIn,
2194                                          const SrcOp &VecIn) {
2195     return buildInstr(TargetOpcode::G_VECREDUCE_FMUL, {Dst}, {ScalarIn, VecIn});
2196   }
2197 
2198   /// Build and insert \p Res = G_VECREDUCE_FMAX \p Src
2199   MachineInstrBuilder buildVecReduceFMax(const DstOp &Dst, const SrcOp &Src) {
2200     return buildInstr(TargetOpcode::G_VECREDUCE_FMAX, {Dst}, {Src});
2201   }
2202 
2203   /// Build and insert \p Res = G_VECREDUCE_FMIN \p Src
2204   MachineInstrBuilder buildVecReduceFMin(const DstOp &Dst, const SrcOp &Src) {
2205     return buildInstr(TargetOpcode::G_VECREDUCE_FMIN, {Dst}, {Src});
2206   }
2207 
2208   /// Build and insert \p Res = G_VECREDUCE_FMAXIMUM \p Src
2209   MachineInstrBuilder buildVecReduceFMaximum(const DstOp &Dst,
2210                                              const SrcOp &Src) {
2211     return buildInstr(TargetOpcode::G_VECREDUCE_FMAXIMUM, {Dst}, {Src});
2212   }
2213 
2214   /// Build and insert \p Res = G_VECREDUCE_FMINIMUM \p Src
2215   MachineInstrBuilder buildVecReduceFMinimum(const DstOp &Dst,
2216                                              const SrcOp &Src) {
2217     return buildInstr(TargetOpcode::G_VECREDUCE_FMINIMUM, {Dst}, {Src});
2218   }
2219 
2220   /// Build and insert \p Res = G_VECREDUCE_ADD \p Src
2221   MachineInstrBuilder buildVecReduceAdd(const DstOp &Dst, const SrcOp &Src) {
2222     return buildInstr(TargetOpcode::G_VECREDUCE_ADD, {Dst}, {Src});
2223   }
2224 
2225   /// Build and insert \p Res = G_VECREDUCE_MUL \p Src
2226   MachineInstrBuilder buildVecReduceMul(const DstOp &Dst, const SrcOp &Src) {
2227     return buildInstr(TargetOpcode::G_VECREDUCE_MUL, {Dst}, {Src});
2228   }
2229 
2230   /// Build and insert \p Res = G_VECREDUCE_AND \p Src
2231   MachineInstrBuilder buildVecReduceAnd(const DstOp &Dst, const SrcOp &Src) {
2232     return buildInstr(TargetOpcode::G_VECREDUCE_AND, {Dst}, {Src});
2233   }
2234 
2235   /// Build and insert \p Res = G_VECREDUCE_OR \p Src
2236   MachineInstrBuilder buildVecReduceOr(const DstOp &Dst, const SrcOp &Src) {
2237     return buildInstr(TargetOpcode::G_VECREDUCE_OR, {Dst}, {Src});
2238   }
2239 
2240   /// Build and insert \p Res = G_VECREDUCE_XOR \p Src
2241   MachineInstrBuilder buildVecReduceXor(const DstOp &Dst, const SrcOp &Src) {
2242     return buildInstr(TargetOpcode::G_VECREDUCE_XOR, {Dst}, {Src});
2243   }
2244 
2245   /// Build and insert \p Res = G_VECREDUCE_SMAX \p Src
2246   MachineInstrBuilder buildVecReduceSMax(const DstOp &Dst, const SrcOp &Src) {
2247     return buildInstr(TargetOpcode::G_VECREDUCE_SMAX, {Dst}, {Src});
2248   }
2249 
2250   /// Build and insert \p Res = G_VECREDUCE_SMIN \p Src
2251   MachineInstrBuilder buildVecReduceSMin(const DstOp &Dst, const SrcOp &Src) {
2252     return buildInstr(TargetOpcode::G_VECREDUCE_SMIN, {Dst}, {Src});
2253   }
2254 
2255   /// Build and insert \p Res = G_VECREDUCE_UMAX \p Src
2256   MachineInstrBuilder buildVecReduceUMax(const DstOp &Dst, const SrcOp &Src) {
2257     return buildInstr(TargetOpcode::G_VECREDUCE_UMAX, {Dst}, {Src});
2258   }
2259 
2260   /// Build and insert \p Res = G_VECREDUCE_UMIN \p Src
2261   MachineInstrBuilder buildVecReduceUMin(const DstOp &Dst, const SrcOp &Src) {
2262     return buildInstr(TargetOpcode::G_VECREDUCE_UMIN, {Dst}, {Src});
2263   }
2264 
2265   /// Build and insert G_MEMCPY or G_MEMMOVE
2266   MachineInstrBuilder buildMemTransferInst(unsigned Opcode, const SrcOp &DstPtr,
2267                                            const SrcOp &SrcPtr,
2268                                            const SrcOp &Size,
2269                                            MachineMemOperand &DstMMO,
2270                                            MachineMemOperand &SrcMMO) {
2271     auto MIB = buildInstr(
2272         Opcode, {}, {DstPtr, SrcPtr, Size, SrcOp(INT64_C(0) /*isTailCall*/)});
2273     MIB.addMemOperand(&DstMMO);
2274     MIB.addMemOperand(&SrcMMO);
2275     return MIB;
2276   }
2277 
2278   MachineInstrBuilder buildMemCpy(const SrcOp &DstPtr, const SrcOp &SrcPtr,
2279                                   const SrcOp &Size, MachineMemOperand &DstMMO,
2280                                   MachineMemOperand &SrcMMO) {
2281     return buildMemTransferInst(TargetOpcode::G_MEMCPY, DstPtr, SrcPtr, Size,
2282                                 DstMMO, SrcMMO);
2283   }
2284 
2285   /// Build and insert G_TRAP or G_DEBUGTRAP
2286   MachineInstrBuilder buildTrap(bool Debug = false) {
2287     return buildInstr(Debug ? TargetOpcode::G_DEBUGTRAP : TargetOpcode::G_TRAP);
2288   }
2289 
2290   /// Build and insert \p Dst = G_SBFX \p Src, \p LSB, \p Width.
2291   MachineInstrBuilder buildSbfx(const DstOp &Dst, const SrcOp &Src,
2292                                 const SrcOp &LSB, const SrcOp &Width) {
2293     return buildInstr(TargetOpcode::G_SBFX, {Dst}, {Src, LSB, Width});
2294   }
2295 
2296   /// Build and insert \p Dst = G_UBFX \p Src, \p LSB, \p Width.
2297   MachineInstrBuilder buildUbfx(const DstOp &Dst, const SrcOp &Src,
2298                                 const SrcOp &LSB, const SrcOp &Width) {
2299     return buildInstr(TargetOpcode::G_UBFX, {Dst}, {Src, LSB, Width});
2300   }
2301 
2302   /// Build and insert \p Dst = G_ROTR \p Src, \p Amt
2303   MachineInstrBuilder buildRotateRight(const DstOp &Dst, const SrcOp &Src,
2304                                        const SrcOp &Amt) {
2305     return buildInstr(TargetOpcode::G_ROTR, {Dst}, {Src, Amt});
2306   }
2307 
2308   /// Build and insert \p Dst = G_ROTL \p Src, \p Amt
2309   MachineInstrBuilder buildRotateLeft(const DstOp &Dst, const SrcOp &Src,
2310                                       const SrcOp &Amt) {
2311     return buildInstr(TargetOpcode::G_ROTL, {Dst}, {Src, Amt});
2312   }
2313 
2314   /// Build and insert \p Dst = G_BITREVERSE \p Src
2315   MachineInstrBuilder buildBitReverse(const DstOp &Dst, const SrcOp &Src) {
2316     return buildInstr(TargetOpcode::G_BITREVERSE, {Dst}, {Src});
2317   }
2318 
2319   /// Build and insert \p Dst = G_GET_FPENV
2320   MachineInstrBuilder buildGetFPEnv(const DstOp &Dst) {
2321     return buildInstr(TargetOpcode::G_GET_FPENV, {Dst}, {});
2322   }
2323 
2324   /// Build and insert G_SET_FPENV \p Src
2325   MachineInstrBuilder buildSetFPEnv(const SrcOp &Src) {
2326     return buildInstr(TargetOpcode::G_SET_FPENV, {}, {Src});
2327   }
2328 
2329   /// Build and insert G_RESET_FPENV
2330   MachineInstrBuilder buildResetFPEnv() {
2331     return buildInstr(TargetOpcode::G_RESET_FPENV, {}, {});
2332   }
2333 
2334   /// Build and insert \p Dst = G_GET_FPMODE
2335   MachineInstrBuilder buildGetFPMode(const DstOp &Dst) {
2336     return buildInstr(TargetOpcode::G_GET_FPMODE, {Dst}, {});
2337   }
2338 
2339   /// Build and insert G_SET_FPMODE \p Src
2340   MachineInstrBuilder buildSetFPMode(const SrcOp &Src) {
2341     return buildInstr(TargetOpcode::G_SET_FPMODE, {}, {Src});
2342   }
2343 
2344   /// Build and insert G_RESET_FPMODE
2345   MachineInstrBuilder buildResetFPMode() {
2346     return buildInstr(TargetOpcode::G_RESET_FPMODE, {}, {});
2347   }
2348 
2349   virtual MachineInstrBuilder
2350   buildInstr(unsigned Opc, ArrayRef<DstOp> DstOps, ArrayRef<SrcOp> SrcOps,
2351              std::optional<unsigned> Flags = std::nullopt);
2352 };
2353 
2354 } // End namespace llvm.
2355 #endif // LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H