SSA-based Optimizations

Slides:



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

8. Static Single Assignment Form Marcus Denker. © Marcus Denker SSA Roadmap  Static Single Assignment Form (SSA)  Converting to SSA Form  Examples.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
7. Optimization Prof. O. Nierstrasz Lecture notes by Marcus Denker.
1 Authors: Vugranam C. Sreedhar, Roy Dz-Ching Ju, David M. Gilles and Vatsa Santhanam Reader: Pushpinder Kaur Chouhan Translating Out of Static Single.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
SSA.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
Optimization Compiler Baojian Hua
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
Recap from last time We were trying to do Common Subexpression Elimination Compute expressions that are available at each program point.
Course project presentations No midterm project presentation Instead of classes, next week I’ll meet with each group individually, 30 mins each Two time.
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.
9. Optimization Marcus Denker. 2 © Marcus Denker Optimization Roadmap  Introduction  Optimizations in the Back-end  The Optimizer  SSA Optimizations.
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)
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.
Loop invariant detection using SSA An expression is invariant in a loop L iff: (base cases) –it’s a constant –it’s a variable use, all of whose single.
1 CS 201 Compiler Construction Lecture 6 Code Optimizations: Constant Propagation & Folding.
Intermediate Code. Local Optimizations
Improving Code Generation Honors Compilers April 16 th 2002.
From last class. The above is Click’s solution (PLDI 95)
Data Flow Analysis Compiler Baojian Hua
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.
Program Representations. Representing programs Goals.
Control Flow Analysis Compiler Baojian Hua
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
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.
Simone Campanoni SSA Simone Campanoni
Simone Campanoni CFA Simone Campanoni
Code Optimization Overview and Examples
Introduction to Optimization
Data Flow Analysis Suman Jana
Static Single Assignment
© Seth Copen Goldstein & Todd C. Mowry
Program Representations
Efficiently Computing SSA
Princeton University Spring 2016
Machine-Independent Optimization
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.
CSC D70: Compiler Optimization Static Single Assignment (SSA)
CS 201 Compiler Construction
Compiler Code Optimizations
Code Optimization Overview and Examples Control Flow Graph
Code Generation Part III
Static Single Assignment Form (SSA)
Optimizations Noam Rinetzky
Optimizations using SSA
Interval Partitioning of a Flow Graph
Data Flow Analysis Compiler Design
Introduction to Optimization
Reference These slides, with minor modification and some deletion, come from U. of Delaware – and the web, of course. 4/17/2019 CPEG421-05S/Topic5.
Taken largely from University of Delaware Compiler Notes
CSC D70: Compiler Optimization Static Single Assignment (SSA)
COMPILERS Liveness Analysis
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Objectives Identify advantages (and disadvantages ?) of optimizing in SSA form Given a CFG in SSA form, perform Global Constant Propagation Dead code elimination.
Objectives Identify advantages (and disadvantages ?) of optimizing in SSA form Given a CFG in SSA form, perform Global Constant Propagation Dead code elimination.
Live Variables – Basic Block
Code Optimization.
Presentation transcript:

SSA-based Optimizations Compiler Baojian Hua bjhua@ustc.edu.cn

SSA-based Optimizations translation AST IR1 opt translation SSA opt opt other IR and translation asm

SSA-based Optimization SSA is an IR which makes optimizations: faster and easier This time, we will re-study the previous dataflow-based optimizations, and to see how SSA be of a help constant propagation, copy propagation, dead-code elimination, constant folding, conditional constant propagation, ...

Constant propagation x = 3 … Each variable has just one unique definition, so we can substitute every use of x with 3. … a = x Can we replace this “x” with constant “3”?

Constant Propagation Example j = 1 k = 0 Constant propagation candidates. 1 1 1 The definitions becomes dead code. Why? 1 1 2 j = Φ(j , j ) k = Φ(k , k ) k < 100? 2 1 5 Copy propagation, dead-code elimination can be performed in a similar way. Leave to you. 2 1 5 2 3 4 j < 20? return j Does block 6 ever execute? 2 2 5 1 6 j = i k = k + 1 j = k k = k + 2 3 1 2 4 3 2 4 2 1 j = Φ(j , j ) k = Φ(k , k ) 7 5 3 4 5 3 4

