Live Variables – Basic Block

Slides:



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

Lecture 11: Code Optimization CS 540 George Mason University.
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.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
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-
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
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
Partial Redundancy Elimination. Partial-Redundancy Elimination Minimize the number of expression evaluations By moving around the places where an expression.
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.
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.
Code Generation Professor Yihjia Tsai Tamkang 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.
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.
Prof. Fateman CS 164 Lecture 221 Global Optimization Lecture 22.
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.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
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.
Iterative Dataflow Problems Taken largely from notes of Alex Aiken (UC Berkeley) and Martin Rinard (MIT) Dataflow information used in optimization Several.
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
High-level optimization Jakub Yaghob
Code Optimization.
Data Flow Analysis Suman Jana
Iterative Dataflow Problems
Basic Block Optimizations
Fall Compiler Principles Lecture 8: Loop Optimizations
Machine-Independent Optimization
Topic 10: Dataflow Analysis
Global optimizations.
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
Optimizations Noam Rinetzky
Fall Compiler Principles Lecture 10: Global Optimizations
Fall Compiler Principles Lecture 10: Loop Optimizations
Data Flow Analysis Compiler Design
Static Single Assignment
Live Variable Analysis
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Basic Block Optimizations
Code Optimization.
Presentation transcript:

Live Variables – Basic Block For each block, BB, compute Written, read, readB4written Initialize each BB Live-in = readB4written, Live-out = Ø Repeat until neither live-in, live-out change For each BB Live-out(BB) = U Live-in(Successor) Live-in(BB) = Live-out(BB) – written(BB)

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 Basic 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)

Copy Propagation Simulate execution of program Use original variable instead of temporary a = x+y; b = x+y; After CSE becomes a = x+y; t = a; b = t; After CP becomes a = x+y; t = a; b = a; Key idea: determine when original variable is NOT overwritten between its assignment statement and the use of the computed value If not overwritten, use original variable

Live Analysis – Statements

Live Variables: Schematic Transfer function: May analysis: property holds on some path Backwards analysis: from outputs to inputs

Available Expressions

Available Expressions: Schematic Transfer function: Must analysis: property holds on all paths Forwards analysis: from inputs to outputs

One Cut at the Dataflow Design Space May Must Forwards Reaching definitions Available expressions Backwards Live variables Very busy expressions