Skip to content

Commit

Permalink
- Updated Switch handling (#1061)
Browse files Browse the repository at this point in the history
* - Updated switch handling
- Refactoring

* - Updated switch handling
- Refactoring
  • Loading branch information
tgiphil committed May 31, 2023
1 parent 5eb79c2 commit afd61df
Show file tree
Hide file tree
Showing 18 changed files with 108 additions and 145 deletions.
106 changes: 50 additions & 56 deletions Source/Mosa.Compiler.Framework/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,63 +168,57 @@ private static List<BaseCompilerStage> GetDefaultCompilerPipeline(CompilerSettin
};
}

private static List<BaseMethodCompilerStage> GetDefaultMethodPipeline(CompilerSettings compilerSettings, bool is64BitPlatform)
private static List<BaseMethodCompilerStage> GetDefaultMethodPipeline(CompilerSettings compilerSettings, bool is64BitPlatform) => new List<BaseMethodCompilerStage>
{
return new List<BaseMethodCompilerStage>
{
new CILDecoderStage(),
new CheckedConversionStage(),
new ExceptionStage(),
compilerSettings.Devirtualization ? new DevirtualizeCallStage() : null,
new PlugStage(),
new RuntimeStage(),
new IRTransformsStage(),

compilerSettings.InlineMethods || compilerSettings.InlineExplicit ? new InlineStage() : null,
new StaticLoadOptimizationStage(),

compilerSettings.BasicOptimizations ? new OptimizationStage(false) : null,
compilerSettings.SSA ? new EdgeSplitStage() : null,
compilerSettings.SSA ? new EnterSSAStage() : null,
compilerSettings.BasicOptimizations && compilerSettings.SSA ? new OptimizationStage(false) : null,
compilerSettings.ValueNumbering && compilerSettings.SSA ? new ValueNumberingStage() : null,
compilerSettings.LoopInvariantCodeMotion && compilerSettings.SSA ? new LoopInvariantCodeMotionStage() : null,
compilerSettings.SparseConditionalConstantPropagation && compilerSettings.SSA ? new SparseConditionalConstantPropagationStage() : null,
compilerSettings.BasicOptimizations && compilerSettings.SSA && (compilerSettings.ValueNumbering || compilerSettings.LoopInvariantCodeMotion || compilerSettings.SparseConditionalConstantPropagation) ? new OptimizationStage(false) : null,
compilerSettings.BitTracker ? new BitTrackerStage() : null,
compilerSettings.BasicOptimizations && compilerSettings.BitTracker ? new OptimizationStage(false) : null,
compilerSettings.BasicOptimizations && compilerSettings.LongExpansion ? new OptimizationStage(compilerSettings.LongExpansion) : null,

compilerSettings.TwoPass && compilerSettings.ValueNumbering && compilerSettings.SSA ? new ValueNumberingStage() : null,
compilerSettings.TwoPass && compilerSettings.LoopInvariantCodeMotion && compilerSettings.SSA ? new LoopInvariantCodeMotionStage() : null,
compilerSettings.TwoPass && compilerSettings.SparseConditionalConstantPropagation && compilerSettings.SSA ? new SparseConditionalConstantPropagationStage() : null,
compilerSettings.TwoPass && compilerSettings.BitTracker ? new BitTrackerStage() : null,
compilerSettings.TwoPass && compilerSettings.BasicOptimizations && compilerSettings.SSA ? new OptimizationStage(compilerSettings.LongExpansion) : null,

compilerSettings.SSA ? new ExitSSAStage() : null,
new IRCleanupStage(),
compilerSettings.InlineMethods || compilerSettings.InlineExplicit ? new InlineEvaluationStage() : null,

//new StopStage(),

new NewObjectStage(),
new CallStage(),
new CompoundStage(),
new PlatformIntrinsicStage(),

new PlatformEdgeSplitStage(),
new VirtualRegisterRenameStage(),
new GreedyRegisterAllocatorStage(),
new StackLayoutStage(),
new DeadBlockStage(),
new BlockOrderingStage(),

//new PreciseGCStage(),

new CodeGenerationStage(),
compilerSettings.EmitBinary ? new ProtectedRegionLayoutStage() : null,
};
}
new CILDecoderStage(),
new ExceptionStage(),
new IRTransformsStage(),
compilerSettings.Devirtualization ? new DevirtualizeCallStage() : null,
new PlugStage(),
new RuntimeStage(),

compilerSettings.InlineMethods || compilerSettings.InlineExplicit ? new InlineStage() : null,

compilerSettings.BasicOptimizations ? new OptimizationStage(false) : null,
compilerSettings.SSA ? new EdgeSplitStage() : null,
compilerSettings.SSA ? new EnterSSAStage() : null,
compilerSettings.BasicOptimizations && compilerSettings.SSA ? new OptimizationStage(false) : null,
compilerSettings.ValueNumbering && compilerSettings.SSA ? new ValueNumberingStage() : null,
compilerSettings.LoopInvariantCodeMotion && compilerSettings.SSA ? new LoopInvariantCodeMotionStage() : null,
compilerSettings.SparseConditionalConstantPropagation && compilerSettings.SSA ? new SparseConditionalConstantPropagationStage() : null,
compilerSettings.BasicOptimizations && compilerSettings.SSA && (compilerSettings.ValueNumbering || compilerSettings.LoopInvariantCodeMotion || compilerSettings.SparseConditionalConstantPropagation) ? new OptimizationStage(false) : null,
compilerSettings.BitTracker ? new BitTrackerStage() : null,
compilerSettings.BasicOptimizations && compilerSettings.BitTracker ? new OptimizationStage(false) : null,
compilerSettings.BasicOptimizations && compilerSettings.LongExpansion ? new OptimizationStage(compilerSettings.LongExpansion) : null,

compilerSettings.TwoPass && compilerSettings.ValueNumbering && compilerSettings.SSA ? new ValueNumberingStage() : null,
compilerSettings.TwoPass && compilerSettings.LoopInvariantCodeMotion && compilerSettings.SSA ? new LoopInvariantCodeMotionStage() : null,
compilerSettings.TwoPass && compilerSettings.SparseConditionalConstantPropagation && compilerSettings.SSA ? new SparseConditionalConstantPropagationStage() : null,
compilerSettings.TwoPass && compilerSettings.BitTracker ? new BitTrackerStage() : null,
compilerSettings.TwoPass && compilerSettings.BasicOptimizations && compilerSettings.SSA ? new OptimizationStage(compilerSettings.LongExpansion) : null,

compilerSettings.SSA ? new ExitSSAStage() : null,

new IRCleanupStage(),

compilerSettings.InlineMethods || compilerSettings.InlineExplicit ? new InlineEvaluationStage() : null,
new NewObjectStage(),
new CallStage(),
new CompoundStage(),
new PlatformIntrinsicStage(),

new PlatformEdgeSplitStage(),
new VirtualRegisterRenameStage(),
new GreedyRegisterAllocatorStage(),
new StackLayoutStage(),
new DeadBlockStage(),
new BlockOrderingStage(),

//new PreciseGCStage(),

new CodeGenerationStage(),
compilerSettings.EmitBinary ? new ProtectedRegionLayoutStage() : null,
};

#endregion Static Methods

Expand Down
2 changes: 1 addition & 1 deletion Source/Mosa.Compiler.Framework/Stages/BitTrackerStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ private void UpdateBranchInstructions()
if (node.Instruction == IRInstruction.Switch)
continue;

Debug.Assert(node.Instruction == IRInstruction.Branch32 || node.Instruction == IRInstruction.Branch64 || node.Instruction == IRInstruction.BranchObject);
Debug.Assert(node.Instruction.IsBranch);

var value1 = TransformContext.GetBitValue(node.Operand1);
var value2 = TransformContext.GetBitValue(node.Operand2);
Expand Down
2 changes: 1 addition & 1 deletion Source/Mosa.Compiler.Framework/Stages/CILDecoderStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5321,7 +5321,7 @@ private bool Switch(Context context, Stack<StackEntry> stack, MosaInstruction in
}

// REFERENCE: The last value is the fall thru - this is not implemented correctly in later stages (fixme)
context.AddBranchTarget(BasicBlocks.GetByLabel(instruction.Next.Value));
//context.AddBranchTarget(BasicBlocks.GetByLabel(instruction.Next.Value));

return true;
}
Expand Down
2 changes: 2 additions & 0 deletions Source/Mosa.Compiler.Framework/Stages/IRTransformsStage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using Mosa.Compiler.Framework.Transforms.CheckedConversion;
using Mosa.Compiler.Framework.Transforms.IR;

namespace Mosa.Compiler.Framework.Stages;
Expand All @@ -13,5 +14,6 @@ public IRTransformsStage()
: base(true, false)
{
AddTranforms(IRTransforms.List);
AddTranforms(CheckedConversionTransforms.List);
}
}
1 change: 1 addition & 0 deletions Source/Mosa.Compiler.Framework/Stages/RuntimeStage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using Mosa.Compiler.Framework.Transforms.CheckedConversion;
using Mosa.Compiler.Framework.Transforms.Runtime;

namespace Mosa.Compiler.Framework.Stages;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ protected static void CopyCompound(TransformContext transform, Context context,
context.AppendInstruction(transform.AddInstruction, srcReg, sourceBase, source);
context.AppendInstruction(transform.AddInstruction, dstReg, destinationBase, destination);

var tmp = transform.VirtualRegisters.Allocate32();
var tmpLarge = transform.Is32BitPlatform && size >= 8 ? null : transform.VirtualRegisters.Allocate64();
var tmp = transform.VirtualRegisters.AllocateNativeInteger();

for (var i = 0; i < size;)
{
Expand All @@ -38,8 +37,8 @@ protected static void CopyCompound(TransformContext transform, Context context,
if (left >= 8 & !transform.Is32BitPlatform)
{
// 64bit move
context.AppendInstruction(IRInstruction.Load64, tmpLarge, srcReg, index);
context.AppendInstruction(IRInstruction.Store64, null, dstReg, index, tmpLarge);
context.AppendInstruction(IRInstruction.Load64, tmp, srcReg, index);
context.AppendInstruction(IRInstruction.Store64, null, dstReg, index, tmp);
i += 8;
continue;
}
Expand Down
2 changes: 2 additions & 0 deletions Source/Mosa.Compiler.Framework/Transforms/IR/IRTransforms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ public static class IRTransforms
new CheckThrowIndexOutOfRange(),
new CheckThrowOverflow(),
new CheckThrowDivideByZero(),

new Switch(),
};
}
40 changes: 40 additions & 0 deletions Source/Mosa.Compiler.Framework/Transforms/IR/Switch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

namespace Mosa.Compiler.Framework.Transforms.IR;

public sealed class Switch : BaseTransform
{
public Switch() : base(IRInstruction.Switch, TransformType.Manual | TransformType.Transform)
{
}

public override bool Match(Context context, TransformContext transform)
{
return true;
}

public override void Transform(Context context, TransformContext transform)
{
var targets = context.BranchTargets;
var blocks = transform.CreateNewBlockContexts(targets.Count, context.Label);
var operand1 = context.Operand1;

var next = transform.Split(context);

context.SetInstruction(IRInstruction.Jmp, blocks[0].Block);

for (int index = 0; index < targets.Count; index++)
{
blocks[index].AppendInstruction(IRInstruction.Branch32, ConditionCode.Equal, null, operand1, Operand.CreateConstant(index), targets[index]);

if (index + 1 < targets.Count)
{
blocks[index].AppendInstruction(IRInstruction.Jmp, blocks[index + 1].Block);
}
else
{
blocks[index].AppendInstruction(IRInstruction.Jmp, next.Block);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,5 +277,8 @@ public static class ManualTransforms

new Overwrite.Move32Overwrite(),
new Overwrite.Move64Overwrite(),

new StaticLoad.Load32(),
new StaticLoad.Load64(),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using Mosa.Compiler.MosaTypeSystem;

namespace Mosa.Compiler.Framework.Transforms.StaticLoad;
namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.StaticLoad;

/// <summary>
/// Load32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using Mosa.Compiler.MosaTypeSystem;

namespace Mosa.Compiler.Framework.Transforms.StaticLoad;
namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.StaticLoad;

/// <summary>
/// Load64
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

using System.Collections.Generic;

namespace Mosa.Compiler.Framework.Transforms.StaticLoad;
namespace Mosa.Compiler.Framework.Transforms.Optimizations.Manual.StaticLoad;

/// <summary>
/// Transformations
Expand Down
2 changes: 2 additions & 0 deletions Source/Mosa.Demo.CoolWorld.x86/Mosa.Application/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public static IConsoleApp AppFactory(string name)
"shutdown" => new Shutdown(),
"reboot" => new Reboot(),
"test" => new Test(),
"test2" => new Test(),
"test3" => new Test(),
_ => null
};
}
Expand Down
1 change: 0 additions & 1 deletion Source/Mosa.Platform.x64/Transforms/IR/IRTransforms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public static class IRTransforms
//new SubOverflowOut32(),
new SubR4(),
new SubR8(),
new Switch(),
new ZeroExtend16x32(),
new ZeroExtend8x32(),
new Add64(),
Expand Down
30 changes: 0 additions & 30 deletions Source/Mosa.Platform.x64/Transforms/IR/Switch.cs

This file was deleted.

1 change: 0 additions & 1 deletion Source/Mosa.Platform.x86/Transforms/IR/IRTransforms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public static class IRTransforms
new SubOverflowOut32(),
new SubR4(),
new SubR8(),
new Switch(),
new ZeroExtend16x32(),
new ZeroExtend8x32(),
new Add64(),
Expand Down
30 changes: 0 additions & 30 deletions Source/Mosa.Platform.x86/Transforms/IR/Switch.cs

This file was deleted.

0 comments on commit afd61df

Please sign in to comment.