Data Flow Analysis. Program Analysis

Similar documents
Data Flow Analysis. Agenda CS738: Advanced Compiler Optimizations. 3-address Code Format. Assumptions

Lecture 2. Introduction to Data Flow Analysis

Lecture 4 Introduction to Data Flow Analysis

Compiler Optimization and Code Generation

Data-flow Analysis. Y.N. Srikant. Department of Computer Science and Automation Indian Institute of Science Bangalore

Data-flow Analysis - Part 2

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions.

Data Flow Information. already computed

Compiler Structure. Data Flow Analysis. Control-Flow Graph. Available Expressions. Data Flow Facts

ELEC 876: Software Reengineering

Dataflow Analysis. Xiaokang Qiu Purdue University. October 17, 2018 ECE 468

COSE312: Compilers. Lecture 20 Data-Flow Analysis (2)

Lecture Compiler Middle-End

Lecture 6 Foundations of Data Flow Analysis

Lecture 6 Foundations of Data Flow Analysis

A main goal is to achieve a better performance. Code Optimization. Chapter 9

Dataflow analysis (ctd.)

Control flow and loop detection. TDT4205 Lecture 29

CS 406/534 Compiler Construction Putting It All Together

Compiler Design. Fall Control-Flow Analysis. Prof. Pedro C. Diniz

Compiler Passes. Optimization. The Role of the Optimizer. Optimizations. The Optimizer (or Middle End) Traditional Three-pass Compiler

Code Optimization. Code Optimization

Register allocation. Register allocation: ffl have value in a register when used. ffl limited resources. ffl changes instruction choices

Compiler Design Spring 2017

Data Flow Analysis. CSCE Lecture 9-02/15/2018

Control Flow Analysis. Reading & Topics. Optimization Overview CS2210. Muchnick: chapter 7

Why Data Flow Models? Dependence and Data Flow Models. Learning objectives. Def-Use Pairs (1) Models from Chapter 5 emphasized control

Review; questions Basic Analyses (2) Assign (see Schedule for links)

Why Global Dataflow Analysis?

Compiler Optimisation

Dependence and Data Flow Models. (c) 2007 Mauro Pezzè & Michal Young Ch 6, slide 1

More Dataflow Analysis

Homework Assignment #1 Sample Solutions

Liveness Analysis and Register Allocation. Xiao Jia May 3 rd, 2013

Advanced Compilers Introduction to Dataflow Analysis by Example. Fall Chungnam National Univ. Eun-Sun Cho

Class 6. Review; questions Assign (see Schedule for links) Slicing overview (cont d) Problem Set 3: due 9/8/09. Program Slicing

Data Flow Analysis. Suman Jana. Adopted From U Penn CIS 570: Modern Programming Language Implementa=on (Autumn 2006)

Compiler Optimisation

Register Allocation. CS 502 Lecture 14 11/25/08

Compiler Design. Fall Data-Flow Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Goals of Program Optimization (1 of 2)

Flow Analysis. Data-flow analysis, Control-flow analysis, Abstract interpretation, AAM

Lecture 5. Partial Redundancy Elimination

Introduction. Data-flow Analysis by Iteration. CSc 553. Principles of Compilation. 28 : Optimization III

Intermediate representation

CS153: Compilers Lecture 17: Control Flow Graph and Data Flow Analysis

Program Static Analysis. Overview

Dependence and Data Flow Models. (c) 2007 Mauro Pezzè & Michal Young Ch 6, slide 1

Midterm 2. CMSC 430 Introduction to Compilers Fall Instructions Total 100. Name: November 21, 2016

Tour of common optimizations

Compiler Design Spring 2017

Challenges in the back end. CS Compiler Design. Basic blocks. Compiler analysis

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Conditional Elimination through Code Duplication

Interprocedural Heap Analysis using Access Graphs and Value Contexts

Compiler Optimizations. Chapter 8, Section 8.5 Chapter 9, Section 9.1.7

Global Optimization. Lecture Outline. Global flow analysis. Global constant propagation. Liveness analysis. Local Optimization. Global Optimization

MIT Introduction to Dataflow Analysis. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Middle End. Code Improvement (or Optimization) Analyzes IR and rewrites (or transforms) IR Primary goal is to reduce running time of the compiled code

Lecture 4. More on Data Flow: Constant Propagation, Speed, Loops

Lecture 10: Lazy Code Motion

Optimizations. Optimization Safety. Optimization Safety CS412/CS413. Introduction to Compilers Tim Teitelbaum

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

