LLVM  8.0.1
MachineInstrBundle.h
Go to the documentation of this file.
1 //===-- CodeGen/MachineInstBundle.h - MI bundle utilities -------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provide utility functions to manipulate machine instruction
11 // bundles.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
16 #define LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
17 
19 
20 namespace 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.
28 void finalizeBundle(MachineBasicBlock &MBB,
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.
39 
40 /// finalizeBundles - Finalize instruction bundles in the specified
41 /// MachineFunction. Return true if any bundles are finalized.
42 bool finalizeBundles(MachineFunction &MF);
43 
44 /// Returns an iterator to the first instruction in the bundle containing \p I.
47  while (I->isBundledWithPred())
48  --I;
49  return I;
50 }
51 
52 /// Returns an iterator to the first instruction in the bundle containing \p I.
55  while (I->isBundledWithPred())
56  --I;
57  return I;
58 }
59 
60 /// Returns an iterator pointing beyond the bundle containing \p I.
63  while (I->isBundledWithSucc())
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  return ++I;
74 }
75 
76 //===----------------------------------------------------------------------===//
77 // MachineOperand iterator
78 //
79 
80 /// MachineOperandIteratorBase - Iterator that can visit all operands on a
81 /// MachineInstr, or all operands on a bundle of MachineInstrs. This class is
82 /// not intended to be used directly, use one of the sub-classes instead.
83 ///
84 /// Intended use:
85 ///
86 /// for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) {
87 /// if (!MIO->isReg())
88 /// continue;
89 /// ...
90 /// }
91 ///
93  MachineBasicBlock::instr_iterator InstrI, InstrE;
95 
96  // If the operands on InstrI are exhausted, advance InstrI to the next
97  // bundled instruction with operands.
98  void advance() {
99  while (OpI == OpE) {
100  // Don't advance off the basic block, or into a new bundle.
101  if (++InstrI == InstrE || !InstrI->isInsideBundle())
102  break;
103  OpI = InstrI->operands_begin();
104  OpE = InstrI->operands_end();
105  }
106  }
107 
108 protected:
109  /// MachineOperandIteratorBase - Create an iterator that visits all operands
110  /// on MI, or all operands on every instruction in the bundle containing MI.
111  ///
112  /// @param MI The instruction to examine.
113  /// @param WholeBundle When true, visit all operands on the entire bundle.
114  ///
115  explicit MachineOperandIteratorBase(MachineInstr &MI, bool WholeBundle) {
116  if (WholeBundle) {
117  InstrI = getBundleStart(MI.getIterator());
118  InstrE = MI.getParent()->instr_end();
119  } else {
120  InstrI = InstrE = MI.getIterator();
121  ++InstrE;
122  }
123  OpI = InstrI->operands_begin();
124  OpE = InstrI->operands_end();
125  if (WholeBundle)
126  advance();
127  }
128 
129  MachineOperand &deref() const { return *OpI; }
130 
131 public:
132  /// isValid - Returns true until all the operands have been visited.
133  bool isValid() const { return OpI != OpE; }
134 
135  /// Preincrement. Move to the next operand.
136  void operator++() {
137  assert(isValid() && "Cannot advance MIOperands beyond the last operand");
138  ++OpI;
139  advance();
140  }
141 
142  /// getOperandNo - Returns the number of the current operand relative to its
143  /// instruction.
144  ///
145  unsigned getOperandNo() const {
146  return OpI - InstrI->operands_begin();
147  }
148 
149  /// VirtRegInfo - Information about a virtual register used by a set of operands.
150  ///
151  struct VirtRegInfo {
152  /// Reads - One of the operands read the virtual register. This does not
153  /// include undef or internal use operands, see MO::readsReg().
154  bool Reads;
155 
156  /// Writes - One of the operands writes the virtual register.
157  bool Writes;
158 
159  /// Tied - Uses and defs must use the same register. This can be because of
160  /// a two-address constraint, or there may be a partial redefinition of a
161  /// sub-register.
162  bool Tied;
163  };
164 
165  /// Information about how a physical register Reg is used by a set of
166  /// operands.
167  struct PhysRegInfo {
168  /// There is a regmask operand indicating Reg is clobbered.
169  /// \see MachineOperand::CreateRegMask().
170  bool Clobbered;
171 
172  /// Reg or one of its aliases is defined. The definition may only cover
173  /// parts of the register.
174  bool Defined;
175  /// Reg or a super-register is defined. The definition covers the full
176  /// register.
178 
179  /// Reg or one of its aliases is read. The register may only be read
180  /// partially.
181  bool Read;
182  /// Reg or a super-register is read. The full register is read.
183  bool FullyRead;
184 
185  /// Either:
186  /// - Reg is FullyDefined and all defs of reg or an overlapping
187  /// register are dead, or
188  /// - Reg is completely dead because "defined" by a clobber.
189  bool DeadDef;
190 
191  /// Reg is Defined and all defs of reg or an overlapping register are
192  /// dead.
194 
195  /// There is a use operand of reg or a super-register with kill flag set.
196  bool Killed;
197  };
198 
199  /// analyzeVirtReg - Analyze how the current instruction or bundle uses a
200  /// virtual register. This function should not be called after operator++(),
201  /// it expects a fresh iterator.
202  ///
203  /// @param Reg The virtual register to analyze.
204  /// @param Ops When set, this vector will receive an (MI, OpNum) entry for
205  /// each operand referring to Reg.
206  /// @returns A filled-in RegInfo struct.
207  VirtRegInfo analyzeVirtReg(unsigned Reg,
208  SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops = nullptr);
209 
210  /// analyzePhysReg - Analyze how the current instruction or bundle uses a
211  /// physical register. This function should not be called after operator++(),
212  /// it expects a fresh iterator.
213  ///
214  /// @param Reg The physical register to analyze.
215  /// @returns A filled-in PhysRegInfo struct.
217 };
218 
219 /// MIOperands - Iterate over operands of a single instruction.
220 ///
222 public:
224  MachineOperand &operator* () const { return deref(); }
225  MachineOperand *operator->() const { return &deref(); }
226 };
227 
228 /// ConstMIOperands - Iterate over operands of a single const instruction.
229 ///
231 public:
233  : MachineOperandIteratorBase(const_cast<MachineInstr &>(MI), false) {}
234  const MachineOperand &operator* () const { return deref(); }
235  const MachineOperand *operator->() const { return &deref(); }
236 };
237 
238 /// MIBundleOperands - Iterate over all operands in a bundle of machine
239 /// instructions.
240 ///
242 public:
244  MachineOperand &operator* () const { return deref(); }
245  MachineOperand *operator->() const { return &deref(); }
246 };
247 
248 /// ConstMIBundleOperands - Iterate over all operands in a const bundle of
249 /// machine instructions.
250 ///
252 public:
254  : MachineOperandIteratorBase(const_cast<MachineInstr &>(MI), true) {}
255  const MachineOperand &operator* () const { return deref(); }
256  const MachineOperand *operator->() const { return &deref(); }
257 };
258 
259 } // End llvm namespace
260 
261 #endif
instr_iterator instr_end()
This class represents lattice values for constants.
Definition: AllocatorList.h:24
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:42
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:2091
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:82
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:254
Representation of each machine instruction.
Definition: MachineInstr.h:64
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.