LLVM API Documentation
00001 //===-LTOCodeGenerator.h - LLVM Link Time Optimizer -----------------------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file declares the LTOCodeGenerator class. 00011 // 00012 // LTO compilation consists of three phases: Pre-IPO, IPO and Post-IPO. 00013 // 00014 // The Pre-IPO phase compiles source code into bitcode file. The resulting 00015 // bitcode files, along with object files and libraries, will be fed to the 00016 // linker to through the IPO and Post-IPO phases. By using obj-file extension, 00017 // the resulting bitcode file disguises itself as an object file, and therefore 00018 // obviates the need of writing a special set of the make-rules only for LTO 00019 // compilation. 00020 // 00021 // The IPO phase perform inter-procedural analyses and optimizations, and 00022 // the Post-IPO consists two sub-phases: intra-procedural scalar optimizations 00023 // (SOPT), and intra-procedural target-dependent code generator (CG). 00024 // 00025 // As of this writing, we don't separate IPO and the Post-IPO SOPT. They 00026 // are intermingled together, and are driven by a single pass manager (see 00027 // PassManagerBuilder::populateLTOPassManager()). 00028 // 00029 // The "LTOCodeGenerator" is the driver for the IPO and Post-IPO stages. 00030 // The "CodeGenerator" here is bit confusing. Don't confuse the "CodeGenerator" 00031 // with the machine specific code generator. 00032 // 00033 //===----------------------------------------------------------------------===// 00034 00035 #ifndef LLVM_LTO_LTOCODEGENERATOR_H 00036 #define LLVM_LTO_LTOCODEGENERATOR_H 00037 00038 #include "llvm-c/lto.h" 00039 #include "llvm/ADT/ArrayRef.h" 00040 #include "llvm/ADT/SmallPtrSet.h" 00041 #include "llvm/ADT/StringMap.h" 00042 #include "llvm/Linker/Linker.h" 00043 #include "llvm/Target/TargetOptions.h" 00044 #include <string> 00045 #include <vector> 00046 00047 namespace llvm { 00048 class LLVMContext; 00049 class DiagnosticInfo; 00050 class GlobalValue; 00051 class Mangler; 00052 class MemoryBuffer; 00053 class TargetLibraryInfo; 00054 class TargetMachine; 00055 class raw_ostream; 00056 00057 //===----------------------------------------------------------------------===// 00058 /// C++ class which implements the opaque lto_code_gen_t type. 00059 /// 00060 struct LTOCodeGenerator { 00061 static const char *getVersionString(); 00062 00063 LTOCodeGenerator(); 00064 ~LTOCodeGenerator(); 00065 00066 // Merge given module, return true on success. 00067 bool addModule(struct LTOModule*, std::string &errMsg); 00068 00069 void setTargetOptions(TargetOptions options); 00070 void setDebugInfo(lto_debug_model); 00071 void setCodePICModel(lto_codegen_model); 00072 00073 void setCpu(const char *mCpu) { MCpu = mCpu; } 00074 void setAttr(const char *mAttr) { MAttr = mAttr; } 00075 00076 void addMustPreserveSymbol(const char *sym) { MustPreserveSymbols[sym] = 1; } 00077 00078 // To pass options to the driver and optimization passes. These options are 00079 // not necessarily for debugging purpose (The function name is misleading). 00080 // This function should be called before LTOCodeGenerator::compilexxx(), 00081 // and LTOCodeGenerator::writeMergedModules(). 00082 void setCodeGenDebugOptions(const char *opts); 00083 00084 // Parse the options set in setCodeGenDebugOptions. Like 00085 // setCodeGenDebugOptions, this must be called before 00086 // LTOCodeGenerator::compilexxx() and LTOCodeGenerator::writeMergedModules() 00087 void parseCodeGenDebugOptions(); 00088 00089 // Write the merged module to the file specified by the given path. 00090 // Return true on success. 00091 bool writeMergedModules(const char *path, std::string &errMsg); 00092 00093 // Compile the merged module into a *single* object file; the path to object 00094 // file is returned to the caller via argument "name". Return true on 00095 // success. 00096 // 00097 // NOTE that it is up to the linker to remove the intermediate object file. 00098 // Do not try to remove the object file in LTOCodeGenerator's destructor 00099 // as we don't who (LTOCodeGenerator or the obj file) will last longer. 00100 bool compile_to_file(const char **name, 00101 bool disableOpt, 00102 bool disableInline, 00103 bool disableGVNLoadPRE, 00104 std::string &errMsg); 00105 00106 // As with compile_to_file(), this function compiles the merged module into 00107 // single object file. Instead of returning the object-file-path to the caller 00108 // (linker), it brings the object to a buffer, and return the buffer to the 00109 // caller. This function should delete intermediate object file once its content 00110 // is brought to memory. Return NULL if the compilation was not successful. 00111 const void *compile(size_t *length, 00112 bool disableOpt, 00113 bool disableInline, 00114 bool disableGVNLoadPRE, 00115 std::string &errMsg); 00116 00117 void setDiagnosticHandler(lto_diagnostic_handler_t, void *); 00118 00119 private: 00120 void initializeLTOPasses(); 00121 00122 bool generateObjectFile(raw_ostream &out, bool disableOpt, bool disableInline, 00123 bool disableGVNLoadPRE, std::string &errMsg); 00124 void applyScopeRestrictions(); 00125 void applyRestriction(GlobalValue &GV, ArrayRef<StringRef> Libcalls, 00126 std::vector<const char *> &MustPreserveList, 00127 SmallPtrSetImpl<GlobalValue *> &AsmUsed, 00128 Mangler &Mangler); 00129 bool determineTarget(std::string &errMsg); 00130 00131 static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context); 00132 00133 void DiagnosticHandler2(const DiagnosticInfo &DI); 00134 00135 typedef StringMap<uint8_t> StringSet; 00136 00137 LLVMContext &Context; 00138 Linker IRLinker; 00139 TargetMachine *TargetMach; 00140 bool EmitDwarfDebugInfo; 00141 bool ScopeRestrictionsDone; 00142 lto_codegen_model CodeModel; 00143 StringSet MustPreserveSymbols; 00144 StringSet AsmUndefinedRefs; 00145 std::unique_ptr<MemoryBuffer> NativeObjectFile; 00146 std::vector<char *> CodegenOptions; 00147 std::string MCpu; 00148 std::string MAttr; 00149 std::string NativeObjectPath; 00150 TargetOptions Options; 00151 lto_diagnostic_handler_t DiagHandler; 00152 void *DiagContext; 00153 }; 00154 } 00155 #endif