Live variables and copy propagation

Slides:



Advertisements
Similar presentations
Data-Flow Analysis II CS 671 March 13, CS 671 – Spring Data-Flow Analysis Gather conservative, approximate information about what a program.
Advertisements

Lecture 11: Code Optimization CS 540 George Mason University.
Data Flow Analysis. Goal: make assertions about the data usage in a program Use these assertions to determine if and when optimizations are legal Local:
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Course Outline Traditional Static Program Analysis –Theory Compiler Optimizations; Control Flow Graphs Data-flow Analysis – today’s class –Classic analyses.
Control-Flow Graphs & Dataflow Analysis CS153: Compilers Greg Morrisett.
Data-Flow Analysis Framework Domain – What kind of solution is the analysis looking for? Ex. Variables have not yet been defined – Algorithm assigns a.
CS412/413 Introduction to Compilers Radu Rugina Lecture 37: DU Chains and SSA Form 29 Apr 02.
Components of representation Control dependencies: sequencing of operations –evaluation of if & then –side-effects of statements occur in right order Data.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Dataflow Analysis Introduction Guo, Yao Part of the slides are adapted from.
School of EECS, Peking University “Advanced Compiler Techniques” (Fall 2011) Partial Redundancy Elimination Guo, Yao.
Partial Redundancy Elimination. Partial-Redundancy Elimination Minimize the number of expression evaluations By moving around the places where an expression.
1 Data flow analysis Goal : collect information about how a procedure manipulates its data This information is used in various optimizations For example,
Foundations of Data-Flow Analysis. Basic Questions Under what circumstances is the iterative algorithm used in the data-flow analysis correct? How precise.
1 CS 201 Compiler Construction Lecture 5 Code Optimizations: Copy Propagation & Elimination.
More Dataflow Analysis CS153: Compilers Greg Morrisett.
1 Data flow analysis Goal : –collect information about how a procedure manipulates its data This information is used in various optimizations –For example,
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Data Flow Analysis Compiler Design Nov. 3, 2005.
From last time: reaching definitions For each use of a variable, determine what assignments could have set the value being read from the variable Information.
Constraints for reaching definitions Using may-point-to information: out = in [ { x ! s | x 2 may-point-to(p) } Using must-point-to aswell: out = in –
1 CS 201 Compiler Construction Lecture 3 Data Flow Analysis.
Data Flow Analysis Compiler Design October 5, 2004 These slides live on the Web. I obtained them from Jeff Foster and he said that he obtained.
CS 412/413 Spring 2007Introduction to Compilers1 Lecture 29: Control Flow Analysis 9 Apr 07 CS412/413 Introduction to Compilers Tim Teitelbaum.
Constraints for reaching definitions Using may-point-to information: out = in [ { x ! s | x 2 may-point-to(p) } Using must-point-to aswell: out = in –
1 Copy Propagation What does it mean? – Given an assignment x = y, replace later uses of x with uses of y, provided there are no intervening assignments.
Machine-Independent Optimizations Ⅰ CS308 Compiler Theory1.
From last time: reaching definitions For each use of a variable, determine what assignments could have set the value being read from the variable Information.
Global optimization. Data flow analysis To generate better code, need to examine definitions and uses of variables beyond basic blocks. With use- definition.
Direction of analysis Although constraints are not directional, flow functions are All flow functions we have seen so far are in the forward direction.
Prof. Bodik CS 164 Lecture 16, Fall Global Optimization Lecture 16.
1 CS 201 Compiler Construction Data Flow Analysis.
Data Flow Analysis Compiler Baojian Hua
Dataflow Analysis Topic today Data flow analysis: Section 3 of Representation and Analysis Paper (Section 3) NOTE we finished through slide 30 on Friday.
MIT Introduction to Program Analysis and Optimization Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology.
Λλ Fernando Magno Quintão Pereira P ROGRAMMING L ANGUAGES L ABORATORY Universidade Federal de Minas Gerais - Department of Computer Science P ROGRAM A.
Jeffrey D. Ullman Stanford University. 2 boolean x = true; while (x) {... // no change to x }  Doesn’t terminate.  Proof: only assignment to x is at.
1 Data Flow Analysis Data flow analysis is used to collect information about the flow of data values across basic blocks. Dominator analysis collected.
Dead Code Elimination This lecture presents the algorithm Dead from EaC2e, Chapter 10. That algorithm derives, in turn, from Rob Shillner’s unpublished.
CS412/413 Introduction to Compilers Radu Rugina Lecture 18: Control Flow Graphs 29 Feb 02.
1 Control Flow Graphs. 2 Optimizations Code transformations to improve program –Mainly: improve execution time –Also: reduce program size Can be done.
1 CS 201 Compiler Construction Lecture 2 Control Flow Analysis.
Data Flow Analysis II AModel Checking and Abstract Interpretation Feb. 2, 2011.
Optimization Simone Campanoni
Dataflow Analysis CS What I s Dataflow Analysis? Static analysis reasoning about flow of data in program Different kinds of data: constants, variables,
Code Optimization Data Flow Analysis. Data Flow Analysis (DFA)  General framework  Can be used for various optimization goals  Some terms  Basic block.
CS 412/413 Spring 2005Introduction to Compilers1 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 30: Loop Optimizations and Pointer Analysis.
DFA foundations Simone Campanoni
Simone Campanoni SSA Simone Campanoni
Simone Campanoni CFA Simone Campanoni
Data Flow Analysis Suman Jana
Simone Campanoni Dependences Simone Campanoni
Iterative Dataflow Problems
Dataflow analysis.
Simone Campanoni DFA foundations Simone Campanoni
Fall Compiler Principles Lecture 8: Loop Optimizations
Topic 10: Dataflow Analysis
University Of Virginia
1. Reaching Definitions Definition d of variable v: a statement d that assigns a value to v. Use of variable v: reference to value of v in an expression.
Dataflow analysis.
Static Single Assignment Form (SSA)
Fall Compiler Principles Lecture 10: Global Optimizations
Data Flow Analysis Compiler Design
Lecture 20: Dataflow Analysis Frameworks 11 Mar 02
Topic-4a Dataflow Analysis 2019/2/22 \course\cpeg421-08s\Topic4-a.ppt.
Final Code Generation and Code Optimization
Static Single Assignment
Dataflow Analysis, cont.
COMPILERS Liveness Analysis
CSE P 501 – Compilers SSA Hal Perkins Autumn /31/2019
Presentation transcript:

Live variables and copy propagation

Control Flow Graphs Control Flow Graph (CFG) = graph representation of computation and control flow in the program framework to statically analyze program control-flow In a CFG: Nodes are basic blocks; they represent computation Edges characterize control flow between basic blocks Can build the CFG representation either from the high IR or from the low IR

Build CFG from High IR while (c) { x = y + 1; x = y+1 y = 2 * z; if (d) z = x if (c) x = y+z z = 1 while (c) { x = y + 1; y = 2 * z; if (d) x = y+z; z = 1; } z = x;

Build CFG from Low IR label L1 fjump c L2 label L1 fjump c L2 x = y + 1; y = 2 * z; fjump d L3 x = y+z; label L3 z = 1; jump L1 label L2 z = x; label L1 fjump c L2 x = y + 1; y = 2 * z; fjump d L3 x = y+z; label L3 z = 1; jump L1 label L2 z = x;

Using CFGs Next: use CFG representation to statically extract information about the program Reason at compile-time About the run-time values of variables and expressions in all program executions Extracted information example: live variables Idea: Define program points in the CFG Reason statically about how the information flows between these program points

Program Points Two program points for each instruction: There is a program point before each instruction There is a program point after each instruction In a basic block: Program point after an instruction = program point before the successor instruction x = y+1 Point before Point after

Program Points: Example Multiple successor blocks means that point at the end of a block has multiple successor program points Depending on the execution, control flows from a program point to one of its successors Also multiple predecessors How does information propagate between program points? x = y+1 y =2*z if (d) x = y+z z = 1

Flow of Extracted Information x = y+1 y =2*z if (d) x = y+z z = 1 Question 1: how does information flow between the program points before and after an instruction? Question 2: how does information flow between successor and predecessor basic blocks? … in other words: Q1: what is the effect of instructions? Q2: what is the effect of control flow?

Using CFGs To extract information: reason about how it propagates between program points Rest of this lecture: how to use CFGs to compute information at each program point for: Live variable analysis, which computes which variables are live at each program point Copy propagation analysis, which computes the variable copies available at each program point

Live variables A statement is a definition of a variable v if it may write to v. A statement is a use of variable v if it may read from v. A variable v is live at a point p in a CFG if there is a path from p to a use of v, and that path does not contain a definition of v {x,y,z,d,c} if (c) {x,y,z,d,c} x = y+1 {x,z,d,c} y=2*z {x,y,z,d,c} if(d) {y,z,d,c} {x} {x,y,d,c} x=y+z {x,y,d,c} z=1 z=x {}

Computing Use/Def Compute use[I] and def[I] for each instruction I: if I is x = y OP z : use[I] = {y, z} def[I] = {x} if I is x = OP y : use[I] = {y} def[I] = {x} if I is x = y : use[I] = {y} def[I] = {x} if I is x = addr y : use[I] = {} def[I] = {x} if I is if (x) : use[I] = {x} def[I] = {} if I is return x : use[I] = {x} def[I] = {} if I is x = f(y1 ,…, yn) : use[I] = {y1, …, yn} def[I] = {x} (For now, ignore load and store instructions)

Part 1: Analyze Instructions Question: what is the relation between sets of reaching definitions before and after an instruction? Examples: … is there a general rule? in[I] I out[I] in[I] = {y,z} in[I] = {y,z,t} conclude in[I] = {x,t} x = y+z; x = y+z; x = x+1; out[I] = {z} out[I] = {x,t} assume out[I] = {x,t}

Live Variable Analysis Computes live variables at each program point I.e., variables holding values that may be used later (in some execution of the program) For an instruction I, consider: in[I] = live variables at program point before I out[I] = live variables at program point after I For a basic block B, consider: in[B] = live variables at beginning of B out[B] = live variables at end of B If I = first instruction in B, then in[B] = in[I] If I’ = last instruction in B, then out[B] = out[I’]

How to Compute Liveness? Answer question 1: for each instruction I, what is the relation between in[I] and out[I] ? Answer question 2: for each basic block B with successor blocks B1, …, Bn, what is the relation between out[B] and in[B1], …, in[Bn]? in[I] I out[I] B out[B] in[B1] B1 … in[Bn] Bn

Part 1: Analyze Instructions Question: what is the relation between sets of live variables before and after an instruction? Examples: … is there a general rule? in[I] I out[I] in[I] = {y,z} in[I] = {y,z,t} conclude in[I] = {x,t} x = y+z; x = y+z; x = x+1; out[I] = {z} out[I] = {x,t} assume out[I] = {x,t}

Analyze Instructions Yes: knowing variables live after I, can compute variables live before I: Each variable live after I is also live before I, unless I defines (writes) it Each variable that I uses (reads) is also live before instruction I in[I] I out[I] Mathematically: in[I] = ( out[I] – def[I] )  use[I] where: def[I] = variables defined (written) by instruction I use[I] = variables used (read) by instruction I

Example Example: block B with three instructions I1, I2, I3: Live1 = in[B] = in[I1] Live2 = out[I1] = in[I2] Live3 = out[I2] = in[I3] Live4 = out[I3] = out[B] Relation between Live sets: Live1 = ( Live2-{x} )  {y} Live2 = ( Live3-{y} )  {z} Live3 = ( Live4-{} )  {d} Block B Live1 x = y+1 Live2 y =2*z Live3 if (d) Live4 I1 I2 I3

Backward Flow Relation: in[I] = ( out[I] – def[I] )  use[I] The information flows backward! Instructions: can compute in[I] if we know out[I] Basic blocks: information about live variables flows from out[B] to in[B] in[I] I out[I] In[B] x = y+1 y =2*z if (d) out[B]

Part 2: Analyze Control Flow Question: for each basic block B with successor blocks B1, …, Bn, what is the relation between out[B] and in[B1], …, in[Bn]? Examples: What is the general rule? B out[B] in[B1] B1 in[Bn] Bn … B {x,y,z} B {x,y,z} {x,z} B1 {x,y} B2 {x} B1 {y} B2 {z} B2

Analyze Control Flow Rule: A variables is live at end of block B if it is live at the beginning of one (or more) successor blocks Characterizes all possible program executions Mathematically: out[B] =  in[B’] B out[B] in[B1] B1 in[Bn] Bn B’  succ(B) … Again, information flows backward: from successors B’ of B to basic block B

Constraint System Put parts together: start with CFG and derive a system of constraints between live variable sets: in[I] = ( out[I] – def[I] )  use[I] for each instruction I out[B] =  in[B’] for each basic block B Solve constraints: Start with empty sets of live variables Iteratively apply constraints Stop when we reach a fixed point B’  succ(B)

Live variables 1 L10 = { } L3 = {x} U (L10- {z}) L9 = L2 U L3 U {c} L8 = L9 – {z} L7 = L9 – {z} L6 = {y,z} U (L8 – {x}) L5 = L6 U L7 U {d} L4 = {z} U (L5 – {y}) L2 = {y} U (L4 – {x}) L1 = L2 U L3 U {c} {x,y,z,d,c} if (c) {x,y,z,d,c} 2 x = y+1 4 {x,z,d,c} 3 y=2*z 5 {x,y,z,d,c} if(d) {y,z,d,c} {x} 6 {x,y,d,c} x=y+z 8 7 {x,y,d,c} z=1 9 z=x 10 {}

Constraint Solving Algorithm for all instructions I do in[I] = out[I] = ; repeat select an instuction I (or a basic block B) such that in[I]  ( out[I] – def[I] )  use[I] or (respectively) out[B]   in[B’] and update in[I] (or out[B]) accordingly until no such change is possible B’  succ(B)

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 def = {}, use = {c} def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} def = {x}, use = {y,z} def = {z}, use = {} def = {z}, use = {x}

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 { }10 def = {}, use = {c} { }9 { }6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} { }5 { }4 def = {x}, use = {y,z} { }3 { }2 def = {z}, use = {} { }1 { }8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 { }10 def = {}, use = {c} { }9 { }6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} { }5 { }4 def = {x}, use = {y,z} { }3 { }2 def = {z}, use = {} { }1 { }8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 { }10 def = {}, use = {c} { }9 { }6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} { }5 { }4 def = {x}, use = {y,z} { }3 { }2 def = {z}, use = {} { }1 { }8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 { }10 def = {}, use = {c} { }9 { }6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} { }5 { }4 def = {x}, use = {y,z} { }3 { }2 def = {z}, use = {} { }1 { }8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 { }10 def = {}, use = {c} { }9 { }6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} { }5 {y,z}4 def = {x}, use = {y,z} { }3 { }2 def = {z}, use = {} { }1 { }8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 { }10 def = {}, use = {c} { }9 { }6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {y,z}5 {y,z}4 def = {x}, use = {y,z} { }3 { }2 def = {z}, use = {} { }1 { }8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 { }10 def = {}, use = {c} { }9 {y,z,d}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {y,z}5 {y,z}4 def = {x}, use = {y,z} { }3 { }2 def = {z}, use = {} { }1 { }8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 { }10 def = {}, use = {c} { }9 {y,z,d}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {y,z}5 {y,z}4 def = {x}, use = {y,z} { }3 { }2 def = {z}, use = {} { }1 { }8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 { }10 def = {}, use = {c} { }9 {y,z,d}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {y,z}5 {y,z}4 def = {x}, use = {y,z} { }3 { }2 def = {z}, use = {} { }1 {x}8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 { }10 def = {}, use = {c} {x,y,z,d}9 {y,z,d}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {y,z}5 {y,z}4 def = {x}, use = {y,z} { }3 { }2 def = {z}, use = {} { }1 {x}8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 {x,y,z,d,c}10 def = {}, use = {c} {x,y,z,d}9 {y,z,d}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {y,z}5 {y,z}4 def = {x}, use = {y,z} { }3 { }2 def = {z}, use = {} { }1 {x}8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 {x,y,z,d,c}10 def = {}, use = {c} {x,y,z,d}9 {y,z,d}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {y,z}5 {y,z}4 def = {x}, use = {y,z} { }3 { }2 def = {z}, use = {} {x,y,z,d,c}1 {x}8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 {x,y,z,d,c}10 def = {}, use = {c} {x,y,z,d}9 {y,z,d}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {y,z}5 {y,z}4 def = {x}, use = {y,z} { }3 {x,y,d,c}2 def = {z}, use = {} {x,y,z,d,c}1 {x}8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 {x,y,z,d,c}10 def = {}, use = {c} {x,y,z,d}9 {y,z,d}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {y,z}5 {y,z}4 def = {x}, use = {y,z} {x,y,d,c}3 {x,y,d,c}2 def = {z}, use = {} {x,y,z,d,c}1 {x}8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 {x,y,z,d,c}10 def = {}, use = {c} {x,y,z,d}9 {y,z,d}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {y,z}5 {y,z,d,c}4 def = {x}, use = {y,z} {x,y,d,c}3 {x,y,d,c}2 def = {z}, use = {} {x,y,z,d,c}1 {x}8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 {x,y,z,d,c}10 def = {}, use = {c} {x,y,z,d}9 {y,z,d}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {x,y,z,d,c}5 {y,z,d,c}4 def = {x}, use = {y,z} {x,y,d,c}3 {x,y,d,c}2 def = {z}, use = {} {x,y,z,d,c}1 {x}8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 {x,y,z,d,c}10 def = {}, use = {c} {x,y,z,d}9 {y,z,d,c}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {x,y,z,d,c}5 {y,z,d,c}4 def = {x}, use = {y,z} {x,y,d,c}3 {x,y,d,c}2 def = {z}, use = {} {x,y,z,d,c}1 {x}8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 {x,y,z,d,c}10 def = {}, use = {c} {x,y,z,d}9 {y,z,d,c}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {x,y,z,d,c}5 {y,z,d,c}4 def = {x}, use = {y,z} {x,y,d,c}3 {x,y,d,c}2 def = {z}, use = {} {x,y,z,d,c}1 {x}8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 {x,y,z,d,c}10 def = {}, use = {c} {x,y,z,d}9 {y,z,d,c}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {x,y,z,d,c}5 {y,z,d,c}4 def = {x}, use = {y,z} {x,y,d,c}3 {x,y,d,c}2 def = {z}, use = {} {x,y,z,d,c}1 {x}8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 {x,y,z,d,c}10 def = {}, use = {c} {x,y,z,d,c}9 {y,z,d,c}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {x,y,z,d,c}5 {y,z,d,c}4 def = {x}, use = {y,z} {x,y,d,c}3 {x,y,d,c}2 def = {z}, use = {} {x,y,z,d,c}1 {x}8 def = {z}, use = {x} { }7

