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

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

Computer Science 160 Translation of Programming Languages

Control-Flow Analysis

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

CS 432 Fall Mike Lam, Professor. Code Generation

Compiler Optimisation

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

Introduction to Machine-Independent Optimizations - 4

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

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

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

CS415 Compilers. Instruction Scheduling and Lexical Analysis

Goals of Program Optimization (1 of 2)

Compiler Construction 2009/2010 SSA Static Single Assignment Form

Intermediate Representations. Reading & Topics. Intermediate Representations CS2210

Control Flow Analysis & Def-Use. Hwansoo Han

Intermediate representation

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

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

Global Register Allocation via Graph Coloring

CS5363 Final Review. cs5363 1

Instruction Scheduling

CS415 Compilers Register Allocation. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Lecture 3 Local Optimizations, Intro to SSA

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

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

Intermediate Representation (IR)

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

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

Intermediate Representations Part II

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

Why Global Dataflow Analysis?

CS 406/534 Compiler Construction Putting It All Together

Lecture 6 Foundations of Data Flow Analysis

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

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

Control flow and loop detection. TDT4205 Lecture 29

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

CSC D70: Compiler Optimization LICM: Loop Invariant Code Motion

CS153: Compilers Lecture 23: Static Single Assignment Form

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

CS415 Compilers. Intermediate Represeation & Code Generation

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

More Dataflow Analysis

Control Flow Analysis

CS 406/534 Compiler Construction Instruction Scheduling

CONTROL FLOW ANALYSIS. The slides adapted from Vikram Adve

Lecture 6 Foundations of Data Flow Analysis

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

CSE 401/M501 Compilers

Introduction to Optimization Local Value Numbering

Programming Language Processor Theory

CS415 Compilers. Lexical Analysis

Lecture 23 CIS 341: COMPILERS

Homework Assignment #1 Sample Solutions

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

CS553 Lecture Generalizing Data-flow Analysis 3

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

Loop Invariant Code Motion. Background: ud- and du-chains. Upward Exposed Uses. Identifying Loop Invariant Code. Last Time Control flow analysis

Induction Variable Identification (cont)

Lecture Compiler Middle-End

Introduction to Optimization. CS434 Compiler Construction Joel Jones Department of Computer Science University of Alabama

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412

opt. front end produce an intermediate representation optimizer transforms the code in IR form into an back end transforms the code in IR form into

Instruction Selection and Scheduling

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

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

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

Instruction Selection, II Tree-pattern matching

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

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

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

Flow Graph Theory. Depth-First Ordering Efficiency of Iterative Algorithms Reducible Flow Graphs

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Towards an SSA based compiler back-end: some interesting properties of SSA and its extensions

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

Combining Optimizations: Sparse Conditional Constant Propagation

Introduction to Optimization, Instruction Selection and Scheduling, and Register Allocation

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

Intermediate Representations

Contents of Lecture 2

Instruction Scheduling Beyond Basic Blocks Extended Basic Blocks, Superblock Cloning, & Traces, with a quick introduction to Dominators.

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

Intermediate Representations

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

6. Intermediate Representation

CS243 Homework 1. Winter Due: January 23, 2019 at 4:30 pm

Control Flow Analysis

Data Flow Analysis. Daqing Hou

Advanced Compiler Construction

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

Graphs. A graph is a data structure consisting of nodes (or vertices) and edges. An edge is a connection between two nodes

CS 6110 S14 Lecture 38 Abstract Interpretation 30 April 2014

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

A Sparse Algorithm for Predicated Global Value Numbering

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

CS2210: Compiler Construction. Compiler Optimization

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

Intermediate Representations

CSC 467 Lecture 13-14: Semantic Analysis

Transcription:

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

Compilers "Back end" Source code Tokens Syntax tree Machine code char data[20]; int main() { float x = 42.0; return 7; } 7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00... Lexing Parsing Code Generation & Optimization "Front end" Current focus

Optimization int a; a = 0; while (a < 10) { a = a + 1; } loadi 0 => r1 loadi 10 => r2 l1: cmp_lt r1, r2 => r4 cbr r4 => l2, l3 l2: addi r1, 1 => r1 jump l1 l3: storeai r1 => [bp-4] loadi 0 => r1 storeai r1 => [bp-4] l1: loadai [bp-4] => r2 loadi 10 => r3 cmp_lt r2, r3 => r4 cbr r4 => l2, l3 l2: loadai [bp-4] => r5 loadi 1 => r6 add r5, r6 => r7 storeai r7 => [bp-4] jump l1 l3: loadi 10 => r1 storeai r1 => [bp-4]

Optimization is Hard Problem: it's hard to reason about all possible executions Preconditions and inputs may differ Optimizations should be correct and efficient in all cases Consider this code: int *p; cin >> p; *p = 42; Optimization tradeoff: investment vs. payoff "Better than naïve" is fairly easy "Optimal" is impossible Real world: somewhere in between Better speedups with more static analysis Usually worth the added compile time Also: linear IRs (e.g., ILOC) don't explicitly expose control flow This makes analysis and optimization difficult

Control-Flow Graphs Basic blocks "Maximal-length sequence of branch-free code" "Atomic" sequences (instructions that always execute together) Control-flow graph (CFG) Nodes/vertices for basic blocks Edges for control transfer Branch/jump instructions (explicit) or fallthrough (implicit) p is a predecessor of q if there is a path from p to q p is an immediate predecessor if there is an edge directly from p to q q is a successor of p if there is a path from p to q a is an immediate successor if there is an edge directly from p to q

Control-Flow Graphs Conversion: linear IR to CFG Find leaders (initial instruction of a basic block) and build blocks Every call or jump target is a leader Add edges between blocks based on branches and fallthrough Complicated by indirect jumps (none in our ILOC!) foo: loadai [bp-4] => r1 cbr r1 => l1, l2 l1: loadi 5 => r2 jump l3 l2: loadi 10 => r2 l3: storeai r2 => [bp-4] loadi 5 => r2 loadai [bp-4] => r1 cbr r1 => l1, l2 storeai r2 => [bp-4] loadi 10 => r2

Static CFG Analysis Single block analysis is easy, and trees are too General CFGs are harder Which branch of a conditional will execute? How many times will a loop execute? How do we handle this? One method: iterative data-flow analysis Simulate all possible paths through a region of code Meet-over-all-paths conservative solution Meet operator combines information across paths

Semilattices In general, a semilattice is a set of values L, special values ( top) and ( bottom), and a meet operator ^ such that a b iff a ^ b = b a > b iff a b and a b a ^ = for all a L a ^ = a for all a L Partial ordering Monotonic Figure 9.22 from Dragon book: semilattice of definitions using U (set union) as the meet operation

Constant propagation For sparse simple constant propagation (SSCP), the lattice is very shallow c i ^ = for all ci c i ^ = a for all ci c i ^ c j = c i if c i = c j c i ^ c j = if ci c j Basically: each SSA value is either a known constant or it is a variable Dataflow analysis propagates this information

Data-Flow Analysis Define properties of interest for basic blocks Usually sets of blocks, variables, definitions, etc. Define a formula for how those properties change within a block F(B) is based on F(A) where A is a predecessor or successor of B This is basically the meet operator for a particular problem Specify initial information for all blocks Entry/exit blocks usually have different values Run an iterative update algorithm to propagate changes Keep running until the properties converge for all basic blocks Key concept: finite descending chain property Properties must be monotonically increasing or decreasing Otherwise, termination is not guaranteed

Data-Flow Analysis This kind of algorithm is called a fixed-point algorithm It runs until it converges to a fixed point Forward vs. backward data-flow analysis Forward: along graph edges (based on predecessors) Backward: reverse of forward (based on successors) Types of data-flow analysis Constant propagation Dominance Liveness Available expressions Reaching definitions Anticipable expressions

Dominance Block A dominates block B if A is on every path from the entry to B Block A immediately dominates block B if there are no blocks between them Block B postdominates block A if B is on every path from A to an exit Simple dataflow analysis formulation preds(b) is the set of blocks that are predecessors of block b Dom(b) is the set of blocks that dominate block b intersection of Dom for all immediate predecessors Initial conditions: Dom(entry ) = {entry } b entry, Dom(b) = {all blocks } Updates : Dom(b) = {b } p preds(b) Dom( p)

Liveness Variable v is live at point p if there is a path from p to a use of v with no intervening assignment to v Useful for finding uninitialized variables (live at function entry) Useful for optimization (remove unused assignments) Useful for register allocation (keep live vars in registers) Initial information: UEVar and VarKill UEVar(B): variables used in B before any redefinition in B ( upwards exposed variables) VarKill(B): variables that are defined in B Textbook notation note: X Y = X - Y Initial conditions: b, LiveOut (b) = Updates : LiveOut (b) = UEVar (s) (LiveOut (s) VarKill(s)) s succs (b)

Liveness example b, LiveOut (b) = LiveOut (b) = UEVar(s) (LiveOut (s) VarKill(s)) s succs (b)

Alternative definition Define LiveIn as well as LiveOut Two formulas for each basic block Makes things a bit simpler to reason about Separates change within block from change between blocks b, LiveOut (b) = LiveIn(b) = UEVar (b) (LiveOut (b) VarKill(b)) LiveOut (b) = LiveIn(s) s succs(b)

Block orderings Forwards dataflow analyses converge faster with reverse postorder processing of CFG blocks Visit as many of a block s predecessors as possible before visiting that block Strict reversal of normal postorder traversal Similar to concept of topological sorting on DAGs NOT EQUIVALENT to preorder traversal! Backwards analyses should use reverse postorder on reverse CFG Depth-first search: A, B, D, B, A, C, A (left first) A, C, D, C, A, B, A (right first) Valid postorderings: D, B, C, A (left first) D, C, B, A (right first) Valid preorderings: A, B, D, C (left first) A, C, D, B (right first) Valid reverse postorderings: A, C, B, D (left first) A, B, C, D (right first)

Summary Dom(entry ) = {entry } b entry, Dom(b) = {all blocks } Dominance Dom(b) = {b } Dom( p) p preds(b) b, LiveOut (b) = LiveOut (b) = UEVar(s) (LiveOut (s) VarKill(s)) s succs(b) Liveness (EAC version) b, LiveOut (b) = LiveIn(b) = UEVar(b) ( LiveOut (b) VarKill(b)) LiveOut (b) = s succs (b) LiveIn(s) Liveness (Dragon version)