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

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

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

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

Data Flow Information. already computed

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

Lecture 21 CIS 341: COMPILERS

Dataflow analysis (ctd.)

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

Register allocation. CS Compiler Design. Liveness analysis. Register allocation. Liveness analysis and Register allocation. V.

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

Foundations of Dataflow Analysis

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

CS202 Compiler Construction

Data Flow Analysis. Program Analysis

CS553 Lecture Generalizing Data-flow Analysis 3

Compilation 2013 Liveness Analysis

Program Optimizations using Data-Flow Analysis

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

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

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

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

Data-flow Analysis - Part 2

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

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

CS202 Compiler Construction

Programming Language Processor Theory

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

Lecture 6 Foundations of Data Flow Analysis

Why Global Dataflow Analysis?

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

Compiler Optimisation

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

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

Lecture 4 Introduction to Data Flow Analysis

Lecture 6 Foundations of Data Flow Analysis

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

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

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

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

Compiler Optimisation

Lecture 2. Introduction to Data Flow Analysis

An example of optimization in LLVM. Compiler construction Step 1: Naive translation to LLVM. Step 2: Translating to SSA form (opt -mem2reg)

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

Code optimization. Have we achieved optimal code? Impossible to answer! We make improvements to the code. Aim: faster code and/or less space

CSCE 548 Building Secure Software Data Flow Analysis

Compiler Optimization and Code Generation

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

CIS 341 Final Examination 4 May 2017

Compiler Construction 2010/2011 Loop Optimizations

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

Plan for Today. Concepts. Next Time. Some slides are from Calvin Lin s grad compiler slides. CS553 Lecture 2 Optimizations and LLVM 1

Compiler Construction 2009/2010 SSA Static Single Assignment Form

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

Example of Global Data-Flow Analysis

Static Detection of Access Anomalies in Ada95

Calvin Lin The University of Texas at Austin

Dependence and Data Flow Models

Data-Flow Analysis Foundations

Calvin Lin The University of Texas at Austin

Lecture 23 CIS 341: COMPILERS

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

Compiler Construction 2016/2017 Loop Optimizations

Data Flow Analysis and Computation of SSA

Introduction to Machine-Independent Optimizations - 4

Lecture Notes: Dataflow Analysis Examples

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

compile-time reasoning about the run-time ow represent facts about run-time behavior represent eect of executing each basic block

More Dataflow Analysis

Topic I (d): Static Single Assignment Form (SSA)

CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion

Compiler construction in4303 answers

Loops. Lather, Rinse, Repeat. CS4410: Spring 2013

Compiler Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Liveness Analysis and Register Allocation

Lecture Notes on Common Subexpression Elimination

ELEC 876: Software Reengineering

Program Static Analysis. Overview

Lecture Notes on Dataflow Analysis

Global Optimizations

Induction Variable Identification (cont)

Code Generation. Frédéric Haziza Spring Department of Computer Systems Uppsala University

When we eliminated global CSE's we introduced copy statements of the form A:=B. There are many

Name: CIS 341 Final Examination 10 December 2008

What Compilers Can and Cannot Do. Saman Amarasinghe Fall 2009

Department of Computer Sciences CS502. Purdue University is an Equal Opportunity/Equal Access institution.

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

Register allocation. instruction selection. machine code. register allocation. errors

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

Program analysis for determining opportunities for optimization: 2. analysis: dataow Organization 1. What kind of optimizations are useful? lattice al

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

Midterm 2. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: April 18, 2012

CSE 401 Final Exam. March 14, 2017 Happy π Day! (3/14) This exam is closed book, closed notes, closed electronics, closed neighbors, open mind,...

Global Optimizations

Chapter 10. Improving the Runtime Type Checker Type-Flow Analysis

CIS 341 Final Examination 30 April 2013

Intermediate representation

This test is not formatted for your answers. Submit your answers via to:

Flow Propagation Algorithm

CSE Section 10 - Dataflow and Single Static Assignment - Solutions

CSE 501 Midterm Exam: Sketch of Some Plausible Solutions Winter 1997

Transcription:

Available expressions Suppose we want to do common-subexpression elimination; that is, given a program that computes x y more than once, can we eliminate one of the duplicate computations? To find places where such optimizations are possible, the notion of available expressions is helpful. An expression x y is available at a node n in the flow graph if, on every path from the entry node of the graph to node n, x y is computed at least once and there are no definitions of x or y since the most recent occurrence of x y on that path. We can express this in dataflow equations using gen and kill sets, where the sets are now sets of expressions. Gen : Any node that computes x y generates {x y}. Kill : any definition of x or y kills {x y}. Table 8 summarizes the generate and kill effects. 1. t b + c generates the expression b + c, but b b + c does not generate b + c After b + c there is a subsequent definition of b. gen[s] = {b c} kill[s] 2. A store instruction (M[a] b) might modify any memory location, so it kills any fetch expression (M[x]). If we were sure that a x, we could be less conservative, and say that M[a] b does not kill M[x]. This is called alias analysis; Given gen and kill, we compute in and out almost as for reaching definitions, except that we compute the intersection of the out sets of the predecessors instead of a union. This reflects the fact that an expression is available only if it is computed on every path into the node. 14

