MachineInstrBundle.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //===- llvm/CodeGen/MachineInstrBundle.h - MI bundle utilities --*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file provide utility functions to manipulate machine instruction
  10. // bundles.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
  14. #define LLVM_CODEGEN_MACHINEINSTRBUNDLE_H
  15. #include "llvm/CodeGen/MachineBasicBlock.h"
  16. namespace llvm {
  17. /// finalizeBundle - Finalize a machine instruction bundle which includes
  18. /// a sequence of instructions starting from FirstMI to LastMI (exclusive).
  19. /// This routine adds a BUNDLE instruction to represent the bundle, it adds
  20. /// IsInternalRead markers to MachineOperands which are defined inside the
  21. /// bundle, and it copies externally visible defs and uses to the BUNDLE
  22. /// instruction.
  23. void finalizeBundle(MachineBasicBlock &MBB,
  24. MachineBasicBlock::instr_iterator FirstMI,
  25. MachineBasicBlock::instr_iterator LastMI);
  26. /// finalizeBundle - Same functionality as the previous finalizeBundle except
  27. /// the last instruction in the bundle is not provided as an input. This is
  28. /// used in cases where bundles are pre-determined by marking instructions
  29. /// with 'InsideBundle' marker. It returns the MBB instruction iterator that
  30. /// points to the end of the bundle.
  31. MachineBasicBlock::instr_iterator finalizeBundle(MachineBasicBlock &MBB,
  32. MachineBasicBlock::instr_iterator FirstMI);
  33. /// finalizeBundles - Finalize instruction bundles in the specified
  34. /// MachineFunction. Return true if any bundles are finalized.
  35. bool finalizeBundles(MachineFunction &MF);
  36. /// Returns an iterator to the first instruction in the bundle containing \p I.
  37. inline MachineBasicBlock::instr_iterator getBundleStart(
  38. MachineBasicBlock::instr_iterator I) {
  39. while (I->isBundledWithPred())
  40. --I;
  41. return I;
  42. }
  43. /// Returns an iterator to the first instruction in the bundle containing \p I.
  44. inline MachineBasicBlock::const_instr_iterator getBundleStart(
  45. MachineBasicBlock::const_instr_iterator I) {
  46. while (I->isBundledWithPred())
  47. --I;
  48. return I;
  49. }
  50. /// Returns an iterator pointing beyond the bundle containing \p I.
  51. inline MachineBasicBlock::instr_iterator getBundleEnd(
  52. MachineBasicBlock::instr_iterator I) {
  53. while (I->isBundledWithSucc())
  54. ++I;
  55. ++I;
  56. return I;
  57. }
  58. /// Returns an iterator pointing beyond the bundle containing \p I.
  59. inline MachineBasicBlock::const_instr_iterator getBundleEnd(
  60. MachineBasicBlock::const_instr_iterator I) {
  61. while (I->isBundledWithSucc())
  62. ++I;
  63. ++I;
  64. return I;
  65. }
  66. //===----------------------------------------------------------------------===//
  67. // MachineBundleOperand iterator
  68. //
  69. /// MIBundleOperandIteratorBase - Iterator that visits all operands in a bundle
  70. /// of MachineInstrs. This class is not intended to be used directly, use one
  71. /// of the sub-classes instead.
  72. ///
  73. /// Intended use:
  74. ///
  75. /// for (MIBundleOperands MIO(MI); MIO.isValid(); ++MIO) {
  76. /// if (!MIO->isReg())
  77. /// continue;
  78. /// ...
  79. /// }
  80. ///
  81. template <typename ValueT>
  82. class MIBundleOperandIteratorBase
  83. : public iterator_facade_base<MIBundleOperandIteratorBase<ValueT>,
  84. std::forward_iterator_tag, ValueT> {
  85. MachineBasicBlock::instr_iterator InstrI, InstrE;
  86. MachineInstr::mop_iterator OpI, OpE;
  87. // If the operands on InstrI are exhausted, advance InstrI to the next
  88. // bundled instruction with operands.
  89. void advance() {
  90. while (OpI == OpE) {
  91. // Don't advance off the basic block, or into a new bundle.
  92. if (++InstrI == InstrE || !InstrI->isInsideBundle()) {
  93. InstrI = InstrE;
  94. break;
  95. }
  96. OpI = InstrI->operands_begin();
  97. OpE = InstrI->operands_end();
  98. }
  99. }
  100. protected:
  101. /// MIBundleOperandIteratorBase - Create an iterator that visits all operands
  102. /// on MI, or all operands on every instruction in the bundle containing MI.
  103. ///
  104. /// @param MI The instruction to examine.
  105. ///
  106. explicit MIBundleOperandIteratorBase(MachineInstr &MI) {
  107. InstrI = getBundleStart(MI.getIterator());
  108. InstrE = MI.getParent()->instr_end();
  109. OpI = InstrI->operands_begin();
  110. OpE = InstrI->operands_end();
  111. advance();
  112. }
  113. /// Constructor for an iterator past the last iteration: both instruction
  114. /// iterators point to the end of the BB and OpI == OpE.
  115. explicit MIBundleOperandIteratorBase(MachineBasicBlock::instr_iterator InstrE,
  116. MachineInstr::mop_iterator OpE)
  117. : InstrI(InstrE), InstrE(InstrE), OpI(OpE), OpE(OpE) {}
  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. ValueT &operator*() const { return *OpI; }
  128. ValueT *operator->() const { return &*OpI; }
  129. bool operator==(const MIBundleOperandIteratorBase &Arg) const {
  130. // Iterators are equal, if InstrI matches and either OpIs match or OpI ==
  131. // OpE match for both. The second condition allows us to construct an 'end'
  132. // iterator, without finding the last instruction in a bundle up-front.
  133. return InstrI == Arg.InstrI &&
  134. (OpI == Arg.OpI || (OpI == OpE && Arg.OpI == Arg.OpE));
  135. }
  136. /// getOperandNo - Returns the number of the current operand relative to its
  137. /// instruction.
  138. ///
  139. unsigned getOperandNo() const {
  140. return OpI - InstrI->operands_begin();
  141. }
  142. };
  143. /// MIBundleOperands - Iterate over all operands in a bundle of machine
  144. /// instructions.
  145. ///
  146. class MIBundleOperands : public MIBundleOperandIteratorBase<MachineOperand> {
  147. /// Constructor for an iterator past the last iteration.
  148. MIBundleOperands(MachineBasicBlock::instr_iterator InstrE,
  149. MachineInstr::mop_iterator OpE)
  150. : MIBundleOperandIteratorBase(InstrE, OpE) {}
  151. public:
  152. MIBundleOperands(MachineInstr &MI) : MIBundleOperandIteratorBase(MI) {}
  153. /// Returns an iterator past the last iteration.
  154. static MIBundleOperands end(const MachineBasicBlock &MBB) {
  155. return {const_cast<MachineBasicBlock &>(MBB).instr_end(),
  156. const_cast<MachineBasicBlock &>(MBB).instr_begin()->operands_end()};
  157. }
  158. };
  159. /// ConstMIBundleOperands - Iterate over all operands in a const bundle of
  160. /// machine instructions.
  161. ///
  162. class ConstMIBundleOperands
  163. : public MIBundleOperandIteratorBase<const MachineOperand> {
  164. /// Constructor for an iterator past the last iteration.
  165. ConstMIBundleOperands(MachineBasicBlock::instr_iterator InstrE,
  166. MachineInstr::mop_iterator OpE)
  167. : MIBundleOperandIteratorBase(InstrE, OpE) {}
  168. public:
  169. ConstMIBundleOperands(const MachineInstr &MI)
  170. : MIBundleOperandIteratorBase(const_cast<MachineInstr &>(MI)) {}
  171. /// Returns an iterator past the last iteration.
  172. static ConstMIBundleOperands end(const MachineBasicBlock &MBB) {
  173. return {const_cast<MachineBasicBlock &>(MBB).instr_end(),
  174. const_cast<MachineBasicBlock &>(MBB).instr_begin()->operands_end()};
  175. }
  176. };
  177. inline iterator_range<ConstMIBundleOperands>
  178. const_mi_bundle_ops(const MachineInstr &MI) {
  179. return make_range(ConstMIBundleOperands(MI),
  180. ConstMIBundleOperands::end(*MI.getParent()));
  181. }
  182. inline iterator_range<MIBundleOperands> mi_bundle_ops(MachineInstr &MI) {
  183. return make_range(MIBundleOperands(MI),
  184. MIBundleOperands::end(*MI.getParent()));
  185. }
  186. /// VirtRegInfo - Information about a virtual register used by a set of
  187. /// operands.
  188. ///
  189. struct VirtRegInfo {
  190. /// Reads - One of the operands read the virtual register. This does not
  191. /// include undef or internal use operands, see MO::readsReg().
  192. bool Reads;
  193. /// Writes - One of the operands writes the virtual register.
  194. bool Writes;
  195. /// Tied - Uses and defs must use the same register. This can be because of
  196. /// a two-address constraint, or there may be a partial redefinition of a
  197. /// sub-register.
  198. bool Tied;
  199. };
  200. /// AnalyzeVirtRegInBundle - Analyze how the current instruction or bundle uses
  201. /// a virtual register. This function should not be called after operator++(),
  202. /// it expects a fresh iterator.
  203. ///
  204. /// @param Reg The virtual register to analyze.
  205. /// @param Ops When set, this vector will receive an (MI, OpNum) entry for
  206. /// each operand referring to Reg.
  207. /// @returns A filled-in RegInfo struct.
  208. VirtRegInfo AnalyzeVirtRegInBundle(
  209. MachineInstr &MI, Register Reg,
  210. SmallVectorImpl<std::pair<MachineInstr *, unsigned>> *Ops = nullptr);
  211. /// Information about how a physical register Reg is used by a set of
  212. /// operands.
  213. struct PhysRegInfo {
  214. /// There is a regmask operand indicating Reg is clobbered.
  215. /// \see MachineOperand::CreateRegMask().
  216. bool Clobbered;
  217. /// Reg or one of its aliases is defined. The definition may only cover
  218. /// parts of the register.
  219. bool Defined;
  220. /// Reg or a super-register is defined. The definition covers the full
  221. /// register.
  222. bool FullyDefined;
  223. /// Reg or one of its aliases is read. The register may only be read
  224. /// partially.
  225. bool Read;
  226. /// Reg or a super-register is read. The full register is read.
  227. bool FullyRead;
  228. /// Either:
  229. /// - Reg is FullyDefined and all defs of reg or an overlapping
  230. /// register are dead, or
  231. /// - Reg is completely dead because "defined" by a clobber.
  232. bool DeadDef;
  233. /// Reg is Defined and all defs of reg or an overlapping register are
  234. /// dead.
  235. bool PartialDeadDef;
  236. /// There is a use operand of reg or a super-register with kill flag set.
  237. bool Killed;
  238. };
  239. /// AnalyzePhysRegInBundle - Analyze how the current instruction or bundle uses
  240. /// a physical register. This function should not be called after operator++(),
  241. /// it expects a fresh iterator.
  242. ///
  243. /// @param Reg The physical register to analyze.
  244. /// @returns A filled-in PhysRegInfo struct.
  245. PhysRegInfo AnalyzePhysRegInBundle(const MachineInstr &MI, Register Reg,
  246. const TargetRegisterInfo *TRI);
  247. } // End llvm namespace
  248. #endif