Skip to content

Commit

Permalink
WIP (#1054)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgiphil committed May 20, 2023
1 parent 95d7ed9 commit 07a02a9
Show file tree
Hide file tree
Showing 26 changed files with 987 additions and 533 deletions.
695 changes: 227 additions & 468 deletions Source/Data/IR-Optimizations-ConstantFolding-MemoryAccess.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Family": "IR",
"Section": "Optimizations.Auto",
"Optimizations": [
]
}
3 changes: 2 additions & 1 deletion Source/Mosa.Compiler.Framework/Operand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -969,7 +969,8 @@ public override string ToString()
sb.Append($"(v{Index}<v{Parent.Index}{(IsHigh ? "H" : "L")}>)");
}
}
else if (IsStaticField)

if (IsStaticField)
{
sb.Append($" ({Field.FullName})");
}
Expand Down
12 changes: 7 additions & 5 deletions Source/Mosa.Compiler.Framework/Stages/CILDecoderStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3537,13 +3537,13 @@ private bool Ldfld(Context context, Stack<StackEntry> stack, MosaInstruction ins
var fieldStacktype = MethodCompiler.GetPrimitiveType(fieldUnderlyingType);
var isFieldPrimitive = MosaTypeLayout.IsPrimitive(fieldUnderlyingType);

//var result = MethodCompiler.AllocateVirtualRegisterOrLocalStack(fieldStacktype, isFieldPrimitive ? fieldUnderlyingType : fieldType);

var stackEntry = CreateStackEntry(fieldType); // new StackEntry(fieldStacktype, result, result.Type);
var stackEntry = CreateStackEntry(fieldType);
var result = stackEntry.Operand;

PushStack(stack, stackEntry);

MethodScanner.AccessedField(field);

var operand = entry.Operand;

if (entry.PrimitiveType == PrimitiveType.ValueType)
Expand Down Expand Up @@ -3658,8 +3658,6 @@ private bool Ldflda(Context context, Stack<StackEntry> stack, MosaInstruction in
MethodScanner.AccessedField(field);

var offset = TypeLayout.GetFieldOffset(field);
//var fieldPtr = field.FieldType.ToManagedPointer(); // FUTURE: MethodCompiler.VirtualRegisters.AllocateManagedPointer();

var result = MethodCompiler.VirtualRegisters.AllocateManagedPointer();

if (offset == 0)
Expand Down Expand Up @@ -3844,6 +3842,8 @@ private bool Ldsfld(Context context, Stack<StackEntry> stack, MosaInstruction in
: PrimitiveType.ManagedPointer,
field);

MethodScanner.AccessedField(field);

if (isPrimitive)
{
var elementType = MethodCompiler.GetElementType(underlyingType);
Expand Down Expand Up @@ -4867,6 +4867,8 @@ private bool Stsfld(Context context, Stack<StackEntry> stack, MosaInstruction in
: PrimitiveType.ManagedPointer,
field);

MethodScanner.AccessedField(field);

if (isPrimitive)
{
var elementType = MethodCompiler.GetElementType(underlyingType);
Expand Down
22 changes: 17 additions & 5 deletions Source/Mosa.Compiler.Framework/Stages/PromoteTemporaryVariables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ protected bool CanPromote(Operand operand)
}
continue;
}
else if (node.Instruction == IRInstruction.Load32 || node.Instruction == IRInstruction.Load64)
else if (node.Instruction == IRInstruction.Load32 || node.Instruction == IRInstruction.Load64 || node.Instruction == IRInstruction.LoadManagedPointer)
{
Debug.Assert(node.Operand2 == operand);
continue;
}
else if (node.Instruction == IRInstruction.Store32 || node.Instruction == IRInstruction.Store64)
else if (node.Instruction == IRInstruction.Store32 || node.Instruction == IRInstruction.Store64 || node.Instruction == IRInstruction.StoreManagedPointer)
{
Debug.Assert(node.Operand2 == operand);
continue;
Expand All @@ -88,7 +88,7 @@ protected bool CanPromote(Operand operand)

private bool Check(InstructionNode node)
{
if (node.Instruction == IRInstruction.Store32 || node.Instruction == IRInstruction.Store64)
if (node.Instruction == IRInstruction.Store32 || node.Instruction == IRInstruction.Store64 || node.Instruction == IRInstruction.StoreManagedPointer)
{
// check offset
return true;
Expand All @@ -107,7 +107,7 @@ private bool Check(InstructionNode node)
trace?.Log($"B-No: {node}");
return false;
}
else if (node.Instruction == IRInstruction.Move32 || node.Instruction == IRInstruction.Move64)
else if (node.Instruction == IRInstruction.Move32 || node.Instruction == IRInstruction.Move64 || node.Instruction == IRInstruction.MoveManagedPointer)
{
foreach (var node2 in node.Result.Uses)
{
Expand All @@ -121,7 +121,7 @@ private bool Check(InstructionNode node)

return true;
}
else if (node.Instruction == IRInstruction.Load32 || node.Instruction == IRInstruction.Load64)
else if (node.Instruction == IRInstruction.Load32 || node.Instruction == IRInstruction.Load64 || node.Instruction == IRInstruction.LoadManagedPointer)
{
// check offset
return true;
Expand Down Expand Up @@ -207,6 +207,12 @@ private void Promote(InstructionNode node, Operand virtualRegister)
node.SetInstruction(IRInstruction.Move64, virtualRegister, node.Operand3);
trace?.Log($"AFTER: \t{node}");
}
else if (node.Instruction == IRInstruction.StoreManagedPointer)
{
trace?.Log($"BEFORE:\t{node}");
node.SetInstruction(IRInstruction.MoveManagedPointer, virtualRegister, node.Operand3);
trace?.Log($"AFTER: \t{node}");
}
else if (node.Instruction == IRInstruction.MemorySet)
{
if (node.Operand3.ConstantUnsigned64 == 4)
Expand Down Expand Up @@ -234,5 +240,11 @@ private void Promote(InstructionNode node, Operand virtualRegister)
node.SetInstruction(IRInstruction.Move64, node.Result, virtualRegister);
trace?.Log($"AFTER: \t{node}");
}
else if (node.Instruction == IRInstruction.LoadManagedPointer)
{
trace?.Log($"BEFORE:\t{node}");
node.SetInstruction(IRInstruction.MoveManagedPointer, node.Result, virtualRegister);
trace?.Log($"AFTER: \t{node}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -629,87 +629,104 @@ public static class AutoTransforms
new ConstantFolding.ShiftRight32x2(),
new ConstantFolding.ShiftRight64x2(),
new ConstantFolding.Load32FoldAdd32(),
new ConstantFolding.Load64FoldAdd32(),
new ConstantFolding.LoadR4FoldAdd32(),
new ConstantFolding.LoadR8FoldAdd32(),
new ConstantFolding.LoadSignExtend8x32FoldAdd32(),
new ConstantFolding.LoadSignExtend16x32FoldAdd32(),
new ConstantFolding.LoadSignExtend8x64FoldAdd32(),
new ConstantFolding.LoadSignExtend16x64FoldAdd32(),
new ConstantFolding.LoadSignExtend32x64FoldAdd32(),
new ConstantFolding.LoadZeroExtend8x32FoldAdd32(),
new ConstantFolding.LoadZeroExtend16x32FoldAdd32(),
new ConstantFolding.LoadZeroExtend8x64FoldAdd32(),
new ConstantFolding.LoadZeroExtend16x64FoldAdd32(),
new ConstantFolding.LoadZeroExtend32x64FoldAdd32(),
new ConstantFolding.Load32FoldAdd64(),
new ConstantFolding.Load64FoldAdd32(),
new ConstantFolding.Load64FoldAdd64(),
new ConstantFolding.LoadManagedPointerFoldAdd32(),
new ConstantFolding.LoadManagedPointerFoldAdd64(),
new ConstantFolding.LoadR4FoldAdd32(),
new ConstantFolding.LoadR4FoldAdd64(),
new ConstantFolding.LoadR8FoldAdd32(),
new ConstantFolding.LoadR8FoldAdd64(),
new ConstantFolding.LoadSignExtend8x32FoldAdd32(),
new ConstantFolding.LoadSignExtend8x32FoldAdd64(),
new ConstantFolding.LoadSignExtend16x32FoldAdd32(),
new ConstantFolding.LoadSignExtend16x32FoldAdd64(),
new ConstantFolding.LoadSignExtend8x64FoldAdd32(),
new ConstantFolding.LoadSignExtend8x64FoldAdd64(),
new ConstantFolding.LoadSignExtend16x64FoldAdd32(),
new ConstantFolding.LoadSignExtend16x64FoldAdd64(),
new ConstantFolding.LoadSignExtend32x64FoldAdd32(),
new ConstantFolding.LoadSignExtend32x64FoldAdd64(),
new ConstantFolding.LoadZeroExtend8x32FoldAdd32(),
new ConstantFolding.LoadZeroExtend8x32FoldAdd64(),
new ConstantFolding.LoadZeroExtend16x32FoldAdd32(),
new ConstantFolding.LoadZeroExtend16x32FoldAdd64(),
new ConstantFolding.LoadZeroExtend8x64FoldAdd32(),
new ConstantFolding.LoadZeroExtend8x64FoldAdd64(),
new ConstantFolding.LoadZeroExtend16x64FoldAdd32(),
new ConstantFolding.LoadZeroExtend16x64FoldAdd64(),
new ConstantFolding.LoadZeroExtend32x64FoldAdd32(),
new ConstantFolding.LoadZeroExtend32x64FoldAdd64(),
new ConstantFolding.Load32FoldSub32(),
new ConstantFolding.Load64FoldSub32(),
new ConstantFolding.LoadR4FoldSub32(),
new ConstantFolding.LoadR8FoldSub32(),
new ConstantFolding.LoadSignExtend8x32FoldSub32(),
new ConstantFolding.LoadSignExtend16x32FoldSub32(),
new ConstantFolding.LoadSignExtend8x64FoldSub32(),
new ConstantFolding.LoadSignExtend16x64FoldSub32(),
new ConstantFolding.LoadSignExtend32x64FoldSub32(),
new ConstantFolding.LoadZeroExtend8x32FoldSub32(),
new ConstantFolding.LoadZeroExtend16x32FoldSub32(),
new ConstantFolding.LoadZeroExtend8x64FoldSub32(),
new ConstantFolding.LoadZeroExtend16x64FoldSub32(),
new ConstantFolding.LoadZeroExtend32x64FoldSub32(),
new ConstantFolding.Load32FoldSub64(),
new ConstantFolding.Load64FoldSub32(),
new ConstantFolding.Load64FoldSub64(),
new ConstantFolding.LoadManagedPointerFoldSub32(),
new ConstantFolding.LoadManagedPointerFoldSub64(),
new ConstantFolding.LoadR4FoldSub32(),
new ConstantFolding.LoadR4FoldSub64(),
new ConstantFolding.LoadR8FoldSub32(),
new ConstantFolding.LoadR8FoldSub64(),
new ConstantFolding.LoadSignExtend8x32FoldSub32(),
new ConstantFolding.LoadSignExtend8x32FoldSub64(),
new ConstantFolding.LoadSignExtend16x32FoldSub32(),
new ConstantFolding.LoadSignExtend16x32FoldSub64(),
new ConstantFolding.LoadZeroExtend16x32FoldSub32(),
new ConstantFolding.LoadZeroExtend16x32FoldSub64(),
new ConstantFolding.LoadZeroExtend8x32FoldSub32(),
new ConstantFolding.LoadZeroExtend8x32FoldSub64(),
new ConstantFolding.LoadSignExtend8x64FoldSub32(),
new ConstantFolding.LoadSignExtend8x64FoldSub64(),
new ConstantFolding.LoadSignExtend16x64FoldSub32(),
new ConstantFolding.LoadSignExtend16x64FoldSub64(),
new ConstantFolding.LoadSignExtend32x64FoldSub32(),
new ConstantFolding.LoadSignExtend32x64FoldSub64(),
new ConstantFolding.LoadZeroExtend8x32FoldSub64(),
new ConstantFolding.LoadZeroExtend16x32FoldSub64(),
new ConstantFolding.LoadZeroExtend8x64FoldSub32(),
new ConstantFolding.LoadZeroExtend8x64FoldSub64(),
new ConstantFolding.LoadZeroExtend16x64FoldSub32(),
new ConstantFolding.LoadZeroExtend16x64FoldSub64(),
new ConstantFolding.LoadZeroExtend32x64FoldSub32(),
new ConstantFolding.LoadZeroExtend32x64FoldSub64(),
new ConstantFolding.Store8FoldAdd32(),
new ConstantFolding.Store8FoldAdd64(),
new ConstantFolding.Store8FoldSub32(),
new ConstantFolding.Store8FoldSub64(),
new ConstantFolding.Store16FoldAdd32(),
new ConstantFolding.Store16FoldAdd64(),
new ConstantFolding.Store16FoldSub32(),
new ConstantFolding.Store16FoldSub64(),
new ConstantFolding.Store32FoldAdd32(),
new ConstantFolding.Store32FoldAdd64(),
new ConstantFolding.Store32FoldSub32(),
new ConstantFolding.Store32FoldSub64(),
new ConstantFolding.Store64FoldAdd32(),
new ConstantFolding.Store64FoldAdd64(),
new ConstantFolding.Store64FoldSub32(),
new ConstantFolding.Store64FoldSub64(),
new ConstantFolding.StoreManagedPointerFoldAdd32(),
new ConstantFolding.StoreManagedPointerFoldAdd64(),
new ConstantFolding.StoreR4FoldAdd32(),
new ConstantFolding.StoreR4FoldAdd64(),
new ConstantFolding.StoreR4FoldSub32(),
new ConstantFolding.StoreR4FoldSub64(),
new ConstantFolding.StoreR8FoldAdd32(),
new ConstantFolding.StoreR8FoldAdd64(),
new ConstantFolding.Store8FoldAdd32(),
new ConstantFolding.Store8FoldAdd64(),
new ConstantFolding.Store8FoldAdd32(),
new ConstantFolding.Store8FoldAdd64(),
new ConstantFolding.Store8FoldAdd32(),
new ConstantFolding.Store8FoldAdd64(),
new ConstantFolding.Store8FoldAdd32(),
new ConstantFolding.Store8FoldAdd64(),
new ConstantFolding.Store8FoldSub32(),
new ConstantFolding.Store8FoldSub64(),
new ConstantFolding.Store16FoldSub32(),
new ConstantFolding.Store16FoldSub64(),
new ConstantFolding.Store32FoldSub32(),
new ConstantFolding.Store32FoldSub64(),
new ConstantFolding.Store64FoldSub32(),
new ConstantFolding.Store64FoldSub64(),
new ConstantFolding.StoreManagedPointerFoldSub32(),
new ConstantFolding.StoreManagedPointerFoldSub64(),
new ConstantFolding.StoreR4FoldSub32(),
new ConstantFolding.StoreR4FoldSub64(),
new ConstantFolding.StoreR8FoldSub32(),
new ConstantFolding.StoreR8FoldSub64(),
new ConstantFolding.Load32AddressFold(),
new ConstantFolding.Load64AddressFold(),
new ConstantFolding.LoadParamManagedPointerAddressFold(),
new ConstantFolding.LoadR4AddressFold(),
new ConstantFolding.LoadR8AddressFold(),
new ConstantFolding.LoadSignExtend8x32AddressFold(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Mosa.Compiler.Framework.Transforms.Optimizations.Auto.ConstantFolding;
[Transform("IR.Optimizations.Auto.ConstantFolding")]
public sealed class Load64FoldAdd64 : BaseTransform
{
public Load64FoldAdd64() : base(IRInstruction.Load32, TransformType.Auto | TransformType.Optimization)
public Load64FoldAdd64() : base(IRInstruction.Load64, TransformType.Auto | TransformType.Optimization)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Mosa.Compiler.Framework.Transforms.Optimizations.Auto.ConstantFolding;
[Transform("IR.Optimizations.Auto.ConstantFolding")]
public sealed class Load64FoldSub32 : BaseTransform
{
public Load64FoldSub32() : base(IRInstruction.Load32, TransformType.Auto | TransformType.Optimization)
public Load64FoldSub32() : base(IRInstruction.Load64, TransformType.Auto | TransformType.Optimization)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Mosa.Compiler.Framework.Transforms.Optimizations.Auto.ConstantFolding;
[Transform("IR.Optimizations.Auto.ConstantFolding")]
public sealed class Load64FoldSub64 : BaseTransform
{
public Load64FoldSub64() : base(IRInstruction.Load32, TransformType.Auto | TransformType.Optimization)
public Load64FoldSub64() : base(IRInstruction.Load64, TransformType.Auto | TransformType.Optimization)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

// This code was generated by an automated template.

using Mosa.Compiler.Framework;

namespace Mosa.Compiler.Framework.Transforms.Optimizations.Auto.ConstantFolding;

/// <summary>
/// LoadManagedPointerFoldAdd32
/// </summary>
[Transform("IR.Optimizations.Auto.ConstantFolding")]
public sealed class LoadManagedPointerFoldAdd32 : BaseTransform
{
public LoadManagedPointerFoldAdd32() : base(IRInstruction.LoadManagedPointer, TransformType.Auto | TransformType.Optimization)
{
}

public override bool Match(Context context, TransformContext transform)
{
if (!context.Operand1.IsVirtualRegister)
return false;

if (context.Operand1.Definitions.Count != 1)
return false;

if (context.Operand1.Definitions[0].Instruction != IRInstruction.Add32)
return false;

if (!IsResolvedConstant(context.Operand2))
return false;

if (!IsResolvedConstant(context.Operand1.Definitions[0].Operand2))
return false;

return true;
}

public override void Transform(Context context, TransformContext transform)
{
var result = context.Result;

var t1 = context.Operand1.Definitions[0].Operand1;
var t2 = context.Operand1.Definitions[0].Operand2;
var t3 = context.Operand2;

var e1 = Operand.CreateConstant(Add32(To32(t2), To32(t3)));

context.SetInstruction(IRInstruction.LoadManagedPointer, result, t1, e1);
}
}

0 comments on commit 07a02a9

Please sign in to comment.