forked from soot-oss/soot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcourse.tex
More file actions
1009 lines (905 loc) · 27.9 KB
/
course.tex
File metadata and controls
1009 lines (905 loc) · 27.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%http://freshmeat.net/articles/view/667/
\documentclass[mcgill,slideColor,colorBG,pdf]{prosper}
\usepackage{epsfig, pstricks, graphicx, alltt, fancyvrb, qtree, ulem}
\title{Optimizing Compilers with the Soot - Eclipse Plugin}
\author{Jennifer Lhot\'ak\\
Sable Research Group\\
McGill University\\
\ \\
\ \\
\ \\
This work is sponsored in part by an IBM Eclipse Innovation Grant and NSERC.\\
}
\begin{document}
\maketitle
\begin{slide} {Introduction}
\begin{itemize}
\item Topic: introduction to optimizing compilers
\item Target: people with some knowledge of compilers
\end{itemize}
\end{slide}
\begin{slide} {Outline}
\begin{itemize}
\item Introduction
\item Structure
\item Intermediate Representations
\item Control Flow Analysis
\item Data Flow Analysis
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Components}
\epsfig{file=components.eps,width=4.3in}
\end{slide}
\begin{slide} {Introduction - Virtual Machines}
\begin{itemize}
\item AOT (ahead-of-time) - can take a long time
\item JIT (just-in-time) - has to be fast
\item JITopt - compiler is slower but code is faster
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Effective Optimizations}
\begin{itemize}
\item apply optimization to correct IR
\item design analysis to statically determine program properties
\item use analysis results to perform transformation
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Where To Optimize?}
\epsfig{file=where_to_opt.eps,width=4.3in}
\end{slide}
\begin{slide} {Introduction - Performance}
\begin{itemize}
\item should work across many programs
\item should affect key parts of the program
\item should use "efficient-enough" algorithms
\end{itemize}
90\% of time spent in inner loops or commonly called methods!
\end{slide}
\begin{slide} {Introduction - Loop Invariant Exercise}
\begin{itemize}
\item switch To Eclipse
\item run Soot on the \texttt{LoopInvariant.java} sample code
\begin{itemize}
\item right click on file in Package Explorer
\item select Soot - Process Source File - Run Soot ...
\end{itemize}
\item enable the Loop Invariant Tagger
\begin{itemize}
\item select Phase Options - Jimple Annotation Pack - Loop Invariant Tagger
\item check the Enable box
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Exercise Notes}
\begin{itemize}
\item the loop invariant expression $y = x + z$ is highlighted in red
\item mouse over to see the "is loop invariant" tooltip
\item notice the expression $y = x + i$ is not highlighted (depends on loop index)
\item the constant $j = 1$ is loop invariant
\item the expression $m = j + 1$ which uses a constant and a constant local is loop invariant
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Loop Invariants}
An expression is loop invariant if:
\begin{itemize}
\item operands are all constants
\item or operands have reaching definitions only from outside the loop body
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Forwards/Backwards Analyses}
Forwards Analysis:
\begin{itemize}
\item use information about the past
\item propagate information forward
\end{itemize}
Backwards Analysis:
\begin{itemize}
\item use information about the future
\item propagate information backwards
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Reaching Defs Exercise}
\begin{itemize}
\item switch To Eclipse
\item run Soot on the \texttt{ReachingDefs.java} sample code
\begin{itemize}
\item right click on file in Package Explorer
\item select Soot - Process Source File - Run Soot ...
\end{itemize}
\item enable the Reaching Defs Tagger
\begin{itemize}
\item select Phase Options - Jimple Annotation Pack - Reaching Defs Tagger
\item check the Enable box
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Exercise Notes}
\begin{itemize}
\item the expressions $j = x + y$, $x = x + 1$ and $z = x + 2$ have SootAttribute icons
\epsfig{file=sa.eps,width=3mm}
in margin
\item mouse over the statements to see the list of reaching defs
\item click icon to get list of reaching defs
\item select reaching def from list to navigate to definition statement
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Reaching Defs}
A definition statement is a reaching definition if it could be the most recent definition of some variable used in the current statement % need better reaching def definition
\begin{itemize}
\item forwards analysis
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Live Variables Exercise}
\begin{itemize}
\item switch To Eclipse
\item run Soot on the \texttt{Liveness.java} sample code
\begin{itemize}
\item right click on file in Package Explorer
\item select Soot - Process Source File - Run Soot ...
\end{itemize}
\item enable the Live Variables Tagger
\begin{itemize}
\item select Phase Options - Jimple Annotation Pack - Live Variables Tagger
\item check the Enable box
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Exercise Notes}
\begin{itemize}
\item the variables highlighted in green are live at that point in the program
\item mouse over the statements to see the live variable tooltips
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Live Variable}
A variable is live if at some point in the future the variable will be used again % need better definition
\begin{itemize}
\item backwards analysis
\item sample use: register allocation
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Live Variables Exercise 2}
\begin{itemize}
\item switch To Eclipse
\item run Soot on the \texttt{LiveInteractive.java} sample code
\begin{itemize}
\item right click on file in Package Explorer
\item select Soot - Process Source File - Run Soot ...
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Live Variables Excercise 2 cont.}
\begin{itemize}
\item enable the Interaction and the Live Variables Tagger
\begin{itemize}
\item under General Options check Interactive Mode
\item select Output Options - under Output Options select Jimple
\item select Phase Options - Jimple Annotation Pack - Live Variables Tagger
\item check the Enable box
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Introduction - Exercise Notes}
\begin{itemize}
\item the control flow graph is displayed in the editor
\item use the Step Forward button
\epsfig{file=step_forward.eps,width=3mm}
to see the analysis as it is being generated
\end{itemize}
\end{slide}
\begin{slide} {Structure of Compilers - Frontend}
\epsfig{file=frontend_struc.eps,width=4in}
\end{slide}
\begin{slide} {Structure of Compilers - Backend}
\epsfig{file=backend_struc.eps,width=4in}
\end{slide}
\begin{slide} {Structure of Compilers - Java Systems}
\epsfig{file=java_struc.eps,width=4in}
\end{slide}
\begin{slide} {Intermediate Representations}
$$n\left\{ \begin{array}{c} Pascal\\C\\Fortran\\Java\\ML\\Scheme\\AspectJ \end{array} \rightarrow \fbox{IR} \rightarrow \begin{array}{c} x86\\Sparc\\Mips\\68000\\Cray\\Smp \end{array}\right\} m$$
\end{slide}
\begin{slide} {Intermediate Representations - Need Many}
\begin{itemize}
\item need enough info about high-level program (ex: types)
\item need to be close enough to low-level architecture (for low-level optimization opportunities)
\item use family of IRs (high, medium, low)
\end{itemize}
\end{slide}
\begin{slide} {Intermediate Representations - Design}
\begin{itemize}
\item designing IRs is an art
\item dependent on kinds of optimizations
\item some optimizations are difficult on some IRs
\begin{itemize}
\item ex: transformations difficult on stack-based IR
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Intermediate Representations - High-Level}
\begin{itemize}
\item used in early stages (front-end)
\item AST based or graph based (ex: Grimp in Soot)
\epsfig{file=highlevel.eps,width=4in}
\end{itemize}
\end{slide}
\begin{slide} {Intermediate Representations - Grimp Exercise}
\begin{itemize}
\item switch to Eclipse
\item run Soot on the \texttt{GrimpExample.java} sample code
\begin{itemize}
\item right click on file in Package Explorer
\item select Soot - Process Source File - Create Grimp ...
\end{itemize}
\item view generated Grimp
\begin{itemize}
\item expand sootOutput folder
\item open \texttt{GrimpExample.grimp} file
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Intermediate Representations - Exercise Notes}
\begin{itemize}
\item notice the high-level expression in the if statement $if$ $x <a[\$i0]$
\item notice the expression $x = x + a[4] + 2$ is all in one statement
\end{itemize}
\end{slide}
\begin{slide} {Intermediate Representations - Medium-Level}
\begin{itemize}
\item somewhat language independent
\item represent variables from program and temps
\item only 1 operation per statement
\item most analyses and transformations are at this level
\end{itemize}
\end{slide}
\begin{slide} {Intermediate Representations - Jimple Exercise}
\begin{itemize}
\item switch to Eclipse
\item run Soot on the \texttt{JimpleExample.java} sample code
\begin{itemize}
\item right click on file in Package Explorer
\item select Soot - Process Source File - Create Jimple ...
\end{itemize}
\item view generated Jimple
\begin{itemize}
\item expand sootOutput folder
\item open \texttt{JimpleExample.jimple} file
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Intermediate Representations - Exercise Notes}
\begin{itemize}
\item notice the expression in the if statement $if$ $x < \$i3$ and the array access is done in a previous statement
\item notice the expression $x = x + a[4] + 2$ has been split into three statements
\end{itemize}
\end{slide}
\begin{slide} {Intermediate Representations - Low-Level}
\begin{itemize}
\item correspond closely to target machine
\item allow maximum low-level optimizations
\item make machine resources explicit
\begin{itemize}
\item registers
\item delay slots
\item instructions
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Control Flow Analysis - Find Control Structure}
\begin{enumerate}
\item basic blocks (straight line code)
\begin{itemize}
\item good for local analysis
\end{itemize}
\item extended basic blocks
\begin{itemize}
\item good for instruction scheduling
\end{itemize}
\item loops/recursive cycles
\begin{itemize}
\item important for optimizations
\end{itemize}
\item reducible (well-structured)
\begin{itemize}
\item inexpensive analysis
\end{itemize}
\end{enumerate}
\end{slide}
\begin{slide} {Control Flow Analysis - 3-addr Statements}
\begin{itemize}
\item Simple:
\begin{itemize}
\item x $\leftarrow$ y op z
\item x $\leftarrow$ op y
\item x $\leftarrow$ y
\item goto L
\item if x relop y goto L
\end{itemize}
\item Procedure Calls:
\begin{itemize}
\item foo(a, z)
\item x $\leftarrow$ foo(a, z)
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Control Flow Analysis - 3-addr Statements}
\begin{itemize}
\item Pointers:
\begin{itemize}
\item x $\leftarrow$ addr y
\item x $\leftarrow$ * y
\item * y $\leftarrow$ x
\item x $\leftarrow$ * y.f
\item * y.f $\leftarrow$ x
\end{itemize}
\item Arrays:
\begin{itemize}
\item x $\leftarrow$ a[i]
\item a[i] $\leftarrow$ x
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Control Flow Analysis - Finding Basic Blocks}
\begin{enumerate}
\item Determine set of leaders (first statement of basic block)
\begin{itemize}
\item first statement of sequence
\item statements that are targets of gotos
\item statements following gotos
\end{itemize}
\item for each leader its basic block consists of the leader and all statements up to the next leader
\item add control flow edges between basic blocks
\end{enumerate}
\end{slide}
\begin{slide} {Control Flow Analysis - Finding BB Example}
\begin{tiny}
$$
\begin{array}{c}
a \leftarrow 1\\
b \leftarrow 3\\
d \leftarrow a + b\\
L1: if a < b goto L2\\
a \leftarrow a - 1\\
goto L3\\
L2: a \leftarrow a + 1\\
b \leftarrow b - 1\\
if a < b goto L2\\
L3: d \leftarrow a + b\\
if d > 0 goto L1\\
print (d)
\end{array}
$$
\end{tiny}
\end{slide}
\begin{slide} {Control Flow Analysis - Leaders - First Statement}
\begin{tiny}
$$
\begin{array}{c}
{\red \fbox{a $\leftarrow$ 1}}\\
b \leftarrow 3\\
d \leftarrow a + b\\
L1: if a < b goto L2\\
a \leftarrow a - 1\\
goto L3\\
L2: a \leftarrow a + 1\\
b \leftarrow b - 1\\
if a < b goto L2\\
L3: d \leftarrow a + b\\
if d > 0 goto L1\\
print (d)
\end{array}
$$
\end{tiny}
\end{slide}
\begin{slide} {Control Flow Analysis - Leaders - Goto Targets}
\begin{tiny}
$$
\begin{array}{c}
{\blue \fbox{a $\leftarrow$ 1}}\\
b \leftarrow 3\\
d \leftarrow a + b\\
{\red \fbox{L1: if a < b goto L2}}\\
a \leftarrow a - 1\\
goto L3\\
{\red \fbox{L2: a $\leftarrow$ a + 1}}\\
b \leftarrow b - 1\\
if a < b goto L2\\
{\red \fbox{L3: d $\leftarrow$ a + b}}\\
if d > 0 goto L1\\
print (d)
\end{array}
$$
\end{tiny}
\end{slide}
\begin{slide} {Control Flow Analysis - Leaders - Follow Gotos}
\begin{tiny}
$$
\begin{array}{c}
{\blue \fbox{a $\leftarrow$ 1}}\\
b \leftarrow 3\\
d \leftarrow a + b\\
{\blue \fbox{L1: if a < b goto L2}}\\
{\red \fbox{a $\leftarrow$ a - 1}}\\
goto L3\\
{\blue \fbox{L2: a $\leftarrow$ a + 1}}\\
b \leftarrow b - 1\\
if a < b goto L2\\
{\blue \fbox{L3: d $\leftarrow$ a + b}}\\
if d > 0 goto L1\\
{\red \fbox{print (d)}}
\end{array}
$$
\end{tiny}
\end{slide}
\begin{slide} {Control Flow Analysis - Find Basic Blocks}
\begin{tiny}
$$
\begin{array}{c}
\fbox{\begin{tabular}{c}{\blue \fbox{a $\leftarrow$ 1}}\\
b $\leftarrow$ 3\\
d $\leftarrow$ a + b \end{tabular}}\\
\fbox{{\blue \fbox{L1: if a < b goto L2}}}\\
\fbox{\begin{tabular}{c}{\blue \fbox{a $\leftarrow$ a - 1}}\\
goto L3 \end{tabular}}\\
\fbox{\begin{tabular}{c}{\blue \fbox{L2: a $\leftarrow$ a + 1}}\\
b $\leftarrow$ b - 1\\
if a < b goto L2 \end{tabular}}\\
\fbox{\begin{tabular}{c}{\blue \fbox{L3: d $\leftarrow$ a + b}}\\
if d > 0 goto L1\end{tabular}}\\
\fbox{{\blue \fbox{print (d)}}}
\end{array}
$$
\end{tiny}
\end{slide}
\begin{slide} {Control Flow Analysis - Add Edges}
\begin{center}
\epsfig{file=basic_blocks.eps,width=4in}
\end{center}
\end{slide}
\begin{slide} {Control Flow Analysis - Irreducible}
A graph is irreducible if it has a pattern like:
\begin{center}
\epsfig{file=irreducible.eps,width=2in}
\end{center}
\end{slide}
\begin{slide} {Control Flow Analysis - Extended Basic Blocks}
Extended basic blocks: a maximal set of instructions beginning with leader that contains no join nodes
\end{slide}
\begin{slide} {Control Flow Analysis - Loops}
\begin{itemize}
\item Explicit Loops (AST)\\
\begin{center}
\textbf{while} (<cond>) <body>
\end{center}
\item Implicit Loops (CFG)
\end{itemize}
\begin{center}
\epsfig{file=implicit_loop.eps,width=2in}
\end{center}
\end{slide}
\begin{slide} {Control Flow Analysis - Loops}
A loop (single entrance, multi-exit) in a cfg is a set of nodes S including a header node h with the following properties
\begin{itemize}
\item from any node in S there is a path of directed edges to h
\item there is a path of directed edges from h to any node in S
\item there is no edge from any node outside S to any node inside S except for h
\end{itemize}
\end{slide}
\begin{slide} {Control Flow Analysis - Loops}
Single entry, multi exit loop:\\
\ \\
\begin{center}
\epsfig{file=loop.eps,width=1.6in}
\end{center}
\end{slide}
\begin{slide} {Control Flow Analysis - Loops}
Loops:
\begin{center}
\epsfig{file=loops.eps,width=1.3in}
\end{center}
Not Loops (irreducible):
\begin{center}
\epsfig{file=not_loops.eps,width=1.3in}
\end{center}
\end{slide}
\begin{slide} {Control Flow Analysis - Reducible Flow Graphs}
\begin{itemize}
\item only get reducible flow graphs in Java and C (without gotos)
\item dataflow analysis can be done on reducible flow graphs
\end{itemize}
\end{slide}
\begin{slide} {Control Flow Analysis - How to Find Loops}
\begin{itemize}
\item need dominators
\begin{center}
\epsfig{file=dominators_simple.eps,height=1in}
\end{center}
\item a node d dominates a node n if every path of directed edges from $S_0$ to n must go through d
\item every node dominates itself
\end{itemize}
\end{slide}
\begin{slide} {Control Flow Analysis - How to Find Loops Cont.}
\begin{center}
\epsfig{file=dominators_complex.eps,height=1.4in}
\end{center}
\begin{itemize}
\item $D(n) = \{n\} U (\cap_{p \in pred[n]} D[p])$
\item solve flow equations - start with full sets
\end{itemize}
\end{slide}
\begin{slide} {Control Flow Analysis - Dominators Exercise}
\begin{itemize}
\item switch to Eclipse
\item run Soot on the \texttt{DominatorExample.java} sample code
\begin{itemize}
\item right click on file in Package Explorer
\item select Soot - Process Source File - Run Soot ...
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Control Flow Analysis - Dominators Exercise}
\begin{itemize}
\item output Jimple
\begin{itemize}
\item select Output Options - Output Format - Jimple File
\end{itemize}
\item enable the Dominators Tagger
\begin{itemize}
\item select Phase Options - Jimple Annotation Pack - Dominators Tagger
\item check the Enable box
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Control Flow Analysis - Exercise Notes}
\begin{itemize}
\item click on the SootAttribute icon
\epsfig{file=sa.eps,width=3mm}
in the margin of a statement to see a list of dominators - select one to navigate to the dominator
\item open the \texttt{sootOutput/DominatorExample.jimple} file to explore the dominators within the Jimple IR
\end{itemize}
\end{slide}
\begin{slide} {Control Flow Analysis - Immediate Dominators}
d dominates n and e dominates n $\Rightarrow$ \\d dominates e or e dominates d\\
\ \\
\begin{center}
\epsfig{file=dominators_imm.eps,height=1.4in}
\end{center}
\end{slide}
\begin{slide} {Control Flow Analysis - Immediate Dominators}
Every node n has no more than one immediate dominator, idom(n) such that:
\begin{enumerate}
\item idom(n) is not the same as n
\item idom(n) dominates n
\item idom(n) does not dominate any other dominator of n
\end{enumerate}
\begin{center}
\epsfig{file=idom.eps,height=1in}
\end{center}
\end{slide}
\begin{slide} {Control Flow Analysis - Natural Loops}
Given an edge n $\rightarrow$ h:
\begin{itemize}
\item h dominates n
\item all x such that h dominates x and $\exists$ a path x $\rightarrow$ n which does not contain h
\begin{center}
\epsfig{file=natural_loops.eps,height=1in}
\end{center}
\item continue is not a natural loop
\end{itemize}
\end{slide}
\begin{slide} {Control Flow Analysis - Dominator Trees}
\begin{itemize}
\item use every node in a cfg
\item add edges for idoms
\begin{center}
\epsfig{file=dom_tree.eps,height=1in}
\end{center}
\item cfg edge from n to h such that h dominates n is a back-edge (5 $\rightarrow$ 1 (1 dominates 5))
\item for each back-edge there is a sub-graph that is a loop
\end{itemize}
\end{slide}
\begin{slide} {Data Flow Analysis}
\begin{itemize}
\item Local: within basic blocks
\item Global/Intra-procedural: on one control flow graph for a method (at call to another method make worst case assumptions)
\item Inter-procedural: hook up all control flow graphs for all methods
\begin{itemize}
\item hard for dynamic linking
\item long / big difficult
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Data Flow Analysis - Steps}
\begin{enumerate}
\item figure out what you're computing
\begin{itemize}
\item ex: set of variables live at each statement
\end{itemize}
\item precisely define what it is you're computing
\begin{itemize}
\item ex: a variable v is live at program point p if $\exists$ a path from p to the end of the program on which there is a use of v
\end{itemize}
\item decide whether the analysis is forwards or backwards
\begin{itemize}
\item ex: backwards
\end{itemize}
\end{enumerate}
\end{slide}
\begin{slide} {Data Flow Analysis - Steps}
\begin{enumerate}
\item[4.] decide which merge operator to use
\begin{itemize}
\item ex: union
\end{itemize}
\item[5.] define flow equations $in(s) = f(out(s))$
\begin{itemize}
\item ex: $S: z = x + y $\\
$defs(S) = \{z\}$ and $uses(S) = \{x, y\}$\\
$in(S) = (out(S) - defs(S)) U uses(S)$\\
$= (out(S) U uses(S)) - defs(S) ->$ is incorrect consider $z = z - 1$
\end{itemize}
\end{enumerate}
\end{slide}
\begin{slide} {Data Flow Analysis - Steps}
\begin{enumerate}
\item[6.] determine value of start or end node
\begin{itemize}
\item ex: \{\}
\end{itemize}
\item[7.] determine approximation to use for all the sets initially
\begin{itemize}
\item ex: \{\} - (least safe)
\end{itemize}
\end{enumerate}
\end{slide}
\begin{slide} {Data Flow Analysis - Standard Approach}
\begin{center}
\epsfig{file=standard.eps,width=4in}
\end{center}
For loop nesting of n - $2^n$ iterations
\end{slide}
\begin{slide} {Data Flow Analysis - Structure Based Approach}
\begin{enumerate}
\item compute gen and kill sets from bottom-up and store in AST
\item proceed top-down
\begin{itemize}
\item compute output for S
\item compute outputs for sub-structures of S
\end{itemize}
\end{enumerate}
\begin{tiny}
\Tree [.while e [.body [.$S_1$ [.if $S_i$ $S_j$ ] ] $S_2$ $S_3$ ] ]
\end{tiny}
\end{slide}
\begin{slide} {Data Flow Analysis - Worklist Method}
\begin{itemize}
\item put all blocks on worklist (order matters)
\item initialize in
\item choose $B_x$ to start
\end{itemize}
\begin{center}
\epsfig{file=worklist.eps,width=1.7in}
\end{center}
\end{slide}
\begin{slide} {Data Flow Analysis - Iterative Method}
Algorithm:
\begin{tiny}
\begin{Verbatim}[commandchars=\\\{\}]
for each block B do {\blue // assume in[B] = \{\}}
out[B] = gen[B]
repeat
change = false
for each block B do {\blue // in depth-first order}
begin
in[B] = \(\cup_{P \in{pred of B}}\) out[P]
oldout = out[B]
out[B] = gen[B] \(\cup\) (in[B] - kill[B])
if out[B] <> oldout then change = true
end
until not change
\end{Verbatim}
\end{tiny}
\end{slide}
\begin{slide} {Data Flow Analysis - Iterative Method}
\begin{center}
\epsfig{file=iterative.eps,height=2.5in}
\end{center}
\end{slide}
\begin{slide} {Data Flow Analysis - One Iteration?}
\begin{center}
\epsfig{file=one_iteration1.eps,height=1.5in}
\end{center}
\ \\
$out(S) = gen(S) \cup (in(S) - kill(S))$\\
$gen(S) = gen(Sthen) \cup gen(Selse)$\\
$kill(S) = kill(Sthen) \cap kill(Selse)$
\end{slide}
\begin{slide} {Data Flow Analysis - One Iteration?}
\begin{center}
\epsfig{file=one_iteration2.eps,height=1.5in}
\end{center}
\ \\
$gen(S) = gen(S2) \cup (gen(S1) - kill(S2))$\\
$kill(S) = kill(S2) \cup (kill(S1) - gen(S2)$
\end{slide}
\begin{slide} {Data Flow Analysis - One Iteration?}
\texttt{while} (e) body
\begin{center}
\epsfig{file=one_iteration3.eps,height=1.5in}
\end{center}
\ \\
$out(S) = in(S) \bowtie gen(Sbody) \cup (in(S) - kill(Sbody))$\\
$gen(S) = gen(Sbody)$\\
$kill(S) = \{\}$
\end{slide}
\begin{slide} {Data Flow Analysis - One Iteration?}
\texttt{do} body \texttt{while} (e)
\begin{center}
\epsfig{file=one_iteration4.eps,height=1.5in}
\end{center}
\ \\
\begin{tiny}
$out(S) = gen(Sbody) \cup (in(S) \cup out(S) - kill(Sbody))$\\
$out(S)_0 = \{\}$\\
$out(S)_1 = gen(Sbody) \cup (in(S) - kill(Sbody)$\\
$out(S)_2 = gen(Sbody) \cup (in(S) \cup [ gen(Sbody) \cup (in(S) - kill(Sbody)] - kill(Sbody)) = gen(Sbody) \cup (in(S) - kill(Sbody))$
\end{tiny}
\end{slide}
\begin{slide} {Data Flow Analysis - CSE Exercise}
\begin{itemize}
\item switch to Eclipse
\item run Soot on the \texttt{CommonSubExp.java} sample code
\begin{itemize}
\item right click on file in Package Explorer
\item select Soot - Process Source File - Run Soot ...
\end{itemize}
\item enable the
\end{itemize}
\end{slide}
\begin{slide} {Data Flow Analysis - Exercise Notes}
\end{slide}
\begin{slide} {Data Flow Analysis - Available Expressions}
\begin{enumerate}
\item computing: availability of expr at each stmt - sets of exprs
\item define: an expr e ("x op y") is available at a program point p if for every path from the start point to p the expr e has been computed and has not been changed by something else (ie: there is no assignment to x or y)
\item direction: forwards
\item merge operator: intersection ($\cap$ - all paths)
\end{enumerate}
\end{slide}
\begin{slide} {Data Flow Analysis - Available Expressions}
\begin{itemize}
\item[5.] flow equations:\\
\begin{center}\epsfig{file=in.eps,height=.6in}\end{center}
\begin{small}
avail: x = y + z\\
not avail: x = x + z\\
$out(S) =$ \sout{$gen(S) \cup (in(S) - kill(S))$} - doesn't work\\
$out(S) = (gen(S) \cup in(S)) - kill(S)$\\
$gen(S) = \{ y + z \}$\\
$kill(S) = \{i$ any expr using x $\}$
\end{small}
\end{itemize}
\end{slide}
\begin{slide} {Data Flow Analysis - Available Expressions}
\begin{itemize}
\item[6.] start sets:\\
$out(start) = \{\}$ $\leftarrow$ most safe\\
$out(Si) = \{$ all exprs $\} $ $\leftarrow$ most unsafe \\
\end{itemize}
\end{slide}
\begin{slide} {Data Flow Analysis - Least Fixed Point}
\begin{center}\epsfig{file=lfp.eps,width=3in}\end{center}
\end{slide}
\begin{slide} {Data Flow Analysis - Avail Expr Exercise}
\begin{itemize}
\item switch to Eclipse
\item run Soot on the \texttt{AvailableExpressions.java} sample code
\begin{itemize}
\item right click on file in Package Explorer
\item select Soot - Process Source File - Run Soot ...
\end{itemize}
\item enable Interaction
\begin{itemize}
\item select General Options - Interactive Mode
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Data Flow Analysis - Avail Expr Exercise}
\begin{itemize}
\item output Jimple
\begin{itemize}
\item select Output Options - Output Format - Jimple File
\end{itemize}
\item enable the Available Expressions Analysis
\begin{itemize}
\item select Phase Options - Jimple Annotation Pack - Available Expressions Tagger
\item check the Enable box
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Data Flow Analysis - Exercise Notes}
\begin{itemize}
\item use the interactive tools to see the generated sets of available expressions
\item notice this is an optimisitic analysis which starts with full sets everywhere
\end{itemize}
\end{slide}
\begin{slide} {Data Flow Analysis - Avail Expr Exercise - 2}
\begin{itemize}
\item switch to Eclipse
\item run Soot on the \texttt{AvailableExpressions.java} sample code
\begin{itemize}
\item right click on file in Package Explorer
\item select Soot - Process Source File - Run Soot ...
\end{itemize}
\item enable Interaction
\begin{itemize}
\item select General Options - Interactive Mode
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Data Flow Analysis - Avail Expr Exercise}
\begin{itemize}
\item output Jimple
\begin{itemize}
\item select Output Options - Output Format - Jimple File
\end{itemize}
\item enable the Available Expressions Analysis
\begin{itemize}
\item select Phase Options - Jimple Annotation Pack - Available Expressions Tagger
\item check the Enable box
\item select Pessimistic for analysis kind
\end{itemize}
\end{itemize}
\end{slide}
\begin{slide} {Data Flow Analysis - Exercise Notes}
\begin{itemize}
\item use the interactive tools to see the generated sets of available expressions
\item notice this is an pessimistic analysis which starts with empty sets everywhere
\item compare this with the optimistic analysis
\end{itemize}
\end{slide}
\begin{slide} {Data Flow Analysis - Very Busy Expressions}
\begin{center}\epsfig{file=verybusy.eps,width=3in}\end{center}
\begin{itemize}
\item $x$ op $y$ is very busy
\end{itemize}
\end{slide}
\begin{slide} {Data Flow Analysis - Very Busy Expressions}
\begin{enumerate}
\item computing: sets of expressions
\item define: an expr e of the form "x op y" is "very busy" at program point p if for all paths from p to the end of the program the expr e is computed and there are no other assignments to x or y from p to the end of the program
\item direction: backwards
\item merge operator: intersection
\end{enumerate}
\end{slide}
\begin{slide} {Data Flow Analysis - Very Busy Expressions}
\begin{itemize}
\item[5.] flow equations:\\
$kill(S) = \{$ any expr involving defs of s$\}$\\
$gen(S) = \{$ x op y$\}$\\
$in(S) = (out(S) - kill(S)) \cup gen(S)$\\
\item[6.] start sets:\\
$in(end) = \{\}$\\
$in(Si) = \{$ all exprs $\}$
\end{itemize}
\end{slide}
\begin{slide} {Data Flow Analysis - Constant Propagation}
Example: \\
\ \\
P1: ((x, 1), (y, ?), (z, 2))\\
P2: ((x, ?), (y, 3), (z, 2))\\
MergePoint: ((x, ?), (y, ?), (z, 2))\\
\end{slide}
\begin{slide} {Data Flow Analysis - Constant Propagation}
\begin{center}\epsfig{file=constant_lattice.eps,width=3in}\end{center}
\ \\
\ \\
\begin{center}
\begin{tabular}[t]{c|c|c|c}
$\bowtie$ & $\bot$ & C1 & $\top$ \\ \hline