1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.

Slides:



Advertisements
Similar presentations
Example of Constructing the DAG (1)t 1 := 4 * iStep (1):create node 4 and i 0 Step (2):create node Step (3):attach identifier t 1 (2)t 2 := a[t 1 ]Step.
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
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.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
SSA.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
C Chuen-Liang Chen, NTUCS&IE / 321 OPTIMIZATION Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
1 Introduction to Data Flow Analysis. 2 Data Flow Analysis Construct representations for the structure of flow-of-data of programs based on the structure.
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.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
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.
05 May Lazy Code Motion in an SSA World A CS 526 Course Project Patrick Meredith Steven Lauterburg 05 May 2006.
Partial Redundancy Elimination & Lazy Code Motion
Λλ 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,
Example in SSA X := Y op Z in out F X := Y op Z (in) = in [ { X ! Y op Z } X :=  (Y,Z) in 0 out F X :=   (in 0, in 1 ) = (in 0 Å in 1 ) [ { X ! E |
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
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.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
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.
1 CS 201 Compiler Construction Lecture 8 Code Optimizations: Partial Dead Code Elimination.
CS 201 Compiler Construction
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
1 CS 201 Compiler Construction Lecture 6 Code Optimizations: Constant Propagation & Folding.
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.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
Improving Code Generation Honors Compilers April 16 th 2002.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
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.
PSUCS322 HM 1 Languages and Compiler Design II IR Code Optimization Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU.
1 CS 201 Compiler Construction Data Flow Analysis.
Computer Science & Engineering, Indian Institute of Technology, Bombay Code optimization by partial redundancy elimination using Eliminatability paths.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
What’s in an optimizing compiler?
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”
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Optimization Simone Campanoni
1 CS 201 Compiler Construction Code Optimizations: Partial Dead Code Elimination.
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
Code Optimization Overview and Examples
Lecture 5 Partial Redundancy Elimination
CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion
Machine-Independent Optimization
Code Generation Part III
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
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
Topic 5a Partial Redundancy Elimination and SSA Form
Static Single Assignment
CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion
Live Variables – Basic Block
Presentation transcript:

1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination

Redundancy Elimination The goal of redundancy elimination is to eliminate unnecessary reevaluations of the same computation. Global Common Sub-expression Elimination Loop Invariant Code Motion Partial Redundancy Elimination – this is the most general and thus it subsumes the above two optimizations. 2

3 Global Common Sub-expression Elimination Analysis Required 1.Available Expressions 2.Reaching Definitions

Transformation Algorithm For each statement S: A = B op C st B op C is available at entry of S’s basic block and neither B or C are redefined prior to S do the following: 1.Find definitions that reach S’s block that have B op C on the right hand side. 2.Create a new name T. 3.Replace each statement D = B op C found in step 1 by: T = B op C; D = T; 4.Replace statement S by A = T. 4

Loop Invariant Code Motion Loop Invariant: A statement that performs the same computation over and over again during multiple iterations of a loop’s execution. To detect invariant statements in a loop L: 1.Mark invariant statements whose operands are either constants or variables with ALL reaching definitions outside the loop. 2.Mark invariant statements whose operands satisfy conditions in step 1 or have exactly one reaching definition that is from inside the loop and has already been marked invariant. 3.Repeat step 2 till no more invariants are identified. 5

Transformation For each statement S (A=B, A=op B, A=B op C) that is marked invariant wrt L, check the following: 1.S is in a block that dominates all exits of L; 2.A is not defined elsewhere in L; and. 3.All uses of A in L can only be reached by the definition of A in S. Move in the order identified as invariant, each invariant statement S found to satisfy the above conditions to a newly created pre-header. 6

Why Conditions are Needed? 7

Limitations of… 8 X+Y is not Available here Global Common Sub-expression Elim. Loop Invariant Code Motion

Partial Redundancy Elimination 9 Use code motionMove expressions

Algorithm for PRE Before performing PRE, split critical edges to create suitable placement points. 10

Algorithm for PRE Key Idea: Move expression evaluations back through the control flow graph and place them at the earliest possible points in the control flow graph.  This process eliminates full and partial redundancy. 11

Algorithm for PRE 12

Algorithm Steps 1.Down-Safety Analysis  backward analysis that determines to where all expressions can be “safely” moved. 2.Earliestness Analysis  forward analysis that determines the earliest points to where an expression can be safely moved. 3.Perform transformation by placing expression evaluations at earliest points and eliminating them from other points. 13

Down-Safety Analysis Used(n) is tre if n evaluates the expression. Transp(n) is true if n does not modify any operands of the expression (i.e., n is transparent, it does not kill the expression) 14

Earliestness Analysis Forward analysis that finds the earliest nodes that are down-safe. A node n is an earliest node if there is a path from start node to n such that along this path n is the first node that is down-safe for the current value of the expression. 15

Transformation Step 1.Introduce a new auxiliary variable h for expression exp. 2.Insert at the entry of every node n for which D-Safe(n) ∧ Earliest(n) is true, the assignment: h = exp. 3.Replace every original computation of exp by h. 16

Example 17

Optional Step Suppressing unnecessary code motion using Delayability Analaysis - reduces register usage. 18