LiveInterval.cs 11 KB

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