Skip to content
Permalink
Browse files

Major bug fixes (#491)

Value Numbering + Major bug fixes
  • Loading branch information...
tgiphil committed Aug 16, 2018
1 parent 045a46a commit d5b45ee6bb1e52d86f7756c956314360a34e4353
Showing with 853 additions and 621 deletions.
  1. +1 −0 .gitignore
  2. +1 −1 Demos/Run-HelloWorld.bat
  3. +1 −1 Demos/Run-TestWorld.bat
  4. +0 −16 Source/MOSA.sln
  5. +5 −4 Source/Mosa.Compiler.Common/GlobalSuppressions.cs
  6. +57 −50 Source/Mosa.Compiler.Framework/Analysis/SimpleFastDominance.cs
  7. +2 −1 Source/Mosa.Compiler.Framework/BaseCodeTransformationStage.cs
  8. +1 −1 Source/Mosa.Compiler.Framework/BaseCodeTransformationStageLegacy.cs
  9. +1 −9 Source/Mosa.Compiler.Framework/BaseCompilerStage.cs
  10. +22 −18 Source/Mosa.Compiler.Framework/BaseMethodCompilerStage.cs
  11. +5 −0 Source/Mosa.Compiler.Framework/BasicBlock.cs
  12. +0 −1 Source/Mosa.Compiler.Framework/CIL/IRInstructions.cs
  13. +18 −12 Source/Mosa.Compiler.Framework/Compiler.cs
  14. +0 −9 Source/Mosa.Compiler.Framework/CompilerOptions.cs
  15. +10 −0 Source/Mosa.Compiler.Framework/CompilerStages/LinkerFinalizationStage.cs
  16. +1 −1 Source/Mosa.Compiler.Framework/CompilerVersion.cs
  17. +15 −5 Source/Mosa.Compiler.Framework/Counters.cs
  18. +1 −1 Source/Mosa.Compiler.Framework/IR/CallDynamic.cs
  19. +0 −1 Source/Mosa.Compiler.Framework/IR/IRInstructionList.cs
  20. +24 −2 Source/Mosa.Compiler.Framework/InstructionNode.cs
  21. +1 −0 Source/Mosa.Compiler.Framework/Mosa.Compiler.Framework.csproj
  22. +50 −27 Source/Mosa.Compiler.Framework/RegisterAllocator/BaseRegisterAllocator.cs
  23. +2 −1 Source/Mosa.Compiler.Framework/RegisterAllocator/ExtendedBlock.cs
  24. +3 −4 Source/Mosa.Compiler.Framework/RegisterAllocator/GreedyRegisterAllocator.cs
  25. +30 −1 Source/Mosa.Compiler.Framework/RegisterAllocator/LiveInterval.cs
  26. +1 −1 Source/Mosa.Compiler.Framework/RegisterAllocator/LiveIntervalTrack.cs
  27. +4 −0 Source/Mosa.Compiler.Framework/RegisterAllocator/LiveRange.cs
  28. +1 −1 Source/Mosa.Compiler.Framework/Stages/BlockMergeStage.cs
  29. +2 −2 Source/Mosa.Compiler.Framework/Stages/CILDecodingStage.cs
  30. +1 −1 Source/Mosa.Compiler.Framework/Stages/CILProtectedRegionStage.cs
  31. +2 −2 Source/Mosa.Compiler.Framework/Stages/CILTransformationStage.cs
  32. +0 −1 Source/Mosa.Compiler.Framework/Stages/DeadBlockStage.cs
  33. +118 −0 Source/Mosa.Compiler.Framework/Stages/DominanceOutputStage.cs
  34. +1 −1 Source/Mosa.Compiler.Framework/Stages/EdgeSplitStage.cs
  35. +116 −27 Source/Mosa.Compiler.Framework/Stages/EnterSSAStage.cs
  36. +1 −1 Source/Mosa.Compiler.Framework/Stages/GraphVizStage.cs
  37. +2 −17 Source/Mosa.Compiler.Framework/Stages/IRCleanupStage.cs
  38. +0 −3 Source/Mosa.Compiler.Framework/Stages/IRLongDecomposeStage.cs
  39. +14 −14 Source/Mosa.Compiler.Framework/Stages/IROptimizationStage.cs
  40. +2 −2 Source/Mosa.Compiler.Framework/Stages/InlineEvaluationStage.cs
  41. +2 −2 Source/Mosa.Compiler.Framework/Stages/InlineStage.cs
  42. +21 −20 Source/Mosa.Compiler.Framework/Stages/OperandAssignmentStage.cs
  43. +2 −2 Source/Mosa.Compiler.Framework/Stages/PlatformIntrinsicStage.cs
  44. +1 −1 Source/Mosa.Compiler.Framework/Stages/PromoteTemporaryVariables.cs
  45. +73 −12 Source/Mosa.Compiler.Framework/Stages/SparseConditionalConstantPropagationStage.cs
  46. +3 −3 Source/Mosa.Compiler.Framework/Stages/StackSetupStage.cs
  47. +1 −1 Source/Mosa.Compiler.Framework/Stages/StaticAllocationResolutionStage.cs
  48. +148 −145 Source/Mosa.Compiler.Framework/Stages/ValueNumberingStage.cs
  49. +0 −4 Source/Mosa.CoolWorld.x86/Mosa.CoolWorld.x86.csproj
  50. +0 −94 Source/Mosa.Kernel/Mosa.Kernel.csproj
  51. +0 −27 Source/Mosa.Kernel/Mosa.Kernel.nuspec
  52. +0 −1 Source/Mosa.Platform.x86/Intrinsic/Div.cs
  53. +0 −1 Source/Mosa.Platform.x86/Intrinsic/GetCS.cs
  54. +1 −1 Source/Mosa.Platform.x86/Intrinsic/XAddLoad32.cs
  55. +1 −2 Source/Mosa.Platform.x86/Stages/AddressModeConversionStage.cs
  56. +12 −6 Source/Mosa.Platform.x86/Stages/BuildStackStage.cs
  57. +1 −0 Source/Mosa.Platform.x86/Stages/ConstantInstructionStage.cs
  58. +4 −0 Source/Mosa.Platform.x86/Stages/FinalTweakStage.cs
  59. +1 −1 Source/Mosa.Platform.x86/Stages/SimpleDeadCodeRemovalStage.cs
  60. +0 −1 Source/Mosa.Platform.x86/Stages/TweakStage.cs
  61. +18 −17 Source/Mosa.Tool.Explorer/MainForm.Designer.cs
  62. +1 −0 Source/Mosa.Tool.Explorer/MainForm.cs
  63. +34 −34 Source/Mosa.Tool.Launcher/MainForm.Designer.cs
  64. +0 −3 Source/Mosa.Tool.Launcher/Mosa.Tool.Launcher.csproj
  65. +13 −0 Source/Mosa.UnitTest.Collection/OptimizationTests.cs
  66. +0 −4 Source/Mosa.VBEWorld.x86/Mosa.VBEWorld.x86.csproj
@@ -12,6 +12,7 @@
[Dd]ebug/
[Rr]elease/
[b]in - Copy/
[b]in2/

*.[Oo]bj
*.suo
@@ -1 +1 @@
start ..\bin\Mosa.Tool.Launcher.exe --q --a --qemu --output-asm --threading-off ..\bin\Mosa.HelloWorld.x86.exe
start ..\bin\Mosa.Tool.Launcher.exe --q --a --qemu --output-asm ..\bin\Mosa.HelloWorld.x86.exe
@@ -1,2 +1,2 @@
start ..\bin\Mosa.Tool.Launcher.exe --q --a --qemu --output-map --output-asm --output-debug --all-optimization-off --threading-off ..\bin2\Mosa.TestWorld.x86.exe
start ..\bin\Mosa.Tool.Launcher.exe --q --a --qemu --output-map --output-asm --output-debug ..\bin\Mosa.TestWorld.x86.exe

@@ -145,8 +145,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mosa.Tool.GDBDebugger", "Mo
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mosa.VBEWorld.x86", "Mosa.VBEWorld.x86\Mosa.VBEWorld.x86.csproj", "{E039B97A-FEA6-4655-94D6-32381A9F9931}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mosa.Kernel", "Mosa.Kernel\Mosa.Kernel.csproj", "{FF099D48-3FA5-4ED6-BBA0-1497D994962A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mosa.Workspace.Experiment.Debug", "Mosa.Workspace.Experiment.Debug\Mosa.Workspace.Experiment.Debug.csproj", "{FD933E22-721A-453B-BC1C-A60AE1D10829}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mosa.Tool.Bootstrap", "Mosa.Tool.Bootstrap\Mosa.Tool.Bootstrap.csproj", "{8534DDDE-8B1A-4B5E-AECE-3D07834D9902}"
@@ -164,7 +162,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mosa.Tools.Package", "Mosa.
{C286833F-D1CD-4618-98E7-F3A5BA9ECFA1} = {C286833F-D1CD-4618-98E7-F3A5BA9ECFA1}
{44725E46-513F-4CFB-8196-2191399C514B} = {44725E46-513F-4CFB-8196-2191399C514B}
{FF099D48-3FA5-4ED6-BBA0-1497D734962A} = {FF099D48-3FA5-4ED6-BBA0-1497D734962A}
{FF099D48-3FA5-4ED6-BBA0-1497D994962A} = {FF099D48-3FA5-4ED6-BBA0-1497D994962A}
{7B8FBA4E-D6AC-4DAD-A607-43BA112F8EC1} = {7B8FBA4E-D6AC-4DAD-A607-43BA112F8EC1}
{4DBD4C50-BEC5-48D9-B471-9FB0A7F69999} = {4DBD4C50-BEC5-48D9-B471-9FB0A7F69999}
{2A98B95B-B900-485C-85F7-7CD2944612C0} = {2A98B95B-B900-485C-85F7-7CD2944612C0}
@@ -737,18 +734,6 @@ Global
{E039B97A-FEA6-4655-94D6-32381A9F9931}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{E039B97A-FEA6-4655-94D6-32381A9F9931}.Release|x86.ActiveCfg = Release|Any CPU
{E039B97A-FEA6-4655-94D6-32381A9F9931}.Release|x86.Build.0 = Release|Any CPU
{FF099D48-3FA5-4ED6-BBA0-1497D994962A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FF099D48-3FA5-4ED6-BBA0-1497D994962A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF099D48-3FA5-4ED6-BBA0-1497D994962A}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{FF099D48-3FA5-4ED6-BBA0-1497D994962A}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{FF099D48-3FA5-4ED6-BBA0-1497D994962A}.Debug|x86.ActiveCfg = Debug|Any CPU
{FF099D48-3FA5-4ED6-BBA0-1497D994962A}.Debug|x86.Build.0 = Debug|Any CPU
{FF099D48-3FA5-4ED6-BBA0-1497D994962A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF099D48-3FA5-4ED6-BBA0-1497D994962A}.Release|Any CPU.Build.0 = Release|Any CPU
{FF099D48-3FA5-4ED6-BBA0-1497D994962A}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{FF099D48-3FA5-4ED6-BBA0-1497D994962A}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{FF099D48-3FA5-4ED6-BBA0-1497D994962A}.Release|x86.ActiveCfg = Release|Any CPU
{FF099D48-3FA5-4ED6-BBA0-1497D994962A}.Release|x86.Build.0 = Release|Any CPU
{FD933E22-721A-453B-BC1C-A60AE1D10829}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FD933E22-721A-453B-BC1C-A60AE1D10829}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD933E22-721A-453B-BC1C-A60AE1D10829}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@@ -860,7 +845,6 @@ Global
{FD266261-809F-483C-99CF-F26A7C143E52} = {1F29608A-4C39-42C7-B981-154F92B9EDAC}
{DFD831EA-79D8-49FC-8923-3AFFDA1283B9} = {D032B24A-CE3A-4881-BACE-CC4FE0AFD69D}
{E039B97A-FEA6-4655-94D6-32381A9F9931} = {1AB7FA6A-829C-4DCF-BCA6-A27156D8DA26}
{FF099D48-3FA5-4ED6-BBA0-1497D994962A} = {FF773BDF-A20C-4A5F-BFF9-F422C2AFF3CE}
{FD933E22-721A-453B-BC1C-A60AE1D10829} = {AC7C3CDA-E0E8-408C-BB32-DBCBB905E3E1}
{8534DDDE-8B1A-4B5E-AECE-3D07834D9902} = {D032B24A-CE3A-4881-BACE-CC4FE0AFD69D}
{3C5B4E7A-0380-4DA4-BE61-4B2E6485841A} = {52646152-3847-437E-9CCC-55DE500A5D6F}
@@ -1,7 +1,8 @@

