1 //===-- llvm/CodeGen/GlobalISel/MachineIRBuilder.h - MIBuilder --*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 /// This file declares the MachineIRBuilder class.
10 /// This is a helper class to build MachineInstr.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
14 #define LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
16 #include "llvm/CodeGen/GlobalISel/CSEInfo.h"
17 #include "llvm/CodeGen/GlobalISel/Types.h"
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"
29 // Forward declarations.
30 class MachineFunction
;
32 class TargetInstrInfo
;
33 class GISelChangeObserver
;
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.
38 struct MachineIRBuilderState
{
39 /// MachineFunction under construction.
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.
48 /// \name Fields describing the insertion point.
50 MachineBasicBlock
*MBB
;
51 MachineBasicBlock::iterator II
;
54 GISelChangeObserver
*Observer
;
56 GISelCSEInfo
*CSEInfo
;
63 const TargetRegisterClass
*RC
;
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
) {}
74 void addDefToMIB(MachineRegisterInfo
&MRI
, MachineInstrBuilder
&MIB
) const {
80 MIB
.addDef(MRI
.createGenericVirtualRegister(LLTTy
));
83 MIB
.addDef(MRI
.createVirtualRegister(RC
));
88 LLT
getLLTTy(const MachineRegisterInfo
&MRI
) const {
95 return MRI
.getType(Reg
);
97 llvm_unreachable("Unrecognised DstOp::DstType enum");
100 Register
getReg() const {
101 assert(Ty
== DstType::Ty_Reg
&& "Not a register");
105 const TargetRegisterClass
*getRegClass() const {
110 llvm_unreachable("Not a RC Operand");
114 DstType
getDstOpKind() const { return Ty
; }
122 MachineInstrBuilder SrcMIB
;
124 CmpInst::Predicate Pred
;
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
137 SrcOp(unsigned) = delete;
139 SrcOp(uint64_t V
) : Imm(V
), Ty(SrcType::Ty_Imm
) {}
140 SrcOp(int64_t V
) : Imm(V
), Ty(SrcType::Ty_Imm
) {}
142 void addSrcToMIB(MachineInstrBuilder
&MIB
) const {
144 case SrcType::Ty_Predicate
:
145 MIB
.addPredicate(Pred
);
147 case SrcType::Ty_Reg
:
150 case SrcType::Ty_MIB
:
151 MIB
.addUse(SrcMIB
->getOperand(0).getReg());
153 case SrcType::Ty_Imm
:
159 LLT
getLLTTy(const MachineRegisterInfo
&MRI
) const {
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());
169 llvm_unreachable("Unrecognised SrcOp::SrcType enum");
172 Register
getReg() const {
174 case SrcType::Ty_Predicate
:
175 case SrcType::Ty_Imm
:
176 llvm_unreachable("Not a register operand");
177 case SrcType::Ty_Reg
:
179 case SrcType::Ty_MIB
:
180 return SrcMIB
->getOperand(0).getReg();
182 llvm_unreachable("Unrecognised SrcOp::SrcType enum");
185 CmpInst::Predicate
getPredicate() const {
187 case SrcType::Ty_Predicate
:
190 llvm_unreachable("Not a register operand");
194 int64_t getImm() const {
196 case SrcType::Ty_Imm
:
199 llvm_unreachable("Not an immediate");
203 SrcType
getSrcOpKind() const { return Ty
; }
210 Optional
<unsigned> Flags
;
213 explicit FlagsOp(unsigned F
) : Flags(F
) {}
214 FlagsOp() : Flags(None
) {}
215 Optional
<unsigned> getFlags() const { return Flags
; }
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.
221 class MachineIRBuilder
{
223 MachineIRBuilderState State
;
226 void validateTruncExt(const LLT
&Dst
, const LLT
&Src
, bool IsExtend
);
228 void validateBinaryOp(const LLT
&Res
, const LLT
&Op0
, const LLT
&Op1
);
229 void validateShiftOp(const LLT
&Res
, const LLT
&Op0
, const LLT
&Op1
);
231 void validateSelectOp(const LLT
&ResTy
, const LLT
&TstTy
, const LLT
&Op0Ty
,
233 void recordInsertion(MachineInstr
*MI
) const;
236 /// Some constructors for easy use.
237 MachineIRBuilder() = default;
238 MachineIRBuilder(MachineFunction
&MF
) { setMF(MF
); }
239 MachineIRBuilder(MachineInstr
&MI
) : MachineIRBuilder(*MI
.getMF()) {
243 virtual ~MachineIRBuilder() = default;
245 MachineIRBuilder(const MachineIRBuilderState
&BState
) : State(BState
) {}
247 const TargetInstrInfo
&getTII() {
248 assert(State
.TII
&& "TargetInstrInfo is not set");
252 /// Getter for the function we currently build.
253 MachineFunction
&getMF() {
254 assert(State
.MF
&& "MachineFunction is not set");
258 const MachineFunction
&getMF() const {
259 assert(State
.MF
&& "MachineFunction is not set");
263 const DataLayout
&getDataLayout() const {
264 return getMF().getFunction().getParent()->getDataLayout();
267 /// Getter for DebugLoc
268 const DebugLoc
&getDL() { return State
.DL
; }
271 MachineRegisterInfo
*getMRI() { return State
.MRI
; }
272 const MachineRegisterInfo
*getMRI() const { return State
.MRI
; }
274 /// Getter for the State
275 MachineIRBuilderState
&getState() { return State
; }
277 /// Getter for the basic block we currently build.
278 const MachineBasicBlock
&getMBB() const {
279 assert(State
.MBB
&& "MachineBasicBlock is not set");
283 MachineBasicBlock
&getMBB() {
284 return const_cast<MachineBasicBlock
&>(
285 const_cast<const MachineIRBuilder
*>(this)->getMBB());
288 GISelCSEInfo
*getCSEInfo() { return State
.CSEInfo
; }
289 const GISelCSEInfo
*getCSEInfo() const { return State
.CSEInfo
; }
291 /// Current insertion point for new instructions.
292 MachineBasicBlock::iterator
getInsertPt() { return State
.II
; }
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
);
300 void setCSEInfo(GISelCSEInfo
*Info
);
302 /// \name Setters for the insertion point.
304 /// Set the MachineFunction where to build instructions.
305 void setMF(MachineFunction
&MF
);
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
);
311 /// Set the insertion point to before MI.
312 /// \pre MI must be in getMF().
313 void setInstr(MachineInstr
&MI
);
316 void setChangeObserver(GISelChangeObserver
&Observer
);
317 void stopObservingChanges();
320 /// Set the debug location to \p DL for all the next build instructions.
321 void setDebugLoc(const DebugLoc
&DL
) { this->State
.DL
= DL
; }
323 /// Get the current instruction's debug location.
324 DebugLoc
getDebugLoc() { return State
.DL
; }
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.
330 /// \pre setBasicBlock or setMI must have been called.
332 /// \return a MachineInstrBuilder for the newly created instruction.
333 MachineInstrBuilder
buildInstr(unsigned Opcode
);
335 /// Build but don't insert <empty> = \p Opcode <empty>.
337 /// \pre setMF, setBasicBlock or setMI must have been called.
339 /// \return a MachineInstrBuilder for the newly created instruction.
340 MachineInstrBuilder
buildInstrNoInsert(unsigned Opcode
);
342 /// Insert an existing instruction at the insertion point.
343 MachineInstrBuilder
insertInstr(MachineInstrBuilder MIB
);
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
,
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
353 MachineInstrBuilder
buildIndirectDbgValue(Register Reg
,
354 const MDNode
*Variable
,
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
,
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
,
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
);
373 /// Build and insert \p Res = G_DYN_STACKALLOC \p Size, \p Align
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.
380 /// \return a MachineInstrBuilder for the newly created instruction.
381 MachineInstrBuilder
buildDynStackAlloc(const DstOp
&Res
, const SrcOp
&Size
,
384 /// Build and insert \p Res = G_FRAME_INDEX \p Idx
386 /// G_FRAME_INDEX materializes the address of an alloca value or other
387 /// stack-based object.
389 /// \pre setBasicBlock or setMI must have been called.
390 /// \pre \p Res must be a generic virtual register with pointer type.
392 /// \return a MachineInstrBuilder for the newly created instruction.
393 MachineInstrBuilder
buildFrameIndex(const DstOp
&Res
, int Idx
);
395 /// Build and insert \p Res = G_GLOBAL_VALUE \p GV
397 /// G_GLOBAL_VALUE materializes the address of the specified global
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.
404 /// \return a MachineInstrBuilder for the newly created instruction.
405 MachineInstrBuilder
buildGlobalValue(const DstOp
&Res
, const GlobalValue
*GV
);
407 /// Build and insert \p Res = G_GEP \p Op0, \p Op1
409 /// G_GEP adds \p Op1 bytes to the pointer specified by \p Op0,
410 /// storing the resulting pointer in \p Res.
412 /// \pre setBasicBlock or setMI must have been called.
413 /// \pre \p Res and \p Op0 must be generic virtual registers with pointer
415 /// \pre \p Op1 must be a generic virtual register with scalar type.
417 /// \return a MachineInstrBuilder for the newly created instruction.
418 MachineInstrBuilder
buildGEP(const DstOp
&Res
, const SrcOp
&Op0
,
421 /// Materialize and insert \p Res = G_GEP \p Op0, (G_CONSTANT \p Value)
423 /// G_GEP adds \p Value bytes to the pointer specified by \p Op0,
424 /// storing the resulting pointer in \p Res. If \p Value is zero then no
425 /// G_GEP or G_CONSTANT will be created and \pre Op0 will be assigned to
428 /// \pre setBasicBlock or setMI must have been called.
429 /// \pre \p Op0 must be a generic virtual register with pointer type.
430 /// \pre \p ValueTy must be a scalar type.
431 /// \pre \p Res must be 0. This is to detect confusion between
432 /// materializeGEP() and buildGEP().
433 /// \post \p Res will either be a new generic virtual register of the same
434 /// type as \p Op0 or \p Op0 itself.
436 /// \return a MachineInstrBuilder for the newly created instruction.
437 Optional
<MachineInstrBuilder
> materializeGEP(Register
&Res
, Register Op0
,
441 /// Build and insert \p Res = G_PTR_MASK \p Op0, \p NumBits
443 /// G_PTR_MASK clears the low bits of a pointer operand without destroying its
444 /// pointer properties. This has the effect of rounding the address *down* to
445 /// a specified alignment in bits.
447 /// \pre setBasicBlock or setMI must have been called.
448 /// \pre \p Res and \p Op0 must be generic virtual registers with pointer
450 /// \pre \p NumBits must be an integer representing the number of low bits to
451 /// be cleared in \p Op0.
453 /// \return a MachineInstrBuilder for the newly created instruction.
454 MachineInstrBuilder
buildPtrMask(const DstOp
&Res
, const SrcOp
&Op0
,
457 /// Build and insert \p Res, \p CarryOut = G_UADDO \p Op0, \p Op1
459 /// G_UADDO sets \p Res to \p Op0 + \p Op1 (truncated to the bit width) and
460 /// sets \p CarryOut to 1 if the result overflowed in unsigned arithmetic.
462 /// \pre setBasicBlock or setMI must have been called.
463 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers with the
464 /// same scalar type.
465 ////\pre \p CarryOut must be generic virtual register with scalar type
468 /// \return The newly created instruction.
469 MachineInstrBuilder
buildUAddo(const DstOp
&Res
, const DstOp
&CarryOut
,
470 const SrcOp
&Op0
, const SrcOp
&Op1
);
472 /// Build and insert \p Res, \p CarryOut = G_UADDE \p Op0,
473 /// \p Op1, \p CarryIn
475 /// G_UADDE sets \p Res to \p Op0 + \p Op1 + \p CarryIn (truncated to the bit
476 /// width) and sets \p CarryOut to 1 if the result overflowed in unsigned
479 /// \pre setBasicBlock or setMI must have been called.
480 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
481 /// with the same scalar type.
482 /// \pre \p CarryOut and \p CarryIn must be generic virtual
483 /// registers with the same scalar type (typically s1)
485 /// \return The newly created instruction.
486 MachineInstrBuilder
buildUAdde(const DstOp
&Res
, const DstOp
&CarryOut
,
487 const SrcOp
&Op0
, const SrcOp
&Op1
,
488 const SrcOp
&CarryIn
);
490 /// Build and insert \p Res = G_ANYEXT \p Op0
492 /// G_ANYEXT produces a register of the specified width, with bits 0 to
493 /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are unspecified
494 /// (i.e. this is neither zero nor sign-extension). For a vector register,
495 /// each element is extended individually.
497 /// \pre setBasicBlock or setMI must have been called.
498 /// \pre \p Res must be a generic virtual register with scalar or vector type.
499 /// \pre \p Op must be a generic virtual register with scalar or vector type.
500 /// \pre \p Op must be smaller than \p Res
502 /// \return The newly created instruction.
504 MachineInstrBuilder
buildAnyExt(const DstOp
&Res
, const SrcOp
&Op
);
506 /// Build and insert \p Res = G_SEXT \p Op
508 /// G_SEXT produces a register of the specified width, with bits 0 to
509 /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are duplicated from the
510 /// high bit of \p Op (i.e. 2s-complement sign extended).
512 /// \pre setBasicBlock or setMI must have been called.
513 /// \pre \p Res must be a generic virtual register with scalar or vector type.
514 /// \pre \p Op must be a generic virtual register with scalar or vector type.
515 /// \pre \p Op must be smaller than \p Res
517 /// \return The newly created instruction.
518 MachineInstrBuilder
buildSExt(const DstOp
&Res
, const SrcOp
&Op
);
520 /// Build and insert a G_PTRTOINT instruction.
521 MachineInstrBuilder
buildPtrToInt(const DstOp
&Dst
, const SrcOp
&Src
) {
522 return buildInstr(TargetOpcode::G_PTRTOINT
, {Dst
}, {Src
});
525 /// Build and insert a G_INTTOPTR instruction.
526 MachineInstrBuilder
buildIntToPtr(const DstOp
&Dst
, const SrcOp
&Src
) {
527 return buildInstr(TargetOpcode::G_INTTOPTR
, {Dst
}, {Src
});
530 /// Build and insert \p Dst = G_BITCAST \p Src
531 MachineInstrBuilder
buildBitcast(const DstOp
&Dst
, const SrcOp
&Src
) {
532 return buildInstr(TargetOpcode::G_BITCAST
, {Dst
}, {Src
});
535 /// \return The opcode of the extension the target wants to use for boolean
537 unsigned getBoolExtOp(bool IsVec
, bool IsFP
) const;
539 // Build and insert \p Res = G_ANYEXT \p Op, \p Res = G_SEXT \p Op, or \p Res
540 // = G_ZEXT \p Op depending on how the target wants to extend boolean values.
541 MachineInstrBuilder
buildBoolExt(const DstOp
&Res
, const SrcOp
&Op
,
544 /// Build and insert \p Res = G_ZEXT \p Op
546 /// G_ZEXT produces a register of the specified width, with bits 0 to
547 /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are 0. For a vector
548 /// register, each element is extended individually.
550 /// \pre setBasicBlock or setMI must have been called.
551 /// \pre \p Res must be a generic virtual register with scalar or vector type.
552 /// \pre \p Op must be a generic virtual register with scalar or vector type.
553 /// \pre \p Op must be smaller than \p Res
555 /// \return The newly created instruction.
556 MachineInstrBuilder
buildZExt(const DstOp
&Res
, const SrcOp
&Op
);
558 /// Build and insert \p Res = G_SEXT \p Op, \p Res = G_TRUNC \p Op, or
559 /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
561 /// \pre setBasicBlock or setMI must have been called.
562 /// \pre \p Res must be a generic virtual register with scalar or vector type.
563 /// \pre \p Op must be a generic virtual register with scalar or vector type.
565 /// \return The newly created instruction.
566 MachineInstrBuilder
buildSExtOrTrunc(const DstOp
&Res
, const SrcOp
&Op
);
568 /// Build and insert \p Res = G_ZEXT \p Op, \p Res = G_TRUNC \p Op, or
569 /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
571 /// \pre setBasicBlock or setMI must have been called.
572 /// \pre \p Res must be a generic virtual register with scalar or vector type.
573 /// \pre \p Op must be a generic virtual register with scalar or vector type.
575 /// \return The newly created instruction.
576 MachineInstrBuilder
buildZExtOrTrunc(const DstOp
&Res
, const SrcOp
&Op
);
578 // Build and insert \p Res = G_ANYEXT \p Op, \p Res = G_TRUNC \p Op, or
579 /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
581 /// \pre setBasicBlock or setMI must have been called.
582 /// \pre \p Res must be a generic virtual register with scalar or vector type.
583 /// \pre \p Op must be a generic virtual register with scalar or vector type.
585 /// \return The newly created instruction.
586 MachineInstrBuilder
buildAnyExtOrTrunc(const DstOp
&Res
, const SrcOp
&Op
);
588 /// Build and insert \p Res = \p ExtOpc, \p Res = G_TRUNC \p
589 /// Op, or \p Res = COPY \p Op depending on the differing sizes of \p Res and
592 /// \pre setBasicBlock or setMI must have been called.
593 /// \pre \p Res must be a generic virtual register with scalar or vector type.
594 /// \pre \p Op must be a generic virtual register with scalar or vector type.
596 /// \return The newly created instruction.
597 MachineInstrBuilder
buildExtOrTrunc(unsigned ExtOpc
, const DstOp
&Res
,
600 /// Build and insert an appropriate cast between two registers of equal size.
601 MachineInstrBuilder
buildCast(const DstOp
&Dst
, const SrcOp
&Src
);
603 /// Build and insert G_BR \p Dest
605 /// G_BR is an unconditional branch to \p Dest.
607 /// \pre setBasicBlock or setMI must have been called.
609 /// \return a MachineInstrBuilder for the newly created instruction.
610 MachineInstrBuilder
buildBr(MachineBasicBlock
&Dest
);
612 /// Build and insert G_BRCOND \p Tst, \p Dest
614 /// G_BRCOND is a conditional branch to \p Dest.
616 /// \pre setBasicBlock or setMI must have been called.
617 /// \pre \p Tst must be a generic virtual register with scalar
618 /// type. At the beginning of legalization, this will be a single
619 /// bit (s1). Targets with interesting flags registers may change
620 /// this. For a wider type, whether the branch is taken must only
621 /// depend on bit 0 (for now).
623 /// \return The newly created instruction.
624 MachineInstrBuilder
buildBrCond(Register Tst
, MachineBasicBlock
&Dest
);
626 /// Build and insert G_BRINDIRECT \p Tgt
628 /// G_BRINDIRECT is an indirect branch to \p Tgt.
630 /// \pre setBasicBlock or setMI must have been called.
631 /// \pre \p Tgt must be a generic virtual register with pointer type.
633 /// \return a MachineInstrBuilder for the newly created instruction.
634 MachineInstrBuilder
buildBrIndirect(Register Tgt
);
636 /// Build and insert G_BRJT \p TablePtr, \p JTI, \p IndexReg
638 /// G_BRJT is a jump table branch using a table base pointer \p TablePtr,
639 /// jump table index \p JTI and index \p IndexReg
641 /// \pre setBasicBlock or setMI must have been called.
642 /// \pre \p TablePtr must be a generic virtual register with pointer type.
643 /// \pre \p JTI must be be a jump table index.
644 /// \pre \p IndexReg must be a generic virtual register with pointer type.
646 /// \return a MachineInstrBuilder for the newly created instruction.
647 MachineInstrBuilder
buildBrJT(Register TablePtr
, unsigned JTI
,
650 /// Build and insert \p Res = G_CONSTANT \p Val
652 /// G_CONSTANT is an integer constant with the specified size and value. \p
653 /// Val will be extended or truncated to the size of \p Reg.
655 /// \pre setBasicBlock or setMI must have been called.
656 /// \pre \p Res must be a generic virtual register with scalar or pointer
659 /// \return The newly created instruction.
660 virtual MachineInstrBuilder
buildConstant(const DstOp
&Res
,
661 const ConstantInt
&Val
);
663 /// Build and insert \p Res = G_CONSTANT \p Val
665 /// G_CONSTANT is an integer constant with the specified size and value.
667 /// \pre setBasicBlock or setMI must have been called.
668 /// \pre \p Res must be a generic virtual register with scalar type.
670 /// \return The newly created instruction.
671 MachineInstrBuilder
buildConstant(const DstOp
&Res
, int64_t Val
);
672 MachineInstrBuilder
buildConstant(const DstOp
&Res
, const APInt
&Val
);
674 /// Build and insert \p Res = G_FCONSTANT \p Val
676 /// G_FCONSTANT is a floating-point constant with the specified size and
679 /// \pre setBasicBlock or setMI must have been called.
680 /// \pre \p Res must be a generic virtual register with scalar type.
682 /// \return The newly created instruction.
683 virtual MachineInstrBuilder
buildFConstant(const DstOp
&Res
,
684 const ConstantFP
&Val
);
686 MachineInstrBuilder
buildFConstant(const DstOp
&Res
, double Val
);
687 MachineInstrBuilder
buildFConstant(const DstOp
&Res
, const APFloat
&Val
);
689 /// Build and insert \p Res = COPY Op
691 /// Register-to-register COPY sets \p Res to \p Op.
693 /// \pre setBasicBlock or setMI must have been called.
695 /// \return a MachineInstrBuilder for the newly created instruction.
696 MachineInstrBuilder
buildCopy(const DstOp
&Res
, const SrcOp
&Op
);
698 /// Build and insert `Res = G_LOAD Addr, MMO`.
700 /// Loads the value stored at \p Addr. Puts the result in \p Res.
702 /// \pre setBasicBlock or setMI must have been called.
703 /// \pre \p Res must be a generic virtual register.
704 /// \pre \p Addr must be a generic virtual register with pointer type.
706 /// \return a MachineInstrBuilder for the newly created instruction.
707 MachineInstrBuilder
buildLoad(const DstOp
&Res
, const SrcOp
&Addr
,
708 MachineMemOperand
&MMO
);
710 /// Build and insert `Res = <opcode> Addr, MMO`.
712 /// Loads the value stored at \p Addr. Puts the result in \p Res.
714 /// \pre setBasicBlock or setMI must have been called.
715 /// \pre \p Res must be a generic virtual register.
716 /// \pre \p Addr must be a generic virtual register with pointer type.
718 /// \return a MachineInstrBuilder for the newly created instruction.
719 MachineInstrBuilder
buildLoadInstr(unsigned Opcode
, const DstOp
&Res
,
720 const SrcOp
&Addr
, MachineMemOperand
&MMO
);
722 /// Build and insert `G_STORE Val, Addr, MMO`.
724 /// Stores the value \p Val to \p Addr.
726 /// \pre setBasicBlock or setMI must have been called.
727 /// \pre \p Val must be a generic virtual register.
728 /// \pre \p Addr must be a generic virtual register with pointer type.
730 /// \return a MachineInstrBuilder for the newly created instruction.
731 MachineInstrBuilder
buildStore(const SrcOp
&Val
, const SrcOp
&Addr
,
732 MachineMemOperand
&MMO
);
734 /// Build and insert `Res0, ... = G_EXTRACT Src, Idx0`.
736 /// \pre setBasicBlock or setMI must have been called.
737 /// \pre \p Res and \p Src must be generic virtual registers.
739 /// \return a MachineInstrBuilder for the newly created instruction.
740 MachineInstrBuilder
buildExtract(const DstOp
&Res
, const SrcOp
&Src
, uint64_t Index
);
742 /// Build and insert \p Res = IMPLICIT_DEF.
743 MachineInstrBuilder
buildUndef(const DstOp
&Res
);
745 /// Build and insert instructions to put \p Ops together at the specified p
746 /// Indices to form a larger register.
748 /// If the types of the input registers are uniform and cover the entirity of
749 /// \p Res then a G_MERGE_VALUES will be produced. Otherwise an IMPLICIT_DEF
750 /// followed by a sequence of G_INSERT instructions.
752 /// \pre setBasicBlock or setMI must have been called.
753 /// \pre The final element of the sequence must not extend past the end of the
754 /// destination register.
755 /// \pre The bits defined by each Op (derived from index and scalar size) must
757 /// \pre \p Indices must be in ascending order of bit position.
758 void buildSequence(Register Res
, ArrayRef
<Register
> Ops
,
759 ArrayRef
<uint64_t> Indices
);
761 /// Build and insert \p Res = G_MERGE_VALUES \p Op0, ...
763 /// G_MERGE_VALUES combines the input elements contiguously into a larger
766 /// \pre setBasicBlock or setMI must have been called.
767 /// \pre The entire register \p Res (and no more) must be covered by the input
769 /// \pre The type of all \p Ops registers must be identical.
771 /// \return a MachineInstrBuilder for the newly created instruction.
772 MachineInstrBuilder
buildMerge(const DstOp
&Res
, ArrayRef
<Register
> Ops
);
774 /// Build and insert \p Res0, ... = G_UNMERGE_VALUES \p Op
776 /// G_UNMERGE_VALUES splits contiguous bits of the input into multiple
778 /// \pre setBasicBlock or setMI must have been called.
779 /// \pre The entire register \p Res (and no more) must be covered by the input
781 /// \pre The type of all \p Res registers must be identical.
783 /// \return a MachineInstrBuilder for the newly created instruction.
784 MachineInstrBuilder
buildUnmerge(ArrayRef
<LLT
> Res
, const SrcOp
&Op
);
785 MachineInstrBuilder
buildUnmerge(ArrayRef
<Register
> Res
, const SrcOp
&Op
);
787 /// Build and insert an unmerge of \p Res sized pieces to cover \p Op
788 MachineInstrBuilder
buildUnmerge(LLT Res
, const SrcOp
&Op
);
790 /// Build and insert \p Res = G_BUILD_VECTOR \p Op0, ...
792 /// G_BUILD_VECTOR creates a vector value from multiple scalar registers.
793 /// \pre setBasicBlock or setMI must have been called.
794 /// \pre The entire register \p Res (and no more) must be covered by the
795 /// input scalar registers.
796 /// \pre The type of all \p Ops registers must be identical.
798 /// \return a MachineInstrBuilder for the newly created instruction.
799 MachineInstrBuilder
buildBuildVector(const DstOp
&Res
,
800 ArrayRef
<Register
> Ops
);
802 /// Build and insert \p Res = G_BUILD_VECTOR with \p Src replicated to fill
803 /// the number of elements
804 MachineInstrBuilder
buildSplatVector(const DstOp
&Res
,
807 /// Build and insert \p Res = G_BUILD_VECTOR_TRUNC \p Op0, ...
809 /// G_BUILD_VECTOR_TRUNC creates a vector value from multiple scalar registers
810 /// which have types larger than the destination vector element type, and
811 /// truncates the values to fit.
813 /// If the operands given are already the same size as the vector elt type,
814 /// then this method will instead create a G_BUILD_VECTOR instruction.
816 /// \pre setBasicBlock or setMI must have been called.
817 /// \pre The type of all \p Ops registers must be identical.
819 /// \return a MachineInstrBuilder for the newly created instruction.
820 MachineInstrBuilder
buildBuildVectorTrunc(const DstOp
&Res
,
821 ArrayRef
<Register
> Ops
);
823 /// Build and insert \p Res = G_CONCAT_VECTORS \p Op0, ...
825 /// G_CONCAT_VECTORS creates a vector from the concatenation of 2 or more
828 /// \pre setBasicBlock or setMI must have been called.
829 /// \pre The entire register \p Res (and no more) must be covered by the input
831 /// \pre The type of all source operands must be identical.
833 /// \return a MachineInstrBuilder for the newly created instruction.
834 MachineInstrBuilder
buildConcatVectors(const DstOp
&Res
,
835 ArrayRef
<Register
> Ops
);
837 MachineInstrBuilder
buildInsert(Register Res
, Register Src
,
838 Register Op
, unsigned Index
);
840 /// Build and insert either a G_INTRINSIC (if \p HasSideEffects is false) or
841 /// G_INTRINSIC_W_SIDE_EFFECTS instruction. Its first operand will be the
842 /// result register definition unless \p Reg is NoReg (== 0). The second
843 /// operand will be the intrinsic's ID.
845 /// Callers are expected to add the required definitions and uses afterwards.
847 /// \pre setBasicBlock or setMI must have been called.
849 /// \return a MachineInstrBuilder for the newly created instruction.
850 MachineInstrBuilder
buildIntrinsic(Intrinsic::ID ID
, ArrayRef
<Register
> Res
,
851 bool HasSideEffects
);
852 MachineInstrBuilder
buildIntrinsic(Intrinsic::ID ID
, ArrayRef
<DstOp
> Res
,
853 bool HasSideEffects
);
855 /// Build and insert \p Res = G_FPTRUNC \p Op
857 /// G_FPTRUNC converts a floating-point value into one with a smaller type.
859 /// \pre setBasicBlock or setMI must have been called.
860 /// \pre \p Res must be a generic virtual register with scalar or vector type.
861 /// \pre \p Op must be a generic virtual register with scalar or vector type.
862 /// \pre \p Res must be smaller than \p Op
864 /// \return The newly created instruction.
865 MachineInstrBuilder
buildFPTrunc(const DstOp
&Res
, const SrcOp
&Op
);
867 /// Build and insert \p Res = G_TRUNC \p Op
869 /// G_TRUNC extracts the low bits of a type. For a vector type each element is
870 /// truncated independently before being packed into the destination.
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
877 /// \return The newly created instruction.
878 MachineInstrBuilder
buildTrunc(const DstOp
&Res
, const SrcOp
&Op
);
880 /// Build and insert a \p Res = G_ICMP \p Pred, \p Op0, \p Op1
882 /// \pre setBasicBlock or setMI must have been called.
884 /// \pre \p Res must be a generic virtual register with scalar or
885 /// vector type. Typically this starts as s1 or <N x s1>.
886 /// \pre \p Op0 and Op1 must be generic virtual registers with the
887 /// same number of elements as \p Res. If \p Res is a scalar,
888 /// \p Op0 must be either a scalar or pointer.
889 /// \pre \p Pred must be an integer predicate.
891 /// \return a MachineInstrBuilder for the newly created instruction.
892 MachineInstrBuilder
buildICmp(CmpInst::Predicate Pred
, const DstOp
&Res
,
893 const SrcOp
&Op0
, const SrcOp
&Op1
);
895 /// Build and insert a \p Res = G_FCMP \p Pred\p Op0, \p Op1
897 /// \pre setBasicBlock or setMI must have been called.
899 /// \pre \p Res must be a generic virtual register with scalar or
900 /// vector type. Typically this starts as s1 or <N x s1>.
901 /// \pre \p Op0 and Op1 must be generic virtual registers with the
902 /// same number of elements as \p Res (or scalar, if \p Res is
904 /// \pre \p Pred must be a floating-point predicate.
906 /// \return a MachineInstrBuilder for the newly created instruction.
907 MachineInstrBuilder
buildFCmp(CmpInst::Predicate Pred
, const DstOp
&Res
,
908 const SrcOp
&Op0
, const SrcOp
&Op1
,
909 Optional
<unsigned> Flags
= None
);
911 /// Build and insert a \p Res = G_SELECT \p Tst, \p Op0, \p Op1
913 /// \pre setBasicBlock or setMI must have been called.
914 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
915 /// with the same type.
916 /// \pre \p Tst must be a generic virtual register with scalar, pointer or
917 /// vector type. If vector then it must have the same number of
918 /// elements as the other parameters.
920 /// \return a MachineInstrBuilder for the newly created instruction.
921 MachineInstrBuilder
buildSelect(const DstOp
&Res
, const SrcOp
&Tst
,
922 const SrcOp
&Op0
, const SrcOp
&Op1
,
923 Optional
<unsigned> Flags
= None
);
925 /// Build and insert \p Res = G_INSERT_VECTOR_ELT \p Val,
928 /// \pre setBasicBlock or setMI must have been called.
929 /// \pre \p Res and \p Val must be a generic virtual register
930 // with the same vector type.
931 /// \pre \p Elt and \p Idx must be a generic virtual register
932 /// with scalar type.
934 /// \return The newly created instruction.
935 MachineInstrBuilder
buildInsertVectorElement(const DstOp
&Res
,
940 /// Build and insert \p Res = G_EXTRACT_VECTOR_ELT \p Val, \p Idx
942 /// \pre setBasicBlock or setMI must have been called.
943 /// \pre \p Res must be a generic virtual register with scalar type.
944 /// \pre \p Val must be a generic virtual register with vector type.
945 /// \pre \p Idx must be a generic virtual register with scalar type.
947 /// \return The newly created instruction.
948 MachineInstrBuilder
buildExtractVectorElement(const DstOp
&Res
,
952 /// Build and insert `OldValRes<def>, SuccessRes<def> =
953 /// G_ATOMIC_CMPXCHG_WITH_SUCCESS Addr, CmpVal, NewVal, MMO`.
955 /// Atomically replace the value at \p Addr with \p NewVal if it is currently
956 /// \p CmpVal otherwise leaves it unchanged. Puts the original value from \p
957 /// Addr in \p Res, along with an s1 indicating whether it was replaced.
959 /// \pre setBasicBlock or setMI must have been called.
960 /// \pre \p OldValRes must be a generic virtual register of scalar type.
961 /// \pre \p SuccessRes must be a generic virtual register of scalar type. It
962 /// will be assigned 0 on failure and 1 on success.
963 /// \pre \p Addr must be a generic virtual register with pointer type.
964 /// \pre \p OldValRes, \p CmpVal, and \p NewVal must be generic virtual
965 /// registers of the same type.
967 /// \return a MachineInstrBuilder for the newly created instruction.
969 buildAtomicCmpXchgWithSuccess(Register OldValRes
, Register SuccessRes
,
970 Register Addr
, Register CmpVal
, Register NewVal
,
971 MachineMemOperand
&MMO
);
973 /// Build and insert `OldValRes<def> = G_ATOMIC_CMPXCHG Addr, CmpVal, NewVal,
976 /// Atomically replace the value at \p Addr with \p NewVal if it is currently
977 /// \p CmpVal otherwise leaves it unchanged. Puts the original value from \p
980 /// \pre setBasicBlock or setMI must have been called.
981 /// \pre \p OldValRes must be a generic virtual register of scalar type.
982 /// \pre \p Addr must be a generic virtual register with pointer type.
983 /// \pre \p OldValRes, \p CmpVal, and \p NewVal must be generic virtual
984 /// registers of the same type.
986 /// \return a MachineInstrBuilder for the newly created instruction.
987 MachineInstrBuilder
buildAtomicCmpXchg(Register OldValRes
, Register Addr
,
988 Register CmpVal
, Register NewVal
,
989 MachineMemOperand
&MMO
);
991 /// Build and insert `OldValRes<def> = G_ATOMICRMW_<Opcode> Addr, Val, MMO`.
993 /// Atomically read-modify-update the value at \p Addr with \p Val. Puts the
994 /// original value from \p Addr in \p OldValRes. The modification is
995 /// determined by the opcode.
997 /// \pre setBasicBlock or setMI must have been called.
998 /// \pre \p OldValRes must be a generic virtual register.
999 /// \pre \p Addr must be a generic virtual register with pointer type.
1000 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1003 /// \return a MachineInstrBuilder for the newly created instruction.
1004 MachineInstrBuilder
buildAtomicRMW(unsigned Opcode
, const DstOp
&OldValRes
,
1005 const SrcOp
&Addr
, const SrcOp
&Val
,
1006 MachineMemOperand
&MMO
);
1008 /// Build and insert `OldValRes<def> = G_ATOMICRMW_XCHG Addr, Val, MMO`.
1010 /// Atomically replace the value at \p Addr with \p Val. Puts the original
1011 /// value from \p Addr in \p OldValRes.
1013 /// \pre setBasicBlock or setMI must have been called.
1014 /// \pre \p OldValRes must be a generic virtual register.
1015 /// \pre \p Addr must be a generic virtual register with pointer type.
1016 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1019 /// \return a MachineInstrBuilder for the newly created instruction.
1020 MachineInstrBuilder
buildAtomicRMWXchg(Register OldValRes
, Register Addr
,
1021 Register Val
, MachineMemOperand
&MMO
);
1023 /// Build and insert `OldValRes<def> = G_ATOMICRMW_ADD Addr, Val, MMO`.
1025 /// Atomically replace the value at \p Addr with the addition of \p Val and
1026 /// the original value. Puts the original value from \p Addr in \p OldValRes.
1028 /// \pre setBasicBlock or setMI must have been called.
1029 /// \pre \p OldValRes must be a generic virtual register.
1030 /// \pre \p Addr must be a generic virtual register with pointer type.
1031 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1034 /// \return a MachineInstrBuilder for the newly created instruction.
1035 MachineInstrBuilder
buildAtomicRMWAdd(Register OldValRes
, Register Addr
,
1036 Register Val
, MachineMemOperand
&MMO
);
1038 /// Build and insert `OldValRes<def> = G_ATOMICRMW_SUB Addr, Val, MMO`.
1040 /// Atomically replace the value at \p Addr with the subtraction of \p Val and
1041 /// the original value. Puts the original value from \p Addr in \p OldValRes.
1043 /// \pre setBasicBlock or setMI must have been called.
1044 /// \pre \p OldValRes must be a generic virtual register.
1045 /// \pre \p Addr must be a generic virtual register with pointer type.
1046 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1049 /// \return a MachineInstrBuilder for the newly created instruction.
1050 MachineInstrBuilder
buildAtomicRMWSub(Register OldValRes
, Register Addr
,
1051 Register Val
, MachineMemOperand
&MMO
);
1053 /// Build and insert `OldValRes<def> = G_ATOMICRMW_AND Addr, Val, MMO`.
1055 /// Atomically replace the value at \p Addr with the bitwise and of \p Val and
1056 /// the original value. Puts the original value from \p Addr in \p OldValRes.
1058 /// \pre setBasicBlock or setMI must have been called.
1059 /// \pre \p OldValRes must be a generic virtual register.
1060 /// \pre \p Addr must be a generic virtual register with pointer type.
1061 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1064 /// \return a MachineInstrBuilder for the newly created instruction.
1065 MachineInstrBuilder
buildAtomicRMWAnd(Register OldValRes
, Register Addr
,
1066 Register Val
, MachineMemOperand
&MMO
);
1068 /// Build and insert `OldValRes<def> = G_ATOMICRMW_NAND Addr, Val, MMO`.
1070 /// Atomically replace the value at \p Addr with the bitwise nand of \p Val
1071 /// and the original value. Puts the original value from \p Addr in \p
1074 /// \pre setBasicBlock or setMI must have been called.
1075 /// \pre \p OldValRes must be a generic virtual register.
1076 /// \pre \p Addr must be a generic virtual register with pointer type.
1077 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1080 /// \return a MachineInstrBuilder for the newly created instruction.
1081 MachineInstrBuilder
buildAtomicRMWNand(Register OldValRes
, Register Addr
,
1082 Register Val
, MachineMemOperand
&MMO
);
1084 /// Build and insert `OldValRes<def> = G_ATOMICRMW_OR Addr, Val, MMO`.
1086 /// Atomically replace the value at \p Addr with the bitwise or of \p Val and
1087 /// the original value. Puts the original value from \p Addr in \p OldValRes.
1089 /// \pre setBasicBlock or setMI must have been called.
1090 /// \pre \p OldValRes must be a generic virtual register.
1091 /// \pre \p Addr must be a generic virtual register with pointer type.
1092 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1095 /// \return a MachineInstrBuilder for the newly created instruction.
1096 MachineInstrBuilder
buildAtomicRMWOr(Register OldValRes
, Register Addr
,
1097 Register Val
, MachineMemOperand
&MMO
);
1099 /// Build and insert `OldValRes<def> = G_ATOMICRMW_XOR Addr, Val, MMO`.
1101 /// Atomically replace the value at \p Addr with the bitwise xor of \p Val and
1102 /// the original value. Puts the original value from \p Addr in \p OldValRes.
1104 /// \pre setBasicBlock or setMI must have been called.
1105 /// \pre \p OldValRes must be a generic virtual register.
1106 /// \pre \p Addr must be a generic virtual register with pointer type.
1107 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1110 /// \return a MachineInstrBuilder for the newly created instruction.
1111 MachineInstrBuilder
buildAtomicRMWXor(Register OldValRes
, Register Addr
,
1112 Register Val
, MachineMemOperand
&MMO
);
1114 /// Build and insert `OldValRes<def> = G_ATOMICRMW_MAX Addr, Val, MMO`.
1116 /// Atomically replace the value at \p Addr with the signed maximum of \p
1117 /// Val and the original value. Puts the original value from \p Addr in \p
1120 /// \pre setBasicBlock or setMI must have been called.
1121 /// \pre \p OldValRes must be a generic virtual register.
1122 /// \pre \p Addr must be a generic virtual register with pointer type.
1123 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1126 /// \return a MachineInstrBuilder for the newly created instruction.
1127 MachineInstrBuilder
buildAtomicRMWMax(Register OldValRes
, Register Addr
,
1128 Register Val
, MachineMemOperand
&MMO
);
1130 /// Build and insert `OldValRes<def> = G_ATOMICRMW_MIN Addr, Val, MMO`.
1132 /// Atomically replace the value at \p Addr with the signed minimum of \p
1133 /// Val and the original value. Puts the original value from \p Addr in \p
1136 /// \pre setBasicBlock or setMI must have been called.
1137 /// \pre \p OldValRes must be a generic virtual register.
1138 /// \pre \p Addr must be a generic virtual register with pointer type.
1139 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1142 /// \return a MachineInstrBuilder for the newly created instruction.
1143 MachineInstrBuilder
buildAtomicRMWMin(Register OldValRes
, Register Addr
,
1144 Register Val
, MachineMemOperand
&MMO
);
1146 /// Build and insert `OldValRes<def> = G_ATOMICRMW_UMAX Addr, Val, MMO`.
1148 /// Atomically replace the value at \p Addr with the unsigned maximum of \p
1149 /// Val and the original value. Puts the original value from \p Addr in \p
1152 /// \pre setBasicBlock or setMI must have been called.
1153 /// \pre \p OldValRes must be a generic virtual register.
1154 /// \pre \p Addr must be a generic virtual register with pointer type.
1155 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1158 /// \return a MachineInstrBuilder for the newly created instruction.
1159 MachineInstrBuilder
buildAtomicRMWUmax(Register OldValRes
, Register Addr
,
1160 Register Val
, MachineMemOperand
&MMO
);
1162 /// Build and insert `OldValRes<def> = G_ATOMICRMW_UMIN Addr, Val, MMO`.
1164 /// Atomically replace the value at \p Addr with the unsigned minimum of \p
1165 /// Val and the original value. Puts the original value from \p Addr in \p
1168 /// \pre setBasicBlock or setMI must have been called.
1169 /// \pre \p OldValRes must be a generic virtual register.
1170 /// \pre \p Addr must be a generic virtual register with pointer type.
1171 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1174 /// \return a MachineInstrBuilder for the newly created instruction.
1175 MachineInstrBuilder
buildAtomicRMWUmin(Register OldValRes
, Register Addr
,
1176 Register Val
, MachineMemOperand
&MMO
);
1178 /// Build and insert `OldValRes<def> = G_ATOMICRMW_FADD Addr, Val, MMO`.
1179 MachineInstrBuilder
buildAtomicRMWFAdd(
1180 const DstOp
&OldValRes
, const SrcOp
&Addr
, const SrcOp
&Val
,
1181 MachineMemOperand
&MMO
);
1183 /// Build and insert `OldValRes<def> = G_ATOMICRMW_FSUB Addr, Val, MMO`.
1184 MachineInstrBuilder
buildAtomicRMWFSub(
1185 const DstOp
&OldValRes
, const SrcOp
&Addr
, const SrcOp
&Val
,
1186 MachineMemOperand
&MMO
);
1188 /// Build and insert `G_FENCE Ordering, Scope`.
1189 MachineInstrBuilder
buildFence(unsigned Ordering
, unsigned Scope
);
1191 /// Build and insert \p Res = G_BLOCK_ADDR \p BA
1193 /// G_BLOCK_ADDR computes the address of a basic block.
1195 /// \pre setBasicBlock or setMI must have been called.
1196 /// \pre \p Res must be a generic virtual register of a pointer type.
1198 /// \return The newly created instruction.
1199 MachineInstrBuilder
buildBlockAddress(Register Res
, const BlockAddress
*BA
);
1201 /// Build and insert \p Res = G_ADD \p Op0, \p Op1
1203 /// G_ADD sets \p Res to the sum of integer parameters \p Op0 and \p Op1,
1204 /// truncated to their width.
1206 /// \pre setBasicBlock or setMI must have been called.
1207 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1208 /// with the same (scalar or vector) type).
1210 /// \return a MachineInstrBuilder for the newly created instruction.
1212 MachineInstrBuilder
buildAdd(const DstOp
&Dst
, const SrcOp
&Src0
,
1214 Optional
<unsigned> Flags
= None
) {
1215 return buildInstr(TargetOpcode::G_ADD
, {Dst
}, {Src0
, Src1
}, Flags
);
1218 /// Build and insert \p Res = G_SUB \p Op0, \p Op1
1220 /// G_SUB sets \p Res to the sum of integer parameters \p Op0 and \p Op1,
1221 /// truncated to their width.
1223 /// \pre setBasicBlock or setMI must have been called.
1224 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1225 /// with the same (scalar or vector) type).
1227 /// \return a MachineInstrBuilder for the newly created instruction.
1229 MachineInstrBuilder
buildSub(const DstOp
&Dst
, const SrcOp
&Src0
,
1231 Optional
<unsigned> Flags
= None
) {
1232 return buildInstr(TargetOpcode::G_SUB
, {Dst
}, {Src0
, Src1
}, Flags
);
1235 /// Build and insert \p Res = G_MUL \p Op0, \p Op1
1237 /// G_MUL sets \p Res to the sum of integer parameters \p Op0 and \p Op1,
1238 /// truncated to their width.
1240 /// \pre setBasicBlock or setMI must have been called.
1241 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1242 /// with the same (scalar or vector) type).
1244 /// \return a MachineInstrBuilder for the newly created instruction.
1245 MachineInstrBuilder
buildMul(const DstOp
&Dst
, const SrcOp
&Src0
,
1247 Optional
<unsigned> Flags
= None
) {
1248 return buildInstr(TargetOpcode::G_MUL
, {Dst
}, {Src0
, Src1
}, Flags
);
1251 MachineInstrBuilder
buildUMulH(const DstOp
&Dst
, const SrcOp
&Src0
,
1253 Optional
<unsigned> Flags
= None
) {
1254 return buildInstr(TargetOpcode::G_UMULH
, {Dst
}, {Src0
, Src1
}, Flags
);
1257 MachineInstrBuilder
buildSMulH(const DstOp
&Dst
, const SrcOp
&Src0
,
1259 Optional
<unsigned> Flags
= None
) {
1260 return buildInstr(TargetOpcode::G_SMULH
, {Dst
}, {Src0
, Src1
}, Flags
);
1263 MachineInstrBuilder
buildFMul(const DstOp
&Dst
, const SrcOp
&Src0
,
1265 Optional
<unsigned> Flags
= None
) {
1266 return buildInstr(TargetOpcode::G_FMUL
, {Dst
}, {Src0
, Src1
}, Flags
);
1269 MachineInstrBuilder
buildShl(const DstOp
&Dst
, const SrcOp
&Src0
,
1271 Optional
<unsigned> Flags
= None
) {
1272 return buildInstr(TargetOpcode::G_SHL
, {Dst
}, {Src0
, Src1
}, Flags
);
1275 MachineInstrBuilder
buildLShr(const DstOp
&Dst
, const SrcOp
&Src0
,
1277 Optional
<unsigned> Flags
= None
) {
1278 return buildInstr(TargetOpcode::G_LSHR
, {Dst
}, {Src0
, Src1
}, Flags
);
1281 MachineInstrBuilder
buildAShr(const DstOp
&Dst
, const SrcOp
&Src0
,
1283 Optional
<unsigned> Flags
= None
) {
1284 return buildInstr(TargetOpcode::G_ASHR
, {Dst
}, {Src0
, Src1
}, Flags
);
1287 /// Build and insert \p Res = G_AND \p Op0, \p Op1
1289 /// G_AND sets \p Res to the bitwise and of integer parameters \p Op0 and \p
1292 /// \pre setBasicBlock or setMI must have been called.
1293 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1294 /// with the same (scalar or vector) type).
1296 /// \return a MachineInstrBuilder for the newly created instruction.
1298 MachineInstrBuilder
buildAnd(const DstOp
&Dst
, const SrcOp
&Src0
,
1299 const SrcOp
&Src1
) {
1300 return buildInstr(TargetOpcode::G_AND
, {Dst
}, {Src0
, Src1
});
1303 /// Build and insert \p Res = G_OR \p Op0, \p Op1
1305 /// G_OR sets \p Res to the bitwise or of integer parameters \p Op0 and \p
1308 /// \pre setBasicBlock or setMI must have been called.
1309 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1310 /// with the same (scalar or vector) type).
1312 /// \return a MachineInstrBuilder for the newly created instruction.
1313 MachineInstrBuilder
buildOr(const DstOp
&Dst
, const SrcOp
&Src0
,
1314 const SrcOp
&Src1
) {
1315 return buildInstr(TargetOpcode::G_OR
, {Dst
}, {Src0
, Src1
});
1318 /// Build and insert \p Res = G_XOR \p Op0, \p Op1
1319 MachineInstrBuilder
buildXor(const DstOp
&Dst
, const SrcOp
&Src0
,
1320 const SrcOp
&Src1
) {
1321 return buildInstr(TargetOpcode::G_XOR
, {Dst
}, {Src0
, Src1
});
1324 /// Build and insert a bitwise not,
1325 /// \p NegOne = G_CONSTANT -1
1326 /// \p Res = G_OR \p Op0, NegOne
1327 MachineInstrBuilder
buildNot(const DstOp
&Dst
, const SrcOp
&Src0
) {
1328 auto NegOne
= buildConstant(Dst
.getLLTTy(*getMRI()), -1);
1329 return buildInstr(TargetOpcode::G_XOR
, {Dst
}, {Src0
, NegOne
});
1332 /// Build and insert \p Res = G_CTPOP \p Op0, \p Src0
1333 MachineInstrBuilder
buildCTPOP(const DstOp
&Dst
, const SrcOp
&Src0
) {
1334 return buildInstr(TargetOpcode::G_CTPOP
, {Dst
}, {Src0
});
1337 /// Build and insert \p Res = G_CTLZ \p Op0, \p Src0
1338 MachineInstrBuilder
buildCTLZ(const DstOp
&Dst
, const SrcOp
&Src0
) {
1339 return buildInstr(TargetOpcode::G_CTLZ
, {Dst
}, {Src0
});
1342 /// Build and insert \p Res = G_CTLZ_ZERO_UNDEF \p Op0, \p Src0
1343 MachineInstrBuilder
buildCTLZ_ZERO_UNDEF(const DstOp
&Dst
, const SrcOp
&Src0
) {
1344 return buildInstr(TargetOpcode::G_CTLZ_ZERO_UNDEF
, {Dst
}, {Src0
});
1347 /// Build and insert \p Res = G_CTTZ \p Op0, \p Src0
1348 MachineInstrBuilder
buildCTTZ(const DstOp
&Dst
, const SrcOp
&Src0
) {
1349 return buildInstr(TargetOpcode::G_CTTZ
, {Dst
}, {Src0
});
1352 /// Build and insert \p Res = G_CTTZ_ZERO_UNDEF \p Op0, \p Src0
1353 MachineInstrBuilder
buildCTTZ_ZERO_UNDEF(const DstOp
&Dst
, const SrcOp
&Src0
) {
1354 return buildInstr(TargetOpcode::G_CTTZ_ZERO_UNDEF
, {Dst
}, {Src0
});
1357 /// Build and insert \p Res = G_FADD \p Op0, \p Op1
1358 MachineInstrBuilder
buildFAdd(const DstOp
&Dst
, const SrcOp
&Src0
,
1359 const SrcOp
&Src1
) {
1360 return buildInstr(TargetOpcode::G_FADD
, {Dst
}, {Src0
, Src1
});
1363 /// Build and insert \p Res = G_FSUB \p Op0, \p Op1
1364 MachineInstrBuilder
buildFSub(const DstOp
&Dst
, const SrcOp
&Src0
,
1365 const SrcOp
&Src1
) {
1366 return buildInstr(TargetOpcode::G_FSUB
, {Dst
}, {Src0
, Src1
});
1369 /// Build and insert \p Res = G_FMA \p Op0, \p Op1, \p Op2
1370 MachineInstrBuilder
buildFMA(const DstOp
&Dst
, const SrcOp
&Src0
,
1371 const SrcOp
&Src1
, const SrcOp
&Src2
) {
1372 return buildInstr(TargetOpcode::G_FMA
, {Dst
}, {Src0
, Src1
, Src2
});
1375 /// Build and insert \p Res = G_FNEG \p Op0
1376 MachineInstrBuilder
buildFNeg(const DstOp
&Dst
, const SrcOp
&Src0
) {
1377 return buildInstr(TargetOpcode::G_FNEG
, {Dst
}, {Src0
});
1380 /// Build and insert \p Res = G_FABS \p Op0
1381 MachineInstrBuilder
buildFAbs(const DstOp
&Dst
, const SrcOp
&Src0
,
1382 Optional
<unsigned> Flags
= None
) {
1383 return buildInstr(TargetOpcode::G_FABS
, {Dst
}, {Src0
}, Flags
);
1386 /// Build and insert \p Dst = G_FCANONICALIZE \p Src0
1387 MachineInstrBuilder
buildFCanonicalize(const DstOp
&Dst
, const SrcOp
&Src0
,
1388 Optional
<unsigned> Flags
= None
) {
1389 return buildInstr(TargetOpcode::G_FCANONICALIZE
, {Dst
}, {Src0
}, Flags
);
1392 /// Build and insert \p Res = G_FCOPYSIGN \p Op0, \p Op1
1393 MachineInstrBuilder
buildFCopysign(const DstOp
&Dst
, const SrcOp
&Src0
,
1394 const SrcOp
&Src1
) {
1395 return buildInstr(TargetOpcode::G_FCOPYSIGN
, {Dst
}, {Src0
, Src1
});
1398 /// Build and insert \p Res = G_UITOFP \p Src0
1399 MachineInstrBuilder
buildUITOFP(const DstOp
&Dst
, const SrcOp
&Src0
) {
1400 return buildInstr(TargetOpcode::G_UITOFP
, {Dst
}, {Src0
});
1403 /// Build and insert \p Res = G_SITOFP \p Src0
1404 MachineInstrBuilder
buildSITOFP(const DstOp
&Dst
, const SrcOp
&Src0
) {
1405 return buildInstr(TargetOpcode::G_SITOFP
, {Dst
}, {Src0
});
1408 /// Build and insert \p Res = G_FPTOUI \p Src0
1409 MachineInstrBuilder
buildFPTOUI(const DstOp
&Dst
, const SrcOp
&Src0
) {
1410 return buildInstr(TargetOpcode::G_FPTOUI
, {Dst
}, {Src0
});
1413 /// Build and insert \p Res = G_FPTOSI \p Src0
1414 MachineInstrBuilder
buildFPTOSI(const DstOp
&Dst
, const SrcOp
&Src0
) {
1415 return buildInstr(TargetOpcode::G_FPTOSI
, {Dst
}, {Src0
});
1418 /// Build and insert \p Res = G_SMIN \p Op0, \p Op1
1419 MachineInstrBuilder
buildSMin(const DstOp
&Dst
, const SrcOp
&Src0
,
1420 const SrcOp
&Src1
) {
1421 return buildInstr(TargetOpcode::G_SMIN
, {Dst
}, {Src0
, Src1
});
1424 /// Build and insert \p Res = G_SMAX \p Op0, \p Op1
1425 MachineInstrBuilder
buildSMax(const DstOp
&Dst
, const SrcOp
&Src0
,
1426 const SrcOp
&Src1
) {
1427 return buildInstr(TargetOpcode::G_SMAX
, {Dst
}, {Src0
, Src1
});
1430 /// Build and insert \p Res = G_UMIN \p Op0, \p Op1
1431 MachineInstrBuilder
buildUMin(const DstOp
&Dst
, const SrcOp
&Src0
,
1432 const SrcOp
&Src1
) {
1433 return buildInstr(TargetOpcode::G_UMIN
, {Dst
}, {Src0
, Src1
});
1436 /// Build and insert \p Res = G_UMAX \p Op0, \p Op1
1437 MachineInstrBuilder
buildUMax(const DstOp
&Dst
, const SrcOp
&Src0
,
1438 const SrcOp
&Src1
) {
1439 return buildInstr(TargetOpcode::G_UMAX
, {Dst
}, {Src0
, Src1
});
1442 /// Build and insert \p Res = G_JUMP_TABLE \p JTI
1444 /// G_JUMP_TABLE sets \p Res to the address of the jump table specified by
1445 /// the jump table index \p JTI.
1447 /// \return a MachineInstrBuilder for the newly created instruction.
1448 MachineInstrBuilder
buildJumpTable(const LLT PtrTy
, unsigned JTI
);
1450 virtual MachineInstrBuilder
buildInstr(unsigned Opc
, ArrayRef
<DstOp
> DstOps
,
1451 ArrayRef
<SrcOp
> SrcOps
,
1452 Optional
<unsigned> Flags
= None
);
1455 } // End namespace llvm.
1456 #endif // LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H