School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao.

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.
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
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.
Architecture-dependent optimizations Functional units, delay slots and dependency analysis.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Jeffrey D. Ullman Stanford University. 2  Generalizes: 1.Moving loop-invariant computations outside the loop. 2.Eliminating common subexpressions. 3.True.
Advanced Compiler Techniques LIU Xianhua School of EECS, Peking University Partial Redundancy Elimination.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
1 Partial Redundancy Elimination Finding the Right Place to Evaluate Expressions Four Necessary Data-Flow Problems.
Lazy Code Motion Comp 512 Spring 2011
Partial Redundancy Elimination & Lazy Code Motion
Lazy Code Motion C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
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,
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
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 Global Optimizations Lecture 23.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
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.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
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.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Improving Code Generation Honors Compilers April 16 th 2002.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Loops Guo, Yao.
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.
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.
Computer Science & Engineering, Indian Institute of Technology, Bombay Code optimization by partial redundancy elimination using Eliminatability paths.
What’s in an optimizing compiler?
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.
1 Code optimization “Code optimization refers to the techniques used by the compiler to improve the execution efficiency of the generated object code”
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.
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.
Optimization Simone Campanoni
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
Optimal Code Generation (for Expressions) and Data-Flow Analysis Pat Morin COMP 3002.
Data Flow Analysis Suman Jana
Lecture 5 Partial Redundancy Elimination
Iterative Dataflow Problems
Basic Block Optimizations
CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion
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.
Advanced Compiler Techniques
Data Flow Analysis Compiler Design
Dataflow Analysis Hal Perkins Winter 2008
Static Single Assignment
CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion
Live Variables – Basic Block
Basic Block Optimizations
Presentation transcript:

School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao

2 Fall 2011 “Advanced Compiler Techniques” Outline Forms of redundancy global common subexpression elimination loop invariant code motion partial redundancy Lazy Code Motion Algorithm A set of four analyses 9.5 Reading: Dragon§9.5

3 Fall 2011 “Advanced Compiler Techniques” Role of PRE Goal: Minimize the number of expression evaluations. Goal: Minimize the number of expression evaluations. Keep the value of evaluation in a temporary variable and use it later. Keep the value of evaluation in a temporary variable and use it later. Sources of redundancy: Sources of redundancy: 1. Global common subexpressions 2. loop-invariant computations 3. True partial redundancy: an expression is sometimes available, sometimes not.

4 Fall 2011 “Advanced Compiler Techniques” Example: Global Common Subexpression A common expression may have different values on different paths! On every path reaching p, expression b+c has been computed b, c not overwritten after the expression

5 Fall 2011 “Advanced Compiler Techniques” Example: Loop-Invariant Code Motion Given an expression (b+c) inside a loop, does the value of b+c change inside the loop? is the code executed at least once?

6 Fall 2011 “Advanced Compiler Techniques” Example: True Partial Redundancy Can we place calculations of b+c such that no path re-executes the same expression? = b+c t = b+c = t

7 Fall 2011 “Advanced Compiler Techniques” Can All Redundancy Be Eliminated? Critical edges source basic block has multiple successors destination basic block has multiple predecessors

8 Fall 2011 “Advanced Compiler Techniques” Code Duplication

9 Fall 2011 “Advanced Compiler Techniques” Problem With Node-Splitting Can exponentiate the number of nodes. Can exponentiate the number of nodes. Our PRE algorithm needs to move code to new blocks along edges, but will not split blocks. Our PRE algorithm needs to move code to new blocks along edges, but will not split blocks. Convention: All new instructions are either inserted at the beginning of a block or placed in a new block. Convention: All new instructions are either inserted at the beginning of a block or placed in a new block.

10 Fall 2011 “Advanced Compiler Techniques” Lazy Code Motion Problem Desired properties of a PRE algorithm All redundant computations of expressions that can be eliminated without code duplication are eliminated. The optimized program does not perform any computation that is not in the original program execution. Expressions are computed at the latest possible time.

11 Fall 2011 “Advanced Compiler Techniques” Full Redundancy Full redundancy at p: expression a+b redundant on all paths cutset: nodes that separate entry from p cutset contains calculation of a+b a, b, not redefined

12 Fall 2011 “Advanced Compiler Techniques” Partial Redundancy Partial redundancy at p: redundant on some but not all paths Add operations to create a cutset containing a+b Note: Moving operations up can eliminate redundancy

13 Fall 2011 “Advanced Compiler Techniques” Anticipated Expressions Expression x+y is anticipated at a point if x+y is certain to be evaluated along any computation path, before any recomputation of x or y. Expression x+y is anticipated at a point if x+y is certain to be evaluated along any computation path, before any recomputation of x or y. anticipated Copies of an expression must be placed only at program points where the expression is anticipated. The earlier an expression is placed, the more redundancy can be removed

