Adds RABasic verification and tracing.
[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/ADT/SparseBitVector.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <algorithm>
22 using namespace llvm;
23
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
27 //
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.
31 //
32 // Upon entry we have segBegin.start < seg.end
33 // seg   |--...
34 //        \   .
35 // lvr ...-|
36 // 
37 // After set::upper_bound, we have segI.start >= seg.start:
38 // seg   |--...
39 //      /
40 // lvr |--...
41 //
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
50   // 
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) {
55     --segI;
56     if (seg.start >= segI->end)
57       return ++segI;
58   }
59   return segI;
60 }
61
62 // Merge a LiveInterval's segments. Guarantee no overlaps.
63 //
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");
74 #ifndef NDEBUG
75     // check for overlap (inductively)
76     if (segPos != segments_.begin()) {
77       assert(prior(segPos)->end <= segment.start && "overlapping segments" );
78     }
79     SegmentIter nextPos = next(segPos);
80     if (nextPos != segments_.end())
81       assert(segment.end <= nextPos->start && "overlapping segments" );
82 #endif // NDEBUG
83   }
84 }
85
86 // Remove a live virtual register's segments from this union.
87 void LiveIntervalUnion::extract(const LiveInterval &lvr) {
88   // Remove each of the virtual register's live segments from the map.
89   SegmentIter segPos = segments_.begin();
90   for (LiveInterval::const_iterator lvrI = lvr.begin(), lvrEnd = lvr.end();
91        lvrI != lvrEnd; ++lvrI) {
92     LiveSegment seg(lvrI->start, lvrI->end, const_cast<LiveInterval*>(&lvr));
93     segPos = upperBound(segPos, seg);
94     assert(segPos != segments_.end() && "missing lvr segment");
95     segments_.erase(segPos++);
96   }
97 }
98
99 raw_ostream& llvm::operator<<(raw_ostream& os, const LiveSegment &ls) {
100   return os << '[' << ls.start << ',' << ls.end << ':' <<
101     ls.liveVirtReg->reg << ")";
102 }
103
104 void LiveSegment::dump() const {
105   dbgs() << *this << "\n";
106 }
107
108 void
109 LiveIntervalUnion::print(raw_ostream &os,
110                          const AbstractRegisterDescription *rdesc) const {
111   os << "LIU ";
112   if (rdesc != NULL)
113     os << rdesc->getName(repReg_);
114   else {
115     os << repReg_;
116   }
117   for (SegmentIter segI = segments_.begin(), segEnd = segments_.end();
118        segI != segEnd; ++segI) {
119     dbgs() << " " << *segI;
120   }
121   os << "\n";
122 }
123
124 void LiveIntervalUnion::dump(const AbstractRegisterDescription *rdesc) const {
125   print(dbgs(), rdesc);
126 }
127
128 #ifndef NDEBUG
129 // Verify the live intervals in this union and add them to the visited set.
130 void LiveIntervalUnion::verify(LvrBitSet& visitedVRegs) {
131   SegmentIter segI = segments_.begin();
132   SegmentIter segEnd = segments_.end();
133   if (segI == segEnd) return;
134   visitedVRegs.set(segI->liveVirtReg->reg);
135   for (++segI; segI != segEnd; ++segI) {
136     visitedVRegs.set(segI->liveVirtReg->reg);
137     assert(prior(segI)->end <= segI->start && "overlapping segments" );
138   }
139 }
140 #endif //!NDEBUG
141
142 // Private interface accessed by Query.
143 //
144 // Find a pair of segments that intersect, one in the live virtual register
145 // (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query)
146 // is responsible for advancing the LiveIntervalUnion segments to find a
147 // "notable" intersection, which requires query-specific logic.
148 // 
149 // This design assumes only a fast mechanism for intersecting a single live
150 // virtual register segment with a set of LiveIntervalUnion segments.  This may
151 // be ok since most LVRs have very few segments.  If we had a data
152 // structure that optimizd MxN intersection of segments, then we would bypass
153 // the loop that advances within the LiveInterval.
154 //
155 // If no intersection exists, set lvrI = lvrEnd, and set segI to the first
156 // segment whose start point is greater than LiveInterval's end point.
157 //
158 // Assumes that segments are sorted by start position in both
159 // LiveInterval and LiveSegments.
160 void LiveIntervalUnion::Query::findIntersection(InterferenceResult &ir) const {
161   LiveInterval::iterator lvrEnd = lvr_->end();
162   SegmentIter liuEnd = liu_->end();
163   while (ir.liuSegI_ != liuEnd) {
164     // Slowly advance the live virtual reg iterator until we surpass the next
165     // segment in this union. If this is ever used for coalescing of fixed
166     // registers and we have a LiveInterval with thousands of segments, then use
167     // upper bound instead.
168     while (ir.lvrSegI_ != lvrEnd && ir.lvrSegI_->end <= ir.liuSegI_->start)
169       ++ir.lvrSegI_;
170     if (ir.lvrSegI_ == lvrEnd)
171       break;
172     // lvrSegI_ may have advanced far beyond liuSegI_,
173     // do a fast intersection test to "catch up"
174     LiveSegment seg(ir.lvrSegI_->start, ir.lvrSegI_->end, lvr_);
175     ir.liuSegI_ = liu_->upperBound(ir.liuSegI_, seg);
176     // Check if no liuSegI_ exists with lvrSegI_->start < liuSegI_.end
177     if (ir.liuSegI_ == liuEnd)
178       break;
179     if (ir.liuSegI_->start < ir.lvrSegI_->end) {
180       assert(overlap(*ir.lvrSegI_, *ir.liuSegI_) && "upperBound postcondition");
181       break;
182     }
183   }
184   if (ir.liuSegI_ == liuEnd)
185     ir.lvrSegI_ = lvrEnd;
186 }
187
188 // Find the first intersection, and cache interference info
189 // (retain segment iterators into both lvr_ and liu_).
190 LiveIntervalUnion::InterferenceResult
191 LiveIntervalUnion::Query::firstInterference() {
192   if (firstInterference_ != LiveIntervalUnion::InterferenceResult()) {
193     return firstInterference_;
194   }
195   firstInterference_ = InterferenceResult(lvr_->begin(), liu_->begin());
196   findIntersection(firstInterference_);
197   return firstInterference_;
198 }
199
200 // Treat the result as an iterator and advance to the next interfering pair
201 // of segments. This is a plain iterator with no filter.
202 bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &ir) const {
203   assert(isInterference(ir) && "iteration past end of interferences");
204   // Advance either the lvr or liu segment to ensure that we visit all unique
205   // overlapping pairs.
206   if (ir.lvrSegI_->end < ir.liuSegI_->end) {
207     if (++ir.lvrSegI_ == lvr_->end())
208       return false;
209   }
210   else {
211     if (++ir.liuSegI_ == liu_->end()) {
212       ir.lvrSegI_ = lvr_->end();
213       return false;
214     }
215   }
216   if (overlap(*ir.lvrSegI_, *ir.liuSegI_))
217     return true;
218   // find the next intersection
219   findIntersection(ir);
220   return isInterference(ir);
221 }