Skip to content

Commit

Permalink
Also use GraphTraversal.DepthFirstSearch for dominance calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrunwald committed Oct 21, 2023
1 parent d58576f commit c121872
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
32 changes: 20 additions & 12 deletions ICSharpCode.Decompiler/FlowAnalysis/ControlFlowNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,24 +88,32 @@ public void AddEdgeTo(ControlFlowNode target)

public void TraversePreOrder(Func<ControlFlowNode, IEnumerable<ControlFlowNode>> children, Action<ControlFlowNode> visitAction)
{
if (Visited)
return;
Visited = true;
visitAction(this);
foreach (ControlFlowNode t in children(this))
t.TraversePreOrder(children, visitAction);
GraphTraversal.DepthFirstSearch(new[] { this }, Visit, children);

bool Visit(ControlFlowNode node)
{
if (node.Visited)
return false;
node.Visited = true;
visitAction(node);
return true;
}
}

public void TraversePostOrder(Func<ControlFlowNode, IEnumerable<ControlFlowNode>> children, Action<ControlFlowNode> visitAction)
{
if (Visited)
return;
Visited = true;
foreach (ControlFlowNode t in children(this))
t.TraversePostOrder(children, visitAction);
visitAction(this);
GraphTraversal.DepthFirstSearch(new[] { this }, Visit, children, postorderAction: visitAction);

bool Visit(ControlFlowNode node)
{
if (node.Visited)
return false;
node.Visited = true;
return true;
}
}


/// <summary>
/// Gets whether <c>this</c> dominates <paramref name="node"/>.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion ICSharpCode.Decompiler/IL/Instructions/BlockContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public List<Block> TopologicalSort(bool deleteUnreachableBlocks = false)
// Visit blocks in post-order
BitSet visited = new BitSet(Blocks.Count);
List<Block> postOrder = new List<Block>();
GraphTraversal.DepthFirstSearch(new[] { EntryPoint }, Successors, postOrder.Add, MarkAsVisited, reverseSuccessors: true);
GraphTraversal.DepthFirstSearch(new[] { EntryPoint }, MarkAsVisited, Successors, postOrder.Add, reverseSuccessors: true);
postOrder.Reverse();
if (!deleteUnreachableBlocks)
{
Expand Down
7 changes: 4 additions & 3 deletions ICSharpCode.Decompiler/Util/GraphTraversal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ static class GraphTraversal
/// </summary>
/// <param name="startNodes">The start nodes.</param>
/// <param name="visitedFunc">Called multiple times per node. The first call should return true, subsequent calls must return false.
/// If this function is not provided, normal Equals/GetHashCode will be used to compare nodes.</param>
/// The first calls to this function occur in pre-order.
/// If null, normal Equals/GetHashCode will be used to compare nodes.</param>
/// <param name="successorFunc">The function that gets the successors of an element. Called in pre-order.</param>
/// <param name="postorderAction">Called in post-order.</param>
/// <param name="reverseSuccessors">
Expand All @@ -40,7 +41,7 @@ static class GraphTraversal
/// so that blocks which could be output in either order (e.g. then-block and else-block of an if)
/// will maintain the order of the edges (then-block before else-block).
/// </param>
public static void DepthFirstSearch<T>(IEnumerable<T> startNodes, Func<T, IEnumerable<T>?> successorFunc, Action<T> postorderAction, Func<T, bool>? visitedFunc = null, bool reverseSuccessors = false)
public static void DepthFirstSearch<T>(IEnumerable<T> startNodes, Func<T, bool>? visitedFunc, Func<T, IEnumerable<T>?> successorFunc, Action<T>? postorderAction = null, bool reverseSuccessors = false)
{
/*
Pseudocode:
Expand Down Expand Up @@ -80,7 +81,7 @@ public static void DepthFirstSearch<T>(IEnumerable<T> startNodes, Func<T, IEnume
if (isPostOrderContinuation)
{
// Execute postorder_action
postorderAction(node);
postorderAction?.Invoke(node);
worklist.RemoveAt(worklist.Count - 1);
continue;
}
Expand Down

0 comments on commit c121872

Please sign in to comment.