Skip to content

Commit

Permalink
Loop Range Optimization (#1219)
Browse files Browse the repository at this point in the history
* - Fix

* - Settings update

* - WIP

* - WIP

* - WIP

* - WIP

* - WIP

* - WIP
  • Loading branch information
tgiphil committed Apr 16, 2024
1 parent 3faa11f commit 9bd605e
Show file tree
Hide file tree
Showing 11 changed files with 1,059 additions and 75 deletions.
32 changes: 16 additions & 16 deletions Source/Mosa.Compiler.Framework/BaseTransform.cs
Expand Up @@ -1183,19 +1183,19 @@ protected static bool IsBitValueZeroOrOne(Operand operand)
{
return (int)value1.BitsSet > (int)value2.BitsSet;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MinValue > value2.MaxValue)
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitKnown32 && value2.IsSignBitKnown32 && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MinValue > value2.MaxValue)
{
return true;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MaxValue < value2.MinValue)
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitKnown32 && value2.IsSignBitKnown32 && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MaxValue < value2.MinValue)
{
return false;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MinValue > value2.MaxValue)
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitKnown64 && value2.IsSignBitKnown64 && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MinValue > value2.MaxValue)
{
return true;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MaxValue < value2.MinValue)
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitKnown64 && value2.IsSignBitKnown64 && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MaxValue < value2.MinValue)
{
return false;
}
Expand All @@ -1211,19 +1211,19 @@ protected static bool IsBitValueZeroOrOne(Operand operand)
{
return (int)value1.BitsSet < (int)value2.BitsSet;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MaxValue < value2.MinValue)
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitKnown32 && value2.IsSignBitKnown32 && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MaxValue < value2.MinValue)
{
return true;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MinValue > value2.MaxValue)
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitKnown32 && value2.IsSignBitKnown32 && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MinValue > value2.MaxValue)
{
return false;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MaxValue < value2.MinValue)
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitKnown64 && value2.IsSignBitKnown64 && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MaxValue < value2.MinValue)
{
return true;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MinValue > value2.MaxValue)
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitKnown64 && value2.IsSignBitKnown64 && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MinValue > value2.MaxValue)
{
return false;
}
Expand All @@ -1239,19 +1239,19 @@ protected static bool IsBitValueZeroOrOne(Operand operand)
{
return (int)value1.BitsSet >= (int)value2.BitsSet;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MinValue >= value2.MaxValue)
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitKnown32 && value2.IsSignBitKnown32 && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MinValue >= value2.MaxValue)
{
return true;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MaxValue <= value2.MinValue)
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitKnown32 && value2.IsSignBitKnown32 && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MaxValue <= value2.MinValue)
{
return false;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MinValue >= value2.MaxValue)
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitKnown64 && value2.IsSignBitKnown64 && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MinValue >= value2.MaxValue)
{
return true;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MaxValue <= value2.MinValue)
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitKnown64 && value2.IsSignBitKnown64 && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MaxValue <= value2.MinValue)
{
return false;
}
Expand All @@ -1267,19 +1267,19 @@ protected static bool IsBitValueZeroOrOne(Operand operand)
{
return (int)value1.BitsSet <= (int)value2.BitsSet;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MaxValue <= value2.MinValue)
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitKnown32 && value2.IsSignBitKnown32 && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MaxValue <= value2.MinValue)
{
return true;
}
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MinValue >= value2.MaxValue)
else if (value1.Is32Bit && value2.Is32Bit && value1.IsSignBitKnown32 && value2.IsSignBitKnown32 && value1.IsSignBitClear32 && value2.IsSignBitClear32 && value1.MinValue >= value2.MaxValue)
{
return false;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MaxValue <= value2.MinValue)
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitKnown64 && value2.IsSignBitKnown64 && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MaxValue <= value2.MinValue)
{
return true;
}
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MinValue >= value2.MaxValue)
else if (value1.Is64Bit && value2.Is64Bit && value1.IsSignBitKnown64 && value2.IsSignBitKnown64 && value1.IsSignBitClear64 && value2.IsSignBitClear64 && value1.MinValue >= value2.MaxValue)
{
return false;
}
Expand Down
34 changes: 29 additions & 5 deletions Source/Mosa.Compiler.Framework/BitValue.cs
Expand Up @@ -188,6 +188,30 @@ public BitValue NarrowMin(ulong minValue)
return Narrow();
}

public BitValue NarrowSignRange(long minValue, long maxValue)
{
if (IsFixed)
return this;

if (minValue > maxValue)
{
(minValue, maxValue) = (maxValue, minValue); // swap
}

if (minValue >= 0 && maxValue >= 0)
{
return NarrowMax((ulong)maxValue).NarrowMin((ulong)minValue);
}
else if (minValue < 0 && maxValue < 0)
{
return NarrowMin((ulong)maxValue).NarrowMax((ulong)minValue);
}

// unable to narrow

return this;
}

