(How Not To Do) Global Optimizations

Similar documents
Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization

Lecture Outline. Global flow analysis. Global Optimization. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization

Topics. Lecture 37: Global Optimization. Issues. A Simple Example: Copy Propagation X := 3 B > 0 Y := 0 X := 4 Y := Z + W A := 2 * 3X

Static and Dataflow Analysis. (two part lecture)

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

More Static Semantics. Static. One-Slide Summary. Lecture Outline. Typing Rules. Dispatch Rules SELF_TYPE

Dataflow Analysis. Xiaokang Qiu Purdue University. October 17, 2018 ECE 468

Operational Semantics. One-Slide Summary. Lecture Outline

Limitations of Algorithmic Solvability In this Chapter we investigate the power of algorithms to solve problems Some can be solved algorithmically and

CFG (Control flow graph)

Static Analysis. Systems and Internet Infrastructure Security

CSE Section 10 - Dataflow and Single Static Assignment - Solutions

Data-Flow Analysis Foundations

More Static Semantics

Static Program Analysis

Lecture 9 Arc Consistency

Class 6. Review; questions Assign (see Schedule for links) Slicing overview (cont d) Problem Set 3: due 9/8/09. Program Slicing

Operational Semantics 1 / 13

CS553 Lecture Generalizing Data-flow Analysis 3

In One Slide. Outline. LR Parsing. Table Construction

Example. Example. Constant Propagation in SSA

CSE 501 Midterm Exam: Sketch of Some Plausible Solutions Winter 1997

More Static Semantics

Shared Variables and Interference

Lecture 5 - Axiomatic semantics

Lecture Compiler Middle-End

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

CS 125 Section #4 RAMs and TMs 9/27/16

What If. Static Single Assignment Form. Phi Functions. What does φ(v i,v j ) Mean?

Static Program Analysis

Lecture 3: Recursion; Structural Induction

Normal Forms for CFG s. Eliminating Useless Variables Removing Epsilon Removing Unit Productions Chomsky Normal Form

Programming Languages Fall 2014

Goals of Program Optimization (1 of 2)

Recursively Enumerable Languages, Turing Machines, and Decidability

(Refer Slide Time: 02.06)

Outline. Introduction. 2 Proof of Correctness. 3 Final Notes. Precondition P 1 : Inputs include

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

CS2 Algorithms and Data Structures Note 10. Depth-First Search and Topological Sorting

Passing Out Review Forms

Compiler Optimisation

Software Testing Prof. Rajib Mall Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur. Lecture 13 Path Testing

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

CS153: Compilers Lecture 23: Static Single Assignment Form

Intermediate representations IR #1: CPS/L 3. Code example. Advanced Compiler Construction Michel Schinz

Computation Club: Gödel s theorem

An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection

Notes slides from before lecture. CSE 21, Winter 2017, Section A00. Lecture 9 Notes. Class URL:

CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis

Lecture Outline. Type systems and their expressiveness. Type Checking in COOL (II) Type checking with SELF_TYPE in COOL

Introduction to Denotational Semantics. Class Likes/Dislikes Survey. Dueling Semantics. Denotational Semantics Learning Goals. You re On Jeopardy!

One-Slide Summary. Lecture Outine. Automatic Memory Management #1. Why Automatic Memory Management? Garbage Collection.

Rice s Theorem and Enumeration

LR Parsing. Table Construction

Data-flow Analysis - Part 2

4.7 Approximate Integration

Shared Variables and Interference

Abstract Interpretation Using Laziness: Proving Conway s Lost Cosmological Theorem

More Dataflow Analysis

Lecture 2: NP-Completeness

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

(Refer Slide Time: 01.26)

CSE 501: Compiler Construction. Course outline. Goals for language implementation. Why study compilers? Models of compilation

Principles of Program Analysis. Lecture 1 Harry Xu Spring 2013

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

CSE 142 Su01 Midterm 1 Sample Solution page 1 of 5

Static Analysis and Dataflow Analysis

University of Nevada, Las Vegas Computer Science 456/656 Fall 2016

Operational Semantics of Cool

Thursday, December 23, The attack model: Static Program Analysis

Abstract Interpretation Continued

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Introduction to Parsing. Lecture 5

Contents of Lecture 2

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Statements or Basic Blocks (Maximal sequence of code with branching only allowed at end) Possible transfer of control

Midterm II CS164, Spring 2006

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

