LiveInterval: Introduce createMainRangeFromSubranges().
[oota-llvm.git] / lib / CodeGen / LiveInterval.cpp
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
21 #include "llvm/CodeGen/LiveInterval.h"
22 #include "RegisterCoalescer.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SmallSet.h"
26 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Format.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Target/TargetRegisterInfo.h"
32 #include <algorithm>
33 using namespace llvm;
34
35 LiveRange::iterator LiveRange::find(SlotIndex Pos) {
36   // This algorithm is basically std::upper_bound.
37   // Unfortunately, std::upper_bound cannot be used with mixed types until we
38   // adopt C++0x. Many libraries can do it, but not all.
39   if (empty() || Pos >= endIndex())
40     return end();
41   iterator I = begin();
42   size_t Len = size();
43   do {
44     size_t Mid = Len >> 1;
45     if (Pos < I[Mid].end)
46       Len = Mid;
47     else
48       I += Mid + 1, Len -= Mid + 1;
49   } while (Len);
50   return I;
51 }
52
53 VNInfo *LiveRange::createDeadDef(SlotIndex Def,
54                                   VNInfo::Allocator &VNInfoAllocator) {
55   assert(!Def.isDead() && "Cannot define a value at the dead slot");
56   iterator I = find(Def);
57   if (I == end()) {
58     VNInfo *VNI = getNextValue(Def, VNInfoAllocator);
59     segments.push_back(Segment(Def, Def.getDeadSlot(), VNI));
60     return VNI;
61   }
62   if (SlotIndex::isSameInstr(Def, I->start)) {
63     assert(I->valno->def == I->start && "Inconsistent existing value def");
64
65     // It is possible to have both normal and early-clobber defs of the same
66     // register on an instruction. It doesn't make a lot of sense, but it is
67     // possible to specify in inline assembly.
68     //
69     // Just convert everything to early-clobber.
70     Def = std::min(Def, I->start);
71     if (Def != I->start)
72       I->start = I->valno->def = Def;
73     return I->valno;
74   }
75   assert(SlotIndex::isEarlierInstr(Def, I->start) && "Already live at def");
76   VNInfo *VNI = getNextValue(Def, VNInfoAllocator);
77   segments.insert(I, Segment(Def, Def.getDeadSlot(), VNI));
78   return VNI;
79 }
80
81 // overlaps - Return true if the intersection of the two live ranges is
82 // not empty.
83 //
84 // An example for overlaps():
85 //
86 // 0: A = ...
87 // 4: B = ...
88 // 8: C = A + B ;; last use of A
89 //
90 // The live ranges should look like:
91 //
92 // A = [3, 11)
93 // B = [7, x)
94 // C = [11, y)
95 //
96 // A->overlaps(C) should return false since we want to be able to join
97 // A and C.
98 //
99 bool LiveRange::overlapsFrom(const LiveRange& other,
100                              const_iterator StartPos) const {
101   assert(!empty() && "empty range");
102   const_iterator i = begin();
103   const_iterator ie = end();
104   const_iterator j = StartPos;
105   const_iterator je = other.end();
106
107   assert((StartPos->start <= i->start || StartPos == other.begin()) &&
108          StartPos != other.end() && "Bogus start position hint!");
109
110   if (i->start < j->start) {
111     i = std::upper_bound(i, ie, j->start);
112     if (i != begin()) --i;
113   } else if (j->start < i->start) {
114     ++StartPos;
115     if (StartPos != other.end() && StartPos->start <= i->start) {
116       assert(StartPos < other.end() && i < end());
117       j = std::upper_bound(j, je, i->start);
118       if (j != other.begin()) --j;
119     }
120   } else {
121     return true;
122   }
123
124   if (j == je) return false;
125
126   while (i != ie) {
127     if (i->start > j->start) {
128       std::swap(i, j);
129       std::swap(ie, je);
130     }
131
132     if (i->end > j->start)
133       return true;
134     ++i;
135   }
136
137   return false;
138 }
139
140 bool LiveRange::overlaps(const LiveRange &Other, const CoalescerPair &CP,
141                          const SlotIndexes &Indexes) const {
142   assert(!empty() && "empty range");
143   if (Other.empty())
144     return false;
145
146   // Use binary searches to find initial positions.
147   const_iterator I = find(Other.beginIndex());
148   const_iterator IE = end();
149   if (I == IE)
150     return false;
151   const_iterator J = Other.find(I->start);
152   const_iterator JE = Other.end();
153   if (J == JE)
154     return false;
155
156   for (;;) {
157     // J has just been advanced to satisfy:
158     assert(J->end >= I->start);
159     // Check for an overlap.
160     if (J->start < I->end) {
161       // I and J are overlapping. Find the later start.
162       SlotIndex Def = std::max(I->start, J->start);
163       // Allow the overlap if Def is a coalescable copy.
164       if (Def.isBlock() ||
165           !CP.isCoalescable(Indexes.getInstructionFromIndex(Def)))
166         return true;
167     }
168     // Advance the iterator that ends first to check for more overlaps.
169     if (J->end > I->end) {
170       std::swap(I, J);
171       std::swap(IE, JE);
172     }
173     // Advance J until J->end >= I->start.
174     do
175       if (++J == JE)
176         return false;
177     while (J->end < I->start);
178   }
179 }
180
181 /// overlaps - Return true if the live range overlaps an interval specified
182 /// by [Start, End).
183 bool LiveRange::overlaps(SlotIndex Start, SlotIndex End) const {
184   assert(Start < End && "Invalid range");
185   const_iterator I = std::lower_bound(begin(), end(), End);
186   return I != begin() && (--I)->end > Start;
187 }
188
189 bool LiveRange::covers(const LiveRange &Other) const {
190   if (empty())
191     return Other.empty();
192
193   const_iterator I = begin();
194   for (const Segment &O : Other.segments) {
195     I = advanceTo(I, O.start);
196     if (I == end() || I->start > O.start)
197       return false;
198
199     // Check adjacent live segments and see if we can get behind O.end.
200     while (I->end < O.end) {
201       const_iterator Last = I;
202       // Get next segment and abort if it was not adjacent.
203       ++I;
204       if (I == end() || Last->end != I->start)
205         return false;
206     }
207   }
208   return true;
209 }
210
211 /// ValNo is dead, remove it.  If it is the largest value number, just nuke it
212 /// (and any other deleted values neighboring it), otherwise mark it as ~1U so
213 /// it can be nuked later.
214 void LiveRange::markValNoForDeletion(VNInfo *ValNo) {
215   if (ValNo->id == getNumValNums()-1) {
216     do {
217       valnos.pop_back();
218     } while (!valnos.empty() && valnos.back()->isUnused());
219   } else {
220     ValNo->markUnused();
221   }
222 }
223
224 /// RenumberValues - Renumber all values in order of appearance and delete the
225 /// remaining unused values.
226 void LiveRange::RenumberValues() {
227   SmallPtrSet<VNInfo*, 8> Seen;
228   valnos.clear();
229   for (const Segment &S : segments) {
230     VNInfo *VNI = S.valno;
231     if (!Seen.insert(VNI).second)
232       continue;
233     assert(!VNI->isUnused() && "Unused valno used by live segment");
234     VNI->id = (unsigned)valnos.size();
235     valnos.push_back(VNI);
236   }
237 }
238
239 /// This method is used when we want to extend the segment specified by I to end
240 /// at the specified endpoint.  To do this, we should merge and eliminate all
241 /// segments that this will overlap with.  The iterator is not invalidated.
242 void LiveRange::extendSegmentEndTo(iterator I, SlotIndex NewEnd) {
243   assert(I != end() && "Not a valid segment!");
244   VNInfo *ValNo = I->valno;
245
246   // Search for the first segment that we can't merge with.
247   iterator MergeTo = std::next(I);
248   for (; MergeTo != end() && NewEnd >= MergeTo->end; ++MergeTo) {
249     assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
250   }
251
252   // If NewEnd was in the middle of a segment, make sure to get its endpoint.
253   I->end = std::max(NewEnd, std::prev(MergeTo)->end);
254
255   // If the newly formed segment now touches the segment after it and if they
256   // have the same value number, merge the two segments into one segment.
257   if (MergeTo != end() && MergeTo->start <= I->end &&
258       MergeTo->valno == ValNo) {
259     I->end = MergeTo->end;
260     ++MergeTo;
261   }
262
263   // Erase any dead segments.
264   segments.erase(std::next(I), MergeTo);
265 }
266
267
268 /// This method is used when we want to extend the segment specified by I to
269 /// start at the specified endpoint.  To do this, we should merge and eliminate
270 /// all segments that this will overlap with.
271 LiveRange::iterator
272 LiveRange::extendSegmentStartTo(iterator I, SlotIndex NewStart) {
273   assert(I != end() && "Not a valid segment!");
274   VNInfo *ValNo = I->valno;
275
276   // Search for the first segment that we can't merge with.
277   iterator MergeTo = I;
278   do {
279     if (MergeTo == begin()) {
280       I->start = NewStart;
281       segments.erase(MergeTo, I);
282       return I;
283     }
284     assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
285     --MergeTo;
286   } while (NewStart <= MergeTo->start);
287
288   // If we start in the middle of another segment, just delete a range and
289   // extend that segment.
290   if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
291     MergeTo->end = I->end;
292   } else {
293     // Otherwise, extend the segment right after.
294     ++MergeTo;
295     MergeTo->start = NewStart;
296     MergeTo->end = I->end;
297   }
298
299   segments.erase(std::next(MergeTo), std::next(I));
300   return MergeTo;
301 }
302
303 void LiveRange::append(const Segment S) {
304   // Check that the segment belongs to the back of the list.
305   assert(segments.empty() || segments.back().end <= S.start);
306   segments.push_back(S);
307 }
308
309 LiveRange::iterator LiveRange::addSegmentFrom(Segment S, iterator From) {
310   SlotIndex Start = S.start, End = S.end;
311   iterator it = std::upper_bound(From, end(), Start);
312
313   // If the inserted segment starts in the middle or right at the end of
314   // another segment, just extend that segment to contain the segment of S.
315   if (it != begin()) {
316     iterator B = std::prev(it);
317     if (S.valno == B->valno) {
318       if (B->start <= Start && B->end >= Start) {
319         extendSegmentEndTo(B, End);
320         return B;
321       }
322     } else {
323       // Check to make sure that we are not overlapping two live segments with
324       // different valno's.
325       assert(B->end <= Start &&
326              "Cannot overlap two segments with differing ValID's"
327              " (did you def the same reg twice in a MachineInstr?)");
328     }
329   }
330
331   // Otherwise, if this segment ends in the middle of, or right next to, another
332   // segment, merge it into that segment.
333   if (it != end()) {
334     if (S.valno == it->valno) {
335       if (it->start <= End) {
336         it = extendSegmentStartTo(it, Start);
337
338         // If S is a complete superset of a segment, we may need to grow its
339         // endpoint as well.
340         if (End > it->end)
341           extendSegmentEndTo(it, End);
342         return it;
343       }
344     } else {
345       // Check to make sure that we are not overlapping two live segments with
346       // different valno's.
347       assert(it->start >= End &&
348              "Cannot overlap two segments with differing ValID's");
349     }
350   }
351
352   // Otherwise, this is just a new segment that doesn't interact with anything.
353   // Insert it.
354   return segments.insert(it, S);
355 }
356
357 /// extendInBlock - If this range is live before Kill in the basic
358 /// block that starts at StartIdx, extend it to be live up to Kill and return
359 /// the value. If there is no live range before Kill, return NULL.
360 VNInfo *LiveRange::extendInBlock(SlotIndex StartIdx, SlotIndex Kill) {
361   if (empty())
362     return nullptr;
363   iterator I = std::upper_bound(begin(), end(), Kill.getPrevSlot());
364   if (I == begin())
365     return nullptr;
366   --I;
367   if (I->end <= StartIdx)
368     return nullptr;
369   if (I->end < Kill)
370     extendSegmentEndTo(I, Kill);
371   return I->valno;
372 }
373
374 /// Remove the specified segment from this range.  Note that the segment must
375 /// be in a single Segment in its entirety.
376 void LiveRange::removeSegment(SlotIndex Start, SlotIndex End,
377                               bool RemoveDeadValNo) {
378   // Find the Segment containing this span.
379   iterator I = find(Start);
380   assert(I != end() && "Segment is not in range!");
381   assert(I->containsInterval(Start, End)
382          && "Segment is not entirely in range!");
383
384   // If the span we are removing is at the start of the Segment, adjust it.
385   VNInfo *ValNo = I->valno;
386   if (I->start == Start) {
387     if (I->end == End) {
388       if (RemoveDeadValNo) {
389         // Check if val# is dead.
390         bool isDead = true;
391         for (const_iterator II = begin(), EE = end(); II != EE; ++II)
392           if (II != I && II->valno == ValNo) {
393             isDead = false;
394             break;
395           }
396         if (isDead) {
397           // Now that ValNo is dead, remove it.
398           markValNoForDeletion(ValNo);
399         }
400       }
401
402       segments.erase(I);  // Removed the whole Segment.
403     } else
404       I->start = End;
405     return;
406   }
407
408   // Otherwise if the span we are removing is at the end of the Segment,
409   // adjust the other way.
410   if (I->end == End) {
411     I->end = Start;
412     return;
413   }
414
415   // Otherwise, we are splitting the Segment into two pieces.
416   SlotIndex OldEnd = I->end;
417   I->end = Start;   // Trim the old segment.
418
419   // Insert the new one.
420   segments.insert(std::next(I), Segment(End, OldEnd, ValNo));
421 }
422
423 /// removeValNo - Remove all the segments defined by the specified value#.
424 /// Also remove the value# from value# list.
425 void LiveRange::removeValNo(VNInfo *ValNo) {
426   if (empty()) return;
427   iterator I = end();
428   iterator E = begin();
429   do {
430     --I;
431     if (I->valno == ValNo)
432       segments.erase(I);
433   } while (I != E);
434   // Now that ValNo is dead, remove it.
435   markValNoForDeletion(ValNo);
436 }
437
438 void LiveRange::join(LiveRange &Other,
439                      const int *LHSValNoAssignments,
440                      const int *RHSValNoAssignments,
441                      SmallVectorImpl<VNInfo *> &NewVNInfo) {
442   verify();
443
444   // Determine if any of our values are mapped.  This is uncommon, so we want
445   // to avoid the range scan if not.
446   bool MustMapCurValNos = false;
447   unsigned NumVals = getNumValNums();
448   unsigned NumNewVals = NewVNInfo.size();
449   for (unsigned i = 0; i != NumVals; ++i) {
450     unsigned LHSValID = LHSValNoAssignments[i];
451     if (i != LHSValID ||
452         (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i))) {
453       MustMapCurValNos = true;
454       break;
455     }
456   }
457
458   // If we have to apply a mapping to our base range assignment, rewrite it now.
459   if (MustMapCurValNos && !empty()) {
460     // Map the first live range.
461
462     iterator OutIt = begin();
463     OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
464     for (iterator I = std::next(OutIt), E = end(); I != E; ++I) {
465       VNInfo* nextValNo = NewVNInfo[LHSValNoAssignments[I->valno->id]];
466       assert(nextValNo && "Huh?");
467
468       // If this live range has the same value # as its immediate predecessor,
469       // and if they are neighbors, remove one Segment.  This happens when we
470       // have [0,4:0)[4,7:1) and map 0/1 onto the same value #.
471       if (OutIt->valno == nextValNo && OutIt->end == I->start) {
472         OutIt->end = I->end;
473       } else {
474         // Didn't merge. Move OutIt to the next segment,
475         ++OutIt;
476         OutIt->valno = nextValNo;
477         if (OutIt != I) {
478           OutIt->start = I->start;
479           OutIt->end = I->end;
480         }
481       }
482     }
483     // If we merge some segments, chop off the end.
484     ++OutIt;
485     segments.erase(OutIt, end());
486   }
487
488   // Rewrite Other values before changing the VNInfo ids.
489   // This can leave Other in an invalid state because we're not coalescing
490   // touching segments that now have identical values. That's OK since Other is
491   // not supposed to be valid after calling join();
492   for (Segment &S : Other.segments)
493     S.valno = NewVNInfo[RHSValNoAssignments[S.valno->id]];
494
495   // Update val# info. Renumber them and make sure they all belong to this
496   // LiveRange now. Also remove dead val#'s.
497   unsigned NumValNos = 0;
498   for (unsigned i = 0; i < NumNewVals; ++i) {
499     VNInfo *VNI = NewVNInfo[i];
500     if (VNI) {
501       if (NumValNos >= NumVals)
502         valnos.push_back(VNI);
503       else
504         valnos[NumValNos] = VNI;
505       VNI->id = NumValNos++;  // Renumber val#.
506     }
507   }
508   if (NumNewVals < NumVals)
509     valnos.resize(NumNewVals);  // shrinkify
510
511   // Okay, now insert the RHS live segments into the LHS.
512   LiveRangeUpdater Updater(this);
513   for (Segment &S : Other.segments)
514     Updater.add(S);
515 }
516
517 /// Merge all of the segments in RHS into this live range as the specified
518 /// value number.  The segments in RHS are allowed to overlap with segments in
519 /// the current range, but only if the overlapping segments have the
520 /// specified value number.
521 void LiveRange::MergeSegmentsInAsValue(const LiveRange &RHS,
522                                        VNInfo *LHSValNo) {
523   LiveRangeUpdater Updater(this);
524   for (const Segment &S : RHS.segments)
525     Updater.add(S.start, S.end, LHSValNo);
526 }
527
528 /// MergeValueInAsValue - Merge all of the live segments of a specific val#
529 /// in RHS into this live range as the specified value number.
530 /// The segments in RHS are allowed to overlap with segments in the
531 /// current range, it will replace the value numbers of the overlaped
532 /// segments with the specified value number.
533 void LiveRange::MergeValueInAsValue(const LiveRange &RHS,
534                                     const VNInfo *RHSValNo,
535                                     VNInfo *LHSValNo) {
536   LiveRangeUpdater Updater(this);
537   for (const Segment &S : RHS.segments)
538     if (S.valno == RHSValNo)
539       Updater.add(S.start, S.end, LHSValNo);
540 }
541
542 /// MergeValueNumberInto - This method is called when two value nubmers
543 /// are found to be equivalent.  This eliminates V1, replacing all
544 /// segments with the V1 value number with the V2 value number.  This can
545 /// cause merging of V1/V2 values numbers and compaction of the value space.
546 VNInfo *LiveRange::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
547   assert(V1 != V2 && "Identical value#'s are always equivalent!");
548
549   // This code actually merges the (numerically) larger value number into the
550   // smaller value number, which is likely to allow us to compactify the value
551   // space.  The only thing we have to be careful of is to preserve the
552   // instruction that defines the result value.
553
554   // Make sure V2 is smaller than V1.
555   if (V1->id < V2->id) {
556     V1->copyFrom(*V2);
557     std::swap(V1, V2);
558   }
559
560   // Merge V1 segments into V2.
561   for (iterator I = begin(); I != end(); ) {
562     iterator S = I++;
563     if (S->valno != V1) continue;  // Not a V1 Segment.
564
565     // Okay, we found a V1 live range.  If it had a previous, touching, V2 live
566     // range, extend it.
567     if (S != begin()) {
568       iterator Prev = S-1;
569       if (Prev->valno == V2 && Prev->end == S->start) {
570         Prev->end = S->end;
571
572         // Erase this live-range.
573         segments.erase(S);
574         I = Prev+1;
575         S = Prev;
576       }
577     }
578
579     // Okay, now we have a V1 or V2 live range that is maximally merged forward.
580     // Ensure that it is a V2 live-range.
581     S->valno = V2;
582
583     // If we can merge it into later V2 segments, do so now.  We ignore any
584     // following V1 segments, as they will be merged in subsequent iterations
585     // of the loop.
586     if (I != end()) {
587       if (I->start == S->end && I->valno == V2) {
588         S->end = I->end;
589         segments.erase(I);
590         I = S+1;
591       }
592     }
593   }
594
595   // Now that V1 is dead, remove it.
596   markValNoForDeletion(V1);
597
598   return V2;
599 }
600
601 void LiveInterval::removeEmptySubRanges() {
602   SubRange **NextPtr = &SubRanges;
603   SubRange *I = *NextPtr;
604   while (I != nullptr) {
605     if (!I->empty()) {
606       NextPtr = &I->Next;
607       I = *NextPtr;
608       continue;
609     }
610     // Skip empty subranges until we find the first nonempty one.
611     do {
612       I = I->Next;
613     } while (I != nullptr && I->empty());
614     *NextPtr = I;
615   }
616 }
617
618 /// Helper function for constructMainRangeFromSubranges(): Search the CFG
619 /// backwards until we find a place covered by a LiveRange segment that actually
620 /// has a valno set.
621 static VNInfo *searchForVNI(const SlotIndexes &Indexes, LiveRange &LR,
622     const MachineBasicBlock *MBB,
623     SmallPtrSetImpl<const MachineBasicBlock*> &Visited) {
624   // We start the search at the end of MBB.
625   SlotIndex EndIdx = Indexes.getMBBEndIdx(MBB);
626   // In our use case we can't live the area covered by the live segments without
627   // finding an actual VNI def.
628   LiveRange::iterator I = LR.find(EndIdx.getPrevSlot());
629   assert(I != LR.end());
630   LiveRange::Segment &S = *I;
631   if (S.valno != nullptr)
632     return S.valno;
633
634   VNInfo *VNI = nullptr;
635   // Continue at predecessors (we could even go to idom with domtree available).
636   for (const MachineBasicBlock *Pred : MBB->predecessors()) {
637     // Avoid going in circles.
638     if (Visited.count(Pred))
639       continue;
640     Visited.insert(Pred);
641
642     VNI = searchForVNI(Indexes, LR, Pred, Visited);
643     if (VNI != nullptr) {
644       S.valno = VNI;
645       break;
646     }
647   }
648
649   return VNI;
650 }
651
652 void LiveInterval::constructMainRangeFromSubranges(
653     const SlotIndexes &Indexes, VNInfo::Allocator &VNIAllocator) {
654   // The basic observations on which this algorithm is based:
655   // - Each Def/ValNo in a subrange must have a corresponding def on the main
656   //   range, but not further defs/valnos are necessary.
657   // - If any of the subranges is live at a point the main liverange has to be
658   //   live too, conversily if no subrange is live the main range mustn't be
659   //   live either.
660   // We do this by scannig through all the subranges simultaneously creating new
661   // segments in the main range as segments start/ends come up in the subranges.
662   assert(hasSubRanges());
663   assert(segments.empty() && valnos.empty() && "expected empty main range");
664
665   // Collect subrange, iterator pairs for the walk and determine first and last
666   // SlotIndex involved.
667   SmallVector<std::pair<const SubRange*, const_iterator>, 4> SRs;
668   SlotIndex First;
669   SlotIndex Last;
670   for (const SubRange &SR : subranges()) {
671     if (SR.empty())
672       continue;
673     SRs.push_back(std::make_pair(&SR, SR.begin()));
674     if (!First.isValid() || SR.segments.front().start < First)
675       First = SR.segments.front().start;
676     if (!Last.isValid() || SR.segments.back().end > Last)
677       Last = SR.segments.back().end;
678   }
679
680   errs() << "Compute: " << *this << "\n";
681
682   // Walk over all subranges simultaneously.
683   Segment CurrentSegment;
684   bool ConstructingSegment = false;
685   bool NeedVNIFixup = false;
686   unsigned ActiveMask = 0;
687   SlotIndex Pos = First;
688   while (true) {
689     SlotIndex NextPos = Last;
690     enum {
691       NOTHING,
692       BEGIN_SEGMENT,
693       END_SEGMENT,
694     } Event = NOTHING;
695     unsigned EventMask = 0;
696     bool IsDef = false;
697     // Find the next begin or end of a subrange segment. Combine masks if we
698     // have multiple begins/ends at the same position. Ends take precedence over
699     // Begins.
700     for (auto &SRP : SRs) {
701       const SubRange &SR = *SRP.first;
702       const_iterator &I = SRP.second;
703       while (I != SR.end() &&
704              (I->end < Pos ||
705               (I->end == Pos && (ActiveMask & SR.LaneMask) == 0)))
706         ++I;
707       if (I == SR.end())
708         continue;
709       if ((ActiveMask & SR.LaneMask) == 0 &&
710           Pos <= I->start && I->start <= NextPos) {
711         // Merge multiple begins at the same position
712         if (I->start == NextPos && Event == BEGIN_SEGMENT) {
713           EventMask |= SR.LaneMask;
714           IsDef |= I->valno->def == I->start;
715         } else if (I->start < NextPos || Event != END_SEGMENT) {
716           Event = BEGIN_SEGMENT;
717           NextPos = I->start;
718           EventMask = SR.LaneMask;
719           IsDef = I->valno->def == I->start;
720         }
721       }
722       if ((ActiveMask & SR.LaneMask) != 0 &&
723           Pos <= I->end && I->end <= NextPos) {
724         // Merge multiple ends at the same position.
725         if (I->end == NextPos && Event == END_SEGMENT)
726           EventMask |= SR.LaneMask;
727         else {
728           Event = END_SEGMENT;
729           NextPos = I->end;
730           EventMask = SR.LaneMask;
731         }
732       }
733     }
734
735 #if 1
736     errs() << '\t' << (Event == NOTHING ? "nothing "
737             : Event == BEGIN_SEGMENT ? "begin "
738             : "end ")
739            << NextPos << " mask " << ActiveMask << " evmask " << EventMask << " def " << IsDef << "\n";
740 #endif
741
742     // Advance scan position.
743     Pos = NextPos;
744     if (Event == BEGIN_SEGMENT) {
745       if (ConstructingSegment && IsDef) {
746         // Finish previous segment because we have to start a new one.
747         CurrentSegment.end = Pos;
748         append(CurrentSegment);
749         ConstructingSegment = false;
750       }
751
752       // Start a new segment if necessary.
753       if (!ConstructingSegment) {
754         // Determine value number for the segment.
755         VNInfo *VNI;
756         if (IsDef) {
757           VNI = getNextValue(Pos, VNIAllocator);
758         } else {
759           // We have to reuse an existing value number, if we are lucky
760           // then we already passed one of the predecessor blocks and determined
761           // its value number (with blocks in reverse postorder this would be
762           // always true but we have no such guarantee).
763           assert(Pos.isBlock());
764           const MachineBasicBlock *MBB = Indexes.getMBBFromIndex(Pos);
765           // See if any of the predecessor blocks has a lower number and a VNI
766           for (const MachineBasicBlock *Pred : MBB->predecessors()) {
767             SlotIndex PredEnd = Indexes.getMBBEndIdx(Pred);
768             VNI = getVNInfoBefore(PredEnd);
769             if (VNI != nullptr)
770               break;
771           }
772           // Def will come later: We have to do an extra fixup pass.
773           if (VNI == nullptr)
774             NeedVNIFixup = true;
775         }
776
777         CurrentSegment.start = Pos;
778         CurrentSegment.valno = VNI;
779         ConstructingSegment = true;
780       }
781       ActiveMask |= EventMask;
782     } else if (Event == END_SEGMENT) {
783       assert(ConstructingSegment);
784       // Finish segment if no lane is active anymore.
785       ActiveMask &= ~EventMask;
786       if (ActiveMask == 0) {
787         CurrentSegment.end = Pos;
788         append(CurrentSegment);
789         ConstructingSegment = false;
790       }
791     } else {
792       // We reached the end of the last subranges and can stop.
793       assert(Event == NOTHING);
794       break;
795     }
796   }
797
798   // We might not be able to assign new valnos for all segments if the basic
799   // block containing the definition comes after a segment using the valno.
800   // Do a fixup pass for this uncommon case.
801   if (NeedVNIFixup) {
802     SmallPtrSet<const MachineBasicBlock*, 5> Visited;
803     for (Segment &S : segments) {
804       if (S.valno != nullptr)
805         continue;
806       // This can only happen at the begin of a basic block.
807       assert(S.start.isBlock());
808
809       Visited.clear();
810       const MachineBasicBlock *MBB = Indexes.getMBBFromIndex(S.start);
811       for (const MachineBasicBlock *Pred : MBB->predecessors()) {
812         VNInfo *VNI = searchForVNI(Indexes, *this, Pred, Visited);
813         if (VNI != nullptr) {
814           S.valno = VNI;
815           break;
816         }
817       }
818       assert(S.valno != nullptr);
819     }
820   }
821   assert(ActiveMask == 0 && !ConstructingSegment);
822   errs() << "Result: " << *this << "\n";
823   verify();
824 }
825
826 unsigned LiveInterval::getSize() const {
827   unsigned Sum = 0;
828   for (const Segment &S : segments)
829     Sum += S.start.distance(S.end);
830   return Sum;
831 }
832
833 raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange::Segment &S) {
834   return os << '[' << S.start << ',' << S.end << ':' << S.valno->id << ")";
835 }
836
837 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
838 void LiveRange::Segment::dump() const {
839   dbgs() << *this << "\n";
840 }
841 #endif
842
843 void LiveRange::print(raw_ostream &OS) const {
844   if (empty())
845     OS << "EMPTY";
846   else {
847     for (const Segment &S : segments) {
848       OS << S;
849       assert(S.valno == getValNumInfo(S.valno->id) && "Bad VNInfo");
850     }
851   }
852
853   // Print value number info.
854   if (getNumValNums()) {
855     OS << "  ";
856     unsigned vnum = 0;
857     for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
858          ++i, ++vnum) {
859       const VNInfo *vni = *i;
860       if (vnum) OS << " ";
861       OS << vnum << "@";
862       if (vni->isUnused()) {
863         OS << "x";
864       } else {
865         OS << vni->def;
866         if (vni->isPHIDef())
867           OS << "-phi";
868       }
869     }
870   }
871 }
872
873 void LiveInterval::print(raw_ostream &OS) const {
874   OS << PrintReg(reg) << ' ';
875   super::print(OS);
876   // Print subranges
877   for (const SubRange &SR : subranges()) {
878     OS << format(" L%04X ", SR.LaneMask) << SR;
879   }
880 }
881
882 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
883 void LiveRange::dump() const {
884   dbgs() << *this << "\n";
885 }
886
887 void LiveInterval::dump() const {
888   dbgs() << *this << "\n";
889 }
890 #endif
891
892 #ifndef NDEBUG
893 void LiveRange::verify() const {
894   for (const_iterator I = begin(), E = end(); I != E; ++I) {
895     assert(I->start.isValid());
896     assert(I->end.isValid());
897     assert(I->start < I->end);
898     assert(I->valno != nullptr);
899     assert(I->valno->id < valnos.size());
900     assert(I->valno == valnos[I->valno->id]);
901     if (std::next(I) != E) {
902       assert(I->end <= std::next(I)->start);
903       if (I->end == std::next(I)->start)
904         assert(I->valno != std::next(I)->valno);
905     }
906   }
907 }
908
909 void LiveInterval::verify(const MachineRegisterInfo *MRI) const {
910   super::verify();
911
912   // Make sure SubRanges are fine and LaneMasks are disjunct.
913   unsigned Mask = 0;
914   unsigned MaxMask = MRI != nullptr ? MRI->getMaxLaneMaskForVReg(reg) : ~0u;
915   for (const SubRange &SR : subranges()) {
916     // Subrange lanemask should be disjunct to any previous subrange masks.
917     assert((Mask & SR.LaneMask) == 0);
918     Mask |= SR.LaneMask;
919
920     // subrange mask should not contained in maximum lane mask for the vreg.
921     assert((Mask & ~MaxMask) == 0);
922
923     SR.verify();
924     // Main liverange should cover subrange.
925     assert(covers(SR));
926   }
927 }
928 #endif
929
930
931 //===----------------------------------------------------------------------===//
932 //                           LiveRangeUpdater class
933 //===----------------------------------------------------------------------===//
934 //
935 // The LiveRangeUpdater class always maintains these invariants:
936 //
937 // - When LastStart is invalid, Spills is empty and the iterators are invalid.
938 //   This is the initial state, and the state created by flush().
939 //   In this state, isDirty() returns false.
940 //
941 // Otherwise, segments are kept in three separate areas:
942 //
943 // 1. [begin; WriteI) at the front of LR.
944 // 2. [ReadI; end) at the back of LR.
945 // 3. Spills.
946 //
947 // - LR.begin() <= WriteI <= ReadI <= LR.end().
948 // - Segments in all three areas are fully ordered and coalesced.
949 // - Segments in area 1 precede and can't coalesce with segments in area 2.
950 // - Segments in Spills precede and can't coalesce with segments in area 2.
951 // - No coalescing is possible between segments in Spills and segments in area
952 //   1, and there are no overlapping segments.
953 //
954 // The segments in Spills are not ordered with respect to the segments in area
955 // 1. They need to be merged.
956 //
957 // When they exist, Spills.back().start <= LastStart,
958 //                 and WriteI[-1].start <= LastStart.
959
960 void LiveRangeUpdater::print(raw_ostream &OS) const {
961   if (!isDirty()) {
962     if (LR)
963       OS << "Clean updater: " << *LR << '\n';
964     else
965       OS << "Null updater.\n";
966     return;
967   }
968   assert(LR && "Can't have null LR in dirty updater.");
969   OS << " updater with gap = " << (ReadI - WriteI)
970      << ", last start = " << LastStart
971      << ":\n  Area 1:";
972   for (const auto &S : make_range(LR->begin(), WriteI))
973     OS << ' ' << S;
974   OS << "\n  Spills:";
975   for (unsigned I = 0, E = Spills.size(); I != E; ++I)
976     OS << ' ' << Spills[I];
977   OS << "\n  Area 2:";
978   for (const auto &S : make_range(ReadI, LR->end()))
979     OS << ' ' << S;
980   OS << '\n';
981 }
982
983 void LiveRangeUpdater::dump() const
984 {
985   print(errs());
986 }
987
988 // Determine if A and B should be coalesced.
989 static inline bool coalescable(const LiveRange::Segment &A,
990                                const LiveRange::Segment &B) {
991   assert(A.start <= B.start && "Unordered live segments.");
992   if (A.end == B.start)
993     return A.valno == B.valno;
994   if (A.end < B.start)
995     return false;
996   assert(A.valno == B.valno && "Cannot overlap different values");
997   return true;
998 }
999
1000 void LiveRangeUpdater::add(LiveRange::Segment Seg) {
1001   assert(LR && "Cannot add to a null destination");
1002
1003   // Flush the state if Start moves backwards.
1004   if (!LastStart.isValid() || LastStart > Seg.start) {
1005     if (isDirty())
1006       flush();
1007     // This brings us to an uninitialized state. Reinitialize.
1008     assert(Spills.empty() && "Leftover spilled segments");
1009     WriteI = ReadI = LR->begin();
1010   }
1011
1012   // Remember start for next time.
1013   LastStart = Seg.start;
1014
1015   // Advance ReadI until it ends after Seg.start.
1016   LiveRange::iterator E = LR->end();
1017   if (ReadI != E && ReadI->end <= Seg.start) {
1018     // First try to close the gap between WriteI and ReadI with spills.
1019     if (ReadI != WriteI)
1020       mergeSpills();
1021     // Then advance ReadI.
1022     if (ReadI == WriteI)
1023       ReadI = WriteI = LR->find(Seg.start);
1024     else
1025       while (ReadI != E && ReadI->end <= Seg.start)
1026         *WriteI++ = *ReadI++;
1027   }
1028
1029   assert(ReadI == E || ReadI->end > Seg.start);
1030
1031   // Check if the ReadI segment begins early.
1032   if (ReadI != E && ReadI->start <= Seg.start) {
1033     assert(ReadI->valno == Seg.valno && "Cannot overlap different values");
1034     // Bail if Seg is completely contained in ReadI.
1035     if (ReadI->end >= Seg.end)
1036       return;
1037     // Coalesce into Seg.
1038     Seg.start = ReadI->start;
1039     ++ReadI;
1040   }
1041
1042   // Coalesce as much as possible from ReadI into Seg.
1043   while (ReadI != E && coalescable(Seg, *ReadI)) {
1044     Seg.end = std::max(Seg.end, ReadI->end);
1045     ++ReadI;
1046   }
1047
1048   // Try coalescing Spills.back() into Seg.
1049   if (!Spills.empty() && coalescable(Spills.back(), Seg)) {
1050     Seg.start = Spills.back().start;
1051     Seg.end = std::max(Spills.back().end, Seg.end);
1052     Spills.pop_back();
1053   }
1054
1055   // Try coalescing Seg into WriteI[-1].
1056   if (WriteI != LR->begin() && coalescable(WriteI[-1], Seg)) {
1057     WriteI[-1].end = std::max(WriteI[-1].end, Seg.end);
1058     return;
1059   }
1060
1061   // Seg doesn't coalesce with anything, and needs to be inserted somewhere.
1062   if (WriteI != ReadI) {
1063     *WriteI++ = Seg;
1064     return;
1065   }
1066
1067   // Finally, append to LR or Spills.
1068   if (WriteI == E) {
1069     LR->segments.push_back(Seg);
1070     WriteI = ReadI = LR->end();
1071   } else
1072     Spills.push_back(Seg);
1073 }
1074
1075 // Merge as many spilled segments as possible into the gap between WriteI
1076 // and ReadI. Advance WriteI to reflect the inserted instructions.
1077 void LiveRangeUpdater::mergeSpills() {
1078   // Perform a backwards merge of Spills and [SpillI;WriteI).
1079   size_t GapSize = ReadI - WriteI;
1080   size_t NumMoved = std::min(Spills.size(), GapSize);
1081   LiveRange::iterator Src = WriteI;
1082   LiveRange::iterator Dst = Src + NumMoved;
1083   LiveRange::iterator SpillSrc = Spills.end();
1084   LiveRange::iterator B = LR->begin();
1085
1086   // This is the new WriteI position after merging spills.
1087   WriteI = Dst;
1088
1089   // Now merge Src and Spills backwards.
1090   while (Src != Dst) {
1091     if (Src != B && Src[-1].start > SpillSrc[-1].start)
1092       *--Dst = *--Src;
1093     else
1094       *--Dst = *--SpillSrc;
1095   }
1096   assert(NumMoved == size_t(Spills.end() - SpillSrc));
1097   Spills.erase(SpillSrc, Spills.end());
1098 }
1099
1100 void LiveRangeUpdater::flush() {
1101   if (!isDirty())
1102     return;
1103   // Clear the dirty state.
1104   LastStart = SlotIndex();
1105
1106   assert(LR && "Cannot add to a null destination");
1107
1108   // Nothing to merge?
1109   if (Spills.empty()) {
1110     LR->segments.erase(WriteI, ReadI);
1111     LR->verify();
1112     return;
1113   }
1114
1115   // Resize the WriteI - ReadI gap to match Spills.
1116   size_t GapSize = ReadI - WriteI;
1117   if (GapSize < Spills.size()) {
1118     // The gap is too small. Make some room.
1119     size_t WritePos = WriteI - LR->begin();
1120     LR->segments.insert(ReadI, Spills.size() - GapSize, LiveRange::Segment());
1121     // This also invalidated ReadI, but it is recomputed below.
1122     WriteI = LR->begin() + WritePos;
1123   } else {
1124     // Shrink the gap if necessary.
1125     LR->segments.erase(WriteI + Spills.size(), ReadI);
1126   }
1127   ReadI = WriteI + Spills.size();
1128   mergeSpills();
1129   LR->verify();
1130 }
1131
1132 unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval *LI) {
1133   // Create initial equivalence classes.
1134   EqClass.clear();
1135   EqClass.grow(LI->getNumValNums());
1136
1137   const VNInfo *used = nullptr, *unused = nullptr;
1138
1139   // Determine connections.
1140   for (const VNInfo *VNI : LI->valnos) {
1141     // Group all unused values into one class.
1142     if (VNI->isUnused()) {
1143       if (unused)
1144         EqClass.join(unused->id, VNI->id);
1145       unused = VNI;
1146       continue;
1147     }
1148     used = VNI;
1149     if (VNI->isPHIDef()) {
1150       const MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
1151       assert(MBB && "Phi-def has no defining MBB");
1152       // Connect to values live out of predecessors.
1153       for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
1154            PE = MBB->pred_end(); PI != PE; ++PI)
1155         if (const VNInfo *PVNI = LI->getVNInfoBefore(LIS.getMBBEndIdx(*PI)))
1156           EqClass.join(VNI->id, PVNI->id);
1157     } else {
1158       // Normal value defined by an instruction. Check for two-addr redef.
1159       // FIXME: This could be coincidental. Should we really check for a tied
1160       // operand constraint?
1161       // Note that VNI->def may be a use slot for an early clobber def.
1162       if (const VNInfo *UVNI = LI->getVNInfoBefore(VNI->def))
1163         EqClass.join(VNI->id, UVNI->id);
1164     }
1165   }
1166
1167   // Lump all the unused values in with the last used value.
1168   if (used && unused)
1169     EqClass.join(used->id, unused->id);
1170
1171   EqClass.compress();
1172   return EqClass.getNumClasses();
1173 }
1174
1175 void ConnectedVNInfoEqClasses::Distribute(LiveInterval *LIV[],
1176                                           MachineRegisterInfo &MRI) {
1177   assert(LIV[0] && "LIV[0] must be set");
1178   LiveInterval &LI = *LIV[0];
1179
1180   // Rewrite instructions.
1181   for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LI.reg),
1182        RE = MRI.reg_end(); RI != RE;) {
1183     MachineOperand &MO = *RI;
1184     MachineInstr *MI = RI->getParent();
1185     ++RI;
1186     // DBG_VALUE instructions don't have slot indexes, so get the index of the
1187     // instruction before them.
1188     // Normally, DBG_VALUE instructions are removed before this function is
1189     // called, but it is not a requirement.
1190     SlotIndex Idx;
1191     if (MI->isDebugValue())
1192       Idx = LIS.getSlotIndexes()->getIndexBefore(MI);
1193     else
1194       Idx = LIS.getInstructionIndex(MI);
1195     LiveQueryResult LRQ = LI.Query(Idx);
1196     const VNInfo *VNI = MO.readsReg() ? LRQ.valueIn() : LRQ.valueDefined();
1197     // In the case of an <undef> use that isn't tied to any def, VNI will be
1198     // NULL. If the use is tied to a def, VNI will be the defined value.
1199     if (!VNI)
1200       continue;
1201     MO.setReg(LIV[getEqClass(VNI)]->reg);
1202   }
1203
1204   // Move runs to new intervals.
1205   LiveInterval::iterator J = LI.begin(), E = LI.end();
1206   while (J != E && EqClass[J->valno->id] == 0)
1207     ++J;
1208   for (LiveInterval::iterator I = J; I != E; ++I) {
1209     if (unsigned eq = EqClass[I->valno->id]) {
1210       assert((LIV[eq]->empty() || LIV[eq]->expiredAt(I->start)) &&
1211              "New intervals should be empty");
1212       LIV[eq]->segments.push_back(*I);
1213     } else
1214       *J++ = *I;
1215   }
1216   // TODO: do not cheat anymore by simply cleaning all subranges
1217   LI.clearSubRanges();
1218   LI.segments.erase(J, E);
1219
1220   // Transfer VNInfos to their new owners and renumber them.
1221   unsigned j = 0, e = LI.getNumValNums();
1222   while (j != e && EqClass[j] == 0)
1223     ++j;
1224   for (unsigned i = j; i != e; ++i) {
1225     VNInfo *VNI = LI.getValNumInfo(i);
1226     if (unsigned eq = EqClass[i]) {
1227       VNI->id = LIV[eq]->getNumValNums();
1228       LIV[eq]->valnos.push_back(VNI);
1229     } else {
1230       VNI->id = j;
1231       LI.valnos[j++] = VNI;
1232     }
1233   }
1234   LI.valnos.resize(j);
1235 }