public void Loop() { var cfg = new ControlFlowGraph <int>(IntArchitecture.Instance); var nodes = new ControlFlowNode <int> [4]; for (int i = 0; i < nodes.Length; i++) { nodes[i] = new ControlFlowNode <int>(i, i); cfg.Nodes.Add(nodes[i]); } nodes[0].ConnectWith(nodes[2]); nodes[1].ConnectWith(nodes[2]); nodes[2].ConnectWith(nodes[1], ControlFlowEdgeType.Conditional); nodes[2].ConnectWith(nodes[3]); cfg.Entrypoint = nodes[0]; var tree = DominatorTree.FromGraph(cfg); Assert.Equal(new HashSet <INode> { nodes[2] }, tree.GetDominanceFrontier(nodes[1])); Assert.Equal(new HashSet <INode> { nodes[2] }, tree.GetDominanceFrontier(nodes[2])); }
public void Loop() { // Artificially construct a looping construct. var graph = TestGraphs.CreateLoop(); var n1 = graph.GetNodeByOffset(0); var n2 = graph.GetNodeByOffset(1); var n3 = graph.GetNodeByOffset(2); var n4 = graph.GetNodeByOffset(4); var dominatorTree = DominatorTree <DummyInstruction> .FromGraph(graph); Assert.Equal(graph.Entrypoint, dominatorTree.Root.OriginalNode); Assert.True(dominatorTree.Dominates(n1, n1)); Assert.True(dominatorTree.Dominates(n1, n2)); Assert.True(dominatorTree.Dominates(n1, n3)); Assert.True(dominatorTree.Dominates(n1, n4)); Assert.False(dominatorTree.Dominates(n2, n1)); Assert.True(dominatorTree.Dominates(n2, n2)); Assert.False(dominatorTree.Dominates(n2, n3)); Assert.False(dominatorTree.Dominates(n2, n4)); Assert.False(dominatorTree.Dominates(n3, n1)); Assert.True(dominatorTree.Dominates(n3, n2)); Assert.True(dominatorTree.Dominates(n3, n3)); Assert.True(dominatorTree.Dominates(n3, n4)); Assert.False(dominatorTree.Dominates(n4, n1)); Assert.False(dominatorTree.Dominates(n4, n2)); Assert.False(dominatorTree.Dominates(n4, n3)); Assert.True(dominatorTree.Dominates(n4, n4)); }
public void Path() { // Artificially construct a path of four nodes in sequential order. var graph = TestGraphs.CreatePath(); var n1 = graph.GetNodeByOffset(0); var n2 = graph.GetNodeByOffset(1); var n3 = graph.GetNodeByOffset(2); var n4 = graph.GetNodeByOffset(3); var dominatorTree = DominatorTree <DummyInstruction> .FromGraph(graph); Assert.Equal(graph.Entrypoint, dominatorTree.Root.OriginalNode); Assert.True(dominatorTree.Dominates(n1, n1)); Assert.True(dominatorTree.Dominates(n1, n2)); Assert.True(dominatorTree.Dominates(n1, n3)); Assert.True(dominatorTree.Dominates(n1, n4)); Assert.False(dominatorTree.Dominates(n2, n1)); Assert.True(dominatorTree.Dominates(n2, n2)); Assert.True(dominatorTree.Dominates(n2, n3)); Assert.True(dominatorTree.Dominates(n2, n4)); Assert.False(dominatorTree.Dominates(n3, n1)); Assert.False(dominatorTree.Dominates(n3, n2)); Assert.True(dominatorTree.Dominates(n3, n3)); Assert.True(dominatorTree.Dominates(n3, n4)); Assert.False(dominatorTree.Dominates(n4, n1)); Assert.False(dominatorTree.Dominates(n4, n2)); Assert.False(dominatorTree.Dominates(n4, n3)); Assert.True(dominatorTree.Dominates(n4, n4)); }
public void SingleNode() { var graph = TestGraphs.CreateSingularGraph(); var dominatorTree = DominatorTree <DummyInstruction> .FromGraph(graph); Assert.Equal(graph.Entrypoint, dominatorTree.Root.OriginalNode); Assert.True(dominatorTree.Dominates(graph.Entrypoint, graph.Entrypoint)); }
public void Simple() { var cfg = new ControlFlowGraph <int>(IntArchitecture.Instance); var n = new ControlFlowNode <int>(0, 0); cfg.Nodes.Add(n); cfg.Entrypoint = n; var tree = DominatorTree.FromGraph(cfg); Assert.Empty(tree.GetDominanceFrontier(n)); }
public void ExceptionHandler() { var cfg = new ControlFlowGraph <int>(IntArchitecture.Instance); for (int i = 0; i < 7; i++) { cfg.Nodes.Add(new ControlFlowNode <int>(i)); } cfg.Entrypoint = cfg.Nodes[0]; cfg.Nodes[0].ConnectWith(cfg.Nodes[1]); cfg.Nodes[1].ConnectWith(cfg.Nodes[2], ControlFlowEdgeType.Conditional); cfg.Nodes[1].ConnectWith(cfg.Nodes[3], ControlFlowEdgeType.FallThrough); cfg.Nodes[2].ConnectWith(cfg.Nodes[4], ControlFlowEdgeType.Unconditional); cfg.Nodes[3].ConnectWith(cfg.Nodes[4], ControlFlowEdgeType.FallThrough); cfg.Nodes[4].ConnectWith(cfg.Nodes[6], ControlFlowEdgeType.Unconditional); cfg.Nodes[5].ConnectWith(cfg.Nodes[6], ControlFlowEdgeType.Unconditional); var ehRegion = new ExceptionHandlerRegion <int>(); cfg.Regions.Add(ehRegion); ehRegion.ProtectedRegion.Entrypoint = cfg.Nodes[1]; ehRegion.ProtectedRegion.Nodes.AddRange(new[] { cfg.Nodes[1], cfg.Nodes[2], cfg.Nodes[3], cfg.Nodes[4], }); var handler = new HandlerRegion <int>(); ehRegion.Handlers.Add(handler); handler.Contents.Nodes.Add(cfg.Nodes[5]); handler.Contents.Entrypoint = cfg.Nodes[5]; var tree = DominatorTree <int> .FromGraph(cfg); Assert.True(tree.Dominates(cfg.Nodes[1], cfg.Nodes[6])); Assert.False(tree.Dominates(cfg.Nodes[4], cfg.Nodes[6])); Assert.False(tree.Dominates(cfg.Nodes[5], cfg.Nodes[6])); }
public void Path() { var cfg = new ControlFlowGraph <int>(IntArchitecture.Instance); var nodes = new ControlFlowNode <int> [3]; for (int i = 0; i < nodes.Length; i++) { nodes[i] = new ControlFlowNode <int>(i, i); cfg.Nodes.Add(nodes[i]); if (i > 0) { nodes[i - 1].ConnectWith(nodes[i]); } } cfg.Entrypoint = nodes[0]; var tree = DominatorTree.FromGraph(cfg); Assert.All(nodes, n => Assert.Empty(tree.GetDominanceFrontier(n))); }
public void Complex() { // Example graph from: // http://www.sable.mcgill.ca/~hendren/621/ControlFlowAnalysis_Handouts.pdf // (slide 57) var cfg = new ControlFlowGraph <int>(IntArchitecture.Instance); var nodes = new ControlFlowNode <int> [11]; for (int i = 0; i < nodes.Length; i++) { nodes[i] = new ControlFlowNode <int>(i, i); cfg.Nodes.Add(nodes[i]); } nodes[0].ConnectWith(nodes[1]); nodes[1].ConnectWith(nodes[2], ControlFlowEdgeType.Conditional); nodes[1].ConnectWith(nodes[3]); nodes[2].ConnectWith(nodes[3]); nodes[3].ConnectWith(nodes[4]); nodes[4].ConnectWith(nodes[3], ControlFlowEdgeType.Conditional); nodes[4].ConnectWith(nodes[5]); nodes[4].ConnectWith(nodes[6], ControlFlowEdgeType.Conditional); nodes[5].ConnectWith(nodes[7]); nodes[6].ConnectWith(nodes[7]); nodes[7].ConnectWith(nodes[8]); nodes[7].ConnectWith(nodes[4], ControlFlowEdgeType.Conditional); nodes[8].ConnectWith(nodes[9]); nodes[8].ConnectWith(nodes[10], ControlFlowEdgeType.Conditional); nodes[8].ConnectWith(nodes[3], ControlFlowEdgeType.Conditional); nodes[9].ConnectWith(nodes[1]); nodes[10].ConnectWith(nodes[7]); cfg.Entrypoint = nodes[0]; var tree = DominatorTree.FromGraph(cfg); Assert.Empty(tree.GetDominanceFrontier(nodes[0])); Assert.Equal(new HashSet <INode> { nodes[1] }, tree.GetDominanceFrontier(nodes[1])); Assert.Equal(new HashSet <INode> { nodes[3] }, tree.GetDominanceFrontier(nodes[2])); Assert.Equal(new HashSet <INode> { nodes[1], nodes[3] }, tree.GetDominanceFrontier(nodes[3])); Assert.Equal(new HashSet <INode> { nodes[1], nodes[3], nodes[4] }, tree.GetDominanceFrontier(nodes[4])); Assert.Equal(new HashSet <INode> { nodes[7] }, tree.GetDominanceFrontier(nodes[5])); Assert.Equal(new HashSet <INode> { nodes[7] }, tree.GetDominanceFrontier(nodes[6])); Assert.Equal(new HashSet <INode> { nodes[1], nodes[3], nodes[4], nodes[7] }, tree.GetDominanceFrontier(nodes[7])); Assert.Equal(new HashSet <INode> { nodes[1], nodes[3], nodes[7] }, tree.GetDominanceFrontier(nodes[8])); Assert.Equal(new HashSet <INode> { nodes[1] }, tree.GetDominanceFrontier(nodes[9])); Assert.Equal(new HashSet <INode> { nodes[7] }, tree.GetDominanceFrontier(nodes[10])); }