Advanced Compiler Techniques

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

P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
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.
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.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao.
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,
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.
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.
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.
Data Flow Analysis Compiler Design Nov. 8, 2005.
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.
1 Data-Flow Frameworks Lattice-Theoretic Formulation Meet-Over-Paths Solution Monotonicity/Distributivity.
Improving Code Generation Honors Compilers April 16 th 2002.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
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.
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.
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.
Global Register Allocation Based on
Data Flow Analysis Suman Jana
Lecture 5 Partial Redundancy Elimination
Iterative Dataflow Problems
CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion
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.
Topic 5a Partial Redundancy Elimination and SSA Form
Data Flow Analysis Compiler Design
Topic-4a Dataflow Analysis 2019/2/22 \course\cpeg421-08s\Topic4-a.ppt.
Dataflow Analysis Hal Perkins Winter 2008
Static Single Assignment
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion
Live variables and copy propagation
Prof. Dhananjay M Dhamdhere
COMPILERS Liveness Analysis
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Live Variables – Basic Block
Presentation transcript:

Advanced Compiler Techniques Partial Redundancy Elimination LIU Xianhua School of EECS, Peking University

“Advanced Compiler Techniques” REVIEW Foundations Data Flow Framework Lattice-Theoretic Formulation Meet-Over-Paths Solution Extensions Other DFA Methods “Advanced Compiler Techniques”

REVIEW Available Expressions Analysis Live Variables Analysis ≤ is ⊆, ∧ is ∩ ≤ is ⊇, ∧ is ∪ “Advanced Compiler Techniques”

“Advanced Compiler Techniques” REVIEW ∀i , Ini = Outi = ⊤ All possible assignments Meet Over Paths Assignment All safe assignments Maximum Fixed Point Least Fixed Point ∀i , Ini = Outi = ⊥ All fixed point solutions “Advanced Compiler Techniques”

“Advanced Compiler Techniques” REVIEW f(x) f(y) MOP considers paths independently and and combines at the last possible moment. OUT = f(x) ∧ f(y) f OUT = x OUT = y IN = x∧y OUT = f(x∧y) In MFP, Values x and y get combined too soon. B Entry Since f(x ∧ y) ≤ f(x) ∧ f(y), it is as if we added nonexistent paths, but we’re safe. “Advanced Compiler Techniques”

“Advanced Compiler Techniques” Summary of DFA Methods Method Speed Simple? Structure Both Way? Graph Class Iterative O(n2) Simple No Yes All Interval Middle Reducible Balance Tree O(nlogn) Complicated Path Comp. Semi Node List Balance Path O(nα(n,n)) ? Grammar n L(Grammar) High Level Parse Trees “Advanced Compiler Techniques”

“Advanced Compiler Techniques” Outline Forms of redundancy global common sub-expression loop invariant partial redundancy expression Lazy Code Motion Algorithm A set of four analysis “Advanced Compiler Techniques”

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

Partial redundancy elimination One of the most complex dataflow analysis Subsumes common sub-expression elimination and loop invariant code motion Originally proposed in 1979 by Morel and Renvoise, Used a bi-directional dataflow analysis Reformulated by Knoop, Rüthing and Steffen in 1992, Uses a backward dataflow analysis followed by a forward analysis We will discuss this latter formulation “Advanced Compiler Techniques”

“Advanced Compiler Techniques” Convention Throughout, assume that neither argument of an expression x+y is modified unless we explicitly assign to x or y. And of course, we assume x+y is the only expression anyone would ever want to compute.  Can easily extend this to multiple expressions by using a bit vector lattice. “Advanced Compiler Techniques”

“Advanced Compiler Techniques” Example: Global CSE A common expression may have different values on different paths. On every path reaching p, expression x+y has been computed x, y not overwritten after the expression a = x+y b = x+y c = x+y t = x+y a = t b = t c = t a=x+y a=x+y x=7 a=x+y x=7 f=x+y d=x+y d=x+y d=x+y “Advanced Compiler Techniques”

Example: Loop-Invariant Code Motion Given an expression (x+y) inside a loop, does the value of x+y change inside the loop? is the code executed at least once? t = x+y a = x+y a = t “Advanced Compiler Techniques”

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

Modifying the Flow Graph We could: Add a new block along an edge. Only necessary if the edge enters a block with several predecessors. Duplicate blocks so an expression x+y is evaluated only along paths where it is needed. “Advanced Compiler Techniques”

Example: Node Splitting = x+y t = x+y t = x+y = t “Advanced Compiler Techniques”

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

“Advanced Compiler Techniques” Code Duplication “Advanced Compiler Techniques”

Problem With Node-Splitting Can exponentiate the number of nodes. 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. “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. “Advanced Compiler Techniques”

