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