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 interval 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 intervals can have holes,
15 // i.e. an interval might look like [1,20), [50,65), [1000,1001). Each
16 // individual range is represented as an instance of LiveRange, and the whole
17 // interval is represented as an instance of LiveInterval.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #include "llvm/CodeGen/LiveInterval.h"
22 #include "llvm/CodeGen/LiveIntervalAnalysis.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/SmallSet.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/Support/Debug.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Target/TargetRegisterInfo.h"
30 #include <algorithm>
31 using namespace llvm;
32
33 // An example for liveAt():
34 //
35 // this = [1,4), liveAt(0) will return false. The instruction defining this
36 // spans slots [0,3]. The interval belongs to an spilled definition of the
37 // variable it represents. This is because slot 1 is used (def slot) and spans
38 // up to slot 3 (store slot).
39 //
liveAt(SlotIndex I) const40 bool LiveInterval::liveAt(SlotIndex I) const {
41 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
42
43 if (r == ranges.begin())
44 return false;
45
46 --r;
47 return r->contains(I);
48 }
49
50 // liveBeforeAndAt - Check if the interval is live at the index and the index
51 // just before it. If index is liveAt, check if it starts a new live range.
52 // If it does, then check if the previous live range ends at index-1.
liveBeforeAndAt(SlotIndex I) const53 bool LiveInterval::liveBeforeAndAt(SlotIndex I) const {
54 Ranges::const_iterator r = std::upper_bound(ranges.begin(), ranges.end(), I);
55
56 if (r == ranges.begin())
57 return false;
58
59 --r;
60 if (!r->contains(I))
61 return false;
62 if (I != r->start)
63 return true;
64 // I is the start of a live range. Check if the previous live range ends
65 // at I-1.
66 if (r == ranges.begin())
67 return false;
68 return r->end == I;
69 }
70
71 /// killedAt - Return true if a live range ends at index. Note that the kill
72 /// point is not contained in the half-open live range. It is usually the
73 /// getDefIndex() slot following its last use.
killedAt(SlotIndex I) const74 bool LiveInterval::killedAt(SlotIndex I) const {
75 Ranges::const_iterator r = std::lower_bound(ranges.begin(), ranges.end(), I);
76
77 // Now r points to the first interval with start >= I, or ranges.end().
78 if (r == ranges.begin())
79 return false;
80
81 --r;
82 // Now r points to the last interval with end <= I.
83 // r->end is the kill point.
84 return r->end == I;
85 }
86
87 /// killedInRange - Return true if the interval has kills in [Start,End).
killedInRange(SlotIndex Start,SlotIndex End) const88 bool LiveInterval::killedInRange(SlotIndex Start, SlotIndex End) const {
89 Ranges::const_iterator r =
90 std::lower_bound(ranges.begin(), ranges.end(), End);
91
92 // Now r points to the first interval with start >= End, or ranges.end().
93 if (r == ranges.begin())
94 return false;
95
96 --r;
97 // Now r points to the last interval with end <= End.
98 // r->end is the kill point.
99 return r->end >= Start && r->end < End;
100 }
101
102 // overlaps - Return true if the intersection of the two live intervals is
103 // not empty.
104 //
105 // An example for overlaps():
106 //
107 // 0: A = ...
108 // 4: B = ...
109 // 8: C = A + B ;; last use of A
110 //
111 // The live intervals should look like:
112 //
113 // A = [3, 11)
114 // B = [7, x)
115 // C = [11, y)
116 //
117 // A->overlaps(C) should return false since we want to be able to join
118 // A and C.
119 //
overlapsFrom(const LiveInterval & other,const_iterator StartPos) const120 bool LiveInterval::overlapsFrom(const LiveInterval& other,
121 const_iterator StartPos) const {
122 assert(!empty() && "empty interval");
123 const_iterator i = begin();
124 const_iterator ie = end();
125 const_iterator j = StartPos;
126 const_iterator je = other.end();
127
128 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
129 StartPos != other.end() && "Bogus start position hint!");
130
131 if (i->start < j->start) {
132 i = std::upper_bound(i, ie, j->start);
133 if (i != ranges.begin()) --i;
134 } else if (j->start < i->start) {
135 ++StartPos;
136 if (StartPos != other.end() && StartPos->start <= i->start) {
137 assert(StartPos < other.end() && i < end());
138 j = std::upper_bound(j, je, i->start);
139 if (j != other.ranges.begin()) --j;
140 }
141 } else {
142 return true;
143 }
144
145 if (j == je) return false;
146
147 while (i != ie) {
148 if (i->start > j->start) {
149 std::swap(i, j);
150 std::swap(ie, je);
151 }
152
153 if (i->end > j->start)
154 return true;
155 ++i;
156 }
157
158 return false;
159 }
160
161 /// overlaps - Return true if the live interval overlaps a range specified
162 /// by [Start, End).
overlaps(SlotIndex Start,SlotIndex End) const163 bool LiveInterval::overlaps(SlotIndex Start, SlotIndex End) const {
164 assert(Start < End && "Invalid range");
165 const_iterator I = std::lower_bound(begin(), end(), End);
166 return I != begin() && (--I)->end > Start;
167 }
168
169
170 /// ValNo is dead, remove it. If it is the largest value number, just nuke it
171 /// (and any other deleted values neighboring it), otherwise mark it as ~1U so
172 /// it can be nuked later.
markValNoForDeletion(VNInfo * ValNo)173 void LiveInterval::markValNoForDeletion(VNInfo *ValNo) {
174 if (ValNo->id == getNumValNums()-1) {
175 do {
176 valnos.pop_back();
177 } while (!valnos.empty() && valnos.back()->isUnused());
178 } else {
179 ValNo->setIsUnused(true);
180 }
181 }
182
183 /// RenumberValues - Renumber all values in order of appearance and delete the
184 /// remaining unused values.
RenumberValues(LiveIntervals & lis)185 void LiveInterval::RenumberValues(LiveIntervals &lis) {
186 SmallPtrSet<VNInfo*, 8> Seen;
187 bool seenPHIDef = false;
188 valnos.clear();
189 for (const_iterator I = begin(), E = end(); I != E; ++I) {
190 VNInfo *VNI = I->valno;
191 if (!Seen.insert(VNI))
192 continue;
193 assert(!VNI->isUnused() && "Unused valno used by live range");
194 VNI->id = (unsigned)valnos.size();
195 valnos.push_back(VNI);
196 VNI->setHasPHIKill(false);
197 if (VNI->isPHIDef())
198 seenPHIDef = true;
199 }
200
201 // Recompute phi kill flags.
202 if (!seenPHIDef)
203 return;
204 for (const_vni_iterator I = vni_begin(), E = vni_end(); I != E; ++I) {
205 VNInfo *VNI = *I;
206 if (!VNI->isPHIDef())
207 continue;
208 const MachineBasicBlock *PHIBB = lis.getMBBFromIndex(VNI->def);
209 assert(PHIBB && "No basic block for phi-def");
210 for (MachineBasicBlock::const_pred_iterator PI = PHIBB->pred_begin(),
211 PE = PHIBB->pred_end(); PI != PE; ++PI) {
212 VNInfo *KVNI = getVNInfoAt(lis.getMBBEndIdx(*PI).getPrevSlot());
213 if (KVNI)
214 KVNI->setHasPHIKill(true);
215 }
216 }
217 }
218
219 /// extendIntervalEndTo - This method is used when we want to extend the range
220 /// specified by I to end at the specified endpoint. To do this, we should
221 /// merge and eliminate all ranges that this will overlap with. The iterator is
222 /// not invalidated.
extendIntervalEndTo(Ranges::iterator I,SlotIndex NewEnd)223 void LiveInterval::extendIntervalEndTo(Ranges::iterator I, SlotIndex NewEnd) {
224 assert(I != ranges.end() && "Not a valid interval!");
225 VNInfo *ValNo = I->valno;
226
227 // Search for the first interval that we can't merge with.
228 Ranges::iterator MergeTo = llvm::next(I);
229 for (; MergeTo != ranges.end() && NewEnd >= MergeTo->end; ++MergeTo) {
230 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
231 }
232
233 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
234 I->end = std::max(NewEnd, prior(MergeTo)->end);
235
236 // Erase any dead ranges.
237 ranges.erase(llvm::next(I), MergeTo);
238
239 // If the newly formed range now touches the range after it and if they have
240 // the same value number, merge the two ranges into one range.
241 Ranges::iterator Next = llvm::next(I);
242 if (Next != ranges.end() && Next->start <= I->end && Next->valno == ValNo) {
243 I->end = Next->end;
244 ranges.erase(Next);
245 }
246 }
247
248
249 /// extendIntervalStartTo - This method is used when we want to extend the range
250 /// specified by I to start at the specified endpoint. To do this, we should
251 /// merge and eliminate all ranges that this will overlap with.
252 LiveInterval::Ranges::iterator
extendIntervalStartTo(Ranges::iterator I,SlotIndex NewStart)253 LiveInterval::extendIntervalStartTo(Ranges::iterator I, SlotIndex NewStart) {
254 assert(I != ranges.end() && "Not a valid interval!");
255 VNInfo *ValNo = I->valno;
256
257 // Search for the first interval that we can't merge with.
258 Ranges::iterator MergeTo = I;
259 do {
260 if (MergeTo == ranges.begin()) {
261 I->start = NewStart;
262 ranges.erase(MergeTo, I);
263 return I;
264 }
265 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
266 --MergeTo;
267 } while (NewStart <= MergeTo->start);
268
269 // If we start in the middle of another interval, just delete a range and
270 // extend that interval.
271 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
272 MergeTo->end = I->end;
273 } else {
274 // Otherwise, extend the interval right after.
275 ++MergeTo;
276 MergeTo->start = NewStart;
277 MergeTo->end = I->end;
278 }
279
280 ranges.erase(llvm::next(MergeTo), llvm::next(I));
281 return MergeTo;
282 }
283
284 LiveInterval::iterator
addRangeFrom(LiveRange LR,iterator From)285 LiveInterval::addRangeFrom(LiveRange LR, iterator From) {
286 SlotIndex Start = LR.start, End = LR.end;
287 iterator it = std::upper_bound(From, ranges.end(), Start);
288
289 // If the inserted interval starts in the middle or right at the end of
290 // another interval, just extend that interval to contain the range of LR.
291 if (it != ranges.begin()) {
292 iterator B = prior(it);
293 if (LR.valno == B->valno) {
294 if (B->start <= Start && B->end >= Start) {
295 extendIntervalEndTo(B, End);
296 return B;
297 }
298 } else {
299 // Check to make sure that we are not overlapping two live ranges with
300 // different valno's.
301 assert(B->end <= Start &&
302 "Cannot overlap two LiveRanges with differing ValID's"
303 " (did you def the same reg twice in a MachineInstr?)");
304 }
305 }
306
307 // Otherwise, if this range ends in the middle of, or right next to, another
308 // interval, merge it into that interval.
309 if (it != ranges.end()) {
310 if (LR.valno == it->valno) {
311 if (it->start <= End) {
312 it = extendIntervalStartTo(it, Start);
313
314 // If LR is a complete superset of an interval, we may need to grow its
315 // endpoint as well.
316 if (End > it->end)
317 extendIntervalEndTo(it, End);
318 return it;
319 }
320 } else {
321 // Check to make sure that we are not overlapping two live ranges with
322 // different valno's.
323 assert(it->start >= End &&
324 "Cannot overlap two LiveRanges with differing ValID's");
325 }
326 }
327
328 // Otherwise, this is just a new range that doesn't interact with anything.
329 // Insert it.
330 return ranges.insert(it, LR);
331 }
332
333 /// isInOneLiveRange - Return true if the range specified is entirely in
334 /// a single LiveRange of the live interval.
isInOneLiveRange(SlotIndex Start,SlotIndex End)335 bool LiveInterval::isInOneLiveRange(SlotIndex Start, SlotIndex End) {
336 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
337 if (I == ranges.begin())
338 return false;
339 --I;
340 return I->containsRange(Start, End);
341 }
342
343
344 /// removeRange - Remove the specified range from this interval. Note that
345 /// the range must be in a single LiveRange in its entirety.
removeRange(SlotIndex Start,SlotIndex End,bool RemoveDeadValNo)346 void LiveInterval::removeRange(SlotIndex Start, SlotIndex End,
347 bool RemoveDeadValNo) {
348 // Find the LiveRange containing this span.
349 Ranges::iterator I = std::upper_bound(ranges.begin(), ranges.end(), Start);
350 assert(I != ranges.begin() && "Range is not in interval!");
351 --I;
352 assert(I->containsRange(Start, End) && "Range is not entirely in interval!");
353
354 // If the span we are removing is at the start of the LiveRange, adjust it.
355 VNInfo *ValNo = I->valno;
356 if (I->start == Start) {
357 if (I->end == End) {
358 if (RemoveDeadValNo) {
359 // Check if val# is dead.
360 bool isDead = true;
361 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
362 if (II != I && II->valno == ValNo) {
363 isDead = false;
364 break;
365 }
366 if (isDead) {
367 // Now that ValNo is dead, remove it.
368 markValNoForDeletion(ValNo);
369 }
370 }
371
372 ranges.erase(I); // Removed the whole LiveRange.
373 } else
374 I->start = End;
375 return;
376 }
377
378 // Otherwise if the span we are removing is at the end of the LiveRange,
379 // adjust the other way.
380 if (I->end == End) {
381 I->end = Start;
382 return;
383 }
384
385 // Otherwise, we are splitting the LiveRange into two pieces.
386 SlotIndex OldEnd = I->end;
387 I->end = Start; // Trim the old interval.
388
389 // Insert the new one.
390 ranges.insert(llvm::next(I), LiveRange(End, OldEnd, ValNo));
391 }
392
393 /// removeValNo - Remove all the ranges defined by the specified value#.
394 /// Also remove the value# from value# list.
removeValNo(VNInfo * ValNo)395 void LiveInterval::removeValNo(VNInfo *ValNo) {
396 if (empty()) return;
397 Ranges::iterator I = ranges.end();
398 Ranges::iterator E = ranges.begin();
399 do {
400 --I;
401 if (I->valno == ValNo)
402 ranges.erase(I);
403 } while (I != E);
404 // Now that ValNo is dead, remove it.
405 markValNoForDeletion(ValNo);
406 }
407
408 /// getLiveRangeContaining - Return the live range that contains the
409 /// specified index, or null if there is none.
410 LiveInterval::const_iterator
FindLiveRangeContaining(SlotIndex Idx) const411 LiveInterval::FindLiveRangeContaining(SlotIndex Idx) const {
412 const_iterator It = std::upper_bound(begin(), end(), Idx);
413 if (It != ranges.begin()) {
414 --It;
415 if (It->contains(Idx))
416 return It;
417 }
418
419 return end();
420 }
421
422 LiveInterval::iterator
FindLiveRangeContaining(SlotIndex Idx)423 LiveInterval::FindLiveRangeContaining(SlotIndex Idx) {
424 iterator It = std::upper_bound(begin(), end(), Idx);
425 if (It != begin()) {
426 --It;
427 if (It->contains(Idx))
428 return It;
429 }
430
431 return end();
432 }
433
434 /// findDefinedVNInfo - Find the VNInfo defined by the specified
435 /// index (register interval).
findDefinedVNInfoForRegInt(SlotIndex Idx) const436 VNInfo *LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx) const {
437 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
438 i != e; ++i) {
439 if ((*i)->def == Idx)
440 return *i;
441 }
442
443 return 0;
444 }
445
446 /// findDefinedVNInfo - Find the VNInfo defined by the specified
447 /// register (stack inteval).
findDefinedVNInfoForStackInt(unsigned reg) const448 VNInfo *LiveInterval::findDefinedVNInfoForStackInt(unsigned reg) const {
449 for (LiveInterval::const_vni_iterator i = vni_begin(), e = vni_end();
450 i != e; ++i) {
451 if ((*i)->getReg() == reg)
452 return *i;
453 }
454 return 0;
455 }
456
457 /// join - Join two live intervals (this, and other) together. This applies
458 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
459 /// the intervals are not joinable, this aborts.
join(LiveInterval & Other,const int * LHSValNoAssignments,const int * RHSValNoAssignments,SmallVector<VNInfo *,16> & NewVNInfo,MachineRegisterInfo * MRI)460 void LiveInterval::join(LiveInterval &Other,
461 const int *LHSValNoAssignments,
462 const int *RHSValNoAssignments,
463 SmallVector<VNInfo*, 16> &NewVNInfo,
464 MachineRegisterInfo *MRI) {
465 // Determine if any of our live range values are mapped. This is uncommon, so
466 // we want to avoid the interval scan if not.
467 bool MustMapCurValNos = false;
468 unsigned NumVals = getNumValNums();
469 unsigned NumNewVals = NewVNInfo.size();
470 for (unsigned i = 0; i != NumVals; ++i) {
471 unsigned LHSValID = LHSValNoAssignments[i];
472 if (i != LHSValID ||
473 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i)))
474 MustMapCurValNos = true;
475 }
476
477 // If we have to apply a mapping to our base interval assignment, rewrite it
478 // now.
479 if (MustMapCurValNos) {
480 // Map the first live range.
481 iterator OutIt = begin();
482 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
483 ++OutIt;
484 for (iterator I = OutIt, E = end(); I != E; ++I) {
485 OutIt->valno = NewVNInfo[LHSValNoAssignments[I->valno->id]];
486
487 // If this live range has the same value # as its immediate predecessor,
488 // and if they are neighbors, remove one LiveRange. This happens when we
489 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
490 if (OutIt->valno == (OutIt-1)->valno && (OutIt-1)->end == OutIt->start) {
491 (OutIt-1)->end = OutIt->end;
492 } else {
493 if (I != OutIt) {
494 OutIt->start = I->start;
495 OutIt->end = I->end;
496 }
497
498 // Didn't merge, on to the next one.
499 ++OutIt;
500 }
501 }
502
503 // If we merge some live ranges, chop off the end.
504 ranges.erase(OutIt, end());
505 }
506
507 // Remember assignements because val# ids are changing.
508 SmallVector<unsigned, 16> OtherAssignments;
509 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I)
510 OtherAssignments.push_back(RHSValNoAssignments[I->valno->id]);
511
512 // Update val# info. Renumber them and make sure they all belong to this
513 // LiveInterval now. Also remove dead val#'s.
514 unsigned NumValNos = 0;
515 for (unsigned i = 0; i < NumNewVals; ++i) {
516 VNInfo *VNI = NewVNInfo[i];
517 if (VNI) {
518 if (NumValNos >= NumVals)
519 valnos.push_back(VNI);
520 else
521 valnos[NumValNos] = VNI;
522 VNI->id = NumValNos++; // Renumber val#.
523 }
524 }
525 if (NumNewVals < NumVals)
526 valnos.resize(NumNewVals); // shrinkify
527
528 // Okay, now insert the RHS live ranges into the LHS.
529 iterator InsertPos = begin();
530 unsigned RangeNo = 0;
531 for (iterator I = Other.begin(), E = Other.end(); I != E; ++I, ++RangeNo) {
532 // Map the valno in the other live range to the current live range.
533 I->valno = NewVNInfo[OtherAssignments[RangeNo]];
534 assert(I->valno && "Adding a dead range?");
535 InsertPos = addRangeFrom(*I, InsertPos);
536 }
537
538 ComputeJoinedWeight(Other);
539 }
540
541 /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
542 /// interval as the specified value number. The LiveRanges in RHS are
543 /// allowed to overlap with LiveRanges in the current interval, but only if
544 /// the overlapping LiveRanges have the specified value number.
MergeRangesInAsValue(const LiveInterval & RHS,VNInfo * LHSValNo)545 void LiveInterval::MergeRangesInAsValue(const LiveInterval &RHS,
546 VNInfo *LHSValNo) {
547 // TODO: Make this more efficient.
548 iterator InsertPos = begin();
549 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
550 // Map the valno in the other live range to the current live range.
551 LiveRange Tmp = *I;
552 Tmp.valno = LHSValNo;
553 InsertPos = addRangeFrom(Tmp, InsertPos);
554 }
555 }
556
557
558 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
559 /// in RHS into this live interval as the specified value number.
560 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
561 /// current interval, it will replace the value numbers of the overlaped
562 /// live ranges with the specified value number.
MergeValueInAsValue(const LiveInterval & RHS,const VNInfo * RHSValNo,VNInfo * LHSValNo)563 void LiveInterval::MergeValueInAsValue(
564 const LiveInterval &RHS,
565 const VNInfo *RHSValNo, VNInfo *LHSValNo) {
566 SmallVector<VNInfo*, 4> ReplacedValNos;
567 iterator IP = begin();
568 for (const_iterator I = RHS.begin(), E = RHS.end(); I != E; ++I) {
569 assert(I->valno == RHS.getValNumInfo(I->valno->id) && "Bad VNInfo");
570 if (I->valno != RHSValNo)
571 continue;
572 SlotIndex Start = I->start, End = I->end;
573 IP = std::upper_bound(IP, end(), Start);
574 // If the start of this range overlaps with an existing liverange, trim it.
575 if (IP != begin() && IP[-1].end > Start) {
576 if (IP[-1].valno != LHSValNo) {
577 ReplacedValNos.push_back(IP[-1].valno);
578 IP[-1].valno = LHSValNo; // Update val#.
579 }
580 Start = IP[-1].end;
581 // Trimmed away the whole range?
582 if (Start >= End) continue;
583 }
584 // If the end of this range overlaps with an existing liverange, trim it.
585 if (IP != end() && End > IP->start) {
586 if (IP->valno != LHSValNo) {
587 ReplacedValNos.push_back(IP->valno);
588 IP->valno = LHSValNo; // Update val#.
589 }
590 End = IP->start;
591 // If this trimmed away the whole range, ignore it.
592 if (Start == End) continue;
593 }
594
595 // Map the valno in the other live range to the current live range.
596 IP = addRangeFrom(LiveRange(Start, End, LHSValNo), IP);
597 }
598
599
600 SmallSet<VNInfo*, 4> Seen;
601 for (unsigned i = 0, e = ReplacedValNos.size(); i != e; ++i) {
602 VNInfo *V1 = ReplacedValNos[i];
603 if (Seen.insert(V1)) {
604 bool isDead = true;
605 for (const_iterator I = begin(), E = end(); I != E; ++I)
606 if (I->valno == V1) {
607 isDead = false;
608 break;
609 }
610 if (isDead) {
611 // Now that V1 is dead, remove it.
612 markValNoForDeletion(V1);
613 }
614 }
615 }
616 }
617
618
619 /// MergeInClobberRanges - For any live ranges that are not defined in the
620 /// current interval, but are defined in the Clobbers interval, mark them
621 /// used with an unknown definition value.
MergeInClobberRanges(LiveIntervals & li_,const LiveInterval & Clobbers,VNInfo::Allocator & VNInfoAllocator)622 void LiveInterval::MergeInClobberRanges(LiveIntervals &li_,
623 const LiveInterval &Clobbers,
624 VNInfo::Allocator &VNInfoAllocator) {
625 if (Clobbers.empty()) return;
626
627 DenseMap<VNInfo*, VNInfo*> ValNoMaps;
628 VNInfo *UnusedValNo = 0;
629 iterator IP = begin();
630 for (const_iterator I = Clobbers.begin(), E = Clobbers.end(); I != E; ++I) {
631 // For every val# in the Clobbers interval, create a new "unknown" val#.
632 VNInfo *ClobberValNo = 0;
633 DenseMap<VNInfo*, VNInfo*>::iterator VI = ValNoMaps.find(I->valno);
634 if (VI != ValNoMaps.end())
635 ClobberValNo = VI->second;
636 else if (UnusedValNo)
637 ClobberValNo = UnusedValNo;
638 else {
639 UnusedValNo = ClobberValNo =
640 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
641 ValNoMaps.insert(std::make_pair(I->valno, ClobberValNo));
642 }
643
644 bool Done = false;
645 SlotIndex Start = I->start, End = I->end;
646 // If a clobber range starts before an existing range and ends after
647 // it, the clobber range will need to be split into multiple ranges.
648 // Loop until the entire clobber range is handled.
649 while (!Done) {
650 Done = true;
651 IP = std::upper_bound(IP, end(), Start);
652 SlotIndex SubRangeStart = Start;
653 SlotIndex SubRangeEnd = End;
654
655 // If the start of this range overlaps with an existing liverange, trim it.
656 if (IP != begin() && IP[-1].end > SubRangeStart) {
657 SubRangeStart = IP[-1].end;
658 // Trimmed away the whole range?
659 if (SubRangeStart >= SubRangeEnd) continue;
660 }
661 // If the end of this range overlaps with an existing liverange, trim it.
662 if (IP != end() && SubRangeEnd > IP->start) {
663 // If the clobber live range extends beyond the existing live range,
664 // it'll need at least another live range, so set the flag to keep
665 // iterating.
666 if (SubRangeEnd > IP->end) {
667 Start = IP->end;
668 Done = false;
669 }
670 SubRangeEnd = IP->start;
671 // If this trimmed away the whole range, ignore it.
672 if (SubRangeStart == SubRangeEnd) continue;
673 }
674
675 // Insert the clobber interval.
676 IP = addRangeFrom(LiveRange(SubRangeStart, SubRangeEnd, ClobberValNo),
677 IP);
678 UnusedValNo = 0;
679 }
680 }
681
682 if (UnusedValNo) {
683 // Delete the last unused val#.
684 valnos.pop_back();
685 }
686 }
687
MergeInClobberRange(LiveIntervals & li_,SlotIndex Start,SlotIndex End,VNInfo::Allocator & VNInfoAllocator)688 void LiveInterval::MergeInClobberRange(LiveIntervals &li_,
689 SlotIndex Start,
690 SlotIndex End,
691 VNInfo::Allocator &VNInfoAllocator) {
692 // Find a value # to use for the clobber ranges. If there is already a value#
693 // for unknown values, use it.
694 VNInfo *ClobberValNo =
695 getNextValue(li_.getInvalidIndex(), 0, false, VNInfoAllocator);
696
697 iterator IP = begin();
698 IP = std::upper_bound(IP, end(), Start);
699
700 // If the start of this range overlaps with an existing liverange, trim it.
701 if (IP != begin() && IP[-1].end > Start) {
702 Start = IP[-1].end;
703 // Trimmed away the whole range?
704 if (Start >= End) return;
705 }
706 // If the end of this range overlaps with an existing liverange, trim it.
707 if (IP != end() && End > IP->start) {
708 End = IP->start;
709 // If this trimmed away the whole range, ignore it.
710 if (Start == End) return;
711 }
712
713 // Insert the clobber interval.
714 addRangeFrom(LiveRange(Start, End, ClobberValNo), IP);
715 }
716
717 /// MergeValueNumberInto - This method is called when two value nubmers
718 /// are found to be equivalent. This eliminates V1, replacing all
719 /// LiveRanges with the V1 value number with the V2 value number. This can
720 /// cause merging of V1/V2 values numbers and compaction of the value space.
MergeValueNumberInto(VNInfo * V1,VNInfo * V2)721 VNInfo* LiveInterval::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
722 assert(V1 != V2 && "Identical value#'s are always equivalent!");
723
724 // This code actually merges the (numerically) larger value number into the
725 // smaller value number, which is likely to allow us to compactify the value
726 // space. The only thing we have to be careful of is to preserve the
727 // instruction that defines the result value.
728
729 // Make sure V2 is smaller than V1.
730 if (V1->id < V2->id) {
731 V1->copyFrom(*V2);
732 std::swap(V1, V2);
733 }
734
735 // Merge V1 live ranges into V2.
736 for (iterator I = begin(); I != end(); ) {
737 iterator LR = I++;
738 if (LR->valno != V1) continue; // Not a V1 LiveRange.
739
740 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
741 // range, extend it.
742 if (LR != begin()) {
743 iterator Prev = LR-1;
744 if (Prev->valno == V2 && Prev->end == LR->start) {
745 Prev->end = LR->end;
746
747 // Erase this live-range.
748 ranges.erase(LR);
749 I = Prev+1;
750 LR = Prev;
751 }
752 }
753
754 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
755 // Ensure that it is a V2 live-range.
756 LR->valno = V2;
757
758 // If we can merge it into later V2 live ranges, do so now. We ignore any
759 // following V1 live ranges, as they will be merged in subsequent iterations
760 // of the loop.
761 if (I != end()) {
762 if (I->start == LR->end && I->valno == V2) {
763 LR->end = I->end;
764 ranges.erase(I);
765 I = LR+1;
766 }
767 }
768 }
769
770 // Now that V1 is dead, remove it.
771 markValNoForDeletion(V1);
772
773 return V2;
774 }
775
Copy(const LiveInterval & RHS,MachineRegisterInfo * MRI,VNInfo::Allocator & VNInfoAllocator)776 void LiveInterval::Copy(const LiveInterval &RHS,
777 MachineRegisterInfo *MRI,
778 VNInfo::Allocator &VNInfoAllocator) {
779 ranges.clear();
780 valnos.clear();
781 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(RHS.reg);
782 MRI->setRegAllocationHint(reg, Hint.first, Hint.second);
783
784 weight = RHS.weight;
785 for (unsigned i = 0, e = RHS.getNumValNums(); i != e; ++i) {
786 const VNInfo *VNI = RHS.getValNumInfo(i);
787 createValueCopy(VNI, VNInfoAllocator);
788 }
789 for (unsigned i = 0, e = RHS.ranges.size(); i != e; ++i) {
790 const LiveRange &LR = RHS.ranges[i];
791 addRange(LiveRange(LR.start, LR.end, getValNumInfo(LR.valno->id)));
792 }
793 }
794
getSize() const795 unsigned LiveInterval::getSize() const {
796 unsigned Sum = 0;
797 for (const_iterator I = begin(), E = end(); I != E; ++I)
798 Sum += I->start.distance(I->end);
799 return Sum;
800 }
801
802 /// ComputeJoinedWeight - Set the weight of a live interval Joined
803 /// after Other has been merged into it.
ComputeJoinedWeight(const LiveInterval & Other)804 void LiveInterval::ComputeJoinedWeight(const LiveInterval &Other) {
805 // If either of these intervals was spilled, the weight is the
806 // weight of the non-spilled interval. This can only happen with
807 // iterative coalescers.
808
809 if (Other.weight != HUGE_VALF) {
810 weight += Other.weight;
811 }
812 else if (weight == HUGE_VALF &&
813 !TargetRegisterInfo::isPhysicalRegister(reg)) {
814 // Remove this assert if you have an iterative coalescer
815 assert(0 && "Joining to spilled interval");
816 weight = Other.weight;
817 }
818 else {
819 // Otherwise the weight stays the same
820 // Remove this assert if you have an iterative coalescer
821 assert(0 && "Joining from spilled interval");
822 }
823 }
824
operator <<(raw_ostream & os,const LiveRange & LR)825 raw_ostream& llvm::operator<<(raw_ostream& os, const LiveRange &LR) {
826 return os << '[' << LR.start << ',' << LR.end << ':' << LR.valno->id << ")";
827 }
828
dump() const829 void LiveRange::dump() const {
830 dbgs() << *this << "\n";
831 }
832
print(raw_ostream & OS,const TargetRegisterInfo * TRI) const833 void LiveInterval::print(raw_ostream &OS, const TargetRegisterInfo *TRI) const {
834 if (isStackSlot())
835 OS << "SS#" << getStackSlotIndex();
836 else if (TRI && TargetRegisterInfo::isPhysicalRegister(reg))
837 OS << TRI->getName(reg);
838 else
839 OS << "%reg" << reg;
840
841 OS << ',' << weight;
842
843 if (empty())
844 OS << " EMPTY";
845 else {
846 OS << " = ";
847 for (LiveInterval::Ranges::const_iterator I = ranges.begin(),
848 E = ranges.end(); I != E; ++I) {
849 OS << *I;
850 assert(I->valno == getValNumInfo(I->valno->id) && "Bad VNInfo");
851 }
852 }
853
854 // Print value number info.
855 if (getNumValNums()) {
856 OS << " ";
857 unsigned vnum = 0;
858 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
859 ++i, ++vnum) {
860 const VNInfo *vni = *i;
861 if (vnum) OS << " ";
862 OS << vnum << "@";
863 if (vni->isUnused()) {
864 OS << "x";
865 } else {
866 if (!vni->isDefAccurate() && !vni->isPHIDef())
867 OS << "?";
868 else
869 OS << vni->def;
870 if (vni->hasPHIKill())
871 OS << "-phikill";
872 if (vni->hasRedefByEC())
873 OS << "-ec";
874 }
875 }
876 }
877 }
878
dump() const879 void LiveInterval::dump() const {
880 dbgs() << *this << "\n";
881 }
882
883
print(raw_ostream & os) const884 void LiveRange::print(raw_ostream &os) const {
885 os << *this;
886 }
887