Live Variable Analysis. Work List Iterative Algorithm Rehashed

Similar documents
Using Static Single Assignment Form

Computing Static Single Assignment (SSA) Form. Control Flow Analysis. Overview. Last Time. Constant propagation Dominator relationships

Combining Optimizations: Sparse Conditional Constant Propagation

Sparse Conditional Constant Propagation

Sparse Conditional Constant Propagation

Sparse Conditional Constant Propagation

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

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

Introduction to Machine-Independent Optimizations - 6

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

Program Optimizations using Data-Flow Analysis

A Sparse Algorithm for Predicated Global Value Numbering

Lecture 5. Data Flow Analysis

Constant Propagation

Introduction to Machine-Independent Optimizations - 4

The Development of Static Single Assignment Form

CS553 Lecture Generalizing Data-flow Analysis 3

CS153: Compilers Lecture 23: Static Single Assignment Form

Lecture 2. Introduction to Data Flow Analysis

Compiler Construction 2009/2010 SSA Static Single Assignment Form

Calvin Lin The University of Texas at Austin

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

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

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

Reuse Optimization. LLVM Compiler Infrastructure. Local Value Numbering. Local Value Numbering (cont)

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

Lecture 4 Introduction to Data Flow Analysis

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

CS 406/534 Compiler Construction Putting It All Together

Advanced Compiler Construction

Lecture 6 Foundations of Data Flow Analysis

Dataflow analysis (ctd.)

EECS 583 Class 6 Dataflow Analysis

Lecture Compiler Middle-End

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

Data-flow Analysis - Part 2

Lecture 6 Foundations of Data Flow Analysis

Thursday, December 23, The attack model: Static Program Analysis

More Dataflow Analysis

Calvin Lin The University of Texas at Austin

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

EECS 583 Class 6 Dataflow Analysis

Compiler Optimisation

Rematerialization. Graph Coloring Register Allocation. Some expressions are especially simple to recompute: Last Time

Control-Flow Analysis

Constant Propagation with Conditional Branches

Static Single Assignment (SSA) Form

Symbolic Solver for Live Variable Analysis of High Level Design Languages

EECS 583 Class 6 Dataflow Analysis

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

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

Extended SSA with factored use-def chains to support optimization and parallelism

CSE P 501 Compilers. SSA Hal Perkins Spring UW CSE P 501 Spring 2018 V-1

Data Flow Information. already computed

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

Code Placement, Code Motion

Why Global Dataflow Analysis?

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

EECS 583 Class 6 More Dataflow Analysis

Lecture Notes: Dataflow Analysis Examples

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

ELEC 876: Software Reengineering

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

The Static Single Assignment Form:

Conditional Elimination through Code Duplication

Lecture 3 Local Optimizations, Intro to SSA

Example of Global Data-Flow Analysis

CS 4120 Lecture 31 Interprocedural analysis, fixed-point algorithms 9 November 2011 Lecturer: Andrew Myers

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

Combining Analyses, Combining Optimizations - Summary

Foundations of Dataflow Analysis

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

Compiler Design. Register Allocation. Hwansoo Han

Interprocedural Analysis. Dealing with Procedures. Course so far: Terminology

Data Flow Analysis. Program Analysis

A Propagation Engine for GCC

Control Flow Analysis & Def-Use. Hwansoo Han

Compiler Optimization and Code Generation

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

Induction Variable Identification (cont)

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

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

Lecture 21 CIS 341: COMPILERS

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

CSE Section 10 - Dataflow and Single Static Assignment - Solutions

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

Principles of Program Analysis: Data Flow Analysis

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

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

CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion

Data Flow Analysis and Computation of SSA

Compilation 2013 Liveness Analysis

SSI revisited: A Program Representation for Sparse Data-flow Analyses

Algorithms: Lecture 10. Chalmers University of Technology

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

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Intermediate representation

Tour of common optimizations

What If. Static Single Assignment Form. Phi Functions. What does φ(v i,v j ) Mean?

CS 6110 S14 Lecture 38 Abstract Interpretation 30 April 2014

Transcription:

Putting Data Flow Analysis to Work Last Time Iterative Worklist Algorithm via Reaching Definitions Why it terminates. What it computes. Why it works. How fast it goes. Today Live Variable Analysis (backward problem) Constant Propagation: A Progression in Analysis Live Variable Analysis Can a variable v at a point p be used before it is redefined along some path starting at p? USE(p) - the set of variables that may be used before they are defined by this statement or basic block. DEF(p) - the set of variables that may be defined by this statement or basic block. 0: read I 1: read N 2: call check (N) 3: I = 1 4: while (I < N) do 5: A(i) = A(i) + I 6: I = I + 1 7: endwhile 8: print A(N) CS 380C Lecture 5 1 Data Flow Analysis CS 380C Lecture 5 2 Data Flow Analysis

Live Variable Analysis A backward data flow problem: For each point p in the program and each variable x, determine whether x can be used before being redefined along some path starting at p. For a basic block, x is live if it is used before being redefined within that block, or if it is live going out of the block. IN(v) is the set variables live coming into a block, and OUT(v) is the set of variables live going out of a block. USE(v): x USE(v) iff x may be used before it is defined in v DEF(v): x DEF(v) iff x must be defined before it is used in v (DEF(v) == KILL(v)) OUT(v) = S s SUCC(v) IN(s) IN(v) = USE(v) S (OUT(v) DEF(v)) The monotone data flow framework uses powerset of X (all variables) lattice. The transfer function Work List Iterative Algorithm Rehashed initialization worklist the set of all nodes while( worklist /0 ) pick and remove a node n from worklist recompute Data Flow Equations if the answer changed then add affected nodes to worklist Initialization: OUT(v) IN(v) Data flow equations: OUT(v) IN(v) The meet is set union. T v (x) = USE(v) [ (x DEF(v)) The operation space is monotone and distributive, therefore the solution will result in the MOP solution. CS 380C Lecture 5 3 Data Flow Analysis CS 380C Lecture 5 4 Data Flow Analysis

Live Variable Algorithm Algorithm: for all v OUT(v) = /0 IN(v) = USE(v) endfor worklist the set of all nodes while ( worklist /0 ) pick and remove [ a node v from worklist OUT(v) = IN(s) s SUCC(v) oldin = IN(v) IN(v) = USE(v) S (OUT(v) - DEF(v)) if oldin IN(v) worklist worklist PRED(v) end while Live Variable Example Can a variable v at a point p be used before it is redefined along some path starting at p? USE(p) - the set of variables that may be used before they are defined by this statement or basic block. DEF(p) - the set of variables that may be defined by this statement or basic block. 0: read I 1: read N 2: call check (N) 3: I = 1 4: while (I < N) do 5: A(i) = A(i) + I 6: I = I + 1 7: endwhile 8: print A(N) USE DEF LIVE CS 380C Lecture 5 5 Data Flow Analysis CS 380C Lecture 5 6 Data Flow Analysis

Constant Propagation Constant Propagation Discover variables and expressions that are constant and propagate them as far forward through the program as possible. Lattice: Uses: Evaluates expressions at compile time instead of runtime. Eliminate dead code, code that can never be executed, e.g., debugging code. Improves the effectiveness of many optimizations, e.g., value numbering, software pipelining. Since it is an analysis, there are no disadvantages.... 3 2 1 0 1 2 3... Meet Rules: a a a constant constant constant (if equal) constant constant (if not equal) Optimistic assumption: all variables start at an unknown constant value ( ) Pessimistic assumption: all variables are not constant ( = ) CS 380C Lecture 5 7 Data Flow Analysis CS 380C Lecture 5 8 Data Flow Analysis

Kildall s algorithm for Constant Propagation Using a worklist 1. Add successor basic blocks (statements) of start. 2. Given v = expression, 3. For every use w in expression, find the reaching definitions. If all constant, set value of w to the constant value, otherwise set value of w =. 4. If any w is, the set value of v =. = What is the effect of a pessimistic vs. optimistic assumption? 5. Otherwise, if they are all constants, set v to the value of the expression. DefUse Graph Reaching definitions and live variables help us find constants. A DefUse chain is a connection from a definition site for a variable to a use site for that variable along a path in the CFG without passing through another definition. How should we build it? z = x = z = x = Simple constants. No information is assumed about which direction branches take, and only one value for each variable is maintained along each path in the program. y = x = y = z y = x = y = z Time: O(E V 2 ) - E V node visits, V operations at a visit. Space: O(N V), N statements in the program = x + y + z DefUse edges = x + y + z DefJoin and JoinUse edges Kildall, G. A., A unified approach to global program optimization. conference Record of the First ACM Symposium on Principles of Programming Languages, October 1973, pages 194-206. Hints of static single assignment (SSA) CS 380C Lecture 5 9 Data Flow Analysis CS 380C Lecture 5 10 Data Flow Analysis

Reif and Lewis Constant Propagation Finds simple constants, but improves the time and space complexity of Kildall. Worklist algorithm: 1. Put all the root edges from the DefUse graph on the worklist. 2. A definition site in the roots, is assigned a constant, if it can be evaluated to a constant, otherwise it is assigned. 3. All other variables are assigned. 4. DefJoin edges are taken off the worklist. The value of the src of an edge is propagated to the use using the meet rules. 5. If the value is lowered, the new value is propagated and evaluated at the use expression. If this causes a variable to be lowered, the node is added to the worklist. Time: The complexity is now in terms of the DefUse graph. Reif and Lewis Constant Propagation i = 1 for () j = i i = f(...) i = j endfor Handles loops because of optimistic assumption. Previous techniques couldn t do loops. What do we need to do to detect that j is constant? i = 1 j = 3... if (i == 1) then j = 1 k = j CS 380C Lecture 5 11 Data Flow Analysis CS 380C Lecture 5 12 Data Flow Analysis

Wegman and Zadeck - Conditional Definition Conditional Definition. Keeps track of the results of conditional branches. A form of dead code elimination. Whenever the expression in a branch is a constant, determine the direction of the branch. Only propagate definitions when the flow graph node is marked as executable. Use symbolic execution of the program to mark edges. When propagating constants ignore edges at join nodes that are not executable. Wegman Zadeck - Conditional Constant Conditional Constant. Adds identity i = i on birth points that are not definitions to determine kills along paths that must be executed. Birth point for a variable v: Each definition site for v is a birth point. Let n be a node with two or more incoming edges. If there is a node m which is a birth point for v and there is a birth point free path from m to n along one in edge, but not the other, then n is a birth point for v. Time: O(2 N + 2 C) C is the number of DefUse chains. Wegman, M. N. and Zadeck, F. K., Constant Propagation with Conditional Branches, Conference Record of the Twelfth Annual ACM Symposium on the Principles of Programming Languages, January, 1985. CS 380C Lecture 5 13 Data Flow Analysis CS 380C Lecture 5 14 Data Flow Analysis

Constant Propagation truth tables Lattice for integer addition, multiplication, mod, etc. op c 1 c 1 c 2 c 2 c 1 op c 2 A last refinement if (i = 4) then j = i + 2... use of j, i endif... use of j, i Lattice for AND AND false true false false false false false false true false true false Insert an assignment/assertion i = 4 on the true branch. Lattice for OR OR false true true false false true true true true true true true CS 380C Lecture 5 15 Data Flow Analysis CS 380C Lecture 5 16 Data Flow Analysis

Worst Case Example Next Time select j when a do i = 1 when b do i = 2 when c do i = 3 end select select k when a do a = i when b do b = i when c do c = i end select i = 1 i = 2 i = 3 a = i b = i c = i i = 1 i = 2 i = 3 More Program Representation Dominators Control Dependence K. D. Cooper, T. Harvey, and K. Kennedy, A Simple, Fast Dominance Algorithm, Software Practice and Experience, 2001:4:1-28. a = i b = i c = i CS 380C Lecture 5 17 Data Flow Analysis CS 380C Lecture 5 18 Data Flow Analysis