2
0

LiveRegMatrix.cpp 6.6 KB

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