aboutsummaryrefslogtreecommitdiff
path: root/ARMeilleure/CodeGen/RegisterAllocators/LiveInterval.cs
diff options
context:
space:
mode:
authorgdkchan <gab.dark.100@gmail.com>2019-08-08 15:56:22 -0300
committeremmauss <emmausssss@gmail.com>2019-08-08 21:56:22 +0300
commita731ab3a2aad56e6ceb8b4e2444a61353246295c (patch)
treec7f13f51bfec6b19431e62167811ae31e9d2fea9 /ARMeilleure/CodeGen/RegisterAllocators/LiveInterval.cs
parent1ba58e9942e54175e3f3a0e1d57a48537f4888b1 (diff)
Add a new JIT compiler for CPU code (#693)
* Start of the ARMeilleure project * Refactoring around the old IRAdapter, now renamed to PreAllocator * Optimize the LowestBitSet method * Add CLZ support and fix CLS implementation * Add missing Equals and GetHashCode overrides on some structs, misc small tweaks * Implement the ByteSwap IR instruction, and some refactoring on the assembler * Implement the DivideUI IR instruction and fix 64-bits IDIV * Correct constant operand type on CSINC * Move division instructions implementation to InstEmitDiv * Fix destination type for the ConditionalSelect IR instruction * Implement UMULH and SMULH, with new IR instructions * Fix some issues with shift instructions * Fix constant types for BFM instructions * Fix up new tests using the new V128 struct * Update tests * Move DIV tests to a separate file * Add support for calls, and some instructions that depends on them * Start adding support for SIMD & FP types, along with some of the related ARM instructions * Fix some typos and the divide instruction with FP operands * Fix wrong method call on Clz_V * Implement ARM FP & SIMD move instructions, Saddlv_V, and misc. fixes * Implement SIMD logical instructions and more misc. fixes * Fix PSRAD x86 instruction encoding, TRN, UABD and UABDL implementations * Implement float conversion instruction, merge in LDj3SNuD fixes, and some other misc. fixes * Implement SIMD shift instruction and fix Dup_V * Add SCVTF and UCVTF (vector, fixed-point) variants to the opcode table * Fix check with tolerance on tester * Implement FP & SIMD comparison instructions, and some fixes * Update FCVT (Scalar) encoding on the table to support the Half-float variants * Support passing V128 structs, some cleanup on the register allocator, merge LDj3SNuD fixes * Use old memory access methods, made a start on SIMD memory insts support, some fixes * Fix float constant passed to functions, save and restore non-volatile XMM registers, other fixes * Fix arguments count with struct return values, other fixes * More instructions * Misc. fixes and integrate LDj3SNuD fixes * Update tests * Add a faster linear scan allocator, unwinding support on windows, and other changes * Update Ryujinx.HLE * Update Ryujinx.Graphics * Fix V128 return pointer passing, RCX is clobbered * Update Ryujinx.Tests * Update ITimeZoneService * Stop using GetFunctionPointer as that can't be called from native code, misc. fixes and tweaks * Use generic GetFunctionPointerForDelegate method and other tweaks * Some refactoring on the code generator, assert on invalid operations and use a separate enum for intrinsics * Remove some unused code on the assembler * Fix REX.W prefix regression on float conversion instructions, add some sort of profiler * Add hardware capability detection * Fix regression on Sha1h and revert Fcm** changes * Add SSE2-only paths on vector extract and insert, some refactoring on the pre-allocator * Fix silly mistake introduced on last commit on CpuId * Generate inline stack probes when the stack allocation is too large * Initial support for the System-V ABI * Support multiple destination operands * Fix SSE2 VectorInsert8 path, and other fixes * Change placement of XMM callee save and restore code to match other compilers * Rename Dest to Destination and Inst to Instruction * Fix a regression related to calls and the V128 type * Add an extra space on comments to match code style * Some refactoring * Fix vector insert FP32 SSE2 path * Port over the ARM32 instructions * Avoid memory protection races on JIT Cache * Another fix on VectorInsert FP32 (thanks to LDj3SNuD * Float operands don't need to use the same register when VEX is supported * Add a new register allocator, higher quality code for hot code (tier up), and other tweaks * Some nits, small improvements on the pre allocator * CpuThreadState is gone * Allow changing CPU emulators with a config entry * Add runtime identifiers on the ARMeilleure project * Allow switching between CPUs through a config entry (pt. 2) * Change win10-x64 to win-x64 on projects * Update the Ryujinx project to use ARMeilleure * Ensure that the selected register is valid on the hybrid allocator * Allow exiting on returns to 0 (should fix test regression) * Remove register assignments for most used variables on the hybrid allocator * Do not use fixed registers as spill temp * Add missing namespace and remove unneeded using * Address PR feedback * Fix types, etc * Enable AssumeStrictAbiCompliance by default * Ensure that Spill and Fill don't load or store any more than necessary
Diffstat (limited to 'ARMeilleure/CodeGen/RegisterAllocators/LiveInterval.cs')
-rw-r--r--ARMeilleure/CodeGen/RegisterAllocators/LiveInterval.cs390
1 files changed, 390 insertions, 0 deletions
diff --git a/ARMeilleure/CodeGen/RegisterAllocators/LiveInterval.cs b/ARMeilleure/CodeGen/RegisterAllocators/LiveInterval.cs
new file mode 100644
index 00000000..18858a76
--- /dev/null
+++ b/ARMeilleure/CodeGen/RegisterAllocators/LiveInterval.cs
@@ -0,0 +1,390 @@
+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 SortedSet<int> _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 SortedSet<int>();
+
+ _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)
+ {
+ _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 IEnumerable<int> UsePositions()
+ {
+ return _usePositions;
+ }
+
+ public int FirstUse()
+ {
+ if (_usePositions.Count == 0)
+ {
+ return NotFound;
+ }
+
+ return _usePositions.First();
+ }
+
+ public int NextUseAfter(int position)
+ {
+ foreach (int usePosition in _usePositions)
+ {
+ if (usePosition >= position)
+ {
+ return usePosition;
+ }
+ }
+
+ return NotFound;
+ }
+
+ 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);
+ }
+
+ foreach (int usePosition in _usePositions.Where(x => x >= position))
+ {
+ right._usePositions.Add(usePosition);
+ }
+
+ _usePositions.RemoveWhere(x => x >= 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);
+ }
+ }
+} \ No newline at end of file