// This file is used by Code Analysis to maintain SuppressMessage
// Copyright (c) MOSA Project. Licensed under the New BSD License.

// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "<Pending>", Scope = "type", Target = "~T:Mosa.Compiler.Common.Exceptions.CompilerException")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "RCS1194:Implement exception constructors.", Justification = "<Pending>", Scope = "type", Target = "~T:Mosa.Compiler.Common.Exceptions.CompilerException")]
@@ -1,8 +1,8 @@
// Copyright (c) MOSA Project. Licensed under the New BSD License.

using Mosa.Compiler.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;

namespace Mosa.Compiler.Framework.Analysis
{
@@ -20,7 +20,7 @@ public sealed class SimpleFastDominance
/// <summary>
/// Holds the dominance information of a block.
/// </summary>
private readonly Dictionary<BasicBlock, BasicBlock> doms = new Dictionary<BasicBlock, BasicBlock>();
private readonly BasicBlock[] doms;

/// <summary>
/// Holds the dominance frontier blocks.
@@ -42,6 +42,8 @@ public sealed class SimpleFastDominance
/// </summary>
private List<BasicBlock> reversePostOrder;

private int[] blockToPostorderIndex;

#endregion Data Members

/// <summary>
@@ -51,12 +53,23 @@ public sealed class SimpleFastDominance
/// <param name="entryBlock">The entry block.</param>
public SimpleFastDominance(BasicBlocks basicBlocks, BasicBlock entryBlock)
{
domFrontierOfBlock = new List<BasicBlock>[basicBlocks.Count];
children = new List<BasicBlock>[basicBlocks.Count];
int blockCount = basicBlocks.Count;

doms = new BasicBlock[blockCount];
domFrontierOfBlock = new List<BasicBlock>[blockCount];
children = new List<BasicBlock>[blockCount];
blockToPostorderIndex = new int[blockCount];

// Blocks in reverse post order topology
reversePostOrder = BasicBlocks.ReversePostOrder(entryBlock);

// Map block to reverse post order index
int i = reversePostOrder.Count;
foreach (var block in reversePostOrder)
{
blockToPostorderIndex[block.Sequence] = --i;
}

CalculateDominance();
CalculateChildren();
CalculateDominanceFrontier();
@@ -67,47 +80,43 @@ public SimpleFastDominance(BasicBlocks basicBlocks, BasicBlock entryBlock)
/// </summary>
private void CalculateDominance()
{
// Changed flag
bool changed = true;
var startNode = reversePostOrder[0];
doms[startNode.Sequence] = startNode;

doms.Add(reversePostOrder[0], reversePostOrder[0]);
bool changed = true;

// Calculate the dominance
while (changed)
{
changed = false;
foreach (var b in reversePostOrder)
{
BasicBlock idom = null;
if (b == startNode)
continue;

BasicBlock newIDom = null;

foreach (var previous in b.PreviousBlocks)
{
if (idom == null)
var dom = doms[previous.Sequence];

if (dom == null)
continue;

if (newIDom == null)
{
idom = previous;
newIDom = previous;
}
else
{
if (doms.ContainsKey(previous))
{
idom = Intersect(previous, idom);
}
newIDom = Intersect(previous, newIDom);
}
}

if (!doms.TryGetValue(b, out BasicBlock dom))
if (doms[b.Sequence] != newIDom || doms[b.Sequence] == null)
{
doms.Add(b, idom);
doms[b.Sequence] = newIDom;
changed = true;
}
else
{
if (!ReferenceEquals(dom, idom))
{
doms[b] = idom;
changed = true;
}
}
}
}
}
@@ -124,9 +133,6 @@ private void CalculateChildren()
if (immediateDominator == null)
continue;

if (block == immediateDominator)
continue;

var list = children[immediateDominator.Sequence];

if (list == null)
@@ -135,6 +141,9 @@ private void CalculateChildren()
children[immediateDominator.Sequence] = list;
}

if (block == immediateDominator)
continue;

list.AddIfNew(block);
}
}
@@ -152,7 +161,7 @@ private void CalculateDominanceFrontier()
{
var runner = p;

while (runner != null && !ReferenceEquals(runner, doms[b]))
while (runner != null && runner != doms[b.Sequence])
{
var runnerFrontier = domFrontierOfBlock[runner.Sequence];

@@ -165,8 +174,7 @@ private void CalculateDominanceFrontier()
domFrontier.AddIfNew(b);
runnerFrontier.AddIfNew(b);

doms.TryGetValue(runner, out BasicBlock newrunner);
runner = newrunner;
runner = doms[runner.Sequence];
}
}
}
@@ -183,7 +191,9 @@ public List<BasicBlock> IteratedDominanceFrontier(List<BasicBlock> blocks)
var workList = new Queue<BasicBlock>();

