private void m_RemoveBackEdge(Loop l) { // first remove the backedges of the nested loops foreach (Loop c in l.NestedLoops) m_RemoveBackEdge(c); //Debugger.Break(); GraphNode loopSkip = null; foreach (GraphNode gn in l.Cutpoint.Suc) { if (l.LoopExitNodes.Contains(gn)) { loopSkip = gn; break; } } if (loopSkip == null) { // We didn't find a loop exit node. There must be a bug Debugger.Break(); } foreach (GraphNode gn in l.Cutpoint.LoopingPred) { List<GraphNode> newsuc = new List<GraphNode>(); foreach (GraphNode s in gn.Suc) { if (s == l.Cutpoint) newsuc.Add(loopSkip); else newsuc.Add(s); } gn.Suc = newsuc; } }
private void m_HavocLoopBody(Loop l) { List<Block> loopblocks = new List<Block>(); foreach (GraphNode g in l.LoopNodes) loopblocks.Add(g.Label); HavocCmd hcmd = m_ComputHavocCmd(loopblocks, l.Cutpoint.Label.tok); //Add Havoc before and after the loop body foreach (GraphNode g in l.Cutpoint.Suc) // before { if (l.LoopNodes.Contains(g)) m_AddHavocCmdToFront(g.Label, hcmd); } foreach (GraphNode g in l.Cutpoint.Pre) // and after { if (l.LoopNodes.Contains(g)) m_AddHavocCmdToFront(g.Label, hcmd); } }
private void CollectLoopBody(GraphNode gn) { if (gn == Cutpoint) return; if (!LoopNodes.Contains(gn)) { if (gn.IsCutpoint) // nested loop found { Loop lo = new Loop(gn); foreach (GraphNode lgn in lo.LoopNodes) { if (!LoopNodes.Contains(lgn)) LoopNodes.Add(lgn); } NestedLoops.Add(lo); } else { LoopNodes.Add(gn); } foreach (GraphNode pre in gn.Pre) if (!gn.LoopingPred.Contains(pre)) CollectLoopBody(pre); } }
private void m_AbstractLoop(Loop l) { foreach (Loop c in l.NestedLoops) m_AbstractLoop(c); m_HavocLoopBody(l); m_RemoveBackEdge(l); }
// Copy Constructor public Loop(Loop l, GraphAnalyzer ga, string prefix) { Dictionary<GraphNode, GraphNode> clonemap = new Dictionary<GraphNode, GraphNode>(); GraphNode clonecutpoint = null; foreach (GraphNode gn in l.LoopNodes) { clonemap[gn] = ga.CloneGraphNode(gn, prefix); if (gn == l.Cutpoint) clonecutpoint = clonemap[gn]; } if (clonecutpoint == null) { Debugger.Break(); return; } // Replace the pre and post nodes by the corresponding clone foreach (GraphNode gn in l.LoopNodes) { List<GraphNode> newl = new List<GraphNode>(); foreach (GraphNode g in clonemap[gn].Pre) { if (clonemap.ContainsKey(g)) newl.Add(clonemap[g]); else newl.Add(g); } clonemap[gn].Pre = newl; newl = new List<GraphNode>(); foreach (GraphNode g in clonemap[gn].Suc) { if (clonemap.ContainsKey(g)) newl.Add(clonemap[g]); else newl.Add(g); } clonemap[gn].Suc = newl; newl = new List<GraphNode>(); foreach (GraphNode g in clonemap[gn].LoopingPred) { if (clonemap.ContainsKey(g)) newl.Add(clonemap[g]); else newl.Add(g); } clonemap[gn].LoopingPred = newl; } foreach (GraphNode gn in l.Cutpoint.LoopingPred) { clonecutpoint.LoopingPred.Remove(gn); clonecutpoint.LoopingPred.Add(clonemap[gn]); } SucLoops.AddRange(l.SucLoops); PreLoops.AddRange(l.PreLoops); Cutpoint = clonecutpoint; LoopNodes.Add(Cutpoint); foreach (GraphNode gn in Cutpoint.LoopingPred) { CollectLoopBody(gn); } CollectLoopExitNodes(); }
private List<Loop> m_CollectLoops(GraphNode gn, Loop lastLoop) { List<Loop> ret = new List<Loop>(); if (gn.Visited) return ret; gn.Visited = true; List<GraphNode> loopingSucs = new List<GraphNode>(); if (gn.IsCutpoint) { Loop l = new Loop(gn); if (lastLoop != null) { lastLoop.SucLoops.Add(l); l.PreLoops.Add(lastLoop); } loopingSucs = l.LoopNodes; lastLoop = l; ret.Add(lastLoop); } foreach (GraphNode suc in gn.Suc) { if (!loopingSucs.Contains(suc)) ret.AddRange(m_CollectLoops(suc, lastLoop)); } //Debugger.Break(); return ret; }
private void m_ReplaceBackEdge(Loop l, GraphNode loopSkip) { foreach (GraphNode gn in l.Cutpoint.LoopingPred) { List<GraphNode> newsuc = new List<GraphNode>(); foreach (GraphNode s in gn.Suc) { if (s == l.Cutpoint) newsuc.Add(loopSkip); else newsuc.Add(s); } gn.Suc = newsuc; } }
private void m_RemoveRegularLoopExit(Loop l) { List<GraphNode> lg = new List<GraphNode>(); lg.AddRange( l.Cutpoint.Suc ); foreach (GraphNode gn in lg) { if (l.LoopExitNodes.Contains(gn)) { l.Cutpoint.RemoveEdgeTo(gn); l.LoopExitNodes.Remove(gn); } } }
private List<GraphNode> m_MarkLoopExitUncheckable(GraphNode g, Loop l) { List<GraphNode> ret = new List<GraphNode>(); if (g.Pre.Count > 1) return ret; ret.Add(g); foreach (GraphNode gn in g.Suc) { ret.AddRange(m_MarkLoopExitUncheckable(gn, l)); } return ret; }
// the loop exit has to be marked uncheckable because otherwise // while(true) would report unreachable code. private void m_MarkLoopExitUncheckable(Loop l) { foreach (GraphNode g in l.Cutpoint.Suc) { if (!l.LoopNodes.Contains(g)) { foreach (GraphNode g_ in m_MarkLoopExitUncheckable(g, l)) { if (!m_GraphAnalyzer.UncheckableNodes.Contains(g_)) m_GraphAnalyzer.UncheckableNodes.Add(g_); } } } }
private void m_AbstractLoopUnrolling(Loop l, Loop parent, string prefix, bool unfold) { //Debugger.Break(); if (unfold) { Loop first = new Loop(l, m_GraphAnalyzer,prefix+"FI_"); Loop last = new Loop(l, m_GraphAnalyzer,prefix+"LA_"); Loop abs = new Loop(l, m_GraphAnalyzer, prefix + "AB_"); foreach (Loop c in first.NestedLoops) m_AbstractLoopUnrolling(c, first, prefix + "FI_", false); foreach (Loop c in last.NestedLoops) m_AbstractLoopUnrolling(c, last, prefix + "LA_", false); foreach (Loop c in abs.NestedLoops) m_AbstractLoopUnrolling(c, abs, prefix + "AB_", true); //Debugger.Break(); if (parent != null) { foreach (GraphNode gn in l.LoopNodes) { if (parent.LoopNodes.Contains(gn)) parent.LoopNodes.Remove(gn); } foreach (GraphNode gn in abs.LoopNodes) { if (!parent.LoopNodes.Contains(gn)) parent.LoopNodes.Add(gn); } foreach (GraphNode gn in first.LoopNodes) { if (!parent.LoopNodes.Contains(gn)) parent.LoopNodes.Add(gn); } foreach (GraphNode gn in last.LoopNodes) { if (!parent.LoopNodes.Contains(gn)) parent.LoopNodes.Add(gn); } } m_HavocLoopBody(abs); List<GraphNode> backupPre = new List<GraphNode>(); backupPre.AddRange(l.Cutpoint.Pre); foreach (GraphNode pre in backupPre) { if (!l.Cutpoint.LoopingPred.Contains(pre)) { pre.RemoveEdgeTo(l.Cutpoint); pre.RemoveEdgeTo(abs.Cutpoint); pre.AddEdgeTo(first.Cutpoint); } } m_RemoveRegularLoopExit(last); m_RemoveRegularLoopExit(abs); m_ReplaceBackEdge(first, abs.Cutpoint); m_ReplaceBackEdge(abs, last.Cutpoint); foreach (GraphNode gn in first.Cutpoint.Suc) { if (!first.LoopNodes.Contains(gn)) { m_ReplaceBackEdge(last, gn); break; } } // Remove all remaining connections to the original loop foreach (GraphNode gn in l.LoopExitNodes) { List<GraphNode> tmp = new List<GraphNode>(); tmp.AddRange(gn.Pre); foreach (GraphNode g in tmp) { if (l.LoopNodes.Contains(g)) { //Debugger.Break(); g.RemoveEdgeTo(gn); } } } foreach (GraphNode gn in l.LoopNodes) { m_GraphAnalyzer.DeleteGraphNode(gn); } foreach (GraphNode gn in first.LoopNodes) { if (gn != first.Cutpoint && !m_GraphAnalyzer.UncheckableNodes.Contains(gn) ) m_GraphAnalyzer.UncheckableNodes.Add(gn); } foreach (GraphNode gn in last.LoopNodes) { if (gn != last.Cutpoint && !m_GraphAnalyzer.UncheckableNodes.Contains(gn)) m_GraphAnalyzer.UncheckableNodes.Add(gn); } MakeLoopExitUncheckable(last.LoopExitNodes); } else { foreach (Loop c in l.NestedLoops) m_AbstractLoopUnrolling(c, l, prefix, false); m_AbstractLoop(l); //MakeLoopExitUncheckable(l.LoopExitNodes); } }