(The Lectures Are Based on the Slides Copyrighted by Keith Cooper And

(The Lectures Are Based on the Slides Copyrighted by Keith Cooper And

CS 515 Programming Language and Compilers I Lecture 10: Data Flow Analysis II (The lectures are based on the slides copyrighted by Keith Cooper and Linda Torczon from Rice University.) Zheng (Eddy) Zhang Rutgers University Fall 2017, 11/14/2017 !2 Review: Compiler Middle End (Optimizer) Front IR Middle IR Back Machine End End End code Errors • Front end works from the syntax of the source code • Rest of the compiler works from intermediate representation (IR) - Analyzes IR to learn about the code - Transforms IR to improve final code - Examples 1. Parallelization 2. Redundancy elimination 3. Data flow analysis (DFA) framework 4. Single static assignment (SSA) !3 Review: Compiler Middle End (Optimizer) Front IR Middle IR Back Machine End End End code Errors • Front end works from the syntax of the source code • Rest of the compiler works from intermediate representation (IR) - Analyzes IR to learn about the code - Transforms IR to improve final code - Examples 1. Parallelization 2. Redundancy elimination 3. Data flow analysis (DFA) framework 4. Single static assignment (SSA) !4 Review: Redundancy Elimination An expression x+y is redundant if and only if, along every path from the procedure’s entry, it has been evaluated, and its constituent subexpressions (x & y) have not been re-defined. If the compiler can prove that an expression is redundant • It can preserve the results of earlier evaluations • It can replace the current evaluation with a reference Two pieces to the problem • Proving that x+y is redundant • Rewriting the code to eliminate the redundant evaluation One single-pass, local technique to accomplish both is called value numbering (VN) !5 Review Redundancy Elimination • Local Value Numbering (LVN) • Superlocal Value Numbering (SVN) • Dominator Value Numbering (DVN) • Global Common Subexpression Elimination (GCSE) !6 Review: Local Value Numbering A m ← a + b n ← a + b Local Value Numbering (LVN) C Find redundant operation within a block q ← a + b B r ← c + d p ← c + d D r ← c + d E e ← b + 18 e ← a + 17 LVN finds 2 redundant ops s ← a + b t ← c + d u ← e + f u ← e + f LVN misses 8 redundant ops! v ← a + b F w ← c + d x ← e + f G y ← a + b z ← c + d !7 Review: Local Value Numbering The Algorithm For each operation o in the block 1. Get value numbers for the operands from a hash lookup 2. Hash <operator,VN(o1),VN(o2)> to get a value number for o 3. If o already had a value number, replace o with a reference 4. If o1 & o2 are constant, evaluate it & use a “load immediate” Focus on each operand’s value number, not its name. !8 Review: Superlocal Value Numbering A m ← a + b n ← a + b Local Value Numbering (LVN) C Find redundant operation within a block q ← a + b B Superlocal Value Numbering (SVN) r ← c + d p ← c + d Find redundant operation within a EBB D r ← c + d E e ← b + 18 e ← a + 17 s ← a + b t ← c + d u ← e + f u ← e + f v ← a + b F w ← c + d x ← e + f G y ← a + b z ← c + d !9 Review: Superlocal Value Numbering A m ← a + b n ← a + b Local Value Numbering (LVN) C Find redundant operation within a block q ← a + b B Superlocal Value Numbering (SVN) r ← c + d p ← c + d Find redundant operation within a EBB D r ← c + d E An Extended Basic Block (EBB) e ← b + 18 e ← a + 17 Set of blocks B1, B2, …, Bn s ← a + b t ← c + d • B1 is either the entry node to the u ← e + f u ← e + f procedure, or it has > 1 pred. •All other B have 1 pred. & that pred. v ← a + b i F w ← c + d is in the EBB x ← e + f Three EBBs in this CFG G { A, B, C, D, E } y ← a + b 1. z ← c + d 2. { F } 3. { G } !10 Review: Superlocal Value Numbering The SVN Algorithm 1. Identify EBBs 2. In depth-first order over an EBB, starting with the head of the EBB, p0 (I) Apply LVN to pi (II) Invoke SVN on each of pi’s EBB successors • When going from pi to its EBB successor pj, extend the symbol table with a new scope for pj, apply LVN to pj , & process pj’s EBB successors • When going from pj to its EBB predecessor pi , discard the scope for pj Use a scoped table & the right name space !11 Review: Superlocal Value Numbering A m ← a + b n ← a + b Local Value Numbering (LVN) C Find redundant operation within a block q ← a + b B Superlocal Value Numbering (SVN) r ← c + d p ← c + d Find redundant operation within a EBB D r ← c + d E An Extended Basic Block (EBB) e ← b + 18 e ← a + 17 Set of blocks B1, B2, …, Bn s ← a + b t ← c + d • B1 is either the entry node to the u ← e + f u ← e + f procedure, or it has > 1 pred. •All other B have 1 pred. & that pred. v ← a + b i F w ← c + d is in the EBB x ← e + f Three EBBs in this CFG G { A, B, C, D, E } y ← a + b 1. z ← c + d 2. { F } 3. { G } !12 Review: Superlocal Value Numbering A m ← a + b n ← a + b Local Value Numbering (LVN) C Find redundancy within a block q ← a + b B Superlocal Value Numbering (SVN) r ← c + d p ← c + d Find redundancy within a EBB D r ← c + d E e ← b + 18 e ← a + 17 s ← a + b t ← c + d u ← e + f u ← e + f SVN finds 5 redundant ops. v ← a + b SVN misses 5 redundant ops! F w ← c + d x ← e + f G y ← a + b z ← c + d !13 Review: Superlocal Value Numbering A m ← a + b n ← a + b Local Value Numbering (LVN) C Find redundancy within a block q ← a + b B Superlocal Value Numbering (SVN) r ← c + d p ← c + d Find redundancy within a EBB D r ← c + d E Dominator Value Numbering (DVN) e ← b + 18 e ← a + 17 Find redundancy in IDOM-tree s ← a + b t ← c + d u ← e + f u ← e + f v ← a + b F w ← c + d x ← e + f G y ← a + b z ← c + d !14 Redundancy Elimination An expression x+y is redundant if and only if, along every path from the procedure’s entry, it has been evaluated, and its constituent subexpressions (x & y) have not been re-defined. We need to understand the properties of those paths A m ← a + b • Specifically, what blocks occur on any path from entry to p n ← a + b C • If we can find those blocks, we can use their hash tables q ← a + b r ← c + d B For example: D E • A is on every path to any other node; and C is on every path p ← c + d e ← b + 18 e ← a + 17 r ← c + d s ← a + b t ← c + d to D, E, and F. u ← e + f u ← e + f • Only A is on every path to G. v ← a + b F w ← c + d x ← e + f G y ← a + b z ← c + d !15 Review: Dominance in a Control Flow Graph Definition In a flow graph, x dominates y if and only if every path from the entry node of the control-flow graph to y includes x • By definition, x dominates x itself A m ← a + b • We associate a DOM set with each node n ← a + b C DOM(x) contains the set of nodes that dominate x • q ← a + b r ← c + d • |DOM(x )| ≥ 1 B D E Block DOM IDOM p ← c + d e ← b + 18 e ← a + 17 A A — r ← c + d s ← a + b t ← c + d B A,B A u ← e + f u ← e + f C A,C A D A,C,D C v ← a + b F w ← c + d E A,C,E C x ← e + f F A,C,F C G y ← a + b G A,G A z ← c + d !16 Review: Dominance in a Control Flow Graph Immediate dominator • For any node x, there must be a y in DOM(x ) closest to x • We call this y the immediate dominator of x - x cannot be its own immediate dominator, unless x is n0 A m ← a + b • As a matter of notation, we write this as IDOM(x ) n ← a + b C q ← a + b r ← c + d B D E Block DOM IDOM p ← c + d e ← b + 18 e ← a + 17 A A — r ← c + d s ← a + b t ← c + d B A,B A u ← e + f u ← e + f C A,C A D A,C,D C v ← a + b F w ← c + d E A,C,E C x ← e + f F A,C,F C G y ← a + b G A,G A z ← c + d !17 Computing Dominators To use dominance information, we need to compute DOM sets • A node n dominates node m iff n is on every path from n0 to m - Every node dominates itself, by definition - n’s immediate dominator is the node in DOM(n ) that is closest to n in the graph IDOM(n) ≠ n, unless n is n0 , by convention. !18 Computing Dominators Computing DOM • We formulate the computation of DOM sets as a data-flow analysis (DFA) problem • Simultaneous equations over sets associated with the nodes in the CFG Equations relate the set value at a node n to those of its predecessors and successors.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    82 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us