1 //===- llvm/CodeGen/MachineInstrBundle.h - MI bundle utilities --*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file provide utility functions to manipulate machine instruction
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
15 #define LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
17 #include "llvm/CodeGen/MachineBasicBlock.h"
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
27 void finalizeBundle(MachineBasicBlock
&MBB
,
28 MachineBasicBlock::instr_iterator FirstMI
,
29 MachineBasicBlock::instr_iterator LastMI
);
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.
36 MachineBasicBlock::instr_iterator
finalizeBundle(MachineBasicBlock
&MBB
,
37 MachineBasicBlock::instr_iterator FirstMI
);
39 /// finalizeBundles - Finalize instruction bundles in the specified
40 /// MachineFunction. Return true if any bundles are finalized.
41 bool finalizeBundles(MachineFunction
&MF
);
43 /// Returns an iterator to the first instruction in the bundle containing \p I.
44 inline MachineBasicBlock::instr_iterator
getBundleStart(
45 MachineBasicBlock::instr_iterator I
) {
46 while (I
->isBundledWithPred())
51 /// Returns an iterator to the first instruction in the bundle containing \p I.
52 inline MachineBasicBlock::const_instr_iterator
getBundleStart(
53 MachineBasicBlock::const_instr_iterator I
) {
54 while (I
->isBundledWithPred())
59 /// Returns an iterator pointing beyond the bundle containing \p I.
60 inline MachineBasicBlock::instr_iterator
getBundleEnd(
61 MachineBasicBlock::instr_iterator I
) {
62 while (I
->isBundledWithSucc())
68 /// Returns an iterator pointing beyond the bundle containing \p I.
69 inline MachineBasicBlock::const_instr_iterator
getBundleEnd(
70 MachineBasicBlock::const_instr_iterator I
) {
71 while (I
->isBundledWithSucc())
77 //===----------------------------------------------------------------------===//
78 // MachineOperand iterator
81 /// MachineOperandIteratorBase - Iterator that can visit all operands on a
82 /// MachineInstr, or all operands on a bundle of MachineInstrs. This class is
83 /// not intended to be used directly, use one of the sub-classes instead.
87 /// for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) {
88 /// if (!MIO->isReg())
93 class MachineOperandIteratorBase
{
94 MachineBasicBlock::instr_iterator InstrI
, InstrE
;
95 MachineInstr::mop_iterator OpI
, OpE
;
97 // If the operands on InstrI are exhausted, advance InstrI to the next
98 // bundled instruction with operands.
101 // Don't advance off the basic block, or into a new bundle.
102 if (++InstrI
== InstrE
|| !InstrI
->isInsideBundle())
104 OpI
= InstrI
->operands_begin();
105 OpE
= InstrI
->operands_end();
110 /// MachineOperandIteratorBase - Create an iterator that visits all operands
111 /// on MI, or all operands on every instruction in the bundle containing MI.
113 /// @param MI The instruction to examine.
114 /// @param WholeBundle When true, visit all operands on the entire bundle.
116 explicit MachineOperandIteratorBase(MachineInstr
&MI
, bool WholeBundle
) {
118 InstrI
= getBundleStart(MI
.getIterator());
119 InstrE
= MI
.getParent()->instr_end();
121 InstrI
= InstrE
= MI
.getIterator();
124 OpI
= InstrI
->operands_begin();
125 OpE
= InstrI
->operands_end();
130 MachineOperand
&deref() const { return *OpI
; }
133 /// isValid - Returns true until all the operands have been visited.
134 bool isValid() const { return OpI
!= OpE
; }
136 /// Preincrement. Move to the next operand.
138 assert(isValid() && "Cannot advance MIOperands beyond the last operand");
143 /// getOperandNo - Returns the number of the current operand relative to its
146 unsigned getOperandNo() const {
147 return OpI
- InstrI
->operands_begin();
150 /// VirtRegInfo - Information about a virtual register used by a set of operands.
153 /// Reads - One of the operands read the virtual register. This does not
154 /// include undef or internal use operands, see MO::readsReg().
157 /// Writes - One of the operands writes the virtual register.
160 /// Tied - Uses and defs must use the same register. This can be because of
161 /// a two-address constraint, or there may be a partial redefinition of a
166 /// Information about how a physical register Reg is used by a set of
169 /// There is a regmask operand indicating Reg is clobbered.
170 /// \see MachineOperand::CreateRegMask().
173 /// Reg or one of its aliases is defined. The definition may only cover
174 /// parts of the register.
176 /// Reg or a super-register is defined. The definition covers the full
180 /// Reg or one of its aliases is read. The register may only be read
183 /// Reg or a super-register is read. The full register is read.
187 /// - Reg is FullyDefined and all defs of reg or an overlapping
188 /// register are dead, or
189 /// - Reg is completely dead because "defined" by a clobber.
192 /// Reg is Defined and all defs of reg or an overlapping register are
196 /// There is a use operand of reg or a super-register with kill flag set.
200 /// analyzeVirtReg - Analyze how the current instruction or bundle uses a
201 /// virtual register. This function should not be called after operator++(),
202 /// it expects a fresh iterator.
204 /// @param Reg The virtual register to analyze.
205 /// @param Ops When set, this vector will receive an (MI, OpNum) entry for
206 /// each operand referring to Reg.
207 /// @returns A filled-in RegInfo struct.
208 VirtRegInfo
analyzeVirtReg(unsigned Reg
,
209 SmallVectorImpl
<std::pair
<MachineInstr
*, unsigned> > *Ops
= nullptr);
211 /// analyzePhysReg - Analyze how the current instruction or bundle uses a
212 /// physical register. This function should not be called after operator++(),
213 /// it expects a fresh iterator.
215 /// @param Reg The physical register to analyze.
216 /// @returns A filled-in PhysRegInfo struct.
217 PhysRegInfo
analyzePhysReg(unsigned Reg
, const TargetRegisterInfo
*TRI
);
220 /// MIOperands - Iterate over operands of a single instruction.
222 class MIOperands
: public MachineOperandIteratorBase
{
224 MIOperands(MachineInstr
&MI
) : MachineOperandIteratorBase(MI
, false) {}
225 MachineOperand
&operator* () const { return deref(); }
226 MachineOperand
*operator->() const { return &deref(); }
229 /// ConstMIOperands - Iterate over operands of a single const instruction.
231 class ConstMIOperands
: public MachineOperandIteratorBase
{
233 ConstMIOperands(const MachineInstr
&MI
)
234 : MachineOperandIteratorBase(const_cast<MachineInstr
&>(MI
), false) {}
235 const MachineOperand
&operator* () const { return deref(); }
236 const MachineOperand
*operator->() const { return &deref(); }
239 /// MIBundleOperands - Iterate over all operands in a bundle of machine
242 class MIBundleOperands
: public MachineOperandIteratorBase
{
244 MIBundleOperands(MachineInstr
&MI
) : MachineOperandIteratorBase(MI
, true) {}
245 MachineOperand
&operator* () const { return deref(); }
246 MachineOperand
*operator->() const { return &deref(); }
249 /// ConstMIBundleOperands - Iterate over all operands in a const bundle of
250 /// machine instructions.
252 class ConstMIBundleOperands
: public MachineOperandIteratorBase
{
254 ConstMIBundleOperands(const MachineInstr
&MI
)
255 : MachineOperandIteratorBase(const_cast<MachineInstr
&>(MI
), true) {}
256 const MachineOperand
&operator* () const { return deref(); }
257 const MachineOperand
*operator->() const { return &deref(); }
260 } // End llvm namespace