1 //===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- C++ -*-===//
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' and 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 #ifndef LLVM_CODEGEN_LIVEINTERVAL_H
22 #define LLVM_CODEGEN_LIVEINTERVAL_H
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).
33 unsigned start; // Start point of the interval (inclusive)
34 unsigned end; // End point of the interval (exclusive)
35 unsigned ValId; // identifier for the value contained in this interval.
37 LiveRange(unsigned S, unsigned E, unsigned V) : start(S), end(E), ValId(V) {
38 assert(S < E && "Cannot create empty or backwards range");
41 /// contains - Return true if the index is covered by this range.
43 bool contains(unsigned I) const {
44 return start <= I && I < end;
47 bool operator<(const LiveRange &LR) const {
48 return start < LR.start || (start == LR.start && end < LR.end);
50 bool operator==(const LiveRange &LR) const {
51 return start == LR.start && end == LR.end;
57 LiveRange(); // DO NOT IMPLEMENT
59 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
61 inline bool operator<(unsigned V, const LiveRange &LR) {
66 /// LiveInterval - This class represents some number of live ranges for a
67 /// register or value. This class also contains a bit of register allocator
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
75 LiveInterval(unsigned Reg, float Weight)
76 : reg(Reg), weight(Weight), NumValues(0) {
79 LiveInterval& operator=(const LiveInterval& rhs) {
83 NumValues = rhs.NumValues;
87 void swap(LiveInterval& other) {
88 std::swap(reg, other.reg);
89 std::swap(weight, other.weight);
90 ranges.swap(other.ranges);
91 std::swap(NumValues, other.NumValues);
94 bool containsOneValue() const { return NumValues == 1; }
96 unsigned getNextValue() {
100 bool empty() const { return ranges.empty(); }
102 /// start - Return the lowest numbered slot covered by interval.
103 unsigned start() const {
104 assert(!empty() && "empty interval for register");
105 return ranges.front().start;
108 /// end - return the maximum point of the interval of the whole,
110 unsigned end() const {
111 assert(!empty() && "empty interval for register");
112 return ranges.back().end;
115 bool expiredAt(unsigned index) const {
116 return end() <= (index + 1);
119 bool liveAt(unsigned index) const;
121 /// getLiveRangeContaining - Return the live range that contains the
122 /// specified index, or null if there is none.
123 LiveRange *getLiveRangeContaining(unsigned Idx);
126 /// joinable - Two intervals are joinable if the either don't overlap at all
127 /// or if the destination of the copy is a single assignment value, and it
128 /// only overlaps with one value in the source interval.
129 bool joinable(const LiveInterval& other, unsigned CopyIdx) const;
132 bool overlaps(const LiveInterval& other) const;
134 /// addRange - Add the specified LiveRange to this interval, merging
135 /// intervals as appropriate. This returns an iterator to the inserted live
136 /// range (which may have grown since it was inserted.
137 void addRange(LiveRange LR) {
138 addRangeFrom(LR, ranges.begin());
141 /// join - Join two live intervals (this, and other) together. This
142 /// operation is the result of a copy instruction in the source program,
143 /// that occurs at index 'CopyIdx' that copies from 'other' to 'this'. This
144 /// destroys 'other'.
145 void join(LiveInterval& other, unsigned CopyIdx);
148 /// removeRange - Remove the specified range from this interval. Note that
149 /// the range must already be in this interval in its entirety.
150 void removeRange(unsigned Start, unsigned End);
152 bool operator<(const LiveInterval& other) const {
153 return start() < other.start();
159 unsigned NumValues; // the number of distinct values in this interval.
160 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
161 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
162 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
165 std::ostream& operator<<(std::ostream& os, const LiveInterval& li);