Jakob's review of the basic register allocator.
[oota-llvm.git] / lib / CodeGen / LiveIntervalUnion.cpp
1 //===-- 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"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include <algorithm>
21 using namespace llvm;
22
23 // Merge a LiveInterval's segments. Guarantee no overlaps.
24 //
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");
40 #ifndef NDEBUG
41     // check for overlap (inductively)
42     if (segPos != segments_.begin()) {
43       SegmentIter prevPos = segPos;
44       --prevPos;
45       assert(prevPos->end <= segment.start && "overlapping segments" );
46     }
47     SegmentIter nextPos = segPos;
48     ++nextPos;
49     if (nextPos != segments_.end())
50       assert(segment.end <= nextPos->start && "overlapping segments" );
51 #endif // NDEBUG
52   }
53 }
54
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
57 //
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.
61 //
62 // Upon entry we have segI.start < lvrSeg.end
63 // seg   |--...
64 //        \   .
65 // lvr ...-|
66 // 
67 // After binary search, we have segI.start >= lvrSeg.start:
68 // seg   |--...
69 //      /
70 // lvr |--...
71 //
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,
77                        SegmentIter segEnd,
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
81   // lvrSeg.start
82   SegmentIter segI = std::upper_bound(segBegin, segEnd, lvrSeg.start);
83   while (segI != segBegin) {
84     --segI;
85     if (lvrSeg.start >= segI->end)
86       return ++segI;
87   }
88   return segI;
89 }
90
91 // Private interface accessed by Query.
92 //
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.
97 // 
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.
103 //
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.
106 //
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)
118       ++ir.lvrSegI_;
119     if (ir.lvrSegI_ == lvrEnd)
120       break;
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)
126       break;
127     if (ir.liuSegI_->start < ir.lvrSegI_->end) {
128       assert(overlap(*ir.lvrSegI_, *ir.liuSegI_) && "upperBound postcondition");
129       break;
130     }
131   }
132   if (ir.liuSegI_ == liuEnd)
133     ir.lvrSegI_ = lvrEnd;
134 }
135
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_;
142   }
143   firstInterference_ = InterferenceResult(lvr_.begin(), liu_.begin());
144   findIntersection(firstInterference_);
145   return firstInterference_;
146 }
147
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())
156       return false;
157   }
158   else {
159     if (++ir.liuSegI_ == liu_.end()) {
160       ir.lvrSegI_ = lvr_.end();
161       return false;
162     }
163   }
164   if (overlap(*ir.lvrSegI_, *ir.liuSegI_))
165     return true;
166   // find the next intersection
167   findIntersection(ir);
168   return isInterference(ir);
169 }