Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved.

Slides:



Advertisements
Similar presentations
Topic G: Static Single-Assignment Form José Nelson Amaral
Advertisements

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.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
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.
Static Single Assignment CS 540. Spring Efficient Representations for Reachability Efficiency is measured in terms of the size of the representation.
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.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
A Deeper Look at Data-flow Analysis Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
Program Representations. Representing programs Goals.
SSA-Based Constant Propagation, SCP, SCCP, & the Issue of Combining Optimizations 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon,
Introduction to Code Optimization Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
6/9/2015© Hal Perkins & UW CSEU-1 CSE P 501 – Compilers SSA Hal Perkins Winter 2008.
Code Shape III Booleans, Relationals, & Control flow Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled.
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Introduction to Optimization Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Instruction Scheduling II: Beyond Basic Blocks Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp.
Precision Going back to constant prop, in what cases would we lose precision?
1 CS 201 Compiler Construction Data Flow Analysis.
Introduction to Optimization, II Value Numbering & Larger Scopes Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Using SSA Dead Code Elimination & Constant Propagation C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
Building SSA Form, III 1COMP 512, Rice University This lecture presents the problems inherent in out- of-SSA translation and some ways to solve them. Copyright.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Static Single Assignment John Cavazos.
Global Redundancy Elimination: Computing Available Expressions Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Cleaning up the CFG Eliminating useless nodes & edges C OMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon,
Introduction to Code Generation Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice.
Building SSA Form (A mildly abridged account) For the full story, see the lecture notes for COMP 512 (lecture 8) and.
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
Boolean & Relational Values Control-flow Constructs Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in.
Introduction to SSA Data-flow Analysis Revisited – Static Single Assignment (SSA) Form Liberally Borrowed from U. Delaware and Cooper and Torczon Text.
Cleaning up the CFG Eliminating useless nodes & edges This lecture describes the algorithm Clean, presented in Chapter 10 of EaC2e. The algorithm is due.
Building SSA Form, I 1COMP 512, Rice University Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
Definition-Use Chains
Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved.
Introduction to Optimization
Data Flow Analysis Suman Jana
Building SSA Form (A mildly abridged account)
Finding Global Redundancies with Hopcroft’s DFA Minimization Algorithm
Introduction to Optimization
Factored Use-Def Chains and Static Single Assignment Forms
Lexical Analysis — Part II: Constructing a Scanner from Regular Expressions Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
University Of Virginia
Intermediate Representations
Introduction to Code Generation
Building SSA Form COMP 512 Rice University Houston, Texas Fall 2003
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.
Lexical Analysis — Part II: Constructing a Scanner from Regular Expressions Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Code Shape III Booleans, Relationals, & Control flow
Intermediate Representations
Optimization through Redundancy Elimination: Value Numbering at Different Scopes COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith.
Static Single Assignment Form (SSA)
The Last Lecture COMP 512 Rice University Houston, Texas Fall 2003
Lexical Analysis — Part II: Constructing a Scanner from Regular Expressions Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.
Optimizations using SSA
Data Flow Analysis Compiler Design
Introduction to Optimization
Static Single Assignment
Optimizing Compilers CISC 673 Spring 2011 Static Single Assignment II
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.
Algebraic Reassociation of Expressions COMP 512 Rice University Houston, Texas Fall 2003 P. Briggs & K.D. Cooper, “Effective Partial Redundancy Elimination,”
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.
Building SSA Harry Xu CS 142 (b) 04/22/2018.
The Partitioning Algorithm for Detecting Congruent Expressions COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper.
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.
Presentation transcript:

Definition-Use Chains COMP 512 Rice University Houston, Texas Fall 2003 Copyright 2003, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 512 at Rice University have explicit permission to make copies of these materials for their personal use.

Information Chains A tuple that connects 2 data-flow events is a chain Chains express data-flow relationships directly Chains provide a graphical representation Chains jump across unrelated code, simplifying search We can build chains efficiently Four interesting types of chain Def-Use chains are the most common COMP 512, Fall 2003

Information Chains Example a  5 b  3 c  b + 2 d  a - 2 e  a + b e  e + c e  13 def-use chains d is dead It has no use f  2 + e Write f COMP 512, Fall 2003 *

X  DEFS(A,y )  y  USES(A,x ) Notation Assume that,  operation i and each variable v, DEFS(v,i ) is the set of operations that may have defined v most recently before i, along some path in the CFG USES(v,i ) is the set of operations that may use the value of v computed at i, along some path in the CFG X  DEFS(A,y )  y  USES(A,x ) To construct DEF-USE chains, we solve reaching definitions (YADFP ) A definition d of some variable v reaches an operation i if and only if i reads v and there is a v-clear path from d to i v-clear  no definition of v on the path Prior definition of v in same block  |DEFS(v,i )| = 1 No prior definition  |DEFS(v,i )| ≥ 1 The chains are non-local in this case COMP 512, Fall 2003

