1 cs 201 compiler construction lecture 2 control flow analysis
TRANSCRIPT
![Page 1: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/1.jpg)
1
CS 201Compiler Construction
Lecture 2Control Flow Analysis
![Page 2: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/2.jpg)
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, 5Header node: 2Loop back edge: 52 TailHead
![Page 3: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/3.jpg)
3
Property
Given two loops: they are either disjoint or one is completely nested within the other.
Loops {1,2,4}and {5,6} are
Disjoint.
Loop {5,6} isnested within
loop {2,4,5,6}.
Loop {5,6} isnested within
loop {1,2,3,4,5,6}.
5555
0
1
2
3 4
5
6
![Page 4: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/4.jpg)
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 AB such that
B dominates A. B is the head & A is the tail.
4
![Page 5: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/5.jpg)
Identifying Loops
Algorithm for finding loops:
1. Compute Dominator Information.
2. Identify Back Edges.
3. Construct Loops corresponding to Back Edges.
5
![Page 6: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/6.jpg)
Dominators: Characteristics
1. Every node dominates itself.
2. Start node dominates every node in the flow graph.
3. If N DOM M and M DOM R then N DOM R.
4. If N DOM M and O DOM M then
either N DOM O or O DOM N
5. Set of dominators of a given node can be linearly ordered according to dominator relationships.
6
![Page 7: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/7.jpg)
Dominators: Characteristics
6. Dominator information can be represented by a Dominator Tree. Edges in the dominator tree represent immediate dominator relationships.
7
1 is the immediatedominator of
2, 3 & 4
CFG Dominator Tree
![Page 8: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/8.jpg)
Computing Dominator Sets
Observation: node m donimates node n iff m dominates all predecessors of n.
8
Let D(n) = set of dominators of n
Where Pred(n) is set of immediate predecessors
of n in the CFG
![Page 9: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/9.jpg)
Computing Dominator Sets
9
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:
![Page 10: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/10.jpg)
Example: Computing Dom. Sets
10
D(1) = {1}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: 94, 108, 105
![Page 11: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/11.jpg)
Natural Loop
Given a back edge N DNatural loop of edge N D = {D} + {X st X can reach N without going
through D}
11
1 dominates 6 61 is a back edge
Loop of 61 = {1} + {3,4,5,6} = {1,3,4,5,6}
![Page 12: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/12.jpg)
Algorithm for Loop Construction
Given a Back Edge ND
12
Stack = emptyLoop = {D}Insert(N)While stack not empty do pop m – top element of stack for each p in pred(m) do Insert(p) endforEndwhile
Insert(m) if m not in Loop then Loop = Loop U {m} push m onto Stack endifEnd Insert
![Page 13: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/13.jpg)
Example
Back Edge 72
13
Loop = {2} + {7} + {6} + {4} + {5} + {3}
Stack = 7 6 4 5 3
D
N
![Page 14: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/14.jpg)
Examples
14
L2 B, S2L1 A,S1,B,S2L2 nested in L1
L1 S1,S2,S3,S4L2 S2,S3,S4L2 nested in L1
While A do S1 While B do S2 EndwhileEndwhile
?
![Page 15: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/15.jpg)
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.
15
![Page 16: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/16.jpg)
Reducible Flow Graph
How to check reducibility ?– Remove all back edges and see if the
resulting graph is acyclic.
16Reducible
Irreducible
23 not a back edge32 not a back edgegraph is not acyclic
Node Splitting
Converts irreducibleto reducible
![Page 17: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/17.jpg)
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.
MN is a back edge iff DFN(M) >=
DFN(N)
17
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --Depth-firstOrdering
Forward edge MN (M is descendant of N in DFST)
Back edge MN(N is ancestor of M in DFST)
![Page 18: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/18.jpg)
Algorithm for DFN Computation
18
Mark all nodes as “unvisited”DFST = {} // set of edges of DFSTI = # of nodes in the graph;DFS(no);
DFS(X) { mark X as “visited” for each successor S of X do if S is “unvisited” then add edge XS to DFST call DFS(S) endif endfor DFN[X] = I; I = I – 1;}
![Page 19: 1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis](https://reader031.vdocuments.mx/reader031/viewer/2022032200/56649cf85503460f949c979a/html5/thumbnails/19.jpg)
Example
19
CFG DFST
1 2 3 4 6 7 8 7 6 4 3 5 3 2 1
1 2 3 5 4 6 7 8Depth FirstOrdering
Back edge
Forward edge