LiveInterval.cpp revision 5649e25ce86b9d89d228ae7c392413571b0f8c19
13ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid//===-- LiveInterval.cpp - Live Interval Representation -------------------===// 23ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// 33ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// The LLVM Compiler Infrastructure 43ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// 53ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// This file is distributed under the University of Illinois Open Source 63ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// License. See LICENSE.TXT for details. 73ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// 83ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid//===----------------------------------------------------------------------===// 92b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org// 103ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// This file implements the LiveRange and LiveInterval classes. Given some 112b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org// numbering of each the machine instructions an interval [i, j) is said to be a 123ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// live range for register v if there is no instruction with number j' >= j 133ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// such that v is live at j' and there is no instruction with number i' < i such 143ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// that v is live at i'. In this implementation ranges can have holes, 153ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// i.e. a range might look like [1,20), [50,65), [1000,1001). Each 163ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// individual segment is represented as an instance of LiveRange::Segment, 173ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// and the whole range is represented as an instance of LiveRange. 183ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// 193ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid//===----------------------------------------------------------------------===// 203ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 212b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org#include "llvm/CodeGen/LiveInterval.h" 222b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org#include "RegisterCoalescer.h" 233ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid#include "llvm/ADT/DenseMap.h" 243ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid#include "llvm/ADT/STLExtras.h" 253ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid#include "llvm/ADT/SmallSet.h" 263ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid#include "llvm/CodeGen/LiveIntervalAnalysis.h" 273ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid#include "llvm/CodeGen/MachineRegisterInfo.h" 283ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid#include "llvm/Support/Debug.h" 292b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org#include "llvm/Support/raw_ostream.h" 303ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid#include "llvm/Target/TargetRegisterInfo.h" 313ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid#include <algorithm> 323ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsidusing namespace llvm; 333ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 343ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsidLiveRange::iterator LiveRange::find(SlotIndex Pos) { 353ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // This algorithm is basically std::upper_bound. 363ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Unfortunately, std::upper_bound cannot be used with mixed types until we 373ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // adopt C++0x. Many libraries can do it, but not all. 383ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (empty() || Pos >= endIndex()) 393ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return end(); 403ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid iterator I = begin(); 413ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid size_t Len = size(); 423ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid do { 433ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid size_t Mid = Len >> 1; 443ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (Pos < I[Mid].end) 453ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid Len = Mid; 463ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid else 472b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org I += Mid + 1, Len -= Mid + 1; 483ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } while (Len); 493ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return I; 503ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid} 513ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 523ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsidVNInfo *LiveRange::createDeadDef(SlotIndex Def, 53a3d4c973369987e14cc0c05964e288ea0eac11dcnealsid VNInfo::Allocator &VNInfoAllocator) { 543ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(!Def.isDead() && "Cannot define a value at the dead slot"); 553ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid iterator I = find(Def); 563ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (I == end()) { 573ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid VNInfo *VNI = getNextValue(Def, VNInfoAllocator); 583ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid segments.push_back(Segment(Def, Def.getDeadSlot(), VNI)); 593ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return VNI; 603ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 613ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (SlotIndex::isSameInstr(Def, I->start)) { 623ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(I->valno->def == I->start && "Inconsistent existing value def"); 633ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 643ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // It is possible to have both normal and early-clobber defs of the same 653ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // register on an instruction. It doesn't make a lot of sense, but it is 662b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org // possible to specify in inline assembly. 673ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // 683ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Just convert everything to early-clobber. 693ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid Def = std::min(Def, I->start); 703ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (Def != I->start) 713ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid I->start = I->valno->def = Def; 723ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return I->valno; 733ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 742b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org assert(SlotIndex::isEarlierInstr(Def, I->start) && "Already live at def"); 753ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid VNInfo *VNI = getNextValue(Def, VNInfoAllocator); 763ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid segments.insert(I, Segment(Def, Def.getDeadSlot(), VNI)); 773ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return VNI; 783ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid} 793ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 803ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// overlaps - Return true if the intersection of the two live ranges is 813ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// not empty. 822b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org// 833ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// An example for overlaps(): 843ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// 853ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// 0: A = ... 863ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// 4: B = ... 873ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// 8: C = A + B ;; last use of A 882b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org// 893ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// The live ranges should look like: 903ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// 913ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// A = [3, 11) 923ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// B = [7, x) 933ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// C = [11, y) 943ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// 953ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// A->overlaps(C) should return false since we want to be able to join 963ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// A and C. 973ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid// 983ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsidbool LiveRange::overlapsFrom(const LiveRange& other, 993ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid const_iterator StartPos) const { 1003ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(!empty() && "empty range"); 1013ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid const_iterator i = begin(); 1023ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid const_iterator ie = end(); 1033ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid const_iterator j = StartPos; 1043ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid const_iterator je = other.end(); 1053ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 1063ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert((StartPos->start <= i->start || StartPos == other.begin()) && 1073ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid StartPos != other.end() && "Bogus start position hint!"); 1083ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 1093ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (i->start < j->start) { 1102b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org i = std::upper_bound(i, ie, j->start); 1113ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (i != begin()) --i; 1123ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } else if (j->start < i->start) { 1132b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org ++StartPos; 1143ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (StartPos != other.end() && StartPos->start <= i->start) { 1153ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(StartPos < other.end() && i < end()); 1163ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid j = std::upper_bound(j, je, i->start); 1173ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (j != other.begin()) --j; 1183ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 1193ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } else { 1203ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return true; 1213ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 1223ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 1233ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (j == je) return false; 1243ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 1253ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid while (i != ie) { 1263ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (i->start > j->start) { 1273ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid std::swap(i, j); 1283ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid std::swap(ie, je); 1293ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 1303ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 1313ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (i->end > j->start) 1323ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return true; 1333ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid ++i; 1343ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 1353ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 1363ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return false; 1373ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid} 1383ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 1393ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsidbool LiveRange::overlaps(const LiveRange &Other, const CoalescerPair &CP, 1403ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid const SlotIndexes &Indexes) const { 1413ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(!empty() && "empty range"); 1423ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (Other.empty()) 1432b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org return false; 1443ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 1453ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Use binary searches to find initial positions. 1463ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid const_iterator I = find(Other.beginIndex()); 1473ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid const_iterator IE = end(); 1483ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (I == IE) 1493ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return false; 1503ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid const_iterator J = Other.find(I->start); 1513ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid const_iterator JE = Other.end(); 1523ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (J == JE) 1533ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return false; 1543ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 1553ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid for (;;) { 1563ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // J has just been advanced to satisfy: 1572b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org assert(J->end >= I->start); 1583ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Check for an overlap. 1593ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (J->start < I->end) { 1603ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // I and J are overlapping. Find the later start. 1613ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid SlotIndex Def = std::max(I->start, J->start); 1623ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Allow the overlap if Def is a coalescable copy. 1633ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (Def.isBlock() || 1642b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org !CP.isCoalescable(Indexes.getInstructionFromIndex(Def))) 1653ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return true; 1663ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 1673ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Advance the iterator that ends first to check for more overlaps. 1683ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (J->end > I->end) { 1693ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid std::swap(I, J); 1703ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid std::swap(IE, JE); 1713ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 1723ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Advance J until J->end >= I->start. 1733ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid do 1743ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (++J == JE) 1753ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return false; 1763ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid while (J->end < I->start); 1773ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 1783ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid} 1793ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 1803ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// overlaps - Return true if the live range overlaps an interval specified 1813ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// by [Start, End). 1822b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.orgbool LiveRange::overlaps(SlotIndex Start, SlotIndex End) const { 1833ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(Start < End && "Invalid range"); 1843ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid const_iterator I = std::lower_bound(begin(), end(), End); 1853ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return I != begin() && (--I)->end > Start; 1862b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org} 1872b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org 1883ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 1893ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// ValNo is dead, remove it. If it is the largest value number, just nuke it 1903ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// (and any other deleted values neighboring it), otherwise mark it as ~1U so 1913ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// it can be nuked later. 1923ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsidvoid LiveRange::markValNoForDeletion(VNInfo *ValNo) { 1933ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (ValNo->id == getNumValNums()-1) { 1943ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid do { 1952b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org valnos.pop_back(); 1963ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } while (!valnos.empty() && valnos.back()->isUnused()); 1973ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } else { 1983ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid ValNo->markUnused(); 1993ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 2003ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid} 2013ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 2023ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// RenumberValues - Renumber all values in order of appearance and delete the 2033ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// remaining unused values. 2043ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsidvoid LiveRange::RenumberValues() { 2053ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid SmallPtrSet<VNInfo*, 8> Seen; 2063ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid valnos.clear(); 2073ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid for (const_iterator I = begin(), E = end(); I != E; ++I) { 2083ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid VNInfo *VNI = I->valno; 2093ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (!Seen.insert(VNI)) 2103ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid continue; 2113ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(!VNI->isUnused() && "Unused valno used by live segment"); 2123ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid VNI->id = (unsigned)valnos.size(); 2133ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid valnos.push_back(VNI); 2143ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 2153ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid} 2163ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 2173ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// This method is used when we want to extend the segment specified by I to end 2183ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// at the specified endpoint. To do this, we should merge and eliminate all 2193ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// segments that this will overlap with. The iterator is not invalidated. 2203ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsidvoid LiveRange::extendSegmentEndTo(iterator I, SlotIndex NewEnd) { 2213ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(I != end() && "Not a valid segment!"); 2223ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid VNInfo *ValNo = I->valno; 2233ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 2243ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Search for the first segment that we can't merge with. 2253ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid iterator MergeTo = llvm::next(I); 2263ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid for (; MergeTo != end() && NewEnd >= MergeTo->end; ++MergeTo) { 2273ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(MergeTo->valno == ValNo && "Cannot merge with differing values!"); 2283ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 2293ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 2303ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // If NewEnd was in the middle of a segment, make sure to get its endpoint. 2313ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid I->end = std::max(NewEnd, prior(MergeTo)->end); 2323ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 2333ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // If the newly formed segment now touches the segment after it and if they 2343ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // have the same value number, merge the two segments into one segment. 2353ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (MergeTo != end() && MergeTo->start <= I->end && 2363ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid MergeTo->valno == ValNo) { 2373ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid I->end = MergeTo->end; 2383ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid ++MergeTo; 2393ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 2403ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 2412b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org // Erase any dead segments. 2422b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org segments.erase(llvm::next(I), MergeTo); 2432b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org} 2442b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org 2453ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 2463ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// This method is used when we want to extend the segment specified by I to 2473ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// start at the specified endpoint. To do this, we should merge and eliminate 2483ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// all segments that this will overlap with. 2493ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsidLiveRange::iterator 2503ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsidLiveRange::extendSegmentStartTo(iterator I, SlotIndex NewStart) { 2513ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(I != end() && "Not a valid segment!"); 2523ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid VNInfo *ValNo = I->valno; 2533ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 2543ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Search for the first segment that we can't merge with. 2553ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid iterator MergeTo = I; 2563ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid do { 2573ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (MergeTo == begin()) { 2583ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid I->start = NewStart; 2593ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid segments.erase(MergeTo, I); 2603ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return I; 2613ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 2623ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(MergeTo->valno == ValNo && "Cannot merge with differing values!"); 2633ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid --MergeTo; 2643ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } while (NewStart <= MergeTo->start); 2653ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 2663ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // If we start in the middle of another segment, just delete a range and 2673ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // extend that segment. 2683ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) { 2693ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid MergeTo->end = I->end; 2703ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } else { 2713ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Otherwise, extend the segment right after. 2723ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid ++MergeTo; 2733ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid MergeTo->start = NewStart; 2743ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid MergeTo->end = I->end; 2752b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org } 2763ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 2772b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org segments.erase(llvm::next(MergeTo), llvm::next(I)); 2783ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return MergeTo; 2792b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org} 2803ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 2812b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.orgLiveRange::iterator LiveRange::addSegmentFrom(Segment S, iterator From) { 2823ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid SlotIndex Start = S.start, End = S.end; 2833ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid iterator it = std::upper_bound(From, end(), Start); 2843ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 2853ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // If the inserted segment starts in the middle or right at the end of 2863ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // another segment, just extend that segment to contain the segment of S. 2873ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (it != begin()) { 2882b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org iterator B = prior(it); 2893ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (S.valno == B->valno) { 2903ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (B->start <= Start && B->end >= Start) { 2913ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid extendSegmentEndTo(B, End); 2923ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return B; 2933ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 2943ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } else { 2953ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Check to make sure that we are not overlapping two live segments with 2963ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // different valno's. 2973ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(B->end <= Start && 2983ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid "Cannot overlap two segments with differing ValID's" 2993ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid " (did you def the same reg twice in a MachineInstr?)"); 3003ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 3013ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 3023ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 3033ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Otherwise, if this segment ends in the middle of, or right next to, another 3043ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // segment, merge it into that segment. 3053ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (it != end()) { 3063ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (S.valno == it->valno) { 3072b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org if (it->start <= End) { 3083ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid it = extendSegmentStartTo(it, Start); 3093ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 3103ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // If S is a complete superset of a segment, we may need to grow its 3112b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org // endpoint as well. 3123ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (End > it->end) 3132b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org extendSegmentEndTo(it, End); 3143ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return it; 3152b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org } 3163ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } else { 3172b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org // Check to make sure that we are not overlapping two live segments with 3183ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // different valno's. 3193ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(it->start >= End && 3203ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid "Cannot overlap two segments with differing ValID's"); 3212b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org } 3222b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org } 3232b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org 3243ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Otherwise, this is just a new segment that doesn't interact with anything. 3253ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Insert it. 3263ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return segments.insert(it, S); 3273ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid} 3283ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 3293ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// extendInBlock - If this range is live before Kill in the basic 3303ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// block that starts at StartIdx, extend it to be live up to Kill and return 3313ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// the value. If there is no live range before Kill, return NULL. 3323ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsidVNInfo *LiveRange::extendInBlock(SlotIndex StartIdx, SlotIndex Kill) { 3333ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (empty()) 3343ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return 0; 3353ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid iterator I = std::upper_bound(begin(), end(), Kill.getPrevSlot()); 3363ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (I == begin()) 3373ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return 0; 3383ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid --I; 3393ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (I->end <= StartIdx) 3403ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return 0; 3413ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (I->end < Kill) 3423ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid extendSegmentEndTo(I, Kill); 3432b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org return I->valno; 3442b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org} 3453ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 3463ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// Remove the specified segment from this range. Note that the segment must 3473ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// be in a single Segment in its entirety. 3483ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsidvoid LiveRange::removeSegment(SlotIndex Start, SlotIndex End, 3493ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid bool RemoveDeadValNo) { 3503ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Find the Segment containing this span. 3513ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid iterator I = find(Start); 3523ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(I != end() && "Segment is not in range!"); 3533ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(I->containsInterval(Start, End) 3543ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid && "Segment is not entirely in range!"); 3553ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 3563ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // If the span we are removing is at the start of the Segment, adjust it. 3573ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid VNInfo *ValNo = I->valno; 3583ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (I->start == Start) { 3593ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (I->end == End) { 3603ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (RemoveDeadValNo) { 3613ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Check if val# is dead. 3623ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid bool isDead = true; 3633ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid for (const_iterator II = begin(), EE = end(); II != EE; ++II) 3643ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (II != I && II->valno == ValNo) { 3653ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid isDead = false; 3663ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid break; 3673ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 3683ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (isDead) { 3693ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Now that ValNo is dead, remove it. 3703ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid markValNoForDeletion(ValNo); 3712b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org } 3723ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 3733ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 3743ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid segments.erase(I); // Removed the whole Segment. 3753ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } else 3763ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid I->start = End; 3773ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return; 3783ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 3793ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 3803ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Otherwise if the span we are removing is at the end of the Segment, 3813ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // adjust the other way. 3823ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (I->end == End) { 3833ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid I->end = Start; 3843ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid return; 3852b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org } 3863ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 3873ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Otherwise, we are splitting the Segment into two pieces. 3883ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid SlotIndex OldEnd = I->end; 3893ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid I->end = Start; // Trim the old segment. 3903ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 3913ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Insert the new one. 3923ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid segments.insert(llvm::next(I), Segment(End, OldEnd, ValNo)); 3933ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid} 3943ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 3953ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid/// removeValNo - Remove all the segments defined by the specified value#. 3962b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org/// Also remove the value# from value# list. 3973ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsidvoid LiveRange::removeValNo(VNInfo *ValNo) { 3983ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (empty()) return; 3993ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid iterator I = end(); 4003ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid iterator E = begin(); 4013ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid do { 4023ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid --I; 4033ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (I->valno == ValNo) 4043ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid segments.erase(I); 4053ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } while (I != E); 4063ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Now that ValNo is dead, remove it. 4073ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid markValNoForDeletion(ValNo); 4083ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid} 4093ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 4103ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsidvoid LiveRange::join(LiveRange &Other, 4113ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid const int *LHSValNoAssignments, 4123ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid const int *RHSValNoAssignments, 4132b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org SmallVectorImpl<VNInfo *> &NewVNInfo) { 4143ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid verify(); 4153ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 4163ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Determine if any of our values are mapped. This is uncommon, so we want 4172b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org // to avoid the range scan if not. 4183ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid bool MustMapCurValNos = false; 4193ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid unsigned NumVals = getNumValNums(); 4203ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid unsigned NumNewVals = NewVNInfo.size(); 4212b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org for (unsigned i = 0; i != NumVals; ++i) { 4222b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org unsigned LHSValID = LHSValNoAssignments[i]; 4232b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org if (i != LHSValID || 4242b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i))) { 4253ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid MustMapCurValNos = true; 4263ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid break; 4273ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 4283ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 4293ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 4303ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // If we have to apply a mapping to our base range assignment, rewrite it now. 4313ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (MustMapCurValNos && !empty()) { 4323ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Map the first live range. 4333ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 4343ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid iterator OutIt = begin(); 4353ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]]; 4363ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid for (iterator I = llvm::next(OutIt), E = end(); I != E; ++I) { 4373ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid VNInfo* nextValNo = NewVNInfo[LHSValNoAssignments[I->valno->id]]; 4383ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid assert(nextValNo != 0 && "Huh?"); 4393ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 4403ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // If this live range has the same value # as its immediate predecessor, 4413ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // and if they are neighbors, remove one Segment. This happens when we 4423ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // have [0,4:0)[4,7:1) and map 0/1 onto the same value #. 4433ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (OutIt->valno == nextValNo && OutIt->end == I->start) { 4443ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid OutIt->end = I->end; 4453ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } else { 4463ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Didn't merge. Move OutIt to the next segment, 4473ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid ++OutIt; 4483ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid OutIt->valno = nextValNo; 4493ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid if (OutIt != I) { 4503ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid OutIt->start = I->start; 4513ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid OutIt->end = I->end; 4523ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 4533ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 4543ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 4553ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // If we merge some segments, chop off the end. 4563ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid ++OutIt; 4573ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid segments.erase(OutIt, end()); 4583ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid } 4593ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid 4603ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // Rewrite Other values before changing the VNInfo ids. 4613ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // This can leave Other in an invalid state because we're not coalescing 4623ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // touching segments that now have identical values. That's OK since Other is 4633ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid // not supposed to be valid after calling join(); 4643ebdb1bd7ae38bf0fb205dfaa2f5fde3d67ea141nealsid for (iterator I = Other.begin(), E = Other.end(); I != E; ++I) 4652b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org I->valno = NewVNInfo[RHSValNoAssignments[I->valno->id]]; 4662b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org 4672b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org // Update val# info. Renumber them and make sure they all belong to this 4682b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org // LiveRange now. Also remove dead val#'s. 4692b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org unsigned NumValNos = 0; 4702b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org for (unsigned i = 0; i < NumNewVals; ++i) { 4712b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org VNInfo *VNI = NewVNInfo[i]; 4722b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org if (VNI) { 4732b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org if (NumValNos >= NumVals) 4742b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org valnos.push_back(VNI); 4752b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org else 4762b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org valnos[NumValNos] = VNI; 4772b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org VNI->id = NumValNos++; // Renumber val#. 4782b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org } 4792b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org } 4802b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org if (NumNewVals < NumVals) 4812b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org valnos.resize(NumNewVals); // shrinkify 4822b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org 4832b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org // Okay, now insert the RHS live segments into the LHS. 4842b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org LiveRangeUpdater Updater(this); 4852b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org for (iterator I = Other.begin(), E = Other.end(); I != E; ++I) 4862b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org Updater.add(*I); 4872b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org} 4882b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org 4892b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org/// Merge all of the segments in RHS into this live range as the specified 4902b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org/// value number. The segments in RHS are allowed to overlap with segments in 4912b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org/// the current range, but only if the overlapping segments have the 4922b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org/// specified value number. 4932b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.orgvoid LiveRange::MergeSegmentsInAsValue(const LiveRange &RHS, 4942b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org VNInfo *LHSValNo) { 4952b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org LiveRangeUpdater Updater(this); 4962b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) 4972b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org Updater.add(I->start, I->end, LHSValNo); 4982b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org} 4992b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org 5002b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org/// MergeValueInAsValue - Merge all of the live segments of a specific val# 5012b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org/// in RHS into this live range as the specified value number. 5022b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org/// The segments in RHS are allowed to overlap with segments in the 5032b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org/// current range, it will replace the value numbers of the overlaped 5042b18e3c814d6e233ef2205026b9bc521044822abqsr@chromium.org/// segments with the specified value number. 505void LiveRange::MergeValueInAsValue(const LiveRange &RHS, 506 const VNInfo *RHSValNo, 507 VNInfo *LHSValNo) { 508 LiveRangeUpdater Updater(this); 509 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) 510 if (I->valno == RHSValNo) 511 Updater.add(I->start, I->end, LHSValNo); 512} 513 514/// MergeValueNumberInto - This method is called when two value nubmers 515/// are found to be equivalent. This eliminates V1, replacing all 516/// segments with the V1 value number with the V2 value number. This can 517/// cause merging of V1/V2 values numbers and compaction of the value space. 518VNInfo *LiveRange::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) { 519 assert(V1 != V2 && "Identical value#'s are always equivalent!"); 520 521 // This code actually merges the (numerically) larger value number into the 522 // smaller value number, which is likely to allow us to compactify the value 523 // space. The only thing we have to be careful of is to preserve the 524 // instruction that defines the result value. 525 526 // Make sure V2 is smaller than V1. 527 if (V1->id < V2->id) { 528 V1->copyFrom(*V2); 529 std::swap(V1, V2); 530 } 531 532 // Merge V1 segments into V2. 533 for (iterator I = begin(); I != end(); ) { 534 iterator S = I++; 535 if (S->valno != V1) continue; // Not a V1 Segment. 536 537 // Okay, we found a V1 live range. If it had a previous, touching, V2 live 538 // range, extend it. 539 if (S != begin()) { 540 iterator Prev = S-1; 541 if (Prev->valno == V2 && Prev->end == S->start) { 542 Prev->end = S->end; 543 544 // Erase this live-range. 545 segments.erase(S); 546 I = Prev+1; 547 S = Prev; 548 } 549 } 550 551 // Okay, now we have a V1 or V2 live range that is maximally merged forward. 552 // Ensure that it is a V2 live-range. 553 S->valno = V2; 554 555 // If we can merge it into later V2 segments, do so now. We ignore any 556 // following V1 segments, as they will be merged in subsequent iterations 557 // of the loop. 558 if (I != end()) { 559 if (I->start == S->end && I->valno == V2) { 560 S->end = I->end; 561 segments.erase(I); 562 I = S+1; 563 } 564 } 565 } 566 567 // Now that V1 is dead, remove it. 568 markValNoForDeletion(V1); 569 570 return V2; 571} 572 573unsigned LiveInterval::getSize() const { 574 unsigned Sum = 0; 575 for (const_iterator I = begin(), E = end(); I != E; ++I) 576 Sum += I->start.distance(I->end); 577 return Sum; 578} 579 580raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange::Segment &S) { 581 return os << '[' << S.start << ',' << S.end << ':' << S.valno->id << ")"; 582} 583 584#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 585void LiveRange::Segment::dump() const { 586 dbgs() << *this << "\n"; 587} 588#endif 589 590void LiveRange::print(raw_ostream &OS) const { 591 if (empty()) 592 OS << "EMPTY"; 593 else { 594 for (const_iterator I = begin(), E = end(); I != E; ++I) { 595 OS << *I; 596 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo"); 597 } 598 } 599 600 // Print value number info. 601 if (getNumValNums()) { 602 OS << " "; 603 unsigned vnum = 0; 604 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e; 605 ++i, ++vnum) { 606 const VNInfo *vni = *i; 607 if (vnum) OS << " "; 608 OS << vnum << "@"; 609 if (vni->isUnused()) { 610 OS << "x"; 611 } else { 612 OS << vni->def; 613 if (vni->isPHIDef()) 614 OS << "-phi"; 615 } 616 } 617 } 618} 619 620#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 621void LiveRange::dump() const { 622 dbgs() << *this << "\n"; 623} 624#endif 625 626#ifndef NDEBUG 627void LiveRange::verify() const { 628 for (const_iterator I = begin(), E = end(); I != E; ++I) { 629 assert(I->start.isValid()); 630 assert(I->end.isValid()); 631 assert(I->start < I->end); 632 assert(I->valno != 0); 633 assert(I->valno->id < valnos.size()); 634 assert(I->valno == valnos[I->valno->id]); 635 if (llvm::next(I) != E) { 636 assert(I->end <= llvm::next(I)->start); 637 if (I->end == llvm::next(I)->start) 638 assert(I->valno != llvm::next(I)->valno); 639 } 640 } 641} 642#endif 643 644 645//===----------------------------------------------------------------------===// 646// LiveRangeUpdater class 647//===----------------------------------------------------------------------===// 648// 649// The LiveRangeUpdater class always maintains these invariants: 650// 651// - When LastStart is invalid, Spills is empty and the iterators are invalid. 652// This is the initial state, and the state created by flush(). 653// In this state, isDirty() returns false. 654// 655// Otherwise, segments are kept in three separate areas: 656// 657// 1. [begin; WriteI) at the front of LR. 658// 2. [ReadI; end) at the back of LR. 659// 3. Spills. 660// 661// - LR.begin() <= WriteI <= ReadI <= LR.end(). 662// - Segments in all three areas are fully ordered and coalesced. 663// - Segments in area 1 precede and can't coalesce with segments in area 2. 664// - Segments in Spills precede and can't coalesce with segments in area 2. 665// - No coalescing is possible between segments in Spills and segments in area 666// 1, and there are no overlapping segments. 667// 668// The segments in Spills are not ordered with respect to the segments in area 669// 1. They need to be merged. 670// 671// When they exist, Spills.back().start <= LastStart, 672// and WriteI[-1].start <= LastStart. 673 674void LiveRangeUpdater::print(raw_ostream &OS) const { 675 if (!isDirty()) { 676 if (LR) 677 OS << "Clean updater: " << *LR << '\n'; 678 else 679 OS << "Null updater.\n"; 680 return; 681 } 682 assert(LR && "Can't have null LR in dirty updater."); 683 OS << " updater with gap = " << (ReadI - WriteI) 684 << ", last start = " << LastStart 685 << ":\n Area 1:"; 686 for (LiveRange::const_iterator I = LR->begin(); I != WriteI; ++I) 687 OS << ' ' << *I; 688 OS << "\n Spills:"; 689 for (unsigned I = 0, E = Spills.size(); I != E; ++I) 690 OS << ' ' << Spills[I]; 691 OS << "\n Area 2:"; 692 for (LiveRange::const_iterator I = ReadI, E = LR->end(); I != E; ++I) 693 OS << ' ' << *I; 694 OS << '\n'; 695} 696 697void LiveRangeUpdater::dump() const 698{ 699 print(errs()); 700} 701 702// Determine if A and B should be coalesced. 703static inline bool coalescable(const LiveRange::Segment &A, 704 const LiveRange::Segment &B) { 705 assert(A.start <= B.start && "Unordered live segments."); 706 if (A.end == B.start) 707 return A.valno == B.valno; 708 if (A.end < B.start) 709 return false; 710 assert(A.valno == B.valno && "Cannot overlap different values"); 711 return true; 712} 713 714void LiveRangeUpdater::add(LiveRange::Segment Seg) { 715 assert(LR && "Cannot add to a null destination"); 716 717 // Flush the state if Start moves backwards. 718 if (!LastStart.isValid() || LastStart > Seg.start) { 719 if (isDirty()) 720 flush(); 721 // This brings us to an uninitialized state. Reinitialize. 722 assert(Spills.empty() && "Leftover spilled segments"); 723 WriteI = ReadI = LR->begin(); 724 } 725 726 // Remember start for next time. 727 LastStart = Seg.start; 728 729 // Advance ReadI until it ends after Seg.start. 730 LiveRange::iterator E = LR->end(); 731 if (ReadI != E && ReadI->end <= Seg.start) { 732 // First try to close the gap between WriteI and ReadI with spills. 733 if (ReadI != WriteI) 734 mergeSpills(); 735 // Then advance ReadI. 736 if (ReadI == WriteI) 737 ReadI = WriteI = LR->find(Seg.start); 738 else 739 while (ReadI != E && ReadI->end <= Seg.start) 740 *WriteI++ = *ReadI++; 741 } 742 743 assert(ReadI == E || ReadI->end > Seg.start); 744 745 // Check if the ReadI segment begins early. 746 if (ReadI != E && ReadI->start <= Seg.start) { 747 assert(ReadI->valno == Seg.valno && "Cannot overlap different values"); 748 // Bail if Seg is completely contained in ReadI. 749 if (ReadI->end >= Seg.end) 750 return; 751 // Coalesce into Seg. 752 Seg.start = ReadI->start; 753 ++ReadI; 754 } 755 756 // Coalesce as much as possible from ReadI into Seg. 757 while (ReadI != E && coalescable(Seg, *ReadI)) { 758 Seg.end = std::max(Seg.end, ReadI->end); 759 ++ReadI; 760 } 761 762 // Try coalescing Spills.back() into Seg. 763 if (!Spills.empty() && coalescable(Spills.back(), Seg)) { 764 Seg.start = Spills.back().start; 765 Seg.end = std::max(Spills.back().end, Seg.end); 766 Spills.pop_back(); 767 } 768 769 // Try coalescing Seg into WriteI[-1]. 770 if (WriteI != LR->begin() && coalescable(WriteI[-1], Seg)) { 771 WriteI[-1].end = std::max(WriteI[-1].end, Seg.end); 772 return; 773 } 774 775 // Seg doesn't coalesce with anything, and needs to be inserted somewhere. 776 if (WriteI != ReadI) { 777 *WriteI++ = Seg; 778 return; 779 } 780 781 // Finally, append to LR or Spills. 782 if (WriteI == E) { 783 LR->segments.push_back(Seg); 784 WriteI = ReadI = LR->end(); 785 } else 786 Spills.push_back(Seg); 787} 788 789// Merge as many spilled segments as possible into the gap between WriteI 790// and ReadI. Advance WriteI to reflect the inserted instructions. 791void LiveRangeUpdater::mergeSpills() { 792 // Perform a backwards merge of Spills and [SpillI;WriteI). 793 size_t GapSize = ReadI - WriteI; 794 size_t NumMoved = std::min(Spills.size(), GapSize); 795 LiveRange::iterator Src = WriteI; 796 LiveRange::iterator Dst = Src + NumMoved; 797 LiveRange::iterator SpillSrc = Spills.end(); 798 LiveRange::iterator B = LR->begin(); 799 800 // This is the new WriteI position after merging spills. 801 WriteI = Dst; 802 803 // Now merge Src and Spills backwards. 804 while (Src != Dst) { 805 if (Src != B && Src[-1].start > SpillSrc[-1].start) 806 *--Dst = *--Src; 807 else 808 *--Dst = *--SpillSrc; 809 } 810 assert(NumMoved == size_t(Spills.end() - SpillSrc)); 811 Spills.erase(SpillSrc, Spills.end()); 812} 813 814void LiveRangeUpdater::flush() { 815 if (!isDirty()) 816 return; 817 // Clear the dirty state. 818 LastStart = SlotIndex(); 819 820 assert(LR && "Cannot add to a null destination"); 821 822 // Nothing to merge? 823 if (Spills.empty()) { 824 LR->segments.erase(WriteI, ReadI); 825 LR->verify(); 826 return; 827 } 828 829 // Resize the WriteI - ReadI gap to match Spills. 830 size_t GapSize = ReadI - WriteI; 831 if (GapSize < Spills.size()) { 832 // The gap is too small. Make some room. 833 size_t WritePos = WriteI - LR->begin(); 834 LR->segments.insert(ReadI, Spills.size() - GapSize, LiveRange::Segment()); 835 // This also invalidated ReadI, but it is recomputed below. 836 WriteI = LR->begin() + WritePos; 837 } else { 838 // Shrink the gap if necessary. 839 LR->segments.erase(WriteI + Spills.size(), ReadI); 840 } 841 ReadI = WriteI + Spills.size(); 842 mergeSpills(); 843 LR->verify(); 844} 845 846unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval *LI) { 847 // Create initial equivalence classes. 848 EqClass.clear(); 849 EqClass.grow(LI->getNumValNums()); 850 851 const VNInfo *used = 0, *unused = 0; 852 853 // Determine connections. 854 for (LiveInterval::const_vni_iterator I = LI->vni_begin(), E = LI->vni_end(); 855 I != E; ++I) { 856 const VNInfo *VNI = *I; 857 // Group all unused values into one class. 858 if (VNI->isUnused()) { 859 if (unused) 860 EqClass.join(unused->id, VNI->id); 861 unused = VNI; 862 continue; 863 } 864 used = VNI; 865 if (VNI->isPHIDef()) { 866 const MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def); 867 assert(MBB && "Phi-def has no defining MBB"); 868 // Connect to values live out of predecessors. 869 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), 870 PE = MBB->pred_end(); PI != PE; ++PI) 871 if (const VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(*PI))) 872 EqClass.join(VNI->id, PVNI->id); 873 } else { 874 // Normal value defined by an instruction. Check for two-addr redef. 875 // FIXME: This could be coincidental. Should we really check for a tied 876 // operand constraint? 877 // Note that VNI->def may be a use slot for an early clobber def. 878 if (const VNInfo *UVNI = LI->getVNInfoBefore(VNI->def)) 879 EqClass.join(VNI->id, UVNI->id); 880 } 881 } 882 883 // Lump all the unused values in with the last used value. 884 if (used && unused) 885 EqClass.join(used->id, unused->id); 886 887 EqClass.compress(); 888 return EqClass.getNumClasses(); 889} 890 891void ConnectedVNInfoEqClasses::Distribute(LiveInterval *LIV[], 892 MachineRegisterInfo &MRI) { 893 assert(LIV[0] && "LIV[0] must be set"); 894 LiveInterval &LI = *LIV[0]; 895 896 // Rewrite instructions. 897 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LI.reg), 898 RE = MRI.reg_end(); RI != RE;) { 899 MachineOperand &MO = RI.getOperand(); 900 MachineInstr *MI = MO.getParent(); 901 ++RI; 902 // DBG_VALUE instructions don't have slot indexes, so get the index of the 903 // instruction before them. 904 // Normally, DBG_VALUE instructions are removed before this function is 905 // called, but it is not a requirement. 906 SlotIndex Idx; 907 if (MI->isDebugValue()) 908 Idx = LIS.getSlotIndexes()->getIndexBefore(MI); 909 else 910 Idx = LIS.getInstructionIndex(MI); 911 LiveQueryResult LRQ = LI.Query(Idx); 912 const VNInfo *VNI = MO.readsReg() ? LRQ.valueIn() : LRQ.valueDefined(); 913 // In the case of an <undef> use that isn't tied to any def, VNI will be 914 // NULL. If the use is tied to a def, VNI will be the defined value. 915 if (!VNI) 916 continue; 917 MO.setReg(LIV[getEqClass(VNI)]->reg); 918 } 919 920 // Move runs to new intervals. 921 LiveInterval::iterator J = LI.begin(), E = LI.end(); 922 while (J != E && EqClass[J->valno->id] == 0) 923 ++J; 924 for (LiveInterval::iterator I = J; I != E; ++I) { 925 if (unsigned eq = EqClass[I->valno->id]) { 926 assert((LIV[eq]->empty() || LIV[eq]->expiredAt(I->start)) && 927 "New intervals should be empty"); 928 LIV[eq]->segments.push_back(*I); 929 } else 930 *J++ = *I; 931 } 932 LI.segments.erase(J, E); 933 934 // Transfer VNInfos to their new owners and renumber them. 935 unsigned j = 0, e = LI.getNumValNums(); 936 while (j != e && EqClass[j] == 0) 937 ++j; 938 for (unsigned i = j; i != e; ++i) { 939 VNInfo *VNI = LI.getValNumInfo(i); 940 if (unsigned eq = EqClass[i]) { 941 VNI->id = LIV[eq]->getNumValNums(); 942 LIV[eq]->valnos.push_back(VNI); 943 } else { 944 VNI->id = j; 945 LI.valnos[j++] = VNI; 946 } 947 } 948 LI.valnos.resize(j); 949} 950