Browse Source

[switch] consider loop back-edges to be exit points when looking for the switch body

pull/887/head
Daniel Grunwald 8 years ago
parent
commit
9ef97703cd
  1. 63
      ICSharpCode.Decompiler/FlowAnalysis/ControlFlowNode.cs
  2. 1
      ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
  3. 51
      ICSharpCode.Decompiler/IL/ControlFlow/LoopDetection.cs
  4. 221
      ICSharpCode.Decompiler/Util/GraphVizGraph.cs

63
ICSharpCode.Decompiler/FlowAnalysis/ControlFlowNode.cs

@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using ICSharpCode.Decompiler.Util;
namespace ICSharpCode.Decompiler.FlowAnalysis
{
@ -118,35 +119,37 @@ namespace ICSharpCode.Decompiler.FlowAnalysis @@ -118,35 +119,37 @@ namespace ICSharpCode.Decompiler.FlowAnalysis
}
return false;
}
//public static GraphVizGraph ExportGraph(IReadOnlyList<ControlFlowNode> nodes, Func<ControlFlowNode, string> labelFunc = null)
//{
// if (labelFunc == null) {
// labelFunc = node => {
// var block = node.UserData as IL.Block;
// return block != null ? block.Label : node.UserData?.ToString();
// };
// }
// GraphVizGraph g = new GraphVizGraph();
// GraphVizNode[] n = new GraphVizNode[nodes.Count];
// for (int i = 0; i < n.Length; i++) {
// n[i] = new GraphVizNode(nodes[i].UserIndex);
// n[i].shape = "box";
// n[i].label = labelFunc(nodes[i]);
// g.AddNode(n[i]);
// }
// foreach (var source in nodes) {
// foreach (var target in source.Successors) {
// g.AddEdge(new GraphVizEdge(source.UserIndex, target.UserIndex));
// }
// if (source.ImmediateDominator != null) {
// g.AddEdge(
// new GraphVizEdge(source.ImmediateDominator.UserIndex, source.UserIndex) {
// color = "green"
// });
// }
// }
// return g;
//}
#if DEBUG
internal static GraphVizGraph ExportGraph(IReadOnlyList<ControlFlowNode> nodes, Func<ControlFlowNode, string> labelFunc = null)
{
if (labelFunc == null) {
labelFunc = node => {
var block = node.UserData as IL.Block;
return block != null ? block.Label : node.UserData?.ToString();
};
}
GraphVizGraph g = new GraphVizGraph();
GraphVizNode[] n = new GraphVizNode[nodes.Count];
for (int i = 0; i < n.Length; i++) {
n[i] = new GraphVizNode(nodes[i].UserIndex);
n[i].shape = "box";
n[i].label = labelFunc(nodes[i]);
g.AddNode(n[i]);
}
foreach (var source in nodes) {
foreach (var target in source.Successors) {
g.AddEdge(new GraphVizEdge(source.UserIndex, target.UserIndex));
}
if (source.ImmediateDominator != null) {
g.AddEdge(
new GraphVizEdge(source.ImmediateDominator.UserIndex, source.UserIndex) {
color = "green"
});
}
}
return g;
}
#endif
}
}

1
ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

@ -300,6 +300,7 @@ @@ -300,6 +300,7 @@
<Compile Include="IL\Transforms\StatementTransform.cs" />
<Compile Include="IL\Transforms\TransformCollectionAndObjectInitializers.cs" />
<Compile Include="Output\TextTokenWriter.cs" />
<Compile Include="Util\GraphVizGraph.cs" />
<Compile Include="Util\KeyComparer.cs" />
<Compile Include="Util\LongDict.cs" />
<Compile Include="Util\UnicodeNewline.cs" />

51
ICSharpCode.Decompiler/IL/ControlFlow/LoopDetection.cs

