blob: 2340b6abd87c8f44e92799f17ece00b91111d663 [file] [log] [blame]
Eugene Zelenko2de563a2017-08-24 21:21:39 +00001//===- LiveInterval.cpp - Live Interval Representation --------------------===//
Chris Lattnerfb449b92004-07-23 17:49:16 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerfb449b92004-07-23 17:49:16 +00007//
8//===----------------------------------------------------------------------===//
9//
Matthias Braun87a86052013-10-10 21:28:47 +000010// This file implements the LiveRange and LiveInterval classes. Given some
Chris Lattnerfb449b92004-07-23 17:49:16 +000011// numbering of each the machine instructions an interval [i, j) is said to be a
Matthias Braun87a86052013-10-10 21:28:47 +000012// live range for register v if there is no instruction with number j' >= j
Bob Wilson86af6552010-01-12 22:18:56 +000013// such that v is live at j' and there is no instruction with number i' < i such
Matthias Braun87a86052013-10-10 21:28:47 +000014// that v is live at i'. In this implementation ranges can have holes,
15// i.e. a range might look like [1,20), [50,65), [1000,1001). Each
16// individual segment is represented as an instance of LiveRange::Segment,
17// and the whole range is represented as an instance of LiveRange.
Chris Lattnerfb449b92004-07-23 17:49:16 +000018//
19//===----------------------------------------------------------------------===//
20
Bill Wendlingd9fd2ac2006-11-28 02:08:17 +000021#include "llvm/CodeGen/LiveInterval.h"
Matthias Braun1cd242f2016-05-31 22:38:06 +000022#include "LiveRangeUtils.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000023#include "RegisterCoalescer.h"
Eugene Zelenko2de563a2017-08-24 21:21:39 +000024#include "llvm/ADT/ArrayRef.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000025#include "llvm/ADT/STLExtras.h"
Eugene Zelenko2de563a2017-08-24 21:21:39 +000026#include "llvm/ADT/SmallPtrSet.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/iterator_range.h"
Matthias Braunfa621d22017-12-13 02:51:04 +000029#include "llvm/CodeGen/LiveIntervals.h"
Eugene Zelenko2de563a2017-08-24 21:21:39 +000030#include "llvm/CodeGen/MachineBasicBlock.h"
31#include "llvm/CodeGen/MachineInstr.h"
32#include "llvm/CodeGen/MachineOperand.h"
Evan Cheng90f95f82009-06-14 20:22:55 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Eugene Zelenko2de563a2017-08-24 21:21:39 +000034#include "llvm/CodeGen/SlotIndexes.h"
David Blaikiee3a9b4c2017-11-17 01:07:10 +000035#include "llvm/CodeGen/TargetRegisterInfo.h"
Nico Weber0f38c602018-04-30 14:59:11 +000036#include "llvm/Config/llvm-config.h"
Eugene Zelenko2de563a2017-08-24 21:21:39 +000037#include "llvm/MC/LaneBitmask.h"
38#include "llvm/Support/Compiler.h"
David Greene52421542010-01-04 22:41:43 +000039#include "llvm/Support/Debug.h"
Daniel Dunbara717b7b2009-07-24 10:47:20 +000040#include "llvm/Support/raw_ostream.h"
Alkis Evlogimenosc4d3b912004-09-28 02:38:58 +000041#include <algorithm>
Eugene Zelenko2de563a2017-08-24 21:21:39 +000042#include <cassert>
43#include <cstddef>
44#include <iterator>
45#include <utility>
46
Chris Lattnerfb449b92004-07-23 17:49:16 +000047using namespace llvm;
48
Benjamin Kramerbd8e1b12015-03-23 12:30:58 +000049namespace {
Eugene Zelenko2de563a2017-08-24 21:21:39 +000050
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +000051//===----------------------------------------------------------------------===//
52// Implementation of various methods necessary for calculation of live ranges.
53// The implementation of the methods abstracts from the concrete type of the
54// segment collection.
55//
56// Implementation of the class follows the Template design pattern. The base
57// class contains generic algorithms that call collection-specific methods,
58// which are provided in concrete subclasses. In order to avoid virtual calls
59// these methods are provided by means of C++ template instantiation.
60// The base class calls the methods of the subclass through method impl(),
61// which casts 'this' pointer to the type of the subclass.
62//
63//===----------------------------------------------------------------------===//
64
65template <typename ImplT, typename IteratorT, typename CollectionT>
66class CalcLiveRangeUtilBase {
67protected:
68 LiveRange *LR;
69
70protected:
71 CalcLiveRangeUtilBase(LiveRange *LR) : LR(LR) {}
72
73public:
Eugene Zelenko2de563a2017-08-24 21:21:39 +000074 using Segment = LiveRange::Segment;
75 using iterator = IteratorT;
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +000076
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +000077 /// A counterpart of LiveRange::createDeadDef: Make sure the range has a
78 /// value defined at @p Def.
79 /// If @p ForVNI is null, and there is no value defined at @p Def, a new
80 /// value will be allocated using @p VNInfoAllocator.
81 /// If @p ForVNI is null, the return value is the value defined at @p Def,
82 /// either a pre-existing one, or the one newly created.
83 /// If @p ForVNI is not null, then @p Def should be the location where
84 /// @p ForVNI is defined. If the range does not have a value defined at
85 /// @p Def, the value @p ForVNI will be used instead of allocating a new
86 /// one. If the range already has a value defined at @p Def, it must be
87 /// same as @p ForVNI. In either case, @p ForVNI will be the return value.
88 VNInfo *createDeadDef(SlotIndex Def, VNInfo::Allocator *VNInfoAllocator,
89 VNInfo *ForVNI) {
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +000090 assert(!Def.isDead() && "Cannot define a value at the dead slot");
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +000091 assert((!ForVNI || ForVNI->def == Def) &&
92 "If ForVNI is specified, it must match Def");
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +000093 iterator I = impl().find(Def);
94 if (I == segments().end()) {
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +000095 VNInfo *VNI = ForVNI ? ForVNI : LR->getNextValue(Def, *VNInfoAllocator);
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +000096 impl().insertAtEnd(Segment(Def, Def.getDeadSlot(), VNI));
97 return VNI;
98 }
99
100 Segment *S = segmentAt(I);
101 if (SlotIndex::isSameInstr(Def, S->start)) {
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000102 assert((!ForVNI || ForVNI == S->valno) && "Value number mismatch");
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000103 assert(S->valno->def == S->start && "Inconsistent existing value def");
104
105 // It is possible to have both normal and early-clobber defs of the same
106 // register on an instruction. It doesn't make a lot of sense, but it is
107 // possible to specify in inline assembly.
108 //
109 // Just convert everything to early-clobber.
110 Def = std::min(Def, S->start);
111 if (Def != S->start)
112 S->start = S->valno->def = Def;
113 return S->valno;
114 }
115 assert(SlotIndex::isEarlierInstr(Def, S->start) && "Already live at def");
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000116 VNInfo *VNI = ForVNI ? ForVNI : LR->getNextValue(Def, *VNInfoAllocator);
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000117 segments().insert(I, Segment(Def, Def.getDeadSlot(), VNI));
118 return VNI;
119 }
120
Matthias Braun62be98d2015-02-18 01:50:52 +0000121 VNInfo *extendInBlock(SlotIndex StartIdx, SlotIndex Use) {
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000122 if (segments().empty())
123 return nullptr;
124 iterator I =
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000125 impl().findInsertPos(Segment(Use.getPrevSlot(), Use, nullptr));
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000126 if (I == segments().begin())
127 return nullptr;
128 --I;
129 if (I->end <= StartIdx)
130 return nullptr;
Matthias Braun62be98d2015-02-18 01:50:52 +0000131 if (I->end < Use)
132 extendSegmentEndTo(I, Use);
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000133 return I->valno;
134 }
135
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000136 std::pair<VNInfo*,bool> extendInBlock(ArrayRef<SlotIndex> Undefs,
137 SlotIndex StartIdx, SlotIndex Use) {
138 if (segments().empty())
139 return std::make_pair(nullptr, false);
140 SlotIndex BeforeUse = Use.getPrevSlot();
141 iterator I = impl().findInsertPos(Segment(BeforeUse, Use, nullptr));
142 if (I == segments().begin())
143 return std::make_pair(nullptr, LR->isUndefIn(Undefs, StartIdx, BeforeUse));
144 --I;
145 if (I->end <= StartIdx)
146 return std::make_pair(nullptr, LR->isUndefIn(Undefs, StartIdx, BeforeUse));
147 if (I->end < Use) {
148 if (LR->isUndefIn(Undefs, I->end, BeforeUse))
149 return std::make_pair(nullptr, true);
150 extendSegmentEndTo(I, Use);
151 }
152 return std::make_pair(I->valno, false);
153 }
154
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000155 /// This method is used when we want to extend the segment specified
156 /// by I to end at the specified endpoint. To do this, we should
157 /// merge and eliminate all segments that this will overlap
158 /// with. The iterator is not invalidated.
159 void extendSegmentEndTo(iterator I, SlotIndex NewEnd) {
160 assert(I != segments().end() && "Not a valid segment!");
161 Segment *S = segmentAt(I);
162 VNInfo *ValNo = I->valno;
163
164 // Search for the first segment that we can't merge with.
165 iterator MergeTo = std::next(I);
166 for (; MergeTo != segments().end() && NewEnd >= MergeTo->end; ++MergeTo)
167 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
168
169 // If NewEnd was in the middle of a segment, make sure to get its endpoint.
170 S->end = std::max(NewEnd, std::prev(MergeTo)->end);
171
172 // If the newly formed segment now touches the segment after it and if they
173 // have the same value number, merge the two segments into one segment.
174 if (MergeTo != segments().end() && MergeTo->start <= I->end &&
175 MergeTo->valno == ValNo) {
176 S->end = MergeTo->end;
177 ++MergeTo;
178 }
179
180 // Erase any dead segments.
181 segments().erase(std::next(I), MergeTo);
182 }
183
184 /// This method is used when we want to extend the segment specified
185 /// by I to start at the specified endpoint. To do this, we should
186 /// merge and eliminate all segments that this will overlap with.
187 iterator extendSegmentStartTo(iterator I, SlotIndex NewStart) {
188 assert(I != segments().end() && "Not a valid segment!");
189 Segment *S = segmentAt(I);
190 VNInfo *ValNo = I->valno;
191
192 // Search for the first segment that we can't merge with.
193 iterator MergeTo = I;
194 do {
195 if (MergeTo == segments().begin()) {
196 S->start = NewStart;
197 segments().erase(MergeTo, I);
198 return I;
199 }
200 assert(MergeTo->valno == ValNo && "Cannot merge with differing values!");
201 --MergeTo;
202 } while (NewStart <= MergeTo->start);
203
204 // If we start in the middle of another segment, just delete a range and
205 // extend that segment.
206 if (MergeTo->end >= NewStart && MergeTo->valno == ValNo) {
207 segmentAt(MergeTo)->end = S->end;
208 } else {
209 // Otherwise, extend the segment right after.
210 ++MergeTo;
211 Segment *MergeToSeg = segmentAt(MergeTo);
212 MergeToSeg->start = NewStart;
213 MergeToSeg->end = S->end;
214 }
215
216 segments().erase(std::next(MergeTo), std::next(I));
217 return MergeTo;
218 }
219
220 iterator addSegment(Segment S) {
221 SlotIndex Start = S.start, End = S.end;
222 iterator I = impl().findInsertPos(S);
223
224 // If the inserted segment starts in the middle or right at the end of
225 // another segment, just extend that segment to contain the segment of S.
226 if (I != segments().begin()) {
227 iterator B = std::prev(I);
228 if (S.valno == B->valno) {
229 if (B->start <= Start && B->end >= Start) {
230 extendSegmentEndTo(B, End);
231 return B;
232 }
233 } else {
234 // Check to make sure that we are not overlapping two live segments with
235 // different valno's.
236 assert(B->end <= Start &&
237 "Cannot overlap two segments with differing ValID's"
238 " (did you def the same reg twice in a MachineInstr?)");
239 }
240 }
241
242 // Otherwise, if this segment ends in the middle of, or right next
243 // to, another segment, merge it into that segment.
244 if (I != segments().end()) {
245 if (S.valno == I->valno) {
246 if (I->start <= End) {
247 I = extendSegmentStartTo(I, Start);
248
249 // If S is a complete superset of a segment, we may need to grow its
250 // endpoint as well.
251 if (End > I->end)
252 extendSegmentEndTo(I, End);
253 return I;
254 }
255 } else {
256 // Check to make sure that we are not overlapping two live segments with
257 // different valno's.
258 assert(I->start >= End &&
259 "Cannot overlap two segments with differing ValID's");
260 }
261 }
262
263 // Otherwise, this is just a new segment that doesn't interact with
264 // anything.
265 // Insert it.
266 return segments().insert(I, S);
267 }
268
269private:
270 ImplT &impl() { return *static_cast<ImplT *>(this); }
271
272 CollectionT &segments() { return impl().segmentsColl(); }
273
274 Segment *segmentAt(iterator I) { return const_cast<Segment *>(&(*I)); }
275};
276
277//===----------------------------------------------------------------------===//
278// Instantiation of the methods for calculation of live ranges
279// based on a segment vector.
280//===----------------------------------------------------------------------===//
281
282class CalcLiveRangeUtilVector;
Eugene Zelenko2de563a2017-08-24 21:21:39 +0000283using CalcLiveRangeUtilVectorBase =
284 CalcLiveRangeUtilBase<CalcLiveRangeUtilVector, LiveRange::iterator,
285 LiveRange::Segments>;
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000286
287class CalcLiveRangeUtilVector : public CalcLiveRangeUtilVectorBase {
288public:
289 CalcLiveRangeUtilVector(LiveRange *LR) : CalcLiveRangeUtilVectorBase(LR) {}
290
291private:
292 friend CalcLiveRangeUtilVectorBase;
293
294 LiveRange::Segments &segmentsColl() { return LR->segments; }
295
296 void insertAtEnd(const Segment &S) { LR->segments.push_back(S); }
297
298 iterator find(SlotIndex Pos) { return LR->find(Pos); }
299
300 iterator findInsertPos(Segment S) {
301 return std::upper_bound(LR->begin(), LR->end(), S.start);
302 }
303};
304
305//===----------------------------------------------------------------------===//
306// Instantiation of the methods for calculation of live ranges
307// based on a segment set.
308//===----------------------------------------------------------------------===//
309
310class CalcLiveRangeUtilSet;
Eugene Zelenko2de563a2017-08-24 21:21:39 +0000311using CalcLiveRangeUtilSetBase =
312 CalcLiveRangeUtilBase<CalcLiveRangeUtilSet, LiveRange::SegmentSet::iterator,
313 LiveRange::SegmentSet>;
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000314
315class CalcLiveRangeUtilSet : public CalcLiveRangeUtilSetBase {
316public:
317 CalcLiveRangeUtilSet(LiveRange *LR) : CalcLiveRangeUtilSetBase(LR) {}
318
319private:
320 friend CalcLiveRangeUtilSetBase;
321
322 LiveRange::SegmentSet &segmentsColl() { return *LR->segmentSet; }
323
324 void insertAtEnd(const Segment &S) {
325 LR->segmentSet->insert(LR->segmentSet->end(), S);
326 }
327
328 iterator find(SlotIndex Pos) {
329 iterator I =
330 LR->segmentSet->upper_bound(Segment(Pos, Pos.getNextSlot(), nullptr));
331 if (I == LR->segmentSet->begin())
332 return I;
333 iterator PrevI = std::prev(I);
334 if (Pos < (*PrevI).end)
335 return PrevI;
336 return I;
337 }
338
339 iterator findInsertPos(Segment S) {
340 iterator I = LR->segmentSet->upper_bound(S);
341 if (I != LR->segmentSet->end() && !(S.start < *I))
342 ++I;
343 return I;
344 }
345};
Eugene Zelenko2de563a2017-08-24 21:21:39 +0000346
347} // end anonymous namespace
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000348
349//===----------------------------------------------------------------------===//
350// LiveRange methods
351//===----------------------------------------------------------------------===//
352
Matthias Braun87a86052013-10-10 21:28:47 +0000353LiveRange::iterator LiveRange::find(SlotIndex Pos) {
Jakob Stoklund Olesen55768d72011-03-12 01:50:35 +0000354 // This algorithm is basically std::upper_bound.
355 // Unfortunately, std::upper_bound cannot be used with mixed types until we
356 // adopt C++0x. Many libraries can do it, but not all.
357 if (empty() || Pos >= endIndex())
358 return end();
359 iterator I = begin();
Matthias Braunb63db852013-09-06 16:44:32 +0000360 size_t Len = size();
Jakob Stoklund Olesen55768d72011-03-12 01:50:35 +0000361 do {
362 size_t Mid = Len >> 1;
Richard Trieu1b96cbe2016-02-18 22:09:30 +0000363 if (Pos < I[Mid].end) {
Jakob Stoklund Olesen55768d72011-03-12 01:50:35 +0000364 Len = Mid;
Richard Trieu1b96cbe2016-02-18 22:09:30 +0000365 } else {
366 I += Mid + 1;
367 Len -= Mid + 1;
368 }
Jakob Stoklund Olesen55768d72011-03-12 01:50:35 +0000369 } while (Len);
370 return I;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000371}
372
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000373VNInfo *LiveRange::createDeadDef(SlotIndex Def, VNInfo::Allocator &VNIAlloc) {
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000374 // Use the segment set, if it is available.
375 if (segmentSet != nullptr)
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000376 return CalcLiveRangeUtilSet(this).createDeadDef(Def, &VNIAlloc, nullptr);
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000377 // Otherwise use the segment vector.
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000378 return CalcLiveRangeUtilVector(this).createDeadDef(Def, &VNIAlloc, nullptr);
379}
380
381VNInfo *LiveRange::createDeadDef(VNInfo *VNI) {
382 // Use the segment set, if it is available.
383 if (segmentSet != nullptr)
384 return CalcLiveRangeUtilSet(this).createDeadDef(VNI->def, nullptr, VNI);
385 // Otherwise use the segment vector.
386 return CalcLiveRangeUtilVector(this).createDeadDef(VNI->def, nullptr, VNI);
Jakob Stoklund Olesen4e53a402012-06-05 21:54:09 +0000387}
388
Matthias Braun87a86052013-10-10 21:28:47 +0000389// overlaps - Return true if the intersection of the two live ranges is
Chris Lattnerbae74d92004-11-18 03:47:34 +0000390// not empty.
391//
Chris Lattnerfb449b92004-07-23 17:49:16 +0000392// An example for overlaps():
393//
394// 0: A = ...
395// 4: B = ...
396// 8: C = A + B ;; last use of A
397//
Matthias Braun87a86052013-10-10 21:28:47 +0000398// The live ranges should look like:
Chris Lattnerfb449b92004-07-23 17:49:16 +0000399//
400// A = [3, 11)
401// B = [7, x)
402// C = [11, y)
403//
404// A->overlaps(C) should return false since we want to be able to join
405// A and C.
Chris Lattnerbae74d92004-11-18 03:47:34 +0000406//
Matthias Braun87a86052013-10-10 21:28:47 +0000407bool LiveRange::overlapsFrom(const LiveRange& other,
408 const_iterator StartPos) const {
409 assert(!empty() && "empty range");
Chris Lattnerbae74d92004-11-18 03:47:34 +0000410 const_iterator i = begin();
411 const_iterator ie = end();
412 const_iterator j = StartPos;
413 const_iterator je = other.end();
414
415 assert((StartPos->start <= i->start || StartPos == other.begin()) &&
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000416 StartPos != other.end() && "Bogus start position hint!");
Chris Lattnerf5426492004-07-25 07:11:19 +0000417
Chris Lattnerfb449b92004-07-23 17:49:16 +0000418 if (i->start < j->start) {
Chris Lattneraa141472004-07-23 18:40:00 +0000419 i = std::upper_bound(i, ie, j->start);
Matthias Braunb63db852013-09-06 16:44:32 +0000420 if (i != begin()) --i;
Chris Lattneraa141472004-07-23 18:40:00 +0000421 } else if (j->start < i->start) {
Chris Lattneread1b3f2004-12-04 01:22:09 +0000422 ++StartPos;
423 if (StartPos != other.end() && StartPos->start <= i->start) {
424 assert(StartPos < other.end() && i < end());
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000425 j = std::upper_bound(j, je, i->start);
Matthias Braunb63db852013-09-06 16:44:32 +0000426 if (j != other.begin()) --j;
Chris Lattner8c68b6a2004-11-18 04:02:11 +0000427 }
Chris Lattneraa141472004-07-23 18:40:00 +0000428 } else {
429 return true;
Chris Lattnerfb449b92004-07-23 17:49:16 +0000430 }
431
Chris Lattner9fddc122004-11-18 05:28:21 +0000432 if (j == je) return false;
433
434 while (i != ie) {
Chris Lattnerfb449b92004-07-23 17:49:16 +0000435 if (i->start > j->start) {
Alkis Evlogimenosa1613db2004-07-24 11:44:15 +0000436 std::swap(i, j);
437 std::swap(ie, je);
Chris Lattnerfb449b92004-07-23 17:49:16 +0000438 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000439
440 if (i->end > j->start)
441 return true;
442 ++i;
443 }
444
445 return false;
446}
447
Matthias Braun87a86052013-10-10 21:28:47 +0000448bool LiveRange::overlaps(const LiveRange &Other, const CoalescerPair &CP,
449 const SlotIndexes &Indexes) const {
450 assert(!empty() && "empty range");
Jakob Stoklund Olesen45c5c572012-09-06 18:15:23 +0000451 if (Other.empty())
452 return false;
453
454 // Use binary searches to find initial positions.
455 const_iterator I = find(Other.beginIndex());
456 const_iterator IE = end();
457 if (I == IE)
458 return false;
459 const_iterator J = Other.find(I->start);
460 const_iterator JE = Other.end();
461 if (J == JE)
462 return false;
463
Eugene Zelenko2de563a2017-08-24 21:21:39 +0000464 while (true) {
Jakob Stoklund Olesen45c5c572012-09-06 18:15:23 +0000465 // J has just been advanced to satisfy:
466 assert(J->end >= I->start);
467 // Check for an overlap.
468 if (J->start < I->end) {
469 // I and J are overlapping. Find the later start.
470 SlotIndex Def = std::max(I->start, J->start);
471 // Allow the overlap if Def is a coalescable copy.
472 if (Def.isBlock() ||
473 !CP.isCoalescable(Indexes.getInstructionFromIndex(Def)))
474 return true;
475 }
476 // Advance the iterator that ends first to check for more overlaps.
477 if (J->end > I->end) {
478 std::swap(I, J);
479 std::swap(IE, JE);
480 }
481 // Advance J until J->end >= I->start.
482 do
483 if (++J == JE)
484 return false;
485 while (J->end < I->start);
486 }
487}
488
Matthias Braun87a86052013-10-10 21:28:47 +0000489/// overlaps - Return true if the live range overlaps an interval specified
Evan Chengcccdb2b2009-04-18 08:52:15 +0000490/// by [Start, End).
Matthias Braun87a86052013-10-10 21:28:47 +0000491bool LiveRange::overlaps(SlotIndex Start, SlotIndex End) const {
Evan Chengcccdb2b2009-04-18 08:52:15 +0000492 assert(Start < End && "Invalid range");
Jakob Stoklund Olesen186eb732010-07-13 19:42:20 +0000493 const_iterator I = std::lower_bound(begin(), end(), End);
494 return I != begin() && (--I)->end > Start;
Evan Chengcccdb2b2009-04-18 08:52:15 +0000495}
496
Matthias Braun58747142014-12-10 01:12:06 +0000497bool LiveRange::covers(const LiveRange &Other) const {
498 if (empty())
499 return Other.empty();
500
501 const_iterator I = begin();
Matthias Braun218d20a2014-12-10 23:07:54 +0000502 for (const Segment &O : Other.segments) {
503 I = advanceTo(I, O.start);
504 if (I == end() || I->start > O.start)
Matthias Braun58747142014-12-10 01:12:06 +0000505 return false;
506
Matthias Braun218d20a2014-12-10 23:07:54 +0000507 // Check adjacent live segments and see if we can get behind O.end.
508 while (I->end < O.end) {
Matthias Braun58747142014-12-10 01:12:06 +0000509 const_iterator Last = I;
510 // Get next segment and abort if it was not adjacent.
511 ++I;
512 if (I == end() || Last->end != I->start)
513 return false;
514 }
515 }
516 return true;
517}
Lang Hames6f4e4df2010-07-26 01:49:41 +0000518
519/// ValNo is dead, remove it. If it is the largest value number, just nuke it
520/// (and any other deleted values neighboring it), otherwise mark it as ~1U so
521/// it can be nuked later.
Matthias Braun87a86052013-10-10 21:28:47 +0000522void LiveRange::markValNoForDeletion(VNInfo *ValNo) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000523 if (ValNo->id == getNumValNums()-1) {
524 do {
525 valnos.pop_back();
526 } while (!valnos.empty() && valnos.back()->isUnused());
527 } else {
Jakob Stoklund Olesenb2beac22012-08-03 20:59:32 +0000528 ValNo->markUnused();
Lang Hames6f4e4df2010-07-26 01:49:41 +0000529 }
530}
531
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000532/// RenumberValues - Renumber all values in order of appearance and delete the
533/// remaining unused values.
Matthias Braun87a86052013-10-10 21:28:47 +0000534void LiveRange::RenumberValues() {
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000535 SmallPtrSet<VNInfo*, 8> Seen;
536 valnos.clear();
Matthias Braun218d20a2014-12-10 23:07:54 +0000537 for (const Segment &S : segments) {
538 VNInfo *VNI = S.valno;
David Blaikie5401ba72014-11-19 07:49:26 +0000539 if (!Seen.insert(VNI).second)
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000540 continue;
Matthias Braun331de112013-10-10 21:28:43 +0000541 assert(!VNI->isUnused() && "Unused valno used by live segment");
Jakob Stoklund Olesen23436592010-08-06 18:46:59 +0000542 VNI->id = (unsigned)valnos.size();
543 valnos.push_back(VNI);
544 }
545}
546
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000547void LiveRange::addSegmentToSet(Segment S) {
548 CalcLiveRangeUtilSet(this).addSegment(S);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000549}
550
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000551LiveRange::iterator LiveRange::addSegment(Segment S) {
552 // Use the segment set, if it is available.
553 if (segmentSet != nullptr) {
554 addSegmentToSet(S);
555 return end();
Chris Lattnerb26c2152004-07-23 19:38:44 +0000556 }
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000557 // Otherwise use the segment vector.
558 return CalcLiveRangeUtilVector(this).addSegment(S);
Chris Lattnerb26c2152004-07-23 19:38:44 +0000559}
560
Matthias Braun88824142014-12-24 02:11:51 +0000561void LiveRange::append(const Segment S) {
562 // Check that the segment belongs to the back of the list.
563 assert(segments.empty() || segments.back().end <= S.start);
564 segments.push_back(S);
565}
566
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000567std::pair<VNInfo*,bool> LiveRange::extendInBlock(ArrayRef<SlotIndex> Undefs,
568 SlotIndex StartIdx, SlotIndex Kill) {
569 // Use the segment set, if it is available.
570 if (segmentSet != nullptr)
571 return CalcLiveRangeUtilSet(this).extendInBlock(Undefs, StartIdx, Kill);
572 // Otherwise use the segment vector.
573 return CalcLiveRangeUtilVector(this).extendInBlock(Undefs, StartIdx, Kill);
574}
575
Matthias Braun87a86052013-10-10 21:28:47 +0000576VNInfo *LiveRange::extendInBlock(SlotIndex StartIdx, SlotIndex Kill) {
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000577 // Use the segment set, if it is available.
578 if (segmentSet != nullptr)
579 return CalcLiveRangeUtilSet(this).extendInBlock(StartIdx, Kill);
580 // Otherwise use the segment vector.
581 return CalcLiveRangeUtilVector(this).extendInBlock(StartIdx, Kill);
Jakob Stoklund Olesen9763e2b2011-03-02 00:06:15 +0000582}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000583
Matthias Braun87a86052013-10-10 21:28:47 +0000584/// Remove the specified segment from this range. Note that the segment must
Matthias Braun331de112013-10-10 21:28:43 +0000585/// be in a single Segment in its entirety.
Matthias Braun87a86052013-10-10 21:28:47 +0000586void LiveRange::removeSegment(SlotIndex Start, SlotIndex End,
587 bool RemoveDeadValNo) {
Matthias Braun331de112013-10-10 21:28:43 +0000588 // Find the Segment containing this span.
Matthias Braunb63db852013-09-06 16:44:32 +0000589 iterator I = find(Start);
Matthias Braun87a86052013-10-10 21:28:47 +0000590 assert(I != end() && "Segment is not in range!");
Matthias Braun331de112013-10-10 21:28:43 +0000591 assert(I->containsInterval(Start, End)
Matthias Braun87a86052013-10-10 21:28:47 +0000592 && "Segment is not entirely in range!");
Chris Lattnerabf295f2004-07-24 02:52:23 +0000593
Matthias Braun331de112013-10-10 21:28:43 +0000594 // If the span we are removing is at the start of the Segment, adjust it.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000595 VNInfo *ValNo = I->valno;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000596 if (I->start == Start) {
Evan Cheng4f8ff162007-08-11 00:59:19 +0000597 if (I->end == End) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000598 if (RemoveDeadValNo) {
599 // Check if val# is dead.
600 bool isDead = true;
601 for (const_iterator II = begin(), EE = end(); II != EE; ++II)
602 if (II != I && II->valno == ValNo) {
603 isDead = false;
604 break;
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000605 }
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000606 if (isDead) {
Lang Hames6f4e4df2010-07-26 01:49:41 +0000607 // Now that ValNo is dead, remove it.
608 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000609 }
610 }
611
Matthias Braun331de112013-10-10 21:28:43 +0000612 segments.erase(I); // Removed the whole Segment.
Evan Cheng4f8ff162007-08-11 00:59:19 +0000613 } else
Chris Lattnerabf295f2004-07-24 02:52:23 +0000614 I->start = End;
615 return;
616 }
617
Matthias Braun331de112013-10-10 21:28:43 +0000618 // Otherwise if the span we are removing is at the end of the Segment,
Chris Lattnerabf295f2004-07-24 02:52:23 +0000619 // adjust the other way.
620 if (I->end == End) {
Chris Lattner6925a9f2004-07-25 05:43:53 +0000621 I->end = Start;
Chris Lattnerabf295f2004-07-24 02:52:23 +0000622 return;
623 }
624
Matthias Braun331de112013-10-10 21:28:43 +0000625 // Otherwise, we are splitting the Segment into two pieces.
Lang Hames233a60e2009-11-03 23:52:08 +0000626 SlotIndex OldEnd = I->end;
Matthias Braun87a86052013-10-10 21:28:47 +0000627 I->end = Start; // Trim the old segment.
Chris Lattnerabf295f2004-07-24 02:52:23 +0000628
629 // Insert the new one.
Benjamin Kramerd628f192014-03-02 12:27:27 +0000630 segments.insert(std::next(I), Segment(End, OldEnd, ValNo));
Chris Lattnerabf295f2004-07-24 02:52:23 +0000631}
632
Matthias Braun331de112013-10-10 21:28:43 +0000633/// removeValNo - Remove all the segments defined by the specified value#.
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000634/// Also remove the value# from value# list.
Matthias Braun87a86052013-10-10 21:28:47 +0000635void LiveRange::removeValNo(VNInfo *ValNo) {
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000636 if (empty()) return;
David Majnemer5d08e372016-08-12 04:32:37 +0000637 segments.erase(remove_if(*this, [ValNo](const Segment &S) {
Benjamin Kramer2bcfcc12015-02-28 20:14:27 +0000638 return S.valno == ValNo;
639 }), end());
Lang Hames6f4e4df2010-07-26 01:49:41 +0000640 // Now that ValNo is dead, remove it.
641 markValNoForDeletion(ValNo);
Evan Chengd2b8d7b2008-02-13 02:48:26 +0000642}
Lang Hames86511252009-09-04 20:41:11 +0000643
Matthias Braun87a86052013-10-10 21:28:47 +0000644void LiveRange::join(LiveRange &Other,
645 const int *LHSValNoAssignments,
646 const int *RHSValNoAssignments,
647 SmallVectorImpl<VNInfo *> &NewVNInfo) {
Chandler Carruth261b6332012-07-10 05:06:03 +0000648 verify();
649
Matthias Braun331de112013-10-10 21:28:43 +0000650 // Determine if any of our values are mapped. This is uncommon, so we want
Matthias Braun87a86052013-10-10 21:28:47 +0000651 // to avoid the range scan if not.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000652 bool MustMapCurValNos = false;
Evan Cheng34301352007-09-01 02:03:17 +0000653 unsigned NumVals = getNumValNums();
654 unsigned NumNewVals = NewVNInfo.size();
655 for (unsigned i = 0; i != NumVals; ++i) {
656 unsigned LHSValID = LHSValNoAssignments[i];
657 if (i != LHSValID ||
Lang Hamesd88710a2012-02-02 06:55:45 +0000658 (NewVNInfo[LHSValID] && NewVNInfo[LHSValID] != getValNumInfo(i))) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000659 MustMapCurValNos = true;
Lang Hamesd88710a2012-02-02 06:55:45 +0000660 break;
661 }
Chris Lattnerdeb99712004-07-24 03:41:50 +0000662 }
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000663
Matthias Braun87a86052013-10-10 21:28:47 +0000664 // If we have to apply a mapping to our base range assignment, rewrite it now.
Jakob Stoklund Olesen657720b2012-09-27 21:05:59 +0000665 if (MustMapCurValNos && !empty()) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000666 // Map the first live range.
Lang Hames02e08d52012-02-02 05:37:34 +0000667
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000668 iterator OutIt = begin();
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000669 OutIt->valno = NewVNInfo[LHSValNoAssignments[OutIt->valno->id]];
Benjamin Kramerd628f192014-03-02 12:27:27 +0000670 for (iterator I = std::next(OutIt), E = end(); I != E; ++I) {
Lang Hames02e08d52012-02-02 05:37:34 +0000671 VNInfo* nextValNo = NewVNInfo[LHSValNoAssignments[I->valno->id]];
Craig Topper4ba84432014-04-14 00:51:57 +0000672 assert(nextValNo && "Huh?");
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000673
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000674 // If this live range has the same value # as its immediate predecessor,
Matthias Braun331de112013-10-10 21:28:43 +0000675 // and if they are neighbors, remove one Segment. This happens when we
Lang Hames02e08d52012-02-02 05:37:34 +0000676 // have [0,4:0)[4,7:1) and map 0/1 onto the same value #.
677 if (OutIt->valno == nextValNo && OutIt->end == I->start) {
678 OutIt->end = I->end;
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000679 } else {
Matthias Braun87a86052013-10-10 21:28:47 +0000680 // Didn't merge. Move OutIt to the next segment,
Lang Hames02e08d52012-02-02 05:37:34 +0000681 ++OutIt;
682 OutIt->valno = nextValNo;
683 if (OutIt != I) {
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000684 OutIt->start = I->start;
685 OutIt->end = I->end;
686 }
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000687 }
688 }
Matthias Braun331de112013-10-10 21:28:43 +0000689 // If we merge some segments, chop off the end.
Lang Hames02e08d52012-02-02 05:37:34 +0000690 ++OutIt;
Matthias Braun331de112013-10-10 21:28:43 +0000691 segments.erase(OutIt, end());
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000692 }
Evan Cheng4f8ff162007-08-11 00:59:19 +0000693
Jakob Stoklund Olesen9bd7c3c2013-02-20 23:51:10 +0000694 // Rewrite Other values before changing the VNInfo ids.
695 // This can leave Other in an invalid state because we're not coalescing
696 // touching segments that now have identical values. That's OK since Other is
697 // not supposed to be valid after calling join();
Matthias Braun218d20a2014-12-10 23:07:54 +0000698 for (Segment &S : Other.segments)
699 S.valno = NewVNInfo[RHSValNoAssignments[S.valno->id]];
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000700
701 // Update val# info. Renumber them and make sure they all belong to this
Matthias Braun87a86052013-10-10 21:28:47 +0000702 // LiveRange now. Also remove dead val#'s.
Evan Chengf3bb2e62007-09-05 21:46:51 +0000703 unsigned NumValNos = 0;
704 for (unsigned i = 0; i < NumNewVals; ++i) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000705 VNInfo *VNI = NewVNInfo[i];
Evan Chengf3bb2e62007-09-05 21:46:51 +0000706 if (VNI) {
Evan Cheng30590f52009-04-28 06:24:09 +0000707 if (NumValNos >= NumVals)
Evan Chengf3bb2e62007-09-05 21:46:51 +0000708 valnos.push_back(VNI);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000709 else
Evan Chengf3bb2e62007-09-05 21:46:51 +0000710 valnos[NumValNos] = VNI;
711 VNI->id = NumValNos++; // Renumber val#.
Evan Cheng34301352007-09-01 02:03:17 +0000712 }
713 }
Evan Cheng34301352007-09-01 02:03:17 +0000714 if (NumNewVals < NumVals)
715 valnos.resize(NumNewVals); // shrinkify
Evan Cheng4f8ff162007-08-11 00:59:19 +0000716
Matthias Braun331de112013-10-10 21:28:43 +0000717 // Okay, now insert the RHS live segments into the LHS.
Jakob Stoklund Olesend983d4c2013-02-20 18:18:15 +0000718 LiveRangeUpdater Updater(this);
Matthias Braun218d20a2014-12-10 23:07:54 +0000719 for (Segment &S : Other.segments)
720 Updater.add(S);
Chandler Carruthe585e752012-07-10 05:16:17 +0000721}
722
Matthias Braun87a86052013-10-10 21:28:47 +0000723/// Merge all of the segments in RHS into this live range as the specified
Matthias Braun331de112013-10-10 21:28:43 +0000724/// value number. The segments in RHS are allowed to overlap with segments in
Matthias Braun87a86052013-10-10 21:28:47 +0000725/// the current range, but only if the overlapping segments have the
Matthias Braun331de112013-10-10 21:28:43 +0000726/// specified value number.
Matthias Braun87a86052013-10-10 21:28:47 +0000727void LiveRange::MergeSegmentsInAsValue(const LiveRange &RHS,
728 VNInfo *LHSValNo) {
Jakob Stoklund Olesend983d4c2013-02-20 18:18:15 +0000729 LiveRangeUpdater Updater(this);
Matthias Braun218d20a2014-12-10 23:07:54 +0000730 for (const Segment &S : RHS.segments)
731 Updater.add(S.start, S.end, LHSValNo);
Chris Lattnerf21f0202006-09-02 05:26:59 +0000732}
733
Matthias Braun331de112013-10-10 21:28:43 +0000734/// MergeValueInAsValue - Merge all of the live segments of a specific val#
Matthias Braun87a86052013-10-10 21:28:47 +0000735/// in RHS into this live range as the specified value number.
Matthias Braun331de112013-10-10 21:28:43 +0000736/// The segments in RHS are allowed to overlap with segments in the
Matthias Braun87a86052013-10-10 21:28:47 +0000737/// current range, it will replace the value numbers of the overlaped
Matthias Braun331de112013-10-10 21:28:43 +0000738/// segments with the specified value number.
Matthias Braun87a86052013-10-10 21:28:47 +0000739void LiveRange::MergeValueInAsValue(const LiveRange &RHS,
740 const VNInfo *RHSValNo,
741 VNInfo *LHSValNo) {
Jakob Stoklund Olesend983d4c2013-02-20 18:18:15 +0000742 LiveRangeUpdater Updater(this);
Matthias Braun218d20a2014-12-10 23:07:54 +0000743 for (const Segment &S : RHS.segments)
744 if (S.valno == RHSValNo)
745 Updater.add(S.start, S.end, LHSValNo);
Evan Cheng32dfbea2007-10-12 08:50:34 +0000746}
747
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000748/// MergeValueNumberInto - This method is called when two value nubmers
749/// are found to be equivalent. This eliminates V1, replacing all
Matthias Braun331de112013-10-10 21:28:43 +0000750/// segments with the V1 value number with the V2 value number. This can
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000751/// cause merging of V1/V2 values numbers and compaction of the value space.
Matthias Braun87a86052013-10-10 21:28:47 +0000752VNInfo *LiveRange::MergeValueNumberInto(VNInfo *V1, VNInfo *V2) {
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000753 assert(V1 != V2 && "Identical value#'s are always equivalent!");
754
755 // This code actually merges the (numerically) larger value number into the
756 // smaller value number, which is likely to allow us to compactify the value
757 // space. The only thing we have to be careful of is to preserve the
758 // instruction that defines the result value.
759
760 // Make sure V2 is smaller than V1.
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000761 if (V1->id < V2->id) {
Lang Hames52c1afc2009-08-10 23:43:28 +0000762 V1->copyFrom(*V2);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000763 std::swap(V1, V2);
764 }
765
Matthias Braun331de112013-10-10 21:28:43 +0000766 // Merge V1 segments into V2.
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000767 for (iterator I = begin(); I != end(); ) {
Matthias Braun331de112013-10-10 21:28:43 +0000768 iterator S = I++;
769 if (S->valno != V1) continue; // Not a V1 Segment.
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000770
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000771 // Okay, we found a V1 live range. If it had a previous, touching, V2 live
772 // range, extend it.
Matthias Braun331de112013-10-10 21:28:43 +0000773 if (S != begin()) {
774 iterator Prev = S-1;
775 if (Prev->valno == V2 && Prev->end == S->start) {
776 Prev->end = S->end;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000777
778 // Erase this live-range.
Matthias Braun331de112013-10-10 21:28:43 +0000779 segments.erase(S);
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000780 I = Prev+1;
Matthias Braun331de112013-10-10 21:28:43 +0000781 S = Prev;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000782 }
783 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000784
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000785 // Okay, now we have a V1 or V2 live range that is maximally merged forward.
786 // Ensure that it is a V2 live-range.
Matthias Braun331de112013-10-10 21:28:43 +0000787 S->valno = V2;
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000788
Matthias Braun331de112013-10-10 21:28:43 +0000789 // If we can merge it into later V2 segments, do so now. We ignore any
790 // following V1 segments, as they will be merged in subsequent iterations
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000791 // of the loop.
792 if (I != end()) {
Matthias Braun331de112013-10-10 21:28:43 +0000793 if (I->start == S->end && I->valno == V2) {
794 S->end = I->end;
795 segments.erase(I);
796 I = S+1;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000797 }
798 }
799 }
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000800
Lang Hames6f4e4df2010-07-26 01:49:41 +0000801 // Now that V1 is dead, remove it.
802 markValNoForDeletion(V1);
Jakob Stoklund Olesen1b293202010-08-12 20:01:23 +0000803
Owen Anderson5b93f6f2009-02-02 22:42:01 +0000804 return V2;
Chris Lattnerf7da2c72006-08-24 22:43:55 +0000805}
806
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000807void LiveRange::flushSegmentSet() {
808 assert(segmentSet != nullptr && "segment set must have been created");
809 assert(
810 segments.empty() &&
811 "segment set can be used only initially before switching to the array");
812 segments.append(segmentSet->begin(), segmentSet->end());
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +0000813 segmentSet = nullptr;
814 verify();
815}
816
Andrew Kaylorfb2b3b02016-02-08 22:52:51 +0000817bool LiveRange::isLiveAtIndexes(ArrayRef<SlotIndex> Slots) const {
818 ArrayRef<SlotIndex>::iterator SlotI = Slots.begin();
819 ArrayRef<SlotIndex>::iterator SlotE = Slots.end();
820
821 // If there are no regmask slots, we have nothing to search.
822 if (SlotI == SlotE)
823 return false;
824
825 // Start our search at the first segment that ends after the first slot.
826 const_iterator SegmentI = find(*SlotI);
827 const_iterator SegmentE = end();
828
829 // If there are no segments that end after the first slot, we're done.
830 if (SegmentI == SegmentE)
831 return false;
832
833 // Look for each slot in the live range.
834 for ( ; SlotI != SlotE; ++SlotI) {
835 // Go to the next segment that ends after the current slot.
836 // The slot may be within a hole in the range.
837 SegmentI = advanceTo(SegmentI, *SlotI);
838 if (SegmentI == SegmentE)
839 return false;
840
841 // If this segment contains the slot, we're done.
842 if (SegmentI->contains(*SlotI))
843 return true;
844 // Otherwise, look for the next slot.
845 }
846
847 // We didn't find a segment containing any of the slots.
848 return false;
849}
850
Matthias Braune81cc342015-02-06 17:28:47 +0000851void LiveInterval::freeSubRange(SubRange *S) {
852 S->~SubRange();
853 // Memory was allocated with BumpPtr allocator and is not freed here.
854}
855
Matthias Braunf2f05892014-12-10 01:12:40 +0000856void LiveInterval::removeEmptySubRanges() {
857 SubRange **NextPtr = &SubRanges;
858 SubRange *I = *NextPtr;
859 while (I != nullptr) {
860 if (!I->empty()) {
861 NextPtr = &I->Next;
862 I = *NextPtr;
863 continue;
864 }
865 // Skip empty subranges until we find the first nonempty one.
866 do {
Matthias Braune81cc342015-02-06 17:28:47 +0000867 SubRange *Next = I->Next;
868 freeSubRange(I);
869 I = Next;
Matthias Braunf2f05892014-12-10 01:12:40 +0000870 } while (I != nullptr && I->empty());
871 *NextPtr = I;
872 }
873}
874
Matthias Braune81cc342015-02-06 17:28:47 +0000875void LiveInterval::clearSubRanges() {
876 for (SubRange *I = SubRanges, *Next; I != nullptr; I = Next) {
877 Next = I->Next;
878 freeSubRange(I);
879 }
880 SubRanges = nullptr;
881}
882
Matthias Braunb6ebe7d2017-03-03 19:05:34 +0000883void LiveInterval::refineSubRanges(BumpPtrAllocator &Allocator,
884 LaneBitmask LaneMask, std::function<void(LiveInterval::SubRange&)> Apply) {
Matthias Braunb6ebe7d2017-03-03 19:05:34 +0000885 LaneBitmask ToApply = LaneMask;
886 for (SubRange &SR : subranges()) {
887 LaneBitmask SRMask = SR.LaneMask;
888 LaneBitmask Matching = SRMask & LaneMask;
889 if (Matching.none())
890 continue;
891
892 SubRange *MatchingRange;
893 if (SRMask == Matching) {
894 // The subrange fits (it does not cover bits outside \p LaneMask).
895 MatchingRange = &SR;
896 } else {
897 // We have to split the subrange into a matching and non-matching part.
898 // Reduce lanemask of existing lane to non-matching part.
899 SR.LaneMask = SRMask & ~Matching;
900 // Create a new subrange for the matching part
901 MatchingRange = createSubRangeFrom(Allocator, Matching, SR);
902 }
903 Apply(*MatchingRange);
904 ToApply &= ~Matching;
905 }
906 // Create a new subrange if there are uncovered bits left.
907 if (ToApply.any()) {
908 SubRange *NewRange = createSubRange(Allocator, ToApply);
909 Apply(*NewRange);
910 }
911}
912
Evan Chenge52eef82007-04-17 20:25:11 +0000913unsigned LiveInterval::getSize() const {
914 unsigned Sum = 0;
Matthias Braun218d20a2014-12-10 23:07:54 +0000915 for (const Segment &S : segments)
916 Sum += S.start.distance(S.end);
Evan Chenge52eef82007-04-17 20:25:11 +0000917 return Sum;
918}
919
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000920void LiveInterval::computeSubRangeUndefs(SmallVectorImpl<SlotIndex> &Undefs,
921 LaneBitmask LaneMask,
922 const MachineRegisterInfo &MRI,
923 const SlotIndexes &Indexes) const {
924 assert(TargetRegisterInfo::isVirtualRegister(reg));
925 LaneBitmask VRegMask = MRI.getMaxLaneMaskForVReg(reg);
Krzysztof Parzyszek308c60d2016-12-16 19:11:56 +0000926 assert((VRegMask & LaneMask).any());
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000927 const TargetRegisterInfo &TRI = *MRI.getTargetRegisterInfo();
928 for (const MachineOperand &MO : MRI.def_operands(reg)) {
929 if (!MO.isUndef())
930 continue;
931 unsigned SubReg = MO.getSubReg();
932 assert(SubReg != 0 && "Undef should only be set on subreg defs");
933 LaneBitmask DefMask = TRI.getSubRegIndexLaneMask(SubReg);
934 LaneBitmask UndefMask = VRegMask & ~DefMask;
Krzysztof Parzyszek308c60d2016-12-16 19:11:56 +0000935 if ((UndefMask & LaneMask).any()) {
Krzysztof Parzyszek31a5f882016-08-24 13:37:55 +0000936 const MachineInstr &MI = *MO.getParent();
937 bool EarlyClobber = MO.isEarlyClobber();
938 SlotIndex Pos = Indexes.getInstructionIndex(MI).getRegSlot(EarlyClobber);
939 Undefs.push_back(Pos);
940 }
941 }
942}
943
Eugene Zelenko2de563a2017-08-24 21:21:39 +0000944raw_ostream& llvm::operator<<(raw_ostream& OS, const LiveRange::Segment &S) {
945 return OS << '[' << S.start << ',' << S.end << ':' << S.valno->id << ')';
Daniel Dunbar1cd1d982009-07-24 10:36:58 +0000946}
Chris Lattnerfb449b92004-07-23 17:49:16 +0000947
Aaron Ballman1d03d382017-10-15 14:32:27 +0000948#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +0000949LLVM_DUMP_METHOD void LiveRange::Segment::dump() const {
Krzysztof Parzyszek29e2ed12016-07-12 17:37:44 +0000950 dbgs() << *this << '\n';
Chris Lattnerabf295f2004-07-24 02:52:23 +0000951}
Manman Ren77e300e2012-09-06 19:06:06 +0000952#endif
Chris Lattnerabf295f2004-07-24 02:52:23 +0000953
Matthias Braun87a86052013-10-10 21:28:47 +0000954void LiveRange::print(raw_ostream &OS) const {
Chris Lattner38135af2005-05-14 05:34:15 +0000955 if (empty())
Jakob Stoklund Olesenb77ec7d2012-06-05 22:51:54 +0000956 OS << "EMPTY";
Chris Lattner38135af2005-05-14 05:34:15 +0000957 else {
Matthias Braun218d20a2014-12-10 23:07:54 +0000958 for (const Segment &S : segments) {
959 OS << S;
960 assert(S.valno == getValNumInfo(S.valno->id) && "Bad VNInfo");
Jakob Stoklund Olesen014b8632010-06-23 15:34:36 +0000961 }
Chris Lattner38135af2005-05-14 05:34:15 +0000962 }
Jakob Stoklund Olesen15a57142010-06-25 22:53:05 +0000963
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000964 // Print value number info.
Chris Lattner6d8fbef2006-08-29 23:18:15 +0000965 if (getNumValNums()) {
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000966 OS << " ";
Evan Cheng1a66f0a2007-08-28 08:28:51 +0000967 unsigned vnum = 0;
968 for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
969 ++i, ++vnum) {
Evan Cheng7ecb38b2007-08-29 20:45:00 +0000970 const VNInfo *vni = *i;
Krzysztof Parzyszek29e2ed12016-07-12 17:37:44 +0000971 if (vnum) OS << ' ';
972 OS << vnum << '@';
Lang Hames857c4e02009-06-17 21:01:20 +0000973 if (vni->isUnused()) {
Krzysztof Parzyszek29e2ed12016-07-12 17:37:44 +0000974 OS << 'x';
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000975 } else {
Lang Hames6e2968c2010-09-25 12:04:16 +0000976 OS << vni->def;
Jakob Stoklund Olesena818c072010-10-05 18:48:57 +0000977 if (vni->isPHIDef())
Jakob Stoklund Olesenbf60aa92012-08-03 20:19:44 +0000978 OS << "-phi";
Evan Chenga8d94f12007-08-07 23:49:57 +0000979 }
Chris Lattnerbe4f88a2006-08-22 18:19:46 +0000980 }
981 }
Chris Lattnerfb449b92004-07-23 17:49:16 +0000982}
Chris Lattnerabf295f2004-07-24 02:52:23 +0000983
Krzysztof Parzyszek29e2ed12016-07-12 17:37:44 +0000984void LiveInterval::SubRange::print(raw_ostream &OS) const {
985 OS << " L" << PrintLaneMask(LaneMask) << ' '
986 << static_cast<const LiveRange&>(*this);
987}
988
Matthias Braun03d96092013-10-10 21:29:05 +0000989void LiveInterval::print(raw_ostream &OS) const {
Francis Visoiu Mistrihaccb3372017-11-28 12:42:37 +0000990 OS << printReg(reg) << ' ';
Matthias Braun03d96092013-10-10 21:29:05 +0000991 super::print(OS);
Matthias Braun01ddf042014-12-10 01:12:10 +0000992 // Print subranges
Krzysztof Parzyszek29e2ed12016-07-12 17:37:44 +0000993 for (const SubRange &SR : subranges())
994 OS << SR;
Matthias Braun88fdf292018-01-29 22:03:00 +0000995 OS << " weight:" << weight;
Matthias Braun03d96092013-10-10 21:29:05 +0000996}
997
Aaron Ballman1d03d382017-10-15 14:32:27 +0000998#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Yaron Keren55307982016-01-29 20:50:44 +0000999LLVM_DUMP_METHOD void LiveRange::dump() const {
Krzysztof Parzyszek29e2ed12016-07-12 17:37:44 +00001000 dbgs() << *this << '\n';
1001}
1002
1003LLVM_DUMP_METHOD void LiveInterval::SubRange::dump() const {
1004 dbgs() << *this << '\n';
Chris Lattnerabf295f2004-07-24 02:52:23 +00001005}
Matthias Braun03d96092013-10-10 21:29:05 +00001006
Yaron Keren55307982016-01-29 20:50:44 +00001007LLVM_DUMP_METHOD void LiveInterval::dump() const {
Krzysztof Parzyszek29e2ed12016-07-12 17:37:44 +00001008 dbgs() << *this << '\n';
Matthias Braun03d96092013-10-10 21:29:05 +00001009}
Manman Ren77e300e2012-09-06 19:06:06 +00001010#endif
Jeff Cohenc21c5ee2006-12-15 22:57:14 +00001011
Chandler Carruth261b6332012-07-10 05:06:03 +00001012#ifndef NDEBUG
Matthias Braun87a86052013-10-10 21:28:47 +00001013void LiveRange::verify() const {
Chandler Carruth261b6332012-07-10 05:06:03 +00001014 for (const_iterator I = begin(), E = end(); I != E; ++I) {
1015 assert(I->start.isValid());
1016 assert(I->end.isValid());
1017 assert(I->start < I->end);
Craig Topper4ba84432014-04-14 00:51:57 +00001018 assert(I->valno != nullptr);
Matthias Braun87a86052013-10-10 21:28:47 +00001019 assert(I->valno->id < valnos.size());
Chandler Carruth261b6332012-07-10 05:06:03 +00001020 assert(I->valno == valnos[I->valno->id]);
Benjamin Kramerd628f192014-03-02 12:27:27 +00001021 if (std::next(I) != E) {
1022 assert(I->end <= std::next(I)->start);
1023 if (I->end == std::next(I)->start)
1024 assert(I->valno != std::next(I)->valno);
Chandler Carruth261b6332012-07-10 05:06:03 +00001025 }
1026 }
1027}
Matthias Braun01ddf042014-12-10 01:12:10 +00001028
1029void LiveInterval::verify(const MachineRegisterInfo *MRI) const {
1030 super::verify();
1031
1032 // Make sure SubRanges are fine and LaneMasks are disjunct.
Krzysztof Parzyszekd6ca3f02016-12-15 14:36:06 +00001033 LaneBitmask Mask;
1034 LaneBitmask MaxMask = MRI != nullptr ? MRI->getMaxLaneMaskForVReg(reg)
1035 : LaneBitmask::getAll();
Matthias Braun1bfcc2d2014-12-11 00:59:06 +00001036 for (const SubRange &SR : subranges()) {
Matthias Braun01ddf042014-12-10 01:12:10 +00001037 // Subrange lanemask should be disjunct to any previous subrange masks.
Krzysztof Parzyszekd6ca3f02016-12-15 14:36:06 +00001038 assert((Mask & SR.LaneMask).none());
Matthias Braun1bfcc2d2014-12-11 00:59:06 +00001039 Mask |= SR.LaneMask;
Matthias Braun01ddf042014-12-10 01:12:10 +00001040
1041 // subrange mask should not contained in maximum lane mask for the vreg.
Krzysztof Parzyszekd6ca3f02016-12-15 14:36:06 +00001042 assert((Mask & ~MaxMask).none());
Matthias Braun0219a272015-07-16 18:55:35 +00001043 // empty subranges must be removed.
1044 assert(!SR.empty());
Matthias Braun01ddf042014-12-10 01:12:10 +00001045
Matthias Braun1bfcc2d2014-12-11 00:59:06 +00001046 SR.verify();
Matthias Braun01ddf042014-12-10 01:12:10 +00001047 // Main liverange should cover subrange.
Matthias Braun1bfcc2d2014-12-11 00:59:06 +00001048 assert(covers(SR));
Matthias Braun01ddf042014-12-10 01:12:10 +00001049 }
1050}
Chandler Carruth261b6332012-07-10 05:06:03 +00001051#endif
1052
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001053//===----------------------------------------------------------------------===//
1054// LiveRangeUpdater class
1055//===----------------------------------------------------------------------===//
1056//
1057// The LiveRangeUpdater class always maintains these invariants:
1058//
1059// - When LastStart is invalid, Spills is empty and the iterators are invalid.
1060// This is the initial state, and the state created by flush().
1061// In this state, isDirty() returns false.
1062//
1063// Otherwise, segments are kept in three separate areas:
1064//
Matthias Braun87a86052013-10-10 21:28:47 +00001065// 1. [begin; WriteI) at the front of LR.
1066// 2. [ReadI; end) at the back of LR.
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001067// 3. Spills.
1068//
Matthias Braun87a86052013-10-10 21:28:47 +00001069// - LR.begin() <= WriteI <= ReadI <= LR.end().
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001070// - Segments in all three areas are fully ordered and coalesced.
1071// - Segments in area 1 precede and can't coalesce with segments in area 2.
1072// - Segments in Spills precede and can't coalesce with segments in area 2.
1073// - No coalescing is possible between segments in Spills and segments in area
1074// 1, and there are no overlapping segments.
1075//
1076// The segments in Spills are not ordered with respect to the segments in area
1077// 1. They need to be merged.
1078//
1079// When they exist, Spills.back().start <= LastStart,
1080// and WriteI[-1].start <= LastStart.
1081
Aaron Ballman1d03d382017-10-15 14:32:27 +00001082#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001083void LiveRangeUpdater::print(raw_ostream &OS) const {
1084 if (!isDirty()) {
Matthias Braun87a86052013-10-10 21:28:47 +00001085 if (LR)
1086 OS << "Clean updater: " << *LR << '\n';
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001087 else
1088 OS << "Null updater.\n";
1089 return;
1090 }
Matthias Braun87a86052013-10-10 21:28:47 +00001091 assert(LR && "Can't have null LR in dirty updater.");
1092 OS << " updater with gap = " << (ReadI - WriteI)
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001093 << ", last start = " << LastStart
1094 << ":\n Area 1:";
Matthias Braun218d20a2014-12-10 23:07:54 +00001095 for (const auto &S : make_range(LR->begin(), WriteI))
1096 OS << ' ' << S;
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001097 OS << "\n Spills:";
1098 for (unsigned I = 0, E = Spills.size(); I != E; ++I)
1099 OS << ' ' << Spills[I];
1100 OS << "\n Area 2:";
Matthias Braun218d20a2014-12-10 23:07:54 +00001101 for (const auto &S : make_range(ReadI, LR->end()))
1102 OS << ' ' << S;
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001103 OS << '\n';
1104}
1105
Yaron Keren55307982016-01-29 20:50:44 +00001106LLVM_DUMP_METHOD void LiveRangeUpdater::dump() const {
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001107 print(errs());
1108}
Matthias Braun88d20752017-01-28 02:02:38 +00001109#endif
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001110
1111// Determine if A and B should be coalesced.
Matthias Braun87a86052013-10-10 21:28:47 +00001112static inline bool coalescable(const LiveRange::Segment &A,
1113 const LiveRange::Segment &B) {
Matthias Braun331de112013-10-10 21:28:43 +00001114 assert(A.start <= B.start && "Unordered live segments.");
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001115 if (A.end == B.start)
1116 return A.valno == B.valno;
1117 if (A.end < B.start)
1118 return false;
1119 assert(A.valno == B.valno && "Cannot overlap different values");
1120 return true;
1121}
1122
Matthias Braun87a86052013-10-10 21:28:47 +00001123void LiveRangeUpdater::add(LiveRange::Segment Seg) {
1124 assert(LR && "Cannot add to a null destination");
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001125
Quentin Colombet4c2a2ac2015-02-06 18:42:41 +00001126 // Fall back to the regular add method if the live range
1127 // is using the segment set instead of the segment vector.
1128 if (LR->segmentSet != nullptr) {
1129 LR->addSegmentToSet(Seg);
1130 return;
1131 }
1132
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001133 // Flush the state if Start moves backwards.
1134 if (!LastStart.isValid() || LastStart > Seg.start) {
1135 if (isDirty())
1136 flush();
1137 // This brings us to an uninitialized state. Reinitialize.
1138 assert(Spills.empty() && "Leftover spilled segments");
Matthias Braun87a86052013-10-10 21:28:47 +00001139 WriteI = ReadI = LR->begin();
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001140 }
1141
1142 // Remember start for next time.
1143 LastStart = Seg.start;
1144
1145 // Advance ReadI until it ends after Seg.start.
Matthias Braun87a86052013-10-10 21:28:47 +00001146 LiveRange::iterator E = LR->end();
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001147 if (ReadI != E && ReadI->end <= Seg.start) {
1148 // First try to close the gap between WriteI and ReadI with spills.
1149 if (ReadI != WriteI)
1150 mergeSpills();
1151 // Then advance ReadI.
1152 if (ReadI == WriteI)
Matthias Braun87a86052013-10-10 21:28:47 +00001153 ReadI = WriteI = LR->find(Seg.start);
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001154 else
1155 while (ReadI != E && ReadI->end <= Seg.start)
1156 *WriteI++ = *ReadI++;
1157 }
1158
1159 assert(ReadI == E || ReadI->end > Seg.start);
1160
1161 // Check if the ReadI segment begins early.
1162 if (ReadI != E && ReadI->start <= Seg.start) {
1163 assert(ReadI->valno == Seg.valno && "Cannot overlap different values");
1164 // Bail if Seg is completely contained in ReadI.
1165 if (ReadI->end >= Seg.end)
1166 return;
1167 // Coalesce into Seg.
1168 Seg.start = ReadI->start;
1169 ++ReadI;
1170 }
1171
1172 // Coalesce as much as possible from ReadI into Seg.
1173 while (ReadI != E && coalescable(Seg, *ReadI)) {
1174 Seg.end = std::max(Seg.end, ReadI->end);
1175 ++ReadI;
1176 }
1177
1178 // Try coalescing Spills.back() into Seg.
1179 if (!Spills.empty() && coalescable(Spills.back(), Seg)) {
1180 Seg.start = Spills.back().start;
1181 Seg.end = std::max(Spills.back().end, Seg.end);
1182 Spills.pop_back();
1183 }
1184
1185 // Try coalescing Seg into WriteI[-1].
Matthias Braun87a86052013-10-10 21:28:47 +00001186 if (WriteI != LR->begin() && coalescable(WriteI[-1], Seg)) {
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001187 WriteI[-1].end = std::max(WriteI[-1].end, Seg.end);
1188 return;
1189 }
1190
1191 // Seg doesn't coalesce with anything, and needs to be inserted somewhere.
1192 if (WriteI != ReadI) {
1193 *WriteI++ = Seg;
1194 return;
1195 }
1196
Matthias Braun87a86052013-10-10 21:28:47 +00001197 // Finally, append to LR or Spills.
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001198 if (WriteI == E) {
Matthias Braun87a86052013-10-10 21:28:47 +00001199 LR->segments.push_back(Seg);
1200 WriteI = ReadI = LR->end();
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001201 } else
1202 Spills.push_back(Seg);
1203}
1204
1205// Merge as many spilled segments as possible into the gap between WriteI
1206// and ReadI. Advance WriteI to reflect the inserted instructions.
1207void LiveRangeUpdater::mergeSpills() {
1208 // Perform a backwards merge of Spills and [SpillI;WriteI).
1209 size_t GapSize = ReadI - WriteI;
1210 size_t NumMoved = std::min(Spills.size(), GapSize);
Matthias Braun87a86052013-10-10 21:28:47 +00001211 LiveRange::iterator Src = WriteI;
1212 LiveRange::iterator Dst = Src + NumMoved;
1213 LiveRange::iterator SpillSrc = Spills.end();
1214 LiveRange::iterator B = LR->begin();
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001215
1216 // This is the new WriteI position after merging spills.
1217 WriteI = Dst;
1218
1219 // Now merge Src and Spills backwards.
1220 while (Src != Dst) {
1221 if (Src != B && Src[-1].start > SpillSrc[-1].start)
1222 *--Dst = *--Src;
1223 else
1224 *--Dst = *--SpillSrc;
1225 }
1226 assert(NumMoved == size_t(Spills.end() - SpillSrc));
1227 Spills.erase(SpillSrc, Spills.end());
1228}
1229
1230void LiveRangeUpdater::flush() {
1231 if (!isDirty())
1232 return;
1233 // Clear the dirty state.
1234 LastStart = SlotIndex();
1235
Matthias Braun87a86052013-10-10 21:28:47 +00001236 assert(LR && "Cannot add to a null destination");
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001237
1238 // Nothing to merge?
1239 if (Spills.empty()) {
Matthias Braun87a86052013-10-10 21:28:47 +00001240 LR->segments.erase(WriteI, ReadI);
1241 LR->verify();
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001242 return;
1243 }
1244
1245 // Resize the WriteI - ReadI gap to match Spills.
1246 size_t GapSize = ReadI - WriteI;
1247 if (GapSize < Spills.size()) {
1248 // The gap is too small. Make some room.
Matthias Braun87a86052013-10-10 21:28:47 +00001249 size_t WritePos = WriteI - LR->begin();
1250 LR->segments.insert(ReadI, Spills.size() - GapSize, LiveRange::Segment());
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001251 // This also invalidated ReadI, but it is recomputed below.
Matthias Braun87a86052013-10-10 21:28:47 +00001252 WriteI = LR->begin() + WritePos;
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001253 } else {
1254 // Shrink the gap if necessary.
Matthias Braun87a86052013-10-10 21:28:47 +00001255 LR->segments.erase(WriteI + Spills.size(), ReadI);
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001256 }
1257 ReadI = WriteI + Spills.size();
1258 mergeSpills();
Matthias Braun87a86052013-10-10 21:28:47 +00001259 LR->verify();
Jakob Stoklund Olesen1a41f322013-02-20 18:18:12 +00001260}
1261
Matthias Braundcaeedf2016-01-08 01:16:35 +00001262unsigned ConnectedVNInfoEqClasses::Classify(const LiveRange &LR) {
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +00001263 // Create initial equivalence classes.
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001264 EqClass.clear();
Matthias Braundcaeedf2016-01-08 01:16:35 +00001265 EqClass.grow(LR.getNumValNums());
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +00001266
Craig Topper4ba84432014-04-14 00:51:57 +00001267 const VNInfo *used = nullptr, *unused = nullptr;
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +00001268
Jakob Stoklund Olesen54f32e62010-10-08 21:19:28 +00001269 // Determine connections.
Matthias Braundcaeedf2016-01-08 01:16:35 +00001270 for (const VNInfo *VNI : LR.valnos) {
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +00001271 // Group all unused values into one class.
1272 if (VNI->isUnused()) {
1273 if (unused)
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001274 EqClass.join(unused->id, VNI->id);
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +00001275 unused = VNI;
1276 continue;
1277 }
1278 used = VNI;
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001279 if (VNI->isPHIDef()) {
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001280 const MachineBasicBlock *MBB = LIS.getMBBFromIndex(VNI->def);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001281 assert(MBB && "Phi-def has no defining MBB");
1282 // Connect to values live out of predecessors.
1283 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(),
1284 PE = MBB->pred_end(); PI != PE; ++PI)
Matthias Braundcaeedf2016-01-08 01:16:35 +00001285 if (const VNInfo *PVNI = LR.getVNInfoBefore(LIS.getMBBEndIdx(*PI)))
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001286 EqClass.join(VNI->id, PVNI->id);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001287 } else {
1288 // Normal value defined by an instruction. Check for two-addr redef.
1289 // FIXME: This could be coincidental. Should we really check for a tied
1290 // operand constraint?
Jakob Stoklund Olesenb907e8a2010-12-21 00:48:17 +00001291 // Note that VNI->def may be a use slot for an early clobber def.
Matthias Braundcaeedf2016-01-08 01:16:35 +00001292 if (const VNInfo *UVNI = LR.getVNInfoBefore(VNI->def))
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001293 EqClass.join(VNI->id, UVNI->id);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001294 }
1295 }
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +00001296
1297 // Lump all the unused values in with the last used value.
1298 if (used && unused)
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001299 EqClass.join(used->id, unused->id);
Jakob Stoklund Olesen6d309052010-10-29 17:37:29 +00001300
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001301 EqClass.compress();
1302 return EqClass.getNumClasses();
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001303}
1304
Matthias Braun95e05dd2015-09-22 03:44:41 +00001305void ConnectedVNInfoEqClasses::Distribute(LiveInterval &LI, LiveInterval *LIV[],
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001306 MachineRegisterInfo &MRI) {
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001307 // Rewrite instructions.
1308 for (MachineRegisterInfo::reg_iterator RI = MRI.reg_begin(LI.reg),
1309 RE = MRI.reg_end(); RI != RE;) {
Owen Andersonbf630222014-03-13 23:12:04 +00001310 MachineOperand &MO = *RI;
1311 MachineInstr *MI = RI->getParent();
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001312 ++RI;
Yury Delendik985ee9e2018-08-21 17:48:28 +00001313 const VNInfo *VNI;
1314 if (MI->isDebugValue()) {
1315 // DBG_VALUE instructions don't have slot indexes, so get the index of
1316 // the instruction before them. The value is defined there too.
1317 SlotIndex Idx = LIS.getSlotIndexes()->getIndexBefore(*MI);
1318 VNI = LI.Query(Idx).valueOut();
1319 } else {
1320 SlotIndex Idx = LIS.getInstructionIndex(*MI);
1321 LiveQueryResult LRQ = LI.Query(Idx);
1322 VNI = MO.readsReg() ? LRQ.valueIn() : LRQ.valueDefined();
1323 }
Jakob Stoklund Olesen84315f02012-07-25 17:15:15 +00001324 // In the case of an <undef> use that isn't tied to any def, VNI will be
1325 // NULL. If the use is tied to a def, VNI will be the defined value.
Jakob Stoklund Olesenbd6f44a2012-05-19 05:25:50 +00001326 if (!VNI)
1327 continue;
Matthias Braun95e05dd2015-09-22 03:44:41 +00001328 if (unsigned EqClass = getEqClass(VNI))
1329 MO.setReg(LIV[EqClass-1]->reg);
Jakob Stoklund Olesen22542272011-03-17 00:23:45 +00001330 }
1331
Matthias Braunc047a222015-09-22 22:37:42 +00001332 // Distribute subregister liveranges.
1333 if (LI.hasSubRanges()) {
1334 unsigned NumComponents = EqClass.getNumClasses();
1335 SmallVector<unsigned, 8> VNIMapping;
1336 SmallVector<LiveInterval::SubRange*, 8> SubRanges;
1337 BumpPtrAllocator &Allocator = LIS.getVNInfoAllocator();
1338 for (LiveInterval::SubRange &SR : LI.subranges()) {
1339 // Create new subranges in the split intervals and construct a mapping
1340 // for the VNInfos in the subrange.
1341 unsigned NumValNos = SR.valnos.size();
1342 VNIMapping.clear();
1343 VNIMapping.reserve(NumValNos);
1344 SubRanges.clear();
1345 SubRanges.resize(NumComponents-1, nullptr);
1346 for (unsigned I = 0; I < NumValNos; ++I) {
1347 const VNInfo &VNI = *SR.valnos[I];
Matthias Braunafb111a2016-03-24 21:41:38 +00001348 unsigned ComponentNum;
1349 if (VNI.isUnused()) {
1350 ComponentNum = 0;
1351 } else {
1352 const VNInfo *MainRangeVNI = LI.getVNInfoAt(VNI.def);
1353 assert(MainRangeVNI != nullptr
1354 && "SubRange def must have corresponding main range def");
1355 ComponentNum = getEqClass(MainRangeVNI);
1356 if (ComponentNum > 0 && SubRanges[ComponentNum-1] == nullptr) {
1357 SubRanges[ComponentNum-1]
1358 = LIV[ComponentNum-1]->createSubRange(Allocator, SR.LaneMask);
1359 }
Matthias Braunc047a222015-09-22 22:37:42 +00001360 }
Matthias Braunafb111a2016-03-24 21:41:38 +00001361 VNIMapping.push_back(ComponentNum);
Matthias Braunc047a222015-09-22 22:37:42 +00001362 }
1363 DistributeRange(SR, SubRanges.data(), VNIMapping);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001364 }
Matthias Braunc047a222015-09-22 22:37:42 +00001365 LI.removeEmptySubRanges();
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001366 }
Matthias Braunc047a222015-09-22 22:37:42 +00001367
1368 // Distribute main liverange.
1369 DistributeRange(LI, LIV, EqClass);
Jakob Stoklund Olesen0253df92010-10-07 23:34:34 +00001370}