2
0

LivePhysRegs.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //===--- LivePhysRegs.cpp - Live Physical Register Set --------------------===//
  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 across machine instructions in forward or backward order.
  12. // A more detailed description can be found in the corresponding header file.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "llvm/CodeGen/LivePhysRegs.h"
  16. #include "llvm/CodeGen/MachineFrameInfo.h"
  17. #include "llvm/CodeGen/MachineFunction.h"
  18. #include "llvm/CodeGen/MachineInstrBundle.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. using namespace llvm;
  22. /// \brief Remove all registers from the set that get clobbered by the register
  23. /// mask.
  24. /// The clobbers set will be the list of live registers clobbered
  25. /// by the regmask.
  26. void LivePhysRegs::removeRegsInMask(const MachineOperand &MO,
  27. SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> *Clobbers) {
  28. SparseSet<unsigned>::iterator LRI = LiveRegs.begin();
  29. while (LRI != LiveRegs.end()) {
  30. if (MO.clobbersPhysReg(*LRI)) {
  31. if (Clobbers)
  32. Clobbers->push_back(std::make_pair(*LRI, &MO));
  33. LRI = LiveRegs.erase(LRI);
  34. } else
  35. ++LRI;
  36. }
  37. }
  38. /// Simulates liveness when stepping backwards over an instruction(bundle):
  39. /// Remove Defs, add uses. This is the recommended way of calculating liveness.
  40. void LivePhysRegs::stepBackward(const MachineInstr &MI) {
  41. // Remove defined registers and regmask kills from the set.
  42. for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
  43. if (O->isReg()) {
  44. if (!O->isDef())
  45. continue;
  46. unsigned Reg = O->getReg();
  47. if (Reg == 0)
  48. continue;
  49. removeReg(Reg);
  50. } else if (O->isRegMask())
  51. removeRegsInMask(*O, nullptr);
  52. }
  53. // Add uses to the set.
  54. for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
  55. if (!O->isReg() || !O->readsReg() || O->isUndef())
  56. continue;
  57. unsigned Reg = O->getReg();
  58. if (Reg == 0)
  59. continue;
  60. addReg(Reg);
  61. }
  62. }
  63. /// Simulates liveness when stepping forward over an instruction(bundle): Remove
  64. /// killed-uses, add defs. This is the not recommended way, because it depends
  65. /// on accurate kill flags. If possible use stepBackwards() instead of this
  66. /// function.
  67. void LivePhysRegs::stepForward(const MachineInstr &MI,
  68. SmallVectorImpl<std::pair<unsigned, const MachineOperand*>> &Clobbers) {
  69. // Remove killed registers from the set.
  70. for (ConstMIBundleOperands O(&MI); O.isValid(); ++O) {
  71. if (O->isReg()) {
  72. unsigned Reg = O->getReg();
  73. if (Reg == 0)
  74. continue;
  75. if (O->isDef()) {
  76. // Note, dead defs are still recorded. The caller should decide how to
  77. // handle them.
  78. Clobbers.push_back(std::make_pair(Reg, &*O));
  79. } else {
  80. if (!O->isKill())
  81. continue;
  82. assert(O->isUse());
  83. removeReg(Reg);
  84. }
  85. } else if (O->isRegMask())
  86. removeRegsInMask(*O, &Clobbers);
  87. }
  88. // Add defs to the set.
  89. for (auto Reg : Clobbers) {
  90. // Skip dead defs. They shouldn't be added to the set.
  91. if (Reg.second->isReg() && Reg.second->isDead())
  92. continue;
  93. addReg(Reg.first);
  94. }
  95. }
  96. /// Prin the currently live registers to OS.
  97. void LivePhysRegs::print(raw_ostream &OS) const {
  98. OS << "Live Registers:";
  99. if (!TRI) {
  100. OS << " (uninitialized)\n";
  101. return;
  102. }
  103. if (empty()) {
  104. OS << " (empty)\n";
  105. return;
  106. }
  107. for (const_iterator I = begin(), E = end(); I != E; ++I)
  108. OS << " " << PrintReg(*I, TRI);
  109. OS << "\n";
  110. }
  111. /// Dumps the currently live registers to the debug output.
  112. void LivePhysRegs::dump() const {
  113. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  114. dbgs() << " " << *this;
  115. #endif
  116. }
  117. /// Add live-in registers of basic block \p MBB to \p LiveRegs.
  118. static void addLiveIns(LivePhysRegs &LiveRegs, const MachineBasicBlock &MBB) {
  119. for (unsigned Reg : make_range(MBB.livein_begin(), MBB.livein_end()))
  120. LiveRegs.addReg(Reg);
  121. }
  122. /// Add pristine registers to the given \p LiveRegs. This function removes
  123. /// actually saved callee save registers when \p InPrologueEpilogue is false.
  124. static void addPristines(LivePhysRegs &LiveRegs, const MachineFunction &MF,
  125. const TargetRegisterInfo &TRI) {
  126. const MachineFrameInfo &MFI = *MF.getFrameInfo();
  127. if (!MFI.isCalleeSavedInfoValid())
  128. return;
  129. for (const MCPhysReg *CSR = TRI.getCalleeSavedRegs(&MF); CSR && *CSR; ++CSR)
  130. LiveRegs.addReg(*CSR);
  131. for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
  132. LiveRegs.removeReg(Info.getReg());
  133. }
  134. void LivePhysRegs::addLiveOuts(const MachineBasicBlock *MBB,
  135. bool AddPristines) {
  136. if (AddPristines) {
  137. const MachineFunction &MF = *MBB->getParent();
  138. addPristines(*this, MF, *TRI);
  139. }
  140. for (const MachineBasicBlock *Succ : MBB->successors())
  141. ::addLiveIns(*this, *Succ);
  142. }
  143. void LivePhysRegs::addLiveIns(const MachineBasicBlock *MBB,
  144. bool AddPristines) {
  145. if (AddPristines) {
  146. const MachineFunction &MF = *MBB->getParent();
  147. addPristines(*this, MF, *TRI);
  148. }
  149. ::addLiveIns(*this, *MBB);
  150. }