Compiler Optimisation

Similar documents
Compiler Optimisation

Compiler Optimisation

Introduction to Machine-Independent Optimizations - 4

Compiler Optimisation

CS577 Modern Language Processors. Spring 2018 Lecture Optimization

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

Lecture 6 Foundations of Data Flow Analysis

Lecture 6 Foundations of Data Flow Analysis

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

Topic 9: Control Flow

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

CSE Section 10 - Dataflow and Single Static Assignment - Solutions

Compiler Construction 2009/2010 SSA Static Single Assignment Form

Dataflow analysis (ctd.)

Homework Assignment #1 Sample Solutions

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

CS 432 Fall Mike Lam, Professor. Data-Flow Analysis

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

Lecture 23 CIS 341: COMPILERS

More Dataflow Analysis

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

Data Flow Analysis. Program Analysis

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

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

CS 6110 S14 Lecture 38 Abstract Interpretation 30 April 2014

Lecture 2. Introduction to Data Flow Analysis

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

Goals of Program Optimization (1 of 2)

Lecture Compiler Middle-End

An Overview of GCC Architecture (source: wikipedia) Control-Flow Analysis and Loop Detection

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

Introduction to Machine-Independent Optimizations - 6

Compiler Optimization and Code Generation

Static Single Assignment (SSA) Form

Code Placement, Code Motion

Data Flow Information. already computed

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

Why Global Dataflow Analysis?

Lecture 4 Introduction to Data Flow Analysis

Intermediate representation

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

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

Contents of Lecture 2

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

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

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

Data-flow Analysis - Part 2

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

CS 406/534 Compiler Construction Putting It All Together

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

Data Flow Analysis and Computation of SSA

CS153: Compilers Lecture 23: Static Single Assignment Form

Discrete Mathematics. Kruskal, order, sorting, induction

ECE 5775 High-Level Digital Design Automation Fall More CFG Static Single Assignment

Lattice Tutorial Version 1.0

Control Flow Analysis

CIS 341 Final Examination 4 May 2017

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

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

Example. Example. Constant Propagation in SSA

Control flow and loop detection. TDT4205 Lecture 29

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

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

Live Variable Analysis. Work List Iterative Algorithm Rehashed

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

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

Control Flow Analysis & Def-Use. Hwansoo Han

Principles of Program Analysis. Lecture 1 Harry Xu Spring 2013

SSA Construction. Daniel Grund & Sebastian Hack. CC Winter Term 09/10. Saarland University

Control-Flow Analysis

Register Allocation. Global Register Allocation Webs and Graph Coloring Node Splitting and Other Transformations

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

CONTROL FLOW ANALYSIS. The slides adapted from Vikram Adve

Compiler Construction 2016/2017 Loop Optimizations

PARTIALLY ORDERED SETS. James T. Smith San Francisco State University

CS202 Compiler Construction

Calvin Lin The University of Texas at Austin

Lecture 25 CIS 341: COMPILERS

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

Foundations of Dataflow Analysis

Lecture Notes on Loop-Invariant Code Motion

Example (cont.): Control Flow Graph. 2. Interval analysis (recursive) 3. Structural analysis (recursive)

Interprocedural Heap Analysis using Access Graphs and Value Contexts

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

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

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

EECS 583 Class 8 Classic Optimization

A Gentle Introduction to Program Analysis

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

8. Static Single Assignment Form. Marcus Denker

The Development of Static Single Assignment Form

Compiler Construction 2010/2011 Loop Optimizations

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

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

Advanced Compilers CMPSCI 710 Spring 2003 Computing SSA

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

Program Optimizations using Data-Flow Analysis

Compiler Design. Register Allocation. Hwansoo Han

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

Transcription:

Compiler Optimisation 4 Dataflow Analysis Hugh Leather IF 1.18a hleather@inf.ed.ac.uk Institute for Computing Systems Architecture School of Informatics University of Edinburgh 2018

Introduction This lecture: Data flow termination More data flow examples Dominance Static single-assignment form

Data flow termination Fixed Point A fixed point, x, of function f : T T is when f (x) = x Partial ordering A binary relation,, among elements of a set, S, that is: a S, a a reflexive a, b S, a b b a = a = b antisymmetric a, b, c S a b b c = a c transitive Partially ordered set (poset) A set with a partial order

Data flow termination Join semi-lattice Partially ordered set that has a join (a least upper bound) for any nonempty finite subset x, y, z V x (y z) = (x y) z x y = y x x x = x x = x Associativity Commutativity Idempotency Top element Note, meet semi-lattice has and complete lattice has both Often just use semi-lattice

Data flow termination Lattices Example lattice Hasse diagram for the set of all subsets of x, y, z, ordered by inclusion Is it a lattice?

Data flow termination Lattices Example lattice Hasse diagram for the set of all subsets of x, y, z, ordered by inclusion Yes, and is lattice for LiveOut, with variables, x, y, z

Data flow termination Lattices Example non-lattices

Data flow termination Lattices Example non-lattices Left: c and d have no common upper bound

Data flow termination Lattices Example non-lattices Right: b and c have common upper bounds (d, e, f), but no least upper bound

Data flow termination Each block or statement has a particular function, e.g. based on Kill and Gen sets Let F be the set of transfer functions Sufficient termination constraints V and for a semi-lattice F has identity: I(x) = x, x V F is closed under composition: f, g F, h = f g F F is monotonic: f F, f (x y) f (x) f (y) See 10.11

Liveness A variable v is live-out of statement s if v is used along some control path starting at s Otherwise, we say that v is dead A variable is live if it holds a value that may be needed in the future Information flows backwards from statement to predecessors Liveness useful for optimisations (e.g. register allocation, store elimination, dead code...)

Liveness A variable v is live-out of statement s if v is used along some control path starting at s

Liveness A variable v is live-out of statement s if v is used along some control path starting at s

Liveness A variable v is live-out of statement s if v is used along some control path starting at s

Liveness A variable v is live-out of statement s if v is used along some control path starting at s

Liveness A variable v is live-out of statement s if v is used along some control path starting at s

Liveness A variable v is live-out of statement s if v is used along some control path starting at s

Liveness A variable v is live-out of statement s if v is used along some control path starting at s

Liveness Live variables come up from their successors using them Out(s) = In(n) n Succ(s) Transfer back across the node In(s) = Out(s) Kill(s) Gen(s) Used variables are live Gen(s) = {u such that u is used in s} Defined but not used variables are killed Kill(s) = {d such that d is defined in s but not used in s} If we don t know, start with empty Init(s) =

Others Constant propagation - show variable has same constant value at some point Strictly speaking does not compute expressions except x := const, or x := y and y is constant Often combined with constant folding that computes expressions Copy propagation - show variable is copy of other variable Available expressions - set of expressions reaching by all paths Very busy expressions - expressions evaluated on all paths leaving block - for code hoisting Definite assignment - variable always assigned before use Redundant expressions, and partial redundant expressions Many more - read about them!

Dominators CFG node b i dominates b j, written b i b j, iff every path from the start node to b j goes through b i Design data flow equations to compute which nodes dominate each node What direction? What value set? What transfer? What Meet? Initial values?

Dominators CFG node b i dominates b j, written b i b j, iff every path from the start node to b j goes through b i Design data flow equations to compute which nodes dominate each node What direction? What value set? What transfer? What Meet? Initial values?

Dominators CFG node b i dominates b j, written b i b j, iff every path from the start node to b j goes through b i Design data flow equations to compute which nodes dominate each node Direction: Forward What value set? What transfer? What Meet? Initial values?

Dominators CFG node b i dominates b j, written b i b j, iff every path from the start node to b j goes through b i Design data flow equations to compute which nodes dominate each node Direction: Forward What value set? What transfer? What Meet? Initial values?

Dominators CFG node b i dominates b j, written b i b j, iff every path from the start node to b j goes through b i Design data flow equations to compute which nodes dominate each node Direction: Forward Values: Sets of nodes What transfer? What Meet? Initial values?

Dominators CFG node b i dominates b j, written b i b j, iff every path from the start node to b j goes through b i Design data flow equations to compute which nodes dominate each node Direction: Forward Values: Sets of nodes What transfer? What Meet? Initial values?

Dominators CFG node b i dominates b j, written b i b j, iff every path from the start node to b j goes through b i Design data flow equations to compute which nodes dominate each node Direction: Forward Values: Sets of nodes Transfer: Out(n) = In(n) {n} What Meet? Initial values?

Dominators CFG node b i dominates b j, written b i b j, iff every path from the start node to b j goes through b i Design data flow equations to compute which nodes dominate each node Direction: Forward Values: Sets of nodes Transfer: Out(n) = In(n) {n} What Meet? Initial values?

Dominators CFG node b i dominates b j, written b i b j, iff every path from the start node to b j goes through b i Design data flow equations to compute which nodes dominate each node Direction: Forward Values: Sets of nodes Transfer: Out(n) = In(n) {n} Meet: In(n) = Out(s) Initial values? n Pred(s)

