cs 201 compiler construction

58
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis

Upload: lavi

Post on 04-Jan-2016

60 views

Category:

Documents


1 download

DESCRIPTION

CS 201 Compiler Construction. Lecture 2 Control Flow Analysis. 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CS 201 Compiler Construction

1

CS 201Compiler Construction

Lecture 2Control Flow Analysis

Page 2: CS 201 Compiler Construction

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: CS 201 Compiler Construction

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: CS 201 Compiler Construction

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: CS 201 Compiler Construction

Identifying Loops

Algorithm for finding loops:

1. Compute Dominator Information.

2. Identify Back Edges.

3. Construct Loops corresponding to Back Edges.

5

Page 6: CS 201 Compiler Construction

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: CS 201 Compiler Construction

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: CS 201 Compiler Construction

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: CS 201 Compiler Construction

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: CS 201 Compiler Construction

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: CS 201 Compiler Construction

Loop

Given a back edge N DLoop corresponding to 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: CS 201 Compiler Construction

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: CS 201 Compiler Construction

Example

Back Edge 72

13

Loop = {2} + {7} + {6} + {4} + {5} + {3}

Stack = 7 6 4 5 3

D

N

Page 14: CS 201 Compiler Construction

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: CS 201 Compiler Construction

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: CS 201 Compiler Construction

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: CS 201 Compiler Construction

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: CS 201 Compiler Construction

Example

18

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

(Reverse of post-order traversal)

Page 19: CS 201 Compiler Construction

Algorithm for DFN Computation

19

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 20: CS 201 Compiler Construction

20

Sample ProblemsControl Flow Analysis

Page 21: CS 201 Compiler Construction

21

Dominators

1. For the given control flow graph: (a)Compute the dominator sets and construct the dominator tree; (b)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.

11

22

4433

55

77

88

66

Page 22: CS 201 Compiler Construction

22

Depth First Numbering

2. For the given reducible control flow graph: (a)Compute the depth first numbering; and

(a)Identify the loops using the computed information.

11

22

55

33 44

66

77

88

99

Page 23: CS 201 Compiler Construction

23

CS 201Compiler Construction

Lecture 3Data Flow Analysis

Page 24: CS 201 Compiler Construction

24

Data Flow Analysis

Data flow analysis is used to collect information about the flow of data values across basic blocks.

• Dominator analysis collected global information regarding the program’s structure

• For performing global code optimizations global information must be collected regarding values of program variables.

– Local optimizations involve statements from same basic block

– Global optimizations involve statements from different basic blocks data flow analysis is performed to collect global information that drives global optimizations

Page 25: CS 201 Compiler Construction

25

Local and Global Optimization

Page 26: CS 201 Compiler Construction

Applications of Data Flow Analysis

• Applicability of code optimizations• Symbolic debugging of code• Static error checking• Type inference• …….

26

Page 27: CS 201 Compiler Construction

Applications of Data Flow Analysis

• Reaching Definition• Available Expression• Live Variables• Very Busy Expression

27

• Definition• How to compute• Application

Page 28: CS 201 Compiler Construction

1. Reaching Definitions

Definition d of variable v: a statement d that assigns a value to v. (d: v = 1;)

Use of variable v: reference to value of v in an expression evaluation. (u: … = v+2;)

Definition d of variable v reaches a point p if there exists a path from immediately after d to p such that definition d is not killed along the path.

Definition d is killed along a path between two points if there exists an assignment to variable v along the path.

28

Page 29: CS 201 Compiler Construction

Example

29

d reaches u along path2 & d does not reach u along path1

Since there exists a path from d to u along which d is not killed (i.e., path2), d reaches u.

Page 30: CS 201 Compiler Construction

Reaching Definitions Contd.

Unambiguous Definition: X = ….;Ambiguous Definition: *p = ….; p may point

to X

For computing reaching definitions, typically we only consider kills by unambiguous definitions.

30

X=..

*p=..

Does definition of X reach here ? Yes

Page 31: CS 201 Compiler Construction

Computing Reaching Definitions

At each program point p, we compute the set of definitions that reach point p.

Reaching definitions are computed by solving a system of equations (data flow equations).

31

d1: X=…

IN[B]

OUT[B]GEN[B] ={d1}KILL[B]={d2,d3}

d2: X=…

d3: X=…

Page 32: CS 201 Compiler Construction

Data Flow Equations

32

GEN[B]: Definitions within B that reach the end of B.KILL[B]: Definitions that never reach the end of B due to redefinitions of variables in B.

IN[B]: Definitions that reach B’s entry.OUT[B]: Definitions that reach B’s exit.

Page 33: CS 201 Compiler Construction

Reaching Definitions Contd.

• Forward problem – information flows forward in the direction of edges.

• May problem – there is a path along which definition reaches a point but it does not always reach the point.

Therefore in a May problem the meet operator is the Union operator.

33

Page 34: CS 201 Compiler Construction

Applications of Reaching Definitions

• Constant Propagation/folding

• Copy Propagation

34

Page 35: CS 201 Compiler Construction

2. Available Expressions

An expression is generated at a point if it is computed at that point.

An expression is killed by redefinitions of operands of the expression.

An expression A+B is available at a point if every path from the start node to the point evaluates A+B and after the last evaluation of A+B on each path there is no redefinition of either A or B (i.e., A+B is not killed).

35

Page 36: CS 201 Compiler Construction

Available Expressions