@ -210,9 +210,9 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -210,9 +210,9 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
/// exit-point (if non-null) is not in this set,
/// nodes are no longer marked as visited in the CFG
/// </remarks>
void ExtendLoop(ControlFlowNode loopHead, List<ControlFlowNode> loop, out ControlFlowNode exitPoint)
void ExtendLoop(ControlFlowNode loopHead, List<ControlFlowNode> loop, out ControlFlowNode exitPoint, bool isSwitch=false)
{
exitPoint = FindExitPoint(loopHead, loop);
exitPoint = FindExitPoint(loopHead, loop, isSwitch);
Debug.Assert(!loop.Contains(exitPoint), "Cannot pick an exit point that is part of the natural loop");
if (exitPoint != null) {
// Either we are in case 1 and just picked an exit that maximizes the amount of code
@ -258,9 +258,16 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -258,9 +258,16 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
/// Finds a suitable single exit point for the specified loop.
/// </summary>
/// <remarks>This method must not write to the Visited flags on the CFG.</remarks>
ControlFlowNode FindExitPoint(ControlFlowNode loopHead, IReadOnlyList<ControlFlowNode> naturalLoop)
ControlFlowNode FindExitPoint(ControlFlowNode loopHead, IReadOnlyList<ControlFlowNode> naturalLoop, bool treatBackEdgesAsExits)
{
if (!context.ControlFlowGraph.HasReachableExit(loopHead)) {
treatBackEdgesAsExits = false;
bool hasReachableExit = context.ControlFlowGraph.HasReachableExit(loopHead);
if (!hasReachableExit && treatBackEdgesAsExits) {
// If we're analyzing the switch, there's no reachable exit, but the loopHead (=switchHead) block
// is also a loop head, we consider the back-edge a reachable exit for the switch.
hasReachableExit = loopHead.Predecessors.Any(p => loopHead.Dominates(p));
}
if (!hasReachableExit) {
// Case 1:
// There are no nodes n so that loopHead dominates a predecessor of n but not n itself
// -> we could build a loop with zero exit points.
@ -275,7 +282,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -275,7 +282,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
// We need to pick our exit point so that all paths from the loop head
// to the reachable exits run through that exit point.
var cfg = context.ControlFlowGraph.cfg;
var revCfg = PrepareReverseCFG(loopHead);
var revCfg = PrepareReverseCFG(loopHead, treatBackEdgesAsExits);
//ControlFlowNode.ExportGraph(cfg).Show("cfg");
//ControlFlowNode.ExportGraph(revCfg).Show("rev");
ControlFlowNode commonAncestor = revCfg[loopHead.UserIndex];
@ -297,7 +304,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -297,7 +304,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
return exitPoint;
}
}
// least common dominator is the artificial exit node
// least common post-dominator is the artificial exit node
return null;
}
}
@ -339,7 +346,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -339,7 +346,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
}
}
ControlFlowNode[] PrepareReverseCFG(ControlFlowNode loopHead)
ControlFlowNode[] PrepareReverseCFG(ControlFlowNode loopHead, bool treatBackEdgesAsExits)
{
ControlFlowNode[] cfg = context.ControlFlowGraph.cfg;
ControlFlowNode[] rev = new ControlFlowNode[cfg.Length + 1];
@ -353,7 +360,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -353,7 +360,7 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
continue;
// Add reverse edges for all edges in cfg
foreach (var succ in cfg[i].Successors) {
if (loopHead.Dominates(succ)) {
if (loopHead.Dominates(succ) && (!treatBackEdgesAsExits || loopHead != succ)) {
rev[succ.UserIndex].AddEdgeTo(rev[i]);
} else {
exitNode.AddEdgeTo(rev[i]);
@ -488,14 +495,22 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -488,14 +495,22 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
private void DetectSwitchBody(Block block, SwitchInstruction switchInst)
{
Debug.Assert(block.Instructions.Last() == switchInst);
ControlFlowNode h = context.ControlFlowNode; // CFG node for our potential loop head
ControlFlowNode h = context.ControlFlowNode; // CFG node for our switch head
Debug.Assert(h.UserData == block);
Debug.Assert(!TreeTraversal.PreOrder(h, n => n.DominatorTreeChildren).Any(n => n.Visited));
var nodesInSwitch = new List<ControlFlowNode>();
nodesInSwitch.Add(h);
h.Visited = true;
ExtendLoop(h, nodesInSwitch, out var exitPoint);
ExtendLoop(h, nodesInSwitch, out var exitPoint, isSwitch: true);
if (IsSimpleSwitchExit(exitPoint)) {
// Also move the exit point into the switch if it's simple enough.
exitPoint.TraversePreOrder(p => p.Successors, nodesInSwitch.Add);
foreach (var node in nodesInSwitch) {
node.Visited = false;
}
exitPoint = null;
}
context.Step("Create BlockContainer for switch", switchInst);
// Sort blocks in the loop in reverse post-order to make the output look a bit nicer.
@ -527,5 +542,21 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow @@ -527,5 +542,21 @@ namespace ICSharpCode.Decompiler.IL.ControlFlow
}
}
}
private bool IsSimpleSwitchExit(ControlFlowNode exitPoint)
{
int totalInstructionCount = 0;
while (exitPoint?.UserData is Block block && block.IncomingEdgeCount == 1) {
totalInstructionCount += block.Instructions.Count;
if (exitPoint.Successors.Count == 1 && block.Instructions.Last() is Branch) {
exitPoint = exitPoint.Successors[0];
} else if (exitPoint.Successors.Count == 0 && block.Instructions.Last() is Leave leave && leave.IsLeavingFunction) {
return totalInstructionCount < 10;
} else {
return false;
}
}
return false;
}
}
}

221
ICSharpCode.Decompiler/Util/GraphVizGraph.cs

@ -0,0 +1,221 @@ @@ -0,0 +1,221 @@
// Copyright (c) 2010-2013 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;
namespace ICSharpCode.Decompiler.Util
{
#if DEBUG
/// <summary>
/// GraphViz graph.
/// </summary>
sealed class GraphVizGraph
{
List<GraphVizNode> nodes = new List<GraphVizNode>();
List<GraphVizEdge> edges = new List<GraphVizEdge>();
public string rankdir;
public string Title;
public void AddEdge(GraphVizEdge edge)
{
edges.Add(edge);
}
public void AddNode(GraphVizNode node)
{
nodes.Add(node);
}
public void Save(string fileName)
{
using (StreamWriter writer = new StreamWriter(fileName))
Save(writer);
}
public void Show()
{
Show(null);
}
public void Show(string name)
{
if (name == null)
name = Title;
if (name != null)
foreach (char c in Path.GetInvalidFileNameChars())
name = name.Replace(c, '-');
string fileName = name != null ? Path.Combine(Path.GetTempPath(), name) : Path.GetTempFileName();
Save(fileName + ".gv");
Process.Start("dot", "\"" + fileName + ".gv\" -Tpng -o \"" + fileName + ".png\"").WaitForExit();
Process.Start(fileName + ".png");
}
static string Escape(string text)
{
if (Regex.IsMatch(text, @"^[\w\d]+$")) {
return text;
} else {
return "\"" + text.Replace("\\", "\\\\").Replace("\r", "").Replace("\n", "\\n").Replace("\"", "\\\"") + "\"";
}
}
static void WriteGraphAttribute(TextWriter writer, string name, string value)
{
if (value != null)
writer.WriteLine("{0}={1};", name, Escape(value));
}
internal static void WriteAttribute(TextWriter writer, string name, double? value, ref bool isFirst)
{
if (value != null) {
WriteAttribute(writer, name, value.Value.ToString(CultureInfo.InvariantCulture), ref isFirst);
}
}
internal static void WriteAttribute(TextWriter writer, string name, bool? value, ref bool isFirst)
{
if (value != null) {
WriteAttribute(writer, name, value.Value ? "true" : "false", ref isFirst);
}
}
internal static void WriteAttribute(TextWriter writer, string name, string value, ref bool isFirst)
{
if (value != null) {
if (isFirst)
isFirst = false;
else
writer.Write(',');
writer.Write("{0}={1}", name, Escape(value));
}
}
public void Save(TextWriter writer)
{
if (writer == null)
throw new ArgumentNullException("writer");
writer.WriteLine("digraph G {");
writer.WriteLine("node [fontsize = 16];");
WriteGraphAttribute(writer, "rankdir", rankdir);
foreach (GraphVizNode node in nodes) {
node.Save(writer);
}
foreach (GraphVizEdge edge in edges) {
edge.Save(writer);
}
writer.WriteLine("}");
}
}
sealed class GraphVizEdge
{
public readonly string Source, Target;
/// <summary>edge stroke color</summary>
public string color;
/// <summary>use edge to affect node ranking</summary>
public bool? constraint;
public string label;
public string style;
/// <summary>point size of label</summary>
public int? fontsize;
public GraphVizEdge(string source, string target)
{
if (source == null)
throw new ArgumentNullException("source");
if (target == null)
throw new ArgumentNullException("target");
this.Source = source;
this.Target = target;
}
public GraphVizEdge(int source, int target)
{
this.Source = source.ToString(CultureInfo.InvariantCulture);
this.Target = target.ToString(CultureInfo.InvariantCulture);
}
public void Save(TextWriter writer)
{
writer.Write("{0} -> {1} [", Source, Target);
bool isFirst = true;
GraphVizGraph.WriteAttribute(writer, "label", label, ref isFirst);
GraphVizGraph.WriteAttribute(writer, "style", style, ref isFirst);
GraphVizGraph.WriteAttribute(writer, "fontsize", fontsize, ref isFirst);
GraphVizGraph.WriteAttribute(writer, "color", color, ref isFirst);
GraphVizGraph.WriteAttribute(writer, "constraint", constraint, ref isFirst);
writer.WriteLine("];");
}
}
sealed class GraphVizNode
{
public readonly string ID;
public string label;
public string labelloc;
/// <summary>point size of label</summary>
public int? fontsize;
/// <summary>minimum height in inches</summary>
public double? height;
/// <summary>space around label</summary>
public string margin;
/// <summary>node shape</summary>
public string shape;
public GraphVizNode(string id)
{
if (id == null)
throw new ArgumentNullException("id");
this.ID = id;
}
public GraphVizNode(int id)
{
this.ID = id.ToString(CultureInfo.InvariantCulture);
}
public void Save(TextWriter writer)
{
writer.Write(ID);
writer.Write(" [");
bool isFirst = true;
GraphVizGraph.WriteAttribute(writer, "label", label, ref isFirst);
GraphVizGraph.WriteAttribute(writer, "labelloc", labelloc, ref isFirst);
GraphVizGraph.WriteAttribute(writer, "fontsize", fontsize, ref isFirst);
GraphVizGraph.WriteAttribute(writer, "margin", margin, ref isFirst);
GraphVizGraph.WriteAttribute(writer, "shape", shape, ref isFirst);
writer.WriteLine("];");
}
}
#endif
}
Loading…
Cancel
Save