12cab237bSDimitry Andric //===- LiveInterval.cpp - Live Interval Representation --------------------===//
2f22ef01cSRoman Divacky //
3f22ef01cSRoman Divacky // The LLVM Compiler Infrastructure
4f22ef01cSRoman Divacky //
5f22ef01cSRoman Divacky // This file is distributed under the University of Illinois Open Source
6f22ef01cSRoman Divacky // License. See LICENSE.TXT for details.
7f22ef01cSRoman Divacky //
8f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
9f22ef01cSRoman Divacky //
10f22ef01cSRoman Divacky // This file implements the LiveRange and LiveInterval classes. Given some
11f22ef01cSRoman Divacky // numbering of each the machine instructions an interval [i, j) is said to be a
12f785676fSDimitry Andric // live range for register v if there is no instruction with number j' >= j
13f22ef01cSRoman Divacky // such that v is live at j' and there is no instruction with number i' < i such
14f785676fSDimitry Andric // that v is live at i'. In this implementation ranges can have holes,
15f785676fSDimitry Andric // i.e. a range might look like [1,20), [50,65), [1000,1001). Each
16f785676fSDimitry Andric // individual segment is represented as an instance of LiveRange::Segment,
17f785676fSDimitry Andric // and the whole range is represented as an instance of LiveRange.
18f22ef01cSRoman Divacky //
19f22ef01cSRoman Divacky //===----------------------------------------------------------------------===//
20f22ef01cSRoman Divacky
21f22ef01cSRoman Divacky #include "llvm/CodeGen/LiveInterval.h"
223ca95b02SDimitry Andric #include "LiveRangeUtils.h"
23139f7f9bSDimitry Andric #include "RegisterCoalescer.h"
242cab237bSDimitry Andric #include "llvm/ADT/ArrayRef.h"
25139f7f9bSDimitry Andric #include "llvm/ADT/STLExtras.h"
262cab237bSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
272cab237bSDimitry Andric #include "llvm/ADT/SmallVector.h"
282cab237bSDimitry Andric #include "llvm/ADT/iterator_range.h"
292cab237bSDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
302cab237bSDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h"
312cab237bSDimitry Andric #include "llvm/CodeGen/MachineInstr.h"
322cab237bSDimitry Andric #include "llvm/CodeGen/MachineOperand.h"
33f22ef01cSRoman Divacky #include "llvm/CodeGen/MachineRegisterInfo.h"
342cab237bSDimitry Andric #include "llvm/CodeGen/SlotIndexes.h"
352cab237bSDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h"
364ba319b5SDimitry Andric #include "llvm/Config/llvm-config.h"
372cab237bSDimitry Andric #include "llvm/MC/LaneBitmask.h"
382cab237bSDimitry Andric #include "llvm/Support/Compiler.h"
39f22ef01cSRoman Divacky #include "llvm/Support/Debug.h"
40f22ef01cSRoman Divacky #include "llvm/Support/raw_ostream.h"
41f22ef01cSRoman Divacky #include <algorithm>
422cab237bSDimitry Andric #include <cassert>
432cab237bSDimitry Andric #include <cstddef>
442cab237bSDimitry Andric #include <iterator>
452cab237bSDimitry Andric #include <utility>
462cab237bSDimitry Andric
47f22ef01cSRoman Divacky using namespace llvm;
48f22ef01cSRoman Divacky
49ff0cc061SDimitry Andric namespace {
502cab237bSDimitry Andric
51ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
52ff0cc061SDimitry Andric // Implementation of various methods necessary for calculation of live ranges.
53ff0cc061SDimitry Andric // The implementation of the methods abstracts from the concrete type of the
54ff0cc061SDimitry Andric // segment collection.
55ff0cc061SDimitry Andric //
56ff0cc061SDimitry Andric // Implementation of the class follows the Template design pattern. The base
57ff0cc061SDimitry Andric // class contains generic algorithms that call collection-specific methods,
58ff0cc061SDimitry Andric // which are provided in concrete subclasses. In order to avoid virtual calls
59ff0cc061SDimitry Andric // these methods are provided by means of C++ template instantiation.
60ff0cc061SDimitry Andric // The base class calls the methods of the subclass through method impl(),
61ff0cc061SDimitry Andric // which casts 'this' pointer to the type of the subclass.
62ff0cc061SDimitry Andric //
63ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
64ff0cc061SDimitry Andric
65ff0cc061SDimitry Andric template <typename ImplT, typename IteratorT, typename CollectionT>
66ff0cc061SDimitry Andric class CalcLiveRangeUtilBase {
67ff0cc061SDimitry Andric protected:
68ff0cc061SDimitry Andric LiveRange *LR;
69ff0cc061SDimitry Andric
70ff0cc061SDimitry Andric protected:
CalcLiveRangeUtilBase(LiveRange * LR)71ff0cc061SDimitry Andric CalcLiveRangeUtilBase(LiveRange *LR) : LR(LR) {}
72ff0cc061SDimitry Andric
73ff0cc061SDimitry Andric public:
742cab237bSDimitry Andric using Segment = LiveRange::Segment;
752cab237bSDimitry Andric using iterator = IteratorT;
76ff0cc061SDimitry Andric
77d88c1a5aSDimitry Andric /// A counterpart of LiveRange::createDeadDef: Make sure the range has a
78d88c1a5aSDimitry Andric /// value defined at @p Def.
79d88c1a5aSDimitry Andric /// If @p ForVNI is null, and there is no value defined at @p Def, a new
80d88c1a5aSDimitry Andric /// value will be allocated using @p VNInfoAllocator.
81d88c1a5aSDimitry Andric /// If @p ForVNI is null, the return value is the value defined at @p Def,
82d88c1a5aSDimitry Andric /// either a pre-existing one, or the one newly created.
83d88c1a5aSDimitry Andric /// If @p ForVNI is not null, then @p Def should be the location where
84d88c1a5aSDimitry Andric /// @p ForVNI is defined. If the range does not have a value defined at
85d88c1a5aSDimitry Andric /// @p Def, the value @p ForVNI will be used instead of allocating a new
86d88c1a5aSDimitry Andric /// one. If the range already has a value defined at @p Def, it must be
87d88c1a5aSDimitry Andric /// same as @p ForVNI. In either case, @p ForVNI will be the return value.
createDeadDef(SlotIndex Def,VNInfo::Allocator * VNInfoAllocator,VNInfo * ForVNI)88d88c1a5aSDimitry Andric VNInfo *createDeadDef(SlotIndex Def, VNInfo::Allocator *VNInfoAllocator,
89d88c1a5aSDimitry Andric VNInfo *ForVNI) {
90ff0cc061SDimitry Andric assert(!Def.isDead() && "Cannot define a value at the dead slot");
91d88c1a5aSDimitry Andric assert((!ForVNI || ForVNI->def == Def) &&
92d88c1a5aSDimitry Andric "If ForVNI is specified, it must match Def");
93ff0cc061SDimitry Andric iterator I = impl().find(Def);
94ff0cc061SDimitry Andric if (I == segments().end()) {
95d88c1a5aSDimitry Andric VNInfo *VNI = ForVNI ? ForVNI : LR->getNextValue(Def, *VNInfoAllocator);
96ff0cc061SDimitry Andric impl().insertAtEnd(Segment(Def, Def.getDeadSlot(), VNI));
97ff0cc061SDimitry Andric return VNI;
98ff0cc061SDimitry Andric }
99ff0cc061SDimitry Andric
100ff0cc061SDimitry Andric Segment *S = segmentAt(I);
101ff0cc061SDimitry Andric if (SlotIndex::isSameInstr(Def, S->start)) {
102d88c1a5aSDimitry Andric assert((!ForVNI || ForVNI == S->valno) && "Value number mismatch");
103ff0cc061SDimitry Andric assert(S->valno->def == S->start && "Inconsistent existing value def");
104ff0cc061SDimitry Andric
105ff0cc061SDimitry Andric // It is possible to have both normal and early-clobber defs of the same
106ff0cc061SDimitry Andric // register on an instruction. It doesn't make a lot of sense, but it is
107ff0cc061SDimitry Andric // possible to specify in inline assembly.
108ff0cc061SDimitry Andric //
109ff0cc061SDimitry Andric // Just convert everything to early-clobber.
110ff0cc061SDimitry Andric Def = std::min(Def, S->start);
111ff0cc061SDimitry Andric if (Def != S->start)
112ff0cc061SDimitry Andric S->start = S->valno->def = Def;
113ff0cc061SDimitry Andric return S->valno;
114ff0cc061SDimitry Andric }
115ff0cc061SDimitry Andric assert(SlotIndex::isEarlierInstr(Def, S->start) && "Already live at def");
116d88c1a5aSDimitry Andric VNInfo *VNI = ForVNI ? ForVNI : LR->getNextValue(Def, *VNInfoAllocator);
117ff0cc061SDimitry Andric segments().insert(I, Segment(Def, Def.getDeadSlot(), VNI));
118ff0cc061SDimitry Andric return VNI;
119ff0cc061SDimitry Andric }
120ff0cc061SDimitry Andric
extendInBlock(SlotIndex StartIdx,SlotIndex Use)121ff0cc061SDimitry Andric VNInfo *extendInBlock(SlotIndex StartIdx, SlotIndex Use) {
122ff0cc061SDimitry Andric if (segments().empty())
123ff0cc061SDimitry Andric return nullptr;
124ff0cc061SDimitry Andric iterator I =
125ff0cc061SDimitry Andric impl().findInsertPos(Segment(Use.getPrevSlot(), Use, nullptr));
126ff0cc061SDimitry Andric if (I == segments().begin())
127ff0cc061SDimitry Andric return nullptr;
128ff0cc061SDimitry Andric --I;
129ff0cc061SDimitry Andric if (I->end <= StartIdx)
130ff0cc061SDimitry Andric return nullptr;
131ff0cc061SDimitry Andric if (I->end < Use)
132ff0cc061SDimitry Andric extendSegmentEndTo(I, Use);
133ff0cc061SDimitry Andric return I->valno;
134ff0cc061SDimitry Andric }
135ff0cc061SDimitry Andric
extendInBlock(ArrayRef<SlotIndex> Undefs,SlotIndex StartIdx,SlotIndex Use)136d88c1a5aSDimitry Andric std::pair<VNInfo*,bool> extendInBlock(ArrayRef<SlotIndex> Undefs,
137d88c1a5aSDimitry Andric SlotIndex StartIdx, SlotIndex Use) {
138d88c1a5aSDimitry Andric if (segments().empty())
139d88c1a5aSDimitry Andric return std::make_pair(nullptr, false);
140d88c1a5aSDimitry Andric SlotIndex BeforeUse = Use.getPrevSlot();
141d88c1a5aSDimitry Andric iterator I = impl().findInsertPos(Segment(BeforeUse, Use, nullptr));
142d88c1a5aSDimitry Andric if (I == segments().begin())
143d88c1a5aSDimitry Andric return std::make_pair(nullptr, LR->isUndefIn(Undefs, StartIdx, BeforeUse));
144d88c1a5aSDimitry Andric --I;
145d88c1a5aSDimitry Andric if (I->end <= StartIdx)
146d88c1a5aSDimitry Andric return std::make_pair(nullptr, LR->isUndefIn(Undefs, StartIdx, BeforeUse));
147d88c1a5aSDimitry Andric if (I->end < Use) {
148d88c1a5aSDimitry Andric if (LR->isUndefIn(Undefs, I->end, BeforeUse))
149d88c1a5aSDimitry Andric return std::make_pair(nullptr, true);
150d88c1a5aSDimitry Andric extendSegmentEndTo(I, Use);
151d88c1a5aSDimitry Andric }
152d88c1a5aSDimitry Andric return std::make_pair(I->valno, false);
153d88c1a5aSDimitry Andric }
154d88c1a5aSDimitry Andric
155ff0cc061SDimitry Andric /// This method is used when we want to extend the segment specified
156ff0cc061SDimitry Andric /// by I to end at the specified endpoint. To do this, we should
157ff0cc061SDimitry Andric /// merge and eliminate all segments that this will overlap
158ff0cc061SDimitry Andric /// with. The iterator is not invalidated.
extendSegmentEndTo(iterator I,SlotIndex NewEnd)159ff0cc061SDimitry Andric void extendSegmentEndTo(iterator I, SlotIndex NewEnd) {
160ff0cc061SDimitry Andric assert(I != segments().end() && "Not a valid segment!");
161ff0cc061SDimitry Andric Segment *S = segmentAt(I);
162ff0cc061SDimitry Andric VNInfo *ValNo = I->valno;
163ff0cc061SDimitry Andric
164ff0cc061SDimitry Andric // Search for the first segment that we can't merge with.
165ff0cc061SDimitry Andric iterator MergeTo = std::next(I);
166ff0cc061SDimitry Andric for (; MergeTo != segments().end() && NewEnd >= MergeTo->end; ++MergeTo)
167ff0cc061SDimitry Andric assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
168ff0cc061SDimitry Andric
169ff0cc061SDimitry Andric // If NewEnd was in the middle of a segment, make sure to get its endpoint.
170ff0cc061SDimitry Andric S->end = std::max(NewEnd, std::prev(MergeTo)->end);
171ff0cc061SDimitry Andric
172ff0cc061SDimitry Andric // If the newly formed segment now touches the segment after it and if they
173ff0cc061SDimitry Andric // have the same value number, merge the two segments into one segment.
174ff0cc061SDimitry Andric if (MergeTo != segments().end() && MergeTo->start <= I->end &&
175ff0cc061SDimitry Andric MergeTo->valno == ValNo) {
176ff0cc061SDimitry Andric S->end = MergeTo->end;
177ff0cc061SDimitry Andric ++MergeTo;
178ff0cc061SDimitry Andric }
179ff0cc061SDimitry Andric
180ff0cc061SDimitry Andric // Erase any dead segments.
181ff0cc061SDimitry Andric segments().erase(std::next(I), MergeTo);
182ff0cc061SDimitry Andric }
183ff0cc061SDimitry Andric
184ff0cc061SDimitry Andric /// This method is used when we want to extend the segment specified
185ff0cc061SDimitry Andric /// by I to start at the specified endpoint. To do this, we should
186ff0cc061SDimitry Andric /// merge and eliminate all segments that this will overlap with.
extendSegmentStartTo(iterator I,SlotIndex NewStart)187ff0cc061SDimitry Andric iterator extendSegmentStartTo(iterator I, SlotIndex NewStart) {
188ff0cc061SDimitry Andric assert(I != segments().end() && "Not a valid segment!");
189ff0cc061SDimitry Andric Segment *S = segmentAt(I);
190ff0cc061SDimitry Andric VNInfo *ValNo = I->valno;
191ff0cc061SDimitry Andric
192ff0cc061SDimitry Andric // Search for the first segment that we can't merge with.
193ff0cc061SDimitry Andric iterator MergeTo = I;
194ff0cc061SDimitry Andric do {
195ff0cc061SDimitry Andric if (MergeTo == segments().begin()) {
196ff0cc061SDimitry Andric S->start = NewStart;
197ff0cc061SDimitry Andric segments().erase(MergeTo, I);
198ff0cc061SDimitry Andric return I;
199ff0cc061SDimitry Andric }
200ff0cc061SDimitry Andric assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
201ff0cc061SDimitry Andric --MergeTo;
202ff0cc061SDimitry Andric } while (NewStart <= MergeTo->start);
203ff0cc061SDimitry Andric
204ff0cc061SDimitry Andric // If we start in the middle of another segment, just delete a range and
205ff0cc061SDimitry Andric // extend that segment.
206ff0cc061SDimitry Andric if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
207ff0cc061SDimitry Andric segmentAt(MergeTo)->end = S->end;
208ff0cc061SDimitry Andric } else {
209ff0cc061SDimitry Andric // Otherwise, extend the segment right after.
210ff0cc061SDimitry Andric ++MergeTo;
211ff0cc061SDimitry Andric Segment *MergeToSeg = segmentAt(MergeTo);
212ff0cc061SDimitry Andric MergeToSeg->start = NewStart;
213ff0cc061SDimitry Andric MergeToSeg->end = S->end;
214ff0cc061SDimitry Andric }
215ff0cc061SDimitry Andric
216ff0cc061SDimitry Andric segments().erase(std::next(MergeTo), std::next(I));
217ff0cc061SDimitry Andric return MergeTo;
218ff0cc061SDimitry Andric }
219ff0cc061SDimitry Andric
addSegment(Segment S)220ff0cc061SDimitry Andric iterator addSegment(Segment S) {
221ff0cc061SDimitry Andric SlotIndex Start = S.start, End = S.end;
222ff0cc061SDimitry Andric iterator I = impl().findInsertPos(S);
223ff0cc061SDimitry Andric
224ff0cc061SDimitry Andric // If the inserted segment starts in the middle or right at the end of
225ff0cc061SDimitry Andric // another segment, just extend that segment to contain the segment of S.
226ff0cc061SDimitry Andric if (I != segments().begin()) {
227ff0cc061SDimitry Andric iterator B = std::prev(I);
228ff0cc061SDimitry Andric if (S.valno == B->valno) {
229ff0cc061SDimitry Andric if (B->start <= Start && B->end >= Start) {
230ff0cc061SDimitry Andric extendSegmentEndTo(B, End);
231ff0cc061SDimitry Andric return B;
232ff0cc061SDimitry Andric }
233ff0cc061SDimitry Andric } else {
234ff0cc061SDimitry Andric // Check to make sure that we are not overlapping two live segments with
235ff0cc061SDimitry Andric // different valno's.
236ff0cc061SDimitry Andric assert(B->end <= Start &&
237ff0cc061SDimitry Andric "Cannot overlap two segments with differing ValID's"
238ff0cc061SDimitry Andric " (did you def the same reg twice in a MachineInstr?)");
239ff0cc061SDimitry Andric }
240ff0cc061SDimitry Andric }
241ff0cc061SDimitry Andric
242ff0cc061SDimitry Andric // Otherwise, if this segment ends in the middle of, or right next
243ff0cc061SDimitry Andric // to, another segment, merge it into that segment.
244ff0cc061SDimitry Andric if (I != segments().end()) {
245ff0cc061SDimitry Andric if (S.valno == I->valno) {
246ff0cc061SDimitry Andric if (I->start <= End) {
247ff0cc061SDimitry Andric I = extendSegmentStartTo(I, Start);
248ff0cc061SDimitry Andric
249ff0cc061SDimitry Andric // If S is a complete superset of a segment, we may need to grow its
250ff0cc061SDimitry Andric // endpoint as well.
251ff0cc061SDimitry Andric if (End > I->end)
252ff0cc061SDimitry Andric extendSegmentEndTo(I, End);
253ff0cc061SDimitry Andric return I;
254ff0cc061SDimitry Andric }
255ff0cc061SDimitry Andric } else {
256ff0cc061SDimitry Andric // Check to make sure that we are not overlapping two live segments with
257ff0cc061SDimitry Andric // different valno's.
258ff0cc061SDimitry Andric assert(I->start >= End &&
259ff0cc061SDimitry Andric "Cannot overlap two segments with differing ValID's");
260ff0cc061SDimitry Andric }
261ff0cc061SDimitry Andric }
262ff0cc061SDimitry Andric
263ff0cc061SDimitry Andric // Otherwise, this is just a new segment that doesn't interact with
264ff0cc061SDimitry Andric // anything.
265ff0cc061SDimitry Andric // Insert it.
266ff0cc061SDimitry Andric return segments().insert(I, S);
267ff0cc061SDimitry Andric }
268ff0cc061SDimitry Andric
269ff0cc061SDimitry Andric private:
impl()270ff0cc061SDimitry Andric ImplT &impl() { return *static_cast<ImplT *>(this); }
271ff0cc061SDimitry Andric
segments()272ff0cc061SDimitry Andric CollectionT &segments() { return impl().segmentsColl(); }
273ff0cc061SDimitry Andric
segmentAt(iterator I)274ff0cc061SDimitry Andric Segment *segmentAt(iterator I) { return const_cast<Segment *>(&(*I)); }
275ff0cc061SDimitry Andric };
276ff0cc061SDimitry Andric
277ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
278ff0cc061SDimitry Andric // Instantiation of the methods for calculation of live ranges
279ff0cc061SDimitry Andric // based on a segment vector.
280ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
281ff0cc061SDimitry Andric
282ff0cc061SDimitry Andric class CalcLiveRangeUtilVector;
2832cab237bSDimitry Andric using CalcLiveRangeUtilVectorBase =
2842cab237bSDimitry Andric CalcLiveRangeUtilBase<CalcLiveRangeUtilVector, LiveRange::iterator,
2852cab237bSDimitry Andric LiveRange::Segments>;
286ff0cc061SDimitry Andric
287ff0cc061SDimitry Andric class CalcLiveRangeUtilVector : public CalcLiveRangeUtilVectorBase {
288ff0cc061SDimitry Andric public:
CalcLiveRangeUtilVector(LiveRange * LR)289ff0cc061SDimitry Andric CalcLiveRangeUtilVector(LiveRange *LR) : CalcLiveRangeUtilVectorBase(LR) {}
290ff0cc061SDimitry Andric
291ff0cc061SDimitry Andric private:
292ff0cc061SDimitry Andric friend CalcLiveRangeUtilVectorBase;
293ff0cc061SDimitry Andric
segmentsColl()294ff0cc061SDimitry Andric LiveRange::Segments &segmentsColl() { return LR->segments; }
295ff0cc061SDimitry Andric
insertAtEnd(const Segment & S)296ff0cc061SDimitry Andric void insertAtEnd(const Segment &S) { LR->segments.push_back(S); }
297ff0cc061SDimitry Andric
find(SlotIndex Pos)298ff0cc061SDimitry Andric iterator find(SlotIndex Pos) { return LR->find(Pos); }
299ff0cc061SDimitry Andric
findInsertPos(Segment S)300ff0cc061SDimitry Andric iterator findInsertPos(Segment S) {
301ff0cc061SDimitry Andric return std::upper_bound(LR->begin(), LR->end(), S.start);
302ff0cc061SDimitry Andric }
303ff0cc061SDimitry Andric };
304ff0cc061SDimitry Andric
305ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
306ff0cc061SDimitry Andric // Instantiation of the methods for calculation of live ranges
307ff0cc061SDimitry Andric // based on a segment set.
308ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
309ff0cc061SDimitry Andric
310ff0cc061SDimitry Andric class CalcLiveRangeUtilSet;
3112cab237bSDimitry Andric using CalcLiveRangeUtilSetBase =
3122cab237bSDimitry Andric CalcLiveRangeUtilBase<CalcLiveRangeUtilSet, LiveRange::SegmentSet::iterator,
3132cab237bSDimitry Andric LiveRange::SegmentSet>;
314ff0cc061SDimitry Andric
315ff0cc061SDimitry Andric class CalcLiveRangeUtilSet : public CalcLiveRangeUtilSetBase {
316ff0cc061SDimitry Andric public:
CalcLiveRangeUtilSet(LiveRange * LR)317ff0cc061SDimitry Andric CalcLiveRangeUtilSet(LiveRange *LR) : CalcLiveRangeUtilSetBase(LR) {}
318ff0cc061SDimitry Andric
319ff0cc061SDimitry Andric private:
320ff0cc061SDimitry Andric friend CalcLiveRangeUtilSetBase;
321ff0cc061SDimitry Andric
segmentsColl()322ff0cc061SDimitry Andric LiveRange::SegmentSet &segmentsColl() { return *LR->segmentSet; }
323ff0cc061SDimitry Andric
insertAtEnd(const Segment & S)324ff0cc061SDimitry Andric void insertAtEnd(const Segment &S) {
325ff0cc061SDimitry Andric LR->segmentSet->insert(LR->segmentSet->end(), S);
326ff0cc061SDimitry Andric }
327ff0cc061SDimitry Andric
find(SlotIndex Pos)328ff0cc061SDimitry Andric iterator find(SlotIndex Pos) {
329ff0cc061SDimitry Andric iterator I =
330ff0cc061SDimitry Andric LR->segmentSet->upper_bound(Segment(Pos, Pos.getNextSlot(), nullptr));
331ff0cc061SDimitry Andric if (I == LR->segmentSet->begin())
332ff0cc061SDimitry Andric return I;
333ff0cc061SDimitry Andric iterator PrevI = std::prev(I);
334ff0cc061SDimitry Andric if (Pos < (*PrevI).end)
335ff0cc061SDimitry Andric return PrevI;
336ff0cc061SDimitry Andric return I;
337ff0cc061SDimitry Andric }
338ff0cc061SDimitry Andric
findInsertPos(Segment S)339ff0cc061SDimitry Andric iterator findInsertPos(Segment S) {
340ff0cc061SDimitry Andric iterator I = LR->segmentSet->upper_bound(S);
341ff0cc061SDimitry Andric if (I != LR->segmentSet->end() && !(S.start < *I))
342ff0cc061SDimitry Andric ++I;
343ff0cc061SDimitry Andric return I;
344ff0cc061SDimitry Andric }
345ff0cc061SDimitry Andric };
3462cab237bSDimitry Andric
3472cab237bSDimitry Andric } // end anonymous namespace
348ff0cc061SDimitry Andric
349ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
350ff0cc061SDimitry Andric // LiveRange methods
351ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
352ff0cc061SDimitry Andric
find(SlotIndex Pos)353f785676fSDimitry Andric LiveRange::iterator LiveRange::find(SlotIndex Pos) {
3543b0f4066SDimitry Andric // This algorithm is basically std::upper_bound.
3553b0f4066SDimitry Andric // Unfortunately, std::upper_bound cannot be used with mixed types until we
3563b0f4066SDimitry Andric // adopt C++0x. Many libraries can do it, but not all.
3573b0f4066SDimitry Andric if (empty() || Pos >= endIndex())
3583b0f4066SDimitry Andric return end();
3593b0f4066SDimitry Andric iterator I = begin();
360f785676fSDimitry Andric size_t Len = size();
3613b0f4066SDimitry Andric do {
3623b0f4066SDimitry Andric size_t Mid = Len >> 1;
3633ca95b02SDimitry Andric if (Pos < I[Mid].end) {
3643b0f4066SDimitry Andric Len = Mid;
3653ca95b02SDimitry Andric } else {
3663ca95b02SDimitry Andric I += Mid + 1;
3673ca95b02SDimitry Andric Len -= Mid + 1;
3683ca95b02SDimitry Andric }
3693b0f4066SDimitry Andric } while (Len);
3703b0f4066SDimitry Andric return I;
371ffd1746dSEd Schouten }
372ffd1746dSEd Schouten
createDeadDef(SlotIndex Def,VNInfo::Allocator & VNIAlloc)373d88c1a5aSDimitry Andric VNInfo *LiveRange::createDeadDef(SlotIndex Def, VNInfo::Allocator &VNIAlloc) {
374ff0cc061SDimitry Andric // Use the segment set, if it is available.
375ff0cc061SDimitry Andric if (segmentSet != nullptr)
376d88c1a5aSDimitry Andric return CalcLiveRangeUtilSet(this).createDeadDef(Def, &VNIAlloc, nullptr);
377ff0cc061SDimitry Andric // Otherwise use the segment vector.
378d88c1a5aSDimitry Andric return CalcLiveRangeUtilVector(this).createDeadDef(Def, &VNIAlloc, nullptr);
379d88c1a5aSDimitry Andric }
380d88c1a5aSDimitry Andric
createDeadDef(VNInfo * VNI)381d88c1a5aSDimitry Andric VNInfo *LiveRange::createDeadDef(VNInfo *VNI) {
382d88c1a5aSDimitry Andric // Use the segment set, if it is available.
383d88c1a5aSDimitry Andric if (segmentSet != nullptr)
384d88c1a5aSDimitry Andric return CalcLiveRangeUtilSet(this).createDeadDef(VNI->def, nullptr, VNI);
385d88c1a5aSDimitry Andric // Otherwise use the segment vector.
386d88c1a5aSDimitry Andric return CalcLiveRangeUtilVector(this).createDeadDef(VNI->def, nullptr, VNI);
3877ae0e2c9SDimitry Andric }
3887ae0e2c9SDimitry Andric
389f785676fSDimitry Andric // overlaps - Return true if the intersection of the two live ranges is
390f22ef01cSRoman Divacky // not empty.
391f22ef01cSRoman Divacky //
392f22ef01cSRoman Divacky // An example for overlaps():
393f22ef01cSRoman Divacky //
394f22ef01cSRoman Divacky // 0: A = ...
395f22ef01cSRoman Divacky // 4: B = ...
396f22ef01cSRoman Divacky // 8: C = A + B ;; last use of A
397f22ef01cSRoman Divacky //
398f785676fSDimitry Andric // The live ranges should look like:
399f22ef01cSRoman Divacky //
400f22ef01cSRoman Divacky // A = [3, 11)
401f22ef01cSRoman Divacky // B = [7, x)
402f22ef01cSRoman Divacky // C = [11, y)
403f22ef01cSRoman Divacky //
404f22ef01cSRoman Divacky // A->overlaps(C) should return false since we want to be able to join
405f22ef01cSRoman Divacky // A and C.
406f22ef01cSRoman Divacky //
overlapsFrom(const LiveRange & other,const_iterator StartPos) const407f785676fSDimitry Andric bool LiveRange::overlapsFrom(const LiveRange& other,
408f22ef01cSRoman Divacky const_iterator StartPos) const {
409f785676fSDimitry Andric assert(!empty() && "empty range");
410f22ef01cSRoman Divacky const_iterator i = begin();
411f22ef01cSRoman Divacky const_iterator ie = end();
412f22ef01cSRoman Divacky const_iterator j = StartPos;
413f22ef01cSRoman Divacky const_iterator je = other.end();
414f22ef01cSRoman Divacky
415f22ef01cSRoman Divacky assert((StartPos->start <= i->start || StartPos == other.begin()) &&
416f22ef01cSRoman Divacky StartPos != other.end() && "Bogus start position hint!");
417f22ef01cSRoman Divacky
418f22ef01cSRoman Divacky if (i->start < j->start) {
419f22ef01cSRoman Divacky i = std::upper_bound(i, ie, j->start);
420f785676fSDimitry Andric if (i != begin()) --i;
421f22ef01cSRoman Divacky } else if (j->start < i->start) {
422f22ef01cSRoman Divacky ++StartPos;
423f22ef01cSRoman Divacky if (StartPos != other.end() && StartPos->start <= i->start) {
424f22ef01cSRoman Divacky assert(StartPos < other.end() && i < end());
425f22ef01cSRoman Divacky j = std::upper_bound(j, je, i->start);
426f785676fSDimitry Andric if (j != other.begin()) --j;
427f22ef01cSRoman Divacky }
428f22ef01cSRoman Divacky } else {
429f22ef01cSRoman Divacky return true;
430f22ef01cSRoman Divacky }
431f22ef01cSRoman Divacky
432f22ef01cSRoman Divacky if (j == je) return false;
433f22ef01cSRoman Divacky
434f22ef01cSRoman Divacky while (i != ie) {
435f22ef01cSRoman Divacky if (i->start > j->start) {
436f22ef01cSRoman Divacky std::swap(i, j);
437f22ef01cSRoman Divacky std::swap(ie, je);
438f22ef01cSRoman Divacky }
439f22ef01cSRoman Divacky
440f22ef01cSRoman Divacky if (i->end > j->start)
441f22ef01cSRoman Divacky return true;
442f22ef01cSRoman Divacky ++i;
443f22ef01cSRoman Divacky }
444f22ef01cSRoman Divacky
445f22ef01cSRoman Divacky return false;
446f22ef01cSRoman Divacky }
447f22ef01cSRoman Divacky
overlaps(const LiveRange & Other,const CoalescerPair & CP,const SlotIndexes & Indexes) const448f785676fSDimitry Andric bool LiveRange::overlaps(const LiveRange &Other, const CoalescerPair &CP,
4493861d79fSDimitry Andric const SlotIndexes &Indexes) const {
450f785676fSDimitry Andric assert(!empty() && "empty range");
4513861d79fSDimitry Andric if (Other.empty())
4523861d79fSDimitry Andric return false;
4533861d79fSDimitry Andric
4543861d79fSDimitry Andric // Use binary searches to find initial positions.
4553861d79fSDimitry Andric const_iterator I = find(Other.beginIndex());
4563861d79fSDimitry Andric const_iterator IE = end();
4573861d79fSDimitry Andric if (I == IE)
4583861d79fSDimitry Andric return false;
4593861d79fSDimitry Andric const_iterator J = Other.find(I->start);
4603861d79fSDimitry Andric const_iterator JE = Other.end();
4613861d79fSDimitry Andric if (J == JE)
4623861d79fSDimitry Andric return false;
4633861d79fSDimitry Andric
4642cab237bSDimitry Andric while (true) {
4653861d79fSDimitry Andric // J has just been advanced to satisfy:
4663861d79fSDimitry Andric assert(J->end >= I->start);
4673861d79fSDimitry Andric // Check for an overlap.
4683861d79fSDimitry Andric if (J->start < I->end) {
4693861d79fSDimitry Andric // I and J are overlapping. Find the later start.
4703861d79fSDimitry Andric SlotIndex Def = std::max(I->start, J->start);
4713861d79fSDimitry Andric // Allow the overlap if Def is a coalescable copy.
4723861d79fSDimitry Andric if (Def.isBlock() ||
4733861d79fSDimitry Andric !CP.isCoalescable(Indexes.getInstructionFromIndex(Def)))
4743861d79fSDimitry Andric return true;
4753861d79fSDimitry Andric }
4763861d79fSDimitry Andric // Advance the iterator that ends first to check for more overlaps.
4773861d79fSDimitry Andric if (J->end > I->end) {
4783861d79fSDimitry Andric std::swap(I, J);
4793861d79fSDimitry Andric std::swap(IE, JE);
4803861d79fSDimitry Andric }
4813861d79fSDimitry Andric // Advance J until J->end >= I->start.
4823861d79fSDimitry Andric do
4833861d79fSDimitry Andric if (++J == JE)
4843861d79fSDimitry Andric return false;
4853861d79fSDimitry Andric while (J->end < I->start);
4863861d79fSDimitry Andric }
4873861d79fSDimitry Andric }
4883861d79fSDimitry Andric
489f785676fSDimitry Andric /// overlaps - Return true if the live range overlaps an interval specified
490f22ef01cSRoman Divacky /// by [Start, End).
overlaps(SlotIndex Start,SlotIndex End) const491f785676fSDimitry Andric bool LiveRange::overlaps(SlotIndex Start, SlotIndex End) const {
492f22ef01cSRoman Divacky assert(Start < End && "Invalid range");
493ffd1746dSEd Schouten const_iterator I = std::lower_bound(begin(), end(), End);
494ffd1746dSEd Schouten return I != begin() && (--I)->end > Start;
495f22ef01cSRoman Divacky }
496f22ef01cSRoman Divacky
covers(const LiveRange & Other) const49739d628a0SDimitry Andric bool LiveRange::covers(const LiveRange &Other) const {
49839d628a0SDimitry Andric if (empty())
49939d628a0SDimitry Andric return Other.empty();
50039d628a0SDimitry Andric
50139d628a0SDimitry Andric const_iterator I = begin();
50239d628a0SDimitry Andric for (const Segment &O : Other.segments) {
50339d628a0SDimitry Andric I = advanceTo(I, O.start);
50439d628a0SDimitry Andric if (I == end() || I->start > O.start)
50539d628a0SDimitry Andric return false;
50639d628a0SDimitry Andric
50739d628a0SDimitry Andric // Check adjacent live segments and see if we can get behind O.end.
50839d628a0SDimitry Andric while (I->end < O.end) {
50939d628a0SDimitry Andric const_iterator Last = I;
51039d628a0SDimitry Andric // Get next segment and abort if it was not adjacent.
51139d628a0SDimitry Andric ++I;
51239d628a0SDimitry Andric if (I == end() || Last->end != I->start)
51339d628a0SDimitry Andric return false;
51439d628a0SDimitry Andric }
51539d628a0SDimitry Andric }
51639d628a0SDimitry Andric return true;
51739d628a0SDimitry Andric }
518e580952dSDimitry Andric
519e580952dSDimitry Andric /// ValNo is dead, remove it. If it is the largest value number, just nuke it
520e580952dSDimitry Andric /// (and any other deleted values neighboring it), otherwise mark it as ~1U so
521e580952dSDimitry Andric /// it can be nuked later.
markValNoForDeletion(VNInfo * ValNo)522f785676fSDimitry Andric void LiveRange::markValNoForDeletion(VNInfo *ValNo) {
523e580952dSDimitry Andric if (ValNo->id == getNumValNums()-1) {
524e580952dSDimitry Andric do {
525e580952dSDimitry Andric valnos.pop_back();
526e580952dSDimitry Andric } while (!valnos.empty() && valnos.back()->isUnused());
527e580952dSDimitry Andric } else {
5287ae0e2c9SDimitry Andric ValNo->markUnused();
529e580952dSDimitry Andric }
530e580952dSDimitry Andric }
531e580952dSDimitry Andric
532e580952dSDimitry Andric /// RenumberValues - Renumber all values in order of appearance and delete the
533e580952dSDimitry Andric /// remaining unused values.
RenumberValues()534f785676fSDimitry Andric void LiveRange::RenumberValues() {
535e580952dSDimitry Andric SmallPtrSet<VNInfo*, 8> Seen;
536e580952dSDimitry Andric valnos.clear();
53739d628a0SDimitry Andric for (const Segment &S : segments) {
53839d628a0SDimitry Andric VNInfo *VNI = S.valno;
53939d628a0SDimitry Andric if (!Seen.insert(VNI).second)
540e580952dSDimitry Andric continue;
541f785676fSDimitry Andric assert(!VNI->isUnused() && "Unused valno used by live segment");
542e580952dSDimitry Andric VNI->id = (unsigned)valnos.size();
543e580952dSDimitry Andric valnos.push_back(VNI);
544e580952dSDimitry Andric }
545e580952dSDimitry Andric }
546e580952dSDimitry Andric
addSegmentToSet(Segment S)547ff0cc061SDimitry Andric void LiveRange::addSegmentToSet(Segment S) {
548ff0cc061SDimitry Andric CalcLiveRangeUtilSet(this).addSegment(S);
549f22ef01cSRoman Divacky }
550f22ef01cSRoman Divacky
addSegment(Segment S)551ff0cc061SDimitry Andric LiveRange::iterator LiveRange::addSegment(Segment S) {
552ff0cc061SDimitry Andric // Use the segment set, if it is available.
553ff0cc061SDimitry Andric if (segmentSet != nullptr) {
554ff0cc061SDimitry Andric addSegmentToSet(S);
555ff0cc061SDimitry Andric return end();
556f22ef01cSRoman Divacky }
557ff0cc061SDimitry Andric // Otherwise use the segment vector.
558ff0cc061SDimitry Andric return CalcLiveRangeUtilVector(this).addSegment(S);
559f22ef01cSRoman Divacky }
560f22ef01cSRoman Divacky
append(const Segment S)56139d628a0SDimitry Andric void LiveRange::append(const Segment S) {
56239d628a0SDimitry Andric // Check that the segment belongs to the back of the list.
56339d628a0SDimitry Andric assert(segments.empty() || segments.back().end <= S.start);
56439d628a0SDimitry Andric segments.push_back(S);
56539d628a0SDimitry Andric }
56639d628a0SDimitry Andric
extendInBlock(ArrayRef<SlotIndex> Undefs,SlotIndex StartIdx,SlotIndex Kill)567d88c1a5aSDimitry Andric std::pair<VNInfo*,bool> LiveRange::extendInBlock(ArrayRef<SlotIndex> Undefs,
568d88c1a5aSDimitry Andric SlotIndex StartIdx, SlotIndex Kill) {
569d88c1a5aSDimitry Andric // Use the segment set, if it is available.
570d88c1a5aSDimitry Andric if (segmentSet != nullptr)
571d88c1a5aSDimitry Andric return CalcLiveRangeUtilSet(this).extendInBlock(Undefs, StartIdx, Kill);
572d88c1a5aSDimitry Andric // Otherwise use the segment vector.
573d88c1a5aSDimitry Andric return CalcLiveRangeUtilVector(this).extendInBlock(Undefs, StartIdx, Kill);
574d88c1a5aSDimitry Andric }
575d88c1a5aSDimitry Andric
extendInBlock(SlotIndex StartIdx,SlotIndex Kill)576f785676fSDimitry Andric VNInfo *LiveRange::extendInBlock(SlotIndex StartIdx, SlotIndex Kill) {
577ff0cc061SDimitry Andric // Use the segment set, if it is available.
578ff0cc061SDimitry Andric if (segmentSet != nullptr)
579ff0cc061SDimitry Andric return CalcLiveRangeUtilSet(this).extendInBlock(StartIdx, Kill);
580ff0cc061SDimitry Andric // Otherwise use the segment vector.
581ff0cc061SDimitry Andric return CalcLiveRangeUtilVector(this).extendInBlock(StartIdx, Kill);
5823b0f4066SDimitry Andric }
583f22ef01cSRoman Divacky
584f785676fSDimitry Andric /// Remove the specified segment from this range. Note that the segment must
585f785676fSDimitry Andric /// be in a single Segment in its entirety.
removeSegment(SlotIndex Start,SlotIndex End,bool RemoveDeadValNo)586f785676fSDimitry Andric void LiveRange::removeSegment(SlotIndex Start, SlotIndex End,
587f22ef01cSRoman Divacky bool RemoveDeadValNo) {
588f785676fSDimitry Andric // Find the Segment containing this span.
589f785676fSDimitry Andric iterator I = find(Start);
590f785676fSDimitry Andric assert(I != end() && "Segment is not in range!");
591f785676fSDimitry Andric assert(I->containsInterval(Start, End)
592f785676fSDimitry Andric && "Segment is not entirely in range!");
593f22ef01cSRoman Divacky
594f785676fSDimitry Andric // If the span we are removing is at the start of the Segment, adjust it.
595f22ef01cSRoman Divacky VNInfo *ValNo = I->valno;
596f22ef01cSRoman Divacky if (I->start == Start) {
597f22ef01cSRoman Divacky if (I->end == End) {
598f22ef01cSRoman Divacky if (RemoveDeadValNo) {
599f22ef01cSRoman Divacky // Check if val# is dead.
600f22ef01cSRoman Divacky bool isDead = true;
601f22ef01cSRoman Divacky for (const_iterator II = begin(), EE = end(); II != EE; ++II)
602f22ef01cSRoman Divacky if (II != I && II->valno == ValNo) {
603f22ef01cSRoman Divacky isDead = false;
604f22ef01cSRoman Divacky break;
605f22ef01cSRoman Divacky }
606f22ef01cSRoman Divacky if (isDead) {
607e580952dSDimitry Andric // Now that ValNo is dead, remove it.
608e580952dSDimitry Andric markValNoForDeletion(ValNo);
609f22ef01cSRoman Divacky }
610f22ef01cSRoman Divacky }
611f22ef01cSRoman Divacky
612f785676fSDimitry Andric segments.erase(I); // Removed the whole Segment.
613f22ef01cSRoman Divacky } else
614f22ef01cSRoman Divacky I->start = End;
615f22ef01cSRoman Divacky return;
616f22ef01cSRoman Divacky }
617f22ef01cSRoman Divacky
618f785676fSDimitry Andric // Otherwise if the span we are removing is at the end of the Segment,
619f22ef01cSRoman Divacky // adjust the other way.
620f22ef01cSRoman Divacky if (I->end == End) {
621f22ef01cSRoman Divacky I->end = Start;
622f22ef01cSRoman Divacky return;
623f22ef01cSRoman Divacky }
624f22ef01cSRoman Divacky
625f785676fSDimitry Andric // Otherwise, we are splitting the Segment into two pieces.
626f22ef01cSRoman Divacky SlotIndex OldEnd = I->end;
627f785676fSDimitry Andric I->end = Start; // Trim the old segment.
628f22ef01cSRoman Divacky
629f22ef01cSRoman Divacky // Insert the new one.
63091bc56edSDimitry Andric segments.insert(std::next(I), Segment(End, OldEnd, ValNo));
631f22ef01cSRoman Divacky }
632f22ef01cSRoman Divacky
633f785676fSDimitry Andric /// removeValNo - Remove all the segments defined by the specified value#.
634f22ef01cSRoman Divacky /// Also remove the value# from value# list.
removeValNo(VNInfo * ValNo)635f785676fSDimitry Andric void LiveRange::removeValNo(VNInfo *ValNo) {
636f22ef01cSRoman Divacky if (empty()) return;
637d88c1a5aSDimitry Andric segments.erase(remove_if(*this, [ValNo](const Segment &S) {
638ff0cc061SDimitry Andric return S.valno == ValNo;
639ff0cc061SDimitry Andric }), end());
640e580952dSDimitry Andric // Now that ValNo is dead, remove it.
641e580952dSDimitry Andric markValNoForDeletion(ValNo);
642f22ef01cSRoman Divacky }
643f22ef01cSRoman Divacky
join(LiveRange & Other,const int * LHSValNoAssignments,const int * RHSValNoAssignments,SmallVectorImpl<VNInfo * > & NewVNInfo)644f785676fSDimitry Andric void LiveRange::join(LiveRange &Other,
645f22ef01cSRoman Divacky const int *LHSValNoAssignments,
646f22ef01cSRoman Divacky const int *RHSValNoAssignments,
647f785676fSDimitry Andric SmallVectorImpl<VNInfo *> &NewVNInfo) {
6487ae0e2c9SDimitry Andric verify();
6497ae0e2c9SDimitry Andric
650f785676fSDimitry Andric // Determine if any of our values are mapped. This is uncommon, so we want
651f785676fSDimitry Andric // to avoid the range scan if not.
652f22ef01cSRoman Divacky bool MustMapCurValNos = false;
653f22ef01cSRoman Divacky unsigned NumVals = getNumValNums();
654f22ef01cSRoman Divacky unsigned NumNewVals = NewVNInfo.size();
655f22ef01cSRoman Divacky for (unsigned i = 0; i != NumVals; ++i) {
656f22ef01cSRoman Divacky unsigned LHSValID = LHSValNoAssignments[i];
657f22ef01cSRoman Divacky if (i != LHSValID ||
658dff0c46cSDimitry Andric (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i))) {
659f22ef01cSRoman Divacky MustMapCurValNos = true;
660dff0c46cSDimitry Andric break;
661dff0c46cSDimitry Andric }
662f22ef01cSRoman Divacky }
663f22ef01cSRoman Divacky
664f785676fSDimitry Andric // If we have to apply a mapping to our base range assignment, rewrite it now.
6653861d79fSDimitry Andric if (MustMapCurValNos && !empty()) {
666f22ef01cSRoman Divacky // Map the first live range.
667dff0c46cSDimitry Andric
668f22ef01cSRoman Divacky iterator OutIt = begin();
669f22ef01cSRoman Divacky OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
67091bc56edSDimitry Andric for (iterator I = std::next(OutIt), E = end(); I != E; ++I) {
671dff0c46cSDimitry Andric VNInfo* nextValNo = NewVNInfo[LHSValNoAssignments[I->valno->id]];
67291bc56edSDimitry Andric assert(nextValNo && "Huh?");
673f22ef01cSRoman Divacky
674f22ef01cSRoman Divacky // If this live range has the same value # as its immediate predecessor,
675f785676fSDimitry Andric // and if they are neighbors, remove one Segment. This happens when we
676dff0c46cSDimitry Andric // have [0,4:0)[4,7:1) and map 0/1 onto the same value #.
677dff0c46cSDimitry Andric if (OutIt->valno == nextValNo && OutIt->end == I->start) {
678dff0c46cSDimitry Andric OutIt->end = I->end;
679f22ef01cSRoman Divacky } else {
680f785676fSDimitry Andric // Didn't merge. Move OutIt to the next segment,
681dff0c46cSDimitry Andric ++OutIt;
682dff0c46cSDimitry Andric OutIt->valno = nextValNo;
683dff0c46cSDimitry Andric if (OutIt != I) {
684f22ef01cSRoman Divacky OutIt->start = I->start;
685f22ef01cSRoman Divacky OutIt->end = I->end;
686f22ef01cSRoman Divacky }
687f22ef01cSRoman Divacky }
688f22ef01cSRoman Divacky }
689f785676fSDimitry Andric // If we merge some segments, chop off the end.
690dff0c46cSDimitry Andric ++OutIt;
691f785676fSDimitry Andric segments.erase(OutIt, end());
692f22ef01cSRoman Divacky }
693f22ef01cSRoman Divacky
694139f7f9bSDimitry Andric // Rewrite Other values before changing the VNInfo ids.
695139f7f9bSDimitry Andric // This can leave Other in an invalid state because we're not coalescing
696139f7f9bSDimitry Andric // touching segments that now have identical values. That's OK since Other is
697139f7f9bSDimitry Andric // not supposed to be valid after calling join();
69839d628a0SDimitry Andric for (Segment &S : Other.segments)
69939d628a0SDimitry Andric S.valno = NewVNInfo[RHSValNoAssignments[S.valno->id]];
700f22ef01cSRoman Divacky
701f22ef01cSRoman Divacky // Update val# info. Renumber them and make sure they all belong to this
702f785676fSDimitry Andric // LiveRange now. Also remove dead val#'s.
703f22ef01cSRoman Divacky unsigned NumValNos = 0;
704f22ef01cSRoman Divacky for (unsigned i = 0; i < NumNewVals; ++i) {
705f22ef01cSRoman Divacky VNInfo *VNI = NewVNInfo[i];
706f22ef01cSRoman Divacky if (VNI) {
707f22ef01cSRoman Divacky if (NumValNos >= NumVals)
708f22ef01cSRoman Divacky valnos.push_back(VNI);
709f22ef01cSRoman Divacky else
710f22ef01cSRoman Divacky valnos[NumValNos] = VNI;
711f22ef01cSRoman Divacky VNI->id = NumValNos++; // Renumber val#.
712f22ef01cSRoman Divacky }
713f22ef01cSRoman Divacky }
714f22ef01cSRoman Divacky if (NumNewVals < NumVals)
715f22ef01cSRoman Divacky valnos.resize(NumNewVals); // shrinkify
716f22ef01cSRoman Divacky
717f785676fSDimitry Andric // Okay, now insert the RHS live segments into the LHS.
718139f7f9bSDimitry Andric LiveRangeUpdater Updater(this);
71939d628a0SDimitry Andric for (Segment &S : Other.segments)
72039d628a0SDimitry Andric Updater.add(S);
721f22ef01cSRoman Divacky }
722f22ef01cSRoman Divacky
723f785676fSDimitry Andric /// Merge all of the segments in RHS into this live range as the specified
724f785676fSDimitry Andric /// value number. The segments in RHS are allowed to overlap with segments in
725f785676fSDimitry Andric /// the current range, but only if the overlapping segments have the
726f785676fSDimitry Andric /// specified value number.
MergeSegmentsInAsValue(const LiveRange & RHS,VNInfo * LHSValNo)727f785676fSDimitry Andric void LiveRange::MergeSegmentsInAsValue(const LiveRange &RHS,
728f22ef01cSRoman Divacky VNInfo *LHSValNo) {
729139f7f9bSDimitry Andric LiveRangeUpdater Updater(this);
73039d628a0SDimitry Andric for (const Segment &S : RHS.segments)
73139d628a0SDimitry Andric Updater.add(S.start, S.end, LHSValNo);
732f22ef01cSRoman Divacky }
733f22ef01cSRoman Divacky
734f785676fSDimitry Andric /// MergeValueInAsValue - Merge all of the live segments of a specific val#
735f785676fSDimitry Andric /// in RHS into this live range as the specified value number.
736f785676fSDimitry Andric /// The segments in RHS are allowed to overlap with segments in the
737f785676fSDimitry Andric /// current range, it will replace the value numbers of the overlaped
738f785676fSDimitry Andric /// segments with the specified value number.
MergeValueInAsValue(const LiveRange & RHS,const VNInfo * RHSValNo,VNInfo * LHSValNo)739f785676fSDimitry Andric void LiveRange::MergeValueInAsValue(const LiveRange &RHS,
7407ae0e2c9SDimitry Andric const VNInfo *RHSValNo,
7417ae0e2c9SDimitry Andric VNInfo *LHSValNo) {
742139f7f9bSDimitry Andric LiveRangeUpdater Updater(this);
74339d628a0SDimitry Andric for (const Segment &S : RHS.segments)
74439d628a0SDimitry Andric if (S.valno == RHSValNo)
74539d628a0SDimitry Andric Updater.add(S.start, S.end, LHSValNo);
746f22ef01cSRoman Divacky }
747f22ef01cSRoman Divacky
748f22ef01cSRoman Divacky /// MergeValueNumberInto - This method is called when two value nubmers
749f22ef01cSRoman Divacky /// are found to be equivalent. This eliminates V1, replacing all
750f785676fSDimitry Andric /// segments with the V1 value number with the V2 value number. This can
751f22ef01cSRoman Divacky /// cause merging of V1/V2 values numbers and compaction of the value space.
MergeValueNumberInto(VNInfo * V1,VNInfo * V2)752f785676fSDimitry Andric VNInfo *LiveRange::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
753f22ef01cSRoman Divacky assert(V1 != V2 && "Identical value#'s are always equivalent!");
754f22ef01cSRoman Divacky
755f22ef01cSRoman Divacky // This code actually merges the (numerically) larger value number into the
756f22ef01cSRoman Divacky // smaller value number, which is likely to allow us to compactify the value
757f22ef01cSRoman Divacky // space. The only thing we have to be careful of is to preserve the
758f22ef01cSRoman Divacky // instruction that defines the result value.
759f22ef01cSRoman Divacky
760f22ef01cSRoman Divacky // Make sure V2 is smaller than V1.
761f22ef01cSRoman Divacky if (V1->id < V2->id) {
762f22ef01cSRoman Divacky V1->copyFrom(*V2);
763f22ef01cSRoman Divacky std::swap(V1, V2);
764f22ef01cSRoman Divacky }
765f22ef01cSRoman Divacky
766f785676fSDimitry Andric // Merge V1 segments into V2.
767f22ef01cSRoman Divacky for (iterator I = begin(); I != end(); ) {
768f785676fSDimitry Andric iterator S = I++;
769f785676fSDimitry Andric if (S->valno != V1) continue; // Not a V1 Segment.
770f22ef01cSRoman Divacky
771f22ef01cSRoman Divacky // Okay, we found a V1 live range. If it had a previous, touching, V2 live
772f22ef01cSRoman Divacky // range, extend it.
773f785676fSDimitry Andric if (S != begin()) {
774f785676fSDimitry Andric iterator Prev = S-1;
775f785676fSDimitry Andric if (Prev->valno == V2 && Prev->end == S->start) {
776f785676fSDimitry Andric Prev->end = S->end;
777f22ef01cSRoman Divacky
778f22ef01cSRoman Divacky // Erase this live-range.
779f785676fSDimitry Andric segments.erase(S);
780f22ef01cSRoman Divacky I = Prev+1;
781f785676fSDimitry Andric S = Prev;
782f22ef01cSRoman Divacky }
783f22ef01cSRoman Divacky }
784f22ef01cSRoman Divacky
785f22ef01cSRoman Divacky // Okay, now we have a V1 or V2 live range that is maximally merged forward.
786f22ef01cSRoman Divacky // Ensure that it is a V2 live-range.
787f785676fSDimitry Andric S->valno = V2;
788f22ef01cSRoman Divacky
789f785676fSDimitry Andric // If we can merge it into later V2 segments, do so now. We ignore any
790f785676fSDimitry Andric // following V1 segments, as they will be merged in subsequent iterations
791f22ef01cSRoman Divacky // of the loop.
792f22ef01cSRoman Divacky if (I != end()) {
793f785676fSDimitry Andric if (I->start == S->end && I->valno == V2) {
794f785676fSDimitry Andric S->end = I->end;
795f785676fSDimitry Andric segments.erase(I);
796f785676fSDimitry Andric I = S+1;
797f22ef01cSRoman Divacky }
798f22ef01cSRoman Divacky }
799f22ef01cSRoman Divacky }
800f22ef01cSRoman Divacky
801e580952dSDimitry Andric // Now that V1 is dead, remove it.
802e580952dSDimitry Andric markValNoForDeletion(V1);
803f22ef01cSRoman Divacky
804f22ef01cSRoman Divacky return V2;
805f22ef01cSRoman Divacky }
806f22ef01cSRoman Divacky
flushSegmentSet()807ff0cc061SDimitry Andric void LiveRange::flushSegmentSet() {
808ff0cc061SDimitry Andric assert(segmentSet != nullptr && "segment set must have been created");
809ff0cc061SDimitry Andric assert(
810ff0cc061SDimitry Andric segments.empty() &&
811ff0cc061SDimitry Andric "segment set can be used only initially before switching to the array");
812ff0cc061SDimitry Andric segments.append(segmentSet->begin(), segmentSet->end());
813ff0cc061SDimitry Andric segmentSet = nullptr;
814ff0cc061SDimitry Andric verify();
815ff0cc061SDimitry Andric }
816ff0cc061SDimitry Andric
isLiveAtIndexes(ArrayRef<SlotIndex> Slots) const817ce479d84SDimitry Andric bool LiveRange::isLiveAtIndexes(ArrayRef<SlotIndex> Slots) const {
818ce479d84SDimitry Andric ArrayRef<SlotIndex>::iterator SlotI = Slots.begin();
819ce479d84SDimitry Andric ArrayRef<SlotIndex>::iterator SlotE = Slots.end();
820ce479d84SDimitry Andric
821ce479d84SDimitry Andric // If there are no regmask slots, we have nothing to search.
822ce479d84SDimitry Andric if (SlotI == SlotE)
823ce479d84SDimitry Andric return false;
824ce479d84SDimitry Andric
825ce479d84SDimitry Andric // Start our search at the first segment that ends after the first slot.
826ce479d84SDimitry Andric const_iterator SegmentI = find(*SlotI);
827ce479d84SDimitry Andric const_iterator SegmentE = end();
828ce479d84SDimitry Andric
829ce479d84SDimitry Andric // If there are no segments that end after the first slot, we're done.
830ce479d84SDimitry Andric if (SegmentI == SegmentE)
831ce479d84SDimitry Andric return false;
832ce479d84SDimitry Andric
833ce479d84SDimitry Andric // Look for each slot in the live range.
834ce479d84SDimitry Andric for ( ; SlotI != SlotE; ++SlotI) {
835ce479d84SDimitry Andric // Go to the next segment that ends after the current slot.
836ce479d84SDimitry Andric // The slot may be within a hole in the range.
837ce479d84SDimitry Andric SegmentI = advanceTo(SegmentI, *SlotI);
838ce479d84SDimitry Andric if (SegmentI == SegmentE)
839ce479d84SDimitry Andric return false;
840ce479d84SDimitry Andric
841ce479d84SDimitry Andric // If this segment contains the slot, we're done.
842ce479d84SDimitry Andric if (SegmentI->contains(*SlotI))
843ce479d84SDimitry Andric return true;
844ce479d84SDimitry Andric // Otherwise, look for the next slot.
845ce479d84SDimitry Andric }
846ce479d84SDimitry Andric
847ce479d84SDimitry Andric // We didn't find a segment containing any of the slots.
848ce479d84SDimitry Andric return false;
849ce479d84SDimitry Andric }
850ce479d84SDimitry Andric
freeSubRange(SubRange * S)851ff0cc061SDimitry Andric void LiveInterval::freeSubRange(SubRange *S) {
852ff0cc061SDimitry Andric S->~SubRange();
853ff0cc061SDimitry Andric // Memory was allocated with BumpPtr allocator and is not freed here.
854ff0cc061SDimitry Andric }
855ff0cc061SDimitry Andric
removeEmptySubRanges()85639d628a0SDimitry Andric void LiveInterval::removeEmptySubRanges() {
85739d628a0SDimitry Andric SubRange **NextPtr = &SubRanges;
85839d628a0SDimitry Andric SubRange *I = *NextPtr;
85939d628a0SDimitry Andric while (I != nullptr) {
86039d628a0SDimitry Andric if (!I->empty()) {
86139d628a0SDimitry Andric NextPtr = &I->Next;
86239d628a0SDimitry Andric I = *NextPtr;
86339d628a0SDimitry Andric continue;
86439d628a0SDimitry Andric }
86539d628a0SDimitry Andric // Skip empty subranges until we find the first nonempty one.
86639d628a0SDimitry Andric do {
867ff0cc061SDimitry Andric SubRange *Next = I->Next;
868ff0cc061SDimitry Andric freeSubRange(I);
869ff0cc061SDimitry Andric I = Next;
87039d628a0SDimitry Andric } while (I != nullptr && I->empty());
87139d628a0SDimitry Andric *NextPtr = I;
87239d628a0SDimitry Andric }
87339d628a0SDimitry Andric }
87439d628a0SDimitry Andric
clearSubRanges()875ff0cc061SDimitry Andric void LiveInterval::clearSubRanges() {
876ff0cc061SDimitry Andric for (SubRange *I = SubRanges, *Next; I != nullptr; I = Next) {
877ff0cc061SDimitry Andric Next = I->Next;
878ff0cc061SDimitry Andric freeSubRange(I);
879ff0cc061SDimitry Andric }
880ff0cc061SDimitry Andric SubRanges = nullptr;
881ff0cc061SDimitry Andric }
882ff0cc061SDimitry Andric
refineSubRanges(BumpPtrAllocator & Allocator,LaneBitmask LaneMask,std::function<void (LiveInterval::SubRange &)> Apply)8837a7e6055SDimitry Andric void LiveInterval::refineSubRanges(BumpPtrAllocator &Allocator,
8847a7e6055SDimitry Andric LaneBitmask LaneMask, std::function<void(LiveInterval::SubRange&)> Apply) {
8857a7e6055SDimitry Andric LaneBitmask ToApply = LaneMask;
8867a7e6055SDimitry Andric for (SubRange &SR : subranges()) {
8877a7e6055SDimitry Andric LaneBitmask SRMask = SR.LaneMask;
8887a7e6055SDimitry Andric LaneBitmask Matching = SRMask & LaneMask;
8897a7e6055SDimitry Andric if (Matching.none())
8907a7e6055SDimitry Andric continue;
8917a7e6055SDimitry Andric
8927a7e6055SDimitry Andric SubRange *MatchingRange;
8937a7e6055SDimitry Andric if (SRMask == Matching) {
8947a7e6055SDimitry Andric // The subrange fits (it does not cover bits outside \p LaneMask).
8957a7e6055SDimitry Andric MatchingRange = &SR;
8967a7e6055SDimitry Andric } else {
8977a7e6055SDimitry Andric // We have to split the subrange into a matching and non-matching part.
8987a7e6055SDimitry Andric // Reduce lanemask of existing lane to non-matching part.
8997a7e6055SDimitry Andric SR.LaneMask = SRMask & ~Matching;
9007a7e6055SDimitry Andric // Create a new subrange for the matching part
9017a7e6055SDimitry Andric MatchingRange = createSubRangeFrom(Allocator, Matching, SR);
9027a7e6055SDimitry Andric }
9037a7e6055SDimitry Andric Apply(*MatchingRange);
9047a7e6055SDimitry Andric ToApply &= ~Matching;
9057a7e6055SDimitry Andric }
9067a7e6055SDimitry Andric // Create a new subrange if there are uncovered bits left.
9077a7e6055SDimitry Andric if (ToApply.any()) {
9087a7e6055SDimitry Andric SubRange *NewRange = createSubRange(Allocator, ToApply);
9097a7e6055SDimitry Andric Apply(*NewRange);
9107a7e6055SDimitry Andric }
9117a7e6055SDimitry Andric }
9127a7e6055SDimitry Andric
getSize() const913f22ef01cSRoman Divacky unsigned LiveInterval::getSize() const {
914f22ef01cSRoman Divacky unsigned Sum = 0;
91539d628a0SDimitry Andric for (const Segment &S : segments)
91639d628a0SDimitry Andric Sum += S.start.distance(S.end);
917f22ef01cSRoman Divacky return Sum;
918f22ef01cSRoman Divacky }
919f22ef01cSRoman Divacky
computeSubRangeUndefs(SmallVectorImpl<SlotIndex> & Undefs,LaneBitmask LaneMask,const MachineRegisterInfo & MRI,const SlotIndexes & Indexes) const920d88c1a5aSDimitry Andric void LiveInterval::computeSubRangeUndefs(SmallVectorImpl<SlotIndex> &Undefs,
921d88c1a5aSDimitry Andric LaneBitmask LaneMask,
922d88c1a5aSDimitry Andric const MachineRegisterInfo &MRI,
923d88c1a5aSDimitry Andric const SlotIndexes &Indexes) const {
924d88c1a5aSDimitry Andric assert(TargetRegisterInfo::isVirtualRegister(reg));
925d88c1a5aSDimitry Andric LaneBitmask VRegMask = MRI.getMaxLaneMaskForVReg(reg);
926d88c1a5aSDimitry Andric assert((VRegMask & LaneMask).any());
927d88c1a5aSDimitry Andric const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
928d88c1a5aSDimitry Andric for (const MachineOperand &MO : MRI.def_operands(reg)) {
929d88c1a5aSDimitry Andric if (!MO.isUndef())
930d88c1a5aSDimitry Andric continue;
931d88c1a5aSDimitry Andric unsigned SubReg = MO.getSubReg();
932d88c1a5aSDimitry Andric assert(SubReg != 0 && "Undef should only be set on subreg defs");
933d88c1a5aSDimitry Andric LaneBitmask DefMask = TRI.getSubRegIndexLaneMask(SubReg);
934d88c1a5aSDimitry Andric LaneBitmask UndefMask = VRegMask & ~DefMask;
935d88c1a5aSDimitry Andric if ((UndefMask & LaneMask).any()) {
936d88c1a5aSDimitry Andric const MachineInstr &MI = *MO.getParent();
937d88c1a5aSDimitry Andric bool EarlyClobber = MO.isEarlyClobber();
938d88c1a5aSDimitry Andric SlotIndex Pos = Indexes.getInstructionIndex(MI).getRegSlot(EarlyClobber);
939d88c1a5aSDimitry Andric Undefs.push_back(Pos);
940d88c1a5aSDimitry Andric }
941d88c1a5aSDimitry Andric }
942d88c1a5aSDimitry Andric }
943d88c1a5aSDimitry Andric
operator <<(raw_ostream & OS,const LiveRange::Segment & S)9442cab237bSDimitry Andric raw_ostream& llvm::operator<<(raw_ostream& OS, const LiveRange::Segment &S) {
9452cab237bSDimitry Andric return OS << '[' << S.start << ',' << S.end << ':' << S.valno->id << ')';
946f22ef01cSRoman Divacky }
947f22ef01cSRoman Divacky
9483861d79fSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const9493ca95b02SDimitry Andric LLVM_DUMP_METHOD void LiveRange::Segment::dump() const {
9503ca95b02SDimitry Andric dbgs() << *this << '\n';
951f22ef01cSRoman Divacky }
9523861d79fSDimitry Andric #endif
953f22ef01cSRoman Divacky
print(raw_ostream & OS) const954f785676fSDimitry Andric void LiveRange::print(raw_ostream &OS) const {
955f22ef01cSRoman Divacky if (empty())
956f22ef01cSRoman Divacky OS << "EMPTY";
957f22ef01cSRoman Divacky else {
95839d628a0SDimitry Andric for (const Segment &S : segments) {
95939d628a0SDimitry Andric OS << S;
96039d628a0SDimitry Andric assert(S.valno == getValNumInfo(S.valno->id) && "Bad VNInfo");
961ffd1746dSEd Schouten }
962f22ef01cSRoman Divacky }
963f22ef01cSRoman Divacky
964f22ef01cSRoman Divacky // Print value number info.
965f22ef01cSRoman Divacky if (getNumValNums()) {
966f22ef01cSRoman Divacky OS << " ";
967f22ef01cSRoman Divacky unsigned vnum = 0;
968f22ef01cSRoman Divacky for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
969f22ef01cSRoman Divacky ++i, ++vnum) {
970f22ef01cSRoman Divacky const VNInfo *vni = *i;
9713ca95b02SDimitry Andric if (vnum) OS << ' ';
9723ca95b02SDimitry Andric OS << vnum << '@';
973f22ef01cSRoman Divacky if (vni->isUnused()) {
9743ca95b02SDimitry Andric OS << 'x';
975f22ef01cSRoman Divacky } else {
976f22ef01cSRoman Divacky OS << vni->def;
9772754fe60SDimitry Andric if (vni->isPHIDef())
9787ae0e2c9SDimitry Andric OS << "-phi";
979f22ef01cSRoman Divacky }
980f22ef01cSRoman Divacky }
981f22ef01cSRoman Divacky }
982f22ef01cSRoman Divacky }
983f22ef01cSRoman Divacky
print(raw_ostream & OS) const9843ca95b02SDimitry Andric void LiveInterval::SubRange::print(raw_ostream &OS) const {
9853ca95b02SDimitry Andric OS << " L" << PrintLaneMask(LaneMask) << ' '
9863ca95b02SDimitry Andric << static_cast<const LiveRange&>(*this);
9873ca95b02SDimitry Andric }
9883ca95b02SDimitry Andric
print(raw_ostream & OS) const989f785676fSDimitry Andric void LiveInterval::print(raw_ostream &OS) const {
9902cab237bSDimitry Andric OS << printReg(reg) << ' ';
991f785676fSDimitry Andric super::print(OS);
99239d628a0SDimitry Andric // Print subranges
9933ca95b02SDimitry Andric for (const SubRange &SR : subranges())
9943ca95b02SDimitry Andric OS << SR;
9954ba319b5SDimitry Andric OS << " weight:" << weight;
996f785676fSDimitry Andric }
997f785676fSDimitry Andric
9983861d79fSDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump() const9993ca95b02SDimitry Andric LLVM_DUMP_METHOD void LiveRange::dump() const {
10003ca95b02SDimitry Andric dbgs() << *this << '\n';
1001f785676fSDimitry Andric }
1002f785676fSDimitry Andric
dump() const10033ca95b02SDimitry Andric LLVM_DUMP_METHOD void LiveInterval::SubRange::dump() const {
10043ca95b02SDimitry Andric dbgs() << *this << '\n';
10053ca95b02SDimitry Andric }
10063ca95b02SDimitry Andric
dump() const10073ca95b02SDimitry Andric LLVM_DUMP_METHOD void LiveInterval::dump() const {
10083ca95b02SDimitry Andric dbgs() << *this << '\n';
1009f22ef01cSRoman Divacky }
10103861d79fSDimitry Andric #endif
1011f22ef01cSRoman Divacky
10127ae0e2c9SDimitry Andric #ifndef NDEBUG
verify() const1013f785676fSDimitry Andric void LiveRange::verify() const {
10147ae0e2c9SDimitry Andric for (const_iterator I = begin(), E = end(); I != E; ++I) {
10157ae0e2c9SDimitry Andric assert(I->start.isValid());
10167ae0e2c9SDimitry Andric assert(I->end.isValid());
10177ae0e2c9SDimitry Andric assert(I->start < I->end);
101891bc56edSDimitry Andric assert(I->valno != nullptr);
1019f785676fSDimitry Andric assert(I->valno->id < valnos.size());
10207ae0e2c9SDimitry Andric assert(I->valno == valnos[I->valno->id]);
102191bc56edSDimitry Andric if (std::next(I) != E) {
102291bc56edSDimitry Andric assert(I->end <= std::next(I)->start);
102391bc56edSDimitry Andric if (I->end == std::next(I)->start)
102491bc56edSDimitry Andric assert(I->valno != std::next(I)->valno);
10257ae0e2c9SDimitry Andric }
10267ae0e2c9SDimitry Andric }
10277ae0e2c9SDimitry Andric }
102839d628a0SDimitry Andric
verify(const MachineRegisterInfo * MRI) const102939d628a0SDimitry Andric void LiveInterval::verify(const MachineRegisterInfo *MRI) const {
103039d628a0SDimitry Andric super::verify();
103139d628a0SDimitry Andric
103239d628a0SDimitry Andric // Make sure SubRanges are fine and LaneMasks are disjunct.
1033d88c1a5aSDimitry Andric LaneBitmask Mask;
1034d88c1a5aSDimitry Andric LaneBitmask MaxMask = MRI != nullptr ? MRI->getMaxLaneMaskForVReg(reg)
1035d88c1a5aSDimitry Andric : LaneBitmask::getAll();
103639d628a0SDimitry Andric for (const SubRange &SR : subranges()) {
103739d628a0SDimitry Andric // Subrange lanemask should be disjunct to any previous subrange masks.
1038d88c1a5aSDimitry Andric assert((Mask & SR.LaneMask).none());
103939d628a0SDimitry Andric Mask |= SR.LaneMask;
104039d628a0SDimitry Andric
104139d628a0SDimitry Andric // subrange mask should not contained in maximum lane mask for the vreg.
1042d88c1a5aSDimitry Andric assert((Mask & ~MaxMask).none());
10437d523365SDimitry Andric // empty subranges must be removed.
10447d523365SDimitry Andric assert(!SR.empty());
104539d628a0SDimitry Andric
104639d628a0SDimitry Andric SR.verify();
104739d628a0SDimitry Andric // Main liverange should cover subrange.
104839d628a0SDimitry Andric assert(covers(SR));
104939d628a0SDimitry Andric }
105039d628a0SDimitry Andric }
10517ae0e2c9SDimitry Andric #endif
10527ae0e2c9SDimitry Andric
1053139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
1054139f7f9bSDimitry Andric // LiveRangeUpdater class
1055139f7f9bSDimitry Andric //===----------------------------------------------------------------------===//
1056139f7f9bSDimitry Andric //
1057139f7f9bSDimitry Andric // The LiveRangeUpdater class always maintains these invariants:
1058139f7f9bSDimitry Andric //
1059139f7f9bSDimitry Andric // - When LastStart is invalid, Spills is empty and the iterators are invalid.
1060139f7f9bSDimitry Andric // This is the initial state, and the state created by flush().
1061139f7f9bSDimitry Andric // In this state, isDirty() returns false.
1062139f7f9bSDimitry Andric //
1063139f7f9bSDimitry Andric // Otherwise, segments are kept in three separate areas:
1064139f7f9bSDimitry Andric //
1065f785676fSDimitry Andric // 1. [begin; WriteI) at the front of LR.
1066f785676fSDimitry Andric // 2. [ReadI; end) at the back of LR.
1067139f7f9bSDimitry Andric // 3. Spills.
1068139f7f9bSDimitry Andric //
1069f785676fSDimitry Andric // - LR.begin() <= WriteI <= ReadI <= LR.end().
1070139f7f9bSDimitry Andric // - Segments in all three areas are fully ordered and coalesced.
1071139f7f9bSDimitry Andric // - Segments in area 1 precede and can't coalesce with segments in area 2.
1072139f7f9bSDimitry Andric // - Segments in Spills precede and can't coalesce with segments in area 2.
1073139f7f9bSDimitry Andric // - No coalescing is possible between segments in Spills and segments in area
1074139f7f9bSDimitry Andric // 1, and there are no overlapping segments.
1075139f7f9bSDimitry Andric //
1076139f7f9bSDimitry Andric // The segments in Spills are not ordered with respect to the segments in area
1077139f7f9bSDimitry Andric // 1. They need to be merged.
1078139f7f9bSDimitry Andric //
1079139f7f9bSDimitry Andric // When they exist, Spills.back().start <= LastStart,
1080139f7f9bSDimitry Andric // and WriteI[-1].start <= LastStart.
1081139f7f9bSDimitry Andric
10827a7e6055SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
print(raw_ostream & OS) const1083139f7f9bSDimitry Andric void LiveRangeUpdater::print(raw_ostream &OS) const {
1084139f7f9bSDimitry Andric if (!isDirty()) {
1085f785676fSDimitry Andric if (LR)
1086f785676fSDimitry Andric OS << "Clean updater: " << *LR << '\n';
1087139f7f9bSDimitry Andric else
1088139f7f9bSDimitry Andric OS << "Null updater.\n";
1089139f7f9bSDimitry Andric return;
1090139f7f9bSDimitry Andric }
1091f785676fSDimitry Andric assert(LR && "Can't have null LR in dirty updater.");
1092f785676fSDimitry Andric OS << " updater with gap = " << (ReadI - WriteI)
1093139f7f9bSDimitry Andric << ", last start = " << LastStart
1094139f7f9bSDimitry Andric << ":\n Area 1:";
109539d628a0SDimitry Andric for (const auto &S : make_range(LR->begin(), WriteI))
109639d628a0SDimitry Andric OS << ' ' << S;
1097139f7f9bSDimitry Andric OS << "\n Spills:";
1098139f7f9bSDimitry Andric for (unsigned I = 0, E = Spills.size(); I != E; ++I)
1099139f7f9bSDimitry Andric OS << ' ' << Spills[I];
1100139f7f9bSDimitry Andric OS << "\n Area 2:";
110139d628a0SDimitry Andric for (const auto &S : make_range(ReadI, LR->end()))
110239d628a0SDimitry Andric OS << ' ' << S;
1103139f7f9bSDimitry Andric OS << '\n';
1104139f7f9bSDimitry Andric }
1105139f7f9bSDimitry Andric
dump() const11063ca95b02SDimitry Andric LLVM_DUMP_METHOD void LiveRangeUpdater::dump() const {
1107139f7f9bSDimitry Andric print(errs());
1108139f7f9bSDimitry Andric }
11097a7e6055SDimitry Andric #endif
1110139f7f9bSDimitry Andric
1111139f7f9bSDimitry Andric // Determine if A and B should be coalesced.
coalescable(const LiveRange::Segment & A,const LiveRange::Segment & B)1112f785676fSDimitry Andric static inline bool coalescable(const LiveRange::Segment &A,
1113f785676fSDimitry Andric const LiveRange::Segment &B) {
1114f785676fSDimitry Andric assert(A.start <= B.start && "Unordered live segments.");
1115139f7f9bSDimitry Andric if (A.end == B.start)
1116139f7f9bSDimitry Andric return A.valno == B.valno;
1117139f7f9bSDimitry Andric if (A.end < B.start)
1118139f7f9bSDimitry Andric return false;
1119139f7f9bSDimitry Andric assert(A.valno == B.valno && "Cannot overlap different values");
1120139f7f9bSDimitry Andric return true;
1121139f7f9bSDimitry Andric }
1122139f7f9bSDimitry Andric
add(LiveRange::Segment Seg)1123f785676fSDimitry Andric void LiveRangeUpdater::add(LiveRange::Segment Seg) {
1124f785676fSDimitry Andric assert(LR && "Cannot add to a null destination");
1125139f7f9bSDimitry Andric
1126ff0cc061SDimitry Andric // Fall back to the regular add method if the live range
1127ff0cc061SDimitry Andric // is using the segment set instead of the segment vector.
1128ff0cc061SDimitry Andric if (LR->segmentSet != nullptr) {
1129ff0cc061SDimitry Andric LR->addSegmentToSet(Seg);
1130ff0cc061SDimitry Andric return;
1131ff0cc061SDimitry Andric }
1132ff0cc061SDimitry Andric
1133139f7f9bSDimitry Andric // Flush the state if Start moves backwards.
1134139f7f9bSDimitry Andric if (!LastStart.isValid() || LastStart > Seg.start) {
1135139f7f9bSDimitry Andric if (isDirty())
1136139f7f9bSDimitry Andric flush();
1137139f7f9bSDimitry Andric // This brings us to an uninitialized state. Reinitialize.
1138139f7f9bSDimitry Andric assert(Spills.empty() && "Leftover spilled segments");
1139f785676fSDimitry Andric WriteI = ReadI = LR->begin();
1140139f7f9bSDimitry Andric }
1141139f7f9bSDimitry Andric
1142139f7f9bSDimitry Andric // Remember start for next time.
1143139f7f9bSDimitry Andric LastStart = Seg.start;
1144139f7f9bSDimitry Andric
1145139f7f9bSDimitry Andric // Advance ReadI until it ends after Seg.start.
1146f785676fSDimitry Andric LiveRange::iterator E = LR->end();
1147139f7f9bSDimitry Andric if (ReadI != E && ReadI->end <= Seg.start) {
1148139f7f9bSDimitry Andric // First try to close the gap between WriteI and ReadI with spills.
1149139f7f9bSDimitry Andric if (ReadI != WriteI)
1150139f7f9bSDimitry Andric mergeSpills();
1151139f7f9bSDimitry Andric // Then advance ReadI.
1152139f7f9bSDimitry Andric if (ReadI == WriteI)
1153f785676fSDimitry Andric ReadI = WriteI = LR->find(Seg.start);
1154139f7f9bSDimitry Andric else
1155139f7f9bSDimitry Andric while (ReadI != E && ReadI->end <= Seg.start)
1156139f7f9bSDimitry Andric *WriteI++ = *ReadI++;
1157139f7f9bSDimitry Andric }
1158139f7f9bSDimitry Andric
1159139f7f9bSDimitry Andric assert(ReadI == E || ReadI->end > Seg.start);
1160139f7f9bSDimitry Andric
1161139f7f9bSDimitry Andric // Check if the ReadI segment begins early.
1162139f7f9bSDimitry Andric if (ReadI != E && ReadI->start <= Seg.start) {
1163139f7f9bSDimitry Andric assert(ReadI->valno == Seg.valno && "Cannot overlap different values");
1164139f7f9bSDimitry Andric // Bail if Seg is completely contained in ReadI.
1165139f7f9bSDimitry Andric if (ReadI->end >= Seg.end)
1166139f7f9bSDimitry Andric return;
1167139f7f9bSDimitry Andric // Coalesce into Seg.
1168139f7f9bSDimitry Andric Seg.start = ReadI->start;
1169139f7f9bSDimitry Andric ++ReadI;
1170139f7f9bSDimitry Andric }
1171139f7f9bSDimitry Andric
1172139f7f9bSDimitry Andric // Coalesce as much as possible from ReadI into Seg.
1173139f7f9bSDimitry Andric while (ReadI != E && coalescable(Seg, *ReadI)) {
1174139f7f9bSDimitry Andric Seg.end = std::max(Seg.end, ReadI->end);
1175139f7f9bSDimitry Andric ++ReadI;
1176139f7f9bSDimitry Andric }
1177139f7f9bSDimitry Andric
1178139f7f9bSDimitry Andric // Try coalescing Spills.back() into Seg.
1179139f7f9bSDimitry Andric if (!Spills.empty() && coalescable(Spills.back(), Seg)) {
1180139f7f9bSDimitry Andric Seg.start = Spills.back().start;
1181139f7f9bSDimitry Andric Seg.end = std::max(Spills.back().end, Seg.end);
1182139f7f9bSDimitry Andric Spills.pop_back();
1183139f7f9bSDimitry Andric }
1184139f7f9bSDimitry Andric
1185139f7f9bSDimitry Andric // Try coalescing Seg into WriteI[-1].
1186f785676fSDimitry Andric if (WriteI != LR->begin() && coalescable(WriteI[-1], Seg)) {
1187139f7f9bSDimitry Andric WriteI[-1].end = std::max(WriteI[-1].end, Seg.end);
1188139f7f9bSDimitry Andric return;
1189139f7f9bSDimitry Andric }
1190139f7f9bSDimitry Andric
1191139f7f9bSDimitry Andric // Seg doesn't coalesce with anything, and needs to be inserted somewhere.
1192139f7f9bSDimitry Andric if (WriteI != ReadI) {
1193139f7f9bSDimitry Andric *WriteI++ = Seg;
1194139f7f9bSDimitry Andric return;
1195139f7f9bSDimitry Andric }
1196139f7f9bSDimitry Andric
1197f785676fSDimitry Andric // Finally, append to LR or Spills.
1198139f7f9bSDimitry Andric if (WriteI == E) {
1199f785676fSDimitry Andric LR->segments.push_back(Seg);
1200f785676fSDimitry Andric WriteI = ReadI = LR->end();
1201139f7f9bSDimitry Andric } else
1202139f7f9bSDimitry Andric Spills.push_back(Seg);
1203139f7f9bSDimitry Andric }
1204139f7f9bSDimitry Andric
1205139f7f9bSDimitry Andric // Merge as many spilled segments as possible into the gap between WriteI
1206139f7f9bSDimitry Andric // and ReadI. Advance WriteI to reflect the inserted instructions.
mergeSpills()1207139f7f9bSDimitry Andric void LiveRangeUpdater::mergeSpills() {
1208139f7f9bSDimitry Andric // Perform a backwards merge of Spills and [SpillI;WriteI).
1209139f7f9bSDimitry Andric size_t GapSize = ReadI - WriteI;
1210139f7f9bSDimitry Andric size_t NumMoved = std::min(Spills.size(), GapSize);
1211f785676fSDimitry Andric LiveRange::iterator Src = WriteI;
1212f785676fSDimitry Andric LiveRange::iterator Dst = Src + NumMoved;
1213f785676fSDimitry Andric LiveRange::iterator SpillSrc = Spills.end();
1214f785676fSDimitry Andric LiveRange::iterator B = LR->begin();
1215139f7f9bSDimitry Andric
1216139f7f9bSDimitry Andric // This is the new WriteI position after merging spills.
1217139f7f9bSDimitry Andric WriteI = Dst;
1218139f7f9bSDimitry Andric
1219139f7f9bSDimitry Andric // Now merge Src and Spills backwards.
1220139f7f9bSDimitry Andric while (Src != Dst) {
1221139f7f9bSDimitry Andric if (Src != B && Src[-1].start > SpillSrc[-1].start)
1222139f7f9bSDimitry Andric *--Dst = *--Src;
1223139f7f9bSDimitry Andric else
1224139f7f9bSDimitry Andric *--Dst = *--SpillSrc;
1225139f7f9bSDimitry Andric }
1226139f7f9bSDimitry Andric assert(NumMoved == size_t(Spills.end() - SpillSrc));
1227139f7f9bSDimitry Andric Spills.erase(SpillSrc, Spills.end());
1228139f7f9bSDimitry Andric }
1229139f7f9bSDimitry Andric
flush()1230139f7f9bSDimitry Andric void LiveRangeUpdater::flush() {
1231139f7f9bSDimitry Andric if (!isDirty())
1232139f7f9bSDimitry Andric return;
1233139f7f9bSDimitry Andric // Clear the dirty state.
1234139f7f9bSDimitry Andric LastStart = SlotIndex();
1235139f7f9bSDimitry Andric
1236f785676fSDimitry Andric assert(LR && "Cannot add to a null destination");
1237139f7f9bSDimitry Andric
1238139f7f9bSDimitry Andric // Nothing to merge?
1239139f7f9bSDimitry Andric if (Spills.empty()) {
1240f785676fSDimitry Andric LR->segments.erase(WriteI, ReadI);
1241f785676fSDimitry Andric LR->verify();
1242139f7f9bSDimitry Andric return;
1243139f7f9bSDimitry Andric }
1244139f7f9bSDimitry Andric
1245139f7f9bSDimitry Andric // Resize the WriteI - ReadI gap to match Spills.
1246139f7f9bSDimitry Andric size_t GapSize = ReadI - WriteI;
1247139f7f9bSDimitry Andric if (GapSize < Spills.size()) {
1248139f7f9bSDimitry Andric // The gap is too small. Make some room.
1249f785676fSDimitry Andric size_t WritePos = WriteI - LR->begin();
1250f785676fSDimitry Andric LR->segments.insert(ReadI, Spills.size() - GapSize, LiveRange::Segment());
1251139f7f9bSDimitry Andric // This also invalidated ReadI, but it is recomputed below.
1252f785676fSDimitry Andric WriteI = LR->begin() + WritePos;
1253139f7f9bSDimitry Andric } else {
1254139f7f9bSDimitry Andric // Shrink the gap if necessary.
1255f785676fSDimitry Andric LR->segments.erase(WriteI + Spills.size(), ReadI);
1256139f7f9bSDimitry Andric }
1257139f7f9bSDimitry Andric ReadI = WriteI + Spills.size();
1258139f7f9bSDimitry Andric mergeSpills();
1259f785676fSDimitry Andric LR->verify();
1260139f7f9bSDimitry Andric }
1261139f7f9bSDimitry Andric
Classify(const LiveRange & LR)1262444ed5c5SDimitry Andric unsigned ConnectedVNInfoEqClasses::Classify(const LiveRange &LR) {
12632754fe60SDimitry Andric // Create initial equivalence classes.
12643b0f4066SDimitry Andric EqClass.clear();
1265444ed5c5SDimitry Andric EqClass.grow(LR.getNumValNums());
12662754fe60SDimitry Andric
126791bc56edSDimitry Andric const VNInfo *used = nullptr, *unused = nullptr;
12682754fe60SDimitry Andric
12692754fe60SDimitry Andric // Determine connections.
1270444ed5c5SDimitry Andric for (const VNInfo *VNI : LR.valnos) {
12712754fe60SDimitry Andric // Group all unused values into one class.
12722754fe60SDimitry Andric if (VNI->isUnused()) {
12732754fe60SDimitry Andric if (unused)
12743b0f4066SDimitry Andric EqClass.join(unused->id, VNI->id);
12752754fe60SDimitry Andric unused = VNI;
12762754fe60SDimitry Andric continue;
12772754fe60SDimitry Andric }
12782754fe60SDimitry Andric used = VNI;
12792754fe60SDimitry Andric if (VNI->isPHIDef()) {
12803b0f4066SDimitry Andric const MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
12812754fe60SDimitry Andric assert(MBB && "Phi-def has no defining MBB");
12822754fe60SDimitry Andric // Connect to values live out of predecessors.
12832754fe60SDimitry Andric for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
12842754fe60SDimitry Andric PE = MBB->pred_end(); PI != PE; ++PI)
1285444ed5c5SDimitry Andric if (const VNInfo *PVNI = LR.getVNInfoBefore(LIS.getMBBEndIdx(*PI)))
12863b0f4066SDimitry Andric EqClass.join(VNI->id, PVNI->id);
12872754fe60SDimitry Andric } else {
12882754fe60SDimitry Andric // Normal value defined by an instruction. Check for two-addr redef.
12892754fe60SDimitry Andric // FIXME: This could be coincidental. Should we really check for a tied
12902754fe60SDimitry Andric // operand constraint?
12912754fe60SDimitry Andric // Note that VNI->def may be a use slot for an early clobber def.
1292444ed5c5SDimitry Andric if (const VNInfo *UVNI = LR.getVNInfoBefore(VNI->def))
12933b0f4066SDimitry Andric EqClass.join(VNI->id, UVNI->id);
12942754fe60SDimitry Andric }
12952754fe60SDimitry Andric }
12962754fe60SDimitry Andric
12972754fe60SDimitry Andric // Lump all the unused values in with the last used value.
12982754fe60SDimitry Andric if (used && unused)
12993b0f4066SDimitry Andric EqClass.join(used->id, unused->id);
13002754fe60SDimitry Andric
13013b0f4066SDimitry Andric EqClass.compress();
13023b0f4066SDimitry Andric return EqClass.getNumClasses();
13032754fe60SDimitry Andric }
13042754fe60SDimitry Andric
Distribute(LiveInterval & LI,LiveInterval * LIV[],MachineRegisterInfo & MRI)13057d523365SDimitry Andric void ConnectedVNInfoEqClasses::Distribute(LiveInterval &LI, LiveInterval *LIV[],
13067d523365SDimitry Andric MachineRegisterInfo &MRI) {
13073b0f4066SDimitry Andric // Rewrite instructions.
13083b0f4066SDimitry Andric for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LI.reg),
13093b0f4066SDimitry Andric RE = MRI.reg_end(); RI != RE;) {
131091bc56edSDimitry Andric MachineOperand &MO = *RI;
131191bc56edSDimitry Andric MachineInstr *MI = RI->getParent();
13123b0f4066SDimitry Andric ++RI;
1313*b5893f02SDimitry Andric const VNInfo *VNI;
1314*b5893f02SDimitry Andric if (MI->isDebugValue()) {
1315*b5893f02SDimitry Andric // DBG_VALUE instructions don't have slot indexes, so get the index of
1316*b5893f02SDimitry Andric // the instruction before them. The value is defined there too.
1317*b5893f02SDimitry Andric SlotIndex Idx = LIS.getSlotIndexes()->getIndexBefore(*MI);
1318*b5893f02SDimitry Andric VNI = LI.Query(Idx).valueOut();
1319*b5893f02SDimitry Andric } else {
1320*b5893f02SDimitry Andric SlotIndex Idx = LIS.getInstructionIndex(*MI);
1321f785676fSDimitry Andric LiveQueryResult LRQ = LI.Query(Idx);
1322*b5893f02SDimitry Andric VNI = MO.readsReg() ? LRQ.valueIn() : LRQ.valueDefined();
1323*b5893f02SDimitry Andric }
13247ae0e2c9SDimitry Andric // In the case of an <undef> use that isn't tied to any def, VNI will be
13257ae0e2c9SDimitry Andric // NULL. If the use is tied to a def, VNI will be the defined value.
13267ae0e2c9SDimitry Andric if (!VNI)
13277ae0e2c9SDimitry Andric continue;
13287d523365SDimitry Andric if (unsigned EqClass = getEqClass(VNI))
13297d523365SDimitry Andric MO.setReg(LIV[EqClass-1]->reg);
13303b0f4066SDimitry Andric }
13313b0f4066SDimitry Andric
13327d523365SDimitry Andric // Distribute subregister liveranges.
13337d523365SDimitry Andric if (LI.hasSubRanges()) {
13347d523365SDimitry Andric unsigned NumComponents = EqClass.getNumClasses();
13357d523365SDimitry Andric SmallVector<unsigned, 8> VNIMapping;
13367d523365SDimitry Andric SmallVector<LiveInterval::SubRange*, 8> SubRanges;
13377d523365SDimitry Andric BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
13387d523365SDimitry Andric for (LiveInterval::SubRange &SR : LI.subranges()) {
13397d523365SDimitry Andric // Create new subranges in the split intervals and construct a mapping
13407d523365SDimitry Andric // for the VNInfos in the subrange.
13417d523365SDimitry Andric unsigned NumValNos = SR.valnos.size();
13427d523365SDimitry Andric VNIMapping.clear();
13437d523365SDimitry Andric VNIMapping.reserve(NumValNos);
13447d523365SDimitry Andric SubRanges.clear();
13457d523365SDimitry Andric SubRanges.resize(NumComponents-1, nullptr);
13467d523365SDimitry Andric for (unsigned I = 0; I < NumValNos; ++I) {
13477d523365SDimitry Andric const VNInfo &VNI = *SR.valnos[I];
13483ca95b02SDimitry Andric unsigned ComponentNum;
13493ca95b02SDimitry Andric if (VNI.isUnused()) {
13503ca95b02SDimitry Andric ComponentNum = 0;
13513ca95b02SDimitry Andric } else {
13527d523365SDimitry Andric const VNInfo *MainRangeVNI = LI.getVNInfoAt(VNI.def);
13537d523365SDimitry Andric assert(MainRangeVNI != nullptr
13547d523365SDimitry Andric && "SubRange def must have corresponding main range def");
13553ca95b02SDimitry Andric ComponentNum = getEqClass(MainRangeVNI);
13567d523365SDimitry Andric if (ComponentNum > 0 && SubRanges[ComponentNum-1] == nullptr) {
13577d523365SDimitry Andric SubRanges[ComponentNum-1]
13587d523365SDimitry Andric = LIV[ComponentNum-1]->createSubRange(Allocator, SR.LaneMask);
13592754fe60SDimitry Andric }
13607d523365SDimitry Andric }
13613ca95b02SDimitry Andric VNIMapping.push_back(ComponentNum);
13623ca95b02SDimitry Andric }
13637d523365SDimitry Andric DistributeRange(SR, SubRanges.data(), VNIMapping);
13647d523365SDimitry Andric }
13657d523365SDimitry Andric LI.removeEmptySubRanges();
13667d523365SDimitry Andric }
13672754fe60SDimitry Andric
13687d523365SDimitry Andric // Distribute main liverange.
13697d523365SDimitry Andric DistributeRange(LI, LIV, EqClass);
13702754fe60SDimitry Andric }
1371