blob: 6e1ff7c3eb5629e02f1437dbc4c71598c81f2d1e [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- llvm/CodeGen/GlobalISel/MachineIRBuilder.h - MIBuilder --*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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
Andrew Walbran16937d02019-10-22 13:54:20 +010016#include "llvm/CodeGen/GlobalISel/CSEInfo.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010017#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;
Andrew Walbran16937d02019-10-22 13:54:20 +010033class GISelChangeObserver;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010034
Andrew Scullcdfcccc2018-10-05 20:58:37 +010035/// 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 {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010039 /// 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
Andrew Walbran16937d02019-10-22 13:54:20 +010054 GISelChangeObserver *Observer;
55
56 GISelCSEInfo *CSEInfo;
Andrew Scullcdfcccc2018-10-05 20:58:37 +010057};
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010058
Andrew Walbran16937d02019-10-22 13:54:20 +010059class DstOp {
60 union {
61 LLT LLTTy;
62 unsigned 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(const MachineOperand &Op) : Reg(Op.getReg()), Ty(DstType::Ty_Reg) {}
70 DstOp(const LLT &T) : LLTTy(T), Ty(DstType::Ty_LLT) {}
71 DstOp(const TargetRegisterClass *TRC) : RC(TRC), Ty(DstType::Ty_RC) {}
72
73 void addDefToMIB(MachineRegisterInfo &MRI, MachineInstrBuilder &MIB) const {
74 switch (Ty) {
75 case DstType::Ty_Reg:
76 MIB.addDef(Reg);
77 break;
78 case DstType::Ty_LLT:
79 MIB.addDef(MRI.createGenericVirtualRegister(LLTTy));
80 break;
81 case DstType::Ty_RC:
82 MIB.addDef(MRI.createVirtualRegister(RC));
83 break;
84 }
85 }
86
87 LLT getLLTTy(const MachineRegisterInfo &MRI) const {
88 switch (Ty) {
89 case DstType::Ty_RC:
90 return LLT{};
91 case DstType::Ty_LLT:
92 return LLTTy;
93 case DstType::Ty_Reg:
94 return MRI.getType(Reg);
95 }
96 llvm_unreachable("Unrecognised DstOp::DstType enum");
97 }
98
99 unsigned getReg() const {
100 assert(Ty == DstType::Ty_Reg && "Not a register");
101 return Reg;
102 }
103
104 const TargetRegisterClass *getRegClass() const {
105 switch (Ty) {
106 case DstType::Ty_RC:
107 return RC;
108 default:
109 llvm_unreachable("Not a RC Operand");
110 }
111 }
112
113 DstType getDstOpKind() const { return Ty; }
114
115private:
116 DstType Ty;
117};
118
119class SrcOp {
120 union {
121 MachineInstrBuilder SrcMIB;
122 unsigned Reg;
123 CmpInst::Predicate Pred;
124 };
125
126public:
127 enum class SrcType { Ty_Reg, Ty_MIB, Ty_Predicate };
128 SrcOp(unsigned R) : Reg(R), Ty(SrcType::Ty_Reg) {}
129 SrcOp(const MachineOperand &Op) : Reg(Op.getReg()), Ty(SrcType::Ty_Reg) {}
130 SrcOp(const MachineInstrBuilder &MIB) : SrcMIB(MIB), Ty(SrcType::Ty_MIB) {}
131 SrcOp(const CmpInst::Predicate P) : Pred(P), Ty(SrcType::Ty_Predicate) {}
132
133 void addSrcToMIB(MachineInstrBuilder &MIB) const {
134 switch (Ty) {
135 case SrcType::Ty_Predicate:
136 MIB.addPredicate(Pred);
137 break;
138 case SrcType::Ty_Reg:
139 MIB.addUse(Reg);
140 break;
141 case SrcType::Ty_MIB:
142 MIB.addUse(SrcMIB->getOperand(0).getReg());
143 break;
144 }
145 }
146
147 LLT getLLTTy(const MachineRegisterInfo &MRI) const {
148 switch (Ty) {
149 case SrcType::Ty_Predicate:
150 llvm_unreachable("Not a register operand");
151 case SrcType::Ty_Reg:
152 return MRI.getType(Reg);
153 case SrcType::Ty_MIB:
154 return MRI.getType(SrcMIB->getOperand(0).getReg());
155 }
156 llvm_unreachable("Unrecognised SrcOp::SrcType enum");
157 }
158
159 unsigned getReg() const {
160 switch (Ty) {
161 case SrcType::Ty_Predicate:
162 llvm_unreachable("Not a register operand");
163 case SrcType::Ty_Reg:
164 return Reg;
165 case SrcType::Ty_MIB:
166 return SrcMIB->getOperand(0).getReg();
167 }
168 llvm_unreachable("Unrecognised SrcOp::SrcType enum");
169 }
170
171 CmpInst::Predicate getPredicate() const {
172 switch (Ty) {
173 case SrcType::Ty_Predicate:
174 return Pred;
175 default:
176 llvm_unreachable("Not a register operand");
177 }
178 }
179
180 SrcType getSrcOpKind() const { return Ty; }
181
182private:
183 SrcType Ty;
184};
185
186class FlagsOp {
187 Optional<unsigned> Flags;
188
189public:
190 explicit FlagsOp(unsigned F) : Flags(F) {}
191 FlagsOp() : Flags(None) {}
192 Optional<unsigned> getFlags() const { return Flags; }
193};
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100194/// Helper class to build MachineInstr.
195/// It keeps internally the insertion point and debug location for all
196/// the new instructions we want to create.
197/// This information can be modify via the related setters.
Andrew Walbran16937d02019-10-22 13:54:20 +0100198class MachineIRBuilder {
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100199
200 MachineIRBuilderState State;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100201
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100202protected:
Andrew Walbran16937d02019-10-22 13:54:20 +0100203 void validateTruncExt(const LLT &Dst, const LLT &Src, bool IsExtend);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100204
Andrew Walbran16937d02019-10-22 13:54:20 +0100205 void validateBinaryOp(const LLT &Res, const LLT &Op0, const LLT &Op1);
206 void validateShiftOp(const LLT &Res, const LLT &Op0, const LLT &Op1);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100207
Andrew Walbran16937d02019-10-22 13:54:20 +0100208 void validateSelectOp(const LLT &ResTy, const LLT &TstTy, const LLT &Op0Ty,
209 const LLT &Op1Ty);
210 void recordInsertion(MachineInstr *MI) const;
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100211
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100212public:
213 /// Some constructors for easy use.
Andrew Walbran16937d02019-10-22 13:54:20 +0100214 MachineIRBuilder() = default;
215 MachineIRBuilder(MachineFunction &MF) { setMF(MF); }
216 MachineIRBuilder(MachineInstr &MI) : MachineIRBuilder(*MI.getMF()) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100217 setInstr(MI);
218 }
219
Andrew Walbran16937d02019-10-22 13:54:20 +0100220 virtual ~MachineIRBuilder() = default;
221
222 MachineIRBuilder(const MachineIRBuilderState &BState) : State(BState) {}
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100223
Andrew Scull0372a572018-11-16 15:47:06 +0000224 const TargetInstrInfo &getTII() {
225 assert(State.TII && "TargetInstrInfo is not set");
226 return *State.TII;
227 }
228
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100229 /// Getter for the function we currently build.
230 MachineFunction &getMF() {
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100231 assert(State.MF && "MachineFunction is not set");
232 return *State.MF;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100233 }
234
Andrew Walbran16937d02019-10-22 13:54:20 +0100235 const MachineFunction &getMF() const {
236 assert(State.MF && "MachineFunction is not set");
237 return *State.MF;
238 }
239
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100240 /// Getter for DebugLoc
241 const DebugLoc &getDL() { return State.DL; }
242
243 /// Getter for MRI
244 MachineRegisterInfo *getMRI() { return State.MRI; }
Andrew Walbran16937d02019-10-22 13:54:20 +0100245 const MachineRegisterInfo *getMRI() const { return State.MRI; }
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100246
247 /// Getter for the State
248 MachineIRBuilderState &getState() { return State; }
249
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100250 /// Getter for the basic block we currently build.
Andrew Walbran16937d02019-10-22 13:54:20 +0100251 const MachineBasicBlock &getMBB() const {
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100252 assert(State.MBB && "MachineBasicBlock is not set");
253 return *State.MBB;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100254 }
255
Andrew Walbran16937d02019-10-22 13:54:20 +0100256 MachineBasicBlock &getMBB() {
257 return const_cast<MachineBasicBlock &>(
258 const_cast<const MachineIRBuilder *>(this)->getMBB());
259 }
260
261 GISelCSEInfo *getCSEInfo() { return State.CSEInfo; }
262 const GISelCSEInfo *getCSEInfo() const { return State.CSEInfo; }
263
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100264 /// Current insertion point for new instructions.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100265 MachineBasicBlock::iterator getInsertPt() { return State.II; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100266
267 /// Set the insertion point before the specified position.
268 /// \pre MBB must be in getMF().
269 /// \pre II must be a valid iterator in MBB.
270 void setInsertPt(MachineBasicBlock &MBB, MachineBasicBlock::iterator II);
271 /// @}
272
Andrew Walbran16937d02019-10-22 13:54:20 +0100273 void setCSEInfo(GISelCSEInfo *Info);
274
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100275 /// \name Setters for the insertion point.
276 /// @{
277 /// Set the MachineFunction where to build instructions.
Andrew Walbran16937d02019-10-22 13:54:20 +0100278 void setMF(MachineFunction &MF);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100279
280 /// Set the insertion point to the end of \p MBB.
281 /// \pre \p MBB must be contained by getMF().
282 void setMBB(MachineBasicBlock &MBB);
283
284 /// Set the insertion point to before MI.
285 /// \pre MI must be in getMF().
286 void setInstr(MachineInstr &MI);
287 /// @}
288
Andrew Walbran16937d02019-10-22 13:54:20 +0100289 void setChangeObserver(GISelChangeObserver &Observer);
290 void stopObservingChanges();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100291 /// @}
292
293 /// Set the debug location to \p DL for all the next build instructions.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100294 void setDebugLoc(const DebugLoc &DL) { this->State.DL = DL; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100295
296 /// Get the current instruction's debug location.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100297 DebugLoc getDebugLoc() { return State.DL; }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100298
299 /// Build and insert <empty> = \p Opcode <empty>.
300 /// The insertion point is the one set by the last call of either
301 /// setBasicBlock or setMI.
302 ///
303 /// \pre setBasicBlock or setMI must have been called.
304 ///
305 /// \return a MachineInstrBuilder for the newly created instruction.
306 MachineInstrBuilder buildInstr(unsigned Opcode);
307
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100308 /// Build but don't insert <empty> = \p Opcode <empty>.
309 ///
310 /// \pre setMF, setBasicBlock or setMI must have been called.
311 ///
312 /// \return a MachineInstrBuilder for the newly created instruction.
313 MachineInstrBuilder buildInstrNoInsert(unsigned Opcode);
314
315 /// Insert an existing instruction at the insertion point.
316 MachineInstrBuilder insertInstr(MachineInstrBuilder MIB);
317
318 /// Build and insert a DBG_VALUE instruction expressing the fact that the
319 /// associated \p Variable lives in \p Reg (suitably modified by \p Expr).
320 MachineInstrBuilder buildDirectDbgValue(unsigned Reg, const MDNode *Variable,
321 const MDNode *Expr);
322
323 /// Build and insert a DBG_VALUE instruction expressing the fact that the
324 /// associated \p Variable lives in memory at \p Reg (suitably modified by \p
325 /// Expr).
326 MachineInstrBuilder buildIndirectDbgValue(unsigned Reg,
327 const MDNode *Variable,
328 const MDNode *Expr);
329
330 /// Build and insert a DBG_VALUE instruction expressing the fact that the
331 /// associated \p Variable lives in the stack slot specified by \p FI
332 /// (suitably modified by \p Expr).
333 MachineInstrBuilder buildFIDbgValue(int FI, const MDNode *Variable,
334 const MDNode *Expr);
335
336 /// Build and insert a DBG_VALUE instructions specifying that \p Variable is
337 /// given by \p C (suitably modified by \p Expr).
338 MachineInstrBuilder buildConstDbgValue(const Constant &C,
339 const MDNode *Variable,
340 const MDNode *Expr);
341
Andrew Scull0372a572018-11-16 15:47:06 +0000342 /// Build and insert a DBG_LABEL instructions specifying that \p Label is
343 /// given. Convert "llvm.dbg.label Label" to "DBG_LABEL Label".
344 MachineInstrBuilder buildDbgLabel(const MDNode *Label);
345
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100346 /// Build and insert \p Res = G_FRAME_INDEX \p Idx
347 ///
348 /// G_FRAME_INDEX materializes the address of an alloca value or other
349 /// stack-based object.
350 ///
351 /// \pre setBasicBlock or setMI must have been called.
352 /// \pre \p Res must be a generic virtual register with pointer type.
353 ///
354 /// \return a MachineInstrBuilder for the newly created instruction.
355 MachineInstrBuilder buildFrameIndex(unsigned Res, int Idx);
356
357 /// Build and insert \p Res = G_GLOBAL_VALUE \p GV
358 ///
359 /// G_GLOBAL_VALUE materializes the address of the specified global
360 /// into \p Res.
361 ///
362 /// \pre setBasicBlock or setMI must have been called.
363 /// \pre \p Res must be a generic virtual register with pointer type
364 /// in the same address space as \p GV.
365 ///
366 /// \return a MachineInstrBuilder for the newly created instruction.
367 MachineInstrBuilder buildGlobalValue(unsigned Res, const GlobalValue *GV);
368
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100369
370 /// Build and insert \p Res = G_GEP \p Op0, \p Op1
371 ///
372 /// G_GEP adds \p Op1 bytes to the pointer specified by \p Op0,
373 /// storing the resulting pointer in \p Res.
374 ///
375 /// \pre setBasicBlock or setMI must have been called.
376 /// \pre \p Res and \p Op0 must be generic virtual registers with pointer
377 /// type.
378 /// \pre \p Op1 must be a generic virtual register with scalar type.
379 ///
380 /// \return a MachineInstrBuilder for the newly created instruction.
381 MachineInstrBuilder buildGEP(unsigned Res, unsigned Op0,
382 unsigned Op1);
383
384 /// Materialize and insert \p Res = G_GEP \p Op0, (G_CONSTANT \p Value)
385 ///
386 /// G_GEP adds \p Value bytes to the pointer specified by \p Op0,
387 /// storing the resulting pointer in \p Res. If \p Value is zero then no
388 /// G_GEP or G_CONSTANT will be created and \pre Op0 will be assigned to
389 /// \p Res.
390 ///
391 /// \pre setBasicBlock or setMI must have been called.
392 /// \pre \p Op0 must be a generic virtual register with pointer type.
393 /// \pre \p ValueTy must be a scalar type.
394 /// \pre \p Res must be 0. This is to detect confusion between
395 /// materializeGEP() and buildGEP().
396 /// \post \p Res will either be a new generic virtual register of the same
397 /// type as \p Op0 or \p Op0 itself.
398 ///
399 /// \return a MachineInstrBuilder for the newly created instruction.
400 Optional<MachineInstrBuilder> materializeGEP(unsigned &Res, unsigned Op0,
401 const LLT &ValueTy,
402 uint64_t Value);
403
404 /// Build and insert \p Res = G_PTR_MASK \p Op0, \p NumBits
405 ///
406 /// G_PTR_MASK clears the low bits of a pointer operand without destroying its
407 /// pointer properties. This has the effect of rounding the address *down* to
408 /// a specified alignment in bits.
409 ///
410 /// \pre setBasicBlock or setMI must have been called.
411 /// \pre \p Res and \p Op0 must be generic virtual registers with pointer
412 /// type.
413 /// \pre \p NumBits must be an integer representing the number of low bits to
414 /// be cleared in \p Op0.
415 ///
416 /// \return a MachineInstrBuilder for the newly created instruction.
417 MachineInstrBuilder buildPtrMask(unsigned Res, unsigned Op0,
418 uint32_t NumBits);
419
420 /// Build and insert \p Res, \p CarryOut = G_UADDE \p Op0,
421 /// \p Op1, \p CarryIn
422 ///
423 /// G_UADDE sets \p Res to \p Op0 + \p Op1 + \p CarryIn (truncated to the bit
424 /// width) and sets \p CarryOut to 1 if the result overflowed in unsigned
425 /// arithmetic.
426 ///
427 /// \pre setBasicBlock or setMI must have been called.
428 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
429 /// with the same scalar type.
430 /// \pre \p CarryOut and \p CarryIn must be generic virtual
431 /// registers with the same scalar type (typically s1)
432 ///
433 /// \return The newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100434 MachineInstrBuilder buildUAdde(const DstOp &Res, const DstOp &CarryOut,
435 const SrcOp &Op0, const SrcOp &Op1,
436 const SrcOp &CarryIn);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100437
438 /// Build and insert \p Res = G_ANYEXT \p Op0
439 ///
440 /// G_ANYEXT produces a register of the specified width, with bits 0 to
441 /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are unspecified
442 /// (i.e. this is neither zero nor sign-extension). For a vector register,
443 /// each element is extended individually.
444 ///
445 /// \pre setBasicBlock or setMI must have been called.
446 /// \pre \p Res must be a generic virtual register with scalar or vector type.
447 /// \pre \p Op must be a generic virtual register with scalar or vector type.
448 /// \pre \p Op must be smaller than \p Res
449 ///
450 /// \return The newly created instruction.
451
Andrew Walbran16937d02019-10-22 13:54:20 +0100452 MachineInstrBuilder buildAnyExt(const DstOp &Res, const SrcOp &Op);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100453
454 /// Build and insert \p Res = G_SEXT \p Op
455 ///
456 /// G_SEXT produces a register of the specified width, with bits 0 to
457 /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are duplicated from the
458 /// high bit of \p Op (i.e. 2s-complement sign extended).
459 ///
460 /// \pre setBasicBlock or setMI must have been called.
461 /// \pre \p Res must be a generic virtual register with scalar or vector type.
462 /// \pre \p Op must be a generic virtual register with scalar or vector type.
463 /// \pre \p Op must be smaller than \p Res
464 ///
465 /// \return The newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100466 MachineInstrBuilder buildSExt(const DstOp &Res, const SrcOp &Op);
467
468 /// \return The opcode of the extension the target wants to use for boolean
469 /// values.
470 unsigned getBoolExtOp(bool IsVec, bool IsFP) const;
471
472 // Build and insert \p Res = G_ANYEXT \p Op, \p Res = G_SEXT \p Op, or \p Res
473 // = G_ZEXT \p Op depending on how the target wants to extend boolean values.
474 MachineInstrBuilder buildBoolExt(const DstOp &Res, const SrcOp &Op,
475 bool IsFP);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100476
477 /// Build and insert \p Res = G_ZEXT \p Op
478 ///
479 /// G_ZEXT produces a register of the specified width, with bits 0 to
480 /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are 0. For a vector
481 /// register, each element is extended individually.
482 ///
483 /// \pre setBasicBlock or setMI must have been called.
484 /// \pre \p Res must be a generic virtual register with scalar or vector type.
485 /// \pre \p Op must be a generic virtual register with scalar or vector type.
486 /// \pre \p Op must be smaller than \p Res
487 ///
488 /// \return The newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100489 MachineInstrBuilder buildZExt(const DstOp &Res, const SrcOp &Op);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100490
491 /// Build and insert \p Res = G_SEXT \p Op, \p Res = G_TRUNC \p Op, or
492 /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
493 /// ///
494 /// \pre setBasicBlock or setMI must have been called.
495 /// \pre \p Res must be a generic virtual register with scalar or vector type.
496 /// \pre \p Op must be a generic virtual register with scalar or vector type.
497 ///
498 /// \return The newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100499 MachineInstrBuilder buildSExtOrTrunc(const DstOp &Res, const SrcOp &Op);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100500
501 /// Build and insert \p Res = G_ZEXT \p Op, \p Res = G_TRUNC \p Op, or
502 /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
503 /// ///
504 /// \pre setBasicBlock or setMI must have been called.
505 /// \pre \p Res must be a generic virtual register with scalar or vector type.
506 /// \pre \p Op must be a generic virtual register with scalar or vector type.
507 ///
508 /// \return The newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100509 MachineInstrBuilder buildZExtOrTrunc(const DstOp &Res, const SrcOp &Op);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100510
511 // Build and insert \p Res = G_ANYEXT \p Op, \p Res = G_TRUNC \p Op, or
512 /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
513 /// ///
514 /// \pre setBasicBlock or setMI must have been called.
515 /// \pre \p Res must be a generic virtual register with scalar or vector type.
516 /// \pre \p Op must be a generic virtual register with scalar or vector type.
517 ///
518 /// \return The newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100519 MachineInstrBuilder buildAnyExtOrTrunc(const DstOp &Res, const SrcOp &Op);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100520
521 /// Build and insert \p Res = \p ExtOpc, \p Res = G_TRUNC \p
522 /// Op, or \p Res = COPY \p Op depending on the differing sizes of \p Res and
523 /// \p Op.
524 /// ///
525 /// \pre setBasicBlock or setMI must have been called.
526 /// \pre \p Res must be a generic virtual register with scalar or vector type.
527 /// \pre \p Op must be a generic virtual register with scalar or vector type.
528 ///
529 /// \return The newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100530 MachineInstrBuilder buildExtOrTrunc(unsigned ExtOpc, const DstOp &Res,
531 const SrcOp &Op);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100532
533 /// Build and insert an appropriate cast between two registers of equal size.
Andrew Walbran16937d02019-10-22 13:54:20 +0100534 MachineInstrBuilder buildCast(const DstOp &Dst, const SrcOp &Src);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100535
536 /// Build and insert G_BR \p Dest
537 ///
538 /// G_BR is an unconditional branch to \p Dest.
539 ///
540 /// \pre setBasicBlock or setMI must have been called.
541 ///
542 /// \return a MachineInstrBuilder for the newly created instruction.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100543 MachineInstrBuilder buildBr(MachineBasicBlock &Dest);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100544
545 /// Build and insert G_BRCOND \p Tst, \p Dest
546 ///
547 /// G_BRCOND is a conditional branch to \p Dest.
548 ///
549 /// \pre setBasicBlock or setMI must have been called.
550 /// \pre \p Tst must be a generic virtual register with scalar
551 /// type. At the beginning of legalization, this will be a single
552 /// bit (s1). Targets with interesting flags registers may change
553 /// this. For a wider type, whether the branch is taken must only
554 /// depend on bit 0 (for now).
555 ///
556 /// \return The newly created instruction.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100557 MachineInstrBuilder buildBrCond(unsigned Tst, MachineBasicBlock &Dest);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100558
559 /// Build and insert G_BRINDIRECT \p Tgt
560 ///
561 /// G_BRINDIRECT is an indirect branch to \p Tgt.
562 ///
563 /// \pre setBasicBlock or setMI must have been called.
564 /// \pre \p Tgt must be a generic virtual register with pointer type.
565 ///
566 /// \return a MachineInstrBuilder for the newly created instruction.
567 MachineInstrBuilder buildBrIndirect(unsigned Tgt);
568
569 /// Build and insert \p Res = G_CONSTANT \p Val
570 ///
571 /// G_CONSTANT is an integer constant with the specified size and value. \p
572 /// Val will be extended or truncated to the size of \p Reg.
573 ///
574 /// \pre setBasicBlock or setMI must have been called.
575 /// \pre \p Res must be a generic virtual register with scalar or pointer
576 /// type.
577 ///
578 /// \return The newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100579 virtual MachineInstrBuilder buildConstant(const DstOp &Res,
580 const ConstantInt &Val);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100581
582 /// Build and insert \p Res = G_CONSTANT \p Val
583 ///
584 /// G_CONSTANT is an integer constant with the specified size and value.
585 ///
586 /// \pre setBasicBlock or setMI must have been called.
587 /// \pre \p Res must be a generic virtual register with scalar type.
588 ///
589 /// \return The newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100590 MachineInstrBuilder buildConstant(const DstOp &Res, int64_t Val);
591 MachineInstrBuilder buildConstant(const DstOp &Res, const APInt &Val);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100592
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100593 /// Build and insert \p Res = G_FCONSTANT \p Val
594 ///
595 /// G_FCONSTANT is a floating-point constant with the specified size and
596 /// value.
597 ///
598 /// \pre setBasicBlock or setMI must have been called.
599 /// \pre \p Res must be a generic virtual register with scalar type.
600 ///
601 /// \return The newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100602 virtual MachineInstrBuilder buildFConstant(const DstOp &Res,
603 const ConstantFP &Val);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100604
Andrew Walbran16937d02019-10-22 13:54:20 +0100605 MachineInstrBuilder buildFConstant(const DstOp &Res, double Val);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100606
607 /// Build and insert \p Res = COPY Op
608 ///
609 /// Register-to-register COPY sets \p Res to \p Op.
610 ///
611 /// \pre setBasicBlock or setMI must have been called.
612 ///
613 /// \return a MachineInstrBuilder for the newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100614 MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100615
616 /// Build and insert `Res = G_LOAD Addr, MMO`.
617 ///
618 /// Loads the value stored at \p Addr. Puts the result in \p Res.
619 ///
620 /// \pre setBasicBlock or setMI must have been called.
621 /// \pre \p Res must be a generic virtual register.
622 /// \pre \p Addr must be a generic virtual register with pointer type.
623 ///
624 /// \return a MachineInstrBuilder for the newly created instruction.
625 MachineInstrBuilder buildLoad(unsigned Res, unsigned Addr,
626 MachineMemOperand &MMO);
627
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100628 /// Build and insert `Res = <opcode> Addr, MMO`.
629 ///
630 /// Loads the value stored at \p Addr. Puts the result in \p Res.
631 ///
632 /// \pre setBasicBlock or setMI must have been called.
633 /// \pre \p Res must be a generic virtual register.
634 /// \pre \p Addr must be a generic virtual register with pointer type.
635 ///
636 /// \return a MachineInstrBuilder for the newly created instruction.
637 MachineInstrBuilder buildLoadInstr(unsigned Opcode, unsigned Res,
638 unsigned Addr, MachineMemOperand &MMO);
639
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100640 /// Build and insert `G_STORE Val, Addr, MMO`.
641 ///
642 /// Stores the value \p Val to \p Addr.
643 ///
644 /// \pre setBasicBlock or setMI must have been called.
645 /// \pre \p Val must be a generic virtual register.
646 /// \pre \p Addr must be a generic virtual register with pointer type.
647 ///
648 /// \return a MachineInstrBuilder for the newly created instruction.
649 MachineInstrBuilder buildStore(unsigned Val, unsigned Addr,
650 MachineMemOperand &MMO);
651
652 /// Build and insert `Res0, ... = G_EXTRACT Src, Idx0`.
653 ///
654 /// \pre setBasicBlock or setMI must have been called.
655 /// \pre \p Res and \p Src must be generic virtual registers.
656 ///
657 /// \return a MachineInstrBuilder for the newly created instruction.
658 MachineInstrBuilder buildExtract(unsigned Res, unsigned Src, uint64_t Index);
659
660 /// Build and insert \p Res = IMPLICIT_DEF.
Andrew Walbran16937d02019-10-22 13:54:20 +0100661 MachineInstrBuilder buildUndef(const DstOp &Res);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100662
663 /// Build and insert instructions to put \p Ops together at the specified p
664 /// Indices to form a larger register.
665 ///
666 /// If the types of the input registers are uniform and cover the entirity of
667 /// \p Res then a G_MERGE_VALUES will be produced. Otherwise an IMPLICIT_DEF
668 /// followed by a sequence of G_INSERT instructions.
669 ///
670 /// \pre setBasicBlock or setMI must have been called.
671 /// \pre The final element of the sequence must not extend past the end of the
672 /// destination register.
673 /// \pre The bits defined by each Op (derived from index and scalar size) must
674 /// not overlap.
675 /// \pre \p Indices must be in ascending order of bit position.
676 void buildSequence(unsigned Res, ArrayRef<unsigned> Ops,
677 ArrayRef<uint64_t> Indices);
678
679 /// Build and insert \p Res = G_MERGE_VALUES \p Op0, ...
680 ///
681 /// G_MERGE_VALUES combines the input elements contiguously into a larger
682 /// register.
683 ///
684 /// \pre setBasicBlock or setMI must have been called.
685 /// \pre The entire register \p Res (and no more) must be covered by the input
686 /// registers.
687 /// \pre The type of all \p Ops registers must be identical.
688 ///
689 /// \return a MachineInstrBuilder for the newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100690 MachineInstrBuilder buildMerge(const DstOp &Res, ArrayRef<unsigned> Ops);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100691
692 /// Build and insert \p Res0, ... = G_UNMERGE_VALUES \p Op
693 ///
694 /// G_UNMERGE_VALUES splits contiguous bits of the input into multiple
695 ///
696 /// \pre setBasicBlock or setMI must have been called.
697 /// \pre The entire register \p Res (and no more) must be covered by the input
698 /// registers.
699 /// \pre The type of all \p Res registers must be identical.
700 ///
701 /// \return a MachineInstrBuilder for the newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100702 MachineInstrBuilder buildUnmerge(ArrayRef<LLT> Res, const SrcOp &Op);
703 MachineInstrBuilder buildUnmerge(ArrayRef<unsigned> Res, const SrcOp &Op);
704
705 /// Build and insert \p Res = G_BUILD_VECTOR \p Op0, ...
706 ///
707 /// G_BUILD_VECTOR creates a vector value from multiple scalar registers.
708 /// \pre setBasicBlock or setMI must have been called.
709 /// \pre The entire register \p Res (and no more) must be covered by the
710 /// input scalar registers.
711 /// \pre The type of all \p Ops registers must be identical.
712 ///
713 /// \return a MachineInstrBuilder for the newly created instruction.
714 MachineInstrBuilder buildBuildVector(const DstOp &Res,
715 ArrayRef<unsigned> Ops);
716
717 /// Build and insert \p Res = G_BUILD_VECTOR with \p Src replicated to fill
718 /// the number of elements
719 MachineInstrBuilder buildSplatVector(const DstOp &Res,
720 const SrcOp &Src);
721
722 /// Build and insert \p Res = G_BUILD_VECTOR_TRUNC \p Op0, ...
723 ///
724 /// G_BUILD_VECTOR_TRUNC creates a vector value from multiple scalar registers
725 /// which have types larger than the destination vector element type, and
726 /// truncates the values to fit.
727 ///
728 /// If the operands given are already the same size as the vector elt type,
729 /// then this method will instead create a G_BUILD_VECTOR instruction.
730 ///
731 /// \pre setBasicBlock or setMI must have been called.
732 /// \pre The type of all \p Ops registers must be identical.
733 ///
734 /// \return a MachineInstrBuilder for the newly created instruction.
735 MachineInstrBuilder buildBuildVectorTrunc(const DstOp &Res,
736 ArrayRef<unsigned> Ops);
737
738 /// Build and insert \p Res = G_CONCAT_VECTORS \p Op0, ...
739 ///
740 /// G_CONCAT_VECTORS creates a vector from the concatenation of 2 or more
741 /// vectors.
742 ///
743 /// \pre setBasicBlock or setMI must have been called.
744 /// \pre The entire register \p Res (and no more) must be covered by the input
745 /// registers.
746 /// \pre The type of all source operands must be identical.
747 ///
748 /// \return a MachineInstrBuilder for the newly created instruction.
749 MachineInstrBuilder buildConcatVectors(const DstOp &Res,
750 ArrayRef<unsigned> Ops);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100751
752 MachineInstrBuilder buildInsert(unsigned Res, unsigned Src,
753 unsigned Op, unsigned Index);
754
755 /// Build and insert either a G_INTRINSIC (if \p HasSideEffects is false) or
756 /// G_INTRINSIC_W_SIDE_EFFECTS instruction. Its first operand will be the
757 /// result register definition unless \p Reg is NoReg (== 0). The second
758 /// operand will be the intrinsic's ID.
759 ///
760 /// Callers are expected to add the required definitions and uses afterwards.
761 ///
762 /// \pre setBasicBlock or setMI must have been called.
763 ///
764 /// \return a MachineInstrBuilder for the newly created instruction.
765 MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, unsigned Res,
766 bool HasSideEffects);
767
768 /// Build and insert \p Res = G_FPTRUNC \p Op
769 ///
770 /// G_FPTRUNC converts a floating-point value into one with a smaller type.
771 ///
772 /// \pre setBasicBlock or setMI must have been called.
773 /// \pre \p Res must be a generic virtual register with scalar or vector type.
774 /// \pre \p Op must be a generic virtual register with scalar or vector type.
775 /// \pre \p Res must be smaller than \p Op
776 ///
777 /// \return The newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100778 MachineInstrBuilder buildFPTrunc(const DstOp &Res, const SrcOp &Op);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100779
780 /// Build and insert \p Res = G_TRUNC \p Op
781 ///
782 /// G_TRUNC extracts the low bits of a type. For a vector type each element is
783 /// truncated independently before being packed into the destination.
784 ///
785 /// \pre setBasicBlock or setMI must have been called.
786 /// \pre \p Res must be a generic virtual register with scalar or vector type.
787 /// \pre \p Op must be a generic virtual register with scalar or vector type.
788 /// \pre \p Res must be smaller than \p Op
789 ///
790 /// \return The newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100791 MachineInstrBuilder buildTrunc(const DstOp &Res, const SrcOp &Op);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100792
793 /// Build and insert a \p Res = G_ICMP \p Pred, \p Op0, \p Op1
794 ///
795 /// \pre setBasicBlock or setMI must have been called.
796
797 /// \pre \p Res must be a generic virtual register with scalar or
798 /// vector type. Typically this starts as s1 or <N x s1>.
799 /// \pre \p Op0 and Op1 must be generic virtual registers with the
800 /// same number of elements as \p Res. If \p Res is a scalar,
801 /// \p Op0 must be either a scalar or pointer.
802 /// \pre \p Pred must be an integer predicate.
803 ///
804 /// \return a MachineInstrBuilder for the newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100805 MachineInstrBuilder buildICmp(CmpInst::Predicate Pred, const DstOp &Res,
806 const SrcOp &Op0, const SrcOp &Op1);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100807
808 /// Build and insert a \p Res = G_FCMP \p Pred\p Op0, \p Op1
809 ///
810 /// \pre setBasicBlock or setMI must have been called.
811
812 /// \pre \p Res must be a generic virtual register with scalar or
813 /// vector type. Typically this starts as s1 or <N x s1>.
814 /// \pre \p Op0 and Op1 must be generic virtual registers with the
815 /// same number of elements as \p Res (or scalar, if \p Res is
816 /// scalar).
817 /// \pre \p Pred must be a floating-point predicate.
818 ///
819 /// \return a MachineInstrBuilder for the newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100820 MachineInstrBuilder buildFCmp(CmpInst::Predicate Pred, const DstOp &Res,
821 const SrcOp &Op0, const SrcOp &Op1);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100822
823 /// Build and insert a \p Res = G_SELECT \p Tst, \p Op0, \p Op1
824 ///
825 /// \pre setBasicBlock or setMI must have been called.
826 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
827 /// with the same type.
828 /// \pre \p Tst must be a generic virtual register with scalar, pointer or
829 /// vector type. If vector then it must have the same number of
830 /// elements as the other parameters.
831 ///
832 /// \return a MachineInstrBuilder for the newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100833 MachineInstrBuilder buildSelect(const DstOp &Res, const SrcOp &Tst,
834 const SrcOp &Op0, const SrcOp &Op1);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100835
836 /// Build and insert \p Res = G_INSERT_VECTOR_ELT \p Val,
837 /// \p Elt, \p Idx
838 ///
839 /// \pre setBasicBlock or setMI must have been called.
840 /// \pre \p Res and \p Val must be a generic virtual register
841 // with the same vector type.
842 /// \pre \p Elt and \p Idx must be a generic virtual register
843 /// with scalar type.
844 ///
845 /// \return The newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100846 MachineInstrBuilder buildInsertVectorElement(const DstOp &Res,
847 const SrcOp &Val,
848 const SrcOp &Elt,
849 const SrcOp &Idx);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100850
851 /// Build and insert \p Res = G_EXTRACT_VECTOR_ELT \p Val, \p Idx
852 ///
853 /// \pre setBasicBlock or setMI must have been called.
854 /// \pre \p Res must be a generic virtual register with scalar type.
855 /// \pre \p Val must be a generic virtual register with vector type.
856 /// \pre \p Idx must be a generic virtual register with scalar type.
857 ///
858 /// \return The newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +0100859 MachineInstrBuilder buildExtractVectorElement(const DstOp &Res,
860 const SrcOp &Val,
861 const SrcOp &Idx);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100862
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100863 /// Build and insert `OldValRes<def>, SuccessRes<def> =
864 /// G_ATOMIC_CMPXCHG_WITH_SUCCESS Addr, CmpVal, NewVal, MMO`.
865 ///
866 /// Atomically replace the value at \p Addr with \p NewVal if it is currently
867 /// \p CmpVal otherwise leaves it unchanged. Puts the original value from \p
868 /// Addr in \p Res, along with an s1 indicating whether it was replaced.
869 ///
870 /// \pre setBasicBlock or setMI must have been called.
871 /// \pre \p OldValRes must be a generic virtual register of scalar type.
872 /// \pre \p SuccessRes must be a generic virtual register of scalar type. It
873 /// will be assigned 0 on failure and 1 on success.
874 /// \pre \p Addr must be a generic virtual register with pointer type.
875 /// \pre \p OldValRes, \p CmpVal, and \p NewVal must be generic virtual
876 /// registers of the same type.
877 ///
878 /// \return a MachineInstrBuilder for the newly created instruction.
879 MachineInstrBuilder
880 buildAtomicCmpXchgWithSuccess(unsigned OldValRes, unsigned SuccessRes,
881 unsigned Addr, unsigned CmpVal, unsigned NewVal,
882 MachineMemOperand &MMO);
883
884 /// Build and insert `OldValRes<def> = G_ATOMIC_CMPXCHG Addr, CmpVal, NewVal,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100885 /// MMO`.
886 ///
887 /// Atomically replace the value at \p Addr with \p NewVal if it is currently
888 /// \p CmpVal otherwise leaves it unchanged. Puts the original value from \p
889 /// Addr in \p Res.
890 ///
891 /// \pre setBasicBlock or setMI must have been called.
892 /// \pre \p OldValRes must be a generic virtual register of scalar type.
893 /// \pre \p Addr must be a generic virtual register with pointer type.
894 /// \pre \p OldValRes, \p CmpVal, and \p NewVal must be generic virtual
895 /// registers of the same type.
896 ///
897 /// \return a MachineInstrBuilder for the newly created instruction.
898 MachineInstrBuilder buildAtomicCmpXchg(unsigned OldValRes, unsigned Addr,
899 unsigned CmpVal, unsigned NewVal,
900 MachineMemOperand &MMO);
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100901
902 /// Build and insert `OldValRes<def> = G_ATOMICRMW_<Opcode> Addr, Val, MMO`.
903 ///
904 /// Atomically read-modify-update the value at \p Addr with \p Val. Puts the
905 /// original value from \p Addr in \p OldValRes. The modification is
906 /// determined by the opcode.
907 ///
908 /// \pre setBasicBlock or setMI must have been called.
909 /// \pre \p OldValRes must be a generic virtual register.
910 /// \pre \p Addr must be a generic virtual register with pointer type.
911 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
912 /// same type.
913 ///
914 /// \return a MachineInstrBuilder for the newly created instruction.
915 MachineInstrBuilder buildAtomicRMW(unsigned Opcode, unsigned OldValRes,
916 unsigned Addr, unsigned Val,
917 MachineMemOperand &MMO);
918
919 /// Build and insert `OldValRes<def> = G_ATOMICRMW_XCHG Addr, Val, MMO`.
920 ///
921 /// Atomically replace the value at \p Addr with \p Val. Puts the original
922 /// value from \p Addr in \p OldValRes.
923 ///
924 /// \pre setBasicBlock or setMI must have been called.
925 /// \pre \p OldValRes must be a generic virtual register.
926 /// \pre \p Addr must be a generic virtual register with pointer type.
927 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
928 /// same type.
929 ///
930 /// \return a MachineInstrBuilder for the newly created instruction.
931 MachineInstrBuilder buildAtomicRMWXchg(unsigned OldValRes, unsigned Addr,
932 unsigned Val, MachineMemOperand &MMO);
933
934 /// Build and insert `OldValRes<def> = G_ATOMICRMW_ADD Addr, Val, MMO`.
935 ///
936 /// Atomically replace the value at \p Addr with the addition of \p Val and
937 /// the original value. Puts the original value from \p Addr in \p OldValRes.
938 ///
939 /// \pre setBasicBlock or setMI must have been called.
940 /// \pre \p OldValRes must be a generic virtual register.
941 /// \pre \p Addr must be a generic virtual register with pointer type.
942 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
943 /// same type.
944 ///
945 /// \return a MachineInstrBuilder for the newly created instruction.
946 MachineInstrBuilder buildAtomicRMWAdd(unsigned OldValRes, unsigned Addr,
947 unsigned Val, MachineMemOperand &MMO);
948
949 /// Build and insert `OldValRes<def> = G_ATOMICRMW_SUB Addr, Val, MMO`.
950 ///
951 /// Atomically replace the value at \p Addr with the subtraction of \p Val and
952 /// the original value. Puts the original value from \p Addr in \p OldValRes.
953 ///
954 /// \pre setBasicBlock or setMI must have been called.
955 /// \pre \p OldValRes must be a generic virtual register.
956 /// \pre \p Addr must be a generic virtual register with pointer type.
957 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
958 /// same type.
959 ///
960 /// \return a MachineInstrBuilder for the newly created instruction.
961 MachineInstrBuilder buildAtomicRMWSub(unsigned OldValRes, unsigned Addr,
962 unsigned Val, MachineMemOperand &MMO);
963
964 /// Build and insert `OldValRes<def> = G_ATOMICRMW_AND Addr, Val, MMO`.
965 ///
966 /// Atomically replace the value at \p Addr with the bitwise and of \p Val and
967 /// the original value. Puts the original value from \p Addr in \p OldValRes.
968 ///
969 /// \pre setBasicBlock or setMI must have been called.
970 /// \pre \p OldValRes must be a generic virtual register.
971 /// \pre \p Addr must be a generic virtual register with pointer type.
972 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
973 /// same type.
974 ///
975 /// \return a MachineInstrBuilder for the newly created instruction.
976 MachineInstrBuilder buildAtomicRMWAnd(unsigned OldValRes, unsigned Addr,
977 unsigned Val, MachineMemOperand &MMO);
978
979 /// Build and insert `OldValRes<def> = G_ATOMICRMW_NAND Addr, Val, MMO`.
980 ///
981 /// Atomically replace the value at \p Addr with the bitwise nand of \p Val
982 /// and the original value. Puts the original value from \p Addr in \p
983 /// OldValRes.
984 ///
985 /// \pre setBasicBlock or setMI must have been called.
986 /// \pre \p OldValRes must be a generic virtual register.
987 /// \pre \p Addr must be a generic virtual register with pointer type.
988 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
989 /// same type.
990 ///
991 /// \return a MachineInstrBuilder for the newly created instruction.
992 MachineInstrBuilder buildAtomicRMWNand(unsigned OldValRes, unsigned Addr,
993 unsigned Val, MachineMemOperand &MMO);
994
995 /// Build and insert `OldValRes<def> = G_ATOMICRMW_OR Addr, Val, MMO`.
996 ///
997 /// Atomically replace the value at \p Addr with the bitwise or of \p Val and
998 /// the original value. Puts the original value from \p Addr in \p OldValRes.
999 ///
1000 /// \pre setBasicBlock or setMI must have been called.
1001 /// \pre \p OldValRes must be a generic virtual register.
1002 /// \pre \p Addr must be a generic virtual register with pointer type.
1003 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1004 /// same type.
1005 ///
1006 /// \return a MachineInstrBuilder for the newly created instruction.
1007 MachineInstrBuilder buildAtomicRMWOr(unsigned OldValRes, unsigned Addr,
1008 unsigned Val, MachineMemOperand &MMO);
1009
1010 /// Build and insert `OldValRes<def> = G_ATOMICRMW_XOR Addr, Val, MMO`.
1011 ///
1012 /// Atomically replace the value at \p Addr with the bitwise xor of \p Val and
1013 /// the original value. Puts the original value from \p Addr in \p OldValRes.
1014 ///
1015 /// \pre setBasicBlock or setMI must have been called.
1016 /// \pre \p OldValRes must be a generic virtual register.
1017 /// \pre \p Addr must be a generic virtual register with pointer type.
1018 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1019 /// same type.
1020 ///
1021 /// \return a MachineInstrBuilder for the newly created instruction.
1022 MachineInstrBuilder buildAtomicRMWXor(unsigned OldValRes, unsigned Addr,
1023 unsigned Val, MachineMemOperand &MMO);
1024
1025 /// Build and insert `OldValRes<def> = G_ATOMICRMW_MAX Addr, Val, MMO`.
1026 ///
1027 /// Atomically replace the value at \p Addr with the signed maximum of \p
1028 /// Val and the original value. Puts the original value from \p Addr in \p
1029 /// OldValRes.
1030 ///
1031 /// \pre setBasicBlock or setMI must have been called.
1032 /// \pre \p OldValRes must be a generic virtual register.
1033 /// \pre \p Addr must be a generic virtual register with pointer type.
1034 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1035 /// same type.
1036 ///
1037 /// \return a MachineInstrBuilder for the newly created instruction.
1038 MachineInstrBuilder buildAtomicRMWMax(unsigned OldValRes, unsigned Addr,
1039 unsigned Val, MachineMemOperand &MMO);
1040
1041 /// Build and insert `OldValRes<def> = G_ATOMICRMW_MIN Addr, Val, MMO`.
1042 ///
1043 /// Atomically replace the value at \p Addr with the signed minimum of \p
1044 /// Val and the original value. Puts the original value from \p Addr in \p
1045 /// OldValRes.
1046 ///
1047 /// \pre setBasicBlock or setMI must have been called.
1048 /// \pre \p OldValRes must be a generic virtual register.
1049 /// \pre \p Addr must be a generic virtual register with pointer type.
1050 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1051 /// same type.
1052 ///
1053 /// \return a MachineInstrBuilder for the newly created instruction.
1054 MachineInstrBuilder buildAtomicRMWMin(unsigned OldValRes, unsigned Addr,
1055 unsigned Val, MachineMemOperand &MMO);
1056
1057 /// Build and insert `OldValRes<def> = G_ATOMICRMW_UMAX Addr, Val, MMO`.
1058 ///
1059 /// Atomically replace the value at \p Addr with the unsigned maximum of \p
1060 /// Val and the original value. Puts the original value from \p Addr in \p
1061 /// OldValRes.
1062 ///
1063 /// \pre setBasicBlock or setMI must have been called.
1064 /// \pre \p OldValRes must be a generic virtual register.
1065 /// \pre \p Addr must be a generic virtual register with pointer type.
1066 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1067 /// same type.
1068 ///
1069 /// \return a MachineInstrBuilder for the newly created instruction.
1070 MachineInstrBuilder buildAtomicRMWUmax(unsigned OldValRes, unsigned Addr,
1071 unsigned Val, MachineMemOperand &MMO);
1072
1073 /// Build and insert `OldValRes<def> = G_ATOMICRMW_UMIN Addr, Val, MMO`.
1074 ///
1075 /// Atomically replace the value at \p Addr with the unsigned minimum of \p
1076 /// Val and the original value. Puts the original value from \p Addr in \p
1077 /// OldValRes.
1078 ///
1079 /// \pre setBasicBlock or setMI must have been called.
1080 /// \pre \p OldValRes must be a generic virtual register.
1081 /// \pre \p Addr must be a generic virtual register with pointer type.
1082 /// \pre \p OldValRes, and \p Val must be generic virtual registers of the
1083 /// same type.
1084 ///
1085 /// \return a MachineInstrBuilder for the newly created instruction.
1086 MachineInstrBuilder buildAtomicRMWUmin(unsigned OldValRes, unsigned Addr,
1087 unsigned Val, MachineMemOperand &MMO);
1088
1089 /// Build and insert \p Res = G_BLOCK_ADDR \p BA
1090 ///
1091 /// G_BLOCK_ADDR computes the address of a basic block.
1092 ///
1093 /// \pre setBasicBlock or setMI must have been called.
1094 /// \pre \p Res must be a generic virtual register of a pointer type.
1095 ///
1096 /// \return The newly created instruction.
1097 MachineInstrBuilder buildBlockAddress(unsigned Res, const BlockAddress *BA);
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001098
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001099 /// Build and insert \p Res = G_ADD \p Op0, \p Op1
1100 ///
1101 /// G_ADD sets \p Res to the sum of integer parameters \p Op0 and \p Op1,
1102 /// truncated to their width.
1103 ///
1104 /// \pre setBasicBlock or setMI must have been called.
1105 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1106 /// with the same (scalar or vector) type).
1107 ///
1108 /// \return a MachineInstrBuilder for the newly created instruction.
1109
Andrew Walbran16937d02019-10-22 13:54:20 +01001110 MachineInstrBuilder buildAdd(const DstOp &Dst, const SrcOp &Src0,
1111 const SrcOp &Src1,
1112 Optional<unsigned> Flags = None) {
1113 return buildInstr(TargetOpcode::G_ADD, {Dst}, {Src0, Src1}, Flags);
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001114 }
1115
1116 /// Build and insert \p Res = G_SUB \p Op0, \p Op1
1117 ///
1118 /// G_SUB sets \p Res to the sum of integer parameters \p Op0 and \p Op1,
1119 /// truncated to their width.
1120 ///
1121 /// \pre setBasicBlock or setMI must have been called.
1122 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1123 /// with the same (scalar or vector) type).
1124 ///
1125 /// \return a MachineInstrBuilder for the newly created instruction.
1126
Andrew Walbran16937d02019-10-22 13:54:20 +01001127 MachineInstrBuilder buildSub(const DstOp &Dst, const SrcOp &Src0,
1128 const SrcOp &Src1,
1129 Optional<unsigned> Flags = None) {
1130 return buildInstr(TargetOpcode::G_SUB, {Dst}, {Src0, Src1}, Flags);
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001131 }
1132
1133 /// Build and insert \p Res = G_MUL \p Op0, \p Op1
1134 ///
1135 /// G_MUL sets \p Res to the sum of integer parameters \p Op0 and \p Op1,
1136 /// truncated to their width.
1137 ///
1138 /// \pre setBasicBlock or setMI must have been called.
1139 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1140 /// with the same (scalar or vector) type).
1141 ///
1142 /// \return a MachineInstrBuilder for the newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +01001143 MachineInstrBuilder buildMul(const DstOp &Dst, const SrcOp &Src0,
1144 const SrcOp &Src1,
1145 Optional<unsigned> Flags = None) {
1146 return buildInstr(TargetOpcode::G_MUL, {Dst}, {Src0, Src1}, Flags);
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001147 }
Andrew Walbran16937d02019-10-22 13:54:20 +01001148
1149 MachineInstrBuilder buildUMulH(const DstOp &Dst, const SrcOp &Src0,
1150 const SrcOp &Src1,
1151 Optional<unsigned> Flags = None) {
1152 return buildInstr(TargetOpcode::G_UMULH, {Dst}, {Src0, Src1}, Flags);
1153 }
1154
1155 MachineInstrBuilder buildSMulH(const DstOp &Dst, const SrcOp &Src0,
1156 const SrcOp &Src1,
1157 Optional<unsigned> Flags = None) {
1158 return buildInstr(TargetOpcode::G_SMULH, {Dst}, {Src0, Src1}, Flags);
1159 }
1160
1161 MachineInstrBuilder buildShl(const DstOp &Dst, const SrcOp &Src0,
1162 const SrcOp &Src1,
1163 Optional<unsigned> Flags = None) {
1164 return buildInstr(TargetOpcode::G_SHL, {Dst}, {Src0, Src1}, Flags);
1165 }
1166
1167 MachineInstrBuilder buildLShr(const DstOp &Dst, const SrcOp &Src0,
1168 const SrcOp &Src1,
1169 Optional<unsigned> Flags = None) {
1170 return buildInstr(TargetOpcode::G_LSHR, {Dst}, {Src0, Src1}, Flags);
1171 }
1172
1173 MachineInstrBuilder buildAShr(const DstOp &Dst, const SrcOp &Src0,
1174 const SrcOp &Src1,
1175 Optional<unsigned> Flags = None) {
1176 return buildInstr(TargetOpcode::G_ASHR, {Dst}, {Src0, Src1}, Flags);
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001177 }
1178
1179 /// Build and insert \p Res = G_AND \p Op0, \p Op1
1180 ///
1181 /// G_AND sets \p Res to the bitwise and of integer parameters \p Op0 and \p
1182 /// Op1.
1183 ///
1184 /// \pre setBasicBlock or setMI must have been called.
1185 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1186 /// with the same (scalar or vector) type).
1187 ///
1188 /// \return a MachineInstrBuilder for the newly created instruction.
1189
Andrew Walbran16937d02019-10-22 13:54:20 +01001190 MachineInstrBuilder buildAnd(const DstOp &Dst, const SrcOp &Src0,
1191 const SrcOp &Src1) {
1192 return buildInstr(TargetOpcode::G_AND, {Dst}, {Src0, Src1});
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001193 }
1194
1195 /// Build and insert \p Res = G_OR \p Op0, \p Op1
1196 ///
1197 /// G_OR sets \p Res to the bitwise or of integer parameters \p Op0 and \p
1198 /// Op1.
1199 ///
1200 /// \pre setBasicBlock or setMI must have been called.
1201 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
1202 /// with the same (scalar or vector) type).
1203 ///
1204 /// \return a MachineInstrBuilder for the newly created instruction.
Andrew Walbran16937d02019-10-22 13:54:20 +01001205 MachineInstrBuilder buildOr(const DstOp &Dst, const SrcOp &Src0,
1206 const SrcOp &Src1) {
1207 return buildInstr(TargetOpcode::G_OR, {Dst}, {Src0, Src1});
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001208 }
Andrew Scullcdfcccc2018-10-05 20:58:37 +01001209
Andrew Walbran16937d02019-10-22 13:54:20 +01001210 virtual MachineInstrBuilder buildInstr(unsigned Opc, ArrayRef<DstOp> DstOps,
1211 ArrayRef<SrcOp> SrcOps,
1212 Optional<unsigned> Flags = None);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001213};
1214
1215} // End namespace llvm.
1216#endif // LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H