Introdution to Data-flow Analysis Last Time Control flow analysis BT disussion Today Introdue iterative data-flow analysis Liveness analysis Introdue other useful onepts CIS570 Leture 4 Introdution to Data-flow Analysis Data-flow Analysis Idea Data-flow analysis derives information about the dynami behavior of a program by only examining the stati ode Example How many registers do we need for the program on the right? Easy bound: the number of variables used () Better answer is found by onsidering the dynami requirements of the program L1: b := a + 1 := + b 5 if a < 9 goto L1 CIS570 Leture 4 Introdution to Data-flow Analysis 1
Liveness Analysis Definition A variable is live at a partiular point in the program if its value at that point will be used in the future (dead, otherwise). To ompute liveness at a given point, we need to look into the future Motivation: Register Alloation A program ontains an unbounded number of variables Must exeute on a mahine with a bounded number of registers Two variables an use the same register if they are never in use at the same time (i.e, never simultaneously live). Register alloation uses liveness information CIS570 Leture 4 Introdution to Data-flow Analysis 4 Control Flow Graphs (CFGs) Simplifiation For now, we will use CFG s in whih nodes represent program statements rather than basi bloks Example 1 a = 0 L1: b := a + 1 := + b 5 if a < 9 goto L1 b = a + 1 = + b 4 a = b * 5 a<9 6 return CIS570 Leture 4 Introdution to Data-flow Analysis 5
Liveness by Example What is the live range of b? Variable b is read in statement 4, so b is live on the ( 4) edge Sine statement does not assign into b, b is also live on the ( ) edge Statement assigns b, so any value of b on the (1 ) and (5 ) edges are not needed, so b is dead along these edges b s live range is ( 4) 6 return 1 a = 0 b = a + 1 4 a = b * 5 = + b a<9 CIS570 Leture 4 Introdution to Data-flow Analysis 6 Liveness by Example (ont) Live range of a a is live from (1 ) and again from (4 5 ) a is dead from ( 4) 1 a = 0 b = a + 1 Live range of b b is live from ( 4) Live range of is live from (entry 1 4 5, 5 6) 6 return 5 = + b 4 a = b * a<9 Variables a and b are never simultaneously live, so they an share a register CIS570 Leture 4 Introdution to Data-flow Analysis 7
Terminology Flow Graph Terms A CFG node has out-edges that lead to suessor nodes and in-edges that ome from predeessor nodes pred[n] is the set of all predeessors of node n 1 a = 0 su[n] is the set of all suessors of node n Examples Out-edges of node 5: su[5] = pred[5] = pred[] = {,6} {4} {1,5} (5 6) and (5 ) 6 return 5 b = a + 1 = + b 4 a = b * a<9 CIS570 Leture 4 Introdution to Data-flow Analysis 8 Uses and Defs Def (or definition) An assignment of a value to a variable def[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[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[v] More preise definition of liveness A variable v is live on a CFG edge if use[v] (1) a direted path from that edge to a use of v (node in use[v]), and () that path does not go through any def of v (no nodes in def[v]) CIS570 Leture 4 Introdution to Data-flow Analysis 9 4
The Flow of Liveness Data-flow Liveness of variables is a property that flows through the edges of the CFG Diretion of Flow Liveness flows bakwards through the CFG, beause the behavior at future nodes determines liveness at a given node Consider a Consider b Later, we ll see other properties that flow forward 4 5 b := a + 1 := + b a := b * a < 9? CIS570 Leture 4 Introdution to Data-flow Analysis 10 Liveness at des edges We have liveness on edges just before omputation How do we talk about a = 0 just after omputation liveness at nodes? Two More Definitions A variable is live-out at a node if it is live on any b of := that a node s + 1 outedges := + b n live-out out-edges 4 a := b * A variable is live-in at a node if it is live on any 5 of that a < node s 9? in-edges n live-in in-edges program points CIS570 Leture 4 Introdution to Data-flow Analysis 11 5
Computing Liveness Rules for omputing liveness (1) Generate liveness: If a variable is in use[n], it is live-in at node n Data-flow equations (1) in[n] = use[n] (out[n] def[n]) () n live-in use () Push liveness aross edges: If a variable is live-in at a node n then it is live-out at all nodes in pred[n] live-out () Push liveness aross nodes: If a variable is live-out at node n and not in def[n] then the variable is also live-in at n live-out n live-in live-out live-in n live-out pred[n] out[n] = in[s] s su[n] () CIS570 Leture 4 Introdution to Data-flow Analysis 1 Solving the Data-flow Equations Algorithm for eah node n in CFG in[n] = ; out[n] = repeat for eah 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 su[n] until in [n]=in[n] and out [n]=out[n] for all n initialize solutions save urrent results solve data-flow equations test for onvergene This is iterative data-flow analysis (for liveness analysis) CIS570 Leture 4 Introdution to Data-flow Analysis 1 6
Example node # 1 a a b b 5 a 1st nd rd 4th 5th 6th 7th use def in out in out in out in out in out in out in out 4 b a 6 a b b a a a a b b b b a a a a a b b b b a a a a a b b b b a a a a a b b b b a a a a a b b b b a a a a a b b b b a a a b := a + 1 := + b Data-flow Equations for Liveness in[n] = use[n] (out[n] def[n]) out[n] = in[s] s su[n] CIS570 Leture 4 Introdution to Data-flow Analysis 14 Example (ont) Data-flow Equations for Liveness in[n] = use[n] (out[n] def[n]) out[n] = in[s] s su[n] Improving Performane Consider the ( 4) edge in the graph: out[4] is used to ompute in[4] in[4] is used to ompute out[]... So we should ompute the sets in the order: out[4], in[4], out[], in[],... out[] in[4] out[4] b := a + 1 := + b The order of omputation should follow the diretion of flow CIS570 Leture 4 Introdution to Data-flow Analysis 15 7
Iterating Through the Flow Graph Bakwards node # 1st nd rd use def out in out in out in 6 5 a a a a a a 4 b a a b a b a b b b b b b b b a b b a b a b a 1 a a a a Converges muh faster! b := a + 1 := + b CIS570 Leture 4 Introdution to Data-flow Analysis 16 Solving the Data-flow Equations (reprise) Algorithm for eah node n in CFG in[n] = ; out[n] = repeat for eah node n in CFG in reverse topsort order in [n] = in[n] Save urrent results out [n] = out[n] out[n] = in[s] s su[n] Solve data-flow equations in[n] = use[n] (out[n] def[n]) until in [n]=in[n] and out [n]=out[n] for all n Initialize solutions Test for onvergene CIS570 Leture 4 Introdution to Data-flow Analysis 17 8
Time Complexity Consider a program of size N Has N nodes in the flow graph (and at most N variables) Eah live-in or live-out set has at most N elements Eah set-union operation takes O(N) time The for loop body onstant # of set operations per node O(N) nodes O(N ) time for the loop Eah iteration of the repeat loop an only make the set larger Eah set an ontain at most N variables N iterations Worst ase: O(N 4 ) Typial ase: to iterations with good ordering & sparse sets O(N) to O(N ) CIS570 Leture 4 Introdution to Data-flow Analysis 18 More Performane Considerations Basi bloks Derease the size of the CFG by merging nodes that have a single predeessor and a single suessor into basi bloks 1 a := 0 b := a + 1 (requires loal analysis before and after global := + 1 analysis) a := b * + b a > 9? One variable at a time 4 a := b * Instead of omputing data-flow information return for all variables at one using sets, ompute a (simplified) analysis for eah variable separately Representation of sets For dense sets, use a bit vetor representation For sparse sets, use a sorted list (e.g., linked list) CIS570 Leture 4 Introdution to Data-flow Analysis 19 9
Conservative Approximation node # X Y Z use def in out in out in out 1 a a d ad a a b a b ad bd a b b b b bd bd b b 4 b a b a bd ad b a 5 a a a ad ad a a 6 b := a + 1 := + b Solution X Our solution as omputed on previous slides CIS570 Leture 4 Introdution to Data-flow Analysis 0 Conservative Approximation (ont) node # X Y Z use def in out in out in out 1 a a d ad a a b a b ad bd a b b b b bd bd b b 4 b a b a bd ad b a 5 a a a ad ad a a 6 b := a + 1 := + b Solution Y Carries variable d uselessly around the loop Does Y solve the equations? Is d live? Does Y lead to a orret program? Impreise onservative solutions sub-optimal but orret programs CIS570 Leture 4 Introdution to Data-flow Analysis 1 10
Conservative Approximation (ont) node # X Y Z use def in out in out in out 1 a a d ad a a b a b ad bd a b b b b bd bd b b 4 b a b a bd ad b a 5 a a a ad ad a a 6 b := a + 1 := + b Solution Z Does not identify as live in all ases Does Z solve the equations? Does Z lead to a orret program? n-onservative solutions inorret programs CIS570 Leture 4 Introdution to Data-flow Analysis The Need for Approximations Stati vs. Dynami Liveness In the following graph, b*b is always non-negative, so >= b is always true and a s value will never be used after node 1 a := b * b := a + b >= b? 4 return a 5 return Rule () for omputing liveness Sine a is live-in at node 4, it is live-out at nodes and This rule ignores atual ontrol flow ompiler an statially know all a program s dynami properties! CIS570 Leture 4 Introdution to Data-flow Analysis 11
Conepts Liveness Use in register alloation Generating liveness Flow and diretion Data-flow equations and analysis Complexity Improving performane (basi bloks, single variable, bit sets) Control flow graphs Predeessors and suessors Defs and uses Conservative approximation Stati versus dynami liveness CIS570 Leture 4 Introdution to Data-flow Analysis 4 Next Time Leture Generalizing data-flow analysis CIS570 Leture 4 Introdution to Data-flow Analysis 5 1