Available expressions problem computes: at each program point the set of expressions available at that point.

36

Page 37: CS 201 Compiler Construction

Data Flow Equations

37

GEN[B]: Expressions computed within B that are available at the end of B.KILL[B]: Expressions whose operands are redefined in B.

IN[B]: Expressions available at B’s entry.OUT[B]: Expressions available at B’s exit.

Page 38: CS 201 Compiler Construction

Available Expressions Contd.

• Forward problem – information flows forward in the direction of edges.

• Must problem – expression is definitely available at a point along all paths.

Therefore in a Must problem the meet operator is the Intersection operator.

• Application:

A

38

Page 39: CS 201 Compiler Construction

3. Live Variable Analysis

A path is X-clear if it contains no definition of X.A variable X is live at point p if there exists a X-

clear path from p to a use of X; otherwise X is dead at p.

39

Live Variable Analysis Computes: At each program point p identify the set of variables that are live at p.

Page 40: CS 201 Compiler Construction

Data Flow Equations

40

GEN[B]: Variables that are used in B prior to their definition in B.KILL[B]: Variables definitely assigned value in B before any use of that variable in B.

IN[B]: Variables live at B’s entry.OUT[B]: Variables live at B’s exit.

Page 41: CS 201 Compiler Construction

Live Variables Contd.

• Backward problem – information flows backward in reverse of the direction of edges.

• May problem – there exists a path along which a use is encountered.

Therefore in a May problem the meet operator is the Union operator.

41

Page 42: CS 201 Compiler Construction

Applications of Live Variables

• Register Allocation

• Dead Code Elimination

• Code Motion Out of Loops

42

Page 43: CS 201 Compiler Construction

4. Very Busy Expressions

A expression A+B is very busy at point p if for all paths starting at p and ending at the end of the program, an evaluation of A+B appears before any definition of A or B.

43

Application: Code Size Reduction

Compute for each program point the set of very busy expressions at the point.

Page 44: CS 201 Compiler Construction

Data Flow Equations

44

GEN[B]: Expression computed in B and variables used in the expression are not redefined in B prior to expression’s evaluation in B.KILL[B]: Expressions that use variables that are redefined in B.

IN[B]: Expressions very busy at B’s entry.OUT[B]: Expressions very busy at B’s exit.

Page 45: CS 201 Compiler Construction

Very Busy Expressions Contd.

• Backward problem – information flows backward in reverse of the direction of edges.

• Must problem – expressions must be computed along all paths.

Therefore in a Must problem the meet operator is the Intersection operator.

45

Page 46: CS 201 Compiler Construction

Summary

May/Union Must/Intersection

Forward Reaching Definitions

Available Expressions

Backward Live Variables

Very Busy Expressions

46

Page 47: CS 201 Compiler Construction

Conservative Analysis

Optimizations that we apply must be Safe => the data flow facts we compute should definitely be true (not simply possibly true).

Two main reasons that cause results of analysis to be conservative:

1. Control Flow2. Pointers & Aliasing

47

Page 48: CS 201 Compiler Construction

Conservative Analysis

1. Control Flow – we assume that all paths are executable; however, some may be infeasible.

48

X+Y is alwaysavailable if weexclude infeasiblepaths.

Page 49: CS 201 Compiler Construction

Conservative Analysis

2. Pointers & Aliasing – we may not know what a pointer points to.1. X = 5

2. *p = … // p may or may not point to X3. … = X

Constant propagation: assume p does point to

X (i.e., in statement 3, X cannot be replaced by 5).

Dead Code Elimination: assume p does not point to X (i.e., statement 1 cannot be deleted).

49

Page 50: CS 201 Compiler Construction

Representation of Data Flow Sets

• Bit vectors – used to represent sets because we are computing binary information.– Does a definition reach a point ? T or F– Is an expression available/very busy ? T or F– Is a variable live ? T or F

• For each expression, variable, definition we have one bit – intersection and union operations can be implemented using bitwise and & or operations.

50

Page 51: CS 201 Compiler Construction

Solving Data Flow Equations

51

Page 52: CS 201 Compiler Construction

Solving Data Flow Equations

52

Page 53: CS 201 Compiler Construction

Solving Data Flow Equations

53

Page 54: CS 201 Compiler Construction

Use-Def & Def-Use Chains

54

Page 55: CS 201 Compiler Construction

55

Sample ProblemsData Flow Analysis

Page 56: CS 201 Compiler Construction

56

Data Flow Analysis

Formulate data flow equations for computing the following information:

1. Postdominators -- postdominator set of a node is the set of nodes that are encountered along all paths from the node to the end node of the control flow graph. This information is used for computing control dependence.

Page 57: CS 201 Compiler Construction

57

2. Reachable uses -- for each definition identify the set of uses reachable by the definition. This information is used for computing def-use chains.

3. Reaching uses -- given a definition of variable x, identify the set of uses of x that are encountered prior to reaching the definition and there is no other definitions of x that intervene the use and the definition. This information is used for computing antidependences.

Page 58: CS 201 Compiler Construction

58

4. Classify Variable Values -- classify the value of each program variable at each program point into one of the following categories: (a) the value is a unique constant -- you must also identify this constant value; (b) the value is one-of-many constants – you do not have to compute the identities of these constants as part of your solution; and (c) the value is not-a-constant, that is, it is neither a unique constant nor a one-of-many constants. This is a generalization of constant propagation.