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

Similar documents
(How Not To Do) Global Optimizations

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

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

CSE Section 10 - Dataflow and Single Static Assignment - Solutions

Lecture Compiler Middle-End

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

Data-flow Analysis - Part 2

Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore

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

Data-Flow Analysis Foundations

More Dataflow Analysis

Compiler Optimisation

Midterm II CS164, Spring 2006

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

Operational Semantics 1 / 13

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

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

Programming Languages Fall 2014

CS153: Compilers Lecture 23: Static Single Assignment Form

Calvin Lin The University of Texas at Austin

Abstract Interpretation Continued

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

CS143 Final Spring 2016

CSC D70: Compiler Optimization Register Allocation

CS 6110 S14 Lecture 38 Abstract Interpretation 30 April 2014

Lecture Notes on Value Propagation

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

A Gentle Introduction to Program Analysis

CS553 Lecture Generalizing Data-flow Analysis 3

Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

Intermediate Code & Local Optimizations. Lecture 20

Data Flow Analysis. Program Analysis

Fall 2004 CS414 Prelim 1

Lecture 3: Recursion; Structural Induction

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

Optimizations. Optimization Safety. Optimization Safety CS412/CS413. Introduction to Compilers Tim Teitelbaum

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

CS 4120 Lecture 31 Interprocedural analysis, fixed-point algorithms 9 November 2011 Lecturer: Andrew Myers

CIS 341 Final Examination 4 May 2017

Principles of Program Analysis. Lecture 1 Harry Xu Spring 2013

Static Analysis. Systems and Internet Infrastructure Security

Compiler Optimisation

Advanced Programming Methods. Introduction in program analysis

More Static Semantics

Introduction to Machine-Independent Optimizations - 6

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

CFG (Control flow graph)

Lecture Notes on Liveness Analysis

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Goals of Program Optimization (1 of 2)

Register Allocation. Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations

Contents of Lecture 2

(Refer Slide Time: 02.06)

1 Leaffix Scan, Rootfix Scan, Tree Size, and Depth

Register Allocation. Lecture 16

Calvin Lin The University of Texas at Austin

Compiler Design Spring 2017

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Standard Boolean Forms

Type Checking in COOL (II) Lecture 10

CS164: Midterm Exam 2

Constant Propagation

Register Allocation & Liveness Analysis

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

CSE 421 Applications of DFS(?) Topological sort

Lecture 4. Part 1. More on "First-Class" Procedures

CS143 Final Fall 2009

Simone Campanoni Alias Analysis

Lecture Outline. Intermediate code Intermediate Code & Local Optimizations. Local optimizations. Lecture 14. Next time: global optimizations

(Refer Slide Time: 01.26)

Why Data Flow Models? Dependence and Data Flow Models. Learning objectives. Def-Use Pairs (1) Models from Chapter 5 emphasized control

Static Program Analysis

Introduction to Machine-Independent Optimizations - 4

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

