Dataflow AnalysisGuido Wachsmuth       Delft                              Course IN4303, 2012/13       University of      ...
Overviewtoday’s lecturecontrol flow graphs                      Dataflow Analysis   2
Overviewtoday’s lecturecontrol flow graphsdata flow analyses  •   liveness analysis  •   reaching definitions  •   availab...
Overviewtoday’s lecturecontrol flow graphsdata flow analyses  •   liveness analysis  •   reaching definitions  •   availab...
Icontrol-flow graphs                      Dataflow Analysis   3
Intermediate languagequadruplesstore                 jumps   a ← b ⊕ c             L:   a ← b                 goto L      ...
Control-flow graphsexample    a ← 0L1: b ← a + 1    c ← c + b    a ← 2 * b    if a < N       goto L1    else       goto L2...
Control-flow graphsexample    a ← 0             a ← 0L1: b ← a + 1                      b ← a + 1    c ← c + b    a ← 2 * ...
Control-flow graphsterminology              a ← 0              b ← a + 1              c ← c + b              a ← 2 * b    ...
Control-flow graphsterminology              a ← 0node          b ← a + 1              c ← c + b              a ← 2 * b    ...
Control-flow graphsterminology              a ← 0                          in-edgenode          b ← a + 1              c ←...
Control-flow graphsterminology              a ← 0                          in-edgenode          b ← a + 1              c ←...
Control-flow graphsterminologypredecessor   a ← 0                          in-edgenode          b ← a + 1              c ←...
Control-flow graphsterminologypredecessor   a ← 0                          in-edgenode          b ← a + 1                 ...
Control-flow graphsterminologypredecessor   a ← 0                          in-edgenode          b ← a + 1                 ...
IIliveness analysis                    Dataflow Analysis   7
Liveness analysisterminology              a ← 0              b ← a + 1              c ← c + b              a ← 2 * b      ...
Liveness analysisterminologydefinition    a ← 0              b ← a + 1              c ← c + b              a ← 2 * b      ...
Liveness analysisterminologydefinition    a ← 0usage         b ← a + 1              c ← c + b              a ← 2 * b      ...
Liveness analysisterminologydefinition    a ← 0usage         b ← a + 1              c ← c + bdefinition    a ← 2 * b      ...
Liveness analysisterminologydefinition    a ← 0usage         b ← a + 1              c ← c + bdefinition    a ← 2 * busage ...
Liveness analysisterminologydefinition    a ← 0usage         b ← a + 1              c ← c + bdefinition    a ← 2 * b      ...
Liveness analysisterminologydefinition    a ← 0usage         b ← a + 1              c ← c + bdefinition    a ← 2 * b      ...
Liveness analysisterminologydefinition    a ← 0                          live-inusage         b ← a + 1              c ← c...
Liveness analysisterminologydefinition    a ← 0                          live-inusage         b ← a + 1              c ← c...
Liveness analysisterminologydefinition    a ← 0       live-out                          live-inusage         b ← a + 1    ...
Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b             i...
Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b             i...
Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b             i...
Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b             i...
Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b             i...
Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b             i...
Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b             i...
Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b             i...
Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b             i...
Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b             i...
Liveness analysisexample             a ← 0             b ← a + 1             c ← c + b             a ← 2 * b             i...
Liveness analysisformalisation                    in[n] = use[n]   ∪ (out[n] - def[n])                          out[n] =  ...
Liveness analysisalgorithmfor each n   in[n] ← {} ; out[n] ← {}repeat   for each n         in[n] = in[n] ; out[n] = out[n]...
Liveness analysisexample1a ← 0                       1       2           3           4           5          6       7     ...
Liveness analysisexample1a ← 0                       1       2           3           4           5          6       7     ...
Liveness analysisexample1a ← 0                       1       2           3           4           5          6       7     ...
Liveness analysisexample1a ← 0                       1       2           3           4           5          6       7     ...
Liveness analysisexample1a ← 0                       1       2           3           4           5          6       7     ...
Liveness analysisexample1a ← 0                       1       2           3           4           5          6       7     ...
Liveness analysisexample1a ← 0                       1       2           3           4           5          6       7     ...
Liveness analysisexample1a ← 0                       1       2           3           4           5          6       7     ...
Liveness analysisexample1a ← 0                       1       2           3           4           5          6       7     ...
Liveness analysisoptimisation1a ← 0                           1            2              3                    u   d   o  ...
Liveness analysisoptimisation1a ← 0                           1            2              3                    u   d   o  ...
Liveness analysisoptimisation1a ← 0                           1            2              3                    u   d   o  ...
Liveness analysisoptimisation1a ← 0                           1            2              3                    u   d   o  ...
Liveness analysisoptimisation1a ← 0                           1            2              3                    u   d   o  ...
Liveness analysisgeneralisation                 a ← 0                 b ← a + 1                 c ← c + b                 ...
Liveness analysisgeneralisationkill             a ← 0                 b ← a + 1                 c ← c + b                 ...
Liveness analysisgeneralisationkill             a ← 0gen              b ← a + 1                 c ← c + b                 ...
Liveness analysisgeneralisationkill             a ← 0gen              b ← a + 1                 c ← c + bkill             ...
Liveness analysisgeneralisationkill             a ← 0gen              b ← a + 1                 c ← c + bkill             ...
Liveness analysisgeneralisationkill             a ← 0                             ingen              b ← a + 1            ...
Liveness analysisgeneralisationkill             a ← 0       out                             ingen              b ← a + 1  ...
Liveness analysisgeneralisationkill             a ← 0       out                             ingen              b ← a + 1  ...
Liveness analysisgeneralisationkill             a ← 0       out                             ingen              b ← a + 1  ...
Liveness analysisgeneralisationkill             a ← 0       out                             ingen              b ← a + 1  ...
Liveness analysisgen & kill                     gen       killa ← b ⊕ c           {b,c}      {a}a ← b                {b}  ...
Liveness analysisgen & kill                     gen       killa ← b ⊕ c           {b,c}      {a}a ← b                {b}  ...
Liveness analysisgeneralisation            in[n] = gen[n]   ∪ (out[n] - kill[n])                 out[n] =   ∪  s∈succ[n]  ...
coffee break               Dataflow Analysis 17
IIImore analyses                Dataflow Analysis 18
More analysesreaching definitions                       1a ← 5                       2c ← 1                       3 if c >...
Reaching definitionsgen & kill                      gen      killd: a ← b ⊕ c          {d}   defs(a)-{d}d: a ← b          ...
Reaching definitionsgen & kill                      gen      killd: a ← b ⊕ c          {d}   defs(a)-{d}d: a ← b          ...
Reaching definitionsformalisation                in[n] =   ∪                          p∈pred[n]   out[p]            out[n]...
More analysesavailable expressions                    1c ← a + b                    2d ← 1                    3e ← a + b  ...
Available expressionsgen & kill                          gen               killd: a ← b ⊕ c          {b⊕c}-kill          e...
Available expressionsgen & kill                          gen               killd: a ← b ⊕ c          {b⊕c}-kill          e...
Available expressionsformalisation                in[n] =   ∩                          p∈pred[n]   out[p]            out[n...
IVoptimisations                Term Rewriting 25
Dead code eliminationexamplea ← 0              a ← 0b ← a + 1          b ← a + 1c ← c + b          c ← c + ba ← 2 * b     ...
Constant propagationexamplea ← 0             a ← 0b ← a + 1         b ← 0 + 1c ← c + b         c ← c + ba ← 2 * b         ...
Copy propagationexamplea ← e              a ← eb ← a + 1          b ← e + 1c ← c + b          c ← c + ba ← 2 * b          ...
Common subexpression eliminationexamplec ← a + b         x ← a + bd ← 1             c ← xe ← a + b         d ← 1          ...
Vsummary          Term Rewriting 30
Summarylessons learnedliveness analysis  •   intermediate language  •   control-flow graphs  •   definition & algorithmmor...
Literaturelearn moreAndrew W. Appel, Jens Palsberg: Modern CompilerImplementation in Java, 2nd edition. 2002              ...
copyrights & credits                       Dataflow Analysis 33
Dataflow Analysis 34
PicturescopyrightsSlide 1:   ink swirl by Graham Richardson, some rights reservedSlide 25:   Seattles Best Coffee by Domin...
Upcoming SlideShare
Loading in …5
×

Compiling Imperative and Object-Oriented Languages - Dataflow Analysis

1,909 views

Published on

Presentation slides for lecture 8 of course IN4303 on Compiler Construction at TU Delft.

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,909
On SlideShare
0
From Embeds
0
Number of Embeds
875
Actions
Shares
0
Downloads
54
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • round-up on every lecture\n\nwhat to take with you\n\ncheck yourself, pre- and post-paration\n
  • \n
  • \n
  • Compiling Imperative and Object-Oriented Languages - Dataflow Analysis

    1. 1. Dataflow AnalysisGuido Wachsmuth Delft Course IN4303, 2012/13 University of Technology Compiler Construction Challenge the future
    2. 2. Overviewtoday’s lecturecontrol flow graphs Dataflow Analysis 2
    3. 3. Overviewtoday’s lecturecontrol flow graphsdata flow analyses • liveness analysis • reaching definitions • available expressions Dataflow Analysis 2
    4. 4. Overviewtoday’s lecturecontrol flow graphsdata flow analyses • liveness analysis • reaching definitions • available expressionsnon-local optimisations • dead code elimination • constant & copy propagation • common subexpression elimination Dataflow Analysis 2
    5. 5. Icontrol-flow graphs Dataflow Analysis 3
    6. 6. Intermediate languagequadruplesstore jumps a ← b ⊕ c L: a ← b goto L if a ⊗ bmemory access goto L1 a ← M[b] else M[a] ← b goto L2functions f(a1, …, an) b ← f(a1, …, an) Dataflow Analysis 4
    7. 7. Control-flow graphsexample a ← 0L1: b ← a + 1 c ← c + b a ← 2 * b if a < N goto L1 else goto L2L2: return c Dataflow Analysis 5
    8. 8. Control-flow graphsexample a ← 0 a ← 0L1: b ← a + 1 b ← a + 1 c ← c + b a ← 2 * b c ← c + b if a < N goto L1 a ← 2 * b else if a < N goto L2L2: return c return c Dataflow Analysis 5
    9. 9. Control-flow graphsterminology a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 6
    10. 10. Control-flow graphsterminology a ← 0node b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 6
    11. 11. Control-flow graphsterminology a ← 0 in-edgenode b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 6
    12. 12. Control-flow graphsterminology a ← 0 in-edgenode b ← a + 1 c ← c + b in-edge a ← 2 * b if a < N return c Dataflow Analysis 6
    13. 13. Control-flow graphsterminologypredecessor a ← 0 in-edgenode b ← a + 1 c ← c + b in-edge a ← 2 * bpredecessor if a < N return c Dataflow Analysis 6
    14. 14. Control-flow graphsterminologypredecessor a ← 0 in-edgenode b ← a + 1 out-edge c ← c + b in-edge a ← 2 * bpredecessor if a < N return c Dataflow Analysis 6
    15. 15. Control-flow graphsterminologypredecessor a ← 0 in-edgenode b ← a + 1 out-edgesuccessor c ← c + b in-edge a ← 2 * bpredecessor if a < N return c Dataflow Analysis 6
    16. 16. IIliveness analysis Dataflow Analysis 7
    17. 17. Liveness analysisterminology a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 8
    18. 18. Liveness analysisterminologydefinition a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 8
    19. 19. Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 8
    20. 20. Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + bdefinition a ← 2 * b if a < N return c Dataflow Analysis 8
    21. 21. Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + bdefinition a ← 2 * busage if a < N return c Dataflow Analysis 8
    22. 22. Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + bdefinition a ← 2 * b live-inusage if a < N return c Dataflow Analysis 8
    23. 23. Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + bdefinition a ← 2 * b live-out live-inusage if a < N return c Dataflow Analysis 8
    24. 24. Liveness analysisterminologydefinition a ← 0 live-inusage b ← a + 1 c ← c + bdefinition a ← 2 * b live-out live-inusage if a < N return c Dataflow Analysis 8
    25. 25. Liveness analysisterminologydefinition a ← 0 live-inusage b ← a + 1 c ← c + bdefinition a ← 2 * b live-out live-inusage if a < N live-out return c Dataflow Analysis 8
    26. 26. Liveness analysisterminologydefinition a ← 0 live-out live-inusage b ← a + 1 c ← c + bdefinition a ← 2 * b live-out live-inusage if a < N live-out return c Dataflow Analysis 8
    27. 27. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    28. 28. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    29. 29. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    30. 30. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    31. 31. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    32. 32. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    33. 33. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    34. 34. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    35. 35. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    36. 36. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    37. 37. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    38. 38. Liveness analysisformalisation in[n] = use[n] ∪ (out[n] - def[n]) out[n] = ∪ s∈succ[n] in[s]1. If a variable is used at node n, then it is live-in at n.2. If a variable is live-out but not defined at node n, it is live-in at n.3. If a variable is live-in at node n, then it is live-out at its predecessors. Dataflow Analysis 10
    39. 39. Liveness analysisalgorithmfor each n in[n] ← {} ; out[n] ← {}repeat for each n in[n] = in[n] ; out[n] = out[n] in[n] = use[n] ∪ (out[n] - def[n]) for each s in succ[n] out[n] = out[n] ∪ in[s]until for all n: in[n] = in[n] and out[n] = out[n] Dataflow Analysis 11
    40. 40. Liveness analysisexample1a ← 0 1 2 3 4 5 6 7 u d i o i o i o i o i o i o i o2b ← a + 1 1 a a a ac c ac c ac c ac3c ← c + b 2 a b a a bc ac bc ac bc ac bc ac bc ac bc 3 bc c bc bc b bc b bc b bc b bc bc bc bc4a ← 2 * b 4 b a b b a b a b ac bc ac bc ac bc ac5 if a < N 5 a a a a ac ac ac ac ac ac ac ac ac ac ac 6 c c c c c c c c6 return c Dataflow Analysis 12
    41. 41. Liveness analysisexample1a ← 0 1 2 3 4 5 6 7 u d i o i o i o i o i o i o i o2b ← a + 1 1 a a a ac c ac c ac c ac3c ← c + b 2 a b a a bc ac bc ac bc ac bc ac bc ac bc 3 bc c bc bc b bc b bc b bc b bc bc bc bc4a ← 2 * b 4 b a b b a b a b ac bc ac bc ac bc ac5 if a < N 5 a a a a ac ac ac ac ac ac ac ac ac ac ac 6 c c c c c c c c6 return c Dataflow Analysis 12
    42. 42. Liveness analysisexample1a ← 0 1 2 3 4 5 6 7 u d i o i o i o i o i o i o i o2b ← a + 1 1 a a a ac c ac c ac c ac3c ← c + b 2 a b a a bc ac bc ac bc ac bc ac bc ac bc 3 bc c bc bc b bc b bc b bc b bc bc bc bc4a ← 2 * b 4 b a b b a b a b ac bc ac bc ac bc ac5 if a < N 5 a a a a ac ac ac ac ac ac ac ac ac ac ac 6 c c c c c c c c6 return c Dataflow Analysis 12
    43. 43. Liveness analysisexample1a ← 0 1 2 3 4 5 6 7 u d i o i o i o i o i o i o i o2b ← a + 1 1 a a a ac c ac c ac c ac3c ← c + b 2 a b a a bc ac bc ac bc ac bc ac bc ac bc 3 bc c bc bc b bc b bc b bc b bc bc bc bc4a ← 2 * b 4 b a b b a b a b ac bc ac bc ac bc ac5 if a < N 5 a a a a ac ac ac ac ac ac ac ac ac ac ac 6 c c c c c c c c6 return c Dataflow Analysis 12
    44. 44. Liveness analysisexample1a ← 0 1 2 3 4 5 6 7 u d i o i o i o i o i o i o i o2b ← a + 1 1 a a a ac c ac c ac c ac3c ← c + b 2 a b a a bc ac bc ac bc ac bc ac bc ac bc 3 bc c bc bc b bc b bc b bc b bc bc bc bc4a ← 2 * b 4 b a b b a b a b ac bc ac bc ac bc ac5 if a < N 5 a a a a ac ac ac ac ac ac ac ac ac ac ac 6 c c c c c c c c6 return c Dataflow Analysis 12
    45. 45. Liveness analysisexample1a ← 0 1 2 3 4 5 6 7 u d i o i o i o i o i o i o i o2b ← a + 1 1 a a a ac c ac c ac c ac3c ← c + b 2 a b a a bc ac bc ac bc ac bc ac bc ac bc 3 bc c bc bc b bc b bc b bc b bc bc bc bc4a ← 2 * b 4 b a b b a b a b ac bc ac bc ac bc ac5 if a < N 5 a a a a ac ac ac ac ac ac ac ac ac ac ac 6 c c c c c c c c6 return c Dataflow Analysis 12
    46. 46. Liveness analysisexample1a ← 0 1 2 3 4 5 6 7 u d i o i o i o i o i o i o i o2b ← a + 1 1 a a a ac c ac c ac c ac3c ← c + b 2 a b a a bc ac bc ac bc ac bc ac bc ac bc 3 bc c bc bc b bc b bc b bc b bc bc bc bc4a ← 2 * b 4 b a b b a b a b ac bc ac bc ac bc ac5 if a < N 5 a a a a ac ac ac ac ac ac ac ac ac ac ac 6 c c c c c c c c6 return c Dataflow Analysis 12
    47. 47. Liveness analysisexample1a ← 0 1 2 3 4 5 6 7 u d i o i o i o i o i o i o i o2b ← a + 1 1 a a a ac c ac c ac c ac3c ← c + b 2 a b a a bc ac bc ac bc ac bc ac bc ac bc 3 bc c bc bc b bc b bc b bc b bc bc bc bc4a ← 2 * b 4 b a b b a b a b ac bc ac bc ac bc ac5 if a < N 5 a a a a ac ac ac ac ac ac ac ac ac ac ac 6 c c c c c c c c6 return c Dataflow Analysis 12
    48. 48. Liveness analysisexample1a ← 0 1 2 3 4 5 6 7 u d i o i o i o i o i o i o i o2b ← a + 1 1 a a a ac c ac c ac c ac3c ← c + b 2 a b a a bc ac bc ac bc ac bc ac bc ac bc 3 bc c bc bc b bc b bc b bc b bc bc bc bc4a ← 2 * b 4 b a b b a b a b ac bc ac bc ac bc ac5 if a < N 5 a a a a ac ac ac ac ac ac ac ac ac ac ac 6 c c c c c c c c6 return c Dataflow Analysis 12
    49. 49. Liveness analysisoptimisation1a ← 0 1 2 3 u d o i o i o i2b ← a + 1 6 c c c c3c ← c + b 5 a c ac ac ac ac ac 4 b a ac bc ac bc ac bc4a ← 2 * b 3 bc c bc bc bc bc bc bc5 if a < N 2 a b bc ac bc ac bc ac 1 a ac a ac c ac c6 return c Dataflow Analysis 13
    50. 50. Liveness analysisoptimisation1a ← 0 1 2 3 u d o i o i o i2b ← a + 1 6 c c c c3c ← c + b 5 a c ac ac ac ac ac 4 b a ac bc ac bc ac bc4a ← 2 * b 3 bc c bc bc bc bc bc bc5 if a < N 2 a b bc ac bc ac bc ac 1 a ac a ac c ac c6 return c Dataflow Analysis 13
    51. 51. Liveness analysisoptimisation1a ← 0 1 2 3 u d o i o i o i2b ← a + 1 6 c c c c3c ← c + b 5 a c ac ac ac ac ac 4 b a ac bc ac bc ac bc4a ← 2 * b 3 bc c bc bc bc bc bc bc5 if a < N 2 a b bc ac bc ac bc ac 1 a ac a ac c ac c6 return c Dataflow Analysis 13
    52. 52. Liveness analysisoptimisation1a ← 0 1 2 3 u d o i o i o i2b ← a + 1 6 c c c c3c ← c + b 5 a c ac ac ac ac ac 4 b a ac bc ac bc ac bc4a ← 2 * b 3 bc c bc bc bc bc bc bc5 if a < N 2 a b bc ac bc ac bc ac 1 a ac a ac c ac c6 return c Dataflow Analysis 13
    53. 53. Liveness analysisoptimisation1a ← 0 1 2 3 u d o i o i o i2b ← a + 1 6 c c c c3c ← c + b 5 a c ac ac ac ac ac 4 b a ac bc ac bc ac bc4a ← 2 * b 3 bc c bc bc bc bc bc bc5 if a < N 2 a b bc ac bc ac bc ac 1 a ac a ac c ac c6 return c Dataflow Analysis 13
    54. 54. Liveness analysisgeneralisation a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 14
    55. 55. Liveness analysisgeneralisationkill a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 14
    56. 56. Liveness analysisgeneralisationkill a ← 0gen b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 14
    57. 57. Liveness analysisgeneralisationkill a ← 0gen b ← a + 1 c ← c + bkill a ← 2 * b if a < N return c Dataflow Analysis 14
    58. 58. Liveness analysisgeneralisationkill a ← 0gen b ← a + 1 c ← c + bkill a ← 2 * bgen if a < N return c Dataflow Analysis 14
    59. 59. Liveness analysisgeneralisationkill a ← 0 ingen b ← a + 1 c ← c + bkill a ← 2 * bgen if a < N return c Dataflow Analysis 14
    60. 60. Liveness analysisgeneralisationkill a ← 0 out ingen b ← a + 1 c ← c + bkill a ← 2 * bgen if a < N return c Dataflow Analysis 14
    61. 61. Liveness analysisgeneralisationkill a ← 0 out ingen b ← a + 1 c ← c + bkill a ← 2 * bgen if a < N out return c Dataflow Analysis 14
    62. 62. Liveness analysisgeneralisationkill a ← 0 out ingen b ← a + 1 c ← c + bkill a ← 2 * b ingen if a < N out return c Dataflow Analysis 14
    63. 63. Liveness analysisgeneralisationkill a ← 0 out ingen b ← a + 1 c ← c + bkill a ← 2 * b out ingen if a < N out return c Dataflow Analysis 14
    64. 64. Liveness analysisgen & kill gen killa ← b ⊕ c {b,c} {a}a ← b {b} {a}a ← M[b] {b} {a}M[a] ← b {a,b}f(a1, …, an) {a1,…,an}a ← f(a1, …, an) {a1,…,an} {a}goto Lif a ⊗ b {a,b} Dataflow Analysis 15
    65. 65. Liveness analysisgen & kill gen killa ← b ⊕ c {b,c} {a}a ← b {b} {a}a ← M[b] {b} {a}M[a] ← b {a,b}f(a1, …, an) {a1,…,an}a ← f(a1, …, an) {a1,…,an} {a}goto Lif a ⊗ b {a,b} Dataflow Analysis 15
    66. 66. Liveness analysisgeneralisation in[n] = gen[n] ∪ (out[n] - kill[n]) out[n] = ∪ s∈succ[n] in[s] Dataflow Analysis 16
    67. 67. coffee break Dataflow Analysis 17
    68. 68. IIImore analyses Dataflow Analysis 18
    69. 69. More analysesreaching definitions 1a ← 5 2c ← 1 3 if c > a 4c ← c + c 5 goto 3 6a ← c - a Dataflow Analysis 19
    70. 70. Reaching definitionsgen & kill gen killd: a ← b ⊕ c {d} defs(a)-{d}d: a ← b {d} defs(a)-{d}d: a ← M[b] {d} defs(a)-{d}M[a] ← bf(a1, …, an)d: a ← f(a1, …, an) {d} defs(a)-{d}goto Lif a ⊗ b Dataflow Analysis 20
    71. 71. Reaching definitionsgen & kill gen killd: a ← b ⊕ c {d} defs(a)-{d}d: a ← b {d} defs(a)-{d}d: a ← M[b] {d} defs(a)-{d}M[a] ← bf(a1, …, an)d: a ← f(a1, …, an) {d} defs(a)-{d}goto Lif a ⊗ b Dataflow Analysis 20
    72. 72. Reaching definitionsformalisation in[n] = ∪ p∈pred[n] out[p] out[n] = gen[n] ∪ (in[n] - kill[n]) Dataflow Analysis 21
    73. 73. More analysesavailable expressions 1c ← a + b 2d ← 1 3e ← a + b Dataflow Analysis 22
    74. 74. Available expressionsgen & kill gen killd: a ← b ⊕ c {b⊕c}-kill exps(a)d: a ← bd: a ← M[b] {M[b]}-kill exps(a)M[a] ← b exps(M[_])f(a1, …, an) exps(M[_])d: a ← f(a1, …, an) exps(M[_]) ∪ exps(a)goto Lif a ⊗ b Dataflow Analysis 23
    75. 75. Available expressionsgen & kill gen killd: a ← b ⊕ c {b⊕c}-kill exps(a)d: a ← bd: a ← M[b] {M[b]}-kill exps(a)M[a] ← b exps(M[_])f(a1, …, an) exps(M[_])d: a ← f(a1, …, an) exps(M[_]) ∪ exps(a)goto Lif a ⊗ b Dataflow Analysis 23
    76. 76. Available expressionsformalisation in[n] = ∩ p∈pred[n] out[p] out[n] = gen[n] ∪ (in[n] - kill[n]) Dataflow Analysis 24
    77. 77. IVoptimisations Term Rewriting 25
    78. 78. Dead code eliminationexamplea ← 0 a ← 0b ← a + 1 b ← a + 1c ← c + b c ← c + ba ← 2 * b return creturn c Dataflow Analysis 26
    79. 79. Constant propagationexamplea ← 0 a ← 0b ← a + 1 b ← 0 + 1c ← c + b c ← c + ba ← 2 * b a ← 2 * breturn c return c Dataflow Analysis 27
    80. 80. Copy propagationexamplea ← e a ← eb ← a + 1 b ← e + 1c ← c + b c ← c + ba ← 2 * b a ← 2 * breturn c return c Dataflow Analysis 28
    81. 81. Common subexpression eliminationexamplec ← a + b x ← a + bd ← 1 c ← xe ← a + b d ← 1 e ← x Dataflow Analysis 29
    82. 82. Vsummary Term Rewriting 30
    83. 83. Summarylessons learnedliveness analysis • intermediate language • control-flow graphs • definition & algorithmmore dataflow analyses • reaching definitions • available expressionsoptimisations Dataflow Analysis 31
    84. 84. Literaturelearn moreAndrew W. Appel, Jens Palsberg: Modern CompilerImplementation in Java, 2nd edition. 2002 Dataflow Analysis 32
    85. 85. copyrights & credits Dataflow Analysis 33
    86. 86. Dataflow Analysis 34
    87. 87. PicturescopyrightsSlide 1: ink swirl by Graham Richardson, some rights reservedSlide 25: Seattles Best Coffee by Dominica Williamson, some rights reservedSlide 32: Animal Ark Reno by Joel Riley, some rights reserved Dataflow Analysis 35

    ×