“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 “Advanced Compiler Techniques”

“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 “Advanced Compiler Techniques”

“Advanced Compiler Techniques” The Plan Determine for each expression the earliest place(s) it can be computed while still being sure that it will be used. 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. “Advanced Compiler Techniques”

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

“Advanced Compiler Techniques” The Plan – (2) 3. Determine those places where it is really necessary to store x+y in a temporary rather than compute it when needed. Example: If x+y is computed in only one place. “Advanced Compiler Techniques”

“Advanced Compiler Techniques” More about the plan Don’t introduce/insert new operations didn’t exist originally: Anticipate the range of code motion Eliminate as many redundant calculations of an expression as possible, without duplicating code Move it up as early as possible Delay computation as much as possible to minimize register Lifetimes move it down unless it creates redundancy (lazy code motion) Remove temporary assignment “Advanced Compiler Techniques”

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

“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 “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. 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 “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. = x+y = x+y “Advanced Compiler Techniques”

Computing Anticipated Expressions 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. “Advanced Compiler Techniques”

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

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

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

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

“Advanced Compiler Techniques” Earliest Placement 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 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. “Advanced Compiler Techniques”

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

Postponable Expressions Now, we need to delay the evaluation of expressions as long as possible, but … Not past the use of the expression. 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. “Advanced Compiler Techniques”

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

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

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

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

Postponable Expressions – (4) Direction = forward. Meet = intersection. Transfer function: OUT[B] = (IN[B] ∪ earliest[B]) – Use(B) “Advanced Compiler Techniques”

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

“Advanced Compiler Techniques” Latest Placement We want to postpone as far as possible. 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. “Advanced Compiler Techniques”

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

“Advanced Compiler Techniques” Example: Latest Earliest Or Postponable to beginning Latest = Blue and red. = x+y = x+y Used Or has a suc- cessor not red. = x+y “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. But there is a small glitch: t may be totally unnecessary. E.g., x+y is computed in exactly one place. “Advanced Compiler Techniques”

“Advanced Compiler Techniques” Used Expressions An expression is used at point p if There exists a path leading from p that uses the expression before the value is reevaluated. Essentially liveness analysis for expressions rather than for variables. “Advanced Compiler Techniques”

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

“Advanced Compiler Techniques” Used Expressions – (2) used[B] = expressions used along some path from the end of B. Direction = backward. Meet = union. Transfer function: IN[B] = (OUT[B] ∪ e-used[B]) – Latest(B) e-used = “expression is used in B.” “Advanced Compiler Techniques”

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

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

Example: Here’s Where t is Used = x+y t = x+y t = x+y = t = t “Advanced Compiler Techniques”

“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 “Advanced Compiler Techniques”

Data Flow Analysis of PRE “Advanced Compiler Techniques”

Data Flow Analysis of PRE “Advanced Compiler Techniques”

Algorithm of Lazy Code Motion INPUT: A flow graph for which e_useB and e_killB have been computed for each block B. OUTPUT: A modified flow graph satisfying the four lazy code motion conditions. METHOD: Insert an empty block along all edges entering a block with more than one predecessor Find anticipated[B] .in for all blocks B Find available[B] .in for all blocks B Compute the earliest placements for all blocks B Find postponable[B].in for all blocks B Compute the latest placements for all blocks B Find used[B] .out for all blocks B “Advanced Compiler Techniques”

Algorithm of Lazy Code Motion METHOD: For each expression, say x+y, computed by the program, do the following: Create a new temporary, say t, for x + y. For all blocks B such that x + y is in latest[B]∩ used[B].out, add t = x +y at the beginning of B. For all blocks B such that x + y is in e_use B ∩ (┐latest[B] ∪ used.out[B] ) replace every original x + y by t. “Advanced Compiler Techniques”

“Advanced Compiler Techniques” Example “Advanced Compiler Techniques”

“Advanced Compiler Techniques” More about PRE Don’t need heuristic Dhamdhere, Drechsler-Stadel, Knoop,et.al. use restricted flow graph or allow edge placements. Data flow can be separated into unidirectional passes Dhamdhere, Knoop, et. al. Improvement still tied to accuracy of computational model Assumes performance depends only on the number of computations along any path. Ignores resource constraint issues: register alloc., etc. Knoop, et.al. give “earliest” and “latest” placement algorithms which begin to address this. Further issues: more than one expression at once, strength reduction, redundant assignments, redundant stores With GVN,SSA… “Advanced Compiler Techniques”

“Advanced Compiler Techniques” Next Time Homework 9.5.1, 9.5.2 Loops Dragon Book: §9.6 “Advanced Compiler Techniques”