1 //===-- LiveInterval.cpp - Live Interval Representation -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LiveRange and LiveInterval classes. Given some
11 // numbering of each the machine instructions an interval [i, j) is said to be a
12 // live interval for register v if there is no instruction with number j' > j
13 // such that v is live at j' abd there is no instruction with number i' < i such
14 // that v is live at i'. In this implementation intervals can have holes,
15 // i.e. an interval might look like [1,20), [50,65), [1000,1001). Each
16 // individual range is represented as an instance of LiveRange, and the whole
17 // interval is represented as an instance of LiveInterval.
19 //===----------------------------------------------------------------------===//
21 #include "LiveInterval.h"
22 #include "Support/STLExtras.h"
26 // An example for liveAt():
28 // this = [1,4), liveAt(0) will return false. The instruction defining
29 // this spans slots [0,3]. The interval belongs to an spilled
30 // definition of the variable it represents. This is because slot 1 is
31 // used (def slot) and spans up to slot 3 (store slot).
33 bool LiveInterval::liveAt(unsigned index) const {
34 LiveRange dummy(index, index+1);
35 Ranges::const_iterator r = std::upper_bound(ranges.begin(),
38 if (r == ranges.begin())
42 return index >= r->start && index < r->end;
45 // An example for overlaps():
49 // 8: C = A + B ;; last use of A
51 // The live intervals should look like:
57 // A->overlaps(C) should return false since we want to be able to join
59 bool LiveInterval::overlaps(const LiveInterval& other) const {
60 Ranges::const_iterator i = ranges.begin();
61 Ranges::const_iterator ie = ranges.end();
62 Ranges::const_iterator j = other.ranges.begin();
63 Ranges::const_iterator je = other.ranges.end();
64 if (i->start < j->start) {
65 i = std::upper_bound(i, ie, *j);
66 if (i != ranges.begin()) --i;
68 else if (j->start < i->start) {
69 j = std::upper_bound(j, je, *i);
70 if (j != other.ranges.begin()) --j;
73 while (i != ie && j != je) {
74 if (i->start == j->start)
77 if (i->start > j->start) {
81 assert(i->start < j->start);
83 if (i->end > j->start)
91 void LiveInterval::addRange(LiveRange LR) {
93 ranges.insert(std::upper_bound(ranges.begin(), ranges.end(), LR), LR);
95 mergeRangesBackward(mergeRangesForward(it));
98 void LiveInterval::join(const LiveInterval& other) {
99 Ranges::iterator cur = ranges.begin();
100 isDefinedOnce &= other.isDefinedOnce;
102 for (Ranges::const_iterator i = other.ranges.begin(),
103 e = other.ranges.end(); i != e; ++i) {
104 cur = ranges.insert(std::upper_bound(cur, ranges.end(), *i), *i);
105 cur = mergeRangesBackward(mergeRangesForward(cur));
107 weight += other.weight;
110 LiveInterval::Ranges::iterator
111 LiveInterval::mergeRangesForward(Ranges::iterator it) {
113 while ((n = next(it)) != ranges.end()) {
114 if (n->start > it->end)
116 it->end = std::max(it->end, n->end);
122 LiveInterval::Ranges::iterator
123 LiveInterval::mergeRangesBackward(Ranges::iterator it) {
124 while (it != ranges.begin()) {
125 Ranges::iterator p = prior(it);
126 if (it->start > p->end)
129 it->start = std::min(it->start, p->start);
130 it->end = std::max(it->end, p->end);
131 it = ranges.erase(p);
137 std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
138 return os << "[" << LR.start << "," << LR.end << ")";
141 std::ostream& llvm::operator<<(std::ostream& os, const LiveInterval& li) {
142 os << "%reg" << li.reg << ',' << li.weight;
144 return os << "EMPTY";
147 for (LiveInterval::Ranges::const_iterator i = li.ranges.begin(),
148 e = li.ranges.end(); i != e; ++i)