Basic Block Optimizations

Slides:



Advertisements
Similar presentations
CSC 4181 Compiler Construction Code Generation & Optimization.
Advertisements

7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
Lecture 11: Code Optimization CS 540 George Mason University.
Computer Science 313 – Advanced Programming Topics.
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.
ECE 454 Computer Systems Programming Compiler and Optimization (I) Ding Yuan ECE Dept., University of Toronto
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
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-
Optimization Compiler Baojian Hua
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
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.
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.
9. Optimization Marcus Denker. 2 © Marcus Denker Optimization Roadmap  Introduction  Optimizations in the Back-end  The Optimizer  SSA Optimizations.
Code Generation Professor Yihjia Tsai Tamkang University.
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 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.
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.
Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University Second Semester 2008/2009.
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.
Topic #10: Optimization EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
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”
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
More on Loop Optimization Data Flow Analysis CS 480.
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.
Code Optimization More Optimization Techniques. More Optimization Techniques  Loop optimization  Code motion  Strength reduction for induction variables.
Code Optimization Overview and Examples
Code Optimization.
Data Flow Analysis Suman Jana
MD5 A Hash Algorithm….
Basic Block Optimizations
MD5 A Hash Algorithm….
Fall Compiler Principles Lecture 8: Loop Optimizations
Machine-Independent Optimization
Topic 10: Dataflow Analysis
Global optimizations.
Programming Languages (CS 550) Mini Language Compiler
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.
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
Fall Compiler Principles Lecture 10: Global Optimizations
EECS 583 – Class 8 Classic Optimization
Fall Compiler Principles Lecture 10: Loop Optimizations
Data Flow Analysis Compiler Design
Local Optimizations.
Optimization 薛智文 (textbook ch# 9) 薛智文 96 Spring.
Compiler Construction
EECS 583 – Class 9 Classic and ILP Optimization
Programming Languages (CS 360) Mini Language Compiler
Live Variables – Basic Block
Code Optimization.
Presentation transcript:

Basic Block Optimizations Common Sub-Expression Elimination a = (x+y)+z; b = x+y; t = x+y; a = t+z; b = t; Constant Propagation x = 5; b = x+y; b = 5+y; Algebraic Identities a = x * 1; a = x; Copy Propagation a = x+y; b = a; c = b+z; a = x+y; b = a; c = a+z; Dead Code Elimination a = x+y; c = a+z Strength Reduction t = i * 4; t = i << 2;

Value Numbering Iterate over statements of each basic block Simulate execution of basic block Assign virtual values to variables, expressions Find vars, exprs with same value Common subexpression elimination (CSE) Save computed values in temporaries Replace expr with temp iff value previously computed

Assumed Intermediate Form var = var op var (op is a binary operator) var = op var (op is a unary operator) var = var

Value Numbering Summary Forward symbolic execution of basic block Each new value assigned to temporary a = x+y; becomes a = x+y; t = a; Temp saves value for later use a = x+y; a = a+z; b = x+y becomes 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 expression Exp to Tmp – specifies temp of each expression

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 Which temp to use if value(y) + value(z) previously computed Exp to Val Used to update Var to Val when processing stmt of form x = y + z, and value(y) + value(z) previously computed

New Block Original Basic Block Var to Val Exp to Val Exp to Tmp a = x+y a = x+y t1 = a b = a+z b = a+z b = b+y t2 = b c = a+z b = b+y t3 = b c = t2 Var to Val x  v1 Exp to Val Exp to Tmp y  v2 a  v3 v1+v2  v3 v1+v2  t1 z  v4 v3+v4  v5 v3+v4  t2 b  v5 b  v6 v5+v2  t3 v5+v2  v6 c  v5

Useful Properties Finds CSE even if exprs use different variables y = a+b; x = b; z = a+x becomes y = a+b; t = y; x = b; z = t Why? Computes with symbolic values Finds CSE even if a variable is overwritten y = a+b; x = b; y = 1; z = a+x becomes y = a+b; t = y; x = b; y = 1; z = t Why? Saves values away in temporaries

Problems Introduces lots of (unnecessary ?) So, Temporaries Copy statements to temporaries So, Remove many copy statements Copy propagation Dead code elimination Map temps to registers (register assignment)