2
0

LiveIntervalUnion.cpp 6.6 KB

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