Skip to content

Commit

Permalink
- X86 Lea Expansion (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgiphil committed Nov 12, 2023
1 parent 254fdde commit 9c3bdec
Show file tree
Hide file tree
Showing 35 changed files with 407 additions and 265 deletions.
296 changes: 152 additions & 144 deletions Source/Data/X86-Instructions.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,28 @@ namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual;
/// </summary>
public static class ManualTransforms
{
public static readonly List<BaseTransform> List = new List<BaseTransform>
public static readonly List<BaseTransform> List = new()
{
new Special.Deadcode(),

//new Standard.Mov32ToXor32(),
new Standard.Mov64ToXor64(),
new Standard.Add32ToInc32(),
new Standard.Sub32ToDec32(),
new Standard.Lea32ToInc32(),
new Standard.Lea32ToDec32(),
new Standard.Cmp32ToZero(),
new Standard.Test32ToZero(),
new Standard.Cmp32ToTest32(),
new Rewrite.Add32ToInc32(),
new Rewrite.Add32ToLea32(),
new Rewrite.Add64ToLea64(),
new Rewrite.Mov32ToXor32(),
new Rewrite.Mov64ToXor64(),
new Rewrite.Sub32ToDec32(),
new Rewrite.Sub64ToLea64(),
new Rewrite.Sub64ToLea64(),
new Rewrite.Lea32ToInc32(),
new Rewrite.Lea32ToDec32(),
new Rewrite.Lea32ToMov32(),
new Rewrite.Lea64ToMov64(),
new Rewrite.Cmp32ToZero(),
new Rewrite.Cmp32ToTest32(),
new Rewrite.Test32ToZero(),

//Add64ToLea64
//Add32ToLea32
//Sub64ToLea64
//Sub64ToLea64
};

public static readonly List<BaseTransform> PostList = new List<BaseTransform>
{
new Special.Mov32ConstantReuse(),
};

public static readonly List<BaseTransform> EarlyList = new List<BaseTransform>
{
new Stack.Add32(),
new Stack.Add64(),

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

// This code was generated by an automated template.

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Standard;
namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;

[Transform("x64.Optimizations.Manual.Standard")]
[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Add32ToInc32 : BaseTransform
{
public Add32ToInc32() : base(X64.Add32, TransformType.Manual | TransformType.Optimization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Standard;
namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;
// This transformation can reduce restrictions placed on the register allocator.
// The LEA does not change any of the status flags, however, the add instruction does modify some flags (carry, zero, etc.)
// Therefore, this transformation can only occur if the status flags are unused later.
// A search is required to determine if a status flag is used.
// However, if the search is not conclusive, the transformation is not made.

[Transform("x64.Optimizations.Manual.Standard")]
[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Add32ToLea32 : BaseTransform
{
public Add32ToLea32() : base(X64.Add32, TransformType.Manual | TransformType.Optimization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Standard;
namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;
// This transformation can reduce restrictions placed on the register allocator.
// The LEA does not change any of the status flags, however, the add instruction does modify some flags (carry, zero, etc.)
// Therefore, this transformation can only occur if the status flags are unused later.
// A search is required to determine if a status flag is used.
// However, if the search is not conclusive, the transformation is not made.

[Transform("x64.Optimizations.Manual.Standard")]
[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Add64ToLea64 : BaseTransform
{
public Add64ToLea64() : base(X64.Add64, TransformType.Manual | TransformType.Optimization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Standard;
namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;

[Transform("x64.Optimizations.Manual.Standard")]
[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Cmp32ToTest32 : BaseTransform
{
public Cmp32ToTest32() : base(X64.Cmp32, TransformType.Manual | TransformType.Optimization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Standard;
namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;

[Transform("x64.Optimizations.Manual.Standard")]
[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Cmp32ToZero : BaseTransform
{
public Cmp32ToZero() : base(X64.Cmp32, TransformType.Manual | TransformType.Optimization)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

// This code was generated by an automated template.

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Standard;
namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;

[Transform("x64.Optimizations.Manual.Standard")]
[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Lea32ToDec32 : BaseTransform
{
public Lea32ToDec32() : base(X64.Lea32, TransformType.Manual | TransformType.Optimization)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

// This code was generated by an automated template.

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Standard;
namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;

[Transform("x64.Optimizations.Manual.Standard")]
[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Lea32ToInc32 : BaseTransform
{
public Lea32ToInc32() : base(X64.Lea32, TransformType.Manual | TransformType.Optimization)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;

[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Lea32ToMov32 : BaseTransform
{
public Lea32ToMov32() : base(X64.Lea32, TransformType.Manual | TransformType.Optimization)
{
}

public override bool Match(Context context, Transform transform)
{
if (!context.Operand2.IsResolvedConstant)
return false;

if (!context.Operand2.IsConstantZero)
return false;

return true;
}

public override void Transform(Context context, Transform transform)
{
var result = context.Result;
var operand1 = context.Operand1;

context.SetInstruction(X64.Mov32, result, operand1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;

[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Lea64ToMov64 : BaseTransform
{
public Lea64ToMov64() : base(X64.Lea64, TransformType.Manual | TransformType.Optimization)
{
}

public override bool Match(Context context, Transform transform)
{
if (!context.Operand2.IsResolvedConstant)
return false;

if (!context.Operand2.IsConstantZero)
return false;

return true;
}

public override void Transform(Context context, Transform transform)
{
var result = context.Result;
var operand1 = context.Operand1;

context.SetInstruction(X64.Mov64, result, operand1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Manual.Standard;
namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;

[Transform("x64.Optimizations.Manual.Standard")]
[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Mov32ToXor32 : BaseTransform
{
public Mov32ToXor32() : base(X64.Mov32, TransformType.Manual | TransformType.Optimization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Standard;
namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;

[Transform("x64.Optimizations.Manual.Standard")]
[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Mov64ToXor64 : BaseTransform
{
public Mov64ToXor64() : base(X64.Mov64, TransformType.Manual | TransformType.Optimization)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

// This code was generated by an automated template.

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Standard;
namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;

[Transform("x64.Optimizations.Manual.Standard")]
[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Sub32ToDec32 : BaseTransform
{
public Sub32ToDec32() : base(X64.Sub32, TransformType.Manual | TransformType.Optimization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Standard;
namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;
// This transformation can reduce restrictions placed on the register allocator.
// The LEA does not change any of the status flags, however, the sub instruction does modify some flags (carry, zero, etc.)
// Therefore, this transformation can only occur if the status flags are unused later.
// A search is required to determine if a status flag is used.
// However, if the search is not conclusive, the transformation is not made.

[Transform("x64.Optimizations.Manual.Standard")]
[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Sub32ToLea32 : BaseTransform
{
public Sub32ToLea32() : base(X64.Sub32, TransformType.Manual | TransformType.Optimization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Standard;
namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;
// This transformation can reduce restrictions placed on the register allocator.
// The LEA does not change any of the status flags, however, the sub instruction does modify some flags (carry, zero, etc.)
// Therefore, this transformation can only occur if the status flags are unused later.
// A search is required to determine if a status flag is used.
// However, if the search is not conclusive, the transformation is not made.

[Transform("x64.Optimizations.Manual.Standard")]
[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Sub64ToLea64 : BaseTransform
{
public Sub64ToLea64() : base(X64.Sub64, TransformType.Manual | TransformType.Optimization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Standard;
namespace Mosa.Compiler.x64.Transforms.Optimizations.Manual.Rewrite;

[Transform("x64.Optimizations.Manual.Standard")]
[Transform("x64.Optimizations.Manual.Rewrite")]
public sealed class Test32ToZero : BaseTransform
{
public Test32ToZero() : base(X64.Test32, TransformType.Manual | TransformType.Optimization)
Expand Down
Loading

0 comments on commit 9c3bdec

Please sign in to comment.