LiveInterval.cpp revision 34729256e8058d4106706e9feb2dfad7893502d1
1d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan//===-- LiveInterval.cpp - Live Interval Representation -------------------===// 2d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// 3d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// The LLVM Compiler Infrastructure 4d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// 5d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// This file was developed by the LLVM research group and is distributed under 6d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// the University of Illinois Open Source License. See LICENSE.TXT for details. 7d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// 8d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan//===----------------------------------------------------------------------===// 9d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// 10d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// This file implements the LiveRange and LiveInterval classes. Given some 11d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// numbering of each the machine instructions an interval [i, j) is said to be a 12d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// live interval for register v if there is no instruction with number j' > j 13d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// such that v is live at j' abd there is no instruction with number i' < i such 14d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// that v is live at i'. In this implementation intervals can have holes, 15d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// i.e. an interval might look like [1,20), [50,65), [1000,1001). Each 16d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// individual range is represented as an instance of LiveRange, and the whole 1761131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger// interval is represented as an instance of LiveInterval. 187c788888872233748da10a8177a9a1eb176c1bc8Peter Collingbourne// 19d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan//===----------------------------------------------------------------------===// 20d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 21d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan#include "llvm/CodeGen/LiveInterval.h" 22d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan#include "llvm/ADT/STLExtras.h" 23d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan#include "llvm/Support/Streams.h" 24d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan#include "llvm/Target/MRegisterInfo.h" 25d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan#include <algorithm> 26d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan#include <map> 27d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan#include <ostream> 28d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callananusing namespace llvm; 29d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 30d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// An example for liveAt(): 31d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// 32d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// this = [1,4), liveAt(0) will return false. The instruction defining this 33d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// spans slots [0,3]. The interval belongs to an spilled definition of the 34d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// variable it represents. This is because slot 1 is used (def slot) and spans 3500f94955efa367a265187456a140a177610e3f3bCraig Topper// up to slot 3 (store slot). 36d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// 37d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callananbool LiveInterval::liveAt(unsigned I) const { 3800f94955efa367a265187456a140a177610e3f3bCraig Topper Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I); 39d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 40d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan if (r == ranges.begin()) 41d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan return false; 420c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar 430c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar --r; 4435c33bd772b3cfb34fdc6b5c9171f955454d0043Chris Lattner return r->contains(I); 45d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan} 46d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 47d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// overlaps - Return true if the intersection of the two live intervals is 48d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// not empty. 49d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// 50d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// An example for overlaps(): 51d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// 52d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// 0: A = ... 530c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar// 4: B = ... 540c7f116bb6950ef819323d855415b2f2b0aad987Pirama Arumuga Nainar// 8: C = A + B ;; last use of A 55d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// 5600f94955efa367a265187456a140a177610e3f3bCraig Topper// The live intervals should look like: 5700f94955efa367a265187456a140a177610e3f3bCraig Topper// 58d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// A = [3, 11) 59d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// B = [7, x) 604d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner// C = [11, y) 61d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// 62d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// A->overlaps(C) should return false since we want to be able to join 63d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan// A and C. 644d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner// 65d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callananbool LiveInterval::overlapsFrom(const LiveInterval& other, 6600f94955efa367a265187456a140a177610e3f3bCraig Topper const_iterator StartPos) const { 67d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan const_iterator i = begin(); 68d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan const_iterator ie = end(); 694d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner const_iterator j = StartPos; 704d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner const_iterator je = other.end(); 714d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner 724d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner assert((StartPos->start <= i->start || StartPos == other.begin()) && 734d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner StartPos != other.end() && "Bogus start position hint!"); 744d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner 7536b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (i->start < j->start) { 764d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner i = std::upper_bound(i, ie, j->start); 774d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner if (i != ranges.begin()) --i; 784d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner } else if (j->start < i->start) { 794d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner ++StartPos; 804d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner if (StartPos != other.end() && StartPos->start <= i->start) { 814d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner assert(StartPos < other.end() && i < end()); 824d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner j = std::upper_bound(j, je, i->start); 834d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner if (j != other.ranges.begin()) --j; 844d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner } 854d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner } else { 864d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner return true; 87d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan } 884d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner 89d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan if (j == je) return false; 90d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 91d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan while (i != ie) { 9236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines if (i->start > j->start) { 9300f94955efa367a265187456a140a177610e3f3bCraig Topper std::swap(i, j); 94d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan std::swap(ie, je); 95d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan } 96d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 9761131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger if (i->end > j->start) 9861131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger return true; 99d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan ++i; 100d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan } 101d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 102d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan return false; 103d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan} 104d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 1054d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner/// extendIntervalEndTo - This method is used when we want to extend the range 106d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan/// specified by I to end at the specified endpoint. To do this, we should 107d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan/// merge and eliminate all ranges that this will overlap with. The iterator is 108d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan/// not invalidated. 109d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callananvoid LiveInterval::extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd) { 11000f94955efa367a265187456a140a177610e3f3bCraig Topper assert(I != ranges.end() && "Not a valid interval!"); 111d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan VNInfo *ValNo = I->valno; 112d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan unsigned OldEnd = I->end; 113d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 114d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan // Search for the first interval that we can't merge with. 115d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan Ranges::iterator MergeTo = next(I); 116d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) { 117d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan assert(MergeTo->valno == ValNo && "Cannot merge with differing values!"); 118d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan } 119d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 12000f94955efa367a265187456a140a177610e3f3bCraig Topper // If NewEnd was in the middle of an interval, make sure to get its endpoint. 121d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan I->end = std::max(NewEnd, prior(MergeTo)->end); 122d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 123d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan // Erase any dead ranges. 124d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan ranges.erase(next(I), MergeTo); 12500f94955efa367a265187456a140a177610e3f3bCraig Topper 126d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan // Update kill info. 127d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan removeKills(ValNo, OldEnd, I->end-1); 128d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 129d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan // If the newly formed range now touches the range after it and if they have 13000f94955efa367a265187456a140a177610e3f3bCraig Topper // the same value number, merge the two ranges into one range. 131d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan Ranges::iterator Next = next(I); 132d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) { 133d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan I->end = Next->end; 134d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan ranges.erase(Next); 135d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan } 13661131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger} 13761131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger 13800f94955efa367a265187456a140a177610e3f3bCraig Topper 139d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan/// extendIntervalStartTo - This method is used when we want to extend the range 140d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan/// specified by I to start at the specified endpoint. To do this, we should 141d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan/// merge and eliminate all ranges that this will overlap with. 142d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean CallananLiveInterval::Ranges::iterator 14361131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg SonnenbergerLiveInterval::extendIntervalStartTo(Ranges::iterator I, unsigned NewStart) { 14461131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger assert(I != ranges.end() && "Not a valid interval!"); 14500f94955efa367a265187456a140a177610e3f3bCraig Topper VNInfo *ValNo = I->valno; 146d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 147d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan // Search for the first interval that we can't merge with. 148d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan Ranges::iterator MergeTo = I; 149d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan do { 150d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan if (MergeTo == ranges.begin()) { 151d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan I->start = NewStart; 15261131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger ranges.erase(MergeTo, I); 153d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan return I; 15400f94955efa367a265187456a140a177610e3f3bCraig Topper } 155d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan assert(MergeTo->valno == ValNo && "Cannot merge with differing values!"); 15661131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger --MergeTo; 15761131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger } while (NewStart <= MergeTo->start); 158d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 159d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan // If we start in the middle of another interval, just delete a range and 160d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan // extend that interval. 16161131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) { 16261131ab15fd593a2e295d79fe2714e7bc21f2ec8Joerg Sonnenberger MergeTo->end = I->end; 16300f94955efa367a265187456a140a177610e3f3bCraig Topper } else { 164d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan // Otherwise, extend the interval right after. 165d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan ++MergeTo; 1666948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar MergeTo->start = NewStart; 1676948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar MergeTo->end = I->end; 168d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan } 169d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 170c240bb0ede0541426254d0e0dc81d891beda4b22Chris Lattner ranges.erase(next(MergeTo), next(I)); 171c240bb0ede0541426254d0e0dc81d891beda4b22Chris Lattner return MergeTo; 17200f94955efa367a265187456a140a177610e3f3bCraig Topper} 1734d43d0fd996a01c2cd21fd51082bc1bba783ef3cChris Lattner 1746948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga NainarLiveInterval::iterator 1756948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga NainarLiveInterval::addRangeFrom(LiveRange LR, iterator From) { 176d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan unsigned Start = LR.start, End = LR.end; 177d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan iterator it = std::upper_bound(From, ranges.end(), Start); 178d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan 179d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan // If the inserted interval starts in the middle or right at the end of 18000f94955efa367a265187456a140a177610e3f3bCraig Topper // another interval, just extend that interval to contain the range of LR. 1816948897e478cbd66626159776a8017b3c18579b9Pirama Arumuga Nainar if (it != ranges.begin()) { 182d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan iterator B = prior(it); 183d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan if (LR.valno == B->valno) { 184d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan if (B->start <= Start && B->end >= Start) { 185d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan extendIntervalEndTo(B, End); 186d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan return B; 187d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan } 188d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan } else { 189d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan // Check to make sure that we are not overlapping two live ranges with 19000f94955efa367a265187456a140a177610e3f3bCraig Topper // different valno's. 191d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan assert(B->end <= Start && 192d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan "Cannot overlap two LiveRanges with differing ValID's" 193d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan " (did you def the same reg twice in a MachineInstr?)"); 194d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan } 195d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan } 196349d6a70a1f17ecfd0695fae1b596cafdd15762eCraig Topper 197d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan // Otherwise, if this range ends in the middle of, or right next to, another 198d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan // interval, merge it into that interval. 199d32c02f3145c8d5114c9bd367e9ff15f4aed2e15Sean Callanan if (it != ranges.end()) 200d0bc7f060ece77c670794ef60f7052e2ff1847c9Sean Callanan if (LR.valno == it->valno) { 201 if (it->start <= End) { 202 it = extendIntervalStartTo(it, Start); 203 204 // If LR is a complete superset of an interval, we may need to grow its 205 // endpoint as well. 206 if (End > it->end) 207 extendIntervalEndTo(it, End); 208 return it; 209 } 210 } else { 211 // Check to make sure that we are not overlapping two live ranges with 212 // different valno's. 213 assert(it->start >= End && 214 "Cannot overlap two LiveRanges with differing ValID's"); 215 } 216 217 // Otherwise, this is just a new range that doesn't interact with anything. 218 // Insert it. 219 return ranges.insert(it, LR); 220} 221 222 223/// removeRange - Remove the specified range from this interval. Note that 224/// the range must already be in this interval in its entirety. 225void LiveInterval::removeRange(unsigned Start, unsigned End) { 226 // Find the LiveRange containing this span. 227 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start); 228 assert(I != ranges.begin() && "Range is not in interval!"); 229 --I; 230 assert(I->contains(Start) && I->contains(End-1) && 231 "Range is not entirely in interval!"); 232 233 // If the span we are removing is at the start of the LiveRange, adjust it. 234 if (I->start == Start) { 235 if (I->end == End) { 236 removeKills(I->valno, Start, End); 237 ranges.erase(I); // Removed the whole LiveRange. 238 } else 239 I->start = End; 240 return; 241 } 242 243 // Otherwise if the span we are removing is at the end of the LiveRange, 244 // adjust the other way. 245 if (I->end == End) { 246 removeKills(I->valno, Start, End); 247 I->end = Start; 248 return; 249 } 250 251 // Otherwise, we are splitting the LiveRange into two pieces. 252 unsigned OldEnd = I->end; 253 I->end = Start; // Trim the old interval. 254 255 // Insert the new one. 256 ranges.insert(next(I), LiveRange(End, OldEnd, I->valno)); 257} 258 259/// getLiveRangeContaining - Return the live range that contains the 260/// specified index, or null if there is none. 261LiveInterval::const_iterator 262LiveInterval::FindLiveRangeContaining(unsigned Idx) const { 263 const_iterator It = std::upper_bound(begin(), end(), Idx); 264 if (It != ranges.begin()) { 265 --It; 266 if (It->contains(Idx)) 267 return It; 268 } 269 270 return end(); 271} 272 273LiveInterval::iterator 274LiveInterval::FindLiveRangeContaining(unsigned Idx) { 275 iterator It = std::upper_bound(begin(), end(), Idx); 276 if (It != begin()) { 277 --It; 278 if (It->contains(Idx)) 279 return It; 280 } 281 282 return end(); 283} 284 285/// join - Join two live intervals (this, and other) together. This applies 286/// mappings to the value numbers in the LHS/RHS intervals as specified. If 287/// the intervals are not joinable, this aborts. 288void LiveInterval::join(LiveInterval &Other, const int *LHSValNoAssignments, 289 const int *RHSValNoAssignments, 290 SmallVector<VNInfo*, 16> &NewVNInfo) { 291 // Determine if any of our live range values are mapped. This is uncommon, so 292 // we want to avoid the interval scan if not. 293 bool MustMapCurValNos = false; 294 unsigned NumVals = getNumValNums(); 295 unsigned NumNewVals = NewVNInfo.size(); 296 for (unsigned i = 0; i != NumVals; ++i) { 297 unsigned LHSValID = LHSValNoAssignments[i]; 298 if (i != LHSValID || 299 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i))) 300 MustMapCurValNos = true; 301 } 302 303 // If we have to apply a mapping to our base interval assignment, rewrite it 304 // now. 305 if (MustMapCurValNos) { 306 // Map the first live range. 307 iterator OutIt = begin(); 308 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]]; 309 ++OutIt; 310 for (iterator I = OutIt, E = end(); I != E; ++I) { 311 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]]; 312 313 // If this live range has the same value # as its immediate predecessor, 314 // and if they are neighbors, remove one LiveRange. This happens when we 315 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #. 316 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) { 317 (OutIt-1)->end = OutIt->end; 318 } else { 319 if (I != OutIt) { 320 OutIt->start = I->start; 321 OutIt->end = I->end; 322 } 323 324 // Didn't merge, on to the next one. 325 ++OutIt; 326 } 327 } 328 329 // If we merge some live ranges, chop off the end. 330 ranges.erase(OutIt, end()); 331 } 332 333 // Remember assignements because val# ids are changing. 334 SmallVector<unsigned, 16> OtherAssignments; 335 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I) 336 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]); 337 338 // Update val# info. Renumber them and make sure they all belong to this 339 // LiveInterval now. Also remove dead val#'s. 340 unsigned NumValNos = 0; 341 for (unsigned i = 0; i < NumNewVals; ++i) { 342 VNInfo *VNI = NewVNInfo[i]; 343 if (VNI) { 344 if (i >= NumVals) 345 valnos.push_back(VNI); 346 else 347 valnos[NumValNos] = VNI; 348 VNI->id = NumValNos++; // Renumber val#. 349 } 350 } 351 if (NumNewVals < NumVals) 352 valnos.resize(NumNewVals); // shrinkify 353 354 // Okay, now insert the RHS live ranges into the LHS. 355 iterator InsertPos = begin(); 356 unsigned RangeNo = 0; 357 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) { 358 // Map the valno in the other live range to the current live range. 359 I->valno = NewVNInfo[OtherAssignments[RangeNo]]; 360 assert(I->valno && "Adding a dead range?"); 361 InsertPos = addRangeFrom(*I, InsertPos); 362 } 363 364 weight += Other.weight; 365 if (Other.preference && !preference) 366 preference = Other.preference; 367} 368 369/// MergeRangesInAsValue - Merge all of the intervals in RHS into this live 370/// interval as the specified value number. The LiveRanges in RHS are 371/// allowed to overlap with LiveRanges in the current interval, but only if 372/// the overlapping LiveRanges have the specified value number. 373void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS, 374 VNInfo *LHSValNo) { 375 // TODO: Make this more efficient. 376 iterator InsertPos = begin(); 377 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) { 378 // Map the valno in the other live range to the current live range. 379 LiveRange Tmp = *I; 380 Tmp.valno = LHSValNo; 381 InsertPos = addRangeFrom(Tmp, InsertPos); 382 } 383} 384 385 386/// MergeValueInAsValue - Merge all of the live ranges of a specific val# 387/// in RHS into this live interval as the specified value number. 388/// The LiveRanges in RHS are allowed to overlap with LiveRanges in the 389/// current interval, but only if the overlapping LiveRanges have the 390/// specified value number. 391void LiveInterval::MergeValueInAsValue(const LiveInterval &RHS, 392 const VNInfo *RHSValNo, VNInfo *LHSValNo) { 393 // TODO: Make this more efficient. 394 iterator InsertPos = begin(); 395 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) { 396 if (I->valno != RHSValNo) 397 continue; 398 // Map the valno in the other live range to the current live range. 399 LiveRange Tmp = *I; 400 Tmp.valno = LHSValNo; 401 InsertPos = addRangeFrom(Tmp, InsertPos); 402 } 403} 404 405 406/// MergeInClobberRanges - For any live ranges that are not defined in the 407/// current interval, but are defined in the Clobbers interval, mark them 408/// used with an unknown definition value. 409void LiveInterval::MergeInClobberRanges(const LiveInterval &Clobbers, 410 BumpPtrAllocator &VNInfoAllocator) { 411 if (Clobbers.begin() == Clobbers.end()) return; 412 413 // Find a value # to use for the clobber ranges. If there is already a value# 414 // for unknown values, use it. 415 // FIXME: Use a single sentinal number for these! 416 VNInfo *ClobberValNo = getNextValue(~0U, 0, VNInfoAllocator); 417 418 iterator IP = begin(); 419 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) { 420 unsigned Start = I->start, End = I->end; 421 IP = std::upper_bound(IP, end(), Start); 422 423 // If the start of this range overlaps with an existing liverange, trim it. 424 if (IP != begin() && IP[-1].end > Start) { 425 Start = IP[-1].end; 426 // Trimmed away the whole range? 427 if (Start >= End) continue; 428 } 429 // If the end of this range overlaps with an existing liverange, trim it. 430 if (IP != end() && End > IP->start) { 431 End = IP->start; 432 // If this trimmed away the whole range, ignore it. 433 if (Start == End) continue; 434 } 435 436 // Insert the clobber interval. 437 IP = addRangeFrom(LiveRange(Start, End, ClobberValNo), IP); 438 } 439} 440 441/// MergeValueNumberInto - This method is called when two value nubmers 442/// are found to be equivalent. This eliminates V1, replacing all 443/// LiveRanges with the V1 value number with the V2 value number. This can 444/// cause merging of V1/V2 values numbers and compaction of the value space. 445void LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) { 446 assert(V1 != V2 && "Identical value#'s are always equivalent!"); 447 448 // This code actually merges the (numerically) larger value number into the 449 // smaller value number, which is likely to allow us to compactify the value 450 // space. The only thing we have to be careful of is to preserve the 451 // instruction that defines the result value. 452 453 // Make sure V2 is smaller than V1. 454 if (V1->id < V2->id) { 455 copyValNumInfo(V1, V2); 456 std::swap(V1, V2); 457 } 458 459 // Merge V1 live ranges into V2. 460 for (iterator I = begin(); I != end(); ) { 461 iterator LR = I++; 462 if (LR->valno != V1) continue; // Not a V1 LiveRange. 463 464 // Okay, we found a V1 live range. If it had a previous, touching, V2 live 465 // range, extend it. 466 if (LR != begin()) { 467 iterator Prev = LR-1; 468 if (Prev->valno == V2 && Prev->end == LR->start) { 469 Prev->end = LR->end; 470 471 // Erase this live-range. 472 ranges.erase(LR); 473 I = Prev+1; 474 LR = Prev; 475 } 476 } 477 478 // Okay, now we have a V1 or V2 live range that is maximally merged forward. 479 // Ensure that it is a V2 live-range. 480 LR->valno = V2; 481 482 // If we can merge it into later V2 live ranges, do so now. We ignore any 483 // following V1 live ranges, as they will be merged in subsequent iterations 484 // of the loop. 485 if (I != end()) { 486 if (I->start == LR->end && I->valno == V2) { 487 LR->end = I->end; 488 ranges.erase(I); 489 I = LR+1; 490 } 491 } 492 } 493 494 // Now that V1 is dead, remove it. If it is the largest value number, just 495 // nuke it (and any other deleted values neighboring it), otherwise mark it as 496 // ~1U so it can be nuked later. 497 if (V1->id == getNumValNums()-1) { 498 do { 499 VNInfo *VNI = valnos.back(); 500 valnos.pop_back(); 501 VNI->~VNInfo(); 502 } while (valnos.back()->def == ~1U); 503 } else { 504 V1->def = ~1U; 505 } 506} 507 508void LiveInterval::Copy(const LiveInterval &RHS, 509 BumpPtrAllocator &VNInfoAllocator) { 510 ranges.clear(); 511 valnos.clear(); 512 preference = RHS.preference; 513 weight = RHS.weight; 514 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) { 515 const VNInfo *VNI = RHS.getValNumInfo(i); 516 VNInfo *NewVNI = getNextValue(~0U, 0, VNInfoAllocator); 517 copyValNumInfo(NewVNI, VNI); 518 } 519 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) { 520 const LiveRange &LR = RHS.ranges[i]; 521 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id))); 522 } 523} 524 525unsigned LiveInterval::getSize() const { 526 unsigned Sum = 0; 527 for (const_iterator I = begin(), E = end(); I != E; ++I) 528 Sum += I->end - I->start; 529 return Sum; 530} 531 532std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) { 533 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")"; 534} 535 536void LiveRange::dump() const { 537 cerr << *this << "\n"; 538} 539 540void LiveInterval::print(std::ostream &OS, const MRegisterInfo *MRI) const { 541 if (MRI && MRegisterInfo::isPhysicalRegister(reg)) 542 OS << MRI->getName(reg); 543 else 544 OS << "%reg" << reg; 545 546 OS << ',' << weight; 547 548 if (empty()) 549 OS << "EMPTY"; 550 else { 551 OS << " = "; 552 for (LiveInterval::Ranges::const_iterator I = ranges.begin(), 553 E = ranges.end(); I != E; ++I) 554 OS << *I; 555 } 556 557 // Print value number info. 558 if (getNumValNums()) { 559 OS << " "; 560 unsigned vnum = 0; 561 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e; 562 ++i, ++vnum) { 563 const VNInfo *vni = *i; 564 if (vnum) OS << " "; 565 OS << vnum << "@"; 566 if (vni->def == ~1U) { 567 OS << "x"; 568 } else { 569 if (vni->def == ~0U) 570 OS << "?"; 571 else 572 OS << vni->def; 573 unsigned ee = vni->kills.size(); 574 if (ee) { 575 OS << "-("; 576 for (unsigned j = 0; j != ee; ++j) { 577 OS << vni->kills[j]; 578 if (j != ee-1) 579 OS << " "; 580 } 581 OS << ")"; 582 } 583 } 584 } 585 } 586} 587 588void LiveInterval::dump() const { 589 cerr << *this << "\n"; 590} 591 592 593void LiveRange::print(std::ostream &os) const { 594 os << *this; 595} 596