Skip to content

Commit

Permalink
- New BitTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
tgiphil committed Nov 6, 2023
1 parent ab67bca commit 9abb6cc
Show file tree
Hide file tree
Showing 21 changed files with 690 additions and 306 deletions.
50 changes: 37 additions & 13 deletions Source/Data/IR-Optimizations-BitValue.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@
{
"Type": "BitValue",
"Name": "MulSigned##",
"SubName": "NotSigned",
"SubName": "ButNotSigned",
"Expression": "IR.MulSigned## a b",
"Filter": "IsBitValueSignBitCleared##(a) && IsBitValueSignBitCleared##(b)",
"Result": "(IR.MulUnsigned## a b)",
"Priority": "20",
"Variations": "Yes",
"Variations": "No",
"Log": "No"
},
{
"Type": "BitValue",
"Name": "RemSigned##",
"SubName": "NotSigned",
"SubName": "ButNotSigned",
"Expression": "IR.RemSigned## a b",
"Filter": "IsBitValueSignBitCleared##(a) && IsBitValueSignBitCleared##(b)",
"Result": "(IR.RemUnsigned## a b)",
Expand All @@ -57,7 +57,7 @@
{
"Type": "BitValue",
"Name": "ArithShiftRight##",
"SubName": "NotSigned",
"SubName": "ButNotSigned",
"Expression": "IR.ArithShiftRight## a b",
"Filter": "IsBitValueSignBitCleared##(a)",
"Result": "(IR.ShiftRight## a b)",
Expand All @@ -68,12 +68,18 @@
{
"Type": "BitValue",
"Name": "Compare##x32SignedLess",
"SubName": "SignedLessAdd##",
"SubName": "Add##",
"Expression": "IR.Compare##x32 {<} (IR.Add## a c1) c2",
"Filter": "IsResolvedConstant(c1) && IsResolvedConstant(c2) && !IsAddOverflow##(BitValueMax##(a), BitValueMax##(c1))",
"Result": "(IR.Compare##x32 {<} a (IR.Sub## c2 c1))",
"Variations": "No",
"Log": "Yes"
"Variations": "Yes",
"Log": "Yes",
"Commutative": [
"IR.Compare32x32",
"IR.Compare32x64",
"IR.Compare64x32",
"IR.Compare64x64"
]
},
{
"Type": "BitValue",
Expand All @@ -82,8 +88,14 @@
"Expression": "IR.Compare##x64 {<} (IR.Add## a c1) c2",
"Filter": "IsResolvedConstant(c1) && IsResolvedConstant(c2) && !IsAddOverflow##(BitValueMax##(a), BitValueMax##(c1))",
"Result": "(IR.Compare##x64 {<} a (IR.Sub## c2 c1))",
"Variations": "No",
"Log": "Yes"
"Variations": "Yes",
"Log": "Yes",
"Commutative": [
"IR.Compare32x32",
"IR.Compare32x64",
"IR.Compare64x32",
"IR.Compare64x64"
]
},
{
"Type": "BitValue",
Expand All @@ -92,8 +104,14 @@
"Expression": "IR.Compare##x32 {< u} (IR.Add## a c1) c2",
"Filter": "IsResolvedConstant(c1) && IsResolvedConstant(c2) && !IsAddOverflow##(BitValueMax##(a), BitValueMax##(c1))",
"Result": "(IR.Compare##x32 {<} a (IR.Sub## c2 c1))",
"Variations": "No",
"Log": "Yes"
"Variations": "Yes",
"Log": "Yes",
"Commutative": [
"IR.Compare32x32",
"IR.Compare32x64",
"IR.Compare64x32",
"IR.Compare64x64"
]
},
{
"Type": "BitValue",
Expand All @@ -102,8 +120,14 @@
"Expression": "IR.Compare##x64 {< u} (IR.Add## a c1) c2",
"Filter": "IsResolvedConstant(c1) && IsResolvedConstant(c2) && !IsAddOverflow##(BitValueMax##(a), BitValueMax##(c1))",
"Result": "(IR.Compare##x64 {<} a (IR.Sub## c2 c1))",
"Variations": "No",
"Log": "Yes"
"Variations": "Yes",
"Log": "Yes",
"Commutative": [
"IR.Compare32x32",
"IR.Compare32x64",
"IR.Compare64x32",
"IR.Compare64x64"
]
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -916,21 +916,27 @@ public static class AutoTransforms
new Useless.ZeroExtend16x64ZeroExtend32x64(),
new Useless.ZeroExtend32x64ZeroExtend8x64(),
new Useless.ZeroExtend32x64ZeroExtend16x64(),
new BitValue.MulSigned32NotSigned(),
new BitValue.MulSigned32NotSigned_v1(),
new BitValue.MulSigned64NotSigned(),
new BitValue.MulSigned64NotSigned_v1(),
new BitValue.RemSigned32NotSigned(),
new BitValue.RemSigned64NotSigned(),
new BitValue.ArithShiftRight32NotSigned(),
new BitValue.ArithShiftRight64NotSigned(),
new BitValue.Compare32x32SignedLessSignedLessAdd32(),
new BitValue.Compare64x32SignedLessSignedLessAdd64(),
new BitValue.MulSigned32ButNotSigned(),
new BitValue.MulSigned64ButNotSigned(),
new BitValue.RemSigned32ButNotSigned(),
new BitValue.RemSigned64ButNotSigned(),
new BitValue.ArithShiftRight32ButNotSigned(),
new BitValue.ArithShiftRight64ButNotSigned(),
new BitValue.Compare32x32SignedLessAdd32(),
new BitValue.Compare32x32SignedLessAdd32_v1(),
new BitValue.Compare64x32SignedLessAdd64(),
new BitValue.Compare64x32SignedLessAdd64_v1(),
new BitValue.Compare32x64SignedLessAdd32(),
new BitValue.Compare32x64SignedLessAdd32_v1(),
new BitValue.Compare64x64SignedLessAdd64(),
new BitValue.Compare64x64SignedLessAdd64_v1(),
new BitValue.Compare32x32UnsignedLessAdd32(),
new BitValue.Compare32x32UnsignedLessAdd32_v1(),
new BitValue.Compare64x32UnsignedLessAdd64(),
new BitValue.Compare64x32UnsignedLessAdd64_v1(),
new BitValue.Compare32x64UnsignedLessAdd32(),
new BitValue.Compare32x64UnsignedLessAdd32_v1(),
new BitValue.Compare64x64UnsignedLessAdd64(),
new BitValue.Compare64x64UnsignedLessAdd64_v1(),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Mosa.Compiler.Framework.Transforms.Optimizations.Auto.BitValue;

/// <summary>
/// ArithShiftRight32NotSigned
/// ArithShiftRight32ButNotSigned
/// </summary>
[Transform("IR.Optimizations.Auto.BitValue")]
public sealed class ArithShiftRight32NotSigned : BaseTransform
public sealed class ArithShiftRight32ButNotSigned : BaseTransform
{
public ArithShiftRight32NotSigned() : base(IRInstruction.ArithShiftRight32, TransformType.Auto | TransformType.Optimization)
public ArithShiftRight32ButNotSigned() : base(IRInstruction.ArithShiftRight32, TransformType.Auto | TransformType.Optimization)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
namespace Mosa.Compiler.Framework.Transforms.Optimizations.Auto.BitValue;

/// <summary>
/// ArithShiftRight64NotSigned
/// ArithShiftRight64ButNotSigned
/// </summary>
[Transform("IR.Optimizations.Auto.BitValue")]
public sealed class ArithShiftRight64NotSigned : BaseTransform
public sealed class ArithShiftRight64ButNotSigned : BaseTransform
{
public ArithShiftRight64NotSigned() : base(IRInstruction.ArithShiftRight64, TransformType.Auto | TransformType.Optimization)
public ArithShiftRight64ButNotSigned() : base(IRInstruction.ArithShiftRight64, TransformType.Auto | TransformType.Optimization)
{
}

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

// This code was generated by an automated template.

namespace Mosa.Compiler.Framework.Transforms.Optimizations.Auto.BitValue;

/// <summary>
/// Compare32x32SignedLessAdd32
/// </summary>
[Transform("IR.Optimizations.Auto.BitValue")]
public sealed class Compare32x32SignedLessAdd32 : BaseTransform
{
public Compare32x32SignedLessAdd32() : base(IRInstruction.Compare32x32, TransformType.Auto | TransformType.Optimization, true)
{
}

public override bool Match(Context context, Transform transform)
{
if (context.ConditionCode != ConditionCode.Less)
return false;

if (!context.Operand1.IsVirtualRegister)
return false;

if (!context.Operand1.IsDefinedOnce)
return false;

if (context.Operand1.Definitions[0].Instruction != IRInstruction.Add32)
return false;

if (!IsResolvedConstant(context.Operand1.Definitions[0].Operand2))
return false;

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

if (IsAddOverflow32(BitValueMax32(context.Operand1.Definitions[0].Operand1), BitValueMax32(context.Operand1.Definitions[0].Operand2)))
return false;

return true;
}

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

var t1 = context.Operand1.Definitions[0].Operand1;
var t2 = context.Operand1.Definitions[0].Operand2;
var t3 = context.Operand2;

var v1 = transform.VirtualRegisters.Allocate32();

context.SetInstruction(IRInstruction.Sub32, v1, t3, t2);
context.AppendInstruction(IRInstruction.Compare32x32, ConditionCode.Less, result, t1, v1);
}
}

/// <summary>
/// Compare32x32SignedLessAdd32_v1
/// </summary>
[Transform("IR.Optimizations.Auto.BitValue")]
public sealed class Compare32x32SignedLessAdd32_v1 : BaseTransform
{
public Compare32x32SignedLessAdd32_v1() : base(IRInstruction.Compare32x32, TransformType.Auto | TransformType.Optimization, true)
{
}

public override bool Match(Context context, Transform transform)
{
if (context.ConditionCode != ConditionCode.Greater)
return false;

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

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

if (context.Operand2.Definitions[0].Instruction != IRInstruction.Add32)
return false;

if (!IsResolvedConstant(context.Operand2.Definitions[0].Operand2))
return false;

if (!IsResolvedConstant(context.Operand1))
return false;

if (IsAddOverflow32(BitValueMax32(context.Operand2.Definitions[0].Operand1), BitValueMax32(context.Operand2.Definitions[0].Operand2)))
return false;

return true;
}

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

var t1 = context.Operand1;
var t2 = context.Operand2.Definitions[0].Operand1;
var t3 = context.Operand2.Definitions[0].Operand2;

var v1 = transform.VirtualRegisters.Allocate32();

context.SetInstruction(IRInstruction.Sub32, v1, t1, t3);
context.AppendInstruction(IRInstruction.Compare32x32, ConditionCode.Less, result, t2, v1);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,54 @@ public override void Transform(Context context, Transform transform)
context.AppendInstruction(IRInstruction.Compare32x32, ConditionCode.Less, result, t1, v1);
}
}

/// <summary>
/// Compare32x32UnsignedLessAdd32_v1
/// </summary>
[Transform("IR.Optimizations.Auto.BitValue")]
public sealed class Compare32x32UnsignedLessAdd32_v1 : BaseTransform
{
public Compare32x32UnsignedLessAdd32_v1() : base(IRInstruction.Compare32x32, TransformType.Auto | TransformType.Optimization, true)
{
}

public override bool Match(Context context, Transform transform)
{
if (context.ConditionCode != ConditionCode.UnsignedGreater)
return false;

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

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

if (context.Operand2.Definitions[0].Instruction != IRInstruction.Add32)
return false;

if (!IsResolvedConstant(context.Operand2.Definitions[0].Operand2))
return false;

if (!IsResolvedConstant(context.Operand1))
return false;

if (IsAddOverflow32(BitValueMax32(context.Operand2.Definitions[0].Operand1), BitValueMax32(context.Operand2.Definitions[0].Operand2)))
return false;

return true;
}

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

var t1 = context.Operand1;
var t2 = context.Operand2.Definitions[0].Operand1;
var t3 = context.Operand2.Definitions[0].Operand2;

var v1 = transform.VirtualRegisters.Allocate32();

context.SetInstruction(IRInstruction.Sub32, v1, t1, t3);
context.AppendInstruction(IRInstruction.Compare32x32, ConditionCode.Less, result, t2, v1);
}
}
Loading

0 comments on commit 9abb6cc

Please sign in to comment.