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