MIT 6.035 Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.

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

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.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
SSA.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
Jeffrey D. Ullman Stanford University. 2  Generalizes: 1.Moving loop-invariant computations outside the loop. 2.Eliminating common subexpressions. 3.True.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
Program Representations. Representing programs Goals.
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 & Lazy Code Motion
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 Copy Propagation What does it mean? Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.
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,
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
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)
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.
1 Copy Propagation What does it mean? – Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.
Improving Code Generation Honors Compilers April 16 th 2002.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
Introduction to Optimization Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
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.
Optimization software for apeNEXT Max Lukyanov,  apeNEXT : a VLIW architecture  Optimization basics  Software optimizer for apeNEXT  Current.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
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 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
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.
Credible Compilation With Pointers Martin Rinard and Darko Marinov Laboratory for Computer Science Massachusetts Institute of Technology.
Data Flow Analysis Suman Jana
Lecture 5 Partial Redundancy Elimination
Basic Block Optimizations
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 using SSA
Fall Compiler Principles Lecture 10: Loop Optimizations
Data Flow Analysis Compiler Design
Final Code Generation and Code Optimization
Static Single Assignment
Live variables and copy propagation
COMPILERS Liveness Analysis
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Live Variables – Basic Block
Basic Block Optimizations
Presentation transcript:

MIT Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Program Analysis Compile-time reasoning about run-time behavior of program –Can discover things that are always true: “x is always 1 in the statement y = x + z” “the pointer p always points into array a” “the statement return 5 can never execute” –Can infer things that are likely to be true: “the reference r usually refers to an object of class C” “the statement a = b + c appears to execute more frequently than the statement x = y + z” –Distinction between data and control-flow properties

Transformations Use analysis results to transform program Overall goal: improve some aspect of program Traditional goals: –Reduce number of executed instructions –Reduce overall code size Other goals emerge as space becomes more complex –Reduce number of cycles Use vector or DSP instructions Improve instruction or data cache hit rate –Reduce power consumption –Reduce memory usage

Control Flow Graph Nodes Represent Computation –Each Node is a Basic Block –Basic Block is a Sequence of Instructions with No Branches Out Of Middle of Basic Block No Branches Into Middle of Basic Block Basic Blocks should be maximal –Execution of basic block starts with first instruction –Includes all instructions in basic block Edges Represent Control Flow

Control Flow Graph into add(n, k) { s = 0; a = 4; i = 0; if (k == 0) b = 1; else b = 2; while (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;

Basic Block Construction s = 0; a = 4; Start with instruction control-flow graph Visit all edges in graph Merge adjacent nodes if –Only one edge from first node –Only one edge into second node s = 0; a = 4;

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;

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;

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

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 = 2;

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 = 2; i < n

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 = 2; i < n 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; s = 0; a = 4; i = 0; k == 0 b = 2; i < n s = s + a*b; i = i + 1;

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 = 2; i < n s = s + a*b; i = i + 1;

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 = 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; 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; 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; s = 0; a = 4; i = 0; k == 0 b = 1;b = 2; i < n s = s + a*b; i = i + 1; return s;

Program Points, Split and Join Points One program point before and after each statement in program Split point has multiple successors – conditional branch statements only split points Merge point has multiple predecessors Each basic block –Either starts with a merge point or its predecessor ends with a split point –Either ends with a split point or its successor starts with a merge point

Two Kinds of Variables Temporaries Introduced By Compiler –Transfer values only within basic block –Introduced as part of instruction flattening –Introduced by optimizations/transformations –Typically assigned to only once Program Variables –Declared in original program –May be assigned to multiple times –May transfer values between basic blocks

Basic Block Optimizations Common Sub- Expression Elimination –a = (x+y)+z; b = x+y; –t = x+y; a = t+z; b = t; Constant Propagation –x = 5; b = x+y; –b = 5+y; Algebraic Identities –a = x * 1; –a = x; Copy Propagation –a = x+y; b = a; c = b+z; –a = x+y; b = a; c = a+z; Dead Code Elimination –a = x+y; b = a; c = a+z; –a = x+y; c = a+z Strength Reduction –t = i * 4; –t = i << 2;

Basic Block Analysis Approach Assume normalized basic block - all statements are of the form –var = var op var (where op is a binary operator) –var = op var (where op is a unary operator) –var = var Simulate a symbolic execution of basic block –Reason about values of variables (or other aspects of computation) –Derive property of interest

Value Numbering Reason about values of variables and expressions in the program –Simulate execution of basic block –Assign virtual value to each variable and expression Discovered property: which variables and expressions have the same value Standard use: –Common subexpression elimination –Typically combined with transformation that Saves computed values in temporaries Replaces expressions with temporaries when value of expression previously computed

b  v5 b  v6 a = x+y b = a+z b = b+y c = a+z a = x+y t1 = a b = a+z t2 = b b = b+y t3 = b x  v1 y  v2 a  v3 z  v4 c  v5 Original Basic Block New Basic Block Var to Val v1+v2  v3 v3+v4  v5 Exp to Val v1+v2  t1 v3+v4  t2 Exp to Tmp c = t2 v5+v2  v6 v5+v2  t3

Value Numbering Summary Forward symbolic execution of basic block Each new value assigned to temporary –a = x+y; becomes a = x+y; t = a; –Temporary preserves value for use later in program even if original variable rewritten a = x+y; a = a+z; b = x+y becomes a = x+y; t = a; a = a+z; b = t; Maps –Var to Val – specifies symbolic value for each variable –Exp to Val – specifies value of each evaluated expression –Exp to Tmp – specifies tmp that holds value of each evaluated expression

Map Usage Var to Val –Used to compute symbolic value of y and z when processing statement of form x = y + z Exp to Tmp –Used to determine which tmp to use if value(y) + value(z) previously computed when processing statement of form x = y + z Exp to Val –Used to update Var to Val when processing statement of the form x = y + z, and value(y) + value(z) previously computed

Interesting Properties Finds common subexpressions even if they use different variables in expressions –y = a+b; x = b; z = a+x becomes –y = a+b; t = y; x = b; z = t –Why? Because computes with symbolic values Finds common subexpressions even if variable that originally held the value was overwritten –y = a+b; x = b; y = 1; z = a+x becomes –y = a+b; t = y; x = b; y = 1; z = t –Why? Because saves values away in temporaries

One More Interesting Property Flattening and CSE combine to capture partial and arbitrarily complex common subexpressions –w = (a+b)+c; x = b; y = (a+x)+c; z = a+b; –After flattening: –t1 = a+b; w = t1+c; x = b; t2 = a+x; y = t2 + c; z = a+b; –CSE algorithm notices that t1+c and t2+c compute same value In the statement z = a+b, a+b has already been computed so generated code can reuse the result –t1=a+b; w = t1+c; t3 = w; x = b; t2=a+x; y = t3; z = t1;

Problems Algorithm has a temporary for each new value –a = x+y; t1 = a; Introduces –lots of temporaries –lots of copy statements to temporaries In many cases, temporaries and copy statements are unnecessary So we eliminate them with copy propagation and dead code elimination

Copy Propagation Once again, simulate execution of program If can, use original variable instead of temporary –a = x+y; b = x+y; –After CSE becomes a = x+y; t = a; b = t; –After CP becomes a = x+y; b = a; Key idea: –determine when original variable is NOT overwritten between its assignment statement and the use of the computed value –If not overwritten, use original variable

Copy Propagation Maps Maintain two maps –tmp to var: tells which variable to use instead of a given temporary variable –var to set: inverse of tmp to var. tells which temps are mapped to a given variable by tmp to var

Copy Propagation Example Original a = x+y b = a+z c = x+y a = b After CSE a = x+y t1 = a b = a+z t2 = b c = t1 a = b After CSE and Copy Propagation a = x+y t1 = a b = a+z t2 = b c = a a = b

Copy Propagation Example a = x+y t1 = a Basic Block After CSE a = x+y t1 = a Basic Block After CSE and Copy Prop tmp to varvar to set t1  aa  {t1}

Copy Propagation Example a = x+y t1 = a b = a+z t2 = b Basic Block After CSE a = x+y t1 = a b = a+z t2 = b Basic Block After CSE and Copy Prop tmp to varvar to set t1  a t2  b a  {t1} b  {t2}

Copy Propagation Example a = x+y t1 = a b = a+z t2 = b c = t1 Basic Block After CSE a = x+y t1 = a b = a+z t2 = b Basic Block After CSE and Copy Prop tmp to varvar to set t1  a t2  b a  {t1} b  {t2}

Copy Propagation Example a = x+y t1 = a b = a+z t2 = b c = t1 Basic Block After CSE a = x+y t1 = a b = a+z t2 = b c = a Basic Block After CSE and Copy Prop tmp to varvar to set t1  a t2  b a  {t1} b  {t2}

Copy Propagation Example a = x+y t1 = a b = a+z t2 = b c = t1 a = b Basic Block After CSE a = x+y t1 = a b = a+z t2 = b c = a a = b Basic Block After CSE and Copy Prop tmp to varvar to set t1  a t2  b a  {t1} b  {t2}

Copy Propagation Example a = x+y t1 = a b = a+z t2 = b c = t1 a = b Basic Block After CSE a = x+y t1 = a b = a+z t2 = b c = a a = b Basic Block After CSE and Copy Prop tmp to varvar to set t1  t1 t2  b a  {} b  {t2}

Dead Code Elimination Copy propagation keeps all temps around May be temps that are never read Dead Code Elimination removes them a = x+y t1 = a b = a+z t2 = b c = a a = b a = x+y b = a+z c = a a = b Basic Block After CSE and Copy Prop Basic Block After CSE and Copy Prop

Dead Code Elimination Basic Idea –Process Code In Reverse Execution Order –Maintain a set of variables that are needed later in computation –If encounter an assignment to a temporary that is not needed, remove assignment

a = x+y t1 = a b = a+z t2 = b c = a a = b Basic Block After CSE and Copy Prop Needed Set {b}

a = x+y t1 = a b = a+z t2 = b c = a a = b Basic Block After CSE and Copy Prop Needed Set {a, b}

a = x+y t1 = a b = a+z t2 = b c = a a = b Basic Block After CSE and Copy Prop Needed Set {a, b}

a = x+y t1 = a b = a+z c = a a = b Basic Block After CSE and Copy Prop Needed Set {a, b}

a = x+y t1 = a b = a+z c = a a = b Basic Block After CSE and Copy Prop Needed Set {a, b, z}

a = x+y t1 = a b = a+z c = a a = b Basic Block After CSE and Copy Prop Needed Set {a, b, z}

a = x+y b = a+z c = a a = b Basic Block After CSE and Copy Prop Needed Set {a, b, z}

a = x+y b = a+z c = a a = b Basic Block After, CSE Copy Propagation, and Dead Code Elimination Needed Set {a, b, z}

a = x+y b = a+z c = a a = b Basic Block After, CSE Copy Propagation, and Dead Code Elimination Needed Set {a, b, z}

Interesting Properties Analysis and Transformation Algorithms Symbolically Simulate Execution of Program –CSE and Copy Propagation go forward –Dead Code Elimination goes backwards Transformations stacked –Group of basic transformations work together –Often, one transformation creates inefficient code that is cleaned up by following transformations –Transformations can be useful even if original code may not benefit from transformation

Other Basic Block Transformations Constant Propagation Strength Reduction –a << 2 = a * 4; a + a + a = 3 * a; Algebraic Simplification –a = a * 1; b = b + 0; Do these in unified transformation framework, not in earlier or later phases

Summary Basic block analyses and transformations Symbolically simulate execution of program –Forward (CSE, copy prop, constant prop) –Backward (Dead code elimination) Stacked groups of analyses and transformations that work together –CSE introduces excess temporaries and copy statements –Copy propagation often eliminates need to keep temporary variables around –Dead code elimination removes useless code Similar in spirit to many analyses and transformations that operate across basic blocks

MIT Introduction to Dataflow Analysis Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Dataflow Analysis Used to determine properties of program that involve multiple basic blocks Typically used to enable transformations –common sub-expression elimination –constant and copy propagation –dead code elimination Analysis and transformation often come in pairs

Reaching Definitions Concept of definition and use –a = x+y –is a definition of a –is a use of x and y A definition reaches a use if –value written by definition –may be read by use

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

Reaching Definitions and Constant Propagation Is a use of a variable a constant? –Check all reaching definitions –If all assign variable to same constant –Then use is in fact a constant Can replace variable with constant

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

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

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

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

1: s = 0; 2: a = 4; 3: i = 0; k == 0 4: b = 1;5: b = 2; i < n 6: s = s + a*b; 7: i = i + 1; return s

Formalizing Analysis Each basic block has –IN - set of definitions that reach beginning of block –OUT - set of definitions that reach end of block –GEN - set of definitions generated in block –KILL - set of definitions killed in in block GEN[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

Dataflow Equations IN[b] = OUT[b1] U... U OUT[bn] –where b1,..., bn are predecessors of b in CFG OUT[b] = (IN[b] - KILL[b]) U GEN[b] IN[entry] = Result: system of equations

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

Reaching Definitions Algorithm for all nodes n in N OUT[n] = emptyset; // OUT[n] = GEN[n]; Changed = D; // D = all definitions in graph while (Changed != emptyset) choose a node n in Changed; Changed = Changed - { n }; IN[n] = emptyset; for all nodes p in predecessors(n) IN[n] = IN[n] U OUT[p]; OUT[n] = GEN[n] U (IN[n] - KILL[n]); if (OUT[n] changed) for all nodes s in successors(n) Changed = Changed U { s };

Questions Does the algorithm halt? –yes, because transfer function is monotonic –if increase IN, increase OUT –in limit, all bits are 1 If bit is 1, is there always an execution in which corresponding definition reaches basic block? If bit is 0, does the corresponding definition ever reach basic block? Concept of conservative analysis

Available Expressions An expression x+y is available at a point p if –every path from the initial node to p evaluates x+y before reaching 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 If expression is available at use, no need to reevaluate it

Computing Available Expressions Represent sets of expressions using bit vectors Each expression corresponds to a bit Run dataflow algorithm similar to reaching definitions Big difference –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

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==

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

Formalizing Analysis Each basic block has –IN - set of expressions available at start of block –OUT - set of expressions available at end of block –GEN - set of expressions computed in block –KILL - set of expressions killed in in block GEN[x = z; b = x+y] = 1000 KILL[x = z; b = x+y] = 1001 Compiler scans each basic block to derive GEN and KILL sets

Dataflow Equations IN[b] = OUT[b1] intersect... intersect OUT[bn] –where b1,..., bn are predecessors of b in CFG OUT[b] = (IN[b] - KILL[b]) U GEN[b] IN[entry] = 0000 Result: system of equations

Solving Equations Use fixed point algorithm IN[entry] = 0000 Initialize OUT[b] = 1111 Repeatedly apply equations –IN[b] = OUT[b1] intersect... intersect OUT[bn] –OUT[b] = (IN[b] - KILL[b]) U GEN[b] Use a worklist algorithm to reach fixed point

Available Expressions Algorithm for all nodes n in N OUT[n] = E; // OUT[n] = E - KILL[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; Changed = Changed - { n }; IN[n] = E; // E is set of all expressions for all nodes p in predecessors(n) IN[n] = IN[n] intersect OUT[p]; OUT[n] = GEN[n] U (IN[n] - KILL[n]); if (OUT[n] changed) for all nodes s in successors(n) Changed = Changed U { s };

Questions Does algorithm always halt? 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? In what sense is algorithm conservative?

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

Liveness Analysis A variable v is live at point p if –v is used along some path starting at p, and –no definition of v along the path before the use. When is a variable v dead at point p? –No use of v on any path from p to exit node, or –If all paths from p redefine v before using v.

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

Conceptual Idea of Analysis Simulate execution But start from exit and go backwards in CFG Compute liveness information from end to beginning of basic blocks

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 So are live on exit Assume x,y,z,t not visible Represent Liveness Using Bit Vector –order is abcxyzt

Dead Code Elimination a = x+y; t = a; x == 0 b = t+z; c = y+1; Assume a,b,c visible outside method So are live on exit Assume x,y,z,t not visible Represent Liveness Using Bit Vector –order is abcxyzt

Formalizing Analysis Each basic block has –IN - set of variables live at start of block –OUT - set of variables live at end of block –USE - set of variables with upwards exposed uses in block –DEF - set of variables defined in block USE[x = z; x = x+1;] = { z } (x not in USE) DEF[x = z; x = x+1;y = 1;] = {x, y} Compiler scans each basic block to derive GEN and KILL sets

Algorithm out[Exit] = emptyset; in[Exit] = use[n]; for all nodes n in N - { Exit } in[n] = emptyset; Changed = N - { Exit }; while (Changed != emptyset) choose a node n in Changed; Changed = Changed - { n }; out[n] = emptyset; 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) for all nodes p in predecessors(n) Changed = Changed U { p };

Similar to Other Dataflow Algorithms Backwards analysis, not forwards Still have transfer functions Still have confluence operators Can generalize framework to work for both forwards and backwards analyses

Analysis Information Inside Basic Blocks One detail: –Given dataflow information at IN and OUT of node –Also need to compute information at each statement of basic block –Simple propagation algorithm usually works fine –Can be viewed as restricted case of dataflow analysis

Summary Basic Blocks and Basic Block Optimizations –Copy and constant propagation –Common sub-expression elimination –Dead code elimination Dataflow Analysis –Control flow graph –IN[b], OUT[b], transfer functions, join points Paired analyses and transformations –Reaching definitions/constant propagation –Available expressions/common sub-expression elimination –Liveness analysis/Dead code elimination Stacked analysis and transformations work together