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/Allocator.h"
26 #include "llvm/Support/Streams.h"
36 /// VNInfo - If the value number definition is undefined (e.g. phi
37 /// merge point), it contains ~0u,x. If the value number is not in use, it
38 /// contains ~1u,x to indicate that the value # is not used.
39 /// def - Instruction # of the definition.
40 /// reg - Source reg iff val# is defined by a copy; zero otherwise.
41 /// kills - Instruction # of the kills. If a kill is an odd #, it means
42 /// the kill is a phi join point.
47 SmallVector<unsigned, 4> kills;
48 VNInfo() : id(~1U), def(~1U), reg(0) {}
49 VNInfo(unsigned i, unsigned d, unsigned r)
50 : id(i), def(d), reg(r) {}
53 /// LiveRange structure - This represents a simple register range in the
54 /// program, with an inclusive start point and an exclusive end point.
55 /// These ranges are rendered as [start,end).
57 unsigned start; // Start point of the interval (inclusive)
58 unsigned end; // End point of the interval (exclusive)
59 VNInfo *valno; // identifier for the value contained in this interval.
61 LiveRange(unsigned S, unsigned E, VNInfo *V) : start(S), end(E), valno(V) {
62 assert(S < E && "Cannot create empty or backwards range");
65 /// contains - Return true if the index is covered by this range.
67 bool contains(unsigned I) const {
68 return start <= I && I < end;
71 bool operator<(const LiveRange &LR) const {
72 return start < LR.start || (start == LR.start && end < LR.end);
74 bool operator==(const LiveRange &LR) const {
75 return start == LR.start && end == LR.end;
79 void print(std::ostream &os) const;
80 void print(std::ostream *os) const { if (os) print(*os); }
83 LiveRange(); // DO NOT IMPLEMENT
86 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
89 inline bool operator<(unsigned V, const LiveRange &LR) {
93 inline bool operator<(const LiveRange &LR, unsigned V) {
97 /// LiveInterval - This class represents some number of live ranges for a
98 /// register or value. This class also contains a bit of register allocator
100 struct LiveInterval {
101 typedef SmallVector<LiveRange,4> Ranges;
102 typedef SmallVector<VNInfo*,4> VNInfoList;
104 unsigned reg; // the register of this interval
105 unsigned preference; // preferred register to allocate for this interval
106 float weight; // weight of this interval
107 Ranges ranges; // the ranges in which this register is live
108 VNInfoList valnos; // value#'s
111 LiveInterval(unsigned Reg, float Weight)
112 : reg(Reg), preference(0), weight(Weight) {
115 typedef Ranges::iterator iterator;
116 iterator begin() { return ranges.begin(); }
117 iterator end() { return ranges.end(); }
119 typedef Ranges::const_iterator const_iterator;
120 const_iterator begin() const { return ranges.begin(); }
121 const_iterator end() const { return ranges.end(); }
123 typedef VNInfoList::iterator vni_iterator;
124 vni_iterator vni_begin() { return valnos.begin(); }
125 vni_iterator vni_end() { return valnos.end(); }
127 typedef VNInfoList::const_iterator const_vni_iterator;
128 const_vni_iterator vni_begin() const { return valnos.begin(); }
129 const_vni_iterator vni_end() const { return valnos.end(); }
131 /// advanceTo - Advance the specified iterator to point to the LiveRange
132 /// containing the specified position, or end() if the position is past the
133 /// end of the interval. If no LiveRange contains this position, but the
134 /// position is in a hole, this method returns an iterator pointing the the
135 /// LiveRange immediately after the hole.
136 iterator advanceTo(iterator I, unsigned Pos) {
137 if (Pos >= endNumber())
139 while (I->end <= Pos) ++I;
143 bool containsOneValue() const { return valnos.size() == 1; }
145 unsigned getNumValNums() const { return valnos.size(); }
147 /// getValNumInfo - Returns pointer to the specified val#.
149 inline VNInfo *getValNumInfo(unsigned ValNo) {
150 return valnos[ValNo];
152 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
153 return valnos[ValNo];
156 /// copyValNumInfo - Copy the value number info for one value number to
158 void copyValNumInfo(VNInfo *DstValNo, VNInfo *SrcValNo) {
159 DstValNo->def = SrcValNo->def;
160 DstValNo->reg = SrcValNo->reg;
161 DstValNo->kills = SrcValNo->kills;
164 /// getNextValue - Create a new value number and return it. MIIdx specifies
165 /// the instruction that defines the value number.
166 VNInfo *getNextValue(unsigned MIIdx, unsigned SrcReg,
167 BumpPtrAllocator &VNInfoAllocator) {
169 unsigned Alignment = __alignof__(VNInfo);
172 unsigned Alignment = 8;
174 VNInfo *VNI= static_cast<VNInfo*>(VNInfoAllocator.Allocate(sizeof(VNInfo),
176 new (VNI) VNInfo(valnos.size(), MIIdx, SrcReg);
177 valnos.push_back(VNI);
181 /// addKillForValNum - Add a kill instruction index to the specified value
183 static void addKill(VNInfo *VNI, unsigned KillIdx) {
184 SmallVector<unsigned, 4> &kills = VNI->kills;
186 kills.push_back(KillIdx);
188 SmallVector<unsigned, 4>::iterator
189 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
190 kills.insert(I, KillIdx);
194 /// addKills - Add a number of kills into the VNInfo kill vector. If this
195 /// interval is live at a kill point, then the kill is not added.
196 void addKills(VNInfo *VNI, const SmallVector<unsigned, 4> &kills) {
197 for (unsigned i = 0, e = kills.size(); i != e; ++i) {
198 unsigned KillIdx = kills[i];
199 if (!liveAt(KillIdx)) {
200 SmallVector<unsigned, 4>::iterator
201 I = std::lower_bound(VNI->kills.begin(), VNI->kills.end(), KillIdx);
202 VNI->kills.insert(I, KillIdx);
207 /// removeKill - Remove the specified kill from the list of kills of
208 /// the specified val#.
209 static bool removeKill(VNInfo *VNI, unsigned KillIdx) {
210 SmallVector<unsigned, 4> &kills = VNI->kills;
211 SmallVector<unsigned, 4>::iterator
212 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
213 if (I != kills.end() && *I == KillIdx) {
220 /// removeKills - Remove all the kills in specified range
221 /// [Start, End] of the specified val#.
222 void removeKills(VNInfo *VNI, unsigned Start, unsigned End) {
223 SmallVector<unsigned, 4> &kills = VNI->kills;
224 SmallVector<unsigned, 4>::iterator
225 I = std::lower_bound(kills.begin(), kills.end(), Start);
226 SmallVector<unsigned, 4>::iterator
227 E = std::upper_bound(kills.begin(), kills.end(), End);
231 /// MergeValueNumberInto - This method is called when two value nubmers
232 /// are found to be equivalent. This eliminates V1, replacing all
233 /// LiveRanges with the V1 value number with the V2 value number. This can
234 /// cause merging of V1/V2 values numbers and compaction of the value space.
235 void MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
237 /// MergeInClobberRanges - For any live ranges that are not defined in the
238 /// current interval, but are defined in the Clobbers interval, mark them
239 /// used with an unknown definition value. Caller must pass in reference to
240 /// VNInfoAllocator since it will create a new val#.
241 void MergeInClobberRanges(const LiveInterval &Clobbers,
242 BumpPtrAllocator &VNInfoAllocator);
244 /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
245 /// interval as the specified value number. The LiveRanges in RHS are
246 /// allowed to overlap with LiveRanges in the current interval, but only if
247 /// the overlapping LiveRanges have the specified value number.
248 void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
250 bool empty() const { return ranges.empty(); }
252 /// beginNumber - Return the lowest numbered slot covered by interval.
253 unsigned beginNumber() const {
254 assert(!empty() && "empty interval for register");
255 return ranges.front().start;
258 /// endNumber - return the maximum point of the interval of the whole,
260 unsigned endNumber() const {
261 assert(!empty() && "empty interval for register");
262 return ranges.back().end;
265 bool expiredAt(unsigned index) const {
266 return index >= endNumber();
269 bool liveAt(unsigned index) const;
271 /// getLiveRangeContaining - Return the live range that contains the
272 /// specified index, or null if there is none.
273 const LiveRange *getLiveRangeContaining(unsigned Idx) const {
274 const_iterator I = FindLiveRangeContaining(Idx);
275 return I == end() ? 0 : &*I;
278 /// FindLiveRangeContaining - Return an iterator to the live range that
279 /// contains the specified index, or end() if there is none.
280 const_iterator FindLiveRangeContaining(unsigned Idx) const;
282 /// FindLiveRangeContaining - Return an iterator to the live range that
283 /// contains the specified index, or end() if there is none.
284 iterator FindLiveRangeContaining(unsigned Idx);
286 /// getOverlapingRanges - Given another live interval which is defined as a
287 /// copy from this one, return a list of all of the live ranges where the
288 /// two overlap and have different value numbers.
289 void getOverlapingRanges(const LiveInterval &Other, unsigned CopyIdx,
290 std::vector<LiveRange*> &Ranges);
292 /// overlaps - Return true if the intersection of the two live intervals is
294 bool overlaps(const LiveInterval& other) const {
295 return overlapsFrom(other, other.begin());
298 /// overlapsFrom - Return true if the intersection of the two live intervals
299 /// is not empty. The specified iterator is a hint that we can begin
300 /// scanning the Other interval starting at I.
301 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
303 /// addRange - Add the specified LiveRange to this interval, merging
304 /// intervals as appropriate. This returns an iterator to the inserted live
305 /// range (which may have grown since it was inserted.
306 void addRange(LiveRange LR) {
307 addRangeFrom(LR, ranges.begin());
310 /// join - Join two live intervals (this, and other) together. This applies
311 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
312 /// the intervals are not joinable, this aborts.
313 void join(LiveInterval &Other, const int *ValNoAssignments,
314 const int *RHSValNoAssignments,
315 SmallVector<VNInfo*, 16> &NewVNInfo);
317 /// removeRange - Remove the specified range from this interval. Note that
318 /// the range must already be in this interval in its entirety.
319 void removeRange(unsigned Start, unsigned End);
321 void removeRange(LiveRange LR) {
322 removeRange(LR.start, LR.end);
325 /// getSize - Returns the sum of sizes of all the LiveRange's.
327 unsigned getSize() const;
329 bool operator<(const LiveInterval& other) const {
330 return beginNumber() < other.beginNumber();
333 void print(std::ostream &OS, const MRegisterInfo *MRI = 0) const;
334 void print(std::ostream *OS, const MRegisterInfo *MRI = 0) const {
335 if (OS) print(*OS, MRI);
340 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
341 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
342 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
343 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
346 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {