LLVM 22.0.0git
MachineInstrBundle.h
Go to the documentation of this file.
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
20
21namespace llvm {
22
23/// finalizeBundle - Finalize a machine instruction bundle which includes
24/// a sequence of instructions starting from FirstMI to LastMI (exclusive).
25/// This routine adds a BUNDLE instruction to represent the bundle, it adds
26/// IsInternalRead markers to MachineOperands which are defined inside the
27/// bundle, and it copies externally visible defs and uses to the BUNDLE
28/// instruction.
29LLVM_ABI void finalizeBundle(MachineBasicBlock &MBB,
32
33/// finalizeBundle - Same functionality as the previous finalizeBundle except
34/// the last instruction in the bundle is not provided as an input. This is
35/// used in cases where bundles are pre-determined by marking instructions
36/// with 'InsideBundle' marker. It returns the MBB instruction iterator that
37/// points to the end of the bundle.
39finalizeBundle(MachineBasicBlock &MBB,
41
42/// finalizeBundles - Finalize instruction bundles in the specified
43/// MachineFunction. Return true if any bundles are finalized.
44LLVM_ABI bool finalizeBundles(MachineFunction &MF);
45
46/// Returns an iterator to the first instruction in the bundle containing \p I.
49 while (I->isBundledWithPred())
50 --I;
51 return I;
52}
53
54/// Returns an iterator to the first instruction in the bundle containing \p I.
57 while (I->isBundledWithPred())
58 --I;
59 return I;
60}
61
62/// Returns an iterator pointing beyond the bundle containing \p I.
65 while (I->isBundledWithSucc())
66 ++I;
67 ++I;
68 return I;
69}
70
71/// Returns an iterator pointing beyond the bundle containing \p I.
74 while (I->isBundledWithSucc())
75 ++I;
76 ++I;
77 return I;
78}
79
80//===----------------------------------------------------------------------===//
81// MachineBundleOperand iterator
82//
83
84/// MIBundleOperandIteratorBase - Iterator that visits all operands in a bundle
85/// of MachineInstrs. This class is not intended to be used directly, use one
86/// of the sub-classes instead.
87///
88/// Intended use:
89///
90/// for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) {
91/// if (!MIO->isReg())
92/// continue;
93/// ...
94/// }
95///
96template <typename ValueT>
98 : public iterator_facade_base<MIBundleOperandIteratorBase<ValueT>,
99 std::forward_iterator_tag, ValueT> {
102
103 // If the operands on InstrI are exhausted, advance InstrI to the next
104 // bundled instruction with operands.
105 void advance() {
106 while (OpI == OpE) {
107 // Don't advance off the basic block, or into a new bundle.
108 if (++InstrI == InstrE || !InstrI->isInsideBundle()) {
109 InstrI = InstrE;
110 break;
111 }
112 OpI = InstrI->operands_begin();
113 OpE = InstrI->operands_end();
114 }
115 }
116
117protected:
118 /// MIBundleOperandIteratorBase - Create an iterator that visits all operands
119 /// on MI, or all operands on every instruction in the bundle containing MI.
120 ///
121 /// @param MI The instruction to examine.
122 ///
124 InstrI = getBundleStart(MI.getIterator());
125 InstrE = MI.getParent()->instr_end();
126 OpI = InstrI->operands_begin();
127 OpE = InstrI->operands_end();
128 advance();
129 }
130
131 /// Constructor for an iterator past the last iteration: both instruction
132 /// iterators point to the end of the BB and OpI == OpE.
135 : InstrI(InstrE), InstrE(InstrE), OpI(OpE), OpE(OpE) {}
136
137public:
138 /// isValid - Returns true until all the operands have been visited.
139 bool isValid() const { return OpI != OpE; }
140
141 /// Preincrement. Move to the next operand.
142 void operator++() {
143 assert(isValid() && "Cannot advance MIOperands beyond the last operand");
144 ++OpI;
145 advance();
146 }
147
148 ValueT &operator*() const { return *OpI; }
149 ValueT *operator->() const { return &*OpI; }
150
152 // Iterators are equal, if InstrI matches and either OpIs match or OpI ==
153 // OpE match for both. The second condition allows us to construct an 'end'
154 // iterator, without finding the last instruction in a bundle up-front.
155 return InstrI == Arg.InstrI &&
156 (OpI == Arg.OpI || (OpI == OpE && Arg.OpI == Arg.OpE));
157 }
158 /// getOperandNo - Returns the number of the current operand relative to its
159 /// instruction.
160 ///
161 unsigned getOperandNo() const {
162 return OpI - InstrI->operands_begin();
163 }
164};
165
166/// MIBundleOperands - Iterate over all operands in a bundle of machine
167/// instructions.
168///
169class MIBundleOperands : public MIBundleOperandIteratorBase<MachineOperand> {
170 /// Constructor for an iterator past the last iteration.
173 : MIBundleOperandIteratorBase(InstrE, OpE) {}
174
175public:
177
178 /// Returns an iterator past the last iteration.
180 return {const_cast<MachineBasicBlock &>(MBB).instr_end(),
181 const_cast<MachineBasicBlock &>(MBB).instr_begin()->operands_end()};
182 }
183};
184
185/// ConstMIBundleOperands - Iterate over all operands in a const bundle of
186/// machine instructions.
187///
189 : public MIBundleOperandIteratorBase<const MachineOperand> {
190
191 /// Constructor for an iterator past the last iteration.
194 : MIBundleOperandIteratorBase(InstrE, OpE) {}
195
196public:
198 : MIBundleOperandIteratorBase(const_cast<MachineInstr &>(MI)) {}
199
200 /// Returns an iterator past the last iteration.
202 return {const_cast<MachineBasicBlock &>(MBB).instr_end(),
203 const_cast<MachineBasicBlock &>(MBB).instr_begin()->operands_end()};
204 }
205};
206
207inline iterator_range<ConstMIBundleOperands>
210 ConstMIBundleOperands::end(*MI.getParent()));
211}
212
215 MIBundleOperands::end(*MI.getParent()));
216}
217
218/// VirtRegInfo - Information about a virtual register used by a set of
219/// operands.
220///
222 /// Reads - One of the operands read the virtual register. This does not
223 /// include undef or internal use operands, see MO::readsReg().
224 bool Reads;
225
226 /// Writes - One of the operands writes the virtual register.
227 bool Writes;
228
229 /// Tied - Uses and defs must use the same register. This can be because of
230 /// a two-address constraint, or there may be a partial redefinition of a
231 /// sub-register.
232 bool Tied;
233};
234
235/// AnalyzeVirtRegInBundle - Analyze how the current instruction or bundle uses
236/// a virtual register. This function should not be called after operator++(),
237/// it expects a fresh iterator.
238///
239/// @param Reg The virtual register to analyze.
240/// @param Ops When set, this vector will receive an (MI, OpNum) entry for
241/// each operand referring to Reg.
242/// @returns A filled-in RegInfo struct.
245 SmallVectorImpl<std::pair<MachineInstr *, unsigned>> *Ops = nullptr);
246
247/// Return a pair of lane masks (reads, writes) indicating which lanes this
248/// instruction uses with Reg.
249LLVM_ABI std::pair<LaneBitmask, LaneBitmask>
252 const TargetRegisterInfo &TRI);
253
254/// Information about how a physical register Reg is used by a set of
255/// operands.
257 /// There is a regmask operand indicating Reg is clobbered.
258 /// \see MachineOperand::CreateRegMask().
260
261 /// Reg or one of its aliases is defined. The definition may only cover
262 /// parts of the register.
264 /// Reg or a super-register is defined. The definition covers the full
265 /// register.
267
268 /// Reg or one of its aliases is read. The register may only be read
269 /// partially.
270 bool Read;
271 /// Reg or a super-register is read. The full register is read.
273
274 /// Either:
275 /// - Reg is FullyDefined and all defs of reg or an overlapping
276 /// register are dead, or
277 /// - Reg is completely dead because "defined" by a clobber.
279
280 /// Reg is Defined and all defs of reg or an overlapping register are
281 /// dead.
283
284 /// There is a use operand of reg or a super-register with kill flag set.
285 bool Killed;
286};
287
288/// AnalyzePhysRegInBundle - Analyze how the current instruction or bundle uses
289/// a physical register. This function should not be called after operator++(),
290/// it expects a fresh iterator.
291///
292/// @param Reg The physical register to analyze.
293/// @returns A filled-in PhysRegInfo struct.
296 const TargetRegisterInfo *TRI);
297
298class FinalizeBundleTestPass : public PassInfoMixin<FinalizeBundleTestPass> {
299public:
302};
303
304} // End llvm namespace
305
306#endif
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
#define LLVM_ABI
Definition: Compiler.h:213
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
Register Reg
Register const TargetRegisterInfo * TRI
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
ConstMIBundleOperands - Iterate over all operands in a const bundle of machine instructions.
static ConstMIBundleOperands end(const MachineBasicBlock &MBB)
Returns an iterator past the last iteration.
ConstMIBundleOperands(const MachineInstr &MI)
LLVM_ABI PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
MIBundleOperandIteratorBase - Iterator that visits all operands in a bundle of MachineInstrs.
unsigned getOperandNo() const
getOperandNo - Returns the number of the current operand relative to its instruction.
MIBundleOperandIteratorBase(MachineInstr &MI)
MIBundleOperandIteratorBase - Create an iterator that visits all operands on MI, or all operands on e...
MIBundleOperandIteratorBase(MachineBasicBlock::instr_iterator InstrE, MachineInstr::mop_iterator OpE)
Constructor for an iterator past the last iteration: both instruction iterators point to the end of t...
bool operator==(const MIBundleOperandIteratorBase &Arg) const
bool isValid() const
isValid - Returns true until all the operands have been visited.
void operator++()
Preincrement. Move to the next operand.
MIBundleOperands - Iterate over all operands in a bundle of machine instructions.
static MIBundleOperands end(const MachineBasicBlock &MBB)
Returns an iterator past the last iteration.
MIBundleOperands(MachineInstr &MI)
Instructions::iterator instr_iterator
Instructions::const_iterator const_instr_iterator
Representation of each machine instruction.
Definition: MachineInstr.h:72
MachineOperand class - Representation of each machine instruction operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:112
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:80
A range adaptor for a pair of iterators.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI void finalizeBundle(MachineBasicBlock &MBB, MachineBasicBlock::instr_iterator FirstMI, MachineBasicBlock::instr_iterator LastMI)
finalizeBundle - Finalize a machine instruction bundle which includes a sequence of instructions star...
MachineBasicBlock::instr_iterator getBundleStart(MachineBasicBlock::instr_iterator I)
Returns an iterator to the first instruction in the bundle containing I.
LLVM_ABI bool finalizeBundles(MachineFunction &MF)
finalizeBundles - Finalize instruction bundles in the specified MachineFunction.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI PhysRegInfo AnalyzePhysRegInBundle(const MachineInstr &MI, Register Reg, const TargetRegisterInfo *TRI)
AnalyzePhysRegInBundle - Analyze how the current instruction or bundle uses a physical register.
MachineBasicBlock::instr_iterator getBundleEnd(MachineBasicBlock::instr_iterator I)
Returns an iterator pointing beyond the bundle containing I.
iterator_range< ConstMIBundleOperands > const_mi_bundle_ops(const MachineInstr &MI)
LLVM_ABI VirtRegInfo AnalyzeVirtRegInBundle(MachineInstr &MI, Register Reg, SmallVectorImpl< std::pair< MachineInstr *, unsigned > > *Ops=nullptr)
AnalyzeVirtRegInBundle - Analyze how the current instruction or bundle uses a virtual register.
iterator_range< MIBundleOperands > mi_bundle_ops(MachineInstr &MI)
LLVM_ABI std::pair< LaneBitmask, LaneBitmask > AnalyzeVirtRegLanesInBundle(const MachineInstr &MI, Register Reg, const MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI)
Return a pair of lane masks (reads, writes) indicating which lanes this instruction uses with Reg.
A CRTP mix-in to automatically provide informational APIs needed for passes.
Definition: PassManager.h:70
Information about how a physical register Reg is used by a set of operands.
bool Read
Reg or one of its aliases is read.
bool Defined
Reg or one of its aliases is defined.
bool Killed
There is a use operand of reg or a super-register with kill flag set.
bool PartialDeadDef
Reg is Defined and all defs of reg or an overlapping register are dead.
bool Clobbered
There is a regmask operand indicating Reg is clobbered.
bool FullyRead
Reg or a super-register is read. The full register is read.
bool FullyDefined
Reg or a super-register is defined.
VirtRegInfo - Information about a virtual register used by a set of operands.
bool Reads
Reads - One of the operands read the virtual register.
bool Tied
Tied - Uses and defs must use the same register.
bool Writes
Writes - One of the operands writes the virtual register.