REACHES(n ) = ppreds(n) DEDEF(p )  (REACHES(p )  DEFKILL(p )) Domain is |DEFINITIONS|, same as number of operations Reaching Definitions The equations REACHES(n0 ) = Ø REACHES(n ) = ppreds(n) DEDEF(p )  (REACHES(p )  DEFKILL(p )) REACHES(n ) is the set of definitions that reach block n DEDEF(n) is the set of definitions in n that reach the end of n DEFKILL(n) is the set of defs obscured by a new def in n Computing REACHES(n) Use any data-flow method (rapid framework) Zadeck shows a simple linear-time scheme Form of f is same as in AVAIL F.K. Zadeck, “Incremental data-flow analysis in a structured program editor,” Proceedings of the SIGPLAN 84 Conf. on Compiler Construction, June, 1984, pages 132-143. COMP 512, Fall 2003 *

Building DEFS sets The Plan 1. Find basic blocks & build the CFG 2.  block b, compute REACHES(b) (to the fixed point) 3.  block b,  operation i,  referenced name v, Set DEFS(v,i ) according to the earlier rule A.) If there is a prior definition, d, of v in b DEFS(v,i )  d B.) Otherwise DEFS(v,i )  {d | d defines v & d  REACHES(b ) } To build USES Invert DEFS, or Solve reachable uses, and use the analogous construction COMP 512, Fall 2003

Building DEF-USE Chains Miscellany Domain of REACHES is the set of definitions ( |operations|) Domain of DEFS & USES is also definitions Need a compact representation of DEFS & USES Detecting Anomalies DEFS(v,i ) = Ø implies use of a never initialized variable Add a definition for each v to n0 to detect larger set of anomalies If initial def  DEFS(v,i ) then  a path with no initialization And, how do we use these information chains? COMP 512, Fall 2003 *

Constant Propagation over DEF-USE Chains Worklist  Ø For i  1 to number of operations if in1 of operation i is a constant ci then Value(in1,i )  ci else Value(in1,i )  T if in2 of operation i is a constant cj then Value(in2,i )  cj else Value(in2,i )  T if (Value(in1,i ) is a constant & Value(in2,i ) is a constant) then Value(out,i )  evaluate op i Worklist  Worklist  {i } else Value(out,i )  T Initialization Step while ( Worklist ≠ Ø) remove a definition i from WorkList for each j  USES(out,i ) set x so that out of i is inx of j Value(inx,j )  Value(inx,j )  Value(out,,i ) if (Value(in1,j ) is a constant & Value(in2,j ) is a constant) then Value(out,j )  evaluate op j Worklist  Worklist  {j } else if (Value(in1,j ) is  or Value(in2,j ) is ) then Value(out,j )   Propagation Step Any T left after fixed point derives from unitinitalized value: what should we do? COMP 512, Fall 2003

Constant Propagation over DEF-USE Chains Optimism Optimism This version of the algorithm is an optimistic formulation Initializes values to Prior version used  (implicit ) Optimistic initializations  Leads to i 12  = 12 12  12 Pessimistic initializations Leads to i 12  =  i  12 while ( … ) … x  i * 17 j  i i  … i  j Clear that i is always 12 at def of x  In general Optimism helps inside loops Largely a matter of initial value M.N. Wegman & F.K. Zadeck, Constant propagation with conditional branches, ACM TOPLAS, 13(2), April 1991, pages 181–210. * COMP 512, Fall 2003

Constant Propagation over DEF-USE Chains Complexity Initial step takes O(1) time per operation Propagation takes |USES(v,i )| for each i pulled from Worklist Summing over all ops, becomes |edges in Def-Use graph| A definition can be on the worklist twice (lattice height) O(|operations| + |edges in DU graph|) Can we do better? Not on the def-use chains … Would like to compute  when new values are “born” Where control flow brings chains together … COMP 512, Fall 2003

Constant Propagation over DEF-USE Chains Birth points Value is born here 17 - 4  y - z 17 - 4  y - z  13 Value is born  13  a+b We should be able to compute the values that we need with fewer meet operations, if only we can find these birth points. Need to identify birth points Need to insert some artifact to force the evaluation to follow the birth points Enter Static Single Assignment form x  17 - 4 x  a + b x  y - z Computes  of three values here Computes  of four values here x  13 z  x * q s  w - x COMP 512, Fall 2003

Constant Propagation over DEF-USE Chains Making Birth Points Explicit x0  17 - 4 There are three birth points for x x5  a + b x1  y - z x3  13 z  x4 * q s  w - x6 COMP 512, Fall 2003 *

Constant Propagation over DEF-USE Chains Making Birth Points Explicit x0  17 - 4 Each needs a definition to reconcile the values of x Insert a ø-function at each birth point Rename values so each name is defined once Now, each use refers to one definition  Static Single Assignment Form x5  a + b x1  y - z x2  Ø(x1,x0) x4  Ø(x3,x2) x6  Ø(x5,x4) x3  13 z  x4 * q s  w - x6 COMP 512, Fall 2003 *

Constant Propagation over DEF-USE Chains Making Birth Points Explicit x0  17 - 4 How do we build SSA form? Simple algorithm 1. Insert a ø at each join point for each name 2. Rename to get single definition & single use This produces Correct SSA form More ø’s than any other known algorithm for SSA construction The rest is optimization (!) DEF-USE chains on SSA form One def per use Meets occur at ø functions For CONSTANTS, 3 binary ’s x5  a + b x1  y - z x2  Ø(x1,x0) x4  Ø(x3,x2) x6  Ø(x5,x4) x3  13 z  x4 * q s  w - x6 Next class: SSA Construction COMP 512, Fall 2003 *