Common Subexpression Elimination

Slides:



Advertisements
Similar presentations
Chapter Modules CSC1310 Fall Modules Modules Modules are the highest level program organization unit, usually correspond to source files and.
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Loops or Lather, Rinse, Repeat… CS153: Compilers Greg Morrisett.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Program Representations. Representing programs Goals.
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.
A small part of what you should know about continuations, but were too afraid to ask Nick Benton.
MT311 Tutorial Li Tak Sing( 李德成 ). Uploading your work You need to upload your work for tutorials and assignments at the following site:
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
1 CS 201 Compiler Construction Lecture 12 Global Register Allocation.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Recap from last time Saw several examples of optimizations –Constant folding –Constant Prop –Copy Prop –Common Sub-expression Elim –Partial Redundancy.
CS 536 Spring Intermediate Code. Local Optimizations. Lecture 22.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
4/23/09Prof. Hilfinger CS 164 Lecture 261 IL for Arrays & Local Optimizations Lecture 26 (Adapted from notes by R. Bodik and G. Necula)
4/25/08Prof. Hilfinger CS164 Lecture 371 Global Optimization Lecture 37 (From notes by R. Bodik & G. Necula)
Another example p := &x; *p := 5 y := x + 1;. Another example p := &x; *p := 5 y := x + 1; x := 5; *p := 3 y := x + 1; ???
CS 201 Compiler Construction
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.
Class canceled next Tuesday. Recap: Components of IR Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements.
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:
Copy Propagation and Common Subexpression Elimination in Titanium Johnathon Jamison David Marin CS265 S. Graham.
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.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
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.
Evaluation of Memory Consistency Models in Titanium.
Fast, Effective Code Generation in a Just-In-Time Java Compiler Rejin P. James & Roshan C. Subudhi CSE Department USC, Columbia.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
More Distributed Garbage Collection DC4 Reference Listing Distributed Mark and Sweep Tracing in Groups.
PLC '06 Experience in Testing Compiler Optimizers Using Comparison Checking Masataka Sassa and Daijiro Sudo Dept. of Mathematical and Computing Sciences.
CS 598 Scripting Languages Design and Implementation 9. Constant propagation and Type Inference.
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Chapter 2 Writing Simple Programs
Operational Semantics of Scheme
STACKS & QUEUES for CLASS XII ( C++).
User-Written Functions
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Static Single Assignment
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Basic Block Optimizations
Case Study: Undefined Variables
Relational Algebra 461 The slides for this text are organized into chapters. This lecture covers relational algebra, from Chapter 4. The relational calculus.
Common Subexpression Elimination
Common Subexpression Elimination and Copy Propagation in Titanium
Topic 10: Dataflow Analysis
Code Generation Part III
Multi-Way Search Trees
Advanced Program Design with C++
Arrays in Java What, why and how Copyright Curt Hill.
CS 201 Compiler Construction
Sridhar Narayan Java Basics Sridhar Narayan
Operator overloading Dr. Bhargavi Goswami
Code Generation Part III
Indirection.
Data Flow Analysis Compiler Design
Data Structures and Algorithms Introduction to Pointers
6.001 SICP Interpretation Parts of an interpreter
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Live Variables – Basic Block
Basic Block Optimizations
CS 201 Compiler Construction
Presentation transcript:

Common Subexpression Elimination Johnathon Jamison CS265 S. Graham

Outline Titanium Def/Use analysis (used by CSE) Common Subexpression Elimination Implementation Examples

Titanium Titanium is an extension of Java The Titanium compiler compiles Titanium code to C The C code is then compiled by the system compiler, e.g. gcc

Def/Use Given: a = … … …a… We want to link the use of a to the definition of a above.

Def/Use Every use of a variable has a list of all possible definitions associated with it Every definition of a variable has a list of all possible uses associated with it Method calls and pointer indirection are included in this analysis

Def/Use The Titanium compile has def/use information available It seems this could be leveraged for CSE

CSE Given: a = f * i … b = f * i We want to compute f * i only once

CSE We could do: a = f * i temp = a … b = temp But only if the value of f * i has not changed

Finding CSEs a = f * i … b = f * i The second f * i can be eliminated if the definitions of f and i that are used are exactly the same as the first Leverage our def/use analysis! But checking for that could be onerous

Finding CSEs So, lets create some fake definitions of f and i immediately before the first f * i Then, there is one explicit definition that can be traced to for checking the previously mentioned condition

Finding CSEs f = f i = i a = f * i … b = f * i Thus, if f and i have the same definitions in both places, then the second f * i can be eliminated

Handing Global CSEs This is fine and dandy for straight line code, but what if you have: a = f * i b = f * i … … c = f * i

Handing Global CSEs So, you need to see if f and i have the same definitions in all pairs of places where the common subexpression exists. I.e., does f or i have any definition that is not associated with a fake definition introduced by this analysis? If not, then an elimination can occur

Simultaneous CSEs The def/use analysis is expensive You can not run the def use analysis for every potential CSE Thus all CSEs should be analyzed simultaneously So, extra assignments are placed everywhere in the code a CSE could be

Simultaneous CSEs When tracing definitions, those introduced definitions must be explicitly ignored Trace back from a use If it is a definition associated with a CSE we are cool If it is an introduced one, pass through If it is neither, we can not use this

Altogether Now… Insert the extra assignments For every similar expression At every site, try to eliminate this expression Delete the assignments, so as not to interfere with anything else

Interaction with Copy Propagation Any temps introduced are placed after the calculation, so that copy propagation can remove them a = f * i a = f * i temp_1 = a … … b = f * i b = temp_1 temp_2 = b c = f * i c = temp_2

CSE Tidbits Compiler temps are placed at top level, as the live range of CSEs are unknown Associativity is accounted for Only binary and unary operations are done Can be extended

Examples

Timings – Preliminary Results CSE alone seems to have negligable effect Global copy propagation gives a few percent increase CSE on top of global copy propagation gives a couple percent more

Local CSE Used Muchnick’s algorithm (described in class) Used defs to find what was killed Fully implemented Except method calls Since we are using defs already, why not so something more substantial?