Fixed some bugs.
[llvm/zpu.git] / lib / CodeGen / LiveIntervalUnion.cpp
blob9a47b3569b49e907eed5dbd4e4d85c45defdd660
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.
14 //===----------------------------------------------------------------------===//
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;
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 // Insert each of the virtual register's live segments into the map
29 SegmentIter segPos = segments_.begin();
30 for (LiveInterval::iterator lvrI = lvr.begin(), lvrEnd = lvr.end();
31 lvrI != lvrEnd; ++lvrI ) {
32 LiveSegment segment(lvrI->start, lvrI->end, lvr);
33 segPos = segments_.insert(segPos, segment);
34 assert(*segPos == segment && "need equal val for equal key");
35 #ifndef NDEBUG
36 // check for overlap (inductively)
37 if (segPos != segments_.begin()) {
38 SegmentIter prevPos = segPos;
39 --prevPos;
40 assert(prevPos->end <= segment.start && "overlapping segments" );
42 SegmentIter nextPos = segPos;
43 ++nextPos;
44 if (nextPos != segments_.end())
45 assert(segment.end <= nextPos->start && "overlapping segments" );
46 #endif // NDEBUG
50 // Low-level helper to find the first segment in the range [segI,segEnd) that
51 // intersects with a live virtual register segment, or segI.start >= lvr.end
53 // This logic is tied to the underlying LiveSegments data structure. For now, we
54 // use a binary search within the vector to find the nearest starting position,
55 // then reverse iterate to find the first overlap.
57 // Upon entry we have segI.start < lvrSeg.end
58 // seg |--...
59 // \ .
60 // lvr ...-|
61 //
62 // After binary search, we have segI.start >= lvrSeg.start:
63 // seg |--...
64 // /
65 // lvr |--...
67 // Assuming intervals are disjoint, if an intersection exists, it must be the
68 // segment found or immediately behind it. We continue reverse iterating to
69 // return the first overlap.
70 typedef LiveIntervalUnion::SegmentIter SegmentIter;
71 static SegmentIter upperBound(SegmentIter segBegin,
72 SegmentIter segEnd,
73 const LiveRange &lvrSeg) {
74 assert(lvrSeg.end > segBegin->start && "segment iterator precondition");
75 // get the next LIU segment such that setg.start is not less than
76 // lvrSeg.start
77 SegmentIter segI = std::upper_bound(segBegin, segEnd, lvrSeg.start);
78 while (segI != segBegin) {
79 --segI;
80 if (lvrSeg.start >= segI->end)
81 return ++segI;
83 return segI;
86 // Private interface accessed by Query.
88 // Find a pair of segments that intersect, one in the live virtual register
89 // (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query)
90 // is responsible for advancing the LiveIntervalUnion segments to find a
91 // "notable" intersection, which requires query-specific logic.
92 //
93 // This design assumes only a fast mechanism for intersecting a single live
94 // virtual register segment with a set of LiveIntervalUnion segments. This may
95 // be ok since most LVRs have very few segments. If we had a data
96 // structure that optimizd MxN intersection of segments, then we would bypass
97 // the loop that advances within the LiveInterval.
99 // If no intersection exists, set lvrI = lvrEnd, and set segI to the first
100 // segment whose start point is greater than LiveInterval's end point.
102 // Assumes that segments are sorted by start position in both
103 // LiveInterval and LiveSegments.
104 void LiveIntervalUnion::Query::findIntersection(InterferenceResult &ir) const {
105 LiveInterval::iterator lvrEnd = lvr_.end();
106 SegmentIter liuEnd = liu_.end();
107 while (ir.liuSegI_ != liuEnd) {
108 // Slowly advance the live virtual reg iterator until we surpass the next
109 // segment in this union. If this is ever used for coalescing of fixed
110 // registers and we have a LiveInterval with thousands of segments, then use
111 // upper bound instead.
112 while (ir.lvrSegI_ != lvrEnd && ir.lvrSegI_->end <= ir.liuSegI_->start)
113 ++ir.lvrSegI_;
114 if (ir.lvrSegI_ == lvrEnd)
115 break;
116 // lvrSegI_ may have advanced far beyond liuSegI_,
117 // do a fast intersection test to "catch up"
118 ir.liuSegI_ = upperBound(ir.liuSegI_, liuEnd, *ir.lvrSegI_);
119 // Check if no liuSegI_ exists with lvrSegI_->start < liuSegI_.end
120 if (ir.liuSegI_ == liuEnd)
121 break;
122 if (ir.liuSegI_->start < ir.lvrSegI_->end) {
123 assert(overlap(*ir.lvrSegI_, *ir.liuSegI_) && "upperBound postcondition");
124 break;
127 if (ir.liuSegI_ == liuEnd)
128 ir.lvrSegI_ = lvrEnd;
131 // Find the first intersection, and cache interference info
132 // (retain segment iterators into both lvr_ and liu_).
133 LiveIntervalUnion::InterferenceResult
134 LiveIntervalUnion::Query::firstInterference() {
135 if (firstInterference_ != LiveIntervalUnion::InterferenceResult()) {
136 return firstInterference_;
138 firstInterference_ = InterferenceResult(lvr_.begin(), liu_.begin());
139 findIntersection(firstInterference_);
140 return firstInterference_;
143 // Treat the result as an iterator and advance to the next interfering pair
144 // of segments. This is a plain iterator with no filter.
145 bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &ir) const {
146 assert(isInterference(ir) && "iteration past end of interferences");
147 // Advance either the lvr or liu segment to ensure that we visit all unique
148 // overlapping pairs.
149 if (ir.lvrSegI_->end < ir.liuSegI_->end) {
150 if (++ir.lvrSegI_ == lvr_.end())
151 return false;
153 else {
154 if (++ir.liuSegI_ == liu_.end()) {
155 ir.lvrSegI_ = lvr_.end();
156 return false;
159 if (overlap(*ir.lvrSegI_, *ir.liuSegI_))
160 return true;
161 // find the next intersection
162 findIntersection(ir);
163 return isInterference(ir);