LiveRegUnits.h 6.4 KB

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