Skip to content

Commit

Permalink
X86 Optimizations (#869)
Browse files Browse the repository at this point in the history
* - Slight optimization that merges more empty block

* - New x86 optimizations (add 1 -> inc, sub 1 -> dec)
- Added SetNop() method on Context and Node
  • Loading branch information
tgiphil authored Sep 8, 2021
1 parent 906f878 commit daafbb1
Show file tree
Hide file tree
Showing 51 changed files with 180 additions and 78 deletions.
4 changes: 2 additions & 2 deletions Source/Data/X86-Instructions.json
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@
"FlagsUnchanged": "C",
"FlagsUndefined": "",
"FlagsUsed": "",
"OperandCount": 2,
"OperandCount": 1,
"ResultCount": 1,
"ThreeTwoAddressConversion": "true",
"OpcodeEncoding": [
Expand Down Expand Up @@ -792,7 +792,7 @@
"FlagsUnchanged": "C",
"FlagsUndefined": "",
"FlagsUsed": "",
"OperandCount": 2,
"OperandCount": 1,
"ResultCount": 1,
"ThreeTwoAddressConversion": "false",
"OpcodeEncoding": [
Expand Down
2 changes: 1 addition & 1 deletion Source/Mosa.Compiler.Framework/BaseMethodCompilerStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ protected static bool EmptyBlockOfAllInstructions(BasicBlock block, bool useNop
continue;
}

node.SetInstruction(IRInstruction.Nop);
node.SetNop();

found = true;
}
Expand Down
5 changes: 5 additions & 0 deletions Source/Mosa.Compiler.Framework/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ public Context(BasicBlock block)

#region Methods

public void SetNop()
{
Node.SetNop();
}

/// <summary>
/// Clones this instance.
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions Source/Mosa.Compiler.Framework/InstructionNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,11 @@ public InstructionNode(BaseInstruction instruction, Operand result, Operand oper

#region SetInstructions

public void SetNop()
{
SetInstruction(IRInstruction.Nop);
}

/// <summary>
/// Sets the instruction.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions Source/Mosa.Compiler.Framework/Stages/BitTrackerStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ private void UpdateInstruction(Operand virtualRegister, Value value)
Debug.Assert(virtualRegister.Uses.Count == 0);

trace?.Log($"REMOVED:\t{node}");
node.SetInstruction(IRInstruction.Nop);
node.SetNop();
trace?.Log();

InstructionsRemovedCount++;
Expand Down Expand Up @@ -595,7 +595,7 @@ private void UpdateBranchInstructions()
var newBranch = node.BranchTargets[0];

trace?.Log($"REMOVED:\t{node}");
node.SetInstruction(IRInstruction.Nop);
node.SetNop();
InstructionsRemovedCount++;
BranchesRemovedCount++;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,7 @@ private void Newobj(Context context)
/// <param name="node">The context.</param>
private void Nop(InstructionNode node)
{
node.SetInstruction(IRInstruction.Nop);
node.SetNop();
}

/// <summary>
Expand Down Expand Up @@ -1280,7 +1280,7 @@ private void Pop(InstructionNode node)

private void PreReadOnly(Context context)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ protected void ReplaceVirtualRegisterWithConstant(Operand target, ulong value)
var defNode = target.Definitions[0];

trace?.Log($"REMOVED:\t{defNode}");
defNode.SetInstruction(IRInstruction.Nop);
defNode.SetNop();
InstructionsRemovedCount++;
}

Expand Down Expand Up @@ -159,7 +159,7 @@ protected void RemoveBranchesToDeadBlocks(BasicBlock deadBlock)
{
trace?.Log("*** RemoveBranchesToDeadBlocks");
trace?.Log($"REMOVED:\t{node}");
node.SetInstruction(IRInstruction.Nop);
node.SetNop();
InstructionsRemovedCount++;
continue;
}
Expand Down
8 changes: 4 additions & 4 deletions Source/Mosa.Compiler.Framework/Stages/ValueNumberingStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private void ValueNumber(BasicBlock block, out List<BasicBlock> nextblocks, out

trace?.Log($"Removed Unless PHI: {node}");

node.SetInstruction(IRInstruction.Nop);
node.SetNop();
InstructionRemovalCount++;
continue;
}
Expand All @@ -261,7 +261,7 @@ private void ValueNumber(BasicBlock block, out List<BasicBlock> nextblocks, out

trace?.Log($"Removed Redundant PHI: {node}");

node.SetInstruction(IRInstruction.Nop);
node.SetNop();
InstructionRemovalCount++;
continue;
}
Expand Down Expand Up @@ -297,7 +297,7 @@ private void ValueNumber(BasicBlock block, out List<BasicBlock> nextblocks, out
}

SetValueNumber(node.Result, node.Operand1);
node.SetInstruction(IRInstruction.Nop);
node.SetNop();
InstructionRemovalCount++;
continue;
}
Expand Down Expand Up @@ -332,7 +332,7 @@ private void ValueNumber(BasicBlock block, out List<BasicBlock> nextblocks, out
if (node.Instruction.IsParameterLoad)
ParameterLoadEliminationCount++;

node.SetInstruction(IRInstruction.Nop);
node.SetNop();
InstructionRemovalCount++;
SubexpressionEliminationCount++;
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public override void Transform(Context context, TransformContext transformContex

if (!compare)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
else
{
Expand All @@ -68,7 +68,7 @@ public override void Transform(Context context, TransformContext transformContex
{
if (!context.IsEmptyOrNop)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
context.GotoNext();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override void Transform(Context context, TransformContext transformContex

if (!compare)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
else
{
Expand All @@ -67,7 +67,7 @@ public override void Transform(Context context, TransformContext transformContex
{
if (!context.IsEmptyOrNop)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
context.GotoNext();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public override void Transform(Context context, TransformContext transformContex

if (!compare)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
else
{
Expand All @@ -67,7 +67,7 @@ public override void Transform(Context context, TransformContext transformContex
{
if (!context.IsEmptyOrNop)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
context.GotoNext();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override bool Match(Context context, TransformContext transformContext)

public override void Transform(Context context, TransformContext transformContext)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override bool Match(Context context, TransformContext transformContext)

public override void Transform(Context context, TransformContext transformContext)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override bool Match(Context context, TransformContext transformContext)

public override void Transform(Context context, TransformContext transformContext)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override bool Match(Context context, TransformContext transformContext)

public override void Transform(Context context, TransformContext transformContext)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override bool Match(Context context, TransformContext transformContext)

public override void Transform(Context context, TransformContext transformContext)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override bool Match(Context context, TransformContext transformContext)

public override void Transform(Context context, TransformContext transformContext)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override bool Match(Context context, TransformContext transformContext)

public override void Transform(Context context, TransformContext transformContext)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public override bool Match(Context context, TransformContext transformContext)

public override void Transform(Context context, TransformContext transformContext)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override void Transform(Context context, TransformContext transformContex
var target = context.BranchTargets[0];
var block = context.Block;

context.SetInstruction(IRInstruction.Nop);
context.SetNop();

TransformContext.RemoveBlockFromPHIInstructions(block, target);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override void Transform(Context context, TransformContext transformContex
var target = context.BranchTargets[0];
var block = context.Block;

context.SetInstruction(IRInstruction.Nop);
context.SetNop();

TransformContext.RemoveBlockFromPHIInstructions(block, target);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override bool Match(Context context, TransformContext transformContext)

public override void Transform(Context context, TransformContext transformContext)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();

//context.SetInstruction(IRInstruction.Branch64, context.ConditionCode.GetReverse(), context.Result, context.Operand2, context.Operand1, context.BranchTargets[0]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public override bool Match(Context context, TransformContext transformContext)

public override void Transform(Context context, TransformContext transformContext)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void Transform(Context context, TransformContext transformContex
}
}

context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void Transform(Context context, TransformContext transformContex
}
}

context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void Transform(Context context, TransformContext transformContex
}
}

context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void Transform(Context context, TransformContext transformContex
}
}

context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void Transform(Context context, TransformContext transformContex
}
}

context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void Transform(Context context, TransformContext transformContex
}
}

context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void Transform(Context context, TransformContext transformContex
}
}

context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void Transform(Context context, TransformContext transformContex
}
}

context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override void Transform(Context context, TransformContext transformContex
}
}

context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public override bool Match(Context context, TransformContext transformContext)

public override void Transform(Context context, TransformContext transformContext)
{
context.SetInstruction(IRInstruction.Nop);
context.SetNop();
}
}
}
Loading

0 comments on commit daafbb1

Please sign in to comment.