1 //===-- LiveInterval.cpp - Live Interval Representation -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
19 //===----------------------------------------------------------------------===//
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"
33 // CompEnd - Compare LiveRange ends.
36 bool operator()(const LiveRange
&A
, const LiveRange
&B
) const {
42 LiveInterval::iterator
LiveInterval::find(SlotIndex Pos
) {
43 assert(Pos
.isValid() && "Cannot search for an invalid index");
44 return std::upper_bound(begin(), end(), LiveRange(SlotIndex(), Pos
, 0),
48 /// killedInRange - Return true if the interval has kills in [Start,End).
49 bool LiveInterval::killedInRange(SlotIndex Start
, SlotIndex End
) const {
50 Ranges::const_iterator r
=
51 std::lower_bound(ranges
.begin(), ranges
.end(), End
);
53 // Now r points to the first interval with start >= End, or ranges.end().
54 if (r
== ranges
.begin())
58 // Now r points to the last interval with end <= End.
59 // r->end is the kill point.
60 return r
->end
>= Start
&& r
->end
< End
;
63 // overlaps - Return true if the intersection of the two live intervals is
66 // An example for overlaps():
70 // 8: C = A + B ;; last use of A
72 // The live intervals should look like:
78 // A->overlaps(C) should return false since we want to be able to join
81 bool LiveInterval::overlapsFrom(const LiveInterval
& other
,
82 const_iterator StartPos
) const {
83 assert(!empty() && "empty interval");
84 const_iterator i
= begin();
85 const_iterator ie
= end();
86 const_iterator j
= StartPos
;
87 const_iterator je
= other
.end();
89 assert((StartPos
->start
<= i
->start
|| StartPos
== other
.begin()) &&
90 StartPos
!= other
.end() && "Bogus start position hint!");
92 if (i
->start
< j
->start
) {
93 i
= std::upper_bound(i
, ie
, j
->start
);
94 if (i
!= ranges
.begin()) --i
;
95 } else if (j
->start
< i
->start
) {
97 if (StartPos
!= other
.end() && StartPos
->start
<= i
->start
) {
98 assert(StartPos
< other
.end() && i
< end());
99 j
= std::upper_bound(j
, je
, i
->start
);
100 if (j
!= other
.ranges
.begin()) --j
;
106 if (j
== je
) return false;
109 if (i
->start
> j
->start
) {
114 if (i
->end
> j
->start
)
122 /// overlaps - Return true if the live interval overlaps a range specified
124 bool LiveInterval::overlaps(SlotIndex Start
, SlotIndex End
) const {
125 assert(Start
< End
&& "Invalid range");
126 const_iterator I
= std::lower_bound(begin(), end(), End
);
127 return I
!= begin() && (--I
)->end
> Start
;
131 /// ValNo is dead, remove it. If it is the largest value number, just nuke it
132 /// (and any other deleted values neighboring it), otherwise mark it as ~1U so
133 /// it can be nuked later.
134 void LiveInterval::markValNoForDeletion(VNInfo
*ValNo
) {
135 if (ValNo
->id
== getNumValNums()-1) {
138 } while (!valnos
.empty() && valnos
.back()->isUnused());
140 ValNo
->setIsUnused(true);
144 /// RenumberValues - Renumber all values in order of appearance and delete the
145 /// remaining unused values.
146 void LiveInterval::RenumberValues(LiveIntervals
&lis
) {
147 SmallPtrSet
<VNInfo
*, 8> Seen
;
148 bool seenPHIDef
= false;
150 for (const_iterator I
= begin(), E
= end(); I
!= E
; ++I
) {
151 VNInfo
*VNI
= I
->valno
;
152 if (!Seen
.insert(VNI
))
154 assert(!VNI
->isUnused() && "Unused valno used by live range");
155 VNI
->id
= (unsigned)valnos
.size();
156 valnos
.push_back(VNI
);
157 VNI
->setHasPHIKill(false);
162 // Recompute phi kill flags.
165 for (const_vni_iterator I
= vni_begin(), E
= vni_end(); I
!= E
; ++I
) {
167 if (!VNI
->isPHIDef())
169 const MachineBasicBlock
*PHIBB
= lis
.getMBBFromIndex(VNI
->def
);
170 assert(PHIBB
&& "No basic block for phi-def");
171 for (MachineBasicBlock::const_pred_iterator PI
= PHIBB
->pred_begin(),
172 PE
= PHIBB
->pred_end(); PI
!= PE
; ++PI
) {
173 VNInfo
*KVNI
= getVNInfoAt(lis
.getMBBEndIdx(*PI
).getPrevSlot());
175 KVNI
->setHasPHIKill(true);
180 /// extendIntervalEndTo - This method is used when we want to extend the range
181 /// specified by I to end at the specified endpoint. To do this, we should
182 /// merge and eliminate all ranges that this will overlap with. The iterator is
184 void LiveInterval::extendIntervalEndTo(Ranges::iterator I
, SlotIndex NewEnd
) {
185 assert(I
!= ranges
.end() && "Not a valid interval!");
186 VNInfo
*ValNo
= I
->valno
;
188 // Search for the first interval that we can't merge with.
189 Ranges::iterator MergeTo
= llvm::next(I
);
190 for (; MergeTo
!= ranges
.end() && NewEnd
>= MergeTo
->end
; ++MergeTo
) {
191 assert(MergeTo
->valno
== ValNo
&& "Cannot merge with differing values!");
194 // If NewEnd was in the middle of an interval, make sure to get its endpoint.
195 I
->end
= std::max(NewEnd
, prior(MergeTo
)->end
);
197 // Erase any dead ranges.
198 ranges
.erase(llvm::next(I
), MergeTo
);
200 // If the newly formed range now touches the range after it and if they have
201 // the same value number, merge the two ranges into one range.
202 Ranges::iterator Next
= llvm::next(I
);
203 if (Next
!= ranges
.end() && Next
->start
<= I
->end
&& Next
->valno
== ValNo
) {
210 /// extendIntervalStartTo - This method is used when we want to extend the range
211 /// specified by I to start at the specified endpoint. To do this, we should
212 /// merge and eliminate all ranges that this will overlap with.
213 LiveInterval::Ranges::iterator
214 LiveInterval::extendIntervalStartTo(Ranges::iterator I
, SlotIndex NewStart
) {
215 assert(I
!= ranges
.end() && "Not a valid interval!");
216 VNInfo
*ValNo
= I
->valno
;
218 // Search for the first interval that we can't merge with.
219 Ranges::iterator MergeTo
= I
;
221 if (MergeTo
== ranges
.begin()) {
223 ranges
.erase(MergeTo
, I
);
226 assert(MergeTo
->valno
== ValNo
&& "Cannot merge with differing values!");
228 } while (NewStart
<= MergeTo
->start
);
230 // If we start in the middle of another interval, just delete a range and
231 // extend that interval.
232 if (MergeTo
->end
>= NewStart
&& MergeTo
->valno
== ValNo
) {
233 MergeTo
->end
= I
->end
;
235 // Otherwise, extend the interval right after.
237 MergeTo
->start
= NewStart
;
238 MergeTo
->end
= I
->end
;
241 ranges
.erase(llvm::next(MergeTo
), llvm::next(I
));
245 LiveInterval::iterator
246 LiveInterval::addRangeFrom(LiveRange LR
, iterator From
) {
247 SlotIndex Start
= LR
.start
, End
= LR
.end
;
248 iterator it
= std::upper_bound(From
, ranges
.end(), Start
);
250 // If the inserted interval starts in the middle or right at the end of
251 // another interval, just extend that interval to contain the range of LR.
252 if (it
!= ranges
.begin()) {
253 iterator B
= prior(it
);
254 if (LR
.valno
== B
->valno
) {
255 if (B
->start
<= Start
&& B
->end
>= Start
) {
256 extendIntervalEndTo(B
, End
);
260 // Check to make sure that we are not overlapping two live ranges with
261 // different valno's.
262 assert(B
->end
<= Start
&&
263 "Cannot overlap two LiveRanges with differing ValID's"
264 " (did you def the same reg twice in a MachineInstr?)");
268 // Otherwise, if this range ends in the middle of, or right next to, another
269 // interval, merge it into that interval.
270 if (it
!= ranges
.end()) {
271 if (LR
.valno
== it
->valno
) {
272 if (it
->start
<= End
) {
273 it
= extendIntervalStartTo(it
, Start
);
275 // If LR is a complete superset of an interval, we may need to grow its
278 extendIntervalEndTo(it
, End
);
282 // Check to make sure that we are not overlapping two live ranges with
283 // different valno's.
284 assert(it
->start
>= End
&&
285 "Cannot overlap two LiveRanges with differing ValID's");
289 // Otherwise, this is just a new range that doesn't interact with anything.
291 return ranges
.insert(it
, LR
);
295 /// removeRange - Remove the specified range from this interval. Note that
296 /// the range must be in a single LiveRange in its entirety.
297 void LiveInterval::removeRange(SlotIndex Start
, SlotIndex End
,
298 bool RemoveDeadValNo
) {
299 // Find the LiveRange containing this span.
300 Ranges::iterator I
= find(Start
);
301 assert(I
!= ranges
.end() && "Range is not in interval!");
302 assert(I
->containsRange(Start
, End
) && "Range is not entirely in interval!");
304 // If the span we are removing is at the start of the LiveRange, adjust it.
305 VNInfo
*ValNo
= I
->valno
;
306 if (I
->start
== Start
) {
308 if (RemoveDeadValNo
) {
309 // Check if val# is dead.
311 for (const_iterator II
= begin(), EE
= end(); II
!= EE
; ++II
)
312 if (II
!= I
&& II
->valno
== ValNo
) {
317 // Now that ValNo is dead, remove it.
318 markValNoForDeletion(ValNo
);
322 ranges
.erase(I
); // Removed the whole LiveRange.
328 // Otherwise if the span we are removing is at the end of the LiveRange,
329 // adjust the other way.
335 // Otherwise, we are splitting the LiveRange into two pieces.
336 SlotIndex OldEnd
= I
->end
;
337 I
->end
= Start
; // Trim the old interval.
339 // Insert the new one.
340 ranges
.insert(llvm::next(I
), LiveRange(End
, OldEnd
, ValNo
));
343 /// removeValNo - Remove all the ranges defined by the specified value#.
344 /// Also remove the value# from value# list.
345 void LiveInterval::removeValNo(VNInfo
*ValNo
) {
347 Ranges::iterator I
= ranges
.end();
348 Ranges::iterator E
= ranges
.begin();
351 if (I
->valno
== ValNo
)
354 // Now that ValNo is dead, remove it.
355 markValNoForDeletion(ValNo
);
358 /// findDefinedVNInfo - Find the VNInfo defined by the specified
359 /// index (register interval).
360 VNInfo
*LiveInterval::findDefinedVNInfoForRegInt(SlotIndex Idx
) const {
361 for (LiveInterval::const_vni_iterator i
= vni_begin(), e
= vni_end();
363 if ((*i
)->def
== Idx
)
370 /// join - Join two live intervals (this, and other) together. This applies
371 /// mappings to the value numbers in the LHS/RHS intervals as specified. If
372 /// the intervals are not joinable, this aborts.
373 void LiveInterval::join(LiveInterval
&Other
,
374 const int *LHSValNoAssignments
,
375 const int *RHSValNoAssignments
,
376 SmallVector
<VNInfo
*, 16> &NewVNInfo
,
377 MachineRegisterInfo
*MRI
) {
378 // Determine if any of our live range values are mapped. This is uncommon, so
379 // we want to avoid the interval scan if not.
380 bool MustMapCurValNos
= false;
381 unsigned NumVals
= getNumValNums();
382 unsigned NumNewVals
= NewVNInfo
.size();
383 for (unsigned i
= 0; i
!= NumVals
; ++i
) {
384 unsigned LHSValID
= LHSValNoAssignments
[i
];
386 (NewVNInfo
[LHSValID
] && NewVNInfo
[LHSValID
] != getValNumInfo(i
)))
387 MustMapCurValNos
= true;
390 // If we have to apply a mapping to our base interval assignment, rewrite it
392 if (MustMapCurValNos
) {
393 // Map the first live range.
394 iterator OutIt
= begin();
395 OutIt
->valno
= NewVNInfo
[LHSValNoAssignments
[OutIt
->valno
->id
]];
397 for (iterator I
= OutIt
, E
= end(); I
!= E
; ++I
) {
398 OutIt
->valno
= NewVNInfo
[LHSValNoAssignments
[I
->valno
->id
]];
400 // If this live range has the same value # as its immediate predecessor,
401 // and if they are neighbors, remove one LiveRange. This happens when we
402 // have [0,3:0)[4,7:1) and map 0/1 onto the same value #.
403 if (OutIt
->valno
== (OutIt
-1)->valno
&& (OutIt
-1)->end
== OutIt
->start
) {
404 (OutIt
-1)->end
= OutIt
->end
;
407 OutIt
->start
= I
->start
;
411 // Didn't merge, on to the next one.
416 // If we merge some live ranges, chop off the end.
417 ranges
.erase(OutIt
, end());
420 // Remember assignements because val# ids are changing.
421 SmallVector
<unsigned, 16> OtherAssignments
;
422 for (iterator I
= Other
.begin(), E
= Other
.end(); I
!= E
; ++I
)
423 OtherAssignments
.push_back(RHSValNoAssignments
[I
->valno
->id
]);
425 // Update val# info. Renumber them and make sure they all belong to this
426 // LiveInterval now. Also remove dead val#'s.
427 unsigned NumValNos
= 0;
428 for (unsigned i
= 0; i
< NumNewVals
; ++i
) {
429 VNInfo
*VNI
= NewVNInfo
[i
];
431 if (NumValNos
>= NumVals
)
432 valnos
.push_back(VNI
);
434 valnos
[NumValNos
] = VNI
;
435 VNI
->id
= NumValNos
++; // Renumber val#.
438 if (NumNewVals
< NumVals
)
439 valnos
.resize(NumNewVals
); // shrinkify
441 // Okay, now insert the RHS live ranges into the LHS.
442 iterator InsertPos
= begin();
443 unsigned RangeNo
= 0;
444 for (iterator I
= Other
.begin(), E
= Other
.end(); I
!= E
; ++I
, ++RangeNo
) {
445 // Map the valno in the other live range to the current live range.
446 I
->valno
= NewVNInfo
[OtherAssignments
[RangeNo
]];
447 assert(I
->valno
&& "Adding a dead range?");
448 InsertPos
= addRangeFrom(*I
, InsertPos
);
451 ComputeJoinedWeight(Other
);
454 /// MergeRangesInAsValue - Merge all of the intervals in RHS into this live
455 /// interval as the specified value number. The LiveRanges in RHS are
456 /// allowed to overlap with LiveRanges in the current interval, but only if
457 /// the overlapping LiveRanges have the specified value number.
458 void LiveInterval::MergeRangesInAsValue(const LiveInterval
&RHS
,
460 // TODO: Make this more efficient.
461 iterator InsertPos
= begin();
462 for (const_iterator I
= RHS
.begin(), E
= RHS
.end(); I
!= E
; ++I
) {
463 // Map the valno in the other live range to the current live range.
465 Tmp
.valno
= LHSValNo
;
466 InsertPos
= addRangeFrom(Tmp
, InsertPos
);
471 /// MergeValueInAsValue - Merge all of the live ranges of a specific val#
472 /// in RHS into this live interval as the specified value number.
473 /// The LiveRanges in RHS are allowed to overlap with LiveRanges in the
474 /// current interval, it will replace the value numbers of the overlaped
475 /// live ranges with the specified value number.
476 void LiveInterval::MergeValueInAsValue(
477 const LiveInterval
&RHS
,
478 const VNInfo
*RHSValNo
, VNInfo
*LHSValNo
) {
479 SmallVector
<VNInfo
*, 4> ReplacedValNos
;
480 iterator IP
= begin();
481 for (const_iterator I
= RHS
.begin(), E
= RHS
.end(); I
!= E
; ++I
) {
482 assert(I
->valno
== RHS
.getValNumInfo(I
->valno
->id
) && "Bad VNInfo");
483 if (I
->valno
!= RHSValNo
)
485 SlotIndex Start
= I
->start
, End
= I
->end
;
486 IP
= std::upper_bound(IP
, end(), Start
);
487 // If the start of this range overlaps with an existing liverange, trim it.
488 if (IP
!= begin() && IP
[-1].end
> Start
) {
489 if (IP
[-1].valno
!= LHSValNo
) {
490 ReplacedValNos
.push_back(IP
[-1].valno
);
491 IP
[-1].valno
= LHSValNo
; // Update val#.
494 // Trimmed away the whole range?
495 if (Start
>= End
) continue;
497 // If the end of this range overlaps with an existing liverange, trim it.
498 if (IP
!= end() && End
> IP
->start
) {
499 if (IP
->valno
!= LHSValNo
) {
500 ReplacedValNos
.push_back(IP
->valno
);
501 IP
->valno
= LHSValNo
; // Update val#.
504 // If this trimmed away the whole range, ignore it.
505 if (Start
== End
) continue;
508 // Map the valno in the other live range to the current live range.
509 IP
= addRangeFrom(LiveRange(Start
, End
, LHSValNo
), IP
);
513 SmallSet
<VNInfo
*, 4> Seen
;
514 for (unsigned i
= 0, e
= ReplacedValNos
.size(); i
!= e
; ++i
) {
515 VNInfo
*V1
= ReplacedValNos
[i
];
516 if (Seen
.insert(V1
)) {
518 for (const_iterator I
= begin(), E
= end(); I
!= E
; ++I
)
519 if (I
->valno
== V1
) {
524 // Now that V1 is dead, remove it.
525 markValNoForDeletion(V1
);
533 /// MergeValueNumberInto - This method is called when two value nubmers
534 /// are found to be equivalent. This eliminates V1, replacing all
535 /// LiveRanges with the V1 value number with the V2 value number. This can
536 /// cause merging of V1/V2 values numbers and compaction of the value space.
537 VNInfo
* LiveInterval::MergeValueNumberInto(VNInfo
*V1
, VNInfo
*V2
) {
538 assert(V1
!= V2
&& "Identical value#'s are always equivalent!");
540 // This code actually merges the (numerically) larger value number into the
541 // smaller value number, which is likely to allow us to compactify the value
542 // space. The only thing we have to be careful of is to preserve the
543 // instruction that defines the result value.
545 // Make sure V2 is smaller than V1.
546 if (V1
->id
< V2
->id
) {
551 // Merge V1 live ranges into V2.
552 for (iterator I
= begin(); I
!= end(); ) {
554 if (LR
->valno
!= V1
) continue; // Not a V1 LiveRange.
556 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
559 iterator Prev
= LR
-1;
560 if (Prev
->valno
== V2
&& Prev
->end
== LR
->start
) {
563 // Erase this live-range.
570 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
571 // Ensure that it is a V2 live-range.
574 // If we can merge it into later V2 live ranges, do so now. We ignore any
575 // following V1 live ranges, as they will be merged in subsequent iterations
578 if (I
->start
== LR
->end
&& I
->valno
== V2
) {
586 // Merge the relevant flags.
589 // Now that V1 is dead, remove it.
590 markValNoForDeletion(V1
);
595 void LiveInterval::Copy(const LiveInterval
&RHS
,
596 MachineRegisterInfo
*MRI
,
597 VNInfo::Allocator
&VNInfoAllocator
) {
600 std::pair
<unsigned, unsigned> Hint
= MRI
->getRegAllocationHint(RHS
.reg
);
601 MRI
->setRegAllocationHint(reg
, Hint
.first
, Hint
.second
);
604 for (unsigned i
= 0, e
= RHS
.getNumValNums(); i
!= e
; ++i
) {
605 const VNInfo
*VNI
= RHS
.getValNumInfo(i
);
606 createValueCopy(VNI
, VNInfoAllocator
);
608 for (unsigned i
= 0, e
= RHS
.ranges
.size(); i
!= e
; ++i
) {
609 const LiveRange
&LR
= RHS
.ranges
[i
];
610 addRange(LiveRange(LR
.start
, LR
.end
, getValNumInfo(LR
.valno
->id
)));
614 unsigned LiveInterval::getSize() const {
616 for (const_iterator I
= begin(), E
= end(); I
!= E
; ++I
)
617 Sum
+= I
->start
.distance(I
->end
);
621 /// ComputeJoinedWeight - Set the weight of a live interval Joined
622 /// after Other has been merged into it.
623 void LiveInterval::ComputeJoinedWeight(const LiveInterval
&Other
) {
624 // If either of these intervals was spilled, the weight is the
625 // weight of the non-spilled interval. This can only happen with
626 // iterative coalescers.
628 if (Other
.weight
!= HUGE_VALF
) {
629 weight
+= Other
.weight
;
631 else if (weight
== HUGE_VALF
&&
632 !TargetRegisterInfo::isPhysicalRegister(reg
)) {
633 // Remove this assert if you have an iterative coalescer
634 assert(0 && "Joining to spilled interval");
635 weight
= Other
.weight
;
638 // Otherwise the weight stays the same
639 // Remove this assert if you have an iterative coalescer
640 assert(0 && "Joining from spilled interval");
644 raw_ostream
& llvm::operator<<(raw_ostream
& os
, const LiveRange
&LR
) {
645 return os
<< '[' << LR
.start
<< ',' << LR
.end
<< ':' << LR
.valno
->id
<< ")";
648 void LiveRange::dump() const {
649 dbgs() << *this << "\n";
652 void LiveInterval::print(raw_ostream
&OS
, const TargetRegisterInfo
*TRI
) const {
654 OS
<< "SS#" << getStackSlotIndex();
655 else if (TRI
&& TargetRegisterInfo::isPhysicalRegister(reg
))
656 OS
<< TRI
->getName(reg
);
666 for (LiveInterval::Ranges::const_iterator I
= ranges
.begin(),
667 E
= ranges
.end(); I
!= E
; ++I
) {
669 assert(I
->valno
== getValNumInfo(I
->valno
->id
) && "Bad VNInfo");
673 // Print value number info.
674 if (getNumValNums()) {
677 for (const_vni_iterator i
= vni_begin(), e
= vni_end(); i
!= e
;
679 const VNInfo
*vni
= *i
;
682 if (vni
->isUnused()) {
688 if (vni
->hasPHIKill())
690 if (vni
->hasRedefByEC())
697 void LiveInterval::dump() const {
698 dbgs() << *this << "\n";
702 void LiveRange::print(raw_ostream
&os
) const {
706 /// ConnectedVNInfoEqClasses - Helper class that can divide VNInfos in a
707 /// LiveInterval into equivalence clases of connected components. A
708 /// LiveInterval that has multiple connected components can be broken into
709 /// multiple LiveIntervals.
711 void ConnectedVNInfoEqClasses::Connect(unsigned a
, unsigned b
) {
712 while (eqClass_
[a
] != eqClass_
[b
]) {
713 if (eqClass_
[a
] > eqClass_
[b
])
715 unsigned t
= eqClass_
[b
];
716 assert(t
<= b
&& "Invariant broken");
717 eqClass_
[b
] = eqClass_
[a
];
722 unsigned ConnectedVNInfoEqClasses::Renumber() {
723 // Assign final class numbers.
724 // We use the fact that eqClass_[i] == i for class leaders.
725 // For others, eqClass_[i] points to an earlier value in the same class.
727 for (unsigned i
= 0, e
= eqClass_
.size(); i
!= e
; ++i
) {
728 unsigned q
= eqClass_
[i
];
729 assert(q
<= i
&& "Invariant broken");
730 eqClass_
[i
] = q
== i
? count
++ : eqClass_
[q
];
736 unsigned ConnectedVNInfoEqClasses::Classify(const LiveInterval
*LI
) {
737 // Create initial equivalence classes.
739 eqClass_
.reserve(LI
->getNumValNums());
740 for (unsigned i
= 0, e
= LI
->getNumValNums(); i
!= e
; ++i
)
741 eqClass_
.push_back(i
);
743 const VNInfo
*used
= 0, *unused
= 0;
745 // Determine connections.
746 for (LiveInterval::const_vni_iterator I
= LI
->vni_begin(), E
= LI
->vni_end();
748 const VNInfo
*VNI
= *I
;
749 // Group all unused values into one class.
750 if (VNI
->isUnused()) {
752 Connect(unused
->id
, VNI
->id
);
757 if (VNI
->isPHIDef()) {
758 const MachineBasicBlock
*MBB
= lis_
.getMBBFromIndex(VNI
->def
);
759 assert(MBB
&& "Phi-def has no defining MBB");
760 // Connect to values live out of predecessors.
761 for (MachineBasicBlock::const_pred_iterator PI
= MBB
->pred_begin(),
762 PE
= MBB
->pred_end(); PI
!= PE
; ++PI
)
763 if (const VNInfo
*PVNI
=
764 LI
->getVNInfoAt(lis_
.getMBBEndIdx(*PI
).getPrevSlot()))
765 Connect(VNI
->id
, PVNI
->id
);
767 // Normal value defined by an instruction. Check for two-addr redef.
768 // FIXME: This could be coincidental. Should we really check for a tied
769 // operand constraint?
770 if (const VNInfo
*UVNI
= LI
->getVNInfoAt(VNI
->def
.getUseIndex()))
771 Connect(VNI
->id
, UVNI
->id
);
775 // Lump all the unused values in with the last used value.
777 Connect(used
->id
, unused
->id
);
782 void ConnectedVNInfoEqClasses::Distribute(LiveInterval
*LIV
[]) {
783 assert(LIV
[0] && "LIV[0] must be set");
784 LiveInterval
&LI
= *LIV
[0];
785 // Check that they likely ran Classify() on LIV[0] first.
786 assert(eqClass_
.size() == LI
.getNumValNums() && "Bad classification data");
788 // First move runs to new intervals.
789 LiveInterval::iterator J
= LI
.begin(), E
= LI
.end();
790 while (J
!= E
&& eqClass_
[J
->valno
->id
] == 0)
792 for (LiveInterval::iterator I
= J
; I
!= E
; ++I
) {
793 if (unsigned eq
= eqClass_
[I
->valno
->id
]) {
794 assert((LIV
[eq
]->empty() || LIV
[eq
]->expiredAt(I
->start
)) &&
795 "New intervals should be empty");
796 LIV
[eq
]->ranges
.push_back(*I
);
800 LI
.ranges
.erase(J
, E
);
802 // Transfer VNInfos to their new owners and renumber them.
803 unsigned j
= 0, e
= LI
.getNumValNums();
804 while (j
!= e
&& eqClass_
[j
] == 0)
806 for (unsigned i
= j
; i
!= e
; ++i
) {
807 VNInfo
*VNI
= LI
.getValNumInfo(i
);
808 if (unsigned eq
= eqClass_
[i
]) {
809 VNI
->id
= LIV
[eq
]->getNumValNums();
810 LIV
[eq
]->valnos
.push_back(VNI
);
813 LI
.valnos
[j
++] = VNI
;