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