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
18#include "llvm/CodeGen/MachineBasicBlock.h"
19
20namespace 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.
28void finalizeBundle(MachineBasicBlock &MBB,
29                    MachineBasicBlock::instr_iterator FirstMI,
30                    MachineBasicBlock::instr_iterator LastMI);
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.
37MachineBasicBlock::instr_iterator finalizeBundle(MachineBasicBlock &MBB,
38                    MachineBasicBlock::instr_iterator FirstMI);
39
40/// finalizeBundles - Finalize instruction bundles in the specified
41/// MachineFunction. Return true if any bundles are finalized.
42bool finalizeBundles(MachineFunction &MF);
43
44/// Returns an iterator to the first instruction in the bundle containing \p I.
45inline MachineBasicBlock::instr_iterator getBundleStart(
46    MachineBasicBlock::instr_iterator 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.
53inline MachineBasicBlock::const_instr_iterator getBundleStart(
54    MachineBasicBlock::const_instr_iterator I) {
55  while (I->isBundledWithPred())
56    --I;
57  return I;
58}
59
60/// Returns an iterator pointing beyond the bundle containing \p I.
61inline MachineBasicBlock::instr_iterator getBundleEnd(
62    MachineBasicBlock::instr_iterator I) {
63  while (I->isBundledWithSucc())
64    ++I;
65  return ++I;
66}
67
68/// Returns an iterator pointing beyond the bundle containing \p I.
69inline MachineBasicBlock::const_instr_iterator getBundleEnd(
70    MachineBasicBlock::const_instr_iterator 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///
92class MachineOperandIteratorBase {
93  MachineBasicBlock::instr_iterator InstrI, InstrE;
94  MachineInstr::mop_iterator OpI, OpE;
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
108protected:
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
131public:
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.
177    bool FullyDefined;
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.
193    bool PartialDeadDef;
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.
216  PhysRegInfo analyzePhysReg(unsigned Reg, const TargetRegisterInfo *TRI);
217};
218
219/// MIOperands - Iterate over operands of a single instruction.
220///
221class MIOperands : public MachineOperandIteratorBase {
222public:
223  MIOperands(MachineInstr &MI) : MachineOperandIteratorBase(MI, false) {}
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///
230class ConstMIOperands : public MachineOperandIteratorBase {
231public:
232  ConstMIOperands(const MachineInstr &MI)
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///
241class MIBundleOperands : public MachineOperandIteratorBase {
242public:
243  MIBundleOperands(MachineInstr &MI) : MachineOperandIteratorBase(MI, true) {}
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///
251class ConstMIBundleOperands : public MachineOperandIteratorBase {
252public:
253  ConstMIBundleOperands(const MachineInstr &MI)
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
262