1 //===-lto.cpp - LLVM Link Time Optimizer ----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Link Time Optimization library. This library is
11 // intended to be used by linker to optimize code at link time.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/lto.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/CodeGen/CommandFlags.h"
18 #include "llvm/IR/DiagnosticInfo.h"
19 #include "llvm/IR/DiagnosticPrinter.h"
20 #include "llvm/IR/LLVMContext.h"
21 #include "llvm/LTO/LTOCodeGenerator.h"
22 #include "llvm/LTO/LTOModule.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include "llvm/Support/Signals.h"
25 #include "llvm/Support/TargetSelect.h"
27 // extra command-line flags needed for LTOCodeGenerator
30 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
37 DisableInline("disable-inlining", cl::init(false),
38 cl::desc("Do not run the inliner pass"));
41 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
42 cl::desc("Do not run the GVN load PRE pass"));
45 DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
46 cl::desc("Do not run loop or slp vectorization during LTO"));
49 static bool VerifyByDefault = false;
51 static bool VerifyByDefault = true;
54 static cl::opt<bool> DisableVerify(
55 "disable-llvm-verifier", cl::init(!VerifyByDefault),
56 cl::desc("Don't run the LLVM verifier during the optimization pipeline"));
58 // Holds most recent error string.
59 // *** Not thread safe ***
60 static std::string sLastErrorString;
62 // Holds the initialization state of the LTO module.
63 // *** Not thread safe ***
64 static bool initialized = false;
66 // Holds the command-line option parsing state of the LTO module.
67 static bool parsedOptions = false;
69 static LLVMContext *LTOContext = nullptr;
71 static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
72 if (DI.getSeverity() != DS_Error) {
73 DiagnosticPrinterRawOStream DP(errs());
78 sLastErrorString = "";
80 raw_string_ostream Stream(sLastErrorString);
81 DiagnosticPrinterRawOStream DP(Stream);
84 sLastErrorString += '\n';
87 // Initialize the configured targets if they have not been initialized.
88 static void lto_initialize() {
91 // Dialog box on crash disabling doesn't work across DLL boundaries, so do
93 llvm::sys::DisableSystemDialogsOnCrash();
96 InitializeAllTargetInfos();
97 InitializeAllTargets();
98 InitializeAllTargetMCs();
99 InitializeAllAsmParsers();
100 InitializeAllAsmPrinters();
101 InitializeAllDisassemblers();
103 LTOContext = &getGlobalContext();
104 LTOContext->setDiagnosticHandler(diagnosticHandler, nullptr, true);
111 static void handleLibLTODiagnostic(lto_codegen_diagnostic_severity_t Severity,
112 const char *Msg, void *) {
113 sLastErrorString = Msg;
114 sLastErrorString += "\n";
117 // This derived class owns the native object file. This helps implement the
118 // libLTO API semantics, which require that the code generator owns the object
120 struct LibLTOCodeGenerator : LTOCodeGenerator {
121 LibLTOCodeGenerator() : LTOCodeGenerator(*LTOContext) {
122 setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
123 LibLTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
124 : LTOCodeGenerator(*Context), OwnedContext(std::move(Context)) {
125 setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
127 // Reset the module first in case MergedModule is created in OwnedContext.
128 // Module must be destructed before its context gets destructed.
129 ~LibLTOCodeGenerator() { resetMergedModule(); }
131 std::unique_ptr<MemoryBuffer> NativeObjectFile;
132 std::unique_ptr<LLVMContext> OwnedContext;
137 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LibLTOCodeGenerator, lto_code_gen_t)
138 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
140 // Convert the subtarget features into a string to pass to LTOCodeGenerator.
141 static void lto_add_attrs(lto_code_gen_t cg) {
142 LTOCodeGenerator *CG = unwrap(cg);
145 for (unsigned i = 0; i < MAttrs.size(); ++i) {
148 attrs.append(MAttrs[i]);
151 CG->setAttr(attrs.c_str());
154 if (OptLevel < '0' || OptLevel > '3')
155 report_fatal_error("Optimization level must be between 0 and 3");
156 CG->setOptLevel(OptLevel - '0');
159 extern const char* lto_get_version() {
160 return LTOCodeGenerator::getVersionString();
163 const char* lto_get_error_message() {
164 return sLastErrorString.c_str();
167 bool lto_module_is_object_file(const char* path) {
168 return LTOModule::isBitcodeFile(path);
171 bool lto_module_is_object_file_for_target(const char* path,
172 const char* target_triplet_prefix) {
173 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
176 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
179 bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
180 return LTOModule::isBitcodeFile(mem, length);
184 lto_module_is_object_file_in_memory_for_target(const void* mem,
186 const char* target_triplet_prefix) {
187 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
190 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
193 lto_module_t lto_module_create(const char* path) {
195 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
196 ErrorOr<std::unique_ptr<LTOModule>> M =
197 LTOModule::createFromFile(*LTOContext, path, Options);
200 return wrap(M->release());
203 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
205 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
206 ErrorOr<std::unique_ptr<LTOModule>> M =
207 LTOModule::createFromOpenFile(*LTOContext, fd, path, size, Options);
210 return wrap(M->release());
213 lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
218 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
219 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromOpenFileSlice(
220 *LTOContext, fd, path, map_size, offset, Options);
223 return wrap(M->release());
226 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
228 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
229 ErrorOr<std::unique_ptr<LTOModule>> M =
230 LTOModule::createFromBuffer(*LTOContext, mem, length, Options);
233 return wrap(M->release());
236 lto_module_t lto_module_create_from_memory_with_path(const void* mem,
240 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
241 ErrorOr<std::unique_ptr<LTOModule>> M =
242 LTOModule::createFromBuffer(*LTOContext, mem, length, Options, path);
245 return wrap(M->release());
248 lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
251 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
252 ErrorOr<std::unique_ptr<LTOModule>> M =
253 LTOModule::createInLocalContext(mem, length, Options, path);
256 return wrap(M->release());
259 lto_module_t lto_module_create_in_codegen_context(const void *mem,
264 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
265 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createInContext(
266 mem, length, Options, path, &unwrap(cg)->getContext());
267 return wrap(M->release());
270 void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
272 const char* lto_module_get_target_triple(lto_module_t mod) {
273 return unwrap(mod)->getTargetTriple().c_str();
276 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
277 return unwrap(mod)->setTargetTriple(triple);
280 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
281 return unwrap(mod)->getSymbolCount();
284 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
285 return unwrap(mod)->getSymbolName(index);
288 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
289 unsigned int index) {
290 return unwrap(mod)->getSymbolAttributes(index);
293 const char* lto_module_get_linkeropts(lto_module_t mod) {
294 return unwrap(mod)->getLinkerOpts();
297 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
298 lto_diagnostic_handler_t diag_handler,
300 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
303 static lto_code_gen_t createCodeGen(bool InLocalContext) {
306 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
308 LibLTOCodeGenerator *CodeGen =
309 InLocalContext ? new LibLTOCodeGenerator(make_unique<LLVMContext>())
310 : new LibLTOCodeGenerator();
311 CodeGen->setTargetOptions(Options);
312 return wrap(CodeGen);
315 lto_code_gen_t lto_codegen_create(void) {
316 return createCodeGen(/* InLocalContext */ false);
319 lto_code_gen_t lto_codegen_create_in_local_context(void) {
320 return createCodeGen(/* InLocalContext */ true);
323 void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
325 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
326 return !unwrap(cg)->addModule(unwrap(mod));
329 void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {
330 unwrap(cg)->setModule(std::unique_ptr<LTOModule>(unwrap(mod)));
333 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
334 unwrap(cg)->setDebugInfo(debug);
338 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
340 case LTO_CODEGEN_PIC_MODEL_STATIC:
341 unwrap(cg)->setCodePICModel(Reloc::Static);
343 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
344 unwrap(cg)->setCodePICModel(Reloc::PIC_);
346 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
347 unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC);
349 case LTO_CODEGEN_PIC_MODEL_DEFAULT:
350 unwrap(cg)->setCodePICModel(Reloc::Default);
353 sLastErrorString = "Unknown PIC model";
357 void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
358 return unwrap(cg)->setCpu(cpu);
361 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
362 // In here only for backwards compatibility. We use MC now.
365 void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
367 // In here only for backwards compatibility. We use MC now.
370 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
371 const char *symbol) {
372 unwrap(cg)->addMustPreserveSymbol(symbol);
375 static void maybeParseOptions(lto_code_gen_t cg) {
376 if (!parsedOptions) {
377 unwrap(cg)->parseCodeGenDebugOptions();
379 parsedOptions = true;
383 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
384 maybeParseOptions(cg);
385 return !unwrap(cg)->writeMergedModules(path);
388 const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
389 maybeParseOptions(cg);
390 LibLTOCodeGenerator *CG = unwrap(cg);
391 CG->NativeObjectFile =
392 CG->compile(DisableVerify, DisableInline, DisableGVNLoadPRE,
393 DisableLTOVectorization);
394 if (!CG->NativeObjectFile)
396 *length = CG->NativeObjectFile->getBufferSize();
397 return CG->NativeObjectFile->getBufferStart();
400 bool lto_codegen_optimize(lto_code_gen_t cg) {
401 maybeParseOptions(cg);
402 return !unwrap(cg)->optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
403 DisableLTOVectorization);
406 const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {
407 maybeParseOptions(cg);
408 LibLTOCodeGenerator *CG = unwrap(cg);
409 CG->NativeObjectFile = CG->compileOptimized();
410 if (!CG->NativeObjectFile)
412 *length = CG->NativeObjectFile->getBufferSize();
413 return CG->NativeObjectFile->getBufferStart();
416 bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
417 maybeParseOptions(cg);
418 return !unwrap(cg)->compile_to_file(
419 name, DisableVerify, DisableInline, DisableGVNLoadPRE,
420 DisableLTOVectorization);
423 void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
424 unwrap(cg)->setCodeGenDebugOptions(opt);
427 unsigned int lto_api_version() { return LTO_API_VERSION; }
429 void lto_codegen_set_should_internalize(lto_code_gen_t cg,
430 bool ShouldInternalize) {
431 unwrap(cg)->setShouldInternalize(ShouldInternalize);
434 void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
435 lto_bool_t ShouldEmbedUselists) {
436 unwrap(cg)->setShouldEmbedUselists(ShouldEmbedUselists);