CMSC 330: Organization of Programming Languages. OCaml Higher Order Functions

Topic I (d): Static Single Assignment Form (SSA)

Lectures 20, 21: Axiomatic Semantics

UNIT 14C The Limits of Computing: Non computable Functions. Problem Classifications

Combining Optimizations: Sparse Conditional Constant Propagation

Data Flow Analysis. Program Analysis

Operational Semantics

Program verification. Generalities about software Verification Model Checking. September 20, 2016

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Compilers. Optimization. Yannis Smaragdakis, U. Athens

Program analysis for determining opportunities for optimization: 2. analysis: dataow Organization 1. What kind of optimizations are useful? lattice al

On Meaning Preservation of a Calculus of Records

Subsumption. Principle of safe substitution

AP Calculus. Extreme Values: Graphically. Slide 1 / 163 Slide 2 / 163. Slide 4 / 163. Slide 3 / 163. Slide 5 / 163. Slide 6 / 163

ASYMPTOTIC COMPLEXITY

Lecture Notes on Value Propagation

Graphs. The ultimate data structure. graphs 1

CS143 Final Spring 2016

CSE P 501 Compilers. SSA Hal Perkins Spring UW CSE P 501 Spring 2018 V-1

CSC D70: Compiler Optimization Register Allocation

CpSc 421 Final Solutions

Transcription:

(How Not To Do) Global Optimizations #1

One-Slide Summary A global optimization changes an entire method (consisting of multiple basic blocks). We must be conservative and only apply global optimizations when they preserve the original semantics. We use global flow analyses to determine if it is OK to apply an optimization. Flow analyses are built out of simple transfer functions and can work forwards or backwards. #2

Lecture Outline Global flow analysis Global constant propagation Liveness analysis #3

Local Optimization Recall the simple basic-block optimizations Constant propagation Dead code elimination X := 3 X := 3 Y := Z * W Y := Z * W Y := Z * W Q := X + Y Q := 3 + Y Q := 3 + Y #4

Global Optimization These optimizations can be extended to an entire control-flow graph X := 3 B>0 Y := Z + W Y := 0 A := 2 * X #5

Global Optimization These optimizations can be extended to an entire control-flow graph X := 3 B>0 Y := Z + W Y := 0 A := 2 * X #6

Global Optimization These optimizations can be extended to an entire control-flow graph X := 3 B>0 Y := Z + W Y := 0 A := 2 * 3 #7

Correctness How do we know it is OK to globally propagate constants? There are situations where it is incorrect: X := 3 B>0 Y := Z + W Y := 0 X := 4 A := 2 * X #8

Correctness (Cont.) To replace a use of x by a constant k we must know this correctness condition: On every path to the use of x, the last assignment to x is x := k ** #9

Example 1 Revisited X := 3 B>0 Y := Z + W Y := 0 A := 2 * X #10

Example 2 Revisited X := 3 B>0 Y := Z + W Y := 0 X := 4 A := 2 * X #11

Discussion The correctness condition is not trivial to check All paths includes paths around loops and through branches of conditionals Checking the condition requires global analysis Global = an analysis of the entire control-flow graph for one method body #12

Global Analysis Global optimization tasks share several traits: The optimization depends on knowing a property P at a particular point in program execution Proving P at any point requires knowledge of the entire method body Property P is typically undecidable! #13

Undecidability of Program Properties Rice s Theorem: Most interesting dynamic properties of a program are undecidable: Does the program halt on all (some) inputs? This is called the halting problem Is the result of a function F always positive? Assume we can answer this question precisely Take function H and find out if it halts by testing function F(x) = { H(x); return 1; } whether it has positive result Contradition! Syntactic properties are decidable! e.g., How many occurrences of x are there? Programs without looping are also decidable! #14

Conservative Program Analyses So, we cannot tell for sure that x is always 3 Then, how can we apply constant propagation? It is OK to be conservative. #15

Conservative Program Analyses So, we cannot tell for sure that x is always 3 Then, how can we apply constant propagation? It is OK to be conservative. If the optimization requires P to be true, then want to know either P is definitely true Don t know if P is true Let's call this truthiness #16

Conservative Program Analyses So, we cannot tell for sure that x is always 3 Then, how can we apply constant propagation? It is OK to be conservative. If the optimization requires P to be true, then want to know either P is definitely true Don t know if P is true It is always correct to say don t know We try to say don t know as rarely as possible All program analyses are conservative #17

