LivePhysRegs.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //===- llvm/CodeGen/LivePhysRegs.h - Live Physical Register Set -*- 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 implements the LivePhysRegs utility for tracking liveness of
  11. // physical registers. This can be used for ad-hoc liveness tracking after
  12. // register allocation. You can start with the live-ins/live-outs at the
  13. // beginning/end of a block and update the information while walking the
  14. // instructions inside the block. This implementation tracks the liveness on a
  15. // sub-register granularity.
  16. //
  17. // We assume that the high bits of a physical super-register are not preserved
  18. // unless the instruction has an implicit-use operand reading the super-
  19. // register.
  20. //
  21. // X86 Example:
  22. // %YMM0<def> = ...
  23. // %XMM0<def> = ... (Kills %XMM0, all %XMM0s sub-registers, and %YMM0)
  24. //
  25. // %YMM0<def> = ...
  26. // %XMM0<def> = ..., %YMM0<imp-use> (%YMM0 and all its sub-registers are alive)
  27. //===----------------------------------------------------------------------===//
  28. #ifndef LLVM_CODEGEN_LIVEPHYSREGS_H
  29. #define LLVM_CODEGEN_LIVEPHYSREGS_H
  30. #include "llvm/ADT/SparseSet.h"
  31. #include "llvm/CodeGen/MachineBasicBlock.h"
  32. #include "llvm/Target/TargetRegisterInfo.h"
  33. #include <cassert>
  34. namespace llvm {
  35. class MachineInstr;
  36. /// \brief A set of live physical registers with functions to track liveness
  37. /// when walking backward/forward through a basic block.
  38. class LivePhysRegs {
  39. const TargetRegisterInfo *TRI;
  40. SparseSet<unsigned> LiveRegs;
  41. LivePhysRegs(const LivePhysRegs&) = delete;
  42. LivePhysRegs &operator=(const LivePhysRegs&) = delete;
  43. public:
  44. /// \brief Constructs a new empty LivePhysRegs set.
  45. LivePhysRegs() : TRI(nullptr), LiveRegs() {}
  46. /// \brief Constructs and initialize an empty LivePhysRegs set.
  47. LivePhysRegs(const TargetRegisterInfo *TRI) : TRI(TRI) {
  48. assert(TRI && "Invalid TargetRegisterInfo pointer.");
  49. LiveRegs.setUniverse(TRI->getNumRegs());
  50. }
  51. /// \brief Clear and initialize the LivePhysRegs set.
  52. void init(const TargetRegisterInfo *TRI) {
  53. assert(TRI && "Invalid TargetRegisterInfo pointer.");
  54. this->TRI = TRI;
  55. LiveRegs.clear();
  56. LiveRegs.setUniverse(TRI->getNumRegs());
  57. }
  58. /// \brief Clears the LivePhysRegs set.
  59. void clear() { LiveRegs.clear(); }
  60. /// \brief Returns true if the set is empty.
  61. bool empty() const { return LiveRegs.empty(); }
  62. /// \brief Adds a physical register and all its sub-registers to the set.
  63. void addReg(unsigned Reg) {
  64. assert(TRI && "LivePhysRegs is not initialized.");
  65. assert(Reg <= TRI->getNumRegs() && "Expected a physical register.");
  66. for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
  67. SubRegs.isValid(); ++SubRegs)
  68. LiveRegs.insert(*SubRegs);
  69. }
  70. /// \brief Removes a physical register, all its sub-registers, and all its
  71. /// super-registers from the set.
  72. void removeReg(unsigned Reg) {
  73. assert(TRI && "LivePhysRegs is not initialized.");
  74. assert(Reg <= TRI->getNumRegs() && "Expected a physical register.");
  75. for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
  76. SubRegs.isValid(); ++SubRegs)
  77. LiveRegs.erase(*SubRegs);
  78. for (MCSuperRegIterator SuperRegs(Reg, TRI, /*IncludeSelf=*/false);
  79. SuperRegs.isValid(); ++SuperRegs)
  80. LiveRegs.erase(*SuperRegs);
  81. }
  82. /// \brief Removes physical registers clobbered by the regmask operand @p MO.
  83. void removeRegsInMask(const MachineOperand &MO,
  84. SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> *Clobbers);
  85. /// \brief Returns true if register @p Reg is contained in the set. This also
  86. /// works if only the super register of @p Reg has been defined, because we
  87. /// always add also all sub-registers to the set.
  88. bool contains(unsigned Reg) const { return LiveRegs.count(Reg); }
  89. /// \brief Simulates liveness when stepping backwards over an
  90. /// instruction(bundle): Remove Defs, add uses. This is the recommended way of
  91. /// calculating liveness.
  92. void stepBackward(const MachineInstr &MI);
  93. /// \brief Simulates liveness when stepping forward over an
  94. /// instruction(bundle): Remove killed-uses, add defs. This is the not
  95. /// recommended way, because it depends on accurate kill flags. If possible
  96. /// use stepBackwards() instead of this function.
  97. /// The clobbers set will be the list of registers either defined or clobbered
  98. /// by a regmask. The operand will identify whether this is a regmask or
  99. /// register operand.
  100. void stepForward(const MachineInstr &MI,
  101. SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> &Clobbers);
  102. /// \brief Adds all live-in registers of basic block @p MBB; After prologue/
  103. /// epilogue insertion \p AddPristines should be set to true to insert the
  104. /// pristine registers.
  105. void addLiveIns(const MachineBasicBlock *MBB, bool AddPristines = false);
  106. /// \brief Adds all live-out registers of basic block @p MBB; After prologue/
  107. /// epilogue insertion \p AddPristines should be set to true to insert the
  108. /// pristine registers.
  109. void addLiveOuts(const MachineBasicBlock *MBB, bool AddPristines = false);
  110. typedef SparseSet<unsigned>::const_iterator const_iterator;
  111. const_iterator begin() const { return LiveRegs.begin(); }
  112. const_iterator end() const { return LiveRegs.end(); }
  113. /// \brief Prints the currently live registers to @p OS.
  114. void print(raw_ostream &OS) const;
  115. /// \brief Dumps the currently live registers to the debug output.
  116. void dump() const;
  117. };
  118. inline raw_ostream &operator<<(raw_ostream &OS, const LivePhysRegs& LR) {
  119. LR.print(OS);
  120. return OS;
  121. }
  122. } // namespace llvm
  123. #endif