Objectives Identify advantages (and disadvantages ?) of optimizing in SSA form Given a CFG in SSA form, perform Global Constant Propagation Dead code elimination.

Slides:



Advertisements
Similar presentations
SSA and CPS CS153: Compilers Greg Morrisett. Monadic Form vs CFGs Consider CFG available exp. analysis: statement gen's kill's x:=v 1 p v 2 x:=v 1 p v.
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
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.
Lecture 11: Code Optimization CS 540 George Mason University.
Chapter 9 Code optimization Section 0 overview 1.Position of code optimizer 2.Purpose of code optimizer to get better efficiency –Run faster –Take less.
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.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Some Properties of SSA Mooly Sagiv. Outline Why is it called Static Single Assignment form What does it buy us? How much does it cost us? Open questions.
Stanford University CS243 Winter 2006 Wei Li 1 Register Allocation.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
Program Representations. Representing programs Goals.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
Common Sub-expression Elim Want to compute when an expression is available in a var Domain:
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 10 SSA-Based Sparse Conditional Constant Propagation.
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.
Representing programs Goals. Representing programs Primary goals –analysis is easy and effective just a few cases to handle directly link related things.
Improving code generation. Better code generation requires greater context Over expressions: optimal ordering of subtrees Over basic blocks: Common subexpression.
1 Intermediate representation Goals: –encode knowledge about the program –facilitate analysis –facilitate retargeting –facilitate optimization scanning.
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.
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 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.
Recap from last time: live variables x := 5 y := x + 2 x := x + 1 y := x y...
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
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 Lecture 10 SSA-Based Sparse Conditional Constant Propagation.
Precision Going back to constant prop, in what cases would we lose precision?
CSE P501 – Compiler Construction
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Emery Berger University of Massachusetts, Amherst Advanced Compilers CMPSCI 710.
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
Compiler Principles Fall Compiler Principles Lecture 0: Local Optimizations Roman Manevich Ben-Gurion University.
Generating SSA Form (mostly from Morgan). Why is SSA form useful? For many dataflow problems, SSA form enables sparse dataflow analysis that –yields the.
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Code Optimization More Optimization Techniques. More Optimization Techniques  Loop optimization  Code motion  Strength reduction for induction variables.
Definition-Use Chains
Code Optimization Overview and Examples
CS 201 Compiler Construction
© Seth Copen Goldstein & Todd C. Mowry
CS 201 Compiler Construction
Topic 10: Dataflow Analysis
CSC D70: Compiler Optimization Static Single Assignment (SSA)
CS 201 Compiler Construction
CS 201 Compiler Construction
Code Optimization Overview and Examples Control Flow Graph
Static Single Assignment Form (SSA)
Optimizations using SSA
Data Flow Analysis Compiler Design
Reference These slides, with minor modification and some deletion, come from U. of Delaware – and the web, of course. 4/4/2019 CPEG421-05S/Topic5.
Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved.
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.
EECS 583 – Class 7 Static Single Assignment Form
SSA-based Optimizations
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.
Presentation transcript:

Objectives Identify advantages (and disadvantages ?) of optimizing in SSA form Given a CFG in SSA form, perform Global Constant Propagation Dead code elimination Global Value Numbering Conversion of SSA back to CFG form

Constant Propagation Operate on sparse graph (SSA) Incorporate branch folding Meet operations at -nodes

EvalStmt Algorithm EvalStmt( Stmt ) if Stmt is arithmetic or -node evaluate Stmt if result is “lowered” foreach j  Uses (Stmt.Lval())) propagate result if j.block  Visited Work = { j } else if Stmt is a branch foreach possible destination DB of Stmt if Edge(Stmt, DB) not executable mark Edge(Stmt, DB) executable Blocks U= DB

Constrant Prop Algorithm Mark all CFG Edges not executable Init node of SSA graph to T Work = Visited = Ø Blocks = {Entry} While Work  Ø  Blocks Ø While Work  Ø Choose Stmt from Work EvalStmt (Stmt) While Blocks  Ø Choose BB from Blocks foreach Stmt in  (BB) if BB  Visited Visited = {BB}

Dead Code Elimination Use SSA to detect dead code Method Remove statement(s) that do not directly or indirectly use data observable outside the procedure Eliminate branches never taken Uses control dependence

EliminateDeadCode () Worklist = Necessary = Ø foreach BB  CFG foreach Stmt  BB if (Stmt defines external data )  (Stmt is and I/O instruction) (Stmt is function call) Necessary U= { Stmt } WorkList U= { Stmt } endif endfor

Eliminate Dead Code Algorithm while Worklist  Ø choose Stmt from Worklist BB = Stmt’s basic block foreach block, CB, upon which BB is control dependent J = CB’s branch statement Necessary U= J Worklist U= J foreach T  Stmt’s operands Def = Stmt’s definition Necessary U= {Def } WorkList U= {Def } foreach BB  CFG foreach Stmt in BB if Stmt  Necessary remove Stmt else if Stmt is a branch  Stmt  Necessary Stmt.target = ipdom(BB)

Global Value Numbering Apply value numbering at function scope Associate a field (for value number) with each temporary. Temps with same value number are = No loops ==> reverse postorder traversal suffices (operands defined before used.)

Strongly Connected Components What about loops ? Value numbers for -nodes ambiguous Assume best case Iterate on the SCC in reverse postorder until no further changes

Processing -nodes If -node has a value number, VN, assign VN to temp defined by -node Consider operands with non-Null value numbers: If two have different values, assign a new value for -node. If all operands have same value number assign that to temp defined by -node

Efficiency Refinements If -node has non-Null value number different from its operands, don’t need a new value number For SCC, use a temp value number table called scratch table. Once that table “stabilizes”, copy to value table.

SSA to CFG -nodes “not executable” directly Must convert SSA back to CFG “Semantic” of -nodes is that they are “executed” simultaneously upon entry to block Insert code for each operand of -node in appropriate predecessor block.