Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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 | |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame^] | 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 |
| 26 | // |
| 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. |
| 30 | // |
| 31 | // Upon entry we have segBegin.start < seg.end |
| 32 | // seg |--... |
| 33 | // \ . |
| 34 | // lvr ...-| |
| 35 | // |
| 36 | // After set::upper_bound, we have segI.start >= seg.start: |
| 37 | // seg |--... |
| 38 | // / |
| 39 | // lvr |--... |
| 40 | // |
| 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 |
| 49 | // |
| 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) { |
| 54 | --segI; |
| 55 | if (seg.start >= segI->end) |
| 56 | return ++segI; |
| 57 | } |
| 58 | return segI; |
| 59 | } |
| 60 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 61 | // Merge a LiveInterval's segments. Guarantee no overlaps. |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 62 | // |
| 63 | // Consider coalescing adjacent segments to save space, even though it makes |
| 64 | // extraction more complicated. |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 65 | void LiveIntervalUnion::unify(LiveInterval &lvr) { |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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 ) { |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame^] | 70 | LiveSegment segment(lvrI->start, lvrI->end, &lvr); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 71 | segPos = segments_.insert(segPos, segment); |
| 72 | assert(*segPos == segment && "need equal val for equal key"); |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 73 | #ifndef NDEBUG |
| 74 | // check for overlap (inductively) |
| 75 | if (segPos != segments_.begin()) { |
| 76 | SegmentIter prevPos = segPos; |
| 77 | --prevPos; |
| 78 | assert(prevPos->end <= segment.start && "overlapping segments" ); |
| 79 | } |
| 80 | SegmentIter nextPos = segPos; |
| 81 | ++nextPos; |
| 82 | if (nextPos != segments_.end()) |
| 83 | assert(segment.end <= nextPos->start && "overlapping segments" ); |
| 84 | #endif // NDEBUG |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame^] | 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++); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 98 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 99 | } |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 100 | |
| 101 | // Private interface accessed by Query. |
| 102 | // |
| 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. |
| 107 | // |
| 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. |
| 113 | // |
| 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. |
| 116 | // |
| 117 | // Assumes that segments are sorted by start position in both |
| 118 | // LiveInterval and LiveSegments. |
| 119 | void LiveIntervalUnion::Query::findIntersection(InterferenceResult &ir) const { |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame^] | 120 | LiveInterval::iterator lvrEnd = lvr_->end(); |
| 121 | SegmentIter liuEnd = liu_->end(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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) |
| 128 | ++ir.lvrSegI_; |
| 129 | if (ir.lvrSegI_ == lvrEnd) |
| 130 | break; |
| 131 | // lvrSegI_ may have advanced far beyond liuSegI_, |
| 132 | // do a fast intersection test to "catch up" |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame^] | 133 | LiveSegment seg(ir.lvrSegI_->start, ir.lvrSegI_->end, lvr_); |
| 134 | ir.liuSegI_ = liu_->upperBound(ir.liuSegI_, seg); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 135 | // Check if no liuSegI_ exists with lvrSegI_->start < liuSegI_.end |
| 136 | if (ir.liuSegI_ == liuEnd) |
| 137 | break; |
| 138 | if (ir.liuSegI_->start < ir.lvrSegI_->end) { |
| 139 | assert(overlap(*ir.lvrSegI_, *ir.liuSegI_) && "upperBound postcondition"); |
| 140 | break; |
| 141 | } |
| 142 | } |
| 143 | if (ir.liuSegI_ == liuEnd) |
| 144 | ir.lvrSegI_ = lvrEnd; |
| 145 | } |
| 146 | |
| 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_; |
| 153 | } |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame^] | 154 | firstInterference_ = InterferenceResult(lvr_->begin(), liu_->begin()); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 155 | findIntersection(firstInterference_); |
| 156 | return firstInterference_; |
| 157 | } |
| 158 | |
| 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) { |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame^] | 166 | if (++ir.lvrSegI_ == lvr_->end()) |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 167 | return false; |
| 168 | } |
| 169 | else { |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame^] | 170 | if (++ir.liuSegI_ == liu_->end()) { |
| 171 | ir.lvrSegI_ = lvr_->end(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 172 | return false; |
| 173 | } |
| 174 | } |
| 175 | if (overlap(*ir.lvrSegI_, *ir.liuSegI_)) |
| 176 | return true; |
| 177 | // find the next intersection |
| 178 | findIntersection(ir); |
| 179 | return isInterference(ir); |
| 180 | } |