blob: 8a73f9a18f472603f6a46112c8f44488404e6e85 [file] [log] [blame]
Andrew Walbran16937d02019-10-22 13:54:20 +01001//===- llvm/CodeGen/MachineInstrBundle.h - MI bundle utilities --*- C++ -*-===//
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01002//
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 provide utility functions to manipulate machine instruction
10// bundles.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
15#define LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
16
17#include "llvm/CodeGen/MachineBasicBlock.h"
18
19namespace llvm {
20
21/// finalizeBundle - Finalize a machine instruction bundle which includes
22/// a sequence of instructions starting from FirstMI to LastMI (exclusive).
23/// This routine adds a BUNDLE instruction to represent the bundle, it adds
24/// IsInternalRead markers to MachineOperands which are defined inside the
25/// bundle, and it copies externally visible defs and uses to the BUNDLE
26/// instruction.
27void finalizeBundle(MachineBasicBlock &MBB,
28 MachineBasicBlock::instr_iterator FirstMI,
29 MachineBasicBlock::instr_iterator LastMI);
30
31/// finalizeBundle - Same functionality as the previous finalizeBundle except
32/// the last instruction in the bundle is not provided as an input. This is
33/// used in cases where bundles are pre-determined by marking instructions
34/// with 'InsideBundle' marker. It returns the MBB instruction iterator that
35/// points to the end of the bundle.
36MachineBasicBlock::instr_iterator finalizeBundle(MachineBasicBlock &MBB,
37 MachineBasicBlock::instr_iterator FirstMI);
38
39/// finalizeBundles - Finalize instruction bundles in the specified
40/// MachineFunction. Return true if any bundles are finalized.
41bool finalizeBundles(MachineFunction &MF);
42
43/// Returns an iterator to the first instruction in the bundle containing \p I.
44inline MachineBasicBlock::instr_iterator getBundleStart(
45 MachineBasicBlock::instr_iterator I) {
46 while (I->isBundledWithPred())
47 --I;
48 return I;
49}
50
51/// Returns an iterator to the first instruction in the bundle containing \p I.
52inline MachineBasicBlock::const_instr_iterator getBundleStart(
53 MachineBasicBlock::const_instr_iterator I) {
54 while (I->isBundledWithPred())
55 --I;
56 return I;
57}
58
59/// Returns an iterator pointing beyond the bundle containing \p I.
60inline MachineBasicBlock::instr_iterator getBundleEnd(
61 MachineBasicBlock::instr_iterator I) {
62 while (I->isBundledWithSucc())
63 ++I;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010064 ++I;
65 return I;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010066}
67
68/// Returns an iterator pointing beyond the bundle containing \p I.
69inline MachineBasicBlock::const_instr_iterator getBundleEnd(
70 MachineBasicBlock::const_instr_iterator I) {
71 while (I->isBundledWithSucc())
72 ++I;
Andrew Walbran3d2c1972020-04-07 12:24:26 +010073 ++I;
74 return I;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010075}
76
77//===----------------------------------------------------------------------===//
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020078// MachineBundleOperand iterator
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010079//
80
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020081/// MIBundleOperandIteratorBase - Iterator that visits all operands in a bundle
82/// of MachineInstrs. This class is not intended to be used directly, use one
83/// of the sub-classes instead.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010084///
85/// Intended use:
86///
87/// for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) {
88/// if (!MIO->isReg())
89/// continue;
90/// ...
91/// }
92///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020093template <typename ValueT>
94class MIBundleOperandIteratorBase
95 : public iterator_facade_base<MIBundleOperandIteratorBase<ValueT>,
96 std::forward_iterator_tag, ValueT> {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010097 MachineBasicBlock::instr_iterator InstrI, InstrE;
98 MachineInstr::mop_iterator OpI, OpE;
99
100 // If the operands on InstrI are exhausted, advance InstrI to the next
101 // bundled instruction with operands.
102 void advance() {
103 while (OpI == OpE) {
104 // Don't advance off the basic block, or into a new bundle.
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200105 if (++InstrI == InstrE || !InstrI->isInsideBundle()) {
106 InstrI = InstrE;
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100107 break;
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200108 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100109 OpI = InstrI->operands_begin();
110 OpE = InstrI->operands_end();
111 }
112 }
113
114protected:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200115 /// MIBundleOperandIteratorBase - Create an iterator that visits all operands
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100116 /// on MI, or all operands on every instruction in the bundle containing MI.
117 ///
118 /// @param MI The instruction to examine.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100119 ///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200120 explicit MIBundleOperandIteratorBase(MachineInstr &MI) {
121 InstrI = getBundleStart(MI.getIterator());
122 InstrE = MI.getParent()->instr_end();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100123 OpI = InstrI->operands_begin();
124 OpE = InstrI->operands_end();
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200125 advance();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100126 }
127
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200128 /// Constructor for an iterator past the last iteration: both instruction
129 /// iterators point to the end of the BB and OpI == OpE.
130 explicit MIBundleOperandIteratorBase(MachineBasicBlock::instr_iterator InstrE,
131 MachineInstr::mop_iterator OpE)
132 : InstrI(InstrE), InstrE(InstrE), OpI(OpE), OpE(OpE) {}
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100133
134public:
135 /// isValid - Returns true until all the operands have been visited.
136 bool isValid() const { return OpI != OpE; }
137
138 /// Preincrement. Move to the next operand.
139 void operator++() {
140 assert(isValid() && "Cannot advance MIOperands beyond the last operand");
141 ++OpI;
142 advance();
143 }
144
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200145 ValueT &operator*() const { return *OpI; }
146 ValueT *operator->() const { return &*OpI; }
147
148 bool operator==(const MIBundleOperandIteratorBase &Arg) const {
149 // Iterators are equal, if InstrI matches and either OpIs match or OpI ==
150 // OpE match for both. The second condition allows us to construct an 'end'
151 // iterator, without finding the last instruction in a bundle up-front.
152 return InstrI == Arg.InstrI &&
153 (OpI == Arg.OpI || (OpI == OpE && Arg.OpI == Arg.OpE));
154 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100155 /// getOperandNo - Returns the number of the current operand relative to its
156 /// instruction.
157 ///
158 unsigned getOperandNo() const {
159 return OpI - InstrI->operands_begin();
160 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100161};
162
163/// MIBundleOperands - Iterate over all operands in a bundle of machine
164/// instructions.
165///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200166class MIBundleOperands : public MIBundleOperandIteratorBase<MachineOperand> {
167 /// Constructor for an iterator past the last iteration.
168 MIBundleOperands(MachineBasicBlock::instr_iterator InstrE,
169 MachineInstr::mop_iterator OpE)
170 : MIBundleOperandIteratorBase(InstrE, OpE) {}
171
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100172public:
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200173 MIBundleOperands(MachineInstr &MI) : MIBundleOperandIteratorBase(MI) {}
174
175 /// Returns an iterator past the last iteration.
176 static MIBundleOperands end(const MachineBasicBlock &MBB) {
177 return {const_cast<MachineBasicBlock &>(MBB).instr_end(),
178 const_cast<MachineBasicBlock &>(MBB).instr_begin()->operands_end()};
179 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100180};
181
182/// ConstMIBundleOperands - Iterate over all operands in a const bundle of
183/// machine instructions.
184///
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200185class ConstMIBundleOperands
186 : public MIBundleOperandIteratorBase<const MachineOperand> {
187
188 /// Constructor for an iterator past the last iteration.
189 ConstMIBundleOperands(MachineBasicBlock::instr_iterator InstrE,
190 MachineInstr::mop_iterator OpE)
191 : MIBundleOperandIteratorBase(InstrE, OpE) {}
192
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100193public:
194 ConstMIBundleOperands(const MachineInstr &MI)
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200195 : MIBundleOperandIteratorBase(const_cast<MachineInstr &>(MI)) {}
196
197 /// Returns an iterator past the last iteration.
198 static ConstMIBundleOperands end(const MachineBasicBlock &MBB) {
199 return {const_cast<MachineBasicBlock &>(MBB).instr_end(),
200 const_cast<MachineBasicBlock &>(MBB).instr_begin()->operands_end()};
201 }
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100202};
203
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200204inline iterator_range<ConstMIBundleOperands>
205const_mi_bundle_ops(const MachineInstr &MI) {
206 return make_range(ConstMIBundleOperands(MI),
207 ConstMIBundleOperands::end(*MI.getParent()));
208}
209
210inline iterator_range<MIBundleOperands> mi_bundle_ops(MachineInstr &MI) {
211 return make_range(MIBundleOperands(MI),
212 MIBundleOperands::end(*MI.getParent()));
213}
214
215/// VirtRegInfo - Information about a virtual register used by a set of
216/// operands.
217///
218struct VirtRegInfo {
219 /// Reads - One of the operands read the virtual register. This does not
220 /// include undef or internal use operands, see MO::readsReg().
221 bool Reads;
222
223 /// Writes - One of the operands writes the virtual register.
224 bool Writes;
225
226 /// Tied - Uses and defs must use the same register. This can be because of
227 /// a two-address constraint, or there may be a partial redefinition of a
228 /// sub-register.
229 bool Tied;
230};
231
232/// AnalyzeVirtRegInBundle - Analyze how the current instruction or bundle uses
233/// a virtual register. This function should not be called after operator++(),
234/// it expects a fresh iterator.
235///
236/// @param Reg The virtual register to analyze.
237/// @param Ops When set, this vector will receive an (MI, OpNum) entry for
238/// each operand referring to Reg.
239/// @returns A filled-in RegInfo struct.
240VirtRegInfo AnalyzeVirtRegInBundle(
241 MachineInstr &MI, Register Reg,
242 SmallVectorImpl<std::pair<MachineInstr *, unsigned>> *Ops = nullptr);
243
244/// Information about how a physical register Reg is used by a set of
245/// operands.
246struct PhysRegInfo {
247 /// There is a regmask operand indicating Reg is clobbered.
248 /// \see MachineOperand::CreateRegMask().
249 bool Clobbered;
250
251 /// Reg or one of its aliases is defined. The definition may only cover
252 /// parts of the register.
253 bool Defined;
254 /// Reg or a super-register is defined. The definition covers the full
255 /// register.
256 bool FullyDefined;
257
258 /// Reg or one of its aliases is read. The register may only be read
259 /// partially.
260 bool Read;
261 /// Reg or a super-register is read. The full register is read.
262 bool FullyRead;
263
264 /// Either:
265 /// - Reg is FullyDefined and all defs of reg or an overlapping
266 /// register are dead, or
267 /// - Reg is completely dead because "defined" by a clobber.
268 bool DeadDef;
269
270 /// Reg is Defined and all defs of reg or an overlapping register are
271 /// dead.
272 bool PartialDeadDef;
273
274 /// There is a use operand of reg or a super-register with kill flag set.
275 bool Killed;
276};
277
278/// AnalyzePhysRegInBundle - Analyze how the current instruction or bundle uses
279/// a physical register. This function should not be called after operator++(),
280/// it expects a fresh iterator.
281///
282/// @param Reg The physical register to analyze.
283/// @returns A filled-in PhysRegInfo struct.
284PhysRegInfo AnalyzePhysRegInBundle(const MachineInstr &MI, Register Reg,
285 const TargetRegisterInfo *TRI);
286
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100287} // End llvm namespace
288
289#endif