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