Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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 | |
Jakob Stoklund Olesen | 1ead68d | 2012-11-28 19:13:06 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/LiveIntervalUnion.h" |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SparseBitVector.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
| 19 | #include "llvm/Support/raw_ostream.h" |
Jakob Stoklund Olesen | 4a84cce | 2010-12-14 18:53:47 +0000 | [diff] [blame] | 20 | #include "llvm/Target/TargetRegisterInfo.h" |
Lang Hames | b638c78 | 2011-12-21 20:16:11 +0000 | [diff] [blame] | 21 | #include <algorithm> |
| 22 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 25 | #define DEBUG_TYPE "regalloc" |
| 26 | |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 27 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 28 | // Merge a LiveInterval's segments. Guarantee no overlaps. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 29 | void LiveIntervalUnion::unify(LiveInterval &VirtReg) { |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 30 | if (VirtReg.empty()) |
| 31 | return; |
Jakob Stoklund Olesen | 4f6364f | 2011-02-09 21:52:03 +0000 | [diff] [blame] | 32 | ++Tag; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 33 | |
| 34 | // Insert each of the virtual register's live segments into the map. |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 35 | LiveInterval::iterator RegPos = VirtReg.begin(); |
| 36 | LiveInterval::iterator RegEnd = VirtReg.end(); |
| 37 | SegmentIter SegPos = Segments.find(RegPos->start); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 38 | |
Jakob Stoklund Olesen | 11983cd | 2011-04-11 15:00:44 +0000 | [diff] [blame] | 39 | while (SegPos.valid()) { |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 40 | SegPos.insert(RegPos->start, RegPos->end, &VirtReg); |
| 41 | if (++RegPos == RegEnd) |
| 42 | return; |
| 43 | SegPos.advanceTo(RegPos->start); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 44 | } |
Jakob Stoklund Olesen | 11983cd | 2011-04-11 15:00:44 +0000 | [diff] [blame] | 45 | |
| 46 | // We have reached the end of Segments, so it is no longer necessary to search |
| 47 | // for the insertion position. |
| 48 | // It is faster to insert the end first. |
| 49 | --RegEnd; |
| 50 | SegPos.insert(RegEnd->start, RegEnd->end, &VirtReg); |
| 51 | for (; RegPos != RegEnd; ++RegPos, ++SegPos) |
| 52 | SegPos.insert(RegPos->start, RegPos->end, &VirtReg); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 55 | // Remove a live virtual register's segments from this union. |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 56 | void LiveIntervalUnion::extract(LiveInterval &VirtReg) { |
| 57 | if (VirtReg.empty()) |
| 58 | return; |
Jakob Stoklund Olesen | 4f6364f | 2011-02-09 21:52:03 +0000 | [diff] [blame] | 59 | ++Tag; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 60 | |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 61 | // Remove each of the virtual register's live segments from the map. |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 62 | LiveInterval::iterator RegPos = VirtReg.begin(); |
| 63 | LiveInterval::iterator RegEnd = VirtReg.end(); |
| 64 | SegmentIter SegPos = Segments.find(RegPos->start); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 65 | |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 66 | for (;;) { |
| 67 | assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval"); |
| 68 | SegPos.erase(); |
| 69 | if (!SegPos.valid()) |
| 70 | return; |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 71 | |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 72 | // Skip all segments that may have been coalesced. |
| 73 | RegPos = VirtReg.advanceTo(RegPos, SegPos.start()); |
| 74 | if (RegPos == RegEnd) |
| 75 | return; |
| 76 | |
| 77 | SegPos.advanceTo(RegPos->start); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 78 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 79 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 80 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 81 | void |
Jakob Stoklund Olesen | 4a84cce | 2010-12-14 18:53:47 +0000 | [diff] [blame] | 82 | LiveIntervalUnion::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const { |
Jakob Stoklund Olesen | bfce678 | 2010-12-14 19:38:49 +0000 | [diff] [blame] | 83 | if (empty()) { |
| 84 | OS << " empty\n"; |
| 85 | return; |
| 86 | } |
Jakob Stoklund Olesen | 4a84cce | 2010-12-14 18:53:47 +0000 | [diff] [blame] | 87 | for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) { |
Jakob Stoklund Olesen | 4314268 | 2011-01-09 03:05:53 +0000 | [diff] [blame] | 88 | OS << " [" << SI.start() << ' ' << SI.stop() << "):" |
| 89 | << PrintReg(SI.value()->reg, TRI); |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 90 | } |
Jakob Stoklund Olesen | bfce678 | 2010-12-14 19:38:49 +0000 | [diff] [blame] | 91 | OS << '\n'; |
| 92 | } |
| 93 | |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 94 | #ifndef NDEBUG |
| 95 | // Verify the live intervals in this union and add them to the visited set. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 96 | void LiveIntervalUnion::verify(LiveVirtRegBitSet& VisitedVRegs) { |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 97 | for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI) |
| 98 | VisitedVRegs.set(SI.value()->reg); |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 99 | } |
| 100 | #endif //!NDEBUG |
| 101 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 102 | // Scan the vector of interfering virtual registers in this union. Assume it's |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 103 | // quite small. |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 104 | bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *VirtReg) const { |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 105 | SmallVectorImpl<LiveInterval*>::const_iterator I = |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 106 | std::find(InterferingVRegs.begin(), InterferingVRegs.end(), VirtReg); |
| 107 | return I != InterferingVRegs.end(); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 110 | // Collect virtual registers in this union that interfere with this |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 111 | // query's live virtual register. |
| 112 | // |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 113 | // The query state is one of: |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 114 | // |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 115 | // 1. CheckedFirstInterference == false: Iterators are uninitialized. |
| 116 | // 2. SeenAllInterferences == true: InterferingVRegs complete, iterators unused. |
| 117 | // 3. Iterators left at the last seen intersection. |
| 118 | // |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 119 | unsigned LiveIntervalUnion::Query:: |
Jakob Stoklund Olesen | 51458ed | 2011-07-08 20:46:18 +0000 | [diff] [blame] | 120 | collectInterferingVRegs(unsigned MaxInterferingRegs) { |
Jakob Stoklund Olesen | fe026e182 | 2011-08-11 22:46:04 +0000 | [diff] [blame] | 121 | // Fast path return if we already have the desired information. |
| 122 | if (SeenAllInterferences || InterferingVRegs.size() >= MaxInterferingRegs) |
Jakob Stoklund Olesen | 9942ba9 | 2011-08-11 21:18:34 +0000 | [diff] [blame] | 123 | return InterferingVRegs.size(); |
Jakob Stoklund Olesen | fe026e182 | 2011-08-11 22:46:04 +0000 | [diff] [blame] | 124 | |
| 125 | // Set up iterators on the first call. |
| 126 | if (!CheckedFirstInterference) { |
| 127 | CheckedFirstInterference = true; |
Jakob Stoklund Olesen | fe026e182 | 2011-08-11 22:46:04 +0000 | [diff] [blame] | 128 | |
| 129 | // Quickly skip interference check for empty sets. |
| 130 | if (VirtReg->empty() || LiveUnion->empty()) { |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 131 | SeenAllInterferences = true; |
| 132 | return 0; |
Jakob Stoklund Olesen | fe026e182 | 2011-08-11 22:46:04 +0000 | [diff] [blame] | 133 | } |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 134 | |
| 135 | // In most cases, the union will start before VirtReg. |
| 136 | VirtRegI = VirtReg->begin(); |
| 137 | LiveUnionI.setMap(LiveUnion->getMap()); |
| 138 | LiveUnionI.find(VirtRegI->start); |
Jakob Stoklund Olesen | fe026e182 | 2011-08-11 22:46:04 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 141 | LiveInterval::iterator VirtRegEnd = VirtReg->end(); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 142 | LiveInterval *RecentReg = nullptr; |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 143 | while (LiveUnionI.valid()) { |
| 144 | assert(VirtRegI != VirtRegEnd && "Reached end of VirtReg"); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 145 | |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 146 | // Check for overlapping interference. |
| 147 | while (VirtRegI->start < LiveUnionI.stop() && |
| 148 | VirtRegI->end > LiveUnionI.start()) { |
| 149 | // This is an overlap, record the interfering register. |
| 150 | LiveInterval *VReg = LiveUnionI.value(); |
| 151 | if (VReg != RecentReg && !isSeenInterference(VReg)) { |
| 152 | RecentReg = VReg; |
| 153 | InterferingVRegs.push_back(VReg); |
| 154 | if (InterferingVRegs.size() >= MaxInterferingRegs) |
| 155 | return InterferingVRegs.size(); |
| 156 | } |
| 157 | // This LiveUnion segment is no longer interesting. |
| 158 | if (!(++LiveUnionI).valid()) { |
| 159 | SeenAllInterferences = true; |
| 160 | return InterferingVRegs.size(); |
| 161 | } |
| 162 | } |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 163 | |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 164 | // The iterators are now not overlapping, LiveUnionI has been advanced |
| 165 | // beyond VirtRegI. |
| 166 | assert(VirtRegI->end <= LiveUnionI.start() && "Expected non-overlap"); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 167 | |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 168 | // Advance the iterator that ends first. |
Jakob Stoklund Olesen | fe026e182 | 2011-08-11 22:46:04 +0000 | [diff] [blame] | 169 | VirtRegI = VirtReg->advanceTo(VirtRegI, LiveUnionI.start()); |
| 170 | if (VirtRegI == VirtRegEnd) |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 171 | break; |
| 172 | |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 173 | // Detect overlap, handle above. |
| 174 | if (VirtRegI->start < LiveUnionI.stop()) |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 175 | continue; |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 176 | |
| 177 | // Still not overlapping. Catch up LiveUnionI. |
Jakob Stoklund Olesen | fe026e182 | 2011-08-11 22:46:04 +0000 | [diff] [blame] | 178 | LiveUnionI.advanceTo(VirtRegI->start); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 179 | } |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 180 | SeenAllInterferences = true; |
| 181 | return InterferingVRegs.size(); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 182 | } |
Jakob Stoklund Olesen | ff2e9b4 | 2010-12-17 04:09:47 +0000 | [diff] [blame] | 183 | |
Jakob Stoklund Olesen | 0e5a60b | 2012-06-05 23:57:30 +0000 | [diff] [blame] | 184 | void LiveIntervalUnion::Array::init(LiveIntervalUnion::Allocator &Alloc, |
| 185 | unsigned NSize) { |
| 186 | // Reuse existing allocation. |
| 187 | if (NSize == Size) |
| 188 | return; |
| 189 | clear(); |
| 190 | Size = NSize; |
| 191 | LIUs = static_cast<LiveIntervalUnion*>( |
| 192 | malloc(sizeof(LiveIntervalUnion)*NSize)); |
| 193 | for (unsigned i = 0; i != Size; ++i) |
| 194 | new(LIUs + i) LiveIntervalUnion(Alloc); |
| 195 | } |
| 196 | |
| 197 | void LiveIntervalUnion::Array::clear() { |
| 198 | if (!LIUs) |
| 199 | return; |
| 200 | for (unsigned i = 0; i != Size; ++i) |
| 201 | LIUs[i].~LiveIntervalUnion(); |
| 202 | free(LIUs); |
| 203 | Size = 0; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 204 | LIUs = nullptr; |
Jakob Stoklund Olesen | 0e5a60b | 2012-06-05 23:57:30 +0000 | [diff] [blame] | 205 | } |