1 //===-- CodeGen/MachineInstBundle.h - MI bundle utilities -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file provide utility functions to manipulate machine instruction
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
16 #define LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
18 #include "llvm/CodeGen/MachineBasicBlock.h"
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
28 void finalizeBundle(MachineBasicBlock &MBB,
29 MachineBasicBlock::instr_iterator FirstMI,
30 MachineBasicBlock::instr_iterator LastMI);
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.
37 MachineBasicBlock::instr_iterator finalizeBundle(MachineBasicBlock &MBB,
38 MachineBasicBlock::instr_iterator FirstMI);
40 /// finalizeBundles - Finalize instruction bundles in the specified
41 /// MachineFunction. Return true if any bundles are finalized.
42 bool finalizeBundles(MachineFunction &MF);
44 /// getBundleStart - Returns the first instruction in the bundle containing MI.
46 inline MachineInstr *getBundleStart(MachineInstr *MI) {
47 MachineBasicBlock::instr_iterator I = MI;
48 while (I->isInsideBundle())
53 inline const MachineInstr *getBundleStart(const MachineInstr *MI) {
54 MachineBasicBlock::const_instr_iterator I = MI;
55 while (I->isInsideBundle())
60 //===----------------------------------------------------------------------===//
61 // MachineOperand iterator
64 /// MachineOperandIteratorBase - Iterator that can visit all operands on a
65 /// MachineInstr, or all operands on a bundle of MachineInstrs. This class is
66 /// not intended to be used directly, use one of the sub-classes instead.
70 /// for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) {
71 /// if (!MIO->isReg())
76 class MachineOperandIteratorBase {
77 MachineBasicBlock::instr_iterator InstrI, InstrE;
78 MachineInstr::mop_iterator OpI, OpE;
80 // If the operands on InstrI are exhausted, advance InstrI to the next
81 // bundled instruction with operands.
84 // Don't advance off the basic block, or into a new bundle.
85 if (++InstrI == InstrE || !InstrI->isInsideBundle())
87 OpI = InstrI->operands_begin();
88 OpE = InstrI->operands_end();
93 /// MachineOperandIteratorBase - Create an iterator that visits all operands
94 /// on MI, or all operands on every instruction in the bundle containing MI.
96 /// @param MI The instruction to examine.
97 /// @param WholeBundle When true, visit all operands on the entire bundle.
99 explicit MachineOperandIteratorBase(MachineInstr *MI, bool WholeBundle) {
101 InstrI = getBundleStart(MI);
102 InstrE = MI->getParent()->instr_end();
104 InstrI = InstrE = MI;
107 OpI = InstrI->operands_begin();
108 OpE = InstrI->operands_end();
113 MachineOperand &deref() const { return *OpI; }
116 /// isValid - Returns true until all the operands have been visited.
117 bool isValid() const { return OpI != OpE; }
119 /// Preincrement. Move to the next operand.
121 assert(isValid() && "Cannot advance MIOperands beyond the last operand");
126 /// getOperandNo - Returns the number of the current operand relative to its
129 unsigned getOperandNo() const {
130 return OpI - InstrI->operands_begin();
133 /// VirtRegInfo - Information about a virtual register used by a set of operands.
136 /// Reads - One of the operands read the virtual register. This does not
137 /// include <undef> or <internal> use operands, see MO::readsReg().
140 /// Writes - One of the operands writes the virtual register.
143 /// Tied - Uses and defs must use the same register. This can be because of
144 /// a two-address constraint, or there may be a partial redefinition of a
149 /// PhysRegInfo - Information about a physical register used by a set of
152 /// Clobbers - Reg or an overlapping register is defined, or a regmask
156 /// Defines - Reg or a super-register is defined.
159 /// Reads - Read or a super-register is read.
162 /// ReadsOverlap - Reg or an overlapping register is read.
165 /// DefinesDead - All defs of a Reg or a super-register are dead.
168 /// There is a kill of Reg or a super-register.
172 /// analyzeVirtReg - Analyze how the current instruction or bundle uses a
173 /// virtual register. This function should not be called after operator++(),
174 /// it expects a fresh iterator.
176 /// @param Reg The virtual register to analyze.
177 /// @param Ops When set, this vector will receive an (MI, OpNum) entry for
178 /// each operand referring to Reg.
179 /// @returns A filled-in RegInfo struct.
180 VirtRegInfo analyzeVirtReg(unsigned Reg,
181 SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops = 0);
183 /// analyzePhysReg - Analyze how the current instruction or bundle uses a
184 /// physical register. This function should not be called after operator++(),
185 /// it expects a fresh iterator.
187 /// @param Reg The physical register to analyze.
188 /// @returns A filled-in PhysRegInfo struct.
189 PhysRegInfo analyzePhysReg(unsigned Reg, const TargetRegisterInfo *TRI);
192 /// MIOperands - Iterate over operands of a single instruction.
194 class MIOperands : public MachineOperandIteratorBase {
196 MIOperands(MachineInstr *MI) : MachineOperandIteratorBase(MI, false) {}
197 MachineOperand &operator* () const { return deref(); }
198 MachineOperand *operator->() const { return &deref(); }
201 /// ConstMIOperands - Iterate over operands of a single const instruction.
203 class ConstMIOperands : public MachineOperandIteratorBase {
205 ConstMIOperands(const MachineInstr *MI)
206 : MachineOperandIteratorBase(const_cast<MachineInstr*>(MI), false) {}
207 const MachineOperand &operator* () const { return deref(); }
208 const MachineOperand *operator->() const { return &deref(); }
211 /// MIBundleOperands - Iterate over all operands in a bundle of machine
214 class MIBundleOperands : public MachineOperandIteratorBase {
216 MIBundleOperands(MachineInstr *MI) : MachineOperandIteratorBase(MI, true) {}
217 MachineOperand &operator* () const { return deref(); }
218 MachineOperand *operator->() const { return &deref(); }
221 /// ConstMIBundleOperands - Iterate over all operands in a const bundle of
222 /// machine instructions.
224 class ConstMIBundleOperands : public MachineOperandIteratorBase {
226 ConstMIBundleOperands(const MachineInstr *MI)
227 : MachineOperandIteratorBase(const_cast<MachineInstr*>(MI), true) {}
228 const MachineOperand &operator* () const { return deref(); }
229 const MachineOperand *operator->() const { return &deref(); }
232 } // End llvm namespace