MachineInstrBundle.h 10 KB

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