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/CodeGen/MachineLoopRanges.h" 20 #include "llvm/Support/Debug.h" 21 #include "llvm/Support/raw_ostream.h" 22 #include "llvm/Target/TargetRegisterInfo.h" 23 24 using namespace llvm; 25 26 27 // Merge a LiveInterval's segments. Guarantee no overlaps. 28 void LiveIntervalUnion::unify(LiveInterval &VirtReg) { 29 if (VirtReg.empty()) 30 return; 31 ++Tag; 32 33 // Insert each of the virtual register's live segments into the map. 34 LiveInterval::iterator RegPos = VirtReg.begin(); 35 LiveInterval::iterator RegEnd = VirtReg.end(); 36 SegmentIter SegPos = Segments.find(RegPos->start); 37 38 while (SegPos.valid()) { 39 SegPos.insert(RegPos->start, RegPos->end, &VirtReg); 40 if (++RegPos == RegEnd) 41 return; 42 SegPos.advanceTo(RegPos->start); 43 } 44 45 // We have reached the end of Segments, so it is no longer necessary to search 46 // for the insertion position. 47 // It is faster to insert the end first. 48 --RegEnd; 49 SegPos.insert(RegEnd->start, RegEnd->end, &VirtReg); 50 for (; RegPos != RegEnd; ++RegPos, ++SegPos) 51 SegPos.insert(RegPos->start, RegPos->end, &VirtReg); 52 } 53 54 // Remove a live virtual register's segments from this union. 55 void LiveIntervalUnion::extract(LiveInterval &VirtReg) { 56 if (VirtReg.empty()) 57 return; 58 ++Tag; 59 60 // Remove each of the virtual register's live segments from the map. 61 LiveInterval::iterator RegPos = VirtReg.begin(); 62 LiveInterval::iterator RegEnd = VirtReg.end(); 63 SegmentIter SegPos = Segments.find(RegPos->start); 64 65 for (;;) { 66 assert(SegPos.value() == &VirtReg && "Inconsistent LiveInterval"); 67 SegPos.erase(); 68 if (!SegPos.valid()) 69 return; 70 71 // Skip all segments that may have been coalesced. 72 RegPos = VirtReg.advanceTo(RegPos, SegPos.start()); 73 if (RegPos == RegEnd) 74 return; 75 76 SegPos.advanceTo(RegPos->start); 77 } 78 } 79 80 void 81 LiveIntervalUnion::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const { 82 OS << "LIU " << PrintReg(RepReg, TRI); 83 if (empty()) { 84 OS << " empty\n"; 85 return; 86 } 87 for (LiveSegments::const_iterator SI = Segments.begin(); SI.valid(); ++SI) { 88 OS << " [" << SI.start() << ' ' << SI.stop() << "):" 89 << PrintReg(SI.value()->reg, TRI); 90 } 91 OS << '\n'; 92 } 93 94 void LiveIntervalUnion::InterferenceResult::print(raw_ostream &OS, 95 const TargetRegisterInfo *TRI) const { 96 OS << '[' << start() << ';' << stop() << "):" 97 << PrintReg(interference()->reg, TRI); 98 } 99 100 void LiveIntervalUnion::Query::print(raw_ostream &OS, 101 const TargetRegisterInfo *TRI) { 102 OS << "Interferences with "; 103 LiveUnion->print(OS, TRI); 104 InterferenceResult IR = firstInterference(); 105 while (isInterference(IR)) { 106 OS << " "; 107 IR.print(OS, TRI); 108 OS << '\n'; 109 nextInterference(IR); 110 } 111 } 112 113 #ifndef NDEBUG 114 // Verify the live intervals in this union and add them to the visited set. 115 void LiveIntervalUnion::verify(LiveVirtRegBitSet& VisitedVRegs) { 116 for (SegmentIter SI = Segments.begin(); SI.valid(); ++SI) 117 VisitedVRegs.set(SI.value()->reg); 118 } 119 #endif //!NDEBUG 120 121 // Private interface accessed by Query. 122 // 123 // Find a pair of segments that intersect, one in the live virtual register 124 // (LiveInterval), and the other in this LiveIntervalUnion. The caller (Query) 125 // is responsible for advancing the LiveIntervalUnion segments to find a 126 // "notable" intersection, which requires query-specific logic. 127 // 128 // This design assumes only a fast mechanism for intersecting a single live 129 // virtual register segment with a set of LiveIntervalUnion segments. This may 130 // be ok since most virtual registers have very few segments. If we had a data 131 // structure that optimizd MxN intersection of segments, then we would bypass 132 // the loop that advances within the LiveInterval. 133 // 134 // If no intersection exists, set VirtRegI = VirtRegEnd, and set SI to the first 135 // segment whose start point is greater than LiveInterval's end point. 136 // 137 // Assumes that segments are sorted by start position in both 138 // LiveInterval and LiveSegments. 139 void LiveIntervalUnion::Query::findIntersection(InterferenceResult &IR) const { 140 // Search until reaching the end of the LiveUnion segments. 141 LiveInterval::iterator VirtRegEnd = VirtReg->end(); 142 if (IR.VirtRegI == VirtRegEnd) 143 return; 144 while (IR.LiveUnionI.valid()) { 145 // Slowly advance the live virtual reg iterator until we surpass the next 146 // segment in LiveUnion. 147 // 148 // Note: If this is ever used for coalescing of fixed registers and we have 149 // a live vreg with thousands of segments, then change this code to use 150 // upperBound instead. 151 IR.VirtRegI = VirtReg->advanceTo(IR.VirtRegI, IR.LiveUnionI.start()); 152 if (IR.VirtRegI == VirtRegEnd) 153 break; // Retain current (nonoverlapping) LiveUnionI 154 155 // VirtRegI may have advanced far beyond LiveUnionI, catch up. 156 IR.LiveUnionI.advanceTo(IR.VirtRegI->start); 157 158 // Check if no LiveUnionI exists with VirtRegI->Start < LiveUnionI.end 159 if (!IR.LiveUnionI.valid()) 160 break; 161 if (IR.LiveUnionI.start() < IR.VirtRegI->end) { 162 assert(overlap(*IR.VirtRegI, IR.LiveUnionI) && 163 "upperBound postcondition"); 164 break; 165 } 166 } 167 if (!IR.LiveUnionI.valid()) 168 IR.VirtRegI = VirtRegEnd; 169 } 170 171 // Find the first intersection, and cache interference info 172 // (retain segment iterators into both VirtReg and LiveUnion). 173 const LiveIntervalUnion::InterferenceResult & 174 LiveIntervalUnion::Query::firstInterference() { 175 if (CheckedFirstInterference) 176 return FirstInterference; 177 CheckedFirstInterference = true; 178 InterferenceResult &IR = FirstInterference; 179 IR.LiveUnionI.setMap(LiveUnion->getMap()); 180 181 // Quickly skip interference check for empty sets. 182 if (VirtReg->empty() || LiveUnion->empty()) { 183 IR.VirtRegI = VirtReg->end(); 184 } else if (VirtReg->beginIndex() < LiveUnion->startIndex()) { 185 // VirtReg starts first, perform double binary search. 186 IR.VirtRegI = VirtReg->find(LiveUnion->startIndex()); 187 if (IR.VirtRegI != VirtReg->end()) 188 IR.LiveUnionI.find(IR.VirtRegI->start); 189 } else { 190 // LiveUnion starts first, perform double binary search. 191 IR.LiveUnionI.find(VirtReg->beginIndex()); 192 if (IR.LiveUnionI.valid()) 193 IR.VirtRegI = VirtReg->find(IR.LiveUnionI.start()); 194 else 195 IR.VirtRegI = VirtReg->end(); 196 } 197 findIntersection(FirstInterference); 198 assert((IR.VirtRegI == VirtReg->end() || IR.LiveUnionI.valid()) 199 && "Uninitialized iterator"); 200 return FirstInterference; 201 } 202 203 // Treat the result as an iterator and advance to the next interfering pair 204 // of segments. This is a plain iterator with no filter. 205 bool LiveIntervalUnion::Query::nextInterference(InterferenceResult &IR) const { 206 assert(isInterference(IR) && "iteration past end of interferences"); 207 208 // Advance either the VirtReg or LiveUnion segment to ensure that we visit all 209 // unique overlapping pairs. 210 if (IR.VirtRegI->end < IR.LiveUnionI.stop()) { 211 if (++IR.VirtRegI == VirtReg->end()) 212 return false; 213 } 214 else { 215 if (!(++IR.LiveUnionI).valid()) { 216 IR.VirtRegI = VirtReg->end(); 217 return false; 218 } 219 } 220 // Short-circuit findIntersection() if possible. 221 if (overlap(*IR.VirtRegI, IR.LiveUnionI)) 222 return true; 223 224 // Find the next intersection. 225 findIntersection(IR); 226 return isInterference(IR); 227 } 228 229 // Scan the vector of interfering virtual registers in this union. Assume it's 230 // quite small. 231 bool LiveIntervalUnion::Query::isSeenInterference(LiveInterval *VirtReg) const { 232 SmallVectorImpl<LiveInterval*>::const_iterator I = 233 std::find(InterferingVRegs.begin(), InterferingVRegs.end(), VirtReg); 234 return I != InterferingVRegs.end(); 235 } 236 237 // Count the number of virtual registers in this union that interfere with this 238 // query's live virtual register. 239 // 240 // The number of times that we either advance IR.VirtRegI or call 241 // LiveUnion.upperBound() will be no more than the number of holes in 242 // VirtReg. So each invocation of collectInterferingVRegs() takes 243 // time proportional to |VirtReg Holes| * time(LiveUnion.upperBound()). 244 // 245 // For comments on how to speed it up, see Query::findIntersection(). 246 unsigned LiveIntervalUnion::Query:: 247 collectInterferingVRegs(unsigned MaxInterferingRegs, float MaxWeight) { 248 InterferenceResult IR = firstInterference(); 249 LiveInterval::iterator VirtRegEnd = VirtReg->end(); 250 LiveInterval *RecentInterferingVReg = NULL; 251 if (IR.VirtRegI != VirtRegEnd) while (IR.LiveUnionI.valid()) { 252 // Advance the union's iterator to reach an unseen interfering vreg. 253 do { 254 if (IR.LiveUnionI.value() == RecentInterferingVReg) 255 continue; 256 257 if (!isSeenInterference(IR.LiveUnionI.value())) 258 break; 259 260 // Cache the most recent interfering vreg to bypass isSeenInterference. 261 RecentInterferingVReg = IR.LiveUnionI.value(); 262 263 } while ((++IR.LiveUnionI).valid()); 264 if (!IR.LiveUnionI.valid()) 265 break; 266 267 // Advance the VirtReg iterator until surpassing the next segment in 268 // LiveUnion. 269 IR.VirtRegI = VirtReg->advanceTo(IR.VirtRegI, IR.LiveUnionI.start()); 270 if (IR.VirtRegI == VirtRegEnd) 271 break; 272 273 // Check for intersection with the union's segment. 274 if (overlap(*IR.VirtRegI, IR.LiveUnionI)) { 275 276 if (!IR.LiveUnionI.value()->isSpillable()) 277 SeenUnspillableVReg = true; 278 279 if (InterferingVRegs.size() == MaxInterferingRegs) 280 // Leave SeenAllInterferences set to false to indicate that at least one 281 // interference exists beyond those we collected. 282 return MaxInterferingRegs; 283 284 InterferingVRegs.push_back(IR.LiveUnionI.value()); 285 286 // Cache the most recent interfering vreg to bypass isSeenInterference. 287 RecentInterferingVReg = IR.LiveUnionI.value(); 288 ++IR.LiveUnionI; 289 290 // Stop collecting when the max weight is exceeded. 291 if (RecentInterferingVReg->weight >= MaxWeight) 292 return InterferingVRegs.size(); 293 294 continue; 295 } 296 // VirtRegI may have advanced far beyond LiveUnionI, 297 // do a fast intersection test to "catch up" 298 IR.LiveUnionI.advanceTo(IR.VirtRegI->start); 299 } 300 SeenAllInterferences = true; 301 return InterferingVRegs.size(); 302 } 303 304 bool LiveIntervalUnion::Query::checkLoopInterference(MachineLoopRange *Loop) { 305 // VirtReg is likely live throughout the loop, so start by checking LIU-Loop 306 // overlaps. 307 IntervalMapOverlaps<LiveIntervalUnion::Map, MachineLoopRange::Map> 308 Overlaps(LiveUnion->getMap(), Loop->getMap()); 309 if (!Overlaps.valid()) 310 return false; 311 312 // The loop is overlapping an LIU assignment. Check VirtReg as well. 313 LiveInterval::iterator VRI = VirtReg->find(Overlaps.start()); 314 315 for (;;) { 316 if (VRI == VirtReg->end()) 317 return false; 318 if (VRI->start < Overlaps.stop()) 319 return true; 320 321 Overlaps.advanceTo(VRI->start); 322 if (!Overlaps.valid()) 323 return false; 324 if (Overlaps.start() < VRI->end) 325 return true; 326 327 VRI = VirtReg->advanceTo(VRI, Overlaps.start()); 328 } 329 } 330