Presentation is loading. Please wait.

Presentation is loading. Please wait.

School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.

Similar presentations


Presentation on theme: "School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao."— Presentation transcript:

1 School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao

2 2 Fall 2011 “Advanced Compiler Techniques” Content Concepts: Concepts: Dominators Dominators Depth-First Ordering Depth-First Ordering Back edges Back edges Graph depth Graph depth Reducibility Reducibility Natural Loops Natural Loops Efficiency of Iterative Algorithms Efficiency of Iterative Algorithms

3 3 Fall 2011 “Advanced Compiler Techniques” Loops are Important! Loops dominate program execution time Loops dominate program execution time Needs special treatment during optimization Needs special treatment during optimization Loops also affect the running time of program analyses Loops also affect the running time of program analyses e.g., A dataflow problem can be solved in just a single pass if a program has no loops e.g., A dataflow problem can be solved in just a single pass if a program has no loops

4 4 Fall 2011 “Advanced Compiler Techniques” Dominators Node d dominates node n if every path from the entry to n goes through d. Node d dominates node n if every path from the entry to n goes through d. written as: d dom n written as: d dom n Quick observations: Quick observations: u Every node dominates itself. u The entry dominates every node. Common Cases: Common Cases: The test of a while loop dominates all blocks in the loop body. 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. The test of an if-then-else dominates all blocks in either branch.

5 5 Fall 2011 “Advanced Compiler Techniques” Example: Dominators 1 3 5 2 4

6 6 Fall 2011 “Advanced Compiler Techniques” Dominator Tree Immediate dominance: d idom n  d dom n, d  n, no m s.t. d dom m and m dom n Immediate dominance relationships form a tree 1 3 5 2 4 1 3 5 2 4

7 7 Fall 2011 “Advanced Compiler Techniques” Finding Dominators A dataflow analysis problem: For each node, find all of its dominators. A dataflow analysis problem: For each node, find all of its dominators. Direction: forward Direction: forward Confluence: set intersection Confluence: set intersection Boundary: OUT[Entry] = {Entry} Boundary: OUT[Entry] = {Entry} Initialization: OUT[B] = All nodes Initialization: OUT[B] = All nodes Equations: Equations: OUT[B] = IN[B] U {B} OUT[B] = IN[B] U {B} IN[B] =  p is a predecessor of B OUT[p] IN[B] =  p is a predecessor of B OUT[p]

8 8 Fall 2011 “Advanced Compiler Techniques” Example: Dominators 1 3 5 2 4 {1,5} {1,4} {1,2,3} {1,2} {1}

9 9 Fall 2011 “Advanced Compiler Techniques” Depth-First Search Start at entry. Start at entry. If you can follow an edge to an unvisited node, do so. If you can follow an edge to an unvisited node, do so. If not, backtrack to your parent (node from which you were visited). If not, backtrack to your parent (node from which you were visited).

10 10 Fall 2011 “Advanced Compiler Techniques” Depth-First Spanning Tree Root = entry. Root = entry. Tree edges are the edges along which we first visit the node at the head. Tree edges are the edges along which we first visit the node at the head. 1 5 3 4 2

11 11 Fall 2011 “Advanced Compiler Techniques” Depth-First Node Order The reverse of the order in which a DFS retreats from the nodes. The reverse of the order in which a DFS retreats from the nodes. Alternatively, reverse of postorder traversal of the tree. Alternatively, reverse of postorder traversal of the tree.

12 12 Fall 2011 “Advanced Compiler Techniques” Example: DF Order 1 3 5 2 4

13 13 Fall 2011 “Advanced Compiler Techniques” Four Kinds of Edges 1. Tree edges. 2. Advancing edges (node to proper descendant). 3. Retreating edges (node to ancestor, including edges to self). 4. Cross edges (between two nodes, neither of which is an ancestor of the other.

14 14 Fall 2011 “Advanced Compiler Techniques” A Little Magic Of these edges, only retreating edges go from high to low in DF order. Of these edges, only retreating edges go from high to low in DF order. Most surprising: all cross edges go right to left in the DFST. Most surprising: all cross edges go right to left in the DFST. Assuming we add children of any node from the left. Assuming we add children of any node from the left.

15 15 Fall 2011 “Advanced Compiler Techniques” Example: Non-Tree Edges 1 3 5 2 4 Retreating Forward Cross

16 16 Fall 2011 “Advanced Compiler Techniques” Back Edges An edge is a back edge if its head dominates its tail. An edge is a back edge if its head dominates its tail. Theorem: Every back edge is a retreating edge in every DFST of every flow graph. Theorem: Every back edge is a retreating edge in every DFST of every flow graph. Converse almost always true, but not always. Converse almost always true, but not always.

17 17 Fall 2011 “Advanced Compiler Techniques” Example: Back Edges 1 3 5 2 4 {1,5} {1,4} {1,2,3} {1,2} {1}

18 18 Fall 2011 “Advanced Compiler Techniques” Reducible Flow Graphs A flow graph is reducible if every retreating edge in any DFST for that flow graph is a back edge. 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. Testing reducibility: Take any DFST for the flow graph, remove the back edges, and check that the result is acyclic.

19 19 Fall 2011 “Advanced Compiler Techniques” Example: Remove Back Edges 1 3 5 2 4

20 20 Fall 2011 “Advanced Compiler Techniques” Example: Remove Back Edges 1 3 5 2 4 Remaining graph is acyclic.

21 21 Fall 2011 “Advanced Compiler Techniques” Why Reducibility? Folk theorem: All flow graphs in practice are reducible. 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. Fact: If you use only while-loops, for- loops, repeat-loops, if-then(-else), break, and continue, then your flow graph is reducible.

22 22 Fall 2011 “Advanced Compiler Techniques” Example: Nonreducible Graph A CB In any DFST, one of these edges will be a retreating edge. A B C A B C

23 23 Fall 2011 “Advanced Compiler Techniques” Why Care About Back/Retreating Edges? 1. Proper ordering of nodes during iterative algorithm assures number of passes limited by the number of “ nested ” back edges. 2. Depth of nested loops upper-bounds the number of nested back edges.

24 24 Fall 2011 “Advanced Compiler Techniques” DF Order and Retreating Edges Suppose that for a Reaching Definitions analysis, we visit nodes during each iteration in DF order. Suppose that for a Reaching Definitions 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. 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. When d arrives along a retreating edge, it is too late to propagate d from OUT to IN.

25 25 Fall 2011 “Advanced Compiler Techniques” Example: DF Order 1 3 5 2 4 d d d d d d d d d d Suppose there is a definition of d in Block 2.

26 26 Fall 2011 “Advanced Compiler Techniques” Depth of a Flow Graph The depth of a flow graph is the greatest number of retreating edges along any acyclic path. 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 in depth+2 passes. For RD, if we use DF order to visit nodes, we converge in depth+2 passes. Depth+1 passes to follow that number of increasing segments. Depth+1 passes to follow that number of increasing segments. 1 more pass to realize we converged. 1 more pass to realize we converged.

27 27 Fall 2011 “Advanced Compiler Techniques” Example: Depth = 2 increasing retreating increasing retreating 1 -> 4 ->7 - - -> 3 -> 10 ->17 - - -> 6 -> 18 -> 20

28 28 Fall 2011 “Advanced Compiler Techniques” Similarly... AE also works in depth+2 passes. AE also works in depth+2 passes. Unavailability propagates along retreat- free node sequences in one pass. Unavailability propagates along retreat- free node sequences in one pass. So does LV if we use reverse of DF order. 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. A use propagates backward along paths that do not use a retreating edge in one pass.

29 29 Fall 2011 “Advanced Compiler Techniques” In General... The depth+2 bound works for any monotone framework, as long as information only needs to propagate along acyclic paths. The depth+2 bound works for any monotone framework, as long as information only needs to propagate along acyclic paths. Example: if a definition reaches a point, it does so along an acyclic path. Example: if a definition reaches a point, it does so along an acyclic path.

30 30 Fall 2011 “Advanced Compiler Techniques” However... Constant propagation does not have this property. Constant propagation does not have this property. a = b b = c c = 1 L: a = b b = c c = 1 goto L

31 31 Fall 2011 “Advanced Compiler Techniques” Why Depth+2 is Good Normal control-flow constructs produce reducible flow graphs with the number of back edges at most the nesting depth of loops. Normal control-flow constructs produce reducible flow graphs with the number of back edges at most the nesting depth of loops. Nesting depth tends to be small. Nesting depth tends to be small. A study by Knuth has shown that average depth of typical flow graphs =~2.75. A study by Knuth has shown that average depth of typical flow graphs =~2.75.

32 32 Fall 2011 “Advanced Compiler Techniques” Example: Nested Loops 3 nested while- loops; depth = 3. 3 nested repeat- loops; depth = 1

33 33 Fall 2011 “Advanced Compiler Techniques” Natural Loops A natural loop is defined by: A natural loop is defined by: A single entry-point called header a header dominates all nodes in the loop A back edge that enters the loop header Otherwise, it is not possible for the flow of control to return to the header directly from the "loop" ; i.e., there really is no loop.

34 34 Fall 2011 “Advanced Compiler Techniques” Find 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. The natural loop of a back edge a->b is {b} plus the set of nodes that can reach a without going through b. Remove b from the flow graph, find all predecessors of a Theorem: two natural loops are either disjoint, identical, or nested. Theorem: two natural loops are either disjoint, identical, or nested.

35 35 Fall 2011 “Advanced Compiler Techniques” Example: Natural Loops 1 3 5 2 4 Natural loop of 3 -> 2 Natural loop of 5 -> 1

36 36 Fall 2011 “Advanced Compiler Techniques” Relationship b/w Loops If two loops do not have the same header they are either disjoint, or one is entirely contained (nested within) the other innermost loop: one that contains no other loop. If two loops share the same header Hard to tell which is the inner loop Combine as one 1 2 3 4

37 37 Fall 2011 “Advanced Compiler Techniques” Next Time Single Static Assignment (SSA) Single Static Assignment (SSA) Readings: Cytron'91, Chow'97 Readings: Cytron'91, Chow'97


Download ppt "School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao."

Similar presentations


Ads by Google