Constant Propagation Example j = 1 k = 0 We found an invariant: j2==1. Assume blocks don’t execute until proven otherwise. Assume values are constants until proven otherwise. 1 1 1 1 j2==1; k2==0 1 To find this, we need conditional constant propagation. 2 j = Φ(j , j ) k = Φ(k , k ) k < 100? 2 1 5 j2==1; k2==1 2 1 5 j2==1; k2==2 2 3 4 j < 20? return j Does block 6 ever execute? 2 2 5 1 6 j = i k = k + 1 j = k k = k + 2 3 1 2 4 j2==1; k3==1 3 2 4 2 j2==1; k3==2 1 j = Φ(j , j ) k = Φ(k , k ) 7 5 3 4 j5==1; k5==1 5 3 4 j5==1; k5==2

Lattice block will executes T  no evidence block will execute Information flows down upwards! variable may be of different constant T … -2 -1 1 2 … evidence is seen that variable is constant n  no evidence variable is a constant

Lattice Example block 6 is dead! 1 2 3 4 5 6 7 T  j2==1 j3==1 j5==1 T j = Φ(1 , j ) k = Φ(0 , k ) k < 100? 2 5 2 5 2 3 4 j2 k2 j3 k3 j4 k4 j5 k5  1 j < 20? return j 2 2 5 6 j = 1 k = k + 1 j = k k = k + 2 3 2 4 1 1 1 1 1 3 2 4 2 1 T T T j = Φ(j , j ) k = Φ(k , k ) 7 5 3 4 5 3 4

Lattice Example i = 1 j = 1 k = 0 1 1 2 k = Φ(0 , k ) k < 100? 2 j = Φ(j , j ) k = Φ(k , k ) k < 100? 2 3 2 1 5 2 1 5 2 2 4 3 4 return 1 j < 20? return j 2 2 5 5 6 k = k + 1 j = i k = k + 1 j = k k = k + 2 3 1 2 4 3 2 3 2 4 2 j = Φ(j , j ) k = Φ(k , k ) 7 5 3 4 5 3 4

Aggressive Dead Code Elimination Do you notice the “useless” code on the left? 1 A statement is live, iff: 1. has side effect (I/O, call, store, …), and 2. defines a var which is used in live stm. 2 k = Φ(0 , k ) k < 100? 2 3 2 Lattice algorithm again: init live stm mark all other dead until proven to be live. 4 return 1 5 k = k + 1 return 1 3 2 This function can be further inlined and eliminated!

Aggressive Dead Code Elimination: pitfalls A statement is live, iff: 1. has side effect (I/O, call, store, …), and 2. defines a var which is used in live stm. 1 The 2nd property is call data-dependency! 2 k = Φ(0 , k ) k < 100? live 2 3 Should this be deleted? 2 4 return k live 2 5 k = k + 1 We need a notion of control-dependency! live 3 2

Fixing this problem If a statement S is live, then if T S is control-dependent on T, T should also be live x = 3 x<10? … live …

Control Dependency A block Y is control-dependent on X iff there exists an edge X->v, which v->exit goes through Y there exists a path X->exit which does not go through y X X u v u v Y Y exit exit

Aggressive Dead Code Elimination Example CFG reverse CFG 1 1 1 2 k = Φ(0 , k ) k < 100? 2 2 2 3 2 5 4 5 4 exit exit 4 return k 2 5 5 1 r k = k + 1 3 2 2 n 1 2 4 5 r DF {r} {2, r} {2} {} 4 exit

Aggressive Dead Code Elimination Example CDG 1 1 2 k = Φ(0 , k ) k < 100? live 2 2 3 live 2 5 4 4 return k live 2 5 k = k + 1 n 1 2 4 5 r DF {r} {2, r} {2} {} live 3 2

Aggressive Dead Code Elimination Example CDG 1 1 2 k = Φ(0 , k ) k < 100? 2 2 3 2 5 4 4 return 1 live return 1 5 k = k + 1 n 1 2 4 5 r DF {r} {2, r} {2} {} 3 2