Example x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 {x,y,z,d,c}10 def = {}, use = {c} {x,y,z,d,c}9 {y,z,d,c}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {x,y,z,d,c}5 {y,z,d,c}4 def = {x}, use = {y,z} {x,y,d,c}3 {x,y,d,c}2 def = {z}, use = {} {x,y,z,d,c}1 {x}8 def = {z}, use = {x} { }7

Fixed Point Reached x = y+1 y =2*z if (d) def = {}, use = {c} if (c) z = x if (c) x = y+z z = 1 {x,y,z,d,c}10 def = {}, use = {c} {x,y,z,d,c}9 {y,z,d,c}6 def = {x}, use = {y} def = {y}, use = {z} def = {}, use = {d} {x,y,z,d,c}5 {y,z,d,c}4 def = {x}, use = {y,z} {x,y,d,c}3 {x,y,d,c}2 def = {z}, use = {} {x,y,z,d,c}1 {x}8 def = {z}, use = {x} { }7

General questions Do systems of equations of this sort always have solutions? If so, do they have unique solutions? If there are multiple solutions, which one is the “right” one? How do we solve such systems of equations in general? If we use the iterative method, does it always terminate and if so, does it always produce a unique answer?

Copy Propagation Goal: determine copies available at each program point Information: set of copies <x=y> at each point For each instruction I: in[I] = copies available at program point before I out[I] = copies available at program point after I For each basic block B: in[B] = copies available at beginning of B out[B] = copies available at end of B If I = first instruction in B, then in[B] = in[I] If I’ = last instruction in B, then out[B] = out[I’]

