Line data Source code
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 : /// Returns an iterator to the first instruction in the bundle containing \p I.
45 : inline MachineBasicBlock::instr_iterator getBundleStart(
46 : MachineBasicBlock::instr_iterator I) {
47 26511657 : while (I->isBundledWithPred())
48 : --I;
49 : return I;
50 : }
51 :
52 : /// Returns an iterator to the first instruction in the bundle containing \p I.
53 : inline MachineBasicBlock::const_instr_iterator getBundleStart(
54 : MachineBasicBlock::const_instr_iterator I) {
55 50736452 : while (I->isBundledWithPred())
56 : --I;
57 : return I;
58 : }
59 :
60 : /// Returns an iterator pointing beyond the bundle containing \p I.
61 : inline MachineBasicBlock::instr_iterator getBundleEnd(
62 : MachineBasicBlock::instr_iterator I) {
63 89 : while (I->isBundledWithSucc())
64 : ++I;
65 : return ++I;
66 : }
67 :
68 : /// Returns an iterator pointing beyond the bundle containing \p I.
69 : inline MachineBasicBlock::const_instr_iterator getBundleEnd(
70 : MachineBasicBlock::const_instr_iterator I) {
71 0 : 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 : ///
92 : class 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 132851831 : void advance() {
99 132860398 : while (OpI == OpE) {
100 : // Don't advance off the basic block, or into a new bundle.
101 24397620 : if (++InstrI == InstrE || !InstrI->isInsideBundle())
102 : break;
103 8567 : OpI = InstrI->operands_begin();
104 17134 : OpE = InstrI->operands_end();
105 : }
106 132851831 : }
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 26273652 : explicit MachineOperandIteratorBase(MachineInstr &MI, bool WholeBundle) {
116 26273652 : if (WholeBundle) {
117 26269252 : InstrI = getBundleStart(MI.getIterator());
118 52538504 : InstrE = MI.getParent()->instr_end();
119 : } else {
120 4400 : InstrI = InstrE = MI.getIterator();
121 : ++InstrE;
122 : }
123 26273652 : OpI = InstrI->operands_begin();
124 26273652 : OpE = InstrI->operands_end();
125 26273652 : if (WholeBundle)
126 26269252 : advance();
127 26273652 : }
128 :
129 0 : MachineOperand &deref() const { return *OpI; }
130 :
131 : public:
132 : /// isValid - Returns true until all the operands have been visited.
133 0 : 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 106582579 : ++OpI;
139 106582579 : advance();
140 : }
141 :
142 : /// getOperandNo - Returns the number of the current operand relative to its
143 : /// instruction.
144 : ///
145 : unsigned getOperandNo() const {
146 198062 : 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 : ///
221 : class MIOperands : public MachineOperandIteratorBase {
222 : public:
223 28 : 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 : ///
230 : class ConstMIOperands : public MachineOperandIteratorBase {
231 : public:
232 : ConstMIOperands(const MachineInstr &MI)
233 4372 : : 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 : ///
241 : class MIBundleOperands : public MachineOperandIteratorBase {
242 : public:
243 208208 : MIBundleOperands(MachineInstr &MI) : MachineOperandIteratorBase(MI, true) {}
244 : MachineOperand &operator* () const { return deref(); }
245 17255 : MachineOperand *operator->() const { return &deref(); }
246 : };
247 :
248 : /// ConstMIBundleOperands - Iterate over all operands in a const bundle of
249 : /// machine instructions.
250 : ///
251 : class ConstMIBundleOperands : public MachineOperandIteratorBase {
252 : public:
253 : ConstMIBundleOperands(const MachineInstr &MI)
254 26061044 : : MachineOperandIteratorBase(const_cast<MachineInstr &>(MI), true) {}
255 : const MachineOperand &operator* () const { return deref(); }
256 6008432 : const MachineOperand *operator->() const { return &deref(); }
257 : };
258 :
259 : } // End llvm namespace
260 :
261 : #endif
|