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
31 /// LiveRange structure - This represents a simple register range in the
32 /// program, with an inclusive start point and an exclusive end point.
33 /// These ranges are rendered as [start,end).
35 unsigned start; // Start point of the interval (inclusive)
36 unsigned end; // End point of the interval (exclusive)
37 unsigned ValId; // identifier for the value contained in this interval.
39 LiveRange(unsigned S, unsigned E, unsigned V) : start(S), end(E), ValId(V) {
40 assert(S < E && "Cannot create empty or backwards range");
43 /// contains - Return true if the index is covered by this range.
45 bool contains(unsigned I) const {
46 return start <= I && I < end;
49 bool operator<(const LiveRange &LR) const {
50 return start < LR.start || (start == LR.start && end < LR.end);
52 bool operator==(const LiveRange &LR) const {
53 return start == LR.start && end == LR.end;
59 LiveRange(); // DO NOT IMPLEMENT
61 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
63 inline bool operator<(unsigned V, const LiveRange &LR) {
67 inline bool operator<(const LiveRange &LR, unsigned V) {
71 /// LiveInterval - This class represents some number of live ranges for a
72 /// register or value. This class also contains a bit of register allocator
75 typedef std::vector<LiveRange> Ranges;
76 unsigned reg; // the register of this interval
77 float weight; // weight of this interval
78 Ranges ranges; // the ranges in which this register is live
80 unsigned NumValues; // the number of distinct values in this interval.
83 LiveInterval(unsigned Reg, float Weight)
84 : reg(Reg), weight(Weight), NumValues(0) {
87 typedef Ranges::iterator iterator;
88 iterator begin() { return ranges.begin(); }
89 iterator end() { return ranges.end(); }
91 typedef Ranges::const_iterator const_iterator;
92 const_iterator begin() const { return ranges.begin(); }
93 const_iterator end() const { return ranges.end(); }
96 /// advanceTo - Advance the specified iterator to point to the LiveRange
97 /// containing the specified position, or end() if the position is past the
98 /// end of the interval. If no LiveRange contains this position, but the
99 /// position is in a hole, this method returns an iterator pointing the the
100 /// LiveRange immediately after the hole.
101 iterator advanceTo(iterator I, unsigned Pos) {
102 if (Pos >= endNumber())
104 while (I->end <= Pos) ++I;
108 void swap(LiveInterval& other) {
109 std::swap(reg, other.reg);
110 std::swap(weight, other.weight);
111 ranges.swap(other.ranges);
112 std::swap(NumValues, other.NumValues);
115 bool containsOneValue() const { return NumValues == 1; }
117 unsigned getNextValue() {
121 bool empty() const { return ranges.empty(); }
123 /// beginNumber - Return the lowest numbered slot covered by interval.
124 unsigned beginNumber() const {
125 assert(!empty() && "empty interval for register");
126 return ranges.front().start;
129 /// endNumber - return the maximum point of the interval of the whole,
131 unsigned endNumber() const {
132 assert(!empty() && "empty interval for register");
133 return ranges.back().end;
136 bool expiredAt(unsigned index) const {
137 return index >= endNumber();
140 bool liveAt(unsigned index) const;
142 /// getLiveRangeContaining - Return the live range that contains the
143 /// specified index, or null if there is none.
144 const LiveRange *getLiveRangeContaining(unsigned Idx) const;
147 /// joinable - Two intervals are joinable if the either don't overlap at all
148 /// or if the destination of the copy is a single assignment value, and it
149 /// only overlaps with one value in the source interval.
150 bool joinable(const LiveInterval& other, unsigned CopyIdx) const;
152 /// getOverlapingRanges - Given another live interval which is defined as a
153 /// copy from this one, return a list of all of the live ranges where the
154 /// two overlap and have different value numbers.
155 void getOverlapingRanges(const LiveInterval &Other, unsigned CopyIdx,
156 std::vector<LiveRange*> &Ranges);
158 /// overlaps - Return true if the intersection of the two live intervals is
160 bool overlaps(const LiveInterval& other) const {
161 return overlapsFrom(other, other.begin());
164 /// overlapsFrom - Return true if the intersection of the two live intervals
165 /// is not empty. The specified iterator is a hint that we can begin
166 /// scanning the Other interval starting at I.
167 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
169 /// addRange - Add the specified LiveRange to this interval, merging
170 /// intervals as appropriate. This returns an iterator to the inserted live
171 /// range (which may have grown since it was inserted.
172 void addRange(LiveRange LR) {
173 addRangeFrom(LR, ranges.begin());
176 /// join - Join two live intervals (this, and other) together. This
177 /// operation is the result of a copy instruction in the source program,
178 /// that occurs at index 'CopyIdx' that copies from 'other' to 'this'. This
179 /// destroys 'other'.
180 void join(LiveInterval& other, unsigned CopyIdx);
183 /// removeRange - Remove the specified range from this interval. Note that
184 /// the range must already be in this interval in its entirety.
185 void removeRange(unsigned Start, unsigned End);
187 bool operator<(const LiveInterval& other) const {
188 return beginNumber() < other.beginNumber();
191 void print(std::ostream &OS, const MRegisterInfo *MRI = 0) const;
195 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
196 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
197 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
198 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
201 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {