Dominators and CFGs Taken largely from University of Delaware Compiler Notes \course\cpeg421-05s\Topic2.ppt.

Presentation on theme: "Dominators and CFGs Taken largely from University of Delaware Compiler Notes \course\cpeg421-05s\Topic2.ppt."— Presentation transcript:

Dominators and CFGs Taken largely from University of Delaware Compiler Notes \course\cpeg421-05s\Topic2.ppt

Basic block Determine control structure of a program build “control flow graphs” Determine the flow of scalar values (Solution: propagation of data flow information along flow graph.) Program Procedure Interprocedural Intraprocedural Local Flow analysis Data flow analysis Control flow analysis

Motivation S 1 :A2(def of A) S 2 :B10(def of B) S 3 :CA + Bdetermine if C is a constant 12? S 4 Do I = 1, C A[I] = B[I] + D[I-1]......

Basic Blocks Only the last statement of a basic block can be a branch statement and only the first statement of a basic block can be a target of a branch. However, procedure calls MAY need be included within a basic block. [See text, pp 219-20] A basic block is a sequence of consecutive intermediate language statements in which flow of control can only enter at the beginning and leave at the end.

Basic Block Partitioning Algorithm 1. Identify leader statements (i.e. the first statements of basic blocks) by using the following rules: (i) The first statement in the program is a leader (ii) Any statement that is the target of a branch statement is a leader (for most IL’s. these are label statements) (iii) Any statement that immediately follows a branch or return statement is a leader 2. The basic block corresponding to a leader consists of the leader, and all statements up to but not including the next leader or up to the end of the program.

Example begin prod := 0; i := 1; do begin prod := prod + a[i] * b[i] i = i+ 1; end while i <= 20 end The following code computes the inner product of two vectors. (1) prod := 0 (2) i := 1 (3) t1 := 4 * i (4) t2 := a[t1] (5) t3 := 4 * i (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i + 1 (11) i := t7 (12) if i <= 20 goto (3) Source code. Three-address code. (AhoSethiUllman, pp. 529)

Example begin prod := 0; i := 1; do begin prod := prod + a[i] * b[i] i = i+ 1; end while i <= 20 end The following code computes the inner product of two vectors. (1) prod := 0 (2) i := 1 (3) t1 := 4 * i (4) t2 := a[t1] (5) t3 := 4 * i (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i + 1 (11) i := t7 (12) if i <= 20 goto (3) (13) … Source code. Three-address code. Rule (i)

Example begin prod := 0; i := 1; do begin prod := prod + a[i] * b[i] i = i+ 1; end while i <= 20 end The following code computes the inner product of two vectors. (1) prod := 0 (2) i := 1 (3) t1 := 4 * i (4) t2 := a[t1] (5) t3 := 4 * i (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i + 1 (11) i := t7 (12) if i <= 20 goto (3) (13) … Source code. Three-address code. Rule (i) Rule (ii)

Example begin prod := 0; i := 1; do begin prod := prod + a[i] * b[i] i = i+ 1; end while i <= 20 end The following code computes the inner product of two vectors. (1) prod := 0 (2) i := 1 (3) t1 := 4 * i (4) t2 := a[t1] (5) t3 := 4 * i (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i + 1 (11) i := t7 (12) if i <= 20 goto (3) (13) … Source code. Three-address code. Rule (i) Rule (ii) Rule (iii)

Example Basic Blocks: (1) prod := 0 (2) i := 1 (3) t1 := 4 * i (4) t2 := a[t1] (5) t3 := 4 * i (6) t4 := b[t3] (7) t5 := t2 * t4 (8) t6 := prod + t5 (9) prod := t6 (10) t7 := i + 1 (11) i := t7 (12) if i <= 20 goto (3) (13) … B1 B2 B3

Control Flow Graph (CFG) The basic block whose leader is the first IL statement is called the initial node or start node There is a directed edge from basic block B1 to basic B2 in the CFG if: (1) There is a branch from the last statement of B1 to the first statement of B2, or (2) Control flow can fall through from B1 to B2 because B2 immediately follows B1, and B1 does not end in an unconditional branch

Dominators Node (basic block) D in a CFG dominates node N if every path from the start node to N goes through D. We say that node D is a dominator of node N. Define dom(N) = set of node N’s dominators, or the dominator set for node N. Note: by definition, each node dominates itself i.e., N  dom(N).

Domination relation: An Example 1 2 3 4 5 6 7 8 9 10 S { (1, 1), (1, 2), (1, 3), (1, 4) … (2, 3), (2, 4), … (2, 10) } Direct domination: DOM : 1 < d 2, 2 < d 3, … DOM(1) = {1} DOM(2) = {1, 2} DOM(3) = {1, 2, 3} DOM(10) = {1, 2, 10)

Immediate Dominators and Dominator Tree Node M is the immediate dominator of node N ==> Node M must be the last dominator of N on any path from the start node to N. Therefore, every node other than the start node must have a unique immediate dominator (the start node has no immediate dominator.) What does this mean ?

Dominator Tree 1 2 3 4 5 6 7 8 9 10 S 1 2 3 4 5 7 8 6 9 A flowgraph (left) and its dominator tree (right)

Question Assume an immediate dominator n’ of a node n, is n’ necessarily an immediate predecessor in the flow graph? Answer: NO! Example: consider nodes 5 and 8.

1 2 3 4 7 5 6 8 9 10 An Example (Dominators)

Depth-First Search (DFS) zAn “ordering” of nodes of CFG zBBs dfs_nums 1..N zEach BB dfs_num represents when that BB first encountered in DFS zWrite dfs(BB root);

DFS and Depth-First Order zCONFUSING: They are NOT the same zDFS as defined: order of first visitation during depth-first search zDepth-First Order y“The depth-first ordering of the nodes is the reverse of the order in which we last visit the nodes in a preorder traversal” (Aho-Sethi-Ullman) yUsed (sometimes) to identify loops yLater …

1 2 3 4 7 5 6 8 9 10 An Example DFS

Download ppt "Dominators and CFGs Taken largely from University of Delaware Compiler Notes \course\cpeg421-05s\Topic2.ppt."

Similar presentations