From 2d9179bc44145675fe103932e1cc463e155b5044 Mon Sep 17 00:00:00 2001 From: Phil Date: Fri, 20 Oct 2023 22:25:32 -0700 Subject: [PATCH] - WIP --- Source/Mosa.Compiler.Framework/Operand.cs | 2 + .../Transforms/LowerTo32/MulSigned64.cs | 7 +-- .../Transforms/LowerTo32/MulUnsigned64.cs | 7 +-- .../Optimizations/Manual/ManualTransforms.cs | 4 +- .../Manual/Special/Mul32Ditto.cs | 48 +++++++++++++++++++ 5 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 Source/Mosa.Compiler.x86/Transforms/Optimizations/Manual/Special/Mul32Ditto.cs diff --git a/Source/Mosa.Compiler.Framework/Operand.cs b/Source/Mosa.Compiler.Framework/Operand.cs index 782466b9d3..bd4e62183c 100644 --- a/Source/Mosa.Compiler.Framework/Operand.cs +++ b/Source/Mosa.Compiler.Framework/Operand.cs @@ -195,6 +195,8 @@ public long Offset public bool IsUsedOnce => Uses.Count == 1; + public bool IsUsedMoreThanOnce => Uses.Count > 1; + public bool IsVirtualRegisterUsed { get diff --git a/Source/Mosa.Compiler.Framework/Transforms/LowerTo32/MulSigned64.cs b/Source/Mosa.Compiler.Framework/Transforms/LowerTo32/MulSigned64.cs index 1e8963e4bd..ee9e95fbae 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/LowerTo32/MulSigned64.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/LowerTo32/MulSigned64.cs @@ -33,16 +33,11 @@ public override void Transform(Context context, TransformContext transform) context.AppendInstruction(IRInstruction.GetLow32, op1Low, operand2); context.AppendInstruction(IRInstruction.GetHigh32, op1High, operand2); - context.SetInstruction(IRInstruction.GetLow32, op0Low, operand1); - context.AppendInstruction(IRInstruction.GetHigh32, op0High, operand1); - context.AppendInstruction(IRInstruction.GetLow32, op1Low, operand2); - context.AppendInstruction(IRInstruction.GetHigh32, op1High, operand2); - context.AppendInstruction(IRInstruction.MulSigned32, v1, op1High, op0Low); context.AppendInstruction(IRInstruction.MulSigned32, v2, op1Low, op0High); + context.AppendInstruction(IRInstruction.Add32, v4, v2, v1); context.AppendInstruction(IRInstruction.MulHu32, v3, op1Low, op0Low); context.AppendInstruction(IRInstruction.MulUnsigned32, v6, op1Low, op0Low); - context.AppendInstruction(IRInstruction.Add32, v4, v2, v1); context.AppendInstruction(IRInstruction.Add32, v5, v3, v4); context.AppendInstruction(IRInstruction.To64, result, v6, v5); diff --git a/Source/Mosa.Compiler.Framework/Transforms/LowerTo32/MulUnsigned64.cs b/Source/Mosa.Compiler.Framework/Transforms/LowerTo32/MulUnsigned64.cs index 4aa29b51a6..b42842e968 100644 --- a/Source/Mosa.Compiler.Framework/Transforms/LowerTo32/MulUnsigned64.cs +++ b/Source/Mosa.Compiler.Framework/Transforms/LowerTo32/MulUnsigned64.cs @@ -33,16 +33,11 @@ public override void Transform(Context context, TransformContext transform) context.AppendInstruction(IRInstruction.GetLow32, op1Low, operand2); context.AppendInstruction(IRInstruction.GetHigh32, op1High, operand2); - context.SetInstruction(IRInstruction.GetLow32, op0Low, operand1); - context.AppendInstruction(IRInstruction.GetHigh32, op0High, operand1); - context.AppendInstruction(IRInstruction.GetLow32, op1Low, operand2); - context.AppendInstruction(IRInstruction.GetHigh32, op1High, operand2); - context.AppendInstruction(IRInstruction.MulSigned32, v1, op1High, op0Low); context.AppendInstruction(IRInstruction.MulSigned32, v2, op1Low, op0High); + context.AppendInstruction(IRInstruction.Add32, v4, v2, v1); context.AppendInstruction(IRInstruction.MulHu32, v3, op1Low, op0Low); context.AppendInstruction(IRInstruction.MulUnsigned32, v6, op1Low, op0Low); - context.AppendInstruction(IRInstruction.Add32, v4, v2, v1); context.AppendInstruction(IRInstruction.Add32, v5, v3, v4); context.AppendInstruction(IRInstruction.To64, result, v6, v5); diff --git a/Source/Mosa.Compiler.x86/Transforms/Optimizations/Manual/ManualTransforms.cs b/Source/Mosa.Compiler.x86/Transforms/Optimizations/Manual/ManualTransforms.cs index 7befc17914..551b52ea95 100644 --- a/Source/Mosa.Compiler.x86/Transforms/Optimizations/Manual/ManualTransforms.cs +++ b/Source/Mosa.Compiler.x86/Transforms/Optimizations/Manual/ManualTransforms.cs @@ -27,11 +27,13 @@ public static class ManualTransforms //new Special.Mov32Propagate(), //Add32ToLea32 //Sub32ToLea32 - + new Special.Mov32Unless(), new Special.Mov32Coalescing(), new StrengthReduction.Mul32ByZero(), new StrengthReduction.Mul32WithMov32ByZero(), + + new Special.Mul32Ditto(), }; } diff --git a/Source/Mosa.Compiler.x86/Transforms/Optimizations/Manual/Special/Mul32Ditto.cs b/Source/Mosa.Compiler.x86/Transforms/Optimizations/Manual/Special/Mul32Ditto.cs new file mode 100644 index 0000000000..5a93835f5f --- /dev/null +++ b/Source/Mosa.Compiler.x86/Transforms/Optimizations/Manual/Special/Mul32Ditto.cs @@ -0,0 +1,48 @@ +// Copyright (c) MOSA Project. Licensed under the New BSD License. + +using Mosa.Compiler.Framework; + +namespace Mosa.Compiler.x86.Transforms.Optimizations.Manual.Special; + +[Transform("x86.Optimizations.Manual.Special")] +public sealed class Mul32Ditto : BaseTransform +{ + public Mul32Ditto() : base(X86.Mul32, TransformType.Manual | TransformType.Optimization) + { + } + + public override bool Match(Context context, TransformContext transform) + { + if (transform.AreCPURegistersAllocated) + return false; + + if (!context.Result.IsVirtualRegister) + return false; + + if (!context.Result2.IsVirtualRegister) + return false; + + var previous = context.Node.PreviousNonEmpty; + + if (previous == null || previous.Instruction != X86.Mul32) + return false; + + if (context.Operand1 != previous.Operand1) + return false; + + if (context.Operand2 != previous.Operand2) + return false; + + return true; + } + + public override void Transform(Context context, TransformContext transform) + { + var previous = context.Node.PreviousNonEmpty; + var result = context.Result; + var result2 = context.Result2; + + context.SetInstruction(X86.Mov32, result, previous.Result); + context.AppendInstruction(X86.Mov32, result2, previous.Result2); + } +}