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/AlignOf.h"
27 #include "llvm/CodeGen/SlotIndexes.h"
34 class MachineRegisterInfo;
35 class TargetRegisterInfo;
38 /// VNInfo - Value Number Information.
39 /// This class holds information about a machine level values, including
40 /// definition and use points.
42 /// Care must be taken in interpreting the def index of the value. The
43 /// following rules apply:
45 /// If the isDefAccurate() method returns false then def does not contain the
46 /// index of the defining MachineInstr, or even (necessarily) to a
47 /// MachineInstr at all. In general such a def index is not meaningful
48 /// and should not be used. The exception is that, for values originally
49 /// defined by PHI instructions, after PHI elimination def will contain the
50 /// index of the MBB in which the PHI originally existed. This can be used
51 /// to insert code (spills or copies) which deals with the value, which will
52 /// be live in to the block.
60 IS_DEF_ACCURATE = 1 << 4
67 typedef BumpPtrAllocator Allocator;
69 /// The ID number of this value.
72 /// The index of the defining instruction (if isDefAccurate() returns true).
75 /// VNInfo constructor.
76 /// d is presumed to point to the actual defining instr. If it doesn't
77 /// setIsDefAccurate(false) should be called after construction.
78 VNInfo(unsigned i, SlotIndex d, MachineInstr *c)
79 : copy(c), flags(IS_DEF_ACCURATE), id(i), def(d)
82 /// VNInfo construtor, copies values from orig, except for the value number.
83 VNInfo(unsigned i, const VNInfo &orig)
84 : copy(orig.copy), flags(orig.flags), id(i), def(orig.def)
87 /// Copy from the parameter into this VNInfo.
88 void copyFrom(VNInfo &src) {
94 /// Used for copying value number info.
95 unsigned getFlags() const { return flags; }
96 void setFlags(unsigned flags) { this->flags = flags; }
98 /// For a register interval, if this VN was definied by a copy instr
99 /// getCopy() returns a pointer to it, otherwise returns 0.
100 /// For a stack interval the behaviour of this method is undefined.
101 MachineInstr* getCopy() const { return copy; }
102 /// For a register interval, set the copy member.
103 /// This method should not be called on stack intervals as it may lead to
104 /// undefined behavior.
105 void setCopy(MachineInstr *c) { copy = c; }
107 /// Returns true if one or more kills are PHI nodes.
108 bool hasPHIKill() const { return flags & HAS_PHI_KILL; }
109 /// Set the PHI kill flag on this value.
110 void setHasPHIKill(bool hasKill) {
112 flags |= HAS_PHI_KILL;
114 flags &= ~HAS_PHI_KILL;
117 /// Returns true if this value is re-defined by an early clobber somewhere
118 /// during the live range.
119 bool hasRedefByEC() const { return flags & REDEF_BY_EC; }
120 /// Set the "redef by early clobber" flag on this value.
121 void setHasRedefByEC(bool hasRedef) {
123 flags |= REDEF_BY_EC;
125 flags &= ~REDEF_BY_EC;
128 /// Returns true if this value is defined by a PHI instruction (or was,
129 /// PHI instrucions may have been eliminated).
130 bool isPHIDef() const { return flags & IS_PHI_DEF; }
131 /// Set the "phi def" flag on this value.
132 void setIsPHIDef(bool phiDef) {
136 flags &= ~IS_PHI_DEF;
139 /// Returns true if this value is unused.
140 bool isUnused() const { return flags & IS_UNUSED; }
141 /// Set the "is unused" flag on this value.
142 void setIsUnused(bool unused) {
149 /// Returns true if the def is accurate.
150 bool isDefAccurate() const { return flags & IS_DEF_ACCURATE; }
151 /// Set the "is def accurate" flag on this value.
152 void setIsDefAccurate(bool defAccurate) {
154 flags |= IS_DEF_ACCURATE;
156 flags &= ~IS_DEF_ACCURATE;
160 /// LiveRange structure - This represents a simple register range in the
161 /// program, with an inclusive start point and an exclusive end point.
162 /// These ranges are rendered as [start,end).
164 SlotIndex start; // Start point of the interval (inclusive)
165 SlotIndex end; // End point of the interval (exclusive)
166 VNInfo *valno; // identifier for the value contained in this interval.
168 LiveRange(SlotIndex S, SlotIndex E, VNInfo *V)
169 : start(S), end(E), valno(V) {
171 assert(S < E && "Cannot create empty or backwards range");
174 /// contains - Return true if the index is covered by this range.
176 bool contains(SlotIndex I) const {
177 return start <= I && I < end;
180 /// containsRange - Return true if the given range, [S, E), is covered by
182 bool containsRange(SlotIndex S, SlotIndex E) const {
183 assert((S < E) && "Backwards interval?");
184 return (start <= S && S < end) && (start < E && E <= end);
187 bool operator<(const LiveRange &LR) const {
188 return start < LR.start || (start == LR.start && end < LR.end);
190 bool operator==(const LiveRange &LR) const {
191 return start == LR.start && end == LR.end;
195 void print(raw_ostream &os) const;
198 LiveRange(); // DO NOT IMPLEMENT
201 template <> struct isPodLike<LiveRange> { static const bool value = true; };
203 raw_ostream& operator<<(raw_ostream& os, const LiveRange &LR);
206 inline bool operator<(SlotIndex V, const LiveRange &LR) {
210 inline bool operator<(const LiveRange &LR, SlotIndex V) {
214 /// LiveInterval - This class represents some number of live ranges for a
215 /// register or value. This class also contains a bit of register allocator
220 typedef SmallVector<LiveRange,4> Ranges;
221 typedef SmallVector<VNInfo*,4> VNInfoList;
223 unsigned reg; // the register or stack slot of this interval
224 // if the top bits is set, it represents a stack slot.
225 float weight; // weight of this interval
226 Ranges ranges; // the ranges in which this register is live
227 VNInfoList valnos; // value#'s
240 LiveInterval(unsigned Reg, float Weight, bool IsSS = false)
241 : reg(Reg), weight(Weight) {
243 reg = reg | (1U << (sizeof(unsigned)*CHAR_BIT-1));
246 typedef Ranges::iterator iterator;
247 iterator begin() { return ranges.begin(); }
248 iterator end() { return ranges.end(); }
250 typedef Ranges::const_iterator const_iterator;
251 const_iterator begin() const { return ranges.begin(); }
252 const_iterator end() const { return ranges.end(); }
254 typedef VNInfoList::iterator vni_iterator;
255 vni_iterator vni_begin() { return valnos.begin(); }
256 vni_iterator vni_end() { return valnos.end(); }
258 typedef VNInfoList::const_iterator const_vni_iterator;
259 const_vni_iterator vni_begin() const { return valnos.begin(); }
260 const_vni_iterator vni_end() const { return valnos.end(); }
262 /// advanceTo - Advance the specified iterator to point to the LiveRange
263 /// containing the specified position, or end() if the position is past the
264 /// end of the interval. If no LiveRange contains this position, but the
265 /// position is in a hole, this method returns an iterator pointing to the
266 /// LiveRange immediately after the hole.
267 iterator advanceTo(iterator I, SlotIndex Pos) {
268 if (Pos >= endIndex())
270 while (I->end <= Pos) ++I;
279 /// isStackSlot - Return true if this is a stack slot interval.
281 bool isStackSlot() const {
282 return reg & (1U << (sizeof(unsigned)*CHAR_BIT-1));
285 /// getStackSlotIndex - Return stack slot index if this is a stack slot
287 int getStackSlotIndex() const {
288 assert(isStackSlot() && "Interval is not a stack slot interval!");
289 return reg & ~(1U << (sizeof(unsigned)*CHAR_BIT-1));
292 bool hasAtLeastOneValue() const { return !valnos.empty(); }
294 bool containsOneValue() const { return valnos.size() == 1; }
296 unsigned getNumValNums() const { return (unsigned)valnos.size(); }
298 /// getValNumInfo - Returns pointer to the specified val#.
300 inline VNInfo *getValNumInfo(unsigned ValNo) {
301 return valnos[ValNo];
303 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
304 return valnos[ValNo];
307 /// getNextValue - Create a new value number and return it. MIIdx specifies
308 /// the instruction that defines the value number.
309 VNInfo *getNextValue(SlotIndex def, MachineInstr *CopyMI,
310 bool isDefAccurate, VNInfo::Allocator &VNInfoAllocator) {
312 new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), def, CopyMI);
313 VNI->setIsDefAccurate(isDefAccurate);
314 valnos.push_back(VNI);
318 /// Create a copy of the given value. The new value will be identical except
319 /// for the Value number.
320 VNInfo *createValueCopy(const VNInfo *orig,
321 VNInfo::Allocator &VNInfoAllocator) {
323 new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), *orig);
324 valnos.push_back(VNI);
328 /// RenumberValues - Renumber all values in order of appearance and remove
330 /// Recalculate phi-kill flags in case any phi-def values were removed.
331 void RenumberValues(LiveIntervals &lis);
333 /// isOnlyLROfValNo - Return true if the specified live range is the only
334 /// one defined by the its val#.
335 bool isOnlyLROfValNo(const LiveRange *LR) {
336 for (const_iterator I = begin(), E = end(); I != E; ++I) {
337 const LiveRange *Tmp = I;
338 if (Tmp != LR && Tmp->valno == LR->valno)
344 /// MergeValueNumberInto - This method is called when two value nubmers
345 /// are found to be equivalent. This eliminates V1, replacing all
346 /// LiveRanges with the V1 value number with the V2 value number. This can
347 /// cause merging of V1/V2 values numbers and compaction of the value space.
348 VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
350 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
351 /// in RHS into this live interval as the specified value number.
352 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
353 /// current interval, it will replace the value numbers of the overlaped
354 /// live ranges with the specified value number.
355 void MergeRangesInAsValue(const LiveInterval &RHS, VNInfo *LHSValNo);
357 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
358 /// in RHS into this live interval as the specified value number.
359 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
360 /// current interval, but only if the overlapping LiveRanges have the
361 /// specified value number.
362 void MergeValueInAsValue(const LiveInterval &RHS,
363 const VNInfo *RHSValNo, VNInfo *LHSValNo);
365 /// Copy - Copy the specified live interval. This copies all the fields
366 /// except for the register of the interval.
367 void Copy(const LiveInterval &RHS, MachineRegisterInfo *MRI,
368 VNInfo::Allocator &VNInfoAllocator);
370 bool empty() const { return ranges.empty(); }
372 /// beginIndex - Return the lowest numbered slot covered by interval.
373 SlotIndex beginIndex() const {
374 assert(!empty() && "Call to beginIndex() on empty interval.");
375 return ranges.front().start;
378 /// endNumber - return the maximum point of the interval of the whole,
380 SlotIndex endIndex() const {
381 assert(!empty() && "Call to endIndex() on empty interval.");
382 return ranges.back().end;
385 bool expiredAt(SlotIndex index) const {
386 return index >= endIndex();
389 bool liveAt(SlotIndex index) const;
391 /// killedAt - Return true if a live range ends at index. Note that the kill
392 /// point is not contained in the half-open live range. It is usually the
393 /// getDefIndex() slot following its last use.
394 bool killedAt(SlotIndex index) const;
396 /// killedInRange - Return true if the interval has kills in [Start,End).
397 /// Note that the kill point is considered the end of a live range, so it is
398 /// not contained in the live range. If a live range ends at End, it won't
399 /// be counted as a kill by this method.
400 bool killedInRange(SlotIndex Start, SlotIndex End) const;
402 /// getLiveRangeContaining - Return the live range that contains the
403 /// specified index, or null if there is none.
404 const LiveRange *getLiveRangeContaining(SlotIndex Idx) const {
405 const_iterator I = FindLiveRangeContaining(Idx);
406 return I == end() ? 0 : &*I;
409 /// getLiveRangeContaining - Return the live range that contains the
410 /// specified index, or null if there is none.
411 LiveRange *getLiveRangeContaining(SlotIndex Idx) {
412 iterator I = FindLiveRangeContaining(Idx);
413 return I == end() ? 0 : &*I;
416 /// getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
417 VNInfo *getVNInfoAt(SlotIndex Idx) const {
418 const_iterator I = FindLiveRangeContaining(Idx);
419 return I == end() ? 0 : I->valno;
422 /// FindLiveRangeContaining - Return an iterator to the live range that
423 /// contains the specified index, or end() if there is none.
424 const_iterator FindLiveRangeContaining(SlotIndex Idx) const;
426 /// FindLiveRangeContaining - Return an iterator to the live range that
427 /// contains the specified index, or end() if there is none.
428 iterator FindLiveRangeContaining(SlotIndex Idx);
430 /// findDefinedVNInfo - Find the by the specified
431 /// index (register interval) or defined
432 VNInfo *findDefinedVNInfoForRegInt(SlotIndex Idx) const;
435 /// overlaps - Return true if the intersection of the two live intervals is
437 bool overlaps(const LiveInterval& other) const {
440 return overlapsFrom(other, other.begin());
443 /// overlaps - Return true if the live interval overlaps a range specified
445 bool overlaps(SlotIndex Start, SlotIndex End) const;
447 /// overlapsFrom - Return true if the intersection of the two live intervals
448 /// is not empty. The specified iterator is a hint that we can begin
449 /// scanning the Other interval starting at I.
450 bool overlapsFrom(const LiveInterval& other, const_iterator I) const;
452 /// addRange - Add the specified LiveRange to this interval, merging
453 /// intervals as appropriate. This returns an iterator to the inserted live
454 /// range (which may have grown since it was inserted.
455 void addRange(LiveRange LR) {
456 addRangeFrom(LR, ranges.begin());
459 /// join - Join two live intervals (this, and other) together. This applies
460 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
461 /// the intervals are not joinable, this aborts.
462 void join(LiveInterval &Other,
463 const int *ValNoAssignments,
464 const int *RHSValNoAssignments,
465 SmallVector<VNInfo*, 16> &NewVNInfo,
466 MachineRegisterInfo *MRI);
468 /// isInOneLiveRange - Return true if the range specified is entirely in the
469 /// a single LiveRange of the live interval.
470 bool isInOneLiveRange(SlotIndex Start, SlotIndex End);
472 /// removeRange - Remove the specified range from this interval. Note that
473 /// the range must be a single LiveRange in its entirety.
474 void removeRange(SlotIndex Start, SlotIndex End,
475 bool RemoveDeadValNo = false);
477 void removeRange(LiveRange LR, bool RemoveDeadValNo = false) {
478 removeRange(LR.start, LR.end, RemoveDeadValNo);
481 /// removeValNo - Remove all the ranges defined by the specified value#.
482 /// Also remove the value# from value# list.
483 void removeValNo(VNInfo *ValNo);
485 /// getSize - Returns the sum of sizes of all the LiveRange's.
487 unsigned getSize() const;
489 /// Returns true if the live interval is zero length, i.e. no live ranges
490 /// span instructions. It doesn't pay to spill such an interval.
491 bool isZeroLength() const {
492 for (const_iterator i = begin(), e = end(); i != e; ++i)
493 if (i->end.getPrevIndex() > i->start)
498 /// isSpillable - Can this interval be spilled?
499 bool isSpillable() const {
500 return weight != HUGE_VALF;
503 /// markNotSpillable - Mark interval as not spillable
504 void markNotSpillable() {
508 /// ComputeJoinedWeight - Set the weight of a live interval after
509 /// Other has been merged into it.
510 void ComputeJoinedWeight(const LiveInterval &Other);
512 bool operator<(const LiveInterval& other) const {
513 const SlotIndex &thisIndex = beginIndex();
514 const SlotIndex &otherIndex = other.beginIndex();
515 return (thisIndex < otherIndex ||
516 (thisIndex == otherIndex && reg < other.reg));
519 void print(raw_ostream &OS, const TargetRegisterInfo *TRI = 0) const;
524 Ranges::iterator addRangeFrom(LiveRange LR, Ranges::iterator From);
525 void extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd);
526 Ranges::iterator extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStr);
527 void markValNoForDeletion(VNInfo *V);
529 LiveInterval& operator=(const LiveInterval& rhs); // DO NOT IMPLEMENT
533 inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) {