RegisterScavenging.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- RegisterScavenging.h - Machine register scavenging -------*- 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 declares the machine register scavenger class. It can provide
  16. /// information such as unused register at any point in a machine basic block.
  17. /// It also provides a mechanism to make registers available by evicting them
  18. /// to spill slots.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_CODEGEN_REGISTERSCAVENGING_H
  22. #define LLVM_CODEGEN_REGISTERSCAVENGING_H
  23. #include "llvm/ADT/BitVector.h"
  24. #include "llvm/ADT/SmallVector.h"
  25. #include "llvm/CodeGen/LiveRegUnits.h"
  26. #include "llvm/CodeGen/MachineBasicBlock.h"
  27. #include "llvm/CodeGen/MachineRegisterInfo.h"
  28. #include "llvm/MC/LaneBitmask.h"
  29. namespace llvm {
  30. class MachineInstr;
  31. class TargetInstrInfo;
  32. class TargetRegisterClass;
  33. class TargetRegisterInfo;
  34. class RegScavenger {
  35. const TargetRegisterInfo *TRI;
  36. const TargetInstrInfo *TII;
  37. MachineRegisterInfo* MRI;
  38. MachineBasicBlock *MBB = nullptr;
  39. MachineBasicBlock::iterator MBBI;
  40. unsigned NumRegUnits = 0;
  41. /// True if RegScavenger is currently tracking the liveness of registers.
  42. bool Tracking = false;
  43. /// Information on scavenged registers (held in a spill slot).
  44. struct ScavengedInfo {
  45. ScavengedInfo(int FI = -1) : FrameIndex(FI) {}
  46. /// A spill slot used for scavenging a register post register allocation.
  47. int FrameIndex;
  48. /// If non-zero, the specific register is currently being
  49. /// scavenged. That is, it is spilled to this scavenging stack slot.
  50. Register Reg;
  51. /// The instruction that restores the scavenged register from stack.
  52. const MachineInstr *Restore = nullptr;
  53. };
  54. /// A vector of information on scavenged registers.
  55. SmallVector<ScavengedInfo, 2> Scavenged;
  56. LiveRegUnits LiveUnits;
  57. // These BitVectors are only used internally to forward(). They are members
  58. // to avoid frequent reallocations.
  59. BitVector KillRegUnits, DefRegUnits;
  60. BitVector TmpRegUnits;
  61. public:
  62. RegScavenger() = default;
  63. /// Start tracking liveness from the begin of basic block \p MBB.
  64. void enterBasicBlock(MachineBasicBlock &MBB);
  65. /// Start tracking liveness from the end of basic block \p MBB.
  66. /// Use backward() to move towards the beginning of the block. This is
  67. /// preferred to enterBasicBlock() and forward() because it does not depend
  68. /// on the presence of kill flags.
  69. void enterBasicBlockEnd(MachineBasicBlock &MBB);
  70. /// Move the internal MBB iterator and update register states.
  71. void forward();
  72. /// Move the internal MBB iterator and update register states until
  73. /// it has processed the specific iterator.
  74. void forward(MachineBasicBlock::iterator I) {
  75. if (!Tracking && MBB->begin() != I) forward();
  76. while (MBBI != I) forward();
  77. }
  78. /// Update internal register state and move MBB iterator backwards.
  79. /// Contrary to unprocess() this method gives precise results even in the
  80. /// absence of kill flags.
  81. void backward();
  82. /// Call backward() as long as the internal iterator does not point to \p I.
  83. void backward(MachineBasicBlock::iterator I) {
  84. while (MBBI != I)
  85. backward();
  86. }
  87. /// Move the internal MBB iterator but do not update register states.
  88. void skipTo(MachineBasicBlock::iterator I) {
  89. if (I == MachineBasicBlock::iterator(nullptr))
  90. Tracking = false;
  91. MBBI = I;
  92. }
  93. MachineBasicBlock::iterator getCurrentPosition() const { return MBBI; }
  94. /// Return if a specific register is currently used.
  95. bool isRegUsed(Register Reg, bool includeReserved = true) const;
  96. /// Return all available registers in the register class in Mask.
  97. BitVector getRegsAvailable(const TargetRegisterClass *RC);
  98. /// Find an unused register of the specified register class.
  99. /// Return 0 if none is found.
  100. Register FindUnusedReg(const TargetRegisterClass *RC) const;
  101. /// Add a scavenging frame index.
  102. void addScavengingFrameIndex(int FI) {
  103. Scavenged.push_back(ScavengedInfo(FI));
  104. }
  105. /// Query whether a frame index is a scavenging frame index.
  106. bool isScavengingFrameIndex(int FI) const {
  107. for (SmallVectorImpl<ScavengedInfo>::const_iterator I = Scavenged.begin(),
  108. IE = Scavenged.end(); I != IE; ++I)
  109. if (I->FrameIndex == FI)
  110. return true;
  111. return false;
  112. }
  113. /// Get an array of scavenging frame indices.
  114. void getScavengingFrameIndices(SmallVectorImpl<int> &A) const {
  115. for (SmallVectorImpl<ScavengedInfo>::const_iterator I = Scavenged.begin(),
  116. IE = Scavenged.end(); I != IE; ++I)
  117. if (I->FrameIndex >= 0)
  118. A.push_back(I->FrameIndex);
  119. }
  120. /// Make a register of the specific register class
  121. /// available and do the appropriate bookkeeping. SPAdj is the stack
  122. /// adjustment due to call frame, it's passed along to eliminateFrameIndex().
  123. /// Returns the scavenged register.
  124. /// This is deprecated as it depends on the quality of the kill flags being
  125. /// present; Use scavengeRegisterBackwards() instead!
  126. ///
  127. /// If \p AllowSpill is false, fail if a spill is required to make the
  128. /// register available, and return NoRegister.
  129. Register scavengeRegister(const TargetRegisterClass *RC,
  130. MachineBasicBlock::iterator I, int SPAdj,
  131. bool AllowSpill = true);
  132. Register scavengeRegister(const TargetRegisterClass *RegClass, int SPAdj,
  133. bool AllowSpill = true) {
  134. return scavengeRegister(RegClass, MBBI, SPAdj, AllowSpill);
  135. }
  136. /// Make a register of the specific register class available from the current
  137. /// position backwards to the place before \p To. If \p RestoreAfter is true
  138. /// this includes the instruction following the current position.
  139. /// SPAdj is the stack adjustment due to call frame, it's passed along to
  140. /// eliminateFrameIndex().
  141. /// Returns the scavenged register.
  142. ///
  143. /// If \p AllowSpill is false, fail if a spill is required to make the
  144. /// register available, and return NoRegister.
  145. Register scavengeRegisterBackwards(const TargetRegisterClass &RC,
  146. MachineBasicBlock::iterator To,
  147. bool RestoreAfter, int SPAdj,
  148. bool AllowSpill = true);
  149. /// Tell the scavenger a register is used.
  150. void setRegUsed(Register Reg, LaneBitmask LaneMask = LaneBitmask::getAll());
  151. private:
  152. /// Returns true if a register is reserved. It is never "unused".
  153. bool isReserved(Register Reg) const { return MRI->isReserved(Reg); }
  154. /// setUsed / setUnused - Mark the state of one or a number of register units.
  155. ///
  156. void setUsed(const BitVector &RegUnits) {
  157. LiveUnits.addUnits(RegUnits);
  158. }
  159. void setUnused(const BitVector &RegUnits) {
  160. LiveUnits.removeUnits(RegUnits);
  161. }
  162. /// Processes the current instruction and fill the KillRegUnits and
  163. /// DefRegUnits bit vectors.
  164. void determineKillsAndDefs();
  165. /// Add all Reg Units that Reg contains to BV.
  166. void addRegUnits(BitVector &BV, MCRegister Reg);
  167. /// Remove all Reg Units that \p Reg contains from \p BV.
  168. void removeRegUnits(BitVector &BV, MCRegister Reg);
  169. /// Return the candidate register that is unused for the longest after
  170. /// StartMI. UseMI is set to the instruction where the search stopped.
  171. ///
  172. /// No more than InstrLimit instructions are inspected.
  173. Register findSurvivorReg(MachineBasicBlock::iterator StartMI,
  174. BitVector &Candidates,
  175. unsigned InstrLimit,
  176. MachineBasicBlock::iterator &UseMI);
  177. /// Initialize RegisterScavenger.
  178. void init(MachineBasicBlock &MBB);
  179. /// Spill a register after position \p After and reload it before position
  180. /// \p UseMI.
  181. ScavengedInfo &spill(Register Reg, const TargetRegisterClass &RC, int SPAdj,
  182. MachineBasicBlock::iterator Before,
  183. MachineBasicBlock::iterator &UseMI);
  184. };
  185. /// Replaces all frame index virtual registers with physical registers. Uses the
  186. /// register scavenger to find an appropriate register to use.
  187. void scavengeFrameVirtualRegs(MachineFunction &MF, RegScavenger &RS);
  188. } // end namespace llvm
  189. #endif // LLVM_CODEGEN_REGISTERSCAVENGING_H
  190. #ifdef __GNUC__
  191. #pragma GCC diagnostic pop
  192. #endif