From: Jakob Stoklund Olesen Date: Wed, 29 Feb 2012 00:33:41 +0000 (+0000) Subject: Move the operand iterator into MachineInstrBundle.h where it belongs. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=30e98a03a3c524026e2da2607e04bb655b0b6350;p=oota-llvm.git Move the operand iterator into MachineInstrBundle.h where it belongs. Extract a base class and provide four specific sub-classes for iterating over const/non-const bundles/instructions. This eliminates the mystery bool constructor argument. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@151684 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/CodeGen/MachineBasicBlock.h b/include/llvm/CodeGen/MachineBasicBlock.h index 1138f92bf86..576ce944469 100644 --- a/include/llvm/CodeGen/MachineBasicBlock.h +++ b/include/llvm/CodeGen/MachineBasicBlock.h @@ -699,73 +699,6 @@ template <> struct GraphTraits > { } }; -//===----------------------------------------------------------------------===// -// MachineOperand iterator -// - -/// MachineOperands - Iterator that can visit all operands on a MachineInstr, -/// or all operands on a bundle of MachineInstrs. -/// -/// Intended use: -/// -/// for (MIOperands MIO(MI, true); MIO.isValid(); ++MIO) { -/// if (!MIO->isReg()) -/// continue; -/// ... -/// } -/// -class MIOperands { - MachineBasicBlock::instr_iterator InstrI, InstrE; - MachineInstr::mop_iterator OpI, OpE; - - // If the operands on InstrI are exhausted, advance InstrI to the next - // bundled instruction with operands. - void advance() { - while (OpI == OpE) { - // Don't advance off the basic block, or into a new bundle. - if (++InstrI == InstrE || !InstrI->isInsideBundle()) - break; - OpI = InstrI->operands_begin(); - OpE = InstrI->operands_end(); - } - } - -public: - /// MIOperands - Create an iterator that visits all operands on MI, or all - /// operands on every instruction in the bundle containing MI. - /// - /// @param MI The instruction to examine. - /// @param WholeBundle When true, visit all operands on the entire bundle. - /// - explicit MIOperands(MachineInstr *MI, bool WholeBundle = false) { - if (WholeBundle) { - InstrI = MI->getBundleStart(); - InstrE = MI->getParent()->instr_end(); - } else { - InstrI = InstrE = MI; - ++InstrE; - } - OpI = InstrI->operands_begin(); - OpE = InstrI->operands_end(); - if (WholeBundle) - advance(); - } - - /// isValid - Returns true until all the operands have been visited. - bool isValid() const { return OpI != OpE; } - - /// Preincrement. Move to the next operand. - MIOperands &operator++() { - assert(isValid() && "Cannot advance MIOperands beyond the last operand"); - ++OpI; - advance(); - return *this; - } - - MachineOperand &operator* () const { return *OpI; } - MachineOperand *operator->() const { return &*OpI; } -}; - } // End llvm namespace #endif diff --git a/include/llvm/CodeGen/MachineInstrBundle.h b/include/llvm/CodeGen/MachineInstrBundle.h index e6fcdad5072..f6525a827b5 100644 --- a/include/llvm/CodeGen/MachineInstrBundle.h +++ b/include/llvm/CodeGen/MachineInstrBundle.h @@ -41,6 +41,114 @@ MachineBasicBlock::instr_iterator finalizeBundle(MachineBasicBlock &MBB, /// MachineFunction. Return true if any bundles are finalized. bool finalizeBundles(MachineFunction &MF); +//===----------------------------------------------------------------------===// +// MachineOperand iterator +// + +/// MachineOperandIteratorBase - Iterator that can visit all operands on a +/// MachineInstr, or all operands on a bundle of MachineInstrs. This class is +/// not intended to be used directly, use one of the sub-classes instead. +/// +/// Intended use: +/// +/// for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) { +/// if (!MIO->isReg()) +/// continue; +/// ... +/// } +/// +class MachineOperandIteratorBase { + MachineBasicBlock::instr_iterator InstrI, InstrE; + MachineInstr::mop_iterator OpI, OpE; + + // If the operands on InstrI are exhausted, advance InstrI to the next + // bundled instruction with operands. + void advance() { + while (OpI == OpE) { + // Don't advance off the basic block, or into a new bundle. + if (++InstrI == InstrE || !InstrI->isInsideBundle()) + break; + OpI = InstrI->operands_begin(); + OpE = InstrI->operands_end(); + } + } + +protected: + /// MachineOperandIteratorBase - Create an iterator that visits all operands + /// on MI, or all operands on every instruction in the bundle containing MI. + /// + /// @param MI The instruction to examine. + /// @param WholeBundle When true, visit all operands on the entire bundle. + /// + explicit MachineOperandIteratorBase(MachineInstr *MI, bool WholeBundle) { + if (WholeBundle) { + InstrI = MI->getBundleStart(); + InstrE = MI->getParent()->instr_end(); + } else { + InstrI = InstrE = MI; + ++InstrE; + } + OpI = InstrI->operands_begin(); + OpE = InstrI->operands_end(); + if (WholeBundle) + advance(); + } + + MachineOperand &deref() const { return *OpI; } + +public: + /// isValid - Returns true until all the operands have been visited. + bool isValid() const { return OpI != OpE; } + + /// Preincrement. Move to the next operand. + void operator++() { + assert(isValid() && "Cannot advance MIOperands beyond the last operand"); + ++OpI; + advance(); + } + +}; + +/// MIOperands - Iterate over operands of a single instruction. +/// +class MIOperands : public MachineOperandIteratorBase { +public: + MIOperands(MachineInstr *MI) : MachineOperandIteratorBase(MI, false) {} + MachineOperand &operator* () const { return deref(); } + MachineOperand *operator->() const { return &deref(); } +}; + +/// ConstMIOperands - Iterate over operands of a single const instruction. +/// +class ConstMIOperands : public MachineOperandIteratorBase { +public: + ConstMIOperands(const MachineInstr *MI) + : MachineOperandIteratorBase(const_cast(MI), false) {} + const MachineOperand &operator* () const { return deref(); } + const MachineOperand *operator->() const { return &deref(); } +}; + +/// MIBundleOperands - Iterate over all operands in a bundle of machine +/// instructions. +/// +class MIBundleOperands : public MachineOperandIteratorBase { +public: + MIBundleOperands(MachineInstr *MI) : MachineOperandIteratorBase(MI, true) {} + MachineOperand &operator* () const { return deref(); } + MachineOperand *operator->() const { return &deref(); } +}; + +/// ConstMIBundleOperands - Iterate over all operands in a const bundle of +/// machine instructions. +/// +class ConstMIBundleOperands : public MachineOperandIteratorBase { +public: + ConstMIBundleOperands(const MachineInstr *MI) + : MachineOperandIteratorBase(const_cast(MI), true) {} + const MachineOperand &operator* () const { return deref(); } + const MachineOperand *operator->() const { return &deref(); } +}; + } // End llvm namespace #endif diff --git a/lib/CodeGen/MachineVerifier.cpp b/lib/CodeGen/MachineVerifier.cpp index ace8252385a..62e263ac900 100644 --- a/lib/CodeGen/MachineVerifier.cpp +++ b/lib/CodeGen/MachineVerifier.cpp @@ -28,6 +28,7 @@ #include "llvm/CodeGen/LiveIntervalAnalysis.h" #include "llvm/CodeGen/LiveVariables.h" #include "llvm/CodeGen/LiveStackAnalysis.h" +#include "llvm/CodeGen/MachineInstrBundle.h" #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachineFrameInfo.h" #include "llvm/CodeGen/MachineMemOperand.h" @@ -1104,7 +1105,7 @@ void MachineVerifier::verifyLiveIntervals() { } } else { // Non-PHI def. - MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def); + const MachineInstr *MI = LiveInts->getInstructionFromIndex(VNI->def); if (!MI) { report("No instruction at def index", MF); *OS << "Valno #" << VNI->id << " is defined at " << VNI->def @@ -1114,7 +1115,7 @@ void MachineVerifier::verifyLiveIntervals() { bool hasDef = false; bool isEarlyClobber = false; - for (MIOperands MOI(MI, true); MOI.isValid(); ++MOI) { + for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) { if (!MOI->isReg() || !MOI->isDef()) continue; if (TargetRegisterInfo::isVirtualRegister(LI.reg)) { @@ -1197,7 +1198,7 @@ void MachineVerifier::verifyLiveIntervals() { continue; // The live segment is ending inside EndMBB - MachineInstr *MI = + const MachineInstr *MI = LiveInts->getInstructionFromIndex(I->end.getPrevSlot()); if (!MI) { report("Live segment doesn't end at a valid instruction", EndMBB); @@ -1242,7 +1243,7 @@ void MachineVerifier::verifyLiveIntervals() { // use, or a dead flag on a def. bool hasRead = false; bool hasDeadDef = false; - for (MIOperands MOI(MI, true); MOI.isValid(); ++MOI) { + for (ConstMIBundleOperands MOI(MI); MOI.isValid(); ++MOI) { if (!MOI->isReg() || MOI->getReg() != LI.reg) continue; if (MOI->readsReg())