blob: d5a81a311c647886ce4fc04fd698e950b1dd768b [file] [log] [blame]
Andrew Trick14e8d712010-10-22 23:09:15 +00001//===-- 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 Olesen1ead68d2012-11-28 19:13:06 +000017#include "llvm/CodeGen/LiveIntervalUnion.h"
Andrew Trick071d1c02010-11-09 21:04:34 +000018#include "llvm/ADT/SparseBitVector.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000021#include "llvm/Target/TargetRegisterInfo.h"
Lang Hamesb638c782011-12-21 20:16:11 +000022#include <algorithm>
23
Andrew Trick14e8d712010-10-22 23:09:15 +000024using namespace llvm;
25
Andrew Tricke141a492010-11-08 18:02:08 +000026
Andrew Trick14e8d712010-10-22 23:09:15 +000027// Merge a LiveInterval's segments. Guarantee no overlaps.
Andrew Trick18c57a82010-11-30 23:18:47 +000028void LiveIntervalUnion::unify(LiveInterval &VirtReg) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000029 if (VirtReg.empty())
30 return;
Jakob Stoklund Olesen4f6364f2011-02-09 21:52:03 +000031 ++Tag;
Andrew Trick18c57a82010-11-30 23:18:47 +000032
33 // Insert each of the virtual register's live segments into the map.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000034 LiveInterval::iterator RegPos = VirtReg.begin();
35 LiveInterval::iterator RegEnd = VirtReg.end();
36 SegmentIter SegPos = Segments.find(RegPos->start);
Andrew Trick18c57a82010-11-30 23:18:47 +000037
Jakob Stoklund Olesen11983cd2011-04-11 15:00:44 +000038 while (SegPos.valid()) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000039 SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
40 if (++RegPos == RegEnd)
41 return;
42 SegPos.advanceTo(RegPos->start);
Andrew Trick14e8d712010-10-22 23:09:15 +000043 }
Jakob Stoklund Olesen11983cd2011-04-11 15:00:44 +000044
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 Trick14e8d712010-10-22 23:09:15 +000052}
53
Andrew Tricke141a492010-11-08 18:02:08 +000054// Remove a live virtual register's segments from this union.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000055void LiveIntervalUnion::extract(LiveInterval &VirtReg) {
56 if (VirtReg.empty())
57 return;
Jakob Stoklund Olesen4f6364f2011-02-09 21:52:03 +000058 ++Tag;
Andrew Trick18c57a82010-11-30 23:18:47 +000059
Andrew Tricke141a492010-11-08 18:02:08 +000060 // Remove each of the virtual register's live segments from the map.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000061 LiveInterval::iterator RegPos = VirtReg.begin();
62 LiveInterval::iterator RegEnd = VirtReg.end();
63 SegmentIter SegPos = Segments.find(RegPos->start);
Andrew Trick18c57a82010-11-30 23:18:47 +000064
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000065 for (;;) {
66 assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval");
67 SegPos.erase();
68 if (!SegPos.valid())
69 return;
Andrew Trick18c57a82010-11-30 23:18:47 +000070
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000071 // 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 Trick14e8d712010-10-22 23:09:15 +000077 }
Andrew Trick14e8d712010-10-22 23:09:15 +000078}
Andrew Trick14e8d712010-10-22 23:09:15 +000079
Andrew Trick071d1c02010-11-09 21:04:34 +000080void
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000081LiveIntervalUnion::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +000082 if (empty()) {
83 OS << " empty\n";
84 return;
85 }
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000086 for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +000087 OS << " [" << SI.start() << ' ' << SI.stop() << "):"
88 << PrintReg(SI.value()->reg, TRI);
Andrew Trick071d1c02010-11-09 21:04:34 +000089 }
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +000090 OS << '\n';
91}
92
Andrew Trick071d1c02010-11-09 21:04:34 +000093#ifndef NDEBUG
94// Verify the live intervals in this union and add them to the visited set.
Andrew Trick18c57a82010-11-30 23:18:47 +000095void LiveIntervalUnion::verify(LiveVirtRegBitSet& VisitedVRegs) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000096 for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI)
97 VisitedVRegs.set(SI.value()->reg);
Andrew Trick071d1c02010-11-09 21:04:34 +000098}
99#endif //!NDEBUG
100
Andrew Trick18c57a82010-11-30 23:18:47 +0000101// Scan the vector of interfering virtual registers in this union. Assume it's
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000102// quite small.
Andrew Trick18c57a82010-11-30 23:18:47 +0000103bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *VirtReg) const {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000104 SmallVectorImpl<LiveInterval*>::const_iterator I =
Andrew Trick18c57a82010-11-30 23:18:47 +0000105 std::find(InterferingVRegs.begin(), InterferingVRegs.end(), VirtReg);
106 return I != InterferingVRegs.end();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000107}
108
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000109// Collect virtual registers in this union that interfere with this
Andrew Trick18c57a82010-11-30 23:18:47 +0000110// query's live virtual register.
111//
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000112// The query state is one of:
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000113//
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000114// 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 Trickf4baeaf2010-11-10 19:18:47 +0000118unsigned LiveIntervalUnion::Query::
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000119collectInterferingVRegs(unsigned MaxInterferingRegs) {
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000120 // Fast path return if we already have the desired information.
121 if (SeenAllInterferences || InterferingVRegs.size() >= MaxInterferingRegs)
Jakob Stoklund Olesen9942ba92011-08-11 21:18:34 +0000122 return InterferingVRegs.size();
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000123
124 // Set up iterators on the first call.
125 if (!CheckedFirstInterference) {
126 CheckedFirstInterference = true;
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000127
128 // Quickly skip interference check for empty sets.
129 if (VirtReg->empty() || LiveUnion->empty()) {
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000130 SeenAllInterferences = true;
131 return 0;
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000132 }
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000133
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 Olesenfe026e1822011-08-11 22:46:04 +0000138 }
139
Andrew Trick18c57a82010-11-30 23:18:47 +0000140 LiveInterval::iterator VirtRegEnd = VirtReg->end();
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000141 LiveInterval *RecentReg = 0;
142 while (LiveUnionI.valid()) {
143 assert(VirtRegI != VirtRegEnd && "Reached end of VirtReg");
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000144
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000145 // 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 Trickf4baeaf2010-11-10 19:18:47 +0000162
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000163 // The iterators are now not overlapping, LiveUnionI has been advanced
164 // beyond VirtRegI.
165 assert(VirtRegI->end <= LiveUnionI.start() && "Expected non-overlap");
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000166
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000167 // Advance the iterator that ends first.
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000168 VirtRegI = VirtReg->advanceTo(VirtRegI, LiveUnionI.start());
169 if (VirtRegI == VirtRegEnd)
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000170 break;
171
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000172 // Detect overlap, handle above.
173 if (VirtRegI->start < LiveUnionI.stop())
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000174 continue;
Jakob Stoklund Olesen9b7ff122011-08-12 00:22:04 +0000175
176 // Still not overlapping. Catch up LiveUnionI.
Jakob Stoklund Olesenfe026e1822011-08-11 22:46:04 +0000177 LiveUnionI.advanceTo(VirtRegI->start);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000178 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000179 SeenAllInterferences = true;
180 return InterferingVRegs.size();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000181}
Jakob Stoklund Olesenff2e9b42010-12-17 04:09:47 +0000182
Jakob Stoklund Olesen0e5a60b2012-06-05 23:57:30 +0000183void 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
196void 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}