Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.

Slides:



Advertisements
Similar presentations
SSA and CPS CS153: Compilers Greg Morrisett. Monadic Form vs CFGs Consider CFG available exp. analysis: statement gen's kill's x:=v 1 p v 2 x:=v 1 p v.
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
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.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
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.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
More Dataflow Analysis CS153: Compilers Greg Morrisett.
CS 536 Spring Global Optimizations Lecture 23.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
Administrative info Subscribe to the class mailing list –instructions are on the class web page, which is accessible from my home page, which is accessible.
Data Flow Analysis Compiler Design Nov. 3, 2005.
From last time: reaching definitions For each use of a variable, determine what assignments could have set the value being read from the variable Information.
Prof. Bodik CS 164 Lecture 171 Register Allocation Lecture 19.
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
Another example p := &x; *p := 5 y := x + 1;. Another example p := &x; *p := 5 y := x + 1; x := 5; *p := 3 y := x + 1; ???
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.
Data Flow Analysis Compiler Design Nov. 8, 2005.
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.
From last lecture x := y op z in out F x := y op z (in) = in [ x ! in(y) op in(z) ] where a op b =
Intermediate Code. Local Optimizations
Prof. Fateman CS164 Lecture 211 Local Optimizations Lecture 21.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
From last time: reaching definitions For each use of a variable, determine what assignments could have set the value being read from the variable Information.
Data Flow Analysis Compiler Design Nov. 8, 2005.
From last lecture We want to find a fixed point of F, that is to say a map m such that m = F(m) Define ?, which is ? lifted to be a map: ? = e. ? Compute.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Ben Livshits Based in part of Stanford class slides from
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
Abstract Interpretation (Cousot, Cousot 1977) also known as Data-Flow Analysis.
1 CS 201 Compiler Construction Data Flow Analysis.
1 ECE 453 – CS 447 – SE 465 Software Testing & Quality Assurance Instructor Kostas Kontogiannis.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Λλ 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.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
Compiler Principles Fall Compiler Principles Lecture 11: Loop Optimizations Roman Manevich Ben-Gurion University.
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
White-Box Testing Statement coverage Branch coverage Path coverage
Dataflow Analysis CS What I s Dataflow Analysis? Static analysis reasoning about flow of data in program Different kinds of data: constants, variables,
Data Flow Analysis Suman Jana
Lecture 5 Partial Redundancy Elimination
Fall Compiler Principles Lecture 8: Loop Optimizations
Machine-Independent Optimization
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.
Static Single Assignment Form (SSA)
Fall Compiler Principles Lecture 10: Global Optimizations
Optimizations using SSA
Fall Compiler Principles Lecture 10: Loop Optimizations
Data Flow Analysis Compiler Design
Midterm COM3220 Open book/open notes Tuesday, April 28, 6pm pm
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Presentation transcript:

Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22

Prof. Fateman CS 164 Lecture 222 Lecture Outline Global flow analysis Global constant propagation Liveness analysis

Prof. Fateman CS 164 Lecture 223 Local Optimization Recall the simple basic-block optimizations –Constant propagation –Dead code elimination X := 3 Y := Z * W Q := X + Y X := 3 Y := Z * W Q := 3 + Y Y := Z * W Q := 3 + Y

Prof. Fateman CS 164 Lecture 224 Global Optimization These optimizations can be extended to an entire control-flow graph X := 3 B > 0 Y := Z + WY := 0 A := 2 * X

Prof. Fateman CS 164 Lecture 225 Global Optimization These optimizations can be extended to an entire control-flow graph X := 3 B > 0 Y := Z + WY := 0 A := 2 * X

Prof. Fateman CS 164 Lecture 226 Global Optimization These optimizations can be extended to an entire control-flow graph X := 3 B > 0 Y := Z + WY := 0 A := 2 * 3

Prof. Fateman CS 164 Lecture 227 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 X := 4 Y := 0 A := 2 * X

Prof. Fateman CS 164 Lecture 228 Correctness (Cont.) 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 **

Prof. Fateman CS 164 Lecture 229 Example 1 Revisited X := 3 B > 0 Y := Z + WY := 0 A := 2 * X

Prof. Fateman CS 164 Lecture 2210 Example 2 Revisited X := 3 B > 0 Y := Z + W X := 4 Y := 0 A := 2 * X

Prof. Fateman CS 164 Lecture 2211 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 of the entire control-flow graph

Prof. Fateman CS 164 Lecture 2212 Global Analysis Global optimization tasks share several traits: –The optimization depends on knowing a property X at a particular point in program execution –Proving X at any point requires knowledge of the entire function body –It is OK to be conservative. If the optimization requires X to be true, then want to know either X is definitely true Don’t know if X is true –It is always safe to say “don’t know”

Prof. Fateman CS 164 Lecture 2213 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

Prof. Fateman CS 164 Lecture 2214 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

Prof. Fateman CS 164 Lecture 2215 Global Constant Propagation (Cont.) To make the problem precise, we associate one of the following values with X at every program point valueinterpretation # This statement never executes cX = constant c *Don’t know if X is a constant

Prof. Fateman CS 164 Lecture 2216 Example X = * X = 3 X = 4 X = * X := 3 B > 0 Y := Z + W X := 4 Y := 0 A := 2 * X X = 3 X = *

Prof. Fateman CS 164 Lecture 2217 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 = ?

Prof. Fateman CS 164 Lecture 2218 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

Prof. Fateman CS 164 Lecture 2219 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 C in (x,s) = value of x before s C out (x,s) = value of x after s

Prof. Fateman CS 164 Lecture 2220 Transfer Functions Define a transfer function that transfers information one statement to another In the following rules, let statement s have immediate predecessor statements p 1,…,p n

Prof. Fateman CS 164 Lecture 2221 Rule 1 (unknown predecessor) if C out (x, p i ) = * for any predecessor i, then C in (x, s) = * s X = * X = ?

Prof. Fateman CS 164 Lecture 2222 Rule 2 (different predecessors) If C out (x, p i ) = c and C out (x, p j ) = d and d <> c then C in (x, s) = * s X = d X = * X = ? X = c

Prof. Fateman CS 164 Lecture 2223 Rule 3 (exclude unreachable values) if C out (x, p i ) = c or # for all i, then C in (x, s) = c s X = c X = # X = c

Prof. Fateman CS 164 Lecture 2224 Rule 4 (unreachable propagation) if C out (x, p i ) = # for all i, then C in (x, s) = # s X = #

Prof. Fateman CS 164 Lecture 2225 The Other Half Rules 1-4 relate the out of one or more statements to the in of the successor statement Now we need rules relating the in of a statement to the out of the same statement

Prof. Fateman CS 164 Lecture 2226 Rule 5 C out (x, s) = # if C in (x, s) = # s X = #

Prof. Fateman CS 164 Lecture 2227 Rule 6 C out (x, x := c) = c if c is a constant x := c X = ? X = c

Prof. Fateman CS 164 Lecture 2228 Rule 7 C out (x, x := f(…)) = * x := f(…) X = ? X = *

Prof. Fateman CS 164 Lecture 2229 Rule 8 C out (x, y := …) = C in (x, y := …) if x <> y y :=... X = a

Prof. Fateman CS 164 Lecture 2230 An Algorithm 1.For every entry s to the program, set C in (x, s) = * 2.Set C in (x, s) = C out (x, s) = # everywhere else 3.Repeat until all points satisfy 1-8: Pick s not satisfying 1-8 and update using the appropriate rule

Prof. Fateman CS 164 Lecture 2231 The Value # To understand why we need #, look at a loop X := 3 B > 0 Y := Z + WY := 0 A := 2 * X A < B X = * X = 3

Prof. Fateman CS 164 Lecture 2232 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 –X := 3 –A := 2 * X But info for A := 2 * X depends on its predecessors, including Y := 0!

Prof. Fateman CS 164 Lecture 2233 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”

Prof. Fateman CS 164 Lecture 2234 Example X := 3 B > 0 Y := Z + WY := 0 A := 2 * X A < B X = * X = 3 X = #

Prof. Fateman CS 164 Lecture 2235 Example X := 3 B > 0 Y := Z + WY := 0 A := 2 * X A < B X = * X = 3 X = # X = 3

Prof. Fateman CS 164 Lecture 2236 Example X := 3 B > 0 Y := Z + WY := 0 A := 2 * X A < B X = * X = 3 X = # X = 3

Prof. Fateman CS 164 Lecture 2237 Example X := 3 B > 0 Y := Z + WY := 0 A := 2 * X A < B X = * X = 3

Prof. Fateman CS 164 Lecture 2238 Orderings We can simplify the presentation of the analysis by ordering the values # < c < * Drawing a picture with “lower” values drawn lower, we get # * 01

Prof. Fateman CS 164 Lecture 2239 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 }

Prof. Fateman CS 164 Lecture 2240 Termination In general, 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

Prof. Fateman CS 164 Lecture 2241 Termination (Cont.) Thus the algorithm is linear in program size Number of steps = Number of C_(….) values computed * 2 = Number of program statements * 4

Prof. Fateman CS 164 Lecture 2242 Liveness Analysis Once constants have been globally propagated, we would like to eliminate dead code After constant propagation, X := 3 is dead (assuming X not used elsewhere) X := 3 B > 0 Y := Z + WY := 0 A := 2 * X

Prof. Fateman CS 164 Lecture 2243 Live and Dead The first value of x is dead (never used) The second value of x is live (may be used) Liveness is an important concept X := 3 X := 4 Y := X

Prof. Fateman CS 164 Lecture 2244 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

Prof. Fateman CS 164 Lecture 2245 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...

Prof. Fateman CS 164 Lecture 2246 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)

Prof. Fateman CS 164 Lecture 2247 Liveness Rule 1 L out (x, p) =  { L in (x, s) | s a successor of p } p X = true X = ?

Prof. Fateman CS 164 Lecture 2248 Liveness Rule 2 L in (x, s) = true if s refers to x on the rhs …:= f(x) X = true X = ?

Prof. Fateman CS 164 Lecture 2249 Liveness Rule 3 L in (x, x := e) = false if e does not refer to x x := e X = false X = ?

Prof. Fateman CS 164 Lecture 2250 Liveness Rule 4 L in (x, s) = L out (x, s) if s does not refer to x s X = a

Prof. Fateman CS 164 Lecture 2251 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

Prof. Fateman CS 164 Lecture 2252 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

Prof. Fateman CS 164 Lecture 2253 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

Prof. Fateman CS 164 Lecture 2254 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

Prof. Fateman CS 164 Lecture 2255 Other parts of optimization Register allocation, graph coloring Instruction scheduling on pipelined machines –Removing bubbles from the pipeline –Unrolling loops to fill bubbles