Download - GCC
![Page 2: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/2.jpg)
2
Outline
Chapter1 - Compiler– Complication … P3– GCC compiler … P12
Chapter2 - GCC Source Code – Configuration & Building … P17– Cross Compile GCC compiler … P21
Chapter3 - Gray box probing of GCC– Gray box probing of GCC … P29– Passes … P33– Gimple Optimization … P46
Reference … P57
![Page 3: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/3.jpg)
3
Chapter1
Complication
![Page 4: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/4.jpg)
Binding
conceptualizing
Programming
Compile
linking
loading
Executing
Problem
Algorithm, Data Structure, Strategy
Function, Variable, Type、 Control Flow
Instruction, Register
Function addressing, library
Code, Data addressing
Value of Variable
unbind object
time
4
![Page 5: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/5.jpg)
5
Problem
Algorithm, Data Structure, Strategy
Function, Variable, Type、 Control Flow
Instruction, Register
Function addressing, library
Code, Data addressing
Value of Variable
unbind object
time
Binding
conceptualizing
Programming
Compile
linking
loading
Executing
![Page 6: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/6.jpg)
6
Interpreter VS Compiler
Source Program
InputData
Interpreter Machine
DevelopFlexibility
![Page 7: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/7.jpg)
7
Interpreter VS Compiler
CompilerTarget
ProgramMachine
Source Program
InputData
ExecutingSpeed
![Page 8: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/8.jpg)
8
Model
Front-End
Optimizer
Code Generator
AST
Ind. IR
TargetProgram
Front-End
Expander
Optimizer
Recognizer
AST
Register Transfer
Register Transfer
Aho Ullman Model
Davidson Fraser Model
Machine & OptimizerIndependent
Machine & OptimizerDependent
![Page 9: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/9.jpg)
9
Structure of Compiler
Front-End
Back-End
Parser
Scanner
Semantic Analyzer
Symbol table Handler
Source Program
InstructionSelector
AssemblyEmitter
Register Allocator
AssemblyProgram
AST
![Page 10: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/10.jpg)
10
Typical Front-End
Parser
ScannerSemantic Analyzer
Symbol table
Handler
Source Program
ASTParse Tree
ErrorHandler
Token
AST / IR + Symbol Table
![Page 11: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/11.jpg)
11
Typical Back-End
Ind. Optimizer
CodeGenerator
Register Allocator
AssemblyProgram
Instruction Scheduler
Peephole Optimizer
Ind. IR
Dep. IR
AST / IR + Symbol Table
AssemblyCode
Generator
![Page 12: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/12.jpg)
12
Chapter1
GCCGNU Compiler CollectionGreat Compiler Challenge
![Page 13: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/13.jpg)
13
GCC
GCC compiler
GCC
Source Program
TargetProgram
CC1 CPP
AS
LD LIB
compiler
assembler
linker
C Preprocessor
GCC is a collection that invokes compiler, assembler and linker…
Static
dynamic
![Page 14: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/14.jpg)
14
Parser
Source Program
AssemblyProgram
14
Architecture
GimpliferTreeSSAOptimize
rExpander
Optimizer
Recoginer
Inputlanguage
Targetname
Language &Machine
IndependentGeneric Code
MachineDependentGenerator
Code
MachineDescriptions
LanguageSpecific
Code
![Page 15: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/15.jpg)
15
Parser
Source Program
AssemblyProgram
15
Different Time
GimpliferTreeSSAOptimize
rExpander
Optimizer
Recoginer
Inputlanguage
Targetname
Language &Machine
IndependentGeneric Code
MachineDependentGenerator
Code
MachineDescriptions
LanguageSpecific
Code
DevelopGCCTime
Build GCCtime
UseGCCtime
Select Copy CopyGenerat
eGenerat
e
![Page 16: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/16.jpg)
16
Parser
Source Program
AssemblyProgram
16
Different View
GimpliferTreeSSAOptimize
rExpander
Optimizer
Recoginer
Inputlanguage
Targetname
Language &Machine
IndependentGeneric Code
MachineDependentGenerator
Code
MachineDescriptions
LanguageSpecific
Code
C or C++?
Arm or x86?
![Page 17: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/17.jpg)
17
Chapter2
GCC Source Code Configuration & Building
![Page 18: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/18.jpg)
18
Pre-requisites
• ISO C90• GCC• GNU Bash• Awk• bzip, gzip, untar• GNU Make
Mpfr libraryMpc libraryPplC LooG-PPLJarLibelfGMP
https://gcc.gnu.org/install/prerequisites.html
![Page 19: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/19.jpg)
19
Directory
• GCC Source – source code– $(SOURCE_D)
• GCC Build – make source code– $(BUILD)
• GCC Install – install binary file– $(INSTALL)
*GCC will generate file in build time
![Page 20: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/20.jpg)
20
Step
1. Build pre-requisites2. --prefix = /usr/local3. ldconfig4. Build gcc
cd $(BUILD)$(SOURCE_D)/configure make; make install
=> #create makefile
=> #install path
=> #link library
![Page 21: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/21.jpg)
29
Chapter3Gray box probing of GCC
![Page 22: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/22.jpg)
30
What is Gray box?
Black Box
Input
Output
![Page 23: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/23.jpg)
31
What is Gray box?
Black Box
Input
Output
White Box
Input
Output
Obj
Obj
Obj
Obj Obj
Obj
Obj
Obj
![Page 24: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/24.jpg)
32
What is Gray box?
Black Box
Input
Output
White Box
Input
Output
Gray Box
Input
Output
Obj
Obj
Obj
Obj Obj
Obj
Obj
Obj Obj
Obj
Obj
![Page 25: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/25.jpg)
33
Chapter3
PassesExamining Dumps
![Page 26: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/26.jpg)
34
Passes
Parse GimplifyTreeSSAOptimize
GenerateRTL
RTLOptimize
GenerateASM
Target Ind. Target dep.
Gimple Passes
RTL -> ASM
RTL Passes
Gimple -> RTL
![Page 27: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/27.jpg)
35
Command
• gcc -fdump-<stage>-<passname> <file>– ex. gcc -fdump-tree-original test.c– ex. gcc -fdump-tree-cfg-raw test.c– ex. gcc -fdump-ipa-all test.c
– Stage:• tree• ipa• rtl
![Page 28: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/28.jpg)
36
Important passes
Parse
Gimplify
Source Program
CFG Grammer
RTL Generator
Reg Allocator
pro_epilogue generator
Pattern Matcher
ASMProgram
AST: 003t.original
GIMPLE: 004t.gimple
CFG: 013t.cfg
RTL expand: 144r.expand
IRA: 191r.ira
198r.prologue-and-epilogue
![Page 29: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/29.jpg)
37
Command Result
![Page 30: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/30.jpg)
38
1. gcc -fdump-tree-original-raw test.c
Examples: AST dumps
test.c
int a;main(){
a = 55;}
![Page 31: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/31.jpg)
39
test.c.004t.gimple
2. gcc -fdump-tree-gimple test.c
test.c
Examples: GIMPLE dumps
int main(){ int a[3], x; a[1] = a[2] = 10; x = a[1] + a[2]; a[0] = a[1] + a[1]*x;}
main (){ int D.1589; int D.1590; int D.1591; int D.1592; int D.1593; int D.1594; int a[3]; int x;
a[2] = 10; D.1589 = a[2]; a[1] = D.1589; D.1590 = a[1]; D.1591 = a[2]; x = D.1590 + D.1591; D.1592 = x + 1; D.1593 = a[1]; D.1594 = D.1592 * D.1593; a[0] = D.1594;}
![Page 32: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/32.jpg)
40
Examples: CFG dumps
3. gcc -fdump-tree-cfg test.c
test.c.004t.gimple (part)
if (a<=12) goto <D.1200>else goto <D.1201><D.1200>:D.1199 = a + b;a = D.1199 + c;<D.1201>:
test.c (part)
If (a<=12)a =
a+b+c;
![Page 33: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/33.jpg)
42
Examples: RTL dumps
4. gcc -fdump-rtl-expand test.ctest.c.144r.expand (part)
test.c
int a;main(){ a = a+1;}
(insn 5 4 6 3 (set (reg:SI 59 [ a.0 ]) (mem/c/i:SI (symbol_ref:DI ("a") <var_decl 0x7f13bdac3000 a>) [0 a+0 S4 A32])) test.c:4 -1 (nil))
(insn 6 5 7 3 (parallel [ (set (reg:SI 60 [ a.1 ]) (plus:SI (reg:SI 59 [ a.0 ]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) test.c:4 -1 (nil))
(insn 7 6 13 3 (set (mem/c/i:SI (symbol_ref:DI ("a") <var_decl 0x7f13bdac3000 a>) [0 a+0 S4 A32]) (reg:SI 60 [ a.1 ])) test.c:4 -1 (nil))
![Page 34: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/34.jpg)
43
(insn 5 4 6 3 (set (reg:SI 59 [ a.0 ]) (mem/c/i:SI (symbol_ref:DI ("a") <var_decl 0x7f13bdac3000 a>) [0 a+0 S4 A32])) test.c:4 -1 (nil))
(insn 6 5 7 3 (parallel [ (set (reg:SI 60 [ a.1 ]) (plus:SI (reg:SI 59 [ a.0 ]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) test.c:4 -1 (nil))
(insn 7 6 13 3 (set (mem/c/i:SI (symbol_ref:DI ("a") <var_decl 0x7f13bdac3000 a>) [0 a+0 S4 A32]) (reg:SI 60 [ a.1 ])) test.c:4 -1 (nil))
r59 = a;
r60 = r59 + 1
a = r60
![Page 35: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/35.jpg)
44
(insn 5 4 6 3 (set (reg:SI 59 [ a.0 ]) (mem/c/i:SI (symbol_ref:DI ("a") <var_decl 0x7f13bdac3000 a>) [0 a+0 S4 A32])) test.c:4 -1 (nil))
(insn 6 5 7 3 (parallel [ (set (reg:SI 60 [ a.1 ]) (plus:SI (reg:SI 59 [ a.0 ]) (const_int 1 [0x1]))) (clobber (reg:CC 17 flags)) ]) test.c:4 -1 (nil))
(insn 7 6 13 3 (set (mem/c/i:SI (symbol_ref:DI ("a") <var_decl 0x7f13bdac3000 a>) [0 a+0 S4 A32]) (reg:SI 60 [ a.1 ])) test.c:4 -1 (nil))
Current Inst
Previous Inst
Next InstCFG: Basic
Block
Single Integar
Register
Integer 1
Scalar
Memory reference
![Page 36: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/36.jpg)
45
Examples: Assembly dumps
5. gcc -S test.c | | objdump -d a.out
test.s (part)test.c main:.LFB0: .cfi_startproc pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movl $1, -4(%rbp) popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc
int main(){int a;a=1;}
test.c.144r.expand (part)
(insn 5 4 11 3 (set (mem/c/i:SI (plus:DI (reg/f:DI 54 virtual-stack-vars) (const_int -4 [0xfffffffffffffffc])) [0 a+0 S4 A32]) (const_int 1 [0x1])) test.c:4 -1 (nil))
![Page 37: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/37.jpg)
46
Chapter3
Gimple Optimization
![Page 38: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/38.jpg)
47
Passes
Parse GimplifyTreeSSAOptimize
GenerateRTL
RTLOptimize
GenerateASM
Target Ind. Target dep.
Gimple Passes
RTL -> ASM
RTL Passes
Gimple -> RTL
![Page 39: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/39.jpg)
48
Brief
0. Pre-procedure (017t.ssa) (022t.copyrename1)1. constant propagation (023t.ccp1) (059t.ccp2)2. copy propagation (027t.copyprop1)3. loop unrolling (058t.cunrolli) 4. dead code elimination (029t.cddce1)
Command:gcc -fdump-tree-all -O2 test.c
![Page 40: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/40.jpg)
49
Source Code
int main(){
int a=1;int b=2;int c=3;int n=c*2;while (a<=n)a = a+1;if (a<12)a = a+b+c;return a;
}
![Page 41: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/41.jpg)
50
cfg (Control Flow Graph)a=1 b=2
c=3 n=c*2
B2
If a<=nB4
If a<12B5
If a<12B3
D.1200 = a+ba=D.1200+c
B6
D.1201=areturn D.1201
B7
T
T
F
F
![Page 42: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/42.jpg)
51
cfg -> ssa (017t.ssa)a_3=1 b_4=2
c_5=3 n_6=c_5*2
B2
a_1=phi(a_3, a_7)
If a_1<=n_6B4
If a_1<12B5
If a_7<12B3
D.1200_8 = a_1+b_4a_9=D.1200_8+c_5
B6
a_2 = phi(a_1, a_9)
D.1201_10=a_2return D.1201_10
B7
T
T
F
F
![Page 43: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/43.jpg)
52
ssa -> copyrename (022t)<bb 7>:#a_2 = PHI<a_1(5),a_9(6)>D.1201_10=a_2;return D.1201_10;
<bb 7>:#a_2 = PHI<a_1(5),a_9(6)>a_10=a_2;return a_10;
017t.ssa
022t.copyrename1
![Page 44: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/44.jpg)
53
copyrename -> ccp (023t)<bb 2>a_3=1;b_4=2;c_5=3;n_6=c_5*2;goto <bb 4>
<bb 2>a_3=1;b_4=2;c_5=3;n_6=6;goto <bb 4>
022t.copyrename1
023t.ccp1
![Page 45: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/45.jpg)
54
ccp -> copyprop (027t)<bb 7>:#a_2 = PHI<a_1(5),a_9(6)>a_10=a_2;return a_10;
<bb 7>:#a_2 = PHI<a_1(5),a_9(6)>return a_2;
023t.ccp1
027t.copyprop1
![Page 46: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/46.jpg)
copyprop -> cddc (029t)
55
029t.cddc
<bb 2>:a_3 = 1;b_4 = 2;c_5 = 3;n_6 = 6;goto <bb 4>;
<bb 3>:a_7 = a_1 + 1;
<bb 4>:…
<bb 2>:goto <bb 4>;
<bb 3>:a_7 = a_1 + 1;
<bb 4>:…
029t.cddc
![Page 47: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/47.jpg)
cddc ->cunrolli (058t)
56
<bb 2>:goto <bb 4>;
<bb 3>:a_7 = a_1 + 1;
<bb 4>:# a_1 = PHI <1(2), a 7(3)>if (a_1 <= 6) goto <bb 3>;else goto <bb 5>;
<bb 5>:if (a_1 <= 11) goto <bb 6>;else goto <bb 7>;
<bb 6>:a_9 = a_1 + 5;
<bb 7>:# a_2 = PHI <a_1(5), a_9(6)>return a_2;
a=1a=a+1a=a+1a=a+1a=a+1a=a+1a=a+1
<bb 2>:a_12 = 2;a_14 = a_12 + 1;a_16 = a_14 + 1;a_18 = a_16 + 1;a_20 = a_18 + 1;a_22 = a_20 + 1;if (a_22 <= 11) goto <bb 3>;else goto <bb 4>;
<bb 3>:a_9 = a_22 + 5;
<bb 4>:# a_2 = PHI <a_22(2), a_9(3)>return a_2;
029t.cddc058t.cunrolli
![Page 48: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/48.jpg)
cunrolli -> ccp2 (059t)
main(){<bb 2>:
return 12;}
57
<bb 2>:a_12 = 2;a_14 = a_12 + 1;a_16 = a_14 + 1;a_18 = a_16 + 1;a_20 = a_18 + 1;a_22 = a_20 + 1;if (a_22 <= 11) goto <bb 3>;else goto <bb 4>;
<bb 3>:a_9 = a_22 + 5;
<bb 4>:# a_2 = PHI <a_22(2), a_9(3)>return a_2;
058t.cunrolli059t.ccp2
![Page 49: GCC](https://reader035.vdocuments.site/reader035/viewer/2022081413/548c1edeb47959d02a8b4686/html5/thumbnails/49.jpg)
Reference
• GCC Source Code– https://github.com/mirrors/gcc
• IITB GCC workshop OCW– http://www.cse.iitb.ac.in/grc/index.php?
page=gcc-pldi14-tut
58