Sparse Conditional Constant Propagation

Program Analysis. Readings

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

A Bad Name. CS 2210: Optimization. Register Allocation. Optimization. Reaching Definitions. Dataflow Analyses 4/10/2013

Compiler Optimization Techniques

Lecture 3 Local Optimizations, Intro to SSA

Principles of Compiler Design

CS553 Lecture Generalizing Data-flow Analysis 3

Compilers. Optimization. Yannis Smaragdakis, U. Athens

Programming Language Processor Theory

CS 6353 Compiler Construction, Homework #3

COP5621 Exam 4 - Spring 2005

CS202 Compiler Construction

General issues. Section 9.1. Compiler Construction: Code Generation p. 1/18

Sparse Conditional Constant Propagation

Static Program Analysis Part 8 control flow analysis

4/24/18. Overview. Program Static Analysis. Has anyone done static analysis? What is static analysis? Why static analysis?

Intermediate Code & Local Optimizations

Compiler Construction 2010/2011 Loop Optimizations

Lecture Notes: Dataflow Analysis Examples

Advanced Compiler Construction

Data Structures and Algorithms in Compiler Optimization. Comp314 Lecture Dave Peixotto

Reaching Definitions and u-d Chaining M.B.Chandak

Dataflow Analysis 2. Data-flow Analysis. Algorithm. Example. Control flow graph (CFG)

Sparse Conditional Constant Propagation

Declarative Intraprocedural Flow Analysis of Java Source Code

20b -Advanced-DFA. J. L. Peterson, "Petri Nets," Computing Surveys, 9 (3), September 1977, pp

Example of Global Data-Flow Analysis

Page # 20b -Advanced-DFA. Reading assignment. State Propagation. GEN and KILL sets. Data Flow Analysis

Control-Flow Analysis

CS293S Redundancy Removal. Yufei Ding

HW/SW Codesign. WCET Analysis

Control-Flow Graphs & Dataflow Analysis. CS4410: Spring 2013

Languages and Compiler Design II IR Code Optimization

Global Optimizations

Transcription:

Program Analysis https://www.cse.iitb.ac.in/~karkare/cs618/ Data Flow Analysis Amey Karkare Dept of Computer Science and Engg IIT Kanpur Visiting IIT Bombay karkare@cse.iitk.ac.in karkare@cse.iitb.ac.in

Recap Optimizations Machine Independent Machine Dependent Analysis Intraprocedural Local Global Interprocedural 2

Agenda For the next few lectures Intraprocedural Data Flow analysis Components Classical examples 3

Assumptions Unless otherwise specified Intraprocedural: Restrict to a single procedure Input in 3 address code format 4

3 Address Code Assignments x = y op z x = op y x = y Jump/Control statements goto L if x relop y goto L Statements can have label(s) L: Arrays, Pointers and Procedures to be added later when needed 5

Data Flow Analysis Class of techniques to derive information about flow of data along program execution paths Used to answer questions such as: whether two identical expressions evaluate to same value used in common subexpression elimination whether the result of an assignment is used later used by dead code elimination 6

Data Flow Abstraction Basic Blocks (BB) sequence of 3-address code stmts single entry at the first statement single exit at the last statement Typically we use maximal basic block (maximal sequence of such instructions) 7

Data Flow Abstraction Leader: First statement of a basic block First instruction of program (procedure) Target of a branch (goto) Instruction immediately following a branch 8

Special Basic Blocks Two special BBs are added to simplify the analysis empty (?) blocks! Entry: Assumed to be the first block to be executed for the procedure analyzed Exit: Assumed to be the last block to be executed 9

Data Flow Abstraction Control Flow Graph (CFG) A rooted directed graph G= (N, E) N = set of BBs including entry, exit E = set of edges 10

CFG Edges Edge B1 B2 E if control can transfer from B1 to B2 Fall through Through jump (goto) Edge from entry to (all?) real first BB(s) Edge to exit from all last BBs BBs containing return Last real BB 11

Data Flow Abstraction Control Flow graph Graph representation of paths that program may exercise during execution Typically one graph per procedure Graphs for separate procedures have to be combined/connected for interprocedural analysis Later! Single procedure, single flow graph for now. 12

Data Flow Abstraction Input state/output state for Stmt Program point before/after a stmt Denoted IN[s] and OUT[s] Within a basic block: Program point after a stmt is same as the program point before the next stmt 13

