LLVM  14.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 
18 
19 namespace 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.
27 void finalizeBundle(MachineBasicBlock &MBB,
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.
38 
39 /// finalizeBundles - Finalize instruction bundles in the specified
40 /// MachineFunction. Return true if any bundles are finalized.
41 bool finalizeBundles(MachineFunction &MF);
42 
43 /// Returns an iterator to the first instruction in the bundle containing \p 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.
54  while (I->isBundledWithPred())
55  --I;
56  return I;
57 }
58 
59 /// Returns an iterator pointing beyond the bundle containing \p I.
62  while (I->isBundledWithSucc())
63  ++I;
64  ++I;
65  return I;
66 }
67 
68 /// Returns an iterator pointing beyond the bundle containing \p I.
71  while (I->isBundledWithSucc())
72  ++I;
73  ++I;
74  return I;
75 }
76 
77 //===----------------------------------------------------------------------===//
78 // MachineBundleOperand iterator
79 //
80 
81 /// 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.
84 ///
85 /// Intended use:
86 ///
87 /// for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) {
88 /// if (!MIO->isReg())
89 /// continue;
90 /// ...
91 /// }
92 ///
93 template <typename ValueT>
95  : public iterator_facade_base<MIBundleOperandIteratorBase<ValueT>,
96  std::forward_iterator_tag, ValueT> {
97  MachineBasicBlock::instr_iterator InstrI, InstrE;
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.
105  if (++InstrI == InstrE || !InstrI->isInsideBundle()) {
106  InstrI = InstrE;
107  break;
108  }
109  OpI = InstrI->operands_begin();
110  OpE = InstrI->operands_end();
111  }
112  }
113 
114 protected:
115  /// MIBundleOperandIteratorBase - Create an iterator that visits all operands
116  /// on MI, or all operands on every instruction in the bundle containing MI.
117  ///
118  /// @param MI The instruction to examine.
119  ///
121  InstrI = getBundleStart(MI.getIterator());
122  InstrE = MI.getParent()->instr_end();
123  OpI = InstrI->operands_begin();
124  OpE = InstrI->operands_end();
125  advance();
126  }
127 
128  /// Constructor for an iterator past the last iteration: both instruction
129  /// iterators point to the end of the BB and OpI == OpE.
132  : InstrI(InstrE), InstrE(InstrE), OpI(OpE), OpE(OpE) {}
133 
134 public:
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 
145  ValueT &operator*() const { return *OpI; }
146  ValueT *operator->() const { return &*OpI; }
147 
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  }
155  /// 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  }
161 };
162 
163 /// MIBundleOperands - Iterate over all operands in a bundle of machine
164 /// instructions.
165 ///
166 class MIBundleOperands : public MIBundleOperandIteratorBase<MachineOperand> {
167  /// Constructor for an iterator past the last iteration.
170  : MIBundleOperandIteratorBase(InstrE, OpE) {}
171 
172 public:
174 
175  /// Returns an iterator past the last iteration.
177  return {const_cast<MachineBasicBlock &>(MBB).instr_end(),
178  const_cast<MachineBasicBlock &>(MBB).instr_begin()->operands_end()};
179  }
180 };
181 
182 /// ConstMIBundleOperands - Iterate over all operands in a const bundle of
183 /// machine instructions.
184 ///
186  : public MIBundleOperandIteratorBase<const MachineOperand> {
187 
188  /// Constructor for an iterator past the last iteration.
191  : MIBundleOperandIteratorBase(InstrE, OpE) {}
192 
193 public:
195  : MIBundleOperandIteratorBase(const_cast<MachineInstr &>(MI)) {}
196 
197  /// Returns an iterator past the last iteration.
199  return {const_cast<MachineBasicBlock &>(MBB).instr_end(),
200  const_cast<MachineBasicBlock &>(MBB).instr_begin()->operands_end()};
201  }
202 };
203 
204 inline iterator_range<ConstMIBundleOperands>
207  ConstMIBundleOperands::end(*MI.getParent()));
208 }
209 
212  MIBundleOperands::end(*MI.getParent()));
213 }
214 
215 /// VirtRegInfo - Information about a virtual register used by a set of
216 /// operands.
217 ///
218 struct 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.
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.
246 struct 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.
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.
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.
285  const TargetRegisterInfo *TRI);
286 
287 } // End llvm namespace
288 
289 #endif
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:103
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
Reg
unsigned Reg
Definition: MachineSink.cpp:1566
llvm::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition: iterator_range.h:53
llvm::VirtRegInfo::Tied
bool Tied
Tied - Uses and defs must use the same register.
Definition: MachineInstrBundle.h:229
llvm::PhysRegInfo::FullyRead
bool FullyRead
Reg or a super-register is read. The full register is read.
Definition: MachineInstrBundle.h:262
llvm::MIBundleOperands::MIBundleOperands
MIBundleOperands(MachineInstr &MI)
Definition: MachineInstrBundle.h:173
llvm::VirtRegInfo::Reads
bool Reads
Reads - One of the operands read the virtual register.
Definition: MachineInstrBundle.h:221
llvm::finalizeBundles
bool finalizeBundles(MachineFunction &MF)
finalizeBundles - Finalize instruction bundles in the specified MachineFunction.
Definition: MachineInstrBundle.cpp:255
llvm::MIBundleOperandIteratorBase::MIBundleOperandIteratorBase
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...
Definition: MachineInstrBundle.h:130
llvm::PhysRegInfo::DeadDef
bool DeadDef
Either:
Definition: MachineInstrBundle.h:268
llvm::MIBundleOperandIteratorBase::operator*
ValueT & operator*() const
Definition: MachineInstrBundle.h:145
MachineBasicBlock.h
llvm::TargetRegisterInfo
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
Definition: TargetRegisterInfo.h:233
llvm::PhysRegInfo::FullyDefined
bool FullyDefined
Reg or a super-register is defined.
Definition: MachineInstrBundle.h:256
llvm::mi_bundle_ops
iterator_range< MIBundleOperands > mi_bundle_ops(MachineInstr &MI)
Definition: MachineInstrBundle.h:210
TRI
unsigned const TargetRegisterInfo * TRI
Definition: MachineSink.cpp:1567
llvm::AnalyzePhysRegInBundle
PhysRegInfo AnalyzePhysRegInBundle(const MachineInstr &MI, Register Reg, const TargetRegisterInfo *TRI)
AnalyzePhysRegInBundle - Analyze how the current instruction or bundle uses a physical register.
Definition: MachineInstrBundle.cpp:308
llvm::MachineBasicBlock::const_instr_iterator
Instructions::const_iterator const_instr_iterator
Definition: MachineBasicBlock.h:229
llvm::PhysRegInfo::Killed
bool Killed
There is a use operand of reg or a super-register with kill flag set.
Definition: MachineInstrBundle.h:275
llvm::getBundleEnd
MachineBasicBlock::instr_iterator getBundleEnd(MachineBasicBlock::instr_iterator I)
Returns an iterator pointing beyond the bundle containing I.
Definition: MachineInstrBundle.h:60
Arg
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
Definition: AMDGPULibCalls.cpp:206
llvm::PhysRegInfo::Clobbered
bool Clobbered
There is a regmask operand indicating Reg is clobbered.
Definition: MachineInstrBundle.h:249
llvm::finalizeBundle
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...
Definition: MachineInstrBundle.cpp:123
llvm::PhysRegInfo::Read
bool Read
Reg or one of its aliases is read.
Definition: MachineInstrBundle.h:260
llvm::MachineOperand
MachineOperand class - Representation of each machine instruction operand.
Definition: MachineOperand.h:49
llvm::VirtRegInfo::Writes
bool Writes
Writes - One of the operands writes the virtual register.
Definition: MachineInstrBundle.h:224
llvm::ConstMIBundleOperands::ConstMIBundleOperands
ConstMIBundleOperands(const MachineInstr &MI)
Definition: MachineInstrBundle.h:194
llvm::MIBundleOperands
MIBundleOperands - Iterate over all operands in a bundle of machine instructions.
Definition: MachineInstrBundle.h:166
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::const_mi_bundle_ops
iterator_range< ConstMIBundleOperands > const_mi_bundle_ops(const MachineInstr &MI)
Definition: MachineInstrBundle.h:205
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
llvm::MIBundleOperands::end
static MIBundleOperands end(const MachineBasicBlock &MBB)
Returns an iterator past the last iteration.
Definition: MachineInstrBundle.h:176
llvm::AnalyzeVirtRegInBundle
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.
Definition: MachineInstrBundle.cpp:278
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::iterator_facade_base
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:66
llvm::MIBundleOperandIteratorBase::isValid
bool isValid() const
isValid - Returns true until all the operands have been visited.
Definition: MachineInstrBundle.h:136
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::PhysRegInfo
Information about how a physical register Reg is used by a set of operands.
Definition: MachineInstrBundle.h:246
llvm::PhysRegInfo::Defined
bool Defined
Reg or one of its aliases is defined.
Definition: MachineInstrBundle.h:253
ValueT
llvm::getBundleStart
MachineBasicBlock::instr_iterator getBundleStart(MachineBasicBlock::instr_iterator I)
Returns an iterator to the first instruction in the bundle containing I.
Definition: MachineInstrBundle.h:44
llvm::Register
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
MBB
MachineBasicBlock & MBB
Definition: AArch64SLSHardening.cpp:74
llvm::MIBundleOperandIteratorBase::operator++
void operator++()
Preincrement. Move to the next operand.
Definition: MachineInstrBundle.h:139
llvm::ConstMIBundleOperands
ConstMIBundleOperands - Iterate over all operands in a const bundle of machine instructions.
Definition: MachineInstrBundle.h:185
llvm::MachineBasicBlock::instr_iterator
Instructions::iterator instr_iterator
Definition: MachineBasicBlock.h:228
llvm::MIBundleOperandIteratorBase::operator->
ValueT * operator->() const
Definition: MachineInstrBundle.h:146
llvm::MIBundleOperandIteratorBase
MIBundleOperandIteratorBase - Iterator that visits all operands in a bundle of MachineInstrs.
Definition: MachineInstrBundle.h:94
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition: iterator_range.h:30
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:43
llvm::MIBundleOperandIteratorBase::operator==
bool operator==(const MIBundleOperandIteratorBase &Arg) const
Definition: MachineInstrBundle.h:148
llvm::ConstMIBundleOperands::end
static ConstMIBundleOperands end(const MachineBasicBlock &MBB)
Returns an iterator past the last iteration.
Definition: MachineInstrBundle.h:198
llvm::VirtRegInfo
VirtRegInfo - Information about a virtual register used by a set of operands.
Definition: MachineInstrBundle.h:218
llvm::MIBundleOperandIteratorBase::getOperandNo
unsigned getOperandNo() const
getOperandNo - Returns the number of the current operand relative to its instruction.
Definition: MachineInstrBundle.h:158
llvm::MIBundleOperandIteratorBase::MIBundleOperandIteratorBase
MIBundleOperandIteratorBase(MachineInstr &MI)
MIBundleOperandIteratorBase - Create an iterator that visits all operands on MI, or all operands on e...
Definition: MachineInstrBundle.h:120
llvm::PhysRegInfo::PartialDeadDef
bool PartialDeadDef
Reg is Defined and all defs of reg or an overlapping register are dead.
Definition: MachineInstrBundle.h:272