250 likes | 266 Views
Learn about control flow analysis in optimizing compilers, including basic blocks, graph algorithms for loops, reducibility, dominators, and strongly-connected components.
E N D
Optimizing CompilersCISC 673Spring 2009More Control Flow John Cavazos University of Delaware
Clarification • Leaders are • The entry point of the function • Any instruction that is a target of a branch • Any instruction following a (conditional or unconditional) branch
Additional Clarification Tree edge: in CFG & ST Advancing edge: (v,w) not tree edge but w is descendant of v in ST Back edge: (v,w): v=w or w is proper ancestor of v in ST Cross edge: (v,w): w neither ancestor nor descendant of v in ST
Class Problem: Identify Edges procedure DFST (v) pre(v) = vnum++ InStack(v) = true for w in Succ(v) if not InTree(w) add v→w to TreeEdges InTree(w) = true DFST(w) else if pre(v) < pre(w) add v→w to AdvancingEdges else if InStack(w) add v→w to BackEdges else add v→w to CrossEdges InStack(v) = false for v in V do inTree(v) = false vnum = 0 InTree(root) DFST(root)
Class Problem: Answer procedure DFST (v) pre(v) = vnum++ InStack(v) = true for w in Succ(v) if not InTree(w) add v→w to TreeEdges InTree(w) = true DFST(w) else if pre(v) < pre(w) add v→w to AdvancingEdges else if InStack(w) add v→w to BackEdges else add v→w to CrossEdges InStack(v) = false for v in V do inTree(v) = false vnum = 0 InTree(root) DFST(root)
Reducibility • Natural loops: • single entry node: no jumps into middle of loop • requires back edge into loop header (single entry point) • Reducible: hierarchical, “well-structured” • flowgraph reducible iff all loops in it natural
Reducibility Example • Some languages only permit procedures with reducible flowgraphs (e.g., Java) • “GOTO Considered Harmful”:introduces irreducibility • FORTRAN • C • C++ • DFST does not find unique header in irreducible graphs reducible graph irreducible graph
Dominance • Node ddominates node i (“d dom i” )if every path from Entry to i includes d • Reflexive: a dom a • Transitive: if a dom b and b dom c then a dom c • Antisymmetric: if a dom b and b dom a then b=a
Immediate Dominance • aidomb iff a dom bthere is no c such that a dom c, c dom b (c a, c b) • Idom’s: • each node has unique idom • relation forms a dominator tree
Dominance Tree • Immediate and other dominators:(excluding Entry) • a idom b; a dom a, b, c, d, e, f, g • b idom c; b dom b, c, d, e, f, g • c idom d; c dom c, d, e, f, g • d idom e; d dom d, e, f, g • e idom f, e idom g; e dom e, f, g control-flow graph dominator tree
Reducible Graph Test • Graph is reducible iff … all back edges are ones whose head dominates its tail
Why is this not a Reducible Graph? Remember: head must dominate tail of back edge.
Nonreducible Graph Spanning Tree B (head) does not dominate C (tail)
Reducible Graph? Build Spanning Tree Back edges: head must dominate tail
Natural Loops • Now we can find natural loops • Given back edge m → n, natural loop is n (loop header) and nodes that can reach m without passing through n
Natural Loops • Back Edge Natural Loop • J → G {G,H,J} • G → D {D,E,F,G,H,J} • D → C {C,D,E,F,G,H,J} • H → C {C,D,E,F,G,H,J} • I → A {A,B,C,D,E,F,G,H,I,J}
Strongly-Connected Components • What about irreducible flowgraphs? • Most general loop form = strongly-connected component (SCC): • subgraph S such that every node in S reachable from every other node by path including only edges in S • Maximal SCC: • S is maximal SCC if it is the largest SCC that contains S.
SCC Example Entry Maximal strongly-connected component B1 B2 Strongly-connected component B3
Computing Maximal SCCs • Tarjan’s algorithm: • Computes all maximal SCCs • Linear-time (in number of nodes and edges) • CLR algorithm: • Also linear-time • Simpler: • Two depth-first searches and one “transpose”:reverse all graph edge
Conclusion • Introduced control-flow analysis • Basic blocks • Control-flow graphs • Discussed application of graph algorithms: loops • Spanning trees, depth-first spanning trees • Reducibility • Dominators • Dominator tree • Strongly-connected components
Next Time • Dataflow analysis • Read Marlowe and Ryder paper