public static Dictionary<CFGBlock, List<CFGBlock>> ComputeContainingLoopMap(ICFG cfg) { Contract.Requires(cfg != null); Contract.Ensures(Contract.Result<Dictionary<CFGBlock, List<CFGBlock>>>() != null); var result = new Dictionary<CFGBlock, List<CFGBlock>>(); var visitedSubroutines = new Set<int>(); var pendingSubroutines = new Stack<Subroutine>(); var pendingAPCs = new Stack<APC>(); var graph = cfg.AsBackwardGraph(includeExceptionEdges:false, skipContracts:true); foreach (var loophead in cfg.LoopHeads) { // push back-edge sources var loopPC = new APC(loophead, 0, null); foreach (var pred in cfg.Predecessors(loopPC)) { if (cfg.IsForwardBackEdge(pred, loopPC)) { var normalizedPred = new APC(pred.Block, 0, null); pendingAPCs.Push(normalizedPred); } } var visit = new DepthFirst.Visitor<APC, Unit>(graph, (APC pc) => { if (pc.SubroutineContext != null) { // push continuation PC pendingAPCs.Push(new APC(pc.SubroutineContext.Head.One, 0, null)); if (visitedSubroutines.AddQ(pc.Block.Subroutine.Id)) { pendingSubroutines.Push(pc.Block.Subroutine); } return false; // stop exploration } return !pc.Equals(loopPC); }); while (pendingAPCs.Count > 0) { var root = pendingAPCs.Pop(); visit.VisitSubGraphNonRecursive(root); while (pendingSubroutines.Count > 0) { var sub = pendingSubroutines.Pop(); pendingAPCs.Push(new APC(sub.Exit, 0, null)); } } foreach (var visited in visit.Visited) { if (visited.SubroutineContext != null) continue; // ignore non-primary pcs MaterializeContainingLoop(result, visited.Block).AssumeNotNull().Add(loophead); } } return result; }
protected void PostProcessBlocks() { var blockStack = new Stack <CFGBlock> (); this.successor_edges = new EdgeMap <EdgeTag> (this.successors); this.edge_info = new DepthFirst.Visitor <CFGBlock, Dummy> (this, null, (block) => blockStack.Push(block), null); this.edge_info.VisitSubGraphNonRecursive(this.exception_exit); this.edge_info.VisitSubGraphNonRecursive(this.exit); this.edge_info.VisitSubGraphNonRecursive(this.entry); foreach (var successorEdge in this.successor_edges) { int idGen = UnusedBlockIndex; successorEdge.From.Renumber(ref idGen); } int idGen1 = 0; foreach (CFGBlock cfgBlock in blockStack) { cfgBlock.Renumber(ref idGen1); } SuccessorEdges.Filter((e) => e.From.Index != UnusedBlockIndex); this.predecessor_edges = this.successor_edges.Reverse(); int finishTime = 0; var visitor = new DepthFirst.Visitor <CFGBlock, EdgeTag> (this.predecessor_edges, null, block => block.ReversePostOrderIndex = finishTime++, null); visitor.VisitSubGraphNonRecursive(this.exit); foreach (CFGBlock node in blockStack) { visitor.VisitSubGraphNonRecursive(node); } SuccessorEdges.Resort(); this.blocks = blockStack.ToArray(); this.LabelsThatStartBlocks = null; Builder = null; }
public static Dictionary <CFGBlock, List <CFGBlock> > ComputeContainingLoopMap(ICFG cfg) { Contract.Requires(cfg != null); Contract.Ensures(Contract.Result <Dictionary <CFGBlock, List <CFGBlock> > >() != null); var result = new Dictionary <CFGBlock, List <CFGBlock> >(); var visitedSubroutines = new Set <int>(); var pendingSubroutines = new Stack <Subroutine>(); var pendingAPCs = new Stack <APC>(); var graph = cfg.AsBackwardGraph(includeExceptionEdges: false, skipContracts: true); foreach (var loophead in cfg.LoopHeads) { // push back-edge sources var loopPC = new APC(loophead, 0, null); foreach (var pred in cfg.Predecessors(loopPC)) { if (cfg.IsForwardBackEdge(pred, loopPC)) { var normalizedPred = new APC(pred.Block, 0, null); pendingAPCs.Push(normalizedPred); } } var visit = new DepthFirst.Visitor <APC, Unit>(graph, (APC pc) => { if (pc.SubroutineContext != null) { // push continuation PC pendingAPCs.Push(new APC(pc.SubroutineContext.Head.One, 0, null)); if (visitedSubroutines.AddQ(pc.Block.Subroutine.Id)) { pendingSubroutines.Push(pc.Block.Subroutine); } return(false); // stop exploration } return(!pc.Equals(loopPC)); }); while (pendingAPCs.Count > 0) { var root = pendingAPCs.Pop(); visit.VisitSubGraphNonRecursive(root); while (pendingSubroutines.Count > 0) { var sub = pendingSubroutines.Pop(); pendingAPCs.Push(new APC(sub.Exit, 0, null)); } } foreach (var visited in visit.Visited) { if (visited.SubroutineContext != null) { continue; // ignore non-primary pcs } MaterializeContainingLoop(result, visited.Block).AssumeNotNull().Add(loophead); } } return(result); }