CD Notes
In-depth exploration of GNU Compiler Collection structure, intermediate representations, and optimization pipeline
GCC (GNU Compiler Collection) is one of the oldest and most widely-used compiler toolchains. Understanding its architecture helps appreciate compiler design principles.
GCC Overview
GCC is a comprehensive compiler system supporting multiple languages (C, C++, Fortran, Ada, Go, Rust and others) and target architectures.
GCC Architecture Layers
| C Parser | C++ Parser | Fortran | ... etc | |
|---|---|---|---|---|
| Parser | ||||
| x86-64 | ARM | MIPS | ... etc | |
| Code Gen | Code Gen | Code Gen |
GCC Compilation Process
| │ Phase 1 | Parse │ |
| │ Phase 2 | Generate GIMPLE IR │ |
| │ Phase 3 | Optimize │ |
| │ Multiple optimization passes | │ |
| │ Phase 4 | RTL Generation │ |
| │ Phase 5 | Generate Assembly │ |
| │ Phase 6 | Assemble │ |
| │ Phase 7 | Link │ |
GCC Intermediate Representations
GENERIC AST
High-level abstract syntax tree representation:
// C code
int x = 5;
int y = x + 3;GIMPLE
Simplified representation with explicit operations:
// C code
if (a > b) {
x = 1;
} else {
x = 2;
}RTL (Register Transfer Language)
Low-level representation for instruction selection:
| RTL representation of | x = a + b |
| (set (reg | SI 1) |
| (plus | SI (reg:SI 2) ; register 2 contains a |
| (reg | SI 3))); register 3 contains b |
GCC Optimization Passes
GCC applies numerous optimization passes:
| Optimization | Purpose |
|---|---|
| Constant Folding | Evaluate constants at compile-time |
| Constant Propagation | Replace variables with known constants |
| Dead Code Elimination | Remove unused variables/statements |
| Common Subexpression Elimination | Reuse computed values |
| Loop Unrolling | Reduce loop iterations count |
| Inlining | Replace function calls with code |
| Vectorization | Use SIMD instructions |
| Strength Reduction | Replace expensive ops with cheaper ones |
| Register Allocation | Assign variables to CPU registers |
GCC Command-Line Options
gcc -c program.c # Compile only (create .o)
gcc -S program.c # Generate assembly only
gcc -E program.c # Preprocess only
gcc -O0 program.c # No optimization (debug)
gcc -O1 program.c # Basic optimization
gcc -O2 program.c # More optimization
gcc -O3 program.c # Maximum optimization
gcc -march=native program.c # Optimize for current CPU
gcc -Wall -Wextra program.c # Enable all warnings
gcc -g program.c # Include debug symbols
gcc -fPIC program.c # Position-independent codeGCC vs LLVM/Clang
| Aspect | GCC | LLVM/Clang |
|---|---|---|
| Age | Older (1987) | Newer (2003) |
| Architecture | Monolithic | Modular |
| IR Usage | Multiple (GENERIC, GIMPLE, RTL) | Single (LLVM IR) |
| Compilation speed | Slower | Faster |
| Optimization passes | Many | Many |
| Diagnostics | Good | Excellent |
| Language support | C, C++, Fortran, etc. | C, C++, Rust, Swift, etc. |
| License | GPL | BSD |
| Modularity | Harder to extend | Easy to extend |
Example: GCC Compilation
C Program (example.c):
int add(int a, int b) {
return a + b;
}
int main() {
int x = add(5, 3);
return x;
}Step 1: Generate GIMPLE IR
gcc -fdump-tree-gimple example.cGIMPLE Output (conceptual):
main ()
{
int x;
int D.1234;
D.1234 = add (5, 3);
x = D.1234;
return x;
}Step 2: Generate Assembly
gcc -S example.cAssembly Output (x86-64):
add:
pushq %rbp
movq %rsp, %rbp
movl %edi, -4(%rbp)
movl %esi, -8(%rbp)
movl -4(%rbp), %eax
addl -8(%rbp), %eax
popq %rbp
ret
main:
pushq %rbp
movq %rsp, %rbp
movl $5, %edi
movl $3, %esi
call add
movl %eax, -4(%rbp)
movl -4(%rbp), %eax
popq %rbp
retInterview Q&A
Q: What are GCC's main components? A: GCC consists of frontends (parsers for different languages), a middle-end (optimizations using GIMPLE IR), and backends (code generation for different architectures).
Q: Why does GCC use multiple intermediate representations? A: GENERIC is AST-like (language-independent structure), GIMPLE is simplified three-address code (easier to analyze), RTL is register-level (for instruction selection).
Q: What's the purpose of RTL in GCC? A: RTL (Register Transfer Language) is a register-level representation used for instruction selection, register allocation, and code generation.
Q: How many optimization passes does GCC run? A: GCC runs many optimization passes (over 100 in some configurations). They can be tuned with -O levels: O0 (none), O1 (basic), O2 (more), O3 (aggressive).
Q: Why is GIMPLE IR useful? A: GIMPLE simplifies the AST into three-address code form, making it easier to implement optimizations consistently across different language frontends.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for GCC Architecture.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Compiler Design topic.
Search Terms
compiler-design, compiler design, compiler, design, modern, technologies, gcc, architecture
Related Compiler Design Topics