Same Methodology Express flow of information (i.e., available copies): For points before and after each instruction (in[I], out[I]) For points at exit and entry of basic blocks (in[B], out[B]) Build constraint system using the relations between available copies Solve constraints to determine available copies at each point in the program

Analyze Instructions Knowing in[I], can compute out[I]: Remove from in[I] all copies <u=v> if variable u or v is written by I Keep all other copies from in[I] If I is of the form x=y, add it to out[I] in[I] I out[I] Mathematically: out[I] = ( in[I] – kill[I] )  gen[I] where: kill[I] = copies “killed” by instruction I gen[I] = copies “generated” by instruction I

Computing Kill/Gen Compute kill[I] and gen[I] for each instruction I: if I is x = y OP z : gen[I] = {} kill[I] = {u=v|u or v is x} if I is x = OP y : gen[I] = {} kill[I] = {u=v|u or v is x} if I is x = y : gen[I] = {x=y} kill[I] = {u=v|u or v is x} if I is x = addr y : gen[I] = {} kill[I] = {u=v|u or v is x} if I is if (x) : gen[I] = {} kill[I] = {} if I is return x : gen[I] = {} kill[I] = {} if I is x = f(y1 ,…, yn) : gen[I] = {} kill[I] = {u=v| u or v is x} (again, ignore load and store instructions)