Dominators CFG node b i dominates b j, written b i b j, iff every path from the start node to b j goes through b i Design data flow equations to compute which nodes dominate each node Direction: Forward Values: Sets of nodes Transfer: Out(n) = In(n) {n} Meet: In(n) = Out(s) Initial values? n Pred(s)

Dominators CFG node b i dominates b j, written b i b j, iff every path from the start node to b j goes through b i Design data flow equations to compute which nodes dominate each node Direction: Forward Values: Sets of nodes Transfer: Out(n) = In(n) {n} Meet: In(n) = Out(s) n Pred(s) Initial: Init(n 0 ) = {n 0 }; Init(n) = all

Dominators Post-dominator Node z is said to post-dominate a node n if all paths to the exit node of the graph starting at n must go through z Strict dominance Node a strictly dominates b iff a b a b Immediate dominator idom(n) strictly dominates n but not any other node that strictly dominates n Dominator tree Tree where node s children are those it immediately dominates Dominance frontier DF (n) is set of nodes, d s.t. n dominates an immediate predecessor of d, but n does not strictly dominate d

Dominators Example: Dominator tree Where are dominance frontiers?

Dominators Example: Dominator tree DF (b 5 ) = {b 3 }

Dominators Example: Dominator tree DF (b 1 ) = {b 1 }

Static single-assignment form (SSA) Often allowing variable redefinition complicates analysis In SSA: One variable per definition Each use refers to one definition Definitions merge with φ functions Φ functions execute instantaneously in parallel Used by or simplifies many analyses

Static single-assignment form (SSA) Example: Intuitive conversion to SSA Original CFG

Static single-assignment form (SSA) Example: Intuitive conversion to SSA Rename multiple definitions of same variable

Static single-assignment form (SSA) Example: Intuitive conversion to SSA Repeatedly merge definitions with φ

Static single-assignment form (SSA) Example: Intuitive conversion to SSA Now in SSA form

Static single-assignment form (SSA) Types of SSA Maximal SSA - Places φ node for variable x at every join block if block uses or defines x Minimal SSA - Places φ node for variable x at every join block with 2+ reaching definitions of x Semipruned SSA - Eliminates φs not live across block boundaries Pruned SSA - Adds liveness test to avoid φs of dead definitions

Static single-assignment form (SSA) Conversion to SSA sketch 2 For each definition 1 of x in block b, add φ for x in each block in DF (b) This introduces more definitions, so repeat Rename variables Can be done in T (n) = O(n), if liveness cheap 1 Different liveness tests (including none) here change SSA type 2 See EaC 9.3.1-9.3.4

Static single-assignment form (SSA) Conversion from SSA sketch 3 Cannot just remove φ nodes; optimisations make this unsafe Place copy operations on incoming edges Split edges if necessary Delete φs Remove redundant copies afterwards 3 See EaC 9.3.5

Static single-assignment form (SSA) Conversion from SSA Example: Intuitive conversion from SSA Original SSA CFG

Static single-assignment form (SSA) Conversion from SSA Example: Intuitive conversion from SSA Place copies

Static single-assignment form (SSA) Conversion from SSA Example: Intuitive conversion from SSA Split where necessary

Static single-assignment form (SSA) Conversion from SSA Example: Intuitive conversion from SSA Remove φs

Static single-assignment form (SSA) Extensions Dataflow assumes that all paths in the CFG are taken hence conservative approximations Guarded SSA attempts to overcome this by having additional meet nodes γ, η and µ to carry conditional information around Arrays considered monolithic objects A[1] =.., =A[2] considered a def-use Array based SSA models access patterns 4 Inter-procedural challenging. Pointers destroy analysis! Large research effort in points-to analysis. 4 Can be generalised using Presburger formula

Static single-assignment form (SSA) Constant propagation Three possible values per variable k Not a constant Constant value k Not computed (maybe never) Meet is x = x, x =, c c = c, c d = if c d Transfer functions compute value if all inputs are constant

Summary Data flow termination More data flow examples Dominance Static single-assignment form

PPar CDT Advert 4-year programme: MSc by Research + PhD Research-focused: Work on your thesis topic from the start Collaboration between: University of Edinburgh s School of Informatics Ranked top in the UK by 2014 REF Edinburgh Parallel Computing Centre UK s largest supercomputing centre Research topics in software, hardware, theory and application of: Parallelism Concurrency Distribution Full funding available Industrial engagement programme includes internships at leading companies Now accepting applications! Find out more and apply at: pervasiveparallelism.inf.ed.ac.uk The biggest revolution in the technological landscape for fifty years