Analyze the obvious algorithm, 5 points Here is the most obvious algorithm for this problem: (LastLargerElement[A[1..n]:

Dependence and Data Flow Models. (c) 2007 Mauro Pezzè & Michal Young Ch 6, slide 1

More Static Semantics

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

Lecture 9 Arc Consistency

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08

Testing. ECE/CS 5780/6780: Embedded System Design. Why is testing so hard? Why do testing?

Static and Dataflow Analysis. (two part lecture)

Abstract Interpretation Using Laziness: Proving Conway s Lost Cosmological Theorem

Intermediate Code & Local Optimizations

Compiler Design Spring 2017

Run-time Environments

Compiler Construction 2009/2010 SSA Static Single Assignment Form

CS 406/534 Compiler Construction Putting It All Together

Run-time Environments

Induction and Semantics in Dafny

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

More Lambda Calculus and Intro to Type Systems

AXIOMS OF AN IMPERATIVE LANGUAGE PARTIAL CORRECTNESS WEAK AND STRONG CONDITIONS. THE AXIOM FOR nop

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,...

Instruction Selection and Scheduling

EECS 583 Class 6 Dataflow Analysis

White-Box Testing Techniques III

Formal Semantics of Programming Languages

Transcription:

Lecture Outline Global Optimization Global flow analysis Global constant propagation Liveness analysis Compiler Design I (2011) 2 Local Optimization Recall the simple basic-block optimizations Constant propagation Dead code elimination Global Optimization These optimizations can be extended to an entire control-flow graph y := z * w y := z * w y := z * w q := y + 42 q := y + 42 Compiler Design I (2011) 3 Compiler Design I (2011) 4

Global Optimization These optimizations can be extended to an entire control-flow graph Global Optimization These optimizations can be extended to an entire control-flow graph q := y + 42 Compiler Design I (2011) 5 Compiler Design I (2011) 6 Correctness How do we know it is OK to globally propagate constants? There are situations where it is incorrect: Correctness (Cont.) To replace a use of x by a constant k we must know that the following property ** holds: On every path to the use of x, the last assignment to x is x := k ** y := z * w y := 0 x := 54 Compiler Design I (2011) 7 Compiler Design I (2011) 8

Example 1 Revisited Example 2 Revisited y := z * w x := 54 y := 0 Compiler Design I (2011) 9 Compiler Design I (2011) 10 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 An analysis that determines how data flows over the entire control-flow graph 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 function body It is OK to be conservative: If the optimization requires P to be true, then want to know either that P is definitely true, or that we don t know whether P is true It is always safe to say don t know Compiler Design I (2011) 11 Compiler Design I (2011) 12

Global Analysis (Cont.) 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 Global Constant Propagation Global constant propagation can be performed at any point where property ** holds Consider the case of computing ** for a single variable x at all program points Compiler Design I (2011) 13 Compiler Design I (2011) 14 Global Constant Propagation (Cont.) Example To make the problem precise, we associate one of the following values with x at every program point value # c * interpretation This statement never executes x = constant c Don t know whether X is a constant y := z * w x := 54 x = 54 y := 0 Compiler Design I (2011) 15 Compiler Design I (2011) 16

Using the Information Given global constant information, it is easy to perform the optimization Simply inspect the associated with a statement using x If x is constant at that point replace that use of x by the constant The Analysis Idea The analysis of a (complicated) program can be expressed as a combination of simple rules relating the change in information between adjacent statements But how do we compute the properties Compiler Design I (2011) 17 Compiler Design I (2011) 18 Explanation The idea is to push or transfer information from one statement to the next Transfer Functions Define a transfer function that transfers information from one statement to another For each statement s, we compute information about the value of x immediately before and after s C in (x,s) = value of x before s C out (x,s) = value of x after s In the following rules, let statement s have as immediate predecessors statements p 1,,p n Compiler Design I (2011) 19 Compiler Design I (2011) 20

Rule 1 Rule 2 x = c x = d s s if C out (x, p i ) = * for any i, then C in (x, s) = * If C out (x, p i ) = c and C out (x, p j ) = d and d c then C in (x, s) = * Compiler Design I (2011) 21 Compiler Design I (2011) 22 Rule 3 Rule 4 x = c x = c s x = c s if C out (x, p i ) = c or # for all i, then C in (x, s) = c if C out (x, p i ) = # for all i, then C in (x, s) = # Compiler Design I (2011) 23 Compiler Design I (2011) 24

The Other Half Rule 5 Rules 1-4 relate the out of one statement to the in of the successor statement We also need rules relating the in of a statement to the out of the same statement s C out (x, s) = # if C in (x, s) = # Compiler Design I (2011) 25 Compiler Design I (2011) 26 Rule 6 Rule 7 x := c x = c x := f( ) C out (x, x := c) = c if c is a constant C out (x, x := f( )) = * This rule says that we do not perform inter-procedural analysis (i.e. we do not look at other functions do) Compiler Design I (2011) 27 Compiler Design I (2011) 28

Rule 8 An Algorithm y :=... x = a x = a 1. For every entry s to the function, set C in (x, s) = * 2. Set C in (x, s) = C out (x, s) = # everywhere else C out (x, y := ) = C in (x, y := ) if x y 3. Repeat until all points satisfy 1-8: Pick s not satisfying 1-8 and update using the appropriate rule Compiler Design I (2011) 29 Compiler Design I (2011) 30 The Value # To understand why we need #, look at a loop Discussion Consider the statement y := 0 To compute whether x is constant at this point, we need to know whether x is constant at the two predecessors q < b But information for depends on its predecessors, including y := 0! Compiler Design I (2011) 31 Compiler Design I (2011) 32

The Value # (Cont.) Because of cycles, all points must have values at all times 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 Example q := x + y q < b Compiler Design I (2011) 33 Compiler Design I (2011) 34 Example Example q := x + y q < b q := x + y q < b Compiler Design I (2011) 35 Compiler Design I (2011) 36

Example Orderings q := x + y q < b 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... # Compiler Design I (2011) 37 Compiler Design I (2011) 38 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 1-4 can be written using lub: C in (x, s) = lub { C out (x, p) p is a predecessor of s } Termination Simply saying repeat until nothing changes doesn t guarantee that eventually we reach a point where 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 Compiler Design I (2011) 39 Compiler Design I (2011) 40

Termination (Cont.) Thus the algorithm is linear in program size Number of steps = Number of C_(.) values computed * 2 = Number of program statements * 4 Liveness Analysis Once constants have been globally propagated, we would like to eliminate dead code After constant propagation, is dead (assuming x is not used elsewhere) Compiler Design I (2011) 41 Compiler Design I (2011) 42 Live and Dead Variables Liveness The first value of x is dead (never used) The second value of x is live (may be used) Liveness is an important concept for the compiler x := 54 y := x 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 Compiler Design I (2011) 43 Compiler Design I (2011) 44

Global Dead Code Elimination A statement x := is dead code if x is dead after the assignment Dead statements can be deleted from the program But we need liveness information first... Computing Liveness We can express liveness in terms of information transferred between adjacent statements, just as in copy propagation Liveness is simpler than constant propagation, since it is a boolean property (true or false) Compiler Design I (2011) 45 Compiler Design I (2011) 46 Liveness Rule 1 Liveness Rule 2 p x = true := f(x) x = true x = true L out (x, p) = { L in (x, s) s a successor of p } L in (x, s) = true if s refers to x on the RHS Compiler Design I (2011) 47 Compiler Design I (2011) 48

Liveness Rule 3 Liveness Rule 4 x = false x = a x := e s x = a L in (x, x := e) = false if e does not refer to x L in (x, s) = L out (x, s) if s does not refer to x Compiler Design I (2011) 49 Compiler Design I (2011) 50 Algorithm 1. Let all L_( ) = false initially 2. Repeat until all statements s satisfy rules 1-4 Pick s where one of 1-4 does not hold and update using the appropriate rule 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 information is computed, it is simple to eliminate dead code Compiler Design I (2011) 51 Compiler Design I (2011) 52

Forward vs. Backward Analysis We have seen two kinds of analysis: Global Flow Analyses There are many other global flow analyses 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 Most can be classified as either forward or backward Most also follow the methodology of local rules relating information between adjacent program points Compiler Design I (2011) 53 Compiler Design I (2011) 54