1 Region-Based Data Flow Analysis
2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving the performance of loops can have a significant impact If a program does not contain any loops, we can obtain the answers to data-flow problems by making just one pass through the program
3 Dominators A node n 1 dominates a node n 2, written n 1 dom n 2, if every path from the ENTRY node of the flow graph to n 2 goes through n 1 Every node dominates itself
4 An Example dom {10} 9 dom {9} 8 dom {8,9,10} 7 dom {7,8,9,10} 6 dom {6} 5 dom {5} 4 dom {4,5,6,7,8,9,10} 3 dom {3,4,5,6,7,8,9,10} 2 dom {2} 1 dom {1,2,3,4,5,6,7,8,9,10}
5 Dominator Trees The dom relation between the nodes of a flow graph can be represented as a tree, called the dominator tree The ENTRY node of the graph is the root of the tree, and each node n dominates only its descendants in the tree Each node n has a unique immediate dominator m that is the last dominator of n on any path from the ENTRY node to n
6 An Example
7 Finding Dominators If p 1, p 2, …, p k are all the predecessors of n, and n d, then d dom n iff d dom p i for each i
8 Finding Dominators Direction Forwards Transfer f B ( x ) = x { B } function Boundary OUT[ENTRY] = { ENTRY } Meet( ) Equations OUT[ B ] = f B (IN[ B ]) IN[ B ] = P, pred ( B ) OUT[ P ] Initialization OUT[ B ] = N (set of nodes)
9 An Example D(1) = {1} D(2) = {1,2} D(3) = {1,3} D(4) = {1,3,4} D(5) = {1,3,4,5} D(6) = {1,3,4,6} D(7) = {1,3,4,7} D(8) = {1,3,4,7,8} D(9) = {1,3,4,7,8,9} D(10) = {1,3,4,7,8,10}
10 Depth-First Ordering A depth-first traversal of a graph starts at the ENTRY node and visits nodes as far away from the ENTRY node as quickly as possible The route of a depth-first traversal forms a tree called a depth-first spanning tree (DEST) of the graph A depth-first ordering of the nodes in a graph is the reverse of the postorder traversal
11 An Example , 9, 8, 7, 6, 5, 4, 3, 2, 1
12 Depth-First Traversal Algorithm void search( n ) { mark n “visited”; for (each successor s of n ) if ( s is “unvisited”) { add edge n s to T ; search( s ); } dfn [ n ] = c ; c = c – 1 ; } main( ) { T = ; for (each node n of G ) mark n “unvisited”; c = number of nodes of G ; search( n 0 ); }
13 Edges in a Depth-First Spanning Tree An advancing edge goes from a node m to a descendant of m in the tree A retreating edge goes from a node m to an ancestor of m in the tree A cross edge is an edge m n such that neither m nor n is an ancestor of the other in the tree
14 An Example
15 Back Edges A back edge is an edge a b whose head b dominates its tail a For any flow graph, every back edge is retreating, but not every retreating edge is a back edge
16 An Example
17 Reducibility A flow graph is said to be reducible if all its retreating edges in any DFST are also back edges If a flow graph is nonreducible, all the back edges are retreating edges in any DFST, but each DFST may have additional retreating edges that are not back edges. A flow graph is reducible if after removing all the back edges, the remaining graph is acyclic
18 A Reducible Example
19 A Nonreducible Example 1 23
20 Depth of a Flow Graph Given a DFST for a flow graph, the depth of the flow is the largest number of retreating edges on any cycle-free path
21 An Example 7 4 3 depth is 3
22 Natural Loops A natural loop is defined by two properties: It must have a single-entry node, called the header. This entry node dominates all nodes in the loop There must be a back edge that enters the loop header Given a back edge n d, we define the natural loop of the edge to be d plus the set of nodes that can reach n without going through d. Node d is the header of the loop.
23 Constructing Natural Loops void insert(m) { if (m is not in loop) { loop = loop {m}; push m onto S; } main(n d) { S = empty; loop = {d}; insert(n); while (S is not empty) { pop m off S; for (each predecessor p of m) insert(p); }
24 An Example 7:{7,8,10} 7 4 :{4,5,6,7,8,10} 4 3 :{3,4,5,6,7,8,10} 8 3 :{3,4,5,6,7,8,10} 9 1 :{1,2,3,4,5,6,7,8,9,10}
25 Innermost Loops Unless two loops have the same header, they are either disjoint or one is nested within the other When two loops have the same header, but neither is nested within the other, they are combined and treated as a single loop An innermost loop is one that contains no other loops
26 An Example B0B0 B1B1 B2B2 B3B3
27 Speed of Convergence of Iterative Data-Flow Algorithms The maximum number of iterations the algorithm may take is the product of the height of the lattice and the number of nodes in the flow graph It is possible to order the evaluation such that the algorithm converges in a much smaller number of iterations
28 Speed of Convergence of Iterative Data-Flow Algorithms If all useful information propagates along acyclic paths, we have an opportunity to tailor the order in which we visit nodes in iterative data-flow algorithms, so that after relatively few passes through the nodes we can be sure information has passed along all the acyclic paths
29 Information Propagation In reaching definitions, if a definition d is in IN[ B ], then there is some acyclic path from the block containing d to B such that d is in the IN’s and OUT’s all along that path In constant propagation, information may propagate through cyclic paths L: a = b b = c c = 1 goto L
30 Using Depth-First Order Process the basic blocks in the flow graph in depth-first order The number of iterations needed is no more than two greater than the depth 3 5 19 35 16 23 45 4 10 17
31 Region-Based Analysis In the iterative analysis, we create transfer functions for basic blocks, then find the fixedpoint solution of data-flow values by repeated passes over the blocks A region-based analysis finds transfer functions that summarize the execution of progressively larger regions of the program Ultimately, transfer functions for entire procedures are constructed and then applied to get the desired data-flow values directly
32 Regions A region of a flow graph is a collection of nodes N and edges E such that There is a header h in N that dominates all the nodes in N If some node m can reach a node n in N without going through h, then m is also in N E is the set of all the control flow edges between nodes n 1 and n 2 in N, except (possibly) for some that enter h
33 An Example B1B1 B2B2 B3B3 B4B4 N: {B 1, B 2 }, E: {B 1 B 2 }. N: {B 1, B 2, B 3 }, E: {B 1 B 2, B 2 B 3, B 1 B 3 }. N: {B 2, B 3 }, E: {B 2 B 3 }.
34 Region Hierarchies First, every block is a region by itself. These regions are called leaf regions Then we order the natural loops from the inside out. We replace each loop by a node in two steps: The body of the loop is replaced by a node. This node is called a body region The body region and the loop back edge are replaced by a node. This node is called a loop region
35 An Example d 1 : i = m - 1 d 2 : j = n d 3 : a = u1 d 4 : i = i + 1 d 5 : a = u2 B1B1 B2B2 B3B3 B4B4 d 6 : j = u3 B5B5
36 An Example R1R1 R2R2 R4R4 R5R5 R3R3 R1R1 R6R6 R5R5 R1R1 R7R7 R5R5 R8R8
37 Overview of Region-Based Analysis This is a two-pass analysis. The first pass computes transfer functions bottom-up. The second pass computes data-flow values top- down We first consider the bottom-up pass For each region R, and for each subregion R’ within R, we compute a transfer function f R,IN[ R ’] that summarizes the effect of executing all possible paths leading from the entry of R to the entry of R’, while staying within R
38 Overview of Region-Based Analysis We say that a block B within R is an exit block of region R if it has an outgoing edge to some block outside R We also compute a transfer function for each exit block B of R, denoted f R,OUT[ B ], that summarizes the effect of executing all possible paths within R, leading from the entry of R to the exit of B
39 Overview of Region-Based Analysis We begin with regions that are single blocks, where f B,IN[ B ] is just the identity function I and f B,OUT[ B ] is the transfer function f B for the block B itself We then proceed up the region hierarchy, computing transfer functions for progressively larger regions Eventually, we reach the top of the hierarchy and compute the transfer functions for the top region R n that is the entire flow graph
40 Overview of Region-Based Analysis If R is a body region, then the edges belonging to R form an acyclic graph on the subregions of R. for (each subregion S immediately contained in R, in topological order) { f R,IN[ S ] = B pred ( S ) f R,OUT[ B ] ; for (each exit block B in S) f R,OUT[ B ] = f S,OUT[ B ] 。 f R,IN[ S ] ; }
41 Overview of Region-Based Analysis If R is a loop region, then we only need to account for the effect of the back edges to the header of R. let S be the body region immediately contained in R ; f R,IN[ S ] = ( B pred ( S ) f S,OUT[ B ] )*; for (each exit block B in R ) f R,OUT[ B ] = f S,OUT[ B ] 。 f R,IN[ S ] ;
42 Overview of Region-Based Analysis We now consider the top-down pass For each region, we compute the data-flow values at the entry For region R n, IN[ R n ] = IN[ENTRY] For each subregion R in R n, IN[ R ] = f R n,IN[ R ] (IN[ R n ]) We repeat until we reach the basic block at the leaves of the region hierarchy
43 Necessary Assumptions about Transfer Functions For region-based analysis to work, we need three primitive operations on transfer functions: composition, meet, and closure Let f 1 and f 2 be transfer functions of nodes n 1 and n 2. The effect of executing n 1 followed by n 2 is represented by f 2 。 f 1 f 2 。 f 1 ( x ) = gen 2 (( gen 1 ( x – kill 1 )) – kill 2 ) = ( gen 2 ( gen 1 – kill 2 )) ( x – ( kill 1 kill 2 ))
44 Necessary Assumptions about Transfer Functions The meet of two transfer functions f 1 and f 2, f 1 f f 2, is defined by ( f 1 f f 2 )( x ) = f 1 ( x ) f 2 ( x ) ( f 1 f f 2 )( x ) = f 1 ( x ) f 2 ( x ) = ( gen 1 ( x – kill 1 )) ( gen 2 ( x – kill 2 )) = ( gen 1 gen 2 ) ( x – ( kill 1 kill 2 ))
45 Necessary Assumptions about Transfer Functions If f represents the transfer function of a cycle. Assume that the cycle may be executed 0 or more times. The closure of f is defined by f* = n 0 f n or f* = I ( n>0 f n ) f 2 (x) = f (f (x)) = gen (( gen ( x – kill )) – kill ) = gen ( x – kill ) = f 1 (x) f*(x) = I f 1 (x) f 2 (x) … = x ( gen ( x – kill )) = gen x
46 An Example d 1 : i = m - 1 d 2 : j = n d 3 : a = u1 d 4 : i = i + 1 d 5 : a = u2 B1B1 B2B2 B3B3 B4B4 d 6 : j = u3 B5B5 gen B, kill B : B 1 :{d 1,d 2,d 3 }, {d 4,d 5,d 6 }. B 2 :{d 4 }, {d 1 }. B 3 :{d 5 }, {d 3 }. B 4 :{d 6 }, {d 2 }. B 5 : , .
47 An Example R1R1 R2R2 R4R4 R5R5 R3R3 For R i, 1 i 5, f R i,IN[ B i ] ( x ) = I, f R i,OUT[ B i ] ( x ) = gen B i ( x – kill B i )
48 An Example R1R1 R2R2 R4R4 R5R5 R3R3 R6R6 Transfer Function genkill f R 6,IN[ R 2 ] = I f R 6,OUT[ B 2 ] = f R 2,OUT[ B 2 ] 。 f R 6,IN[ R 2 ] { d 4 } { d 1 } f R 6,IN[ R 3 ] = f R 6,OUT[ B 2 ] { d 4 } { d 1 } f R 6,OUT[ B 3 ] = f R 3,OUT[ B 3 ] 。 f R 6,IN[ R 3 ] { d 4, d 5 } { d 1, d 3 } f R 6,IN[ R 4 ] = f R 6,OUT[ B 2 ] f R 6,OUT[ B 3 ] { d 4, d 5 } { d 1 } f R 6,OUT[ B 4 ] = f R 4,OUT[ B 4 ] 。 f R 6,IN[ R 4 ] { d 4, d 5, d 6 } { d 1, d 2 }
49 An Example R1R1 R6R6 R5R5 R7R7 Transfer Function genkill f R 7,IN[ R 6 ] = f* R 6,OUT[ B 4 ] { d 4, d 5, d 6 } f R 7,OUT[ B 3 ] = f R 6,OUT[ B 3 ] 。 f R 7,IN[ R 6 ] { d 4, d 5, d 6 } { d 1, d 3 } f R 7,OUT[ B 4 ] = f R 6,OUT[ B 4 ] 。 f R 7,IN[ R 6 ] { d 4, d 5, d 6 } { d 1, d 2 }
50 An Example R1R1 R7R7 R5R5 R8R8 Transfer Function gen kill f R 8,IN[ R 1 ] = I f R 8,OUT[ B 1 ] = f R 1,OUT[ B 1 ] { d 1, d 2, d 3 } { d 4, d 5, d 6 } f R 8,IN[ R 7 ] = f R 8,OUT[ B 1 ] { d 1, d 2, d 3 } { d 4, d 5, d 6 } f R 8,OUT[ B 3 ] = f R 7,OUT[ B 3 ] 。 f R 8,IN[ R 7 ] { d 2, d 4, d 5, d 6 } { d 1, d 3 } f R 8,OUT[ B 4 ] = f R 7,OUT[ B 4 ] 。 f R 8,IN[ R 7 ] { d 3, d 4, d 5, d 6 } { d 1, d 2 } f R 8,IN[ R 5 ] = f R 8,OUT[ B 3 ] f R 8,OUT[ B 4 ] { d 2, d 3, d 4, d 5, d 6 } { d 1 } f R 8,OUT[ B 5 ] = f R 5,OUT[ B 5 ] 。 f R 8,IN[ R 5 ] { d 2, d 3, d 4, d 5, d 6 } { d 1 }
51 An Example IN[ R 8 ] = IN[ R 1 ] = f R 8,IN[ R1 ] (IN[ R 8 ]) = IN[ R 7 ] = f R 8,IN[ R7 ] (IN[ R 8 ]) = { d 1, d 2, d 3 } IN[ R 5 ] = f R 8,IN[ R5 ] (IN[ R 8 ]) = { d 2, d 3, d 4, d 5, d 6 } IN[ R 6 ] = f R 7,IN[ R6 ] (IN[ R 7 ]) = { d 1, d 2, d 3, d 4, d 5, d 6 } IN[ R 4 ] = f R 6,IN[ R4 ] (IN[ R 6 ]) = { d 2, d 3, d 4, d 5, d 6 } IN[ R 3 ] = f R 6,IN[ R3 ] (IN[ R 6 ]) = { d 2, d 3, d 4, d 5, d 6 } IN[ R 2 ] = f R 6,IN[ R2 ] (IN[ R 6 ]) = { d 1, d 2, d 3, d 4, d 5, d 6 }
52 Handling Nonreducible Graphs A node n has k predecessors can be splitted by replacing n by k nodes n 1, n 2, …, n k. The ith predecessor of n becomes the predecessor of n i only, while all successors of n become successors of all of the n i ’s a 3 2b
53 Handling Nonreducible Graphs When we split a node, the transfer functions for the nodes in the region must be duplicated When we compute the in’s, the in’s is computed by taking the meet of the in’s of all its representatives. We compute the out’s similarly