1
play

1 Liveness by Example (cont) Control Flow Graphs (CFGs) Live range - PowerPoint PPT Presentation

Introduction to Data-flow analysis Data-flow Analysis Last Time Idea Undergraduate compilers review Data-flow analysis derives information about the dynamic behavior of a program by only examining the static code Assem.Instr data


  1. Introduction to Data-flow analysis Data-flow Analysis Last Time Idea – Undergraduate compilers review – Data-flow analysis derives information about the dynamic behavior of a program by only examining the static code – Assem.Instr data structure in the MiniJava compiler Example Today – How many registers do we need – Control flow graph construction a := 0 1 for the program on the right? – Liveness analysis L1: b := a + 1 2 – Easy bound: the number of c := c + b variables used (3) 3 a := b * 2 – Better answer is found by 4 considering the dynamic if a < 9 goto L1 5 requirements of the program return c 6 CS553 Lecture Introduction to Data-flow Analysis 2 CS553 Lecture Introduction to Data-flow Analysis 3 Liveness Analysis Liveness by Example What is the live range of b ? Definition – A variable is live at a particular point in the program if its value at that – Variable b is read in statement 4, a = 0 1 so b is live on the (3 → 4) edge point will be used in the future ( dead , otherwise). ∴ To compute liveness at a given point, we need to look into the future – Since statement 3 does not assign b = a + 1 2 into b , b is also live on the (2 → 3) edge Motivation: Register Allocation c = c + b 3 – Statement 2 assigns b , so any – A program contains an unbounded number of variables value of b on the (1 → 2) and (5 → 4 a = b * 2 – Must execute on a machine with a bounded number of registers 2) edges are not needed, so b is – Two variables can use the same register if they are never in use at the same dead along these edges time ( i.e, never simultaneously live). 5 a<9 ∴ Register allocation uses liveness information No Yes b ’s live range is (2 → 3 → 4) return c 6 CS553 Lecture Introduction to Data-flow Analysis 4 CS553 Lecture Introduction to Data-flow Analysis 5 1

  2. Liveness by Example (cont) Control Flow Graphs (CFGs) Live range of a Definition – a is live from (1 → 2) and again from – A CFG is a graph whose nodes represent program statements and 1 a = 0 (4 → 5 → 2) whose directed edges represent control flow – a is dead from (2 → 3 → 4) b = a + 1 2 a = 0 1 Example Live range of b 3 c = c + b a := 0 1 b = a + 1 2 – b is live from (2 → 3 → 4) L1: b := a + 1 2 4 a = b * 2 c := c + b c = c + b 3 3 Live range of c a := b * 2 4 – c is live from a<9 5 4 a = b * 2 if a < 9 goto L1 5 (entry → 1 → 2 → 3 → 4 → 5 → 2, 5 → 6) No Yes return c 6 return c 6 5 a<9 No Yes Variables a and b are never simultaneously live, so they can share a register return c 6 CS553 Lecture Introduction to Data-flow Analysis 6 CS553 Lecture Introduction to Data-flow Analysis 7 Terminology Uses and Defs Flow Graph Terms Def (or definition) a = 0 – A CFG node has out-edges that lead to successor nodes and in-edges that – An assignment of a value to a variable come from predecessor nodes – def[v] = set of CFG nodes that define variable v – pred[n] is the set of all predecessors of node n – def[n] = set of variables that are defined at node n a = 0 1 succ[n] is the set of all successors of node n a < 9? Use 2 b = a + 1 Examples – A read of a variable’s value – Out-edges of node 5: (5 → 6) and (5 → 2) v live – use[v] = set of CFG nodes that use variable v c = c + b 3 – succ[5] = {2,6} – use[n] = set of variables that are used at node n – pred[5] = {4} ∉ def[v] 4 a = b * 2 – pred[2] = {1,5} More precise definition of liveness ∈ use[v] 5 a<9 – A variable v is live on a CFG edge if (1) ∃ a directed path from that edge to a use of v (node in use[v]), and No Yes return c 6 (2) that path does not go through any def of v (no nodes in def[v]) CS553 Lecture Introduction to Data-flow Analysis 8 CS553 Lecture Introduction to Data-flow Analysis 9 2

  3. program points The Flow of Liveness Liveness at Nodes edges Data-flow We have liveness on edges just before computation a = 0 – Liveness of variables is a property that flows – How do we talk about just after computation through the edges of the CFG liveness at nodes? a := 0 1 Two More Definitions Direction of Flow b := a + 1 – A variable is live-out at a node if it is live on any of that node’s out- 2 – Liveness flows backwards through the CFG, edges because the behavior at future nodes 3 c := c + b n determines liveness at a given node live-out out-edges a := b * 2 4 – Consider a – A variable is live-in at a node if it is live on any of that node’s in-edges – Consider b a < 9? 5 – Later, we’ll see other properties No Yes in-edges that flow forward return c 6 n live-in CS553 Lecture Introduction to Data-flow Analysis 10 CS553 Lecture Introduction to Data-flow Analysis 11 Computing Liveness Solving the Data-flow Equations Rules for computing liveness Algorithm (1) Generate liveness: live-in n If a variable is in use[n], use for each node n in CFG initialize solutions it is live-in at node n in[n] = ∅ ; out[n] = ∅ (2) Push liveness across edges: repeat pred[n] live-out live-out live-out If a variable is live-in at a node n for each node n in CFG then it is live-out at all nodes in pred[n] n live-in in’[n] = in[n] save current results out’[n] = out[n] (3) Push liveness across nodes: If a variable is live-out at node n and not in def[n] live-in in[n] = use[n] ∪ (out[n] – def[n]) solve data-flow equations n then the variable is also live-in at n out[n] = ∪ in[s] live-out s ∈ succ[n] Data-flow equations until in’[n]=in[n] and out’[n]=out[n] for all n test for convergence in[n] = use[n] ∪ (out[n] – def[n]) (1) (3) This is iterative data-flow analysis (for liveness analysis) out[n] = ∪ in[s] (2) s ∈ succ[n] CS553 Lecture Introduction to Data-flow Analysis 12 CS553 Lecture Introduction to Data-flow Analysis 13 3

  4. Example Liveness in the MiniJava compiler 1st 2nd 3rd 4th 5th 6th 7th node use def in out in out in out in out in out in out in out # a := 0 1 1 a a a ac c ac c ac c ac 2 a b a a bc ac bc ac bc ac bc ac bc ac bc 2 b := a + 1 3 bc c bc bc b bc b bc b bc b bc bc bc bc 3 c := c + b 4 b a b b a b a b ac bc ac bc ac bc ac a ac 5 a a a ac ac ac ac ac ac ac ac ac ac 4 a := b * 2 6 c c c c c c c c a < 9? Data-flow Equations for Liveness 5 No Yes in[n] = use[n] ∪ (out[n] – def[n]) return c 6 out[n] = ∪ in[s] s ∈ succ[n] CS553 Lecture Introduction to Data-flow Analysis 14 CS553 Lecture Introduction to Data-flow Analysis 15 Concepts Next Time Liveness Reading – Used in register allocation – Ch 11, register allocation – Generating liveness – Flow and direction Lecture – Data-flow equations and analysis – Register allocation Control flow graphs – Predecessors and successors Defs and uses CS553 Lecture Introduction to Data-flow Analysis 16 CS553 Lecture Introduction to Data-flow Analysis 17 4

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend