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"
31 class TargetRegisterInfo;
34 /// VNInfo - If the value number definition is undefined (e.g. phi
35 /// merge point), it contains ~0u,x. If the value number is not in use, it
36 /// contains ~1u,x to indicate that the value # is not used.
37 /// def - Instruction # of the definition.
38 /// copy - Copy iff val# is defined by a copy; zero otherwise.
39 /// hasPHIKill - One or more of the kills are PHI nodes.
40 /// kills - Instruction # of the kills.
46 SmallVector<unsigned, 4> kills;
47 VNInfo() : id(~1U), def(~1U), copy(0), hasPHIKill(false) {}
48 VNInfo(unsigned i, unsigned d, MachineInstr *c)
49 : id(i), def(d), copy(c), hasPHIKill(false) {}
52 /// LiveRange structure - This represents a simple register range in the
53 /// program, with an inclusive start point and an exclusive end point.
54 /// These ranges are rendered as [start,end).
56 unsigned start; // Start point of the interval (inclusive)
57 unsigned end; // End point of the interval (exclusive)
58 VNInfo *valno; // identifier for the value contained in this interval.
60 LiveRange(unsigned S, unsigned E, VNInfo *V) : start(S), end(E), valno(V) {
61 assert(S < E && "Cannot create empty or backwards range");
64 /// contains - Return true if the index is covered by this range.
66 bool contains(unsigned I) const {
67 return start <= I && I < end;
70 bool operator<(const LiveRange &LR) const {
71 return start < LR.start || (start == LR.start && end < LR.end);
73 bool operator==(const LiveRange &LR) const {
74 return start == LR.start && end == LR.end;
78 void print(std::ostream &os) const;
79 void print(std::ostream *os) const { if (os) print(*os); }
82 LiveRange(); // DO NOT IMPLEMENT
85 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
88 inline bool operator<(unsigned V, const LiveRange &LR) {
92 inline bool operator<(const LiveRange &LR, unsigned V) {
96 /// LiveInterval - This class represents some number of live ranges for a
97 /// register or value. This class also contains a bit of register allocator
100 typedef SmallVector<LiveRange,4> Ranges;
101 typedef SmallVector<VNInfo*,4> VNInfoList;
103 unsigned reg; // the register of this interval
104 unsigned preference; // preferred register to allocate for this interval
105 float weight; // weight of this interval
106 Ranges ranges; // the ranges in which this register is live
107 VNInfoList valnos; // value#'s
110 LiveInterval(unsigned Reg, float Weight)
111 : reg(Reg), preference(0), weight(Weight) {
114 typedef Ranges::iterator iterator;
115 iterator begin() { return ranges.begin(); }
116 iterator end() { return ranges.end(); }
118 typedef Ranges::const_iterator const_iterator;
119 const_iterator begin() const { return ranges.begin(); }
120 const_iterator end() const { return ranges.end(); }
122 typedef VNInfoList::iterator vni_iterator;
123 vni_iterator vni_begin() { return valnos.begin(); }
124 vni_iterator vni_end() { return valnos.end(); }
126 typedef VNInfoList::const_iterator const_vni_iterator;
127 const_vni_iterator vni_begin() const { return valnos.begin(); }
128 const_vni_iterator vni_end() const { return valnos.end(); }
130 /// advanceTo - Advance the specified iterator to point to the LiveRange
131 /// containing the specified position, or end() if the position is past the
132 /// end of the interval. If no LiveRange contains this position, but the
133 /// position is in a hole, this method returns an iterator pointing the the
134 /// LiveRange immediately after the hole.
135 iterator advanceTo(iterator I, unsigned Pos) {
136 if (Pos >= endNumber())
138 while (I->end <= Pos) ++I;
142 bool containsOneValue() const { return valnos.size() == 1; }
144 unsigned getNumValNums() const { return (unsigned)valnos.size(); }
146 /// getValNumInfo - Returns pointer to the specified val#.
148 inline VNInfo *getValNumInfo(unsigned ValNo) {
149 return valnos[ValNo];
151 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
152 return valnos[ValNo];
155 /// copyValNumInfo - Copy the value number info for one value number to
157 void copyValNumInfo(VNInfo *DstValNo, const VNInfo *SrcValNo) {
158 DstValNo->def = SrcValNo->def;
159 DstValNo->copy = SrcValNo->copy;
160 DstValNo->hasPHIKill = SrcValNo->hasPHIKill;
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, MachineInstr *CopyMI,
167 BumpPtrAllocator &VNInfoAllocator) {
169 unsigned Alignment = (unsigned)__alignof__(VNInfo);
172 unsigned Alignment = 8;
175 static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
177 new (VNI) VNInfo((unsigned)valnos.size(), MIIdx, CopyMI);
178 valnos.push_back(VNI);
182 /// addKillForValNum - Add a kill instruction index to the specified value
184 static void addKill(VNInfo *VNI, unsigned KillIdx) {
185 SmallVector<unsigned, 4> &kills = VNI->kills;
187 kills.push_back(KillIdx);
189 SmallVector<unsigned, 4>::iterator
190 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
191 kills.insert(I, KillIdx);
195 /// addKills - Add a number of kills into the VNInfo kill vector. If this
196 /// interval is live at a kill point, then the kill is not added.
197 void addKills(VNInfo *VNI, const SmallVector<unsigned, 4> &kills) {
198 for (unsigned i = 0, e = static_cast<unsigned>(kills.size());
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 {
280 return ranges.front().start;
283 /// endNumber - return the maximum point of the interval of the whole,
285 unsigned endNumber() const {
288 return ranges.back().end;
291 bool expiredAt(unsigned index) const {
292 return index >= endNumber();
295 bool liveAt(unsigned index) const;
297 // liveBeforeAndAt - Check if the interval is live at the index and the
298 // index just before it. If index is liveAt, check if it starts a new live
299 // range.If it does, then check if the previous live range ends at index-1.
300 bool liveBeforeAndAt(unsigned index) const;
302 /// getLiveRangeContaining - Return the live range that contains the
303 /// specified index, or null if there is none.
304 const LiveRange *getLiveRangeContaining(unsigned Idx) const {
305 const_iterator I = FindLiveRangeContaining(Idx);
306 return I == end() ? 0 : &*I;
309 /// FindLiveRangeContaining - Return an iterator to the live range that
310 /// contains the specified index, or end() if there is none.
311 const_iterator FindLiveRangeContaining(unsigned Idx) const;
313 /// FindLiveRangeContaining - Return an iterator to the live range that
314 /// contains the specified index, or end() if there is none.
315 iterator FindLiveRangeContaining(unsigned Idx);
317 /// overlaps - Return true if the intersection of the two live intervals is
319 bool overlaps(const LiveInterval& other) const {
320 return overlapsFrom(other, other.begin());
323 /// overlapsFrom - Return true if the intersection of the two live intervals
324 /// is not empty. The specified iterator is a hint that we can begin
325 /// scanning the Other interval starting at I.
326 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
328 /// addRange - Add the specified LiveRange to this interval, merging
329 /// intervals as appropriate. This returns an iterator to the inserted live
330 /// range (which may have grown since it was inserted.
331 void addRange(LiveRange LR) {
332 addRangeFrom(LR, ranges.begin());
335 /// join - Join two live intervals (this, and other) together. This applies
336 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
337 /// the intervals are not joinable, this aborts.
338 void join(LiveInterval &Other, const int *ValNoAssignments,
339 const int *RHSValNoAssignments,
340 SmallVector<VNInfo*, 16> &NewVNInfo);
342 /// removeRange - Remove the specified range from this interval. Note that
343 /// the range must already be in this interval in its entirety.
344 void removeRange(unsigned Start, unsigned End, bool RemoveDeadValNo = false);
346 void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
347 removeRange(LR.start, LR.end, RemoveDeadValNo);
350 /// removeValNo - Remove all the ranges defined by the specified value#.
351 /// Also remove the value# from value# list.
352 void removeValNo(VNInfo *ValNo);
354 /// getSize - Returns the sum of sizes of all the LiveRange's.
356 unsigned getSize() const;
358 bool operator<(const LiveInterval& other) const {
359 return beginNumber() < other.beginNumber();
362 void print(std::ostream &OS, const TargetRegisterInfo *TRI = 0) const;
363 void print(std::ostream *OS, const TargetRegisterInfo *TRI = 0) const {
364 if (OS) print(*OS, TRI);
369 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
370 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
371 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
372 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
375 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {