Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-enabled Loop Invariant Code Motion #1208

Merged
merged 24 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Source/Mosa.Compiler.Framework/Analysis/LoopDetector.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System.Collections;
using Mosa.Compiler.Framework.Common;

namespace Mosa.Compiler.Framework.Analysis;
Expand Down
27 changes: 15 additions & 12 deletions Source/Mosa.Compiler.Framework/Analysis/SimpleTraceBlockOrder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System.Collections;
using Mosa.Compiler.Framework.Common;

namespace Mosa.Compiler.Framework.Analysis;
Expand Down Expand Up @@ -39,20 +38,24 @@ public override void Analyze(BasicBlocks basicBlocks)
{
var block = workList.Pop();

if (!referenced.Contains(block))
{
referenced.Add(block);
NewBlockOrder.Add(block);
if (referenced.Contains(block))
continue;

referenced.Add(block);
NewBlockOrder.Add(block);

var nextBlocks = new List<BasicBlock>(block.NextBlocks);
nextBlocks.Sort();
if (block.NextBlocks.Count == 0)
continue;

foreach (var successor in nextBlocks)
var nextBlocks = (block.NextBlocks.Count == 2 && block.NextBlocks[0].Label < block.NextBlocks[1].Label)
? new List<BasicBlock>() { block.NextBlocks[1], block.NextBlocks[0] }
: block.NextBlocks;

foreach (var successor in nextBlocks)
{
if (!referenced.Contains(successor))
{
if (!referenced.Contains(successor))
{
workList.Push(successor);
}
workList.Push(successor);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ private void IntegerOperation1(Node node)

var operand1 = GetVariableState(node.Operand1);

if (operand1.IsOverDefined )
if (operand1.IsOverDefined)
{
UpdateToOverDefined(result);
return;
Expand Down Expand Up @@ -947,7 +947,6 @@ private static bool IntegerOperation1(BaseInstruction instruction, ulong operand
return false;
}


private void IntegerOperation2(Node node)
{
var result = GetVariableState(node.Result);
Expand Down
1 change: 0 additions & 1 deletion Source/Mosa.Compiler.Framework/BaseTransform.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System.Diagnostics;
using Mosa.Compiler.Common;
using Mosa.Compiler.Common.Exceptions;

Expand Down
1 change: 0 additions & 1 deletion Source/Mosa.Compiler.Framework/BitValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Diagnostics;
using System.Text;
using Mosa.Compiler.Common;
using Mosa.Compiler.Common.Configuration;

namespace Mosa.Compiler.Framework;

Expand Down
1 change: 0 additions & 1 deletion Source/Mosa.Compiler.Framework/CodeEmitter.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System.Diagnostics;
using Mosa.Compiler.Common;
using Mosa.Compiler.Framework.Linker;

namespace Mosa.Compiler.Framework;
Expand Down
3 changes: 2 additions & 1 deletion Source/Mosa.Compiler.Framework/Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ public sealed class Compiler
{
new CILDecoderStage(),
new ExceptionStage(),
new IRTransformsStage(),
mosaSettings.Devirtualization ? new DevirtualizeCallStage() : null,
mosaSettings.BasicOptimizations ? new OptimizationStage(false) : null,
new IRTransformsStage(),
new PlugStage(),
new RuntimeStage(),

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

using System.Reflection.Metadata;

namespace Mosa.Compiler.Framework.CompilerStages;

/// <summary>
Expand Down
21 changes: 2 additions & 19 deletions Source/Mosa.Compiler.Framework/Node.cs
Original file line number Diff line number Diff line change
Expand Up @@ -479,22 +479,6 @@ public void UpdateBranchTarget(int index, BasicBlock block)

#region Methods

/// <summary>
/// Clears this instance.
/// </summary>
private void Clear()
{
Label = -1;
Instruction = null;

ClearOperands();

ConditionCode = ConditionCode.Undefined;
Options = InstructionOption.None;
Block = null;
BranchTargets = null;
}

/// <summary>
/// Empties this node.
/// </summary>
Expand All @@ -507,8 +491,7 @@ public void Empty()
Instruction = null;
Block.RemoveBranchInstruction(this);
BranchTargets = null;

PhiBlocks?.Clear();
PhiBlocks = null;

if (AdditionalOperands != null)
{
Expand Down Expand Up @@ -1207,7 +1190,7 @@ public void SetInstruction(BaseInstruction instruction, int operandCount, int re
var label = Label;
var block = Block;

Clear();
Empty();

Instruction = instruction;
OperandCount = operandCount;
Expand Down
1 change: 0 additions & 1 deletion Source/Mosa.Compiler.Framework/OpcodeEncoder.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System.Diagnostics;
using System.Drawing;

namespace Mosa.Compiler.Framework;

Expand Down
1 change: 0 additions & 1 deletion Source/Mosa.Compiler.Framework/Stages/BitTrackerStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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

namespace Mosa.Compiler.Framework.Stages;

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

using System.Collections;
using System.Diagnostics;
using System.Text;
using Mosa.Compiler.Common;
Expand Down Expand Up @@ -135,17 +134,17 @@ private List<Node> FindLoopInvariantInstructions(Loop loop)
{
for (var node = block.AfterFirst; !node.IsBlockEndInstruction; node = node.Next)
{
if (node.IsEmpty)
if (node.IsEmptyOrNop)
continue;

// note - same code from ValueNumberingStage::CanAssignValueNumberToExpression()
// note - similar code in ValueNumberingStage::CanAssignValueNumberToExpression()
if (node.ResultCount != 1
|| node.OperandCount is 0 or > 2
|| node.Instruction.IsMemoryWrite
|| node.Instruction.IsIOOperation
|| node.Instruction.HasUnspecifiedSideEffect
|| node.Instruction.HasVariableOperands
|| node.Instruction.IsFlowNext
|| !node.Instruction.IsFlowNext
|| node.Instruction.IgnoreDuringCodeGeneration
|| node.Operand1.IsUnresolvedConstant
|| (node.OperandCount == 2 && node.Operand2.IsUnresolvedConstant)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ private bool CanAssignValueNumberToExpression(Node node)
|| node.Instruction.IsIOOperation
|| node.Instruction.HasUnspecifiedSideEffect
|| node.Instruction.HasVariableOperands
|| node.Instruction.IsFlowNext
|| !node.Instruction.IsFlowNext
|| node.Instruction.IgnoreDuringCodeGeneration
|| node.Operand1.IsUnresolvedConstant
|| (node.OperandCount == 2 && node.Operand2.IsUnresolvedConstant))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override void Transform(Context context, Transform transform)

var handler = FindNextEnclosingFinallyHandler(transform, immediate);

if (handler == null)
if (handler == null || handler.HandlerStart >= target.Label)
{
context.SetInstruction(IR.Jmp, target);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static class ExceptionTransforms
new ExceptionStart(),
new ExceptionEnd(),
new Flow(),
new TryEnd(),
new TryStart(),
new TryEnd(),
};
}
19 changes: 11 additions & 8 deletions Source/Mosa.Compiler.Framework/Transforms/Exception/FinallyEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,25 @@ public override void Transform(Context context, Transform transform)
var target = targets[i];
var conditionBlock = newBlocks[i + 1];

conditionBlock.AppendInstruction(transform.BranchInstruction, ConditionCode.Equal, null, leaveTargetRegister, Operand.CreateConstant32(target.Label), target);
conditionBlock.AppendInstruction(IR.Jmp, newBlocks[i + 2].Block);
if (next == null && i == targets.Count - 1)
{
conditionBlock.AppendInstruction(IR.Jmp, target);
}
else
{
conditionBlock.AppendInstruction(transform.BranchInstruction, ConditionCode.Equal, null, leaveTargetRegister, Operand.CreateConstant32(target.Label), target);
conditionBlock.AppendInstruction(IR.Jmp, newBlocks[i + 2].Block);
}
}
}

var finallyCallBlock = newBlocks[targetcount - 1];

if (next != null)
{
var finallyCallBlock = newBlocks[targetcount - 1];

finallyCallBlock.AppendInstruction(IR.MoveObject, transform.ExceptionRegister, Operand.NullObject);
finallyCallBlock.AppendInstruction(IR.MoveObject, transform.LeaveTargetRegister, leaveTargetRegister);
finallyCallBlock.AppendInstruction(IR.Jmp, transform.BasicBlocks.GetByLabel(next.HandlerStart));
}
else
{
// should be an unreachable block
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override void Transform(Context context, Transform transform)

var next = FindNextEnclosingFinallyHandler(transform, immediate);

if (next != null && next.HandlerEnd > immediate.HandlerEnd)
if (next != null && next.HandlerStart <= target.Label && next.HandlerEnd > immediate.HandlerEnd)
{
context.SetInstruction(IR.MoveObject, transform.LeaveTargetRegister, Operand.CreateConstant32(target.Label));
context.AppendInstruction(IR.MoveObject, transform.ExceptionRegister, Operand.NullObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected static Node GetMotionLocation(Node start, Node end, int window)

if (next.IsBlockEndInstruction
|| next.Instruction.IsMemoryWrite
|| next.Instruction.IsFlowNext
|| !next.Instruction.IsFlowNext
|| next.Instruction.HasUnspecifiedSideEffect)
return next;

Expand Down
2 changes: 1 addition & 1 deletion Source/Mosa.Kernel.BareMetal/BitMap/BitMapIndexTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Mosa.Kernel.BareMetal;

public struct BitMapIndexTable
public readonly struct BitMapIndexTable
{
private readonly Pointer Pointer;

Expand Down
2 changes: 1 addition & 1 deletion Source/Mosa.Kernel.BareMetal/IPC/ServiceQueueRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Mosa.Kernel.BareMetal.IPC;

internal static class ServiceQueueRegistry
{
public static List<MessageQueue> Queues = new List<MessageQueue>();
public static List<MessageQueue> Queues = new();

public static void Register(ServiceIdentification serviceID, MessageQueue queue)
{
Expand Down
21 changes: 21 additions & 0 deletions Source/Mosa.Kernel.BareMetal/Messaging/Message.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

namespace Mosa.Kernel.BareMetal.Messaging;

internal struct Message
{
public readonly ServiceIdentification ServiceIdentification;
public readonly object Data;
public readonly bool Direction;
public readonly uint Sequence;
public readonly Thread Thread;

public Message(ServiceIdentification serviceIdentification, object requestData, bool direction, Thread thread, uint sequence)
{
ServiceIdentification = serviceIdentification;
Data = requestData;
Direction = direction;
Thread = thread;
Sequence = sequence;
}
}
42 changes: 42 additions & 0 deletions Source/Mosa.Kernel.BareMetal/Messaging/MessageQueue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System.Collections.Generic;

namespace Mosa.Kernel.BareMetal.Messaging;

internal class MessageQueue
{
public readonly string Name;

public readonly ServiceIdentification ServiceIdentification;

public readonly Thread Thread;

private readonly Queue<Message> Queue = new();

public MessageQueue(string name, ServiceIdentification serviceIdentification, Thread thread)
{
Name = name;
ServiceIdentification = serviceIdentification;
Thread = thread;
}

public void Add(Message message)
{
lock (this)
{
Queue.Enqueue(message);
}
}

//public Message Pop()
//{
// lock (this)
// {
// if (Queue.Count == 0)
// return null;

// return Queue.Dequeue();
// }
//}
}
26 changes: 26 additions & 0 deletions Source/Mosa.Kernel.BareMetal/Messaging/MessageSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using System.Collections.Generic;

namespace Mosa.Kernel.BareMetal.Messaging;

internal static class MessageSystem
{
public static Queue<Message> Queue = new();

public static void DeliverMessage(ServiceIdentification service, bool direction, object requestData)
{
var thread = Scheduler.GetCurrentThread();

// queue the message
lock (Queue)
{
Queue.Enqueue(new Message(service, requestData, direction, thread, 0));
}
}

public static void ProcessMessages()
{
//
}
}
Loading
Loading