1 //===-- LiveIntervalUnion.cpp - Live interval union data structure --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #define DEBUG_TYPE "regalloc"
17 #include "LiveIntervalUnion.h"
18 #include "llvm/ADT/SparseBitVector.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
24 // 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
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.
32 // Upon entry we have segBegin.start < seg.end
37 // After set::upper_bound, we have segI.start >= seg.start:
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.
45 LiveIntervalUnion::SegmentIter
46 LiveIntervalUnion::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
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) {
56 if (seg.start >= segI->end)
62 // Merge a LiveInterval's segments. Guarantee no overlaps.
64 // Consider coalescing adjacent segments to save space, even though it makes
65 // extraction more complicated.
66 void LiveIntervalUnion::unify(LiveInterval &lvr) {
67 // 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 ) {
71 LiveSegment segment(lvrI->start, lvrI->end, &lvr);
72 segPos = segments_.insert(segPos, segment);
73 assert(*segPos == segment && "need equal val for equal key");
75 // check for overlap (inductively)
76 if (segPos != segments_.begin()) {
77 assert(llvm::prior(segPos)->end <= segment.start &&
78 "overlapping segments" );
80 SegmentIter nextPos = llvm::next(segPos);
81 if (nextPos != segments_.end())
82 assert(segment.end <= nextPos->start && "overlapping segments" );
87 // Remove a live virtual register's segments from this union.
88 void LiveIntervalUnion::extract(const LiveInterval &lvr) {
89 // Remove each of the virtual register's live segments from the map.
90 SegmentIter segPos = segments_.begin();
91 for (LiveInterval::const_iterator lvrI = lvr.begin(), lvrEnd = lvr.end();
92 lvrI != lvrEnd; ++lvrI) {
93 LiveSegment seg(lvrI->start, lvrI->end, const_cast<LiveInterval*>(&lvr));
94 segPos = upperBound(segPos, seg);
95 assert(segPos != segments_.end() && "missing lvr segment");
96 segments_.erase(segPos++);
100 raw_ostream& llvm::operator<<(raw_ostream& os, const LiveSegment &ls) {
101 return os << '[' << ls.start << ',' << ls.end << ':' <<
102 ls.liveVirtReg->reg << ")";
105 void LiveSegment::dump() const {
106 dbgs() << *this << "\n";
110 LiveIntervalUnion::print(raw_ostream &os,
111 const AbstractRegisterDescription *rdesc) const {
114 os << rdesc->getName(repReg_);
118 for (LiveSegments::const_iterator segI = segments_.begin(),
119 segEnd = segments_.end(); segI != segEnd; ++segI) {
120 dbgs() << " " << *segI;
125 void LiveIntervalUnion::dump(const AbstractRegisterDescription *rdesc) const {
126 print(dbgs(), rdesc);
130 // Verify the live intervals in this union and add them to the visited set.
131 void LiveIntervalUnion::verify(LvrBitSet& visitedVRegs) {
132 SegmentIter segI = segments_.begin();
133 SegmentIter segEnd = segments_.end();
134 if (segI == segEnd) return;
135 visitedVRegs.set(segI->liveVirtReg->reg);
136 for (++segI; segI != segEnd; ++segI) {
137 visitedVRegs.set(segI->liveVirtReg->reg);
138 assert(llvm::prior(segI)->end <= segI->start && "overlapping segments" );
143 // Private interface accessed by Query.
145 // Find a pair of segments that intersect, one in the live virtual register
146 // (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query)
147 // is responsible for advancing the LiveIntervalUnion segments to find a
148 // "notable" intersection, which requires query-specific logic.
150 // This design assumes only a fast mechanism for intersecting a single live
151 // virtual register segment with a set of LiveIntervalUnion segments. This may
152 // be ok since most LVRs have very few segments. If we had a data
153 // structure that optimizd MxN intersection of segments, then we would bypass
154 // the loop that advances within the LiveInterval.
156 // If no intersection exists, set lvrI = lvrEnd, and set segI to the first
157 // segment whose start point is greater than LiveInterval's end point.
159 // Assumes that segments are sorted by start position in both
160 // LiveInterval and LiveSegments.
161 void LiveIntervalUnion::Query::findIntersection(InterferenceResult &ir) const {
162 LiveInterval::iterator lvrEnd = lvr_->end();
163 SegmentIter liuEnd = liu_->end();
164 while (ir.liuSegI_ != liuEnd) {
165 // Slowly advance the live virtual reg iterator until we surpass the next
166 // segment in this union. If this is ever used for coalescing of fixed
167 // registers and we have a live vreg with thousands of segments, then use
168 // upper bound instead.
169 while (ir.lvrSegI_ != lvrEnd && ir.lvrSegI_->end <= ir.liuSegI_->start)
171 if (ir.lvrSegI_ == lvrEnd)
173 // lvrSegI_ may have advanced far beyond liuSegI_,
174 // do a fast intersection test to "catch up"
175 LiveSegment seg(ir.lvrSegI_->start, ir.lvrSegI_->end, lvr_);
176 ir.liuSegI_ = liu_->upperBound(ir.liuSegI_, seg);
177 // Check if no liuSegI_ exists with lvrSegI_->start < liuSegI_.end
178 if (ir.liuSegI_ == liuEnd)
180 if (ir.liuSegI_->start < ir.lvrSegI_->end) {
181 assert(overlap(*ir.lvrSegI_, *ir.liuSegI_) && "upperBound postcondition");
185 if (ir.liuSegI_ == liuEnd)
186 ir.lvrSegI_ = lvrEnd;
189 // Find the first intersection, and cache interference info
190 // (retain segment iterators into both lvr_ and liu_).
191 LiveIntervalUnion::InterferenceResult
192 LiveIntervalUnion::Query::firstInterference() {
193 if (firstInterference_ != LiveIntervalUnion::InterferenceResult()) {
194 return firstInterference_;
196 firstInterference_ = InterferenceResult(lvr_->begin(), liu_->begin());
197 findIntersection(firstInterference_);
198 return firstInterference_;
201 // Treat the result as an iterator and advance to the next interfering pair
202 // of segments. This is a plain iterator with no filter.
203 bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &ir) const {
204 assert(isInterference(ir) && "iteration past end of interferences");
205 // Advance either the lvr or liu segment to ensure that we visit all unique
206 // overlapping pairs.
207 if (ir.lvrSegI_->end < ir.liuSegI_->end) {
208 if (++ir.lvrSegI_ == lvr_->end())
212 if (++ir.liuSegI_ == liu_->end()) {
213 ir.lvrSegI_ = lvr_->end();
217 if (overlap(*ir.lvrSegI_, *ir.liuSegI_))
219 // find the next intersection
220 findIntersection(ir);
221 return isInterference(ir);
224 // Scan the vector of interfering virtual registers in this union. Assuming it's
226 bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *lvr) const {
227 SmallVectorImpl<LiveInterval*>::const_iterator I =
228 std::find(interferingVRegs_.begin(), interferingVRegs_.end(), lvr);
229 return I != interferingVRegs_.end();
232 // Count the number of virtual registers in this union that interfere with this
233 // query's live virtual register.
235 // The number of times that we either advance ir.lvrSegI_ or call
236 // liu_.upperBound() will be no more than the number of holes in
237 // lvr_. So each invocation of collectInterferingVirtReg() takes
238 // time proportional to |lvr-holes| * time(liu_.upperBound()).
240 // For comments on how to speed it up, see Query::findIntersection().
241 unsigned LiveIntervalUnion::Query::
242 collectInterferingVRegs(unsigned maxInterferingRegs) {
243 InterferenceResult ir = firstInterference();
244 LiveInterval::iterator lvrEnd = lvr_->end();
245 SegmentIter liuEnd = liu_->end();
246 LiveInterval *recentInterferingVReg = NULL;
247 while (ir.liuSegI_ != liuEnd) {
248 // Advance the union's iterator to reach an unseen interfering vreg.
250 if (ir.liuSegI_->liveVirtReg == recentInterferingVReg)
253 if (!isSeenInterference(ir.liuSegI_->liveVirtReg))
256 // Cache the most recent interfering vreg to bypass isSeenInterference.
257 recentInterferingVReg = ir.liuSegI_->liveVirtReg;
259 } while( ++ir.liuSegI_ != liuEnd);
260 if (ir.liuSegI_ == liuEnd)
263 // Advance the live vreg reg iterator until surpassing the next
264 // segment in this union. If this is ever used for coalescing of fixed
265 // registers and we have a live vreg with thousands of segments, then use
266 // upper bound instead.
267 while (ir.lvrSegI_ != lvrEnd && ir.lvrSegI_->end <= ir.liuSegI_->start)
269 if (ir.lvrSegI_ == lvrEnd)
272 // Check for intersection with the union's segment.
273 if (overlap(*ir.lvrSegI_, *ir.liuSegI_)) {
274 if (!ir.liuSegI_->liveVirtReg->isSpillable())
275 seenUnspillableVReg_ = true;
277 interferingVRegs_.push_back(ir.liuSegI_->liveVirtReg);
278 if (interferingVRegs_.size() == maxInterferingRegs)
279 return maxInterferingRegs;
281 // Cache the most recent interfering vreg to bypass isSeenInterference.
282 recentInterferingVReg = ir.liuSegI_->liveVirtReg;
286 // lvrSegI_ may have advanced far beyond liuSegI_,
287 // do a fast intersection test to "catch up"
288 LiveSegment seg(ir.lvrSegI_->start, ir.lvrSegI_->end, lvr_);
289 ir.liuSegI_ = liu_->upperBound(ir.liuSegI_, seg);
291 return interferingVRegs_.size();