1//===-- llvm/CodeGen/LiveInterval.h - Interval representation ---*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
18//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLVM_CODEGEN_LIVEINTERVAL_H
22#define LLVM_CODEGEN_LIVEINTERVAL_H
23
24#include "llvm/ADT/IntEqClasses.h"
25#include "llvm/CodeGen/SlotIndexes.h"
26#include "llvm/Support/AlignOf.h"
27#include "llvm/Support/Allocator.h"
28#include <cassert>
29#include <climits>
30#include <set>
31
32namespace llvm {
33  class CoalescerPair;
34  class LiveIntervals;
35  class MachineInstr;
36  class MachineRegisterInfo;
37  class TargetRegisterInfo;
38  class raw_ostream;
39  template <typename T, unsigned Small> class SmallPtrSet;
40
41  /// VNInfo - Value Number Information.
42  /// This class holds information about a machine level values, including
43  /// definition and use points.
44  ///
45  class VNInfo {
46  public:
47    typedef BumpPtrAllocator Allocator;
48
49    /// The ID number of this value.
50    unsigned id;
51
52    /// The index of the defining instruction.
53    SlotIndex def;
54
55    /// VNInfo constructor.
56    VNInfo(unsigned i, SlotIndex d)
57      : id(i), def(d)
58    { }
59
60    /// VNInfo construtor, copies values from orig, except for the value number.
61    VNInfo(unsigned i, const VNInfo &orig)
62      : id(i), def(orig.def)
63    { }
64
65    /// Copy from the parameter into this VNInfo.
66    void copyFrom(VNInfo &src) {
67      def = src.def;
68    }
69
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
73    /// EC slots.
74    bool isPHIDef() const { return def.isBlock(); }
75
76    /// Returns true if this value is unused.
77    bool isUnused() const { return !def.isValid(); }
78
79    /// Mark this value as unused.
80    void markUnused() { def = SlotIndex(); }
81  };
82
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;
90    const bool Kill;
91
92  public:
93    LiveQueryResult(VNInfo *EarlyVal, VNInfo *LateVal, SlotIndex EndPoint,
94                    bool Kill)
95      : EarlyVal(EarlyVal), LateVal(LateVal), EndPoint(EndPoint), Kill(Kill)
96    {}
97
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 {
102      return EarlyVal;
103    }
104
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
107    /// value.
108    bool isKill() const {
109      return Kill;
110    }
111
112    /// Return true if this instruction has a dead def.
113    bool isDeadDef() const {
114      return EndPoint.isDead();
115    }
116
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;
121    }
122
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 {
126      return LateVal;
127    }
128
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;
133    }
134
135    /// Return the end point of the last live range segment to interact with
136    /// the instruction, if any.
137    ///
138    /// The end point is an invalid SlotIndex only if the live range doesn't
139    /// intersect the instruction at all.
140    ///
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 {
144      return EndPoint;
145    }
146  };
147
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.
153  class LiveRange {
154  public:
155
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).
159    struct Segment {
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.
163
164      Segment() : valno(nullptr) {}
165
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");
169      }
170
171      /// Return true if the index is covered by this segment.
172      bool contains(SlotIndex I) const {
173        return start <= I && I < end;
174      }
175
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);
180      }
181
182      bool operator<(const Segment &Other) const {
183        return std::tie(start, end) < std::tie(Other.start, Other.end);
184      }
185      bool operator==(const Segment &Other) const {
186        return start == Other.start && end == Other.end;
187      }
188
189      void dump() const;
190    };
191
192    typedef SmallVector<Segment,4> Segments;
193    typedef SmallVector<VNInfo*,4> VNInfoList;
194
195    Segments segments;   // the liveness segments
196    VNInfoList valnos;   // value#'s
197
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;
203
204    typedef Segments::iterator iterator;
205    iterator begin() { return segments.begin(); }
206    iterator end()   { return segments.end(); }
207
208    typedef Segments::const_iterator const_iterator;
209    const_iterator begin() const { return segments.begin(); }
210    const_iterator end() const  { return segments.end(); }
211
212    typedef VNInfoList::iterator vni_iterator;
213    vni_iterator vni_begin() { return valnos.begin(); }
214    vni_iterator vni_end()   { return valnos.end(); }
215
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(); }
219
220    /// Constructs a new LiveRange object.
221    LiveRange(bool UseSegmentSet = false)
222        : segmentSet(UseSegmentSet ? llvm::make_unique<SegmentSet>()
223                                   : nullptr) {}
224
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");
230
231      // Duplicate valnos.
232      for (const VNInfo *VNI : Other.valnos) {
233        createValueCopy(VNI, Allocator);
234      }
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]));
238      }
239    }
240
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) {
247      assert(I != end());
248      if (Pos >= endIndex())
249        return end();
250      while (I->end <= Pos) ++I;
251      return I;
252    }
253
254    const_iterator advanceTo(const_iterator I, SlotIndex Pos) const {
255      assert(I != end());
256      if (Pos >= endIndex())
257        return end();
258      while (I->end <= Pos) ++I;
259      return I;
260    }
261
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.
265    ///
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);
270
271    const_iterator find(SlotIndex Pos) const {
272      return const_cast<LiveRange*>(this)->find(Pos);
273    }
274
275    void clear() {
276      valnos.clear();
277      segments.clear();
278    }
279
280    size_t size() const {
281      return segments.size();
282    }
283
284    bool hasAtLeastOneValue() const { return !valnos.empty(); }
285
286    bool containsOneValue() const { return valnos.size() == 1; }
287
288    unsigned getNumValNums() const { return (unsigned)valnos.size(); }
289
290    /// getValNumInfo - Returns pointer to the specified val#.
291    ///
292    inline VNInfo *getValNumInfo(unsigned ValNo) {
293      return valnos[ValNo];
294    }
295    inline const VNInfo *getValNumInfo(unsigned ValNo) const {
296      return valnos[ValNo];
297    }
298
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);
302    }
303
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) {
307      VNInfo *VNI =
308        new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), def);
309      valnos.push_back(VNI);
310      return VNI;
311    }
312
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);
317
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) {
322      VNInfo *VNI =
323        new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), *orig);
324      valnos.push_back(VNI);
325      return VNI;
326    }
327
328    /// RenumberValues - Renumber all values in order of appearance and remove
329    /// unused values.
330    void RenumberValues();
331
332    /// MergeValueNumberInto - This method is called when two value numbers
333    /// are found to be equivalent.  This eliminates V1, replacing all
334    /// segments with the V1 value number with the V2 value number.  This can
335    /// cause merging of V1/V2 values numbers and compaction of the value space.
336    VNInfo* MergeValueNumberInto(VNInfo *V1, VNInfo *V2);
337
338    /// Merge all of the live segments of a specific val# in RHS into this live
339    /// range as the specified value number. The segments in RHS are allowed
340    /// to overlap with segments in the current range, it will replace the
341    /// value numbers of the overlaped live segments with the specified value
342    /// number.
343    void MergeSegmentsInAsValue(const LiveRange &RHS, VNInfo *LHSValNo);
344
345    /// MergeValueInAsValue - Merge all of the segments of a specific val#
346    /// in RHS into this live range as the specified value number.
347    /// The segments in RHS are allowed to overlap with segments in the
348    /// current range, but only if the overlapping segments have the
349    /// specified value number.
350    void MergeValueInAsValue(const LiveRange &RHS,
351                             const VNInfo *RHSValNo, VNInfo *LHSValNo);
352
353    bool empty() const { return segments.empty(); }
354
355    /// beginIndex - Return the lowest numbered slot covered.
356    SlotIndex beginIndex() const {
357      assert(!empty() && "Call to beginIndex() on empty range.");
358      return segments.front().start;
359    }
360
361    /// endNumber - return the maximum point of the range of the whole,
362    /// exclusive.
363    SlotIndex endIndex() const {
364      assert(!empty() && "Call to endIndex() on empty range.");
365      return segments.back().end;
366    }
367
368    bool expiredAt(SlotIndex index) const {
369      return index >= endIndex();
370    }
371
372    bool liveAt(SlotIndex index) const {
373      const_iterator r = find(index);
374      return r != end() && r->start <= index;
375    }
376
377    /// Return the segment that contains the specified index, or null if there
378    /// is none.
379    const Segment *getSegmentContaining(SlotIndex Idx) const {
380      const_iterator I = FindSegmentContaining(Idx);
381      return I == end() ? nullptr : &*I;
382    }
383
384    /// Return the live segment that contains the specified index, or null if
385    /// there is none.
386    Segment *getSegmentContaining(SlotIndex Idx) {
387      iterator I = FindSegmentContaining(Idx);
388      return I == end() ? nullptr : &*I;
389    }
390
391    /// getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
392    VNInfo *getVNInfoAt(SlotIndex Idx) const {
393      const_iterator I = FindSegmentContaining(Idx);
394      return I == end() ? nullptr : I->valno;
395    }
396
397    /// getVNInfoBefore - Return the VNInfo that is live up to but not
398    /// necessarilly including Idx, or NULL. Use this to find the reaching def
399    /// used by an instruction at this SlotIndex position.
400    VNInfo *getVNInfoBefore(SlotIndex Idx) const {
401      const_iterator I = FindSegmentContaining(Idx.getPrevSlot());
402      return I == end() ? nullptr : I->valno;
403    }
404
405    /// Return an iterator to the segment that contains the specified index, or
406    /// end() if there is none.
407    iterator FindSegmentContaining(SlotIndex Idx) {
408      iterator I = find(Idx);
409      return I != end() && I->start <= Idx ? I : end();
410    }
411
412    const_iterator FindSegmentContaining(SlotIndex Idx) const {
413      const_iterator I = find(Idx);
414      return I != end() && I->start <= Idx ? I : end();
415    }
416
417    /// overlaps - Return true if the intersection of the two live ranges is
418    /// not empty.
419    bool overlaps(const LiveRange &other) const {
420      if (other.empty())
421        return false;
422      return overlapsFrom(other, other.begin());
423    }
424
425    /// overlaps - Return true if the two ranges have overlapping segments
426    /// that are not coalescable according to CP.
427    ///
428    /// Overlapping segments where one range is defined by a coalescable
429    /// copy are allowed.
430    bool overlaps(const LiveRange &Other, const CoalescerPair &CP,
431                  const SlotIndexes&) const;
432
433    /// overlaps - Return true if the live range overlaps an interval specified
434    /// by [Start, End).
435    bool overlaps(SlotIndex Start, SlotIndex End) const;
436
437    /// overlapsFrom - Return true if the intersection of the two live ranges
438    /// is not empty.  The specified iterator is a hint that we can begin
439    /// scanning the Other range starting at I.
440    bool overlapsFrom(const LiveRange &Other, const_iterator I) const;
441
442    /// Returns true if all segments of the @p Other live range are completely
443    /// covered by this live range.
444    /// Adjacent live ranges do not affect the covering:the liverange
445    /// [1,5](5,10] covers (3,7].
446    bool covers(const LiveRange &Other) const;
447
448    /// Add the specified Segment to this range, merging segments as
449    /// appropriate.  This returns an iterator to the inserted segment (which
450    /// may have grown since it was inserted).
451    iterator addSegment(Segment S);
452
453    /// If this range is live before @p Use in the basic block that starts at
454    /// @p StartIdx, extend it to be live up to @p Use, and return the value. If
455    /// there is no segment before @p Use, return nullptr.
456    VNInfo *extendInBlock(SlotIndex StartIdx, SlotIndex Use);
457
458    /// join - Join two live ranges (this, and other) together.  This applies
459    /// mappings to the value numbers in the LHS/RHS ranges as specified.  If
460    /// the ranges are not joinable, this aborts.
461    void join(LiveRange &Other,
462              const int *ValNoAssignments,
463              const int *RHSValNoAssignments,
464              SmallVectorImpl<VNInfo *> &NewVNInfo);
465
466    /// True iff this segment is a single segment that lies between the
467    /// specified boundaries, exclusively. Vregs live across a backedge are not
468    /// considered local. The boundaries are expected to lie within an extended
469    /// basic block, so vregs that are not live out should contain no holes.
470    bool isLocal(SlotIndex Start, SlotIndex End) const {
471      return beginIndex() > Start.getBaseIndex() &&
472        endIndex() < End.getBoundaryIndex();
473    }
474
475    /// Remove the specified segment from this range.  Note that the segment
476    /// must be a single Segment in its entirety.
477    void removeSegment(SlotIndex Start, SlotIndex End,
478                       bool RemoveDeadValNo = false);
479
480    void removeSegment(Segment S, bool RemoveDeadValNo = false) {
481      removeSegment(S.start, S.end, RemoveDeadValNo);
482    }
483
484    /// Remove segment pointed to by iterator @p I from this range.  This does
485    /// not remove dead value numbers.
486    iterator removeSegment(iterator I) {
487      return segments.erase(I);
488    }
489
490    /// Query Liveness at Idx.
491    /// The sub-instruction slot of Idx doesn't matter, only the instruction
492    /// it refers to is considered.
493    LiveQueryResult Query(SlotIndex Idx) const {
494      // Find the segment that enters the instruction.
495      const_iterator I = find(Idx.getBaseIndex());
496      const_iterator E = end();
497      if (I == E)
498        return LiveQueryResult(nullptr, nullptr, SlotIndex(), false);
499
500      // Is this an instruction live-in segment?
501      // If Idx is the start index of a basic block, include live-in segments
502      // that start at Idx.getBaseIndex().
503      VNInfo *EarlyVal = nullptr;
504      VNInfo *LateVal  = nullptr;
505      SlotIndex EndPoint;
506      bool Kill = false;
507      if (I->start <= Idx.getBaseIndex()) {
508        EarlyVal = I->valno;
509        EndPoint = I->end;
510        // Move to the potentially live-out segment.
511        if (SlotIndex::isSameInstr(Idx, I->end)) {
512          Kill = true;
513          if (++I == E)
514            return LiveQueryResult(EarlyVal, LateVal, EndPoint, Kill);
515        }
516        // Special case: A PHIDef value can have its def in the middle of a
517        // segment if the value happens to be live out of the layout
518        // predecessor.
519        // Such a value is not live-in.
520        if (EarlyVal->def == Idx.getBaseIndex())
521          EarlyVal = nullptr;
522      }
523      // I now points to the segment that may be live-through, or defined by
524      // this instr. Ignore segments starting after the current instr.
525      if (!SlotIndex::isEarlierInstr(Idx, I->start)) {
526        LateVal = I->valno;
527        EndPoint = I->end;
528      }
529      return LiveQueryResult(EarlyVal, LateVal, EndPoint, Kill);
530    }
531
532    /// removeValNo - Remove all the segments defined by the specified value#.
533    /// Also remove the value# from value# list.
534    void removeValNo(VNInfo *ValNo);
535
536    /// Returns true if the live range is zero length, i.e. no live segments
537    /// span instructions. It doesn't pay to spill such a range.
538    bool isZeroLength(SlotIndexes *Indexes) const {
539      for (const Segment &S : segments)
540        if (Indexes->getNextNonNullIndex(S.start).getBaseIndex() <
541            S.end.getBaseIndex())
542          return false;
543      return true;
544    }
545
546    bool operator<(const LiveRange& other) const {
547      const SlotIndex &thisIndex = beginIndex();
548      const SlotIndex &otherIndex = other.beginIndex();
549      return thisIndex < otherIndex;
550    }
551
552    /// Flush segment set into the regular segment vector.
553    /// The method is to be called after the live range
554    /// has been created, if use of the segment set was
555    /// activated in the constructor of the live range.
556    void flushSegmentSet();
557
558    void print(raw_ostream &OS) const;
559    void dump() const;
560
561    /// \brief Walk the range and assert if any invariants fail to hold.
562    ///
563    /// Note that this is a no-op when asserts are disabled.
564#ifdef NDEBUG
565    void verify() const {}
566#else
567    void verify() const;
568#endif
569
570  protected:
571    /// Append a segment to the list of segments.
572    void append(const LiveRange::Segment S);
573
574  private:
575    friend class LiveRangeUpdater;
576    void addSegmentToSet(Segment S);
577    void markValNoForDeletion(VNInfo *V);
578
579  };
580
581  inline raw_ostream &operator<<(raw_ostream &OS, const LiveRange &LR) {
582    LR.print(OS);
583    return OS;
584  }
585
586  /// LiveInterval - This class represents the liveness of a register,
587  /// or stack slot.
588  class LiveInterval : public LiveRange {
589  public:
590    typedef LiveRange super;
591
592    /// A live range for subregisters. The LaneMask specifies which parts of the
593    /// super register are covered by the interval.
594    /// (@sa TargetRegisterInfo::getSubRegIndexLaneMask()).
595    class SubRange : public LiveRange {
596    public:
597      SubRange *Next;
598      unsigned LaneMask;
599
600      /// Constructs a new SubRange object.
601      SubRange(unsigned LaneMask)
602        : Next(nullptr), LaneMask(LaneMask) {
603      }
604
605      /// Constructs a new SubRange object by copying liveness from @p Other.
606      SubRange(unsigned LaneMask, const LiveRange &Other,
607               BumpPtrAllocator &Allocator)
608        : LiveRange(Other, Allocator), Next(nullptr), LaneMask(LaneMask) {
609      }
610    };
611
612  private:
613    SubRange *SubRanges; ///< Single linked list of subregister live ranges.
614
615  public:
616    const unsigned reg;  // the register or stack slot of this interval.
617    float weight;        // weight of this interval
618
619    LiveInterval(unsigned Reg, float Weight)
620      : SubRanges(nullptr), reg(Reg), weight(Weight) {}
621
622    ~LiveInterval() {
623      clearSubRanges();
624    }
625
626    template<typename T>
627    class SingleLinkedListIterator {
628      T *P;
629    public:
630      SingleLinkedListIterator<T>(T *P) : P(P) {}
631      SingleLinkedListIterator<T> &operator++() {
632        P = P->Next;
633        return *this;
634      }
635      SingleLinkedListIterator<T> &operator++(int) {
636        SingleLinkedListIterator res = *this;
637        ++*this;
638        return res;
639      }
640      bool operator!=(const SingleLinkedListIterator<T> &Other) {
641        return P != Other.operator->();
642      }
643      bool operator==(const SingleLinkedListIterator<T> &Other) {
644        return P == Other.operator->();
645      }
646      T &operator*() const {
647        return *P;
648      }
649      T *operator->() const {
650        return P;
651      }
652    };
653
654    typedef SingleLinkedListIterator<SubRange> subrange_iterator;
655    subrange_iterator subrange_begin() {
656      return subrange_iterator(SubRanges);
657    }
658    subrange_iterator subrange_end() {
659      return subrange_iterator(nullptr);
660    }
661
662    typedef SingleLinkedListIterator<const SubRange> const_subrange_iterator;
663    const_subrange_iterator subrange_begin() const {
664      return const_subrange_iterator(SubRanges);
665    }
666    const_subrange_iterator subrange_end() const {
667      return const_subrange_iterator(nullptr);
668    }
669
670    iterator_range<subrange_iterator> subranges() {
671      return make_range(subrange_begin(), subrange_end());
672    }
673
674    iterator_range<const_subrange_iterator> subranges() const {
675      return make_range(subrange_begin(), subrange_end());
676    }
677
678    /// Creates a new empty subregister live range. The range is added at the
679    /// beginning of the subrange list; subrange iterators stay valid.
680    SubRange *createSubRange(BumpPtrAllocator &Allocator, unsigned LaneMask) {
681      SubRange *Range = new (Allocator) SubRange(LaneMask);
682      appendSubRange(Range);
683      return Range;
684    }
685
686    /// Like createSubRange() but the new range is filled with a copy of the
687    /// liveness information in @p CopyFrom.
688    SubRange *createSubRangeFrom(BumpPtrAllocator &Allocator, unsigned LaneMask,
689                                 const LiveRange &CopyFrom) {
690      SubRange *Range = new (Allocator) SubRange(LaneMask, CopyFrom, Allocator);
691      appendSubRange(Range);
692      return Range;
693    }
694
695    /// Returns true if subregister liveness information is available.
696    bool hasSubRanges() const {
697      return SubRanges != nullptr;
698    }
699
700    /// Removes all subregister liveness information.
701    void clearSubRanges();
702
703    /// Removes all subranges without any segments (subranges without segments
704    /// are not considered valid and should only exist temporarily).
705    void removeEmptySubRanges();
706
707    /// Construct main live range by merging the SubRanges of @p LI.
708    void constructMainRangeFromSubranges(const SlotIndexes &Indexes,
709                                         VNInfo::Allocator &VNIAllocator);
710
711    /// getSize - Returns the sum of sizes of all the LiveRange's.
712    ///
713    unsigned getSize() const;
714
715    /// isSpillable - Can this interval be spilled?
716    bool isSpillable() const {
717      return weight != llvm::huge_valf;
718    }
719
720    /// markNotSpillable - Mark interval as not spillable
721    void markNotSpillable() {
722      weight = llvm::huge_valf;
723    }
724
725    bool operator<(const LiveInterval& other) const {
726      const SlotIndex &thisIndex = beginIndex();
727      const SlotIndex &otherIndex = other.beginIndex();
728      return std::tie(thisIndex, reg) < std::tie(otherIndex, other.reg);
729    }
730
731    void print(raw_ostream &OS) const;
732    void dump() const;
733
734    /// \brief Walks the interval and assert if any invariants fail to hold.
735    ///
736    /// Note that this is a no-op when asserts are disabled.
737#ifdef NDEBUG
738    void verify(const MachineRegisterInfo *MRI = nullptr) const {}
739#else
740    void verify(const MachineRegisterInfo *MRI = nullptr) const;
741#endif
742
743  private:
744    /// Appends @p Range to SubRanges list.
745    void appendSubRange(SubRange *Range) {
746      Range->Next = SubRanges;
747      SubRanges = Range;
748    }
749
750    /// Free memory held by SubRange.
751    void freeSubRange(SubRange *S);
752  };
753
754  inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) {
755    LI.print(OS);
756    return OS;
757  }
758
759  raw_ostream &operator<<(raw_ostream &OS, const LiveRange::Segment &S);
760
761  inline bool operator<(SlotIndex V, const LiveRange::Segment &S) {
762    return V < S.start;
763  }
764
765  inline bool operator<(const LiveRange::Segment &S, SlotIndex V) {
766    return S.start < V;
767  }
768
769  /// Helper class for performant LiveRange bulk updates.
770  ///
771  /// Calling LiveRange::addSegment() repeatedly can be expensive on large
772  /// live ranges because segments after the insertion point may need to be
773  /// shifted. The LiveRangeUpdater class can defer the shifting when adding
774  /// many segments in order.
775  ///
776  /// The LiveRange will be in an invalid state until flush() is called.
777  class LiveRangeUpdater {
778    LiveRange *LR;
779    SlotIndex LastStart;
780    LiveRange::iterator WriteI;
781    LiveRange::iterator ReadI;
782    SmallVector<LiveRange::Segment, 16> Spills;
783    void mergeSpills();
784
785  public:
786    /// Create a LiveRangeUpdater for adding segments to LR.
787    /// LR will temporarily be in an invalid state until flush() is called.
788    LiveRangeUpdater(LiveRange *lr = nullptr) : LR(lr) {}
789
790    ~LiveRangeUpdater() { flush(); }
791
792    /// Add a segment to LR and coalesce when possible, just like
793    /// LR.addSegment(). Segments should be added in increasing start order for
794    /// best performance.
795    void add(LiveRange::Segment);
796
797    void add(SlotIndex Start, SlotIndex End, VNInfo *VNI) {
798      add(LiveRange::Segment(Start, End, VNI));
799    }
800
801    /// Return true if the LR is currently in an invalid state, and flush()
802    /// needs to be called.
803    bool isDirty() const { return LastStart.isValid(); }
804
805    /// Flush the updater state to LR so it is valid and contains all added
806    /// segments.
807    void flush();
808
809    /// Select a different destination live range.
810    void setDest(LiveRange *lr) {
811      if (LR != lr && isDirty())
812        flush();
813      LR = lr;
814    }
815
816    /// Get the current destination live range.
817    LiveRange *getDest() const { return LR; }
818
819    void dump() const;
820    void print(raw_ostream&) const;
821  };
822
823  inline raw_ostream &operator<<(raw_ostream &OS, const LiveRangeUpdater &X) {
824    X.print(OS);
825    return OS;
826  }
827
828  /// ConnectedVNInfoEqClasses - Helper class that can divide VNInfos in a
829  /// LiveInterval into equivalence clases of connected components. A
830  /// LiveInterval that has multiple connected components can be broken into
831  /// multiple LiveIntervals.
832  ///
833  /// Given a LiveInterval that may have multiple connected components, run:
834  ///
835  ///   unsigned numComps = ConEQ.Classify(LI);
836  ///   if (numComps > 1) {
837  ///     // allocate numComps-1 new LiveIntervals into LIS[1..]
838  ///     ConEQ.Distribute(LIS);
839  /// }
840
841  class ConnectedVNInfoEqClasses {
842    LiveIntervals &LIS;
843    IntEqClasses EqClass;
844
845    // Note that values a and b are connected.
846    void Connect(unsigned a, unsigned b);
847
848    unsigned Renumber();
849
850  public:
851    explicit ConnectedVNInfoEqClasses(LiveIntervals &lis) : LIS(lis) {}
852
853    /// Classify - Classify the values in LI into connected components.
854    /// Return the number of connected components.
855    unsigned Classify(const LiveInterval *LI);
856
857    /// getEqClass - Classify creates equivalence classes numbered 0..N. Return
858    /// the equivalence class assigned the VNI.
859    unsigned getEqClass(const VNInfo *VNI) const { return EqClass[VNI->id]; }
860
861    /// Distribute - Distribute values in LIV[0] into a separate LiveInterval
862    /// for each connected component. LIV must have a LiveInterval for each
863    /// connected component. The LiveIntervals in Liv[1..] must be empty.
864    /// Instructions using LIV[0] are rewritten.
865    void Distribute(LiveInterval *LIV[], MachineRegisterInfo &MRI);
866
867  };
868
869}
870#endif
871