Forward Flow Relation: out[I] = ( in[I] – kill[I] )  gen[I] The information flows forward! Instructions: can compute out[I] if we know in[I] Basic blocks: information about available copies flows from in[B] to out[B] in[I] I out[I] In[B] x = y y =2*z if (d) out[B]

Analyze Control Flow Rule: A copy is available at beginning of block B if it is available at the end of all predecessor blocks Characterizes all possible program executions Mathematically: in[B] =  out[B’] B1 out[B1] Bn out[Bn] … B’  pred(B) in[B] B Information flows forward: from predecessors B’ of B to basic block B

Constraint System Build constraints: start with CFG and derive a system of constraints between sets of available copies: out[I] = ( in[I] – kill[I] )  gen[I] for each instruction I in[B] =  out[B’] for each basic block B Solve constraints: Start with empty set of available copies at start and universal set of available copies everywhere else Iteratively apply constraints Stop when we reach a fixed point B’  pred(B)

Example What are the available copies at the end of the program? x=y? z=t L2 What are the available copies at the end of the program? x=y? z=t? x=z? L3 L4 if (c) L5 L6 x = z y =2*z if (d) L7 L8 L9 L10 t=1 L11 L12 u=z+1 z = t L13 L14

Example What are the available copies at the end of the program? x=y? z=t L2 ={all} What are the available copies at the end of the program? x=y? z=t? x=z? L3 ={all} L4 ={all} if (c) L5 ={all} L6 ={all} x = z y =2*z if (d) L7 ={all} L8 ={all} L9 ={all} L10 ={all} t=1 L11 ={all} L12 ={all} u=z+1 z = t L13 ={all} L14 ={all}

