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