Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

252 lines
8.4 KiB

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