LiveRegUnits.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. }
  25. }
  26. }
  27. void LiveRegUnits::addRegsInMask(const uint32_t *RegMask) {
  28. for (unsigned U = 0, E = TRI->getNumRegUnits(); U != E; ++U) {
  29. for (MCRegUnitRootIterator RootReg(U, TRI); RootReg.isValid(); ++RootReg) {
  30. if (MachineOperand::clobbersPhysReg(RegMask, *RootReg))
  31. Units.set(U);
  32. }
  33. }
  34. }
  35. void LiveRegUnits::stepBackward(const MachineInstr &MI) {
  36. // Remove defined registers and regmask kills from the set.
  37. for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
  38. if (MOP.isRegMask()) {
  39. removeRegsNotPreserved(MOP.getRegMask());
  40. continue;
  41. }
  42. if (MOP.isDef())
  43. removeReg(MOP.getReg());
  44. }
  45. // Add uses to the set.
  46. for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
  47. if (!MOP.isReg() || !MOP.readsReg())
  48. continue;
  49. addReg(MOP.getReg());
  50. }
  51. }
  52. void LiveRegUnits::accumulate(const MachineInstr &MI) {
  53. // Add defs, uses and regmask clobbers to the set.
  54. for (const MachineOperand &MOP : phys_regs_and_masks(MI)) {
  55. if (MOP.isRegMask()) {
  56. addRegsInMask(MOP.getRegMask());
  57. continue;
  58. }
  59. if (!MOP.isDef() && !MOP.readsReg())
  60. continue;
  61. addReg(MOP.getReg());
  62. }
  63. }
  64. /// Add live-in registers of basic block \p MBB to \p LiveUnits.
  65. static void addBlockLiveIns(LiveRegUnits &LiveUnits,
  66. const MachineBasicBlock &MBB) {
  67. for (const auto &LI : MBB.liveins())
  68. LiveUnits.addRegMasked(LI.PhysReg, LI.LaneMask);
  69. }
  70. /// Adds all callee saved registers to \p LiveUnits.
  71. static void addCalleeSavedRegs(LiveRegUnits &LiveUnits,
  72. const MachineFunction &MF) {
  73. const MachineRegisterInfo &MRI = MF.getRegInfo();
  74. const MachineFrameInfo &MFI = MF.getFrameInfo();
  75. for (const MCPhysReg *CSR = MRI.getCalleeSavedRegs(); CSR && *CSR; ++CSR) {
  76. const unsigned N = *CSR;
  77. const auto &CSI = MFI.getCalleeSavedInfo();
  78. auto Info =
  79. llvm::find_if(CSI, [N](auto Info) { return Info.getReg() == N; });
  80. // If we have no info for this callee-saved register, assume it is liveout
  81. if (Info == CSI.end() || Info->isRestored())
  82. LiveUnits.addReg(N);
  83. }
  84. }
  85. void LiveRegUnits::addPristines(const MachineFunction &MF) {
  86. const MachineFrameInfo &MFI = MF.getFrameInfo();
  87. if (!MFI.isCalleeSavedInfoValid())
  88. return;
  89. /// This function will usually be called on an empty object, handle this
  90. /// as a special case.
  91. if (empty()) {
  92. /// Add all callee saved regs, then remove the ones that are saved and
  93. /// restored.
  94. addCalleeSavedRegs(*this, MF);
  95. /// Remove the ones that are not saved/restored; they are pristine.
  96. for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
  97. removeReg(Info.getReg());
  98. return;
  99. }
  100. /// If a callee-saved register that is not pristine is already present
  101. /// in the set, we should make sure that it stays in it. Precompute the
  102. /// set of pristine registers in a separate object.
  103. /// Add all callee saved regs, then remove the ones that are saved+restored.
  104. LiveRegUnits Pristine(*TRI);
  105. addCalleeSavedRegs(Pristine, MF);
  106. /// Remove the ones that are not saved/restored; they are pristine.
  107. for (const CalleeSavedInfo &Info : MFI.getCalleeSavedInfo())
  108. Pristine.removeReg(Info.getReg());
  109. addUnits(Pristine.getBitVector());
  110. }
  111. void LiveRegUnits::addLiveOuts(const MachineBasicBlock &MBB) {
  112. const MachineFunction &MF = *MBB.getParent();
  113. addPristines(MF);
  114. // To get the live-outs we simply merge the live-ins of all successors.
  115. for (const MachineBasicBlock *Succ : MBB.successors())
  116. addBlockLiveIns(*this, *Succ);
  117. // For the return block: Add all callee saved registers.
  118. if (MBB.isReturnBlock()) {
  119. const MachineFrameInfo &MFI = MF.getFrameInfo();
  120. if (MFI.isCalleeSavedInfoValid())
  121. addCalleeSavedRegs(*this, MF);
  122. }
  123. }
  124. void LiveRegUnits::addLiveIns(const MachineBasicBlock &MBB) {
  125. const MachineFunction &MF = *MBB.getParent();
  126. addPristines(MF);
  127. addBlockLiveIns(*this, MBB);
  128. }