Your SlideShare is downloading. ×
Compiling Imperative and Object-Oriented Languages - Dataflow Analysis
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Compiling Imperative and Object-Oriented Languages - Dataflow Analysis

1,027

Published on

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

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,027
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
17
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
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
  • Transcript

    • 1. Dataflow AnalysisGuido Wachsmuth Delft Course IN4303, 2012/13 University of Technology Compiler Construction Challenge the future
    • 2. Overviewtoday’s lecturecontrol flow graphs Dataflow Analysis 2
    • 3. Overviewtoday’s lecturecontrol flow graphsdata flow analyses • liveness analysis • reaching definitions • available expressions Dataflow Analysis 2
    • 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. Icontrol-flow graphs Dataflow Analysis 3
    • 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. 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. 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. Control-flow graphsterminology a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 6
    • 10. Control-flow graphsterminology a ← 0node b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 6
    • 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. 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. 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. 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. 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. IIliveness analysis Dataflow Analysis 7
    • 17. Liveness analysisterminology a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 8
    • 18. Liveness analysisterminologydefinition a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 8
    • 19. Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 8
    • 20. Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + bdefinition a ← 2 * b if a < N return c Dataflow Analysis 8
    • 21. Liveness analysisterminologydefinition a ← 0usage b ← a + 1 c ← c + bdefinition a ← 2 * busage if a < N return c Dataflow Analysis 8
    • 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. 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. 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. 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. 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. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    • 28. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    • 29. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    • 30. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    • 31. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    • 32. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    • 33. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    • 34. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    • 35. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    • 36. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    • 37. Liveness analysisexample a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 9
    • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Liveness analysisgeneralisation a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 14
    • 55. Liveness analysisgeneralisationkill a ← 0 b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 14
    • 56. Liveness analysisgeneralisationkill a ← 0gen b ← a + 1 c ← c + b a ← 2 * b if a < N return c Dataflow Analysis 14
    • 57. Liveness analysisgeneralisationkill a ← 0gen b ← a + 1 c ← c + bkill a ← 2 * b if a < N return c Dataflow Analysis 14
    • 58. Liveness analysisgeneralisationkill a ← 0gen b ← a + 1 c ← c + bkill a ← 2 * bgen if a < N return c Dataflow Analysis 14
    • 59. Liveness analysisgeneralisationkill a ← 0 ingen b ← a + 1 c ← c + bkill a ← 2 * bgen if a < N return c Dataflow Analysis 14
    • 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. 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. 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. 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. 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. 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. Liveness analysisgeneralisation in[n] = gen[n] ∪ (out[n] - kill[n]) out[n] = ∪ s∈succ[n] in[s] Dataflow Analysis 16
    • 67. coffee break Dataflow Analysis 17
    • 68. IIImore analyses Dataflow Analysis 18
    • 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. 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. 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. Reaching definitionsformalisation in[n] = ∪ p∈pred[n] out[p] out[n] = gen[n] ∪ (in[n] - kill[n]) Dataflow Analysis 21
    • 73. More analysesavailable expressions 1c ← a + b 2d ← 1 3e ← a + b Dataflow Analysis 22
    • 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. 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. Available expressionsformalisation in[n] = ∩ p∈pred[n] out[p] out[n] = gen[n] ∪ (in[n] - kill[n]) Dataflow Analysis 24
    • 77. IVoptimisations Term Rewriting 25
    • 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. 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. 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. Common subexpression eliminationexamplec ← a + b x ← a + bd ← 1 c ← xe ← a + b d ← 1 e ← x Dataflow Analysis 29
    • 82. Vsummary Term Rewriting 30
    • 83. Summarylessons learnedliveness analysis • intermediate language • control-flow graphs • definition & algorithmmore dataflow analyses • reaching definitions • available expressionsoptimisations Dataflow Analysis 31
    • 84. Literaturelearn moreAndrew W. Appel, Jens Palsberg: Modern CompilerImplementation in Java, 2nd edition. 2002 Dataflow Analysis 32
    • 85. copyrights & credits Dataflow Analysis 33
    • 86. Dataflow Analysis 34
    • 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

    ×