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;
60 void print(std::ostream &os) const;
61 void print(std::ostream *os) const { if (os) print(*os); }
64 LiveRange(); // DO NOT IMPLEMENT
67 std::ostream& operator<<(std::ostream& os, const LiveRange &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 unsigned preference; // preferred register to allocate for this interval
85 float weight; // weight of this interval
86 MachineInstr* remat; // definition if the definition rematerializable
87 Ranges ranges; // the ranges in which this register is live
89 /// ValueNumberInfo - If the value number definition is undefined (e.g. phi
90 /// merge point), it contains ~0u,x. If the value number is not in use, it
91 /// contains ~1u,x to indicate that the value # is not used.
93 unsigned def; // instruction # of the definition
94 unsigned reg; // src reg: non-zero iff val# is defined by a copy
95 SmallVector<unsigned, 4> kills; // instruction #s of the kills
96 VNInfo() : def(~1U), reg(0) {};
97 VNInfo(unsigned d, unsigned r) : def(d), reg(r) {};
100 SmallVector<VNInfo, 4> ValueNumberInfo;
103 LiveInterval(unsigned Reg, float Weight)
104 : reg(Reg), preference(0), weight(Weight), remat(NULL) {
107 typedef Ranges::iterator iterator;
108 iterator begin() { return ranges.begin(); }
109 iterator end() { return ranges.end(); }
111 typedef Ranges::const_iterator const_iterator;
112 const_iterator begin() const { return ranges.begin(); }
113 const_iterator end() const { return ranges.end(); }
116 /// advanceTo - Advance the specified iterator to point to the LiveRange
117 /// containing the specified position, or end() if the position is past the
118 /// end of the interval. If no LiveRange contains this position, but the
119 /// position is in a hole, this method returns an iterator pointing the the
120 /// LiveRange immediately after the hole.
121 iterator advanceTo(iterator I, unsigned Pos) {
122 if (Pos >= endNumber())
124 while (I->end <= Pos) ++I;
128 void swap(LiveInterval& other) {
129 std::swap(reg, other.reg);
130 std::swap(weight, other.weight);
131 std::swap(remat, other.remat);
132 std::swap(ranges, other.ranges);
133 std::swap(ValueNumberInfo, other.ValueNumberInfo);
136 bool containsOneValue() const { return ValueNumberInfo.size() == 1; }
138 unsigned getNumValNums() const { return ValueNumberInfo.size(); }
140 /// getNextValue - Create a new value number and return it. MIIdx specifies
141 /// the instruction that defines the value number.
142 unsigned getNextValue(unsigned MIIdx, unsigned SrcReg) {
143 ValueNumberInfo.push_back(VNInfo(MIIdx, SrcReg));
144 return ValueNumberInfo.size()-1;
147 /// getInstForValNum - Return the machine instruction index that defines the
148 /// specified value number.
149 unsigned getInstForValNum(unsigned ValNo) const {
150 assert(ValNo < ValueNumberInfo.size());
151 return ValueNumberInfo[ValNo].def;
154 unsigned getSrcRegForValNum(unsigned ValNo) const {
155 assert(ValNo < ValueNumberInfo.size());
156 return ValueNumberInfo[ValNo].reg;
159 /// getKillsForValNum - Return the kill instruction indexes of the specified
161 const SmallVector<unsigned, 4> &getKillsForValNum(unsigned ValNo) const {
162 assert(ValNo < ValueNumberInfo.size());
163 return ValueNumberInfo[ValNo].kills;
166 /// addKillForValNum - Add a kill instruction index to the specified value
168 void addKillForValNum(unsigned ValNo, unsigned KillIdx) {
169 assert(ValNo < ValueNumberInfo.size());
170 ValueNumberInfo[ValNo].kills.push_back(KillIdx);
173 /// replaceKillForValNum - Replace a kill index of the specified value with
174 /// a new kill index.
175 bool replaceKillForValNum(unsigned ValNo, unsigned OldKill,
177 SmallVector<unsigned, 4> kills = ValueNumberInfo[ValNo].kills;
178 SmallVector<unsigned, 4>::iterator I =
179 std::find(kills.begin(), kills.end(), OldKill);
180 if (I == kills.end())
183 kills.push_back(NewKill);
187 VNInfo getValNumInfo(unsigned ValNo) const {
188 assert(ValNo < ValueNumberInfo.size());
189 return ValueNumberInfo[ValNo];
192 /// setValueNumberInfo - Change the value number info for the specified
194 void setValueNumberInfo(unsigned ValNo, const VNInfo &I) {
195 ValueNumberInfo[ValNo] = I;
198 /// MergeValueNumberInto - This method is called when two value nubmers
199 /// are found to be equivalent. This eliminates V1, replacing all
200 /// LiveRanges with the V1 value number with the V2 value number. This can
201 /// cause merging of V1/V2 values numbers and compaction of the value space.
202 void MergeValueNumberInto(unsigned V1, unsigned V2);
204 /// MergeInClobberRanges - For any live ranges that are not defined in the
205 /// current interval, but are defined in the Clobbers interval, mark them
206 /// used with an unknown definition value.
207 void MergeInClobberRanges(const LiveInterval &Clobbers);
210 /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
211 /// interval as the specified value number. The LiveRanges in RHS are
212 /// allowed to overlap with LiveRanges in the current interval, but only if
213 /// the overlapping LiveRanges have the specified value number.
214 void MergeRangesInAsValue(const LiveInterval &RHS, unsigned LHSValNo);
216 bool empty() const { return ranges.empty(); }
218 /// beginNumber - Return the lowest numbered slot covered by interval.
219 unsigned beginNumber() const {
220 assert(!empty() && "empty interval for register");
221 return ranges.front().start;
224 /// endNumber - return the maximum point of the interval of the whole,
226 unsigned endNumber() const {
227 assert(!empty() && "empty interval for register");
228 return ranges.back().end;
231 bool expiredAt(unsigned index) const {
232 return index >= endNumber();
235 bool liveAt(unsigned index) const;
237 /// getLiveRangeContaining - Return the live range that contains the
238 /// specified index, or null if there is none.
239 const LiveRange *getLiveRangeContaining(unsigned Idx) const {
240 const_iterator I = FindLiveRangeContaining(Idx);
241 return I == end() ? 0 : &*I;
244 /// FindLiveRangeContaining - Return an iterator to the live range that
245 /// contains the specified index, or end() if there is none.
246 const_iterator FindLiveRangeContaining(unsigned Idx) const;
248 /// FindLiveRangeContaining - Return an iterator to the live range that
249 /// contains the specified index, or end() if there is none.
250 iterator FindLiveRangeContaining(unsigned Idx);
252 /// getOverlapingRanges - Given another live interval which is defined as a
253 /// copy from this one, return a list of all of the live ranges where the
254 /// two overlap and have different value numbers.
255 void getOverlapingRanges(const LiveInterval &Other, unsigned CopyIdx,
256 std::vector<LiveRange*> &Ranges);
258 /// overlaps - Return true if the intersection of the two live intervals is
260 bool overlaps(const LiveInterval& other) const {
261 return overlapsFrom(other, other.begin());
264 /// overlapsFrom - Return true if the intersection of the two live intervals
265 /// is not empty. The specified iterator is a hint that we can begin
266 /// scanning the Other interval starting at I.
267 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
269 /// addRange - Add the specified LiveRange to this interval, merging
270 /// intervals as appropriate. This returns an iterator to the inserted live
271 /// range (which may have grown since it was inserted.
272 void addRange(LiveRange LR) {
273 addRangeFrom(LR, ranges.begin());
276 /// join - Join two live intervals (this, and other) together. This applies
277 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
278 /// the intervals are not joinable, this aborts.
279 void join(LiveInterval &Other, int *ValNoAssignments,
280 int *RHSValNoAssignments,
281 SmallVector<VNInfo,16> &NewValueNumberInfo);
283 /// removeRange - Remove the specified range from this interval. Note that
284 /// the range must already be in this interval in its entirety.
285 void removeRange(unsigned Start, unsigned End);
287 void removeRange(LiveRange LR) {
288 removeRange(LR.start, LR.end);
291 /// getSize - Returns the sum of sizes of all the LiveRange's.
293 unsigned getSize() const;
295 bool operator<(const LiveInterval& other) const {
296 return beginNumber() < other.beginNumber();
299 void print(std::ostream &OS, const MRegisterInfo *MRI = 0) const;
300 void print(std::ostream *OS, const MRegisterInfo *MRI = 0) const {
301 if (OS) print(*OS, MRI);
306 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
307 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
308 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
309 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
312 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {