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 // Merge a LiveInterval's segments. Guarantee no overlaps.
25 // Consider coalescing adjacent segments to save space, even though it makes
26 // extraction more complicated.
27 void LiveIntervalUnion::unify(LiveInterval &lvr) {
28 // Add this live virtual register to the union
29 LiveVirtRegs::iterator pos = std::upper_bound(lvrs_.begin(), lvrs_.end(),
30 &lvr, less_ptr<LiveInterval>());
31 assert((pos == lvrs_.end() || *pos != &lvr) && "duplicate LVR insertion");
32 lvrs_.insert(pos, &lvr);
33 // Insert each of the virtual register's live segments into the map
34 SegmentIter segPos = segments_.begin();
35 for (LiveInterval::iterator lvrI = lvr.begin(), lvrEnd = lvr.end();
36 lvrI != lvrEnd; ++lvrI ) {
37 LiveSegment segment(lvrI->start, lvrI->end, lvr);
38 segPos = segments_.insert(segPos, segment);
39 assert(*segPos == segment && "need equal val for equal key");
41 // check for overlap (inductively)
42 if (segPos != segments_.begin()) {
43 SegmentIter prevPos = segPos;
45 assert(prevPos->end <= segment.start && "overlapping segments" );
47 SegmentIter nextPos = segPos;
49 if (nextPos != segments_.end())
50 assert(segment.end <= nextPos->start && "overlapping segments" );
55 // Low-level helper to find the first segment in the range [segI,segEnd) that
56 // intersects with a live virtual register segment, or segI.start >= lvr.end
58 // This logic is tied to the underlying LiveSegments data structure. For now, we
59 // use a binary search within the vector to find the nearest starting position,
60 // then reverse iterate to find the first overlap.
62 // Upon entry we have segI.start < lvrSeg.end
67 // After binary search, we have segI.start >= lvrSeg.start:
72 // Assuming intervals are disjoint, if an intersection exists, it must be the
73 // segment found or immediately behind it. We continue reverse iterating to
74 // return the first overlap.
75 typedef LiveIntervalUnion::SegmentIter SegmentIter;
76 static SegmentIter upperBound(SegmentIter segBegin,
78 const LiveRange &lvrSeg) {
79 assert(lvrSeg.end > segBegin->start && "segment iterator precondition");
80 // get the next LIU segment such that setg.start is not less than
82 SegmentIter segI = std::upper_bound(segBegin, segEnd, lvrSeg.start);
83 while (segI != segBegin) {
85 if (lvrSeg.start >= segI->end)
91 // Private interface accessed by Query.
93 // Find a pair of segments that intersect, one in the live virtual register
94 // (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query)
95 // is responsible for advancing the LiveIntervalUnion segments to find a
96 // "notable" intersection, which requires query-specific logic.
98 // This design assumes only a fast mechanism for intersecting a single live
99 // virtual register segment with a set of LiveIntervalUnion segments. This may
100 // be ok since most LVRs have very few segments. If we had a data
101 // structure that optimizd MxN intersection of segments, then we would bypass
102 // the loop that advances within the LiveInterval.
104 // If no intersection exists, set lvrI = lvrEnd, and set segI to the first
105 // segment whose start point is greater than LiveInterval's end point.
107 // Assumes that segments are sorted by start position in both
108 // LiveInterval and LiveSegments.
109 void LiveIntervalUnion::Query::findIntersection(InterferenceResult &ir) const {
110 LiveInterval::iterator lvrEnd = lvr_.end();
111 SegmentIter liuEnd = liu_.end();
112 while (ir.liuSegI_ != liuEnd) {
113 // Slowly advance the live virtual reg iterator until we surpass the next
114 // segment in this union. If this is ever used for coalescing of fixed
115 // registers and we have a LiveInterval with thousands of segments, then use
116 // upper bound instead.
117 while (ir.lvrSegI_ != lvrEnd && ir.lvrSegI_->end <= ir.liuSegI_->start)
119 if (ir.lvrSegI_ == lvrEnd)
121 // lvrSegI_ may have advanced far beyond liuSegI_,
122 // do a fast intersection test to "catch up"
123 ir.liuSegI_ = upperBound(ir.liuSegI_, liuEnd, *ir.lvrSegI_);
124 // Check if no liuSegI_ exists with lvrSegI_->start < liuSegI_.end
125 if (ir.liuSegI_ == liuEnd)
127 if (ir.liuSegI_->start < ir.lvrSegI_->end) {
128 assert(overlap(*ir.lvrSegI_, *ir.liuSegI_) && "upperBound postcondition");
132 if (ir.liuSegI_ == liuEnd)
133 ir.lvrSegI_ = lvrEnd;
136 // Find the first intersection, and cache interference info
137 // (retain segment iterators into both lvr_ and liu_).
138 LiveIntervalUnion::InterferenceResult
139 LiveIntervalUnion::Query::firstInterference() {
140 if (firstInterference_ != LiveIntervalUnion::InterferenceResult()) {
141 return firstInterference_;
143 firstInterference_ = InterferenceResult(lvr_.begin(), liu_.begin());
144 findIntersection(firstInterference_);
145 return firstInterference_;
148 // Treat the result as an iterator and advance to the next interfering pair
149 // of segments. This is a plain iterator with no filter.
150 bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &ir) const {
151 assert(isInterference(ir) && "iteration past end of interferences");
152 // Advance either the lvr or liu segment to ensure that we visit all unique
153 // overlapping pairs.
154 if (ir.lvrSegI_->end < ir.liuSegI_->end) {
155 if (++ir.lvrSegI_ == lvr_.end())
159 if (++ir.liuSegI_ == liu_.end()) {
160 ir.lvrSegI_ = lvr_.end();
164 if (overlap(*ir.lvrSegI_, *ir.liuSegI_))
166 // find the next intersection
167 findIntersection(ir);
168 return isInterference(ir);