blob: 9d4de6a7c24a3fdab46b75de0885a4042d64768a [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 Trick39a5ce42010-11-09 19:01:17 +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// Find the first segment in the range [segBegin,segments_.end()) that
25// intersects with seg. If no intersection is found, return the first segI
26// such that segI.start >= seg.end
27//
28// This logic is tied to the underlying LiveSegments data structure. For now, we
29// use set::upper_bound to find the nearest starting position,
30// then reverse iterate to find the first overlap.
31//
32// Upon entry we have segBegin.start < seg.end
33// seg |--...
34// \ .
35// lvr ...-|
36//
37// After set::upper_bound, we have segI.start >= seg.start:
38// seg |--...
39// /
40// lvr |--...
41//
42// Assuming intervals are disjoint, if an intersection exists, it must be the
43// segment found or the one immediately preceeding it. We continue reverse
44// iterating to return the first overlapping segment.
45LiveIntervalUnion::SegmentIter
46LiveIntervalUnion::upperBound(SegmentIter segBegin,
47 const LiveSegment &seg) {
48 assert(seg.end > segBegin->start && "segment iterator precondition");
49 // get the next LIU segment such that segI->start is not less than seg.start
50 //
51 // FIXME: Once we have a B+tree, we can make good use of segBegin as a hint to
52 // upper_bound. For now, we're forced to search again from the root each time.
53 SegmentIter segI = segments_.upper_bound(seg);
54 while (segI != segBegin) {
55 --segI;
56 if (seg.start >= segI->end)
57 return ++segI;
58 }
59 return segI;
60}
61
Andrew Trick14e8d712010-10-22 23:09:15 +000062// Merge a LiveInterval's segments. Guarantee no overlaps.
Andrew Tricke16eecc2010-10-26 18:34:01 +000063//
64// Consider coalescing adjacent segments to save space, even though it makes
65// extraction more complicated.
Andrew Trick14e8d712010-10-22 23:09:15 +000066void LiveIntervalUnion::unify(LiveInterval &lvr) {
Andrew Trick14e8d712010-10-22 23:09:15 +000067 // Insert each of the virtual register's live segments into the map
68 SegmentIter segPos = segments_.begin();
69 for (LiveInterval::iterator lvrI = lvr.begin(), lvrEnd = lvr.end();
70 lvrI != lvrEnd; ++lvrI ) {
Andrew Tricke141a492010-11-08 18:02:08 +000071 LiveSegment segment(lvrI->start, lvrI->end, &lvr);
Andrew Trick14e8d712010-10-22 23:09:15 +000072 segPos = segments_.insert(segPos, segment);
73 assert(*segPos == segment && "need equal val for equal key");
Andrew Tricke16eecc2010-10-26 18:34:01 +000074#ifndef NDEBUG
75 // check for overlap (inductively)
76 if (segPos != segments_.begin()) {
Andrew Trick39a5ce42010-11-09 19:01:17 +000077 assert(prior(segPos)->end <= segment.start && "overlapping segments" );
Andrew Tricke16eecc2010-10-26 18:34:01 +000078 }
Andrew Trick39a5ce42010-11-09 19:01:17 +000079 SegmentIter nextPos = next(segPos);
Andrew Tricke16eecc2010-10-26 18:34:01 +000080 if (nextPos != segments_.end())
81 assert(segment.end <= nextPos->start && "overlapping segments" );
82#endif // NDEBUG
Andrew Trick14e8d712010-10-22 23:09:15 +000083 }
84}
85
Andrew Tricke141a492010-11-08 18:02:08 +000086// Remove a live virtual register's segments from this union.
87void LiveIntervalUnion::extract(const LiveInterval &lvr) {
88 // Remove each of the virtual register's live segments from the map.
89 SegmentIter segPos = segments_.begin();
90 for (LiveInterval::const_iterator lvrI = lvr.begin(), lvrEnd = lvr.end();
91 lvrI != lvrEnd; ++lvrI) {
92 LiveSegment seg(lvrI->start, lvrI->end, const_cast<LiveInterval*>(&lvr));
93 segPos = upperBound(segPos, seg);
94 assert(segPos != segments_.end() && "missing lvr segment");
95 segments_.erase(segPos++);
Andrew Trick14e8d712010-10-22 23:09:15 +000096 }
Andrew Trick14e8d712010-10-22 23:09:15 +000097}
Andrew Trick14e8d712010-10-22 23:09:15 +000098
Andrew Trick39a5ce42010-11-09 19:01:17 +000099raw_ostream& llvm::operator<<(raw_ostream& os, const LiveSegment &ls) {
100 return os << '[' << ls.start << ',' << ls.end << ':' <<
101 ls.liveVirtReg->reg << ")";
102}
103
104void LiveSegment::dump() const {
105 dbgs() << *this << "\n";
106}
107
108void
109LiveIntervalUnion::print(raw_ostream &os,
110 const AbstractRegisterDescription *rdesc) const {
111 os << "LIU ";
112 if (rdesc != NULL)
113 os << rdesc->getName(repReg_);
114 else {
115 os << repReg_;
116 }
117 for (SegmentIter segI = segments_.begin(), segEnd = segments_.end();
118 segI != segEnd; ++segI) {
119 dbgs() << " " << *segI;
120 }
121 os << "\n";
122}
123
124void LiveIntervalUnion::dump(const AbstractRegisterDescription *rdesc) const {
125 print(dbgs(), rdesc);
126}
127
128#ifndef NDEBUG
129// Verify the live intervals in this union and add them to the visited set.
130void LiveIntervalUnion::verify(LvrBitSet& visitedVRegs) {
131 SegmentIter segI = segments_.begin();
132 SegmentIter segEnd = segments_.end();
133 if (segI == segEnd) return;
134 visitedVRegs.set(segI->liveVirtReg->reg);
135 for (++segI; segI != segEnd; ++segI) {
136 visitedVRegs.set(segI->liveVirtReg->reg);
137 assert(prior(segI)->end <= segI->start && "overlapping segments" );
138 }
139}
140#endif //!NDEBUG
141
Andrew Trick14e8d712010-10-22 23:09:15 +0000142// Private interface accessed by Query.
143//
144// Find a pair of segments that intersect, one in the live virtual register
145// (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query)
146// is responsible for advancing the LiveIntervalUnion segments to find a
147// "notable" intersection, which requires query-specific logic.
148//
149// This design assumes only a fast mechanism for intersecting a single live
150// virtual register segment with a set of LiveIntervalUnion segments. This may
151// be ok since most LVRs have very few segments. If we had a data
152// structure that optimizd MxN intersection of segments, then we would bypass
153// the loop that advances within the LiveInterval.
154//
155// If no intersection exists, set lvrI = lvrEnd, and set segI to the first
156// segment whose start point is greater than LiveInterval's end point.
157//
158// Assumes that segments are sorted by start position in both
159// LiveInterval and LiveSegments.
160void LiveIntervalUnion::Query::findIntersection(InterferenceResult &ir) const {
Andrew Tricke141a492010-11-08 18:02:08 +0000161 LiveInterval::iterator lvrEnd = lvr_->end();
162 SegmentIter liuEnd = liu_->end();
Andrew Trick14e8d712010-10-22 23:09:15 +0000163 while (ir.liuSegI_ != liuEnd) {
164 // Slowly advance the live virtual reg iterator until we surpass the next
165 // segment in this union. If this is ever used for coalescing of fixed
166 // registers and we have a LiveInterval with thousands of segments, then use
167 // upper bound instead.
168 while (ir.lvrSegI_ != lvrEnd && ir.lvrSegI_->end <= ir.liuSegI_->start)
169 ++ir.lvrSegI_;
170 if (ir.lvrSegI_ == lvrEnd)
171 break;
172 // lvrSegI_ may have advanced far beyond liuSegI_,
173 // do a fast intersection test to "catch up"
Andrew Tricke141a492010-11-08 18:02:08 +0000174 LiveSegment seg(ir.lvrSegI_->start, ir.lvrSegI_->end, lvr_);
175 ir.liuSegI_ = liu_->upperBound(ir.liuSegI_, seg);
Andrew Trick14e8d712010-10-22 23:09:15 +0000176 // Check if no liuSegI_ exists with lvrSegI_->start < liuSegI_.end
177 if (ir.liuSegI_ == liuEnd)
178 break;
179 if (ir.liuSegI_->start < ir.lvrSegI_->end) {
180 assert(overlap(*ir.lvrSegI_, *ir.liuSegI_) && "upperBound postcondition");
181 break;
182 }
183 }
184 if (ir.liuSegI_ == liuEnd)
185 ir.lvrSegI_ = lvrEnd;
186}
187
188// Find the first intersection, and cache interference info
189// (retain segment iterators into both lvr_ and liu_).
190LiveIntervalUnion::InterferenceResult
191LiveIntervalUnion::Query::firstInterference() {
192 if (firstInterference_ != LiveIntervalUnion::InterferenceResult()) {
193 return firstInterference_;
194 }
Andrew Tricke141a492010-11-08 18:02:08 +0000195 firstInterference_ = InterferenceResult(lvr_->begin(), liu_->begin());
Andrew Trick14e8d712010-10-22 23:09:15 +0000196 findIntersection(firstInterference_);
197 return firstInterference_;
198}
199
200// Treat the result as an iterator and advance to the next interfering pair
201// of segments. This is a plain iterator with no filter.
202bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &ir) const {
203 assert(isInterference(ir) && "iteration past end of interferences");
204 // Advance either the lvr or liu segment to ensure that we visit all unique
205 // overlapping pairs.
206 if (ir.lvrSegI_->end < ir.liuSegI_->end) {
Andrew Tricke141a492010-11-08 18:02:08 +0000207 if (++ir.lvrSegI_ == lvr_->end())
Andrew Trick14e8d712010-10-22 23:09:15 +0000208 return false;
209 }
210 else {
Andrew Tricke141a492010-11-08 18:02:08 +0000211 if (++ir.liuSegI_ == liu_->end()) {
212 ir.lvrSegI_ = lvr_->end();
Andrew Trick14e8d712010-10-22 23:09:15 +0000213 return false;
214 }
215 }
216 if (overlap(*ir.lvrSegI_, *ir.liuSegI_))
217 return true;
218 // find the next intersection
219 findIntersection(ir);
220 return isInterference(ir);
221}