Skip to content

Commit

Permalink
- New BitTracker
Browse files Browse the repository at this point in the history
  • Loading branch information
tgiphil committed Nov 5, 2023
1 parent e073e86 commit 67509a2
Show file tree
Hide file tree
Showing 30 changed files with 670 additions and 30 deletions.
6 changes: 3 additions & 3 deletions Source/Data/IR-Optimizations-BitValue.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"Result": "(IR.MulUnsigned## a b)",
"Priority": "20",
"Variations": "Yes",
"Log": "Yes"
"Log": "No"
},
{
"Type": "BitValue",
Expand All @@ -52,7 +52,7 @@
"Result": "(IR.RemUnsigned## a b)",
"Priority": "20",
"Variations": "No",
"Log": "Yes"
"Log": "No"
},
{
"Type": "BitValue",
Expand All @@ -63,7 +63,7 @@
"Result": "(IR.ShiftRight## a b)",
"Priority": "20",
"Variations": "No",
"Log": "Yes"
"Log": "No"
}
]
}
24 changes: 16 additions & 8 deletions Source/Data/IR-Optimizations-Simplification.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,63 +38,71 @@
"SubName": "Coalescing",
"Expression": "IR.Move## (IR.Move## a)",
"Filter": "!IsCPURegister(a)",
"Result": "(IR.Move## a)"
"Result": "(IR.Move## a)",
"Priority": "25"
},
{
"Type": "Simplification",
"Name": "MoveObject",
"SubName": "Coalescing",
"Expression": "IR.MoveObject (IR.MoveObject a)",
"Filter": "",
"Result": "(IR.MoveObject a)"
"Result": "(IR.MoveObject a)",
"Priority": "25"
},
{
"Type": "Simplification",
"Name": "MoveManagedPointer",
"SubName": "Coalescing",
"Expression": "IR.MoveManagedPointer (IR.MoveManagedPointer a)",
"Filter": "",
"Result": "(IR.MoveManagedPointer a)"
"Result": "(IR.MoveManagedPointer a)",
"Priority": "25"
},
{
"Type": "Simplification",
"Name": "Not##",
"SubName": "Twice",
"Expression": "IR.Not## (IR.Not## x)",
"Filter": "",
"Result": "(IR.Move## x)"
"Result": "(IR.Move## x)",
"Priority": "25"
},
{
"Type": "Simplification",
"Name": "GetLow32",
"SubName": "FromTo64",
"Expression": "IR.GetLow32 (IR.To64 a b))",
"Filter": "",
"Result": "(IR.Move32 a)"
"Result": "(IR.Move32 a)",
"Priority": "25"
},
{
"Type": "Simplification",
"Name": "GetHigh32",
"SubName": "FromTo64",
"Expression": "IR.GetHigh32 (IR.To64 a b))",
"Filter": "",
"Result": "(IR.Move32 b)"
"Result": "(IR.Move32 b)",
"Priority": "25"
},
{
"Type": "Simplification",
"Name": "GetHigh32",
"SubName": "To64",
"Expression": "IR.GetHigh32 (IR.To64 a b)",
"Filter": "",
"Result": "(IR.Move32 b)"
"Result": "(IR.Move32 b)",
"Priority": "25"
},
{
"Type": "Simplification",
"Name": "GetLow32",
"SubName": "To64",
"Expression": "IR.GetLow32 (IR.To64 a b)",
"Filter": "",
"Result": "(IR.Move32 a)"
"Result": "(IR.Move32 a)",
"Priority": "25"
},
{
"Type": "Simplification",
Expand Down
265 changes: 265 additions & 0 deletions Source/Mosa.Compiler.Framework/BaseTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,271 @@ protected static bool IsBitValueSignBitCleared64(Operand operand1)
return operand1.BitValue.IsSignBitClear64;
}

protected static bool? EvaluateCompare(Operand operand1, Operand operand2, ConditionCode condition)
{
return EvaluateCompare(operand1.BitValue, operand2.BitValue, condition);
}

