blob: 25081e1f890ce63c1ad8cd19e1e1be6a84ebbe98 [file] [log] [blame]
Chris Lattner78f62e32004-07-23 17:49:16 +00001//===-- LiveInterval.cpp - Live Interval Representation -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
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.
18//
19//===----------------------------------------------------------------------===//
20
21#include "LiveInterval.h"
22#include "Support/STLExtras.h"
23#include <ostream>
24using namespace llvm;
25
26// An example for liveAt():
27//
Chris Lattner2fcc5e42004-07-23 18:40:00 +000028// this = [1,4), liveAt(0) will return false. The instruction defining this
29// spans slots [0,3]. The interval belongs to an spilled definition of the
30// variable it represents. This is because slot 1 is used (def slot) and spans
31// up to slot 3 (store slot).
Chris Lattner78f62e32004-07-23 17:49:16 +000032//
Chris Lattnerc96d2992004-07-23 18:13:24 +000033bool LiveInterval::liveAt(unsigned I) const {
34 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
35
Chris Lattner78f62e32004-07-23 17:49:16 +000036 if (r == ranges.begin())
37 return false;
38
39 --r;
Chris Lattner2fcc5e42004-07-23 18:40:00 +000040 return r->contains(I);
Chris Lattner78f62e32004-07-23 17:49:16 +000041}
42
43// An example for overlaps():
44//
45// 0: A = ...
46// 4: B = ...
47// 8: C = A + B ;; last use of A
48//
49// The live intervals should look like:
50//
51// A = [3, 11)
52// B = [7, x)
53// C = [11, y)
54//
55// A->overlaps(C) should return false since we want to be able to join
56// A and C.
57bool LiveInterval::overlaps(const LiveInterval& other) const {
58 Ranges::const_iterator i = ranges.begin();
59 Ranges::const_iterator ie = ranges.end();
60 Ranges::const_iterator j = other.ranges.begin();
61 Ranges::const_iterator je = other.ranges.end();
62 if (i->start < j->start) {
Chris Lattner2fcc5e42004-07-23 18:40:00 +000063 i = std::upper_bound(i, ie, j->start);
Chris Lattner78f62e32004-07-23 17:49:16 +000064 if (i != ranges.begin()) --i;
Chris Lattner2fcc5e42004-07-23 18:40:00 +000065 } else if (j->start < i->start) {
66 j = std::upper_bound(j, je, i->start);
Chris Lattner78f62e32004-07-23 17:49:16 +000067 if (j != other.ranges.begin()) --j;
Chris Lattner2fcc5e42004-07-23 18:40:00 +000068 } else {
69 return true;
Chris Lattner78f62e32004-07-23 17:49:16 +000070 }
71
72 while (i != ie && j != je) {
73 if (i->start == j->start)
74 return true;
75
76 if (i->start > j->start) {
77 swap(i, j);
78 swap(ie, je);
79 }
80 assert(i->start < j->start);
81
82 if (i->end > j->start)
83 return true;
84 ++i;
85 }
86
87 return false;
88}
89
90void LiveInterval::addRange(LiveRange LR) {
91 Ranges::iterator it =
Chris Lattner2fcc5e42004-07-23 18:40:00 +000092 ranges.insert(std::upper_bound(ranges.begin(), ranges.end(), LR.start), LR);
Chris Lattner78f62e32004-07-23 17:49:16 +000093
94 mergeRangesBackward(mergeRangesForward(it));
95}
96
97void LiveInterval::join(const LiveInterval& other) {
98 Ranges::iterator cur = ranges.begin();
99 isDefinedOnce &= other.isDefinedOnce;
100
101 for (Ranges::const_iterator i = other.ranges.begin(),
102 e = other.ranges.end(); i != e; ++i) {
Chris Lattner2fcc5e42004-07-23 18:40:00 +0000103 cur = ranges.insert(std::upper_bound(cur, ranges.end(), i->start), *i);
Chris Lattner78f62e32004-07-23 17:49:16 +0000104 cur = mergeRangesBackward(mergeRangesForward(cur));
105 }
106 weight += other.weight;
107}
108
109LiveInterval::Ranges::iterator
110LiveInterval::mergeRangesForward(Ranges::iterator it) {
111 Ranges::iterator n;
112 while ((n = next(it)) != ranges.end()) {
113 if (n->start > it->end)
114 break;
115 it->end = std::max(it->end, n->end);
116 n = ranges.erase(n);
117 }
118 return it;
119}
120
121LiveInterval::Ranges::iterator
122LiveInterval::mergeRangesBackward(Ranges::iterator it) {
123 while (it != ranges.begin()) {
124 Ranges::iterator p = prior(it);
125 if (it->start > p->end)
126 break;
127
128 it->start = std::min(it->start, p->start);
129 it->end = std::max(it->end, p->end);
130 it = ranges.erase(p);
131 }
132
133 return it;
134}
135
136std::ostream& llvm::operator<<(std::ostream& os, const LiveRange &LR) {
137 return os << "[" << LR.start << "," << LR.end << ")";
138}
139
140std::ostream& llvm::operator<<(std::ostream& os, const LiveInterval& li) {
141 os << "%reg" << li.reg << ',' << li.weight;
142 if (li.empty())
143 return os << "EMPTY";
144
145 os << " = ";
146 for (LiveInterval::Ranges::const_iterator i = li.ranges.begin(),
147 e = li.ranges.end(); i != e; ++i)
148 os << *i;
149 return os;
150}