LLVM  5.0.0
LiveInterval.cpp
Go to the documentation of this file.
1 //===-- LiveInterval.cpp - Live Interval Representation -------------------===//
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 
22 
23 #include "LiveRangeUtils.h"
24 #include "RegisterCoalescer.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/SmallSet.h"
29 #include "llvm/Support/Debug.h"
32 #include <algorithm>
33 using namespace llvm;
34 
35 namespace {
36 //===----------------------------------------------------------------------===//
37 // Implementation of various methods necessary for calculation of live ranges.
38 // The implementation of the methods abstracts from the concrete type of the
39 // segment collection.
40 //
41 // Implementation of the class follows the Template design pattern. The base
42 // class contains generic algorithms that call collection-specific methods,
43 // which are provided in concrete subclasses. In order to avoid virtual calls
44 // these methods are provided by means of C++ template instantiation.
45 // The base class calls the methods of the subclass through method impl(),
46 // which casts 'this' pointer to the type of the subclass.
47 //
48 //===----------------------------------------------------------------------===//
49 
50 template <typename ImplT, typename IteratorT, typename CollectionT>
51 class CalcLiveRangeUtilBase {
52 protected:
53  LiveRange *LR;
54 
55 protected:
56  CalcLiveRangeUtilBase(LiveRange *LR) : LR(LR) {}
57 
58 public:
59  typedef LiveRange::Segment Segment;
60  typedef IteratorT iterator;
61 
62  /// A counterpart of LiveRange::createDeadDef: Make sure the range has a
63  /// value defined at @p Def.
64  /// If @p ForVNI is null, and there is no value defined at @p Def, a new
65  /// value will be allocated using @p VNInfoAllocator.
66  /// If @p ForVNI is null, the return value is the value defined at @p Def,
67  /// either a pre-existing one, or the one newly created.
68  /// If @p ForVNI is not null, then @p Def should be the location where
69  /// @p ForVNI is defined. If the range does not have a value defined at
70  /// @p Def, the value @p ForVNI will be used instead of allocating a new
71  /// one. If the range already has a value defined at @p Def, it must be
72  /// same as @p ForVNI. In either case, @p ForVNI will be the return value.
74  VNInfo *ForVNI) {
75  assert(!Def.isDead() && "Cannot define a value at the dead slot");
76  assert((!ForVNI || ForVNI->def == Def) &&
77  "If ForVNI is specified, it must match Def");
78  iterator I = impl().find(Def);
79  if (I == segments().end()) {
80  VNInfo *VNI = ForVNI ? ForVNI : LR->getNextValue(Def, *VNInfoAllocator);
81  impl().insertAtEnd(Segment(Def, Def.getDeadSlot(), VNI));
82  return VNI;
83  }
84 
85  Segment *S = segmentAt(I);
86  if (SlotIndex::isSameInstr(Def, S->start)) {
87  assert((!ForVNI || ForVNI == S->valno) && "Value number mismatch");
88  assert(S->valno->def == S->start && "Inconsistent existing value def");
89 
90  // It is possible to have both normal and early-clobber defs of the same
91  // register on an instruction. It doesn't make a lot of sense, but it is
92  // possible to specify in inline assembly.
93  //
94  // Just convert everything to early-clobber.
95  Def = std::min(Def, S->start);
96  if (Def != S->start)
97  S->start = S->valno->def = Def;
98  return S->valno;
99  }
100  assert(SlotIndex::isEarlierInstr(Def, S->start) && "Already live at def");
101  VNInfo *VNI = ForVNI ? ForVNI : LR->getNextValue(Def, *VNInfoAllocator);
102  segments().insert(I, Segment(Def, Def.getDeadSlot(), VNI));
103  return VNI;
104  }
105 
106  VNInfo *extendInBlock(SlotIndex StartIdx, SlotIndex Use) {
107  if (segments().empty())
108  return nullptr;
109  iterator I =
110  impl().findInsertPos(Segment(Use.getPrevSlot(), Use, nullptr));
111  if (I == segments().begin())
112  return nullptr;
113  --I;
114  if (I->end <= StartIdx)
115  return nullptr;
116  if (I->end < Use)
117  extendSegmentEndTo(I, Use);
118  return I->valno;
119  }
120 
121  std::pair<VNInfo*,bool> extendInBlock(ArrayRef<SlotIndex> Undefs,
122  SlotIndex StartIdx, SlotIndex Use) {
123  if (segments().empty())
124  return std::make_pair(nullptr, false);
125  SlotIndex BeforeUse = Use.getPrevSlot();
126  iterator I = impl().findInsertPos(Segment(BeforeUse, Use, nullptr));
127  if (I == segments().begin())
128  return std::make_pair(nullptr, LR->isUndefIn(Undefs, StartIdx, BeforeUse));
129  --I;
130  if (I->end <= StartIdx)
131  return std::make_pair(nullptr, LR->isUndefIn(Undefs, StartIdx, BeforeUse));
132  if (I->end < Use) {
133  if (LR->isUndefIn(Undefs, I->end, BeforeUse))
134  return std::make_pair(nullptr, true);
135  extendSegmentEndTo(I, Use);
136  }
137  return std::make_pair(I->valno, false);
138  }
139 
140  /// This method is used when we want to extend the segment specified
141  /// by I to end at the specified endpoint. To do this, we should
142  /// merge and eliminate all segments that this will overlap
143  /// with. The iterator is not invalidated.
144  void extendSegmentEndTo(iterator I, SlotIndex NewEnd) {
145  assert(I != segments().end() && "Not a valid segment!");
146  Segment *S = segmentAt(I);
147  VNInfo *ValNo = I->valno;
148 
149  // Search for the first segment that we can't merge with.
150  iterator MergeTo = std::next(I);
151  for (; MergeTo != segments().end() && NewEnd >= MergeTo->end; ++MergeTo)
152  assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
153 
154  // If NewEnd was in the middle of a segment, make sure to get its endpoint.
155  S->end = std::max(NewEnd, std::prev(MergeTo)->end);
156 
157  // If the newly formed segment now touches the segment after it and if they
158  // have the same value number, merge the two segments into one segment.
159  if (MergeTo != segments().end() && MergeTo->start <= I->end &&
160  MergeTo->valno == ValNo) {
161  S->end = MergeTo->end;
162  ++MergeTo;
163  }
164 
165  // Erase any dead segments.
166  segments().erase(std::next(I), MergeTo);
167  }
168 
169  /// This method is used when we want to extend the segment specified
170  /// by I to start at the specified endpoint. To do this, we should
171  /// merge and eliminate all segments that this will overlap with.
172  iterator extendSegmentStartTo(iterator I, SlotIndex NewStart) {
173  assert(I != segments().end() && "Not a valid segment!");
174  Segment *S = segmentAt(I);
175  VNInfo *ValNo = I->valno;
176 
177  // Search for the first segment that we can't merge with.
178  iterator MergeTo = I;
179  do {
180  if (MergeTo == segments().begin()) {
181  S->start = NewStart;
182  segments().erase(MergeTo, I);
183  return I;
184  }
185  assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
186  --MergeTo;
187  } while (NewStart <= MergeTo->start);
188 
189  // If we start in the middle of another segment, just delete a range and
190  // extend that segment.
191  if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
192  segmentAt(MergeTo)->end = S->end;
193  } else {
194  // Otherwise, extend the segment right after.
195  ++MergeTo;
196  Segment *MergeToSeg = segmentAt(MergeTo);
197  MergeToSeg->start = NewStart;
198  MergeToSeg->end = S->end;
199  }
200 
201  segments().erase(std::next(MergeTo), std::next(I));
202  return MergeTo;
203  }
204 
205  iterator addSegment(Segment S) {
206  SlotIndex Start = S.start, End = S.end;
207  iterator I = impl().findInsertPos(S);
208 
209  // If the inserted segment starts in the middle or right at the end of
210  // another segment, just extend that segment to contain the segment of S.
211  if (I != segments().begin()) {
212  iterator B = std::prev(I);
213  if (S.valno == B->valno) {
214  if (B->start <= Start && B->end >= Start) {
215  extendSegmentEndTo(B, End);
216  return B;
217  }
218  } else {
219  // Check to make sure that we are not overlapping two live segments with
220  // different valno's.
221  assert(B->end <= Start &&
222  "Cannot overlap two segments with differing ValID's"
223  " (did you def the same reg twice in a MachineInstr?)");
224  }
225  }
226 
227  // Otherwise, if this segment ends in the middle of, or right next
228  // to, another segment, merge it into that segment.
229  if (I != segments().end()) {
230  if (S.valno == I->valno) {
231  if (I->start <= End) {
232  I = extendSegmentStartTo(I, Start);
233 
234  // If S is a complete superset of a segment, we may need to grow its
235  // endpoint as well.
236  if (End > I->end)
237  extendSegmentEndTo(I, End);
238  return I;
239  }
240  } else {
241  // Check to make sure that we are not overlapping two live segments with
242  // different valno's.
243  assert(I->start >= End &&
244  "Cannot overlap two segments with differing ValID's");
245  }
246  }
247 
248  // Otherwise, this is just a new segment that doesn't interact with
249  // anything.
250  // Insert it.
251  return segments().insert(I, S);
252  }
253 
254 private:
255  ImplT &impl() { return *static_cast<ImplT *>(this); }
256 
257  CollectionT &segments() { return impl().segmentsColl(); }
258 
259  Segment *segmentAt(iterator I) { return const_cast<Segment *>(&(*I)); }
260 };
261 
262 //===----------------------------------------------------------------------===//
263 // Instantiation of the methods for calculation of live ranges
264 // based on a segment vector.
265 //===----------------------------------------------------------------------===//
266 
267 class CalcLiveRangeUtilVector;
268 typedef CalcLiveRangeUtilBase<CalcLiveRangeUtilVector, LiveRange::iterator,
269  LiveRange::Segments> CalcLiveRangeUtilVectorBase;
270 
271 class CalcLiveRangeUtilVector : public CalcLiveRangeUtilVectorBase {
272 public:
273  CalcLiveRangeUtilVector(LiveRange *LR) : CalcLiveRangeUtilVectorBase(LR) {}
274 
275 private:
276  friend CalcLiveRangeUtilVectorBase;
277 
278  LiveRange::Segments &segmentsColl() { return LR->segments; }
279 
280  void insertAtEnd(const Segment &S) { LR->segments.push_back(S); }
281 
282  iterator find(SlotIndex Pos) { return LR->find(Pos); }
283 
284  iterator findInsertPos(Segment S) {
285  return std::upper_bound(LR->begin(), LR->end(), S.start);
286  }
287 };
288 
289 //===----------------------------------------------------------------------===//
290 // Instantiation of the methods for calculation of live ranges
291 // based on a segment set.
292 //===----------------------------------------------------------------------===//
293 
294 class CalcLiveRangeUtilSet;
295 typedef CalcLiveRangeUtilBase<CalcLiveRangeUtilSet,
296  LiveRange::SegmentSet::iterator,
297  LiveRange::SegmentSet> CalcLiveRangeUtilSetBase;
298 
299 class CalcLiveRangeUtilSet : public CalcLiveRangeUtilSetBase {
300 public:
301  CalcLiveRangeUtilSet(LiveRange *LR) : CalcLiveRangeUtilSetBase(LR) {}
302 
303 private:
304  friend CalcLiveRangeUtilSetBase;
305 
306  LiveRange::SegmentSet &segmentsColl() { return *LR->segmentSet; }
307 
308  void insertAtEnd(const Segment &S) {
309  LR->segmentSet->insert(LR->segmentSet->end(), S);
310  }
311 
312  iterator find(SlotIndex Pos) {
313  iterator I =
314  LR->segmentSet->upper_bound(Segment(Pos, Pos.getNextSlot(), nullptr));
315  if (I == LR->segmentSet->begin())
316  return I;
317  iterator PrevI = std::prev(I);
318  if (Pos < (*PrevI).end)
319  return PrevI;
320  return I;
321  }
322 
323  iterator findInsertPos(Segment S) {
324  iterator I = LR->segmentSet->upper_bound(S);
325  if (I != LR->segmentSet->end() && !(S.start < *I))
326  ++I;
327  return I;
328  }
329 };
330 } // namespace
331 
332 //===----------------------------------------------------------------------===//
333 // LiveRange methods
334 //===----------------------------------------------------------------------===//
335 
336 LiveRange::iterator LiveRange::find(SlotIndex Pos) {
337  // This algorithm is basically std::upper_bound.
338  // Unfortunately, std::upper_bound cannot be used with mixed types until we
339  // adopt C++0x. Many libraries can do it, but not all.
340  if (empty() || Pos >= endIndex())
341  return end();
342  iterator I = begin();
343  size_t Len = size();
344  do {
345  size_t Mid = Len >> 1;
346  if (Pos < I[Mid].end) {
347  Len = Mid;
348  } else {
349  I += Mid + 1;
350  Len -= Mid + 1;
351  }
352  } while (Len);
353  return I;
354 }
355 
357  // Use the segment set, if it is available.
358  if (segmentSet != nullptr)
359  return CalcLiveRangeUtilSet(this).createDeadDef(Def, &VNIAlloc, nullptr);
360  // Otherwise use the segment vector.
361  return CalcLiveRangeUtilVector(this).createDeadDef(Def, &VNIAlloc, nullptr);
362 }
363 
365  // Use the segment set, if it is available.
366  if (segmentSet != nullptr)
367  return CalcLiveRangeUtilSet(this).createDeadDef(VNI->def, nullptr, VNI);
368  // Otherwise use the segment vector.
369  return CalcLiveRangeUtilVector(this).createDeadDef(VNI->def, nullptr, VNI);
370 }
371 
372 // overlaps - Return true if the intersection of the two live ranges is
373 // not empty.
374 //
375 // An example for overlaps():
376 //
377 // 0: A = ...
378 // 4: B = ...
379 // 8: C = A + B ;; last use of A
380 //
381 // The live ranges should look like:
382 //
383 // A = [3, 11)
384 // B = [7, x)
385 // C = [11, y)
386 //
387 // A->overlaps(C) should return false since we want to be able to join
388 // A and C.
389 //
391  const_iterator StartPos) const {
392  assert(!empty() && "empty range");
393  const_iterator i = begin();
394  const_iterator ie = end();
395  const_iterator j = StartPos;
396  const_iterator je = other.end();
397 
398  assert((StartPos->start <= i->start || StartPos == other.begin()) &&
399  StartPos != other.end() && "Bogus start position hint!");
400 
401  if (i->start < j->start) {
402  i = std::upper_bound(i, ie, j->start);
403  if (i != begin()) --i;
404  } else if (j->start < i->start) {
405  ++StartPos;
406  if (StartPos != other.end() && StartPos->start <= i->start) {
407  assert(StartPos < other.end() && i < end());
408  j = std::upper_bound(j, je, i->start);
409  if (j != other.begin()) --j;
410  }
411  } else {
412  return true;
413  }
414 
415  if (j == je) return false;
416 
417  while (i != ie) {
418  if (i->start > j->start) {
419  std::swap(i, j);
420  std::swap(ie, je);
421  }
422 
423  if (i->end > j->start)
424  return true;
425  ++i;
426  }
427 
428  return false;
429 }
430 
431 bool LiveRange::overlaps(const LiveRange &Other, const CoalescerPair &CP,
432  const SlotIndexes &Indexes) const {
433  assert(!empty() && "empty range");
434  if (Other.empty())
435  return false;
436 
437  // Use binary searches to find initial positions.
438  const_iterator I = find(Other.beginIndex());
439  const_iterator IE = end();
440  if (I == IE)
441  return false;
442  const_iterator J = Other.find(I->start);
443  const_iterator JE = Other.end();
444  if (J == JE)
445  return false;
446 
447  for (;;) {
448  // J has just been advanced to satisfy:
449  assert(J->end >= I->start);
450  // Check for an overlap.
451  if (J->start < I->end) {
452  // I and J are overlapping. Find the later start.
453  SlotIndex Def = std::max(I->start, J->start);
454  // Allow the overlap if Def is a coalescable copy.
455  if (Def.isBlock() ||
456  !CP.isCoalescable(Indexes.getInstructionFromIndex(Def)))
457  return true;
458  }
459  // Advance the iterator that ends first to check for more overlaps.
460  if (J->end > I->end) {
461  std::swap(I, J);
462  std::swap(IE, JE);
463  }
464  // Advance J until J->end >= I->start.
465  do
466  if (++J == JE)
467  return false;
468  while (J->end < I->start);
469  }
470 }
471 
472 /// overlaps - Return true if the live range overlaps an interval specified
473 /// by [Start, End).
475  assert(Start < End && "Invalid range");
476  const_iterator I = std::lower_bound(begin(), end(), End);
477  return I != begin() && (--I)->end > Start;
478 }
479 
480 bool LiveRange::covers(const LiveRange &Other) const {
481  if (empty())
482  return Other.empty();
483 
484  const_iterator I = begin();
485  for (const Segment &O : Other.segments) {
486  I = advanceTo(I, O.start);
487  if (I == end() || I->start > O.start)
488  return false;
489 
490  // Check adjacent live segments and see if we can get behind O.end.
491  while (I->end < O.end) {
492  const_iterator Last = I;
493  // Get next segment and abort if it was not adjacent.
494  ++I;
495  if (I == end() || Last->end != I->start)
496  return false;
497  }
498  }
499  return true;
500 }
501 
502 /// ValNo is dead, remove it. If it is the largest value number, just nuke it
503 /// (and any other deleted values neighboring it), otherwise mark it as ~1U so
504 /// it can be nuked later.
505 void LiveRange::markValNoForDeletion(VNInfo *ValNo) {
506  if (ValNo->id == getNumValNums()-1) {
507  do {
508  valnos.pop_back();
509  } while (!valnos.empty() && valnos.back()->isUnused());
510  } else {
511  ValNo->markUnused();
512  }
513 }
514 
515 /// RenumberValues - Renumber all values in order of appearance and delete the
516 /// remaining unused values.
519  valnos.clear();
520  for (const Segment &S : segments) {
521  VNInfo *VNI = S.valno;
522  if (!Seen.insert(VNI).second)
523  continue;
524  assert(!VNI->isUnused() && "Unused valno used by live segment");
525  VNI->id = (unsigned)valnos.size();
526  valnos.push_back(VNI);
527  }
528 }
529 
530 void LiveRange::addSegmentToSet(Segment S) {
531  CalcLiveRangeUtilSet(this).addSegment(S);
532 }
533 
534 LiveRange::iterator LiveRange::addSegment(Segment S) {
535  // Use the segment set, if it is available.
536  if (segmentSet != nullptr) {
537  addSegmentToSet(S);
538  return end();
539  }
540  // Otherwise use the segment vector.
541  return CalcLiveRangeUtilVector(this).addSegment(S);
542 }
543 
544 void LiveRange::append(const Segment S) {
545  // Check that the segment belongs to the back of the list.
546  assert(segments.empty() || segments.back().end <= S.start);
547  segments.push_back(S);
548 }
549 
550 std::pair<VNInfo*,bool> LiveRange::extendInBlock(ArrayRef<SlotIndex> Undefs,
551  SlotIndex StartIdx, SlotIndex Kill) {
552  // Use the segment set, if it is available.
553  if (segmentSet != nullptr)
554  return CalcLiveRangeUtilSet(this).extendInBlock(Undefs, StartIdx, Kill);
555  // Otherwise use the segment vector.
556  return CalcLiveRangeUtilVector(this).extendInBlock(Undefs, StartIdx, Kill);
557 }
558 
560  // Use the segment set, if it is available.
561  if (segmentSet != nullptr)
562  return CalcLiveRangeUtilSet(this).extendInBlock(StartIdx, Kill);
563  // Otherwise use the segment vector.
564  return CalcLiveRangeUtilVector(this).extendInBlock(StartIdx, Kill);
565 }
566 
567 /// Remove the specified segment from this range. Note that the segment must
568 /// be in a single Segment in its entirety.
570  bool RemoveDeadValNo) {
571  // Find the Segment containing this span.
572  iterator I = find(Start);
573  assert(I != end() && "Segment is not in range!");
574  assert(I->containsInterval(Start, End)
575  && "Segment is not entirely in range!");
576 
577  // If the span we are removing is at the start of the Segment, adjust it.
578  VNInfo *ValNo = I->valno;
579  if (I->start == Start) {
580  if (I->end == End) {
581  if (RemoveDeadValNo) {
582  // Check if val# is dead.
583  bool isDead = true;
584  for (const_iterator II = begin(), EE = end(); II != EE; ++II)
585  if (II != I && II->valno == ValNo) {
586  isDead = false;
587  break;
588  }
589  if (isDead) {
590  // Now that ValNo is dead, remove it.
591  markValNoForDeletion(ValNo);
592  }
593  }
594 
595  segments.erase(I); // Removed the whole Segment.
596  } else
597  I->start = End;
598  return;
599  }
600 
601  // Otherwise if the span we are removing is at the end of the Segment,
602  // adjust the other way.
603  if (I->end == End) {
604  I->end = Start;
605  return;
606  }
607 
608  // Otherwise, we are splitting the Segment into two pieces.
609  SlotIndex OldEnd = I->end;
610  I->end = Start; // Trim the old segment.
611 
612  // Insert the new one.
613  segments.insert(std::next(I), Segment(End, OldEnd, ValNo));
614 }
615 
616 /// removeValNo - Remove all the segments defined by the specified value#.
617 /// Also remove the value# from value# list.
619  if (empty()) return;
620  segments.erase(remove_if(*this, [ValNo](const Segment &S) {
621  return S.valno == ValNo;
622  }), end());
623  // Now that ValNo is dead, remove it.
624  markValNoForDeletion(ValNo);
625 }
626 
628  const int *LHSValNoAssignments,
629  const int *RHSValNoAssignments,
630  SmallVectorImpl<VNInfo *> &NewVNInfo) {
631  verify();
632 
633  // Determine if any of our values are mapped. This is uncommon, so we want
634  // to avoid the range scan if not.
635  bool MustMapCurValNos = false;
636  unsigned NumVals = getNumValNums();
637  unsigned NumNewVals = NewVNInfo.size();
638  for (unsigned i = 0; i != NumVals; ++i) {
639  unsigned LHSValID = LHSValNoAssignments[i];
640  if (i != LHSValID ||
641  (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i))) {
642  MustMapCurValNos = true;
643  break;
644  }
645  }
646 
647  // If we have to apply a mapping to our base range assignment, rewrite it now.
648  if (MustMapCurValNos && !empty()) {
649  // Map the first live range.
650 
651  iterator OutIt = begin();
652  OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
653  for (iterator I = std::next(OutIt), E = end(); I != E; ++I) {
654  VNInfo* nextValNo = NewVNInfo[LHSValNoAssignments[I->valno->id]];
655  assert(nextValNo && "Huh?");
656 
657  // If this live range has the same value # as its immediate predecessor,
658  // and if they are neighbors, remove one Segment. This happens when we
659  // have [0,4:0)[4,7:1) and map 0/1 onto the same value #.
660  if (OutIt->valno == nextValNo && OutIt->end == I->start) {
661  OutIt->end = I->end;
662  } else {
663  // Didn't merge. Move OutIt to the next segment,
664  ++OutIt;
665  OutIt->valno = nextValNo;
666  if (OutIt != I) {
667  OutIt->start = I->start;
668  OutIt->end = I->end;
669  }
670  }
671  }
672  // If we merge some segments, chop off the end.
673  ++OutIt;
674  segments.erase(OutIt, end());
675  }
676 
677  // Rewrite Other values before changing the VNInfo ids.
678  // This can leave Other in an invalid state because we're not coalescing
679  // touching segments that now have identical values. That's OK since Other is
680  // not supposed to be valid after calling join();
681  for (Segment &S : Other.segments)
682  S.valno = NewVNInfo[RHSValNoAssignments[S.valno->id]];
683 
684  // Update val# info. Renumber them and make sure they all belong to this
685  // LiveRange now. Also remove dead val#'s.
686  unsigned NumValNos = 0;
687  for (unsigned i = 0; i < NumNewVals; ++i) {
688  VNInfo *VNI = NewVNInfo[i];
689  if (VNI) {
690  if (NumValNos >= NumVals)
691  valnos.push_back(VNI);
692  else
693  valnos[NumValNos] = VNI;
694  VNI->id = NumValNos++; // Renumber val#.
695  }
696  }
697  if (NumNewVals < NumVals)
698  valnos.resize(NumNewVals); // shrinkify
699 
700  // Okay, now insert the RHS live segments into the LHS.
701  LiveRangeUpdater Updater(this);
702  for (Segment &S : Other.segments)
703  Updater.add(S);
704 }
705 
706 /// Merge all of the segments in RHS into this live range as the specified
707 /// value number. The segments in RHS are allowed to overlap with segments in
708 /// the current range, but only if the overlapping segments have the
709 /// specified value number.
711  VNInfo *LHSValNo) {
712  LiveRangeUpdater Updater(this);
713  for (const Segment &S : RHS.segments)
714  Updater.add(S.start, S.end, LHSValNo);
715 }
716 
717 /// MergeValueInAsValue - Merge all of the live segments of a specific val#
718 /// in RHS into this live range as the specified value number.
719 /// The segments in RHS are allowed to overlap with segments in the
720 /// current range, it will replace the value numbers of the overlaped
721 /// segments with the specified value number.
723  const VNInfo *RHSValNo,
724  VNInfo *LHSValNo) {
725  LiveRangeUpdater Updater(this);
726  for (const Segment &S : RHS.segments)
727  if (S.valno == RHSValNo)
728  Updater.add(S.start, S.end, LHSValNo);
729 }
730 
731 /// MergeValueNumberInto - This method is called when two value nubmers
732 /// are found to be equivalent. This eliminates V1, replacing all
733 /// segments with the V1 value number with the V2 value number. This can
734 /// cause merging of V1/V2 values numbers and compaction of the value space.
736  assert(V1 != V2 && "Identical value#'s are always equivalent!");
737 
738  // This code actually merges the (numerically) larger value number into the
739  // smaller value number, which is likely to allow us to compactify the value
740  // space. The only thing we have to be careful of is to preserve the
741  // instruction that defines the result value.
742 
743  // Make sure V2 is smaller than V1.
744  if (V1->id < V2->id) {
745  V1->copyFrom(*V2);
746  std::swap(V1, V2);
747  }
748 
749  // Merge V1 segments into V2.
750  for (iterator I = begin(); I != end(); ) {
751  iterator S = I++;
752  if (S->valno != V1) continue; // Not a V1 Segment.
753 
754  // Okay, we found a V1 live range. If it had a previous, touching, V2 live
755  // range, extend it.
756  if (S != begin()) {
757  iterator Prev = S-1;
758  if (Prev->valno == V2 && Prev->end == S->start) {
759  Prev->end = S->end;
760 
761  // Erase this live-range.
762  segments.erase(S);
763  I = Prev+1;
764  S = Prev;
765  }
766  }
767 
768  // Okay, now we have a V1 or V2 live range that is maximally merged forward.
769  // Ensure that it is a V2 live-range.
770  S->valno = V2;
771 
772  // If we can merge it into later V2 segments, do so now. We ignore any
773  // following V1 segments, as they will be merged in subsequent iterations
774  // of the loop.
775  if (I != end()) {
776  if (I->start == S->end && I->valno == V2) {
777  S->end = I->end;
778  segments.erase(I);
779  I = S+1;
780  }
781  }
782  }
783 
784  // Now that V1 is dead, remove it.
785  markValNoForDeletion(V1);
786 
787  return V2;
788 }
789 
791  assert(segmentSet != nullptr && "segment set must have been created");
792  assert(
793  segments.empty() &&
794  "segment set can be used only initially before switching to the array");
795  segments.append(segmentSet->begin(), segmentSet->end());
796  segmentSet = nullptr;
797  verify();
798 }
799 
801  ArrayRef<SlotIndex>::iterator SlotI = Slots.begin();
802  ArrayRef<SlotIndex>::iterator SlotE = Slots.end();
803 
804  // If there are no regmask slots, we have nothing to search.
805  if (SlotI == SlotE)
806  return false;
807 
808  // Start our search at the first segment that ends after the first slot.
809  const_iterator SegmentI = find(*SlotI);
810  const_iterator SegmentE = end();
811 
812  // If there are no segments that end after the first slot, we're done.
813  if (SegmentI == SegmentE)
814  return false;
815 
816  // Look for each slot in the live range.
817  for ( ; SlotI != SlotE; ++SlotI) {
818  // Go to the next segment that ends after the current slot.
819  // The slot may be within a hole in the range.
820  SegmentI = advanceTo(SegmentI, *SlotI);
821  if (SegmentI == SegmentE)
822  return false;
823 
824  // If this segment contains the slot, we're done.
825  if (SegmentI->contains(*SlotI))
826  return true;
827  // Otherwise, look for the next slot.
828  }
829 
830  // We didn't find a segment containing any of the slots.
831  return false;
832 }
833 
834 void LiveInterval::freeSubRange(SubRange *S) {
835  S->~SubRange();
836  // Memory was allocated with BumpPtr allocator and is not freed here.
837 }
838 
840  SubRange **NextPtr = &SubRanges;
841  SubRange *I = *NextPtr;
842  while (I != nullptr) {
843  if (!I->empty()) {
844  NextPtr = &I->Next;
845  I = *NextPtr;
846  continue;
847  }
848  // Skip empty subranges until we find the first nonempty one.
849  do {
850  SubRange *Next = I->Next;
851  freeSubRange(I);
852  I = Next;
853  } while (I != nullptr && I->empty());
854  *NextPtr = I;
855  }
856 }
857 
859  for (SubRange *I = SubRanges, *Next; I != nullptr; I = Next) {
860  Next = I->Next;
861  freeSubRange(I);
862  }
863  SubRanges = nullptr;
864 }
865 
867  LaneBitmask LaneMask, std::function<void(LiveInterval::SubRange&)> Apply) {
868 
869  LaneBitmask ToApply = LaneMask;
870  for (SubRange &SR : subranges()) {
871  LaneBitmask SRMask = SR.LaneMask;
872  LaneBitmask Matching = SRMask & LaneMask;
873  if (Matching.none())
874  continue;
875 
876  SubRange *MatchingRange;
877  if (SRMask == Matching) {
878  // The subrange fits (it does not cover bits outside \p LaneMask).
879  MatchingRange = &SR;
880  } else {
881  // We have to split the subrange into a matching and non-matching part.
882  // Reduce lanemask of existing lane to non-matching part.
883  SR.LaneMask = SRMask & ~Matching;
884  // Create a new subrange for the matching part
885  MatchingRange = createSubRangeFrom(Allocator, Matching, SR);
886  }
887  Apply(*MatchingRange);
888  ToApply &= ~Matching;
889  }
890  // Create a new subrange if there are uncovered bits left.
891  if (ToApply.any()) {
892  SubRange *NewRange = createSubRange(Allocator, ToApply);
893  Apply(*NewRange);
894  }
895 }
896 
897 unsigned LiveInterval::getSize() const {
898  unsigned Sum = 0;
899  for (const Segment &S : segments)
900  Sum += S.start.distance(S.end);
901  return Sum;
902 }
903 
905  LaneBitmask LaneMask,
906  const MachineRegisterInfo &MRI,
907  const SlotIndexes &Indexes) const {
909  LaneBitmask VRegMask = MRI.getMaxLaneMaskForVReg(reg);
910  assert((VRegMask & LaneMask).any());
911  const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
912  for (const MachineOperand &MO : MRI.def_operands(reg)) {
913  if (!MO.isUndef())
914  continue;
915  unsigned SubReg = MO.getSubReg();
916  assert(SubReg != 0 && "Undef should only be set on subreg defs");
917  LaneBitmask DefMask = TRI.getSubRegIndexLaneMask(SubReg);
918  LaneBitmask UndefMask = VRegMask & ~DefMask;
919  if ((UndefMask & LaneMask).any()) {
920  const MachineInstr &MI = *MO.getParent();
921  bool EarlyClobber = MO.isEarlyClobber();
922  SlotIndex Pos = Indexes.getInstructionIndex(MI).getRegSlot(EarlyClobber);
923  Undefs.push_back(Pos);
924  }
925  }
926 }
927 
929  return os << '[' << S.start << ',' << S.end << ':' << S.valno->id << ')';
930 }
931 
932 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
934  dbgs() << *this << '\n';
935 }
936 #endif
937 
938 void LiveRange::print(raw_ostream &OS) const {
939  if (empty())
940  OS << "EMPTY";
941  else {
942  for (const Segment &S : segments) {
943  OS << S;
944  assert(S.valno == getValNumInfo(S.valno->id) && "Bad VNInfo");
945  }
946  }
947 
948  // Print value number info.
949  if (getNumValNums()) {
950  OS << " ";
951  unsigned vnum = 0;
952  for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
953  ++i, ++vnum) {
954  const VNInfo *vni = *i;
955  if (vnum) OS << ' ';
956  OS << vnum << '@';
957  if (vni->isUnused()) {
958  OS << 'x';
959  } else {
960  OS << vni->def;
961  if (vni->isPHIDef())
962  OS << "-phi";
963  }
964  }
965  }
966 }
967 
969  OS << " L" << PrintLaneMask(LaneMask) << ' '
970  << static_cast<const LiveRange&>(*this);
971 }
972 
974  OS << PrintReg(reg) << ' ';
975  super::print(OS);
976  // Print subranges
977  for (const SubRange &SR : subranges())
978  OS << SR;
979 }
980 
981 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
983  dbgs() << *this << '\n';
984 }
985 
987  dbgs() << *this << '\n';
988 }
989 
991  dbgs() << *this << '\n';
992 }
993 #endif
994 
995 #ifndef NDEBUG
996 void LiveRange::verify() const {
997  for (const_iterator I = begin(), E = end(); I != E; ++I) {
998  assert(I->start.isValid());
999  assert(I->end.isValid());
1000  assert(I->start < I->end);
1001  assert(I->valno != nullptr);
1002  assert(I->valno->id < valnos.size());
1003  assert(I->valno == valnos[I->valno->id]);
1004  if (std::next(I) != E) {
1005  assert(I->end <= std::next(I)->start);
1006  if (I->end == std::next(I)->start)
1007  assert(I->valno != std::next(I)->valno);
1008  }
1009  }
1010 }
1011 
1013  super::verify();
1014 
1015  // Make sure SubRanges are fine and LaneMasks are disjunct.
1016  LaneBitmask Mask;
1017  LaneBitmask MaxMask = MRI != nullptr ? MRI->getMaxLaneMaskForVReg(reg)
1018  : LaneBitmask::getAll();
1019  for (const SubRange &SR : subranges()) {
1020  // Subrange lanemask should be disjunct to any previous subrange masks.
1021  assert((Mask & SR.LaneMask).none());
1022  Mask |= SR.LaneMask;
1023 
1024  // subrange mask should not contained in maximum lane mask for the vreg.
1025  assert((Mask & ~MaxMask).none());
1026  // empty subranges must be removed.
1027  assert(!SR.empty());
1028 
1029  SR.verify();
1030  // Main liverange should cover subrange.
1031  assert(covers(SR));
1032  }
1033 }
1034 #endif
1035 
1036 
1037 //===----------------------------------------------------------------------===//
1038 // LiveRangeUpdater class
1039 //===----------------------------------------------------------------------===//
1040 //
1041 // The LiveRangeUpdater class always maintains these invariants:
1042 //
1043 // - When LastStart is invalid, Spills is empty and the iterators are invalid.
1044 // This is the initial state, and the state created by flush().
1045 // In this state, isDirty() returns false.
1046 //
1047 // Otherwise, segments are kept in three separate areas:
1048 //
1049 // 1. [begin; WriteI) at the front of LR.
1050 // 2. [ReadI; end) at the back of LR.
1051 // 3. Spills.
1052 //
1053 // - LR.begin() <= WriteI <= ReadI <= LR.end().
1054 // - Segments in all three areas are fully ordered and coalesced.
1055 // - Segments in area 1 precede and can't coalesce with segments in area 2.
1056 // - Segments in Spills precede and can't coalesce with segments in area 2.
1057 // - No coalescing is possible between segments in Spills and segments in area
1058 // 1, and there are no overlapping segments.
1059 //
1060 // The segments in Spills are not ordered with respect to the segments in area
1061 // 1. They need to be merged.
1062 //
1063 // When they exist, Spills.back().start <= LastStart,
1064 // and WriteI[-1].start <= LastStart.
1065 
1066 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
1068  if (!isDirty()) {
1069  if (LR)
1070  OS << "Clean updater: " << *LR << '\n';
1071  else
1072  OS << "Null updater.\n";
1073  return;
1074  }
1075  assert(LR && "Can't have null LR in dirty updater.");
1076  OS << " updater with gap = " << (ReadI - WriteI)
1077  << ", last start = " << LastStart
1078  << ":\n Area 1:";
1079  for (const auto &S : make_range(LR->begin(), WriteI))
1080  OS << ' ' << S;
1081  OS << "\n Spills:";
1082  for (unsigned I = 0, E = Spills.size(); I != E; ++I)
1083  OS << ' ' << Spills[I];
1084  OS << "\n Area 2:";
1085  for (const auto &S : make_range(ReadI, LR->end()))
1086  OS << ' ' << S;
1087  OS << '\n';
1088 }
1089 
1091  print(errs());
1092 }
1093 #endif
1094 
1095 // Determine if A and B should be coalesced.
1096 static inline bool coalescable(const LiveRange::Segment &A,
1097  const LiveRange::Segment &B) {
1098  assert(A.start <= B.start && "Unordered live segments.");
1099  if (A.end == B.start)
1100  return A.valno == B.valno;
1101  if (A.end < B.start)
1102  return false;
1103  assert(A.valno == B.valno && "Cannot overlap different values");
1104  return true;
1105 }
1106 
1108  assert(LR && "Cannot add to a null destination");
1109 
1110  // Fall back to the regular add method if the live range
1111  // is using the segment set instead of the segment vector.
1112  if (LR->segmentSet != nullptr) {
1113  LR->addSegmentToSet(Seg);
1114  return;
1115  }
1116 
1117  // Flush the state if Start moves backwards.
1118  if (!LastStart.isValid() || LastStart > Seg.start) {
1119  if (isDirty())
1120  flush();
1121  // This brings us to an uninitialized state. Reinitialize.
1122  assert(Spills.empty() && "Leftover spilled segments");
1123  WriteI = ReadI = LR->begin();
1124  }
1125 
1126  // Remember start for next time.
1127  LastStart = Seg.start;
1128 
1129  // Advance ReadI until it ends after Seg.start.
1130  LiveRange::iterator E = LR->end();
1131  if (ReadI != E && ReadI->end <= Seg.start) {
1132  // First try to close the gap between WriteI and ReadI with spills.
1133  if (ReadI != WriteI)
1134  mergeSpills();
1135  // Then advance ReadI.
1136  if (ReadI == WriteI)
1137  ReadI = WriteI = LR->find(Seg.start);
1138  else
1139  while (ReadI != E && ReadI->end <= Seg.start)
1140  *WriteI++ = *ReadI++;
1141  }
1142 
1143  assert(ReadI == E || ReadI->end > Seg.start);
1144 
1145  // Check if the ReadI segment begins early.
1146  if (ReadI != E && ReadI->start <= Seg.start) {
1147  assert(ReadI->valno == Seg.valno && "Cannot overlap different values");
1148  // Bail if Seg is completely contained in ReadI.
1149  if (ReadI->end >= Seg.end)
1150  return;
1151  // Coalesce into Seg.
1152  Seg.start = ReadI->start;
1153  ++ReadI;
1154  }
1155 
1156  // Coalesce as much as possible from ReadI into Seg.
1157  while (ReadI != E && coalescable(Seg, *ReadI)) {
1158  Seg.end = std::max(Seg.end, ReadI->end);
1159  ++ReadI;
1160  }
1161 
1162  // Try coalescing Spills.back() into Seg.
1163  if (!Spills.empty() && coalescable(Spills.back(), Seg)) {
1164  Seg.start = Spills.back().start;
1165  Seg.end = std::max(Spills.back().end, Seg.end);
1166  Spills.pop_back();
1167  }
1168 
1169  // Try coalescing Seg into WriteI[-1].
1170  if (WriteI != LR->begin() && coalescable(WriteI[-1], Seg)) {
1171  WriteI[-1].end = std::max(WriteI[-1].end, Seg.end);
1172  return;
1173  }
1174 
1175  // Seg doesn't coalesce with anything, and needs to be inserted somewhere.
1176  if (WriteI != ReadI) {
1177  *WriteI++ = Seg;
1178  return;
1179  }
1180 
1181  // Finally, append to LR or Spills.
1182  if (WriteI == E) {
1183  LR->segments.push_back(Seg);
1184  WriteI = ReadI = LR->end();
1185  } else
1186  Spills.push_back(Seg);
1187 }
1188 
1189 // Merge as many spilled segments as possible into the gap between WriteI
1190 // and ReadI. Advance WriteI to reflect the inserted instructions.
1191 void LiveRangeUpdater::mergeSpills() {
1192  // Perform a backwards merge of Spills and [SpillI;WriteI).
1193  size_t GapSize = ReadI - WriteI;
1194  size_t NumMoved = std::min(Spills.size(), GapSize);
1195  LiveRange::iterator Src = WriteI;
1196  LiveRange::iterator Dst = Src + NumMoved;
1197  LiveRange::iterator SpillSrc = Spills.end();
1198  LiveRange::iterator B = LR->begin();
1199 
1200  // This is the new WriteI position after merging spills.
1201  WriteI = Dst;
1202 
1203  // Now merge Src and Spills backwards.
1204  while (Src != Dst) {
1205  if (Src != B && Src[-1].start > SpillSrc[-1].start)
1206  *--Dst = *--Src;
1207  else
1208  *--Dst = *--SpillSrc;
1209  }
1210  assert(NumMoved == size_t(Spills.end() - SpillSrc));
1211  Spills.erase(SpillSrc, Spills.end());
1212 }
1213 
1215  if (!isDirty())
1216  return;
1217  // Clear the dirty state.
1218  LastStart = SlotIndex();
1219 
1220  assert(LR && "Cannot add to a null destination");
1221 
1222  // Nothing to merge?
1223  if (Spills.empty()) {
1224  LR->segments.erase(WriteI, ReadI);
1225  LR->verify();
1226  return;
1227  }
1228 
1229  // Resize the WriteI - ReadI gap to match Spills.
1230  size_t GapSize = ReadI - WriteI;
1231  if (GapSize < Spills.size()) {
1232  // The gap is too small. Make some room.
1233  size_t WritePos = WriteI - LR->begin();
1234  LR->segments.insert(ReadI, Spills.size() - GapSize, LiveRange::Segment());
1235  // This also invalidated ReadI, but it is recomputed below.
1236  WriteI = LR->begin() + WritePos;
1237  } else {
1238  // Shrink the gap if necessary.
1239  LR->segments.erase(WriteI + Spills.size(), ReadI);
1240  }
1241  ReadI = WriteI + Spills.size();
1242  mergeSpills();
1243  LR->verify();
1244 }
1245 
1247  // Create initial equivalence classes.
1248  EqClass.clear();
1249  EqClass.grow(LR.getNumValNums());
1250 
1251  const VNInfo *used = nullptr, *unused = nullptr;
1252 
1253  // Determine connections.
1254  for (const VNInfo *VNI : LR.valnos) {
1255  // Group all unused values into one class.
1256  if (VNI->isUnused()) {
1257  if (unused)
1258  EqClass.join(unused->id, VNI->id);
1259  unused = VNI;
1260  continue;
1261  }
1262  used = VNI;
1263  if (VNI->isPHIDef()) {
1264  const MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
1265  assert(MBB && "Phi-def has no defining MBB");
1266  // Connect to values live out of predecessors.
1268  PE = MBB->pred_end(); PI != PE; ++PI)
1269  if (const VNInfo *PVNI = LR.getVNInfoBefore(LIS.getMBBEndIdx(*PI)))
1270  EqClass.join(VNI->id, PVNI->id);
1271  } else {
1272  // Normal value defined by an instruction. Check for two-addr redef.
1273  // FIXME: This could be coincidental. Should we really check for a tied
1274  // operand constraint?
1275  // Note that VNI->def may be a use slot for an early clobber def.
1276  if (const VNInfo *UVNI = LR.getVNInfoBefore(VNI->def))
1277  EqClass.join(VNI->id, UVNI->id);
1278  }
1279  }
1280 
1281  // Lump all the unused values in with the last used value.
1282  if (used && unused)
1283  EqClass.join(used->id, unused->id);
1284 
1285  EqClass.compress();
1286  return EqClass.getNumClasses();
1287 }
1288 
1291  // Rewrite instructions.
1293  RE = MRI.reg_end(); RI != RE;) {
1294  MachineOperand &MO = *RI;
1295  MachineInstr *MI = RI->getParent();
1296  ++RI;
1297  // DBG_VALUE instructions don't have slot indexes, so get the index of the
1298  // instruction before them.
1299  // Normally, DBG_VALUE instructions are removed before this function is
1300  // called, but it is not a requirement.
1301  SlotIndex Idx;
1302  if (MI->isDebugValue())
1303  Idx = LIS.getSlotIndexes()->getIndexBefore(*MI);
1304  else
1305  Idx = LIS.getInstructionIndex(*MI);
1306  LiveQueryResult LRQ = LI.Query(Idx);
1307  const VNInfo *VNI = MO.readsReg() ? LRQ.valueIn() : LRQ.valueDefined();
1308  // In the case of an <undef> use that isn't tied to any def, VNI will be
1309  // NULL. If the use is tied to a def, VNI will be the defined value.
1310  if (!VNI)
1311  continue;
1312  if (unsigned EqClass = getEqClass(VNI))
1313  MO.setReg(LIV[EqClass-1]->reg);
1314  }
1315 
1316  // Distribute subregister liveranges.
1317  if (LI.hasSubRanges()) {
1318  unsigned NumComponents = EqClass.getNumClasses();
1319  SmallVector<unsigned, 8> VNIMapping;
1321  BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
1322  for (LiveInterval::SubRange &SR : LI.subranges()) {
1323  // Create new subranges in the split intervals and construct a mapping
1324  // for the VNInfos in the subrange.
1325  unsigned NumValNos = SR.valnos.size();
1326  VNIMapping.clear();
1327  VNIMapping.reserve(NumValNos);
1328  SubRanges.clear();
1329  SubRanges.resize(NumComponents-1, nullptr);
1330  for (unsigned I = 0; I < NumValNos; ++I) {
1331  const VNInfo &VNI = *SR.valnos[I];
1332  unsigned ComponentNum;
1333  if (VNI.isUnused()) {
1334  ComponentNum = 0;
1335  } else {
1336  const VNInfo *MainRangeVNI = LI.getVNInfoAt(VNI.def);
1337  assert(MainRangeVNI != nullptr
1338  && "SubRange def must have corresponding main range def");
1339  ComponentNum = getEqClass(MainRangeVNI);
1340  if (ComponentNum > 0 && SubRanges[ComponentNum-1] == nullptr) {
1341  SubRanges[ComponentNum-1]
1342  = LIV[ComponentNum-1]->createSubRange(Allocator, SR.LaneMask);
1343  }
1344  }
1345  VNIMapping.push_back(ComponentNum);
1346  }
1347  DistributeRange(SR, SubRanges.data(), VNIMapping);
1348  }
1349  LI.removeEmptySubRanges();
1350  }
1351 
1352  // Distribute main liverange.
1353  DistributeRange(LI, LIV, EqClass);
1354 }
void add(LiveRange::Segment)
Add a segment to LR and coalesce when possible, just like LR.addSegment().
std::set< Segment > SegmentSet
Definition: LiveInterval.h:205
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_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:243
void flush()
Flush the updater state to LR so it is valid and contains all added segments.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
SlotIndex getInstructionIndex(const MachineInstr &MI) const
Returns the base index for the given instruction.
Definition: SlotIndexes.h:414
const unsigned reg
Definition: LiveInterval.h:667
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:234
void refineSubRanges(BumpPtrAllocator &Allocator, LaneBitmask LaneMask, std::function< void(LiveInterval::SubRange &)> Mod)
Refines the subranges to support LaneMask.
SlotIndex def
The index of the defining instruction.
Definition: LiveInterval.h:61
static bool coalescable(const LiveRange::Segment &A, const LiveRange::Segment &B)
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:449
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...
auto remove_if(R &&Range, UnaryPredicate P) -> decltype(std::begin(Range))
Provide wrappers to std::remove_if which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:858
Maximum length of the test input libFuzzer tries to guess a good value based on the corpus and reports it always prefer smaller inputs during the corpus shuffle When libFuzzer itself reports a bug this exit code will be used If indicates the maximal total time in seconds to run the fuzzer minimizes the provided crash input Use with tries to cleanse the provided crash input to make it contain fewer original bytes Use with exact_artifact_path to specify the output Experimental Use value profile to guide fuzzing Number of jobs to run If min(jobs, NumberOfCpuCores()/2)\" is used.") FUZZER_FLAG_INT(reload
Segments::iterator iterator
Definition: LiveInterval.h:208
static LaneBitmask getAll()
Definition: LaneBitmask.h:77
vni_iterator vni_begin()
Definition: LiveInterval.h:220
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:638
iterator end() const
Definition: ArrayRef.h:138
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
iterator advanceTo(iterator I, SlotIndex Pos)
advanceTo - Advance the specified iterator to point to the Segment containing the specified position...
Definition: LiveInterval.h:259
bool isLiveAtIndexes(ArrayRef< SlotIndex > Slots) const
size_t size() const
Definition: LiveInterval.h:293
A live range for subregisters.
Definition: LiveInterval.h:645
This represents a simple continuous liveness interval for a value.
Definition: LiveInterval.h:162
void markUnused()
Mark this value as unused.
Definition: LiveInterval.h:84
std::vector< MachineBasicBlock * >::const_iterator const_pred_iterator
LaneBitmask getSubRegIndexLaneMask(unsigned SubIdx) const
Return a bitmask representing the parts of a register that are covered by SubIdx. ...
void reserve(size_type N)
Definition: SmallVector.h:377
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:742
VNInfo - Value Number Information.
Definition: LiveInterval.h:53
unsigned getNumValNums() const
Definition: LiveInterval.h:301
void flushSegmentSet()
Flush segment set into the regular segment vector.
bool empty() const
Definition: LiveInterval.h:370
VNInfo * getVNInfoAt(SlotIndex Idx) const
getVNInfoAt - Return the VNInfo that is live at Idx, or NULL.
Definition: LiveInterval.h:409
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:157
static bool isEarlierInstr(SlotIndex A, SlotIndex B)
isEarlierInstr - Return true if A refers to an instruction earlier than B.
Definition: SlotIndexes.h:204
void removeEmptySubRanges()
Removes all subranges without any segments (subranges without segments are not considered valid and s...
const TargetRegisterInfo * getTargetRegisterInfo() const
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
iterator end()
Definition: LiveInterval.h:212
A helper class for register coalescers.
iterator_range< subrange_iterator > subranges()
Definition: LiveInterval.h:723
unsigned SubReg
Result of a LiveRange query.
Definition: LiveInterval.h:90
bool isBlock() const
isBlock - Returns true if this is a block boundary slot.
Definition: SlotIndexes.h:227
unsigned getSize() const
getSize - Returns the sum of sizes of all the LiveRange's.
SlotIndex getDeadSlot() const
Returns the dead def kill slot for the current instruction.
Definition: SlotIndexes.h:260
constexpr bool any() const
Definition: LaneBitmask.h:53
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:81
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 ...
place backedge safepoints impl
bool isDead() const
isDead - Returns true if this is a dead def kill slot.
Definition: SlotIndexes.h:237
SlotIndexes pass.
Definition: SlotIndexes.h:331
iterator addSegment(Segment S)
Add the specified Segment to this range, merging segments as appropriate.
Segments segments
Definition: LiveInterval.h:199
VNInfo * MergeValueNumberInto(VNInfo *V1, VNInfo *V2)
MergeValueNumberInto - This method is called when two value numbers are found to be equivalent...
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
Printable PrintReg(unsigned Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubRegIdx=0)
Prints virtual and physical registers with or without a TRI instance.
static void DistributeRange(LiveRangeT &LR, LiveRangeT *SplitLRs[], EqClassesT VNIClasses)
Helper function that distributes live range value numbers and the corresponding segments of a master ...
void copyFrom(VNInfo &src)
Copy from the parameter into this VNInfo.
Definition: LiveInterval.h:70
SlotIndex getPrevSlot() const
Returns the previous slot in the index list.
Definition: SlotIndexes.h:290
iterator_range< def_iterator > def_operands(unsigned Reg) const
bool isDebugValue() const
Definition: MachineInstr.h:785
void removeValNo(VNInfo *ValNo)
removeValNo - Remove all the segments defined by the specified value#.
friend const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:243
LiveQueryResult Query(SlotIndex Idx) const
Query Liveness at Idx.
Definition: LiveInterval.h:529
SubRange * createSubRange(BumpPtrAllocator &Allocator, LaneBitmask LaneMask)
Creates a new empty subregister live range.
Definition: LiveInterval.h:733
unsigned const MachineRegisterInfo * MRI
VNInfoList::const_iterator const_vni_iterator
Definition: LiveInterval.h:218
constexpr bool none() const
Definition: LaneBitmask.h:52
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.
unsigned Classify(const LiveRange &LR)
Classify the values in LR into connected components.
#define A
Definition: LargeTest.cpp:12
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:372
bool overlaps(const LiveRange &other) const
overlaps - Return true if the intersection of the two live ranges is not empty.
Definition: LiveInterval.h:436
void dump() const
static const unsigned End
iterator begin() const
Definition: ArrayRef.h:137
VNInfo * valueDefined() const
Return the value defined by this instruction, if any.
Definition: LiveInterval.h:135
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.
iterator erase(const_iterator CI)
Definition: SmallVector.h:446
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
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:78
MachineInstr * getInstructionFromIndex(SlotIndex index) const
Returns the instruction for the given index, or null if the given index has no instruction associated...
Definition: SlotIndexes.h:424
iterator find(SlotIndex Pos)
find - Return an iterator pointing to the first segment that ends after Pos, or end().
unsigned id
The ID number of this value.
Definition: LiveInterval.h:58
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.
Basic Register Allocator
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:423
#define E
Definition: LargeTest.cpp:27
auto find(R &&Range, const T &Val) -> decltype(std::begin(Range))
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:839
#define B
Definition: LargeTest.cpp:24
static bool isSameInstr(SlotIndex A, SlotIndex B)
isSameInstr - Return true if A and B refer to the same instruction.
Definition: SlotIndexes.h:198
Segments::const_iterator const_iterator
Definition: LiveInterval.h:209
MachineOperand class - Representation of each machine instruction operand.
std::unique_ptr< SegmentSet > segmentSet
Definition: LiveInterval.h:206
bool isCoalescable(const MachineInstr *) const
Return true if MI is a copy instruction that will become an identity copy after coalescing.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void clearSubRanges()
Removes all subregister liveness information.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:923
void print(raw_ostream &OS) const
VNInfoList valnos
Definition: LiveInterval.h:200
iterator insert(iterator I, T &&Elt)
Definition: SmallVector.h:479
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:395
VNInfo * getValNumInfo(unsigned ValNo)
getValNumInfo - Returns pointer to the specified val#.
Definition: LiveInterval.h:305
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:373
Representation of each machine instruction.
Definition: MachineInstr.h:59
void print(raw_ostream &OS) const
void print(raw_ostream &) const
LaneBitmask getMaxLaneMaskForVReg(unsigned Reg) const
Returns a mask covering all bits that can appear in lane masks of subregisters of the virtual registe...
pointer data()
Return a pointer to the vector's buffer, even if empty().
Definition: SmallVector.h:142
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:380
void setReg(unsigned Reg)
Change the register this operand corresponds to.
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
static void createDeadDef(SlotIndexes &Indexes, VNInfo::Allocator &Alloc, LiveRange &LR, const MachineOperand &MO)
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
Definition: APInt.h:2018
SlotIndex getRegSlot(bool EC=false) const
Returns the register use/def slot in the current instruction for a normal or early-clobber def...
Definition: SlotIndexes.h:255
iterator begin()
Definition: LiveInterval.h:211
Helper class for performant LiveRange bulk updates.
Definition: LiveInterval.h:854
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static LLVM_ATTRIBUTE_UNUSED Printable PrintLaneMask(LaneBitmask LaneMask)
Create Printable object to print LaneBitmasks on a raw_ostream.
Definition: LaneBitmask.h:87
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:81
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
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 ...
print Print MemDeps of function
IRTranslator LLVM IR MI
void join(LiveRange &Other, const int *ValNoAssignments, const int *RHSValNoAssignments, SmallVectorImpl< VNInfo * > &NewVNInfo)
join - Join two live ranges (this, and other) together.
reg_iterator reg_begin(unsigned RegNo) const
bool readsReg() const
readsReg - Returns true if this operand reads the previous value of its register. ...
ppc ctr loops verify
VNInfo * valueIn() const
Return the value that is live-in to the instruction.
Definition: LiveInterval.h:105
SlotIndex getNextSlot() const
Returns the next slot in the index list.
Definition: SlotIndexes.h:270
bool hasSubRanges() const
Returns true if subregister liveness information is available.
Definition: LiveInterval.h:751
static reg_iterator reg_end()
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:84
reg_begin/reg_end - Provide iteration support to walk over all definitions and uses of a register wit...
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:221
VNInfo * getVNInfoBefore(SlotIndex Idx) const
getVNInfoBefore - Return the VNInfo that is live up to but not necessarilly including Idx...
Definition: LiveInterval.h:417
void resize(size_type N)
Definition: SmallVector.h:352