Skip to content

Commit

Permalink
Improve the SSA Exit Stage (#1145)
Browse files Browse the repository at this point in the history
* - Additional shift/division optimizations

* - Additional optimizations

* - Additional optimizations

* - WIP

* - WIP

* - WIP

* - WIP

* - Implement #1142 (Phi improvement)

* - Implement #1142 (Phi improvement)

* - WIP
  • Loading branch information
tgiphil committed Sep 16, 2023
1 parent 2f7f8d7 commit 7c9945c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
25 changes: 25 additions & 0 deletions Source/Mosa.Compiler.Framework/InstructionNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,31 @@ public void ReplaceInstruction(BaseInstruction instruction)
Instruction = instruction;
}

public bool ContainsOperand(Operand operand)
{
foreach (var op in Operands)
{
if (op == operand)
return true;
}

return false;
}

public bool ContainsResult(Operand operand)
{
if (ResultCount == 0)
return false;

if (ResultCount >= 1 && Result == operand)
return true;

if (ResultCount == 2 && Result2 == operand)
return true;

return false;
}

#endregion Methods

#region Navigation
Expand Down
31 changes: 30 additions & 1 deletion Source/Mosa.Compiler.Framework/Stages/ExitSSAStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ namespace Mosa.Compiler.Framework.Stages;
public class ExitSSAStage : BaseMethodCompilerStage
{
private readonly Counter InstructionCount = new("ExitSSAStage.IRInstructions");
private readonly Counter MoveAvoidedCount = new("ExitSSAStage.MoveAvoided");

protected override void Initialize()
{
Register(InstructionCount);
Register(MoveAvoidedCount);
}

protected override void Run()
Expand Down Expand Up @@ -56,7 +58,7 @@ private void ProcessPhiInstruction(InstructionNode node)
{
var sourceBlocks = node.PhiBlocks;

for (var index = 0; index < node.Block.PreviousBlocks.Count; index++)
for (var index = 0; index < sourceBlocks.Count; index++)
{
var operand = node.GetOperand(index);
var predecessor = sourceBlocks[index];
Expand All @@ -78,6 +80,16 @@ private void InsertCopyStatement(BasicBlock predecessor, Operand destination, Op
if (destination == source)
return;

if (source.IsDefinedOnce && source.IsUsedOnce && source.Definitions[0].Block == predecessor)
{
if (destination.IsUsedOnce || CheckIfLast(predecessor, destination, source))
{
source.Definitions[0].Result = destination;
MoveAvoidedCount.Increment();
return;
}
}

var node = predecessor.BeforeLast;

while (node.Instruction != IRInstruction.Jmp)
Expand All @@ -96,4 +108,21 @@ private void InsertCopyStatement(BasicBlock predecessor, Operand destination, Op
var moveInstruction = MethodCompiler.GetMoveInstruction(destination.Primitive);
context.AppendInstruction(moveInstruction, destination, source);
}

private bool CheckIfLast(BasicBlock block, Operand stop, Operand okay)
{
for (var node = block.BeforeLast; !node.IsBlockStartInstruction; node = node.Previous)
{
if (node.IsEmptyOrNop)
continue;

if (node.ContainsOperand(stop))
return false;

if (node.ContainsOperand(okay))
return true;
}

return false;
}
}
47 changes: 47 additions & 0 deletions Source/Mosa.UnitTests/Optimization/LoopTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

namespace Mosa.UnitTests.Optimization;

public static class LoopTests
{
private static uint[] array = { 1, 2, 3, 4 };

[MosaUnitTest]
public static uint Loop0()
{
var x = 0u;

for (uint i = 0; i < 4; i++)
{
x += 2;
}

return x;
}

[MosaUnitTest]
public static uint Loop1()
{
var x = 0u;

for (uint i = 0; i < 4; i++)
{
x += i * 4 + 4;
}

return x;
}

[MosaUnitTest]
public static uint Loop2()
{
var x = 0u;

for (uint i = 0; i < 4; i++)
{
x += array[i];
}

return x;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<Compile Include="..\Mosa.UnitTests\Optimization\RemAndModTests.cs" Link="Tests\Optimization\RemAndModTests.cs" />
<Compile Include="..\Mosa.UnitTests\Optimization\SwitchTests.cs" Link="Tests\Optimization\SwitchTests.cs" />
<Compile Include="..\Mosa.UnitTests\Optimization\SpecificTests.cs" Link="Tests\Optimization\SpecificTests.cs" />
<Compile Include="..\Mosa.UnitTests\Optimization\LoopTests.cs" Link="Tests\Optimization\LoopTests.cs" />

<Compile Include="..\Mosa.UnitTests\Optimization\LoopUnrollingTests.cs" Link="Tests\Optimization\LoopUnrollingTests.cs" />

Expand Down

0 comments on commit 7c9945c

Please sign in to comment.