Skip to content

Commit

Permalink
- Implement mosa#1142 (Phi improvement)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgiphil committed Sep 16, 2023
1 parent dcb83e5 commit e69c9d1
Show file tree
Hide file tree
Showing 4 changed files with 100 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 ConstainsOperand(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
28 changes: 27 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,18 @@ 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.ConstainsOperand(stop))
return false;

if (node.ConstainsOperand(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 e69c9d1

Please sign in to comment.