flow graph theory - github pages · depth of a flow graph the depth of a flow graph is the...

107
CS738: Advanced Compiler Optimizations Flow Graph Theory Amey Karkare [email protected] http://www.cse.iitk.ac.in/~karkare/cs738 Department of CSE, IIT Kanpur

Upload: others

Post on 19-Mar-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

CS738: Advanced Compiler Optimizations

Flow Graph Theory

Amey Karkare

[email protected]

http://www.cse.iitk.ac.in/~karkare/cs738

Department of CSE, IIT Kanpur

Page 2: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Agenda

◮ Speeding up DFA

◮ Depth of a flow graph

◮ Natural Loops

Page 3: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Acknowledgement

Rest of the slides based on the material at

http://infolab.stanford.edu/~ullman/dragon/w06/

w06.html

Page 4: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Speeding up DFA

◮ Proper ordering of nodes of a flow graph speeds up the

iterative algorithms: depth-first ordering.

Page 5: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Speeding up DFA

◮ Proper ordering of nodes of a flow graph speeds up the

iterative algorithms: depth-first ordering.

◮ “Normal” flow graphs have a surprising property —

reducibility — that simplifies several matters.

Page 6: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Speeding up DFA

◮ Proper ordering of nodes of a flow graph speeds up the

iterative algorithms: depth-first ordering.

◮ “Normal” flow graphs have a surprising property —

reducibility — that simplifies several matters.

◮ Outcome: few iterations “normally” needed.

Page 7: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Depth-First Search

◮ Start at entry.

Page 8: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Depth-First Search

◮ Start at entry.

◮ If you can follow an edge to an unvisited node, do so.

Page 9: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Depth-First Search

◮ Start at entry.

◮ If you can follow an edge to an unvisited node, do so.

◮ If not, backtrack to your parent (node from which you were

visited).

Page 10: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Depth-First Spanning Tree (DFST)

◮ Root = Entry.

Page 11: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Depth-First Spanning Tree (DFST)

◮ Root = Entry.

◮ Tree edges are the edges along which we first visit the

node at the head.

Page 12: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

DFST Example

1

2

3

4

5

Page 13: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

DFST Example

1

2

3

4

5

Page 14: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

DFST Example

1

2

3

4

5

Page 15: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

DFST Example

1

2

3

4

5

Page 16: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

DFST Example

1

2

3

4

5

Page 17: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Depth-First Node Order

◮ The reverse of the order in which a DFS retreats from the

nodes.

Page 18: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Depth-First Node Order

◮ The reverse of the order in which a DFS retreats from the

nodes.

◮ Alternatively, reverse of postorder traversal of the tree.

Page 19: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

DF Order Example

1

4

5

2

3

Page 20: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Four Kind of Edges

1. Tree edges.

Page 21: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Four Kind of Edges

1. Tree edges.

2. Forward edges: node to proper descendant.

Page 22: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Four Kind of Edges

1. Tree edges.

2. Forward edges: node to proper descendant.

3. Retreating edges: node to ancestor.

Page 23: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Four Kind of Edges

1. Tree edges.

2. Forward edges: node to proper descendant.

3. Retreating edges: node to ancestor.

4. Cross edges: between two node, niether of which is an

ancestor of the other.

Page 24: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

A Little Magic

◮ Of these edges, only retreating edges go from high to low

in DF order.

Page 25: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

A Little Magic

◮ Of these edges, only retreating edges go from high to low

in DF order.

◮ Most surprising: all cross edges go right to left in theDFST.

Page 26: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

A Little Magic

◮ Of these edges, only retreating edges go from high to low

in DF order.

◮ Most surprising: all cross edges go right to left in theDFST.◮ Assuming we add children of any node from the left.

Page 27: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Non-Tree Edges

1

4

5

2

3

Page 28: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Non-Tree Edges

1

4

5

2

3

Retreating

Page 29: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Non-Tree Edges

1

4

5

2

3

Retreating

Forward

Page 30: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Non-Tree Edges

1

4

5

2

3

Retreating

Forward

Cross

Page 31: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Roadmap

◮ “Normal” flow graphs are “reducible.”

Page 32: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Roadmap

◮ “Normal” flow graphs are “reducible.”

◮ “Dominators” needed to explain reducibility.

Page 33: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Roadmap

◮ “Normal” flow graphs are “reducible.”

◮ “Dominators” needed to explain reducibility.

◮ In reducible flow graphs, loops are well defined, retreating

edges are unique (and called “back” edges).

Page 34: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Roadmap

◮ “Normal” flow graphs are “reducible.”

◮ “Dominators” needed to explain reducibility.

◮ In reducible flow graphs, loops are well defined, retreating

edges are unique (and called “back” edges).

◮ Leads to relationship between DF order and efficient

iterative algorithm.

Page 35: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Dominators

◮ Node d dominates node n if every path from the Entry to

n goes through d .

Page 36: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Dominators

◮ Node d dominates node n if every path from the Entry to

n goes through d .

◮ [Exercise] A forward-intersection iterative algorithm for

finding dominators.

Page 37: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Dominators

◮ Node d dominates node n if every path from the Entry to

n goes through d .

◮ [Exercise] A forward-intersection iterative algorithm for

finding dominators.

◮ Quick observations:

Page 38: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Dominators

◮ Node d dominates node n if every path from the Entry to

n goes through d .

◮ [Exercise] A forward-intersection iterative algorithm for

finding dominators.

◮ Quick observations:◮ Every node dominates itself.

Page 39: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Dominators

◮ Node d dominates node n if every path from the Entry to

n goes through d .

◮ [Exercise] A forward-intersection iterative algorithm for

finding dominators.

◮ Quick observations:◮ Every node dominates itself.◮ The entry dominates every node.

Page 40: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Dominators

1

4

5

2

3

Node Dominators

1

2

3

4

5

Page 41: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Dominators

1

4

5

2

3

Node Dominators

1 1

2

3

4

5

Page 42: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Dominators

1

4

5

2

3

Node Dominators

1 1

2 1, 2

3

4

5

Page 43: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Dominators

1

4

5

2

3

Node Dominators

1 1

2 1, 2

3 1, 2, 3

4

5

Page 44: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Dominators

1

4

5

2

3

Node Dominators

1 1

2 1, 2

3 1, 2, 3

4 1, 4

5

Page 45: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Dominators

1

4

5

2

3

Node Dominators

1 1

2 1, 2

3 1, 2, 3

4 1, 4

5 1, 5

Page 46: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Common Dominator Cases

◮ The test of a while loop dominates all blocks in the loop

body.

Page 47: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Common Dominator Cases

◮ The test of a while loop dominates all blocks in the loop

body.

◮ The test of an if-then-else dominates all blocks in either

branch.

Page 48: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Back Edges

◮ An edge is a back edge if its head dominates its tail.

Page 49: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Back Edges

◮ An edge is a back edge if its head dominates its tail.

◮ Theorem: Every back edge is a retreating edge in everyDFST of every flow graph.

Page 50: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Back Edges

◮ An edge is a back edge if its head dominates its tail.

◮ Theorem: Every back edge is a retreating edge in everyDFST of every flow graph.◮ Proof? Discuss/Exercise

Page 51: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Back Edges

◮ An edge is a back edge if its head dominates its tail.

◮ Theorem: Every back edge is a retreating edge in everyDFST of every flow graph.◮ Proof? Discuss/Exercise◮ Converse almost always true, but not always.

Page 52: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Back Edges

1

4

5

2

3

Node Dominators

1 1

2 1, 2

3 1, 2, 3

4 1, 4

5 1, 5

Page 53: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Back Edges

1

4

5

2

3

Node Dominators

1 1

2 1, 2

3 1, 2, 3

4 1, 4

5 1, 5

Page 54: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Reducible Flow Graphs

◮ A flow graph is reducible if every retreating edge in any

DFST for that flow graph is a back edge.

Page 55: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Reducible Flow Graphs

◮ A flow graph is reducible if every retreating edge in any

DFST for that flow graph is a back edge.

◮ Testing reducibility: Take any DFST for the flow graph,

remove the back edges, and check that the result is

acyclic.

Page 56: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Remove Back Edges

1

4

5

2

3

Node Dominators

1 1

2 1, 2

3 1, 2, 3

4 1, 4

5 1, 5

Page 57: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Remove Back Edges

1

4

5

2

3

Node Dominators

1 1

2 1, 2

3 1, 2, 3

4 1, 4

5 1, 5

Remaining graph is acyclic.

Page 58: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Why Reducibility?

◮ Folk theorem: All flow graphs in practice are reducible.

◮ Fact: If you use only while-loops, for-loops, repeat-loops,

if-then(-else), break, and continue, then your flow graph is

reducible.

Page 59: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Nonreducible Graph

A

B C

Page 60: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Nonreducible Graph

A

B C

In any DFST, one of these

edges will be a retreating

edge.

Page 61: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Nonreducible Graph

A

B C

In any DFST, one of these

edges will be a retreating

edge.

A

B

C

Page 62: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Nonreducible Graph

A

B C

In any DFST, one of these

edges will be a retreating

edge.

A

B

C

A

C

B

Page 63: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Why Care About Back/Retreating Edges?

◮ Proper ordering of nodes during iterative algorithm assures

number of passes limited by the number of “nested” back

edges.

Page 64: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Why Care About Back/Retreating Edges?

◮ Proper ordering of nodes during iterative algorithm assures

number of passes limited by the number of “nested” back

edges.

◮ Depth of nested loops upper-bounds the number of nested

back edges.

Page 65: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

DF Order and Retreating Edges

◮ Suppose that for a RD analysis, we visit nodes during each

iteration in DF order.

Page 66: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

DF Order and Retreating Edges

◮ Suppose that for a RD analysis, we visit nodes during each

iteration in DF order.

◮ The fact that a definition d reaches a block will propagate

in one pass along any increasing sequence of blocks.

Page 67: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

DF Order and Retreating Edges

◮ Suppose that for a RD analysis, we visit nodes during each

iteration in DF order.

◮ The fact that a definition d reaches a block will propagate

in one pass along any increasing sequence of blocks.

◮ When d arrives along a retreating edge, it is too late to

propagate d from OUT to IN.

Page 68: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: DF Order

1

4

5

2

3

Node 2 generates definition d.

Page 69: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: DF Order

1

4

5

2

3

Node 2 generates definition d.

Other nodes “empty” w.r.t. d.

Page 70: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: DF Order

1

4

5

2

3

Node 2 generates definition d.

Other nodes “empty” w.r.t. d.

Does d reach node 4?

Page 71: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: DF Order

1

4

5

2

3

Node 2 generates definition d.

Other nodes “empty” w.r.t. d.

Does d reach node 4?

d

Page 72: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: DF Order

1

4

5

2

3

Node 2 generates definition d.

Other nodes “empty” w.r.t. d.

Does d reach node 4?

d

d

Page 73: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: DF Order

1

4

5

2

3

Node 2 generates definition d.

Other nodes “empty” w.r.t. d.

Does d reach node 4?

d

d

d

Page 74: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: DF Order

1

4

5

2

3

Node 2 generates definition d.

Other nodes “empty” w.r.t. d.

Does d reach node 4?

d

d

d

d

Page 75: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: DF Order

1

4

5

2

3

Node 2 generates definition d.

Other nodes “empty” w.r.t. d.

Does d reach node 4?

d

d

d

d

d

Page 76: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: DF Order

1

4

5

2

3

Node 2 generates definition d.

Other nodes “empty” w.r.t. d.

Does d reach node 4?

d

d

d

d

d

d

Page 77: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: DF Order

1

4

5

2

3

Node 2 generates definition d.

Other nodes “empty” w.r.t. d.

Does d reach node 4?

d

d

d

d

d

d

d

Page 78: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: DF Order

1

4

5

2

3

Node 2 generates definition d.

Other nodes “empty” w.r.t. d.

Does d reach node 4?

d

d

d

d

d

d

d

d

Page 79: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: DF Order

1

4

5

2

3

Node 2 generates definition d.

Other nodes “empty” w.r.t. d.

Does d reach node 4?

d

d

d

d

d

d

d

dd

Page 80: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: DF Order

1

4

5

2

3

Node 2 generates definition d.

Other nodes “empty” w.r.t. d.

Does d reach node 4?

d

d

d

d

d

d

d

dd

d

Page 81: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Depth of a Flow Graph

◮ The depth of a flow graph is the greatest number of

retreating edges along any acyclic path.

Page 82: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Depth of a Flow Graph

◮ The depth of a flow graph is the greatest number of

retreating edges along any acyclic path.

◮ For RD, if we use DF order to visit nodes, we converge indepth+2 passes.

Page 83: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Depth of a Flow Graph

◮ The depth of a flow graph is the greatest number of

retreating edges along any acyclic path.

◮ For RD, if we use DF order to visit nodes, we converge indepth+2 passes.◮ Depth+1 passes to follow that number of increasing

segments.

Page 84: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Depth of a Flow Graph

◮ The depth of a flow graph is the greatest number of

retreating edges along any acyclic path.

◮ For RD, if we use DF order to visit nodes, we converge indepth+2 passes.◮ Depth+1 passes to follow that number of increasing

segments.◮ 1 more pass to realize we converged.

Page 85: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Depth = 2

Page 86: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Depth = 2

increasing

Page 87: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Depth = 2

increasing

retreating

Page 88: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Depth = 2

increasing

retreating

increasing

Page 89: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Depth = 2

increasing

retreating

increasing

retreating

Page 90: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Depth = 2

increasing

retreating

increasing

retreating

increasing

Page 91: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Similarly . . .

◮ AE also works in depth+2 passes.

Page 92: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Similarly . . .

◮ AE also works in depth+2 passes.◮ Unavailability propagates along retreat-free node

sequences in one pass.

Page 93: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Similarly . . .

◮ AE also works in depth+2 passes.◮ Unavailability propagates along retreat-free node

sequences in one pass.

◮ So does LV if we use reverse of DF order.

Page 94: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Similarly . . .

◮ AE also works in depth+2 passes.◮ Unavailability propagates along retreat-free node

sequences in one pass.

◮ So does LV if we use reverse of DF order.◮ A use propagates backward along paths that do not use a

retreating edge in one pass.

Page 95: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

In General . . .

◮ The depth+2 bound works for any monotone bit-vectorframework, as long as information only needs to propagatealong acyclic paths.◮ Example: if a definition reaches a point, it does so along an

acyclic path.

Page 96: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Why Depth+2 is Good?

◮ Normal control-flow constructs produce reducible flowgraphs with the number of back edges at most the nestingdepth of loops.◮ Nesting depth tends to be small.

Page 97: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Nested Loops

3 nested while loops.

Page 98: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Nested Loops

3 nested while loops.

depth = 3.

Page 99: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Nested Loops

3 nested while loops.

depth = 3.

3 nested do-while loops.

Page 100: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Nested Loops

3 nested while loops.

depth = 3.

3 nested do-while loops.

depth = 1.

Page 101: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Natural Loops

◮ The natural loop of a back edge a → b is {b} plus the set

of nodes that can reach a without going through b.

Page 102: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Natural Loops

◮ The natural loop of a back edge a → b is {b} plus the set

of nodes that can reach a without going through b.

◮ Theorem: two natural loops are either disjoint, identical, or

nested.

Page 103: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Natural Loops

◮ The natural loop of a back edge a → b is {b} plus the set

of nodes that can reach a without going through b.

◮ Theorem: two natural loops are either disjoint, identical, or

nested.

◮ Proof: Discuss/Exercise

Page 104: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Natural Loops

1

4

5

2

3

1

4

5

2

3

Page 105: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Natural Loops

1

4

5

2

3

1

4

5

2

3

Natural loop 3 → 2

Page 106: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Example: Natural Loops

1

4

5

2

3

1

4

5

2

3

Natural loop 3 → 2

Natural loop 5 → 1

Page 107: Flow Graph Theory - GitHub Pages · Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. For RD, if we use DF order

Reading Assignment

◮ New Dragon Book (Aho, Lam, Sethi, Ullman)◮ Chapter 9