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 Ranges ranges; // the ranges in which this register is live
88 /// ValueNumberInfo - If the value number definition is undefined (e.g. phi
89 /// merge point), it contains ~0u,x. If the value number is not in use, it
90 /// contains ~1u,x to indicate that the value # is not used.
91 /// def - Instruction # of the definition.
92 /// reg - Source reg iff val# is defined by a copy; zero otherwise.
93 /// kills - Instruction # of the kills. If a kill is an odd #, it means
94 /// the kill is a phi join point.
98 SmallVector<unsigned, 4> kills;
99 VNInfo() : def(~1U), reg(0) {};
100 VNInfo(unsigned d, unsigned r) : def(d), reg(r) {};
103 SmallVector<VNInfo, 4> ValueNumberInfo;
106 LiveInterval(unsigned Reg, float Weight)
107 : reg(Reg), preference(0), weight(Weight) {
110 typedef Ranges::iterator iterator;
111 iterator begin() { return ranges.begin(); }
112 iterator end() { return ranges.end(); }
114 typedef Ranges::const_iterator const_iterator;
115 const_iterator begin() const { return ranges.begin(); }
116 const_iterator end() const { return ranges.end(); }
119 /// advanceTo - Advance the specified iterator to point to the LiveRange
120 /// containing the specified position, or end() if the position is past the
121 /// end of the interval. If no LiveRange contains this position, but the
122 /// position is in a hole, this method returns an iterator pointing the the
123 /// LiveRange immediately after the hole.
124 iterator advanceTo(iterator I, unsigned Pos) {
125 if (Pos >= endNumber())
127 while (I->end <= Pos) ++I;
131 void swap(LiveInterval& other) {
132 std::swap(reg, other.reg);
133 std::swap(weight, other.weight);
134 std::swap(ranges, other.ranges);
135 std::swap(ValueNumberInfo, other.ValueNumberInfo);
138 bool containsOneValue() const { return ValueNumberInfo.size() == 1; }
140 unsigned getNumValNums() const { return ValueNumberInfo.size(); }
142 /// getNextValue - Create a new value number and return it. MIIdx specifies
143 /// the instruction that defines the value number.
144 unsigned getNextValue(unsigned MIIdx, unsigned SrcReg) {
145 ValueNumberInfo.push_back(VNInfo(MIIdx, SrcReg));
146 return ValueNumberInfo.size()-1;
149 /// getDefForValNum - Return the machine instruction index that defines the
150 /// specified value number.
151 unsigned getDefForValNum(unsigned ValNo) const {
152 assert(ValNo < ValueNumberInfo.size());
153 return ValueNumberInfo[ValNo].def;
156 /// getSrcRegForValNum - If the machine instruction that defines the
157 /// specified value number is a copy, returns the source register. Otherwise,
159 unsigned getSrcRegForValNum(unsigned ValNo) const {
160 assert(ValNo < ValueNumberInfo.size());
161 return ValueNumberInfo[ValNo].reg;
164 /// setDefForValNum - Set the machine instruction index that defines the
165 /// specified value number.
166 void setDefForValNum(unsigned ValNo, unsigned NewDef) {
167 assert(ValNo < ValueNumberInfo.size());
168 ValueNumberInfo[ValNo].def = NewDef;
171 /// setSrcRegForValNum - Set the source register of the specified value
173 void setSrcRegForValNum(unsigned ValNo, unsigned NewReg) {
174 assert(ValNo < ValueNumberInfo.size());
175 ValueNumberInfo[ValNo].reg = NewReg;
178 /// getKillsForValNum - Return the kill instruction indexes of the specified
180 const SmallVector<unsigned, 4> &getKillsForValNum(unsigned ValNo) const {
181 assert(ValNo < ValueNumberInfo.size());
182 return ValueNumberInfo[ValNo].kills;
185 /// addKillForValNum - Add a kill instruction index to the specified value
187 void addKillForValNum(unsigned ValNo, unsigned KillIdx) {
188 assert(ValNo < ValueNumberInfo.size());
189 SmallVector<unsigned, 4> &kills = ValueNumberInfo[ValNo].kills;
191 kills.push_back(KillIdx);
193 SmallVector<unsigned, 4>::iterator
194 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
195 kills.insert(I, KillIdx);
199 /// addKills - Add a number of kills into the VNInfo kill vector. If this
200 /// interval is live at a kill point, then the kill is not added.
201 void addKills(VNInfo &VNI, const SmallVector<unsigned, 4> &kills) {
202 for (unsigned i = 0, e = kills.size(); i != e; ++i) {
203 unsigned KillIdx = kills[i];
204 if (!liveAt(KillIdx)) {
205 SmallVector<unsigned, 4>::iterator
206 I = std::lower_bound(VNI.kills.begin(), VNI.kills.end(), KillIdx);
207 VNI.kills.insert(I, KillIdx);
212 /// addKillsForValNum - Add a number of kills into the kills vector of
213 /// the specified value number.
214 void addKillsForValNum(unsigned ValNo,
215 const SmallVector<unsigned, 4> &kills) {
216 addKills(ValueNumberInfo[ValNo], kills);
219 /// isKillForValNum - Returns true if KillIdx is a kill of the specified
221 bool isKillForValNum(unsigned ValNo, unsigned KillIdx) const {
222 assert(ValNo < ValueNumberInfo.size());
223 const SmallVector<unsigned, 4> &kills = ValueNumberInfo[ValNo].kills;
224 SmallVector<unsigned, 4>::const_iterator
225 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
226 if (I == kills.end())
228 return *I == KillIdx;
231 /// removeKill - Remove the specified kill from the list of kills of
232 /// the specified val#.
233 static bool removeKill(VNInfo &VNI, unsigned KillIdx) {
234 SmallVector<unsigned, 4> &kills = VNI.kills;
235 SmallVector<unsigned, 4>::iterator
236 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
237 if (I != kills.end() && *I == KillIdx) {
244 /// removeKillForValNum - Remove the specified kill from the list of kills
245 /// of the specified val#.
246 bool removeKillForValNum(unsigned ValNo, unsigned KillIdx) {
247 assert(ValNo < ValueNumberInfo.size());
248 return removeKill(ValueNumberInfo[ValNo], KillIdx);
251 /// removeKillForValNum - Remove all the kills in specified range
252 /// [Start, End] of the specified val#.
253 void removeKillForValNum(unsigned ValNo, unsigned Start, unsigned End) {
254 assert(ValNo < ValueNumberInfo.size());
255 SmallVector<unsigned, 4> &kills = ValueNumberInfo[ValNo].kills;
256 SmallVector<unsigned, 4>::iterator
257 I = std::lower_bound(kills.begin(), kills.end(), Start);
258 SmallVector<unsigned, 4>::iterator
259 E = std::upper_bound(kills.begin(), kills.end(), End);
263 /// replaceKill - Replace a kill index of the specified value# with a new
264 /// kill. Returns true if OldKill was indeed a kill point.
265 static bool replaceKill(VNInfo &VNI, unsigned OldKill, unsigned NewKill) {
266 SmallVector<unsigned, 4> &kills = VNI.kills;
267 SmallVector<unsigned, 4>::iterator
268 I = std::lower_bound(kills.begin(), kills.end(), OldKill);
269 if (I != kills.end() && *I == OldKill) {
276 /// replaceKillForValNum - Replace a kill index of the specified value# with
277 /// a new kill. Returns true if OldKill was indeed a kill point.
278 bool replaceKillForValNum(unsigned ValNo, unsigned OldKill,
280 assert(ValNo < ValueNumberInfo.size());
281 return replaceKill(ValueNumberInfo[ValNo], OldKill, NewKill);
284 /// getValNumInfo - Returns a copy of the specified val#.
286 VNInfo getValNumInfo(unsigned ValNo) const {
287 assert(ValNo < ValueNumberInfo.size());
288 return ValueNumberInfo[ValNo];
291 /// setValNumInfo - Change the value number info for the specified
293 void setValNumInfo(unsigned ValNo, const VNInfo &I) {
294 ValueNumberInfo[ValNo] = I;
297 /// copyValNumInfo - Copy the value number info for one value number to
299 void copyValNumInfo(unsigned DstValNo, unsigned SrcValNo) {
300 ValueNumberInfo[DstValNo] = ValueNumberInfo[SrcValNo];
302 void copyValNumInfo(unsigned DstValNo, const LiveInterval &SrcLI,
304 ValueNumberInfo[DstValNo] = SrcLI.ValueNumberInfo[SrcValNo];
307 /// MergeValueNumberInto - This method is called when two value nubmers
308 /// are found to be equivalent. This eliminates V1, replacing all
309 /// LiveRanges with the V1 value number with the V2 value number. This can
310 /// cause merging of V1/V2 values numbers and compaction of the value space.
311 void MergeValueNumberInto(unsigned V1, unsigned V2);
313 /// MergeInClobberRanges - For any live ranges that are not defined in the
314 /// current interval, but are defined in the Clobbers interval, mark them
315 /// used with an unknown definition value.
316 void MergeInClobberRanges(const LiveInterval &Clobbers);
319 /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
320 /// interval as the specified value number. The LiveRanges in RHS are
321 /// allowed to overlap with LiveRanges in the current interval, but only if
322 /// the overlapping LiveRanges have the specified value number.
323 void MergeRangesInAsValue(const LiveInterval &RHS, unsigned LHSValNo);
325 bool empty() const { return ranges.empty(); }
327 /// beginNumber - Return the lowest numbered slot covered by interval.
328 unsigned beginNumber() const {
329 assert(!empty() && "empty interval for register");
330 return ranges.front().start;
333 /// endNumber - return the maximum point of the interval of the whole,
335 unsigned endNumber() const {
336 assert(!empty() && "empty interval for register");
337 return ranges.back().end;
340 bool expiredAt(unsigned index) const {
341 return index >= endNumber();
344 bool liveAt(unsigned index) const;
346 /// getLiveRangeContaining - Return the live range that contains the
347 /// specified index, or null if there is none.
348 const LiveRange *getLiveRangeContaining(unsigned Idx) const {
349 const_iterator I = FindLiveRangeContaining(Idx);
350 return I == end() ? 0 : &*I;
353 /// FindLiveRangeContaining - Return an iterator to the live range that
354 /// contains the specified index, or end() if there is none.
355 const_iterator FindLiveRangeContaining(unsigned Idx) const;
357 /// FindLiveRangeContaining - Return an iterator to the live range that
358 /// contains the specified index, or end() if there is none.
359 iterator FindLiveRangeContaining(unsigned Idx);
361 /// getOverlapingRanges - Given another live interval which is defined as a
362 /// copy from this one, return a list of all of the live ranges where the
363 /// two overlap and have different value numbers.
364 void getOverlapingRanges(const LiveInterval &Other, unsigned CopyIdx,
365 std::vector<LiveRange*> &Ranges);
367 /// overlaps - Return true if the intersection of the two live intervals is
369 bool overlaps(const LiveInterval& other) const {
370 return overlapsFrom(other, other.begin());
373 /// overlapsFrom - Return true if the intersection of the two live intervals
374 /// is not empty. The specified iterator is a hint that we can begin
375 /// scanning the Other interval starting at I.
376 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
378 /// addRange - Add the specified LiveRange to this interval, merging
379 /// intervals as appropriate. This returns an iterator to the inserted live
380 /// range (which may have grown since it was inserted.
381 void addRange(LiveRange LR) {
382 addRangeFrom(LR, ranges.begin());
385 /// join - Join two live intervals (this, and other) together. This applies
386 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
387 /// the intervals are not joinable, this aborts.
388 void join(LiveInterval &Other, int *ValNoAssignments,
389 int *RHSValNoAssignments,
390 SmallVector<VNInfo,16> &NewValueNumberInfo);
392 /// removeRange - Remove the specified range from this interval. Note that
393 /// the range must already be in this interval in its entirety.
394 void removeRange(unsigned Start, unsigned End);
396 void removeRange(LiveRange LR) {
397 removeRange(LR.start, LR.end);
400 /// getSize - Returns the sum of sizes of all the LiveRange's.
402 unsigned getSize() const;
404 bool operator<(const LiveInterval& other) const {
405 return beginNumber() < other.beginNumber();
408 void print(std::ostream &OS, const MRegisterInfo *MRI = 0) const;
409 void print(std::ostream *OS, const MRegisterInfo *MRI = 0) const {
410 if (OS) print(*OS, MRI);
415 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
416 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
417 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
418 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
421 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {