Introduction to Data-flow analysis



Similar documents
CIS570 Lecture 4 Introduction to Data-flow Analysis 3

Lecture 2 Introduction to Data Flow Analysis

CSE 504: Compiler Design. Data Flow Analysis

Why have SSA? Birth Points (cont) Birth Points (a notion due to Tarjan)

Optimizations. Optimization Safety. Optimization Safety. Control Flow Graphs. Code transformations to improve program

Absolute Value Equations and Inequalities

Address Taken FIAlias, which is equivalent to Steensgaard. CS553 Lecture Alias Analysis II 2

EECS 583 Class 11 Instruction Scheduling Software Pipelining Intro

Algorithm Design and Analysis

Software Pipelining - Modulo Scheduling

Administration. Instruction scheduling. Modern processors. Examples. Simplified architecture model. CS 412 Introduction to Compilers

Scanning and parsing. Topics. Announcements Pick a partner by Monday Makeup lecture will be on Monday August 29th at 3pm

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Making Dynamic Memory Allocation Static To Support WCET Analyses

The PageRank Citation Ranking: Bring Order to the Web

Dynamic programming. Doctoral course Optimization on graphs - Lecture 4.1. Giovanni Righini. January 17 th, 2013

Obfuscation: know your enemy

Chapter 2: Linear Equations and Inequalities Lecture notes Math 1010

Introduction to Scheduling Theory

Software Testing. Jeffrey Carver University of Alabama. April 7, 2016

Static Analysis. Find the Bug! : Analysis of Software Artifacts. Jonathan Aldrich. disable interrupts. ERROR: returning with interrupts disabled

SIMS 255 Foundations of Software Design. Complexity and NP-completeness

Dynamic programming formulation

2) Write in detail the issues in the design of code generator.

FPGA area allocation for parallel C applications

Review; questions Discussion of Semester Project Arbitrary interprocedural control flow Assign (see Schedule for links)

Static Taint-Analysis on Binary Executables

Measuring the Performance of an Agent

Optimizing compilers. CS Modern Compilers: Theory and Practise. Optimization. Compiler structure. Overview of different optimizations

Inequalities - Solve and Graph Inequalities

Transparent Monitoring of a Process Self in a Virtual Environment

IKOS: A Framework for Static Analysis based on Abstract Interpretation (Tool Paper)

Small Maximal Independent Sets and Faster Exact Graph Coloring

How to Write a Checker in 24 Hours

Instruction scheduling

Hagit Attiya and Eshcar Hillel. Computer Science Department Technion

On the effect of forwarding table size on SDN network utilization

Basic Parsing Algorithms Chart Parsing

CODE FACTORING IN GCC ON DIFFERENT INTERMEDIATE LANGUAGES

Factoring Polynomials: Factoring by Grouping

Analysis of Algorithms, I

COLLEGE ALGEBRA 10 TH EDITION LIAL HORNSBY SCHNEIDER 1.1-1

Network (Tree) Topology Inference Based on Prüfer Sequence

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. #-approximation algorithm.

IE 680 Special Topics in Production Systems: Networks, Routing and Logistics*

B4: Experience with a Globally-Deployed Software Defined WAN TO APPEAR IN SIGCOMM 13

How to make the computer understand? Lecture 15: Putting it all together. Example (Output assembly code) Example (input program) Anatomy of a Computer

Chapter Load Balancing. Approximation Algorithms. Load Balancing. Load Balancing on 2 Machines. Load Balancing: Greedy Scheduling

Contents. System Development Models and Methods. Design Abstraction and Views. Synthesis. Control/Data-Flow Models. System Synthesis Models

Semantic Analysis: Types and Type Checking

How To Trace

Linear Scan Register Allocation on SSA Form

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

CSE 4351/5351 Notes 7: Task Scheduling & Load Balancing

Distributed Computing over Communication Networks: Topology. (with an excursion to P2P)

Recursion vs. Iteration Eliminating Recursion

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. !-approximation algorithm.

Seminar. Path planning using Voronoi diagrams and B-Splines. Stefano Martina

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Introduction to Cloud Computing

Integrated System Modeling for Handling Big Data in Electric Utility Systems

Midterm Practice Problems

EMSCRIPTEN - COMPILING LLVM BITCODE TO JAVASCRIPT (?!)

Lecture Notes on Static Analysis

A Fast Algorithm For Finding Hamilton Cycles

CS473 - Algorithms I

SYSM 6304: Risk and Decision Analysis Lecture 5: Methods of Risk Analysis

Static Analysis of Virtualization- Obfuscated Binaries

Guessing Game: NP-Complete?

How To Get A Computer Science Degree At Appalachian State

A Fresh Look at Optimizing Array Bound Checking

HVM TP : A Time Predictable and Portable Java Virtual Machine for Hard Real-Time Embedded Systems JTRES 2014

HAVING a good mental model of how a

Outline. NP-completeness. When is a problem easy? When is a problem hard? Today. Euler Circuits

On the Unique Games Conjecture

LaTTe: An Open-Source Java VM Just-in Compiler

Transcription:

Introduction to Data-flow analysis Last Time LULESH intro Typed, 3-address code Basic blocks and control flow graphs LLVM Pass architecture Data dependencies, DU chains, and SSA Today CFG and SSA example Liveness analysis Register allocation CS553 Lecture Introduction to Data-flow Analysis 1

C Code for Review Example int main() { int x,y,z; } x = 0; y=1, z = 2; while (x<42) { } if (x>0) { } y++; x++; z++; continue; y = z*y; return 0; CS553 Lecture Introduction to Data-flow Analysis 2

Control Flow Graph Review while.cond: %0 = load i32* %x, align 4 %cmp = icmp slt i32 %0, 42 br i1 %cmp, label %while.body, label %while.end while.body: %1 = load i32* %x, align 4 %cmp1 = icmp sgt i32 %1, 0 br i1 %cmp1, label %if.then, label %if.end if.end:... store i32 %mul, i32* %y, align 4 br label %while.cond while.end: ret i32 0 if.then:... br label %while.cond CS553 Lecture Introduction to Data-flow Analysis 3

Beginning of Same Example After opt mem2reg while.cond: %y.0 = phi i32 [1,%entry], [%inc,%if.then], [%mul, %if.end] %x.0 = phi i32 [0,%entry ], [%inc2,%if.then ], [%x.0, %if.end] %z.0 = phi i32 [2,%entry ], [%inc3,%if.then ], [%z.0, %if.end] %cmp = icmp slt i32 %x.0, 42 %cmp = icmp slt i32 %0, 42 br i1 %cmp, label %while.body, label %while.end while.body: %cmp1 = icmp sgt i32 %x.0, 0 ; previously ; %1 = load i32* %x, align 4 ; %cmp1 = icmp sgt i32 %1, 0 br i1 %cmp1, label %if.then, label %if.end CS553 Lecture Introduction to Data-flow Analysis 4

Data-flow Analysis Idea Data-flow analysis derives information about the dynamic behavior of a program by only examining the static code Example How many registers do we need for the program on the right? Easy bound: the number of variables used (3) Better answer is found by considering the dynamic requirements of the program 1 a := 0 2 L1: b := a + 1 3 c := c + b 4 a := b * 2 5 if a < 9 goto L1 6 return c CS553 Lecture Introduction to Data-flow Analysis 5

Liveness Analysis Definition A variable is live at a particular point in the program if its value at that point will be used in the future (dead, otherwise). To compute liveness at a given point, we need to look into the future Motivation: Register Allocation A program contains an unbounded number of variables Must execute on a machine with a bounded number of registers Two variables can use the same register if they are never in use at the same time (i.e, never simultaneously live). Register allocation uses liveness information CS553 Lecture Introduction to Data-flow Analysis 6

Control Flow Graphs (CFGs) Definition A CFG is a graph whose nodes represent program statements and whose directed edges represent control flow Example 1 a = 0 1 a := 0 2 L1: b := a + 1 3 c := c + b 4 a := b * 2 5 if a < 9 goto L1 6 return c 2 3 b = a + 1 c = c + b 4 a = b * 2 5 a<9 6 return c No Yes CS553 Lecture Introduction to Data-flow Analysis 7

Terminology Flow Graph Terms A CFG node has out-edges that lead to successor nodes and in-edges that come from predecessor nodes pred[n] is the set of all predecessors of node n succ[n] is the set of all successors of node n 1 a = 0 Examples Out-edges of node 5: succ[5] = {2,6} pred[5] = {4} pred[2] = {1,5} (5 6) and (5 2) 2 3 b = a + 1 c = c + b 4 a = b * 2 5 a<9 6 return c No Yes CS553 Lecture Introduction to Data-flow Analysis 8

Liveness by Example What is the live range of b? Variable b is read in statement 4, so b is live on the (3 4) edge Since statement 3 does not assign into b, b is also live on the (2 3) edge Statement 2 assigns b, so any value of b on the (1 2) and (5 2) edges are not needed, so b is dead along these edges 1 2 3 a = 0 b = a + 1 c = c + b 4 a = b * 2 5 a<9 b s live range is (2 3 4) 6 return c No Yes CS553 Lecture Introduction to Data-flow Analysis 9

Liveness by Example (cont) Live range of a a is live from (1 2) and again from (4 5 2) a is dead from (2 3 4) 1 2 a = 0 b = a + 1 Live range of b b is live from (2 3 4) Live range of c c is live from (entry 1 2 3 4 5 2, 5 6) 6 return c 3 5 c = c + b 4 a = b * 2 a<9 Variables a and b are never simultaneously live, so they can share a register No Yes CS553 Lecture Introduction to Data-flow Analysis 10

Uses and Defs Def (or definition) An assignment of a value to a variable def_node[v] = set of CFG nodes that define variable v def[n] = set of variables that are defined at node n a = 0 Use A read of a variable s value use_node[v] = set of CFG nodes that use variable v use[n] = set of variables that are used at node n a < 9? v live def_node[v] More precise definition of liveness A variable v is live on a CFG edge if use_node[v] (1) a directed path from that edge to a use of v (node in use_node[v]), and (2) that path does not go through any def of v (no nodes in def_node[v]) CS553 Lecture Introduction to Data-flow Analysis 11

The Flow of Liveness Data-flow Liveness of variables is a property that flows through the edges of the CFG Direction of Flow Liveness flows backwards through the CFG, because the behavior at future nodes determines liveness at a given node 1 a := 0 2 3 b := a + 1 c := c + b Consider a Consider b Later, we ll see other properties that flow forward 4 5 6 return c a := b * 2 a < 9? No Yes CS553 Lecture Introduction to Data-flow Analysis 12

Liveness at Nodes We have liveness on edges How do we talk about liveness at nodes? a = 0 edges program points just before computation just after computation Two More Definitions A variable is live-out at a node if it is live on any of that node s out-edges n live-out out-edges A variable is live-in at a node if it is live on any of that node s in-edges n live-in in-edges CS553 Lecture Introduction to Data-flow Analysis 13

Computing Liveness Rules for computing liveness (1) Generate liveness: If a variable is in use[n], it is live-in at node n n live-in use (2) Push liveness across edges: If a variable is live-in at a node n then it is live-out at all nodes in pred[n] live-out live-out n live-in live-out pred[n] (3) Push liveness across nodes: If a variable is live-out at node n and not in def[n] then the variable is also live-in at n Data-flow equations (1) in[n] = use[n] (out[n] def[n]) (3) n live-in live-out out[n] = in[s] s succ[n] (2) CS553 Lecture Introduction to Data-flow Analysis 14

Solving the Data-flow Equations Algorithm for each node n in CFG repeat in[n] = ; out[n] = for each node n in CFG in [n] = in[n] out [n] = out[n] in[n] = use[n] (out[n] def[n]) out[n] = in[s] s succ[n] until in [n]=in[n] and out [n]=out[n] for all n initialize solutions save current results solve data-flow equations test for convergence This is iterative data-flow analysis (for liveness analysis) CS553 Lecture Introduction to Data-flow Analysis 15

Example node # 1 a 2 a b 3 bc c 5 a 1st 2nd 3rd 4th 5th 6th 7th use def in out in out in out in out in out in out in out 4 b a 6 c a bc b a a c c a a bc bc b b a a ac c a ac bc bc b b a ac ac c ac ac bc bc b b ac ac ac c ac ac bc bc b bc ac ac ac c c c ac ac bc bc bc bc ac ac ac c c ac ac bc bc bc bc ac ac ac 1 a := 0 2 b := a + 1 3 c := c + b 4 a := b * 2 Data-flow Equations for Liveness in[n] = use[n] (out[n] def[n]) out[n] = in[s] s succ[n] No 6 return c 5 a < 9? Yes CS553 Lecture Introduction to Data-flow Analysis 16

Do Liveness Example from Old Midterm CS553 Lecture Introduction to Data-flow Analysis 17

Register Allocation in LLVM Have a reading assignment for Monday about this. What register allocation does Takes code out of SSA. Replaces virtual registers with physical registers. As of LLVM 3.0, Basic and Greedy Used to be linear scan but their were efficiency and maintenance issues Basic algorithm orders live ranges by size Allocates large live ranges first Let s talk about live ranges and the general register allocation problem CS553 Lecture Introduction to Data-flow Analysis 18

Register Allocation Problem Assign an unbounded number of symbolic registers to a fixed number of architectural registers Simultaneously live data must be assigned to different architectural registers Goal Minimize overhead of accessing data Memory operations (loads & stores) Register moves CS553 Lecture Register Allocation I 19

Scope of Register Allocation Expression Local Loop Global (over the control-flow graph for a function) Interprocedural CS553 Lecture Register Allocation I 20

Granularity of Allocation What is allocated to registers? Variables Webs (i.e., du-chains with common uses) Values (i.e., definitions; same as variables with SSA) b 1 s 1 : x := 5 b 2 s 2 : y := x b 3 s 3 : x := y+1 s 4 :... x... s 5 : x := 3 Variables: 2 (x & y) Webs: 3 (s 1 s 2,s 4 ; s 2 s 3 ; s 3,s 5 s 6 ) Values: 4 (s 1, s 2, s 3, s 5, φ (s 3,s 5 )) b 4 s 6 :... x... What are the tradeoffs? Each allocation unit is given a symbolic register name (e.g., t1, t2, etc.) CS553 Lecture Register Allocation I 21

Global Register Allocation by Graph Coloring Idea [Cocke 71], First allocator [Chaitin 81] 1. Construct interference graph G=(N,E) Represents notion of simultaneously live Nodes are units of allocation (e.g., variables, live ranges, values) edge (n 1,n 2 ) E if n 1 and n 2 are simultaneously live Symmetric (not reflexive nor transitive) 2. Find k-coloring of G (for k registers) Adjacent nodes can t have same color 3. Allocate the same register to all allocation units of the same color Adjacent nodes must be allocated to distinct registers t2 t1 t3 CS553 Lecture Register Allocation I 22

Granularity of Allocation (Draw 3 Interference Graphs) s 1 : x := 5 b 2 s 2 : y := x b 3 s 3 : x := y+1 b 4 b 1 s 6 :... x... s 4 :... x... s 5 : x := 3 Variables: 3 (x & y) Webs: 3 (s 1 s 2,s 4 ; s 2 s 3 ; s 3,s 5 s 6 ) Values: 4 (s 1, s 2, s 3, s 5, φ (s 3,s 5 )) CS553 Lecture Register Allocation I 23

Next Time Reading Slide set and article about register allocation in LLVM, see progress page Lecture Improvements to graph coloring register allocators Register allocation across procedure calls Assignments PA1 is due Monday!! CS553 Lecture Introduction to Data-flow Analysis 24