foreach (var block in blocks)
{
workList.Enqueue(block);
}

while (workList.Count > 0)
{
@@ -211,27 +221,21 @@ public List<BasicBlock> IteratedDominanceFrontier(List<BasicBlock> blocks)

public BasicBlock GetImmediateDominator(BasicBlock block)
{
if (block == null)
throw new ArgumentNullException(nameof(block));

doms.TryGetValue(block, out BasicBlock idom);

return idom;
return doms[block.Sequence];
}

public List<BasicBlock> GetDominators(BasicBlock block)
{
if (block == null)
throw new ArgumentNullException(nameof(block));

var result = new List<BasicBlock>();
var b = block;

while (b != null)
{
result.Add(b);

if (!doms.TryGetValue(b, out b))
b = doms[b.Sequence];

if (b == null)
{
return result;
}
@@ -272,7 +276,9 @@ public bool IsDominator(BasicBlock dom, BasicBlock block)

while (b != null)
{
if (!doms.TryGetValue(b, out b))
b = doms[b.Sequence];

if (b == null)
{
return false;
}
@@ -307,20 +313,21 @@ public List<BasicBlock> GetReversePostOrder()
/// <returns>The highest common dominator.</returns>
private BasicBlock Intersect(BasicBlock b1, BasicBlock b2)
{
BasicBlock finger1 = b1, finger2 = b2;
var finger1 = b1;
var finger2 = b2;

while (finger2 != null && finger1 != null && finger1 != finger2)
while (finger1 != finger2)
{
while (finger1?.Sequence > finger2.Sequence)
while (blockToPostorderIndex[finger1.Sequence] < blockToPostorderIndex[finger2.Sequence])
{
doms.TryGetValue(finger1, out BasicBlock f);
finger1 = f;
Debug.Assert(doms[finger1.Sequence] != null);
finger1 = doms[finger1.Sequence];
}

while (finger2 != null && finger1 != null && finger2.Sequence > finger1.Sequence)
while (blockToPostorderIndex[finger2.Sequence] < blockToPostorderIndex[finger1.Sequence])
{
doms.TryGetValue(finger2, out BasicBlock f);
finger2 = f;
Debug.Assert(doms[finger2.Sequence] != null);
finger2 = doms[finger2.Sequence];
}
}

@@ -11,6 +11,7 @@ public abstract class BaseCodeTransformationStage : BaseMethodCompilerStage
private const int MaxInstructions = 1024;

protected delegate void ContextVisitationDelegate(Context context);

protected delegate void NodeVisitationDelegate(InstructionNode node);

protected ContextVisitationDelegate[] visitationContexts;
@@ -34,7 +35,7 @@ protected override void Run()

for (int index = 0; index < BasicBlocks.Count; index++)
{
for (var node = BasicBlocks[index].First; !node.IsBlockEndInstruction; node = node.Next)
for (var node = BasicBlocks[index].AfterFirst; !node.IsBlockEndInstruction; node = node.Next)
{
if (node.IsEmpty)
continue;
@@ -37,7 +37,7 @@ protected override void Run()

for (int index = 0; index < BasicBlocks.Count; index++)
{
for (var node = BasicBlocks[index].First; !node.IsBlockEndInstruction; node = node.Next)
for (var node = BasicBlocks[index].AfterFirst; !node.IsBlockEndInstruction; node = node.Next)
{
if (node.IsEmpty)
continue;
@@ -56,18 +56,12 @@ public abstract class BaseCompilerStage

#endregion Properties

#region IPipelineStage Members

/// <summary>
/// Retrieves the name of the compilation stage.
/// </summary>
/// <value>The name of the compilation stage.</value>
public virtual string Name { get { return GetType().Name; } }

#endregion IPipelineStage Members

#region ICompilerStage members

public void Initialize(Compiler compiler)
{
Debug.Assert(compiler != null);
@@ -78,7 +72,7 @@ public void Initialize(Compiler compiler)
}

/// <summary>
/// Executes the pre compile phase.
/// Executes the pre-compile phase.
/// </summary>
public void ExecutePreCompile()
{
@@ -93,8 +87,6 @@ public void ExecutePostCompile()
RunPostCompile();
}

#endregion ICompilerStage members

#region Overrides

/// <summary>
Oops, something went wrong.

0 comments on commit d5b45ee

Please sign in to comment.
You can’t perform that action at this time.