LiveRegUnits.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //===- llvm/CodeGen/LiveRegUnits.h - Register Unit Set ----------*- 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. /// \file
  10. /// A set of register units. It is intended for register liveness tracking.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_CODEGEN_LIVEREGUNITS_H
  14. #define LLVM_CODEGEN_LIVEREGUNITS_H
  15. #include "llvm/ADT/BitVector.h"
  16. #include "llvm/CodeGen/MachineInstrBundle.h"
  17. #include "llvm/CodeGen/TargetRegisterInfo.h"
  18. #include "llvm/MC/LaneBitmask.h"
  19. #include "llvm/MC/MCRegisterInfo.h"
  20. #include <cstdint>
  21. namespace llvm {
  22. class MachineInstr;
  23. class MachineBasicBlock;
  24. /// A set of register units used to track register liveness.
  25. class LiveRegUnits {
  26. const TargetRegisterInfo *TRI = nullptr;
  27. BitVector Units;
  28. public:
  29. /// Constructs a new empty LiveRegUnits set.
  30. LiveRegUnits() = default;
  31. /// Constructs and initialize an empty LiveRegUnits set.
  32. LiveRegUnits(const TargetRegisterInfo &TRI) {
  33. init(TRI);
  34. }
  35. /// For a machine instruction \p MI, adds all register units used in
  36. /// \p UsedRegUnits and defined or clobbered in \p ModifiedRegUnits. This is
  37. /// useful when walking over a range of instructions to track registers
  38. /// used or defined seperately.
  39. static void accumulateUsedDefed(const MachineInstr &MI,
  40. LiveRegUnits &ModifiedRegUnits,
  41. LiveRegUnits &UsedRegUnits,
  42. const TargetRegisterInfo *TRI) {
  43. for (ConstMIBundleOperands O(MI); O.isValid(); ++O) {
  44. if (O->isRegMask())
  45. ModifiedRegUnits.addRegsInMask(O->getRegMask());
  46. if (!O->isReg())
  47. continue;
  48. Register Reg = O->getReg();
  49. if (!Reg.isPhysical())
  50. continue;
  51. if (O->isDef()) {
  52. // Some architectures (e.g. AArch64 XZR/WZR) have registers that are
  53. // constant and may be used as destinations to indicate the generated
  54. // value is discarded. No need to track such case as a def.
  55. if (!TRI->isConstantPhysReg(Reg))
  56. ModifiedRegUnits.addReg(Reg);
  57. } else {
  58. assert(O->isUse() && "Reg operand not a def and not a use");
  59. UsedRegUnits.addReg(Reg);
  60. }
  61. }
  62. }
  63. /// Initialize and clear the set.
  64. void init(const TargetRegisterInfo &TRI) {
  65. this->TRI = &TRI;
  66. Units.reset();
  67. Units.resize(TRI.getNumRegUnits());
  68. }
  69. /// Clears the set.
  70. void clear() { Units.reset(); }
  71. /// Returns true if the set is empty.
  72. bool empty() const { return Units.none(); }
  73. /// Adds register units covered by physical register \p Reg.
  74. void addReg(MCPhysReg Reg) {
  75. for (MCRegUnitIterator Unit(Reg, TRI); Unit.isValid(); ++Unit)
  76. Units.set(*Unit);
  77. }
  78. /// Adds register units covered by physical register \p Reg that are
  79. /// part of the lanemask \p Mask.
  80. void addRegMasked(MCPhysReg Reg, LaneBitmask Mask) {
  81. for (MCRegUnitMaskIterator Unit(Reg, TRI); Unit.isValid(); ++Unit) {
  82. LaneBitmask UnitMask = (*Unit).second;
  83. if (UnitMask.none() || (UnitMask & Mask).any())
  84. Units.set((*Unit).first);
  85. }
  86. }
  87. /// Removes all register units covered by physical register \p Reg.
  88. void removeReg(MCPhysReg Reg) {
  89. for (MCRegUnitIterator Unit(Reg, TRI); Unit.isValid(); ++Unit)
  90. Units.reset(*Unit);
  91. }
  92. /// Removes register units not preserved by the regmask \p RegMask.
  93. /// The regmask has the same format as the one in the RegMask machine operand.
  94. void removeRegsNotPreserved(const uint32_t *RegMask);
  95. /// Adds register units not preserved by the regmask \p RegMask.
  96. /// The regmask has the same format as the one in the RegMask machine operand.
  97. void addRegsInMask(const uint32_t *RegMask);
  98. /// Returns true if no part of physical register \p Reg is live.
  99. bool available(MCPhysReg Reg) const {
  100. for (MCRegUnitIterator Unit(Reg, TRI); Unit.isValid(); ++Unit) {
  101. if (Units.test(*Unit))
  102. return false;
  103. }
  104. return true;
  105. }
  106. /// Updates liveness when stepping backwards over the instruction \p MI.
  107. /// This removes all register units defined or clobbered in \p MI and then
  108. /// adds the units used (as in use operands) in \p MI.
  109. void stepBackward(const MachineInstr &MI);
  110. /// Adds all register units used, defined or clobbered in \p MI.
  111. /// This is useful when walking over a range of instruction to find registers
  112. /// unused over the whole range.
  113. void accumulate(const MachineInstr &MI);
  114. /// Adds registers living out of block \p MBB.
  115. /// Live out registers are the union of the live-in registers of the successor
  116. /// blocks and pristine registers. Live out registers of the end block are the
  117. /// callee saved registers.
  118. void addLiveOuts(const MachineBasicBlock &MBB);
  119. /// Adds registers living into block \p MBB.
  120. void addLiveIns(const MachineBasicBlock &MBB);
  121. /// Adds all register units marked in the bitvector \p RegUnits.
  122. void addUnits(const BitVector &RegUnits) {
  123. Units |= RegUnits;
  124. }
  125. /// Removes all register units marked in the bitvector \p RegUnits.
  126. void removeUnits(const BitVector &RegUnits) {
  127. Units.reset(RegUnits);
  128. }
  129. /// Return the internal bitvector representation of the set.
  130. const BitVector &getBitVector() const {
  131. return Units;
  132. }
  133. private:
  134. /// Adds pristine registers. Pristine registers are callee saved registers
  135. /// that are unused in the function.
  136. void addPristines(const MachineFunction &MF);
  137. };
  138. /// Returns an iterator range over all physical register and mask operands for
  139. /// \p MI and bundled instructions. This also skips any debug operands.
  140. inline iterator_range<filter_iterator<
  141. ConstMIBundleOperands, std::function<bool(const MachineOperand &)>>>
  142. phys_regs_and_masks(const MachineInstr &MI) {
  143. std::function<bool(const MachineOperand &)> Pred =
  144. [](const MachineOperand &MOP) {
  145. return MOP.isRegMask() || (MOP.isReg() && !MOP.isDebug() &&
  146. Register::isPhysicalRegister(MOP.getReg()));
  147. };
  148. return make_filter_range(const_mi_bundle_ops(MI), Pred);
  149. }
  150. } // end namespace llvm
  151. #endif // LLVM_CODEGEN_LIVEREGUNITS_H