1//===- llvm/CodeGen/MachineInstrBundle.h - MI bundle utilities --*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
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#include "llvm/Support/Compiler.h"
19
20namespace llvm {
21
22/// finalizeBundle - Finalize a machine instruction bundle which includes
23/// a sequence of instructions starting from FirstMI to LastMI (exclusive).
24/// This routine adds a BUNDLE instruction to represent the bundle, it adds
25/// IsInternalRead markers to MachineOperands which are defined inside the
26/// bundle, and it copies externally visible defs and uses to the BUNDLE
27/// instruction.
28LLVM_ABI void finalizeBundle(MachineBasicBlock &MBB,
29 MachineBasicBlock::instr_iterator FirstMI,
30 MachineBasicBlock::instr_iterator LastMI);
31
32/// finalizeBundle - Same functionality as the previous finalizeBundle except
33/// the last instruction in the bundle is not provided as an input. This is
34/// used in cases where bundles are pre-determined by marking instructions
35/// with 'InsideBundle' marker. It returns the MBB instruction iterator that
36/// points to the end of the bundle.
37LLVM_ABI MachineBasicBlock::instr_iterator
38finalizeBundle(MachineBasicBlock &MBB,
39 MachineBasicBlock::instr_iterator FirstMI);
40
41/// finalizeBundles - Finalize instruction bundles in the specified
42/// MachineFunction. Return true if any bundles are finalized.
43LLVM_ABI bool finalizeBundles(MachineFunction &MF);
44
45/// Returns an iterator to the first instruction in the bundle containing \p I.
46inline MachineBasicBlock::instr_iterator getBundleStart(
47 MachineBasicBlock::instr_iterator I) {
48 while (I->isBundledWithPred())
49 --I;
50 return I;
51}
52
53/// Returns an iterator to the first instruction in the bundle containing \p I.
54inline MachineBasicBlock::const_instr_iterator getBundleStart(
55 MachineBasicBlock::const_instr_iterator I) {
56 while (I->isBundledWithPred())
57 --I;
58 return I;
59}
60
61/// Returns an iterator pointing beyond the bundle containing \p I.
62inline MachineBasicBlock::instr_iterator getBundleEnd(
63 MachineBasicBlock::instr_iterator I) {
64 while (I->isBundledWithSucc())
65 ++I;
66 ++I;
67 return I;
68}
69
70/// Returns an iterator pointing beyond the bundle containing \p I.
71inline MachineBasicBlock::const_instr_iterator getBundleEnd(
72 MachineBasicBlock::const_instr_iterator I) {
73 while (I->isBundledWithSucc())
74 ++I;
75 ++I;
76 return I;
77}
78
79//===----------------------------------------------------------------------===//
80// MachineBundleOperand iterator
81//
82
83/// MIBundleOperandIteratorBase - Iterator that visits all operands in a bundle
84/// of MachineInstrs. This class is not intended to be used directly, use one
85/// of the sub-classes instead.
86///
87/// Intended use:
88///
89/// for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) {
90/// if (!MIO->isReg())
91/// continue;
92/// ...
93/// }
94///
95template <typename ValueT>
96class MIBundleOperandIteratorBase
97 : public iterator_facade_base<MIBundleOperandIteratorBase<ValueT>,
98 std::forward_iterator_tag, ValueT> {
99 MachineBasicBlock::instr_iterator InstrI, InstrE;
100 MachineInstr::mop_iterator OpI, OpE;
101
102 // If the operands on InstrI are exhausted, advance InstrI to the next
103 // bundled instruction with operands.
104 void advance() {
105 while (OpI == OpE) {
106 // Don't advance off the basic block, or into a new bundle.
107 if (++InstrI == InstrE || !InstrI->isInsideBundle()) {
108 InstrI = InstrE;
109 break;
110 }
111 OpI = InstrI->operands_begin();
112 OpE = InstrI->operands_end();
113 }
114 }
115
116protected:
117 /// MIBundleOperandIteratorBase - Create an iterator that visits all operands
118 /// on MI, or all operands on every instruction in the bundle containing MI.
119 ///
120 /// @param MI The instruction to examine.
121 ///
122 explicit MIBundleOperandIteratorBase(MachineInstr &MI) {
123 InstrI = getBundleStart(I: MI.getIterator());
124 InstrE = MI.getParent()->instr_end();
125 OpI = InstrI->operands_begin();
126 OpE = InstrI->operands_end();
127 advance();
128 }
129
130 /// Constructor for an iterator past the last iteration: both instruction
131 /// iterators point to the end of the BB and OpI == OpE.
132 explicit MIBundleOperandIteratorBase(MachineBasicBlock::instr_iterator InstrE,
133 MachineInstr::mop_iterator OpE)
134 : InstrI(InstrE), InstrE(InstrE), OpI(OpE), OpE(OpE) {}
135
136public:
137 /// isValid - Returns true until all the operands have been visited.
138 bool isValid() const { return OpI != OpE; }
139
140 /// Preincrement. Move to the next operand.
141 void operator++() {
142 assert(isValid() && "Cannot advance MIOperands beyond the last operand");
143 ++OpI;
144 advance();
145 }
146
147 ValueT &operator*() const { return *OpI; }
148 ValueT *operator->() const { return &*OpI; }
149
150 bool operator==(const MIBundleOperandIteratorBase &Arg) const {
151 // Iterators are equal, if InstrI matches and either OpIs match or OpI ==
152 // OpE match for both. The second condition allows us to construct an 'end'
153 // iterator, without finding the last instruction in a bundle up-front.
154 return InstrI == Arg.InstrI &&
155 (OpI == Arg.OpI || (OpI == OpE && Arg.OpI == Arg.OpE));
156 }
157 /// getOperandNo - Returns the number of the current operand relative to its
158 /// instruction.
159 ///
160 unsigned getOperandNo() const {
161 return OpI - InstrI->operands_begin();
162 }
163};
164
165/// MIBundleOperands - Iterate over all operands in a bundle of machine
166/// instructions.
167///
168class MIBundleOperands : public MIBundleOperandIteratorBase<MachineOperand> {
169 /// Constructor for an iterator past the last iteration.
170 MIBundleOperands(MachineBasicBlock::instr_iterator InstrE,
171 MachineInstr::mop_iterator OpE)
172 : MIBundleOperandIteratorBase(InstrE, OpE) {}
173
174public:
175 MIBundleOperands(MachineInstr &MI) : MIBundleOperandIteratorBase(MI) {}
176
177 /// Returns an iterator past the last iteration.
178 static MIBundleOperands end(const MachineBasicBlock &MBB) {
179 return {const_cast<MachineBasicBlock &>(MBB).instr_end(),
180 const_cast<MachineBasicBlock &>(MBB).instr_begin()->operands_end()};
181 }
182};
183
184/// ConstMIBundleOperands - Iterate over all operands in a const bundle of
185/// machine instructions.
186///
187class ConstMIBundleOperands
188 : public MIBundleOperandIteratorBase<const MachineOperand> {
189
190 /// Constructor for an iterator past the last iteration.
191 ConstMIBundleOperands(MachineBasicBlock::instr_iterator InstrE,
192 MachineInstr::mop_iterator OpE)
193 : MIBundleOperandIteratorBase(InstrE, OpE) {}
194
195public:
196 ConstMIBundleOperands(const MachineInstr &MI)
197 : MIBundleOperandIteratorBase(const_cast<MachineInstr &>(MI)) {}
198
199 /// Returns an iterator past the last iteration.
200 static ConstMIBundleOperands end(const MachineBasicBlock &MBB) {
201 return {const_cast<MachineBasicBlock &>(MBB).instr_end(),
202 const_cast<MachineBasicBlock &>(MBB).instr_begin()->operands_end()};
203 }
204};
205
206inline iterator_range<ConstMIBundleOperands>
207const_mi_bundle_ops(const MachineInstr &MI) {
208 return make_range(x: ConstMIBundleOperands(MI),
209 y: ConstMIBundleOperands::end(MBB: *MI.getParent()));
210}
211
212inline iterator_range<MIBundleOperands> mi_bundle_ops(MachineInstr &MI) {
213 return make_range(x: MIBundleOperands(MI),
214 y: MIBundleOperands::end(MBB: *MI.getParent()));
215}
216
217/// VirtRegInfo - Information about a virtual register used by a set of
218/// operands.
219///
220struct VirtRegInfo {
221 /// Reads - One of the operands read the virtual register. This does not
222 /// include undef or internal use operands, see MO::readsReg().
223 bool Reads;
224
225 /// Writes - One of the operands writes the virtual register.
226 bool Writes;
227
228 /// Tied - Uses and defs must use the same register. This can be because of
229 /// a two-address constraint, or there may be a partial redefinition of a
230 /// sub-register.
231 bool Tied;
232};
233
234/// AnalyzeVirtRegInBundle - Analyze how the current instruction or bundle uses
235/// a virtual register. This function should not be called after operator++(),
236/// it expects a fresh iterator.
237///
238/// @param Reg The virtual register to analyze.
239/// @param Ops When set, this vector will receive an (MI, OpNum) entry for
240/// each operand referring to Reg.
241/// @returns A filled-in RegInfo struct.
242LLVM_ABI VirtRegInfo AnalyzeVirtRegInBundle(
243 MachineInstr &MI, Register Reg,
244 SmallVectorImpl<std::pair<MachineInstr *, unsigned>> *Ops = nullptr);
245
246/// Return a pair of lane masks (reads, writes) indicating which lanes this
247/// instruction uses with Reg.
248LLVM_ABI std::pair<LaneBitmask, LaneBitmask>
249AnalyzeVirtRegLanesInBundle(const MachineInstr &MI, Register Reg,
250 const MachineRegisterInfo &MRI,
251 const TargetRegisterInfo &TRI);
252
253/// Information about how a physical register Reg is used by a set of
254/// operands.
255struct PhysRegInfo {
256 /// There is a regmask operand indicating Reg is clobbered.
257 /// \see MachineOperand::CreateRegMask().
258 bool Clobbered;
259
260 /// Reg or one of its aliases is defined. The definition may only cover
261 /// parts of the register.
262 bool Defined;
263 /// Reg or a super-register is defined. The definition covers the full
264 /// register.
265 bool FullyDefined;
266
267 /// Reg or one of its aliases is read. The register may only be read
268 /// partially.
269 bool Read;
270 /// Reg or a super-register is read. The full register is read.
271 bool FullyRead;
272
273 /// Either:
274 /// - Reg is FullyDefined and all defs of reg or an overlapping
275 /// register are dead, or
276 /// - Reg is completely dead because "defined" by a clobber.
277 bool DeadDef;
278
279 /// Reg is Defined and all defs of reg or an overlapping register are
280 /// dead.
281 bool PartialDeadDef;
282
283 /// There is a use operand of reg or a super-register with kill flag set.
284 bool Killed;
285};
286
287/// AnalyzePhysRegInBundle - Analyze how the current instruction or bundle uses
288/// a physical register. This function should not be called after operator++(),
289/// it expects a fresh iterator.
290///
291/// @param Reg The physical register to analyze.
292/// @returns A filled-in PhysRegInfo struct.
293LLVM_ABI PhysRegInfo AnalyzePhysRegInBundle(const MachineInstr &MI,
294 Register Reg,
295 const TargetRegisterInfo *TRI);
296
297} // End llvm namespace
298
299#endif
300