LivePhysRegs.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/CodeGen/LivePhysRegs.h - Live Physical Register 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. /// This file implements the LivePhysRegs utility for tracking liveness of
  16. /// physical registers. This can be used for ad-hoc liveness tracking after
  17. /// register allocation. You can start with the live-ins/live-outs at the
  18. /// beginning/end of a block and update the information while walking the
  19. /// instructions inside the block. This implementation tracks the liveness on a
  20. /// sub-register granularity.
  21. ///
  22. /// We assume that the high bits of a physical super-register are not preserved
  23. /// unless the instruction has an implicit-use operand reading the super-
  24. /// register.
  25. ///
  26. /// X86 Example:
  27. /// %ymm0 = ...
  28. /// %xmm0 = ... (Kills %xmm0, all %xmm0s sub-registers, and %ymm0)
  29. ///
  30. /// %ymm0 = ...
  31. /// %xmm0 = ..., implicit %ymm0 (%ymm0 and all its sub-registers are alive)
  32. //===----------------------------------------------------------------------===//
  33. #ifndef LLVM_CODEGEN_LIVEPHYSREGS_H
  34. #define LLVM_CODEGEN_LIVEPHYSREGS_H
  35. #include "llvm/ADT/SparseSet.h"
  36. #include "llvm/CodeGen/MachineBasicBlock.h"
  37. #include "llvm/CodeGen/TargetRegisterInfo.h"
  38. #include "llvm/MC/MCRegisterInfo.h"
  39. #include <cassert>
  40. #include <utility>
  41. namespace llvm {
  42. class MachineInstr;
  43. class MachineOperand;
  44. class MachineRegisterInfo;
  45. class raw_ostream;
  46. /// A set of physical registers with utility functions to track liveness
  47. /// when walking backward/forward through a basic block.
  48. class LivePhysRegs {
  49. const TargetRegisterInfo *TRI = nullptr;
  50. using RegisterSet = SparseSet<MCPhysReg, identity<MCPhysReg>>;
  51. RegisterSet LiveRegs;
  52. public:
  53. /// Constructs an unitialized set. init() needs to be called to initialize it.
  54. LivePhysRegs() = default;
  55. /// Constructs and initializes an empty set.
  56. LivePhysRegs(const TargetRegisterInfo &TRI) : TRI(&TRI) {
  57. LiveRegs.setUniverse(TRI.getNumRegs());
  58. }
  59. LivePhysRegs(const LivePhysRegs&) = delete;
  60. LivePhysRegs &operator=(const LivePhysRegs&) = delete;
  61. /// (re-)initializes and clears the set.
  62. void init(const TargetRegisterInfo &TRI) {
  63. this->TRI = &TRI;
  64. LiveRegs.clear();
  65. LiveRegs.setUniverse(TRI.getNumRegs());
  66. }
  67. /// Clears the set.
  68. void clear() { LiveRegs.clear(); }
  69. /// Returns true if the set is empty.
  70. bool empty() const { return LiveRegs.empty(); }
  71. /// Adds a physical register and all its sub-registers to the set.
  72. void addReg(MCPhysReg 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.insert(*SubRegs);
  78. }
  79. /// Removes a physical register, all its sub-registers, and all its
  80. /// super-registers from the set.
  81. void removeReg(MCPhysReg Reg) {
  82. assert(TRI && "LivePhysRegs is not initialized.");
  83. assert(Reg <= TRI->getNumRegs() && "Expected a physical register.");
  84. for (MCRegAliasIterator R(Reg, TRI, true); R.isValid(); ++R)
  85. LiveRegs.erase(*R);
  86. }
  87. /// Removes physical registers clobbered by the regmask operand \p MO.
  88. void removeRegsInMask(const MachineOperand &MO,
  89. SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> *Clobbers =
  90. nullptr);
  91. /// Returns true if register \p Reg is contained in the set. This also
  92. /// works if only the super register of \p Reg has been defined, because
  93. /// addReg() always adds all sub-registers to the set as well.
  94. /// Note: Returns false if just some sub registers are live, use available()
  95. /// when searching a free register.
  96. bool contains(MCPhysReg Reg) const { return LiveRegs.count(Reg); }
  97. /// Returns true if register \p Reg and no aliasing register is in the set.
  98. bool available(const MachineRegisterInfo &MRI, MCPhysReg Reg) const;
  99. /// Remove defined registers and regmask kills from the set.
  100. void removeDefs(const MachineInstr &MI);
  101. /// Add uses to the set.
  102. void addUses(const MachineInstr &MI);
  103. /// Simulates liveness when stepping backwards over an instruction(bundle).
  104. /// Remove Defs, add uses. This is the recommended way of calculating
  105. /// liveness.
  106. void stepBackward(const MachineInstr &MI);
  107. /// Simulates liveness when stepping forward over an instruction(bundle).
  108. /// Remove killed-uses, add defs. This is the not recommended way, because it
  109. /// depends on accurate kill flags. If possible use stepBackward() instead of
  110. /// this function. The clobbers set will be the list of registers either
  111. /// defined or clobbered by a regmask. The operand will identify whether this
  112. /// is a regmask or register operand.
  113. void stepForward(const MachineInstr &MI,
  114. SmallVectorImpl<std::pair<MCPhysReg, const MachineOperand*>> &Clobbers);
  115. /// Adds all live-in registers of basic block \p MBB.
  116. /// Live in registers are the registers in the blocks live-in list and the
  117. /// pristine registers.
  118. void addLiveIns(const MachineBasicBlock &MBB);
  119. /// Adds all live-in registers of basic block \p MBB but skips pristine
  120. /// registers.
  121. void addLiveInsNoPristines(const MachineBasicBlock &MBB);
  122. /// Adds all live-out registers of basic block \p MBB.
  123. /// Live out registers are the union of the live-in registers of the successor
  124. /// blocks and pristine registers. Live out registers of the end block are the
  125. /// callee saved registers.
  126. /// If a register is not added by this method, it is guaranteed to not be
  127. /// live out from MBB, although a sub-register may be. This is true
  128. /// both before and after regalloc.
  129. void addLiveOuts(const MachineBasicBlock &MBB);
  130. /// Adds all live-out registers of basic block \p MBB but skips pristine
  131. /// registers.
  132. void addLiveOutsNoPristines(const MachineBasicBlock &MBB);
  133. using const_iterator = RegisterSet::const_iterator;
  134. const_iterator begin() const { return LiveRegs.begin(); }
  135. const_iterator end() const { return LiveRegs.end(); }
  136. /// Prints the currently live registers to \p OS.
  137. void print(raw_ostream &OS) const;
  138. /// Dumps the currently live registers to the debug output.
  139. void dump() const;
  140. private:
  141. /// Adds live-in registers from basic block \p MBB, taking associated
  142. /// lane masks into consideration.
  143. void addBlockLiveIns(const MachineBasicBlock &MBB);
  144. /// Adds pristine registers. Pristine registers are callee saved registers
  145. /// that are unused in the function.
  146. void addPristines(const MachineFunction &MF);
  147. };
  148. inline raw_ostream &operator<<(raw_ostream &OS, const LivePhysRegs& LR) {
  149. LR.print(OS);
  150. return OS;
  151. }
  152. /// Computes registers live-in to \p MBB assuming all of its successors
  153. /// live-in lists are up-to-date. Puts the result into the given LivePhysReg
  154. /// instance \p LiveRegs.
  155. void computeLiveIns(LivePhysRegs &LiveRegs, const MachineBasicBlock &MBB);
  156. /// Recomputes dead and kill flags in \p MBB.
  157. void recomputeLivenessFlags(MachineBasicBlock &MBB);
  158. /// Adds registers contained in \p LiveRegs to the block live-in list of \p MBB.
  159. /// Does not add reserved registers.
  160. void addLiveIns(MachineBasicBlock &MBB, const LivePhysRegs &LiveRegs);
  161. /// Convenience function combining computeLiveIns() and addLiveIns().
  162. void computeAndAddLiveIns(LivePhysRegs &LiveRegs,
  163. MachineBasicBlock &MBB);
  164. /// Convenience function for recomputing live-in's for \p MBB.
  165. static inline void recomputeLiveIns(MachineBasicBlock &MBB) {
  166. LivePhysRegs LPR;
  167. MBB.clearLiveIns();
  168. computeAndAddLiveIns(LPR, MBB);
  169. }
  170. } // end namespace llvm
  171. #endif // LLVM_CODEGEN_LIVEPHYSREGS_H
  172. #ifdef __GNUC__
  173. #pragma GCC diagnostic pop
  174. #endif