Global Optimization: Review X := 3 B>0 Y := Z + W Y := 0 A := 2 * X #18

Global Optimization: Review X := 3 X := 3 B>0 B>0 Y := Z + W Y := 0 Y := Z + W Y := 0 X := 4 A := 2 * 3 A := 2 * X #19

Global Optimization: Review X := 3 X := 3 B>0 B>0 Y := Z + W Y := 0 Y := Z + W Y := 0 X := 4 A := 2 * 3 A := 2 * 3 To replace a use of x by a constant k we must know that: On every path to the use of x, the last assignment to x is x := k ** #20

Review The correctness condition is hard to check Checking it requires global analysis An analysis of the entire control-flow graph for one method body We said that was impossible, right? #21

Global Analysis Global dataflow analysis is a standard technique for solving problems with these characteristics Global constant propagation is one example of an optimization that requires global dataflow analysis #22

Global Constant Propagation Global constant propagation can be performed at any point where ** holds Consider the case of computing ** for a single variable X at all program points Valid points cannot hide! We will find you! (sometimes) #23

Global Constant Propagation (Cont.) To make the problem precise, we associate one of the following values with X at every program point value interpretation # c * This statement is not reachable X = constant c Don t know if X is a constant #24

Example Get out a piece of paper. Let's fill in these blanks now. X := 3 X= X= X= B>0 Y := Z + W X := 4 X= X=* X= Y := 0 A := 2 * X X= X= Recall: # = not reachable, c = constant, * = don't know. #25

Example Answers X := 3 X=3 X=3 X=3 B>0 Y := Z + W X := 4 X=4 X=* X=3 Y := 0 A := 2 * X X=* X=3 X=* #26

Using the Information Given global constant information, it is easy to perform the optimization Simply inspect the x =? associated with a statement using x If x is constant at that point replace that use of x by the constant But how do we compute the properties x =? #27

The Idea The analysis of a complicated program can be expressed as a combination of simple rules relating the change in information between adjacent statements #28

Explanation The idea is to push or transfer information from one statement to the next For each statement s, we compute information about the value of x immediately before and after s Cin(x,s) = value of x before s Cout(x,s) = value of x after s #29

Transfer Functions Define a transfer function that transfers information from one statement to another #30

Rule 1 X=# s X=# Cout(x, s) = # if Cin(x, s) = # Recall: # = unreachable code #31

Rule 2 X=? x := c X=c Cout(x, x := c) = c if c is a constant #32

Rule 3 X=? x := f( ) X=* Cout(x, x := f( )) = * This is a conservative approximation! It might be possible to figure out that f(...) always returns 5, but we won't even try! #33

Rule 4 X=a y :=... X=a Cout(x, y := ) = Cin(x, y := ) if x y #34

The Other Half Rules 1-4 relate the in of a statement to the out of the same statement they propagate information across statements Now we need rules relating the out of one statement to the in of the successor statement to propagate information forward across CFG edges In the following rules, let statement s have immediate predecessor statements p1,,pn #35

Rule 5 X=? X=* X=? s X=? X=* if Cout(x, pi) = * for some i, then Cin(x, s) = * #36

Rule 6 X=c X=d X=? s X=? X=* if Cout(x, pi) = c and Cout(x, pj) = d and d c then Cin (x, s) = * #37

Rule 7 X=c X=c X=# s X=# X=c if Cout(x, pi) = c or # for all i, then Cin(x, s) = c #38

Rule 8 X=# X=# X=# s X=# X=# if Cout(x, pi) = # for all i, then Cin(x, s) = # #39

An Algorithm For every entry s to the program, set Cin(x, s) = * Set Cin(x, s) = Cout(x, s) = # everywhere else Repeat until all points satisfy 1-8: Pick s not satisfying 1-8 and update using the appropriate rule #40

The Value # To understand why we need #, look at a loop X := 3 X=3 B>0 Y := Z + W X=* X=3 X=3 Y := 0 X=3 A := 2 * X A<B #41

The Value # To understand why we need #, look at a loop X := 3 X=3 B>0 Y := Z + W X=* X=3 X=3 Y := 0 X=3 X =??? A := 2 * X A<B X =??? X =??? #42

The Value # (Cont.) Because of cycles, all points must have values at all times during the analysis Intuitively, assigning some initial value allows the analysis to break cycles The initial value # means so far as we know, control never reaches this point #43

Sometimes all paths lead to the same place. Thus you need #. #44

