blob: ef4e0ad86d2d41023fce538eab8017da2a5faf37 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===-- llvm/CodeGen/GlobalISel/MachineIRBuilder.h - MIBuilder --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// This file declares the MachineIRBuilder class.
11/// This is a helper class to build MachineInstr.
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
15#define LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H
16
17#include "llvm/CodeGen/GlobalISel/Types.h"
18
19#include "llvm/CodeGen/LowLevelType.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/IR/Constants.h"
24#include "llvm/IR/DebugLoc.h"
25
26
27namespace llvm {
28
29// Forward declarations.
30class MachineFunction;
31class MachineInstr;
32class TargetInstrInfo;
33
34/// Helper class to build MachineInstr.
35/// It keeps internally the insertion point and debug location for all
36/// the new instructions we want to create.
37/// This information can be modify via the related setters.
38class MachineIRBuilder {
39 /// MachineFunction under construction.
40 MachineFunction *MF;
41 /// Information used to access the description of the opcodes.
42 const TargetInstrInfo *TII;
43 /// Information used to verify types are consistent and to create virtual registers.
44 MachineRegisterInfo *MRI;
45 /// Debug location to be set to any instruction we create.
46 DebugLoc DL;
47
48 /// \name Fields describing the insertion point.
49 /// @{
50 MachineBasicBlock *MBB;
51 MachineBasicBlock::iterator II;
52 /// @}
53
54 std::function<void(MachineInstr *)> InsertedInstr;
55
56 const TargetInstrInfo &getTII() {
57 assert(TII && "TargetInstrInfo is not set");
58 return *TII;
59 }
60
61 void validateTruncExt(unsigned Dst, unsigned Src, bool IsExtend);
62 MachineInstrBuilder buildBinaryOp(unsigned Opcode, unsigned Res, unsigned Op0, unsigned Op1);
63
64 unsigned getDestFromArg(unsigned Reg) { return Reg; }
65 unsigned getDestFromArg(LLT Ty) {
66 return getMF().getRegInfo().createGenericVirtualRegister(Ty);
67 }
68 unsigned getDestFromArg(const TargetRegisterClass *RC) {
69 return getMF().getRegInfo().createVirtualRegister(RC);
70 }
71
72 void addUseFromArg(MachineInstrBuilder &MIB, unsigned Reg) {
73 MIB.addUse(Reg);
74 }
75
76 void addUseFromArg(MachineInstrBuilder &MIB, const MachineInstrBuilder &UseMIB) {
77 MIB.addUse(UseMIB->getOperand(0).getReg());
78 }
79
80 void addUsesFromArgs(MachineInstrBuilder &MIB) { }
81 template<typename UseArgTy, typename ... UseArgsTy>
82 void addUsesFromArgs(MachineInstrBuilder &MIB, UseArgTy &&Arg1, UseArgsTy &&... Args) {
83 addUseFromArg(MIB, Arg1);
84 addUsesFromArgs(MIB, std::forward<UseArgsTy>(Args)...);
85 }
86 unsigned getRegFromArg(unsigned Reg) { return Reg; }
87 unsigned getRegFromArg(const MachineInstrBuilder &MIB) {
88 return MIB->getOperand(0).getReg();
89 }
90
91public:
92 /// Some constructors for easy use.
93 MachineIRBuilder() = default;
94 MachineIRBuilder(MachineFunction &MF) { setMF(MF); }
95 MachineIRBuilder(MachineInstr &MI) : MachineIRBuilder(*MI.getMF()) {
96 setInstr(MI);
97 }
98
99 /// Getter for the function we currently build.
100 MachineFunction &getMF() {
101 assert(MF && "MachineFunction is not set");
102 return *MF;
103 }
104
105 /// Getter for the basic block we currently build.
106 MachineBasicBlock &getMBB() {
107 assert(MBB && "MachineBasicBlock is not set");
108 return *MBB;
109 }
110
111 /// Current insertion point for new instructions.
112 MachineBasicBlock::iterator getInsertPt() {
113 return II;
114 }
115
116 /// Set the insertion point before the specified position.
117 /// \pre MBB must be in getMF().
118 /// \pre II must be a valid iterator in MBB.
119 void setInsertPt(MachineBasicBlock &MBB, MachineBasicBlock::iterator II);
120 /// @}
121
122 /// \name Setters for the insertion point.
123 /// @{
124 /// Set the MachineFunction where to build instructions.
125 void setMF(MachineFunction &);
126
127 /// Set the insertion point to the end of \p MBB.
128 /// \pre \p MBB must be contained by getMF().
129 void setMBB(MachineBasicBlock &MBB);
130
131 /// Set the insertion point to before MI.
132 /// \pre MI must be in getMF().
133 void setInstr(MachineInstr &MI);
134 /// @}
135
136 /// \name Control where instructions we create are recorded (typically for
137 /// visiting again later during legalization).
138 /// @{
139 void recordInsertions(std::function<void(MachineInstr *)> InsertedInstr);
140 void stopRecordingInsertions();
141 /// @}
142
143 /// Set the debug location to \p DL for all the next build instructions.
144 void setDebugLoc(const DebugLoc &DL) { this->DL = DL; }
145
146 /// Get the current instruction's debug location.
147 DebugLoc getDebugLoc() { return DL; }
148
149 /// Build and insert <empty> = \p Opcode <empty>.
150 /// The insertion point is the one set by the last call of either
151 /// setBasicBlock or setMI.
152 ///
153 /// \pre setBasicBlock or setMI must have been called.
154 ///
155 /// \return a MachineInstrBuilder for the newly created instruction.
156 MachineInstrBuilder buildInstr(unsigned Opcode);
157
158 /// DAG like Generic method for building arbitrary instructions as above.
159 /// \Opc opcode for the instruction.
160 /// \Ty Either LLT/TargetRegisterClass/unsigned types for Dst
161 /// \Args Variadic list of uses of types(unsigned/MachineInstrBuilder)
162 /// Uses of type MachineInstrBuilder will perform
163 /// getOperand(0).getReg() to convert to register.
164 template <typename DstTy, typename... UseArgsTy>
165 MachineInstrBuilder buildInstr(unsigned Opc, DstTy &&Ty,
166 UseArgsTy &&... Args) {
167 auto MIB = buildInstr(Opc).addDef(getDestFromArg(Ty));
168 addUsesFromArgs(MIB, std::forward<UseArgsTy>(Args)...);
169 return MIB;
170 }
171
172 /// Build but don't insert <empty> = \p Opcode <empty>.
173 ///
174 /// \pre setMF, setBasicBlock or setMI must have been called.
175 ///
176 /// \return a MachineInstrBuilder for the newly created instruction.
177 MachineInstrBuilder buildInstrNoInsert(unsigned Opcode);
178
179 /// Insert an existing instruction at the insertion point.
180 MachineInstrBuilder insertInstr(MachineInstrBuilder MIB);
181
182 /// Build and insert a DBG_VALUE instruction expressing the fact that the
183 /// associated \p Variable lives in \p Reg (suitably modified by \p Expr).
184 MachineInstrBuilder buildDirectDbgValue(unsigned Reg, const MDNode *Variable,
185 const MDNode *Expr);
186
187 /// Build and insert a DBG_VALUE instruction expressing the fact that the
188 /// associated \p Variable lives in memory at \p Reg (suitably modified by \p
189 /// Expr).
190 MachineInstrBuilder buildIndirectDbgValue(unsigned Reg,
191 const MDNode *Variable,
192 const MDNode *Expr);
193
194 /// Build and insert a DBG_VALUE instruction expressing the fact that the
195 /// associated \p Variable lives in the stack slot specified by \p FI
196 /// (suitably modified by \p Expr).
197 MachineInstrBuilder buildFIDbgValue(int FI, const MDNode *Variable,
198 const MDNode *Expr);
199
200 /// Build and insert a DBG_VALUE instructions specifying that \p Variable is
201 /// given by \p C (suitably modified by \p Expr).
202 MachineInstrBuilder buildConstDbgValue(const Constant &C,
203 const MDNode *Variable,
204 const MDNode *Expr);
205
206 /// Build and insert \p Res = G_FRAME_INDEX \p Idx
207 ///
208 /// G_FRAME_INDEX materializes the address of an alloca value or other
209 /// stack-based object.
210 ///
211 /// \pre setBasicBlock or setMI must have been called.
212 /// \pre \p Res must be a generic virtual register with pointer type.
213 ///
214 /// \return a MachineInstrBuilder for the newly created instruction.
215 MachineInstrBuilder buildFrameIndex(unsigned Res, int Idx);
216
217 /// Build and insert \p Res = G_GLOBAL_VALUE \p GV
218 ///
219 /// G_GLOBAL_VALUE materializes the address of the specified global
220 /// into \p Res.
221 ///
222 /// \pre setBasicBlock or setMI must have been called.
223 /// \pre \p Res must be a generic virtual register with pointer type
224 /// in the same address space as \p GV.
225 ///
226 /// \return a MachineInstrBuilder for the newly created instruction.
227 MachineInstrBuilder buildGlobalValue(unsigned Res, const GlobalValue *GV);
228
229 /// Build and insert \p Res = G_ADD \p Op0, \p Op1
230 ///
231 /// G_ADD sets \p Res to the sum of integer parameters \p Op0 and \p Op1,
232 /// truncated to their width.
233 ///
234 /// \pre setBasicBlock or setMI must have been called.
235 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
236 /// with the same (scalar or vector) type).
237 ///
238 /// \return a MachineInstrBuilder for the newly created instruction.
239 MachineInstrBuilder buildAdd(unsigned Res, unsigned Op0,
240 unsigned Op1);
241 template <typename DstTy, typename... UseArgsTy>
242 MachineInstrBuilder buildAdd(DstTy &&Ty, UseArgsTy &&... UseArgs) {
243 unsigned Res = getDestFromArg(Ty);
244 return buildAdd(Res, (getRegFromArg(UseArgs))...);
245 }
246
247 /// Build and insert \p Res = G_SUB \p Op0, \p Op1
248 ///
249 /// G_SUB sets \p Res to the sum of integer parameters \p Op0 and \p Op1,
250 /// truncated to their width.
251 ///
252 /// \pre setBasicBlock or setMI must have been called.
253 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
254 /// with the same (scalar or vector) type).
255 ///
256 /// \return a MachineInstrBuilder for the newly created instruction.
257 template <typename DstTy, typename... UseArgsTy>
258 MachineInstrBuilder buildSub(DstTy &&Ty, UseArgsTy &&... UseArgs) {
259 unsigned Res = getDestFromArg(Ty);
260 return buildSub(Res, (getRegFromArg(UseArgs))...);
261 }
262 MachineInstrBuilder buildSub(unsigned Res, unsigned Op0,
263 unsigned Op1);
264
265 /// Build and insert \p Res = G_MUL \p Op0, \p Op1
266 ///
267 /// G_MUL sets \p Res to the sum of integer parameters \p Op0 and \p Op1,
268 /// truncated to their width.
269 ///
270 /// \pre setBasicBlock or setMI must have been called.
271 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
272 /// with the same (scalar or vector) type).
273 ///
274 /// \return a MachineInstrBuilder for the newly created instruction.
275 template <typename DstTy, typename... UseArgsTy>
276 MachineInstrBuilder buildMul(DstTy &&Ty, UseArgsTy &&... UseArgs) {
277 unsigned Res = getDestFromArg(Ty);
278 return buildMul(Res, (getRegFromArg(UseArgs))...);
279 }
280 MachineInstrBuilder buildMul(unsigned Res, unsigned Op0,
281 unsigned Op1);
282
283 /// Build and insert \p Res = G_GEP \p Op0, \p Op1
284 ///
285 /// G_GEP adds \p Op1 bytes to the pointer specified by \p Op0,
286 /// storing the resulting pointer in \p Res.
287 ///
288 /// \pre setBasicBlock or setMI must have been called.
289 /// \pre \p Res and \p Op0 must be generic virtual registers with pointer
290 /// type.
291 /// \pre \p Op1 must be a generic virtual register with scalar type.
292 ///
293 /// \return a MachineInstrBuilder for the newly created instruction.
294 MachineInstrBuilder buildGEP(unsigned Res, unsigned Op0,
295 unsigned Op1);
296
297 /// Materialize and insert \p Res = G_GEP \p Op0, (G_CONSTANT \p Value)
298 ///
299 /// G_GEP adds \p Value bytes to the pointer specified by \p Op0,
300 /// storing the resulting pointer in \p Res. If \p Value is zero then no
301 /// G_GEP or G_CONSTANT will be created and \pre Op0 will be assigned to
302 /// \p Res.
303 ///
304 /// \pre setBasicBlock or setMI must have been called.
305 /// \pre \p Op0 must be a generic virtual register with pointer type.
306 /// \pre \p ValueTy must be a scalar type.
307 /// \pre \p Res must be 0. This is to detect confusion between
308 /// materializeGEP() and buildGEP().
309 /// \post \p Res will either be a new generic virtual register of the same
310 /// type as \p Op0 or \p Op0 itself.
311 ///
312 /// \return a MachineInstrBuilder for the newly created instruction.
313 Optional<MachineInstrBuilder> materializeGEP(unsigned &Res, unsigned Op0,
314 const LLT &ValueTy,
315 uint64_t Value);
316
317 /// Build and insert \p Res = G_PTR_MASK \p Op0, \p NumBits
318 ///
319 /// G_PTR_MASK clears the low bits of a pointer operand without destroying its
320 /// pointer properties. This has the effect of rounding the address *down* to
321 /// a specified alignment in bits.
322 ///
323 /// \pre setBasicBlock or setMI must have been called.
324 /// \pre \p Res and \p Op0 must be generic virtual registers with pointer
325 /// type.
326 /// \pre \p NumBits must be an integer representing the number of low bits to
327 /// be cleared in \p Op0.
328 ///
329 /// \return a MachineInstrBuilder for the newly created instruction.
330 MachineInstrBuilder buildPtrMask(unsigned Res, unsigned Op0,
331 uint32_t NumBits);
332
333 /// Build and insert \p Res, \p CarryOut = G_UADDE \p Op0,
334 /// \p Op1, \p CarryIn
335 ///
336 /// G_UADDE sets \p Res to \p Op0 + \p Op1 + \p CarryIn (truncated to the bit
337 /// width) and sets \p CarryOut to 1 if the result overflowed in unsigned
338 /// arithmetic.
339 ///
340 /// \pre setBasicBlock or setMI must have been called.
341 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
342 /// with the same scalar type.
343 /// \pre \p CarryOut and \p CarryIn must be generic virtual
344 /// registers with the same scalar type (typically s1)
345 ///
346 /// \return The newly created instruction.
347 MachineInstrBuilder buildUAdde(unsigned Res, unsigned CarryOut, unsigned Op0,
348 unsigned Op1, unsigned CarryIn);
349
350 /// Build and insert \p Res = G_AND \p Op0, \p Op1
351 ///
352 /// G_AND sets \p Res to the bitwise and of integer parameters \p Op0 and \p
353 /// Op1.
354 ///
355 /// \pre setBasicBlock or setMI must have been called.
356 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
357 /// with the same (scalar or vector) type).
358 ///
359 /// \return a MachineInstrBuilder for the newly created instruction.
360 template <typename DstTy, typename... UseArgsTy>
361 MachineInstrBuilder buildAnd(DstTy &&Dst, UseArgsTy &&... UseArgs) {
362 return buildAnd(getDestFromArg(Dst), getRegFromArg(UseArgs)...);
363 }
364 MachineInstrBuilder buildAnd(unsigned Res, unsigned Op0,
365 unsigned Op1);
366
367 /// Build and insert \p Res = G_OR \p Op0, \p Op1
368 ///
369 /// G_OR sets \p Res to the bitwise or of integer parameters \p Op0 and \p
370 /// Op1.
371 ///
372 /// \pre setBasicBlock or setMI must have been called.
373 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
374 /// with the same (scalar or vector) type).
375 ///
376 /// \return a MachineInstrBuilder for the newly created instruction.
377 template <typename DstTy, typename... UseArgsTy>
378 MachineInstrBuilder buildOr(DstTy &&Dst, UseArgsTy &&... UseArgs) {
379 return buildOr(getDestFromArg(Dst), getRegFromArg(UseArgs)...);
380 }
381 MachineInstrBuilder buildOr(unsigned Res, unsigned Op0, unsigned Op1);
382
383 /// Build and insert \p Res = G_ANYEXT \p Op0
384 ///
385 /// G_ANYEXT produces a register of the specified width, with bits 0 to
386 /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are unspecified
387 /// (i.e. this is neither zero nor sign-extension). For a vector register,
388 /// each element is extended individually.
389 ///
390 /// \pre setBasicBlock or setMI must have been called.
391 /// \pre \p Res must be a generic virtual register with scalar or vector type.
392 /// \pre \p Op must be a generic virtual register with scalar or vector type.
393 /// \pre \p Op must be smaller than \p Res
394 ///
395 /// \return The newly created instruction.
396
397 MachineInstrBuilder buildAnyExt(unsigned Res, unsigned Op);
398 template <typename DstType, typename ArgType>
399 MachineInstrBuilder buildAnyExt(DstType &&Res, ArgType &&Arg) {
400 return buildAnyExt(getDestFromArg(Res), getRegFromArg(Arg));
401 }
402
403 /// Build and insert \p Res = G_SEXT \p Op
404 ///
405 /// G_SEXT produces a register of the specified width, with bits 0 to
406 /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are duplicated from the
407 /// high bit of \p Op (i.e. 2s-complement sign extended).
408 ///
409 /// \pre setBasicBlock or setMI must have been called.
410 /// \pre \p Res must be a generic virtual register with scalar or vector type.
411 /// \pre \p Op must be a generic virtual register with scalar or vector type.
412 /// \pre \p Op must be smaller than \p Res
413 ///
414 /// \return The newly created instruction.
415 template <typename DstType, typename ArgType>
416 MachineInstrBuilder buildSExt(DstType &&Res, ArgType &&Arg) {
417 return buildSExt(getDestFromArg(Res), getRegFromArg(Arg));
418 }
419 MachineInstrBuilder buildSExt(unsigned Res, unsigned Op);
420
421 /// Build and insert \p Res = G_ZEXT \p Op
422 ///
423 /// G_ZEXT produces a register of the specified width, with bits 0 to
424 /// sizeof(\p Ty) * 8 set to \p Op. The remaining bits are 0. For a vector
425 /// register, each element is extended individually.
426 ///
427 /// \pre setBasicBlock or setMI must have been called.
428 /// \pre \p Res must be a generic virtual register with scalar or vector type.
429 /// \pre \p Op must be a generic virtual register with scalar or vector type.
430 /// \pre \p Op must be smaller than \p Res
431 ///
432 /// \return The newly created instruction.
433 template <typename DstType, typename ArgType>
434 MachineInstrBuilder buildZExt(DstType &&Res, ArgType &&Arg) {
435 return buildZExt(getDestFromArg(Res), getRegFromArg(Arg));
436 }
437 MachineInstrBuilder buildZExt(unsigned Res, unsigned Op);
438
439 /// Build and insert \p Res = G_SEXT \p Op, \p Res = G_TRUNC \p Op, or
440 /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
441 /// ///
442 /// \pre setBasicBlock or setMI must have been called.
443 /// \pre \p Res must be a generic virtual register with scalar or vector type.
444 /// \pre \p Op must be a generic virtual register with scalar or vector type.
445 ///
446 /// \return The newly created instruction.
447 template <typename DstTy, typename UseArgTy>
448 MachineInstrBuilder buildSExtOrTrunc(DstTy &&Dst, UseArgTy &&Use) {
449 return buildSExtOrTrunc(getDestFromArg(Dst), getRegFromArg(Use));
450 }
451 MachineInstrBuilder buildSExtOrTrunc(unsigned Res, unsigned Op);
452
453 /// Build and insert \p Res = G_ZEXT \p Op, \p Res = G_TRUNC \p Op, or
454 /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
455 /// ///
456 /// \pre setBasicBlock or setMI must have been called.
457 /// \pre \p Res must be a generic virtual register with scalar or vector type.
458 /// \pre \p Op must be a generic virtual register with scalar or vector type.
459 ///
460 /// \return The newly created instruction.
461 template <typename DstTy, typename UseArgTy>
462 MachineInstrBuilder buildZExtOrTrunc(DstTy &&Dst, UseArgTy &&Use) {
463 return buildZExtOrTrunc(getDestFromArg(Dst), getRegFromArg(Use));
464 }
465 MachineInstrBuilder buildZExtOrTrunc(unsigned Res, unsigned Op);
466
467 // Build and insert \p Res = G_ANYEXT \p Op, \p Res = G_TRUNC \p Op, or
468 /// \p Res = COPY \p Op depending on the differing sizes of \p Res and \p Op.
469 /// ///
470 /// \pre setBasicBlock or setMI must have been called.
471 /// \pre \p Res must be a generic virtual register with scalar or vector type.
472 /// \pre \p Op must be a generic virtual register with scalar or vector type.
473 ///
474 /// \return The newly created instruction.
475 template <typename DstTy, typename UseArgTy>
476 MachineInstrBuilder buildAnyExtOrTrunc(DstTy &&Dst, UseArgTy &&Use) {
477 return buildAnyExtOrTrunc(getDestFromArg(Dst), getRegFromArg(Use));
478 }
479 MachineInstrBuilder buildAnyExtOrTrunc(unsigned Res, unsigned Op);
480
481 /// Build and insert \p Res = \p ExtOpc, \p Res = G_TRUNC \p
482 /// Op, or \p Res = COPY \p Op depending on the differing sizes of \p Res and
483 /// \p Op.
484 /// ///
485 /// \pre setBasicBlock or setMI must have been called.
486 /// \pre \p Res must be a generic virtual register with scalar or vector type.
487 /// \pre \p Op must be a generic virtual register with scalar or vector type.
488 ///
489 /// \return The newly created instruction.
490 MachineInstrBuilder buildExtOrTrunc(unsigned ExtOpc, unsigned Res,
491 unsigned Op);
492
493 /// Build and insert an appropriate cast between two registers of equal size.
494 template <typename DstType, typename ArgType>
495 MachineInstrBuilder buildCast(DstType &&Res, ArgType &&Arg) {
496 return buildCast(getDestFromArg(Res), getRegFromArg(Arg));
497 }
498 MachineInstrBuilder buildCast(unsigned Dst, unsigned Src);
499
500 /// Build and insert G_BR \p Dest
501 ///
502 /// G_BR is an unconditional branch to \p Dest.
503 ///
504 /// \pre setBasicBlock or setMI must have been called.
505 ///
506 /// \return a MachineInstrBuilder for the newly created instruction.
507 MachineInstrBuilder buildBr(MachineBasicBlock &BB);
508
509 /// Build and insert G_BRCOND \p Tst, \p Dest
510 ///
511 /// G_BRCOND is a conditional branch to \p Dest.
512 ///
513 /// \pre setBasicBlock or setMI must have been called.
514 /// \pre \p Tst must be a generic virtual register with scalar
515 /// type. At the beginning of legalization, this will be a single
516 /// bit (s1). Targets with interesting flags registers may change
517 /// this. For a wider type, whether the branch is taken must only
518 /// depend on bit 0 (for now).
519 ///
520 /// \return The newly created instruction.
521 MachineInstrBuilder buildBrCond(unsigned Tst, MachineBasicBlock &BB);
522
523 /// Build and insert G_BRINDIRECT \p Tgt
524 ///
525 /// G_BRINDIRECT is an indirect branch to \p Tgt.
526 ///
527 /// \pre setBasicBlock or setMI must have been called.
528 /// \pre \p Tgt must be a generic virtual register with pointer type.
529 ///
530 /// \return a MachineInstrBuilder for the newly created instruction.
531 MachineInstrBuilder buildBrIndirect(unsigned Tgt);
532
533 /// Build and insert \p Res = G_CONSTANT \p Val
534 ///
535 /// G_CONSTANT is an integer constant with the specified size and value. \p
536 /// Val will be extended or truncated to the size of \p Reg.
537 ///
538 /// \pre setBasicBlock or setMI must have been called.
539 /// \pre \p Res must be a generic virtual register with scalar or pointer
540 /// type.
541 ///
542 /// \return The newly created instruction.
543 MachineInstrBuilder buildConstant(unsigned Res, const ConstantInt &Val);
544
545 /// Build and insert \p Res = G_CONSTANT \p Val
546 ///
547 /// G_CONSTANT is an integer constant with the specified size and value.
548 ///
549 /// \pre setBasicBlock or setMI must have been called.
550 /// \pre \p Res must be a generic virtual register with scalar type.
551 ///
552 /// \return The newly created instruction.
553 MachineInstrBuilder buildConstant(unsigned Res, int64_t Val);
554
555 template <typename DstType>
556 MachineInstrBuilder buildConstant(DstType &&Res, int64_t Val) {
557 return buildConstant(getDestFromArg(Res), Val);
558 }
559 /// Build and insert \p Res = G_FCONSTANT \p Val
560 ///
561 /// G_FCONSTANT is a floating-point constant with the specified size and
562 /// value.
563 ///
564 /// \pre setBasicBlock or setMI must have been called.
565 /// \pre \p Res must be a generic virtual register with scalar type.
566 ///
567 /// \return The newly created instruction.
568 template <typename DstType>
569 MachineInstrBuilder buildFConstant(DstType &&Res, const ConstantFP &Val) {
570 return buildFConstant(getDestFromArg(Res), Val);
571 }
572 MachineInstrBuilder buildFConstant(unsigned Res, const ConstantFP &Val);
573
574 template <typename DstType>
575 MachineInstrBuilder buildFConstant(DstType &&Res, double Val) {
576 return buildFConstant(getDestFromArg(Res), Val);
577 }
578 MachineInstrBuilder buildFConstant(unsigned Res, double Val);
579
580 /// Build and insert \p Res = COPY Op
581 ///
582 /// Register-to-register COPY sets \p Res to \p Op.
583 ///
584 /// \pre setBasicBlock or setMI must have been called.
585 ///
586 /// \return a MachineInstrBuilder for the newly created instruction.
587 MachineInstrBuilder buildCopy(unsigned Res, unsigned Op);
588 template <typename DstType, typename SrcType>
589 MachineInstrBuilder buildCopy(DstType &&Res, SrcType &&Src) {
590 return buildCopy(getDestFromArg(Res), getRegFromArg(Src));
591 }
592
593 /// Build and insert `Res = G_LOAD Addr, MMO`.
594 ///
595 /// Loads the value stored at \p Addr. Puts the result in \p Res.
596 ///
597 /// \pre setBasicBlock or setMI must have been called.
598 /// \pre \p Res must be a generic virtual register.
599 /// \pre \p Addr must be a generic virtual register with pointer type.
600 ///
601 /// \return a MachineInstrBuilder for the newly created instruction.
602 MachineInstrBuilder buildLoad(unsigned Res, unsigned Addr,
603 MachineMemOperand &MMO);
604
605 /// Build and insert `G_STORE Val, Addr, MMO`.
606 ///
607 /// Stores the value \p Val to \p Addr.
608 ///
609 /// \pre setBasicBlock or setMI must have been called.
610 /// \pre \p Val must be a generic virtual register.
611 /// \pre \p Addr must be a generic virtual register with pointer type.
612 ///
613 /// \return a MachineInstrBuilder for the newly created instruction.
614 MachineInstrBuilder buildStore(unsigned Val, unsigned Addr,
615 MachineMemOperand &MMO);
616
617 /// Build and insert `Res0, ... = G_EXTRACT Src, Idx0`.
618 ///
619 /// \pre setBasicBlock or setMI must have been called.
620 /// \pre \p Res and \p Src must be generic virtual registers.
621 ///
622 /// \return a MachineInstrBuilder for the newly created instruction.
623 MachineInstrBuilder buildExtract(unsigned Res, unsigned Src, uint64_t Index);
624
625 /// Build and insert \p Res = IMPLICIT_DEF.
626 template <typename DstType> MachineInstrBuilder buildUndef(DstType &&Res) {
627 return buildUndef(getDestFromArg(Res));
628 }
629 MachineInstrBuilder buildUndef(unsigned Dst);
630
631 /// Build and insert instructions to put \p Ops together at the specified p
632 /// Indices to form a larger register.
633 ///
634 /// If the types of the input registers are uniform and cover the entirity of
635 /// \p Res then a G_MERGE_VALUES will be produced. Otherwise an IMPLICIT_DEF
636 /// followed by a sequence of G_INSERT instructions.
637 ///
638 /// \pre setBasicBlock or setMI must have been called.
639 /// \pre The final element of the sequence must not extend past the end of the
640 /// destination register.
641 /// \pre The bits defined by each Op (derived from index and scalar size) must
642 /// not overlap.
643 /// \pre \p Indices must be in ascending order of bit position.
644 void buildSequence(unsigned Res, ArrayRef<unsigned> Ops,
645 ArrayRef<uint64_t> Indices);
646
647 /// Build and insert \p Res = G_MERGE_VALUES \p Op0, ...
648 ///
649 /// G_MERGE_VALUES combines the input elements contiguously into a larger
650 /// register.
651 ///
652 /// \pre setBasicBlock or setMI must have been called.
653 /// \pre The entire register \p Res (and no more) must be covered by the input
654 /// registers.
655 /// \pre The type of all \p Ops registers must be identical.
656 ///
657 /// \return a MachineInstrBuilder for the newly created instruction.
658 MachineInstrBuilder buildMerge(unsigned Res, ArrayRef<unsigned> Ops);
659
660 /// Build and insert \p Res0, ... = G_UNMERGE_VALUES \p Op
661 ///
662 /// G_UNMERGE_VALUES splits contiguous bits of the input into multiple
663 ///
664 /// \pre setBasicBlock or setMI must have been called.
665 /// \pre The entire register \p Res (and no more) must be covered by the input
666 /// registers.
667 /// \pre The type of all \p Res registers must be identical.
668 ///
669 /// \return a MachineInstrBuilder for the newly created instruction.
670 MachineInstrBuilder buildUnmerge(ArrayRef<unsigned> Res, unsigned Op);
671
672 MachineInstrBuilder buildInsert(unsigned Res, unsigned Src,
673 unsigned Op, unsigned Index);
674
675 /// Build and insert either a G_INTRINSIC (if \p HasSideEffects is false) or
676 /// G_INTRINSIC_W_SIDE_EFFECTS instruction. Its first operand will be the
677 /// result register definition unless \p Reg is NoReg (== 0). The second
678 /// operand will be the intrinsic's ID.
679 ///
680 /// Callers are expected to add the required definitions and uses afterwards.
681 ///
682 /// \pre setBasicBlock or setMI must have been called.
683 ///
684 /// \return a MachineInstrBuilder for the newly created instruction.
685 MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, unsigned Res,
686 bool HasSideEffects);
687
688 /// Build and insert \p Res = G_FPTRUNC \p Op
689 ///
690 /// G_FPTRUNC converts a floating-point value into one with a smaller type.
691 ///
692 /// \pre setBasicBlock or setMI must have been called.
693 /// \pre \p Res must be a generic virtual register with scalar or vector type.
694 /// \pre \p Op must be a generic virtual register with scalar or vector type.
695 /// \pre \p Res must be smaller than \p Op
696 ///
697 /// \return The newly created instruction.
698 template <typename DstType, typename SrcType>
699 MachineInstrBuilder buildFPTrunc(DstType &&Res, SrcType &&Src) {
700 return buildFPTrunc(getDestFromArg(Res), getRegFromArg(Src));
701 }
702 MachineInstrBuilder buildFPTrunc(unsigned Res, unsigned Op);
703
704 /// Build and insert \p Res = G_TRUNC \p Op
705 ///
706 /// G_TRUNC extracts the low bits of a type. For a vector type each element is
707 /// truncated independently before being packed into the destination.
708 ///
709 /// \pre setBasicBlock or setMI must have been called.
710 /// \pre \p Res must be a generic virtual register with scalar or vector type.
711 /// \pre \p Op must be a generic virtual register with scalar or vector type.
712 /// \pre \p Res must be smaller than \p Op
713 ///
714 /// \return The newly created instruction.
715 MachineInstrBuilder buildTrunc(unsigned Res, unsigned Op);
716 template <typename DstType, typename SrcType>
717 MachineInstrBuilder buildTrunc(DstType &&Res, SrcType &&Src) {
718 return buildTrunc(getDestFromArg(Res), getRegFromArg(Src));
719 }
720
721 /// Build and insert a \p Res = G_ICMP \p Pred, \p Op0, \p Op1
722 ///
723 /// \pre setBasicBlock or setMI must have been called.
724
725 /// \pre \p Res must be a generic virtual register with scalar or
726 /// vector type. Typically this starts as s1 or <N x s1>.
727 /// \pre \p Op0 and Op1 must be generic virtual registers with the
728 /// same number of elements as \p Res. If \p Res is a scalar,
729 /// \p Op0 must be either a scalar or pointer.
730 /// \pre \p Pred must be an integer predicate.
731 ///
732 /// \return a MachineInstrBuilder for the newly created instruction.
733 MachineInstrBuilder buildICmp(CmpInst::Predicate Pred,
734 unsigned Res, unsigned Op0, unsigned Op1);
735
736 /// Build and insert a \p Res = G_FCMP \p Pred\p Op0, \p Op1
737 ///
738 /// \pre setBasicBlock or setMI must have been called.
739
740 /// \pre \p Res must be a generic virtual register with scalar or
741 /// vector type. Typically this starts as s1 or <N x s1>.
742 /// \pre \p Op0 and Op1 must be generic virtual registers with the
743 /// same number of elements as \p Res (or scalar, if \p Res is
744 /// scalar).
745 /// \pre \p Pred must be a floating-point predicate.
746 ///
747 /// \return a MachineInstrBuilder for the newly created instruction.
748 MachineInstrBuilder buildFCmp(CmpInst::Predicate Pred,
749 unsigned Res, unsigned Op0, unsigned Op1);
750
751 /// Build and insert a \p Res = G_SELECT \p Tst, \p Op0, \p Op1
752 ///
753 /// \pre setBasicBlock or setMI must have been called.
754 /// \pre \p Res, \p Op0 and \p Op1 must be generic virtual registers
755 /// with the same type.
756 /// \pre \p Tst must be a generic virtual register with scalar, pointer or
757 /// vector type. If vector then it must have the same number of
758 /// elements as the other parameters.
759 ///
760 /// \return a MachineInstrBuilder for the newly created instruction.
761 MachineInstrBuilder buildSelect(unsigned Res, unsigned Tst,
762 unsigned Op0, unsigned Op1);
763
764 /// Build and insert \p Res = G_INSERT_VECTOR_ELT \p Val,
765 /// \p Elt, \p Idx
766 ///
767 /// \pre setBasicBlock or setMI must have been called.
768 /// \pre \p Res and \p Val must be a generic virtual register
769 // with the same vector type.
770 /// \pre \p Elt and \p Idx must be a generic virtual register
771 /// with scalar type.
772 ///
773 /// \return The newly created instruction.
774 MachineInstrBuilder buildInsertVectorElement(unsigned Res, unsigned Val,
775 unsigned Elt, unsigned Idx);
776
777 /// Build and insert \p Res = G_EXTRACT_VECTOR_ELT \p Val, \p Idx
778 ///
779 /// \pre setBasicBlock or setMI must have been called.
780 /// \pre \p Res must be a generic virtual register with scalar type.
781 /// \pre \p Val must be a generic virtual register with vector type.
782 /// \pre \p Idx must be a generic virtual register with scalar type.
783 ///
784 /// \return The newly created instruction.
785 MachineInstrBuilder buildExtractVectorElement(unsigned Res, unsigned Val,
786 unsigned Idx);
787
788 /// Build and insert `OldValRes = G_ATOMIC_CMPXCHG Addr, CmpVal, NewVal,
789 /// MMO`.
790 ///
791 /// Atomically replace the value at \p Addr with \p NewVal if it is currently
792 /// \p CmpVal otherwise leaves it unchanged. Puts the original value from \p
793 /// Addr in \p Res.
794 ///
795 /// \pre setBasicBlock or setMI must have been called.
796 /// \pre \p OldValRes must be a generic virtual register of scalar type.
797 /// \pre \p Addr must be a generic virtual register with pointer type.
798 /// \pre \p OldValRes, \p CmpVal, and \p NewVal must be generic virtual
799 /// registers of the same type.
800 ///
801 /// \return a MachineInstrBuilder for the newly created instruction.
802 MachineInstrBuilder buildAtomicCmpXchg(unsigned OldValRes, unsigned Addr,
803 unsigned CmpVal, unsigned NewVal,
804 MachineMemOperand &MMO);
805};
806
807} // End namespace llvm.
808#endif // LLVM_CODEGEN_GLOBALISEL_MACHINEIRBUILDER_H