LiveRegMatrix.cpp 8.7 KB

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