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 void LiveIntervalUnion::unify(LiveInterval &lvr) { 25 // Add this live virtual register to the union 26 LiveVirtRegs::iterator pos = std::upper_bound(lvrs_.begin(), lvrs_.end(), 27 &lvr, less_ptr<LiveInterval>()); 28 assert((pos == lvrs_.end() || *pos != &lvr) && "duplicate LVR insertion"); 29 lvrs_.insert(pos, &lvr); 30 // Insert each of the virtual register's live segments into the map 31 SegmentIter segPos = segments_.begin(); 32 for (LiveInterval::iterator lvrI = lvr.begin(), lvrEnd = lvr.end(); 33 lvrI != lvrEnd; ++lvrI ) { 34 LiveSegment segment(lvrI->start, lvrI->end, lvr); 35 segPos = segments_.insert(segPos, segment); 36 assert(*segPos == segment && "need equal val for equal key"); 37 } 38 } 39 40 namespace { 41 42 // Keep LVRs sorted for fast membership test and extraction. 43 struct LessReg 44 : public std::binary_function<LiveInterval*, LiveInterval*, bool> { 45 bool operator()(const LiveInterval *left, const LiveInterval *right) const { 46 return left->reg < right->reg; 47 } 48 }; 49 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 52 // 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. 56 // 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 |--... 66 // 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 // 71 // FIXME: support extract(), handle tombstones of extracted lvrs. 72 typedef LiveIntervalUnion::SegmentIter SegmentIter; 73 SegmentIter upperBound(SegmentIter segBegin, 74 SegmentIter segEnd, 75 const LiveRange &lvrSeg) { 76 assert(lvrSeg.end > segBegin->start && "segment iterator precondition"); 77 // get the next LIU segment such that setg.start is not less than 78 // lvrSeg.start 79 SegmentIter segI = std::upper_bound(segBegin, segEnd, lvrSeg.start); 80 while (segI != segBegin) { 81 --segI; 82 if (lvrSeg.start >= segI->end) 83 return ++segI; 84 } 85 return segI; 86 } 87 } // end anonymous namespace 88 89 // Private interface accessed by Query. 90 // 91 // Find a pair of segments that intersect, one in the live virtual register 92 // (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query) 93 // is responsible for advancing the LiveIntervalUnion segments to find a 94 // "notable" intersection, which requires query-specific logic. 95 // 96 // This design assumes only a fast mechanism for intersecting a single live 97 // virtual register segment with a set of LiveIntervalUnion segments. This may 98 // be ok since most LVRs have very few segments. If we had a data 99 // structure that optimizd MxN intersection of segments, then we would bypass 100 // the loop that advances within the LiveInterval. 101 // 102 // If no intersection exists, set lvrI = lvrEnd, and set segI to the first 103 // segment whose start point is greater than LiveInterval's end point. 104 // 105 // Assumes that segments are sorted by start position in both 106 // LiveInterval and LiveSegments. 107 void LiveIntervalUnion::Query::findIntersection(InterferenceResult &ir) const { 108 LiveInterval::iterator lvrEnd = lvr_.end(); 109 SegmentIter liuEnd = liu_.end(); 110 while (ir.liuSegI_ != liuEnd) { 111 // Slowly advance the live virtual reg iterator until we surpass the next 112 // segment in this union. If this is ever used for coalescing of fixed 113 // registers and we have a LiveInterval with thousands of segments, then use 114 // upper bound instead. 115 while (ir.lvrSegI_ != lvrEnd && ir.lvrSegI_->end <= ir.liuSegI_->start) 116 ++ir.lvrSegI_; 117 if (ir.lvrSegI_ == lvrEnd) 118 break; 119 // lvrSegI_ may have advanced far beyond liuSegI_, 120 // do a fast intersection test to "catch up" 121 ir.liuSegI_ = upperBound(ir.liuSegI_, liuEnd, *ir.lvrSegI_); 122 // Check if no liuSegI_ exists with lvrSegI_->start < liuSegI_.end 123 if (ir.liuSegI_ == liuEnd) 124 break; 125 if (ir.liuSegI_->start < ir.lvrSegI_->end) { 126 assert(overlap(*ir.lvrSegI_, *ir.liuSegI_) && "upperBound postcondition"); 127 break; 128 } 129 } 130 if (ir.liuSegI_ == liuEnd) 131 ir.lvrSegI_ = lvrEnd; 132 } 133 134 // Find the first intersection, and cache interference info 135 // (retain segment iterators into both lvr_ and liu_). 136 LiveIntervalUnion::InterferenceResult 137 LiveIntervalUnion::Query::firstInterference() { 138 if (firstInterference_ != LiveIntervalUnion::InterferenceResult()) { 139 return firstInterference_; 140 } 141 firstInterference_ = InterferenceResult(lvr_.begin(), liu_.begin()); 142 findIntersection(firstInterference_); 143 return firstInterference_; 144 } 145 146 // Treat the result as an iterator and advance to the next interfering pair 147 // of segments. This is a plain iterator with no filter. 148 bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &ir) const { 149 assert(isInterference(ir) && "iteration past end of interferences"); 150 // Advance either the lvr or liu segment to ensure that we visit all unique 151 // overlapping pairs. 152 if (ir.lvrSegI_->end < ir.liuSegI_->end) { 153 if (++ir.lvrSegI_ == lvr_.end()) 154 return false; 155 } 156 else { 157 if (++ir.liuSegI_ == liu_.end()) { 158 ir.lvrSegI_ = lvr_.end(); 159 return false; 160 } 161 } 162 if (overlap(*ir.lvrSegI_, *ir.liuSegI_)) 163 return true; 164 // find the next intersection 165 findIntersection(ir); 166 return isInterference(ir); 167 } 168