14 Fall 2011 “Advanced Compiler Techniques” Example: Anticipated Expressions = x+y x+y is anticipated here and could be computed now rather than later. = x+y x+y is anticipated here, but is also available. No computa- tion is needed.

15 Fall 2011 “Advanced Compiler Techniques” The Plan 1. Determine for each expression the earliest place(s) it can be computed while still being sure that it will be used. 2. Postpone the expressions as long as possible without introducing redundancy. We trade space for time --- an expression can be computed in many places, but never if it is already computed. We trade space for time --- an expression can be computed in many places, but never if it is already computed.

16 Fall 2011 “Advanced Compiler Techniques” The Guarantee No expression is computed at a place where it its value might have been computed previously, and preserved instead. No expression is computed at a place where it its value might have been computed previously, and preserved instead. Even along a subset of the possible paths. Even along a subset of the possible paths.

17 Fall 2011 “Advanced Compiler Techniques” More About the Plan We use four data-flow analyses, in succession, plus some set operations on the results of these analyses. We use four data-flow analyses, in succession, plus some set operations on the results of these analyses. Anticipated Expressions Anticipated Expressions Available Expressions Available Expressions Postponable Expressions Postponable Expressions Used Expressions Used Expressions After the first, each analysis uses the results of the previous ones. After the first, each analysis uses the results of the previous ones.

18 Fall 2011 “Advanced Compiler Techniques” Assumptions Assume every statement is a basic block Only place statements at the beginning of a basic block Add a basic block for every edge that leads to a basic block with multiple predecessors

19 Fall 2011 “Advanced Compiler Techniques” Computing Anticipated Expressions Use(B) = set of expressions x+y evaluated in B before any assignment to x or y. Use(B) = set of expressions x+y evaluated in B before any assignment to x or y. Def(B) = set of expressions one of whose arguments is assigned in B. Def(B) = set of expressions one of whose arguments is assigned in B.

20 Fall 2011 “Advanced Compiler Techniques” Computing Anticipated Expressions --- (2) Direction = backwards. Direction = backwards. Join (or Meet) = intersection. Join (or Meet) = intersection. Boundary condition: IN[exit] = ∅. Boundary condition: IN[exit] = ∅. Transfer function: Transfer function: IN[B] = (OUT[B] – Def(B)) ∪ Use(B) IN[B] = (OUT[B] – Def(B)) ∪ Use(B)

21 Fall 2011 “Advanced Compiler Techniques” Example: Anticipated Expressions = x+y Anticipated Backwards; Intersection; IN[B] = (OUT[B] – Def(B)) ∪ Use(B)

22 Fall 2011 “Advanced Compiler Techniques” “ Available ” Expressions Modification of the usual AE. Modification of the usual AE. x+y is “ available ” at a point if either: x+y is “ available ” at a point if either: 1. It is available in the usual sense; i.e., it has been computed and not killed, or 2. It is anticipated; i.e., it could be available if we chose to precompute it there.

23 Fall 2011 “Advanced Compiler Techniques” “ Available ” Expressions x+y is in Kill(B) if x or y is defined, and x+y is not later recomputed (same as previously). x+y is in Kill(B) if x or y is defined, and x+y is not later recomputed (same as previously). Confluence = intersection. Confluence = intersection. Transfer function: Transfer function: OUT[B] = (IN[B] ∪ IN ANTICIPATED [B]) – Kill(B)

24 Fall 2011 “Advanced Compiler Techniques” Earliest Placement x+y is in Earliest[B] if it is anticipated at the beginning of B but not “ available ” there. x+y is in Earliest[B] if it is anticipated at the beginning of B but not “ available ” there. That is: when we compute anticipated expressions, x+y is in IN[B], but That is: when we compute anticipated expressions, x+y is in IN[B], but When we compute “ available ” expressions, x+y is not in IN[B]. When we compute “ available ” expressions, x+y is not in IN[B]. I.e., x+y is anticipated at B, but not anticipated at OUT of some predecessor. I.e., x+y is anticipated at B, but not anticipated at OUT of some predecessor.

25 Fall 2011 “Advanced Compiler Techniques” Example: Available/Earliest = x+y Anticipated “Available” Earliest = anticipated but not available Forward; Intersection; OUT[B] = (IN[B] ∪ IN ANTICIPATED [B]) – Kill(B)

26 Fall 2011 “Advanced Compiler Techniques” Postponable Expressions Now, we need to delay the evaluation of expressions as long as possible. Now, we need to delay the evaluation of expressions as long as possible. u Not past the use of the expression. u Not so far that we wind up computing an expression that is already evaluated. Note viewpoint: It is OK to use code space if we save register use. Note viewpoint: It is OK to use code space if we save register use.

27 Fall 2011 “Advanced Compiler Techniques” Example t = b+c a = t d = t

28 Fall 2011 “Advanced Compiler Techniques” Example t = b+c a = t d = t

29 Fall 2011 “Advanced Compiler Techniques” Postponable Expressions - (2) x+y is postponable to a point p if on every path from the entry to p: x+y is postponable to a point p if on every path from the entry to p: 1. There is a block B for which x+y is in earliest[B], and 2. After that block, there is no use of x+y.

30 Fall 2011 “Advanced Compiler Techniques” Postponable Expressions - (3) Computed like “ available ” expressions, with two differences: Computed like “ available ” expressions, with two differences: 1. In place of killing an expression (assigning to one of its arguments): Use(B), the set of expressions used in block B. 2. In place of IN ANTICIPATED [B]: earliest[B].

31 Fall 2011 “Advanced Compiler Techniques” Postponable Expressions - (4) Confluence = intersection. Confluence = intersection. Transfer function: Transfer function: OUT[B] = (IN[B] ∪ earliest[B]) – Use(B)

32 Fall 2011 “Advanced Compiler Techniques” Example: Postponable Expressions = x+y Earliest Postponable Three places to compute x+y Forward; Intersection; OUT[B] = (IN[B] ∪ earliest[B]) – Use(B)

33 Fall 2011 “Advanced Compiler Techniques” Latest Placement We want to postpone as far as possible. We want to postpone as far as possible. How do we compute the “ winners ” --- the blocks such that we can postpone no further? How do we compute the “ winners ” --- the blocks such that we can postpone no further? Remember --- postponing stops at a use or at a block with another predecessor where x+y is not postponable. Remember --- postponing stops at a use or at a block with another predecessor where x+y is not postponable.

34 Fall 2011 “Advanced Compiler Techniques” Latest[B] For x+y to be in latest[B]: For x+y to be in latest[B]: 1. x+y is either in earliest[B] or in IN POSTPONABLE [B]. u I.e., we can place the computation at B. 2. x+y is either used in B or there is some successor of B for which (1) does not hold. u I.e., we cannot postpone further along all branches.

35 Fall 2011 “Advanced Compiler Techniques” Example: Latest = x+y Earliest Or Postponable to beginning Used Or has a suc- cessor not red. Latest = green and red.

36 Fall 2011 “Advanced Compiler Techniques” Final Touch --- Used Expressions We ’ re now ready to introduce a temporary t to hold the value of expression x+y everywhere. We ’ re now ready to introduce a temporary t to hold the value of expression x+y everywhere. But there is a small glitch: t may be totally unnecessary. But there is a small glitch: t may be totally unnecessary. E.g., x+y is computed in exactly one place. E.g., x+y is computed in exactly one place.

37 Fall 2011 “Advanced Compiler Techniques” Used Expressions --- (2) used[B] = expressions used along some path from the exit of B. used[B] = expressions used along some path from the exit of B. Backward flow analysis. Backward flow analysis. Confluence = union. Confluence = union. Transfer function: Transfer function: IN[B] = (OUT[B] ∪ e-used[B]) – Latest(B) e-used = “ expression is used in B. ” e-used = “ expression is used in B. ”

38 Fall 2011 “Advanced Compiler Techniques” Example: Used = x+y Recall: Latest Used Backwards; Union; IN[B] = (OUT[B] ∪ e-used[B]) – Latest(B)

39 Fall 2011 “Advanced Compiler Techniques” Rules for Introducing Temporaries 1. If x+y is in both Latest[B] and OUT USED [B], introduce t = x+y at the beginning of B. 2. If x+y is used in B, but either 1. Is not in Latest[B] or 2. Is in OUT USED [B], replace the use(s) of x+y by uses of t.

40 Fall 2011 “Advanced Compiler Techniques” Example: Where is a Temporary Used? = x+y Recall: Latest Recall OUT USED Create temp- orary here Use it here But not here --- x+y is in Latest and not in OUTUSED

41 Fall 2011 “Advanced Compiler Techniques” Example: Here ’ s Where It ’ s Used = x+y t = x+y = t t = x+y

42 Fall 2011 “Advanced Compiler Techniques” Summary Cannot execute any operations not executed originally Pass 1: Anticipation: range of code motion Eliminate as many redundant calculations of an expression as possible, without duplicating code Pass 2: Availability: move it up as early as possible Delay computation as much as possible to minimize register lifetimes Pass 3: Postponable: move it down unless it creates redundancy (lazy code motion) Pass 4: Remove temporary assignment

43 Fall 2011 “Advanced Compiler Techniques” Next Time Loops Loops Dragon 9.6 Dragon 9.6 Region-based Analysis Region-based Analysis Dragon 9.7 Dragon 9.7