Statement s gen[s] kill[s] t b c {b c} kill[s] expressions containing t t M[b] {M[b]} kill[s] expressions containing t M[a] b {} expressions of the form M[x] if a > b goto L 1 else goto L 2 {} {} goto L {} {} L : {} {} f(a 1,..., a n ) {} expressions of the form M[x] t f(a 1,..., a n ) {} expressions containing t, and expressions of the form M[x] Figure 8: Table: Gen and kill for available expressions. in[n] = out[p] p pred[n] out[n] = gen[n] (in[n] kill[n]) if n is not the start node To solve the dataflow equations by iteration, 1. we define the in set of the start node as empty, and 2. initialize all other sets to full (the set of all expressions), not empty. The intersection operator makes sets smaller, not bigger as the union operator does in the computation of reaching definitions. This algorithm then finds the greatest fixed point of the equations. in[ start node ] { } for each n other than the start node in[n] { all expressions }; out[n] { all expressions } repeat for each n in [n] in[n]; out [n] out[n] in[n] p pred[n] out[p] out[n] gen[n] (in[n] kill[n]) until in [n] = in[n] and out [n] = out[n] for all n 15

1 : x a + b 2 : if c > d goto L 1 3 : a p 4 : y a + b 5 : goto L 2 6 : L 1 : if e < f goto L 1 7 : L 2 : z a + b 1 : t a + b 1 : x t 2 : if c > d goto L 1 3 : a p 4 : t a + b 4 : y t 5 : goto L 2 6 : L 1 : if e < f goto L 1 7 : L 2 : z t Figure 9: Program We will take Program 9 as an example; Init Iter. 1 Iter. 2 n gen[n] kill[n] in[n] out[n] in[n] out[n] in[n] out[n] 1 a+b a+b a+b a+b 2 a+b a+b a+b a+b a+b a+b 3 a+b a+b a+b a+b a+b 4 a+b a+b a+b a+b a+b 5 a+b a+b a+b a+b a+b a+b 6 a+b a+b a+b a+b a+b a+b 7 a+b a+b a+b a+b a+b a+b a+b As a result, we can eliminate a + b at statement 7 by replacing it with temporary t introduced for statements 1 and 4. 16

Statement s gen[s] kill[s] t b c {b, c} {t} t M[b] {b} {t} M[a] b {a, b} {} if a > b goto L 1 else goto L 2 {a, b} {} goto L {} {} L : {} {} f(a 1,..., a n ) {a 1,..., a n } {} t f(a 1,..., a n ) {a 1,..., a n } {t} Liveness analysis Figure 10: Table: Gen and kill for liveness analysis. Two temporaries a and b can fit into the same register, if a and b are never in use at the same time. We say a variable is live if it holds a value that may be needed in the future. this analysis is called liveness analysis. Defs and uses of variable x Defs : assignments to variable/temporary x. Uses : assignments with occurrences of variable x on the right-hand sides of the assignments. A variable is live on an edge if there is a directed path from that edge to a use of the variable that does not go through any def. Gen and kill are defined as shown by Figure 10. Gen : any use of a variable generates liveness. Kill : any definition kills liveness. The equations for in and out are similar to the ones for reaching definition, but backward because liveness is a backward dataflow analysis: in[n] = gen[n] (out[n] kill[n]) out[n] = in[s] s succ[n] 17

To solve the dataflow equations by iteration, for each n in[n] { }; out[n] { } repeat for each n in [n] in[n]; out [n] out[n] out[n] s succ[n] in[s] in[n] gen[n] (out[n] kill[n]) until in [n] = in[n] and out [n] = out[n] for all n We will take Program 7 as an example. Iter. 1 Iter. 2 Iter. 3 n gen[n] kill[n] in[n] out[n] in[n] out[n] in[n] out[n] 1 a a a a 2 c a c,a a c,a a c,a 3 c,a c,a c,a c,a c,a c,a c,a 4 c c c c,a c,a c,a c,a 5 c,a c,a c,a c,a 6 c,a a c,a c,a c,a 7 c As a result, we can remove statements 6 and 7, because their results are never used. 18

2.4 Transformations using dataflow analysis Using the results of dataflow analysis, the optimizing compiler can improve the program in several ways. Common-subexpression elimination Given a flow-graph statement s : t x y, where the expression x y is available at s, the computation within s can be eliminated. Elimination steps of s: 1. Choose a new temporary w, and rewrite n with the same expression as s as follows: n : w x y n : v w 2. Finally, modify statement s to be s : t w We will rely on copy propagation to remove some or all of the extra assignment quadruples. Constant propagation Suppose that 1. we have a statement d : t c where c is a constant, and 2. another statement n that uses t, such as n : y t x. We know that t is constant in n if d reaches n, and no other definitions of t reach n. We can rewrite n as y c x. 19

Copy propagation This is like constant propagation, but instead of a constant c, we have a variable z. Suppose that 1. we have a statement d : t z, and 2. another statement n that uses t, such as n : y t x. If d reaches n, and no other definition of t reaches n, and there is no definition of z on any path from d to n (including a path that goes through n one or more times), then we can rewrite n as n : y z x. Copy propagation may enable the recognition of other optimizations such as common-subexpression elimination. Ex. in the program a y + z u y c u + z the two +-expressions are not recognized as common subexpressions until after the copy propagation of u y is performed. Dead-code elimination If there is a quadruple s : a b c or s : a M[x], such that a is not live-out of s, then the quadruple can be deleted. In some cases, applying dead-code elimination to a statement is not allowed because of its implicit side effects. Ex. if the computer is configured to raise an exception on an arithmetic overflow or divide by zero, then deletion of an exception-causing instruction will change the result of the computation. The optimizer should never make a change that changes program behavior. 20