blob: 115c501756043715525e7909a58da56bd18494b7 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- CodeGen/MachineInstrBuilder.h - Simplify creation of MIs --*- 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//
9// This file exposes a function named BuildMI, which is useful for dramatically
10// simplifying how MachineInstr's are created. It allows use of code like this:
11//
12// M = BuildMI(MBB, MI, DL, TII.get(X86::ADD8rr), Dst)
13// .addReg(argVal1)
14// .addReg(argVal2);
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_CODEGEN_MACHINEINSTRBUILDER_H
19#define LLVM_CODEGEN_MACHINEINSTRBUILDER_H
20
21#include "llvm/ADT/ArrayRef.h"
22#include "llvm/CodeGen/GlobalISel/Utils.h"
23#include "llvm/CodeGen/MachineBasicBlock.h"
24#include "llvm/CodeGen/MachineFunction.h"
25#include "llvm/CodeGen/MachineInstr.h"
26#include "llvm/CodeGen/MachineInstrBundle.h"
27#include "llvm/CodeGen/MachineOperand.h"
28#include "llvm/CodeGen/TargetRegisterInfo.h"
29#include "llvm/IR/InstrTypes.h"
30#include "llvm/IR/Intrinsics.h"
31#include "llvm/Support/ErrorHandling.h"
32#include <cassert>
33#include <cstdint>
34#include <utility>
35
36namespace llvm {
37
38class MCInstrDesc;
39class MDNode;
40
41namespace RegState {
42
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020043enum {
44 /// Register definition.
45 Define = 0x2,
46 /// Not emitted register (e.g. carry, or temporary result).
47 Implicit = 0x4,
48 /// The last use of a register.
49 Kill = 0x8,
50 /// Unused definition.
51 Dead = 0x10,
52 /// Value of the register doesn't matter.
53 Undef = 0x20,
54 /// Register definition happens before uses.
55 EarlyClobber = 0x40,
56 /// Register 'use' is for debugging purpose.
57 Debug = 0x80,
58 /// Register reads a value that is defined inside the same instruction or
59 /// bundle.
60 InternalRead = 0x100,
61 /// Register that may be renamed.
62 Renamable = 0x200,
63 DefineNoRead = Define | Undef,
64 ImplicitDefine = Implicit | Define,
65 ImplicitKill = Implicit | Kill
66};
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010067
68} // end namespace RegState
69
70class MachineInstrBuilder {
71 MachineFunction *MF = nullptr;
72 MachineInstr *MI = nullptr;
73
74public:
75 MachineInstrBuilder() = default;
76
77 /// Create a MachineInstrBuilder for manipulating an existing instruction.
78 /// F must be the machine function that was used to allocate I.
79 MachineInstrBuilder(MachineFunction &F, MachineInstr *I) : MF(&F), MI(I) {}
80 MachineInstrBuilder(MachineFunction &F, MachineBasicBlock::iterator I)
81 : MF(&F), MI(&*I) {}
82
83 /// Allow automatic conversion to the machine instruction we are working on.
84 operator MachineInstr*() const { return MI; }
85 MachineInstr *operator->() const { return MI; }
86 operator MachineBasicBlock::iterator() const { return MI; }
87
88 /// If conversion operators fail, use this method to get the MachineInstr
89 /// explicitly.
90 MachineInstr *getInstr() const { return MI; }
91
Andrew Walbran16937d02019-10-22 13:54:20 +010092 /// Get the register for the operand index.
93 /// The operand at the index should be a register (asserted by
94 /// MachineOperand).
Andrew Walbran3d2c1972020-04-07 12:24:26 +010095 Register getReg(unsigned Idx) const { return MI->getOperand(Idx).getReg(); }
Andrew Walbran16937d02019-10-22 13:54:20 +010096
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010097 /// Add a new virtual register operand.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020098 const MachineInstrBuilder &addReg(Register RegNo, unsigned flags = 0,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010099 unsigned SubReg = 0) const {
100 assert((flags & 0x1) == 0 &&
101 "Passing in 'true' to addReg is forbidden! Use enums instead.");
102 MI->addOperand(*MF, MachineOperand::CreateReg(RegNo,
103 flags & RegState::Define,
104 flags & RegState::Implicit,
105 flags & RegState::Kill,
106 flags & RegState::Dead,
107 flags & RegState::Undef,
108 flags & RegState::EarlyClobber,
109 SubReg,
110 flags & RegState::Debug,
111 flags & RegState::InternalRead,
112 flags & RegState::Renamable));
113 return *this;
114 }
115
116 /// Add a virtual register definition operand.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200117 const MachineInstrBuilder &addDef(Register RegNo, unsigned Flags = 0,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100118 unsigned SubReg = 0) const {
119 return addReg(RegNo, Flags | RegState::Define, SubReg);
120 }
121
122 /// Add a virtual register use operand. It is an error for Flags to contain
123 /// `RegState::Define` when calling this function.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200124 const MachineInstrBuilder &addUse(Register RegNo, unsigned Flags = 0,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100125 unsigned SubReg = 0) const {
126 assert(!(Flags & RegState::Define) &&
127 "Misleading addUse defines register, use addReg instead.");
128 return addReg(RegNo, Flags, SubReg);
129 }
130
131 /// Add a new immediate operand.
132 const MachineInstrBuilder &addImm(int64_t Val) const {
133 MI->addOperand(*MF, MachineOperand::CreateImm(Val));
134 return *this;
135 }
136
137 const MachineInstrBuilder &addCImm(const ConstantInt *Val) const {
138 MI->addOperand(*MF, MachineOperand::CreateCImm(Val));
139 return *this;
140 }
141
142 const MachineInstrBuilder &addFPImm(const ConstantFP *Val) const {
143 MI->addOperand(*MF, MachineOperand::CreateFPImm(Val));
144 return *this;
145 }
146
147 const MachineInstrBuilder &addMBB(MachineBasicBlock *MBB,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200148 unsigned TargetFlags = 0) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100149 MI->addOperand(*MF, MachineOperand::CreateMBB(MBB, TargetFlags));
150 return *this;
151 }
152
153 const MachineInstrBuilder &addFrameIndex(int Idx) const {
154 MI->addOperand(*MF, MachineOperand::CreateFI(Idx));
155 return *this;
156 }
157
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200158 const MachineInstrBuilder &
159 addConstantPoolIndex(unsigned Idx, int Offset = 0,
160 unsigned TargetFlags = 0) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100161 MI->addOperand(*MF, MachineOperand::CreateCPI(Idx, Offset, TargetFlags));
162 return *this;
163 }
164
165 const MachineInstrBuilder &addTargetIndex(unsigned Idx, int64_t Offset = 0,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200166 unsigned TargetFlags = 0) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100167 MI->addOperand(*MF, MachineOperand::CreateTargetIndex(Idx, Offset,
168 TargetFlags));
169 return *this;
170 }
171
172 const MachineInstrBuilder &addJumpTableIndex(unsigned Idx,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200173 unsigned TargetFlags = 0) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100174 MI->addOperand(*MF, MachineOperand::CreateJTI(Idx, TargetFlags));
175 return *this;
176 }
177
178 const MachineInstrBuilder &addGlobalAddress(const GlobalValue *GV,
179 int64_t Offset = 0,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200180 unsigned TargetFlags = 0) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100181 MI->addOperand(*MF, MachineOperand::CreateGA(GV, Offset, TargetFlags));
182 return *this;
183 }
184
185 const MachineInstrBuilder &addExternalSymbol(const char *FnName,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200186 unsigned TargetFlags = 0) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100187 MI->addOperand(*MF, MachineOperand::CreateES(FnName, TargetFlags));
188 return *this;
189 }
190
191 const MachineInstrBuilder &addBlockAddress(const BlockAddress *BA,
192 int64_t Offset = 0,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200193 unsigned TargetFlags = 0) const {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100194 MI->addOperand(*MF, MachineOperand::CreateBA(BA, Offset, TargetFlags));
195 return *this;
196 }
197
198 const MachineInstrBuilder &addRegMask(const uint32_t *Mask) const {
199 MI->addOperand(*MF, MachineOperand::CreateRegMask(Mask));
200 return *this;
201 }
202
203 const MachineInstrBuilder &addMemOperand(MachineMemOperand *MMO) const {
204 MI->addMemOperand(*MF, MMO);
205 return *this;
206 }
207
Andrew Scull0372a572018-11-16 15:47:06 +0000208 const MachineInstrBuilder &
209 setMemRefs(ArrayRef<MachineMemOperand *> MMOs) const {
210 MI->setMemRefs(*MF, MMOs);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100211 return *this;
212 }
213
Andrew Scull0372a572018-11-16 15:47:06 +0000214 const MachineInstrBuilder &cloneMemRefs(const MachineInstr &OtherMI) const {
215 MI->cloneMemRefs(*MF, OtherMI);
216 return *this;
217 }
218
219 const MachineInstrBuilder &
220 cloneMergedMemRefs(ArrayRef<const MachineInstr *> OtherMIs) const {
221 MI->cloneMergedMemRefs(*MF, OtherMIs);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100222 return *this;
223 }
224
225 const MachineInstrBuilder &add(const MachineOperand &MO) const {
226 MI->addOperand(*MF, MO);
227 return *this;
228 }
229
230 const MachineInstrBuilder &add(ArrayRef<MachineOperand> MOs) const {
231 for (const MachineOperand &MO : MOs) {
232 MI->addOperand(*MF, MO);
233 }
234 return *this;
235 }
236
237 const MachineInstrBuilder &addMetadata(const MDNode *MD) const {
238 MI->addOperand(*MF, MachineOperand::CreateMetadata(MD));
239 assert((MI->isDebugValue() ? static_cast<bool>(MI->getDebugVariable())
240 : true) &&
241 "first MDNode argument of a DBG_VALUE not a variable");
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100242 assert((MI->isDebugLabel() ? static_cast<bool>(MI->getDebugLabel())
243 : true) &&
244 "first MDNode argument of a DBG_LABEL not a label");
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100245 return *this;
246 }
247
248 const MachineInstrBuilder &addCFIIndex(unsigned CFIIndex) const {
249 MI->addOperand(*MF, MachineOperand::CreateCFIIndex(CFIIndex));
250 return *this;
251 }
252
253 const MachineInstrBuilder &addIntrinsicID(Intrinsic::ID ID) const {
254 MI->addOperand(*MF, MachineOperand::CreateIntrinsicID(ID));
255 return *this;
256 }
257
258 const MachineInstrBuilder &addPredicate(CmpInst::Predicate Pred) const {
259 MI->addOperand(*MF, MachineOperand::CreatePredicate(Pred));
260 return *this;
261 }
262
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200263 const MachineInstrBuilder &addShuffleMask(ArrayRef<int> Val) const {
264 MI->addOperand(*MF, MachineOperand::CreateShuffleMask(Val));
265 return *this;
266 }
267
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100268 const MachineInstrBuilder &addSym(MCSymbol *Sym,
269 unsigned char TargetFlags = 0) const {
270 MI->addOperand(*MF, MachineOperand::CreateMCSymbol(Sym, TargetFlags));
271 return *this;
272 }
273
274 const MachineInstrBuilder &setMIFlags(unsigned Flags) const {
275 MI->setFlags(Flags);
276 return *this;
277 }
278
279 const MachineInstrBuilder &setMIFlag(MachineInstr::MIFlag Flag) const {
280 MI->setFlag(Flag);
281 return *this;
282 }
283
284 // Add a displacement from an existing MachineOperand with an added offset.
285 const MachineInstrBuilder &addDisp(const MachineOperand &Disp, int64_t off,
286 unsigned char TargetFlags = 0) const {
287 // If caller specifies new TargetFlags then use it, otherwise the
288 // default behavior is to copy the target flags from the existing
289 // MachineOperand. This means if the caller wants to clear the
290 // target flags it needs to do so explicitly.
291 if (0 == TargetFlags)
292 TargetFlags = Disp.getTargetFlags();
293
294 switch (Disp.getType()) {
295 default:
296 llvm_unreachable("Unhandled operand type in addDisp()");
297 case MachineOperand::MO_Immediate:
298 return addImm(Disp.getImm() + off);
299 case MachineOperand::MO_ConstantPoolIndex:
300 return addConstantPoolIndex(Disp.getIndex(), Disp.getOffset() + off,
301 TargetFlags);
302 case MachineOperand::MO_GlobalAddress:
303 return addGlobalAddress(Disp.getGlobal(), Disp.getOffset() + off,
304 TargetFlags);
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100305 case MachineOperand::MO_BlockAddress:
306 return addBlockAddress(Disp.getBlockAddress(), Disp.getOffset() + off,
307 TargetFlags);
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200308 case MachineOperand::MO_JumpTableIndex:
309 assert(off == 0 && "cannot create offset into jump tables");
310 return addJumpTableIndex(Disp.getIndex(), TargetFlags);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100311 }
312 }
313
314 /// Copy all the implicit operands from OtherMI onto this one.
315 const MachineInstrBuilder &
316 copyImplicitOps(const MachineInstr &OtherMI) const {
317 MI->copyImplicitOps(*MF, OtherMI);
318 return *this;
319 }
320
321 bool constrainAllUses(const TargetInstrInfo &TII,
322 const TargetRegisterInfo &TRI,
323 const RegisterBankInfo &RBI) const {
324 return constrainSelectedInstRegOperands(*MI, TII, TRI, RBI);
325 }
326};
327
328/// Builder interface. Specify how to create the initial instruction itself.
329inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
330 const MCInstrDesc &MCID) {
331 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL));
332}
333
334/// This version of the builder sets up the first operand as a
335/// destination virtual register.
336inline MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200337 const MCInstrDesc &MCID, Register DestReg) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100338 return MachineInstrBuilder(MF, MF.CreateMachineInstr(MCID, DL))
339 .addReg(DestReg, RegState::Define);
340}
341
342/// This version of the builder inserts the newly-built instruction before
343/// the given position in the given MachineBasicBlock, and sets up the first
344/// operand as a destination virtual register.
345inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
346 MachineBasicBlock::iterator I,
347 const DebugLoc &DL, const MCInstrDesc &MCID,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200348 Register DestReg) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100349 MachineFunction &MF = *BB.getParent();
350 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
351 BB.insert(I, MI);
352 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
353}
354
355/// This version of the builder inserts the newly-built instruction before
356/// the given position in the given MachineBasicBlock, and sets up the first
357/// operand as a destination virtual register.
358///
359/// If \c I is inside a bundle, then the newly inserted \a MachineInstr is
360/// added to the same bundle.
361inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
362 MachineBasicBlock::instr_iterator I,
363 const DebugLoc &DL, const MCInstrDesc &MCID,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200364 Register DestReg) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100365 MachineFunction &MF = *BB.getParent();
366 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
367 BB.insert(I, MI);
368 return MachineInstrBuilder(MF, MI).addReg(DestReg, RegState::Define);
369}
370
371inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
372 const DebugLoc &DL, const MCInstrDesc &MCID,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200373 Register DestReg) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100374 // Calling the overload for instr_iterator is always correct. However, the
375 // definition is not available in headers, so inline the check.
376 if (I.isInsideBundle())
377 return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID, DestReg);
378 return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID, DestReg);
379}
380
381inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
382 const DebugLoc &DL, const MCInstrDesc &MCID,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200383 Register DestReg) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100384 return BuildMI(BB, *I, DL, MCID, DestReg);
385}
386
387/// This version of the builder inserts the newly-built instruction before the
388/// given position in the given MachineBasicBlock, and does NOT take a
389/// destination register.
390inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
391 MachineBasicBlock::iterator I,
392 const DebugLoc &DL,
393 const MCInstrDesc &MCID) {
394 MachineFunction &MF = *BB.getParent();
395 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
396 BB.insert(I, MI);
397 return MachineInstrBuilder(MF, MI);
398}
399
400inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
401 MachineBasicBlock::instr_iterator I,
402 const DebugLoc &DL,
403 const MCInstrDesc &MCID) {
404 MachineFunction &MF = *BB.getParent();
405 MachineInstr *MI = MF.CreateMachineInstr(MCID, DL);
406 BB.insert(I, MI);
407 return MachineInstrBuilder(MF, MI);
408}
409
410inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr &I,
411 const DebugLoc &DL,
412 const MCInstrDesc &MCID) {
413 // Calling the overload for instr_iterator is always correct. However, the
414 // definition is not available in headers, so inline the check.
415 if (I.isInsideBundle())
416 return BuildMI(BB, MachineBasicBlock::instr_iterator(I), DL, MCID);
417 return BuildMI(BB, MachineBasicBlock::iterator(I), DL, MCID);
418}
419
420inline MachineInstrBuilder BuildMI(MachineBasicBlock &BB, MachineInstr *I,
421 const DebugLoc &DL,
422 const MCInstrDesc &MCID) {
423 return BuildMI(BB, *I, DL, MCID);
424}
425
426/// This version of the builder inserts the newly-built instruction at the end
427/// of the given MachineBasicBlock, and does NOT take a destination register.
428inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
429 const MCInstrDesc &MCID) {
430 return BuildMI(*BB, BB->end(), DL, MCID);
431}
432
433/// This version of the builder inserts the newly-built instruction at the
434/// end of the given MachineBasicBlock, and sets up the first operand as a
435/// destination virtual register.
436inline MachineInstrBuilder BuildMI(MachineBasicBlock *BB, const DebugLoc &DL,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200437 const MCInstrDesc &MCID, Register DestReg) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100438 return BuildMI(*BB, BB->end(), DL, MCID, DestReg);
439}
440
441/// This version of the builder builds a DBG_VALUE intrinsic
442/// for either a value in a register or a register-indirect
443/// address. The convention is that a DBG_VALUE is indirect iff the
444/// second operand is an immediate.
445MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
446 const MCInstrDesc &MCID, bool IsIndirect,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200447 Register Reg, const MDNode *Variable,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100448 const MDNode *Expr);
449
450/// This version of the builder builds a DBG_VALUE intrinsic
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100451/// for a MachineOperand.
452MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL,
453 const MCInstrDesc &MCID, bool IsIndirect,
454 MachineOperand &MO, const MDNode *Variable,
455 const MDNode *Expr);
456
457/// This version of the builder builds a DBG_VALUE intrinsic
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100458/// for either a value in a register or a register-indirect
459/// address and inserts it at position I.
460MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
461 MachineBasicBlock::iterator I, const DebugLoc &DL,
462 const MCInstrDesc &MCID, bool IsIndirect,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200463 Register Reg, const MDNode *Variable,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100464 const MDNode *Expr);
465
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100466/// This version of the builder builds a DBG_VALUE intrinsic
467/// for a machine operand and inserts it at position I.
468MachineInstrBuilder BuildMI(MachineBasicBlock &BB,
469 MachineBasicBlock::iterator I, const DebugLoc &DL,
470 const MCInstrDesc &MCID, bool IsIndirect,
471 MachineOperand &MO, const MDNode *Variable,
472 const MDNode *Expr);
473
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100474/// Clone a DBG_VALUE whose value has been spilled to FrameIndex.
475MachineInstr *buildDbgValueForSpill(MachineBasicBlock &BB,
476 MachineBasicBlock::iterator I,
477 const MachineInstr &Orig, int FrameIndex);
478
479/// Update a DBG_VALUE whose value has been spilled to FrameIndex. Useful when
480/// modifying an instruction in place while iterating over a basic block.
481void updateDbgValueForSpill(MachineInstr &Orig, int FrameIndex);
482
483inline unsigned getDefRegState(bool B) {
484 return B ? RegState::Define : 0;
485}
486inline unsigned getImplRegState(bool B) {
487 return B ? RegState::Implicit : 0;
488}
489inline unsigned getKillRegState(bool B) {
490 return B ? RegState::Kill : 0;
491}
492inline unsigned getDeadRegState(bool B) {
493 return B ? RegState::Dead : 0;
494}
495inline unsigned getUndefRegState(bool B) {
496 return B ? RegState::Undef : 0;
497}
498inline unsigned getInternalReadRegState(bool B) {
499 return B ? RegState::InternalRead : 0;
500}
501inline unsigned getDebugRegState(bool B) {
502 return B ? RegState::Debug : 0;
503}
504inline unsigned getRenamableRegState(bool B) {
505 return B ? RegState::Renamable : 0;
506}
507
508/// Get all register state flags from machine operand \p RegOp.
509inline unsigned getRegState(const MachineOperand &RegOp) {
510 assert(RegOp.isReg() && "Not a register operand");
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200511 return getDefRegState(RegOp.isDef()) | getImplRegState(RegOp.isImplicit()) |
512 getKillRegState(RegOp.isKill()) | getDeadRegState(RegOp.isDead()) |
513 getUndefRegState(RegOp.isUndef()) |
514 getInternalReadRegState(RegOp.isInternalRead()) |
515 getDebugRegState(RegOp.isDebug()) |
516 getRenamableRegState(Register::isPhysicalRegister(RegOp.getReg()) &&
517 RegOp.isRenamable());
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100518}
519
520/// Helper class for constructing bundles of MachineInstrs.
521///
522/// MIBundleBuilder can create a bundle from scratch by inserting new
523/// MachineInstrs one at a time, or it can create a bundle from a sequence of
524/// existing MachineInstrs in a basic block.
525class MIBundleBuilder {
526 MachineBasicBlock &MBB;
527 MachineBasicBlock::instr_iterator Begin;
528 MachineBasicBlock::instr_iterator End;
529
530public:
531 /// Create an MIBundleBuilder that inserts instructions into a new bundle in
532 /// BB above the bundle or instruction at Pos.
533 MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator Pos)
534 : MBB(BB), Begin(Pos.getInstrIterator()), End(Begin) {}
535
536 /// Create a bundle from the sequence of instructions between B and E.
537 MIBundleBuilder(MachineBasicBlock &BB, MachineBasicBlock::iterator B,
538 MachineBasicBlock::iterator E)
539 : MBB(BB), Begin(B.getInstrIterator()), End(E.getInstrIterator()) {
540 assert(B != E && "No instructions to bundle");
541 ++B;
542 while (B != E) {
543 MachineInstr &MI = *B;
544 ++B;
545 MI.bundleWithPred();
546 }
547 }
548
549 /// Create an MIBundleBuilder representing an existing instruction or bundle
550 /// that has MI as its head.
551 explicit MIBundleBuilder(MachineInstr *MI)
552 : MBB(*MI->getParent()), Begin(MI),
553 End(getBundleEnd(MI->getIterator())) {}
554
555 /// Return a reference to the basic block containing this bundle.
556 MachineBasicBlock &getMBB() const { return MBB; }
557
558 /// Return true if no instructions have been inserted in this bundle yet.
559 /// Empty bundles aren't representable in a MachineBasicBlock.
560 bool empty() const { return Begin == End; }
561
562 /// Return an iterator to the first bundled instruction.
563 MachineBasicBlock::instr_iterator begin() const { return Begin; }
564
565 /// Return an iterator beyond the last bundled instruction.
566 MachineBasicBlock::instr_iterator end() const { return End; }
567
568 /// Insert MI into this bundle before I which must point to an instruction in
569 /// the bundle, or end().
570 MIBundleBuilder &insert(MachineBasicBlock::instr_iterator I,
571 MachineInstr *MI) {
572 MBB.insert(I, MI);
573 if (I == Begin) {
574 if (!empty())
575 MI->bundleWithSucc();
576 Begin = MI->getIterator();
577 return *this;
578 }
579 if (I == End) {
580 MI->bundleWithPred();
581 return *this;
582 }
583 // MI was inserted in the middle of the bundle, so its neighbors' flags are
584 // already fine. Update MI's bundle flags manually.
585 MI->setFlag(MachineInstr::BundledPred);
586 MI->setFlag(MachineInstr::BundledSucc);
587 return *this;
588 }
589
590 /// Insert MI into MBB by prepending it to the instructions in the bundle.
591 /// MI will become the first instruction in the bundle.
592 MIBundleBuilder &prepend(MachineInstr *MI) {
593 return insert(begin(), MI);
594 }
595
596 /// Insert MI into MBB by appending it to the instructions in the bundle.
597 /// MI will become the last instruction in the bundle.
598 MIBundleBuilder &append(MachineInstr *MI) {
599 return insert(end(), MI);
600 }
601};
602
603} // end namespace llvm
604
605#endif // LLVM_CODEGEN_MACHINEINSTRBUILDER_H