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/ArrayRef.h"
25 #include "llvm/ADT/IntEqClasses.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/iterator_range.h"
29 #include "llvm/CodeGen/SlotIndexes.h"
30 #include "llvm/MC/LaneBitmask.h"
31 #include "llvm/Support/Allocator.h"
32 #include "llvm/Support/MathExtras.h"
46 class MachineRegisterInfo;
49 /// VNInfo - Value Number Information.
50 /// This class holds information about a machine level values, including
51 /// definition and use points.
55 using Allocator = BumpPtrAllocator;
57 /// The ID number of this value.
60 /// The index of the defining instruction.
63 /// VNInfo constructor.
64 VNInfo(unsigned i, SlotIndex d) : id(i), def(d) {}
66 /// VNInfo constructor, copies values from orig, except for the value number.
67 VNInfo(unsigned i, const VNInfo &orig) : id(i), def(orig.def) {}
69 /// Copy from the parameter into this VNInfo.
70 void copyFrom(VNInfo &src) {
74 /// Returns true if this value is defined by a PHI instruction (or was,
75 /// PHI instructions may have been eliminated).
76 /// PHI-defs begin at a block boundary, all other defs begin at register or
78 bool isPHIDef() const { return def.isBlock(); }
80 /// Returns true if this value is unused.
81 bool isUnused() const { return !def.isValid(); }
83 /// Mark this value as unused.
84 void markUnused() { def = SlotIndex(); }
87 /// Result of a LiveRange query. This class hides the implementation details
88 /// of live ranges, and it should be used as the primary interface for
89 /// examining live ranges around instructions.
90 class LiveQueryResult {
91 VNInfo *const EarlyVal;
92 VNInfo *const LateVal;
93 const SlotIndex EndPoint;
97 LiveQueryResult(VNInfo *EarlyVal, VNInfo *LateVal, SlotIndex EndPoint,
99 : EarlyVal(EarlyVal), LateVal(LateVal), EndPoint(EndPoint), Kill(Kill)
102 /// Return the value that is live-in to the instruction. This is the value
103 /// that will be read by the instruction's use operands. Return NULL if no
104 /// value is live-in.
105 VNInfo *valueIn() const {
109 /// Return true if the live-in value is killed by this instruction. This
110 /// means that either the live range ends at the instruction, or it changes
112 bool isKill() const {
116 /// Return true if this instruction has a dead def.
117 bool isDeadDef() const {
118 return EndPoint.isDead();
121 /// Return the value leaving the instruction, if any. This can be a
122 /// live-through value, or a live def. A dead def returns NULL.
123 VNInfo *valueOut() const {
124 return isDeadDef() ? nullptr : LateVal;
127 /// Returns the value alive at the end of the instruction, if any. This can
128 /// be a live-through value, a live def or a dead def.
129 VNInfo *valueOutOrDead() const {
133 /// Return the value defined by this instruction, if any. This includes
134 /// dead defs, it is the value created by the instruction's def operands.
135 VNInfo *valueDefined() const {
136 return EarlyVal == LateVal ? nullptr : LateVal;
139 /// Return the end point of the last live range segment to interact with
140 /// the instruction, if any.
142 /// The end point is an invalid SlotIndex only if the live range doesn't
143 /// intersect the instruction at all.
145 /// The end point may be at or past the end of the instruction's basic
146 /// block. That means the value was live out of the block.
147 SlotIndex endPoint() const {
152 /// This class represents the liveness of a register, stack slot, etc.
153 /// It manages an ordered list of Segment objects.
154 /// The Segments are organized in a static single assignment form: At places
155 /// where a new value is defined or different values reach a CFG join a new
156 /// segment with a new value number is used.
159 /// This represents a simple continuous liveness interval for a value.
160 /// The start point is inclusive, the end point exclusive. These intervals
161 /// are rendered as [start,end).
163 SlotIndex start; // Start point of the interval (inclusive)
164 SlotIndex end; // End point of the interval (exclusive)
165 VNInfo *valno = nullptr; // identifier for the value contained in this
170 Segment(SlotIndex S, SlotIndex E, VNInfo *V)
171 : start(S), end(E), valno(V) {
172 assert(S < E && "Cannot create empty or backwards segment");
175 /// Return true if the index is covered by this segment.
176 bool contains(SlotIndex I) const {
177 return start <= I && I < end;
180 /// Return true if the given interval, [S, E), is covered by this segment.
181 bool containsInterval(SlotIndex S, SlotIndex E) const {
182 assert((S < E) && "Backwards interval?");
183 return (start <= S && S < end) && (start < E && E <= end);
186 bool operator<(const Segment &Other) const {
187 return std::tie(start, end) < std::tie(Other.start, Other.end);
189 bool operator==(const Segment &Other) const {
190 return start == Other.start && end == Other.end;
196 using Segments = SmallVector<Segment, 2>;
197 using VNInfoList = SmallVector<VNInfo *, 2>;
199 Segments segments; // the liveness segments
200 VNInfoList valnos; // value#'s
202 // The segment set is used temporarily to accelerate initial computation
203 // of live ranges of physical registers in computeRegUnitRange.
204 // After that the set is flushed to the segment vector and deleted.
205 using SegmentSet = std::set<Segment>;
206 std::unique_ptr<SegmentSet> segmentSet;
208 using iterator = Segments::iterator;
209 using const_iterator = Segments::const_iterator;
211 iterator begin() { return segments.begin(); }
212 iterator end() { return segments.end(); }
214 const_iterator begin() const { return segments.begin(); }
215 const_iterator end() const { return segments.end(); }
217 using vni_iterator = VNInfoList::iterator;
218 using const_vni_iterator = VNInfoList::const_iterator;
220 vni_iterator vni_begin() { return valnos.begin(); }
221 vni_iterator vni_end() { return valnos.end(); }
223 const_vni_iterator vni_begin() const { return valnos.begin(); }
224 const_vni_iterator vni_end() const { return valnos.end(); }
226 /// Constructs a new LiveRange object.
227 LiveRange(bool UseSegmentSet = false)
228 : segmentSet(UseSegmentSet ? llvm::make_unique<SegmentSet>()
231 /// Constructs a new LiveRange object by copying segments and valnos from
232 /// another LiveRange.
233 LiveRange(const LiveRange &Other, BumpPtrAllocator &Allocator) {
234 assert(Other.segmentSet == nullptr &&
235 "Copying of LiveRanges with active SegmentSets is not supported");
236 assign(Other, Allocator);
239 /// Copies values numbers and live segments from \p Other into this range.
240 void assign(const LiveRange &Other, BumpPtrAllocator &Allocator) {
244 assert(Other.segmentSet == nullptr &&
245 "Copying of LiveRanges with active SegmentSets is not supported");
247 for (const VNInfo *VNI : Other.valnos)
248 createValueCopy(VNI, Allocator);
249 // Now we can copy segments and remap their valnos.
250 for (const Segment &S : Other.segments)
251 segments.push_back(Segment(S.start, S.end, valnos[S.valno->id]));
254 /// advanceTo - Advance the specified iterator to point to the Segment
255 /// containing the specified position, or end() if the position is past the
256 /// end of the range. If no Segment contains this position, but the
257 /// position is in a hole, this method returns an iterator pointing to the
258 /// Segment immediately after the hole.
259 iterator advanceTo(iterator I, SlotIndex Pos) {
261 if (Pos >= endIndex())
263 while (I->end <= Pos) ++I;
267 const_iterator advanceTo(const_iterator I, SlotIndex Pos) const {
269 if (Pos >= endIndex())
271 while (I->end <= Pos) ++I;
275 /// find - Return an iterator pointing to the first segment that ends after
276 /// Pos, or end(). This is the same as advanceTo(begin(), Pos), but faster
277 /// when searching large ranges.
279 /// If Pos is contained in a Segment, that segment is returned.
280 /// If Pos is in a hole, the following Segment is returned.
281 /// If Pos is beyond endIndex, end() is returned.
282 iterator find(SlotIndex Pos);
284 const_iterator find(SlotIndex Pos) const {
285 return const_cast<LiveRange*>(this)->find(Pos);
293 size_t size() const {
294 return segments.size();
297 bool hasAtLeastOneValue() const { return !valnos.empty(); }
299 bool containsOneValue() const { return valnos.size() == 1; }
301 unsigned getNumValNums() const { return (unsigned)valnos.size(); }
303 /// getValNumInfo - Returns pointer to the specified val#.
305 inline VNInfo *getValNumInfo(unsigned ValNo) {
306 return valnos[ValNo];
308 inline const VNInfo *getValNumInfo(unsigned ValNo) const {
309 return valnos[ValNo];
312 /// containsValue - Returns true if VNI belongs to this range.
313 bool containsValue(const VNInfo *VNI) const {
314 return VNI && VNI->id < getNumValNums() && VNI == getValNumInfo(VNI->id);
317 /// getNextValue - Create a new value number and return it. MIIdx specifies
318 /// the instruction that defines the value number.
319 VNInfo *getNextValue(SlotIndex def, VNInfo::Allocator &VNInfoAllocator) {
321 new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), def);
322 valnos.push_back(VNI);
326 /// createDeadDef - Make sure the range has a value defined at Def.
327 /// If one already exists, return it. Otherwise allocate a new value and
328 /// add liveness for a dead def.
329 VNInfo *createDeadDef(SlotIndex Def, VNInfo::Allocator &VNInfoAllocator);
331 /// Create a def of value @p VNI. Return @p VNI. If there already exists
332 /// a definition at VNI->def, the value defined there must be @p VNI.
333 VNInfo *createDeadDef(VNInfo *VNI);
335 /// Create a copy of the given value. The new value will be identical except
336 /// for the Value number.
337 VNInfo *createValueCopy(const VNInfo *orig,
338 VNInfo::Allocator &VNInfoAllocator) {
340 new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), *orig);
341 valnos.push_back(VNI);
345 /// RenumberValues - Renumber all values in order of appearance and remove
347 void RenumberValues();
349 /// MergeValueNumberInto - This method is called when two value numbers
350 /// are found to be equivalent. This eliminates V1, replacing all
351 /// segments with the V1 value number with the V2 value number. This can
352 /// cause merging of V1/V2 values numbers and compaction of the value space.
353 VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
355 /// Merge all of the live segments of a specific val# in RHS into this live
356 /// range as the specified value number. The segments in RHS are allowed
357 /// to overlap with segments in the current range, it will replace the
358 /// value numbers of the overlaped live segments with the specified value
360 void MergeSegmentsInAsValue(const LiveRange &RHS, VNInfo *LHSValNo);
362 /// MergeValueInAsValue - Merge all of the segments of a specific val#
363 /// in RHS into this live range as the specified value number.
364 /// The segments in RHS are allowed to overlap with segments in the
365 /// current range, but only if the overlapping segments have the
366 /// specified value number.
367 void MergeValueInAsValue(const LiveRange &RHS,
368 const VNInfo *RHSValNo, VNInfo *LHSValNo);
370 bool empty() const { return segments.empty(); }
372 /// beginIndex - Return the lowest numbered slot covered.
373 SlotIndex beginIndex() const {
374 assert(!empty() && "Call to beginIndex() on empty range.");
375 return segments.front().start;
378 /// endNumber - return the maximum point of the range of the whole,
380 SlotIndex endIndex() const {
381 assert(!empty() && "Call to endIndex() on empty range.");
382 return segments.back().end;
385 bool expiredAt(SlotIndex index) const {
386 return index >= endIndex();
389 bool liveAt(SlotIndex index) const {
390 const_iterator r = find(index);
391 return r != end() && r->start <= index;
394 /// Return the segment that contains the specified index, or null if there
396 const Segment *getSegmentContaining(SlotIndex Idx) const {
397 const_iterator I = FindSegmentContaining(Idx);
398 return I == end() ? nullptr : &*I;
401 /// Return the live segment that contains the specified index, or null if
403 Segment *getSegmentContaining(SlotIndex Idx) {
404 iterator I = FindSegmentContaining(Idx);
405 return I == end() ? nullptr : &*I;
408 /// getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
409 VNInfo *getVNInfoAt(SlotIndex Idx) const {
410 const_iterator I = FindSegmentContaining(Idx);
411 return I == end() ? nullptr : I->valno;
414 /// getVNInfoBefore - Return the VNInfo that is live up to but not
415 /// necessarilly including Idx, or NULL. Use this to find the reaching def
416 /// used by an instruction at this SlotIndex position.
417 VNInfo *getVNInfoBefore(SlotIndex Idx) const {
418 const_iterator I = FindSegmentContaining(Idx.getPrevSlot());
419 return I == end() ? nullptr : I->valno;
422 /// Return an iterator to the segment that contains the specified index, or
423 /// end() if there is none.
424 iterator FindSegmentContaining(SlotIndex Idx) {
425 iterator I = find(Idx);
426 return I != end() && I->start <= Idx ? I : end();
429 const_iterator FindSegmentContaining(SlotIndex Idx) const {
430 const_iterator I = find(Idx);
431 return I != end() && I->start <= Idx ? I : end();
434 /// overlaps - Return true if the intersection of the two live ranges is
436 bool overlaps(const LiveRange &other) const {
439 return overlapsFrom(other, other.begin());
442 /// overlaps - Return true if the two ranges have overlapping segments
443 /// that are not coalescable according to CP.
445 /// Overlapping segments where one range is defined by a coalescable
446 /// copy are allowed.
447 bool overlaps(const LiveRange &Other, const CoalescerPair &CP,
448 const SlotIndexes&) const;
450 /// overlaps - Return true if the live range overlaps an interval specified
452 bool overlaps(SlotIndex Start, SlotIndex End) const;
454 /// overlapsFrom - Return true if the intersection of the two live ranges
455 /// is not empty. The specified iterator is a hint that we can begin
456 /// scanning the Other range starting at I.
457 bool overlapsFrom(const LiveRange &Other, const_iterator I) const;
459 /// Returns true if all segments of the @p Other live range are completely
460 /// covered by this live range.
461 /// Adjacent live ranges do not affect the covering:the liverange
462 /// [1,5](5,10] covers (3,7].
463 bool covers(const LiveRange &Other) const;
465 /// Add the specified Segment to this range, merging segments as
466 /// appropriate. This returns an iterator to the inserted segment (which
467 /// may have grown since it was inserted).
468 iterator addSegment(Segment S);
470 /// Attempt to extend a value defined after @p StartIdx to include @p Use.
471 /// Both @p StartIdx and @p Use should be in the same basic block. In case
472 /// of subranges, an extension could be prevented by an explicit "undef"
473 /// caused by a <def,read-undef> on a non-overlapping lane. The list of
474 /// location of such "undefs" should be provided in @p Undefs.
475 /// The return value is a pair: the first element is VNInfo of the value
476 /// that was extended (possibly nullptr), the second is a boolean value
477 /// indicating whether an "undef" was encountered.
478 /// If this range is live before @p Use in the basic block that starts at
479 /// @p StartIdx, and there is no intervening "undef", extend it to be live
480 /// up to @p Use, and return the pair {value, false}. If there is no
481 /// segment before @p Use and there is no "undef" between @p StartIdx and
482 /// @p Use, return {nullptr, false}. If there is an "undef" before @p Use,
483 /// return {nullptr, true}.
484 std::pair<VNInfo*,bool> extendInBlock(ArrayRef<SlotIndex> Undefs,
485 SlotIndex StartIdx, SlotIndex Use);
487 /// Simplified version of the above "extendInBlock", which assumes that
488 /// no register lanes are undefined by <def,read-undef> operands.
489 /// If this range is live before @p Use in the basic block that starts
490 /// at @p StartIdx, extend it to be live up to @p Use, and return the
491 /// value. If there is no segment before @p Use, return nullptr.
492 VNInfo *extendInBlock(SlotIndex StartIdx, SlotIndex Kill);
494 /// join - Join two live ranges (this, and other) together. This applies
495 /// mappings to the value numbers in the LHS/RHS ranges as specified. If
496 /// the ranges are not joinable, this aborts.
497 void join(LiveRange &Other,
498 const int *ValNoAssignments,
499 const int *RHSValNoAssignments,
500 SmallVectorImpl<VNInfo *> &NewVNInfo);
502 /// True iff this segment is a single segment that lies between the
503 /// specified boundaries, exclusively. Vregs live across a backedge are not
504 /// considered local. The boundaries are expected to lie within an extended
505 /// basic block, so vregs that are not live out should contain no holes.
506 bool isLocal(SlotIndex Start, SlotIndex End) const {
507 return beginIndex() > Start.getBaseIndex() &&
508 endIndex() < End.getBoundaryIndex();
511 /// Remove the specified segment from this range. Note that the segment
512 /// must be a single Segment in its entirety.
513 void removeSegment(SlotIndex Start, SlotIndex End,
514 bool RemoveDeadValNo = false);
516 void removeSegment(Segment S, bool RemoveDeadValNo = false) {
517 removeSegment(S.start, S.end, RemoveDeadValNo);
520 /// Remove segment pointed to by iterator @p I from this range. This does
521 /// not remove dead value numbers.
522 iterator removeSegment(iterator I) {
523 return segments.erase(I);
526 /// Query Liveness at Idx.
527 /// The sub-instruction slot of Idx doesn't matter, only the instruction
528 /// it refers to is considered.
529 LiveQueryResult Query(SlotIndex Idx) const {
530 // Find the segment that enters the instruction.
531 const_iterator I = find(Idx.getBaseIndex());
532 const_iterator E = end();
534 return LiveQueryResult(nullptr, nullptr, SlotIndex(), false);
536 // Is this an instruction live-in segment?
537 // If Idx is the start index of a basic block, include live-in segments
538 // that start at Idx.getBaseIndex().
539 VNInfo *EarlyVal = nullptr;
540 VNInfo *LateVal = nullptr;
543 if (I->start <= Idx.getBaseIndex()) {
546 // Move to the potentially live-out segment.
547 if (SlotIndex::isSameInstr(Idx, I->end)) {
550 return LiveQueryResult(EarlyVal, LateVal, EndPoint, Kill);
552 // Special case: A PHIDef value can have its def in the middle of a
553 // segment if the value happens to be live out of the layout
555 // Such a value is not live-in.
556 if (EarlyVal->def == Idx.getBaseIndex())
559 // I now points to the segment that may be live-through, or defined by
560 // this instr. Ignore segments starting after the current instr.
561 if (!SlotIndex::isEarlierInstr(Idx, I->start)) {
565 return LiveQueryResult(EarlyVal, LateVal, EndPoint, Kill);
568 /// removeValNo - Remove all the segments defined by the specified value#.
569 /// Also remove the value# from value# list.
570 void removeValNo(VNInfo *ValNo);
572 /// Returns true if the live range is zero length, i.e. no live segments
573 /// span instructions. It doesn't pay to spill such a range.
574 bool isZeroLength(SlotIndexes *Indexes) const {
575 for (const Segment &S : segments)
576 if (Indexes->getNextNonNullIndex(S.start).getBaseIndex() <
577 S.end.getBaseIndex())
582 // Returns true if any segment in the live range contains any of the
583 // provided slot indexes. Slots which occur in holes between
584 // segments will not cause the function to return true.
585 bool isLiveAtIndexes(ArrayRef<SlotIndex> Slots) const;
587 bool operator<(const LiveRange& other) const {
588 const SlotIndex &thisIndex = beginIndex();
589 const SlotIndex &otherIndex = other.beginIndex();
590 return thisIndex < otherIndex;
593 /// Returns true if there is an explicit "undef" between @p Begin
595 bool isUndefIn(ArrayRef<SlotIndex> Undefs, SlotIndex Begin,
596 SlotIndex End) const {
597 return std::any_of(Undefs.begin(), Undefs.end(),
598 [Begin,End] (SlotIndex Idx) -> bool {
599 return Begin <= Idx && Idx < End;
603 /// Flush segment set into the regular segment vector.
604 /// The method is to be called after the live range
605 /// has been created, if use of the segment set was
606 /// activated in the constructor of the live range.
607 void flushSegmentSet();
609 void print(raw_ostream &OS) const;
612 /// \brief Walk the range and assert if any invariants fail to hold.
614 /// Note that this is a no-op when asserts are disabled.
616 void verify() const {}
622 /// Append a segment to the list of segments.
623 void append(const LiveRange::Segment S);
626 friend class LiveRangeUpdater;
627 void addSegmentToSet(Segment S);
628 void markValNoForDeletion(VNInfo *V);
631 inline raw_ostream &operator<<(raw_ostream &OS, const LiveRange &LR) {
636 /// LiveInterval - This class represents the liveness of a register,
638 class LiveInterval : public LiveRange {
640 using super = LiveRange;
642 /// A live range for subregisters. The LaneMask specifies which parts of the
643 /// super register are covered by the interval.
644 /// (@sa TargetRegisterInfo::getSubRegIndexLaneMask()).
645 class SubRange : public LiveRange {
647 SubRange *Next = nullptr;
648 LaneBitmask LaneMask;
650 /// Constructs a new SubRange object.
651 SubRange(LaneBitmask LaneMask) : LaneMask(LaneMask) {}
653 /// Constructs a new SubRange object by copying liveness from @p Other.
654 SubRange(LaneBitmask LaneMask, const LiveRange &Other,
655 BumpPtrAllocator &Allocator)
656 : LiveRange(Other, Allocator), LaneMask(LaneMask) {}
658 void print(raw_ostream &OS) const;
663 SubRange *SubRanges = nullptr; ///< Single linked list of subregister live
667 const unsigned reg; // the register or stack slot of this interval.
668 float weight; // weight of this interval
670 LiveInterval(unsigned Reg, float Weight) : reg(Reg), weight(Weight) {}
677 class SingleLinkedListIterator {
681 SingleLinkedListIterator<T>(T *P) : P(P) {}
683 SingleLinkedListIterator<T> &operator++() {
687 SingleLinkedListIterator<T> operator++(int) {
688 SingleLinkedListIterator res = *this;
692 bool operator!=(const SingleLinkedListIterator<T> &Other) {
693 return P != Other.operator->();
695 bool operator==(const SingleLinkedListIterator<T> &Other) {
696 return P == Other.operator->();
698 T &operator*() const {
701 T *operator->() const {
706 using subrange_iterator = SingleLinkedListIterator<SubRange>;
707 using const_subrange_iterator = SingleLinkedListIterator<const SubRange>;
709 subrange_iterator subrange_begin() {
710 return subrange_iterator(SubRanges);
712 subrange_iterator subrange_end() {
713 return subrange_iterator(nullptr);
716 const_subrange_iterator subrange_begin() const {
717 return const_subrange_iterator(SubRanges);
719 const_subrange_iterator subrange_end() const {
720 return const_subrange_iterator(nullptr);
723 iterator_range<subrange_iterator> subranges() {
724 return make_range(subrange_begin(), subrange_end());
727 iterator_range<const_subrange_iterator> subranges() const {
728 return make_range(subrange_begin(), subrange_end());
731 /// Creates a new empty subregister live range. The range is added at the
732 /// beginning of the subrange list; subrange iterators stay valid.
733 SubRange *createSubRange(BumpPtrAllocator &Allocator,
734 LaneBitmask LaneMask) {
735 SubRange *Range = new (Allocator) SubRange(LaneMask);
736 appendSubRange(Range);
740 /// Like createSubRange() but the new range is filled with a copy of the
741 /// liveness information in @p CopyFrom.
742 SubRange *createSubRangeFrom(BumpPtrAllocator &Allocator,
743 LaneBitmask LaneMask,
744 const LiveRange &CopyFrom) {
745 SubRange *Range = new (Allocator) SubRange(LaneMask, CopyFrom, Allocator);
746 appendSubRange(Range);
750 /// Returns true if subregister liveness information is available.
751 bool hasSubRanges() const {
752 return SubRanges != nullptr;
755 /// Removes all subregister liveness information.
756 void clearSubRanges();
758 /// Removes all subranges without any segments (subranges without segments
759 /// are not considered valid and should only exist temporarily).
760 void removeEmptySubRanges();
762 /// getSize - Returns the sum of sizes of all the LiveRange's.
764 unsigned getSize() const;
766 /// isSpillable - Can this interval be spilled?
767 bool isSpillable() const {
768 return weight != huge_valf;
771 /// markNotSpillable - Mark interval as not spillable
772 void markNotSpillable() {
776 /// For a given lane mask @p LaneMask, compute indexes at which the
777 /// lane is marked undefined by subregister <def,read-undef> definitions.
778 void computeSubRangeUndefs(SmallVectorImpl<SlotIndex> &Undefs,
779 LaneBitmask LaneMask,
780 const MachineRegisterInfo &MRI,
781 const SlotIndexes &Indexes) const;
783 /// Refines the subranges to support \p LaneMask. This may only be called
784 /// for LI.hasSubrange()==true. Subregister ranges are split or created
785 /// until \p LaneMask can be matched exactly. \p Mod is executed on the
786 /// matching subranges.
789 /// Given an interval with subranges with lanemasks L0F00, L00F0 and
790 /// L000F, refining for mask L0018. Will split the L00F0 lane into
791 /// L00E0 and L0010 and the L000F lane into L0007 and L0008. The Mod
792 /// function will be applied to the L0010 and L0008 subranges.
793 void refineSubRanges(BumpPtrAllocator &Allocator, LaneBitmask LaneMask,
794 std::function<void(LiveInterval::SubRange&)> Mod);
796 bool operator<(const LiveInterval& other) const {
797 const SlotIndex &thisIndex = beginIndex();
798 const SlotIndex &otherIndex = other.beginIndex();
799 return std::tie(thisIndex, reg) < std::tie(otherIndex, other.reg);
802 void print(raw_ostream &OS) const;
805 /// \brief Walks the interval and assert if any invariants fail to hold.
807 /// Note that this is a no-op when asserts are disabled.
809 void verify(const MachineRegisterInfo *MRI = nullptr) const {}
811 void verify(const MachineRegisterInfo *MRI = nullptr) const;
815 /// Appends @p Range to SubRanges list.
816 void appendSubRange(SubRange *Range) {
817 Range->Next = SubRanges;
821 /// Free memory held by SubRange.
822 void freeSubRange(SubRange *S);
825 inline raw_ostream &operator<<(raw_ostream &OS,
826 const LiveInterval::SubRange &SR) {
831 inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) {
836 raw_ostream &operator<<(raw_ostream &OS, const LiveRange::Segment &S);
838 inline bool operator<(SlotIndex V, const LiveRange::Segment &S) {
842 inline bool operator<(const LiveRange::Segment &S, SlotIndex V) {
846 /// Helper class for performant LiveRange bulk updates.
848 /// Calling LiveRange::addSegment() repeatedly can be expensive on large
849 /// live ranges because segments after the insertion point may need to be
850 /// shifted. The LiveRangeUpdater class can defer the shifting when adding
851 /// many segments in order.
853 /// The LiveRange will be in an invalid state until flush() is called.
854 class LiveRangeUpdater {
857 LiveRange::iterator WriteI;
858 LiveRange::iterator ReadI;
859 SmallVector<LiveRange::Segment, 16> Spills;
863 /// Create a LiveRangeUpdater for adding segments to LR.
864 /// LR will temporarily be in an invalid state until flush() is called.
865 LiveRangeUpdater(LiveRange *lr = nullptr) : LR(lr) {}
867 ~LiveRangeUpdater() { flush(); }
869 /// Add a segment to LR and coalesce when possible, just like
870 /// LR.addSegment(). Segments should be added in increasing start order for
871 /// best performance.
872 void add(LiveRange::Segment);
874 void add(SlotIndex Start, SlotIndex End, VNInfo *VNI) {
875 add(LiveRange::Segment(Start, End, VNI));
878 /// Return true if the LR is currently in an invalid state, and flush()
879 /// needs to be called.
880 bool isDirty() const { return LastStart.isValid(); }
882 /// Flush the updater state to LR so it is valid and contains all added
886 /// Select a different destination live range.
887 void setDest(LiveRange *lr) {
888 if (LR != lr && isDirty())
893 /// Get the current destination live range.
894 LiveRange *getDest() const { return LR; }
897 void print(raw_ostream&) const;
900 inline raw_ostream &operator<<(raw_ostream &OS, const LiveRangeUpdater &X) {
905 /// ConnectedVNInfoEqClasses - Helper class that can divide VNInfos in a
906 /// LiveInterval into equivalence clases of connected components. A
907 /// LiveInterval that has multiple connected components can be broken into
908 /// multiple LiveIntervals.
910 /// Given a LiveInterval that may have multiple connected components, run:
912 /// unsigned numComps = ConEQ.Classify(LI);
913 /// if (numComps > 1) {
914 /// // allocate numComps-1 new LiveIntervals into LIS[1..]
915 /// ConEQ.Distribute(LIS);
918 class ConnectedVNInfoEqClasses {
920 IntEqClasses EqClass;
923 explicit ConnectedVNInfoEqClasses(LiveIntervals &lis) : LIS(lis) {}
925 /// Classify the values in \p LR into connected components.
926 /// Returns the number of connected components.
927 unsigned Classify(const LiveRange &LR);
929 /// getEqClass - Classify creates equivalence classes numbered 0..N. Return
930 /// the equivalence class assigned the VNI.
931 unsigned getEqClass(const VNInfo *VNI) const { return EqClass[VNI->id]; }
933 /// Distribute values in \p LI into a separate LiveIntervals
934 /// for each connected component. LIV must have an empty LiveInterval for
935 /// each additional connected component. The first connected component is
937 void Distribute(LiveInterval &LI, LiveInterval *LIV[],
938 MachineRegisterInfo &MRI);
941 } // end namespace llvm
943 #endif // LLVM_CODEGEN_LIVEINTERVAL_H