blob: 7ebe96f0660e9c089c9f832881ddddd2e468c2f0 [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;
Andrew Trick18c57a82010-11-30 23:18:47 +000031
32 // Insert each of the virtual register's live segments into the map.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000033 LiveInterval::iterator RegPos = VirtReg.begin();
34 LiveInterval::iterator RegEnd = VirtReg.end();
35 SegmentIter SegPos = Segments.find(RegPos->start);
Andrew Trick18c57a82010-11-30 23:18:47 +000036
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000037 for (;;) {
38 SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
39 if (++RegPos == RegEnd)
40 return;
41 SegPos.advanceTo(RegPos->start);
Andrew Trick14e8d712010-10-22 23:09:15 +000042 }
43}
44
Andrew Tricke141a492010-11-08 18:02:08 +000045// Remove a live virtual register's segments from this union.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000046void LiveIntervalUnion::extract(LiveInterval &VirtReg) {
47 if (VirtReg.empty())
48 return;
Andrew Trick18c57a82010-11-30 23:18:47 +000049
Andrew Tricke141a492010-11-08 18:02:08 +000050 // Remove each of the virtual register's live segments from the map.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000051 LiveInterval::iterator RegPos = VirtReg.begin();
52 LiveInterval::iterator RegEnd = VirtReg.end();
53 SegmentIter SegPos = Segments.find(RegPos->start);
Andrew Trick18c57a82010-11-30 23:18:47 +000054
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000055 for (;;) {
56 assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval");
57 SegPos.erase();
58 if (!SegPos.valid())
59 return;
Andrew Trick18c57a82010-11-30 23:18:47 +000060
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000061 // Skip all segments that may have been coalesced.
62 RegPos = VirtReg.advanceTo(RegPos, SegPos.start());
63 if (RegPos == RegEnd)
64 return;
65
66 SegPos.advanceTo(RegPos->start);
Andrew Trick14e8d712010-10-22 23:09:15 +000067 }
Andrew Trick14e8d712010-10-22 23:09:15 +000068}
Andrew Trick14e8d712010-10-22 23:09:15 +000069
Andrew Trick071d1c02010-11-09 21:04:34 +000070void
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000071LiveIntervalUnion::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Andrew Trick18c57a82010-11-30 23:18:47 +000072 OS << "LIU ";
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000073 TRI->printReg(RepReg, OS);
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +000074 if (empty()) {
75 OS << " empty\n";
76 return;
77 }
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000078 for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
79 OS << " [" << SI.start() << ' ' << SI.stop() << "):";
80 TRI->printReg(SI.value()->reg, OS);
Andrew Trick071d1c02010-11-09 21:04:34 +000081 }
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +000082 OS << '\n';
83}
84
85void LiveIntervalUnion::InterferenceResult::print(raw_ostream &OS,
86 const TargetRegisterInfo *TRI) const {
Jakob Stoklund Olesend0bb5e22010-12-15 23:46:13 +000087 OS << '[' << start() << ';' << stop() << "):";
88 TRI->printReg(interference()->reg, OS);
Jakob Stoklund Olesenbfce6782010-12-14 19:38:49 +000089}
90
91void LiveIntervalUnion::Query::print(raw_ostream &OS,
92 const TargetRegisterInfo *TRI) {
93 OS << "Interferences with ";
94 LiveUnion->print(OS, TRI);
95 InterferenceResult IR = firstInterference();
96 while (isInterference(IR)) {
97 OS << " ";
98 IR.print(OS, TRI);
99 OS << '\n';
100 nextInterference(IR);
101 }
Andrew Trick071d1c02010-11-09 21:04:34 +0000102}
103
Andrew Trick071d1c02010-11-09 21:04:34 +0000104#ifndef NDEBUG
105// Verify the live intervals in this union and add them to the visited set.
Andrew Trick18c57a82010-11-30 23:18:47 +0000106void LiveIntervalUnion::verify(LiveVirtRegBitSet& VisitedVRegs) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000107 for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI)
108 VisitedVRegs.set(SI.value()->reg);
Andrew Trick071d1c02010-11-09 21:04:34 +0000109}
110#endif //!NDEBUG
111
Andrew Trick14e8d712010-10-22 23:09:15 +0000112// Private interface accessed by Query.
113//
114// Find a pair of segments that intersect, one in the live virtual register
115// (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query)
116// is responsible for advancing the LiveIntervalUnion segments to find a
117// "notable" intersection, which requires query-specific logic.
Andrew Trick18c57a82010-11-30 23:18:47 +0000118//
Andrew Trick14e8d712010-10-22 23:09:15 +0000119// This design assumes only a fast mechanism for intersecting a single live
120// virtual register segment with a set of LiveIntervalUnion segments. This may
Andrew Trick34fff592010-11-30 23:59:50 +0000121// be ok since most virtual registers have very few segments. If we had a data
Andrew Trick14e8d712010-10-22 23:09:15 +0000122// structure that optimizd MxN intersection of segments, then we would bypass
123// the loop that advances within the LiveInterval.
124//
Andrew Trick18c57a82010-11-30 23:18:47 +0000125// If no intersection exists, set VirtRegI = VirtRegEnd, and set SI to the first
Andrew Trick14e8d712010-10-22 23:09:15 +0000126// segment whose start point is greater than LiveInterval's end point.
127//
128// Assumes that segments are sorted by start position in both
129// LiveInterval and LiveSegments.
Andrew Trick18c57a82010-11-30 23:18:47 +0000130void LiveIntervalUnion::Query::findIntersection(InterferenceResult &IR) const {
Andrew Trick18c57a82010-11-30 23:18:47 +0000131 // Search until reaching the end of the LiveUnion segments.
132 LiveInterval::iterator VirtRegEnd = VirtReg->end();
Jakob Stoklund Olesen9b0c4f82010-12-08 23:51:35 +0000133 if (IR.VirtRegI == VirtRegEnd)
134 return;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000135 while (IR.LiveUnionI.valid()) {
Andrew Trick14e8d712010-10-22 23:09:15 +0000136 // Slowly advance the live virtual reg iterator until we surpass the next
Andrew Trick18c57a82010-11-30 23:18:47 +0000137 // segment in LiveUnion.
138 //
139 // Note: If this is ever used for coalescing of fixed registers and we have
140 // a live vreg with thousands of segments, then change this code to use
141 // upperBound instead.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000142 IR.VirtRegI = VirtReg->advanceTo(IR.VirtRegI, IR.LiveUnionI.start());
Andrew Trick18c57a82010-11-30 23:18:47 +0000143 if (IR.VirtRegI == VirtRegEnd)
144 break; // Retain current (nonoverlapping) LiveUnionI
145
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000146 // VirtRegI may have advanced far beyond LiveUnionI, catch up.
147 IR.LiveUnionI.advanceTo(IR.VirtRegI->start);
Andrew Trick18c57a82010-11-30 23:18:47 +0000148
149 // Check if no LiveUnionI exists with VirtRegI->Start < LiveUnionI.end
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000150 if (!IR.LiveUnionI.valid())
Andrew Trick14e8d712010-10-22 23:09:15 +0000151 break;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000152 if (IR.LiveUnionI.start() < IR.VirtRegI->end) {
153 assert(overlap(*IR.VirtRegI, IR.LiveUnionI) &&
Andrew Trick18c57a82010-11-30 23:18:47 +0000154 "upperBound postcondition");
Andrew Trick14e8d712010-10-22 23:09:15 +0000155 break;
156 }
157 }
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000158 if (!IR.LiveUnionI.valid())
Andrew Trick18c57a82010-11-30 23:18:47 +0000159 IR.VirtRegI = VirtRegEnd;
Andrew Trick14e8d712010-10-22 23:09:15 +0000160}
161
162// Find the first intersection, and cache interference info
Andrew Trick18c57a82010-11-30 23:18:47 +0000163// (retain segment iterators into both VirtReg and LiveUnion).
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000164const LiveIntervalUnion::InterferenceResult &
Andrew Trick14e8d712010-10-22 23:09:15 +0000165LiveIntervalUnion::Query::firstInterference() {
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000166 if (CheckedFirstInterference)
Andrew Trick18c57a82010-11-30 23:18:47 +0000167 return FirstInterference;
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000168 CheckedFirstInterference = true;
169 InterferenceResult &IR = FirstInterference;
170
171 // Quickly skip interference check for empty sets.
172 if (VirtReg->empty() || LiveUnion->empty()) {
173 IR.VirtRegI = VirtReg->end();
174 } else if (VirtReg->beginIndex() < LiveUnion->startIndex()) {
175 // VirtReg starts first, perform double binary search.
176 IR.VirtRegI = VirtReg->find(LiveUnion->startIndex());
177 if (IR.VirtRegI != VirtReg->end())
178 IR.LiveUnionI = LiveUnion->find(IR.VirtRegI->start);
179 } else {
180 // LiveUnion starts first, perform double binary search.
181 IR.LiveUnionI = LiveUnion->find(VirtReg->beginIndex());
182 if (IR.LiveUnionI.valid())
183 IR.VirtRegI = VirtReg->find(IR.LiveUnionI.start());
184 else
185 IR.VirtRegI = VirtReg->end();
Andrew Trick14e8d712010-10-22 23:09:15 +0000186 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000187 findIntersection(FirstInterference);
Jakob Stoklund Olesena0382c62010-12-09 21:20:44 +0000188 assert((IR.VirtRegI == VirtReg->end() || IR.LiveUnionI.valid())
189 && "Uninitialized iterator");
Andrew Trick18c57a82010-11-30 23:18:47 +0000190 return FirstInterference;
Andrew Trick14e8d712010-10-22 23:09:15 +0000191}
192
193// Treat the result as an iterator and advance to the next interfering pair
194// of segments. This is a plain iterator with no filter.
Andrew Trick18c57a82010-11-30 23:18:47 +0000195bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &IR) const {
196 assert(isInterference(IR) && "iteration past end of interferences");
197
198 // Advance either the VirtReg or LiveUnion segment to ensure that we visit all
199 // unique overlapping pairs.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000200 if (IR.VirtRegI->end < IR.LiveUnionI.stop()) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000201 if (++IR.VirtRegI == VirtReg->end())
Andrew Trick14e8d712010-10-22 23:09:15 +0000202 return false;
203 }
204 else {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000205 if (!(++IR.LiveUnionI).valid()) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000206 IR.VirtRegI = VirtReg->end();
Andrew Trick14e8d712010-10-22 23:09:15 +0000207 return false;
208 }
209 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000210 // Short-circuit findIntersection() if possible.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000211 if (overlap(*IR.VirtRegI, IR.LiveUnionI))
Andrew Trick14e8d712010-10-22 23:09:15 +0000212 return true;
Andrew Trick18c57a82010-11-30 23:18:47 +0000213
214 // Find the next intersection.
215 findIntersection(IR);
216 return isInterference(IR);
Andrew Trick14e8d712010-10-22 23:09:15 +0000217}
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000218
Andrew Trick18c57a82010-11-30 23:18:47 +0000219// Scan the vector of interfering virtual registers in this union. Assume it's
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000220// quite small.
Andrew Trick18c57a82010-11-30 23:18:47 +0000221bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *VirtReg) const {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000222 SmallVectorImpl<LiveInterval*>::const_iterator I =
Andrew Trick18c57a82010-11-30 23:18:47 +0000223 std::find(InterferingVRegs.begin(), InterferingVRegs.end(), VirtReg);
224 return I != InterferingVRegs.end();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000225}
226
227// Count the number of virtual registers in this union that interfere with this
Andrew Trick18c57a82010-11-30 23:18:47 +0000228// query's live virtual register.
229//
230// The number of times that we either advance IR.VirtRegI or call
231// LiveUnion.upperBound() will be no more than the number of holes in
232// VirtReg. So each invocation of collectInterferingVRegs() takes
233// time proportional to |VirtReg Holes| * time(LiveUnion.upperBound()).
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000234//
235// For comments on how to speed it up, see Query::findIntersection().
236unsigned LiveIntervalUnion::Query::
Andrew Trick18c57a82010-11-30 23:18:47 +0000237collectInterferingVRegs(unsigned MaxInterferingRegs) {
238 InterferenceResult IR = firstInterference();
239 LiveInterval::iterator VirtRegEnd = VirtReg->end();
Andrew Trick18c57a82010-11-30 23:18:47 +0000240 LiveInterval *RecentInterferingVReg = NULL;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000241 while (IR.LiveUnionI.valid()) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000242 // Advance the union's iterator to reach an unseen interfering vreg.
243 do {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000244 if (IR.LiveUnionI.value() == RecentInterferingVReg)
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000245 continue;
246
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000247 if (!isSeenInterference(IR.LiveUnionI.value()))
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000248 break;
249
250 // Cache the most recent interfering vreg to bypass isSeenInterference.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000251 RecentInterferingVReg = IR.LiveUnionI.value();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000252
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000253 } while ((++IR.LiveUnionI).valid());
254 if (!IR.LiveUnionI.valid())
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000255 break;
256
Andrew Trick18c57a82010-11-30 23:18:47 +0000257 // Advance the VirtReg iterator until surpassing the next segment in
258 // LiveUnion.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000259 IR.VirtRegI = VirtReg->advanceTo(IR.VirtRegI, IR.LiveUnionI.start());
Andrew Trick18c57a82010-11-30 23:18:47 +0000260 if (IR.VirtRegI == VirtRegEnd)
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000261 break;
262
263 // Check for intersection with the union's segment.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000264 if (overlap(*IR.VirtRegI, IR.LiveUnionI)) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000265
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000266 if (!IR.LiveUnionI.value()->isSpillable())
Andrew Trick18c57a82010-11-30 23:18:47 +0000267 SeenUnspillableVReg = true;
268
Andrew Trick18c57a82010-11-30 23:18:47 +0000269 if (InterferingVRegs.size() == MaxInterferingRegs)
Andrew Trickb853e6c2010-12-09 18:15:21 +0000270 // Leave SeenAllInterferences set to false to indicate that at least one
271 // interference exists beyond those we collected.
Andrew Trick18c57a82010-11-30 23:18:47 +0000272 return MaxInterferingRegs;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000273
Andrew Trickb853e6c2010-12-09 18:15:21 +0000274 InterferingVRegs.push_back(IR.LiveUnionI.value());
275
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000276 // Cache the most recent interfering vreg to bypass isSeenInterference.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000277 RecentInterferingVReg = IR.LiveUnionI.value();
Andrew Trick18c57a82010-11-30 23:18:47 +0000278 ++IR.LiveUnionI;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000279 continue;
280 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000281 // VirtRegI may have advanced far beyond LiveUnionI,
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000282 // do a fast intersection test to "catch up"
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000283 IR.LiveUnionI.advanceTo(IR.VirtRegI->start);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000284 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000285 SeenAllInterferences = true;
286 return InterferingVRegs.size();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000287}
Jakob Stoklund Olesenff2e9b42010-12-17 04:09:47 +0000288
289bool LiveIntervalUnion::Query::checkLoopInterference(MachineLoopRange *Loop) {
290 // VirtReg is likely live throughout the loop, so start by checking LIU-Loop
291 // overlaps.
292 IntervalMapOverlaps<LiveIntervalUnion::Map, MachineLoopRange::Map>
293 Overlaps(LiveUnion->getMap(), Loop->getMap());
294 if (!Overlaps.valid())
295 return false;
296
297 // The loop is overlapping an LIU assignment. Check VirtReg as well.
298 LiveInterval::iterator VRI = VirtReg->find(Overlaps.start());
299
300 for (;;) {
301 if (VRI == VirtReg->end())
302 return false;
303 if (VRI->start < Overlaps.stop())
304 return true;
305
306 Overlaps.advanceTo(VRI->start);
307 if (!Overlaps.valid())
308 return false;
309 if (Overlaps.start() < VRI->end)
310 return true;
311
312 VRI = VirtReg->advanceTo(VRI, Overlaps.start());
313 }
314}