Data Flow Abstraction Input state/output state for BBs Program point before/after a bb Denoted IN[B] and OUT[B] For B1 and B2: if there is an edge from B1 to B2 in CFG, then the program point after the last stmt of B1 may be followed immediately by the program point before the first stmt of B2. 14

Data Flow Abstraction Execution Path p 1,p 2,, p n p i -> p i+1 are adjacent program points in the CFG Infinite number of possible execution paths. No finite upper bound on the length. Need to Summarize the information at a program point with a finite set of facts. 15

Data Flow Schema Data flow values associated with each program point Summarize all possible states at that point Domain: set of all possible data flow values Different domains for different analysis/optimization 16

Data Flow Problem Constraints on data flow values Transfer constraints Control flow constraints AIM: To find a solution to the constraints Multiple solutions possible Trivial solutions,, Exact solutions We typically compute approximate solution, close to the exact solution Why not exact solution? 17

Data Flow Constraints Transfer functions relationship between the data flow values before and after a stmt forward functions OUT[s] = f s (IN[s]) backward functions IN[s] = f s (OUT[s]) 18

Data Flow Constraints Control flow constraints relationship between the data flow values of two points that are related by program execution semantics For a basic block having n statements: IN[s i+1 ] = OUT[s i ], i = 1,2,,n-1 IN[s 1 ], OUT[s n ] to come later 19

Data Flow Constraints: Basic Block Forward For B consisting of s 1, s 2, s n f B = f s n o o f s2 o f s1 OUT[B] = f B (IN[B]) Control flow constraints IN[B] = P a predecessor of B OUT[P] Backward IN[B] = f B (OUT[B]) OUT[B] = S a successor of B IN[S] 20

Data Flow Equations Typical Equation out[s] = in[s] kill[s] U gen[s] gen(s): information generated kill(s) : information killed For example: a = b * c // generates expression b*c c = 5 // kills expression b*c d = b * c // is b*c redundant here? 21

Analysis of Structured Programs Reaching Definitions Analysis 22

Analysis of Structured Programs Reaching Definitions Analysis definition of a variable x : x = something 22

Analysis of Structured Programs Reaching Definitions Analysis definition of a variable x : x = something Could be more complex (e.g. through pointers, references, implicit) 22

Analysis of Structured Programs Reaching Definitions Analysis definition of a variable x : x = something Could be more complex (e.g. through pointers, references, implicit) definition d reaches a point p if 22

Analysis of Structured Programs Reaching Definitions Analysis definition of a variable x : x = something Could be more complex (e.g. through pointers, references, implicit) definition d reaches a point p if there is a path from the point immediately following d to p 22

Analysis of Structured Programs Reaching Definitions Analysis definition of a variable x : x = something Could be more complex (e.g. through pointers, references, implicit) definition d reaches a point p if there is a path from the point immediately following d to p d is not killed along that path 22

Analysis of Structured Programs Reaching Definitions Analysis definition of a variable x : x = something Could be more complex (e.g. through pointers, references, implicit) definition d reaches a point p if there is a path from the point immediately following d to p d is not killed along that path Kill means redefinition of the left hand side (x in the above case) 22

Analysis of Structured Programs d: x = y+z s1 out(s1) = in(s1) - kill(s1) U gen(s1) gen(s1) = {d} kill(s1) = D x {d} // D x : set of all defs of x kill(s1) = D x will also work here! But may not work in general! 29

Analysis of Structured Programs S1 S S2 gen(s) = gen(s2) U (gen(s1) kill(s2)) kill(s) = kill(s2) U (kill(s1) gen(s2)) in(s1) = in(s) in(s2) = out(s1) out(s) = out(s2) 30

Analysis of Structured Programs S S1 S2 gen(s) = gen(s1) U gen(s2) kill(s) = kill(s1) kill(s2) in(s1) = in(s2) = in(s) out(s) = out(s1) U out(s2) 31

Analysis of Structured Programs S S1 gen(s) = gen(s1) kill(s) = kill(s1) in(s1) = in(s) U gen(s1) out(s) = out(s1) 32

Conservative Analysis S1 S2 S Assumption: All paths are feasible. Consider: if (true) s1; else s2 s2 is never executed gen(s) = gen(s1) gen(s1) gen(s2) kill(s) = kill(s1) kill(s1) kill(s2) 33

Conservative Analysis S1 S2 S Thus: true gen (s) analysis gen(s) true kill (s) analysis kill(s) True is what is computed at run time This is SAFE estimate prevents optimization but no wrong optimization 34