LLVM  4.0.0
LiveInterval.h
Go to the documentation of this file.
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"
26 #include "llvm/Support/Allocator.h"
28 #include <cassert>
29 #include <climits>
30 #include <set>
31 
32 namespace 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:
48 
49  /// The ID number of this value.
50  unsigned id;
51 
52  /// The index of the defining instruction.
54 
55  /// VNInfo constructor.
56  VNInfo(unsigned i, SlotIndex d)
57  : id(i), def(d)
58  { }
59 
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)
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.
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.
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 
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.
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 
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 
205  iterator begin() { return segments.begin(); }
206  iterator end() { return segments.end(); }
207 
209  const_iterator begin() const { return segments.begin(); }
210  const_iterator end() const { return segments.end(); }
211 
214  vni_iterator vni_end() { return valnos.end(); }
215 
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.
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) {
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.
247  assert(I != end());
248  if (Pos >= endIndex())
249  return end();
250  while (I->end <= Pos) ++I;
251  return I;
252  }
253 
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 
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 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);
321 
322  /// Create a copy of the given value. The new value will be identical except
323  /// for the Value number.
325  VNInfo::Allocator &VNInfoAllocator) {
326  VNInfo *VNI =
327  new (VNInfoAllocator) VNInfo((unsigned)valnos.size(), *orig);
328  valnos.push_back(VNI);
329  return VNI;
330  }
331 
332  /// RenumberValues - Renumber all values in order of appearance and remove
333  /// unused values.
334  void RenumberValues();
335 
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.
341 
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
346  /// number.
347  void MergeSegmentsInAsValue(const LiveRange &RHS, VNInfo *LHSValNo);
348 
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);
356 
357  bool empty() const { return segments.empty(); }
358 
359  /// beginIndex - Return the lowest numbered slot covered.
361  assert(!empty() && "Call to beginIndex() on empty range.");
362  return segments.front().start;
363  }
364 
365  /// endNumber - return the maximum point of the range of the whole,
366  /// exclusive.
367  SlotIndex endIndex() const {
368  assert(!empty() && "Call to endIndex() on empty range.");
369  return segments.back().end;
370  }
371 
372  bool expiredAt(SlotIndex index) const {
373  return index >= endIndex();
374  }
375 
376  bool liveAt(SlotIndex index) const {
377  const_iterator r = find(index);
378  return r != end() && r->start <= index;
379  }
380 
381  /// Return the segment that contains the specified index, or null if there
382  /// is none.
385  return I == end() ? nullptr : &*I;
386  }
387 
388  /// Return the live segment that contains the specified index, or null if
389  /// there is none.
392  return I == end() ? nullptr : &*I;
393  }
394 
395  /// getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
398  return I == end() ? nullptr : I->valno;
399  }
400 
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.
406  return I == end() ? nullptr : I->valno;
407  }
408 
409  /// Return an iterator to the segment that contains the specified index, or
410  /// end() if there is none.
412  iterator I = find(Idx);
413  return I != end() && I->start <= Idx ? I : end();
414  }
415 
417  const_iterator I = find(Idx);
418  return I != end() && I->start <= Idx ? I : end();
419  }
420 
421  /// overlaps - Return true if the intersection of the two live ranges is
422  /// not empty.
423  bool overlaps(const LiveRange &other) const {
424  if (other.empty())
425  return false;
426  return overlapsFrom(other, other.begin());
427  }
428 
429  /// overlaps - Return true if the two ranges have overlapping segments
430  /// that are not coalescable according to CP.
431  ///
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;
436 
437  /// overlaps - Return true if the live range overlaps an interval specified
438  /// by [Start, End).
439  bool overlaps(SlotIndex Start, SlotIndex End) const;
440 
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;
445 
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;
451 
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);
456 
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);
473 
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.
480 
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);
488 
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();
496  }
497 
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);
502 
503  void removeSegment(Segment S, bool RemoveDeadValNo = false) {
504  removeSegment(S.start, S.end, RemoveDeadValNo);
505  }
506 
507  /// Remove segment pointed to by iterator @p I from this range. This does
508  /// not remove dead value numbers.
510  return segments.erase(I);
511  }
512 
513  /// Query Liveness at Idx.
514  /// The sub-instruction slot of Idx doesn't matter, only the instruction
515  /// it refers to is considered.
517  // Find the segment that enters the instruction.
519  const_iterator E = end();
520  if (I == E)
521  return LiveQueryResult(nullptr, nullptr, SlotIndex(), false);
522 
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;
528  SlotIndex EndPoint;
529  bool Kill = false;
530  if (I->start <= Idx.getBaseIndex()) {
531  EarlyVal = I->valno;
532  EndPoint = I->end;
533  // Move to the potentially live-out segment.
534  if (SlotIndex::isSameInstr(Idx, I->end)) {
535  Kill = true;
536  if (++I == E)
537  return LiveQueryResult(EarlyVal, LateVal, EndPoint, Kill);
538  }
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
541  // predecessor.
542  // Such a value is not live-in.
543  if (EarlyVal->def == Idx.getBaseIndex())
544  EarlyVal = nullptr;
545  }
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)) {
549  LateVal = I->valno;
550  EndPoint = I->end;
551  }
552  return LiveQueryResult(EarlyVal, LateVal, EndPoint, Kill);
553  }
554 
555  /// removeValNo - Remove all the segments defined by the specified value#.
556  /// Also remove the value# from value# list.
557  void removeValNo(VNInfo *ValNo);
558 
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())
565  return false;
566  return true;
567  }
568 
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;
573 
574  bool operator<(const LiveRange& other) const {
575  const SlotIndex &thisIndex = beginIndex();
576  const SlotIndex &otherIndex = other.beginIndex();
577  return thisIndex < otherIndex;
578  }
579 
580  /// Returns true if there is an explicit "undef" between @p Begin
581  /// @p End.
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;
587  });
588  }
589 
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();
595 
596  void print(raw_ostream &OS) const;
597  void dump() const;
598 
599  /// \brief Walk the range and assert if any invariants fail to hold.
600  ///
601  /// Note that this is a no-op when asserts are disabled.
602 #ifdef NDEBUG
603  void verify() const {}
604 #else
605  void verify() const;
606 #endif
607 
608  protected:
609  /// Append a segment to the list of segments.
610  void append(const LiveRange::Segment S);
611 
612  private:
613  friend class LiveRangeUpdater;
614  void addSegmentToSet(Segment S);
615  void markValNoForDeletion(VNInfo *V);
616  };
617 
618  inline raw_ostream &operator<<(raw_ostream &OS, const LiveRange &LR) {
619  LR.print(OS);
620  return OS;
621  }
622 
623  /// LiveInterval - This class represents the liveness of a register,
624  /// or stack slot.
625  class LiveInterval : public LiveRange {
626  public:
627  typedef LiveRange super;
628 
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 {
633  public:
636 
637  /// Constructs a new SubRange object.
639  : Next(nullptr), LaneMask(LaneMask) {
640  }
641 
642  /// Constructs a new SubRange object by copying liveness from @p Other.
645  : LiveRange(Other, Allocator), Next(nullptr), LaneMask(LaneMask) {
646  }
647 
648  void print(raw_ostream &OS) const;
649  void dump() const;
650  };
651 
652  private:
653  SubRange *SubRanges; ///< Single linked list of subregister live ranges.
654 
655  public:
656  const unsigned reg; // the register or stack slot of this interval.
657  float weight; // weight of this interval
658 
659  LiveInterval(unsigned Reg, float Weight)
660  : SubRanges(nullptr), reg(Reg), weight(Weight) {}
661 
663  clearSubRanges();
664  }
665 
666  template<typename T>
668  T *P;
669  public:
672  P = P->Next;
673  return *this;
674  }
676  SingleLinkedListIterator res = *this;
677  ++*this;
678  return res;
679  }
681  return P != Other.operator->();
682  }
684  return P == Other.operator->();
685  }
686  T &operator*() const {
687  return *P;
688  }
689  T *operator->() const {
690  return P;
691  }
692  };
693 
696  return subrange_iterator(SubRanges);
697  }
699  return subrange_iterator(nullptr);
700  }
701 
704  return const_subrange_iterator(SubRanges);
705  }
707  return const_subrange_iterator(nullptr);
708  }
709 
712  }
713 
716  }
717 
718  /// Creates a new empty subregister live range. The range is added at the
719  /// beginning of the subrange list; subrange iterators stay valid.
721  LaneBitmask LaneMask) {
722  SubRange *Range = new (Allocator) SubRange(LaneMask);
723  appendSubRange(Range);
724  return Range;
725  }
726 
727  /// Like createSubRange() but the new range is filled with a copy of the
728  /// liveness information in @p CopyFrom.
730  LaneBitmask LaneMask,
731  const LiveRange &CopyFrom) {
732  SubRange *Range = new (Allocator) SubRange(LaneMask, CopyFrom, Allocator);
733  appendSubRange(Range);
734  return Range;
735  }
736 
737  /// Returns true if subregister liveness information is available.
738  bool hasSubRanges() const {
739  return SubRanges != nullptr;
740  }
741 
742  /// Removes all subregister liveness information.
743  void clearSubRanges();
744 
745  /// Removes all subranges without any segments (subranges without segments
746  /// are not considered valid and should only exist temporarily).
747  void removeEmptySubRanges();
748 
749  /// getSize - Returns the sum of sizes of all the LiveRange's.
750  ///
751  unsigned getSize() const;
752 
753  /// isSpillable - Can this interval be spilled?
754  bool isSpillable() const {
755  return weight != llvm::huge_valf;
756  }
757 
758  /// markNotSpillable - Mark interval as not spillable
761  }
762 
763  /// For a given lane mask @p LaneMask, compute indexes at which the
764  /// lane is marked undefined by subregister <def,read-undef> definitions.
766  LaneBitmask LaneMask,
767  const MachineRegisterInfo &MRI,
768  const SlotIndexes &Indexes) const;
769 
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);
774  }
775 
776  void print(raw_ostream &OS) const;
777  void dump() const;
778 
779  /// \brief Walks the interval and assert if any invariants fail to hold.
780  ///
781  /// Note that this is a no-op when asserts are disabled.
782 #ifdef NDEBUG
783  void verify(const MachineRegisterInfo *MRI = nullptr) const {}
784 #else
785  void verify(const MachineRegisterInfo *MRI = nullptr) const;
786 #endif
787 
788  private:
789  /// Appends @p Range to SubRanges list.
790  void appendSubRange(SubRange *Range) {
791  Range->Next = SubRanges;
792  SubRanges = Range;
793  }
794 
795  /// Free memory held by SubRange.
796  void freeSubRange(SubRange *S);
797  };
798 
800  const LiveInterval::SubRange &SR) {
801  SR.print(OS);
802  return OS;
803  }
804 
805  inline raw_ostream &operator<<(raw_ostream &OS, const LiveInterval &LI) {
806  LI.print(OS);
807  return OS;
808  }
809 
810  raw_ostream &operator<<(raw_ostream &OS, const LiveRange::Segment &S);
811 
812  inline bool operator<(SlotIndex V, const LiveRange::Segment &S) {
813  return V < S.start;
814  }
815 
816  inline bool operator<(const LiveRange::Segment &S, SlotIndex V) {
817  return S.start < V;
818  }
819 
820  /// Helper class for performant LiveRange bulk updates.
821  ///
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.
826  ///
827  /// The LiveRange will be in an invalid state until flush() is called.
829  LiveRange *LR;
830  SlotIndex LastStart;
831  LiveRange::iterator WriteI;
832  LiveRange::iterator ReadI;
834  void mergeSpills();
835 
836  public:
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) {}
840 
842 
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);
847 
848  void add(SlotIndex Start, SlotIndex End, VNInfo *VNI) {
849  add(LiveRange::Segment(Start, End, VNI));
850  }
851 
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(); }
855 
856  /// Flush the updater state to LR so it is valid and contains all added
857  /// segments.
858  void flush();
859 
860  /// Select a different destination live range.
861  void setDest(LiveRange *lr) {
862  if (LR != lr && isDirty())
863  flush();
864  LR = lr;
865  }
866 
867  /// Get the current destination live range.
868  LiveRange *getDest() const { return LR; }
869 
870  void dump() const;
871  void print(raw_ostream&) const;
872  };
873 
875  X.print(OS);
876  return OS;
877  }
878 
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.
883  ///
884  /// Given a LiveInterval that may have multiple connected components, run:
885  ///
886  /// unsigned numComps = ConEQ.Classify(LI);
887  /// if (numComps > 1) {
888  /// // allocate numComps-1 new LiveIntervals into LIS[1..]
889  /// ConEQ.Distribute(LIS);
890  /// }
891 
893  LiveIntervals &LIS;
894  IntEqClasses EqClass;
895 
896  public:
897  explicit ConnectedVNInfoEqClasses(LiveIntervals &lis) : LIS(lis) {}
898 
899  /// Classify the values in \p LR into connected components.
900  /// Returns the number of connected components.
901  unsigned Classify(const LiveRange &LR);
902 
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]; }
906 
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
910  /// left in \p LI.
911  void Distribute(LiveInterval &LI, LiveInterval *LIV[],
913  };
914 }
915 #endif
void add(LiveRange::Segment)
Add a segment to LR and coalesce when possible, just like LR.addSegment().
void RenumberValues()
RenumberValues - Renumber all values in order of appearance and remove unused values.
void push_back(const T &Elt)
Definition: SmallVector.h:211
const_vni_iterator vni_end() const
Definition: LiveInterval.h:218
const Segment * getSegmentContaining(SlotIndex Idx) const
Return the segment that contains the specified index, or null if there is none.
Definition: LiveInterval.h:383
void flush()
Flush the updater state to LR so it is valid and contains all added segments.
const_subrange_iterator subrange_begin() const
Definition: LiveInterval.h:703
Segments::iterator iterator
Definition: LiveInterval.h:204
VNInfo(unsigned i, SlotIndex d)
VNInfo constructor.
Definition: LiveInterval.h:56
const unsigned reg
Definition: LiveInterval.h:656
SlotIndex def
The index of the defining instruction.
Definition: LiveInterval.h:53
size_t i
bool contains(SlotIndex I) const
Return true if the index is covered by this segment.
Definition: LiveInterval.h:172
SlotIndex getBoundaryIndex() const
Returns the boundary index for associated with this index.
Definition: SlotIndexes.h:241
SlotIndex getBaseIndex() const
Returns the base index for associated with this index.
Definition: SlotIndexes.h:234
void MergeValueInAsValue(const LiveRange &RHS, const VNInfo *RHSValNo, VNInfo *LHSValNo)
MergeValueInAsValue - Merge all of the segments of a specific val# in RHS into this live range as the...
Segment * getSegmentContaining(SlotIndex Idx)
Return the live segment that contains the specified index, or null if there is none.
Definition: LiveInterval.h:390
SubRange(LaneBitmask LaneMask)
Constructs a new SubRange object.
Definition: LiveInterval.h:638
vni_iterator vni_begin()
Definition: LiveInterval.h:213
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:625
bool isSpillable() const
isSpillable - Can this interval be spilled?
Definition: LiveInterval.h:754
iterator end() const
Definition: ArrayRef.h:130
iterator advanceTo(iterator I, SlotIndex Pos)
advanceTo - Advance the specified iterator to point to the Segment containing the specified position...
Definition: LiveInterval.h:246
bool isLocal(SlotIndex Start, SlotIndex End) const
True iff this segment is a single segment that lies between the specified boundaries, exclusively.
Definition: LiveInterval.h:493
bool isLiveAtIndexes(ArrayRef< SlotIndex > Slots) const
LiveRangeUpdater(LiveRange *lr=nullptr)
Create a LiveRangeUpdater for adding segments to LR.
Definition: LiveInterval.h:839
size_t size() const
Definition: LiveInterval.h:280
bool isKill() const
Return true if the live-in value is killed by this instruction.
Definition: LiveInterval.h:108
LiveInterval(unsigned Reg, float Weight)
Definition: LiveInterval.h:659
A live range for subregisters.
Definition: LiveInterval.h:632
This represents a simple continuous liveness interval for a value.
Definition: LiveInterval.h:159
bool containsInterval(SlotIndex S, SlotIndex E) const
Return true if the given interval, [S, E), is covered by this segment.
Definition: LiveInterval.h:177
SlotIndex endPoint() const
Return the end point of the last live range segment to interact with the instruction, if any.
Definition: LiveInterval.h:143
bool operator<(const LiveInterval &other) const
Definition: LiveInterval.h:770
void markUnused()
Mark this value as unused.
Definition: LiveInterval.h:80
SubRange * createSubRangeFrom(BumpPtrAllocator &Allocator, LaneBitmask LaneMask, const LiveRange &CopyFrom)
Like createSubRange() but the new range is filled with a copy of the liveness information in CopyFrom...
Definition: LiveInterval.h:729
VNInfo - Value Number Information.
Definition: LiveInterval.h:45
unsigned getNumValNums() const
Definition: LiveInterval.h:288
void flushSegmentSet()
Flush segment set into the regular segment vector.
bool empty() const
Definition: LiveInterval.h:357
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
Definition: LiveInterval.h:396
This file defines the MallocAllocator and BumpPtrAllocator interfaces.
void Distribute(LiveInterval &LI, LiveInterval *LIV[], MachineRegisterInfo &MRI)
Distribute values in LI into a separate LiveIntervals for each connected component.
This class represents the liveness of a register, stack slot, etc.
Definition: LiveInterval.h:153
bool isDirty() const
Return true if the LR is currently in an invalid state, and flush() needs to be called.
Definition: LiveInterval.h:854
static bool isEarlierInstr(SlotIndex A, SlotIndex B)
isEarlierInstr - Return true if A refers to an instruction earlier than B.
Definition: SlotIndexes.h:196
void removeEmptySubRanges()
Removes all subranges without any segments (subranges without segments are not considered valid and s...
const_iterator FindSegmentContaining(SlotIndex Idx) const
Definition: LiveInterval.h:416
bool operator<(const LiveRange &other) const
Definition: LiveInterval.h:574
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
iterator end()
Definition: LiveInterval.h:206
A helper class for register coalescers.
iterator_range< subrange_iterator > subranges()
Definition: LiveInterval.h:710
Result of a LiveRange query.
Definition: LiveInterval.h:86
SingleLinkedListIterator< T > & operator++()
Definition: LiveInterval.h:671
Reg
All possible values of the reg field in the ModR/M byte.
bool operator<(const Segment &Other) const
Definition: LiveInterval.h:182
bool isBlock() const
isBlock - Returns true if this is a block boundary slot.
Definition: SlotIndexes.h:219
unsigned getSize() const
getSize - Returns the sum of sizes of all the LiveRange's.
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
void print(raw_ostream &OS) const
bool isUnused() const
Returns true if this value is unused.
Definition: LiveInterval.h:77
void MergeSegmentsInAsValue(const LiveRange &RHS, VNInfo *LHSValNo)
Merge all of the live segments of a specific val# in RHS into this live range as the specified value ...
SlotIndex getNextNonNullIndex(SlotIndex Index)
Returns the next non-null index, if one exists.
Definition: SlotIndexes.h:422
bool isDead() const
isDead - Returns true if this is a dead def kill slot.
Definition: SlotIndexes.h:229
SlotIndexes pass.
Definition: SlotIndexes.h:323
iterator addSegment(Segment S)
Add the specified Segment to this range, merging segments as appropriate.
LiveQueryResult(VNInfo *EarlyVal, VNInfo *LateVal, SlotIndex EndPoint, bool Kill)
Definition: LiveInterval.h:93
Segments segments
Definition: LiveInterval.h:195
VNInfo * MergeValueNumberInto(VNInfo *V1, VNInfo *V2)
MergeValueNumberInto - This method is called when two value numbers are found to be equivalent...
VNInfo * valueOutOrDead() const
Returns the value alive at the end of the instruction, if any.
Definition: LiveInterval.h:125
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
const float huge_valf
Use this rather than HUGE_VALF; the latter causes warnings on MSVC.
Definition: MathExtras.cpp:29
LiveRange(const LiveRange &Other, BumpPtrAllocator &Allocator)
Constructs a new LiveRange object by copying segments and valnos from another LiveRange.
Definition: LiveInterval.h:227
iterator_range< const_subrange_iterator > subranges() const
Definition: LiveInterval.h:714
bool expiredAt(SlotIndex index) const
Definition: LiveInterval.h:372
VNInfoList::const_iterator const_vni_iterator
Definition: LiveInterval.h:216
const VNInfo * getValNumInfo(unsigned ValNo) const
Definition: LiveInterval.h:295
const_iterator advanceTo(const_iterator I, SlotIndex Pos) const
Definition: LiveInterval.h:254
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
void copyFrom(VNInfo &src)
Copy from the parameter into this VNInfo.
Definition: LiveInterval.h:66
SlotIndex getPrevSlot() const
Returns the previous slot in the index list.
Definition: SlotIndexes.h:282
bool isDeadDef() const
Return true if this instruction has a dead def.
Definition: LiveInterval.h:113
void removeValNo(VNInfo *ValNo)
removeValNo - Remove all the segments defined by the specified value#.
SubRange(LaneBitmask LaneMask, const LiveRange &Other, BumpPtrAllocator &Allocator)
Constructs a new SubRange object by copying liveness from Other.
Definition: LiveInterval.h:643
bool hasAtLeastOneValue() const
Definition: LiveInterval.h:284
LiveQueryResult Query(SlotIndex Idx) const
Query Liveness at Idx.
Definition: LiveInterval.h:516
bool isValid() const
Returns true if this is a valid index.
Definition: SlotIndexes.h:144
SubRange * createSubRange(BumpPtrAllocator &Allocator, LaneBitmask LaneMask)
Creates a new empty subregister live range.
Definition: LiveInterval.h:720
void removeSegment(Segment S, bool RemoveDeadValNo=false)
Definition: LiveInterval.h:503
unsigned const MachineRegisterInfo * MRI
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&...args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
Definition: STLExtras.h:845
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:138
std::pair< VNInfo *, bool > extendInBlock(ArrayRef< SlotIndex > Undefs, SlotIndex StartIdx, SlotIndex Use)
Attempt to extend a value defined after StartIdx to include Use.
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
unsigned Classify(const LiveRange &LR)
Classify the values in LR into connected components.
LiveRange(bool UseSegmentSet=false)
Constructs a new LiveRange object.
Definition: LiveInterval.h:221
void setDest(LiveRange *lr)
Select a different destination live range.
Definition: LiveInterval.h:861
const_iterator begin() const
Definition: LiveInterval.h:209
bool overlaps(const LiveRange &other) const
overlaps - Return true if the intersection of the two live ranges is not empty.
Definition: LiveInterval.h:423
Greedy Register Allocator
bool any_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:743
void dump() const
void add(SlotIndex Start, SlotIndex End, VNInfo *VNI)
Definition: LiveInterval.h:848
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang","erlang-compatible garbage collector")
static const unsigned End
iterator begin() const
Definition: ArrayRef.h:129
VNInfo * valueDefined() const
Return the value defined by this instruction, if any.
Definition: LiveInterval.h:131
void append(const LiveRange::Segment S)
Append a segment to the list of segments.
void dump() const
void verify() const
Walk the range and assert if any invariants fail to hold.
SmallVector< Segment, 2 > Segments
Definition: LiveInterval.h:192
iterator removeSegment(iterator I)
Remove segment pointed to by iterator I from this range.
Definition: LiveInterval.h:509
LiveRange * getDest() const
Get the current destination live range.
Definition: LiveInterval.h:868
iterator erase(const_iterator CI)
Definition: SmallVector.h:431
bool liveAt(SlotIndex index) const
Definition: LiveInterval.h:376
VNInfoList::iterator vni_iterator
Definition: LiveInterval.h:212
VNInfo(unsigned i, const VNInfo &orig)
VNInfo constructor, copies values from orig, except for the value number.
Definition: LiveInterval.h:61
bool isPHIDef() const
Returns true if this value is defined by a PHI instruction (or was, PHI instructions may have been el...
Definition: LiveInterval.h:74
bool containsValue(const VNInfo *VNI) const
containsValue - Returns true if VNI belongs to this range.
Definition: LiveInterval.h:300
bool operator==(const Segment &Other) const
Definition: LiveInterval.h:185
iterator find(SlotIndex Pos)
find - Return an iterator pointing to the first segment that ends after Pos, or end().
VNInfo * valueOut() const
Return the value leaving the instruction, if any.
Definition: LiveInterval.h:119
bool isUndefIn(ArrayRef< SlotIndex > Undefs, SlotIndex Begin, SlotIndex End) const
Returns true if there is an explicit "undef" between Begin End.
Definition: LiveInterval.h:582
unsigned id
The ID number of this value.
Definition: LiveInterval.h:50
void removeSegment(SlotIndex Start, SlotIndex End, bool RemoveDeadValNo=false)
Remove the specified segment from this range.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
ConnectedVNInfoEqClasses(LiveIntervals &lis)
Definition: LiveInterval.h:897
const_vni_iterator vni_begin() const
Definition: LiveInterval.h:217
Segments::const_iterator const_iterator
Definition: LiveInterval.h:208
static bool isSameInstr(SlotIndex A, SlotIndex B)
isSameInstr - Return true if A and B refer to the same instruction.
Definition: SlotIndexes.h:190
BumpPtrAllocator Allocator
Definition: LiveInterval.h:47
SingleLinkedListIterator< SubRange > subrange_iterator
Definition: LiveInterval.h:694
ConnectedVNInfoEqClasses - Helper class that can divide VNInfos in a LiveInterval into equivalence cl...
Definition: LiveInterval.h:892
bool operator==(const SingleLinkedListIterator< T > &Other)
Definition: LiveInterval.h:683
std::unique_ptr< SegmentSet > segmentSet
Definition: LiveInterval.h:202
void markNotSpillable()
markNotSpillable - Mark interval as not spillable
Definition: LiveInterval.h:759
SingleLinkedListIterator< const SubRange > const_subrange_iterator
Definition: LiveInterval.h:702
SmallVector< VNInfo *, 2 > VNInfoList
Definition: LiveInterval.h:193
void clearSubRanges()
Removes all subregister liveness information.
void print(raw_ostream &OS) const
A range adaptor for a pair of iterators.
bool isZeroLength(SlotIndexes *Indexes) const
Returns true if the live range is zero length, i.e.
Definition: LiveInterval.h:561
VNInfoList valnos
Definition: LiveInterval.h:196
VNInfo * getValNumInfo(unsigned ValNo)
getValNumInfo - Returns pointer to the specified val#.
Definition: LiveInterval.h:292
bool containsOneValue() const
Definition: LiveInterval.h:286
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
SlotIndex beginIndex() const
beginIndex - Return the lowest numbered slot covered.
Definition: LiveInterval.h:360
void print(raw_ostream &OS) const
void print(raw_ostream &) const
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
VNInfo * createDeadDef(SlotIndex Def, VNInfo::Allocator &VNInfoAllocator)
createDeadDef - Make sure the range has a value defined at Def.
SlotIndex endIndex() const
endNumber - return the maximum point of the range of the whole, exclusive.
Definition: LiveInterval.h:367
const_subrange_iterator subrange_end() const
Definition: LiveInterval.h:706
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
bool operator!=(const SingleLinkedListIterator< T > &Other)
Definition: LiveInterval.h:680
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:1726
iterator begin()
Definition: LiveInterval.h:205
Helper class for performant LiveRange bulk updates.
Definition: LiveInterval.h:828
std::set< Segment > SegmentSet
Definition: LiveInterval.h:201
VNInfo * getNextValue(SlotIndex def, VNInfo::Allocator &VNInfoAllocator)
getNextValue - Create a new value number and return it.
Definition: LiveInterval.h:306
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool operator<(int64_t V1, const APSInt &V2)
Definition: APSInt.h:326
SingleLinkedListIterator< T > operator++(int)
Definition: LiveInterval.h:675
Segment(SlotIndex S, SlotIndex E, VNInfo *V)
Definition: LiveInterval.h:166
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
subrange_iterator subrange_end()
Definition: LiveInterval.h:698
void computeSubRangeUndefs(SmallVectorImpl< SlotIndex > &Undefs, LaneBitmask LaneMask, const MachineRegisterInfo &MRI, const SlotIndexes &Indexes) const
For a given lane mask LaneMask, compute indexes at which the lane is marked undefined by subregister ...
void join(LiveRange &Other, const int *ValNoAssignments, const int *RHSValNoAssignments, SmallVectorImpl< VNInfo * > &NewVNInfo)
join - Join two live ranges (this, and other) together.
iterator FindSegmentContaining(SlotIndex Idx)
Return an iterator to the segment that contains the specified index, or end() if there is none...
Definition: LiveInterval.h:411
VNInfo * valueIn() const
Return the value that is live-in to the instruction.
Definition: LiveInterval.h:101
unsigned getEqClass(const VNInfo *VNI) const
getEqClass - Classify creates equivalence classes numbered 0..N.
Definition: LiveInterval.h:905
const_iterator find(SlotIndex Pos) const
Definition: LiveInterval.h:271
bool hasSubRanges() const
Returns true if subregister liveness information is available.
Definition: LiveInterval.h:738
subrange_iterator subrange_begin()
Definition: LiveInterval.h:695
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:76
bool overlapsFrom(const LiveRange &Other, const_iterator I) const
overlapsFrom - Return true if the intersection of the two live ranges is not empty.
bool covers(const LiveRange &Other) const
Returns true if all segments of the Other live range are completely covered by this live range...
vni_iterator vni_end()
Definition: LiveInterval.h:214
const_iterator end() const
Definition: LiveInterval.h:210
VNInfo * getVNInfoBefore(SlotIndex Idx) const
getVNInfoBefore - Return the VNInfo that is live up to but not necessarilly including Idx...
Definition: LiveInterval.h:404
VNInfo * createValueCopy(const VNInfo *orig, VNInfo::Allocator &VNInfoAllocator)
Create a copy of the given value.
Definition: LiveInterval.h:324