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