Skip to content

Commit

Permalink
Added Code Motion Optimizations, Transform Manager & Refactoring (#1025)
Browse files Browse the repository at this point in the history
* - Update version

* - Transformation of Call Stage

* - Simple code motion for loads (reduced register allocator spills)

* - Removed Stack Setup Stage

* - Removed Stack Setup Stage

* - Removed Stack Setup Stage

* - Removed Stack Setup Stage

* - Removed Stack Setup Stage

* - Removed Stack Setup Stage

* - Removed Stack Setup Stage

* - Removed Stack Setup Stage

* - Adding Transform Managers
- Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Refactoring

* - Fix

* - Removed code motion (for now)

* - Removed code motion (for now)

* - Fixed namespace

* - Fixed namespace
  • Loading branch information
tgiphil committed Mar 11, 2023
1 parent 143e173 commit e921a6d
Show file tree
Hide file tree
Showing 779 changed files with 4,688 additions and 5,608 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ jobs:
run: dotnet bin/Mosa.Utility.UnitTests.dll -oMax -s Emulator.Display=false
- name: Unit Tests - No Optimizations
run: dotnet bin/Mosa.Utility.UnitTests.dll -oNone -s Emulator.Display=false
- name: Unit Tests - Optimization Level - 1
- name: Unit Tests - Optimization Level 1
run: dotnet bin/Mosa.Utility.UnitTests.dll -o1 -s Emulator.Display=false
- name: Unit Tests - Optimization Level 2
run: dotnet bin/Mosa.Utility.UnitTests.dll -o2 -s Emulator.Display=false
Expand Down
2 changes: 1 addition & 1 deletion Demos/Run-BareMetal.HelloWorld.x86.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cd %~dp0
cd ..\bin
Mosa.Tool.Launcher.exe -autostart -oMax -output-asm -output-debug -output-hash Mosa.BareMetal.HelloWorld.x86.dll
start Mosa.Tool.Launcher.exe -autostart -oMax -output-asm -output-debug -output-hash Mosa.BareMetal.HelloWorld.x86.dll
2 changes: 1 addition & 1 deletion Demos/Run-Demo.CoolWorld.x86.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cd %~dp0
cd ..\bin
Mosa.Tool.Launcher.exe -autostart -oMax -output-asm -output-debug -output-hash Mosa.Demo.CoolWorld.x86.dll
start Mosa.Tool.Launcher.exe -autostart -oMax -output-asm -output-debug -output-hash Mosa.Demo.CoolWorld.x86.dll
2 changes: 1 addition & 1 deletion Demos/Run-Demo.HelloWorld.x86.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cd %~dp0
cd ..\bin
Mosa.Tool.Launcher.exe -autostart -oMax -output-asm -output-debug -output-hash Mosa.Demo.HelloWorld.x86.dll
start Mosa.Tool.Launcher.exe -autostart -oMax -output-asm -output-debug -output-hash Mosa.Demo.HelloWorld.x86.dll
2 changes: 1 addition & 1 deletion Demos/Run-Demo.SVGAWorld.x86.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cd %~dp0
cd ..\bin
Mosa.Tool.Launcher -autostart -oMax -output-asm -output-debug -output-hash -vmdk -vmware-svga -include Include Mosa.Demo.SVGAWorld.x86.dll
start Mosa.Tool.Launcher -autostart -oMax -output-asm -output-debug -output-hash -vmdk -vmware-svga -include Include Mosa.Demo.SVGAWorld.x86.dll
3 changes: 0 additions & 3 deletions Demos/Run-Demo.TestWorld.x86-test.bat

This file was deleted.

2 changes: 1 addition & 1 deletion Demos/Run-Demo.TestWorld.x86.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
cd %~dp0
cd ..\bin
Mosa.Tool.Launcher.exe -autostart -oMax -output-asm -output-debug -output-hash Mosa.Demo.TestWorld.x86.dll
start Mosa.Tool.Launcher.exe -autostart -oMax -output-asm -output-debug -output-hash Mosa.Demo.TestWorld.x86.dll
6 changes: 3 additions & 3 deletions Source/Data/X64-Optimizations-Standard.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"Commutative": [
"X64.Add32",
"X64.And32",
"X64.Or32",
"X64.Add64",
"X64.And64",
"X64.Or32",
"X64.Or64"
],
"Optimizations": [
Expand Down Expand Up @@ -84,9 +84,9 @@
"Type": "Simplication",
"Name": "SubFromZero",
"SubName": "",
"Expression": "X86.Sub## 0 x",
"Expression": "X64.Sub## 0 x",
"Filter": "IsVirtualRegister(x)",
"Result": "(X86.Neg## x)",
"Result": "(X64.Neg## x)",
"Variations": "No",
"Log": "No"
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Data/X86-Optimizations-Standard.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Commutative": [
"X86.Add32",
"X86.And32",
"x64.Or32"
"X86.Or32"
],
"Optimizations": [
{
Expand Down Expand Up @@ -140,7 +140,7 @@
"Filter": "IsVirtualRegister(x)",
"Result": "(X86.Neg32 x)",
"Variations": "No",
"Log": "Yes"
"Log": "No"
}
]
}
2 changes: 1 addition & 1 deletion Source/Mosa.Compiler.Common/BitTwiddling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static int CountLeadingZeros(ulong value)
{
return BitOperations.LeadingZeroCount(value);
}

