Languages Used
in LLVM During
Compilation
IPP, FIT BUT
Adam Husár, ihusar@fit.vutbr.cz
30.4.2014
Contents
• Programming electronic systems
• LLVM compiler and intermediate representations
used during compilation
 Frontend
 Parsing
 Semantics analysis
 IR generation
 Optimizer
 Backend
 Instruction selection
 Register allocation
 Assembly printing
2
/32
Programming electronic
systems
Electrons
Programmer
El. components
Natural language
Programming language
El. voltage
RTL
Compiler
ISA (architecture)
Microarchitecture
3
/32
Programming electronic
systems
Electrons
Programmer
El. components
Natural language
Programming language
El. voltage
RTL
Compiler
ISA (architecture)
Microarchitecture
4
/32
C/C++ language program to
binary representation
C/C++ compiler
Assembler
Linker
C/C++ language
Compiler
Binary (w/o relocations)
Assembly (textual)
Binary (w. relocations)
5
/32
C/C++ language program to
binary representation
C/C++ compiler
Assembler
Linker
C/C++ language
Compiler
Binary (w/o relocations)
Assembly (textual)
Binary (w. relocations)
6
/32
LLVM compiler
• LLVM – original acronym meaning was Low Level
Virtual Machine, now just LLVM
• Collection of tools and libraries,.competition to gcc (GNU
Compiler Collection)
LLVM C/C++
compiler
Frontend
Optimizer
Backend
C/C++ language
LLVM IR
LLVM IR
Assembly (textual)
• LLVM has many different frontends
 C/C++, Objective C/C++, Haskell, Open CL, Ada, ...
• and backends
 x86, ARM, MicroBLaze, MIPS, PowerPC, SPARC, R600, Hexagon, ...
