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
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Support/Streams.h"
34 /// LiveRange structure - This represents a simple register range in the
35 /// program, with an inclusive start point and an exclusive end point.
36 /// These ranges are rendered as [start,end).
38 unsigned start; // Start point of the interval (inclusive)
39 unsigned end; // End point of the interval (exclusive)
40 unsigned ValId; // identifier for the value contained in this interval.
42 LiveRange(unsigned S, unsigned E, unsigned V) : start(S), end(E), ValId(V) {
43 assert(S < E && "Cannot create empty or backwards range");
46 /// contains - Return true if the index is covered by this range.
48 bool contains(unsigned I) const {
49 return start <= I && I < end;
52 bool operator<(const LiveRange &LR) const {
53 return start < LR.start || (start == LR.start && end < LR.end);
55 bool operator==(const LiveRange &LR) const {
56 return start == LR.start && end == LR.end;
62 LiveRange(); // DO NOT IMPLEMENT
65 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
66 inline OStream& operator<<(OStream& os, const LiveRange &LR) {
67 if (os.stream()) *os.stream() << LR;
71 inline bool operator<(unsigned V, const LiveRange &LR) {
75 inline bool operator<(const LiveRange &LR, unsigned V) {
79 /// LiveInterval - This class represents some number of live ranges for a
80 /// register or value. This class also contains a bit of register allocator
83 typedef SmallVector<LiveRange,4> Ranges;
84 unsigned reg; // the register of this interval
85 float weight; // weight of this interval
86 Ranges ranges; // the ranges in which this register is live
88 /// ValueNumberInfo - If this value number is not defined by a copy, this
89 /// holds ~0,x. If the value number is not in use, it contains ~1,x to
90 /// indicate that the value # is not used. If the val# is defined by a
91 /// copy, the first entry is the instruction # of the copy, and the second
92 /// is the register number copied from.
93 SmallVector<std::pair<unsigned,unsigned>, 4> ValueNumberInfo;
96 LiveInterval(unsigned Reg, float Weight)
97 : reg(Reg), weight(Weight) {
100 typedef Ranges::iterator iterator;
101 iterator begin() { return ranges.begin(); }
102 iterator end() { return ranges.end(); }
104 typedef Ranges::const_iterator const_iterator;
105 const_iterator begin() const { return ranges.begin(); }
106 const_iterator end() const { return ranges.end(); }
109 /// advanceTo - Advance the specified iterator to point to the LiveRange
110 /// containing the specified position, or end() if the position is past the
111 /// end of the interval. If no LiveRange contains this position, but the
112 /// position is in a hole, this method returns an iterator pointing the the
113 /// LiveRange immediately after the hole.
114 iterator advanceTo(iterator I, unsigned Pos) {
115 if (Pos >= endNumber())
117 while (I->end <= Pos) ++I;
121 void swap(LiveInterval& other) {
122 std::swap(reg, other.reg);
123 std::swap(weight, other.weight);
124 std::swap(ranges, other.ranges);
125 std::swap(ValueNumberInfo, other.ValueNumberInfo);
128 bool containsOneValue() const { return ValueNumberInfo.size() == 1; }
130 unsigned getNumValNums() const { return ValueNumberInfo.size(); }
132 /// getNextValue - Create a new value number and return it. MIIdx specifies
133 /// the instruction that defines the value number.
134 unsigned getNextValue(unsigned MIIdx, unsigned SrcReg) {
135 ValueNumberInfo.push_back(std::make_pair(MIIdx, SrcReg));
136 return ValueNumberInfo.size()-1;
139 /// getInstForValNum - Return the machine instruction index that defines the
140 /// specified value number.
141 unsigned getInstForValNum(unsigned ValNo) const {
142 //assert(ValNo < ValueNumberInfo.size());
143 return ValueNumberInfo[ValNo].first;
146 unsigned getSrcRegForValNum(unsigned ValNo) const {
147 //assert(ValNo < ValueNumberInfo.size());
148 if (ValueNumberInfo[ValNo].first < ~2U)
149 return ValueNumberInfo[ValNo].second;
153 std::pair<unsigned, unsigned> getValNumInfo(unsigned ValNo) const {
154 //assert(ValNo < ValueNumberInfo.size());
155 return ValueNumberInfo[ValNo];
158 /// setValueNumberInfo - Change the value number info for the specified
160 void setValueNumberInfo(unsigned ValNo,
161 const std::pair<unsigned, unsigned> &I){
162 ValueNumberInfo[ValNo] = I;
165 /// MergeValueNumberInto - This method is called when two value nubmers
166 /// are found to be equivalent. This eliminates V1, replacing all
167 /// LiveRanges with the V1 value number with the V2 value number. This can
168 /// cause merging of V1/V2 values numbers and compaction of the value space.
169 void MergeValueNumberInto(unsigned V1, unsigned V2);
171 /// MergeInClobberRanges - For any live ranges that are not defined in the
172 /// current interval, but are defined in the Clobbers interval, mark them
173 /// used with an unknown definition value.
174 void MergeInClobberRanges(const LiveInterval &Clobbers);
177 /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
178 /// interval as the specified value number. The LiveRanges in RHS are
179 /// allowed to overlap with LiveRanges in the current interval, but only if
180 /// the overlapping LiveRanges have the specified value number.
181 void MergeRangesInAsValue(const LiveInterval &RHS, unsigned LHSValNo);
183 bool empty() const { return ranges.empty(); }
185 /// beginNumber - Return the lowest numbered slot covered by interval.
186 unsigned beginNumber() const {
187 assert(!empty() && "empty interval for register");
188 return ranges.front().start;
191 /// endNumber - return the maximum point of the interval of the whole,
193 unsigned endNumber() const {
194 assert(!empty() && "empty interval for register");
195 return ranges.back().end;
198 bool expiredAt(unsigned index) const {
199 return index >= endNumber();
202 bool liveAt(unsigned index) const;
204 /// getLiveRangeContaining - Return the live range that contains the
205 /// specified index, or null if there is none.
206 const LiveRange *getLiveRangeContaining(unsigned Idx) const {
207 const_iterator I = FindLiveRangeContaining(Idx);
208 return I == end() ? 0 : &*I;
211 /// FindLiveRangeContaining - Return an iterator to the live range that
212 /// contains the specified index, or end() if there is none.
213 const_iterator FindLiveRangeContaining(unsigned Idx) const;
215 /// FindLiveRangeContaining - Return an iterator to the live range that
216 /// contains the specified index, or end() if there is none.
217 iterator FindLiveRangeContaining(unsigned Idx);
219 /// getOverlapingRanges - Given another live interval which is defined as a
220 /// copy from this one, return a list of all of the live ranges where the
221 /// two overlap and have different value numbers.
222 void getOverlapingRanges(const LiveInterval &Other, unsigned CopyIdx,
223 std::vector<LiveRange*> &Ranges);
225 /// overlaps - Return true if the intersection of the two live intervals is
227 bool overlaps(const LiveInterval& other) const {
228 return overlapsFrom(other, other.begin());
231 /// overlapsFrom - Return true if the intersection of the two live intervals
232 /// is not empty. The specified iterator is a hint that we can begin
233 /// scanning the Other interval starting at I.
234 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
236 /// addRange - Add the specified LiveRange to this interval, merging
237 /// intervals as appropriate. This returns an iterator to the inserted live
238 /// range (which may have grown since it was inserted.
239 void addRange(LiveRange LR) {
240 addRangeFrom(LR, ranges.begin());
243 /// join - Join two live intervals (this, and other) together. This applies
244 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
245 /// the intervals are not joinable, this aborts.
246 void join(LiveInterval &Other, int *ValNoAssignments,
247 int *RHSValNoAssignments,
248 SmallVector<std::pair<unsigned,unsigned>,16> &NewValueNumberInfo);
250 /// removeRange - Remove the specified range from this interval. Note that
251 /// the range must already be in this interval in its entirety.
252 void removeRange(unsigned Start, unsigned End);
254 void removeRange(LiveRange LR) {
255 removeRange(LR.start, LR.end);
258 bool operator<(const LiveInterval& other) const {
259 return beginNumber() < other.beginNumber();
262 void print(OStream OS, const MRegisterInfo *MRI = 0) const;
263 void print(std::ostream &OS, const MRegisterInfo *MRI = 0) const {
264 print(OStream(OS), MRI);
269 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
270 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
271 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
272 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
275 inline OStream &operator<<(OStream &OS, const LiveInterval &LI) {
280 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {