public static ISet <Edge <BasicBlock> > FindReverseEdges(ControlFlowGraph.Graph g) { ISet <Edge <BasicBlock> > res = SetFactory.GetEdgesSet();//new SortedSet<Edge <BasicBlock>>(); Dictionary <Edge <BasicBlock>, EdgeType> ClassifiedEdges = EdgeClassification.EdgeClassification.ClassifyEdge(g); Dictionary <int, int> Dominators = ImmediateDominator.FindImmediateDominator(g); var RetreatingEdges = ClassifiedEdges.Where(x => x.Value == EdgeType.Retreating); foreach (var edg in RetreatingEdges) { var edge = edg.Key; int key = edge.Source.BlockId; int value = edge.Target.BlockId; bool isReverse = false; while (Dominators.ContainsKey(key) && Dominators[key] != key && !isReverse) { key = Dominators[key]; isReverse = (key == value); } if (isReverse) { res.Add(edge); } } return(res); }
public static bool CheckReverseEdges(ControlFlowGraph.Graph g) { Dictionary <Edge <BasicBlock>, EdgeType> ClassifiedEdges = EdgeClassification.EdgeClassification.ClassifyEdge(g); var RetreatingEdges = ClassifiedEdges.Where(x => x.Value == EdgeType.Retreating).Select(x => x.Key); var ReverseEdges = FindReverseEdge.FindReverseEdges(g); return(ReverseEdges.SetEquals(RetreatingEdges)); }
public static Dictionary <Edge <BasicBlock>, EdgeType> ClassifyEdge(ControlFlowGraph.Graph g) { Dictionary <Edge <BasicBlock>, EdgeType> edgeTypes = new Dictionary <Edge <BasicBlock>, EdgeType>(); Dictionary <int, int> dfn = g.GetDFN(); DominatorsTree tree = new DominatorsTree(g); foreach (Edge <BasicBlock> e in g.GetEdges()) { if (dfn[e.Source.BlockId] >= dfn[e.Target.BlockId]) { edgeTypes.Add(e, EdgeType.Retreating); } else if (g.IsAncestor(e.Target.BlockId, e.Source.BlockId) && tree.GetParent(e.Target.BlockId) == e.Source.BlockId) { edgeTypes.Add(e, EdgeType.Advancing); } else { edgeTypes.Add(e, EdgeType.Cross); } } return(edgeTypes); }
public static Dictionary <Edge <BasicBlock>, ISet <int> > FindAllNaturalLoops(ControlFlowGraph.Graph g) { var classify = EdgeClassification.EdgeClassification.ClassifyEdge(g); var result = new Dictionary <Edge <BasicBlock>, ISet <int> >(); foreach (var pair in classify) { if (pair.Value == EdgeClassification.Model.EdgeType.Retreating) { Stack = new Stack <int>(); Loop = SetFactory.GetSet(new int[] { pair.Key.Target.BlockId }); Insert(pair.Key.Source.BlockId); while (Stack.Count() > 0) { int m = Stack.Pop(); foreach (BasicBlock p in g.getParents(m)) { Insert(p.BlockId); } } result.Add(pair.Key, Loop); } } return(result); }