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;
59 void print(std::ostream &os) const;
60 void print(std::ostream *os) const { if (os) print(*os); }
63 LiveRange(); // DO NOT IMPLEMENT
66 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
69 inline bool operator<(unsigned V, const LiveRange &LR) {
73 inline bool operator<(const LiveRange &LR, unsigned V) {
77 /// LiveInterval - This class represents some number of live ranges for a
78 /// register or value. This class also contains a bit of register allocator
81 typedef SmallVector<LiveRange,4> Ranges;
82 unsigned reg; // the register of this interval
83 float weight; // weight of this interval
84 Ranges ranges; // the ranges in which this register is live
86 /// ValueNumberInfo - If this value number is not defined by a copy, this
87 /// holds ~0,x. If the value number is not in use, it contains ~1,x to
88 /// indicate that the value # is not used. If the val# is defined by a
89 /// copy, the first entry is the instruction # of the copy, and the second
90 /// is the register number copied from.
91 SmallVector<std::pair<unsigned,unsigned>, 4> ValueNumberInfo;
94 LiveInterval(unsigned Reg, float Weight)
95 : reg(Reg), weight(Weight) {
98 typedef Ranges::iterator iterator;
99 iterator begin() { return ranges.begin(); }
100 iterator end() { return ranges.end(); }
102 typedef Ranges::const_iterator const_iterator;
103 const_iterator begin() const { return ranges.begin(); }
104 const_iterator end() const { return ranges.end(); }
107 /// advanceTo - Advance the specified iterator to point to the LiveRange
108 /// containing the specified position, or end() if the position is past the
109 /// end of the interval. If no LiveRange contains this position, but the
110 /// position is in a hole, this method returns an iterator pointing the the
111 /// LiveRange immediately after the hole.
112 iterator advanceTo(iterator I, unsigned Pos) {
113 if (Pos >= endNumber())
115 while (I->end <= Pos) ++I;
119 void swap(LiveInterval& other) {
120 std::swap(reg, other.reg);
121 std::swap(weight, other.weight);
122 std::swap(ranges, other.ranges);
123 std::swap(ValueNumberInfo, other.ValueNumberInfo);
126 bool containsOneValue() const { return ValueNumberInfo.size() == 1; }
128 unsigned getNumValNums() const { return ValueNumberInfo.size(); }
130 /// getNextValue - Create a new value number and return it. MIIdx specifies
131 /// the instruction that defines the value number.
132 unsigned getNextValue(unsigned MIIdx, unsigned SrcReg) {
133 ValueNumberInfo.push_back(std::make_pair(MIIdx, SrcReg));
134 return ValueNumberInfo.size()-1;
137 /// getInstForValNum - Return the machine instruction index that defines the
138 /// specified value number.
139 unsigned getInstForValNum(unsigned ValNo) const {
140 //assert(ValNo < ValueNumberInfo.size());
141 return ValueNumberInfo[ValNo].first;
144 unsigned getSrcRegForValNum(unsigned ValNo) const {
145 //assert(ValNo < ValueNumberInfo.size());
146 if (ValueNumberInfo[ValNo].first < ~2U)
147 return ValueNumberInfo[ValNo].second;
151 std::pair<unsigned, unsigned> getValNumInfo(unsigned ValNo) const {
152 //assert(ValNo < ValueNumberInfo.size());
153 return ValueNumberInfo[ValNo];
156 /// setValueNumberInfo - Change the value number info for the specified
158 void setValueNumberInfo(unsigned ValNo,
159 const std::pair<unsigned, unsigned> &I){
160 ValueNumberInfo[ValNo] = I;
163 /// MergeValueNumberInto - This method is called when two value nubmers
164 /// are found to be equivalent. This eliminates V1, replacing all
165 /// LiveRanges with the V1 value number with the V2 value number. This can
166 /// cause merging of V1/V2 values numbers and compaction of the value space.
167 void MergeValueNumberInto(unsigned V1, unsigned V2);
169 /// MergeInClobberRanges - For any live ranges that are not defined in the
170 /// current interval, but are defined in the Clobbers interval, mark them
171 /// used with an unknown definition value.
172 void MergeInClobberRanges(const LiveInterval &Clobbers);
175 /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
176 /// interval as the specified value number. The LiveRanges in RHS are
177 /// allowed to overlap with LiveRanges in the current interval, but only if
178 /// the overlapping LiveRanges have the specified value number.
179 void MergeRangesInAsValue(const LiveInterval &RHS, unsigned LHSValNo);
181 bool empty() const { return ranges.empty(); }
183 /// beginNumber - Return the lowest numbered slot covered by interval.
184 unsigned beginNumber() const {
185 assert(!empty() && "empty interval for register");
186 return ranges.front().start;
189 /// endNumber - return the maximum point of the interval of the whole,
191 unsigned endNumber() const {
192 assert(!empty() && "empty interval for register");
193 return ranges.back().end;
196 bool expiredAt(unsigned index) const {
197 return index >= endNumber();
200 bool liveAt(unsigned index) const;
202 /// getLiveRangeContaining - Return the live range that contains the
203 /// specified index, or null if there is none.
204 const LiveRange *getLiveRangeContaining(unsigned Idx) const {
205 const_iterator I = FindLiveRangeContaining(Idx);
206 return I == end() ? 0 : &*I;
209 /// FindLiveRangeContaining - Return an iterator to the live range that
210 /// contains the specified index, or end() if there is none.
211 const_iterator FindLiveRangeContaining(unsigned Idx) const;
213 /// FindLiveRangeContaining - Return an iterator to the live range that
214 /// contains the specified index, or end() if there is none.
215 iterator FindLiveRangeContaining(unsigned Idx);
217 /// getOverlapingRanges - Given another live interval which is defined as a
218 /// copy from this one, return a list of all of the live ranges where the
219 /// two overlap and have different value numbers.
220 void getOverlapingRanges(const LiveInterval &Other, unsigned CopyIdx,
221 std::vector<LiveRange*> &Ranges);
223 /// overlaps - Return true if the intersection of the two live intervals is
225 bool overlaps(const LiveInterval& other) const {
226 return overlapsFrom(other, other.begin());
229 /// overlapsFrom - Return true if the intersection of the two live intervals
230 /// is not empty. The specified iterator is a hint that we can begin
231 /// scanning the Other interval starting at I.
232 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
234 /// addRange - Add the specified LiveRange to this interval, merging
235 /// intervals as appropriate. This returns an iterator to the inserted live
236 /// range (which may have grown since it was inserted.
237 void addRange(LiveRange LR) {
238 addRangeFrom(LR, ranges.begin());
241 /// join - Join two live intervals (this, and other) together. This applies
242 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
243 /// the intervals are not joinable, this aborts.
244 void join(LiveInterval &Other, int *ValNoAssignments,
245 int *RHSValNoAssignments,
246 SmallVector<std::pair<unsigned,unsigned>,16> &NewValueNumberInfo);
248 /// removeRange - Remove the specified range from this interval. Note that
249 /// the range must already be in this interval in its entirety.
250 void removeRange(unsigned Start, unsigned End);
252 void removeRange(LiveRange LR) {
253 removeRange(LR.start, LR.end);
256 bool operator<(const LiveInterval& other) const {
257 return beginNumber() < other.beginNumber();
260 void print(std::ostream &OS, const MRegisterInfo *MRI = 0) const;
261 void print(std::ostream *OS, const MRegisterInfo *MRI = 0) const {
262 if (OS) print(*OS, MRI);
267 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
268 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
269 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
270 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
273 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {