Docsity

Control Flow Analysis: Identifying Loops and Dominators in Compiler Construction - Prof. R, Study notes of Computer Science

Toggle sidebar
bg1
3/30/09
1
1
CS 201
Compiler Construction
Lecture 2
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: 52
TailHead
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Control Flow Analysis: Identifying Loops and Dominators in Compiler Construction - Prof. R and more Study notes Computer Science in PDF only on Docsity!

1

CS 201

Compiler Construction

Lecture 2 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. 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 {2,3,4,5,6}. 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 Natural 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. 4

Dominators: Characteristics

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

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

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: 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: 94, 108, 10 5

Example

Back Edge 7 2

13 Loop = {2,7} + {6} + {4} + {5} + {3} Stack = 7 6 4 5 3

D

N

Examples 14

L2  B, S

L1  A,S1,B,S

L2 nested in L L1  S1,S2,S3,S L2  S2,S3,S L2 nested in L While A do S While B do S Endwhile Endwhile ?

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 Reducible Flow Graph

How to check reducibility?

  • Remove all back edges and see if the resulting graph is acyclic. Reducible^16 Irreducible 2 3 not a back edge 3 2 not a back edge graph is not acyclic Node Splitting Converts irreducible to reducible

Example 19

CFG DFST

Depth First Ordering Back edge Forward edge