blob: 4b9a2d302c09cd2e7de8cdef47e13bc8b2a95578 [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"
Andrew Trick14e8d712010-10-22 23:09:15 +000019#include "llvm/Support/Debug.h"
20#include "llvm/Support/raw_ostream.h"
21#include <algorithm>
22using namespace llvm;
23
Andrew Tricke141a492010-11-08 18:02:08 +000024
Andrew Trick14e8d712010-10-22 23:09:15 +000025// Merge a LiveInterval's segments. Guarantee no overlaps.
Andrew Trick18c57a82010-11-30 23:18:47 +000026void LiveIntervalUnion::unify(LiveInterval &VirtReg) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000027 if (VirtReg.empty())
28 return;
Andrew Trick18c57a82010-11-30 23:18:47 +000029
30 // Insert each of the virtual register's live segments into the map.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000031 LiveInterval::iterator RegPos = VirtReg.begin();
32 LiveInterval::iterator RegEnd = VirtReg.end();
33 SegmentIter SegPos = Segments.find(RegPos->start);
Andrew Trick18c57a82010-11-30 23:18:47 +000034
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000035 for (;;) {
36 SegPos.insert(RegPos->start, RegPos->end, &VirtReg);
37 if (++RegPos == RegEnd)
38 return;
39 SegPos.advanceTo(RegPos->start);
Andrew Trick14e8d712010-10-22 23:09:15 +000040 }
41}
42
Andrew Tricke141a492010-11-08 18:02:08 +000043// Remove a live virtual register's segments from this union.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000044void LiveIntervalUnion::extract(LiveInterval &VirtReg) {
45 if (VirtReg.empty())
46 return;
Andrew Trick18c57a82010-11-30 23:18:47 +000047
Andrew Tricke141a492010-11-08 18:02:08 +000048 // Remove each of the virtual register's live segments from the map.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000049 LiveInterval::iterator RegPos = VirtReg.begin();
50 LiveInterval::iterator RegEnd = VirtReg.end();
51 SegmentIter SegPos = Segments.find(RegPos->start);
Andrew Trick18c57a82010-11-30 23:18:47 +000052
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000053 for (;;) {
54 assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval");
55 SegPos.erase();
56 if (!SegPos.valid())
57 return;
Andrew Trick18c57a82010-11-30 23:18:47 +000058
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000059 // Skip all segments that may have been coalesced.
60 RegPos = VirtReg.advanceTo(RegPos, SegPos.start());
61 if (RegPos == RegEnd)
62 return;
63
64 SegPos.advanceTo(RegPos->start);
Andrew Trick14e8d712010-10-22 23:09:15 +000065 }
Andrew Trick14e8d712010-10-22 23:09:15 +000066}
Andrew Trick14e8d712010-10-22 23:09:15 +000067
Andrew Trick071d1c02010-11-09 21:04:34 +000068void
Andrew Trick18c57a82010-11-30 23:18:47 +000069LiveIntervalUnion::print(raw_ostream &OS,
70 const AbstractRegisterDescription *RegDesc) const {
71 OS << "LIU ";
72 if (RegDesc != NULL)
73 OS << RegDesc->getName(RepReg);
Andrew Trick071d1c02010-11-09 21:04:34 +000074 else {
Andrew Trick18c57a82010-11-30 23:18:47 +000075 OS << RepReg;
Andrew Trick071d1c02010-11-09 21:04:34 +000076 }
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000077 for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI)
78 dbgs() << " [" << SI.start() << ' ' << SI.stop() << "):%reg"
79 << SI.value()->reg;
Andrew Trick18c57a82010-11-30 23:18:47 +000080 OS << "\n";
Andrew Trick071d1c02010-11-09 21:04:34 +000081}
82
Andrew Trick18c57a82010-11-30 23:18:47 +000083void LiveIntervalUnion::dump(const AbstractRegisterDescription *RegDesc) const {
84 print(dbgs(), RegDesc);
Andrew Trick071d1c02010-11-09 21:04:34 +000085}
86
87#ifndef NDEBUG
88// Verify the live intervals in this union and add them to the visited set.
Andrew Trick18c57a82010-11-30 23:18:47 +000089void LiveIntervalUnion::verify(LiveVirtRegBitSet& VisitedVRegs) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000090 for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI)
91 VisitedVRegs.set(SI.value()->reg);
Andrew Trick071d1c02010-11-09 21:04:34 +000092}
93#endif //!NDEBUG
94
Andrew Trick14e8d712010-10-22 23:09:15 +000095// Private interface accessed by Query.
96//
97// Find a pair of segments that intersect, one in the live virtual register
98// (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query)
99// is responsible for advancing the LiveIntervalUnion segments to find a
100// "notable" intersection, which requires query-specific logic.
Andrew Trick18c57a82010-11-30 23:18:47 +0000101//
Andrew Trick14e8d712010-10-22 23:09:15 +0000102// This design assumes only a fast mechanism for intersecting a single live
103// virtual register segment with a set of LiveIntervalUnion segments. This may
Andrew Trick34fff592010-11-30 23:59:50 +0000104// be ok since most virtual registers have very few segments. If we had a data
Andrew Trick14e8d712010-10-22 23:09:15 +0000105// structure that optimizd MxN intersection of segments, then we would bypass
106// the loop that advances within the LiveInterval.
107//
Andrew Trick18c57a82010-11-30 23:18:47 +0000108// If no intersection exists, set VirtRegI = VirtRegEnd, and set SI to the first
Andrew Trick14e8d712010-10-22 23:09:15 +0000109// segment whose start point is greater than LiveInterval's end point.
110//
111// Assumes that segments are sorted by start position in both
112// LiveInterval and LiveSegments.
Andrew Trick18c57a82010-11-30 23:18:47 +0000113void LiveIntervalUnion::Query::findIntersection(InterferenceResult &IR) const {
Andrew Trick18c57a82010-11-30 23:18:47 +0000114 // Search until reaching the end of the LiveUnion segments.
115 LiveInterval::iterator VirtRegEnd = VirtReg->end();
Jakob Stoklund Olesen9b0c4f82010-12-08 23:51:35 +0000116 if (IR.VirtRegI == VirtRegEnd)
117 return;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000118 while (IR.LiveUnionI.valid()) {
Andrew Trick14e8d712010-10-22 23:09:15 +0000119 // Slowly advance the live virtual reg iterator until we surpass the next
Andrew Trick18c57a82010-11-30 23:18:47 +0000120 // segment in LiveUnion.
121 //
122 // Note: If this is ever used for coalescing of fixed registers and we have
123 // a live vreg with thousands of segments, then change this code to use
124 // upperBound instead.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000125 IR.VirtRegI = VirtReg->advanceTo(IR.VirtRegI, IR.LiveUnionI.start());
Andrew Trick18c57a82010-11-30 23:18:47 +0000126 if (IR.VirtRegI == VirtRegEnd)
127 break; // Retain current (nonoverlapping) LiveUnionI
128
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000129 // VirtRegI may have advanced far beyond LiveUnionI, catch up.
130 IR.LiveUnionI.advanceTo(IR.VirtRegI->start);
Andrew Trick18c57a82010-11-30 23:18:47 +0000131
132 // Check if no LiveUnionI exists with VirtRegI->Start < LiveUnionI.end
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000133 if (!IR.LiveUnionI.valid())
Andrew Trick14e8d712010-10-22 23:09:15 +0000134 break;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000135 if (IR.LiveUnionI.start() < IR.VirtRegI->end) {
136 assert(overlap(*IR.VirtRegI, IR.LiveUnionI) &&
Andrew Trick18c57a82010-11-30 23:18:47 +0000137 "upperBound postcondition");
Andrew Trick14e8d712010-10-22 23:09:15 +0000138 break;
139 }
140 }
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000141 if (!IR.LiveUnionI.valid())
Andrew Trick18c57a82010-11-30 23:18:47 +0000142 IR.VirtRegI = VirtRegEnd;
Andrew Trick14e8d712010-10-22 23:09:15 +0000143}
144
145// Find the first intersection, and cache interference info
Andrew Trick18c57a82010-11-30 23:18:47 +0000146// (retain segment iterators into both VirtReg and LiveUnion).
Andrew Trick14e8d712010-10-22 23:09:15 +0000147LiveIntervalUnion::InterferenceResult
148LiveIntervalUnion::Query::firstInterference() {
Andrew Trick18c57a82010-11-30 23:18:47 +0000149 if (FirstInterference != LiveIntervalUnion::InterferenceResult()) {
150 return FirstInterference;
Andrew Trick14e8d712010-10-22 23:09:15 +0000151 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000152 FirstInterference = InterferenceResult(VirtReg->begin(), LiveUnion->begin());
153 findIntersection(FirstInterference);
154 return FirstInterference;
Andrew Trick14e8d712010-10-22 23:09:15 +0000155}
156
157// Treat the result as an iterator and advance to the next interfering pair
158// of segments. This is a plain iterator with no filter.
Andrew Trick18c57a82010-11-30 23:18:47 +0000159bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &IR) const {
160 assert(isInterference(IR) && "iteration past end of interferences");
161
162 // Advance either the VirtReg or LiveUnion segment to ensure that we visit all
163 // unique overlapping pairs.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000164 if (IR.VirtRegI->end < IR.LiveUnionI.stop()) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000165 if (++IR.VirtRegI == VirtReg->end())
Andrew Trick14e8d712010-10-22 23:09:15 +0000166 return false;
167 }
168 else {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000169 if (!(++IR.LiveUnionI).valid()) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000170 IR.VirtRegI = VirtReg->end();
Andrew Trick14e8d712010-10-22 23:09:15 +0000171 return false;
172 }
173 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000174 // Short-circuit findIntersection() if possible.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000175 if (overlap(*IR.VirtRegI, IR.LiveUnionI))
Andrew Trick14e8d712010-10-22 23:09:15 +0000176 return true;
Andrew Trick18c57a82010-11-30 23:18:47 +0000177
178 // Find the next intersection.
179 findIntersection(IR);
180 return isInterference(IR);
Andrew Trick14e8d712010-10-22 23:09:15 +0000181}
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000182
Andrew Trick18c57a82010-11-30 23:18:47 +0000183// Scan the vector of interfering virtual registers in this union. Assume it's
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000184// quite small.
Andrew Trick18c57a82010-11-30 23:18:47 +0000185bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *VirtReg) const {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000186 SmallVectorImpl<LiveInterval*>::const_iterator I =
Andrew Trick18c57a82010-11-30 23:18:47 +0000187 std::find(InterferingVRegs.begin(), InterferingVRegs.end(), VirtReg);
188 return I != InterferingVRegs.end();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000189}
190
191// Count the number of virtual registers in this union that interfere with this
Andrew Trick18c57a82010-11-30 23:18:47 +0000192// query's live virtual register.
193//
194// The number of times that we either advance IR.VirtRegI or call
195// LiveUnion.upperBound() will be no more than the number of holes in
196// VirtReg. So each invocation of collectInterferingVRegs() takes
197// time proportional to |VirtReg Holes| * time(LiveUnion.upperBound()).
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000198//
199// For comments on how to speed it up, see Query::findIntersection().
200unsigned LiveIntervalUnion::Query::
Andrew Trick18c57a82010-11-30 23:18:47 +0000201collectInterferingVRegs(unsigned MaxInterferingRegs) {
202 InterferenceResult IR = firstInterference();
203 LiveInterval::iterator VirtRegEnd = VirtReg->end();
Andrew Trick18c57a82010-11-30 23:18:47 +0000204 LiveInterval *RecentInterferingVReg = NULL;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000205 while (IR.LiveUnionI.valid()) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000206 // Advance the union's iterator to reach an unseen interfering vreg.
207 do {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000208 if (IR.LiveUnionI.value() == RecentInterferingVReg)
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000209 continue;
210
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000211 if (!isSeenInterference(IR.LiveUnionI.value()))
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000212 break;
213
214 // Cache the most recent interfering vreg to bypass isSeenInterference.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000215 RecentInterferingVReg = IR.LiveUnionI.value();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000216
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000217 } while ((++IR.LiveUnionI).valid());
218 if (!IR.LiveUnionI.valid())
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000219 break;
220
Andrew Trick18c57a82010-11-30 23:18:47 +0000221 // Advance the VirtReg iterator until surpassing the next segment in
222 // LiveUnion.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000223 IR.VirtRegI = VirtReg->advanceTo(IR.VirtRegI, IR.LiveUnionI.start());
Andrew Trick18c57a82010-11-30 23:18:47 +0000224 if (IR.VirtRegI == VirtRegEnd)
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000225 break;
226
227 // Check for intersection with the union's segment.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000228 if (overlap(*IR.VirtRegI, IR.LiveUnionI)) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000229
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000230 if (!IR.LiveUnionI.value()->isSpillable())
Andrew Trick18c57a82010-11-30 23:18:47 +0000231 SeenUnspillableVReg = true;
232
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000233 InterferingVRegs.push_back(IR.LiveUnionI.value());
Andrew Trick18c57a82010-11-30 23:18:47 +0000234 if (InterferingVRegs.size() == MaxInterferingRegs)
235 return MaxInterferingRegs;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000236
237 // Cache the most recent interfering vreg to bypass isSeenInterference.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000238 RecentInterferingVReg = IR.LiveUnionI.value();
Andrew Trick18c57a82010-11-30 23:18:47 +0000239 ++IR.LiveUnionI;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000240 continue;
241 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000242 // VirtRegI may have advanced far beyond LiveUnionI,
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000243 // do a fast intersection test to "catch up"
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000244 IR.LiveUnionI.advanceTo(IR.VirtRegI->start);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000245 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000246 SeenAllInterferences = true;
247 return InterferingVRegs.size();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000248}