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"
32 class MachineRegisterInfo;
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 /// - or reg # of the definition if it's a stack slot liveinterval.
41 /// copy - Copy iff val# is defined by a copy; zero otherwise.
42 /// hasPHIKill - One or more of the kills are PHI nodes.
43 /// redefByEC - Re-defined by early clobber somewhere during the live range.
44 /// kills - Instruction # of the kills.
51 SmallVector<unsigned, 4> kills;
53 : id(~1U), def(~1U), copy(0), hasPHIKill(false), redefByEC(false) {}
54 VNInfo(unsigned i, unsigned d, MachineInstr *c)
55 : id(i), def(d), copy(c), hasPHIKill(false), redefByEC(false) {}
58 /// LiveRange structure - This represents a simple register range in the
59 /// program, with an inclusive start point and an exclusive end point.
60 /// These ranges are rendered as [start,end).
62 unsigned start; // Start point of the interval (inclusive)
63 unsigned end; // End point of the interval (exclusive)
64 VNInfo *valno; // identifier for the value contained in this interval.
66 LiveRange(unsigned S, unsigned E, VNInfo *V) : start(S), end(E), valno(V) {
67 assert(S < E && "Cannot create empty or backwards range");
70 /// contains - Return true if the index is covered by this range.
72 bool contains(unsigned I) const {
73 return start <= I && I < end;
76 bool operator<(const LiveRange &LR) const {
77 return start < LR.start || (start == LR.start && end < LR.end);
79 bool operator==(const LiveRange &LR) const {
80 return start == LR.start && end == LR.end;
84 void print(std::ostream &os) const;
85 void print(std::ostream *os) const { if (os) print(*os); }
88 LiveRange(); // DO NOT IMPLEMENT
91 std::ostream& operator<<(std::ostream& os, const LiveRange &LR);
94 inline bool operator<(unsigned V, const LiveRange &LR) {
98 inline bool operator<(const LiveRange &LR, unsigned V) {
102 /// LiveInterval - This class represents some number of live ranges for a
103 /// register or value. This class also contains a bit of register allocator
105 struct LiveInterval {
106 typedef SmallVector<LiveRange,4> Ranges;
107 typedef SmallVector<VNInfo*,4> VNInfoList;
109 unsigned reg; // the register or stack slot of this interval
110 // if the top bits is set, it represents a stack slot.
111 float weight; // weight of this interval
112 Ranges ranges; // the ranges in which this register is live
113 VNInfoList valnos; // value#'s
126 static unsigned scale(unsigned slot, unsigned factor) {
127 unsigned index = slot / NUM,
129 assert(index <= ~0U / (factor * NUM) &&
130 "Rescaled interval would overflow");
131 return index * NUM * factor + offset;
136 LiveInterval(unsigned Reg, float Weight, bool IsSS = false)
137 : reg(Reg), weight(Weight) {
139 reg = reg | (1U << (sizeof(unsigned)*CHAR_BIT-1));
142 typedef Ranges::iterator iterator;
143 iterator begin() { return ranges.begin(); }
144 iterator end() { return ranges.end(); }
146 typedef Ranges::const_iterator const_iterator;
147 const_iterator begin() const { return ranges.begin(); }
148 const_iterator end() const { return ranges.end(); }
150 typedef VNInfoList::iterator vni_iterator;
151 vni_iterator vni_begin() { return valnos.begin(); }
152 vni_iterator vni_end() { return valnos.end(); }
154 typedef VNInfoList::const_iterator const_vni_iterator;
155 const_vni_iterator vni_begin() const { return valnos.begin(); }
156 const_vni_iterator vni_end() const { return valnos.end(); }
158 /// advanceTo - Advance the specified iterator to point to the LiveRange
159 /// containing the specified position, or end() if the position is past the
160 /// end of the interval. If no LiveRange contains this position, but the
161 /// position is in a hole, this method returns an iterator pointing the the
162 /// LiveRange immediately after the hole.
163 iterator advanceTo(iterator I, unsigned Pos) {
164 if (Pos >= endNumber())
166 while (I->end <= Pos) ++I;
171 while (!valnos.empty()) {
172 VNInfo *VNI = valnos.back();
180 /// isStackSlot - Return true if this is a stack slot interval.
182 bool isStackSlot() const {
183 return reg & (1U << (sizeof(unsigned)*CHAR_BIT-1));
186 /// getStackSlotIndex - Return stack slot index if this is a stack slot
188 int getStackSlotIndex() const {
189 assert(isStackSlot() && "Interval is not a stack slot interval!");
190 return reg & ~(1U << (sizeof(unsigned)*CHAR_BIT-1));
193 bool hasAtLeastOneValue() const { return !valnos.empty(); }
195 bool containsOneValue() const { return valnos.size() == 1; }
197 unsigned getNumValNums() const { return (unsigned)valnos.size(); }
199 /// getValNumInfo - Returns pointer to the specified val#.
201 inline VNInfo *getValNumInfo(unsigned ValNo) {
202 return valnos[ValNo];
204 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
205 return valnos[ValNo];
208 /// copyValNumInfo - Copy the value number info for one value number to
210 void copyValNumInfo(VNInfo *DstValNo, const VNInfo *SrcValNo) {
211 DstValNo->def = SrcValNo->def;
212 DstValNo->copy = SrcValNo->copy;
213 DstValNo->hasPHIKill = SrcValNo->hasPHIKill;
214 DstValNo->redefByEC = SrcValNo->redefByEC;
215 DstValNo->kills = SrcValNo->kills;
218 /// getNextValue - Create a new value number and return it. MIIdx specifies
219 /// the instruction that defines the value number.
220 VNInfo *getNextValue(unsigned MIIdx, MachineInstr *CopyMI,
221 BumpPtrAllocator &VNInfoAllocator) {
223 unsigned Alignment = (unsigned)__alignof__(VNInfo);
226 unsigned Alignment = 8;
229 static_cast<VNInfo*>(VNInfoAllocator.Allocate((unsigned)sizeof(VNInfo),
231 new (VNI) VNInfo((unsigned)valnos.size(), MIIdx, CopyMI);
232 valnos.push_back(VNI);
236 /// addKill - Add a kill instruction index to the specified value
238 static void addKill(VNInfo *VNI, unsigned KillIdx) {
239 SmallVector<unsigned, 4> &kills = VNI->kills;
241 kills.push_back(KillIdx);
243 SmallVector<unsigned, 4>::iterator
244 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
245 kills.insert(I, KillIdx);
249 /// addKills - Add a number of kills into the VNInfo kill vector. If this
250 /// interval is live at a kill point, then the kill is not added.
251 void addKills(VNInfo *VNI, const SmallVector<unsigned, 4> &kills) {
252 for (unsigned i = 0, e = static_cast<unsigned>(kills.size());
254 unsigned KillIdx = kills[i];
255 if (!liveBeforeAndAt(KillIdx)) {
256 SmallVector<unsigned, 4>::iterator
257 I = std::lower_bound(VNI->kills.begin(), VNI->kills.end(), KillIdx);
258 VNI->kills.insert(I, KillIdx);
263 /// removeKill - Remove the specified kill from the list of kills of
264 /// the specified val#.
265 static bool removeKill(VNInfo *VNI, unsigned KillIdx) {
266 SmallVector<unsigned, 4> &kills = VNI->kills;
267 SmallVector<unsigned, 4>::iterator
268 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
269 if (I != kills.end() && *I == KillIdx) {
276 /// removeKills - Remove all the kills in specified range
277 /// [Start, End] of the specified val#.
278 static void removeKills(VNInfo *VNI, unsigned Start, unsigned End) {
279 SmallVector<unsigned, 4> &kills = VNI->kills;
280 SmallVector<unsigned, 4>::iterator
281 I = std::lower_bound(kills.begin(), kills.end(), Start);
282 SmallVector<unsigned, 4>::iterator
283 E = std::upper_bound(kills.begin(), kills.end(), End);
287 /// isKill - Return true if the specified index is a kill of the
289 static bool isKill(const VNInfo *VNI, unsigned KillIdx) {
290 const SmallVector<unsigned, 4> &kills = VNI->kills;
291 SmallVector<unsigned, 4>::const_iterator
292 I = std::lower_bound(kills.begin(), kills.end(), KillIdx);
293 return I != kills.end() && *I == KillIdx;
296 /// isOnlyLROfValNo - Return true if the specified live range is the only
297 /// one defined by the its val#.
298 bool isOnlyLROfValNo( const LiveRange *LR) {
299 for (const_iterator I = begin(), E = end(); I != E; ++I) {
300 const LiveRange *Tmp = I;
301 if (Tmp != LR && Tmp->valno == LR->valno)
307 /// MergeValueNumberInto - This method is called when two value nubmers
308 /// are found to be equivalent. This eliminates V1, replacing all
309 /// LiveRanges with the V1 value number with the V2 value number. This can
310 /// cause merging of V1/V2 values numbers and compaction of the value space.
311 VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
313 /// MergeInClobberRanges - For any live ranges that are not defined in the
314 /// current interval, but are defined in the Clobbers interval, mark them
315 /// used with an unknown definition value. Caller must pass in reference to
316 /// VNInfoAllocator since it will create a new val#.
317 void MergeInClobberRanges(const LiveInterval &Clobbers,
318 BumpPtrAllocator &VNInfoAllocator);
320 /// MergeInClobberRange - Same as MergeInClobberRanges except it merge in a
321 /// single LiveRange only.
322 void MergeInClobberRange(unsigned Start, unsigned End,
323 BumpPtrAllocator &VNInfoAllocator);
325 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
326 /// in RHS into this live interval as the specified value number.
327 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
328 /// current interval, it will replace the value numbers of the overlaped
329 /// live ranges with the specified value number.
330 void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
332 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
333 /// in RHS into this live interval as the specified value number.
334 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
335 /// current interval, but only if the overlapping LiveRanges have the
336 /// specified value number.
337 void MergeValueInAsValue(const LiveInterval &RHS,
338 const VNInfo *RHSValNo, VNInfo *LHSValNo);
340 /// Copy - Copy the specified live interval. This copies all the fields
341 /// except for the register of the interval.
342 void Copy(const LiveInterval &RHS, MachineRegisterInfo *MRI,
343 BumpPtrAllocator &VNInfoAllocator);
345 bool empty() const { return ranges.empty(); }
347 /// beginNumber - Return the lowest numbered slot covered by interval.
348 unsigned beginNumber() const {
351 return ranges.front().start;
354 /// endNumber - return the maximum point of the interval of the whole,
356 unsigned endNumber() const {
359 return ranges.back().end;
362 bool expiredAt(unsigned index) const {
363 return index >= endNumber();
366 bool liveAt(unsigned index) const;
368 // liveBeforeAndAt - Check if the interval is live at the index and the
369 // index just before it. If index is liveAt, check if it starts a new live
370 // range.If it does, then check if the previous live range ends at index-1.
371 bool liveBeforeAndAt(unsigned index) const;
373 /// getLiveRangeContaining - Return the live range that contains the
374 /// specified index, or null if there is none.
375 const LiveRange *getLiveRangeContaining(unsigned Idx) const {
376 const_iterator I = FindLiveRangeContaining(Idx);
377 return I == end() ? 0 : &*I;
380 /// FindLiveRangeContaining - Return an iterator to the live range that
381 /// contains the specified index, or end() if there is none.
382 const_iterator FindLiveRangeContaining(unsigned Idx) const;
384 /// FindLiveRangeContaining - Return an iterator to the live range that
385 /// contains the specified index, or end() if there is none.
386 iterator FindLiveRangeContaining(unsigned Idx);
388 /// findDefinedVNInfo - Find the VNInfo that's defined at the specified
389 /// index (register interval) or defined by the specified register (stack
391 VNInfo *findDefinedVNInfo(unsigned DefIdxOrReg) const;
393 /// overlaps - Return true if the intersection of the two live intervals is
395 bool overlaps(const LiveInterval& other) const {
396 return overlapsFrom(other, other.begin());
399 /// overlaps - Return true if the live interval overlaps a range specified
401 bool overlaps(unsigned Start, unsigned End) const;
403 /// overlapsFrom - Return true if the intersection of the two live intervals
404 /// is not empty. The specified iterator is a hint that we can begin
405 /// scanning the Other interval starting at I.
406 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
408 /// addRange - Add the specified LiveRange to this interval, merging
409 /// intervals as appropriate. This returns an iterator to the inserted live
410 /// range (which may have grown since it was inserted.
411 void addRange(LiveRange LR) {
412 addRangeFrom(LR, ranges.begin());
415 /// join - Join two live intervals (this, and other) together. This applies
416 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
417 /// the intervals are not joinable, this aborts.
418 void join(LiveInterval &Other, const int *ValNoAssignments,
419 const int *RHSValNoAssignments,
420 SmallVector<VNInfo*, 16> &NewVNInfo,
421 MachineRegisterInfo *MRI);
423 /// isInOneLiveRange - Return true if the range specified is entirely in the
424 /// a single LiveRange of the live interval.
425 bool isInOneLiveRange(unsigned Start, unsigned End);
427 /// removeRange - Remove the specified range from this interval. Note that
428 /// the range must be a single LiveRange in its entirety.
429 void removeRange(unsigned Start, unsigned End, bool RemoveDeadValNo = false);
431 void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
432 removeRange(LR.start, LR.end, RemoveDeadValNo);
435 /// removeValNo - Remove all the ranges defined by the specified value#.
436 /// Also remove the value# from value# list.
437 void removeValNo(VNInfo *ValNo);
439 /// scaleNumbering - Renumber VNI and ranges to provide gaps for new
441 void scaleNumbering(unsigned factor);
443 /// getSize - Returns the sum of sizes of all the LiveRange's.
445 unsigned getSize() const;
447 bool operator<(const LiveInterval& other) const {
448 return beginNumber() < other.beginNumber();
451 void print(std::ostream &OS, const TargetRegisterInfo *TRI = 0) const;
452 void print(std::ostream *OS, const TargetRegisterInfo *TRI = 0) const {
453 if (OS) print(*OS, TRI);
458 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
459 void extendIntervalEndTo(Ranges::iterator I, unsigned NewEnd);
460 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, unsigned NewStr);
461 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
464 inline std::ostream &operator<<(std::ostream &OS, const LiveInterval &LI) {