LiveRegMatrix.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //===- LiveRegMatrix.cpp - Track register interference --------------------===//
  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. // This file defines the LiveRegMatrix analysis pass.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/LiveRegMatrix.h"
  13. #include "RegisterCoalescer.h"
  14. #include "llvm/ADT/Statistic.h"
  15. #include "llvm/CodeGen/LiveInterval.h"
  16. #include "llvm/CodeGen/LiveIntervalUnion.h"
  17. #include "llvm/CodeGen/LiveIntervals.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/CodeGen/TargetRegisterInfo.h"
  20. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  21. #include "llvm/CodeGen/VirtRegMap.h"
  22. #include "llvm/MC/LaneBitmask.h"
  23. #include "llvm/MC/MCRegisterInfo.h"
  24. #include "llvm/Pass.h"
  25. #include "llvm/Support/Debug.h"
  26. #include "llvm/Support/raw_ostream.h"
  27. #include <cassert>
  28. using namespace llvm;
  29. #define DEBUG_TYPE "regalloc"
  30. STATISTIC(NumAssigned , "Number of registers assigned");
  31. STATISTIC(NumUnassigned , "Number of registers unassigned");
  32. char LiveRegMatrix::ID = 0;
  33. INITIALIZE_PASS_BEGIN(LiveRegMatrix, "liveregmatrix",
  34. "Live Register Matrix", false, false)
  35. INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
  36. INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
  37. INITIALIZE_PASS_END(LiveRegMatrix, "liveregmatrix",
  38. "Live Register Matrix", false, false)
  39. LiveRegMatrix::LiveRegMatrix() : MachineFunctionPass(ID) {}
  40. void LiveRegMatrix::getAnalysisUsage(AnalysisUsage &AU) const {
  41. AU.setPreservesAll();
  42. AU.addRequiredTransitive<LiveIntervals>();
  43. AU.addRequiredTransitive<VirtRegMap>();
  44. MachineFunctionPass::getAnalysisUsage(AU);
  45. }
  46. bool LiveRegMatrix::runOnMachineFunction(MachineFunction &MF) {
  47. TRI = MF.getSubtarget().getRegisterInfo();
  48. LIS = &getAnalysis<LiveIntervals>();
  49. VRM = &getAnalysis<VirtRegMap>();
  50. unsigned NumRegUnits = TRI->getNumRegUnits();
  51. if (NumRegUnits != Matrix.size())
  52. Queries.reset(new LiveIntervalUnion::Query[NumRegUnits]);
  53. Matrix.init(LIUAlloc, NumRegUnits);
  54. // Make sure no stale queries get reused.
  55. invalidateVirtRegs();
  56. return false;
  57. }
  58. void LiveRegMatrix::releaseMemory() {
  59. for (unsigned i = 0, e = Matrix.size(); i != e; ++i) {
  60. Matrix[i].clear();
  61. // No need to clear Queries here, since LiveIntervalUnion::Query doesn't
  62. // have anything important to clear and LiveRegMatrix's runOnFunction()
  63. // does a std::unique_ptr::reset anyways.
  64. }
  65. }
  66. template <typename Callable>
  67. static bool foreachUnit(const TargetRegisterInfo *TRI,
  68. LiveInterval &VRegInterval, unsigned PhysReg,
  69. Callable Func) {
  70. if (VRegInterval.hasSubRanges()) {
  71. for (MCRegUnitMaskIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
  72. unsigned Unit = (*Units).first;
  73. LaneBitmask Mask = (*Units).second;
  74. for (LiveInterval::SubRange &S : VRegInterval.subranges()) {
  75. if ((S.LaneMask & Mask).any()) {
  76. if (Func(Unit, S))
  77. return true;
  78. break;
  79. }
  80. }
  81. }
  82. } else {
  83. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
  84. if (Func(*Units, VRegInterval))
  85. return true;
  86. }
  87. }
  88. return false;
  89. }
  90. void LiveRegMatrix::assign(LiveInterval &VirtReg, unsigned PhysReg) {
  91. LLVM_DEBUG(dbgs() << "assigning " << printReg(VirtReg.reg, TRI) << " to "
  92. << printReg(PhysReg, TRI) << ':');
  93. assert(!VRM->hasPhys(VirtReg.reg) && "Duplicate VirtReg assignment");
  94. VRM->assignVirt2Phys(VirtReg.reg, PhysReg);
  95. foreachUnit(
  96. TRI, VirtReg, PhysReg, [&](unsigned Unit, const LiveRange &Range) {
  97. LLVM_DEBUG(dbgs() << ' ' << printRegUnit(Unit, TRI) << ' ' << Range);
  98. Matrix[Unit].unify(VirtReg, Range);
  99. return false;
  100. });
  101. ++NumAssigned;
  102. LLVM_DEBUG(dbgs() << '\n');
  103. }
  104. void LiveRegMatrix::unassign(LiveInterval &VirtReg) {
  105. Register PhysReg = VRM->getPhys(VirtReg.reg);
  106. LLVM_DEBUG(dbgs() << "unassigning " << printReg(VirtReg.reg, TRI) << " from "
  107. << printReg(PhysReg, TRI) << ':');
  108. VRM->clearVirt(VirtReg.reg);
  109. foreachUnit(TRI, VirtReg, PhysReg,
  110. [&](unsigned Unit, const LiveRange &Range) {
  111. LLVM_DEBUG(dbgs() << ' ' << printRegUnit(Unit, TRI));
  112. Matrix[Unit].extract(VirtReg, Range);
  113. return false;
  114. });
  115. ++NumUnassigned;
  116. LLVM_DEBUG(dbgs() << '\n');
  117. }
  118. bool LiveRegMatrix::isPhysRegUsed(unsigned PhysReg) const {
  119. for (MCRegUnitIterator Unit(PhysReg, TRI); Unit.isValid(); ++Unit) {
  120. if (!Matrix[*Unit].empty())
  121. return true;
  122. }
  123. return false;
  124. }
  125. bool LiveRegMatrix::checkRegMaskInterference(LiveInterval &VirtReg,
  126. unsigned PhysReg) {
  127. // Check if the cached information is valid.
  128. // The same BitVector can be reused for all PhysRegs.
  129. // We could cache multiple VirtRegs if it becomes necessary.
  130. if (RegMaskVirtReg != VirtReg.reg || RegMaskTag != UserTag) {
  131. RegMaskVirtReg = VirtReg.reg;
  132. RegMaskTag = UserTag;
  133. RegMaskUsable.clear();
  134. LIS->checkRegMaskInterference(VirtReg, RegMaskUsable);
  135. }
  136. // The BitVector is indexed by PhysReg, not register unit.
  137. // Regmask interference is more fine grained than regunits.
  138. // For example, a Win64 call can clobber %ymm8 yet preserve %xmm8.
  139. return !RegMaskUsable.empty() && (!PhysReg || !RegMaskUsable.test(PhysReg));
  140. }
  141. bool LiveRegMatrix::checkRegUnitInterference(LiveInterval &VirtReg,
  142. unsigned PhysReg) {
  143. if (VirtReg.empty())
  144. return false;
  145. CoalescerPair CP(VirtReg.reg, PhysReg, *TRI);
  146. bool Result = foreachUnit(TRI, VirtReg, PhysReg, [&](unsigned Unit,
  147. const LiveRange &Range) {
  148. const LiveRange &UnitRange = LIS->getRegUnit(Unit);
  149. return Range.overlaps(UnitRange, CP, *LIS->getSlotIndexes());
  150. });
  151. return Result;
  152. }
  153. LiveIntervalUnion::Query &LiveRegMatrix::query(const LiveRange &LR,
  154. unsigned RegUnit) {
  155. LiveIntervalUnion::Query &Q = Queries[RegUnit];
  156. Q.init(UserTag, LR, Matrix[RegUnit]);
  157. return Q;
  158. }
  159. LiveRegMatrix::InterferenceKind
  160. LiveRegMatrix::checkInterference(LiveInterval &VirtReg, unsigned PhysReg) {
  161. if (VirtReg.empty())
  162. return IK_Free;
  163. // Regmask interference is the fastest check.
  164. if (checkRegMaskInterference(VirtReg, PhysReg))
  165. return IK_RegMask;
  166. // Check for fixed interference.
  167. if (checkRegUnitInterference(VirtReg, PhysReg))
  168. return IK_RegUnit;
  169. // Check the matrix for virtual register interference.
  170. bool Interference = foreachUnit(TRI, VirtReg, PhysReg,
  171. [&](unsigned Unit, const LiveRange &LR) {
  172. return query(LR, Unit).checkInterference();
  173. });
  174. if (Interference)
  175. return IK_VirtReg;
  176. return IK_Free;
  177. }
  178. bool LiveRegMatrix::checkInterference(SlotIndex Start, SlotIndex End,
  179. unsigned PhysReg) {
  180. // Construct artificial live range containing only one segment [Start, End).
  181. VNInfo valno(0, Start);
  182. LiveRange::Segment Seg(Start, End, &valno);
  183. LiveRange LR;
  184. LR.addSegment(Seg);
  185. // Check for interference with that segment
  186. for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) {
  187. if (query(LR, *Units).checkInterference())
  188. return true;
  189. }
  190. return false;
  191. }