public static int CountLeadingZeros(uint value)
{
return BitOperations.LeadingZeroCount(value);
Expand Down Expand Up @@ -67,5 +68,4 @@ public static int CountBits(uint value)
{
return BitOperations.PopCount(value);
}

}
3 changes: 3 additions & 0 deletions Source/Mosa.Compiler.Common/IncludeFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ public class IncludeFile
public int Length => Content.Length;

public bool ReadOnly { get; set; } = false;

public bool Hidden { get; set; } = false;

public bool Archive { get; set; } = true;

public bool System { get; set; } = false;

public string SourceFileName { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public sealed class LiveRanges

public Range LastRange => Ranges[Count - 1];

public Range FirstRange { get => Ranges[0];
public Range FirstRange
{
get => Ranges[0];
set => Ranges[0] = value;
}

Expand Down
35 changes: 19 additions & 16 deletions Source/Mosa.Compiler.Framework/BaseMethodCompilerStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,24 @@ public abstract class BaseMethodCompilerStage

#region Instructions Properties

protected BaseInstruction LoadInstruction { get; private set; }
protected BaseInstruction MoveInstruction { get; private set; }

protected BaseInstruction StoreInstruction { get; private set; }
//protected BaseInstruction LoadInstruction { get; private set; }

protected BaseInstruction MoveInstruction { get; private set; }
//protected BaseInstruction StoreInstruction { get; private set; }

protected BaseInstruction SubInstruction { get; private set; }
//protected BaseInstruction SubInstruction { get; private set; }

protected BaseInstruction AddInstruction { get; private set; }
//protected BaseInstruction AddInstruction { get; private set; }

protected BaseInstruction BranchInstruction { get; private set; }
//protected BaseInstruction BranchInstruction { get; private set; }

#endregion Instructions Properties

#region Compiler Properties

protected Compiler Compiler { get; private set; }

/// <summary>
/// The architecture of the compilation process
/// </summary>
Expand Down Expand Up @@ -242,6 +244,7 @@ public abstract class BaseMethodCompilerStage
/// <param name="compiler">The base compiler.</param>
public void Initialize(Compiler compiler)
{
Compiler = compiler;
Architecture = compiler.Architecture;
TypeSystem = compiler.TypeSystem;
TypeLayout = compiler.TypeLayout;
Expand All @@ -259,21 +262,21 @@ public void Initialize(Compiler compiler)

if (Is32BitPlatform)
{
LoadInstruction = IRInstruction.Load32;
StoreInstruction = IRInstruction.Store32;
MoveInstruction = IRInstruction.Move32;
AddInstruction = IRInstruction.Add32;
SubInstruction = IRInstruction.Sub32;
BranchInstruction = IRInstruction.Branch32;
//LoadInstruction = IRInstruction.Load32;
//StoreInstruction = IRInstruction.Store32;
//AddInstruction = IRInstruction.Add32;
//SubInstruction = IRInstruction.Sub32;
//BranchInstruction = IRInstruction.Branch32;
}
else
{
LoadInstruction = IRInstruction.Load64;
StoreInstruction = IRInstruction.Store64;
MoveInstruction = IRInstruction.Move64;
AddInstruction = IRInstruction.Add64;
SubInstruction = IRInstruction.Sub64;
BranchInstruction = IRInstruction.Branch64;
//LoadInstruction = IRInstruction.Load64;
//StoreInstruction = IRInstruction.Store64;
//AddInstruction = IRInstruction.Add64;
//SubInstruction = IRInstruction.Sub64;
//BranchInstruction = IRInstruction.Branch64;
}

Initialize();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

using System;
using Mosa.Compiler.Common;
using Mosa.Compiler.Framework.Managers;
using Mosa.Compiler.Framework.Transforms;

namespace Mosa.Compiler.Framework.Transforms;
namespace Mosa.Compiler.Framework;

[Flags]
public enum TransformType
Expand Down Expand Up @@ -42,7 +44,6 @@ public BaseTransform(BaseInstruction instruction, TransformType type, bool log =
IsOptimization = type.HasFlag(TransformType.Optimization);

Name = GetType().FullName.Replace("Mosa.Platform.", string.Empty).Replace("Mosa.Compiler.Framework.Transforms", "IR").Replace("Transforms.", string.Empty);
TransformDirectory.Add(this);
}

public BaseTransform(TransformType type, bool log = false)
Expand All @@ -61,10 +62,14 @@ int IComparable<BaseTransform>.CompareTo(BaseTransform other)

#endregion Internals

#region Abstract Methods

public abstract bool Match(Context context, TransformContext transform);

public abstract void Transform(Context context, TransformContext transform);

#endregion Abstract Methods

#region Filter Methods

protected static bool IsSSAForm(Operand operand)
Expand Down Expand Up @@ -840,19 +845,7 @@ public static TriState AreStatusFlagsUsed(InstructionNode node, bool checkZero,

#endregion Status Helpers

protected static void RemoveRemainingInstructionInBlock(Context context)
{
var node = context.Node.Next;

while (!node.IsBlockEndInstruction)
{
if (!node.IsEmptyOrNop)
{
node.SetNop();
}
node = node.Next;
}
}
#region Navigation

protected static InstructionNode GetPreviousNodeUntil(Context context, BaseInstruction untilInstruction, int window, Operand operand1 = null, Operand operand2 = null)
{
Expand Down Expand Up @@ -958,42 +951,42 @@ protected static InstructionNode GetNextNodeUntil(Context context, BaseInstructi
return null;
}

protected static bool Compare64(Context context)
#endregion Navigation

#region Helpers

protected static bool Compare32(ConditionCode conditionCode, Operand a, Operand b)
{
return context.ConditionCode switch
return conditionCode switch
{
ConditionCode.Equal => context.Operand1.ConstantSigned64 == context.Operand2.ConstantSigned64,
ConditionCode.NotEqual => context.Operand1.ConstantSigned64 != context.Operand2.ConstantSigned64,
ConditionCode.GreaterOrEqual => context.Operand1.ConstantSigned64 >= context.Operand2.ConstantSigned64,
ConditionCode.Greater => context.Operand1.ConstantSigned64 > context.Operand2.ConstantSigned64,
ConditionCode.LessOrEqual => context.Operand1.ConstantSigned64 <= context.Operand2.ConstantSigned64,
ConditionCode.Less => context.Operand1.ConstantSigned64 < context.Operand2.ConstantSigned64,
ConditionCode.UnsignedGreater => context.Operand1.ConstantUnsigned64 > context.Operand2.ConstantUnsigned64,
ConditionCode.UnsignedGreaterOrEqual => context.Operand1.ConstantUnsigned64 >=
context.Operand2.ConstantUnsigned64,
ConditionCode.UnsignedLess => context.Operand1.ConstantUnsigned64 < context.Operand2.ConstantUnsigned64,
ConditionCode.UnsignedLessOrEqual => context.Operand1.ConstantUnsigned64 <=
context.Operand2.ConstantUnsigned64,
ConditionCode.Equal => a.ConstantSigned32 == b.ConstantSigned32,
ConditionCode.NotEqual => a.ConstantSigned32 != b.ConstantSigned32,
ConditionCode.GreaterOrEqual => a.ConstantSigned32 >= b.ConstantSigned32,
ConditionCode.Greater => a.ConstantSigned32 > b.ConstantSigned32,
ConditionCode.LessOrEqual => a.ConstantSigned32 <= b.ConstantSigned32,
ConditionCode.Less => a.ConstantSigned32 < b.ConstantSigned32,
ConditionCode.UnsignedGreater => a.ConstantUnsigned32 > b.ConstantUnsigned32,
ConditionCode.UnsignedGreaterOrEqual => a.ConstantUnsigned32 >= b.ConstantUnsigned32,
ConditionCode.UnsignedLess => a.ConstantUnsigned32 < b.ConstantUnsigned32,
ConditionCode.UnsignedLessOrEqual => a.ConstantUnsigned32 <= b.ConstantUnsigned32,
_ => throw new InvalidOperationException()
};
}

protected static bool Compare32(Context context)
protected static bool Compare64(ConditionCode conditionCode, Operand a, Operand b)
{
return context.ConditionCode switch
return conditionCode switch
{
ConditionCode.Equal => context.Operand1.ConstantSigned32 == context.Operand2.ConstantSigned32,
ConditionCode.NotEqual => context.Operand1.ConstantSigned32 != context.Operand2.ConstantSigned32,
ConditionCode.GreaterOrEqual => context.Operand1.ConstantSigned32 >= context.Operand2.ConstantSigned32,
ConditionCode.Greater => context.Operand1.ConstantSigned32 > context.Operand2.ConstantSigned32,
ConditionCode.LessOrEqual => context.Operand1.ConstantSigned32 <= context.Operand2.ConstantSigned32,
ConditionCode.Less => context.Operand1.ConstantSigned32 < context.Operand2.ConstantSigned32,
ConditionCode.UnsignedGreater => context.Operand1.ConstantUnsigned32 > context.Operand2.ConstantUnsigned32,
ConditionCode.UnsignedGreaterOrEqual => context.Operand1.ConstantUnsigned32 >=
context.Operand2.ConstantUnsigned32,
ConditionCode.UnsignedLess => context.Operand1.ConstantUnsigned32 < context.Operand2.ConstantUnsigned32,
ConditionCode.UnsignedLessOrEqual => context.Operand1.ConstantUnsigned32 <=
context.Operand2.ConstantUnsigned32,
ConditionCode.Equal => a.ConstantSigned64 == b.ConstantSigned64,
ConditionCode.NotEqual => a.ConstantSigned64 != b.ConstantSigned64,
ConditionCode.GreaterOrEqual => a.ConstantSigned64 >= b.ConstantSigned64,
ConditionCode.Greater => a.ConstantSigned64 > b.ConstantSigned64,
ConditionCode.LessOrEqual => a.ConstantSigned64 <= b.ConstantSigned64,
ConditionCode.Less => a.ConstantSigned64 < b.ConstantSigned64,
ConditionCode.UnsignedGreater => a.ConstantUnsigned64 > b.ConstantUnsigned64,
ConditionCode.UnsignedGreaterOrEqual => a.ConstantUnsigned64 >= b.ConstantUnsigned64,
ConditionCode.UnsignedLess => a.ConstantUnsigned64 < b.ConstantUnsigned64,
ConditionCode.UnsignedLessOrEqual => a.ConstantUnsigned64 <= b.ConstantUnsigned64,
_ => throw new InvalidOperationException()
};
}
Expand All @@ -1016,16 +1009,38 @@ protected static bool IsNormal(ConditionCode conditionCode)
};
}

protected static BasicBlock GetOtherBranchTarget(BasicBlock block, BasicBlock target)
public static bool IsPhiInstruction(BaseInstruction instruction)
{
return block.NextBlocks[0] == target ? block.NextBlocks[1] : block.NextBlocks[0];
return BaseCodeTransformationStage.IsPhiInstruction(instruction);
}

public static bool IsPhiInstruction(BaseInstruction instruction)
#endregion Helpers

#region Block Helpers

protected static void RemoveRemainingInstructionInBlock(Context context)
{
return BaseCodeTransformationStage.IsPhiInstruction(instruction);
var node = context.Node.Next;

while (!node.IsBlockEndInstruction)
{
if (!node.IsEmptyOrNop)
{
node.SetNop();
}
node = node.Next;
}
}

protected static BasicBlock GetOtherBranchTarget(BasicBlock block, BasicBlock target)
{
return block.NextBlocks[0] == target ? block.NextBlocks[1] : block.NextBlocks[0];
}

#endregion Block Helpers

#region Transform Helpers

public static void SwapOperands1And2(Context context)
{
var operand1 = context.Operand1;
Expand All @@ -1034,4 +1049,6 @@ public static void SwapOperands1And2(Context context)
context.Operand1 = operand2;
context.Operand2 = operand1;
}

#endregion Transform Helpers
}
18 changes: 18 additions & 0 deletions Source/Mosa.Compiler.Framework/BaseTransformManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

namespace Mosa.Compiler.Framework;

/// <summary>
/// Base Transform Manager
/// </summary>
public abstract class BaseTransformManager
{
public virtual void Initialize(Compiler compiler)
{ }

public virtual void Setup(MethodCompiler methodCompiler)
{ }

public virtual void Finish()
{ }
}
2 changes: 1 addition & 1 deletion Source/Mosa.Compiler.Framework/BasicBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public InstructionNode BeforeBranch
var node = BeforeLast;

while (node.IsEmpty
|| node.Instruction.FlowControl is FlowControl.UnconditionalBranch or FlowControl.ConditionalBranch or FlowControl.Return)
|| node.Instruction.FlowControl is FlowControl.UnconditionalBranch or FlowControl.ConditionalBranch or FlowControl.Return)
{
node = node.Previous;
}
Expand Down

0 comments on commit e921a6d

Please sign in to comment.