Data Flow Analysis. Goal: make assertions about the data usage in a program Use these assertions to determine if and when optimizations are legal Local:

Slides:



Advertisements
Similar presentations
Example of Constructing the DAG (1)t 1 := 4 * iStep (1):create node 4 and i 0 Step (2):create node Step (3):attach identifier t 1 (2)t 2 := a[t 1 ]Step.
Advertisements

Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
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.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
1 Code Optimization. 2 The Code Optimizer Control flow analysis: control flow graph Data-flow analysis Transformations Front end Code generator Code optimizer.
1 Introduction to Data Flow Analysis. 2 Data Flow Analysis Construct representations for the structure of flow-of-data of programs based on the structure.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
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.
Optimizing Compilers for Modern Architectures Preliminary Transformations Chapter 4 of Allen and Kennedy.
1 CS 201 Compiler Construction Lecture 7 Code Optimizations: Partial Redundancy Elimination.
Partial Redundancy Elimination. Partial-Redundancy Elimination Minimize the number of expression evaluations By moving around the places where an expression.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
Foundations of Data-Flow Analysis. Basic Questions Under what circumstances is the iterative algorithm used in the data-flow analysis correct? How precise.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 More data flow analysis Emery Berger.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Data Flow Analysis Compiler Design Nov. 3, 2005.
Lecture 6 Program Flow Analysis Forrest Brewer Ryan Kastner Jose Amaral.
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.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
1 CS 201 Compiler Construction Lecture 6 Code Optimizations: Constant Propagation & Folding.
Data Flow Analysis Compiler Design Nov. 8, 2005.
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.
U NIVERSITY OF M ASSACHUSETTS, A MHERST D EPARTMENT OF C OMPUTER S CIENCE Advanced Compilers CMPSCI 710 Spring 2003 Data flow analysis Emery Berger University.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Data Flow Analysis Compiler Design Nov. 8, 2005.
A UNIFIED APPROACH TO GLOBAL PROGRAM OPTIMIZATION Proseminar „Programmanalyse”, Prof. Dr. Heike Wehrheim Universität Paderborn, WS 2011/2012.
Constant Propagation. The constant propagation framework is different from all the data-flow problems discussed so far, in that It has an unbounded set.
1 Region-Based Data Flow Analysis. 2 Loops Loops in programs deserve special treatment Because programs spend most of their time executing loops, improving.
Precision Going back to constant prop, in what cases would we lose precision?
1 CS 201 Compiler Construction Data Flow Analysis.
1 ECE 453 – CS 447 – SE 465 Software Testing & Quality Assurance Instructor Kostas Kontogiannis.
Data Flow Analysis. 2 Source code parsed to produce AST AST transformed to CFG Data flow analysis operates on control flow graph (and other intermediate.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
MIT Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
Global Redundancy Elimination: Computing Available Expressions Copyright 2011, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled.
Data-Flow Analysis (Chapter 8). Outline What is Data-Flow Analysis? Structure of an optimizing compiler An example: Reaching Definitions Basic Concepts:
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
CS 614: Theory and Construction of Compilers Lecture 15 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
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.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
1 Code Optimization Chapter 9 (1 st ed. Ch.10) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
Optimization Simone Campanoni
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2011 Yet More Data flow analysis John Cavazos.
Program Analysis and Verification Spring 2015 Program Analysis and Verification Lecture 8: Static Analysis II Roman Manevich Ben-Gurion University.
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
Data Flow Analysis Suman Jana
Dataflow Testing G. Rothermel.
CSC D70: Compiler Optimization Dataflow Analysis
Machine-Independent Optimization
Topic 10: Dataflow Analysis
University Of Virginia
Code Optimization Chapter 10
Code Optimization Chapter 9 (1st ed. Ch.10)
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.
Preliminary Transformations
Data Flow Analysis Compiler Design
Topic-4a Dataflow Analysis 2019/2/22 \course\cpeg421-08s\Topic4-a.ppt.
Static Single Assignment
Live variables and copy propagation
Presentation transcript:

Data Flow Analysis

Goal: make assertions about the data usage in a program Use these assertions to determine if and when optimizations are legal Local: within a single basic block  Analyze effect of each instruction  Compose effect at beginning/end of BB Global: within a procedure (across BBs)  Consider the effect of control flow Inter-procedural: across procedures References: Muchnick, Chapter 8. Dragon Book, , , 631. d=a+3 : d = 8 c=a+3 e=d+2

Data Flow Analysis Compile-time reasoning about the run-time flow of values in the program Represent facts about the run-time behavior Represent effect of executing each basic block Propagate facts around the control flow graph

Data Flow Analysis Formulated as a set of simultaneous equations  Sets attached to the nodes and edges  Lattice to describe the relation between values  Usually represented as a bit or bit vectors Solve equations using iterative framework  Start with initial guess of facts at each node  Propagate until stabilizes at maximal fixed point.  Would like meet over all paths (MOP) solution

Basic Approach: d=a+3 : d = 8 c=a+3 e=d+2 Perform analysis on each instruction in a basic block and compose the results at its boundaries. (local analysis) Must be conservative! Consider the effect of control flow (global analysis)

Example: Reaching Definitions Problem statement: for each basic block b find which of all definitions in the program reach the boundaries of b. Definition: A definition of a variable x is an instruction that assigns (or may assign) a value to x. Reaches: A definition d of variable x reaches a point p in the program if there exists a path from the point immediately following d to p such that d is not killed by another definition of x along this path.

Reaching Definitions: Gen Set Gen(b): the set of definitions that appear in a basic block b and reach its end. Finding Gen(b) is doing local reaching definitions analysis. c = c + c Exit BB 1 BB 2BB 3 a = 5 c = 1 a = a + 1 c > a? Entry a = c - a c = 0 d3 d1 d2 Gen (BB 1) = {d2, d3} d4 Gen (BB 2) = {d4} d5 d6 Gen (BB 3) = {d5, d6}

Reaching Definitions: Kill Set Kill(b): Set of definitions in other basic blocks that are killed in b (i.e., by instructions in b). For each variable v defined in b, the kill set contains all definitions of v in other basic blocks. c = c + c Exit BB 1 BB 2BB 3 a = 5 c = 1 a = a + 1 c > a? Entry a = c - a c = 0 d3 d1 d2 d4 d5 d6 Kill (BB 1) = {d5, d4, d6} Kill (BB 2) = {d2, d6} Kill (BB 3) = {d1, d3, d2, d4}

Reaching Definitions: Data Flow Equations RDin(b) : Set of definitions that reach the beginning of b. RDout(b) : Set of definitions that reach the end of b. BB nBB 1 BB b Inherited Set Synthesized Set

Reaching Definitions - Solving the Data Flow Equations c = c + c Exit BB 1 BB 2 BB 3 a = 5 c = 1 a = a + 1 c > a? Entry a = c - a c = 0 d1 d2 d3 d4 d5 d6 Repeat! RDin(BB 1)= RDout(Entry)  RDout(BB 2) =  RDout(BB 1) = Gen(BB 1)  [RDin(BB 1)-Kill (BB 1)] = {d2,d3}  [  -{d4,d5,d6}] = {d2,d3} RDin(BB 3)= RDout(BB 1) = {d2,d3} RDout(BB 3) = Gen(BB 3)  [RDin(BB 3)-Kill (BB 3)] = {d5,d6}  [{d2,d3}-{d1,d2,d3,d4}] = {d5,d6} RDin(BB 2)= RDout(BB 1) = {d2,d3} RDout(BB 2) = Gen(BB 2)  [RDin(BB 2)-Kill (BB 2)] = {d4}  [{d2,d3}-{d2,d6}] = {d3,d4} RDin(BB 1)= RDout(Entry)  RDout(BB 2)={d3,d4} RDout(BB 1) = Gen(BB 1)  [RDin(BB 1)-Kill (BB 1)] = {d2,d3}  [{d3,d4}-{d4,d5,d6}]={d2,d3} no change  done Where do we start? Why?  RDin(BB 3)= RDout(BB 1) = {d2,d3} RDout(BB 3) = Gen(BB 3)  [RDin(BB 3)-Kill (BB 3)] = {d5,d6}  [{d2,d3}-{d1,d2,d3,d4}] = {d5,d6} RDin(BB 2)= RDout(BB 1) = {d2,d3} RDout(BB 2) = Gen(BB 2)  [RDin(BB 2)-Kill (BB 2)] = {d4}  [{d2,d3}-{d2,d6}] = {d3,d4}

Other data flow problems Reaching definitions Live variables Available expressions Very busy expressions