LiveIntervalUnion.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //===- LiveIntervalUnion.cpp - Live interval union data structure ---------===//
  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. // LiveIntervalUnion represents a coalesced set of live intervals. This may be
  10. // used during coalescing to represent a congruence class, or during register
  11. // allocation to model liveness of a physical register.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/CodeGen/LiveIntervalUnion.h"
  15. #include "llvm/ADT/STLExtras.h"
  16. #include "llvm/ADT/SparseBitVector.h"
  17. #include "llvm/CodeGen/LiveInterval.h"
  18. #include "llvm/CodeGen/TargetRegisterInfo.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. #include <cassert>
  21. #include <cstdlib>
  22. using namespace llvm;
  23. #define DEBUG_TYPE "regalloc"
  24. // Merge a LiveInterval's segments. Guarantee no overlaps.
  25. void LiveIntervalUnion::unify(LiveInterval &VirtReg, const LiveRange &Range) {
  26. if (Range.empty())
  27. return;
  28. ++Tag;
  29. // Insert each of the virtual register's live segments into the map.
  30. LiveRange::const_iterator RegPos = Range.begin();
  31. LiveRange::const_iterator RegEnd = Range.end();
  32. SegmentIter SegPos = Segments.find(RegPos->start);
  33. while (SegPos.valid()) {
  34. SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
  35. if (++RegPos == RegEnd)
  36. return;
  37. SegPos.advanceTo(RegPos->start);
  38. }
  39. // We have reached the end of Segments, so it is no longer necessary to search
  40. // for the insertion position.
  41. // It is faster to insert the end first.
  42. --RegEnd;
  43. SegPos.insert(RegEnd->start, RegEnd->end, &VirtReg);
  44. for (; RegPos != RegEnd; ++RegPos, ++SegPos)
  45. SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
  46. }
  47. // Remove a live virtual register's segments from this union.
  48. void LiveIntervalUnion::extract(LiveInterval &VirtReg, const LiveRange &Range) {
  49. if (Range.empty())
  50. return;
  51. ++Tag;
  52. // Remove each of the virtual register's live segments from the map.
  53. LiveRange::const_iterator RegPos = Range.begin();
  54. LiveRange::const_iterator RegEnd = Range.end();
  55. SegmentIter SegPos = Segments.find(RegPos->start);
  56. while (true) {
  57. assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval");
  58. SegPos.erase();
  59. if (!SegPos.valid())
  60. return;
  61. // Skip all segments that may have been coalesced.
  62. RegPos = Range.advanceTo(RegPos, SegPos.start());
  63. if (RegPos == RegEnd)
  64. return;
  65. SegPos.advanceTo(RegPos->start);
  66. }
  67. }
  68. void
  69. LiveIntervalUnion::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
  70. if (empty()) {
  71. OS << " empty\n";
  72. return;
  73. }
  74. for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
  75. OS << " [" << SI.start() << ' ' << SI.stop() << "):"
  76. << printReg(SI.value()->reg, TRI);
  77. }
  78. OS << '\n';
  79. }
  80. #ifndef NDEBUG
  81. // Verify the live intervals in this union and add them to the visited set.
  82. void LiveIntervalUnion::verify(LiveVirtRegBitSet& VisitedVRegs) {
  83. for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI)
  84. VisitedVRegs.set(SI.value()->reg);
  85. }
  86. #endif //!NDEBUG
  87. // Scan the vector of interfering virtual registers in this union. Assume it's
  88. // quite small.
  89. bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *VirtReg) const {
  90. return is_contained(InterferingVRegs, VirtReg);
  91. }
  92. // Collect virtual registers in this union that interfere with this
  93. // query's live virtual register.
  94. //
  95. // The query state is one of:
  96. //
  97. // 1. CheckedFirstInterference == false: Iterators are uninitialized.
  98. // 2. SeenAllInterferences == true: InterferingVRegs complete, iterators unused.
  99. // 3. Iterators left at the last seen intersection.
  100. //
  101. unsigned LiveIntervalUnion::Query::
  102. collectInterferingVRegs(unsigned MaxInterferingRegs) {
  103. // Fast path return if we already have the desired information.
  104. if (SeenAllInterferences || InterferingVRegs.size() >= MaxInterferingRegs)
  105. return InterferingVRegs.size();
  106. // Set up iterators on the first call.
  107. if (!CheckedFirstInterference) {
  108. CheckedFirstInterference = true;
  109. // Quickly skip interference check for empty sets.
  110. if (LR->empty() || LiveUnion->empty()) {
  111. SeenAllInterferences = true;
  112. return 0;
  113. }
  114. // In most cases, the union will start before LR.
  115. LRI = LR->begin();
  116. LiveUnionI.setMap(LiveUnion->getMap());
  117. LiveUnionI.find(LRI->start);
  118. }
  119. LiveRange::const_iterator LREnd = LR->end();
  120. LiveInterval *RecentReg = nullptr;
  121. while (LiveUnionI.valid()) {
  122. assert(LRI != LREnd && "Reached end of LR");
  123. // Check for overlapping interference.
  124. while (LRI->start < LiveUnionI.stop() && LRI->end > LiveUnionI.start()) {
  125. // This is an overlap, record the interfering register.
  126. LiveInterval *VReg = LiveUnionI.value();
  127. if (VReg != RecentReg && !isSeenInterference(VReg)) {
  128. RecentReg = VReg;
  129. InterferingVRegs.push_back(VReg);
  130. if (InterferingVRegs.size() >= MaxInterferingRegs)
  131. return InterferingVRegs.size();
  132. }
  133. // This LiveUnion segment is no longer interesting.
  134. if (!(++LiveUnionI).valid()) {
  135. SeenAllInterferences = true;
  136. return InterferingVRegs.size();
  137. }
  138. }
  139. // The iterators are now not overlapping, LiveUnionI has been advanced
  140. // beyond LRI.
  141. assert(LRI->end <= LiveUnionI.start() && "Expected non-overlap");
  142. // Advance the iterator that ends first.
  143. LRI = LR->advanceTo(LRI, LiveUnionI.start());
  144. if (LRI == LREnd)
  145. break;
  146. // Detect overlap, handle above.
  147. if (LRI->start < LiveUnionI.stop())
  148. continue;
  149. // Still not overlapping. Catch up LiveUnionI.
  150. LiveUnionI.advanceTo(LRI->start);
  151. }
  152. SeenAllInterferences = true;
  153. return InterferingVRegs.size();
  154. }
  155. void LiveIntervalUnion::Array::init(LiveIntervalUnion::Allocator &Alloc,
  156. unsigned NSize) {
  157. // Reuse existing allocation.
  158. if (NSize == Size)
  159. return;
  160. clear();
  161. Size = NSize;
  162. LIUs = static_cast<LiveIntervalUnion*>(
  163. safe_malloc(sizeof(LiveIntervalUnion)*NSize));
  164. for (unsigned i = 0; i != Size; ++i)
  165. new(LIUs + i) LiveIntervalUnion(Alloc);
  166. }
  167. void LiveIntervalUnion::Array::clear() {
  168. if (!LIUs)
  169. return;
  170. for (unsigned i = 0; i != Size; ++i)
  171. LIUs[i].~LiveIntervalUnion();
  172. free(LIUs);
  173. Size = 0;
  174. LIUs = nullptr;
  175. }