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 /// kills - Instruction # of the kills.
47 SmallVector<unsigned, 4> kills;
48 VNInfo() : id(~1U), def(~1U), copy(0), hasPHIKill(false) {}
49 VNInfo(unsigned i, unsigned d, MachineInstr *c)
50 : id(i), def(d), copy(c), hasPHIKill(false) {}
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 or stack slot of this interval
105 // if the top bits is set, it represents a stack slot.
106 float weight; // weight of this interval
107 unsigned short preference; // preferred register for this interval
108 Ranges ranges; // the ranges in which this register is live
109 VNInfoList valnos; // value#'s
112 LiveInterval(unsigned Reg, float Weight, bool IsSS = false)
113 : reg(Reg), weight(Weight), preference(0) {
115 reg = reg | (1U << (sizeof(unsigned)*8-1));
118 typedef Ranges::iterator iterator;
119 iterator begin() { return ranges.begin(); }
120 iterator end() { return ranges.end(); }
122 typedef Ranges::const_iterator const_iterator;
123 const_iterator begin() const { return ranges.begin(); }
124 const_iterator end() const { return ranges.end(); }
126 typedef VNInfoList::iterator vni_iterator;
127 vni_iterator vni_begin() { return valnos.begin(); }
128 vni_iterator vni_end() { return valnos.end(); }
130 typedef VNInfoList::const_iterator const_vni_iterator;
131 const_vni_iterator vni_begin() const { return valnos.begin(); }
132 const_vni_iterator vni_end() const { return valnos.end(); }
134 /// advanceTo - Advance the specified iterator to point to the LiveRange
135 /// containing the specified position, or end() if the position is past the
136 /// end of the interval. If no LiveRange contains this position, but the
137 /// position is in a hole, this method returns an iterator pointing the the
138 /// LiveRange immediately after the hole.
139 iterator advanceTo(iterator I, unsigned Pos) {
140 if (Pos >= endNumber())
142 while (I->end <= Pos) ++I;
146 /// isStackSlot - Return true if this is a stack slot interval.
148 bool isStackSlot() const {
149 return reg & (1U << (sizeof(unsigned)*8-1));
152 /// getStackSlotIndex - Return stack slot index if this is a stack slot
154 int getStackSlotIndex() const {
155 assert(isStackSlot() && "Interval is not a stack slot interval!");
156 return reg & ~(1U << (sizeof(unsigned)*8-1));
159 bool hasAtLeastOneValue() const { return !valnos.empty(); }
161 bool containsOneValue() const { return valnos.size() == 1; }
163 unsigned getNumValNums() const { return (unsigned)valnos.size(); }
165 /// getValNumInfo - Returns pointer to the specified val#.
167 inline VNInfo *getValNumInfo(unsigned ValNo) {
168 return valnos[ValNo];
170 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
171 return valnos[ValNo];
174 /// copyValNumInfo - Copy the value number info for one value number to
176 void copyValNumInfo(VNInfo *DstValNo, const VNInfo *SrcValNo) {
177 DstValNo->def = SrcValNo->def;
178 DstValNo->copy = SrcValNo->copy;
179 DstValNo->hasPHIKill = SrcValNo->hasPHIKill;
180 DstValNo->kills = SrcValNo->kills;
183 /// getNextValue - Create a new value number and return it. MIIdx specifies
184 /// the instruction that defines the value number.
185 VNInfo *getNextValue(unsigned MIIdx, MachineInstr *CopyMI,
186 BumpPtrAllocator &VNInfoAllocator) {
188 unsigned Alignment = (unsigned)__alignof__(VNInfo);
191 unsigned Alignment = 8;
194 static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
196 new (VNI) VNInfo((unsigned)valnos.size(), MIIdx, CopyMI);
197 valnos.push_back(VNI);
201 /// addKill - Add a kill instruction index to the specified value
203 static void addKill(VNInfo *VNI, unsigned KillIdx) {
204 SmallVector<unsigned, 4> &kills = VNI->kills;
206 kills.push_back(KillIdx);
208 SmallVector<unsigned, 4>::iterator
209 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
210 kills.insert(I, KillIdx);
214 /// addKills - Add a number of kills into the VNInfo kill vector. If this
215 /// interval is live at a kill point, then the kill is not added.
216 void addKills(VNInfo *VNI, const SmallVector<unsigned, 4> &kills) {
217 for (unsigned i = 0, e = static_cast<unsigned>(kills.size());
219 unsigned KillIdx = kills[i];
220 if (!liveBeforeAndAt(KillIdx)) {
221 SmallVector<unsigned, 4>::iterator
222 I = std::lower_bound(VNI->kills.begin(), VNI->kills.end(), KillIdx);
223 VNI->kills.insert(I, KillIdx);
228 /// removeKill - Remove the specified kill from the list of kills of
229 /// the specified val#.
230 static bool removeKill(VNInfo *VNI, unsigned KillIdx) {
231 SmallVector<unsigned, 4> &kills = VNI->kills;
232 SmallVector<unsigned, 4>::iterator
233 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
234 if (I != kills.end() && *I == KillIdx) {
241 /// removeKills - Remove all the kills in specified range
242 /// [Start, End] of the specified val#.
243 void removeKills(VNInfo *VNI, unsigned Start, unsigned End) {
244 SmallVector<unsigned, 4> &kills = VNI->kills;
245 SmallVector<unsigned, 4>::iterator
246 I = std::lower_bound(kills.begin(), kills.end(), Start);
247 SmallVector<unsigned, 4>::iterator
248 E = std::upper_bound(kills.begin(), kills.end(), End);
252 /// isKill - Return true if the specified index is a kill of the
254 bool isKill(const VNInfo *VNI, unsigned KillIdx) const {
255 const SmallVector<unsigned, 4> &kills = VNI->kills;
256 SmallVector<unsigned, 4>::const_iterator
257 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
258 return I != kills.end() && *I == KillIdx;
261 /// MergeValueNumberInto - This method is called when two value nubmers
262 /// are found to be equivalent. This eliminates V1, replacing all
263 /// LiveRanges with the V1 value number with the V2 value number. This can
264 /// cause merging of V1/V2 values numbers and compaction of the value space.
265 void MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
267 /// MergeInClobberRanges - For any live ranges that are not defined in the
268 /// current interval, but are defined in the Clobbers interval, mark them
269 /// used with an unknown definition value. Caller must pass in reference to
270 /// VNInfoAllocator since it will create a new val#.
271 void MergeInClobberRanges(const LiveInterval &Clobbers,
272 BumpPtrAllocator &VNInfoAllocator);
274 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
275 /// in RHS into this live interval as the specified value number.
276 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
277 /// current interval, it will replace the value numbers of the overlaped
278 /// live ranges with the specified value number.
279 void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
281 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
282 /// in RHS into this live interval as the specified value number.
283 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
284 /// current interval, but only if the overlapping LiveRanges have the
285 /// specified value number.
286 void MergeValueInAsValue(const LiveInterval &RHS,
287 const VNInfo *RHSValNo, VNInfo *LHSValNo);
289 /// Copy - Copy the specified live interval. This copies all the fields
290 /// except for the register of the interval.
291 void Copy(const LiveInterval &RHS, BumpPtrAllocator &VNInfoAllocator);
293 bool empty() const { return ranges.empty(); }
295 /// beginNumber - Return the lowest numbered slot covered by interval.
296 unsigned beginNumber() const {
299 return ranges.front().start;
302 /// endNumber - return the maximum point of the interval of the whole,
304 unsigned endNumber() const {
307 return ranges.back().end;
310 bool expiredAt(unsigned index) const {
311 return index >= endNumber();
314 bool liveAt(unsigned index) const;
316 // liveBeforeAndAt - Check if the interval is live at the index and the
317 // index just before it. If index is liveAt, check if it starts a new live
318 // range.If it does, then check if the previous live range ends at index-1.
319 bool liveBeforeAndAt(unsigned index) const;
321 /// getLiveRangeContaining - Return the live range that contains the
322 /// specified index, or null if there is none.
323 const LiveRange *getLiveRangeContaining(unsigned Idx) const {
324 const_iterator I = FindLiveRangeContaining(Idx);
325 return I == end() ? 0 : &*I;
328 /// FindLiveRangeContaining - Return an iterator to the live range that
329 /// contains the specified index, or end() if there is none.
330 const_iterator FindLiveRangeContaining(unsigned Idx) const;
332 /// FindLiveRangeContaining - Return an iterator to the live range that
333 /// contains the specified index, or end() if there is none.
334 iterator FindLiveRangeContaining(unsigned Idx);
336 /// findDefinedVNInfo - Find the VNInfo that's defined at the specified
337 /// index (register interval) or defined by the specified register (stack
339 VNInfo *findDefinedVNInfo(unsigned DefIdxOrReg) const;
341 /// overlaps - Return true if the intersection of the two live intervals is
343 bool overlaps(const LiveInterval& other) const {
344 return overlapsFrom(other, other.begin());
347 /// overlapsFrom - Return true if the intersection of the two live intervals
348 /// is not empty. The specified iterator is a hint that we can begin
349 /// scanning the Other interval starting at I.
350 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
352 /// addRange - Add the specified LiveRange to this interval, merging
353 /// intervals as appropriate. This returns an iterator to the inserted live
354 /// range (which may have grown since it was inserted.
355 void addRange(LiveRange LR) {
356 addRangeFrom(LR, ranges.begin());
359 /// join - Join two live intervals (this, and other) together. This applies
360 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
361 /// the intervals are not joinable, this aborts.
362 void join(LiveInterval &Other, const int *ValNoAssignments,
363 const int *RHSValNoAssignments,
364 SmallVector<VNInfo*, 16> &NewVNInfo);
366 /// removeRange - Remove the specified range from this interval. Note that
367 /// the range must already be in this interval in its entirety.
368 void removeRange(unsigned Start, unsigned End, bool RemoveDeadValNo = false);
370 void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
371 removeRange(LR.start, LR.end, RemoveDeadValNo);
374 /// removeValNo - Remove all the ranges defined by the specified value#.
375 /// Also remove the value# from value# list.
376 void removeValNo(VNInfo *ValNo);
378 /// getSize - Returns the sum of sizes of all the LiveRange's.
380 unsigned getSize() const;
382 bool operator<(const LiveInterval& other) const {
383 return beginNumber() < other.beginNumber();
386 void print(std::ostream &OS, const TargetRegisterInfo *TRI = 0) const;
387 void print(std::ostream *OS, const TargetRegisterInfo *TRI = 0) const {
388 if (OS) print(*OS, TRI);
393 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
394 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
395 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
396 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
399 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {