CSC D70: Compiler Optimization Dataflow-2 and Loops

Similar documents
Reading: Chapter 9.3. Carnegie Mellon

CSC D70: Compiler Optimization Static Single Assignment (SSA)

Lecture 5 Introduction to Data Flow Analysis

We define a dataflow framework. A dataflow framework consists of:

Data flow analysis. DataFlow analysis

Dataflow Analysis. Chapter 9, Section 9.2, 9.3, 9.4

Topic-I-C Dataflow Analysis

Lecture 4 Introduc-on to Data Flow Analysis

Compiler Design. Data Flow Analysis. Hwansoo Han

CMSC 631 Program Analysis and Understanding. Spring Data Flow Analysis

Dataflow Analysis. Dragon book, Chapter 9, Section 9.2, 9.3, 9.4

Dataflow Analysis - 2. Monotone Dataflow Frameworks

Monotone Data Flow Analysis Framework

The Meet-Over-All-Paths Solution to Data-Flow Problems

MIT Loop Optimizations. Martin Rinard

Data-Flow Analysis. Compiler Design CSE 504. Preliminaries

Compiler Design Spring 2017

Dataflow Analysis. A sample program int fib10(void) { int n = 10; int older = 0; int old = 1; Simple Constant Propagation

Dataflow analysis. Theory and Applications. cs6463 1

Dataflow Analysis Lecture 2. Simple Constant Propagation. A sample program int fib10(void) {

MIT Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Verifying the LLVM. Steve Zdancewic DeepSpec Summer School 2017

Generalizing Data-flow Analysis

(b). Identify all basic blocks in your three address code. B1: 1; B2: 2; B3: 3,4,5,6; B4: 7,8,9; B5: 10; B6: 11; B7: 12,13,14; B8: 15;

What is SSA? each assignment to a variable is given a unique name all of the uses reached by that assignment are renamed

Lecture 11: Data Flow Analysis III

Data Flow Analysis (I)

Goal. Partially-ordered set. Game plan 2/2/2013. Solving fixpoint equations

Data Flow Analysis. Lecture 6 ECS 240. ECS 240 Data Flow Analysis 1

Compiler Design Spring 2017

Path Testing and Test Coverage. Chapter 9

Path Testing and Test Coverage. Chapter 9

Graph Transformations T1 and T2

Static Program Analysis

Lecture 10: Data Flow Analysis II

Model Checking & Program Analysis

Flow grammars a flow analysis methodology

Mechanics of Static Analysis

Space-aware data flow analysis

: Compiler Design. 9.5 Live variables 9.6 Available expressions. Thomas R. Gross. Computer Science Department ETH Zurich, Switzerland

DFA of non-distributive properties

Static Program Analysis

Chapter 9: Relations Relations

Computing Static Single Assignment (SSA) Form

ECE 5775 (Fall 17) High-Level Digital Design Automation. Control Flow Graph

Scalar Optimisation Part 2

Introduction to data-flow analysis. Data-flow analysis. Control-flow graphs. Data-flow analysis. Example: liveness. Requirements

Lecture Overview SSA Maximal SSA Semipruned SSA o Placing -functions o Renaming Translation out of SSA Using SSA: Sparse Simple Constant Propagation

Chapter 6: Computation Tree Logic

COSE312: Compilers. Lecture 17 Intermediate Representation (2)

Discrete Wiskunde II. Lecture 5: Shortest Paths & Spanning Trees

Foundations of Informatics: a Bridging Course

On Finding Subpaths With High Demand

CPSC 421: Tutorial #1

Software Verification

Department of Computer Science and Engineering, IIT Kanpur, INDIA. Code Optimization. Sanjeev K Aggarwal Code Optimization 1 of I

CS 6110 Lecture 21 The Fixed-Point Theorem 8 March 2013 Lecturer: Andrew Myers. 1 Complete partial orders (CPOs) 2 Least fixed points of functions

Lecture Notes: Program Analysis Correctness

CS357: CTL Model Checking (two lectures worth) David Dill

Binary Decision Diagrams. Graphs. Boolean Functions

Class 5. Review; questions. Data-flow Analysis Review. Slicing overview Problem Set 2: due 9/3/09 Problem Set 3: due 9/8/09

CS411 Notes 3 Induction and Recursion

Connectivity and tree structure in finite graphs arxiv: v5 [math.co] 1 Sep 2014

3/11/18. Final Code Generation and Code Optimization

n CS 160 or CS122 n Sets and Functions n Propositions and Predicates n Inference Rules n Proof Techniques n Program Verification n CS 161

The algorithmic analysis of hybrid system

Static Analysis of Programs: A Heap-Centric View

Worst-Case Execution Time Analysis. LS 12, TU Dortmund

ADVANCED CALCULUS - MTH433 LECTURE 4 - FINITE AND INFINITE SETS

Part V. Matchings. Matching. 19 Augmenting Paths for Matchings. 18 Bipartite Matching via Flows

Construction of Static Single-Assignment Form

COSE312: Compilers. Lecture 14 Semantic Analysis (4)

CMSC 451: Lecture 7 Greedy Algorithms for Scheduling Tuesday, Sep 19, 2017

MATH 614 Dynamical Systems and Chaos Lecture 2: Periodic points. Hyperbolicity.

Preliminaries. Graphs. E : set of edges (arcs) (Undirected) Graph : (i, j) = (j, i) (edges) V = {1, 2, 3, 4, 5}, E = {(1, 3), (3, 2), (2, 4)}

Adventures in Dataflow Analysis

Optimization and Optimal Control in Banach Spaces

Search and Lookahead. Bernhard Nebel, Julien Hué, and Stefan Wölfl. June 4/6, 2012

Notes. Relations. Introduction. Notes. Relations. Notes. Definition. Example. Slides by Christopher M. Bourke Instructor: Berthe Y.

Chapter 4: Computation tree logic

CS188: Artificial Intelligence, Fall 2009 Written 2: MDPs, RL, and Probability

Size-Change Termination and Transition Invariants

2 GRAPH AND NETWORK OPTIMIZATION. E. Amaldi Introduction to Operations Research Politecnico Milano 1

Petri nets. s 1 s 2. s 3 s 4. directed arcs.

Binary Decision Diagrams

Formal Methods in Software Engineering

Principles of AI Planning

Binary Decision Diagrams

EC 521 MATHEMATICAL METHODS FOR ECONOMICS. Lecture 1: Preliminaries

Axioms for Set Theory

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

The Turing Machine. Computability. The Church-Turing Thesis (1936) Theory Hall of Fame. Theory Hall of Fame. Undecidability

Notes on induction proofs and recursive definitions

Walker Ray Econ 204 Problem Set 3 Suggested Solutions August 6, 2015

Section Summary. Relations and Functions Properties of Relations. Combining Relations

Equational Logic. Chapter Syntax Terms and Term Algebras

Static Program Analysis using Abstract Interpretation

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

Data Structures in Java

Transcription:

CSC D70: Compiler Optimization Dataflow-2 and Loops Prof. Gennady Pekhimenko University of Toronto Winter 2018 The content of this lecture is adapted from the lectures of Todd Mowry and Phillip Gibbons

Refreshing from Last Lecture Reaching definitions Live variables 2

Framework Reaching Definitions Live Variables Domain Sets of definitions Sets of variables Direction forward: out[b] = f b (in[b]) in[b] = out[pred(b)] backward: in[b] = f b (out[b]) out[b] = in[succ(b)] Transfer function f b (x) = Gen b (x Kill b ) f b (x) = Use b (x -Def b ) Meet Operation ( ) Boundary Condition out[entry] = in[exit] = Initial interior points out[b] = in[b] = Other examples (e.g., Available expressions), defined in ALSU 9.2.6 3

Questions Correctness equations are satisfied, if the program terminates. Precision: how good is the answer? is the answer ONLY a union of all possible executions? Convergence: will the analysis terminate? or, will there always be some nodes that change? Speed: how fast is the convergence? how many times will we visit each node? 4

Foundations of Data Flow Analysis 1. Meet operator 2. Transfer functions 3. Correctness, Precision, Convergence 4. Efficiency Reference: ALSU pp. 613-631 Background: Hecht and Ullman, Kildall, Allen and Cocke[76] Marlowe & Ryder, Properties of data flow frameworks: a unified model. Rutgers tech report, Apr. 1988 5

A Unified Framework Data flow problems are defined by Domain of values: V Meet operator (V V V), initial value A set of transfer functions (V V) Usefulness of unified framework To answer questions such as correctness, precision, convergence, speed of convergence for a family of problems If meet operators and transfer functions have properties X, then we know Y about the above. Reuse code 6

Meet Operator Properties of the meet operator commutative: x y = y x x y x y idempotent: x x = x associative: x (y z) = (x y) z there is a Top element T such that x T = x Meet operator defines a partial ordering on values x y if and only if x y = x (y -> x in diagram) Transitivity: if x y and y z then x z Antisymmetry: if x y and y x then x = y Reflexitivity: x x 7

Partial Order Example: let V = {x such that x { d 1, d 2 }}, = Top and Bottom elements Top T such that: x T = x Bottom such that: x = Values and meet operator in a data flow problem define a semilattice: there exists a T, but not necessarily a. x, y are ordered: x y then x y = x (y -> x in diagram) what if x and y are not ordered? x y x, x y y, and if w x, w y, then w x y 8

One vs. All Variables/Definitions Lattice for each variable: e.g. intersection 1 0 Lattice for three variables: x 111 x 101 x 110 x 011 x 100 x 010 x 001 x 000 9

Descending Chain Definition The height of a lattice is the largest number of > relations that will fit in a descending chain. x 0 > x 1 > x 2 > Height of values in reaching definitions? Important property: finite descending chain Can an infinite lattice have a finite descending chain? yes Height n number of definitions Example: Constant Propagation/Folding To determine if a variable is a constant Data values undef,... -1, 0, 1, 2,..., not-a-constant 10

Transfer Functions Basic Properties f: V V Has an identity function There exists an f such that f (x) = x, for all x. Closed under composition if f 1, f 2 F, then f 1 f 2 F 11

Monotonicity A framework (F, V, ) is monotone if and only if x y implies f(x) f(y) i.e. a smaller or equal input to the same function will always give a smaller or equal output Equivalently, a framework (F, V, ) is monotone if and only if f(x y) f(x) f(y) i.e. merge input, then apply f is small than or equal to apply the transfer function individually and then merge the result 12

Example Reaching definitions: f(x) = Gen (x - Kill), = Definition 1: x 1 x 2, Gen (x 1 - Kill) Gen (x 2 - Kill) Definition 2: (Gen (x 1 - Kill) ) (Gen (x 2 - Kill) ) = (Gen ((x 1 x 2 ) - Kill)) Note: Monotone framework does not mean that f(x) x e.g., reaching definition for two definitions in program suppose: f x : Gen x = {d 1, d 2 } ; Kill x = {} If input(second iteration) input(first iteration) result(second iteration) result(first iteration) 13

Distributivity A framework (F, V, ) is distributive if and only if f(x y) = f(x) f(y) i.e. merge input, then apply f is equal to apply the transfer function individually then merge result Example: Constant Propagation is NOT distributive a = 2 b = 3 a = 3 b = 2 c = a + b 14

Data Flow Analysis Definition Let f 1,..., f m : F, where f i is the transfer function for node i f p = f nk f n1, where p is a path through nodes n 1,..., n k f p = identify function, if p is an empty path Ideal data flow answer: For each node n: f pi (T), for all possibly executed paths p i reaching n. if sqrt(y) >= 0 x = 0 x = 1 But determining all possibly executed paths is undecidable 15

Meet-Over-Paths (MOP) Error in the conservative direction Meet-Over-Paths (MOP): For each node n: MOP(n) = f pi (T), for all paths p i reaching n a path exists as long there is an edge in the code consider more paths than necessary MOP = Perfect-Solution Solution-to-Unexecuted-Paths MOP Perfect-Solution Potentially more constrained, solution is small hence conservative It is not safe to be > Perfect-Solution! Desirable solution: as close to MOP as possible 16

MOP Example 17

Solving Data Flow Equations Example: Reaching definitions out[entry] = {} Values = {subsets of definitions} Meet operator: in[b] = out[p], for all predecessors p of b Transfer functions: out[b] = gen b (in[b] -kill b ) Any solution satisfying equations = Fixed Point Solution (FP) Iterative algorithm initializes out[b] to {} if converges, then it computes Maximum Fixed Point (MFP): MFP is the largest of all solutions to equations Properties: FP MFP MOP Perfect-solution FP, MFP are safe in(b) MOP(b) 18

Partial Correctness of Algorithm If data flow framework is monotone, then if the algorithm converges, IN[b] MOP[b] Proof: Induction on path lengths Define IN[entry] = OUT[entry] and transfer function of entry = Identity function Base case: path of length 0 Proper initialization of IN[entry] If true for path of length k, p k = (n 1,..., n k ), then true for path of length k+1: p k+1 = (n 1,..., n k+1 ) Assume: IN[n k ] f nk-1 (f nk-2 (... f n1 (IN[entry]))) IN[n k+1 ] = OUT[n k ]... OUT[n k ] f nk (IN[n k ]) f nk-1 (f nk-2 (... f n1 (IN[entry]))) 19

Precision If data flow framework is distributive,then if the algorithm converges, IN[b] = MOP[b] a = 2 b = 3 a = 3 b = 2 c = a + b Monotone but not distributive: behaves as if there are additional paths 20

Additional Property to Guarantee Convergence Data flow framework (monotone) converges if there is a finite descending chain For each variable IN[b], OUT[b], consider the sequence of values set to each variable across iterations: if sequence for in[b] is monotonically decreasing sequence for out[b] is monotonically decreasing (out[b] initialized to T) if sequence for out[b] is monotonically decreasing sequence of in[b] is monotonically decreasing 21

Speed of Convergence Speed of convergence depends on order of node visits Reverse direction for backward flow problems 22

Reverse Postorder Step 1: depth-first post order main() { count = 1; Visit(root); } Visit(n) { for each successor s that has not been visited Visit(s); PostOrder(n) = count; count = count+1; } Step 2: reverse order For each node i rpostorder = NumNodes - PostOrder(i) 23

Depth-First Iterative Algorithm (forward) input: control flow graph CFG = (N, E, Entry, Exit) /* Initialize */ out[entry] = init_value For all nodes i out[i] = T Change = True /* iterate */ While Change { Change = False For each node i in rpostorder { in[i] = (out[p]), for all predecessors p of i oldout = out[i] out[i] = f i (in[i]) if oldout out[i] Change = True } } 24

Speed of Convergence If cycles do not add information information can flow in one pass down a series of nodes of increasing order number: e.g., 1 -> 4 -> 5 -> 7 -> 2 -> 4... passes determined by number of back edges in the path essentially the nesting depth of the graph Number of iterations = number of back edges in any acyclic path + 2 (2 are necessary even if there are no cycles) What is the depth? corresponds to depth of intervals for reducible graphs in real programs: average of 2.75 25

A Check List for Data Flow Problems Semi-lattice set of values meet operator top, bottom finite descending chain? Transfer functions function of each basic block monotone distributive? Algorithm initialization step (entry/exit, other nodes) visit order: rpostorder depth of the graph 26

Conclusions Dataflow analysis examples Reaching definitions Live variables Dataflow formation definition Meet operator Transfer functions Correctness, Precision, Convergence Efficiency 27

CSC D70: Compiler Optimization Loops Prof. Gennady Pekhimenko University of Toronto Winter 2018 The content of this lecture is adapted from the lectures of Todd Mowry and Phillip Gibbons

What is a Loop? Goals: Define a loop in graph-theoretic terms (control flow graph) Not sensitive to input syntax A uniform treatment for all loops: DO, while, goto s Not every cycle is a loop from an optimization perspective start a b Is this a loop? Intuitive properties of a loop single entry point edges must form at least a cycle c d Is this a loop? 29

Formal Definitions Dominators Node d dominates node n in a graph (d dom n) if every path from the start node to n goes through d 1 3 2 4 5 6 7 8 Dominators can be organized as a tree a ->b in the dominator tree iff a immediately dominates b 30

Dominance 1 1 2 5 9 2 4 5 9 12 13 3 6 7 10 11 3 6 7 8 10 11 4 8 12 13 CFG D-Tree x strictly dominates w (x sdom w) iff x dom w AND x w 31

Natural Loops Definitions Single entry-point: header a header dominates all nodes in the loop A back edge is an arc whose head dominates its tail (tail -> head) a back edge must be a part of at least one loop The natural loop of a back edge is the smallest set of nodes that includes the head and tail of the back edge, and has no predecessors outside the set, except for the predecessors of the header. 32

Natural Loops - Example 33

Algorithm to Find Natural Loops Find the dominator relations in a flow graph Identify the back edges Find the natural loop associated with the back edge 34

1. Finding Dominators Definition Node d dominates node n in a graph (d dom n) if every path from the start node to n goes through d Formulated as MOP problem: node d lies on all possible paths reaching node n d dom n Direction: Values: Meet operator: Top: Bottom: Boundary condition: start/entry node = Initialization for internal nodes Finite descending chain? Transfer function: Speed: With reverse postorder, most flow graphs (reducible flow graphs) converge in 1 pass 35

Example 36

2. Finding Back Edges Depth-first spanning tree Edges traversed in a depth-first search of the flow graph form a depth-first spanning tree 1 3 2 4 5 6 7 8 Categorizing edges in graph Advancing (A) edges: from ancestor to proper descendant Cross (C) edges: from right to left Retreating (R) edges: from descendant to ancestor (not necessarily proper) 37

Back Edges Definition Back edge: t->h, h dominates t Relationships between graph edges and back edges Algorithm Perform a depth first search For each retreating edge t->h, check if h is in t s dominator list Most programs (all structured code, and most GOTO programs) have reducible flow graphs retreating edges = back edges 38

Examples 39

3. Constructing Natural Loops The natural loop of a back edge is the smallest set of nodes that includes the head and tail of the back edge, and has no predecessors outside the set, except for the predecessors of the header. Algorithm delete h from the flow graph find those nodes that can reach t (those nodes plus h form the natural loop of t -> h) 1 2 3 4 5 6 7 8 40

Inner Loops If two loops do not have the same header: they are either disjoint, or one is entirely contained (nested within) the other inner loop: one that contains no other loop. If two loops share the same header: Hard to tell which is the inner loop Combine as one a b c 41

Preheader Optimizations often require code to be executed once before the loop Create a preheader basic block for every loop preheader header header rest of loop rest of loop 42

Finding Loops: Summary Define loops in graph theoretic terms Definitions and algorithms for: Dominators Back edges Natural loops 43

CSC D70: Compiler Optimization Dataflow-2 and Loops Prof. Gennady Pekhimenko University of Toronto Winter 2018 The content of this lecture is adapted from the lectures of Todd Mowry and Phillip Gibbons

Backup Slides 45

Dominance Frontier 1 1 2 5 9 2 4 5 9 12 13 3 6 7 10 11 3 6 7 8 10 11 4 8 13 12 The Dominance Frontier of a node x = { w x dom pred(w) AND!(x sdom w)} CFG D-Tree x strictly dominates w (x sdom w) iff x dom w AND x w 46

Dominance Frontier and Path Convergence 1 1 2 5 9 2 5 9 3 6 7 10 11 3 6 7 10 11 4 8 12 4 8 12 13 13 47