blob: 70003e7cc86a22d4fbffbb02862cd24773d3b373 [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"
17#include "LiveIntervalUnion.h"
Andrew Trick071d1c02010-11-09 21:04:34 +000018#include "llvm/ADT/SparseBitVector.h"
Jakob Stoklund Olesenff2e9b42010-12-17 04:09:47 +000019#include "llvm/CodeGen/MachineLoopRanges.h"
Andrew Trick14e8d712010-10-22 23:09:15 +000020#include "llvm/Support/Debug.h"
21#include "llvm/Support/raw_ostream.h"
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000022#include "llvm/Target/TargetRegisterInfo.h"
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 Olesen43142682011-01-09 03:05:53 +000082 OS << "LIU " << PrintReg(RepReg, TRI);
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +000083 if (empty()) {
84 OS << " empty\n";
85 return;
86 }
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000087 for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +000088 OS << " [" << SI.start() << ' ' << SI.stop() << "):"
89 << PrintReg(SI.value()->reg, TRI);
Andrew Trick071d1c02010-11-09 21:04:34 +000090 }
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +000091 OS << '\n';
92}
93
94void LiveIntervalUnion::InterferenceResult::print(raw_ostream &OS,
95 const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesen43142682011-01-09 03:05:53 +000096 OS << '[' << start() << ';' << stop() << "):"
97 << PrintReg(interference()->reg, TRI);
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +000098}
99
100void LiveIntervalUnion::Query::print(raw_ostream &OS,
101 const TargetRegisterInfo *TRI) {
102 OS << "Interferences with ";
103 LiveUnion->print(OS, TRI);
104 InterferenceResult IR = firstInterference();
105 while (isInterference(IR)) {
106 OS << " ";
107 IR.print(OS, TRI);
108 OS << '\n';
109 nextInterference(IR);
110 }
Andrew Trick071d1c02010-11-09 21:04:34 +0000111}
112
Andrew Trick071d1c02010-11-09 21:04:34 +0000113#ifndef NDEBUG
114// Verify the live intervals in this union and add them to the visited set.
Andrew Trick18c57a82010-11-30 23:18:47 +0000115void LiveIntervalUnion::verify(LiveVirtRegBitSet& VisitedVRegs) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000116 for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI)
117 VisitedVRegs.set(SI.value()->reg);
Andrew Trick071d1c02010-11-09 21:04:34 +0000118}
119#endif //!NDEBUG
120
Andrew Trick14e8d712010-10-22 23:09:15 +0000121// Private interface accessed by Query.
122//
123// Find a pair of segments that intersect, one in the live virtual register
124// (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query)
125// is responsible for advancing the LiveIntervalUnion segments to find a
126// "notable" intersection, which requires query-specific logic.
Andrew Trick18c57a82010-11-30 23:18:47 +0000127//
Andrew Trick14e8d712010-10-22 23:09:15 +0000128// This design assumes only a fast mechanism for intersecting a single live
129// virtual register segment with a set of LiveIntervalUnion segments. This may
Andrew Trick34fff592010-11-30 23:59:50 +0000130// be ok since most virtual registers have very few segments. If we had a data
Andrew Trick14e8d712010-10-22 23:09:15 +0000131// structure that optimizd MxN intersection of segments, then we would bypass
132// the loop that advances within the LiveInterval.
133//
Andrew Trick18c57a82010-11-30 23:18:47 +0000134// If no intersection exists, set VirtRegI = VirtRegEnd, and set SI to the first
Andrew Trick14e8d712010-10-22 23:09:15 +0000135// segment whose start point is greater than LiveInterval's end point.
136//
137// Assumes that segments are sorted by start position in both
138// LiveInterval and LiveSegments.
Andrew Trick18c57a82010-11-30 23:18:47 +0000139void LiveIntervalUnion::Query::findIntersection(InterferenceResult &IR) const {
Andrew Trick18c57a82010-11-30 23:18:47 +0000140 // Search until reaching the end of the LiveUnion segments.
141 LiveInterval::iterator VirtRegEnd = VirtReg->end();
Jakob Stoklund Olesen9b0c4f82010-12-08 23:51:35 +0000142 if (IR.VirtRegI == VirtRegEnd)
143 return;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000144 while (IR.LiveUnionI.valid()) {
Andrew Trick14e8d712010-10-22 23:09:15 +0000145 // Slowly advance the live virtual reg iterator until we surpass the next
Andrew Trick18c57a82010-11-30 23:18:47 +0000146 // segment in LiveUnion.
147 //
148 // Note: If this is ever used for coalescing of fixed registers and we have
149 // a live vreg with thousands of segments, then change this code to use
150 // upperBound instead.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000151 IR.VirtRegI = VirtReg->advanceTo(IR.VirtRegI, IR.LiveUnionI.start());
Andrew Trick18c57a82010-11-30 23:18:47 +0000152 if (IR.VirtRegI == VirtRegEnd)
153 break; // Retain current (nonoverlapping) LiveUnionI
154
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000155 // VirtRegI may have advanced far beyond LiveUnionI, catch up.
156 IR.LiveUnionI.advanceTo(IR.VirtRegI->start);
Andrew Trick18c57a82010-11-30 23:18:47 +0000157
158 // Check if no LiveUnionI exists with VirtRegI->Start < LiveUnionI.end
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000159 if (!IR.LiveUnionI.valid())
Andrew Trick14e8d712010-10-22 23:09:15 +0000160 break;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000161 if (IR.LiveUnionI.start() < IR.VirtRegI->end) {
162 assert(overlap(*IR.VirtRegI, IR.LiveUnionI) &&
Andrew Trick18c57a82010-11-30 23:18:47 +0000163 "upperBound postcondition");
Andrew Trick14e8d712010-10-22 23:09:15 +0000164 break;
165 }
166 }
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000167 if (!IR.LiveUnionI.valid())
Andrew Trick18c57a82010-11-30 23:18:47 +0000168 IR.VirtRegI = VirtRegEnd;
Andrew Trick14e8d712010-10-22 23:09:15 +0000169}
170
171// Find the first intersection, and cache interference info
Andrew Trick18c57a82010-11-30 23:18:47 +0000172// (retain segment iterators into both VirtReg and LiveUnion).
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000173const LiveIntervalUnion::InterferenceResult &
Andrew Trick14e8d712010-10-22 23:09:15 +0000174LiveIntervalUnion::Query::firstInterference() {
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000175 if (CheckedFirstInterference)
Andrew Trick18c57a82010-11-30 23:18:47 +0000176 return FirstInterference;
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000177 CheckedFirstInterference = true;
178 InterferenceResult &IR = FirstInterference;
Jakob Stoklund Olesen3f5bedf2011-04-11 21:47:01 +0000179 IR.LiveUnionI.setMap(LiveUnion->getMap());
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000180
181 // Quickly skip interference check for empty sets.
182 if (VirtReg->empty() || LiveUnion->empty()) {
183 IR.VirtRegI = VirtReg->end();
184 } else if (VirtReg->beginIndex() < LiveUnion->startIndex()) {
185 // VirtReg starts first, perform double binary search.
186 IR.VirtRegI = VirtReg->find(LiveUnion->startIndex());
187 if (IR.VirtRegI != VirtReg->end())
Jakob Stoklund Olesen3f5bedf2011-04-11 21:47:01 +0000188 IR.LiveUnionI.find(IR.VirtRegI->start);
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000189 } else {
190 // LiveUnion starts first, perform double binary search.
Jakob Stoklund Olesen3f5bedf2011-04-11 21:47:01 +0000191 IR.LiveUnionI.find(VirtReg->beginIndex());
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000192 if (IR.LiveUnionI.valid())
193 IR.VirtRegI = VirtReg->find(IR.LiveUnionI.start());
194 else
195 IR.VirtRegI = VirtReg->end();
Andrew Trick14e8d712010-10-22 23:09:15 +0000196 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000197 findIntersection(FirstInterference);
Jakob Stoklund Olesena0382c62010-12-09 21:20:44 +0000198 assert((IR.VirtRegI == VirtReg->end() || IR.LiveUnionI.valid())
199 && "Uninitialized iterator");
Andrew Trick18c57a82010-11-30 23:18:47 +0000200 return FirstInterference;
Andrew Trick14e8d712010-10-22 23:09:15 +0000201}
202
203// Treat the result as an iterator and advance to the next interfering pair
204// of segments. This is a plain iterator with no filter.
Andrew Trick18c57a82010-11-30 23:18:47 +0000205bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &IR) const {
206 assert(isInterference(IR) && "iteration past end of interferences");
207
208 // Advance either the VirtReg or LiveUnion segment to ensure that we visit all
209 // unique overlapping pairs.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000210 if (IR.VirtRegI->end < IR.LiveUnionI.stop()) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000211 if (++IR.VirtRegI == VirtReg->end())
Andrew Trick14e8d712010-10-22 23:09:15 +0000212 return false;
213 }
214 else {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000215 if (!(++IR.LiveUnionI).valid()) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000216 IR.VirtRegI = VirtReg->end();
Andrew Trick14e8d712010-10-22 23:09:15 +0000217 return false;
218 }
219 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000220 // Short-circuit findIntersection() if possible.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000221 if (overlap(*IR.VirtRegI, IR.LiveUnionI))
Andrew Trick14e8d712010-10-22 23:09:15 +0000222 return true;
Andrew Trick18c57a82010-11-30 23:18:47 +0000223
224 // Find the next intersection.
225 findIntersection(IR);
226 return isInterference(IR);
Andrew Trick14e8d712010-10-22 23:09:15 +0000227}
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000228
Andrew Trick18c57a82010-11-30 23:18:47 +0000229// Scan the vector of interfering virtual registers in this union. Assume it's
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000230// quite small.
Andrew Trick18c57a82010-11-30 23:18:47 +0000231bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *VirtReg) const {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000232 SmallVectorImpl<LiveInterval*>::const_iterator I =
Andrew Trick18c57a82010-11-30 23:18:47 +0000233 std::find(InterferingVRegs.begin(), InterferingVRegs.end(), VirtReg);
234 return I != InterferingVRegs.end();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000235}
236
237// Count the number of virtual registers in this union that interfere with this
Andrew Trick18c57a82010-11-30 23:18:47 +0000238// query's live virtual register.
239//
240// The number of times that we either advance IR.VirtRegI or call
241// LiveUnion.upperBound() will be no more than the number of holes in
242// VirtReg. So each invocation of collectInterferingVRegs() takes
243// time proportional to |VirtReg Holes| * time(LiveUnion.upperBound()).
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000244//
245// For comments on how to speed it up, see Query::findIntersection().
246unsigned LiveIntervalUnion::Query::
Jakob Stoklund Olesen51458ed2011-07-08 20:46:18 +0000247collectInterferingVRegs(unsigned MaxInterferingRegs) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000248 InterferenceResult IR = firstInterference();
249 LiveInterval::iterator VirtRegEnd = VirtReg->end();
Andrew Trick18c57a82010-11-30 23:18:47 +0000250 LiveInterval *RecentInterferingVReg = NULL;
Jakob Stoklund Olesen8d121402010-12-17 23:16:38 +0000251 if (IR.VirtRegI != VirtRegEnd) while (IR.LiveUnionI.valid()) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000252 // Advance the union's iterator to reach an unseen interfering vreg.
253 do {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000254 if (IR.LiveUnionI.value() == RecentInterferingVReg)
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000255 continue;
256
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000257 if (!isSeenInterference(IR.LiveUnionI.value()))
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000258 break;
259
260 // Cache the most recent interfering vreg to bypass isSeenInterference.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000261 RecentInterferingVReg = IR.LiveUnionI.value();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000262
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000263 } while ((++IR.LiveUnionI).valid());
264 if (!IR.LiveUnionI.valid())
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000265 break;
266
Andrew Trick18c57a82010-11-30 23:18:47 +0000267 // Advance the VirtReg iterator until surpassing the next segment in
268 // LiveUnion.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000269 IR.VirtRegI = VirtReg->advanceTo(IR.VirtRegI, IR.LiveUnionI.start());
Andrew Trick18c57a82010-11-30 23:18:47 +0000270 if (IR.VirtRegI == VirtRegEnd)
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000271 break;
272
273 // Check for intersection with the union's segment.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000274 if (overlap(*IR.VirtRegI, IR.LiveUnionI)) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000275
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000276 if (!IR.LiveUnionI.value()->isSpillable())
Andrew Trick18c57a82010-11-30 23:18:47 +0000277 SeenUnspillableVReg = true;
278
Andrew Trick18c57a82010-11-30 23:18:47 +0000279 if (InterferingVRegs.size() == MaxInterferingRegs)
Andrew Trickb853e6c2010-12-09 18:15:21 +0000280 // Leave SeenAllInterferences set to false to indicate that at least one
281 // interference exists beyond those we collected.
Andrew Trick18c57a82010-11-30 23:18:47 +0000282 return MaxInterferingRegs;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000283
Andrew Trickb853e6c2010-12-09 18:15:21 +0000284 InterferingVRegs.push_back(IR.LiveUnionI.value());
285
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000286 // Cache the most recent interfering vreg to bypass isSeenInterference.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000287 RecentInterferingVReg = IR.LiveUnionI.value();
Andrew Trick18c57a82010-11-30 23:18:47 +0000288 ++IR.LiveUnionI;
Jakob Stoklund Olesen3f5bedf2011-04-11 21:47:01 +0000289
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000290 continue;
291 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000292 // VirtRegI may have advanced far beyond LiveUnionI,
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000293 // do a fast intersection test to "catch up"
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000294 IR.LiveUnionI.advanceTo(IR.VirtRegI->start);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000295 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000296 SeenAllInterferences = true;
297 return InterferingVRegs.size();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000298}
Jakob Stoklund Olesenff2e9b42010-12-17 04:09:47 +0000299
300bool LiveIntervalUnion::Query::checkLoopInterference(MachineLoopRange *Loop) {
301 // VirtReg is likely live throughout the loop, so start by checking LIU-Loop
302 // overlaps.
303 IntervalMapOverlaps<LiveIntervalUnion::Map, MachineLoopRange::Map>
304 Overlaps(LiveUnion->getMap(), Loop->getMap());
305 if (!Overlaps.valid())
306 return false;
307
308 // The loop is overlapping an LIU assignment. Check VirtReg as well.
309 LiveInterval::iterator VRI = VirtReg->find(Overlaps.start());
310
311 for (;;) {
312 if (VRI == VirtReg->end())
313 return false;
314 if (VRI->start < Overlaps.stop())
315 return true;
316
317 Overlaps.advanceTo(VRI->start);
318 if (!Overlaps.valid())
319 return false;
320 if (Overlaps.start() < VRI->end)
321 return true;
322
323 VRI = VirtReg->advanceTo(VRI, Overlaps.start());
324 }
325}