U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.

Slides:



Advertisements
Similar presentations
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Advertisements

Lecture 11: Code Optimization CS 540 George Mason University.
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.
C Chuen-Liang Chen, NTUCS&IE / 321 OPTIMIZATION Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
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 & Lazy Code Motion
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Lecture 2 Emery Berger University of.
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,
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Computing SSA Emery Berger University.
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
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.
Improving Code Generation Honors Compilers April 16 th 2002.
Introduction to Optimization Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Data flow analysis Emery Berger University.
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.
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.
Global Common Subexpression Elimination with Data-flow Analysis Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Introduction to Optimization, II Value Numbering & Larger Scopes Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
MIT Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Dominators, etc. Emery Berger University.
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.
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.
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.
Introduction to Optimization
Data Flow Analysis Suman Jana
Lecture 5 Partial Redundancy Elimination
Basic Block Optimizations
Topic 10: Dataflow Analysis
Introduction to Optimization
Code Generation Part III
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
Optimization through Redundancy Elimination: Value Numbering at Different Scopes COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith.
Code Generation Part III
Static Single Assignment Form (SSA)
Data Flow Analysis Compiler Design
Introduction to Optimization
Static Single Assignment
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Optimizing Compilers CISC 673 Spring 2009 Data flow analysis
Live Variables – Basic Block
Basic Block Optimizations
Presentation transcript:

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710 Spring 2003 Common Subexpression Elimination

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 2 Topics Last time Dynamic storage allocation, garbage collection This time Common subexpression elimination Value numbering Global CSE

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 3 Determining Equivalence Goal: eliminate redundant computations Sparse conditional constant propagation: Eliminates multiple computations Eliminates unnecessary branches Can we eliminate equivalent expressions without constants?

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 4 Common Subexpression Elimination Recognizes textually identical (or commutative) redundant computations Replaces second computation by result of the first How do we do this efficiently?

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 5 Value Numbering Each variable, expression, and constant: unique value number Same number ) computes same value Based on information from within block Use hash functions to compute these

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 6 Computing Value Numbers Assign values to variables a = 3 ) value(a) = 3 Map expressions to values a = b + 2 ) value(a) = hash(+,value(b),2) Use appropriate hash function Plus: commutative hashc(+,value(b),2) = hashc(+,2,value(b)) Minus: not commutative hash(-,value(b),2)  hash(-,2,value(b))

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 7 Value Numbering Summary Forward symbolic execution of basic block Each new value assigned to temporary Preserves value for later use even if original variable rewritten a = x+y; a = a+z; b = x+y ) a = x+y; t = a; a = a+z; b = t; Maps Var to Val specifies symbolic value for each variable Exp to Val specifies value of each evaluated expression Exp to Tmp specifies tmp that holds value of each evaluated expression

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 8 Map Usage Var to Val Used to compute symbolic value of y and z when processing statement of form x = y + z Exp to Tmp Used to determine which temp to use if value(y) + value(z) previously computed when processing statement of form x = y + z Exp to Val Used to update Var to Val when processing statement of the form x = y + z, and value(y) + value(z) previously computed

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 9 b  v5 b  v6 a = x+y b = a+z b = b+y c = a+z a = x+y t1 = a b = a+z t2 = b b = b+y t3 = b x  v1 y  v2 a  v3 z  v4 c  v5 Original Basic Block New Basic Block Var  Val v1+v2  v3 v3+v4  v5 Exp  Val v1+v2  t1 v3+v4  t2 Exp  Tmp c = t2 v5+v2  v6 v5+v2  t3 Computing Value Numbers, Example

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 10 Interesting Properties Finds common subexpressions even if they use different variables in expressions y = a+b; x = b; z = a+x ) y = a+b; t = y; x = b; z = t Finds common subexpressions even if variable that originally held the value was overwritten y = a+b; x = b; y = 1; z = a+x ) y = a+b; t = y; x = b; y = 1; z = t

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 11 Problems Algorithm has a temporary for each new value a = x+y; t1 = a; Introduces lots of temporaries lots of copy statements to temporaries In many cases, temporaries and copy statements are unnecessary Eliminate with copy propagation and dead code elimination

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 12 Global CSE Value numbering eliminates some subexpressions but not all l’s value is not always equal to j’s or k’s value

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 13 Available Expressions Global CSE requires computation of available expressions for blocks b: Expressions on every path in cfg from entry to b No operand in expression redefined Then use appropriate temp variable for used available expressions

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 14 Available Expressions: Dataflow Equations For a block b: AEin(b) = expressions available on entry to b KILL(b) = expressions killed in b EVAL(b) = expressions defined in b and not subsequently killed in b

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 15 Available Expressions, Example Build control-flow graph Solve dataflow problem Initialize AEin(i) = universal set of expressions AEin(b) = Å j 2 Pred(i) AEout(j) AEout(b) = EVAL(i) [ (AEin(i) – KILL(i))

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 16 Next Time Partial Redundancy Elimination Read ACDI: Ch. 13

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 17 Value Numbering Example Step 1: insert temps for conditionals

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 18 Value Numbering Example Step 2: Add entries for each rhs Remove entries when dependent variable changes