Iteration 1 What are the available copies at the end of the program? x=y z=t L2 ={x=y} What are the available copies at the end of the program? x=y? z=t? x=z? L3 ={x=y, z=t} L4 ={x=y, z=t} if (c) L5 ={x=y, z=t} L6 ={x=y, z=t} x = z y =2*z if (d) L7 ={x=z,z=t} L8 ={x=z, z=t} L9 ={x=z, z=t} L10 ={x=z, z=t} t=1 L11 ={x=z} L12 ={x=z} u=z+1 z = t L13 ={x=z} L14 ={z=t}

Iteration 2 What are the available copies at the end of the program? x=y z=t L2 ={x=y} What are the available copies at the end of the program? x=y? z=t? x=z? L3 ={x=y, z=t} L4 ={z=t} if (c) L5 ={z=t} L6 ={z=t} x = z y =2*z if (d) L7 ={z=t, x=z} L8 ={z=t, x=z} L9 ={z=t, x=z} L10 ={z=t, x=z} t=1 L11 ={x=z} L12 ={x=z} u=z+1 z = t L13 ={x=z} L14 ={z=t}

Fixed Point Reached! L1 ={} x=y z=t L2 ={x=y} What are the available copies at the end of the program? x=y? NO z=t? YES x=z? NO L3 ={x=y, z=t} L4 ={z=t} if (c) L5 ={z=t} L6 ={z=t} x = z y =2*z if (d) L7 ={z=t, x=z} L8 ={z=t, x=z} L9 ={z=t, x=z} L10 ={z=t, x=z} t=1 L11 ={x=z} L12 ={x=z} u=z+1 z = t L13 ={x=z} L14 ={z=t}

Summary Extracting information about live variables and available copies is similar Define the required information Define information before/after instructions Define information at entry/exit of blocks Build constraints for instructions/control flow Solve constraints to get needed information …is there a general framework? Yes: dataflow analysis!