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/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
23 // Find the first segment in the range [segBegin,segments_.end()) that
24 // intersects with seg. If no intersection is found, return the first segI
25 // such that segI.start >= seg.end
27 // This logic is tied to the underlying LiveSegments data structure. For now, we
28 // use set::upper_bound to find the nearest starting position,
29 // then reverse iterate to find the first overlap.
31 // Upon entry we have segBegin.start < seg.end
36 // After set::upper_bound, we have segI.start >= seg.start:
41 // Assuming intervals are disjoint, if an intersection exists, it must be the
42 // segment found or the one immediately preceeding it. We continue reverse
43 // iterating to return the first overlapping segment.
44 LiveIntervalUnion::SegmentIter
45 LiveIntervalUnion::upperBound(SegmentIter segBegin,
46 const LiveSegment &seg) {
47 assert(seg.end > segBegin->start && "segment iterator precondition");
48 // get the next LIU segment such that segI->start is not less than seg.start
50 // FIXME: Once we have a B+tree, we can make good use of segBegin as a hint to
51 // upper_bound. For now, we're forced to search again from the root each time.
52 SegmentIter segI = segments_.upper_bound(seg);
53 while (segI != segBegin) {
55 if (seg.start >= segI->end)
61 // Merge a LiveInterval's segments. Guarantee no overlaps.
63 // Consider coalescing adjacent segments to save space, even though it makes
64 // extraction more complicated.
65 void LiveIntervalUnion::unify(LiveInterval &lvr) {
66 // Insert each of the virtual register's live segments into the map
67 SegmentIter segPos = segments_.begin();
68 for (LiveInterval::iterator lvrI = lvr.begin(), lvrEnd = lvr.end();
69 lvrI != lvrEnd; ++lvrI ) {
70 LiveSegment segment(lvrI->start, lvrI->end, &lvr);
71 segPos = segments_.insert(segPos, segment);
72 assert(*segPos == segment && "need equal val for equal key");
74 // check for overlap (inductively)
75 if (segPos != segments_.begin()) {
76 SegmentIter prevPos = segPos;
78 assert(prevPos->end <= segment.start && "overlapping segments" );
80 SegmentIter nextPos = segPos;
82 if (nextPos != segments_.end())
83 assert(segment.end <= nextPos->start && "overlapping segments" );
88 // Remove a live virtual register's segments from this union.
89 void LiveIntervalUnion::extract(const LiveInterval &lvr) {
90 // Remove each of the virtual register's live segments from the map.
91 SegmentIter segPos = segments_.begin();
92 for (LiveInterval::const_iterator lvrI = lvr.begin(), lvrEnd = lvr.end();
93 lvrI != lvrEnd; ++lvrI) {
94 LiveSegment seg(lvrI->start, lvrI->end, const_cast<LiveInterval*>(&lvr));
95 segPos = upperBound(segPos, seg);
96 assert(segPos != segments_.end() && "missing lvr segment");
97 segments_.erase(segPos++);
101 // Private interface accessed by Query.
103 // Find a pair of segments that intersect, one in the live virtual register
104 // (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query)
105 // is responsible for advancing the LiveIntervalUnion segments to find a
106 // "notable" intersection, which requires query-specific logic.
108 // This design assumes only a fast mechanism for intersecting a single live
109 // virtual register segment with a set of LiveIntervalUnion segments. This may
110 // be ok since most LVRs have very few segments. If we had a data
111 // structure that optimizd MxN intersection of segments, then we would bypass
112 // the loop that advances within the LiveInterval.
114 // If no intersection exists, set lvrI = lvrEnd, and set segI to the first
115 // segment whose start point is greater than LiveInterval's end point.
117 // Assumes that segments are sorted by start position in both
118 // LiveInterval and LiveSegments.
119 void LiveIntervalUnion::Query::findIntersection(InterferenceResult &ir) const {
120 LiveInterval::iterator lvrEnd = lvr_->end();
121 SegmentIter liuEnd = liu_->end();
122 while (ir.liuSegI_ != liuEnd) {
123 // Slowly advance the live virtual reg iterator until we surpass the next
124 // segment in this union. If this is ever used for coalescing of fixed
125 // registers and we have a LiveInterval with thousands of segments, then use
126 // upper bound instead.
127 while (ir.lvrSegI_ != lvrEnd && ir.lvrSegI_->end <= ir.liuSegI_->start)
129 if (ir.lvrSegI_ == lvrEnd)
131 // lvrSegI_ may have advanced far beyond liuSegI_,
132 // do a fast intersection test to "catch up"
133 LiveSegment seg(ir.lvrSegI_->start, ir.lvrSegI_->end, lvr_);
134 ir.liuSegI_ = liu_->upperBound(ir.liuSegI_, seg);
135 // Check if no liuSegI_ exists with lvrSegI_->start < liuSegI_.end
136 if (ir.liuSegI_ == liuEnd)
138 if (ir.liuSegI_->start < ir.lvrSegI_->end) {
139 assert(overlap(*ir.lvrSegI_, *ir.liuSegI_) && "upperBound postcondition");
143 if (ir.liuSegI_ == liuEnd)
144 ir.lvrSegI_ = lvrEnd;
147 // Find the first intersection, and cache interference info
148 // (retain segment iterators into both lvr_ and liu_).
149 LiveIntervalUnion::InterferenceResult
150 LiveIntervalUnion::Query::firstInterference() {
151 if (firstInterference_ != LiveIntervalUnion::InterferenceResult()) {
152 return firstInterference_;
154 firstInterference_ = InterferenceResult(lvr_->begin(), liu_->begin());
155 findIntersection(firstInterference_);
156 return firstInterference_;
159 // Treat the result as an iterator and advance to the next interfering pair
160 // of segments. This is a plain iterator with no filter.
161 bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &ir) const {
162 assert(isInterference(ir) && "iteration past end of interferences");
163 // Advance either the lvr or liu segment to ensure that we visit all unique
164 // overlapping pairs.
165 if (ir.lvrSegI_->end < ir.liuSegI_->end) {
166 if (++ir.lvrSegI_ == lvr_->end())
170 if (++ir.liuSegI_ == liu_->end()) {
171 ir.lvrSegI_ = lvr_->end();
175 if (overlap(*ir.lvrSegI_, *ir.liuSegI_))
177 // find the next intersection
178 findIntersection(ir);
179 return isInterference(ir);