public BitValue NarrowSetBits(ulong bitsSet)
{
if (IsFixed)
Expand Down Expand Up @@ -322,11 +346,11 @@ public override string ToString()
{
var sb = new StringBuilder();

sb.Append($"MaxValue: {MaxValue}");
sb.Append($"MinValue: {MinValue}");
sb.Append($"BitsSet: {Convert.ToString((long)BitsSet, 2).PadLeft(64, '0')}");
sb.Append($"BitsClear: {Convert.ToString((long)BitsClear, 2).PadLeft(64, '0')}");
sb.Append($"BitsKnown: {Convert.ToString((long)BitsKnown, 2).PadLeft(64, '0')}");
sb.AppendLine($" Min: {MinValue}");
sb.AppendLine($" Max: {MaxValue}");
sb.AppendLine($" BitsSet: {Convert.ToString((long)BitsSet, 2).PadLeft(64, '0')}");
sb.AppendLine($" BitsClear: {Convert.ToString((long)BitsClear, 2).PadLeft(64, '0')}");
sb.AppendLine($" BitsKnown: {Convert.ToString((long)BitsKnown, 2).PadLeft(64, '0')}");

return sb.ToString();
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Mosa.Compiler.Framework/Compiler.cs
Expand Up @@ -161,14 +161,14 @@ public sealed class Compiler
mosaSettings.SparseConditionalConstantPropagation && mosaSettings.SSA ? new SparseConditionalConstantPropagationStage() : null,
mosaSettings.BasicOptimizations && mosaSettings.SSA && (mosaSettings.ValueNumbering || mosaSettings.LoopInvariantCodeMotion || mosaSettings.SparseConditionalConstantPropagation) ? new OptimizationStage(false) : null,
mosaSettings.BitTracker ? new BitTrackerStage() : null,
//mosaSettings.LoopRangeTracker && mosaSettings.SSA ? new LoopRangeTrackerStage() : null,
mosaSettings.LoopRangeTracker && mosaSettings.SSA ? new LoopRangeTrackerStage() : null,
mosaSettings.BasicOptimizations ? new OptimizationStage(mosaSettings.LongExpansion) : null,

mosaSettings.TwoPassOptimization && mosaSettings.ValueNumbering && mosaSettings.SSA ? new ValueNumberingStage() : null,
mosaSettings.TwoPassOptimization && mosaSettings.LoopInvariantCodeMotion && mosaSettings.SSA ? new LoopInvariantCodeMotionStage() : null,
mosaSettings.TwoPassOptimization && mosaSettings.SparseConditionalConstantPropagation && mosaSettings.SSA ? new SparseConditionalConstantPropagationStage() : null,
mosaSettings.TwoPassOptimization && mosaSettings.BitTracker ? new BitTrackerStage() : null,
//mosaSettings.TwoPassOptimization && mosaSettings.LoopRangeTracker && mosaSettings.SSA ? new LoopRangeTrackerStage() : null,
mosaSettings.TwoPassOptimization && mosaSettings.LoopRangeTracker && mosaSettings.SSA ? new LoopRangeTrackerStage() : null,
mosaSettings.TwoPassOptimization && mosaSettings.BasicOptimizations && mosaSettings.SSA ? new OptimizationStage(mosaSettings.LongExpansion) : null,

new NopRemovalStage(),
Expand Down
21 changes: 21 additions & 0 deletions Source/Mosa.Compiler.Framework/Operand.cs
Expand Up @@ -113,6 +113,27 @@ public bool IsConstantZero
}
}

public bool IsNegative
{
get
{
if (!IsResolvedConstant)
return false;
else if (IsLocalStack || IsOnStack || IsParameter)
return ConstantUnsigned64 < 0;
else if (IsNull)
return false;
else if (IsR8)
return ConstantDouble < 0;
else if (IsR4)
return ConstantFloat < 0;
else if (IsInt32)
return ConstantSigned32 < 0;
else
return ConstantSigned64 < 0;
}
}

public bool IsPhysicalRegister => Location == LocationType.PhysicalRegister;

public bool IsFloatingPoint => IsR4 | IsR8;
Expand Down
10 changes: 8 additions & 2 deletions Source/Mosa.Compiler.Framework/Stages/BaseTransformStage.cs
Expand Up @@ -275,9 +275,15 @@ private bool ApplyBlockTransforms()
var updated = count != 0;
changed |= updated;

if (updated && MethodCompiler.Statistics)
if (updated)
{
UpdateCounter(transform.Name, count);
MethodCompiler.CreateTranformInstructionTrace(this, Steps++);

if (MosaSettings.FullCheckMode)
FullCheck(false);

if (MethodCompiler.Statistics)
UpdateCounter(transform.Name, count);
}
}

Expand Down

0 comments on commit 9bd605e

Please sign in to comment.