School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.

Slides:



Advertisements
Similar presentations
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Advertisements

School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Lecture 11: Code Optimization CS 540 George Mason University.
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
Data Flow Analysis. Goal: make assertions about the data usage in a program Use these assertions to determine if and when optimizations are legal Local:
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
1 Introduction to Data Flow Analysis. 2 Data Flow Analysis Construct representations for the structure of flow-of-data of programs based on the structure.
Jeffrey D. Ullman Stanford University. 2  Generalizes: 1.Moving loop-invariant computations outside the loop. 2.Eliminating common subexpressions. 3.True.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao.
Partial Redundancy Elimination. Partial-Redundancy Elimination Minimize the number of expression evaluations By moving around the places where an expression.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Data Flow Analysis Compiler Design Nov. 3, 2005.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
Lecture 6 Program Flow Analysis Forrest Brewer Ryan Kastner Jose Amaral.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Data flow analysis Emery Berger University.
2015/6/29\course\cpeg421-08s\Topic4-a.ppt1 Topic-I-C Dataflow Analysis.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Ben Livshits Based in part of Stanford class slides from
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
1 CS 201 Compiler Construction Data Flow Analysis.
1 ECE 453 – CS 447 – SE 465 Software Testing & Quality Assurance Instructor Kostas Kontogiannis.
1 Data-Flow Analysis Proving Little Theorems Data-Flow Equations Major Examples.
Data Flow Analysis Compiler Baojian Hua
Formal Methods Program Slicing & Dataflow Analysis February 2015.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
MIT Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
Compiler Principles Winter Compiler Principles Global Optimizations Mayer Goldberg and Roman Manevich Ben-Gurion University.
Jeffrey D. Ullman Stanford University. 2 boolean x = true; while (x) {... // no change to x }  Doesn’t terminate.  Proof: only assignment to x is at.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
12/5/2002© 2002 Hal Perkins & UW CSER-1 CSE 582 – Compilers Data-flow Analysis Hal Perkins Autumn 2002.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Data Flow Analysis II AModel Checking and Abstract Interpretation Feb. 2, 2011.
Optimization Simone Campanoni
Dataflow Analysis CS What I s Dataflow Analysis? Static analysis reasoning about flow of data in program Different kinds of data: constants, variables,
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
DFA foundations Simone Campanoni
COMPILERS Liveness Analysis hussein suleman uct csc3003s 2009.
Data Flow Analysis Suman Jana
Lecture 5 Partial Redundancy Elimination
Simone Campanoni DFA foundations Simone Campanoni
Dataflow Testing G. Rothermel.
Fall Compiler Principles Lecture 8: Loop Optimizations
Topic 10: Dataflow Analysis
University Of Virginia
1. Reaching Definitions Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression.
Optimizations Noam Rinetzky
Fall Compiler Principles Lecture 10: Global Optimizations
Fall Compiler Principles Lecture 10: Loop Optimizations
Data Flow Analysis Compiler Design
Topic-4a Dataflow Analysis 2019/2/22 \course\cpeg421-08s\Topic4-a.ppt.
Static Single Assignment
EECS 583 – Class 5 Dataflow Analysis
Live variables and copy propagation
COMPILERS Liveness Analysis
Presentation transcript:

School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from MIT “Computer Language Engineering”

2 Fall 2011 “Advanced Compiler Techniques” Dataflow Analysis Last lecture: Last lecture: How to analyze and transform within a basic block How to analyze and transform within a basic block This lecture: This lecture: How to do it for the entire procedure How to do it for the entire procedure

3 Fall 2011 “Advanced Compiler Techniques” Outline Reaching Definitions Reaching Definitions Available Expressions Available Expressions Live Variables Live Variables

4 Fall 2011 “Advanced Compiler Techniques” Reaching Definitions Concept of definition and use Concept of definition and use a = x+y a = x+y is a definition of a is a definition of a is a use of x and y is a use of x and y A definition reaches a use if A definition reaches a use if value written by definition may be read by use

5 Fall 2011 “Advanced Compiler Techniques” Reaching Definitions s = 0; a = 4; i = 0; k == 0 b = 1;b = 2; i < n s = s + a*b; i = i + 1; return s

6 Fall 2011 “Advanced Compiler Techniques” Reaching Definitions and Constant Propagation Is a use of a variable a constant? Is a use of a variable a constant? Check all reaching definitions Check all reaching definitions If all assign variable to same constant If all assign variable to same constant Then use is in fact a constant Then use is in fact a constant Can replace variable with constant Can replace variable with constant

7 Fall 2011 “Advanced Compiler Techniques” Is a Constant in s = s+a*b? s = 0; a = 4; i = 0; k == 0 b = 1;b = 2; i < n s = s + a*b; i = i + 1; return s Yes! On all reaching definitions a = 4

8 Fall 2011 “Advanced Compiler Techniques” Constant Propagation Transform s = 0; a = 4; i = 0; k == 0 b = 1;b = 2; i < n s = s + 4*b; i = i + 1; return s Yes! On all reaching definitions a = 4

9 Fall 2011 “Advanced Compiler Techniques” Is b Constant in s = s+a*b? s = 0; a = 4; i = 0; k == 0 b = 1;b = 2; i < n s = s + a*b; i = i + 1; return s No! One reaching definition with b = 1 One reaching definition with b = 2

10 Fall 2011 “Advanced Compiler Techniques” Splitting Preserves Information Lost At Merges s = 0; a = 4; i = 0; k == 0 b = 1;b = 2; i < n s = s + a*b; i = i + 1; return s s = 0; a = 4; i = 0; k == 0 b = 1;b = 2; i < n s = s + a*b; i = i + 1; return s i < n s = s + a*b; i = i + 1; return s

11 Fall 2011 “Advanced Compiler Techniques” Splitting Preserves Information Lost At Merges s = 0; a = 4; i = 0; k == 0 b = 1;b = 2; i < n s = s + a*b; i = i + 1; return s s = 0; a = 4; i = 0; k == 0 b = 1;b = 2; i < n s = s + a* 1 ; i = i + 1; return s i < n s = s + a* 2 ; i = i + 1; return s

12 Fall 2011 “Advanced Compiler Techniques” Computing Reaching Definitions Compute with sets of definitions Compute with sets of definitions represent sets using bit vectors represent sets using bit vectors each definition has a position in bit vector each definition has a position in bit vector At each basic block, compute At each basic block, compute definitions that reach start of block definitions that reach start of block definitions that reach end of block definitions that reach end of block Do computation by simulating execution of program until reach fixed point Do computation by simulating execution of program until reach fixed point

13 Fall 2011 “Advanced Compiler Techniques” 1: s = 0; 2: a = 4; 3: i = 0; k == 0 4: b = 1;5: b = 2; i < n return s 6: s = s + a*b; 7: i = i + 1;

14 Fall 2011 “Advanced Compiler Techniques” Data-Flow Analysis Schema Data-flow value: at every program point Domain: The set of possible data-flow values for this application IN[S] and OUT[S]: the data-flow values before and after each statement s Data-flow problem: find a solution to a set of constraints on the IN [s] ‘s and OUT[s] ‘s, for all statements s. based on the semantics of the statements ("transfer functions" ) based on the flow of control.

15 Fall 2011 “Advanced Compiler Techniques” Constraints Transfer function: relationship between the data-flow values before and after a statement. Forward: OUT[s] = f s (IN[s]) Backward: IN[s] = f s (OUT[s]) Within a basic block (s 1,s 2,…,s n ) Within a basic block (s 1,s 2,…,s n ) IN[s i +1 ] = OUT[s i ], for all i = 1, 2,..., n-1

16 Fall 2011 “Advanced Compiler Techniques” Data-Flow Schemas on Basic Blocks Each basic block B (s 1,s 2,…,s n ) has Each basic block B (s 1,s 2,…,s n ) has IN – data-flow values immediately before a block IN – data-flow values immediately before a block OUT – data-flow values immediately after a block OUT – data-flow values immediately after a block IN[B] = IN[S 1 ] OUT[B] = OUT[S n ] OUT[B] = f B (IN[B] ) Where fB = fs n ◦ ◦ fs 2 ◦ fs 1

17 Fall 2011 “Advanced Compiler Techniques” Between Blocks Forward analysis Forward analysis (eg: Reaching definitions) (eg: Reaching definitions) IN[B] = U P a predecessor of B OUT[P] Backward analysis Backward analysis (eg: live variables) IN[B] = f B (OUT[B]) OUT[B] = U S a successor of B IN[S].

18 Fall 2011 “Advanced Compiler Techniques” Formalizing Reaching Definitions Each basic block has Each basic block has IN - set of definitions that reach beginning of block IN - set of definitions that reach beginning of block OUT - set of definitions that reach end of block OUT - set of definitions that reach end of block GEN - set of definitions generated in block GEN - set of definitions generated in block KILL - set of definitions killed in block KILL - set of definitions killed in block GEN[s = s + a*b; i = i + 1;] = GEN[s = s + a*b; i = i + 1;] = KILL[s = s + a*b; i = i + 1;] = KILL[s = s + a*b; i = i + 1;] = Compiler scans each basic block to derive GEN and KILL sets Compiler scans each basic block to derive GEN and KILL sets

19 Fall 2011 “Advanced Compiler Techniques” Example

20 Fall 2011 “Advanced Compiler Techniques” Dataflow Equations IN[b] = OUT[b1] U... U OUT[bn] IN[b] = OUT[b1] U... U OUT[bn] where b1,..., bn are predecessors of b in CFG where b1,..., bn are predecessors of b in CFG OUT[b] = (IN[b] - KILL[b]) U GEN[b] OUT[b] = (IN[b] - KILL[b]) U GEN[b] IN[entry] = IN[entry] = Result: system of equations Result: system of equations

21 Fall 2011 “Advanced Compiler Techniques” Solving Equations Use fixed point algorithm Use fixed point algorithm Initialize with solution of OUT[b] = Initialize with solution of OUT[b] = Repeatedly apply equations Repeatedly apply equations IN[b] = OUT[b1] U... U OUT[bn] IN[b] = OUT[b1] U... U OUT[bn] OUT[b] = (IN[b] - KILL[b]) U GEN[b] OUT[b] = (IN[b] - KILL[b]) U GEN[b] Until reach fixed point Until reach fixed point Until equation application has no further effect Until equation application has no further effect Use a worklist to track which equation applications may have a further effect Use a worklist to track which equation applications may have a further effect

22 Fall 2011 “Advanced Compiler Techniques” Reaching Definitions Algorithm for all nodes n in N OUT[n] = emptyset; // OUT[n] = GEN[n]; IN[Entry] = emptyset; OUT[Entry] = GEN[Entry]; Changed = N - { Entry }; // N = all nodes in graph while (Changed != emptyset) choose a node n in Changed; choose a node n in Changed; Changed = Changed - { n }; Changed = Changed - { n }; IN[n] = emptyset; IN[n] = emptyset; for all nodes p in predecessors(n) for all nodes p in predecessors(n) IN[n] = IN[n] U OUT[p]; OUT[n] = GEN[n] U (IN[n] - KILL[n]); OUT[n] = GEN[n] U (IN[n] - KILL[n]); if (OUT[n] changed) if (OUT[n] changed) for all nodes s in successors(n) for all nodes s in successors(n) Changed = Changed U { s };

23 Fall 2011 “Advanced Compiler Techniques” Questions Does the algorithm halt? Does the algorithm halt? yes, because transfer function is monotonic yes, because transfer function is monotonic if increase IN, increase OUT if increase IN, increase OUT in limit, all bits are 1 in limit, all bits are 1 If bit is 0, does the corresponding definition ever reach basic block? If bit is 0, does the corresponding definition ever reach basic block? If bit is 1, does the corresponding definition always reach the basic block? If bit is 1, does the corresponding definition always reach the basic block?

24 Fall 2011 “Advanced Compiler Techniques” Outline Reaching Definitions Reaching Definitions Available Expressions Available Expressions Live Variables Live Variables

25 Fall 2011 “Advanced Compiler Techniques” Available Expressions An expression x+y is available at a point p if An expression x+y is available at a point p if every path from the initial node to p must evaluate x+y before reaching p, every path from the initial node to p must evaluate x+y before reaching p, and there are no assignments to x or y after the evaluation but before p. and there are no assignments to x or y after the evaluation but before p. Available Expression information can be used to do global (across basic blocks) CSE Available Expression information can be used to do global (across basic blocks) CSE If expression is available at use, no need to reevaluate it If expression is available at use, no need to reevaluate it

26 Fall 2011 “Advanced Compiler Techniques” Example: Available Expression a = b + c d = e + f f = a + c g = a + c j = a + b + c + d b = a + d h = c + f

27 Fall 2011 “Advanced Compiler Techniques” Is the Expression Available? a = b + c d = e + f f = a + c g = a + c j = a + b + c + d b = a + d h = c + f YES!

28 Fall 2011 “Advanced Compiler Techniques” Is the Expression Available? a = b + c d = e + f f = a + c g = a + c j = a + b + c + d b = a + d h = c + f YES!

29 Fall 2011 “Advanced Compiler Techniques” Is the Expression Available? a = b + c d = e + f f = a + c g = a + c j = a + b + c + d b = a + d h = c + f NO!

30 Fall 2011 “Advanced Compiler Techniques” Is the Expression Available? a = b + c d = e + f f = a + c g = a + c j = a + b + c + d b = a + d h = c + f NO!

31 Fall 2011 “Advanced Compiler Techniques” Is the Expression Available? a = b + c d = e + f f = a + c g = a + c j = a + b + c + d b = a + d h = c + f NO!

32 Fall 2011 “Advanced Compiler Techniques” Is the Expression Available? a = b + c d = e + f f = a + c g = a + c j = a + b + c + d b = a + d h = c + f YES!

33 Fall 2011 “Advanced Compiler Techniques” Is the Expression Available? a = b + c d = e + f f = a + c g = a + c j = a + b + c + d b = a + d h = c + f YES!

34 Fall 2011 “Advanced Compiler Techniques” Use of Available Expressions a = b + c d = e + f f = a + c g = a + c j = a + b + c + d b = a + d h = c + f

35 Fall 2011 “Advanced Compiler Techniques” Use of Available Expressions a = b + c d = e + f f = a + c g = a + c j = a + b + c + d b = a + d h = c + f

36 Fall 2011 “Advanced Compiler Techniques” Use of Available Expressions a = b + c d = e + f f = a + c g = a + c j = a + b + c + d b = a + d h = c + f

37 Fall 2011 “Advanced Compiler Techniques” Use of Available Expressions a = b + c d = e + f f = a + c g = f j = a + b + c + d b = a + d h = c + f

38 Fall 2011 “Advanced Compiler Techniques” Use of Available Expressions a = b + c d = e + f f = a + c g = f j = a + b + c + d b = a + d h = c + f

39 Fall 2011 “Advanced Compiler Techniques” Use of Available Expressions a = b + c d = e + f f = a + c g = f j = a + c + b + d b = a + d h = c + f

40 Fall 2011 “Advanced Compiler Techniques” Use of Available Expressions a = b + c d = e + f f = a + c g = f j = f + b + d b = a + d h = c + f

41 Fall 2011 “Advanced Compiler Techniques” Use of Available Expressions a = b + c d = e + f f = a + c g = f j = f + b + d b = a + d h = c + f

42 Fall 2011 “Advanced Compiler Techniques” Computing Available Expressions Represent sets of expressions using bit vectors Represent sets of expressions using bit vectors Each expression corresponds to a bit Each expression corresponds to a bit Run dataflow algorithm similar to reaching definitions Run dataflow algorithm similar to reaching definitions Big difference Big difference definition reaches a basic block if it comes from ANY predecessor in CFG definition reaches a basic block if it comes from ANY predecessor in CFG expression is available at a basic block only if it is available from ALL predecessors in CFG expression is available at a basic block only if it is available from ALL predecessors in CFG

43 Fall 2011 “Advanced Compiler Techniques” a = x+y; x == 0 x = z; b = x+y; i < n c = x+y; i = i+c; d = x+y i = x+y; Expressions 1: x+y 2: i<n 3: i+c 4: x==

44 Fall 2011 “Advanced Compiler Techniques” a = x+y; t = a x == 0 x = z; b = x+y; t = b i < n c = x+y; i = i+c; d = x+y i = x+y; Expressions 1: x+y 2: i<n 3: i+c 4: x== Global CSE Transform must use same temp for CSE in all blocks

45 Fall 2011 “Advanced Compiler Techniques” a = x+y; t = a x == 0 x = z; b = x+y; t = b i < n c = t; i = i+c; d = t i = t; Expressions 1: x+y 2: i<n 3: i+c 4: x== Global CSE Transform must use same temp for CSE in all blocks

46 Fall 2011 “Advanced Compiler Techniques” Formalizing Analysis Each basic block has Each basic block has IN - set of expressions available at start of block IN - set of expressions available at start of block OUT - set of expressions available at end of block OUT - set of expressions available at end of block GEN - set of expressions computed in block (and not killed later) GEN - set of expressions computed in block (and not killed later) KILL - set of expressions killed in in block (and not re-computed later) KILL - set of expressions killed in in block (and not re-computed later) GEN[x = z; b = x+y] = 1000 GEN[x = z; b = x+y] = 1000 KILL[x = z; b = x+y] = 0001 KILL[x = z; b = x+y] = 0001 Compiler scans each basic block to derive GEN and KILL sets Compiler scans each basic block to derive GEN and KILL sets

47 Fall 2011 “Advanced Compiler Techniques” Dataflow Equations IN[b] = OUT[b1] ...  OUT[bn] IN[b] = OUT[b1] ...  OUT[bn] where b1,..., bn are predecessors of b in CFG where b1,..., bn are predecessors of b in CFG OUT[b] = (IN[b] - KILL[b]) U GEN[b] OUT[b] = (IN[b] - KILL[b]) U GEN[b] IN[entry] = 0000 IN[entry] = 0000 Result: system of equations Result: system of equations

48 Fall 2011 “Advanced Compiler Techniques” Solving Equations Use fixed point algorithm Use fixed point algorithm IN[entry] = 0000 IN[entry] = 0000 Initialize OUT[b] = 1111 Initialize OUT[b] = 1111 Repeatedly apply equations Repeatedly apply equations IN[b] = OUT[b1] ...  OUT[bn] IN[b] = OUT[b1] ...  OUT[bn] OUT[b] = (IN[b] - KILL[b]) U GEN[b] OUT[b] = (IN[b] - KILL[b]) U GEN[b] Use a worklist algorithm to reach fixed point Use a worklist algorithm to reach fixed point

49 Fall 2011 “Advanced Compiler Techniques” Available Expressions Algorithm for all nodes n in N OUT[n] = E; IN[Entry] = emptyset; OUT[Entry] = GEN[Entry]; Changed = N - { Entry }; // N = all nodes in graph while (Changed != emptyset) choose a node n in Changed; choose a node n in Changed; Changed = Changed - { n }; Changed = Changed - { n }; IN[n] = E; // E is set of all expressions IN[n] = E; // E is set of all expressions for all nodes p in predecessors(n) for all nodes p in predecessors(n) IN[n] = IN[n]  OUT[p]; OUT[n] = GEN[n] U (IN[n] - KILL[n]); OUT[n] = GEN[n] U (IN[n] - KILL[n]); if (OUT[n] changed) if (OUT[n] changed) for all nodes s in successors(n) for all nodes s in successors(n) Changed = Changed U { s };

50 Fall 2011 “Advanced Compiler Techniques” Questions Does algorithm always halt? Does algorithm always halt? If expression is available in some execution, is it always marked as available in analysis? If expression is available in some execution, is it always marked as available in analysis? If expression is not available in some execution, can it be marked as available in analysis? If expression is not available in some execution, can it be marked as available in analysis?

51 Fall 2011 “Advanced Compiler Techniques” General Correctness Concept in actual program execution Concept in actual program execution Reaching definition: definition D, execution E at program point P Reaching definition: definition D, execution E at program point P Available expression: expression X, execution E at program point P Available expression: expression X, execution E at program point P Analysis reasons about all possible executions Analysis reasons about all possible executions For all executions E at program point P, For all executions E at program point P, if a definition D reaches P in E if a definition D reaches P in E then D is in the set of reaching definitions at P from analysis then D is in the set of reaching definitions at P from analysis Other way around Other way around if D is not in the set of reaching definitions at P from analysis if D is not in the set of reaching definitions at P from analysis then D never reaches P in any execution E then D never reaches P in any execution E For all executions E at program point P, For all executions E at program point P, if an expression X is in set of available expressions at P from analysis if an expression X is in set of available expressions at P from analysis then X is available in E at P then X is available in E at P

52 Fall 2011 “Advanced Compiler Techniques” Duality In Two Algorithms Reaching definitions Reaching definitions Confluence operation is set union Confluence operation is set union OUT[b] initialized to empty set OUT[b] initialized to empty set Available expressions Available expressions Confluence operation is set intersection Confluence operation is set intersection OUT[b] initialized to set of available expressions OUT[b] initialized to set of available expressions General framework for dataflow algorithms. General framework for dataflow algorithms. Build parameterized dataflow analyzer once, use for all dataflow problems Build parameterized dataflow analyzer once, use for all dataflow problems

53 Fall 2011 “Advanced Compiler Techniques” Outline Reaching Definitions Reaching Definitions Available Expressions Available Expressions Live Variables Live Variables

54 Fall 2011 “Advanced Compiler Techniques” Live Variable Analysis A variable v is live at point p if A variable v is live at point p if v is used along some path starting at p, and v is used along some path starting at p, and no definition of v along the path before the use. no definition of v along the path before the use. When is a variable v dead at point p? When is a variable v dead at point p? No use of v on any path from p to exit node, or No use of v on any path from p to exit node, or If all paths from p redefine v before using v. If all paths from p redefine v before using v.

55 Fall 2011 “Advanced Compiler Techniques” What Use is Liveness Information? Register allocation. Register allocation. If a variable is dead, can reassign its register If a variable is dead, can reassign its register Dead code elimination. Dead code elimination. Eliminate assignments to variables not read later. Eliminate assignments to variables not read later. But must not eliminate last assignment to variable (such as instance variable) visible outside CFG. But must not eliminate last assignment to variable (such as instance variable) visible outside CFG. Can eliminate other dead assignments. Can eliminate other dead assignments. Handle by making all externally visible variables live on exit from CFG Handle by making all externally visible variables live on exit from CFG

56 Fall 2011 “Advanced Compiler Techniques” Conceptual Idea of Analysis Simulate execution Simulate execution But start from exit and go backwards in CFG But start from exit and go backwards in CFG Compute liveness information from end to beginning of basic blocks Compute liveness information from end to beginning of basic blocks

57 Fall 2011 “Advanced Compiler Techniques” Liveness Example a = x+y; t = a; c = a+x; x == 0 b = t+z; c = y+1; Assume a,b,c visible outside method Assume a,b,c visible outside method So are live on exit So are live on exit Assume x,y,z,t not visible Assume x,y,z,t not visible Represent Liveness Using Bit Vector Represent Liveness Using Bit Vector order is abcxyzt order is abcxyzt a b c x y z t

58 Fall 2011 “Advanced Compiler Techniques” Dead Code Elimination a = x+y; t = a; c = a+x; x == 0 b = t+z; c = y+1; Assume a,b,c visible outside method Assume a,b,c visible outside method So are live on exit So are live on exit Assume x,y,z,t not visible Assume x,y,z,t not visible Represent Liveness Using Bit Vector Represent Liveness Using Bit Vector order is abcxyzt order is abcxyzt a b c x y z t

59 Fall 2011 “Advanced Compiler Techniques” Formalizing Analysis Each basic block has Each basic block has IN - set of variables live at start of block IN - set of variables live at start of block OUT - set of variables live at end of block OUT - set of variables live at end of block USE - set of variables with upwards exposed uses in block (use prior to definition) USE - set of variables with upwards exposed uses in block (use prior to definition) DEF - set of variables defined in block prior to use DEF - set of variables defined in block prior to use USE[x = z; x = x+1;] = { z } (x not in USE) USE[x = z; x = x+1;] = { z } (x not in USE) DEF[x = z; x = x+1; y = 1;] = {x, y} DEF[x = z; x = x+1; y = 1;] = {x, y} Compiler scans each basic block to derive USE and DEF sets Compiler scans each basic block to derive USE and DEF sets

60 Fall 2011 “Advanced Compiler Techniques” Algorithm for all nodes n in N - { Exit } IN[n] = emptyset; OUT[Exit] = emptyset; IN[Exit] = use[Exit]; Changed = N - { Exit }; while (Changed != emptyset) choose a node n in Changed; choose a node n in Changed; Changed = Changed - { n }; Changed = Changed - { n }; OUT[n] = emptyset; for all nodes s in successors(n) for all nodes s in successors(n) OUT[n] = OUT[n] U IN[p]; IN[n] = use[n] U (out[n] - def[n]); if (IN[n] changed) if (IN[n] changed) for all nodes p in predecessors(n) for all nodes p in predecessors(n) Changed = Changed U { p }; Changed = Changed U { p };

61 Fall 2011 “Advanced Compiler Techniques” Similar to Other Dataflow Algorithms Backward analysis, not forward Backward analysis, not forward Still have transfer functions Still have transfer functions Still have confluence operators Still have confluence operators Can generalize framework to work for both forwards and backwards analyses Can generalize framework to work for both forwards and backwards analyses

62 Fall 2011 “Advanced Compiler Techniques” Comparison

63 Fall 2011 “Advanced Compiler Techniques” Comparison Available Expressions for all nodes n in N OUT[n] = E; IN[Entry] = emptyset; OUT[Entry] = GEN[Entry]; Changed = N - { Entry }; while (Changed != emptyset) choose a node n in Changed; choose a node n in Changed; Changed = Changed - { n }; Changed = Changed - { n }; IN[n] = E; IN[n] = E; for all nodes p in predecessors(n) for all nodes p in predecessors(n) IN[n] = IN[n]  OUT[p]; OUT[n] = GEN[n] U (IN[n] - KILL[n]); OUT[n] = GEN[n] U (IN[n] - KILL[n]); if (OUT[n] changed) if (OUT[n] changed) for all nodes s in successors(n) for all nodes s in successors(n) Changed = Changed U { s }; Reaching Definitions for all nodes n in N OUT[n] = emptyset; IN[Entry] = emptyset; OUT[Entry] = GEN[Entry]; Changed = N - { Entry }; while (Changed != emptyset) choose a node n in Changed; choose a node n in Changed; Changed = Changed - { n }; Changed = Changed - { n }; IN[n] = emptyset; IN[n] = emptyset; for all nodes p in predecessors(n) for all nodes p in predecessors(n) IN[n] = IN[n] U OUT[p]; OUT[n] = GEN[n] U (IN[n] - KILL[n]); OUT[n] = GEN[n] U (IN[n] - KILL[n]); if (OUT[n] changed) if (OUT[n] changed) for all nodes s in successors(n) for all nodes s in successors(n) Changed = Changed U { s }; Live Variables Live Variables for all nodes n in N - { Exit } IN[n] = emptyset; OUT[Exit] = emptyset; IN[Exit] = use[Exit]; Changed = N - { Exit }; while (Changed != emptyset) choose a node n in Changed; choose a node n in Changed; Changed = Changed - { n }; Changed = Changed - { n }; OUT[n] = emptyset; for all nodes s in successors(n) for all nodes s in successors(n) OUT[n] = OUT[n] U IN[p]; IN[n] = use[n] U (out[n] - def[n]); if (IN[n] changed) if (IN[n] changed) for all nodes p in predecessors(n) for all nodes p in predecessors(n) Changed = Changed U { p }; Changed = Changed U { p };

64 Fall 2011 “Advanced Compiler Techniques” Comparison Available Expressions for all nodes n in N OUT[n] = E; IN[Entry] = emptyset; OUT[Entry] = GEN[Entry]; Changed = N - { Entry }; while (Changed != emptyset) choose a node n in Changed; choose a node n in Changed; Changed = Changed - { n }; Changed = Changed - { n }; IN[n] = E; IN[n] = E; for all nodes p in predecessors(n) for all nodes p in predecessors(n) IN[n] = IN[n]  OUT[p]; OUT[n] = GEN[n] U (IN[n] - KILL[n]); OUT[n] = GEN[n] U (IN[n] - KILL[n]); if (OUT[n] changed) if (OUT[n] changed) for all nodes s in successors(n) for all nodes s in successors(n) Changed = Changed U { s }; Reaching Definitions for all nodes n in N OUT[n] = emptyset; IN[Entry] = emptyset; OUT[Entry] = GEN[Entry]; Changed = N - { Entry }; while (Changed != emptyset) choose a node n in Changed; choose a node n in Changed; Changed = Changed - { n }; Changed = Changed - { n }; IN[n] = emptyset; IN[n] = emptyset; for all nodes p in predecessors(n) for all nodes p in predecessors(n) IN[n] = IN[n] U OUT[p]; OUT[n] = GEN[n] U (IN[n] - KILL[n]); OUT[n] = GEN[n] U (IN[n] - KILL[n]); if (OUT[n] changed) if (OUT[n] changed) for all nodes s in successors(n) for all nodes s in successors(n) Changed = Changed U { s };

65 Fall 2011 “Advanced Compiler Techniques” Comparison Reaching Definitions for all nodes n in N OUT[n] = emptyset; IN[Entry] = emptyset; OUT[Entry] = GEN[Entry]; Changed = N - { Entry }; while (Changed != emptyset) choose a node n in Changed; choose a node n in Changed; Changed = Changed - { n }; Changed = Changed - { n }; IN[n] = emptyset; IN[n] = emptyset; for all nodes p in predecessors(n) for all nodes p in predecessors(n) IN[n] = IN[n] U OUT[p]; OUT[n] = GEN[n] U (IN[n] - KILL[n]); OUT[n] = GEN[n] U (IN[n] - KILL[n]); if (OUT[n] changed) if (OUT[n] changed) for all nodes s in successors(n) for all nodes s in successors(n) Changed = Changed U { s }; Live Variable Live Variable for all nodes n in N IN[n] = emptyset; OUT[Exit] = emptyset; IN[Exit] = use[Exit]; Changed = N - { Exit }; while (Changed != emptyset) choose a node n in Changed; choose a node n in Changed; Changed = Changed - { n }; Changed = Changed - { n }; OUT[n] = emptyset; for all nodes s in successors(n) for all nodes s in successors(n) OUT[n] = OUT[n] U IN[p]; IN[n] = use[n] U (out[n] - def[n]); if (IN[n] changed) if (IN[n] changed) for all nodes p in predecessors(n) for all nodes p in predecessors(n) Changed = Changed U { p }; Changed = Changed U { p };

66 Fall 2011 “Advanced Compiler Techniques” Pessimistic vs. Optimistic Analyses Available expressions is optimistic (for common sub-expression elimination) Available expressions is optimistic (for common sub-expression elimination) Assume expressions are available at start of analysis Assume expressions are available at start of analysis Analysis eliminates all that are not available Analysis eliminates all that are not available Cannot stop analysis early and use current result Cannot stop analysis early and use current result Live variables is pessimistic (for dead code elimination) Live variables is pessimistic (for dead code elimination) Assume all variables are live at start of analysis Assume all variables are live at start of analysis Analysis finds variables that are dead Analysis finds variables that are dead Can stop analysis early and use current result Can stop analysis early and use current result Dataflow setup same for both analyses Dataflow setup same for both analyses Optimism/pessimism depends on intended use Optimism/pessimism depends on intended use

67 Fall 2011 “Advanced Compiler Techniques” Summary Dataflow Analysis Dataflow Analysis Control flow graph Control flow graph IN[b], OUT[b], transfer functions, join points IN[b], OUT[b], transfer functions, join points Paired analyses and transformations Paired analyses and transformations Reaching definitions/constant propagation Reaching definitions/constant propagation Available expressions/common sub-expression elimination Available expressions/common sub-expression elimination Live-variable analysis/Dead code elimination Live-variable analysis/Dead code elimination Stacked analysis and transformations work together Stacked analysis and transformations work together

68 Fall 2011 “Advanced Compiler Techniques” Next Time Data Flow Analysis: Foundation Data Flow Analysis: Foundation DragonBook: §9.3 DragonBook: §9.3