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 range 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 ranges can have holes,
15 // i.e. a range might look like [1,20), [50,65), [1000,1001). Each
16 // individual segment is represented as an instance of LiveRange::Segment,
17 // and the whole range is represented as an instance of LiveRange.
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_CODEGEN_LIVEINTERVAL_H
22 #define LLVM_CODEGEN_LIVEINTERVAL_H
24 #include "llvm/ADT/IntEqClasses.h"
25 #include "llvm/CodeGen/SlotIndexes.h"
26 #include "llvm/Support/Allocator.h"
27 #include "llvm/Target/TargetRegisterInfo.h"
36 class MachineRegisterInfo;
37 class TargetRegisterInfo;
39 template <typename T, unsigned Small> class SmallPtrSet;
41 /// VNInfo - Value Number Information.
42 /// This class holds information about a machine level values, including
43 /// definition and use points.
47 typedef BumpPtrAllocator Allocator;
49 /// The ID number of this value.
52 /// The index of the defining instruction.
55 /// VNInfo constructor.
56 VNInfo(unsigned i, SlotIndex d)
60 /// VNInfo constructor, copies values from orig, except for the value number.
61 VNInfo(unsigned i, const VNInfo &orig)
62 : id(i), def(orig.def)
65 /// Copy from the parameter into this VNInfo.
66 void copyFrom(VNInfo &src) {
70 /// Returns true if this value is defined by a PHI instruction (or was,
71 /// PHI instructions may have been eliminated).
72 /// PHI-defs begin at a block boundary, all other defs begin at register or
74 bool isPHIDef() const { return def.isBlock(); }
76 /// Returns true if this value is unused.
77 bool isUnused() const { return !def.isValid(); }
79 /// Mark this value as unused.
80 void markUnused() { def = SlotIndex(); }
83 /// Result of a LiveRange query. This class hides the implementation details
84 /// of live ranges, and it should be used as the primary interface for
85 /// examining live ranges around instructions.
86 class LiveQueryResult {
87 VNInfo *const EarlyVal;
88 VNInfo *const LateVal;
89 const SlotIndex EndPoint;
93 LiveQueryResult(VNInfo *EarlyVal, VNInfo *LateVal, SlotIndex EndPoint,
95 : EarlyVal(EarlyVal), LateVal(LateVal), EndPoint(EndPoint), Kill(Kill)
98 /// Return the value that is live-in to the instruction. This is the value
99 /// that will be read by the instruction's use operands. Return NULL if no
100 /// value is live-in.
101 VNInfo *valueIn() const {
105 /// Return true if the live-in value is killed by this instruction. This
106 /// means that either the live range ends at the instruction, or it changes
108 bool isKill() const {
112 /// Return true if this instruction has a dead def.
113 bool isDeadDef() const {
114 return EndPoint.isDead();
117 /// Return the value leaving the instruction, if any. This can be a
118 /// live-through value, or a live def. A dead def returns NULL.
119 VNInfo *valueOut() const {
120 return isDeadDef() ? nullptr : LateVal;
123 /// Returns the value alive at the end of the instruction, if any. This can
124 /// be a live-through value, a live def or a dead def.
125 VNInfo *valueOutOrDead() const {
129 /// Return the value defined by this instruction, if any. This includes
130 /// dead defs, it is the value created by the instruction's def operands.
131 VNInfo *valueDefined() const {
132 return EarlyVal == LateVal ? nullptr : LateVal;
135 /// Return the end point of the last live range segment to interact with
136 /// the instruction, if any.
138 /// The end point is an invalid SlotIndex only if the live range doesn't
139 /// intersect the instruction at all.
141 /// The end point may be at or past the end of the instruction's basic
142 /// block. That means the value was live out of the block.
143 SlotIndex endPoint() const {
148 /// This class represents the liveness of a register, stack slot, etc.
149 /// It manages an ordered list of Segment objects.
150 /// The Segments are organized in a static single assignment form: At places
151 /// where a new value is defined or different values reach a CFG join a new
152 /// segment with a new value number is used.
156 /// This represents a simple continuous liveness interval for a value.
157 /// The start point is inclusive, the end point exclusive. These intervals
158 /// are rendered as [start,end).
160 SlotIndex start; // Start point of the interval (inclusive)
161 SlotIndex end; // End point of the interval (exclusive)
162 VNInfo *valno; // identifier for the value contained in this segment.
164 Segment() : valno(nullptr) {}
166 Segment(SlotIndex S, SlotIndex E, VNInfo *V)
167 : start(S), end(E), valno(V) {
168 assert(S < E && "Cannot create empty or backwards segment");
171 /// Return true if the index is covered by this segment.
172 bool contains(SlotIndex I) const {
173 return start <= I && I < end;
176 /// Return true if the given interval, [S, E), is covered by this segment.
177 bool containsInterval(SlotIndex S, SlotIndex E) const {
178 assert((S < E) && "Backwards interval?");
179 return (start <= S && S < end) && (start < E && E <= end);
182 bool operator<(const Segment &Other) const {
183 return std::tie(start, end) < std::tie(Other.start, Other.end);
185 bool operator==(const Segment &Other) const {
186 return start == Other.start && end == Other.end;
192 typedef SmallVector<Segment, 2> Segments;
193 typedef SmallVector<VNInfo *, 2> VNInfoList;
195 Segments segments; // the liveness segments
196 VNInfoList valnos; // value#'s
198 // The segment set is used temporarily to accelerate initial computation
199 // of live ranges of physical registers in computeRegUnitRange.
200 // After that the set is flushed to the segment vector and deleted.
201 typedef std::set<Segment> SegmentSet;
202 std::unique_ptr<SegmentSet> segmentSet;
204 typedef Segments::iterator iterator;
205 iterator begin() { return segments.begin(); }
206 iterator end() { return segments.end(); }
208 typedef Segments::const_iterator const_iterator;
209 const_iterator begin() const { return segments.begin(); }
210 const_iterator end() const { return segments.end(); }
212 typedef VNInfoList::iterator vni_iterator;
213 vni_iterator vni_begin() { return valnos.begin(); }
214 vni_iterator vni_end() { return valnos.end(); }
216 typedef VNInfoList::const_iterator const_vni_iterator;
217 const_vni_iterator vni_begin() const { return valnos.begin(); }
218 const_vni_iterator vni_end() const { return valnos.end(); }
220 /// Constructs a new LiveRange object.
221 LiveRange(bool UseSegmentSet = false)
222 : segmentSet(UseSegmentSet ? llvm::make_unique<SegmentSet>()
225 /// Constructs a new LiveRange object by copying segments and valnos from
226 /// another LiveRange.
227 LiveRange(const LiveRange &Other, BumpPtrAllocator &Allocator) {
228 assert(Other.segmentSet == nullptr &&
229 "Copying of LiveRanges with active SegmentSets is not supported");
232 for (const VNInfo *VNI : Other.valnos) {
233 createValueCopy(VNI, Allocator);
235 // Now we can copy segments and remap their valnos.
236 for (const Segment &S : Other.segments) {
237 segments.push_back(Segment(S.start, S.end, valnos[S.valno->id]));
241 /// advanceTo - Advance the specified iterator to point to the Segment
242 /// containing the specified position, or end() if the position is past the
243 /// end of the range. If no Segment contains this position, but the
244 /// position is in a hole, this method returns an iterator pointing to the
245 /// Segment immediately after the hole.
246 iterator advanceTo(iterator I, SlotIndex Pos) {
248 if (Pos >= endIndex())
250 while (I->end <= Pos) ++I;
254 const_iterator advanceTo(const_iterator I, SlotIndex Pos) const {
256 if (Pos >= endIndex())
258 while (I->end <= Pos) ++I;
262 /// find - Return an iterator pointing to the first segment that ends after
263 /// Pos, or end(). This is the same as advanceTo(begin(), Pos), but faster
264 /// when searching large ranges.
266 /// If Pos is contained in a Segment, that segment is returned.
267 /// If Pos is in a hole, the following Segment is returned.
268 /// If Pos is beyond endIndex, end() is returned.
269 iterator find(SlotIndex Pos);
271 const_iterator find(SlotIndex Pos) const {
272 return const_cast<LiveRange*>(this)->find(Pos);
280 size_t size() const {
281 return segments.size();
284 bool hasAtLeastOneValue() const { return !valnos.empty(); }
286 bool containsOneValue() const { return valnos.size() == 1; }
288 unsigned getNumValNums() const { return (unsigned)valnos.size(); }
290 /// getValNumInfo - Returns pointer to the specified val#.
292 inline VNInfo *getValNumInfo(unsigned ValNo) {
293 return valnos[ValNo];
295 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
296 return valnos[ValNo];
299 /// containsValue - Returns true if VNI belongs to this range.
300 bool containsValue(const VNInfo *VNI) const {
301 return VNI && VNI->id < getNumValNums() && VNI == getValNumInfo(VNI->id);
304 /// getNextValue - Create a new value number and return it. MIIdx specifies
305 /// the instruction that defines the value number.
306 VNInfo *getNextValue(SlotIndex def, VNInfo::Allocator &VNInfoAllocator) {
308 new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), def);
309 valnos.push_back(VNI);
313 /// createDeadDef - Make sure the range has a value defined at Def.
314 /// If one already exists, return it. Otherwise allocate a new value and
315 /// add liveness for a dead def.
316 VNInfo *createDeadDef(SlotIndex Def, VNInfo::Allocator &VNInfoAllocator);
318 /// Create a def of value @p VNI. Return @p VNI. If there already exists
319 /// a definition at VNI->def, the value defined there must be @p VNI.
320 VNInfo *createDeadDef(VNInfo *VNI);
322 /// Create a copy of the given value. The new value will be identical except
323 /// for the Value number.
324 VNInfo *createValueCopy(const VNInfo *orig,
325 VNInfo::Allocator &VNInfoAllocator) {
327 new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), *orig);
328 valnos.push_back(VNI);
332 /// RenumberValues - Renumber all values in order of appearance and remove
334 void RenumberValues();
336 /// MergeValueNumberInto - This method is called when two value numbers
337 /// are found to be equivalent. This eliminates V1, replacing all
338 /// segments with the V1 value number with the V2 value number. This can
339 /// cause merging of V1/V2 values numbers and compaction of the value space.
340 VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
342 /// Merge all of the live segments of a specific val# in RHS into this live
343 /// range as the specified value number. The segments in RHS are allowed
344 /// to overlap with segments in the current range, it will replace the
345 /// value numbers of the overlaped live segments with the specified value
347 void MergeSegmentsInAsValue(const LiveRange &RHS, VNInfo *LHSValNo);
349 /// MergeValueInAsValue - Merge all of the segments of a specific val#
350 /// in RHS into this live range as the specified value number.
351 /// The segments in RHS are allowed to overlap with segments in the
352 /// current range, but only if the overlapping segments have the
353 /// specified value number.
354 void MergeValueInAsValue(const LiveRange &RHS,
355 const VNInfo *RHSValNo, VNInfo *LHSValNo);
357 bool empty() const { return segments.empty(); }
359 /// beginIndex - Return the lowest numbered slot covered.
360 SlotIndex beginIndex() const {
361 assert(!empty() && "Call to beginIndex() on empty range.");
362 return segments.front().start;
365 /// endNumber - return the maximum point of the range of the whole,
367 SlotIndex endIndex() const {
368 assert(!empty() && "Call to endIndex() on empty range.");
369 return segments.back().end;
372 bool expiredAt(SlotIndex index) const {
373 return index >= endIndex();
376 bool liveAt(SlotIndex index) const {
377 const_iterator r = find(index);
378 return r != end() && r->start <= index;
381 /// Return the segment that contains the specified index, or null if there
383 const Segment *getSegmentContaining(SlotIndex Idx) const {
384 const_iterator I = FindSegmentContaining(Idx);
385 return I == end() ? nullptr : &*I;
388 /// Return the live segment that contains the specified index, or null if
390 Segment *getSegmentContaining(SlotIndex Idx) {
391 iterator I = FindSegmentContaining(Idx);
392 return I == end() ? nullptr : &*I;
395 /// getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
396 VNInfo *getVNInfoAt(SlotIndex Idx) const {
397 const_iterator I = FindSegmentContaining(Idx);
398 return I == end() ? nullptr : I->valno;
401 /// getVNInfoBefore - Return the VNInfo that is live up to but not
402 /// necessarilly including Idx, or NULL. Use this to find the reaching def
403 /// used by an instruction at this SlotIndex position.
404 VNInfo *getVNInfoBefore(SlotIndex Idx) const {
405 const_iterator I = FindSegmentContaining(Idx.getPrevSlot());
406 return I == end() ? nullptr : I->valno;
409 /// Return an iterator to the segment that contains the specified index, or
410 /// end() if there is none.
411 iterator FindSegmentContaining(SlotIndex Idx) {
412 iterator I = find(Idx);
413 return I != end() && I->start <= Idx ? I : end();
416 const_iterator FindSegmentContaining(SlotIndex Idx) const {
417 const_iterator I = find(Idx);
418 return I != end() && I->start <= Idx ? I : end();
421 /// overlaps - Return true if the intersection of the two live ranges is
423 bool overlaps(const LiveRange &other) const {
426 return overlapsFrom(other, other.begin());
429 /// overlaps - Return true if the two ranges have overlapping segments
430 /// that are not coalescable according to CP.
432 /// Overlapping segments where one range is defined by a coalescable
433 /// copy are allowed.
434 bool overlaps(const LiveRange &Other, const CoalescerPair &CP,
435 const SlotIndexes&) const;
437 /// overlaps - Return true if the live range overlaps an interval specified
439 bool overlaps(SlotIndex Start, SlotIndex End) const;
441 /// overlapsFrom - Return true if the intersection of the two live ranges
442 /// is not empty. The specified iterator is a hint that we can begin
443 /// scanning the Other range starting at I.
444 bool overlapsFrom(const LiveRange &Other, const_iterator I) const;
446 /// Returns true if all segments of the @p Other live range are completely
447 /// covered by this live range.
448 /// Adjacent live ranges do not affect the covering:the liverange
449 /// [1,5](5,10] covers (3,7].
450 bool covers(const LiveRange &Other) const;
452 /// Add the specified Segment to this range, merging segments as
453 /// appropriate. This returns an iterator to the inserted segment (which
454 /// may have grown since it was inserted).
455 iterator addSegment(Segment S);
457 /// Attempt to extend a value defined after @p StartIdx to include @p Use.
458 /// Both @p StartIdx and @p Use should be in the same basic block. In case
459 /// of subranges, an extension could be prevented by an explicit "undef"
460 /// caused by a <def,read-undef> on a non-overlapping lane. The list of
461 /// location of such "undefs" should be provided in @p Undefs.
462 /// The return value is a pair: the first element is VNInfo of the value
463 /// that was extended (possibly nullptr), the second is a boolean value
464 /// indicating whether an "undef" was encountered.
465 /// If this range is live before @p Use in the basic block that starts at
466 /// @p StartIdx, and there is no intervening "undef", extend it to be live
467 /// up to @p Use, and return the pair {value, false}. If there is no
468 /// segment before @p Use and there is no "undef" between @p StartIdx and
469 /// @p Use, return {nullptr, false}. If there is an "undef" before @p Use,
470 /// return {nullptr, true}.
471 std::pair<VNInfo*,bool> extendInBlock(ArrayRef<SlotIndex> Undefs,
472 SlotIndex StartIdx, SlotIndex Use);
474 /// Simplified version of the above "extendInBlock", which assumes that
475 /// no register lanes are undefined by <def,read-undef> operands.
476 /// If this range is live before @p Use in the basic block that starts
477 /// at @p StartIdx, extend it to be live up to @p Use, and return the
478 /// value. If there is no segment before @p Use, return nullptr.
479 VNInfo *extendInBlock(SlotIndex StartIdx, SlotIndex Kill);
481 /// join - Join two live ranges (this, and other) together. This applies
482 /// mappings to the value numbers in the LHS/RHS ranges as specified. If
483 /// the ranges are not joinable, this aborts.
484 void join(LiveRange &Other,
485 const int *ValNoAssignments,
486 const int *RHSValNoAssignments,
487 SmallVectorImpl<VNInfo *> &NewVNInfo);
489 /// True iff this segment is a single segment that lies between the
490 /// specified boundaries, exclusively. Vregs live across a backedge are not
491 /// considered local. The boundaries are expected to lie within an extended
492 /// basic block, so vregs that are not live out should contain no holes.
493 bool isLocal(SlotIndex Start, SlotIndex End) const {
494 return beginIndex() > Start.getBaseIndex() &&
495 endIndex() < End.getBoundaryIndex();
498 /// Remove the specified segment from this range. Note that the segment
499 /// must be a single Segment in its entirety.
500 void removeSegment(SlotIndex Start, SlotIndex End,
501 bool RemoveDeadValNo = false);
503 void removeSegment(Segment S, bool RemoveDeadValNo = false) {
504 removeSegment(S.start, S.end, RemoveDeadValNo);
507 /// Remove segment pointed to by iterator @p I from this range. This does
508 /// not remove dead value numbers.
509 iterator removeSegment(iterator I) {
510 return segments.erase(I);
513 /// Query Liveness at Idx.
514 /// The sub-instruction slot of Idx doesn't matter, only the instruction
515 /// it refers to is considered.
516 LiveQueryResult Query(SlotIndex Idx) const {
517 // Find the segment that enters the instruction.
518 const_iterator I = find(Idx.getBaseIndex());
519 const_iterator E = end();
521 return LiveQueryResult(nullptr, nullptr, SlotIndex(), false);
523 // Is this an instruction live-in segment?
524 // If Idx is the start index of a basic block, include live-in segments
525 // that start at Idx.getBaseIndex().
526 VNInfo *EarlyVal = nullptr;
527 VNInfo *LateVal = nullptr;
530 if (I->start <= Idx.getBaseIndex()) {
533 // Move to the potentially live-out segment.
534 if (SlotIndex::isSameInstr(Idx, I->end)) {
537 return LiveQueryResult(EarlyVal, LateVal, EndPoint, Kill);
539 // Special case: A PHIDef value can have its def in the middle of a
540 // segment if the value happens to be live out of the layout
542 // Such a value is not live-in.
543 if (EarlyVal->def == Idx.getBaseIndex())
546 // I now points to the segment that may be live-through, or defined by
547 // this instr. Ignore segments starting after the current instr.
548 if (!SlotIndex::isEarlierInstr(Idx, I->start)) {
552 return LiveQueryResult(EarlyVal, LateVal, EndPoint, Kill);
555 /// removeValNo - Remove all the segments defined by the specified value#.
556 /// Also remove the value# from value# list.
557 void removeValNo(VNInfo *ValNo);
559 /// Returns true if the live range is zero length, i.e. no live segments
560 /// span instructions. It doesn't pay to spill such a range.
561 bool isZeroLength(SlotIndexes *Indexes) const {
562 for (const Segment &S : segments)
563 if (Indexes->getNextNonNullIndex(S.start).getBaseIndex() <
564 S.end.getBaseIndex())
569 // Returns true if any segment in the live range contains any of the
570 // provided slot indexes. Slots which occur in holes between
571 // segments will not cause the function to return true.
572 bool isLiveAtIndexes(ArrayRef<SlotIndex> Slots) const;
574 bool operator<(const LiveRange& other) const {
575 const SlotIndex &thisIndex = beginIndex();
576 const SlotIndex &otherIndex = other.beginIndex();
577 return thisIndex < otherIndex;
580 /// Returns true if there is an explicit "undef" between @p Begin
582 bool isUndefIn(ArrayRef<SlotIndex> Undefs, SlotIndex Begin,
583 SlotIndex End) const {
584 return std::any_of(Undefs.begin(), Undefs.end(),
585 [Begin,End] (SlotIndex Idx) -> bool {
586 return Begin <= Idx && Idx < End;
590 /// Flush segment set into the regular segment vector.
591 /// The method is to be called after the live range
592 /// has been created, if use of the segment set was
593 /// activated in the constructor of the live range.
594 void flushSegmentSet();
596 void print(raw_ostream &OS) const;
599 /// \brief Walk the range and assert if any invariants fail to hold.
601 /// Note that this is a no-op when asserts are disabled.
603 void verify() const {}
609 /// Append a segment to the list of segments.
610 void append(const LiveRange::Segment S);
613 friend class LiveRangeUpdater;
614 void addSegmentToSet(Segment S);
615 void markValNoForDeletion(VNInfo *V);
618 inline raw_ostream &operator<<(raw_ostream &OS, const LiveRange &LR) {
623 /// LiveInterval - This class represents the liveness of a register,
625 class LiveInterval : public LiveRange {
627 typedef LiveRange super;
629 /// A live range for subregisters. The LaneMask specifies which parts of the
630 /// super register are covered by the interval.
631 /// (@sa TargetRegisterInfo::getSubRegIndexLaneMask()).
632 class SubRange : public LiveRange {
635 LaneBitmask LaneMask;
637 /// Constructs a new SubRange object.
638 SubRange(LaneBitmask LaneMask)
639 : Next(nullptr), LaneMask(LaneMask) {
642 /// Constructs a new SubRange object by copying liveness from @p Other.
643 SubRange(LaneBitmask LaneMask, const LiveRange &Other,
644 BumpPtrAllocator &Allocator)
645 : LiveRange(Other, Allocator), Next(nullptr), LaneMask(LaneMask) {
648 void print(raw_ostream &OS) const;
653 SubRange *SubRanges; ///< Single linked list of subregister live ranges.
656 const unsigned reg; // the register or stack slot of this interval.
657 float weight; // weight of this interval
659 LiveInterval(unsigned Reg, float Weight)
660 : SubRanges(nullptr), reg(Reg), weight(Weight) {}
667 class SingleLinkedListIterator {
670 SingleLinkedListIterator<T>(T *P) : P(P) {}
671 SingleLinkedListIterator<T> &operator++() {
675 SingleLinkedListIterator<T> operator++(int) {
676 SingleLinkedListIterator res = *this;
680 bool operator!=(const SingleLinkedListIterator<T> &Other) {
681 return P != Other.operator->();
683 bool operator==(const SingleLinkedListIterator<T> &Other) {
684 return P == Other.operator->();
686 T &operator*() const {
689 T *operator->() const {
694 typedef SingleLinkedListIterator<SubRange> subrange_iterator;
695 subrange_iterator subrange_begin() {
696 return subrange_iterator(SubRanges);
698 subrange_iterator subrange_end() {
699 return subrange_iterator(nullptr);
702 typedef SingleLinkedListIterator<const SubRange> const_subrange_iterator;
703 const_subrange_iterator subrange_begin() const {
704 return const_subrange_iterator(SubRanges);
706 const_subrange_iterator subrange_end() const {
707 return const_subrange_iterator(nullptr);
710 iterator_range<subrange_iterator> subranges() {
711 return make_range(subrange_begin(), subrange_end());
714 iterator_range<const_subrange_iterator> subranges() const {
715 return make_range(subrange_begin(), subrange_end());
718 /// Creates a new empty subregister live range. The range is added at the
719 /// beginning of the subrange list; subrange iterators stay valid.
720 SubRange *createSubRange(BumpPtrAllocator &Allocator,
721 LaneBitmask LaneMask) {
722 SubRange *Range = new (Allocator) SubRange(LaneMask);
723 appendSubRange(Range);
727 /// Like createSubRange() but the new range is filled with a copy of the
728 /// liveness information in @p CopyFrom.
729 SubRange *createSubRangeFrom(BumpPtrAllocator &Allocator,
730 LaneBitmask LaneMask,
731 const LiveRange &CopyFrom) {
732 SubRange *Range = new (Allocator) SubRange(LaneMask, CopyFrom, Allocator);
733 appendSubRange(Range);
737 /// Returns true if subregister liveness information is available.
738 bool hasSubRanges() const {
739 return SubRanges != nullptr;
742 /// Removes all subregister liveness information.
743 void clearSubRanges();
745 /// Removes all subranges without any segments (subranges without segments
746 /// are not considered valid and should only exist temporarily).
747 void removeEmptySubRanges();
749 /// getSize - Returns the sum of sizes of all the LiveRange's.
751 unsigned getSize() const;
753 /// isSpillable - Can this interval be spilled?
754 bool isSpillable() const {
755 return weight != llvm::huge_valf;
758 /// markNotSpillable - Mark interval as not spillable
759 void markNotSpillable() {
760 weight = llvm::huge_valf;
763 /// For a given lane mask @p LaneMask, compute indexes at which the
764 /// lane is marked undefined by subregister <def,read-undef> definitions.
765 void computeSubRangeUndefs(SmallVectorImpl<SlotIndex> &Undefs,
766 LaneBitmask LaneMask,
767 const MachineRegisterInfo &MRI,
768 const SlotIndexes &Indexes) const;
770 bool operator<(const LiveInterval& other) const {
771 const SlotIndex &thisIndex = beginIndex();
772 const SlotIndex &otherIndex = other.beginIndex();
773 return std::tie(thisIndex, reg) < std::tie(otherIndex, other.reg);
776 void print(raw_ostream &OS) const;
779 /// \brief Walks the interval and assert if any invariants fail to hold.
781 /// Note that this is a no-op when asserts are disabled.
783 void verify(const MachineRegisterInfo *MRI = nullptr) const {}
785 void verify(const MachineRegisterInfo *MRI = nullptr) const;
789 /// Appends @p Range to SubRanges list.
790 void appendSubRange(SubRange *Range) {
791 Range->Next = SubRanges;
795 /// Free memory held by SubRange.
796 void freeSubRange(SubRange *S);
799 inline raw_ostream &operator<<(raw_ostream &OS,
800 const LiveInterval::SubRange &SR) {
805 inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) {
810 raw_ostream &operator<<(raw_ostream &OS, const LiveRange::Segment &S);
812 inline bool operator<(SlotIndex V, const LiveRange::Segment &S) {
816 inline bool operator<(const LiveRange::Segment &S, SlotIndex V) {
820 /// Helper class for performant LiveRange bulk updates.
822 /// Calling LiveRange::addSegment() repeatedly can be expensive on large
823 /// live ranges because segments after the insertion point may need to be
824 /// shifted. The LiveRangeUpdater class can defer the shifting when adding
825 /// many segments in order.
827 /// The LiveRange will be in an invalid state until flush() is called.
828 class LiveRangeUpdater {
831 LiveRange::iterator WriteI;
832 LiveRange::iterator ReadI;
833 SmallVector<LiveRange::Segment, 16> Spills;
837 /// Create a LiveRangeUpdater for adding segments to LR.
838 /// LR will temporarily be in an invalid state until flush() is called.
839 LiveRangeUpdater(LiveRange *lr = nullptr) : LR(lr) {}
841 ~LiveRangeUpdater() { flush(); }
843 /// Add a segment to LR and coalesce when possible, just like
844 /// LR.addSegment(). Segments should be added in increasing start order for
845 /// best performance.
846 void add(LiveRange::Segment);
848 void add(SlotIndex Start, SlotIndex End, VNInfo *VNI) {
849 add(LiveRange::Segment(Start, End, VNI));
852 /// Return true if the LR is currently in an invalid state, and flush()
853 /// needs to be called.
854 bool isDirty() const { return LastStart.isValid(); }
856 /// Flush the updater state to LR so it is valid and contains all added
860 /// Select a different destination live range.
861 void setDest(LiveRange *lr) {
862 if (LR != lr && isDirty())
867 /// Get the current destination live range.
868 LiveRange *getDest() const { return LR; }
871 void print(raw_ostream&) const;
874 inline raw_ostream &operator<<(raw_ostream &OS, const LiveRangeUpdater &X) {
879 /// ConnectedVNInfoEqClasses - Helper class that can divide VNInfos in a
880 /// LiveInterval into equivalence clases of connected components. A
881 /// LiveInterval that has multiple connected components can be broken into
882 /// multiple LiveIntervals.
884 /// Given a LiveInterval that may have multiple connected components, run:
886 /// unsigned numComps = ConEQ.Classify(LI);
887 /// if (numComps > 1) {
888 /// // allocate numComps-1 new LiveIntervals into LIS[1..]
889 /// ConEQ.Distribute(LIS);
892 class ConnectedVNInfoEqClasses {
894 IntEqClasses EqClass;
897 explicit ConnectedVNInfoEqClasses(LiveIntervals &lis) : LIS(lis) {}
899 /// Classify the values in \p LR into connected components.
900 /// Returns the number of connected components.
901 unsigned Classify(const LiveRange &LR);
903 /// getEqClass - Classify creates equivalence classes numbered 0..N. Return
904 /// the equivalence class assigned the VNI.
905 unsigned getEqClass(const VNInfo *VNI) const { return EqClass[VNI->id]; }
907 /// Distribute values in \p LI into a separate LiveIntervals
908 /// for each connected component. LIV must have an empty LiveInterval for
909 /// each additional connected component. The first connected component is
911 void Distribute(LiveInterval &LI, LiveInterval *LIV[],
912 MachineRegisterInfo &MRI);