blob: e8b39914ca8978767525e06de09233418337dd24 [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"
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000021#include "llvm/Target/TargetRegisterInfo.h"
22
Andrew Trick14e8d712010-10-22 23:09:15 +000023#include <algorithm>
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000024
Andrew Trick14e8d712010-10-22 23:09:15 +000025using namespace llvm;
26
Andrew Tricke141a492010-11-08 18:02:08 +000027
Andrew Trick14e8d712010-10-22 23:09:15 +000028// Merge a LiveInterval's segments. Guarantee no overlaps.
Andrew Trick18c57a82010-11-30 23:18:47 +000029void LiveIntervalUnion::unify(LiveInterval &VirtReg) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000030 if (VirtReg.empty())
31 return;
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 Olesen953af2c2010-12-07 23:18:47 +000038 for (;;) {
39 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 }
44}
45
Andrew Tricke141a492010-11-08 18:02:08 +000046// Remove a live virtual register's segments from this union.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000047void LiveIntervalUnion::extract(LiveInterval &VirtReg) {
48 if (VirtReg.empty())
49 return;
Andrew Trick18c57a82010-11-30 23:18:47 +000050
Andrew Tricke141a492010-11-08 18:02:08 +000051 // Remove each of the virtual register's live segments from the map.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000052 LiveInterval::iterator RegPos = VirtReg.begin();
53 LiveInterval::iterator RegEnd = VirtReg.end();
54 SegmentIter SegPos = Segments.find(RegPos->start);
Andrew Trick18c57a82010-11-30 23:18:47 +000055
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000056 for (;;) {
57 assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval");
58 SegPos.erase();
59 if (!SegPos.valid())
60 return;
Andrew Trick18c57a82010-11-30 23:18:47 +000061
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000062 // Skip all segments that may have been coalesced.
63 RegPos = VirtReg.advanceTo(RegPos, SegPos.start());
64 if (RegPos == RegEnd)
65 return;
66
67 SegPos.advanceTo(RegPos->start);
Andrew Trick14e8d712010-10-22 23:09:15 +000068 }
Andrew Trick14e8d712010-10-22 23:09:15 +000069}
Andrew Trick14e8d712010-10-22 23:09:15 +000070
Andrew Trick071d1c02010-11-09 21:04:34 +000071void
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000072LiveIntervalUnion::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
Andrew Trick18c57a82010-11-30 23:18:47 +000073 OS << "LIU ";
Jakob Stoklund Olesen4a84cce2010-12-14 18:53:47 +000074 TRI->printReg(RepReg, OS);
75 for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) {
76 OS << " [" << SI.start() << ' ' << SI.stop() << "):";
77 TRI->printReg(SI.value()->reg, OS);
Andrew Trick071d1c02010-11-09 21:04:34 +000078 }
Andrew Trick18c57a82010-11-30 23:18:47 +000079 OS << "\n";
Andrew Trick071d1c02010-11-09 21:04:34 +000080}
81
Andrew Trick071d1c02010-11-09 21:04:34 +000082#ifndef NDEBUG
83// Verify the live intervals in this union and add them to the visited set.
Andrew Trick18c57a82010-11-30 23:18:47 +000084void LiveIntervalUnion::verify(LiveVirtRegBitSet& VisitedVRegs) {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +000085 for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI)
86 VisitedVRegs.set(SI.value()->reg);
Andrew Trick071d1c02010-11-09 21:04:34 +000087}
88#endif //!NDEBUG
89
Andrew Trick14e8d712010-10-22 23:09:15 +000090// Private interface accessed by Query.
91//
92// Find a pair of segments that intersect, one in the live virtual register
93// (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query)
94// is responsible for advancing the LiveIntervalUnion segments to find a
95// "notable" intersection, which requires query-specific logic.
Andrew Trick18c57a82010-11-30 23:18:47 +000096//
Andrew Trick14e8d712010-10-22 23:09:15 +000097// This design assumes only a fast mechanism for intersecting a single live
98// virtual register segment with a set of LiveIntervalUnion segments. This may
Andrew Trick34fff592010-11-30 23:59:50 +000099// be ok since most virtual registers have very few segments. If we had a data
Andrew Trick14e8d712010-10-22 23:09:15 +0000100// structure that optimizd MxN intersection of segments, then we would bypass
101// the loop that advances within the LiveInterval.
102//
Andrew Trick18c57a82010-11-30 23:18:47 +0000103// If no intersection exists, set VirtRegI = VirtRegEnd, and set SI to the first
Andrew Trick14e8d712010-10-22 23:09:15 +0000104// segment whose start point is greater than LiveInterval's end point.
105//
106// Assumes that segments are sorted by start position in both
107// LiveInterval and LiveSegments.
Andrew Trick18c57a82010-11-30 23:18:47 +0000108void LiveIntervalUnion::Query::findIntersection(InterferenceResult &IR) const {
Andrew Trick18c57a82010-11-30 23:18:47 +0000109 // Search until reaching the end of the LiveUnion segments.
110 LiveInterval::iterator VirtRegEnd = VirtReg->end();
Jakob Stoklund Olesen9b0c4f82010-12-08 23:51:35 +0000111 if (IR.VirtRegI == VirtRegEnd)
112 return;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000113 while (IR.LiveUnionI.valid()) {
Andrew Trick14e8d712010-10-22 23:09:15 +0000114 // Slowly advance the live virtual reg iterator until we surpass the next
Andrew Trick18c57a82010-11-30 23:18:47 +0000115 // segment in LiveUnion.
116 //
117 // Note: If this is ever used for coalescing of fixed registers and we have
118 // a live vreg with thousands of segments, then change this code to use
119 // upperBound instead.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000120 IR.VirtRegI = VirtReg->advanceTo(IR.VirtRegI, IR.LiveUnionI.start());
Andrew Trick18c57a82010-11-30 23:18:47 +0000121 if (IR.VirtRegI == VirtRegEnd)
122 break; // Retain current (nonoverlapping) LiveUnionI
123
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000124 // VirtRegI may have advanced far beyond LiveUnionI, catch up.
125 IR.LiveUnionI.advanceTo(IR.VirtRegI->start);
Andrew Trick18c57a82010-11-30 23:18:47 +0000126
127 // Check if no LiveUnionI exists with VirtRegI->Start < LiveUnionI.end
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000128 if (!IR.LiveUnionI.valid())
Andrew Trick14e8d712010-10-22 23:09:15 +0000129 break;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000130 if (IR.LiveUnionI.start() < IR.VirtRegI->end) {
131 assert(overlap(*IR.VirtRegI, IR.LiveUnionI) &&
Andrew Trick18c57a82010-11-30 23:18:47 +0000132 "upperBound postcondition");
Andrew Trick14e8d712010-10-22 23:09:15 +0000133 break;
134 }
135 }
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000136 if (!IR.LiveUnionI.valid())
Andrew Trick18c57a82010-11-30 23:18:47 +0000137 IR.VirtRegI = VirtRegEnd;
Andrew Trick14e8d712010-10-22 23:09:15 +0000138}
139
140// Find the first intersection, and cache interference info
Andrew Trick18c57a82010-11-30 23:18:47 +0000141// (retain segment iterators into both VirtReg and LiveUnion).
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000142const LiveIntervalUnion::InterferenceResult &
Andrew Trick14e8d712010-10-22 23:09:15 +0000143LiveIntervalUnion::Query::firstInterference() {
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000144 if (CheckedFirstInterference)
Andrew Trick18c57a82010-11-30 23:18:47 +0000145 return FirstInterference;
Jakob Stoklund Olesena35cce12010-12-09 01:06:52 +0000146 CheckedFirstInterference = true;
147 InterferenceResult &IR = FirstInterference;
148
149 // Quickly skip interference check for empty sets.
150 if (VirtReg->empty() || LiveUnion->empty()) {
151 IR.VirtRegI = VirtReg->end();
152 } else if (VirtReg->beginIndex() < LiveUnion->startIndex()) {
153 // VirtReg starts first, perform double binary search.
154 IR.VirtRegI = VirtReg->find(LiveUnion->startIndex());
155 if (IR.VirtRegI != VirtReg->end())
156 IR.LiveUnionI = LiveUnion->find(IR.VirtRegI->start);
157 } else {
158 // LiveUnion starts first, perform double binary search.
159 IR.LiveUnionI = LiveUnion->find(VirtReg->beginIndex());
160 if (IR.LiveUnionI.valid())
161 IR.VirtRegI = VirtReg->find(IR.LiveUnionI.start());
162 else
163 IR.VirtRegI = VirtReg->end();
Andrew Trick14e8d712010-10-22 23:09:15 +0000164 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000165 findIntersection(FirstInterference);
Jakob Stoklund Olesena0382c62010-12-09 21:20:44 +0000166 assert((IR.VirtRegI == VirtReg->end() || IR.LiveUnionI.valid())
167 && "Uninitialized iterator");
Andrew Trick18c57a82010-11-30 23:18:47 +0000168 return FirstInterference;
Andrew Trick14e8d712010-10-22 23:09:15 +0000169}
170
171// Treat the result as an iterator and advance to the next interfering pair
172// of segments. This is a plain iterator with no filter.
Andrew Trick18c57a82010-11-30 23:18:47 +0000173bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &IR) const {
174 assert(isInterference(IR) && "iteration past end of interferences");
175
176 // Advance either the VirtReg or LiveUnion segment to ensure that we visit all
177 // unique overlapping pairs.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000178 if (IR.VirtRegI->end < IR.LiveUnionI.stop()) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000179 if (++IR.VirtRegI == VirtReg->end())
Andrew Trick14e8d712010-10-22 23:09:15 +0000180 return false;
181 }
182 else {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000183 if (!(++IR.LiveUnionI).valid()) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000184 IR.VirtRegI = VirtReg->end();
Andrew Trick14e8d712010-10-22 23:09:15 +0000185 return false;
186 }
187 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000188 // Short-circuit findIntersection() if possible.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000189 if (overlap(*IR.VirtRegI, IR.LiveUnionI))
Andrew Trick14e8d712010-10-22 23:09:15 +0000190 return true;
Andrew Trick18c57a82010-11-30 23:18:47 +0000191
192 // Find the next intersection.
193 findIntersection(IR);
194 return isInterference(IR);
Andrew Trick14e8d712010-10-22 23:09:15 +0000195}
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000196
Andrew Trick18c57a82010-11-30 23:18:47 +0000197// Scan the vector of interfering virtual registers in this union. Assume it's
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000198// quite small.
Andrew Trick18c57a82010-11-30 23:18:47 +0000199bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *VirtReg) const {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000200 SmallVectorImpl<LiveInterval*>::const_iterator I =
Andrew Trick18c57a82010-11-30 23:18:47 +0000201 std::find(InterferingVRegs.begin(), InterferingVRegs.end(), VirtReg);
202 return I != InterferingVRegs.end();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000203}
204
205// Count the number of virtual registers in this union that interfere with this
Andrew Trick18c57a82010-11-30 23:18:47 +0000206// query's live virtual register.
207//
208// The number of times that we either advance IR.VirtRegI or call
209// LiveUnion.upperBound() will be no more than the number of holes in
210// VirtReg. So each invocation of collectInterferingVRegs() takes
211// time proportional to |VirtReg Holes| * time(LiveUnion.upperBound()).
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000212//
213// For comments on how to speed it up, see Query::findIntersection().
214unsigned LiveIntervalUnion::Query::
Andrew Trick18c57a82010-11-30 23:18:47 +0000215collectInterferingVRegs(unsigned MaxInterferingRegs) {
216 InterferenceResult IR = firstInterference();
217 LiveInterval::iterator VirtRegEnd = VirtReg->end();
Andrew Trick18c57a82010-11-30 23:18:47 +0000218 LiveInterval *RecentInterferingVReg = NULL;
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000219 while (IR.LiveUnionI.valid()) {
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000220 // Advance the union's iterator to reach an unseen interfering vreg.
221 do {
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000222 if (IR.LiveUnionI.value() == RecentInterferingVReg)
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000223 continue;
224
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000225 if (!isSeenInterference(IR.LiveUnionI.value()))
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000226 break;
227
228 // Cache the most recent interfering vreg to bypass isSeenInterference.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000229 RecentInterferingVReg = IR.LiveUnionI.value();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000230
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000231 } while ((++IR.LiveUnionI).valid());
232 if (!IR.LiveUnionI.valid())
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000233 break;
234
Andrew Trick18c57a82010-11-30 23:18:47 +0000235 // Advance the VirtReg iterator until surpassing the next segment in
236 // LiveUnion.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000237 IR.VirtRegI = VirtReg->advanceTo(IR.VirtRegI, IR.LiveUnionI.start());
Andrew Trick18c57a82010-11-30 23:18:47 +0000238 if (IR.VirtRegI == VirtRegEnd)
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000239 break;
240
241 // Check for intersection with the union's segment.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000242 if (overlap(*IR.VirtRegI, IR.LiveUnionI)) {
Andrew Trick18c57a82010-11-30 23:18:47 +0000243
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000244 if (!IR.LiveUnionI.value()->isSpillable())
Andrew Trick18c57a82010-11-30 23:18:47 +0000245 SeenUnspillableVReg = true;
246
Andrew Trick18c57a82010-11-30 23:18:47 +0000247 if (InterferingVRegs.size() == MaxInterferingRegs)
Andrew Trickb853e6c2010-12-09 18:15:21 +0000248 // Leave SeenAllInterferences set to false to indicate that at least one
249 // interference exists beyond those we collected.
Andrew Trick18c57a82010-11-30 23:18:47 +0000250 return MaxInterferingRegs;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000251
Andrew Trickb853e6c2010-12-09 18:15:21 +0000252 InterferingVRegs.push_back(IR.LiveUnionI.value());
253
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000254 // Cache the most recent interfering vreg to bypass isSeenInterference.
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000255 RecentInterferingVReg = IR.LiveUnionI.value();
Andrew Trick18c57a82010-11-30 23:18:47 +0000256 ++IR.LiveUnionI;
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000257 continue;
258 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000259 // VirtRegI may have advanced far beyond LiveUnionI,
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000260 // do a fast intersection test to "catch up"
Jakob Stoklund Olesen953af2c2010-12-07 23:18:47 +0000261 IR.LiveUnionI.advanceTo(IR.VirtRegI->start);
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000262 }
Andrew Trick18c57a82010-11-30 23:18:47 +0000263 SeenAllInterferences = true;
264 return InterferingVRegs.size();
Andrew Trickf4baeaf2010-11-10 19:18:47 +0000265}