Download presentation
Presentation is loading. Please wait.
Published byFrantišek Přemysl Janda Modified over 5 years ago
1
Taken largely from University of Delaware Compiler Notes
Dominators and CFGs Taken largely from University of Delaware Compiler Notes \course\cpeg421-05s\Topic2.ppt
2
Determine control structure of a program build “control flow graphs”
Control flow analysis Interprocedural Program Flow analysis Intraprocedural Procedure Data flow analysis Local 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.)
3
Motivation . S1: A 2 (def of A) S2: B 10 (def of B)
S3: C A + B determine if C is a constant 12? S4 Do I = 1, C A[I] = B[I] + D[I-1] .
4
Basic Blocks 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. 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 ]
5
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.
6
Example The following code computes the inner product of two vectors.
(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) begin prod := 0; i := 1; do begin prod := prod + a[i] * b[i] i = i+ 1; end while i <= 20 Source code. Three-address code. (AhoSethiUllman, pp. 529)
7
Example The following code computes the inner product of two vectors.
Rule (i) (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) … begin prod := 0; i := 1; do begin prod := prod + a[i] * b[i] i = i+ 1; end while i <= 20 Source code. Three-address code.
8
Example The following code computes the inner product of two vectors.
Rule (i) (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) … begin prod := 0; i := 1; do begin prod := prod + a[i] * b[i] i = i+ 1; end while i <= 20 Rule (ii) Source code. Three-address code.
9
Example The following code computes the inner product of two vectors.
Rule (i) (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) … begin prod := 0; i := 1; do begin prod := prod + a[i] * b[i] i = i+ 1; end while i <= 20 Rule (ii) Rule (iii) Source code. Three-address code.
10
Example B1 (1) prod := 0 (2) i := 1 B2 (3) t1 := 4 * i (4) t2 := a[t1]
(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) Basic Blocks: B3 (13) …
11
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
12
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).
13
An Example Domination relation: { (1, 1), (1, 2), (1, 3), (1, 4) …
(2, 3), (2, 4), … (2, 10) } 1 S 2 3 Direct domination: 4 1 <d 2, 2 <d 3, … 5 6 7 DOM: 8 DOM(1) = {1} DOM(2) = {1, 2} DOM(3) = {1, 2, 3} DOM(10) = {1, 2, 10) 9 10
14
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 ?
15
Dominator Tree A flowgraph (left) and its dominator tree (right) S 1 2
3 4 5 6 7 8 9 10 S 1 2 3 4 5 7 8 6 9 10 A flowgraph (left) and its dominator tree (right)
16
Question Answer: NO! Example: consider nodes 5 and 8.
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.
17
An Example (Dominators)
1 2 3 4 5 6 7 8 9 10
18
Depth-First Search (DFS)
An “ordering” of nodes of CFG BBs dfs_nums 1..N Each BB dfs_num represents when that BB first encountered in DFS Write dfs(BB root);
19
DFS and Depth-First Order
CONFUSING: They are NOT the same DFS as defined: order of first visitation during depth-first search Depth-First Order “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) Used (sometimes) to identify loops Later …
20
An Example DFS 1 2 3 4 5 6 7 8 9 10
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.