Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 201 Compiler Construction

Similar presentations


Presentation on theme: "CS 201 Compiler Construction"— Presentation transcript:

1 CS 201 Compiler Construction
Control Flow Analysis

2 What is a loop ? A subgraph of CFG with the following properties:
Strongly Connected: there is a path from any node in the loop to any other node in the loop; and Single Entry: there is a single entry into the loop from outside the loop. The entry node of the loop is called the loop header. Loop nodes: 2, 3, 5 Header node: 2 Loop back edge: 52 TailHead

3 Property Given two loops: they are either disjoint or one is completely nested within the other. 1 2 3 4 5 5555 6 Loops {1,2,4} and {5,6} are Disjoint. Loop {5,6} is nested within loop {2,4,5,6}. Loop {5,6} is nested within loop {1,2,3,4,5,6}.

4 Identifying Loops Definitions:
Dominates: node n dominates node m iff all paths from start node to node m pass through node n, i.e. to visit node m we must first visit node n. A loop has A single entry  the entry node dominates all nodes in the loop; and A back edge, and edge AB such that B dominates A. B is the head & A is the tail.

5 Identifying Loops Algorithm for finding loops:
Compute Dominator Information. Identify Back Edges. Construct Loops corresponding to Back Edges.

6 Dominators: Characteristics
Every node dominates itself. Start node dominates every node in the flow graph. If N DOM M and M DOM R then N DOM R. If N DOM M and O DOM M then either N DOM O or O DOM N Set of dominators of a given node can be linearly ordered according to dominator relationships.

7 Dominators: Characteristics
6. Dominator information can be represented by a Dominator Tree. Edges in the dominator tree represent immediate dominator relationships. CFG Dominator Tree 1 is the immediate dominator of 2, 3 & 4

8 Computing Dominator Sets
Observation: node m donimates node n iff m dominates all predecessors of n. Let D(n) = set of dominators of n Where Pred(n) is set of immediate predecessors of n in the CFG

9 Computing Dominator Sets
Initial Approximation: D(no) = {no} no is the start node. D(n) = N, for all n!=no N is set of all nodes. Iteratively Refine D(n)’s: Algorithm:

10 Example: Computing Dom. Sets
D(2) = {2} U D(1) = {1,2} D(3) = {3} U D(1) = {1,3} D(4) = {4} U (D(2) D(3) D(9)) = {1,4} D(5) = {5} U (D(4) D(10)) = {1,4,5} D(6) = {6} U (D(5) D(7)) = {1,4,5,6} D(7) = {7} U D(5) = {1,4,5,7} D(8) = {8} U (D(6) D(10)) = {1,4,5,6,8} D(9) = {9} U D(8) = {1,4,5,6,8,9} D(10)= {10} U D(8) = {1,4,5,6,8,10} Back Edges: 94, 108, 105

11 Loop Given a back edge N  D Loop corresponding to edge N  D = {D} + {X st X can reach N without going through D} 1 dominates 6 61 is a back edge Loop of 61 = {1} + {3,4,5,6} = {1,3,4,5,6}

12 Algorithm for Loop Construction
Given a Back Edge ND Stack = empty Loop = {D} Insert(N) While stack not empty do pop m – top element of stack for each p in pred(m) do Insert(p) endfor Endwhile Insert(m) if m not in Loop then Loop = Loop U {m} push m onto Stack endif End Insert

13 Example Back Edge 72 D N Stack - 7 6 4 4,5 4,3 Loop 2 2,7 2,7,6
2,7,6,4 2,7,6,4,5 2,7,6,4,5,3 Insert(7) Pop; Insert(6) Pop; Insert(4) Insert(5) Pop; Insert(3) Pop; Insert(2)

14 Examples ? While A do S1 While B do S2 Endwhile L2  B, S2
L1  A,S1,B,S2 L2 nested in L1 L1  S1,S2,S3,S4 L2  S2,S3,S4 L2 nested in L1 ?

15 Reducible Flow Graph The edges of a reducible flow graph can be partitioned into two disjoint sets: Forward – from an acyclic graph in which every node can be reached from the initial node. Back – edges whose heads (sink) dominate tails (source). Any flow graph that cannot be partitioned as above is a non-reducible or irreducible.

16 Reducible Flow Graph How to check reducibility ?
Remove all back edges and see if the resulting graph is acyclic. Reducible D N 8 4 5 3 2 1 7 6 Irreducible Node Splitting 23 not a back edge 32 not a back edge graph is not acyclic Converts irreducible to reducible

17 Loop Detection in Reducible Graphs
Depth-first Ordering: numbering of nodes in the reverse order in which they were last visited during depth first search. Depth First Number – position in the ordering MN is a back edge iff DFN(M) >= DFN(N) Forward edge MN (M is descendant of N in DFST) Depth-first Ordering Back edge MN (N is ancestor of M in DFST)

18 Example 1 2 3 4 6 7 6 4 3 5 3 2 8 2 1 D Depth first ordering:
DFN: Forward edge Depth First Ordering Back edge

19 Algorithm for DFN Computation
DFS(X) { mark X as “visited” for each successor S of X do if S is “unvisited” then call DFS(S) endif endfor DFN[X] = I; I = I – 1; } Mark all nodes as “unvisited” I = # of nodes in the graph; DFS(no);

20 Sample Problems Control Flow Analysis

21 Dominators 1. For the given control flow graph:
Compute the dominator sets and construct the dominator tree; Identify the loops using the dominator information; and (c) Is this control flow graph reducible? If it is so, covert it into a reducible graph. 1 2 4 3 5 6 7 8

22 Depth First Numbering 1 2. For the given reducible control flow graph:
Compute the depth first numbering; and Identify the loops using the computed information. 2 3 4 5 6 7 8 9


Download ppt "CS 201 Compiler Construction"

Similar presentations


Ads by Google