LLVM  9.0.0svn
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  return ++I;
65 }
66 
67 /// Returns an iterator pointing beyond the bundle containing \p I.
70  while (I->isBundledWithSucc())
71  ++I;
72  return ++I;
73 }
74 
75 //===----------------------------------------------------------------------===//
76 // MachineOperand iterator
77 //
78 
79 /// MachineOperandIteratorBase - Iterator that can visit all operands on a
80 /// MachineInstr, or all operands on a bundle of MachineInstrs. This class is
81 /// not intended to be used directly, use one of the sub-classes instead.
82 ///
83 /// Intended use:
84 ///
85 /// for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) {
86 /// if (!MIO->isReg())
87 /// continue;
88 /// ...
89 /// }
90 ///
92  MachineBasicBlock::instr_iterator InstrI, InstrE;
94 
95  // If the operands on InstrI are exhausted, advance InstrI to the next
96  // bundled instruction with operands.
97  void advance() {
98  while (OpI == OpE) {
99  // Don't advance off the basic block, or into a new bundle.
100  if (++InstrI == InstrE || !InstrI->isInsideBundle())
101  break;
102  OpI = InstrI->operands_begin();
103  OpE = InstrI->operands_end();
104  }
105  }
106 
107 protected:
108  /// MachineOperandIteratorBase - Create an iterator that visits all operands
109  /// on MI, or all operands on every instruction in the bundle containing MI.
110  ///
111  /// @param MI The instruction to examine.
112  /// @param WholeBundle When true, visit all operands on the entire bundle.
113  ///
114  explicit MachineOperandIteratorBase(MachineInstr &MI, bool WholeBundle) {
115  if (WholeBundle) {
116  InstrI = getBundleStart(MI.getIterator());
117  InstrE = MI.getParent()->instr_end();
118  } else {
119  InstrI = InstrE = MI.getIterator();
120  ++InstrE;
121  }
122  OpI = InstrI->operands_begin();
123  OpE = InstrI->operands_end();
124  if (WholeBundle)
125  advance();
126  }
127 
128  MachineOperand &deref() const { return *OpI; }
129 
130 public:
131  /// isValid - Returns true until all the operands have been visited.
132  bool isValid() const { return OpI != OpE; }
133 
134  /// Preincrement. Move to the next operand.
135  void operator++() {
136  assert(isValid() && "Cannot advance MIOperands beyond the last operand");
137  ++OpI;
138  advance();
139  }
140 
141  /// getOperandNo - Returns the number of the current operand relative to its
142  /// instruction.
143  ///
144  unsigned getOperandNo() const {
145  return OpI - InstrI->operands_begin();
146  }
147 
148  /// VirtRegInfo - Information about a virtual register used by a set of operands.
149  ///
150  struct VirtRegInfo {
151  /// Reads - One of the operands read the virtual register. This does not
152  /// include undef or internal use operands, see MO::readsReg().
153  bool Reads;
154 
155  /// Writes - One of the operands writes the virtual register.
156  bool Writes;
157 
158  /// Tied - Uses and defs must use the same register. This can be because of
159  /// a two-address constraint, or there may be a partial redefinition of a
160  /// sub-register.
161  bool Tied;
162  };
163 
164  /// Information about how a physical register Reg is used by a set of
165  /// operands.
166  struct PhysRegInfo {
167  /// There is a regmask operand indicating Reg is clobbered.
168  /// \see MachineOperand::CreateRegMask().
169  bool Clobbered;
170 
171  /// Reg or one of its aliases is defined. The definition may only cover
172  /// parts of the register.
173  bool Defined;
174  /// Reg or a super-register is defined. The definition covers the full
175  /// register.
177 
178  /// Reg or one of its aliases is read. The register may only be read
179  /// partially.
180  bool Read;
181  /// Reg or a super-register is read. The full register is read.
182  bool FullyRead;
183 
184  /// Either:
185  /// - Reg is FullyDefined and all defs of reg or an overlapping
186  /// register are dead, or
187  /// - Reg is completely dead because "defined" by a clobber.
188  bool DeadDef;
189 
190  /// Reg is Defined and all defs of reg or an overlapping register are
191  /// dead.
193 
194  /// There is a use operand of reg or a super-register with kill flag set.
195  bool Killed;
196  };
197 
198  /// analyzeVirtReg - Analyze how the current instruction or bundle uses a
199  /// virtual register. This function should not be called after operator++(),
200  /// it expects a fresh iterator.
201  ///
202  /// @param Reg The virtual register to analyze.
203  /// @param Ops When set, this vector will receive an (MI, OpNum) entry for
204  /// each operand referring to Reg.
205  /// @returns A filled-in RegInfo struct.
206  VirtRegInfo analyzeVirtReg(unsigned Reg,
207  SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops = nullptr);
208 
209  /// analyzePhysReg - Analyze how the current instruction or bundle uses a
210  /// physical register. This function should not be called after operator++(),
211  /// it expects a fresh iterator.
212  ///
213  /// @param Reg The physical register to analyze.
214  /// @returns A filled-in PhysRegInfo struct.
216 };
217 
218 /// MIOperands - Iterate over operands of a single instruction.
219 ///
221 public:
223  MachineOperand &operator* () const { return deref(); }
224  MachineOperand *operator->() const { return &deref(); }
225 };
226 
227 /// ConstMIOperands - Iterate over operands of a single const instruction.
228 ///
230 public:
232  : MachineOperandIteratorBase(const_cast<MachineInstr &>(MI), false) {}
233  const MachineOperand &operator* () const { return deref(); }
234  const MachineOperand *operator->() const { return &deref(); }
235 };
236 
237 /// MIBundleOperands - Iterate over all operands in a bundle of machine
238 /// instructions.
239 ///
241 public:
243  MachineOperand &operator* () const { return deref(); }
244  MachineOperand *operator->() const { return &deref(); }
245 };
246 
247 /// ConstMIBundleOperands - Iterate over all operands in a const bundle of
248 /// machine instructions.
249 ///
251 public:
253  : MachineOperandIteratorBase(const_cast<MachineInstr &>(MI), true) {}
254  const MachineOperand &operator* () const { return deref(); }
255  const MachineOperand *operator->() const { return &deref(); }
256 };
257 
258 } // End llvm namespace
259 
260 #endif
instr_iterator instr_end()
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool Tied
Tied - Uses and defs must use the same register.
MIBundleOperands - Iterate over all operands in a bundle of machine instructions. ...
unsigned Reg
unsigned const TargetRegisterInfo * TRI
block Block Frequency true
MachineOperandIteratorBase(MachineInstr &MI, bool WholeBundle)
MachineOperandIteratorBase - Create an iterator that visits all operands on MI, or all operands on ev...
void operator++()
Preincrement. Move to the next operand.
VirtRegInfo analyzeVirtReg(unsigned Reg, SmallVectorImpl< std::pair< MachineInstr *, unsigned > > *Ops=nullptr)
analyzeVirtReg - Analyze how the current instruction or bundle uses a virtual register.
ConstMIBundleOperands(const MachineInstr &MI)
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
PhysRegInfo analyzePhysReg(unsigned Reg, const TargetRegisterInfo *TRI)
analyzePhysReg - Analyze how the current instruction or bundle uses a physical register.
APInt operator*(APInt a, uint64_t RHS)
Definition: APInt.h:2090
bool isValid() const
isValid - Returns true until all the operands have been visited.
MachineBasicBlock::instr_iterator getBundleStart(MachineBasicBlock::instr_iterator I)
Returns an iterator to the first instruction in the bundle containing I.
MIBundleOperands(MachineInstr &MI)
bool PartialDeadDef
Reg is Defined and all defs of reg or an overlapping register are dead.
VirtRegInfo - Information about a virtual register used by a set of operands.
bool Writes
Writes - One of the operands writes the virtual register.
MachineOperand & deref() const
bool Read
Reg or one of its aliases is read.
bool Clobbered
There is a regmask operand indicating Reg is clobbered.
bool Killed
There is a use operand of reg or a super-register with kill flag set.
self_iterator getIterator()
Definition: ilist_node.h:81
bool Reads
Reads - One of the operands read the virtual register.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
unsigned getOperandNo() const
getOperandNo - Returns the number of the current operand relative to its instruction.
const MachineOperand * operator->() const
ConstMIBundleOperands - Iterate over all operands in a const bundle of machine instructions.
MachineOperand class - Representation of each machine instruction operand.
ConstMIOperands(const MachineInstr &MI)
bool finalizeBundles(MachineFunction &MF)
finalizeBundles - Finalize instruction bundles in the specified MachineFunction.
MIOperands - Iterate over operands of a single instruction.
bool FullyDefined
Reg or a super-register is defined.
bool FullyRead
Reg or a super-register is read. The full register is read.
ConstMIOperands - Iterate over operands of a single const instruction.
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:253
Representation of each machine instruction.
Definition: MachineInstr.h:63
MIOperands(MachineInstr &MI)
MachineOperand * operator->() const
#define I(x, y, z)
Definition: MD5.cpp:58
Information about how a physical register Reg is used by a set of operands.
Instructions::iterator instr_iterator
bool Defined
Reg or one of its aliases is defined.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
IRTranslator LLVM IR MI
MachineBasicBlock::instr_iterator getBundleEnd(MachineBasicBlock::instr_iterator I)
Returns an iterator pointing beyond the bundle containing I.
MachineOperand * operator->() const
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...
const MachineOperand * operator->() const
Instructions::const_iterator const_instr_iterator
MachineOperandIteratorBase - Iterator that can visit all operands on a MachineInstr, or all operands on a bundle of MachineInstrs.