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" |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SparseBitVector.h" |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | #include <algorithm> |
| 22 | using namespace llvm; |
| 23 | |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 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 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 62 | // Merge a LiveInterval's segments. Guarantee no overlaps. |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 63 | // |
| 64 | // Consider coalescing adjacent segments to save space, even though it makes |
| 65 | // extraction more complicated. |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 66 | void LiveIntervalUnion::unify(LiveInterval &lvr) { |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 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 ) { |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 71 | LiveSegment segment(lvrI->start, lvrI->end, &lvr); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 72 | segPos = segments_.insert(segPos, segment); |
| 73 | assert(*segPos == segment && "need equal val for equal key"); |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 74 | #ifndef NDEBUG |
| 75 | // check for overlap (inductively) |
| 76 | if (segPos != segments_.begin()) { |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 77 | assert(llvm::prior(segPos)->end <= segment.start && |
| 78 | "overlapping segments" ); |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 79 | } |
Andrew Trick | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 80 | SegmentIter nextPos = llvm::next(segPos); |
Andrew Trick | e16eecc | 2010-10-26 18:34:01 +0000 | [diff] [blame] | 81 | if (nextPos != segments_.end()) |
| 82 | assert(segment.end <= nextPos->start && "overlapping segments" ); |
| 83 | #endif // NDEBUG |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 87 | // Remove a live virtual register's segments from this union. |
| 88 | void LiveIntervalUnion::extract(const LiveInterval &lvr) { |
| 89 | // Remove each of the virtual register's live segments from the map. |
| 90 | SegmentIter segPos = segments_.begin(); |
| 91 | for (LiveInterval::const_iterator lvrI = lvr.begin(), lvrEnd = lvr.end(); |
| 92 | lvrI != lvrEnd; ++lvrI) { |
| 93 | LiveSegment seg(lvrI->start, lvrI->end, const_cast<LiveInterval*>(&lvr)); |
| 94 | segPos = upperBound(segPos, seg); |
| 95 | assert(segPos != segments_.end() && "missing lvr segment"); |
| 96 | segments_.erase(segPos++); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 97 | } |
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 | 071d1c0 | 2010-11-09 21:04:34 +0000 | [diff] [blame] | 100 | raw_ostream& llvm::operator<<(raw_ostream& os, const LiveSegment &ls) { |
| 101 | return os << '[' << ls.start << ',' << ls.end << ':' << |
| 102 | ls.liveVirtReg->reg << ")"; |
| 103 | } |
| 104 | |
| 105 | void LiveSegment::dump() const { |
| 106 | dbgs() << *this << "\n"; |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | LiveIntervalUnion::print(raw_ostream &os, |
| 111 | const AbstractRegisterDescription *rdesc) const { |
| 112 | os << "LIU "; |
| 113 | if (rdesc != NULL) |
| 114 | os << rdesc->getName(repReg_); |
| 115 | else { |
| 116 | os << repReg_; |
| 117 | } |
| 118 | for (LiveSegments::const_iterator segI = segments_.begin(), |
| 119 | segEnd = segments_.end(); segI != segEnd; ++segI) { |
| 120 | dbgs() << " " << *segI; |
| 121 | } |
| 122 | os << "\n"; |
| 123 | } |
| 124 | |
| 125 | void LiveIntervalUnion::dump(const AbstractRegisterDescription *rdesc) const { |
| 126 | print(dbgs(), rdesc); |
| 127 | } |
| 128 | |
| 129 | #ifndef NDEBUG |
| 130 | // Verify the live intervals in this union and add them to the visited set. |
| 131 | void LiveIntervalUnion::verify(LvrBitSet& visitedVRegs) { |
| 132 | SegmentIter segI = segments_.begin(); |
| 133 | SegmentIter segEnd = segments_.end(); |
| 134 | if (segI == segEnd) return; |
| 135 | visitedVRegs.set(segI->liveVirtReg->reg); |
| 136 | for (++segI; segI != segEnd; ++segI) { |
| 137 | visitedVRegs.set(segI->liveVirtReg->reg); |
| 138 | assert(llvm::prior(segI)->end <= segI->start && "overlapping segments" ); |
| 139 | } |
| 140 | } |
| 141 | #endif //!NDEBUG |
| 142 | |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 143 | // Private interface accessed by Query. |
| 144 | // |
| 145 | // Find a pair of segments that intersect, one in the live virtual register |
| 146 | // (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query) |
| 147 | // is responsible for advancing the LiveIntervalUnion segments to find a |
| 148 | // "notable" intersection, which requires query-specific logic. |
| 149 | // |
| 150 | // This design assumes only a fast mechanism for intersecting a single live |
| 151 | // virtual register segment with a set of LiveIntervalUnion segments. This may |
| 152 | // be ok since most LVRs have very few segments. If we had a data |
| 153 | // structure that optimizd MxN intersection of segments, then we would bypass |
| 154 | // the loop that advances within the LiveInterval. |
| 155 | // |
| 156 | // If no intersection exists, set lvrI = lvrEnd, and set segI to the first |
| 157 | // segment whose start point is greater than LiveInterval's end point. |
| 158 | // |
| 159 | // Assumes that segments are sorted by start position in both |
| 160 | // LiveInterval and LiveSegments. |
| 161 | void LiveIntervalUnion::Query::findIntersection(InterferenceResult &ir) const { |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 162 | LiveInterval::iterator lvrEnd = lvr_->end(); |
| 163 | SegmentIter liuEnd = liu_->end(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 164 | while (ir.liuSegI_ != liuEnd) { |
| 165 | // Slowly advance the live virtual reg iterator until we surpass the next |
| 166 | // segment in this union. If this is ever used for coalescing of fixed |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 167 | // registers and we have a live vreg with thousands of segments, then use |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 168 | // upper bound instead. |
| 169 | while (ir.lvrSegI_ != lvrEnd && ir.lvrSegI_->end <= ir.liuSegI_->start) |
| 170 | ++ir.lvrSegI_; |
| 171 | if (ir.lvrSegI_ == lvrEnd) |
| 172 | break; |
| 173 | // lvrSegI_ may have advanced far beyond liuSegI_, |
| 174 | // do a fast intersection test to "catch up" |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 175 | LiveSegment seg(ir.lvrSegI_->start, ir.lvrSegI_->end, lvr_); |
| 176 | ir.liuSegI_ = liu_->upperBound(ir.liuSegI_, seg); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 177 | // Check if no liuSegI_ exists with lvrSegI_->start < liuSegI_.end |
| 178 | if (ir.liuSegI_ == liuEnd) |
| 179 | break; |
| 180 | if (ir.liuSegI_->start < ir.lvrSegI_->end) { |
| 181 | assert(overlap(*ir.lvrSegI_, *ir.liuSegI_) && "upperBound postcondition"); |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | if (ir.liuSegI_ == liuEnd) |
| 186 | ir.lvrSegI_ = lvrEnd; |
| 187 | } |
| 188 | |
| 189 | // Find the first intersection, and cache interference info |
| 190 | // (retain segment iterators into both lvr_ and liu_). |
| 191 | LiveIntervalUnion::InterferenceResult |
| 192 | LiveIntervalUnion::Query::firstInterference() { |
| 193 | if (firstInterference_ != LiveIntervalUnion::InterferenceResult()) { |
| 194 | return firstInterference_; |
| 195 | } |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 196 | firstInterference_ = InterferenceResult(lvr_->begin(), liu_->begin()); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 197 | findIntersection(firstInterference_); |
| 198 | return firstInterference_; |
| 199 | } |
| 200 | |
| 201 | // Treat the result as an iterator and advance to the next interfering pair |
| 202 | // of segments. This is a plain iterator with no filter. |
| 203 | bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &ir) const { |
| 204 | assert(isInterference(ir) && "iteration past end of interferences"); |
| 205 | // Advance either the lvr or liu segment to ensure that we visit all unique |
| 206 | // overlapping pairs. |
| 207 | if (ir.lvrSegI_->end < ir.liuSegI_->end) { |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 208 | if (++ir.lvrSegI_ == lvr_->end()) |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | else { |
Andrew Trick | e141a49 | 2010-11-08 18:02:08 +0000 | [diff] [blame] | 212 | if (++ir.liuSegI_ == liu_->end()) { |
| 213 | ir.lvrSegI_ = lvr_->end(); |
Andrew Trick | 14e8d71 | 2010-10-22 23:09:15 +0000 | [diff] [blame] | 214 | return false; |
| 215 | } |
| 216 | } |
| 217 | if (overlap(*ir.lvrSegI_, *ir.liuSegI_)) |
| 218 | return true; |
| 219 | // find the next intersection |
| 220 | findIntersection(ir); |
| 221 | return isInterference(ir); |
| 222 | } |
Andrew Trick | f4baeaf | 2010-11-10 19:18:47 +0000 | [diff] [blame] | 223 | |
| 224 | // Scan the vector of interfering virtual registers in this union. Assuming it's |
| 225 | // quite small. |
| 226 | bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *lvr) const { |
| 227 | SmallVectorImpl<LiveInterval*>::const_iterator I = |
| 228 | std::find(interferingVRegs_.begin(), interferingVRegs_.end(), lvr); |
| 229 | return I != interferingVRegs_.end(); |
| 230 | } |
| 231 | |
| 232 | // Count the number of virtual registers in this union that interfere with this |
| 233 | // query's live virtual register. |
| 234 | // |
| 235 | // The number of times that we either advance ir.lvrSegI_ or call |
| 236 | // liu_.upperBound() will be no more than the number of holes in |
| 237 | // lvr_. So each invocation of collectInterferingVirtReg() takes |
| 238 | // time proportional to |lvr-holes| * time(liu_.upperBound()). |
| 239 | // |
| 240 | // For comments on how to speed it up, see Query::findIntersection(). |
| 241 | unsigned LiveIntervalUnion::Query:: |
| 242 | collectInterferingVRegs(unsigned maxInterferingRegs) { |
| 243 | InterferenceResult ir = firstInterference(); |
| 244 | LiveInterval::iterator lvrEnd = lvr_->end(); |
| 245 | SegmentIter liuEnd = liu_->end(); |
| 246 | LiveInterval *recentInterferingVReg = NULL; |
| 247 | while (ir.liuSegI_ != liuEnd) { |
| 248 | // Advance the union's iterator to reach an unseen interfering vreg. |
| 249 | do { |
| 250 | if (ir.liuSegI_->liveVirtReg == recentInterferingVReg) |
| 251 | continue; |
| 252 | |
| 253 | if (!isSeenInterference(ir.liuSegI_->liveVirtReg)) |
| 254 | break; |
| 255 | |
| 256 | // Cache the most recent interfering vreg to bypass isSeenInterference. |
| 257 | recentInterferingVReg = ir.liuSegI_->liveVirtReg; |
| 258 | |
| 259 | } while( ++ir.liuSegI_ != liuEnd); |
| 260 | if (ir.liuSegI_ == liuEnd) |
| 261 | break; |
| 262 | |
| 263 | // Advance the live vreg reg iterator until surpassing the next |
| 264 | // segment in this union. If this is ever used for coalescing of fixed |
| 265 | // registers and we have a live vreg with thousands of segments, then use |
| 266 | // upper bound instead. |
| 267 | while (ir.lvrSegI_ != lvrEnd && ir.lvrSegI_->end <= ir.liuSegI_->start) |
| 268 | ++ir.lvrSegI_; |
| 269 | if (ir.lvrSegI_ == lvrEnd) |
| 270 | break; |
| 271 | |
| 272 | // Check for intersection with the union's segment. |
| 273 | if (overlap(*ir.lvrSegI_, *ir.liuSegI_)) { |
| 274 | if (!ir.liuSegI_->liveVirtReg->isSpillable()) |
| 275 | seenUnspillableVReg_ = true; |
| 276 | |
| 277 | interferingVRegs_.push_back(ir.liuSegI_->liveVirtReg); |
| 278 | if (interferingVRegs_.size() == maxInterferingRegs) |
| 279 | return maxInterferingRegs; |
| 280 | |
| 281 | // Cache the most recent interfering vreg to bypass isSeenInterference. |
| 282 | recentInterferingVReg = ir.liuSegI_->liveVirtReg; |
| 283 | ++ir.liuSegI_; |
| 284 | continue; |
| 285 | } |
| 286 | // lvrSegI_ may have advanced far beyond liuSegI_, |
| 287 | // do a fast intersection test to "catch up" |
| 288 | LiveSegment seg(ir.lvrSegI_->start, ir.lvrSegI_->end, lvr_); |
| 289 | ir.liuSegI_ = liu_->upperBound(ir.liuSegI_, seg); |
| 290 | } |
| 291 | return interferingVRegs_.size(); |
| 292 | } |