1 //===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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"
33 class TargetRegisterInfo;
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 /// copy - Copy iff val# is defined by a copy; zero otherwise.
41 /// hasPHIKill - One or more of the kills are PHI nodes.
42 /// kills - Instruction # of the kills.
48 SmallVector<unsigned, 4> kills;
49 VNInfo() : id(~1U), def(~1U), copy(0), hasPHIKill(false) {}
50 VNInfo(unsigned i, unsigned d, MachineInstr *c)
51 : id(i), def(d), copy(c), hasPHIKill(false) {}
54 /// LiveRange structure - This represents a simple register range in the
55 /// program, with an inclusive start point and an exclusive end point.
56 /// These ranges are rendered as [start,end).
58 unsigned start; // Start point of the interval (inclusive)
59 unsigned end; // End point of the interval (exclusive)
60 VNInfo *valno; // identifier for the value contained in this interval.
62 LiveRange(unsigned S, unsigned E, VNInfo *V) : start(S), end(E), valno(V) {
63 assert(S < E && "Cannot create empty or backwards range");
66 /// contains - Return true if the index is covered by this range.
68 bool contains(unsigned I) const {
69 return start <= I && I < end;
72 bool operator<(const LiveRange &LR) const {
73 return start < LR.start || (start == LR.start && end < LR.end);
75 bool operator==(const LiveRange &LR) const {
76 return start == LR.start && end == LR.end;
80 void print(std::ostream &os) const;
81 void print(std::ostream *os) const { if (os) print(*os); }
84 LiveRange(); // DO NOT IMPLEMENT
87 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
90 inline bool operator<(unsigned V, const LiveRange &LR) {
94 inline bool operator<(const LiveRange &LR, unsigned V) {
98 /// LiveInterval - This class represents some number of live ranges for a
99 /// register or value. This class also contains a bit of register allocator
101 struct LiveInterval {
102 typedef SmallVector<LiveRange,4> Ranges;
103 typedef SmallVector<VNInfo*,4> VNInfoList;
105 unsigned reg; // the register of this interval
106 unsigned preference; // preferred register to allocate for this interval
107 float weight; // weight of this interval
108 Ranges ranges; // the ranges in which this register is live
109 VNInfoList valnos; // value#'s
112 LiveInterval(unsigned Reg, float Weight)
113 : reg(Reg), preference(0), weight(Weight) {
116 typedef Ranges::iterator iterator;
117 iterator begin() { return ranges.begin(); }
118 iterator end() { return ranges.end(); }
120 typedef Ranges::const_iterator const_iterator;
121 const_iterator begin() const { return ranges.begin(); }
122 const_iterator end() const { return ranges.end(); }
124 typedef VNInfoList::iterator vni_iterator;
125 vni_iterator vni_begin() { return valnos.begin(); }
126 vni_iterator vni_end() { return valnos.end(); }
128 typedef VNInfoList::const_iterator const_vni_iterator;
129 const_vni_iterator vni_begin() const { return valnos.begin(); }
130 const_vni_iterator vni_end() const { return valnos.end(); }
132 /// advanceTo - Advance the specified iterator to point to the LiveRange
133 /// containing the specified position, or end() if the position is past the
134 /// end of the interval. If no LiveRange contains this position, but the
135 /// position is in a hole, this method returns an iterator pointing the the
136 /// LiveRange immediately after the hole.
137 iterator advanceTo(iterator I, unsigned Pos) {
138 if (Pos >= endNumber())
140 while (I->end <= Pos) ++I;
144 bool containsOneValue() const { return valnos.size() == 1; }
146 unsigned getNumValNums() const { return valnos.size(); }
148 /// getValNumInfo - Returns pointer to the specified val#.
150 inline VNInfo *getValNumInfo(unsigned ValNo) {
151 return valnos[ValNo];
153 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
154 return valnos[ValNo];
157 /// copyValNumInfo - Copy the value number info for one value number to
159 void copyValNumInfo(VNInfo *DstValNo, const VNInfo *SrcValNo) {
160 DstValNo->def = SrcValNo->def;
161 DstValNo->copy = SrcValNo->copy;
162 DstValNo->hasPHIKill = SrcValNo->hasPHIKill;
163 DstValNo->kills = SrcValNo->kills;
166 /// getNextValue - Create a new value number and return it. MIIdx specifies
167 /// the instruction that defines the value number.
168 VNInfo *getNextValue(unsigned MIIdx, MachineInstr *CopyMI,
169 BumpPtrAllocator &VNInfoAllocator) {
171 unsigned Alignment = __alignof__(VNInfo);
174 unsigned Alignment = 8;
176 VNInfo *VNI= static_cast<VNInfo*>(VNInfoAllocator.Allocate(sizeof(VNInfo),
178 new (VNI) VNInfo(valnos.size(), MIIdx, CopyMI);
179 valnos.push_back(VNI);
183 /// addKillForValNum - Add a kill instruction index to the specified value
185 static void addKill(VNInfo *VNI, unsigned KillIdx) {
186 SmallVector<unsigned, 4> &kills = VNI->kills;
188 kills.push_back(KillIdx);
190 SmallVector<unsigned, 4>::iterator
191 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
192 kills.insert(I, KillIdx);
196 /// addKills - Add a number of kills into the VNInfo kill vector. If this
197 /// interval is live at a kill point, then the kill is not added.
198 void addKills(VNInfo *VNI, const SmallVector<unsigned, 4> &kills) {
199 for (unsigned i = 0, e = kills.size(); i != e; ++i) {
200 unsigned KillIdx = kills[i];
201 if (!liveBeforeAndAt(KillIdx)) {
202 SmallVector<unsigned, 4>::iterator
203 I = std::lower_bound(VNI->kills.begin(), VNI->kills.end(), KillIdx);
204 VNI->kills.insert(I, KillIdx);
209 /// removeKill - Remove the specified kill from the list of kills of
210 /// the specified val#.
211 static bool removeKill(VNInfo *VNI, unsigned KillIdx) {
212 SmallVector<unsigned, 4> &kills = VNI->kills;
213 SmallVector<unsigned, 4>::iterator
214 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
215 if (I != kills.end() && *I == KillIdx) {
222 /// removeKills - Remove all the kills in specified range
223 /// [Start, End] of the specified val#.
224 void removeKills(VNInfo *VNI, unsigned Start, unsigned End) {
225 SmallVector<unsigned, 4> &kills = VNI->kills;
226 SmallVector<unsigned, 4>::iterator
227 I = std::lower_bound(kills.begin(), kills.end(), Start);
228 SmallVector<unsigned, 4>::iterator
229 E = std::upper_bound(kills.begin(), kills.end(), End);
233 /// isKill - Return true if the specified index is a kill of the
235 bool isKill(const VNInfo *VNI, unsigned KillIdx) const {
236 const SmallVector<unsigned, 4> &kills = VNI->kills;
237 SmallVector<unsigned, 4>::const_iterator
238 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
239 return I != kills.end() && *I == KillIdx;
242 /// MergeValueNumberInto - This method is called when two value nubmers
243 /// are found to be equivalent. This eliminates V1, replacing all
244 /// LiveRanges with the V1 value number with the V2 value number. This can
245 /// cause merging of V1/V2 values numbers and compaction of the value space.
246 void MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
248 /// MergeInClobberRanges - For any live ranges that are not defined in the
249 /// current interval, but are defined in the Clobbers interval, mark them
250 /// used with an unknown definition value. Caller must pass in reference to
251 /// VNInfoAllocator since it will create a new val#.
252 void MergeInClobberRanges(const LiveInterval &Clobbers,
253 BumpPtrAllocator &VNInfoAllocator);
255 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
256 /// in RHS into this live interval as the specified value number.
257 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
258 /// current interval, it will replace the value numbers of the overlaped
259 /// live ranges with the specified value number.
260 void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
262 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
263 /// in RHS into this live interval as the specified value number.
264 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
265 /// current interval, but only if the overlapping LiveRanges have the
266 /// specified value number.
267 void MergeValueInAsValue(const LiveInterval &RHS,
268 const VNInfo *RHSValNo, VNInfo *LHSValNo);
270 /// Copy - Copy the specified live interval. This copies all the fields
271 /// except for the register of the interval.
272 void Copy(const LiveInterval &RHS, BumpPtrAllocator &VNInfoAllocator);
274 bool empty() const { return ranges.empty(); }
276 /// beginNumber - Return the lowest numbered slot covered by interval.
277 unsigned beginNumber() const {
278 assert(!empty() && "empty interval for register");
279 return ranges.front().start;
282 /// endNumber - return the maximum point of the interval of the whole,
284 unsigned endNumber() const {
285 assert(!empty() && "empty interval for register");
286 return ranges.back().end;
289 bool expiredAt(unsigned index) const {
290 return index >= endNumber();
293 bool liveAt(unsigned index) const;
295 // liveBeforeAndAt - Check if the interval is live at the index and the
296 // index just before it. If index is liveAt, check if it starts a new live
297 // range.If it does, then check if the previous live range ends at index-1.
298 bool liveBeforeAndAt(unsigned index) const;
300 /// getLiveRangeContaining - Return the live range that contains the
301 /// specified index, or null if there is none.
302 const LiveRange *getLiveRangeContaining(unsigned Idx) const {
303 const_iterator I = FindLiveRangeContaining(Idx);
304 return I == end() ? 0 : &*I;
307 /// FindLiveRangeContaining - Return an iterator to the live range that
308 /// contains the specified index, or end() if there is none.
309 const_iterator FindLiveRangeContaining(unsigned Idx) const;
311 /// FindLiveRangeContaining - Return an iterator to the live range that
312 /// contains the specified index, or end() if there is none.
313 iterator FindLiveRangeContaining(unsigned Idx);
315 /// overlaps - Return true if the intersection of the two live intervals is
317 bool overlaps(const LiveInterval& other) const {
318 return overlapsFrom(other, other.begin());
321 /// overlapsFrom - Return true if the intersection of the two live intervals
322 /// is not empty. The specified iterator is a hint that we can begin
323 /// scanning the Other interval starting at I.
324 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
326 /// addRange - Add the specified LiveRange to this interval, merging
327 /// intervals as appropriate. This returns an iterator to the inserted live
328 /// range (which may have grown since it was inserted.
329 void addRange(LiveRange LR) {
330 addRangeFrom(LR, ranges.begin());
333 /// join - Join two live intervals (this, and other) together. This applies
334 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
335 /// the intervals are not joinable, this aborts.
336 void join(LiveInterval &Other, const int *ValNoAssignments,
337 const int *RHSValNoAssignments,
338 SmallVector<VNInfo*, 16> &NewVNInfo);
340 /// removeRange - Remove the specified range from this interval. Note that
341 /// the range must already be in this interval in its entirety.
342 void removeRange(unsigned Start, unsigned End, bool RemoveDeadValNo = false);
344 void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
345 removeRange(LR.start, LR.end, RemoveDeadValNo);
348 /// removeValNo - Remove all the ranges defined by the specified value#.
349 /// Also remove the value# from value# list.
350 void removeValNo(VNInfo *ValNo);
352 /// getSize - Returns the sum of sizes of all the LiveRange's.
354 unsigned getSize() const;
356 bool operator<(const LiveInterval& other) const {
357 return beginNumber() < other.beginNumber();
360 void print(std::ostream &OS, const TargetRegisterInfo *TRI = 0) const;
361 void print(std::ostream *OS, const TargetRegisterInfo *TRI = 0) const {
362 if (OS) print(*OS, TRI);
367 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
368 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
369 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
370 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
373 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {