LiveRegMatrix.cpp 6.6 KB

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