advanced compiler techniques 2004 03 19 08 51
play

Advanced Compiler Techniques 2004-03-19 08:51 Foundations of - PDF document

Advanced Compiler Techniques 2004-03-19 08:51 Foundations of Dataflow Analysis This lecture is primarily based on Konstantinos Sagonas set of slides (Advanced ed Co Compiler er T Techniques es , (2AD518) at Uppsala University,


  1. Advanced Compiler Techniques 2004-03-19 08:51 Foundations of Dataflow Analysis This lecture is primarily based on Konstantinos Sagonas set of slides (Advanced ed Co Compiler er T Techniques es , (2AD518) at Uppsala University, January-February 2004) . Used with kind permission. Advanced Compiler Techniques 1 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Terminology: Program Representation Terminology: Program Representation Control Flow Graph (CFG): ♦ Nodes N – statements of program ♦ Edges E – flow of control ♦ pred(n) = set of all immediate predecessors of n ♦ succ(n) = set of all immediate successors of n ♦ Start node n 0 ♦ Set of final nodes N final Advanced Compiler Techniques 2 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Terminology: Control-Flow Graph Terminology: Program Representation Control-flow graph (CFG) A m ← a + b • Nodes for basic blocks n ← a + b • Edges for branches B C p ← c + d q ← a + b • Basis for much of program r ← c + d r ← c + d analysis & transformation D E e ← b + 18 e ← a + 17 s ← a + b t ← c + d u ← e + f u ← e + f This CFG, F v ← a + b G = ( N , E ) w ← c + d x ← e + f N = { A , B , C , D , E , F , G } E = {( A , B ), ( A , C ), ( B , G ), G y ← a + b ( C , D ), ( C , E ), ( D , F ), z ← c + d ( E , F ),( F , G )} | N | = 7 | E | = 8 Advanced Compiler Techniques 3 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Lecture 2: Foundations 1

  2. Advanced Compiler Techniques 2004-03-19 08:51 Terminology: Extended Basic Block EBB: Conceptually it is a Terminology: Program Representation A m ← a + b program sequence with only n ← a + b one entry point but possibly several exit points. B C p ← c + d q ← a + b r ← c + d r ← c + d An EBB contains 1 or An EBB contains 1 or more paths. more paths. This EBB D E e ← b + 18 e ← a + 17 ({ A , B , C , D , E }) contains s ← a + b t ← c + d the paths { A , B } { A , C , D } u ← e + f u ← e + f { A , C , E } F v ← a + b w ← c + d Extended Basic Block (EBB): x ← e + f A sequence of basic blocks B 1 , B 2 , …, B n where B 1 has more than 1 predecessor, G all other B i have a unique predecessor. y ← a + b z ← c + d Path: A sequence of basic blocks B 1 , B 2 , …, B n where B i is the predecessor of B i+1 . Advanced Compiler Techniques 4 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Terminology: Program Points Terminology: Program Representation ♦ One program point before each node. ♦ One program point after each node. ♦ Join point – Program point with multiple predecessors. ♦ Split point – Program point with multiple successors. Advanced Compiler Techniques 5 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Dataflow Analysis Compile-Time Reasoning About Dataflow Analysis ♦ Run-Time Values of Variables or Expressions at different program points: ♦ Which assignment statements produced the value of the variables at this point? ♦ Which variables contain values that are no longer used after this program point? ♦ What is the range of possible values of a variable at this program point? Advanced Compiler Techniques 6 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Lecture 2: Foundations 2

  3. Advanced Compiler Techniques 2004-03-19 08:51 Dataflow Analysis ♦ Assumptions: Dataflow Analysis ♦ We have a syntactically and semantically correct program (as far as compile time analysis can determine this). ♦ We have the “whole” program, or a clearly defined subset of the program which will only interact with the rest of the program through a predefined interface. (That is, no self modifying code, and if the interface is a function then the parameters can take any value of the given type.) Advanced Compiler Techniques 7 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Dataflow Analysis: Basic Idea ♦ Information about a program represented Dataflow Analysis using values from an algebraic structure called lattice. (We will call this set of values P .) ♦ Analysis produces a lattice value for each program point. ♦ Two flavors of analyses: ♦ Forward dataflow analyses. ♦ Backward dataflow analyses. Advanced Compiler Techniques 8 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Forward Dataflow Analysis ♦ Analysis propagates values forward through control flow graph with flow of control ♦ Each node has a transfer function ƒ Dataflow Analysis ♦ Input – value at program point before node. ♦ Output – new value at program point after node. ♦ Values flow from program points after predecessor nodes to program points before successor nodes. ♦ At join points, values are combined using a merge function. ♦ Canonical Example: Reaching Definitions. Advanced Compiler Techniques 9 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Lecture 2: Foundations 3

  4. Advanced Compiler Techniques 2004-03-19 08:51 Backward Dataflow Analysis ♦ Analysis propagates values backward through control flow graph against flow of control: ♦ Each node has a transfer function ƒ Dataflow Analysis ♦ Input – value at program point after node. ♦ Output – new value at program point before node. ♦ Values flow from program points before successor nodes to program points after predecessor nodes. ♦ At split points, values are combined using a merge function. ♦ Canonical Example: Live Variables. Advanced Compiler Techniques 10 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Partial Orders Theory Foundation: Partial Orders ♦ Set P ♦ Partial order · such that ∀ x,y,z ∈ P x · x (reflexive) i. x · y and y · x ⇒ x = y (antisymmetric) ii. x · y and y · z ⇒ x · z (transitive) iii. Advanced Compiler Techniques 11 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Upper Bounds Theory Foundation: Partial Orders ♦ If S ⊆ P then ♦ x ∈ P is an upper bound of S if ∀ y ∈ S , y ≤ x ♦ x ∈ P is the least upper bound (lub) of S if ♦ x is an upper bound of S , and ♦ x ≤ y for all upper bounds y of S ♦ ∨ - join , least upper bound, supremum (sup) ♦ ∨ S is the least upper bound of S ♦ x ∨ y is the least upper bound of { x , y } Advanced Compiler Techniques 12 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Lecture 2: Foundations 4

  5. Advanced Compiler Techniques 2004-03-19 08:51 Lower Bounds Theory Foundation: Partial Orders ♦ If S ⊆ P then ♦ x ∈ P is a lower bound of S if ∀ y ∈ S , x ≤ y ♦ x ∈ P is the greatest lower bound (glb) of S if ♦ x is a lower bound of S , and ♦ y ≤ x for all lower bounds y of S ♦∧ - meet , greatest lower bound, infimum (inf) ♦ ∧ S is the greatest lower bound of S ♦ x ∧ y is the greatest lower bound of { x , y } Advanced Compiler Techniques 13 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Coverings Theory Foundation: Partial Orders ♦ Notation: x < y if x ≤ y and x ≠ y ♦ x is covered by y ( y covers x ) if ♦ x < y , and ♦ x ≤ z < y ⇒ x = z ♦ Conceptually, y covers x if there are no elements between x and y Advanced Compiler Techniques 14 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Dataflow Analysis: Basic Idea ♦ Information about a program represented Dataflow Analysis using values from an algebraic structure called lattice. (We will call this set of values P .) ♦ Analysis produces a lattice value for each program point. ♦ Two flavors of analyses: ♦ Forward dataflow analyses. ♦ Backward dataflow analyses. Advanced Compiler Techniques 15 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Lecture 2: Foundations 5

  6. Advanced Compiler Techniques 2004-03-19 08:51 Hasse Diagram Theory Foundation: Partial Orders ♦ We can visualize a partial order with a Hasse Diagram. ♦ For each element x we draw a circle: ♦ If y covers x ♦ Line from y to x y ♦ y above x in diagram x Advanced Compiler Techniques 16 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Hasse Diagram: Example Theory Foundation: Partial Orders P = {000, 001, 010, 011, 100, 101, 110, 111} x ≤ y if ( x bitwise_and y ) = x 111 (standard boolean lattice, also called hypercube) 110 011 101 010 001 100 000 Advanced Compiler Techniques 17 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Lattices ♦ If x ∧ y and x ∨ y exist for all x,y ∈ P , Theory Foundation: Lattices then P is a lattice . ♦ If ∧ S and ∨ S exist for all S ⊆ P , then P is a complete lattice . ♦ Theorem: All finite lattices are complete. ♦ Example of a lattice that is not complete ♦ Integers Z ♦ For any x,y ∈ Z , x ∨ y = max ( x,y ), x ∧ y = min ( x,y ) ♦ But ∨ Z and ∧ Z do not exist ♦ Z ∪ { +∞ , −∞ } is a complete lattice Advanced Compiler Techniques 18 ht t p: / / l am p. epf l . ch/ t eachi ng/ advancedCom pi l er / Lecture 2: Foundations 6

Recommend


More recommend