Definition-Use Chains

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

School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) SSA Guo, Yao.
Operator Strength Reduction From Cooper, Simpson, & Vick, “Operator Strength Reduction”, ACM TOPLAS, 23(5), See also § of EaC2e. 1COMP 512,
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,
Lazy Code Motion Comp 512 Spring 2011
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.
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.
Data Flow Analysis Compiler Design Nov. 8, 2005.
Instruction Scheduling II: Beyond Basic Blocks Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Precision Going back to constant prop, in what cases would we lose precision?
Code Optimization, Part III Global Methods Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Structural Data-flow Analysis Algorithms: Allen-Cocke Interval Analysis Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students.
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.
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.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
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.
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
Single Static Assignment Intermediate Representation (or SSA IR) Many examples and pictures taken from Wikipedia.
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
Topic 10: Dataflow Analysis
Introduction to Optimization
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
Introduction to Code Generation
Building SSA Form 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.
Code Shape III Booleans, Relationals, & Control flow
CS 201 Compiler Construction
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)
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
Fall Compiler Principles Lecture 10: Loop Optimizations
Data Flow Analysis Compiler Design
Introduction to Optimization
Static Single Assignment
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.
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.
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 Spring 2011 Definition-Use Chains Copyright 2011, 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. Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved. COMP 512, Rice University

Last Class Worklist iterative algorithm for data-flow analysis Series of data-flow problems LIVE, AVAIL, VERYBUSY, CONSTANTS Example of non-distributive framework – CONSTANTS Example of non-rapid framework – Interprocedural MOD If we continue along this direction, the compiler solves one data-flow problem per transformation. Instead, we would like to solve one data-flow problem and use it for multiple transformations. Instead, the community invented information chains COMP 512, Rice University

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 event ≅ definition or use Def-Use chains are the most common COMP 512, Rice University

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

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, Rice University

REACHES(n) = ppreds(n) (DEDEF(p)  (REACHES(p)  DEFKILL(p))) Domain is |definitions|, same as number of operations Reaching Definitions The equations REACHES(n) = Ø,  n  N 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 LIVE 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, Rice University *

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, Rice University

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 to i with no initialization And, how do we use these information chains? COMP 512, Rice University *

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, Rice University

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, Rice University

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|) This approach (on a sparse graph) is faster than the straightforward iterative approach in the Kildall style COMP 512, Rice University

Constant Propagation (Classic formulation) Reminder of last lecture Constant Propagation (Classic formulation) Transformation: Global Constant Folding Along every path to p, v has same known value Specialize computation at p based on v’s value Data-flow problem: Constant Propagation Domain is the set of pairs <vi,ci> where vi is a variable and ci  C CONSTANTS(b) = p  preds(b) fp(CONSTANTS(p))  performs a pairwise meet on two sets of pairs fp(x) is a block specific function that models the effects of block p on the <vi,ci> pairs in x Constant propagation is a forward flow problem … c-2 c-1 c0 c1 c2 ... The Lattice C Form of f is quite different than in AVAIL COMP 512, Rice University *

Reminder of last lecture Example Meet operation requires more explanation c1  c2 = c1 if c1 = c2, else  (bottom & top as expected) What about fp ? If p has one statement then x  y with CONSTANTS(p) = {…<x,l1>,…<y,l2>…} then fp(CONSTANTS(p)) = CONSTANTS(p) - <x,l1> + <x,l2> X  y op z with CONSTANTS(p) = {…<x,l1>,…<y,l2>… >,…<z,l3>…} then fp(CONSTANTS(p)) = CONSTANTS(p) - <x,l1> + <x,l2 op l3> If p has n statements then fp(CONSTANTS(p)) = fn(fn-1(fn-2(…f2(f1(CONSTANTS(p)))…))) where fi is the function generated by the ith statement in p COMP 512, Rice University fp interprets p over CONSTANTS

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, Rice University

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, Rice University

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, Rice University *

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, Rice University *

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, Rice University *