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 67509a2 commit ab67bca
Show file tree
Hide file tree
Showing 14 changed files with 594 additions and 6 deletions.
40 changes: 40 additions & 0 deletions Source/Data/IR-Optimizations-BitValue.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,46 @@
"Priority": "20",
"Variations": "No",
"Log": "No"
},
{
"Type": "BitValue",
"Name": "Compare##x32SignedLess",
"SubName": "SignedLessAdd##",
"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"
},
{
"Type": "BitValue",
"Name": "Compare##x64SignedLess",
"SubName": "Add##",
"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"
},
{
"Type": "BitValue",
"Name": "Compare##x32UnsignedLess",
"SubName": "Add##",
"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"
},
{
"Type": "BitValue",
"Name": "Compare##x64UnsignedLess",
"SubName": "Add##",
"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"
}
]
}
59 changes: 55 additions & 4 deletions Source/Mosa.Compiler.Framework/BaseTransform.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System.Diagnostics;
using Mosa.Compiler.Common;
using Mosa.Compiler.Common.Exceptions;

Expand Down Expand Up @@ -716,6 +717,36 @@ protected static uint UseCount(Operand operand)
return (uint)operand.Uses.Count;
}

protected static bool IsAddSignedOverflow32(int a, int b)
{
return IntegerTwiddling.IsAddSignedOverflow(a, b);
}

protected static bool IsAddSignedOverflow64(long a, long b)
{
return IntegerTwiddling.IsAddSignedOverflow(a, b);
}

protected static bool IsAddUnsignedOverflow32(uint a, uint b)
{
return IntegerTwiddling.IsAddUnsignedCarry(a, b);
}

protected static bool IsAddUnsignedOverflow64(ulong a, ulong b)
{
return IntegerTwiddling.IsAddUnsignedCarry(a, b);
}

protected static bool IsAddOverflow32(ulong a, ulong b)
{
return IsAddSignedOverflow32((int)a, (int)b) || IsAddUnsignedOverflow32((uint)a, (uint)b);
}

protected static bool IsAddOverflow64(ulong a, ulong b)
{
return IsAddSignedOverflow64((long)a, (long)b) || IsAddUnsignedOverflow64(a, b);
}

#endregion Expression Methods

#region SignExtend Helpers
Expand Down Expand Up @@ -850,14 +881,34 @@ public static TriState AreStatusFlagsUsed(Node node, bool checkZero, bool checkC

#region BitTracker Helpers

protected static bool IsBitValueSignBitCleared32(Operand operand1)
protected static uint BitValueMax32(Operand operand)
{
return (uint)operand.BitValue.MaxValue;
}

protected static ulong BitValueMax64(Operand operand)
{
return (ulong)operand.BitValue.MaxValue;
}

protected static uint BitValueMin32(Operand operand)
{
return (uint)operand.BitValue.MinValue;
}

protected static ulong BitValueMin64(Operand operand)
{
return (ulong)operand.BitValue.MinValue;
}

protected static bool IsBitValueSignBitCleared32(Operand operand)
{
return operand1.BitValue.IsSignBitClear32;
return operand.BitValue.IsSignBitClear32;
}

protected static bool IsBitValueSignBitCleared64(Operand operand1)
protected static bool IsBitValueSignBitCleared64(Operand operand)
{
return operand1.BitValue.IsSignBitClear64;
return operand.BitValue.IsSignBitClear64;
}

protected static bool? EvaluateCompare(Operand operand1, Operand operand2, ConditionCode condition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -924,5 +924,13 @@ public static class AutoTransforms
new BitValue.RemSigned64NotSigned(),
new BitValue.ArithShiftRight32NotSigned(),
new BitValue.ArithShiftRight64NotSigned(),
new BitValue.Compare32x32SignedLessSignedLessAdd32(),
new BitValue.Compare64x32SignedLessSignedLessAdd64(),
new BitValue.Compare32x64SignedLessAdd32(),
new BitValue.Compare64x64SignedLessAdd64(),
new BitValue.Compare32x32UnsignedLessAdd32(),
new BitValue.Compare64x32UnsignedLessAdd64(),
new BitValue.Compare32x64UnsignedLessAdd32(),
new BitValue.Compare64x64UnsignedLessAdd64(),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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>
/// Compare32x32SignedLessSignedLessAdd32
/// </summary>
[Transform("IR.Optimizations.Auto.BitValue")]
public sealed class Compare32x32SignedLessSignedLessAdd32 : BaseTransform
{
public Compare32x32SignedLessSignedLessAdd32() : 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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>
/// Compare32x32UnsignedLessAdd32
/// </summary>
[Transform("IR.Optimizations.Auto.BitValue")]
public sealed class Compare32x32UnsignedLessAdd32 : BaseTransform
{
public Compare32x32UnsignedLessAdd32() : base(IRInstruction.Compare32x32, TransformType.Auto | TransformType.Optimization, true)
{
}

public override bool Match(Context context, Transform transform)
{
if (context.ConditionCode != ConditionCode.UnsignedLess)
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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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>
/// Compare32x64SignedLessAdd32
/// </summary>
[Transform("IR.Optimizations.Auto.BitValue")]
public sealed class Compare32x64SignedLessAdd32 : BaseTransform
{
public Compare32x64SignedLessAdd32() : base(IRInstruction.Compare32x64, 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.Compare32x64, ConditionCode.Less, result, t1, v1);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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>
/// Compare32x64UnsignedLessAdd32
/// </summary>
[Transform("IR.Optimizations.Auto.BitValue")]
public sealed class Compare32x64UnsignedLessAdd32 : BaseTransform
{
public Compare32x64UnsignedLessAdd32() : base(IRInstruction.Compare32x64, TransformType.Auto | TransformType.Optimization, true)
{
}

public override bool Match(Context context, Transform transform)
{
if (context.ConditionCode != ConditionCode.UnsignedLess)
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.Compare32x64, ConditionCode.Less, result, t1, v1);
}
}
Loading

0 comments on commit ab67bca

Please sign in to comment.