1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & 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

CSC 4181 Compiler Construction Code Generation & Optimization.
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Synopsys University Courseware Copyright © 2012 Synopsys, Inc. All rights reserved. Compiler Optimization and Code Generation Lecture - 3 Developed By:
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.
19 Classic Examples of Local and Global Code Optimizations Local Constant folding Constant combining Strength reduction.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
Chapter 10 Code Optimization. A main goal is to achieve a better performance Front End Code Gen Intermediate Code source Code target Code user Machine-
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.
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.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao.
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 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.
Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy.
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.
9. Optimization Marcus Denker. 2 © Marcus Denker Optimization Roadmap  Introduction  Optimizations in the Back-end  The Optimizer  SSA Optimizations.
1 CS 201 Compiler Construction Lecture 8 Code Optimizations: Partial Dead Code Elimination.
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.
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:
Topic 6 -Code Generation Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
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.
Ben Livshits Based in part of Stanford class slides from
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.
1 Data-Flow Analysis Proving Little Theorems Data-Flow Equations Major Examples.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
CPSC 388 – Compiler Design and Construction Optimization.
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.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
Final Code Generation and Code Optimization.
More on Loop Optimization Data Flow Analysis CS 480.
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.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 Data flow analysis John Cavazos University.
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.
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
Code Optimization More Optimization Techniques. More Optimization Techniques  Loop optimization  Code motion  Strength reduction for induction variables.
Code Optimization Code produced by compilation algorithms can often be improved (ideally optimized) in terms of run-time speed and the amount of memory.
Code Optimization Overview and Examples
Lecture 5 Partial Redundancy Elimination
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.
CS 201 Compiler Construction
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
Topic-4a Dataflow Analysis 2019/2/22 \course\cpeg421-08s\Topic4-a.ppt.
Final Code Generation and Code Optimization
Static Single Assignment
Lecture 19: Code Optimisation
Optimizing Compilers CISC 673 Spring 2009 Data flow analysis
Code Optimization.
Presentation transcript:

1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination

2 Global Code Optimizations –Copy Propagation & Elimination: copy assignments are eliminated by replacing lhs variable with rhs variable. –Constant Folding & Propagation: constant assignments are exploited by substituting constant values for variables; if all operands of an expression are found to be constants, the expression can be computed at compile time. –Strength Reduction: replaces expensive operations with cheaper ones – multiplaction  addition; exponentiation  multiplication.

3 Global Code Optimizations –Partial Redundancy Elimination: eliminating repeated evaluation of the same expression. Loop Invariant Code Motion (full redundancy) Common Sub-Expression Elimination (full redundancy) The idea applies to expressions, loads, and even conditional branches. –Partial Dead Code Elimination: avoiding execution of statements whose results are not used. Dead Code Elimination The idea applies to expressions and stores.

4 Copy Propagation & Elimination 1. Uses of A replaced by uses of B. 2. A=B is eliminated because no more uses of A are left.

Contd.. Conditions under which optimization can be performed: Copy A=B is the only definition of A that reaches each use of A in def-use chain of A=B, i.e. no definition of A is encountered along the way. There is no definition of B on any path from d to any use u including paths through u that do not pass through d again. 5

Data Flow Equations 6 GEN[B]: Copies in B that reach the end of the block, i.e., no redefinitions of variables used in the copy statement. KILL[B]: Copies not in B whose variables (lhs or rhs) are defined in B. IN[B]: Copies that reach B’s entry. OUT[B]: Copies that reach B’s exit.

Data Flow Analysis The analysis propagates copies such that a copy reaches its use only if it can be propagated to the use. However, the copy can only be eliminated if it can be propagated to all of its uses. 7

Code Transformation Algorithm for each copy S: X=Y do OK = true for each use u on def-use(X,S) do if (s is in IN[B] where B contains a use of X) and (there is no redinifition of X or Y in B prior to u) then /* can propagate */ else OK = false endif /* cannot propagate */ endfor if Ok then replace all uses u of X in def-use(X,S) by use of Y; remove S endif endfor 8

9 Example Eliminated: D=A Not Eliminated: A=B Y=D W=A A=B X=A+2 W=A D=A A=3*M Y=D Z=D+2 W=D+3 Assume W,A,X,Y,Z Are live A=B X=B+2 A=3*M Y=B Z=B+2 W=B+3 W=B