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