Skip to content

Commit

Permalink
Merge pull request #510 from tgiphil/loop-optimization
Browse files Browse the repository at this point in the history
Implement Loop Invariant Code Motion Optimization
  • Loading branch information
tgiphil committed Oct 21, 2018
2 parents 20d4ffa + 06f9ab1 commit 32b5f56
Show file tree
Hide file tree
Showing 16 changed files with 626 additions and 464 deletions.
16 changes: 12 additions & 4 deletions Source/Mosa.Compiler.Framework/BaseMethodCompilerStage.cs
Expand Up @@ -172,7 +172,7 @@ public abstract class BaseMethodCompilerStage : ITraceFactory
/// <summary>
/// The counters
/// </summary>
private List<Counter> Counters = new List<Counter>();
private readonly List<Counter> Counters = new List<Counter>();

#endregion Method Properties

Expand Down Expand Up @@ -722,6 +722,8 @@ protected void NewCompilerTraceEvent(CompilerEvent compileEvent, string message)

#endregion Trace Helper Methods

#region Helper Methods

/// <summary>
/// Updates the counter.
/// </summary>
Expand All @@ -731,8 +733,6 @@ public void UpdateCounter(Counter counter)
MethodData.Counters.UpdateNoLock(counter.Name, counter.Count);
}

#region Helpers

/// <summary>
/// Gets the size of the type.
/// </summary>
Expand Down Expand Up @@ -980,7 +980,15 @@ public BaseInstruction Select(bool is64bit, BaseInstruction instruction32, BaseI
return !is64bit ? instruction32 : instruction64;
}

#endregion Helpers
public static bool IsSimpleIRMoveInstruction(BaseInstruction instruction)
{
return (instruction == IRInstruction.MoveInt32
|| instruction == IRInstruction.MoveInt64
|| instruction == IRInstruction.MoveFloatR8
|| instruction == IRInstruction.MoveFloatR4);
}

#endregion Helper Methods

#region Constant Helper Methods

Expand Down
1 change: 1 addition & 0 deletions Source/Mosa.Compiler.Framework/BasicBlocks.cs
Expand Up @@ -133,6 +133,7 @@ public BasicBlock EpilogueBlock
/// Creates the block.
/// </summary>
/// <param name="blockLabel">The label.</param>
/// <param name="instructionLabel">The instruction label.</param>
/// <returns></returns>
public BasicBlock CreateBlock(int blockLabel = -1, int instructionLabel = -1)
{
Expand Down
6 changes: 2 additions & 4 deletions Source/Mosa.Compiler.Framework/Compiler.cs
Expand Up @@ -166,13 +166,11 @@ private static List<BaseMethodCompilerStage> GetDefaultMethodPipeline(CompilerOp

(compilerOptions.EnableSSA) ? new EnterSSAStage() : null,
(compilerOptions.EnableValueNumbering && compilerOptions.EnableSSA) ? new ValueNumberingStage() : null,

(compilerOptions.EnableLoopInvariantCodeMotion) ? new LoopInvariantCodeMotionStage() : null,
(compilerOptions.EnableSparseConditionalConstantPropagation && compilerOptions.EnableSSA) ? new SparseConditionalConstantPropagationStage() : null,
(compilerOptions.EnableIROptimizations) ? new IROptimizationStage() : null,

//new GraphVizStage(),
//new DominanceOutputStage(),
//(compilerOptions.EnableLoopInvariantCodeMotion) ? new LoopInvariantCodeMotionStage() : null,

(compilerOptions.IRLongExpansion && compilerOptions.Architecture.NativePointerSize == 4) ? new IRLongDecompositionStage() : null,
new LowerIRStage(),
(compilerOptions.TwoPassOptimizations && compilerOptions.EnableValueNumbering && compilerOptions.EnableSSA) ? new ValueNumberingStage() : null,
Expand Down
38 changes: 22 additions & 16 deletions Source/Mosa.Compiler.Framework/Context.cs
Expand Up @@ -126,7 +126,7 @@ public sealed class Context
/// Gets or sets number of operand results.
/// </summary>
/// <value>The number of operand results.</value>
public byte ResultCount { get { return Node.ResultCount; } set { Node.ResultCount = value; } }
public int ResultCount { get { return Node.ResultCount; } set { Node.ResultCount = value; } }

/// <summary>
/// Gets a value indicating whether this instance is empty.
Expand Down Expand Up @@ -517,7 +517,7 @@ public void SetInstruction(BaseInstruction instruction, Operand result, Operand
/// <param name="instruction">The instruction.</param>
/// <param name="result">The result.</param>
/// <param name="operands">The operands.</param>
public void SetInstruction(BaseInstruction instruction, Operand result, IList<Operand> operands)
public void SetInstruction(BaseInstruction instruction, Operand result, List<Operand> operands)
{
Node.SetInstruction(instruction, result, operands);
}
Expand All @@ -541,7 +541,7 @@ public void SetInstruction(BaseInstruction instruction, bool updateStatus, Opera
/// <param name="result">The result.</param>
/// <param name="operand1">The operand1.</param>
/// <param name="operands">The operands.</param>
public void SetInstruction(BaseInstruction instruction, Operand result, Operand operand1, IList<Operand> operands)
public void SetInstruction(BaseInstruction instruction, Operand result, Operand operand1, List<Operand> operands)
{
Node.SetInstruction(instruction, result, operand1, operands);
}
Expand Down Expand Up @@ -714,6 +714,15 @@ public void SetInstruction(BaseInstruction instruction, Operand result, Operand
Node.SetInstruction(instruction, result, operand1, operand2, operand3, operand4);
}

/// <summary>
/// Sets the instruction.
/// </summary>
/// <param name="instruction">The instruction.</param>
public void SetInstruction(SimpleInstruction instruction)
{
Node.SetInstruction(instruction);
}

#endregion Set Instruction Methods

#region Append Instruction Methods
Expand Down Expand Up @@ -833,19 +842,6 @@ public void AppendInstruction(BaseInstruction instruction, Operand result, Opera
Node.SetInstruction(instruction, result, operand1);
}

/// <summary>
/// Appends the instruction.
/// </summary>
/// <param name="instruction">The instruction.</param>
/// <param name="size">The size.</param>
/// <param name="result">The result.</param>
/// <param name="operand1">The operand1.</param>
public void AppendInstruction(BaseInstruction instruction, InstructionSize size, Operand result, Operand operand1)
{
AppendInstruction();
Node.SetInstruction(instruction, result, operand1);
}

/// <summary>
/// Appends the instruction.
/// </summary>
Expand Down Expand Up @@ -1091,6 +1087,16 @@ public void AppendInstruction(BaseInstruction instruction, Operand result, Opera
Node.SetInstruction(instruction, result, operand1, operand2, operand3, operand4);
}

/// <summary>
/// Appends the instruction.
/// </summary>
/// <param name="instruction">The instruction.</param>
public void AppendInstruction(SimpleInstruction instruction)
{
AppendInstruction();
Node.SetInstruction(instruction);
}

#endregion Append Instruction Methods
}
}
6 changes: 6 additions & 0 deletions Source/Mosa.Compiler.Framework/Counter.cs
Expand Up @@ -37,5 +37,11 @@ public void Set(bool condition, int truevalue = 1, int falsevalue = 0)
counter.Count++;
return counter;
}

public static Counter operator +(Counter counter, int increment)
{
counter.Count += increment;
return counter;
}
}
}

0 comments on commit 32b5f56

Please sign in to comment.