optimizing compilers cisc 673 spring 2009 more control flow
DESCRIPTION
Optimizing Compilers CISC 673 Spring 2009 More 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. - PowerPoint PPT PresentationTRANSCRIPT
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT
Optimizing CompilersCISC 673
Spring 2009More Control Flow
John CavazosUniversity of Delaware
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 2
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
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 3
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
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 4
Class Problem: Identify Edgesprocedure 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 = 0InTree(root)DFST(root)
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 5
Class Problem: Answerprocedure 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 = 0InTree(root)DFST(root)
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 6
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
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 7
Reducibility Example
A
B
C
D
E
F G
Entry
A
B
C
D
E
F G
Entry
reducible graph irreducible graph
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
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 8
Dominance Node d dominates 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
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 9
Immediate Dominance
a idom b iff a dom b there 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
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 10
Dominance Tree
A
B
C
D
E
F G
Entry
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
A
B
C
D
E
F G
Entry
control-flow graph dominator tree
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 11
Dominator Tree?
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 12
Dominator Tree
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 13
Reducible Graph Test
Graph is reducible iff …all back edges are ones whose
head dominates its tail
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 14
Why is this not a Reducible Graph?
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 15
Why is this not a Reducible Graph?
Remember: head must dominate tail of back edge.
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 16
Nonreducible Graph
Spanning TreeB (head) does not dominate C
(tail)
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 17
Reducible Graph?
Build Spanning TreeBack edges: head must dominate tail
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 18
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
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 19
Find Natural Loops?
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 20
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}
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 21
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.
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 22
SCC Example
Entry
B1
B2
B3
Strongly-connected component
Maximal strongly-connected component
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 23
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
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 24
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
UUNIVERSITYNIVERSITY OFOF D DELAWARE ELAWARE • • C COMPUTER & OMPUTER & IINFORMATION NFORMATION SSCIENCES CIENCES DDEPARTMENTEPARTMENT 25
Next Time
Dataflow analysis Read Marlowe and Ryder paper