1The LLVM CompilerThe LLVM CompilerFramework and InfrastructureFramework and Infrastructure15-745: Optimizing Compilers15-745: Optimizing CompilersDavid David KoesKoes1/22/20081/22/2008Substantial portions courtesy Chris Lattner and Vikram Adve2LLVM Compiler SystemLLVM Compiler SystemThe LLVM Compiler InfrastructureThe LLVM Compiler InfrastructureProvides reusable components for building compilersProvides reusable components for building compilersReduce the time/cost to build a new compilerReduce the time/cost to build a new compilerBuild static compilers, Build static compilers, JITsJITs, trace-based optimizers, ..., trace-based optimizers, ...The LLVM Compiler FrameworkThe LLVM Compiler FrameworkEnd-to-end compilers using the LLVM infrastructureEnd-to-end compilers using the LLVM infrastructureC and C++ C and C++ gcc frontendgcc frontendBackends Backends for C, X86, for C, X86, SparcSparc, PowerPC, Alpha, Arm,, PowerPC, Alpha, Arm,Thumb, IA-64Thumb, IA-64……3Three primary LLVM componentsThree primary LLVM componentsThe LLVM The LLVM Virtual Instruction SetVirtual Instruction SetThe common language- and target-independent IRThe common language- and target-independent IRInternal (IR) and external (persistent) representationInternal (IR) and external (persistent) representationA collection of well-integrated librariesA collection of well-integrated librariesAnalyses, optimizations, code generators, JITAnalyses, optimizations, code generators, JITcompiler, garbage collection support, profiling, compiler, garbage collection support, profiling, ……A collection of tools built from the librariesA collection of tools built from the librariesAssemblers, automatic debugger, linker, codeAssemblers, automatic debugger, linker, codegenerator, compiler driver, modular optimizer, generator, compiler driver, modular optimizer, ……4Tutorial OverviewTutorial OverviewIntroduction to the running exampleIntroduction to the running exampleLLVM C/C++ Compiler OverviewLLVM C/C++ Compiler OverviewHigh-level view of an example LLVM compilerHigh-level view of an example LLVM compilerThe LLVM Virtual Instruction SetThe LLVM Virtual Instruction SetIR overview and type-systemIR overview and type-systemLLVM C++ IR and important APILLVM C++ IR and important API’’ssBasics, Basics, PassManagerPassManager, dataflow, , dataflow, ArgPromotionArgPromotionImportant LLVM ToolsImportant LLVM Tools25Running example: Running example: arg arg promotionpromotionConsider use of by-reference parameters:Consider use of by-reference parameters:int int callee(constcallee(const int &X) { int &X) { return X+1; return X+1;}}int caller() {int caller() { return callee(4); return callee(4);}}int int callee(constcallee(const int *X) { int *X) { return *X+1; return *X+1; // memory load// memory load}}int caller() {int caller() { int int tmptmp; ; // stack object// stack object tmptmp = 4; = 4; // memory store// memory store return return callee(&tmpcallee(&tmp););}}compiles toEliminated load in calleeEliminated store in callerEliminated stack slot for ‘tmp’int int callee(intcallee(int X) { X) { return X+1; return X+1;}}int caller() {int caller() { return callee(4); return callee(4);}}We want:We want:6Why is this hard?Why is this hard?Requires interprocedural analysis:Requires interprocedural analysis:Must change the prototype of the Must change the prototype of the calleecalleeMust update all call sites Must update all call sites we must we must knowknow all callers all callersWhat about callers outside the translation unit?What about callers outside the translation unit?Requires alias analysis:Requires alias analysis:Reference could alias other pointers in Reference could alias other pointers in calleecalleeMust know that loaded value doesnMust know that loaded value doesn’’t change fromt change fromfunction entry to the loadfunction entry to the loadMust know the pointer is not being stored throughMust know the pointer is not being stored throughReference might not be to a stack object!Reference might not be to a stack object!7Tutorial OverviewTutorial OverviewIntroduction to the running exampleIntroduction to the running exampleLLVM C/C++ Compiler OverviewLLVM C/C++ Compiler OverviewHigh-level view of an example LLVM compilerHigh-level view of an example LLVM compilerThe LLVM Virtual Instruction SetThe LLVM Virtual Instruction SetIR overview and type-systemIR overview and type-systemLLVM C++ IR and important APILLVM C++ IR and important API’’ssBasics, Basics, PassManagerPassManager, dataflow, , dataflow, ArgPromotionArgPromotionImportant LLVM ToolsImportant LLVM Tools8The LLVM C/C++ CompilerThe LLVM C/C++ CompilerFrom the high level, it is a standard compiler:From the high level, it is a standard compiler:Compatible with standard Compatible with standard makefilesmakefilesUses GCCUses GCC 4.2 C and C++ parser4.2 C and C++ parserGenerates nativeGenerates native executables/object files/assemblyexecutables/object files/assemblyDistinguishing features:Distinguishing features:Uses LLVM optimizers, not GCC optimizersUses LLVM optimizers, not GCC optimizersPass Pass -emit-llvm -emit-llvm to outputto output LLVM IRLLVM IR-S: human readable -S: human readable ““assemblyassembly””-c-c: efficient: efficient ““bitcodebitcode”” binary binary39Looking into events at compile-timeLooking into events at compile-timellvm-gcc/llvm-g++ -O -SC/C++ file assemblyGENERICGIMPLE(tree-ssa)LLVM IRMachineCode IR>50 LLVM Analysis & Optimization Passes:Dead Global Elimination, IP Constant Propagation, DeadArgument Elimination, Inlining, Reassociation, LICM, LoopOpts, Memory Promotion, Dead Store Elimination, ADCE, …IR-emit-llvmLLVMasm10Looking into events at link-timeLooking into events at link-timeLLVM bitcode .o fileLLVM bitcode .o filellvm-ldexecutableLLVMLinkerLink-timeOptimizer.bc file for LLVM JIT>30 LLVM Analysis &Optimization PassesOptionally “internalizes”:marks most functions asinternal, to improve IPOPerfect place for argumentpromotion optimization!Native executable-native11Goals of the compiler designGoals of the compiler
View Full Document