MachineInstrBundle.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #ifndef LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
  15. #define LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
  16. #include "llvm/CodeGen/MachineBasicBlock.h"
  17. namespace llvm {
  18. /// finalizeBundle - Finalize a machine instruction bundle which includes
  19. /// a sequence of instructions starting from FirstMI to LastMI (exclusive).
  20. /// This routine adds a BUNDLE instruction to represent the bundle, it adds
  21. /// IsInternalRead markers to MachineOperands which are defined inside the
  22. /// bundle, and it copies externally visible defs and uses to the BUNDLE
  23. /// instruction.
  24. void finalizeBundle(MachineBasicBlock &MBB,
  25. MachineBasicBlock::instr_iterator FirstMI,
  26. MachineBasicBlock::instr_iterator LastMI);
  27. /// finalizeBundle - Same functionality as the previous finalizeBundle except
  28. /// the last instruction in the bundle is not provided as an input. This is
  29. /// used in cases where bundles are pre-determined by marking instructions
  30. /// with 'InsideBundle' marker. It returns the MBB instruction iterator that
  31. /// points to the end of the bundle.
  32. MachineBasicBlock::instr_iterator finalizeBundle(MachineBasicBlock &MBB,
  33. MachineBasicBlock::instr_iterator FirstMI);
  34. /// finalizeBundles - Finalize instruction bundles in the specified
  35. /// MachineFunction. Return true if any bundles are finalized.
  36. bool finalizeBundles(MachineFunction &MF);
  37. /// getBundleStart - Returns the first instruction in the bundle containing MI.
  38. ///
  39. inline MachineInstr *getBundleStart(MachineInstr *MI) {
  40. MachineBasicBlock::instr_iterator I = MI;
  41. while (I->isBundledWithPred())
  42. --I;
  43. return I;
  44. }
  45. inline const MachineInstr *getBundleStart(const MachineInstr *MI) {
  46. MachineBasicBlock::const_instr_iterator I = MI;
  47. while (I->isBundledWithPred())
  48. --I;
  49. return I;
  50. }
  51. /// Return an iterator pointing beyond the bundle containing MI.
  52. inline MachineBasicBlock::instr_iterator
  53. getBundleEnd(MachineInstr *MI) {
  54. MachineBasicBlock::instr_iterator I = MI;
  55. while (I->isBundledWithSucc())
  56. ++I;
  57. return ++I;
  58. }
  59. /// Return an iterator pointing beyond the bundle containing MI.
  60. inline MachineBasicBlock::const_instr_iterator
  61. getBundleEnd(const MachineInstr *MI) {
  62. MachineBasicBlock::const_instr_iterator I = MI;
  63. while (I->isBundledWithSucc())
  64. ++I;
  65. return ++I;
  66. }
  67. // //
  68. ///////////////////////////////////////////////////////////////////////////////
  69. // MachineOperand iterator
  70. //
  71. /// MachineOperandIteratorBase - Iterator that can visit all operands on a
  72. /// MachineInstr, or all operands on a bundle of MachineInstrs. This class is
  73. /// not intended to be used directly, use one of the sub-classes instead.
  74. ///
  75. /// Intended use:
  76. ///
  77. /// for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) {
  78. /// if (!MIO->isReg())
  79. /// continue;
  80. /// ...
  81. /// }
  82. ///
  83. class MachineOperandIteratorBase {
  84. MachineBasicBlock::instr_iterator InstrI, InstrE;
  85. MachineInstr::mop_iterator OpI, OpE;
  86. // If the operands on InstrI are exhausted, advance InstrI to the next
  87. // bundled instruction with operands.
  88. void advance() {
  89. while (OpI == OpE) {
  90. // Don't advance off the basic block, or into a new bundle.
  91. if (++InstrI == InstrE || !InstrI->isInsideBundle())
  92. break;
  93. OpI = InstrI->operands_begin();
  94. OpE = InstrI->operands_end();
  95. }
  96. }
  97. protected:
  98. /// MachineOperandIteratorBase - Create an iterator that visits all operands
  99. /// on MI, or all operands on every instruction in the bundle containing MI.
  100. ///
  101. /// @param MI The instruction to examine.
  102. /// @param WholeBundle When true, visit all operands on the entire bundle.
  103. ///
  104. explicit MachineOperandIteratorBase(MachineInstr *MI, bool WholeBundle) {
  105. if (WholeBundle) {
  106. InstrI = getBundleStart(MI);
  107. InstrE = MI->getParent()->instr_end();
  108. } else {
  109. InstrI = InstrE = MI;
  110. ++InstrE;
  111. }
  112. OpI = InstrI->operands_begin();
  113. OpE = InstrI->operands_end();
  114. if (WholeBundle)
  115. advance();
  116. }
  117. MachineOperand &deref() const { return *OpI; }
  118. public:
  119. /// isValid - Returns true until all the operands have been visited.
  120. bool isValid() const { return OpI != OpE; }
  121. /// Preincrement. Move to the next operand.
  122. void operator++() {
  123. assert(isValid() && "Cannot advance MIOperands beyond the last operand");
  124. ++OpI;
  125. advance();
  126. }
  127. /// getOperandNo - Returns the number of the current operand relative to its
  128. /// instruction.
  129. ///
  130. unsigned getOperandNo() const {
  131. return OpI - InstrI->operands_begin();
  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. /// Writes - One of the operands writes the virtual register.
  140. bool Writes;
  141. /// Tied - Uses and defs must use the same register. This can be because of
  142. /// a two-address constraint, or there may be a partial redefinition of a
  143. /// sub-register.
  144. bool Tied;
  145. };
  146. /// PhysRegInfo - Information about a physical register used by a set of
  147. /// operands.
  148. struct PhysRegInfo {
  149. /// Clobbers - Reg or an overlapping register is defined, or a regmask
  150. /// clobbers Reg.
  151. bool Clobbers;
  152. /// Defines - Reg or a super-register is defined.
  153. bool Defines;
  154. /// Reads - Read or a super-register is read.
  155. bool Reads;
  156. /// ReadsOverlap - Reg or an overlapping register is read.
  157. bool ReadsOverlap;
  158. /// DefinesDead - All defs of a Reg or a super-register are dead.
  159. bool DefinesDead;
  160. /// There is a kill of Reg or a super-register.
  161. bool Kills;
  162. };
  163. /// analyzeVirtReg - Analyze how the current instruction or bundle uses a
  164. /// virtual register. This function should not be called after operator++(),
  165. /// it expects a fresh iterator.
  166. ///
  167. /// @param Reg The virtual register to analyze.
  168. /// @param Ops When set, this vector will receive an (MI, OpNum) entry for
  169. /// each operand referring to Reg.
  170. /// @returns A filled-in RegInfo struct.
  171. VirtRegInfo analyzeVirtReg(unsigned Reg,
  172. SmallVectorImpl<std::pair<MachineInstr*, unsigned> > *Ops = nullptr);
  173. /// analyzePhysReg - Analyze how the current instruction or bundle uses a
  174. /// physical register. This function should not be called after operator++(),
  175. /// it expects a fresh iterator.
  176. ///
  177. /// @param Reg The physical register to analyze.
  178. /// @returns A filled-in PhysRegInfo struct.
  179. PhysRegInfo analyzePhysReg(unsigned Reg, const TargetRegisterInfo *TRI);
  180. };
  181. /// MIOperands - Iterate over operands of a single instruction.
  182. ///
  183. class MIOperands : public MachineOperandIteratorBase {
  184. public:
  185. MIOperands(MachineInstr *MI) : MachineOperandIteratorBase(MI, false) {}
  186. MachineOperand &operator* () const { return deref(); }
  187. MachineOperand *operator->() const { return &deref(); }
  188. };
  189. /// ConstMIOperands - Iterate over operands of a single const instruction.
  190. ///
  191. class ConstMIOperands : public MachineOperandIteratorBase {
  192. public:
  193. ConstMIOperands(const MachineInstr *MI)
  194. : MachineOperandIteratorBase(const_cast<MachineInstr*>(MI), false) {}
  195. const MachineOperand &operator* () const { return deref(); }
  196. const MachineOperand *operator->() const { return &deref(); }
  197. };
  198. /// MIBundleOperands - Iterate over all operands in a bundle of machine
  199. /// instructions.
  200. ///
  201. class MIBundleOperands : public MachineOperandIteratorBase {
  202. public:
  203. MIBundleOperands(MachineInstr *MI) : MachineOperandIteratorBase(MI, true) {}
  204. MachineOperand &operator* () const { return deref(); }
  205. MachineOperand *operator->() const { return &deref(); }
  206. };
  207. /// ConstMIBundleOperands - Iterate over all operands in a const bundle of
  208. /// machine instructions.
  209. ///
  210. class ConstMIBundleOperands : public MachineOperandIteratorBase {
  211. public:
  212. ConstMIBundleOperands(const MachineInstr *MI)
  213. : MachineOperandIteratorBase(const_cast<MachineInstr*>(MI), true) {}
  214. const MachineOperand &operator* () const { return deref(); }
  215. const MachineOperand *operator->() const { return &deref(); }
  216. };
  217. } // End llvm namespace
  218. #endif