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"
33 /// LiveRange structure - This represents a simple register range in the
34 /// program, with an inclusive start point and an exclusive end point.
35 /// These ranges are rendered as [start,end).
37 unsigned start; // Start point of the interval (inclusive)
38 unsigned end; // End point of the interval (exclusive)
39 unsigned ValId; // identifier for the value contained in this interval.
41 LiveRange(unsigned S, unsigned E, unsigned V) : start(S), end(E), ValId(V) {
42 assert(S < E && "Cannot create empty or backwards range");
45 /// contains - Return true if the index is covered by this range.
47 bool contains(unsigned I) const {
48 return start <= I && I < end;
51 bool operator<(const LiveRange &LR) const {
52 return start < LR.start || (start == LR.start && end < LR.end);
54 bool operator==(const LiveRange &LR) const {
55 return start == LR.start && end == LR.end;
61 LiveRange(); // DO NOT IMPLEMENT
64 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
65 inline llvm_ostream& operator<<(llvm_ostream& os, const LiveRange &LR) {
66 if (os.stream()) *os.stream() << LR;
70 inline bool operator<(unsigned V, const LiveRange &LR) {
74 inline bool operator<(const LiveRange &LR, unsigned V) {
78 /// LiveInterval - This class represents some number of live ranges for a
79 /// register or value. This class also contains a bit of register allocator
82 typedef SmallVector<LiveRange,4> Ranges;
83 unsigned reg; // the register of this interval
84 float weight; // weight of this interval
85 Ranges ranges; // the ranges in which this register is live
87 /// ValueNumberInfo - If this value number is not defined by a copy, this
88 /// holds ~0,x. If the value number is not in use, it contains ~1,x to
89 /// indicate that the value # is not used. If the val# is defined by a
90 /// copy, the first entry is the instruction # of the copy, and the second
91 /// is the register number copied from.
92 SmallVector<std::pair<unsigned,unsigned>, 4> ValueNumberInfo;
95 LiveInterval(unsigned Reg, float Weight)
96 : reg(Reg), weight(Weight) {
99 typedef Ranges::iterator iterator;
100 iterator begin() { return ranges.begin(); }
101 iterator end() { return ranges.end(); }
103 typedef Ranges::const_iterator const_iterator;
104 const_iterator begin() const { return ranges.begin(); }
105 const_iterator end() const { return ranges.end(); }
108 /// advanceTo - Advance the specified iterator to point to the LiveRange
109 /// containing the specified position, or end() if the position is past the
110 /// end of the interval. If no LiveRange contains this position, but the
111 /// position is in a hole, this method returns an iterator pointing the the
112 /// LiveRange immediately after the hole.
113 iterator advanceTo(iterator I, unsigned Pos) {
114 if (Pos >= endNumber())
116 while (I->end <= Pos) ++I;
120 void swap(LiveInterval& other) {
121 std::swap(reg, other.reg);
122 std::swap(weight, other.weight);
123 std::swap(ranges, other.ranges);
124 std::swap(ValueNumberInfo, other.ValueNumberInfo);
127 bool containsOneValue() const { return ValueNumberInfo.size() == 1; }
129 unsigned getNumValNums() const { return ValueNumberInfo.size(); }
131 /// getNextValue - Create a new value number and return it. MIIdx specifies
132 /// the instruction that defines the value number.
133 unsigned getNextValue(unsigned MIIdx, unsigned SrcReg) {
134 ValueNumberInfo.push_back(std::make_pair(MIIdx, SrcReg));
135 return ValueNumberInfo.size()-1;
138 /// getInstForValNum - Return the machine instruction index that defines the
139 /// specified value number.
140 unsigned getInstForValNum(unsigned ValNo) const {
141 //assert(ValNo < ValueNumberInfo.size());
142 return ValueNumberInfo[ValNo].first;
145 unsigned getSrcRegForValNum(unsigned ValNo) const {
146 //assert(ValNo < ValueNumberInfo.size());
147 if (ValueNumberInfo[ValNo].first < ~2U)
148 return ValueNumberInfo[ValNo].second;
152 std::pair<unsigned, unsigned> getValNumInfo(unsigned ValNo) const {
153 //assert(ValNo < ValueNumberInfo.size());
154 return ValueNumberInfo[ValNo];
157 /// setValueNumberInfo - Change the value number info for the specified
159 void setValueNumberInfo(unsigned ValNo,
160 const std::pair<unsigned, unsigned> &I){
161 ValueNumberInfo[ValNo] = I;
164 /// MergeValueNumberInto - This method is called when two value nubmers
165 /// are found to be equivalent. This eliminates V1, replacing all
166 /// LiveRanges with the V1 value number with the V2 value number. This can
167 /// cause merging of V1/V2 values numbers and compaction of the value space.
168 void MergeValueNumberInto(unsigned V1, unsigned V2);
170 /// MergeInClobberRanges - For any live ranges that are not defined in the
171 /// current interval, but are defined in the Clobbers interval, mark them
172 /// used with an unknown definition value.
173 void MergeInClobberRanges(const LiveInterval &Clobbers);
176 /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
177 /// interval as the specified value number. The LiveRanges in RHS are
178 /// allowed to overlap with LiveRanges in the current interval, but only if
179 /// the overlapping LiveRanges have the specified value number.
180 void MergeRangesInAsValue(const LiveInterval &RHS, unsigned LHSValNo);
182 bool empty() const { return ranges.empty(); }
184 /// beginNumber - Return the lowest numbered slot covered by interval.
185 unsigned beginNumber() const {
186 assert(!empty() && "empty interval for register");
187 return ranges.front().start;
190 /// endNumber - return the maximum point of the interval of the whole,
192 unsigned endNumber() const {
193 assert(!empty() && "empty interval for register");
194 return ranges.back().end;
197 bool expiredAt(unsigned index) const {
198 return index >= endNumber();
201 bool liveAt(unsigned index) const;
203 /// getLiveRangeContaining - Return the live range that contains the
204 /// specified index, or null if there is none.
205 const LiveRange *getLiveRangeContaining(unsigned Idx) const {
206 const_iterator I = FindLiveRangeContaining(Idx);
207 return I == end() ? 0 : &*I;
210 /// FindLiveRangeContaining - Return an iterator to the live range that
211 /// contains the specified index, or end() if there is none.
212 const_iterator FindLiveRangeContaining(unsigned Idx) const;
214 /// FindLiveRangeContaining - Return an iterator to the live range that
215 /// contains the specified index, or end() if there is none.
216 iterator FindLiveRangeContaining(unsigned Idx);
218 /// getOverlapingRanges - Given another live interval which is defined as a
219 /// copy from this one, return a list of all of the live ranges where the
220 /// two overlap and have different value numbers.
221 void getOverlapingRanges(const LiveInterval &Other, unsigned CopyIdx,
222 std::vector<LiveRange*> &Ranges);
224 /// overlaps - Return true if the intersection of the two live intervals is
226 bool overlaps(const LiveInterval& other) const {
227 return overlapsFrom(other, other.begin());
230 /// overlapsFrom - Return true if the intersection of the two live intervals
231 /// is not empty. The specified iterator is a hint that we can begin
232 /// scanning the Other interval starting at I.
233 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
235 /// addRange - Add the specified LiveRange to this interval, merging
236 /// intervals as appropriate. This returns an iterator to the inserted live
237 /// range (which may have grown since it was inserted.
238 void addRange(LiveRange LR) {
239 addRangeFrom(LR, ranges.begin());
242 /// join - Join two live intervals (this, and other) together. This applies
243 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
244 /// the intervals are not joinable, this aborts.
245 void join(LiveInterval &Other, int *ValNoAssignments,
246 int *RHSValNoAssignments,
247 SmallVector<std::pair<unsigned,unsigned>,16> &NewValueNumberInfo);
249 /// removeRange - Remove the specified range from this interval. Note that
250 /// the range must already be in this interval in its entirety.
251 void removeRange(unsigned Start, unsigned End);
253 void removeRange(LiveRange LR) {
254 removeRange(LR.start, LR.end);
257 bool operator<(const LiveInterval& other) const {
258 return beginNumber() < other.beginNumber();
261 void print(llvm_ostream OS, const MRegisterInfo *MRI = 0) const;
262 void print(std::ostream &OS, const MRegisterInfo *MRI = 0) const {
263 print(llvm_ostream(OS), MRI);
268 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
269 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
270 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
271 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
274 inline llvm_ostream &operator<<(llvm_ostream &OS, const LiveInterval &LI) {
279 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {