The LLVM Compiler Framework and Infrastructure
The LLVM Compiler Framework and Infrastructure
Thumb, IA-64
2
Tutorial Overview
We want:
int callee(int X) {
return X+1;
}
int caller() {
return callee(4);
}
compiles to
Tutorial Overview
Distinguishing features:
Uses LLVM optimizers, not GCC optimizers
Pass -emit-llvm to output LLVM IR
IR
GENERIC
llvm-gcc/llvm-g++ -O -S
GIMPLE
(tree-ssa)
LLVM IR
assembly
Machine
Code IR
-emit-llvm
LLVM
asm
LLVM
Linker
llvm-ld
Link-time
Optimizer
executable
Native executable
Tutorial Overview
12
Goals of LLVM IR
bb:
; preds = %bb, %entry
%i.1 = phi i32 [ 0, %entry ], [ %i.2, %bb ]
%AiAddr = getelementptr float* %A, i32 %i.1
for (i = 0; i < N;
call void @Sum( float* %AiAddr, %pair* %P )
%i.2 = add i32 %i.1, 1
++i)
%exitcond = icmp eq i32 %i.2, %N
Sum(&A[i], &P);
br i1 %exitcond, label %return, label %bb
14
bb:
; preds = %bb, %entry
%i.1 = phi i32 [ 0, %entry ], [ %i.2, %bb ]
%AiAddr = getelementptr float* %A, i32 %i.1
for (i = 0; i < N;
call void @Sum( float* %AiAddr, %pair* %P )
%i.2 = add i32 %i.1, 1
++i)
%exitcond = icmp eq i32 %i.2, %N
Sum(&A[i], &P);
br i1 %exitcond, label %return, label %bb
15
Examples of lowering:
References turn into pointers: T& T*
Complex numbers: complex float { float, float }
Bitfields: struct X { int Y:4; int Z:2; } { i32 }
Inheritance: class T : S { int X; } { S, i32 }
Methods: class T { void foo(); } void foo(T*)
18
19
Other transformation
Update
all
call
sites of
Change
theinstructions
prototype
Insert
load
(-mem2reg) cleans up
callee
for
intothe
allfunction
callers
the rest
20
Tutorial Overview
21
valgrind toinitialized
the rescue!
primitive class variable not automatically
http://valgrind.org
you must manage memory
virtual vs. non-virtual functions
and much much more
23
Types of Pass:
ModulePass: general interprocedural pass
CallGraphSCCPass: bottom-up on the call graph
FunctionPass: process a function at a time
LoopPass: process a natural loop at a time
BasicBlockPass: process a basic block at a time
const {
aliases
data layout
CallGraph
26
bool SimpleArgPromotion::
runOnSCC(const std::vector<CallGraphNode*> &SCC) {
bool Changed = false, LocalChange;
do {
// Iterate until we stop promoting from this SCC.
LocalChange = false;
// Attempt to promote arguments from all functions in this SCC.
for (unsigned i = 0, e = SCC.size(); i != e; ++i)
LocalChange |= PromoteArguments(SCC[i]);
Changed |= LocalChange; // Remember that we changed something.
} while (LocalChange);
return Changed;
// Passes return true if something changed.
}
load P
Modifie
s *P?
Modifie
s *P?
Modifie
s *P?
Entry
load P
load P
175: for (pred_iterator PI = pred_begin(BB), E = pred_end(BB);
PI != E; ++PI)
// Loop over predecessors of BB.
// Check each block from BB to entry (DF search on inverse graph).
for (idf_iterator<BasicBlock*> I = idf_begin(*PI);
I != idf_end(*PI); ++I)
// Might *P be modified in this basic block?
if (AA.canBasicBlockModify(**I, Arg, LoadSize))
return false;
32
33
34
35
Tutorial Overview
37
(default)
41
42
43