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