protected static bool? EvaluateCompare(BitValue value1, BitValue value2, ConditionCode condition)
{
switch (condition)
{
case ConditionCode.Equal:
if (value1.AreAll64BitsKnown && value2.AreAll64BitsKnown)
{
return value1.BitsSet == value2.BitsSet;
}
else if (value1.MaxValue == value1.MinValue && value1.MaxValue == value2.MaxValue && value1.MinValue == value2.MinValue)
{
return true;
}
else if (((value1.BitsSet & value2.BitsSet) != value1.BitsSet || (value1.BitsClear & value2.BitsClear) != value1.BitsClear) && !value1.AreAnyBitsKnown && !value2.AreAnyBitsKnown)
{
return false;
}
else if ((value1.BitsSet & value2.BitsClear) != 0 || (value2.BitsSet & value1.BitsClear) != 0)
{
return false;
}
else if (value1.MaxValue < value2.MinValue)
{
return false;
}
else if (value1.MinValue > value2.MaxValue)
{
return false;
}
break;

case ConditionCode.NotEqual:
if (value1.AreAll64BitsKnown && value2.AreAll64BitsKnown)
{
return value1.BitsSet != value2.BitsSet;
}
else if (value1.MaxValue == value1.MinValue && value1.MaxValue == value2.MaxValue && value1.MinValue == value2.MinValue)
{
return false;
}
else if (value1.AreAll64BitsKnown && value1.MaxValue == 0 && value2.BitsSet != 0)
{
return true;
}
else if (value2.AreAll64BitsKnown && value2.MaxValue == 0 && value1.BitsSet != 0)
{
return true;
}
else if ((value1.BitsSet & value2.BitsClear) != 0 || (value2.BitsSet & value1.BitsClear) != 0)
{
return true;
}
else if (value1.MaxValue < value2.MinValue)
{
return true;
}
else if (value1.MinValue > value2.MaxValue)
{
return true;
}
break;

case ConditionCode.UnsignedGreater:
if (value1.AreAll64BitsKnown && value2.AreAll64BitsKnown)
{
return value1.BitsSet > value2.BitsSet;
}
else if (value2.AreAll64BitsKnown && value2.MaxValue == 0 && value1.BitsSet != 0)
{
return true;
}
else if (value1.MinValue > value2.MaxValue)
{
return true;
}
else if (value1.MaxValue <= value2.MinValue)
{
return false;
}
break;

case ConditionCode.UnsignedLess:
if (value1.AreAll64BitsKnown && value2.AreAll64BitsKnown)
{
return value1.MaxValue < value2.MaxValue;
}
else if (value1.AreAll64BitsKnown && value1.MaxValue == 0 && value2.BitsSet != 0)
{
return true;
}
else if (value2.MinValue > value1.MaxValue)
{
return true;
}
else if (value2.MaxValue <= value1.MinValue)
{
return false;
}

break;

case ConditionCode.UnsignedGreaterOrEqual:
if (value1.AreAll64BitsKnown && value2.AreAll64BitsKnown)
{
return value1.BitsSet <= value2.BitsSet;
}
else if (value1.AreAll64BitsKnown && value1.MaxValue == 0 && value2.BitsSet != 0)
{
return true;
}
else if (value1.MinValue >= value2.MaxValue)
{
return true;
}
else if (value1.MaxValue < value2.MinValue)
{
return false;
}

break;

case ConditionCode.UnsignedLessOrEqual:
if (value1.AreAll64BitsKnown && value2.AreAll64BitsKnown)
{
return value1.BitsSet <= value2.BitsSet;
}
else if (value1.AreAll64BitsKnown && value1.MaxValue == 0 && value2.BitsSet != 0)
{
return true;
}
else if (value2.MinValue >= value1.MaxValue)
{
return true;
}
else if (value2.MaxValue < value1.MinValue)
{
return false;
}

break;

case ConditionCode.Greater:
if (value1.AreAll64BitsKnown && value2.AreAll64BitsKnown && value1.Is64Bit && value2.Is64Bit)
{
return (long)value1.BitsSet > (long)value2.BitsSet;
}
else if (value1.AreAll64BitsKnown && value2.AreAll64BitsKnown && value1.Is32Bit && value2.Is32Bit)
{
return (int)value1.BitsSet > (int)value2.BitsSet;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MinValue > value2.MaxValue)
{
return true;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MaxValue < value2.MinValue)
{
return false;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MinValue > value2.MaxValue)
{
return true;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MaxValue < value2.MinValue)
{
return false;
}

break;

case ConditionCode.Less:
if (value1.AreAll64BitsKnown && value2.AreAll64BitsKnown && value1.Is64Bit && value2.Is64Bit)
{
return (long)value1.BitsSet < (long)value2.BitsSet;
}
else if (value1.AreAll64BitsKnown && value2.AreAll64BitsKnown && value1.Is32Bit && value2.Is32Bit)
{
return (int)value1.BitsSet < (int)value2.BitsSet;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MaxValue < value2.MinValue)
{
return true;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MinValue > value2.MaxValue)
{
return false;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MaxValue < value2.MinValue)
{
return true;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MinValue > value2.MaxValue)
{
return false;
}

break;

case ConditionCode.GreaterOrEqual:
if (value1.AreAll64BitsKnown && value2.AreAll64BitsKnown && value1.Is64Bit && value2.Is64Bit)
{
return (long)value1.BitsSet >= (long)value2.BitsSet;
}
else if (value1.AreAll64BitsKnown && value2.AreAll64BitsKnown && value1.Is32Bit && value2.Is32Bit)
{
return (int)value1.BitsSet >= (int)value2.BitsSet;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MinValue >= value2.MaxValue)
{
return true;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MaxValue <= value2.MinValue)
{
return false;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MinValue >= value2.MaxValue)
{
return true;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MaxValue <= value2.MinValue)
{
return false;
}

break;

case ConditionCode.LessOrEqual:
if (value1.AreAll64BitsKnown && value2.AreAll64BitsKnown && value1.Is64Bit && value2.Is64Bit)
{
return (long)value1.BitsSet <= (long)value2.BitsSet;
}
else if (value1.AreAll64BitsKnown && value2.AreAll64BitsKnown && value1.Is32Bit && value2.Is32Bit)
{
return (int)value1.BitsSet <= (int)value2.BitsSet;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MaxValue <= value2.MinValue)
{
return true;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MinValue >= value2.MaxValue)
{
return false;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MaxValue <= value2.MinValue)
{
return true;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MinValue >= value2.MaxValue)
{
return false;
}

break;

default:
return null;
}

return null;
}

#endregion BitTracker Helpers

#region Navigation
Expand Down
10 changes: 0 additions & 10 deletions Source/Mosa.Compiler.Framework/BitValueStage.cs

This file was deleted.

1 change: 0 additions & 1 deletion Source/Mosa.Compiler.Framework/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

using System.Diagnostics;
using Mosa.Compiler.Framework.Linker;
using Mosa.Compiler.Framework.Managers;
using Mosa.Compiler.MosaTypeSystem;

namespace Mosa.Compiler.Framework;
Expand Down
Loading

0 comments on commit 67509a2

Please sign in to comment.