Add an analyzeVirtReg() function.
[oota-llvm.git] / include / llvm / CodeGen / MachineInstrBundle.h
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
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,
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.
37 MachineBasicBlock::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.
42 bool finalizeBundles(MachineFunction &MF);
43
44 //===----------------------------------------------------------------------===//
45 // MachineOperand iterator
46 //
47
48 /// MachineOperandIteratorBase - Iterator that can visit all operands on a
49 /// MachineInstr, or all operands on a bundle of MachineInstrs.  This class is
50 /// not intended to be used directly, use one of the sub-classes instead.
51 ///
52 /// Intended use:
53 ///
54 ///   for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) {
55 ///     if (!MIO->isReg())
56 ///       continue;
57 ///     ...
58 ///   }
59 ///
60 class MachineOperandIteratorBase {
61   MachineBasicBlock::instr_iterator InstrI, InstrE;
62   MachineInstr::mop_iterator OpI, OpE;
63
64   // If the operands on InstrI are exhausted, advance InstrI to the next
65   // bundled instruction with operands.
66   void advance() {
67     while (OpI == OpE) {
68       // Don't advance off the basic block, or into a new bundle.
69       if (++InstrI == InstrE || !InstrI->isInsideBundle())
70         break;
71       OpI = InstrI->operands_begin();
72       OpE = InstrI->operands_end();
73     }
74   }
75
76 protected:
77   /// MachineOperandIteratorBase - Create an iterator that visits all operands
78   /// on MI, or all operands on every instruction in the bundle containing MI.
79   ///
80   /// @param MI The instruction to examine.
81   /// @param WholeBundle When true, visit all operands on the entire bundle.
82   ///
83   explicit MachineOperandIteratorBase(MachineInstr *MI, bool WholeBundle) {
84     if (WholeBundle) {
85       InstrI = MI->getBundleStart();
86       InstrE = MI->getParent()->instr_end();
87     } else {
88       InstrI = InstrE = MI;
89       ++InstrE;
90     }
91     OpI = InstrI->operands_begin();
92     OpE = InstrI->operands_end();
93     if (WholeBundle)
94       advance();
95   }
96
97   MachineOperand &deref() const { return *OpI; }
98
99 public:
100   /// isValid - Returns true until all the operands have been visited.
101   bool isValid() const { return OpI != OpE; }
102
103   /// Preincrement.  Move to the next operand.
104   void operator++() {
105     assert(isValid() && "Cannot advance MIOperands beyond the last operand");
106     ++OpI;
107     advance();
108   }
109
110   /// getOperandNo - Returns the number of the current operand relative to its
111   /// instruction.
112   ///
113   unsigned getOperandNo() const {
114     return OpI - InstrI->operands_begin();
115   }
116
117   /// RegInfo - Information about a virtual register used by a set of operands.
118   ///
119   struct RegInfo {
120     /// Reads - One of the operands read the virtual register.  This does not
121     /// include <undef> or <internal> use operands, see MO::readsReg().
122     bool Reads;
123
124     /// Writes - One of the operands writes the virtual register.
125     bool Writes;
126
127     /// Tied - Uses and defs must use the same register. This can be because of
128     /// a two-address constraint, or there may be a partial redefinition of a
129     /// sub-register.
130     bool Tied;
131   };
132
133   /// analyzeVirtReg - Analyze how the current instruction or bundle uses a
134   /// virtual register.  This function should not be called after operator++(),
135   /// it expects a fresh iterator.
136   ///
137   /// @param Reg The virtual register to analyze.
138   /// @param Ops When set, this vector will receive an (MI, OpNum) entry for
139   ///            each operand referring to Reg.
140   /// @returns A filled-in RegInfo struct.
141   RegInfo analyzeVirtReg(unsigned Reg,
142                  SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops = 0);
143 };
144
145 /// MIOperands - Iterate over operands of a single instruction.
146 ///
147 class MIOperands : public MachineOperandIteratorBase {
148 public:
149   MIOperands(MachineInstr *MI) : MachineOperandIteratorBase(MI, false) {}
150   MachineOperand &operator* () const { return deref(); }
151   MachineOperand *operator->() const { return &deref(); }
152 };
153
154 /// ConstMIOperands - Iterate over operands of a single const instruction.
155 ///
156 class ConstMIOperands : public MachineOperandIteratorBase {
157 public:
158   ConstMIOperands(const MachineInstr *MI)
159     : MachineOperandIteratorBase(const_cast<MachineInstr*>(MI), false) {}
160   const MachineOperand &operator* () const { return deref(); }
161   const MachineOperand *operator->() const { return &deref(); }
162 };
163
164 /// MIBundleOperands - Iterate over all operands in a bundle of machine
165 /// instructions.
166 ///
167 class MIBundleOperands : public MachineOperandIteratorBase {
168 public:
169   MIBundleOperands(MachineInstr *MI) : MachineOperandIteratorBase(MI, true) {}
170   MachineOperand &operator* () const { return deref(); }
171   MachineOperand *operator->() const { return &deref(); }
172 };
173
174 /// ConstMIBundleOperands - Iterate over all operands in a const bundle of
175 /// machine instructions.
176 ///
177 class ConstMIBundleOperands : public MachineOperandIteratorBase {
178 public:
179   ConstMIBundleOperands(const MachineInstr *MI)
180     : MachineOperandIteratorBase(const_cast<MachineInstr*>(MI), true) {}
181   const MachineOperand &operator* () const { return deref(); }
182   const MachineOperand *operator->() const { return &deref(); }
183 };
184
185 } // End llvm namespace
186
187 #endif