Example X := 3 X=# 3 B>0 Y := Z + W X=# X=* 3 X=# X=# Y := 0 3 A := 2 * X A<B X=# X=# X=# 3 3 3 3 We are done when all rules are satisfied! #45

Another Example X := 3 Let's do it on paper! B>0 Y := Z + W Y := 0 A := 2 * X X := 4 A<B #46

Another Example: Answer X := 3 X=# 3 B>0 Y := Z + W X=# X=* X=# 3 X=# 3 Y := 0 3 A := 2 * X X := 4 A<B X=# X=# 4 3 * X=# 3 * X=# 4 Must continue until all rules are satisfied! #47

Orderings We can simplify the presentation of the analysis by ordering the values # < c < * Drawing a picture with lower values drawn lower, we get * -1 0 1 # #48

Orderings (Cont.) * is the greatest value, # is the least All constants are in between and incomparable Let lub be the least-upper bound in this ordering Rules 5-8 can be written using lub: Cin(x, s) = lub { Cout(x, p) p is a predecessor of s } #49

Termination Simply saying repeat until nothing changes doesn t guarantee that eventually nothing changes The use of lub explains why the algorithm terminates Values start as # and only increase # can change to a constant, and a constant to * Thus, C_(x, s) can change at most twice #50

Number Crunching The algorithm is polynomial in program size: Number of steps = Number of C_(.) values changed * 2 = 2 (Number of program statements) * 2 #51

Liveness Analysis Once constants have been globally propagated, we would like to eliminate dead code X := 3 B>0 Y := Z + W Y := 0 A := 2 * X After constant propagation, X := 3 is dead (assuming this is the entire CFG) #52

Live and Dead The first value of x is dead (never used) X := 3 The second value of x is live (may be used) X := 4 Liveness is an important concept Y := X #53

Liveness A variable x is live at statement s if There exists a statement s that uses x There is a path from s to s That path has no intervening assignment to x #54

Global Dead Code Elimination A statement x := is dead code if x is dead after the assignment Dead code can be deleted from the program But we need liveness information first... #55

Computing Liveness We can express liveness in terms of information transferred between adjacent statements, just as in constant propagation Liveness is simpler than constant propagation, since it is a boolean property (true or false) #56

Liveness Rule 1 X = true := x + X=? Lin(x, s) = true if s refers to x on the rhs #57

Liveness Rule 2 X = false x := e X=? Lin(x, x := e) = false if e does not refer to x #58

Liveness Rule 3 X=a s X=a Lin(x, s) = Lout(x, s) if s does not refer to x #59

Liveness Rule 4 p X=? X=? X = true X = true X=? Lout(x, p) = { Lin(x, s) s a successor of p } #60

Algorithm Let all L_( ) = false initially Repeat process until all statements s satisfy rules 1-4 : Pick s where one of 1-4 does not hold and update using the appropriate rule #61

Liveness Example X := 3 L(X) = false B>0 Y := Z + W L(X) = false L(X) = false L(X) = false Y := 0 L(X) = false X := X * X X := 4 A<B L(X) = false L(X) = false L(X) = false L(X) = false L(X) = false L(X) = false #62

Liveness Example Answers X := 3 L(X) = false true B>0 Y := Z + W L(X) = false true Dead code X := X * X X := 4 A<B L(X) = false L(X) = false true L(X) = false true L(X) = false true Y := 0 L(X) = false true L(X) = false true L(X) = false L(X) = false true true L(X) = false #63

Liveness Example Answers Also dead code? L(X) = false true X := 3 B>0 Y := Z + W L(X) = false true Dead code X := X * X X := 4 A<B L(X) = false L(X) = false true L(X) = false true L(X) = false true Y := 0 L(X) = false true L(X) = false true L(X) = false L(X) = false true true L(X) = false #64

Termination A value can change from false to true, but not the other way around Each value can change only once, so termination is guaranteed Once the analysis is computed, it is simple to eliminate dead code #65

Forward vs. Backward Analysis We ve seen two kinds of analysis: Constant propagation is a forwards analysis: information is pushed from inputs to outputs Liveness is a backwards analysis: information is pushed from outputs back towards inputs #66

Analysis Analysis There are many other global flow analyses Most can be classified as either forward or backward Most also follow the methodology of local rules relating information between adjacent program points #67

Homework WA6 Due next Read chapter 7.7 Optional David Bacon article Midterm 2 Tue Nov 24 #68