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 //===----------------------------------------------------------------------===//
45 // MachineOperand iterator
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.
54 /// for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) {
55 /// if (!MIO->isReg())
60 class MachineOperandIteratorBase {
61 MachineBasicBlock::instr_iterator InstrI, InstrE;
62 MachineInstr::mop_iterator OpI, OpE;
64 // If the operands on InstrI are exhausted, advance InstrI to the next
65 // bundled instruction with operands.
68 // Don't advance off the basic block, or into a new bundle.
69 if (++InstrI == InstrE || !InstrI->isInsideBundle())
71 OpI = InstrI->operands_begin();
72 OpE = InstrI->operands_end();
77 /// MachineOperandIteratorBase - Create an iterator that visits all operands
78 /// on MI, or all operands on every instruction in the bundle containing MI.
80 /// @param MI The instruction to examine.
81 /// @param WholeBundle When true, visit all operands on the entire bundle.
83 explicit MachineOperandIteratorBase(MachineInstr *MI, bool WholeBundle) {
85 InstrI = MI->getBundleStart();
86 InstrE = MI->getParent()->instr_end();
91 OpI = InstrI->operands_begin();
92 OpE = InstrI->operands_end();
97 MachineOperand &deref() const { return *OpI; }
100 /// isValid - Returns true until all the operands have been visited.
101 bool isValid() const { return OpI != OpE; }
103 /// Preincrement. Move to the next operand.
105 assert(isValid() && "Cannot advance MIOperands beyond the last operand");
112 /// MIOperands - Iterate over operands of a single instruction.
114 class MIOperands : public MachineOperandIteratorBase {
116 MIOperands(MachineInstr *MI) : MachineOperandIteratorBase(MI, false) {}
117 MachineOperand &operator* () const { return deref(); }
118 MachineOperand *operator->() const { return &deref(); }
121 /// ConstMIOperands - Iterate over operands of a single const instruction.
123 class ConstMIOperands : public MachineOperandIteratorBase {
125 ConstMIOperands(const MachineInstr *MI)
126 : MachineOperandIteratorBase(const_cast<MachineInstr*>(MI), false) {}
127 const MachineOperand &operator* () const { return deref(); }
128 const MachineOperand *operator->() const { return &deref(); }
131 /// MIBundleOperands - Iterate over all operands in a bundle of machine
134 class MIBundleOperands : public MachineOperandIteratorBase {
136 MIBundleOperands(MachineInstr *MI) : MachineOperandIteratorBase(MI, true) {}
137 MachineOperand &operator* () const { return deref(); }
138 MachineOperand *operator->() const { return &deref(); }
141 /// ConstMIBundleOperands - Iterate over all operands in a const bundle of
142 /// machine instructions.
144 class ConstMIBundleOperands : public MachineOperandIteratorBase {
146 ConstMIBundleOperands(const MachineInstr *MI)
147 : MachineOperandIteratorBase(const_cast<MachineInstr*>(MI), true) {}
148 const MachineOperand &operator* () const { return deref(); }
149 const MachineOperand *operator->() const { return &deref(); }
152 } // End llvm namespace