7
/32
LLVM compiler components
LLVM C/C++
compiler
Frontend
Optimizer
Backend
C/C++ language
LLVM IR
LLVM IR
Assembly (textual)
8
/32
Frontend compilation
• Frontend transforms input source code into a compiler
intermediate representation
• LLVM frontend is called clang
• AST (abstract syntax tree) is an internal representation
used in frontend
• For C/C++ must know target architecture data type
sizes (e.g. int size - 16 or 32 bits, and pointer size – 16,
32, or 64)
C language Tokens AST AST LLVM IR 9
/32
Example – C -> AST
int foo(int aa, int bb, int cc) {
int sum = aa + bb;
return sum / cc;
}
10/
32
AST – Abstract Syntax Tree
• Representation used to do semantic checks
• Contains all information about input source code, can be
used to reconstruct original source code
• Source-to-source compilers
• Static analysis (LLVM static analyzer)
• Clang also performs some optimizations over AST such
as inlining
11/
32
(used slides from [2])
12/
32
(used slides from [2])
Source to source compilation
13/
32
Example AST -> LLVM IR
• The previous AST is transformed into the LLVM IR
(representation
ASTint foo
(int aa, int bb, int cc)
{
int sum = aa + bb;
return sum / cc;
}
14/
32
LLVM IR
• Common internal representation for optimization
passes
• Used both as input for backend and for
interpreter/virtual machine
• Very close to assembly languages with several
differences
 Typed
 integer (iX)
 floating point
 vector
 structures, arrays, unions
 Unlimited number of registers (temporary variables)
 SSA – based
 Operators have their signedness specified
 Some high-level operations, e.g. exception handling
Frontend
Optimizer
Backend
C/C++ language
LLVM IR
LLVM IR
Assembly
15/
32
LLVM IR operations (1/2)
• Standard integer and floating point operations
 add, sub, mul, udiv, sdiv, and, or, xor, shifts, ...
 fadd, fsub, fmul, ...
• Conversions
 trunc .. to, zext .. to, ...
• Comparisons, selects
 icmp, fcmp, select, phi, ...
• Memory access and addressing
 load, store
 fence, cmpxchg, ...
 getelementptr
16/
32
LLVM IR operations (2/2)
• Terminator instructions
 br, switch, ret, ...
• Other instructions
 call, va_arg,
• Intrinsic instructions
 garbage collector
 code generator
 standard C library
 memcpy, memmove, sqrt, ...
 debugger
 exception handling
17/
32
(used slides from [1])
18/
32
(used slides from [1])
(used slides from [1])
19/
32
20/
32
LLVM compiler components
LLVM C/C++
compiler
Frontend
Optimizer
Backend
C/C++ language
LLVM IR
LLVM IR
Assembly (textual)
21/
32
Optimizer
• Also called midend
• Uses only LLVM IR language
• Over 80 transform and analysis passes
• Analysis pass – computes analysis information,
may use analysis information from other analysis
passes
• Transform pass – changes LLVM IR program
representation into equivalent program
• Pass ordering is important, several
transformations are executed repeatedly
• Mostly architecture independent
Frontend
Optimizer
Backend
C/C++ language
LLVM IR
LLVM IR
Assembly
22/
32
LLVM Optimizer Passes
$opt -O3 test.ll -o testO3 -debug-pass=Structure
No Alias Analysis (always returns 'may' alias)
Andersens Alias Analysis (statefull AA impl)
Type-Based Alias Analysis
Basic Alias Analysis (stateless AA impl)
ModulePass Manager
Global Variable Optimizer
Interprocedural Sparse Conditional Constant Propagation
Dead Argument Elimination
FunctionPass Manager
Combine redundant instructions
Simplify the CFG
CallGraph Construction
Call Graph SCC Pass Manager
Remove unused exception handling info
Inline Cost Analysis
Function Integration/Inlining
Deduce function attributes
Promote 'by reference' arguments to scalars
FunctionPass Manager
SROA
Dominator Tree Construction
Early CSE
Lazy Value Information Analysis
Jump Threading
Value Propagation
Simplify the CFG
Combine redundant instructions
Tail Call Elimination
Simplify the CFG
Reassociate expressions
Dominator Tree Construction
Natural Loop Information
Loop Pass Manager
Canonicalize natural loops
Loop-Closed SSA Form Pass
Rotate Loops
Loop Invariant Code Motion
Loop-Closed SSA Form Pass
Unswitch loops
Combine redundant instructions
Scalar Evolution Analysis
Loop Pass Manager
Canonicalize natural loops
Loop-Closed SSA Form Pass
Induction Variable Simplification
Recognize loop idioms
Delete dead loops
Unroll loops
Memory Dependence Analysis
Global Value Numbering
Memory Dependence Analysis
MemCpy Optimization
Sparse Conditional Constant Propagation
Combine redundant instructions
Lazy Value Information Analysis
Jump Threading
Value Propagation
Dominator Tree Construction
Memory Dependence Analysis
Dead Store Elimination
Natural Loop Information
Scalar Evolution Analysis
SLP Vectorizer
Aggressive Dead Code Elimination
Simplify the CFG
...
Dead Global Elimination
Merge Duplicate Global Constants
FunctionPass Manager
Preliminary module verification
Dominator Tree Construction
Module Verifier
Bitcode Writer
23/
32
Optimizer - Example Optimizer
LLVM IR
LLVM IR
Original code
int foo
(int aa, int bb, int cc)
{
int sum = aa + bb;
return sum / cc;
}
24/
32
LLVM compiler components
LLVM C/C++
compiler
Frontend
Optimizer
Backend
C/C++ language
LLVM IR
LLVM IR
Assembly (textual)
25/
32
Backend
• Backend (also called code generator) transforms LLVM
IR into assembly code
• Architecture dependent
• Only main transformation passes are shown here:
Lowering
Instr. selection
Assembly printer
LLVM IR
Selection DAG w. SD (LLVM) nodes
Selection DAG w. Machine Instruction nodes
Assembly code
Scheduling
Machine Code w. virt. regs
Reg. allocation
Machine Code w. machine regs
26/
32
Lowering, instruction
selection, and basic scheduling
• Switch to presentation Dan Gohman: SelectionDAG
Phases ([3])
Lowering
Instr. selection
Assembly printer
LLVM IR
Selection DAG w. SD (LLVM) nodes
Selection DAG w. Machine Instruction nodes
Assembly code
Scheduling
Machine Code w. virt. regs
Reg. allocation
Machine Code w. machine regs
27/
32
LLVM IR -> Machine code
Function Live Ins: %gpregs_3 in %vreg0,
%gpregs_4 in %vreg1, %gpregs_5 in %vreg2
Live Ins: %gpregs_3 %gpregs_4 %gpregs_5
%vreg2<def> = COPY %gpregs_5
%vreg1<def> = COPY %gpregs_4
%vreg0<def> = COPY %gpregs_3
%vreg3<def> = instr_addu
%vreg0, %vreg1
instr_div
%vreg3<kill>, %vreg2,
%rlo_0<imp-def>,%rhi_0<imp-def,dead>
%vreg4<def> = COPY %rlo_0
%gpregs_3<def> = COPY %vreg4
instr_jump %gpregs_31, %gpregs_3
Lowering
Instr. selection
LLVM IR
Selection DAG w. SD (LLVM) nodes
Selection DAG w. Machine Instruction nodes
Scheduling
28/
32
Machine code passes in backend
Expand ISel Pseudo-instructions
Tail Duplication
Optimize machine instruction PHIs
Slot index numbering
Merge disjoint stack slots
Local Stack Slot Allocation
Remove dead machine instructions
Machine Loop Invariant Code Motion
Machine Common Subexpression
Elimination
Machine code sinking
Peephole Optimizations
Process Implicit Definitions
Remove unreachable machine basic blocks
Live Variable Analysis
Eliminate PHI nodes for
register allocation
Two-Address instruction pass
Slot index numbering
Live Interval Analysis
Simple Register Coalescing
Debug Variable Analysis
Live Stack Slot Analysis
Virtual Register Map
Live Register Matrix
Virtual Register Rewriter
Stack Slot Coloring
Machine Loop Invariant Code Motion
Prologue/Epilogue Insertion & Frame
Finalization
Control Flow Optimizer
Tail Duplication
Machine Copy Propagation Pass
Post-RA pseudo instruction expansion pass
Post RA top-down list latency
scheduler
Analyze Machine Code For Garbage Collection
Branch Probability Basic Block Placement
Assembly Printing
29/
32
Register allocation
Live Ins: %gpregs_3 %gpregs_4 %gpregs_5
%vreg2<def> = COPY %gpregs_5
%vreg1<def> = COPY %gpregs_4
%vreg0<def> = COPY %gpregs_3
%vreg3<def> = instr_addu
%vreg0, %vreg1
instr_div
%vreg3<kill>, %vreg2,
%rlo_0<imp-def>,%rhi_0<imp-def,dead>
%vreg4<def> = COPY %rlo_0
%gpregs_3<def> = COPY %vreg4
instr_jump %gpregs_31, %gpregs_3
Live Ins: %gpregs_3 %gpregs_4 %gpregs_5
%gpregs_3<def> = instr_addu %gpregs_3<kill>, %gpregs_4<kill>
instr_div %gpregs_3<kill>, %gpregs_5<kill>,
%rlo_0<imp-def>, %rhi_0<imp-def,dead>
%gpregs_3<def> = COPY %rlo_0<kill>
instr_jump %gpregs_31<kill>, %gpregs_3
Machine Code w. virt. regs
Reg. allocation
Machine Code w. machine regs
30/
32
Assembly printing (finally)
Live Ins: %gpregs_3 %gpregs_4 %gpregs_5
%gpregs_3<def> = instr_addu %gpregs_3<kill>, %gpregs_4<kill>
instr_div %gpregs_3<kill>, %gpregs_5<kill>,
%rlo_0<imp-def>, %rhi_0<imp-def,dead>
%gpregs_3<def> = COPY %rlo_0<kill>
instr_jump %gpregs_31<kill>, %gpregs_3
Machine Code w. machine regs
Passes after RA
Machine Code w. machine regs
$foo:
ADDIU $1, $1, -8
SW $31, +4 ( $1 )
SW $2, 0 ( $1 )
MOVE $2, $1
LW $31, +4 ( $1 )
ADDU $3, $3, $4
DIV $3, $5
LW $2, 0 ( $1 )
MFLO $3
ADDIU $1, $1, +8
JR $31
• Additional instructions were added
in Prologue/Epilogue Insertion &
Frame Finalization
• These instructions can be
optimized away for leaf calls
Original code
int foo
(int aa, int bb,
int cc)
{
int sum = aa + bb;
return sum / cc;
}
31/
32
Summary
• Languages and representations used in LLVM
 Frontend
 C/C++
 AST
 LLVM IR
 Optimizer
 LLVM IR
 Backend
 LLVM IR
 Selection DAG with SD nodes
 Selection DAG with Machine instruction nodes
 Machine Code w. virt. regs
 Machine Code w. machine regs
 Assembly code
• Original representation abstraction is lowered down to
the target machine assembly code
Frontend
Optimizer
Backend
C/C++ language
LLVM IR
LLVM IR
Assembly (textual)
32/
32
References
• [1] Christian Plessl: Introduction to the LLVM Compiler
Framework, Univ. of Paderborn, 2012
• [2] Olaf Krzikalla: Performing Source-to-Source Transformations
with Clang, European LLVM Conference Paris, 2013
• [3] Dan Gohman: SelectionDAG Phases- llvm.org/devmtg/2008-
08/Gohman_CodeGenAndSelectionDAGs.pdf
33/
32

07 140430-ipp-languages used in llvm during compilation

  • 1.
    Languages Used in LLVMDuring Compilation IPP, FIT BUT Adam Husár, ihusar@fit.vutbr.cz 30.4.2014
  • 2.
    Contents • Programming electronicsystems • LLVM compiler and intermediate representations used during compilation  Frontend  Parsing  Semantics analysis  IR generation  Optimizer  Backend  Instruction selection  Register allocation  Assembly printing 2 /32
  • 3.
    Programming electronic systems Electrons Programmer El. components Naturallanguage Programming language El. voltage RTL Compiler ISA (architecture) Microarchitecture 3 /32
  • 4.
    Programming electronic systems Electrons Programmer El. components Naturallanguage Programming language El. voltage RTL Compiler ISA (architecture) Microarchitecture 4 /32
  • 5.
    C/C++ language programto binary representation C/C++ compiler Assembler Linker C/C++ language Compiler Binary (w/o relocations) Assembly (textual) Binary (w. relocations) 5 /32
  • 6.
    C/C++ language programto binary representation C/C++ compiler Assembler Linker C/C++ language Compiler Binary (w/o relocations) Assembly (textual) Binary (w. relocations) 6 /32
  • 7.
    LLVM compiler • LLVM– original acronym meaning was Low Level Virtual Machine, now just LLVM • Collection of tools and libraries,.competition to gcc (GNU Compiler Collection) LLVM C/C++ compiler Frontend Optimizer Backend C/C++ language LLVM IR LLVM IR Assembly (textual) • LLVM has many different frontends  C/C++, Objective C/C++, Haskell, Open CL, Ada, ... • and backends  x86, ARM, MicroBLaze, MIPS, PowerPC, SPARC, R600, Hexagon, ... 7 /32
  • 8.
    LLVM compiler components LLVMC/C++ compiler Frontend Optimizer Backend C/C++ language LLVM IR LLVM IR Assembly (textual) 8 /32
  • 9.
    Frontend compilation • Frontendtransforms input source code into a compiler intermediate representation • LLVM frontend is called clang • AST (abstract syntax tree) is an internal representation used in frontend • For C/C++ must know target architecture data type sizes (e.g. int size - 16 or 32 bits, and pointer size – 16, 32, or 64) C language Tokens AST AST LLVM IR 9 /32
  • 10.
    Example – C-> AST int foo(int aa, int bb, int cc) { int sum = aa + bb; return sum / cc; } 10/ 32
  • 11.
    AST – AbstractSyntax Tree • Representation used to do semantic checks • Contains all information about input source code, can be used to reconstruct original source code • Source-to-source compilers • Static analysis (LLVM static analyzer) • Clang also performs some optimizations over AST such as inlining 11/ 32
  • 12.
    (used slides from[2]) 12/ 32
  • 13.
    (used slides from[2]) Source to source compilation 13/ 32
  • 14.
    Example AST ->LLVM IR • The previous AST is transformed into the LLVM IR (representation ASTint foo (int aa, int bb, int cc) { int sum = aa + bb; return sum / cc; } 14/ 32
  • 15.
    LLVM IR • Commoninternal representation for optimization passes • Used both as input for backend and for interpreter/virtual machine • Very close to assembly languages with several differences  Typed  integer (iX)  floating point  vector  structures, arrays, unions  Unlimited number of registers (temporary variables)  SSA – based  Operators have their signedness specified  Some high-level operations, e.g. exception handling Frontend Optimizer Backend C/C++ language LLVM IR LLVM IR Assembly 15/ 32
  • 16.
    LLVM IR operations(1/2) • Standard integer and floating point operations  add, sub, mul, udiv, sdiv, and, or, xor, shifts, ...  fadd, fsub, fmul, ... • Conversions  trunc .. to, zext .. to, ... • Comparisons, selects  icmp, fcmp, select, phi, ... • Memory access and addressing  load, store  fence, cmpxchg, ...  getelementptr 16/ 32
  • 17.
    LLVM IR operations(2/2) • Terminator instructions  br, switch, ret, ... • Other instructions  call, va_arg, • Intrinsic instructions  garbage collector  code generator  standard C library  memcpy, memmove, sqrt, ...  debugger  exception handling 17/ 32
  • 18.
    (used slides from[1]) 18/ 32
  • 19.
    (used slides from[1]) (used slides from [1]) 19/ 32
  • 20.
  • 21.
    LLVM compiler components LLVMC/C++ compiler Frontend Optimizer Backend C/C++ language LLVM IR LLVM IR Assembly (textual) 21/ 32
  • 22.
    Optimizer • Also calledmidend • Uses only LLVM IR language • Over 80 transform and analysis passes • Analysis pass – computes analysis information, may use analysis information from other analysis passes • Transform pass – changes LLVM IR program representation into equivalent program • Pass ordering is important, several transformations are executed repeatedly • Mostly architecture independent Frontend Optimizer Backend C/C++ language LLVM IR LLVM IR Assembly 22/ 32
  • 23.
    LLVM Optimizer Passes $opt-O3 test.ll -o testO3 -debug-pass=Structure No Alias Analysis (always returns 'may' alias) Andersens Alias Analysis (statefull AA impl) Type-Based Alias Analysis Basic Alias Analysis (stateless AA impl) ModulePass Manager Global Variable Optimizer Interprocedural Sparse Conditional Constant Propagation Dead Argument Elimination FunctionPass Manager Combine redundant instructions Simplify the CFG CallGraph Construction Call Graph SCC Pass Manager Remove unused exception handling info Inline Cost Analysis Function Integration/Inlining Deduce function attributes Promote 'by reference' arguments to scalars FunctionPass Manager SROA Dominator Tree Construction Early CSE Lazy Value Information Analysis Jump Threading Value Propagation Simplify the CFG Combine redundant instructions Tail Call Elimination Simplify the CFG Reassociate expressions Dominator Tree Construction Natural Loop Information Loop Pass Manager Canonicalize natural loops Loop-Closed SSA Form Pass Rotate Loops Loop Invariant Code Motion Loop-Closed SSA Form Pass Unswitch loops Combine redundant instructions Scalar Evolution Analysis Loop Pass Manager Canonicalize natural loops Loop-Closed SSA Form Pass Induction Variable Simplification Recognize loop idioms Delete dead loops Unroll loops Memory Dependence Analysis Global Value Numbering Memory Dependence Analysis MemCpy Optimization Sparse Conditional Constant Propagation Combine redundant instructions Lazy Value Information Analysis Jump Threading Value Propagation Dominator Tree Construction Memory Dependence Analysis Dead Store Elimination Natural Loop Information Scalar Evolution Analysis SLP Vectorizer Aggressive Dead Code Elimination Simplify the CFG ... Dead Global Elimination Merge Duplicate Global Constants FunctionPass Manager Preliminary module verification Dominator Tree Construction Module Verifier Bitcode Writer 23/ 32
  • 24.
    Optimizer - ExampleOptimizer LLVM IR LLVM IR Original code int foo (int aa, int bb, int cc) { int sum = aa + bb; return sum / cc; } 24/ 32
  • 25.
    LLVM compiler components LLVMC/C++ compiler Frontend Optimizer Backend C/C++ language LLVM IR LLVM IR Assembly (textual) 25/ 32
  • 26.
    Backend • Backend (alsocalled code generator) transforms LLVM IR into assembly code • Architecture dependent • Only main transformation passes are shown here: Lowering Instr. selection Assembly printer LLVM IR Selection DAG w. SD (LLVM) nodes Selection DAG w. Machine Instruction nodes Assembly code Scheduling Machine Code w. virt. regs Reg. allocation Machine Code w. machine regs 26/ 32
  • 27.
    Lowering, instruction selection, andbasic scheduling • Switch to presentation Dan Gohman: SelectionDAG Phases ([3]) Lowering Instr. selection Assembly printer LLVM IR Selection DAG w. SD (LLVM) nodes Selection DAG w. Machine Instruction nodes Assembly code Scheduling Machine Code w. virt. regs Reg. allocation Machine Code w. machine regs 27/ 32
  • 28.
    LLVM IR ->Machine code Function Live Ins: %gpregs_3 in %vreg0, %gpregs_4 in %vreg1, %gpregs_5 in %vreg2 Live Ins: %gpregs_3 %gpregs_4 %gpregs_5 %vreg2<def> = COPY %gpregs_5 %vreg1<def> = COPY %gpregs_4 %vreg0<def> = COPY %gpregs_3 %vreg3<def> = instr_addu %vreg0, %vreg1 instr_div %vreg3<kill>, %vreg2, %rlo_0<imp-def>,%rhi_0<imp-def,dead> %vreg4<def> = COPY %rlo_0 %gpregs_3<def> = COPY %vreg4 instr_jump %gpregs_31, %gpregs_3 Lowering Instr. selection LLVM IR Selection DAG w. SD (LLVM) nodes Selection DAG w. Machine Instruction nodes Scheduling 28/ 32
  • 29.
    Machine code passesin backend Expand ISel Pseudo-instructions Tail Duplication Optimize machine instruction PHIs Slot index numbering Merge disjoint stack slots Local Stack Slot Allocation Remove dead machine instructions Machine Loop Invariant Code Motion Machine Common Subexpression Elimination Machine code sinking Peephole Optimizations Process Implicit Definitions Remove unreachable machine basic blocks Live Variable Analysis Eliminate PHI nodes for register allocation Two-Address instruction pass Slot index numbering Live Interval Analysis Simple Register Coalescing Debug Variable Analysis Live Stack Slot Analysis Virtual Register Map Live Register Matrix Virtual Register Rewriter Stack Slot Coloring Machine Loop Invariant Code Motion Prologue/Epilogue Insertion & Frame Finalization Control Flow Optimizer Tail Duplication Machine Copy Propagation Pass Post-RA pseudo instruction expansion pass Post RA top-down list latency scheduler Analyze Machine Code For Garbage Collection Branch Probability Basic Block Placement Assembly Printing 29/ 32
  • 30.
    Register allocation Live Ins:%gpregs_3 %gpregs_4 %gpregs_5 %vreg2<def> = COPY %gpregs_5 %vreg1<def> = COPY %gpregs_4 %vreg0<def> = COPY %gpregs_3 %vreg3<def> = instr_addu %vreg0, %vreg1 instr_div %vreg3<kill>, %vreg2, %rlo_0<imp-def>,%rhi_0<imp-def,dead> %vreg4<def> = COPY %rlo_0 %gpregs_3<def> = COPY %vreg4 instr_jump %gpregs_31, %gpregs_3 Live Ins: %gpregs_3 %gpregs_4 %gpregs_5 %gpregs_3<def> = instr_addu %gpregs_3<kill>, %gpregs_4<kill> instr_div %gpregs_3<kill>, %gpregs_5<kill>, %rlo_0<imp-def>, %rhi_0<imp-def,dead> %gpregs_3<def> = COPY %rlo_0<kill> instr_jump %gpregs_31<kill>, %gpregs_3 Machine Code w. virt. regs Reg. allocation Machine Code w. machine regs 30/ 32
  • 31.
    Assembly printing (finally) LiveIns: %gpregs_3 %gpregs_4 %gpregs_5 %gpregs_3<def> = instr_addu %gpregs_3<kill>, %gpregs_4<kill> instr_div %gpregs_3<kill>, %gpregs_5<kill>, %rlo_0<imp-def>, %rhi_0<imp-def,dead> %gpregs_3<def> = COPY %rlo_0<kill> instr_jump %gpregs_31<kill>, %gpregs_3 Machine Code w. machine regs Passes after RA Machine Code w. machine regs $foo: ADDIU $1, $1, -8 SW $31, +4 ( $1 ) SW $2, 0 ( $1 ) MOVE $2, $1 LW $31, +4 ( $1 ) ADDU $3, $3, $4 DIV $3, $5 LW $2, 0 ( $1 ) MFLO $3 ADDIU $1, $1, +8 JR $31 • Additional instructions were added in Prologue/Epilogue Insertion & Frame Finalization • These instructions can be optimized away for leaf calls Original code int foo (int aa, int bb, int cc) { int sum = aa + bb; return sum / cc; } 31/ 32
  • 32.
    Summary • Languages andrepresentations used in LLVM  Frontend  C/C++  AST  LLVM IR  Optimizer  LLVM IR  Backend  LLVM IR  Selection DAG with SD nodes  Selection DAG with Machine instruction nodes  Machine Code w. virt. regs  Machine Code w. machine regs  Assembly code • Original representation abstraction is lowered down to the target machine assembly code Frontend Optimizer Backend C/C++ language LLVM IR LLVM IR Assembly (textual) 32/ 32
  • 33.
    References • [1] ChristianPlessl: Introduction to the LLVM Compiler Framework, Univ. of Paderborn, 2012 • [2] Olaf Krzikalla: Performing Source-to-Source Transformations with Clang, European LLVM Conference Paris, 2013 • [3] Dan Gohman: SelectionDAG Phases- llvm.org/devmtg/2008- 08/Gohman_CodeGenAndSelectionDAGs.pdf 33/ 32