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 /// - or reg # of the definition if it's a stack slot liveinterval.
39 /// copy - Copy iff val# is defined by a copy; zero otherwise.
40 /// hasPHIKill - One or more of the kills are PHI nodes.
41 /// redefByEC - Re-defined by early clobber somewhere during the live range.
42 /// kills - Instruction # of the kills.
49 SmallVector<unsigned, 4> kills;
51 : id(~1U), def(~1U), copy(0), hasPHIKill(false), redefByEC(false) {}
52 VNInfo(unsigned i, unsigned d, MachineInstr *c)
53 : id(i), def(d), copy(c), hasPHIKill(false), redefByEC(false) {}
56 /// LiveRange structure - This represents a simple register range in the
57 /// program, with an inclusive start point and an exclusive end point.
58 /// These ranges are rendered as [start,end).
60 unsigned start; // Start point of the interval (inclusive)
61 unsigned end; // End point of the interval (exclusive)
62 VNInfo *valno; // identifier for the value contained in this interval.
64 LiveRange(unsigned S, unsigned E, VNInfo *V) : start(S), end(E), valno(V) {
65 assert(S < E && "Cannot create empty or backwards range");
68 /// contains - Return true if the index is covered by this range.
70 bool contains(unsigned I) const {
71 return start <= I && I < end;
74 bool operator<(const LiveRange &LR) const {
75 return start < LR.start || (start == LR.start && end < LR.end);
77 bool operator==(const LiveRange &LR) const {
78 return start == LR.start && end == LR.end;
82 void print(std::ostream &os) const;
83 void print(std::ostream *os) const { if (os) print(*os); }
86 LiveRange(); // DO NOT IMPLEMENT
89 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
92 inline bool operator<(unsigned V, const LiveRange &LR) {
96 inline bool operator<(const LiveRange &LR, unsigned V) {
100 /// LiveInterval - This class represents some number of live ranges for a
101 /// register or value. This class also contains a bit of register allocator
103 struct LiveInterval {
104 typedef SmallVector<LiveRange,4> Ranges;
105 typedef SmallVector<VNInfo*,4> VNInfoList;
107 unsigned reg; // the register or stack slot of this interval
108 // if the top bits is set, it represents a stack slot.
109 float weight; // weight of this interval
110 unsigned short preference; // preferred register for this interval
111 Ranges ranges; // the ranges in which this register is live
112 VNInfoList valnos; // value#'s
115 LiveInterval(unsigned Reg, float Weight, bool IsSS = false)
116 : reg(Reg), weight(Weight), preference(0) {
118 reg = reg | (1U << (sizeof(unsigned)*8-1));
121 typedef Ranges::iterator iterator;
122 iterator begin() { return ranges.begin(); }
123 iterator end() { return ranges.end(); }
125 typedef Ranges::const_iterator const_iterator;
126 const_iterator begin() const { return ranges.begin(); }
127 const_iterator end() const { return ranges.end(); }
129 typedef VNInfoList::iterator vni_iterator;
130 vni_iterator vni_begin() { return valnos.begin(); }
131 vni_iterator vni_end() { return valnos.end(); }
133 typedef VNInfoList::const_iterator const_vni_iterator;
134 const_vni_iterator vni_begin() const { return valnos.begin(); }
135 const_vni_iterator vni_end() const { return valnos.end(); }
137 /// advanceTo - Advance the specified iterator to point to the LiveRange
138 /// containing the specified position, or end() if the position is past the
139 /// end of the interval. If no LiveRange contains this position, but the
140 /// position is in a hole, this method returns an iterator pointing the the
141 /// LiveRange immediately after the hole.
142 iterator advanceTo(iterator I, unsigned Pos) {
143 if (Pos >= endNumber())
145 while (I->end <= Pos) ++I;
149 /// isStackSlot - Return true if this is a stack slot interval.
151 bool isStackSlot() const {
152 return reg & (1U << (sizeof(unsigned)*8-1));
155 /// getStackSlotIndex - Return stack slot index if this is a stack slot
157 int getStackSlotIndex() const {
158 assert(isStackSlot() && "Interval is not a stack slot interval!");
159 return reg & ~(1U << (sizeof(unsigned)*8-1));
162 bool hasAtLeastOneValue() const { return !valnos.empty(); }
164 bool containsOneValue() const { return valnos.size() == 1; }
166 unsigned getNumValNums() const { return (unsigned)valnos.size(); }
168 /// getValNumInfo - Returns pointer to the specified val#.
170 inline VNInfo *getValNumInfo(unsigned ValNo) {
171 return valnos[ValNo];
173 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
174 return valnos[ValNo];
177 /// copyValNumInfo - Copy the value number info for one value number to
179 void copyValNumInfo(VNInfo *DstValNo, const VNInfo *SrcValNo) {
180 DstValNo->def = SrcValNo->def;
181 DstValNo->copy = SrcValNo->copy;
182 DstValNo->hasPHIKill = SrcValNo->hasPHIKill;
183 DstValNo->redefByEC = SrcValNo->redefByEC;
184 DstValNo->kills = SrcValNo->kills;
187 /// getNextValue - Create a new value number and return it. MIIdx specifies
188 /// the instruction that defines the value number.
189 VNInfo *getNextValue(unsigned MIIdx, MachineInstr *CopyMI,
190 BumpPtrAllocator &VNInfoAllocator) {
192 unsigned Alignment = (unsigned)__alignof__(VNInfo);
195 unsigned Alignment = 8;
198 static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
200 new (VNI) VNInfo((unsigned)valnos.size(), MIIdx, CopyMI);
201 valnos.push_back(VNI);
205 /// addKill - Add a kill instruction index to the specified value
207 static void addKill(VNInfo *VNI, unsigned KillIdx) {
208 SmallVector<unsigned, 4> &kills = VNI->kills;
210 kills.push_back(KillIdx);
212 SmallVector<unsigned, 4>::iterator
213 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
214 kills.insert(I, KillIdx);
218 /// addKills - Add a number of kills into the VNInfo kill vector. If this
219 /// interval is live at a kill point, then the kill is not added.
220 void addKills(VNInfo *VNI, const SmallVector<unsigned, 4> &kills) {
221 for (unsigned i = 0, e = static_cast<unsigned>(kills.size());
223 unsigned KillIdx = kills[i];
224 if (!liveBeforeAndAt(KillIdx)) {
225 SmallVector<unsigned, 4>::iterator
226 I = std::lower_bound(VNI->kills.begin(), VNI->kills.end(), KillIdx);
227 VNI->kills.insert(I, KillIdx);
232 /// removeKill - Remove the specified kill from the list of kills of
233 /// the specified val#.
234 static bool removeKill(VNInfo *VNI, unsigned KillIdx) {
235 SmallVector<unsigned, 4> &kills = VNI->kills;
236 SmallVector<unsigned, 4>::iterator
237 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
238 if (I != kills.end() && *I == KillIdx) {
245 /// removeKills - Remove all the kills in specified range
246 /// [Start, End] of the specified val#.
247 static void removeKills(VNInfo *VNI, unsigned Start, unsigned End) {
248 SmallVector<unsigned, 4> &kills = VNI->kills;
249 SmallVector<unsigned, 4>::iterator
250 I = std::lower_bound(kills.begin(), kills.end(), Start);
251 SmallVector<unsigned, 4>::iterator
252 E = std::upper_bound(kills.begin(), kills.end(), End);
256 /// isKill - Return true if the specified index is a kill of the
258 static bool isKill(const VNInfo *VNI, unsigned KillIdx) {
259 const SmallVector<unsigned, 4> &kills = VNI->kills;
260 SmallVector<unsigned, 4>::const_iterator
261 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
262 return I != kills.end() && *I == KillIdx;
265 /// MergeValueNumberInto - This method is called when two value nubmers
266 /// are found to be equivalent. This eliminates V1, replacing all
267 /// LiveRanges with the V1 value number with the V2 value number. This can
268 /// cause merging of V1/V2 values numbers and compaction of the value space.
269 void MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
271 /// MergeInClobberRanges - For any live ranges that are not defined in the
272 /// current interval, but are defined in the Clobbers interval, mark them
273 /// used with an unknown definition value. Caller must pass in reference to
274 /// VNInfoAllocator since it will create a new val#.
275 void MergeInClobberRanges(const LiveInterval &Clobbers,
276 BumpPtrAllocator &VNInfoAllocator);
278 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
279 /// in RHS into this live interval as the specified value number.
280 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
281 /// current interval, it will replace the value numbers of the overlaped
282 /// live ranges with the specified value number.
283 void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
285 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
286 /// in RHS into this live interval as the specified value number.
287 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
288 /// current interval, but only if the overlapping LiveRanges have the
289 /// specified value number.
290 void MergeValueInAsValue(const LiveInterval &RHS,
291 const VNInfo *RHSValNo, VNInfo *LHSValNo);
293 /// Copy - Copy the specified live interval. This copies all the fields
294 /// except for the register of the interval.
295 void Copy(const LiveInterval &RHS, BumpPtrAllocator &VNInfoAllocator);
297 bool empty() const { return ranges.empty(); }
299 /// beginNumber - Return the lowest numbered slot covered by interval.
300 unsigned beginNumber() const {
303 return ranges.front().start;
306 /// endNumber - return the maximum point of the interval of the whole,
308 unsigned endNumber() const {
311 return ranges.back().end;
314 bool expiredAt(unsigned index) const {
315 return index >= endNumber();
318 bool liveAt(unsigned index) const;
320 // liveBeforeAndAt - Check if the interval is live at the index and the
321 // index just before it. If index is liveAt, check if it starts a new live
322 // range.If it does, then check if the previous live range ends at index-1.
323 bool liveBeforeAndAt(unsigned index) const;
325 /// getLiveRangeContaining - Return the live range that contains the
326 /// specified index, or null if there is none.
327 const LiveRange *getLiveRangeContaining(unsigned Idx) const {
328 const_iterator I = FindLiveRangeContaining(Idx);
329 return I == end() ? 0 : &*I;
332 /// FindLiveRangeContaining - Return an iterator to the live range that
333 /// contains the specified index, or end() if there is none.
334 const_iterator FindLiveRangeContaining(unsigned Idx) const;
336 /// FindLiveRangeContaining - Return an iterator to the live range that
337 /// contains the specified index, or end() if there is none.
338 iterator FindLiveRangeContaining(unsigned Idx);
340 /// findDefinedVNInfo - Find the VNInfo that's defined at the specified
341 /// index (register interval) or defined by the specified register (stack
343 VNInfo *findDefinedVNInfo(unsigned DefIdxOrReg) const;
345 /// overlaps - Return true if the intersection of the two live intervals is
347 bool overlaps(const LiveInterval& other) const {
348 return overlapsFrom(other, other.begin());
351 /// overlapsFrom - Return true if the intersection of the two live intervals
352 /// is not empty. The specified iterator is a hint that we can begin
353 /// scanning the Other interval starting at I.
354 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
356 /// addRange - Add the specified LiveRange to this interval, merging
357 /// intervals as appropriate. This returns an iterator to the inserted live
358 /// range (which may have grown since it was inserted.
359 void addRange(LiveRange LR) {
360 addRangeFrom(LR, ranges.begin());
363 /// join - Join two live intervals (this, and other) together. This applies
364 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
365 /// the intervals are not joinable, this aborts.
366 void join(LiveInterval &Other, const int *ValNoAssignments,
367 const int *RHSValNoAssignments,
368 SmallVector<VNInfo*, 16> &NewVNInfo);
370 /// removeRange - Remove the specified range from this interval. Note that
371 /// the range must already be in this interval in its entirety.
372 void removeRange(unsigned Start, unsigned End, bool RemoveDeadValNo = false);
374 void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
375 removeRange(LR.start, LR.end, RemoveDeadValNo);
378 /// removeValNo - Remove all the ranges defined by the specified value#.
379 /// Also remove the value# from value# list.
380 void removeValNo(VNInfo *ValNo);
382 /// getSize - Returns the sum of sizes of all the LiveRange's.
384 unsigned getSize() const;
386 bool operator<(const LiveInterval& other) const {
387 return beginNumber() < other.beginNumber();
390 void print(std::ostream &OS, const TargetRegisterInfo *TRI = 0) const;
391 void print(std::ostream *OS, const TargetRegisterInfo *TRI = 0) const {
392 if (OS) print(*OS, TRI);
397 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
398 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
399 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
400 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
403 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {