blob: 05ebcc1ca2fd2b9856434de40ebc8c398984b81c [file] [log] [blame]
Chris Lattnerfb449b92004-07-23 17:49:16 +00001//===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- C++ -*-===//
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
Chris Lattnerabf295f2004-07-24 02:52:23 +000013// such that v is live at j' and there is no instruction with number i' < i such
Chris Lattnerfb449b92004-07-23 17:49:16 +000014// 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#ifndef LLVM_CODEGEN_LIVEINTERVAL_H
22#define LLVM_CODEGEN_LIVEINTERVAL_H
23
24#include <iosfwd>
25#include <vector>
26#include <cassert>
27
28namespace llvm {
29 /// LiveRange structure - This represents a simple register range in the
30 /// program, with an inclusive start point and an exclusive end point.
31 /// These ranges are rendered as [start,end).
32 struct LiveRange {
33 unsigned start; // Start point of the interval (inclusive)
Chris Lattnerabf295f2004-07-24 02:52:23 +000034 unsigned end; // End point of the interval (exclusive)
35 unsigned ValId; // identifier for the value contained in this interval.
36
37 LiveRange(unsigned S, unsigned E, unsigned V) : start(S), end(E), ValId(V) {
Chris Lattnerfb449b92004-07-23 17:49:16 +000038 assert(S < E && "Cannot create empty or backwards range");
39 }
40
Chris Lattnere6ad3922004-07-23 18:39:12 +000041 /// contains - Return true if the index is covered by this range.
42 ///
43 bool contains(unsigned I) const {
44 return start <= I && I < end;
45 }
46
Chris Lattnerfb449b92004-07-23 17:49:16 +000047 bool operator<(const LiveRange &LR) const {
48 return start < LR.start || (start == LR.start && end < LR.end);
49 }
50 bool operator==(const LiveRange &LR) const {
51 return start == LR.start && end == LR.end;
52 }
Chris Lattnerabf295f2004-07-24 02:52:23 +000053
54 void dump() const;
55
Chris Lattnere6ad3922004-07-23 18:39:12 +000056 private:
Chris Lattnerfb449b92004-07-23 17:49:16 +000057 LiveRange(); // DO NOT IMPLEMENT
58 };
59 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
60
Chris Lattnerebd7e6c2004-07-23 18:13:24 +000061 inline bool operator<(unsigned V, const LiveRange &LR) {
62 return V < LR.start;
63 }
64
65
Chris Lattnerfb449b92004-07-23 17:49:16 +000066 /// LiveInterval - This class represents some number of live ranges for a
67 /// register or value. This class also contains a bit of register allocator
68 /// state.
69 struct LiveInterval {
70 typedef std::vector<LiveRange> Ranges;
71 unsigned reg; // the register of this interval
72 float weight; // weight of this interval
73 Ranges ranges; // the ranges in which this register is live
Chris Lattnerfb449b92004-07-23 17:49:16 +000074
75 LiveInterval(unsigned Reg, float Weight)
Chris Lattnerabf295f2004-07-24 02:52:23 +000076 : reg(Reg), weight(Weight), NumValues(0) {
Chris Lattnerfb449b92004-07-23 17:49:16 +000077 }
78
Chris Lattner23b71c12004-11-18 01:29:39 +000079
80 typedef Ranges::iterator iterator;
81 iterator begin() { return ranges.begin(); }
82 iterator end() { return ranges.end(); }
83
Chris Lattner743ef6d2004-11-18 02:37:31 +000084
85 /// advanceTo - Advance the specified iterator to point to the LiveRange
86 /// containing the specified position, or end() if the position is past the
87 /// end of the interval. If no LiveRange contains this position, but the
88 /// position is in a hole, this method returns an iterator pointing the the
89 /// LiveRange immediately after the hold.
90 iterator advanceTo(iterator I, unsigned Pos) {
91 if (Pos >= endNumber())
92 return end();
93 while (I->end <= Pos) ++I;
94 return I;
95 }
96
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +000097 void swap(LiveInterval& other) {
98 std::swap(reg, other.reg);
99 std::swap(weight, other.weight);
100 ranges.swap(other.ranges);
101 std::swap(NumValues, other.NumValues);
102 }
103
Chris Lattnerabf295f2004-07-24 02:52:23 +0000104 bool containsOneValue() const { return NumValues == 1; }
105
106 unsigned getNextValue() {
107 return NumValues++;
108 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000109
110 bool empty() const { return ranges.empty(); }
111
Chris Lattner23b71c12004-11-18 01:29:39 +0000112 /// beginNumber - Return the lowest numbered slot covered by interval.
113 unsigned beginNumber() const {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000114 assert(!empty() && "empty interval for register");
115 return ranges.front().start;
116 }
117
Chris Lattner23b71c12004-11-18 01:29:39 +0000118 /// endNumber - return the maximum point of the interval of the whole,
Chris Lattnerfb449b92004-07-23 17:49:16 +0000119 /// exclusive.
Chris Lattner23b71c12004-11-18 01:29:39 +0000120 unsigned endNumber() const {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000121 assert(!empty() && "empty interval for register");
122 return ranges.back().end;
123 }
124
125 bool expiredAt(unsigned index) const {
Chris Lattner8d8d5132004-11-18 01:34:44 +0000126 return index >= endNumber();
Chris Lattnerfb449b92004-07-23 17:49:16 +0000127 }
128
129 bool liveAt(unsigned index) const;
130
Chris Lattnerabf295f2004-07-24 02:52:23 +0000131 /// getLiveRangeContaining - Return the live range that contains the
132 /// specified index, or null if there is none.
Chris Lattnerd3a205e2004-07-25 06:23:01 +0000133 const LiveRange *getLiveRangeContaining(unsigned Idx) const;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000134
135
136 /// joinable - Two intervals are joinable if the either don't overlap at all
137 /// or if the destination of the copy is a single assignment value, and it
138 /// only overlaps with one value in the source interval.
139 bool joinable(const LiveInterval& other, unsigned CopyIdx) const;
140
141
Chris Lattnerfb449b92004-07-23 17:49:16 +0000142 bool overlaps(const LiveInterval& other) const;
143
Chris Lattnerb26c2152004-07-23 19:38:44 +0000144 /// addRange - Add the specified LiveRange to this interval, merging
145 /// intervals as appropriate. This returns an iterator to the inserted live
146 /// range (which may have grown since it was inserted.
147 void addRange(LiveRange LR) {
148 addRangeFrom(LR, ranges.begin());
149 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000150
Chris Lattnerabf295f2004-07-24 02:52:23 +0000151 /// join - Join two live intervals (this, and other) together. This
152 /// operation is the result of a copy instruction in the source program,
153 /// that occurs at index 'CopyIdx' that copies from 'other' to 'this'. This
154 /// destroys 'other'.
155 void join(LiveInterval& other, unsigned CopyIdx);
156
157
158 /// removeRange - Remove the specified range from this interval. Note that
159 /// the range must already be in this interval in its entirety.
160 void removeRange(unsigned Start, unsigned End);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000161
162 bool operator<(const LiveInterval& other) const {
Chris Lattner23b71c12004-11-18 01:29:39 +0000163 return beginNumber() < other.beginNumber();
Chris Lattnerfb449b92004-07-23 17:49:16 +0000164 }
165
Chris Lattnerabf295f2004-07-24 02:52:23 +0000166 void dump() const;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000167
168 private:
Chris Lattnerabf295f2004-07-24 02:52:23 +0000169 unsigned NumValues; // the number of distinct values in this interval.
Chris Lattnerb26c2152004-07-23 19:38:44 +0000170 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
171 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
172 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
Alkis Evlogimenos3f861932004-07-24 18:55:15 +0000173 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
Chris Lattnerfb449b92004-07-23 17:49:16 +0000174 };
175
176 std::ostream& operator<<(std::ostream& os, const LiveInterval& li);
177}
178
179#endif