LiveRegUnits.cpp 5.0 KB

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