RegisterScavenging.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //===-- RegisterScavenging.h - Machine register scavenging ------*- 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 declares the machine register scavenger class. It can provide
  11. // information such as unused register at any point in a machine basic block.
  12. // It also provides a mechanism to make registers available by evicting them
  13. // to spill slots.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #ifndef LLVM_CODEGEN_REGISTERSCAVENGING_H
  17. #define LLVM_CODEGEN_REGISTERSCAVENGING_H
  18. #include "llvm/ADT/BitVector.h"
  19. #include "llvm/CodeGen/MachineBasicBlock.h"
  20. #include "llvm/CodeGen/MachineRegisterInfo.h"
  21. namespace llvm {
  22. class MachineRegisterInfo;
  23. class TargetRegisterInfo;
  24. class TargetInstrInfo;
  25. class TargetRegisterClass;
  26. class RegScavenger {
  27. const TargetRegisterInfo *TRI;
  28. const TargetInstrInfo *TII;
  29. MachineRegisterInfo* MRI;
  30. MachineBasicBlock *MBB;
  31. MachineBasicBlock::iterator MBBI;
  32. unsigned NumRegUnits;
  33. /// True if RegScavenger is currently tracking the liveness of registers.
  34. bool Tracking;
  35. /// Information on scavenged registers (held in a spill slot).
  36. struct ScavengedInfo {
  37. ScavengedInfo(int FI = -1) : FrameIndex(FI), Reg(0), Restore(nullptr) {}
  38. /// A spill slot used for scavenging a register post register allocation.
  39. int FrameIndex;
  40. /// If non-zero, the specific register is currently being
  41. /// scavenged. That is, it is spilled to this scavenging stack slot.
  42. unsigned Reg;
  43. /// The instruction that restores the scavenged register from stack.
  44. const MachineInstr *Restore;
  45. };
  46. /// A vector of information on scavenged registers.
  47. SmallVector<ScavengedInfo, 2> Scavenged;
  48. /// The current state of each reg unit immediately before MBBI.
  49. /// One bit per register unit. If bit is not set it means any
  50. /// register containing that register unit is currently being used.
  51. BitVector RegUnitsAvailable;
  52. // These BitVectors are only used internally to forward(). They are members
  53. // to avoid frequent reallocations.
  54. BitVector KillRegUnits, DefRegUnits;
  55. BitVector TmpRegUnits;
  56. public:
  57. RegScavenger()
  58. : MBB(nullptr), NumRegUnits(0), Tracking(false) {}
  59. /// Start tracking liveness from the begin of the specific basic block.
  60. void enterBasicBlock(MachineBasicBlock *mbb);
  61. /// Allow resetting register state info for multiple
  62. /// passes over/within the same function.
  63. void initRegState();
  64. /// Move the internal MBB iterator and update register states.
  65. void forward();
  66. /// Move the internal MBB iterator and update register states until
  67. /// it has processed the specific iterator.
  68. void forward(MachineBasicBlock::iterator I) {
  69. if (!Tracking && MBB->begin() != I) forward();
  70. while (MBBI != I) forward();
  71. }
  72. /// Invert the behavior of forward() on the current instruction (undo the
  73. /// changes to the available registers made by forward()).
  74. void unprocess();
  75. /// Unprocess instructions until you reach the provided iterator.
  76. void unprocess(MachineBasicBlock::iterator I) {
  77. while (MBBI != I) unprocess();
  78. }
  79. /// Move the internal MBB iterator but do not update register states.
  80. void skipTo(MachineBasicBlock::iterator I) {
  81. if (I == MachineBasicBlock::iterator(nullptr))
  82. Tracking = false;
  83. MBBI = I;
  84. }
  85. MachineBasicBlock::iterator getCurrentPosition() const {
  86. return MBBI;
  87. }
  88. /// Return if a specific register is currently used.
  89. bool isRegUsed(unsigned Reg, bool includeReserved = true) const;
  90. /// Return all available registers in the register class in Mask.
  91. BitVector getRegsAvailable(const TargetRegisterClass *RC);
  92. /// Find an unused register of the specified register class.
  93. /// Return 0 if none is found.
  94. unsigned FindUnusedReg(const TargetRegisterClass *RegClass) const;
  95. /// Add a scavenging frame index.
  96. void addScavengingFrameIndex(int FI) {
  97. Scavenged.push_back(ScavengedInfo(FI));
  98. }
  99. /// Query whether a frame index is a scavenging frame index.
  100. bool isScavengingFrameIndex(int FI) const {
  101. for (SmallVectorImpl<ScavengedInfo>::const_iterator I = Scavenged.begin(),
  102. IE = Scavenged.end(); I != IE; ++I)
  103. if (I->FrameIndex == FI)
  104. return true;
  105. return false;
  106. }
  107. /// Get an array of scavenging frame indices.
  108. void getScavengingFrameIndices(SmallVectorImpl<int> &A) const {
  109. for (SmallVectorImpl<ScavengedInfo>::const_iterator I = Scavenged.begin(),
  110. IE = Scavenged.end(); I != IE; ++I)
  111. if (I->FrameIndex >= 0)
  112. A.push_back(I->FrameIndex);
  113. }
  114. /// Make a register of the specific register class
  115. /// available and do the appropriate bookkeeping. SPAdj is the stack
  116. /// adjustment due to call frame, it's passed along to eliminateFrameIndex().
  117. /// Returns the scavenged register.
  118. unsigned scavengeRegister(const TargetRegisterClass *RegClass,
  119. MachineBasicBlock::iterator I, int SPAdj);
  120. unsigned scavengeRegister(const TargetRegisterClass *RegClass, int SPAdj) {
  121. return scavengeRegister(RegClass, MBBI, SPAdj);
  122. }
  123. /// Tell the scavenger a register is used.
  124. void setRegUsed(unsigned Reg);
  125. private:
  126. /// Returns true if a register is reserved. It is never "unused".
  127. bool isReserved(unsigned Reg) const { return MRI->isReserved(Reg); }
  128. /// setUsed / setUnused - Mark the state of one or a number of register units.
  129. ///
  130. void setUsed(BitVector &RegUnits) {
  131. RegUnitsAvailable.reset(RegUnits);
  132. }
  133. void setUnused(BitVector &RegUnits) {
  134. RegUnitsAvailable |= RegUnits;
  135. }
  136. /// Processes the current instruction and fill the KillRegUnits and
  137. /// DefRegUnits bit vectors.
  138. void determineKillsAndDefs();
  139. /// Add all Reg Units that Reg contains to BV.
  140. void addRegUnits(BitVector &BV, unsigned Reg);
  141. /// Return the candidate register that is unused for the longest after
  142. /// StartMI. UseMI is set to the instruction where the search stopped.
  143. ///
  144. /// No more than InstrLimit instructions are inspected.
  145. unsigned findSurvivorReg(MachineBasicBlock::iterator StartMI,
  146. BitVector &Candidates,
  147. unsigned InstrLimit,
  148. MachineBasicBlock::iterator &UseMI);
  149. };
  150. } // End llvm namespace
  151. #endif