Eugene Zelenko | ad3a540 | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 1 | //===- LiveIntervalUnion.cpp - Live interval union data structure ---------===// |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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 | |
Eugene Zelenko | ad3a540 | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 16 | #include "llvm/CodeGen/LiveIntervalUnion.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
| 18 | #include "llvm/ADT/SparseBitVector.h" |
| 19 | #include "llvm/CodeGen/LiveInterval.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/TargetRegisterInfo.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 21 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | ad3a540 | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 22 | #include <cassert> |
| 23 | #include <cstdlib> |
Lang Hames | b638c78 | 2011-12-21 20:16:11 +0000 | [diff] [blame] | 24 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 25 | using namespace llvm; |
| 26 | |
Chandler Carruth | 8677f2f | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 27 | #define DEBUG_TYPE "regalloc" |
| 28 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 29 | // Merge a LiveInterval's segments. Guarantee no overlaps. |
Matthias Braun | 7b54b4d | 2014-12-10 01:12:59 +0000 | [diff] [blame] | 30 | void LiveIntervalUnion::unify(LiveInterval &VirtReg, const LiveRange &Range) { |
| 31 | if (Range.empty()) |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 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. |
Matthias Braun | 7b54b4d | 2014-12-10 01:12:59 +0000 | [diff] [blame] | 36 | LiveRange::const_iterator RegPos = Range.begin(); |
| 37 | LiveRange::const_iterator RegEnd = Range.end(); |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 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. |
Matthias Braun | 7b54b4d | 2014-12-10 01:12:59 +0000 | [diff] [blame] | 57 | void LiveIntervalUnion::extract(LiveInterval &VirtReg, const LiveRange &Range) { |
| 58 | if (Range.empty()) |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 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. |
Matthias Braun | 7b54b4d | 2014-12-10 01:12:59 +0000 | [diff] [blame] | 63 | LiveRange::const_iterator RegPos = Range.begin(); |
| 64 | LiveRange::const_iterator RegEnd = Range.end(); |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 65 | SegmentIter SegPos = Segments.find(RegPos->start); |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 66 | |
Eugene Zelenko | ad3a540 | 2017-02-17 21:43:25 +0000 | [diff] [blame] | 67 | while (true) { |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 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. |
Matthias Braun | 7b54b4d | 2014-12-10 01:12:59 +0000 | [diff] [blame] | 74 | RegPos = Range.advanceTo(RegPos, SegPos.start()); |
Jakob Stoklund Olesen | 953af2c | 2010-12-07 23:18:47 +0000 | [diff] [blame] | 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() << "):" |
Francis Visoiu Mistrih | accb337 | 2017-11-28 12:42:37 +0000 | [diff] [blame] | 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 { |
David Majnemer | 975248e | 2016-08-11 22:21:41 +0000 | [diff] [blame] | 106 | return is_contained(InterferingVRegs, VirtReg); |
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. |
Matthias Braun | a9e2ca0 | 2017-03-01 21:48:12 +0000 | [diff] [blame] | 129 | if (LR->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 | |
Matthias Braun | a9e2ca0 | 2017-03-01 21:48:12 +0000 | [diff] [blame] | 134 | // In most cases, the union will start before LR. |
| 135 | LRI = LR->begin(); |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 136 | LiveUnionI.setMap(LiveUnion->getMap()); |
Matthias Braun | a9e2ca0 | 2017-03-01 21:48:12 +0000 | [diff] [blame] | 137 | LiveUnionI.find(LRI->start); |
Jakob Stoklund Olesen | fe026e182 | 2011-08-11 22:46:04 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Matthias Braun | a9e2ca0 | 2017-03-01 21:48:12 +0000 | [diff] [blame] | 140 | LiveRange::const_iterator LREnd = LR->end(); |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 141 | LiveInterval *RecentReg = nullptr; |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 142 | while (LiveUnionI.valid()) { |
Matthias Braun | a9e2ca0 | 2017-03-01 21:48:12 +0000 | [diff] [blame] | 143 | assert(LRI != LREnd && "Reached end of LR"); |
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. |
Matthias Braun | a9e2ca0 | 2017-03-01 21:48:12 +0000 | [diff] [blame] | 146 | while (LRI->start < LiveUnionI.stop() && LRI->end > LiveUnionI.start()) { |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 147 | // This is an overlap, record the interfering register. |
| 148 | LiveInterval *VReg = LiveUnionI.value(); |
| 149 | if (VReg != RecentReg && !isSeenInterference(VReg)) { |
| 150 | RecentReg = VReg; |
| 151 | InterferingVRegs.push_back(VReg); |
| 152 | if (InterferingVRegs.size() >= MaxInterferingRegs) |
| 153 | return InterferingVRegs.size(); |
| 154 | } |
| 155 | // This LiveUnion segment is no longer interesting. |
| 156 | if (!(++LiveUnionI).valid()) { |
| 157 | SeenAllInterferences = true; |
| 158 | return InterferingVRegs.size(); |
| 159 | } |
| 160 | } |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 161 | |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 162 | // The iterators are now not overlapping, LiveUnionI has been advanced |
Matthias Braun | a9e2ca0 | 2017-03-01 21:48:12 +0000 | [diff] [blame] | 163 | // beyond LRI. |
| 164 | assert(LRI->end <= LiveUnionI.start() && "Expected non-overlap"); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 165 | |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 166 | // Advance the iterator that ends first. |
Matthias Braun | a9e2ca0 | 2017-03-01 21:48:12 +0000 | [diff] [blame] | 167 | LRI = LR->advanceTo(LRI, LiveUnionI.start()); |
| 168 | if (LRI == LREnd) |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 169 | break; |
| 170 | |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 171 | // Detect overlap, handle above. |
Matthias Braun | a9e2ca0 | 2017-03-01 21:48:12 +0000 | [diff] [blame] | 172 | if (LRI->start < LiveUnionI.stop()) |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 173 | continue; |
Jakob Stoklund Olesen | 9b7ff12 | 2011-08-12 00:22:04 +0000 | [diff] [blame] | 174 | |
| 175 | // Still not overlapping. Catch up LiveUnionI. |
Matthias Braun | a9e2ca0 | 2017-03-01 21:48:12 +0000 | [diff] [blame] | 176 | LiveUnionI.advanceTo(LRI->start); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 177 | } |
Andrew Trick | 18c57a8 | 2010-11-30 23:18:47 +0000 | [diff] [blame] | 178 | SeenAllInterferences = true; |
| 179 | return InterferingVRegs.size(); |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 180 | } |
Jakob Stoklund Olesen | ff2e9b4 | 2010-12-17 04:09:47 +0000 | [diff] [blame] | 181 | |
Jakob Stoklund Olesen | 0e5a60b | 2012-06-05 23:57:30 +0000 | [diff] [blame] | 182 | void LiveIntervalUnion::Array::init(LiveIntervalUnion::Allocator &Alloc, |
| 183 | unsigned NSize) { |
| 184 | // Reuse existing allocation. |
| 185 | if (NSize == Size) |
| 186 | return; |
| 187 | clear(); |
| 188 | Size = NSize; |
| 189 | LIUs = static_cast<LiveIntervalUnion*>( |
Serge Pavlov | 06c71d8 | 2018-02-20 05:41:26 +0000 | [diff] [blame] | 190 | safe_malloc(sizeof(LiveIntervalUnion)*NSize)); |
Jakob Stoklund Olesen | 0e5a60b | 2012-06-05 23:57:30 +0000 | [diff] [blame] | 191 | for (unsigned i = 0; i != Size; ++i) |
| 192 | new(LIUs + i) LiveIntervalUnion(Alloc); |
| 193 | } |
| 194 | |
| 195 | void LiveIntervalUnion::Array::clear() { |
| 196 | if (!LIUs) |
| 197 | return; |
| 198 | for (unsigned i = 0; i != Size; ++i) |
| 199 | LIUs[i].~LiveIntervalUnion(); |
| 200 | free(LIUs); |
| 201 | Size = 0; |
Craig Topper | 4ba8443 | 2014-04-14 00:51:57 +0000 | [diff] [blame] | 202 | LIUs = nullptr; |
Jakob Stoklund Olesen | 0e5a60b | 2012-06-05 23:57:30 +0000 | [diff] [blame] | 203 | } |