LiveRegUnits.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //===- LiveRegUnits.cpp - Register Unit Set -------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. /// \file This file imlements the LiveRegUnits set.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/LiveRegUnits.h"
  13. #include "llvm/CodeGen/MachineBasicBlock.h"
  14. #include "llvm/CodeGen/MachineFrameInfo.h"
  15. #include "llvm/CodeGen/MachineFunction.h"
  16. #include "llvm/CodeGen/MachineOperand.h"
  17. #include "llvm/CodeGen/MachineRegisterInfo.h"
  18. using namespace llvm;
  19. void LiveRegUnits::removeRegsNotPreserved(const uint32_t *RegMask) {
  20. for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
  21. for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
  22. if (MachineOperand::clobbersPhysReg(RegMask, *RootReg)) {
  23. Units.reset(U);
  24. break;
  25. }
  26. }
  27. }
  28. }
  29. void LiveRegUnits::addRegsInMask(const uint32_t *RegMask) {
  30. for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
  31. for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
  32. if (MachineOperand::clobbersPhysReg(RegMask, *RootReg)) {
  33. Units.set(U);
  34. break;
  35. }
  36. }
  37. }
  38. }
  39. void LiveRegUnits::stepBackward(const MachineInstr &MI) {
  40. // Remove defined registers and regmask kills from the set.
  41. for (const MachineOperand &MOP : MI.operands()) {
  42. if (MOP.isReg()) {
  43. if (MOP.isDef() && MOP.getReg().isPhysical())
  44. removeReg(MOP.getReg());
  45. continue;
  46. }
  47. if (MOP.isRegMask()) {
  48. removeRegsNotPreserved(MOP.getRegMask());
  49. continue;
  50. }
  51. }
  52. // Add uses to the set.
  53. for (const MachineOperand &MOP : MI.operands()) {
  54. if (!MOP.isReg() || !MOP.readsReg())
  55. continue;
  56. if (MOP.getReg().isPhysical())
  57. addReg(MOP.getReg());
  58. }
  59. }
  60. void LiveRegUnits::accumulate(const MachineInstr &MI) {
  61. // Add defs, uses and regmask clobbers to the set.
  62. for (const MachineOperand &MOP : MI.operands()) {
  63. if (MOP.isReg()) {
  64. if (!MOP.getReg().isPhysical())
  65. continue;
  66. if (MOP.isDef() || MOP.readsReg())
  67. addReg(MOP.getReg());
  68. continue;
  69. }
  70. if (MOP.isRegMask()) {
  71. addRegsInMask(MOP.getRegMask());
  72. continue;
  73. }
  74. }
  75. }
  76. /// Add live-in registers of basic block \p MBB to \p LiveUnits.
  77. static void addBlockLiveIns(LiveRegUnits &LiveUnits,
  78. const MachineBasicBlock &MBB) {
  79. for (const auto &LI : MBB.liveins())
  80. LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask);
  81. }
  82. /// Adds all callee saved registers to \p LiveUnits.
  83. static void addCalleeSavedRegs(LiveRegUnits &LiveUnits,
  84. const MachineFunction &MF) {
  85. const MachineRegisterInfo &MRI = MF.getRegInfo();
  86. const MachineFrameInfo &MFI = MF.getFrameInfo();
  87. for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR) {
  88. const unsigned N = *CSR;
  89. const auto &CSI = MFI.getCalleeSavedInfo();
  90. auto Info =
  91. llvm::find_if(CSI, [N](auto Info) { return Info.getReg() == N; });
  92. // If we have no info for this callee-saved register, assume it is liveout
  93. if (Info == CSI.end() || Info->isRestored())
  94. LiveUnits.addReg(N);
  95. }
  96. }
  97. void LiveRegUnits::addPristines(const MachineFunction &MF) {
  98. const MachineFrameInfo &MFI = MF.getFrameInfo();
  99. if (!MFI.isCalleeSavedInfoValid())
  100. return;
  101. /// This function will usually be called on an empty object, handle this
  102. /// as a special case.
  103. if (empty()) {
  104. /// Add all callee saved regs, then remove the ones that are saved and
  105. /// restored.
  106. addCalleeSavedRegs(*this, MF);
  107. /// Remove the ones that are not saved/restored; they are pristine.
  108. for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
  109. removeReg(Info.getReg());
  110. return;
  111. }
  112. /// If a callee-saved register that is not pristine is already present
  113. /// in the set, we should make sure that it stays in it. Precompute the
  114. /// set of pristine registers in a separate object.
  115. /// Add all callee saved regs, then remove the ones that are saved+restored.
  116. LiveRegUnits Pristine(*TRI);
  117. addCalleeSavedRegs(Pristine, MF);
  118. /// Remove the ones that are not saved/restored; they are pristine.
  119. for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
  120. Pristine.removeReg(Info.getReg());
  121. addUnits(Pristine.getBitVector());
  122. }
  123. void LiveRegUnits::addLiveOuts(const MachineBasicBlock &MBB) {
  124. const MachineFunction &MF = *MBB.getParent();
  125. addPristines(MF);
  126. // To get the live-outs we simply merge the live-ins of all successors.
  127. for (const MachineBasicBlock *Succ : MBB.successors())
  128. addBlockLiveIns(*this, *Succ);
  129. // For the return block: Add all callee saved registers.
  130. if (MBB.isReturnBlock()) {
  131. const MachineFrameInfo &MFI = MF.getFrameInfo();
  132. if (MFI.isCalleeSavedInfoValid())
  133. addCalleeSavedRegs(*this, MF);
  134. }
  135. }
  136. void LiveRegUnits::addLiveIns(const MachineBasicBlock &MBB) {
  137. const MachineFunction &MF = *MBB.getParent();
  138. addPristines(MF);
  139. addBlockLiveIns(*this, MBB);
  140. }