LLVM API Documentation

MachineInstrBundle.h
Go to the documentation of this file.
00001 //===-- CodeGen/MachineInstBundle.h - MI bundle utilities -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file provide utility functions to manipulate machine instruction
00011 // bundles.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
00016 #define LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
00017 
00018 #include "llvm/CodeGen/MachineBasicBlock.h"
00019 
00020 namespace llvm {
00021 
00022 /// finalizeBundle - Finalize a machine instruction bundle which includes
00023 /// a sequence of instructions starting from FirstMI to LastMI (exclusive).
00024 /// This routine adds a BUNDLE instruction to represent the bundle, it adds
00025 /// IsInternalRead markers to MachineOperands which are defined inside the
00026 /// bundle, and it copies externally visible defs and uses to the BUNDLE
00027 /// instruction.
00028 void finalizeBundle(MachineBasicBlock &MBB,
00029                     MachineBasicBlock::instr_iterator FirstMI,
00030                     MachineBasicBlock::instr_iterator LastMI);
00031   
00032 /// finalizeBundle - Same functionality as the previous finalizeBundle except
00033 /// the last instruction in the bundle is not provided as an input. This is
00034 /// used in cases where bundles are pre-determined by marking instructions
00035 /// with 'InsideBundle' marker. It returns the MBB instruction iterator that
00036 /// points to the end of the bundle.
00037 MachineBasicBlock::instr_iterator finalizeBundle(MachineBasicBlock &MBB,
00038                     MachineBasicBlock::instr_iterator FirstMI);
00039 
00040 /// finalizeBundles - Finalize instruction bundles in the specified
00041 /// MachineFunction. Return true if any bundles are finalized.
00042 bool finalizeBundles(MachineFunction &MF);
00043 
00044 /// getBundleStart - Returns the first instruction in the bundle containing MI.
00045 ///
00046 inline MachineInstr *getBundleStart(MachineInstr *MI) {
00047   MachineBasicBlock::instr_iterator I = MI;
00048   while (I->isBundledWithPred())
00049     --I;
00050   return I;
00051 }
00052 
00053 inline const MachineInstr *getBundleStart(const MachineInstr *MI) {
00054   MachineBasicBlock::const_instr_iterator I = MI;
00055   while (I->isBundledWithPred())
00056     --I;
00057   return I;
00058 }
00059 
00060 /// Return an iterator pointing beyond the bundle containing MI.
00061 inline MachineBasicBlock::instr_iterator
00062 getBundleEnd(MachineInstr *MI) {
00063   MachineBasicBlock::instr_iterator I = MI;
00064   while (I->isBundledWithSucc())
00065     ++I;
00066   return ++I;
00067 }
00068 
00069 /// Return an iterator pointing beyond the bundle containing MI.
00070 inline MachineBasicBlock::const_instr_iterator
00071 getBundleEnd(const MachineInstr *MI) {
00072   MachineBasicBlock::const_instr_iterator I = MI;
00073   while (I->isBundledWithSucc())
00074     ++I;
00075   return ++I;
00076 }
00077 
00078 //===----------------------------------------------------------------------===//
00079 // MachineOperand iterator
00080 //
00081 
00082 /// MachineOperandIteratorBase - Iterator that can visit all operands on a
00083 /// MachineInstr, or all operands on a bundle of MachineInstrs.  This class is
00084 /// not intended to be used directly, use one of the sub-classes instead.
00085 ///
00086 /// Intended use:
00087 ///
00088 ///   for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) {
00089 ///     if (!MIO->isReg())
00090 ///       continue;
00091 ///     ...
00092 ///   }
00093 ///
00094 class MachineOperandIteratorBase {
00095   MachineBasicBlock::instr_iterator InstrI, InstrE;
00096   MachineInstr::mop_iterator OpI, OpE;
00097 
00098   // If the operands on InstrI are exhausted, advance InstrI to the next
00099   // bundled instruction with operands.
00100   void advance() {
00101     while (OpI == OpE) {
00102       // Don't advance off the basic block, or into a new bundle.
00103       if (++InstrI == InstrE || !InstrI->isInsideBundle())
00104         break;
00105       OpI = InstrI->operands_begin();
00106       OpE = InstrI->operands_end();
00107     }
00108   }
00109 
00110 protected:
00111   /// MachineOperandIteratorBase - Create an iterator that visits all operands
00112   /// on MI, or all operands on every instruction in the bundle containing MI.
00113   ///
00114   /// @param MI The instruction to examine.
00115   /// @param WholeBundle When true, visit all operands on the entire bundle.
00116   ///
00117   explicit MachineOperandIteratorBase(MachineInstr *MI, bool WholeBundle) {
00118     if (WholeBundle) {
00119       InstrI = getBundleStart(MI);
00120       InstrE = MI->getParent()->instr_end();
00121     } else {
00122       InstrI = InstrE = MI;
00123       ++InstrE;
00124     }
00125     OpI = InstrI->operands_begin();
00126     OpE = InstrI->operands_end();
00127     if (WholeBundle)
00128       advance();
00129   }
00130 
00131   MachineOperand &deref() const { return *OpI; }
00132 
00133 public:
00134   /// isValid - Returns true until all the operands have been visited.
00135   bool isValid() const { return OpI != OpE; }
00136 
00137   /// Preincrement.  Move to the next operand.
00138   void operator++() {
00139     assert(isValid() && "Cannot advance MIOperands beyond the last operand");
00140     ++OpI;
00141     advance();
00142   }
00143 
00144   /// getOperandNo - Returns the number of the current operand relative to its
00145   /// instruction.
00146   ///
00147   unsigned getOperandNo() const {
00148     return OpI - InstrI->operands_begin();
00149   }
00150 
00151   /// VirtRegInfo - Information about a virtual register used by a set of operands.
00152   ///
00153   struct VirtRegInfo {
00154     /// Reads - One of the operands read the virtual register.  This does not
00155     /// include <undef> or <internal> use operands, see MO::readsReg().
00156     bool Reads;
00157 
00158     /// Writes - One of the operands writes the virtual register.
00159     bool Writes;
00160 
00161     /// Tied - Uses and defs must use the same register. This can be because of
00162     /// a two-address constraint, or there may be a partial redefinition of a
00163     /// sub-register.
00164     bool Tied;
00165   };
00166 
00167   /// PhysRegInfo - Information about a physical register used by a set of
00168   /// operands.
00169   struct PhysRegInfo {
00170     /// Clobbers - Reg or an overlapping register is defined, or a regmask
00171     /// clobbers Reg.
00172     bool Clobbers;
00173 
00174     /// Defines - Reg or a super-register is defined.
00175     bool Defines;
00176 
00177     /// Reads - Read or a super-register is read.
00178     bool Reads;
00179 
00180     /// ReadsOverlap - Reg or an overlapping register is read.
00181     bool ReadsOverlap;
00182 
00183     /// DefinesDead - All defs of a Reg or a super-register are dead.
00184     bool DefinesDead;
00185 
00186     /// There is a kill of Reg or a super-register.
00187     bool Kills;
00188   };
00189 
00190   /// analyzeVirtReg - Analyze how the current instruction or bundle uses a
00191   /// virtual register.  This function should not be called after operator++(),
00192   /// it expects a fresh iterator.
00193   ///
00194   /// @param Reg The virtual register to analyze.
00195   /// @param Ops When set, this vector will receive an (MI, OpNum) entry for
00196   ///            each operand referring to Reg.
00197   /// @returns A filled-in RegInfo struct.
00198   VirtRegInfo analyzeVirtReg(unsigned Reg,
00199            SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops = nullptr);
00200 
00201   /// analyzePhysReg - Analyze how the current instruction or bundle uses a
00202   /// physical register.  This function should not be called after operator++(),
00203   /// it expects a fresh iterator.
00204   ///
00205   /// @param Reg The physical register to analyze.
00206   /// @returns A filled-in PhysRegInfo struct.
00207   PhysRegInfo analyzePhysReg(unsigned Reg, const TargetRegisterInfo *TRI);
00208 };
00209 
00210 /// MIOperands - Iterate over operands of a single instruction.
00211 ///
00212 class MIOperands : public MachineOperandIteratorBase {
00213 public:
00214   MIOperands(MachineInstr *MI) : MachineOperandIteratorBase(MI, false) {}
00215   MachineOperand &operator* () const { return deref(); }
00216   MachineOperand *operator->() const { return &deref(); }
00217 };
00218 
00219 /// ConstMIOperands - Iterate over operands of a single const instruction.
00220 ///
00221 class ConstMIOperands : public MachineOperandIteratorBase {
00222 public:
00223   ConstMIOperands(const MachineInstr *MI)
00224     : MachineOperandIteratorBase(const_cast<MachineInstr*>(MI), false) {}
00225   const MachineOperand &operator* () const { return deref(); }
00226   const MachineOperand *operator->() const { return &deref(); }
00227 };
00228 
00229 /// MIBundleOperands - Iterate over all operands in a bundle of machine
00230 /// instructions.
00231 ///
00232 class MIBundleOperands : public MachineOperandIteratorBase {
00233 public:
00234   MIBundleOperands(MachineInstr *MI) : MachineOperandIteratorBase(MI, true) {}
00235   MachineOperand &operator* () const { return deref(); }
00236   MachineOperand *operator->() const { return &deref(); }
00237 };
00238 
00239 /// ConstMIBundleOperands - Iterate over all operands in a const bundle of
00240 /// machine instructions.
00241 ///
00242 class ConstMIBundleOperands : public MachineOperandIteratorBase {
00243 public:
00244   ConstMIBundleOperands(const MachineInstr *MI)
00245     : MachineOperandIteratorBase(const_cast<MachineInstr*>(MI), true) {}
00246   const MachineOperand &operator* () const { return deref(); }
00247   const MachineOperand *operator->() const { return &deref(); }
00248 };
00249 
00250 } // End llvm namespace
00251 
00252 #endif