mirror of
https://github.com/ryujinx-mirror/ryujinx.git
synced 2024-11-23 10:35:10 +00:00
22b2cb39af
* Turn `MemoryOperand` into a struct * Remove `IntrinsicOperation` * Remove `PhiNode` * Remove `Node` * Turn `Operand` into a struct * Turn `Operation` into a struct * Clean up pool management methods * Add `Arena` allocator * Move `OperationHelper` to `Operation.Factory` * Move `OperandHelper` to `Operand.Factory` * Optimize `Operation` a bit * Fix `Arena` initialization * Rename `NativeList<T>` to `ArenaList<T>` * Reduce `Operand` size from 88 to 56 bytes * Reduce `Operation` size from 56 to 40 bytes * Add optimistic interning of Register & Constant operands * Optimize `RegisterUsage` pass a bit * Optimize `RemoveUnusedNodes` pass a bit Iterating in reverse-order allows killing dependency chains in a single pass. * Fix PPTC symbols * Optimize `BasicBlock` a bit Reduce allocations from `_successor` & `DominanceFrontiers` * Fix `Operation` resize * Make `Arena` expandable Change the arena allocator to be expandable by allocating in pages, with some of them being pooled. Currently 32 pages are pooled. An LRU removal mechanism should probably be added to it. Apparently MHR can allocate bitmaps large enough to exceed the 16MB limit for the type. * Move `Arena` & `ArenaList` to `Common` * Remove `ThreadStaticPool` & co * Add `PhiOperation` * Reduce `Operand` size from 56 from 48 bytes * Add linear-probing to `Operand` intern table * Optimize `HybridAllocator` a bit * Add `Allocators` class * Tune `ArenaAllocator` sizes * Add page removal mechanism to `ArenaAllocator` Remove pages which have not been used for more than 5s after each reset. I am on fence if this would be better using a Gen2 callback object like the one in System.Buffers.ArrayPool<T>, to trim the pool. Because right now if a large translation happens, the pages will be freed only after a reset. This reset may not happen for a while because no new translation is hit, but the arena base sizes are rather small. * Fix `OOM` when allocating larger than page size in `ArenaAllocator` Tweak resizing mechanism for Operand.Uses and Assignemnts. * Optimize `Optimizer` a bit * Optimize `Operand.Add<T>/Remove<T>` a bit * Clean up `PreAllocator` * Fix phi insertion order Reduce codegen diffs. * Fix code alignment * Use new heuristics for degree of parallelism * Suppress warnings * Address gdkchan's feedback Renamed `GetValue()` to `GetValueUnsafe()` to make it more clear that `Operand.Value` should usually not be modified directly. * Add fast path to `ArenaAllocator` * Assembly for `ArenaAllocator.Allocate(ulong)`: .L0: mov rax, [rcx+0x18] lea r8, [rax+rdx] cmp r8, [rcx+0x10] ja short .L2 .L1: mov rdx, [rcx+8] add rax, [rdx+8] mov [rcx+0x18], r8 ret .L2: jmp ArenaAllocator.AllocateSlow(UInt64) A few variable/field had to be changed to ulong so that RyuJIT avoids emitting zero-extends. * Implement a new heuristic to free pooled pages. If an arena is used often, it is more likely that its pages will be needed, so the pages are kept for longer (e.g: during PPTC rebuild or burst sof compilations). If is not used often, then it is more likely that its pages will not be needed (e.g: after PPTC rebuild or bursts of compilations). * Address riperiperi's feedback * Use `EqualityComparer<T>` in `IntrusiveList<T>` Avoids a potential GC hole in `Equals(T, T)`.
340 lines
12 KiB
C#
340 lines
12 KiB
C#
using ARMeilleure.Decoders;
|
|
using ARMeilleure.IntermediateRepresentation;
|
|
using ARMeilleure.Translation;
|
|
using System;
|
|
using System.Diagnostics;
|
|
using System.Reflection;
|
|
|
|
using static ARMeilleure.Instructions.InstEmitHelper;
|
|
using static ARMeilleure.Instructions.InstEmitSimdHelper;
|
|
using static ARMeilleure.Instructions.InstEmitSimdHelper32;
|
|
using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
|
|
|
|
namespace ARMeilleure.Instructions
|
|
{
|
|
static partial class InstEmit32
|
|
{
|
|
public static void Vqrshrn(ArmEmitterContext context)
|
|
{
|
|
OpCode32SimdShImm op = (OpCode32SimdShImm)context.CurrOp;
|
|
|
|
EmitRoundShrImmSaturatingNarrowOp(context, op.U ? ShrImmSaturatingNarrowFlags.VectorZxZx : ShrImmSaturatingNarrowFlags.VectorSxSx);
|
|
}
|
|
|
|
public static void Vqrshrun(ArmEmitterContext context)
|
|
{
|
|
EmitRoundShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.VectorSxZx);
|
|
}
|
|
|
|
public static void Vqshrn(ArmEmitterContext context)
|
|
{
|
|
OpCode32SimdShImm op = (OpCode32SimdShImm)context.CurrOp;
|
|
|
|
EmitShrImmSaturatingNarrowOp(context, op.U ? ShrImmSaturatingNarrowFlags.VectorZxZx : ShrImmSaturatingNarrowFlags.VectorSxSx);
|
|
}
|
|
|
|
public static void Vrshr(ArmEmitterContext context)
|
|
{
|
|
OpCode32SimdShImm op = (OpCode32SimdShImm)context.CurrOp;
|
|
int shift = GetImmShr(op);
|
|
long roundConst = 1L << (shift - 1);
|
|
|
|
if (op.U)
|
|
{
|
|
if (op.Size < 2)
|
|
{
|
|
EmitVectorUnaryOpZx32(context, (op1) =>
|
|
{
|
|
op1 = context.Add(op1, Const(op1.Type, roundConst));
|
|
|
|
return context.ShiftRightUI(op1, Const(shift));
|
|
});
|
|
}
|
|
else if (op.Size == 2)
|
|
{
|
|
EmitVectorUnaryOpZx32(context, (op1) =>
|
|
{
|
|
op1 = context.ZeroExtend32(OperandType.I64, op1);
|
|
op1 = context.Add(op1, Const(op1.Type, roundConst));
|
|
|
|
return context.ConvertI64ToI32(context.ShiftRightUI(op1, Const(shift)));
|
|
});
|
|
}
|
|
else /* if (op.Size == 3) */
|
|
{
|
|
EmitVectorUnaryOpZx32(context, (op1) => EmitShrImm64(context, op1, signed: false, roundConst, shift));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (op.Size < 2)
|
|
{
|
|
EmitVectorUnaryOpSx32(context, (op1) =>
|
|
{
|
|
op1 = context.Add(op1, Const(op1.Type, roundConst));
|
|
|
|
return context.ShiftRightSI(op1, Const(shift));
|
|
});
|
|
}
|
|
else if (op.Size == 2)
|
|
{
|
|
EmitVectorUnaryOpSx32(context, (op1) =>
|
|
{
|
|
op1 = context.SignExtend32(OperandType.I64, op1);
|
|
op1 = context.Add(op1, Const(op1.Type, roundConst));
|
|
|
|
return context.ConvertI64ToI32(context.ShiftRightSI(op1, Const(shift)));
|
|
});
|
|
}
|
|
else /* if (op.Size == 3) */
|
|
{
|
|
EmitVectorUnaryOpZx32(context, (op1) => EmitShrImm64(context, op1, signed: true, roundConst, shift));
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void Vshl(ArmEmitterContext context)
|
|
{
|
|
OpCode32SimdShImm op = (OpCode32SimdShImm)context.CurrOp;
|
|
|
|
EmitVectorUnaryOpZx32(context, (op1) => context.ShiftLeft(op1, Const(op.Shift)));
|
|
}
|
|
|
|
public static void Vshl_I(ArmEmitterContext context)
|
|
{
|
|
OpCode32SimdReg op = (OpCode32SimdReg)context.CurrOp;
|
|
|
|
if (op.U)
|
|
{
|
|
EmitVectorBinaryOpZx32(context, (op1, op2) => EmitShlRegOp(context, op2, op1, op.Size, true));
|
|
}
|
|
else
|
|
{
|
|
EmitVectorBinaryOpSx32(context, (op1, op2) => EmitShlRegOp(context, op2, op1, op.Size, false));
|
|
}
|
|
}
|
|
|
|
public static void Vshll(ArmEmitterContext context)
|
|
{
|
|
OpCode32SimdShImmLong op = (OpCode32SimdShImmLong)context.CurrOp;
|
|
|
|
Operand res = context.VectorZero();
|
|
|
|
int elems = op.GetBytesCount() >> op.Size;
|
|
|
|
for (int index = 0; index < elems; index++)
|
|
{
|
|
Operand me = EmitVectorExtract32(context, op.Qm, op.Im + index, op.Size, !op.U);
|
|
|
|
if (op.Size == 2)
|
|
{
|
|
if (op.U)
|
|
{
|
|
me = context.ZeroExtend32(OperandType.I64, me);
|
|
}
|
|
else
|
|
{
|
|
me = context.SignExtend32(OperandType.I64, me);
|
|
}
|
|
}
|
|
|
|
me = context.ShiftLeft(me, Const(op.Shift));
|
|
|
|
res = EmitVectorInsert(context, res, me, index, op.Size + 1);
|
|
}
|
|
|
|
context.Copy(GetVecA32(op.Qd), res);
|
|
}
|
|
|
|
public static void Vshr(ArmEmitterContext context)
|
|
{
|
|
OpCode32SimdShImm op = (OpCode32SimdShImm)context.CurrOp;
|
|
int shift = GetImmShr(op);
|
|
int maxShift = (8 << op.Size) - 1;
|
|
|
|
if (op.U)
|
|
{
|
|
EmitVectorUnaryOpZx32(context, (op1) => (shift > maxShift) ? Const(op1.Type, 0) : context.ShiftRightUI(op1, Const(shift)));
|
|
}
|
|
else
|
|
{
|
|
EmitVectorUnaryOpSx32(context, (op1) => context.ShiftRightSI(op1, Const(Math.Min(maxShift, shift))));
|
|
}
|
|
}
|
|
|
|
public static void Vshrn(ArmEmitterContext context)
|
|
{
|
|
OpCode32SimdShImm op = (OpCode32SimdShImm)context.CurrOp;
|
|
int shift = GetImmShr(op);
|
|
|
|
EmitVectorUnaryNarrowOp32(context, (op1) => context.ShiftRightUI(op1, Const(shift)));
|
|
}
|
|
|
|
public static void Vsra(ArmEmitterContext context)
|
|
{
|
|
OpCode32SimdShImm op = (OpCode32SimdShImm)context.CurrOp;
|
|
int shift = GetImmShr(op);
|
|
int maxShift = (8 << op.Size) - 1;
|
|
|
|
if (op.U)
|
|
{
|
|
EmitVectorImmBinaryQdQmOpZx32(context, (op1, op2) =>
|
|
{
|
|
Operand shiftRes = shift > maxShift ? Const(op2.Type, 0) : context.ShiftRightUI(op2, Const(shift));
|
|
|
|
return context.Add(op1, shiftRes);
|
|
});
|
|
}
|
|
else
|
|
{
|
|
EmitVectorImmBinaryQdQmOpSx32(context, (op1, op2) => context.Add(op1, context.ShiftRightSI(op2, Const(Math.Min(maxShift, shift)))));
|
|
}
|
|
}
|
|
|
|
private static Operand EmitShlRegOp(ArmEmitterContext context, Operand op, Operand shiftLsB, int size, bool unsigned)
|
|
{
|
|
if (shiftLsB.Type == OperandType.I64)
|
|
{
|
|
shiftLsB = context.ConvertI64ToI32(shiftLsB);
|
|
}
|
|
|
|
shiftLsB = context.SignExtend8(OperandType.I32, shiftLsB);
|
|
Debug.Assert((uint)size < 4u);
|
|
|
|
Operand negShiftLsB = context.Negate(shiftLsB);
|
|
|
|
Operand isPositive = context.ICompareGreaterOrEqual(shiftLsB, Const(0));
|
|
|
|
Operand shl = context.ShiftLeft(op, shiftLsB);
|
|
Operand shr = unsigned ? context.ShiftRightUI(op, negShiftLsB) : context.ShiftRightSI(op, negShiftLsB);
|
|
|
|
Operand res = context.ConditionalSelect(isPositive, shl, shr);
|
|
|
|
if (unsigned)
|
|
{
|
|
Operand isOutOfRange = context.BitwiseOr(
|
|
context.ICompareGreaterOrEqual(shiftLsB, Const(8 << size)),
|
|
context.ICompareGreaterOrEqual(negShiftLsB, Const(8 << size)));
|
|
|
|
return context.ConditionalSelect(isOutOfRange, Const(op.Type, 0), res);
|
|
}
|
|
else
|
|
{
|
|
Operand isOutOfRange0 = context.ICompareGreaterOrEqual(shiftLsB, Const(8 << size));
|
|
Operand isOutOfRangeN = context.ICompareGreaterOrEqual(negShiftLsB, Const(8 << size));
|
|
|
|
// Also zero if shift is too negative, but value was positive.
|
|
isOutOfRange0 = context.BitwiseOr(isOutOfRange0, context.BitwiseAnd(isOutOfRangeN, context.ICompareGreaterOrEqual(op, Const(op.Type, 0))));
|
|
|
|
Operand min = (op.Type == OperandType.I64) ? Const(-1L) : Const(-1);
|
|
|
|
return context.ConditionalSelect(isOutOfRange0, Const(op.Type, 0), context.ConditionalSelect(isOutOfRangeN, min, res));
|
|
}
|
|
}
|
|
|
|
[Flags]
|
|
private enum ShrImmSaturatingNarrowFlags
|
|
{
|
|
Scalar = 1 << 0,
|
|
SignedSrc = 1 << 1,
|
|
SignedDst = 1 << 2,
|
|
|
|
Round = 1 << 3,
|
|
|
|
ScalarSxSx = Scalar | SignedSrc | SignedDst,
|
|
ScalarSxZx = Scalar | SignedSrc,
|
|
ScalarZxZx = Scalar,
|
|
|
|
VectorSxSx = SignedSrc | SignedDst,
|
|
VectorSxZx = SignedSrc,
|
|
VectorZxZx = 0
|
|
}
|
|
|
|
private static void EmitRoundShrImmSaturatingNarrowOp(ArmEmitterContext context, ShrImmSaturatingNarrowFlags flags)
|
|
{
|
|
EmitShrImmSaturatingNarrowOp(context, ShrImmSaturatingNarrowFlags.Round | flags);
|
|
}
|
|
|
|
private static void EmitShrImmSaturatingNarrowOp(ArmEmitterContext context, ShrImmSaturatingNarrowFlags flags)
|
|
{
|
|
OpCode32SimdShImm op = (OpCode32SimdShImm)context.CurrOp;
|
|
|
|
bool scalar = (flags & ShrImmSaturatingNarrowFlags.Scalar) != 0;
|
|
bool signedSrc = (flags & ShrImmSaturatingNarrowFlags.SignedSrc) != 0;
|
|
bool signedDst = (flags & ShrImmSaturatingNarrowFlags.SignedDst) != 0;
|
|
bool round = (flags & ShrImmSaturatingNarrowFlags.Round) != 0;
|
|
|
|
if (scalar)
|
|
{
|
|
// TODO: Support scalar operation.
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
int shift = GetImmShr(op);
|
|
long roundConst = 1L << (shift - 1);
|
|
|
|
EmitVectorUnaryNarrowOp32(context, (op1) =>
|
|
{
|
|
if (op.Size <= 1 || !round)
|
|
{
|
|
if (round)
|
|
{
|
|
op1 = context.Add(op1, Const(op1.Type, roundConst));
|
|
}
|
|
|
|
op1 = signedSrc ? context.ShiftRightSI(op1, Const(shift)) : context.ShiftRightUI(op1, Const(shift));
|
|
}
|
|
else /* if (op.Size == 2 && round) */
|
|
{
|
|
op1 = EmitShrImm64(context, op1, signedSrc, roundConst, shift); // shift <= 32
|
|
}
|
|
|
|
return EmitSatQ(context, op1, 8 << op.Size, signedDst);
|
|
}, signedSrc);
|
|
}
|
|
|
|
private static int GetImmShr(OpCode32SimdShImm op)
|
|
{
|
|
return (8 << op.Size) - op.Shift; // Shr amount is flipped.
|
|
}
|
|
|
|
// dst64 = (Int(src64, signed) + roundConst) >> shift;
|
|
private static Operand EmitShrImm64(
|
|
ArmEmitterContext context,
|
|
Operand value,
|
|
bool signed,
|
|
long roundConst,
|
|
int shift)
|
|
{
|
|
MethodInfo info = signed
|
|
? typeof(SoftFallback).GetMethod(nameof(SoftFallback.SignedShrImm64))
|
|
: typeof(SoftFallback).GetMethod(nameof(SoftFallback.UnsignedShrImm64));
|
|
|
|
return context.Call(info, value, Const(roundConst), Const(shift));
|
|
}
|
|
|
|
private static Operand EmitSatQ(ArmEmitterContext context, Operand value, int eSize, bool signed)
|
|
{
|
|
Debug.Assert(eSize <= 32);
|
|
|
|
long intMin = signed ? -(1L << (eSize - 1)) : 0;
|
|
long intMax = signed ? (1L << (eSize - 1)) - 1 : (1L << eSize) - 1;
|
|
|
|
Operand gt = context.ICompareGreater(value, Const(value.Type, intMax));
|
|
Operand lt = context.ICompareLess(value, Const(value.Type, intMin));
|
|
|
|
value = context.ConditionalSelect(gt, Const(value.Type, intMax), value);
|
|
value = context.ConditionalSelect(lt, Const(value.Type, intMin), value);
|
|
|
|
Operand lblNoSat = Label();
|
|
|
|
context.BranchIfFalse(lblNoSat, context.BitwiseOr(gt, lt));
|
|
|
|
context.Call(typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsrQc)));
|
|
|
|
context.MarkLabel(lblNoSat);
|
|
|
|
return value;
|
|
}
|
|
}
|
|
}
|