1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
using ARMeilleure.Common;
using ARMeilleure.IntermediateRepresentation;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace ARMeilleure.CodeGen.RegisterAllocators
{
class LiveInterval : IComparable<LiveInterval>
{
public const int NotFound = -1;
private LiveInterval _parent;
private SortedIntegerList _usePositions;
public int UsesCount => _usePositions.Count;
private List<LiveRange> _ranges;
private SortedList<int, LiveInterval> _childs;
public bool IsSplit => _childs.Count != 0;
public Operand Local { get; }
public Register Register { get; set; }
public int SpillOffset { get; private set; }
public bool IsSpilled => SpillOffset != -1;
public bool IsFixed { get; }
public bool IsEmpty => _ranges.Count == 0;
public LiveInterval(Operand local = null, LiveInterval parent = null)
{
Local = local;
_parent = parent ?? this;
_usePositions = new SortedIntegerList();
_ranges = new List<LiveRange>();
_childs = new SortedList<int, LiveInterval>();
SpillOffset = -1;
}
public LiveInterval(Register register) : this()
{
IsFixed = true;
Register = register;
}
public void SetStart(int position)
{
if (_ranges.Count != 0)
{
Debug.Assert(position != _ranges[0].End);
_ranges[0] = new LiveRange(position, _ranges[0].End);
}
else
{
_ranges.Add(new LiveRange(position, position + 1));
}
}
public int GetStart()
{
if (_ranges.Count == 0)
{
throw new InvalidOperationException("Empty interval.");
}
return _ranges[0].Start;
}
public void SetEnd(int position)
{
if (_ranges.Count != 0)
{
int lastIdx = _ranges.Count - 1;
Debug.Assert(position != _ranges[lastIdx].Start);
_ranges[lastIdx] = new LiveRange(_ranges[lastIdx].Start, position);
}
else
{
_ranges.Add(new LiveRange(position, position + 1));
}
}
public int GetEnd()
{
if (_ranges.Count == 0)
{
throw new InvalidOperationException("Empty interval.");
}
return _ranges[_ranges.Count - 1].End;
}
public void AddRange(int start, int end)
{
if (start >= end)
{
throw new ArgumentException("Invalid range start position " + start + ", " + end);
}
int index = _ranges.BinarySearch(new LiveRange(start, end));
if (index >= 0)
{
// New range insersects with an existing range, we need to remove
// all the intersecting ranges before adding the new one.
// We also extend the new range as needed, based on the values of
// the existing ranges being removed.
int lIndex = index;
int rIndex = index;
while (lIndex > 0 && _ranges[lIndex - 1].End >= start)
{
lIndex--;
}
while (rIndex + 1 < _ranges.Count && _ranges[rIndex + 1].Start <= end)
{
rIndex++;
}
if (start > _ranges[lIndex].Start)
{
start = _ranges[lIndex].Start;
}
if (end < _ranges[rIndex].End)
{
end = _ranges[rIndex].End;
}
_ranges.RemoveRange(lIndex, (rIndex - lIndex) + 1);
InsertRange(lIndex, start, end);
}
else
{
InsertRange(~index, start, end);
}
}
private void InsertRange(int index, int start, int end)
{
// Here we insert a new range on the ranges list.
// If possible, we extend an existing range rather than inserting a new one.
// We can extend an existing range if any of the following conditions are true:
// - The new range starts right after the end of the previous range on the list.
// - The new range ends right before the start of the next range on the list.
// If both cases are true, we can extend either one. We prefer to extend the
// previous range, and then remove the next one, but theres no specific reason
// for that, extending either one will do.
int? extIndex = null;
if (index > 0 && _ranges[index - 1].End == start)
{
start = _ranges[index - 1].Start;
extIndex = index - 1;
}
if (index < _ranges.Count && _ranges[index].Start == end)
{
end = _ranges[index].End;
if (extIndex.HasValue)
{
_ranges.RemoveAt(index);
}
else
{
extIndex = index;
}
}
if (extIndex.HasValue)
{
_ranges[extIndex.Value] = new LiveRange(start, end);
}
else
{
_ranges.Insert(index, new LiveRange(start, end));
}
}
public void AddUsePosition(int position)
{
// Inserts are in descending order, but ascending is faster for SortedIntegerList<>.
// We flip the ordering, then iterate backwards when using the final list.
_usePositions.Add(-position);
}
public bool Overlaps(int position)
{
return _ranges.BinarySearch(new LiveRange(position, position + 1)) >= 0;
}
public bool Overlaps(LiveInterval other)
{
foreach (LiveRange range in other._ranges)
{
if (_ranges.BinarySearch(range) >= 0)
{
return true;
}
}
return false;
}
public int GetOverlapPosition(LiveInterval other)
{
foreach (LiveRange range in other._ranges)
{
int overlapIndex = _ranges.BinarySearch(range);
if (overlapIndex >= 0)
{
// It's possible that we have multiple overlaps within a single interval,
// in this case, we pick the one with the lowest start position, since
// we return the first overlap position.
while (overlapIndex > 0 && _ranges[overlapIndex - 1].End > range.Start)
{
overlapIndex--;
}
LiveRange overlappingRange = _ranges[overlapIndex];
return overlappingRange.Start;
}
}
return NotFound;
}
public IEnumerable<LiveInterval> SplitChilds()
{
return _childs.Values;
}
public IList<int> UsePositions()
{
return _usePositions.GetList();
}
public int FirstUse()
{
if (_usePositions.Count == 0)
{
return NotFound;
}
return -_usePositions.Last();
}
public int NextUseAfter(int position)
{
int index = _usePositions.FindLessEqualIndex(-position);
return (index >= 0) ? -_usePositions[index] : NotFound;
}
public void RemoveAfter(int position)
{
int index = _usePositions.FindLessEqualIndex(-position);
_usePositions.RemoveRange(0, index + 1);
}
public LiveInterval Split(int position)
{
LiveInterval right = new LiveInterval(Local, _parent);
int splitIndex = 0;
for (; splitIndex < _ranges.Count; splitIndex++)
{
LiveRange range = _ranges[splitIndex];
if (position > range.Start && position < range.End)
{
right._ranges.Add(new LiveRange(position, range.End));
range = new LiveRange(range.Start, position);
_ranges[splitIndex++] = range;
break;
}
if (range.Start >= position)
{
break;
}
}
if (splitIndex < _ranges.Count)
{
int count = _ranges.Count - splitIndex;
right._ranges.AddRange(_ranges.GetRange(splitIndex, count));
_ranges.RemoveRange(splitIndex, count);
}
int addAfter = _usePositions.FindLessEqualIndex(-position);
for (int index = addAfter; index >= 0; index--)
{
int usePosition = _usePositions[index];
right._usePositions.Add(usePosition);
}
RemoveAfter(position);
Debug.Assert(_ranges.Count != 0, "Left interval is empty after split.");
Debug.Assert(right._ranges.Count != 0, "Right interval is empty after split.");
AddSplitChild(right);
return right;
}
private void AddSplitChild(LiveInterval child)
{
Debug.Assert(!child.IsEmpty, "Trying to insert a empty interval.");
_parent._childs.Add(child.GetStart(), child);
}
public LiveInterval GetSplitChild(int position)
{
if (Overlaps(position))
{
return this;
}
foreach (LiveInterval splitChild in _childs.Values)
{
if (splitChild.Overlaps(position))
{
return splitChild;
}
}
return null;
}
public bool TrySpillWithSiblingOffset()
{
foreach (LiveInterval splitChild in _parent._childs.Values)
{
if (splitChild.IsSpilled)
{
Spill(splitChild.SpillOffset);
return true;
}
}
return false;
}
public void Spill(int offset)
{
SpillOffset = offset;
}
public int CompareTo(LiveInterval other)
{
if (_ranges.Count == 0 || other._ranges.Count == 0)
{
return _ranges.Count.CompareTo(other._ranges.Count);
}
return _ranges[0].Start.CompareTo(other._ranges[0].Start);
}
public override string ToString()
{
return string.Join("; ", _ranges);
}
}
}
|