This repository was archived by the owner on Aug 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtpopt1.html
More file actions
1470 lines (1406 loc) · 57.2 KB
/
Copy pathtpopt1.html
File metadata and controls
1470 lines (1406 loc) · 57.2 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Pragmatic optimization in modern programming</title>
<meta name="description" content="Pragmatic optimization in modern programming">
<meta name="author" content="geek">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, minimal-ui">
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/beige.css" id="theme">
<link rel="stylesheet" href="css/customization.css">
<link rel="stylesheet" href="lib/css/zenburn.css">
<link rel="shortcut icon" href="images/ico/favicon.ico">
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<style type="text/css">
span.opt0 {
color: green;
}
span.opt1 {
color: yellow;
}
span.opt2 {
color: red;
}
</style>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h1>Pragmatic optimization</h1>
<h2>In modern programming</h2>
<h3>Demystifying a compiler</h3>
<p>
<small>Created by
<a href="https://github.com/cuda-geek">Marina (geek) Kolpakova</a>
for
<a href="unn.ru">UNN</a>
/ 2015-2016
</small>
</p>
</section>
<section>
<h2>Course Topics</h2>
<ul>
<li>Ordering optimization approaches</li>
<li><b>Demystifying a compiler</b></li>
<li>Mastering compiler optimizations</li>
<li>Modern computer architectures concepts</li>
</ul>
</section>
<section>
<h2>Outline</h2>
<ul>
<li>Compilation trajectory</li>
<li>Intermediate language</li>
<li>Dealing with local variables</li>
<li>link-time and whole program optimization</li>
<li>Optimization levels</li>
<li>Compiler optimization taxonomies
<ul>
<li>Classic</li>
<li>Scope</li>
<li>Code pattern</li>
</ul>
</li>
<li>How to get the feedback from optimization?</li>
<li>Compiler optimization challenges</li>
<li>Summary</li>
</ul>
</section>
<section>
<h2>Executable generation phases</h2>
<ul>
<li><b>Pre-processing.</b> Pre-process, but don't compile.
<ul style="font-size: 0.9em;">
<li><code class="console">gcc -E test.cc</code></li>
<li><code class="console">cl /E test.cc</code></li>
</ul>
</li>
<li><b>Compilation.</b> Compile, but don't assemble.
<ul style="font-size: 0.9em;">
<li><code class="console">gcc -S test.cc</code></li>
<li><code class="console">cl /FA test.cc</code></li>
</ul>
</li>
<li><b>Assembling.</b> Assemble, but don't link.
<ul style="font-size: 0.9em;">
<li><code class="console">gcc -c test.cc</code></li>
<li><code class="console">cl /c test.cc</code></li>
</ul>
</li>
<li><b>Linking.</b> Link object files to generate the executable.
<ul style="font-size: 0.9em;">
<li><code class="console">gcc test.cc</code></li>
<li><code class="console">cl test.cc</code></li>
</ul>
</li>
</ul>
</section>
<section>
<section>
<h2>Compilation trajectory</h2>
<img alt="compiler phases" class="simple" src="images/popt/compiler_phases.svg">
<dl>
<dt><b>Lexical Analysis</b></dt>
<dd>scans the source code as a stream of characters converting it into lexemes (tokens).</dd>
<dt><b>Syntax Analysis</b></dt>
<dd>takes the tokens, produced by lexical analysis, as input and generates a syntax tree.
Source code grammar (syntactical correctness) is checked here.</dd>
</dl>
</section>
<section>
<h2>Compilation trajectory</h2>
<img alt="compiler phases" class="simple" src="images/popt/compiler_phases.svg">
<dl>
<dt><b>Semantic Analysis</b></dt>
<dd>checks whether the constructed syntax tree follows the language rules (including the type checking).</dd>
<dt><b>Intermediate Code Generation</b></dt>
<dd> builds a program representation for some abstract machine. It is in between the high-level language and the
target machine language.</dd>
</dl>
</section>
<section>
<h2>Compilation trajectory</h2>
<img alt="compiler phases" class="simple" src="images/popt/compiler_phases.svg">
<dl>
<dt><b>Code Optimization</b></dt>
<dd>does optimization of the intermediate code (eg, redundancy elimination).</dd>
<dt><b>Code Generation</b></dt>
<dd>takes an optimized representation of the intermediate code and maps it to the target machine language.</dd>
</dl>
</section>
</section>
<section>
<h2>Frontend and backend</h2>
<img alt="frontend and backend" src="images/popt/front-back.svg" class="simple">
<ul>
<li>Only a <b>backend</b> is required for <b>new machine</b> support</li>
<li>Only a <b>frontend</b> is required for <b>new language</b> support</li>
<li>Most of optimizations resemble each other for all targets and could be applied in between frontend and backend</li>
</ul>
</section>
<section>
<section>
<h2>Intermediate language</h2>
<img alt="intermediate language" src="images/popt/intermediate_code.svg" class="simple">
<p>Optimization techniques become much easier to conduct on the level of intermediate code.
Modern compilers usually use 2 levels of intermediate representation (IR).</p>
</section>
<section>
<h2>Intermediate language</h2>
<dl>
<dt><b>High Level IR</b></dt>
<dd>is close to the source and can be easily generated from the source code. Some code optimizations
are possible. It is not very suitable for target machine optimization.</dd>
<dt><b>Low Level IR</b></dt>
<dd>is close to the target machine and used for machine-dependent optimizations: register allocation,
instruction selection, peephole optimization.</dd>
</dl>
</section>
<!-- <section>
<h2>S<small>ingle</small> S<small>static</small> A<small>assignmet</small> form</h2>
<p>Polynomial complexity of register allocation, simplified algorithm for spilling.</p>
<p>add more from paper "survey on register allocation"</p>
</section> -->
<section>
<h2>Intermediate language</h2>
<ul>
<li><b>Language-specific</b> to be used for JIT compilation later:
<ul>
<li>Java byte code; .NET CLI, NVIDIA PTX.</li>
</ul>
</li>
<li><b>Language independent</b>, like three-(four-)address code (similar to a classic RISC ISA).</li>
</ul>
<pre><code class="cpp">a = b + c * d + c * d;</code></pre>
<p>Three-Address Code (TAC)</p>
<pre><code class="c">r1 = c * d;
r2 = b + r1;
r3 = r2 + r1;
a = r3</code></pre>
<p>Here <strong>r<small>th</small></strong> is an abstract register.</p>
</section>
<section>
<h2>Three-Address Code</h2>
<ul>
<li>Quadruples has four fields
<table style="font-size: 0.75em;">
<thead>
<tr>
<th>Op</th>
<th>arg1</th>
<th>arg2</th>
<th>result</th>
</tr>
</thead>
<tbody>
<tr>
<td>*</td>
<td>c</td>
<td>d</td>
<td>r1</td>
</tr>
<tr>
<td>+</td>
<td>b</td>
<td>r1</td>
<td>r2</td>
</tr>
<tr>
<td>+</td>
<td>r2</td>
<td>r1</td>
<td>r3</td>
</tr>
<tr>
<td>=</td>
<td>r3</td>
<td></td>
<td>a</td>
</tr>
</tbody>
</table>
</li>
<li>Triples or Indirect triples have three fields
<table style="font-size: 0.75em;">
<thead>
<tr>
<th>Op</th>
<th>arg1</th>
<th>arg2</th>
</tr>
</thead>
<tbody>
<tr>
<td>*</td>
<td>c</td>
<td>d</td>
</tr>
<tr>
<td>+</td>
<td>b</td>
<td>(0)</td>
</tr>
<tr>
<td>+</td>
<td>(1)</td>
<td>(0)</td>
</tr>
<tr>
<td>=</td>
<td>(2)</td>
<td></td>
</tr>
</tbody>
</table>
</li>
</ul>
</section>
<section>
<h2>Intermediate language</h2>
<p>Provides frontend independent code representation.</p>
<dl>
<dt><a href="https://gcc.gnu.org/onlinedocs/gccint/GENERIC.html#GENERIC">GENERIC</a>
and <a href="https://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html">GIMPLE</a></dt>
<dd>GNU Compiler Collection<br>
<div class="console">
-fdump-tree-all -fdump-tree-optimized <br>-fdump-tree-<abbr title="Static Single Assignment">ssa</abbr>
-fdump-<abbr title="Register Transfer Language">rtl</abbr>-all
</div>
</dd>
<dt><a href="http://llvm.org/docs/LangRef.html">LLVM IL</a></dt>
<dd>clang and other LLWM based compilers<br>
<div style="vertical-align:middle;" class="console">-emit-llvm</div>
</dd>
<dt>CIL (C Intermediate Language)</dt>
<dd>Visual Studio cl.exe</dd>
</dl>
</section>
<section>
<h2>Intermediate language</h2>
<img alt="Gray to RGBA" src="images/popt/g2rgba.svg" class="simple" style="margin: -10px; width: 30%"/>
<pre><code class="cpp">uint32_t gray2rgba_v1(uint8_t c)
{
return c + (c<<8) + (c<<16) + (c<<24);
}</code></pre>
<div style="text-align: left;margin: 10px; font-size: 0.9em;" class="console">$ clang -Os -S -emit-llvm test.c -o test.ll<br>
$ cat test.ll</div>
<pre><code class="scala">define i32 @gray2rgba_v1(i8 zeroext %c) #0 {
%1 = zext i8 %c to i32
%2 = mul i32 %1, 16843009
ret i32 %2
}</code></pre>
<pre><code class="asm">gray2rgba_v1:
movzbl %dil, %eax
imull $16843009, %eax, %eax
ret</code></pre>
<!-- <p>c + (c<<8) + (c<<16) + (c<<24) = c + c*(1<<8) + c*(1<<16) + c*(1<<24) = c*(1 + 1<<8 + 1<<16 + 1<<24)</p> -->
</section>
</section>
<section>
<section>
<h2>Dealing Local variables</h2>
<p>Compiler don't care how many variables are used in code, register allocation is done after IR rotations.</p>
<pre class="cpp"><code>for( ; j <= roi.width - 4; j += 4 )
{
uchar t0 = tab[src[j]];
uchar t1 = tab[src[j+1]];
dst[j] = t0;
dst[j+1] = t1;
t0 = tab[src[j+2]];
t1 = tab[src[j+3]];
dst[j+2] = t0;
dst[j+3] = t1;
}</code></pre>
</section>
<section>
<h2>Dealing Local variables</h2>
<pre class="cpp" style="font-size:0.4em;"><code style=" max-height: 540px;">.lr.ph4: ; preds = %0, %.lr.ph4
%indvars.iv5 = phi i64 [ %indvars.iv.next6, %.lr.ph4 ], [ 0, %0 ]
%6 = getelementptr inbounds i8* %src, i64 %indvars.iv5
%7 = load i8* %6, align 1, !tbaa !1
%8 = zext i8 %7 to i64
%9 = getelementptr inbounds i8* %tab, i64 %8
%10 = load i8* %9, align 1, !tbaa !1
%11 = or i64 %indvars.iv5, 1
%12 = getelementptr inbounds i8* %src, i64 %11
%13 = load i8* %12, align 1, !tbaa !1
%14 = zext i8 %13 to i64
%15 = getelementptr inbounds i8* %tab, i64 %14
%16 = load i8* %15, align 1, !tbaa !1
%17 = getelementptr inbounds i8* %dst, i64 %indvars.iv5
store i8 %10, i8* %17, align 1, !tbaa !1
%18 = getelementptr inbounds i8* %dst, i64 %11
store i8 %16, i8* %18, align 1, !tbaa !1
%19 = or i64 %indvars.iv5, 2
// ...
%28 = zext i8 %27 to i64
%29 = getelementptr inbounds i8* %tab, i64 %28
%30 = load i8* %29, align 1, !tbaa !1
%31 = getelementptr inbounds i8* %dst, i64 %19
store i8 %24, i8* %31, align 1, !tbaa !1
%32 = getelementptr inbounds i8* %dst, i64 %25
store i8 %30, i8* %32, align 1, !tbaa !1
%indvars.iv.next6 = add nuw nsw i64 %indvars.iv5, 4
%33 = trunc i64 %indvars.iv.next6 to i32
%34 = icmp sgt i32 %33, %1
br i1 %34, label %..preheader_crit_edge, label %.lr.ph4
</code></pre>
<!-- %20 = getelementptr inbounds i8* %src, i64 %19
%21 = load i8* %20, align 1, !tbaa !1
%22 = zext i8 %21 to i64
%23 = getelementptr inbounds i8* %tab, i64 %22
%24 = load i8* %23, align 1, !tbaa !1
%25 = or i64 %indvars.iv5, 3
%26 = getelementptr inbounds i8* %src, i64 %25
%27 = load i8* %26, align 1, !tbaa !1
-->
</section>
<!-- <section> -->
<!-- <h2>Variables of non register type</h2> -->
<!-- use of uint8_t/int8_t variables -->
<!-- </section> -->
</section>
<section>
<section>
<h2>Link-Time Optimization (LTO)</h2>
Perform inter-procedural optimizations during linking.
<img alt="LTO" class="simple" src="images/popt/lto.svg"/>
Most compilers support this feature:
<ul>
<li><strong><a href="http://llvm.org/docs/LinkTimeOptimization.html">clang</a></strong> (-flto)</li>
<li><strong><a href="https://gcc.gnu.org/onlinedocs/gccint/LTO-Overview.html">gcc</a></strong>
(-flto) stating with 4.9</li>
<li><strong><a href="https://msdn.microsoft.com/en-us/library/0zza0de8.aspx">cl.exe</a></strong> (/GL, /LTCG)</li>
<li>...</li>
</ul>
</section>
<section>
<h2>WHOPR: Whole program optimization</h2>
<ol>
<li>Compile each source file separately, add extra information to the object file</li>
<li>Analyze information collected from all object files</li>
<li>Perform second optimization phase to generate object file</li>
<li>Link the final binary</li>
</ol>
<img alt="WHOPR" class="simple" src="images/popt/wpo.svg">
<ul>
<li>Eliminate even more redundant code</li>
<li>Compilations is better optimized for multi-core systems</li>
</ul>
</section>
</section>
<section>
<section>
<h2>Optimization levels</h2>
<dl>
<dt><b>-O0</b> (the default) No optimization</dt>
<dd>generates unoptimized code but has the fastest compilation time.</dd>
<dt><b>-O1</b> Moderate optimization</dt>
<dd>optimizes reasonably well but does not degrade compilation time significantly.</dd>
<dt><b>-O2</b> Full optimization</dt>
<dd>generates highly optimized code and has the slowest compilation time.</dd>
</dl>
</section>
<section>
<h2>Optimization levels</h2>
<dl>
<dt><b>-O3</b> Aggressive optimization</dt>
<dd>employees more aggressive automatic inlining of subprograms within a unit and attempts to vectorize.</dd>
<dt><b>-Os</b> Optimizes with focus on program size</dt>
<dd>enables all <code>-O2</code> optimizations that do not
typically increase code size. It also performs further optimizations designed to reduce code size.
</dl>
</section>
<section>
<h2>enabled optimizations: GCC -O0 </h2>
<p>GNU C version 4.9.2 (x86_64-linux-gnu)<br/>
<code class="console" style="font-size: 0.9em;">$ touch 1.c; gcc -O0 -S -fverbose-asm 1.c -o 1.s</code>
</p>
<blockquote style="font-size: 0.44em">
options enabled:
-faggressive-loop-optimizations
-fasynchronous-unwind-tables
-fauto-inc-dec
-fcommon
-fdelete-null-pointer-checks
-fdwarf2-cfi-asm
-fearly-inlining
-feliminate-unused-debug-types
-ffunction-cse
-fgcse-lm
-fgnu-runtime
-fgnu-unique
-fident
-finline-atomics
-fira-hoist-pressure
-fira-share-save-slots
-fira-share-spill-slots
-fivopts
-fkeep-static-consts
-fleading-underscore
-fmath-errno
-fmerge-debug-strings
-fpeephole
-fprefetch-loop-arrays
-freg-struct-return
-fsched-critical-path-heuristic
-fsched-dep-count-heuristic
-fsched-group-heuristic
-fsched-interblock
-fsched-last-insn-heuristic
-fsched-rank-heuristic
-fsched-spec
-fsched-spec-insn-heuristic
-fsched-stalled-insns-dep
-fshow-column
-fsigned-zeros
-fsplit-ivs-in-unroller
-fstack-protector
-fstrict-volatile-bitfields
-fsync-libcalls
-ftrapping-math
-ftree-coalesce-vars
-ftree-cselim
-ftree-forwprop
-ftree-loop-if-convert
-ftree-loop-im
-ftree-loop-ivcanon
-ftree-loop-optimize
-ftree-parallelize-loops=
-ftree-phiprop
-ftree-reassoc
-ftree-scev-cprop
-funit-at-a-time
-funwind-tables
-fverbose-asm
-fzero-initialized-in-bss
-m128bit-long-double
-m64
-m80387
-malign-stringops
-mavx256-split-unaligned-load
-mavx256-split-unaligned-store
-mfancy-math-387
-mfp-ret-in-387
-mfxsr
-mglibc
-mieee-fp
-mlong-double-80
-mmmx
-mno-sse4
-mpush-args
-mred-zone
-msse
-msse2
-mtls-direct-seg-refs
</blockquote>
</section>
<section>
<h2>enabled optimizations: GCC -O1 </h2>
<p>GNU C version 4.9.2 (x86_64-linux-gnu)<br/>
<code class="console" style="font-size: 0.9em;">$ touch 1.c; gcc -O1 -S -fverbose-asm 1.c -o 1.s</code>
</p>
<blockquote style="font-size: 0.44em">
options enabled:
-faggressive-loop-optimizations
-fasynchronous-unwind-tables
-fauto-inc-dec
<span class="opt0"> -fbranch-count-reg</span>
<span class="opt0"> -fcombine-stack-adjustments</span>
-fcommon
<span class="opt0"> -fcompare-elim</span>
<span class="opt0"> -fcprop-registers</span>
<span class="opt0"> -fdefer-pop</span>
-fdelete-null-pointer-checks
-fdwarf2-cfi-asm
-fearly-inlining
-feliminate-unused-debug-types
<span class="opt0"> -fforward-propagate</span>
-ffunction-cse
-fgcse-lm
-fgnu-runtime
-fgnu-unique
<span class="opt0"> -fguess-branch-probability</span>
-fident
<span class="opt0"> -fif-conversion</span>
<span class="opt0"> -fif-conversion2</span>
<span class="opt0"> -finline</span>
-finline-atomics
<span class="opt0"> -finline-functions-called-once</span>
<span class="opt0"> -fipa-profile</span>
<span class="opt0"> -fipa-pure-const</span>
<span class="opt0"> -fipa-reference</span>
-fira-hoist-pressure
-fira-share-save-slots
-fira-share-spill-slots
-fivopts
-fkeep-static-consts
-fleading-underscore
-fmath-errno
<span class="opt0"> -fmerge-constants</span>
-fmerge-debug-strings
<span class="opt0"> -fmove-loop-invariants</span>
<span class="opt0"> -fomit-frame-pointer</span>
-fpeephole
-fprefetch-loop-arrays
-freg-struct-return
-fsched-critical-path-heuristic
-fsched-dep-count-heuristic
-fsched-group-heuristic
-fsched-interblock
-fsched-last-insn-heuristic
-fsched-rank-heuristic
-fsched-spec
-fsched-spec-insn-heuristic
-fsched-stalled-insns-dep
-fshow-column
<span class="opt0"> -fshrink-wrap</span>
-fsigned-zeros
-fsplit-ivs-in-unroller
<span class="opt0"> -fsplit-wide-types</span>
-fstack-protector
-fstrict-volatile-bitfields
-fsync-libcalls
<span class="opt0"> -ftoplevel-reorder</span>
-ftrapping-math
<span class="opt0"> -ftree-bit-ccp</span>
<span class="opt0"> -ftree-ccp</span>
<span class="opt0"> -ftree-ch</span>
-ftree-coalesce-vars
<span class="opt0"> -ftree-copy-prop</span>
<span class="opt0"> -ftree-copyrename</span>
-ftree-cselim
<span class="opt0"> -ftree-dce</span>
<span class="opt0"> -ftree-dominator-opts</span>
<span class="opt0"> -ftree-dse</span>
-ftree-forwprop
<span class="opt0"> -ftree-fre</span>
-ftree-loop-if-convert
-ftree-loop-im
-ftree-loop-ivcanon
-ftree-loop-optimize
-ftree-parallelize-loops=
-ftree-phiprop
<span class="opt0"> -ftree-pta</span>
-ftree-reassoc
-ftree-scev-cprop
<span class="opt0"> -ftree-sink</span>
<span class="opt0"> -ftree-slsr</span>
<span class="opt0"> -ftree-sra</span>
<span class="opt0"> -ftree-ter</span>
-funit-at-a-time
-funwind-tables
-fverbose-asm
-fzero-initialized-in-bss
-m128bit-long-double
-m64
-m80387
-malign-stringops
-mavx256-split-unaligned-load
-mavx256-split-unaligned-store
-mfancy-math-387
-mfp-ret-in-387
-mfxsr
-mglibc
-mieee-fp
-mlong-double-80
-mmmx
-mno-sse4
-mpush-args
-mred-zone
-msse
-msse2
-mtls-direct-seg-refs
</blockquote>
</section>
<section>
<h2>enabled optimizations: GCC -O2 </h2>
<p>GNU C version 4.9.2 (x86_64-linux-gnu)<br/>
<code class="console" style="font-size: 0.9em;">$ touch 1.c; gcc -O2 -S -fverbose-asm 1.c -o 1.s</code>
</p>
<blockquote style="font-size: 0.44em">
options enabled:
-faggressive-loop-optimizations
-fasynchronous-unwind-tables
-fauto-inc-dec
<span class="opt0"> -fbranch-count-reg</span>
<span class="opt1"> -fcaller-saves</span>
<span class="opt0"> -fcombine-stack-adjustments</span>
-fcommon
<span class="opt0"> -fcompare-elim</span>
<span class="opt0"> -fcprop-registers</span>
<span class="opt1"> -fcse-follow-jumps</span>
<span class="opt0"> -fdefer-pop</span>
-fdelete-null-pointer-checks
<span class="opt1"> -fdevirtualize</span>
<span class="opt1"> -fdevirtualize-speculatively</span>
-fdwarf2-cfi-asm
-fearly-inlining
-feliminate-unused-debug-types
<span class="opt1"> -fexpensive-optimizations</span>
<span class="opt0"> -fforward-propagate</span>
-ffunction-cse
<span class="opt1"> -fgcse</span>
-fgcse-lm
-fgnu-runtime
-fgnu-unique
<span class="opt0"> -fguess-branch-probability</span>
<span class="opt1"> -fhoist-adjacent-loads</span>
-fident
<span class="opt0"> -fif-conversion</span>
<span class="opt0"> -fif-conversion2</span>
<span class="opt1"> -findirect-inlining</span>
<span class="opt0"> -finline</span>
-finline-atomics
<span class="opt0"> -finline-functions-called-once</span>
<span class="opt1"> -finline-small-functions</span>
<span class="opt1"> -fipa-cp</span>
<span class="opt0"> -fipa-profile</span>
<span class="opt0"> -fipa-pure-const</span>
<span class="opt0"> -fipa-reference</span>
<span class="opt1"> -fipa-sra</span>
-fira-hoist-pressure
-fira-share-save-slots
-fira-share-spill-slots
<span class="opt1"> -fisolate-erroneous-paths-dereference</span>
-fivopts
-fkeep-static-consts
-fleading-underscore
-fmath-errno
<span class="opt0"> -fmerge-constants</span>
-fmerge-debug-strings
<span class="opt0"> -fmove-loop-invariants</span>
<span class="opt0"> -fomit-frame-pointer</span>
<span class="opt1"> -foptimize-sibling-calls</span>
<span class="opt1"> -foptimize-strlen</span>
<span class="opt1"> -fpartial-inlining</span>
-fpeephole
<span class="opt1"> -fpeephole2</span>
-fprefetch-loop-arrays
<span class="opt1"> -free</span>
-freg-struct-return
<span class="opt1"> -freorder-blocks</span>
<span class="opt1"> -freorder-blocks-and-partition</span>
<span class="opt1"> -freorder-functions</span>
<span class="opt1"> -frerun-cse-after-loop</span>
-fsched-critical-path-heuristic
-fsched-dep-count-heuristic
-fsched-group-heuristic
-fsched-interblock
-fsched-last-insn-heuristic
-fsched-rank-heuristic
-fsched-spec
-fsched-spec-insn-heuristic
-fsched-stalled-insns-dep
<span class="opt1"> -fschedule-insns2</span>
-fshow-column
<span class="opt0"> -fshrink-wrap</span>
-fsigned-zeros
-fsplit-ivs-in-unroller
<span class="opt0"> -fsplit-wide-types</span>
-fstack-protector
<span class="opt1"> -fstrict-aliasing</span>
<span class="opt1"> -fstrict-overflow</span>
-fstrict-volatile-bitfields
-fsync-libcalls
<span class="opt1"> -fthread-jumps</span>
<span class="opt0"> -ftoplevel-reorder</span>
-ftrapping-math
<span class="opt0"> -ftree-bit-ccp</span>
<span class="opt1"> -ftree-builtin-call-dce</span>
<span class="opt0"> -ftree-ccp</span>
<span class="opt0"> -ftree-ch</span>
-ftree-coalesce-vars
<span class="opt0"> -ftree-copy-prop</span>
<span class="opt0"> -ftree-copyrename</span>
-ftree-cselim
<span class="opt0"> -ftree-dce</span>
<span class="opt0"> -ftree-dominator-opts</span>
<span class="opt0"> -ftree-dse</span>
-ftree-forwprop
<span class="opt0"> -ftree-fre</span>
-ftree-loop-if-convert
-ftree-loop-im
-ftree-loop-ivcanon
-ftree-loop-optimize
-ftree-parallelize-loops=
-ftree-phiprop
<span class="opt1"> -ftree-pre</span>
<span class="opt0"> -ftree-pta</span>
-ftree-reassoc
-ftree-scev-cprop
<span class="opt0"> -ftree-sink</span>
<span class="opt0"> -ftree-slsr</span>
<span class="opt0"> -ftree-sra</span>
<span class="opt1"> -ftree-switch-conversion</span>
<span class="opt1"> -ftree-tail-merge</span>
<span class="opt0"> -ftree-ter</span>
<span class="opt1"> -ftree-vrp</span>
-funit-at-a-time
-funwind-tables
-fverbose-asm
-fzero-initialized-in-bss
-m128bit-long-double
-m64
-m80387
-malign-stringops
-mavx256-split-unaligned-load
-mavx256-split-unaligned-store
-mfancy-math-387
-mfp-ret-in-387
-mfxsr
-mglibc
-mieee-fp
-mlong-double-80
-mmmx
-mno-sse4
-mpush-args
-mred-zone
-msse
-msse2
-mtls-direct-seg-refs
<span class="opt1"> -mvzeroupper</span>
</blockquote>
</section>
<section>
<h2>enabled optimizations: GCC -O3 </h2>
<p>GNU C version 4.9.2 (x86_64-linux-gnu)<br/>
<code class="console" style="font-size: 0.9em;">$ touch 1.c; gcc -O3 -S -fverbose-asm 1.c -o 1.s</code>
</p>
<blockquote style="font-size: 0.44em">
options enabled:
-faggressive-loop-optimizations
-fasynchronous-unwind-tables
-fauto-inc-dec
<span class="opt0"> -fbranch-count-reg</span>
<span class="opt1"> -fcaller-saves</span>
<span class="opt0"> -fcombine-stack-adjustments</span>
-fcommon
<span class="opt0"> -fcompare-elim</span>
<span class="opt0"> -fcprop-registers</span>
<span class="opt2"> -fcrossjumping</span>
<span class="opt1"> -fcse-follow-jumps</span>
<span class="opt0"> -fdefer-pop</span>
-fdelete-null-pointer-checks
<span class="opt1"> -fdevirtualize</span>
<span class="opt1"> -fdevirtualize-speculatively</span>
-fdwarf2-cfi-asm
-fearly-inlining
-feliminate-unused-debug-types
<span class="opt1"> -fexpensive-optimizations</span>
<span class="opt0"> -fforward-propagate</span>
-ffunction-cse
<span class="opt1"> -fgcse</span>
<span class="opt2"> -fgcse-after-reload</span>
-fgcse-lm
-fgnu-runtime
-fgnu-unique
<span class="opt0"> -fguess-branch-probability</span>
<span class="opt1"> -fhoist-adjacent-loads</span>
-fident
<span class="opt0"> -fif-conversion</span>
<span class="opt0"> -fif-conversion2</span>
<span class="opt1"> -findirect-inlining</span>
<span class="opt0"> -finline</span>
-finline-atomics
<span class="opt2"> -finline-functions</span>
<span class="opt0"> -finline-functions-called-once</span>
<span class="opt1"> -finline-small-functions</span>
<span class="opt1"> -fipa-cp</span>
<span class="opt2"> -fipa-cp-clone</span>
<span class="opt0"> -fipa-profile</span>
<span class="opt0"> -fipa-pure-const</span>
<span class="opt0"> -fipa-reference</span>
<span class="opt1"> -fipa-sra</span>
-fira-hoist-pressure
-fira-share-save-slots
-fira-share-spill-slots
<span class="opt1"> -fisolate-erroneous-paths-dereference</span>
-fivopts
-fkeep-static-consts
-fleading-underscore
-fmath-errno
<span class="opt0"> -fmerge-constants</span>
-fmerge-debug-strings
<span class="opt0"> -fmove-loop-invariants</span>
<span class="opt0"> -fomit-frame-pointer</span>
<span class="opt1"> -foptimize-sibling-calls</span>
<span class="opt1"> -foptimize-strlen</span>
<span class="opt1"> -fpartial-inlining</span>
-fpeephole
<span class="opt1"> -fpeephole2</span>
<span class="opt2"> -fpredictive-commoning</span>
-fprefetch-loop-arrays
<span class="opt1"> -free</span>
-freg-struct-return
<span class="opt1"> -freorder-blocks</span>
<span class="opt1"> -freorder-blocks-and-partition</span>
<span class="opt1"> -freorder-functions</span>
<span class="opt1"> -frerun-cse-after-loop</span>
-fsched-critical-path-heuristic
-fsched-dep-count-heuristic
-fsched-group-heuristic
-fsched-interblock
-fsched-last-insn-heuristic
-fsched-rank-heuristic
-fsched-spec
-fsched-spec-insn-heuristic
-fsched-stalled-insns-dep
<span class="opt1"> -fschedule-insns2</span>
-fshow-column
<span class="opt0"> -fshrink-wrap</span>
-fsigned-zeros
-fsplit-ivs-in-unroller
<span class="opt0"> -fsplit-wide-types</span>
-fstack-protector
<span class="opt1"> -fstrict-aliasing</span>
<span class="opt1"> -fstrict-overflow</span>
-fstrict-volatile-bitfields
-fsync-libcalls
<span class="opt1"> -fthread-jumps</span>
<span class="opt0"> -ftoplevel-reorder</span>
-ftrapping-math
<span class="opt0"> -ftree-bit-ccp</span>
<span class="opt1"> -ftree-builtin-call-dce</span>
<span class="opt0"> -ftree-ccp</span>
<span class="opt0"> -ftree-ch</span>
-ftree-coalesce-vars
<span class="opt0"> -ftree-copy-prop</span>
<span class="opt0"> -ftree-copyrename</span>
-ftree-cselim
<span class="opt0"> -ftree-dce</span>
<span class="opt0"> -ftree-dominator-opts</span>
<span class="opt0"> -ftree-dse</span>
-ftree-forwprop
<span class="opt0"> -ftree-fre</span>
<span class="opt2"> -ftree-loop-distribute-patterns</span>
-ftree-loop-if-convert
-ftree-loop-im
-ftree-loop-ivcanon
-ftree-loop-optimize
<span class="opt2"> -ftree-loop-vectorize</span>
-ftree-parallelize-loops=
<span class="opt2"> -ftree-partial-pre</span>
-ftree-phiprop
<span class="opt1"> -ftree-pre</span>
<span class="opt0"> -ftree-pta</span>
-ftree-reassoc
-ftree-scev-cprop
<span class="opt0"> -ftree-sink</span>
<span class="opt2"> -ftree-slp-vectorize</span>
<span class="opt0"> -ftree-slsr</span>
<span class="opt0"> -ftree-sra</span>
<span class="opt1"> -ftree-switch-conversion</span>
<span class="opt1"> -ftree-tail-merge</span>
<span class="opt0"> -ftree-ter</span>
<span class="opt1"> -ftree-vrp</span>
-funit-at-a-time
<span class="opt2"> -funswitch-loops</span>
-funwind-tables
-fverbose-asm
-fzero-initialized-in-bss
-m128bit-long-double
-m64
-m80387
-malign-stringops
-mavx256-split-unaligned-load
-mavx256-split-unaligned-store
-mfancy-math-387
-mfp-ret-in-387
-mfxsr
-mglibc
-mieee-fp
-mlong-double-80
-mmmx
-mno-sse4
-mpush-args
-mred-zone
-msse
-msse2
-mtls-direct-seg-refs
<span class="opt1"> -mvzeroupper</span>
</blockquote>
</section>
</section>
<section>
<h2>Classic compiler optimization taxonomy</h2>
<table style="width: 100%;">
<colgroup>
<col/>
<col/>
</colgroup>
<tbody>
<tr>
<td style="width: 50%;">
<b>Machine independent</b>
<strong>Applicable across a broad range of machines</strong>
<ol style="width: 90%;">
<li>Eliminate redundant computations, dead code</li>
<li>Reduce running time and space</li>
<li>Decrease ratio of overhead to real work</li>
<li>Specialize code on a context</li>
<li><i>Enable other optimizations</i></li>
</ol>
</td>
<td>
<b>Machine dependent</b>
<strong>Capitalize on specific machine properties</strong>
<ol style="width: 90%;">
<li>Manage or hide latency</li>
<li>Manage resources (registers, stack)</li>
<li>Improve mapping from IR to concrete machine</li>
<li>Use some exotic instructions (eg VLDM )</li>
</ol>
</td>
</tr>