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/LLVMContext.h"
19 #include "llvm/LTO/LTOCodeGenerator.h"
20 #include "llvm/LTO/LTOModule.h"
21 #include "llvm/Support/MemoryBuffer.h"
22 #include "llvm/Support/Signals.h"
23 #include "llvm/Support/TargetSelect.h"
25 // extra command-line flags needed for LTOCodeGenerator
28 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
35 DisableInline("disable-inlining", cl::init(false),
36 cl::desc("Do not run the inliner pass"));
39 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
40 cl::desc("Do not run the GVN load PRE pass"));
43 DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
44 cl::desc("Do not run loop or slp vectorization during LTO"));
46 // Holds most recent error string.
47 // *** Not thread safe ***
48 static std::string sLastErrorString;
50 // Holds the initialization state of the LTO module.
51 // *** Not thread safe ***
52 static bool initialized = false;
54 // Holds the command-line option parsing state of the LTO module.
55 static bool parsedOptions = false;
57 // Initialize the configured targets if they have not been initialized.
58 static void lto_initialize() {
61 // Dialog box on crash disabling doesn't work across DLL boundaries, so do
63 llvm::sys::DisableSystemDialogsOnCrash();
66 InitializeAllTargetInfos();
67 InitializeAllTargets();
68 InitializeAllTargetMCs();
69 InitializeAllAsmParsers();
70 InitializeAllAsmPrinters();
71 InitializeAllDisassemblers();
78 // This derived class owns the native object file. This helps implement the
79 // libLTO API semantics, which require that the code generator owns the object
81 struct LibLTOCodeGenerator : LTOCodeGenerator {
82 LibLTOCodeGenerator() {}
83 LibLTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
84 : LTOCodeGenerator(std::move(Context)) {}
86 std::unique_ptr<MemoryBuffer> NativeObjectFile;
91 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LibLTOCodeGenerator, lto_code_gen_t)
92 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
94 // Convert the subtarget features into a string to pass to LTOCodeGenerator.
95 static void lto_add_attrs(lto_code_gen_t cg) {
96 LTOCodeGenerator *CG = unwrap(cg);
99 for (unsigned i = 0; i < MAttrs.size(); ++i) {
102 attrs.append(MAttrs[i]);
105 CG->setAttr(attrs.c_str());
108 if (OptLevel < '0' || OptLevel > '3')
109 report_fatal_error("Optimization level must be between 0 and 3");
110 CG->setOptLevel(OptLevel - '0');
113 extern const char* lto_get_version() {
114 return LTOCodeGenerator::getVersionString();
117 const char* lto_get_error_message() {
118 return sLastErrorString.c_str();
121 bool lto_module_is_object_file(const char* path) {
122 return LTOModule::isBitcodeFile(path);
125 bool lto_module_is_object_file_for_target(const char* path,
126 const char* target_triplet_prefix) {
127 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
130 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
133 bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
134 return LTOModule::isBitcodeFile(mem, length);
138 lto_module_is_object_file_in_memory_for_target(const void* mem,
140 const char* target_triplet_prefix) {
141 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
144 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
147 lto_module_t lto_module_create(const char* path) {
149 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
150 return wrap(LTOModule::createFromFile(path, Options, sLastErrorString));
153 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
155 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
157 LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString));
160 lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
165 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
166 return wrap(LTOModule::createFromOpenFileSlice(fd, path, map_size, offset,
167 Options, sLastErrorString));
170 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
172 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
173 return wrap(LTOModule::createFromBuffer(mem, length, Options, sLastErrorString));
176 lto_module_t lto_module_create_from_memory_with_path(const void* mem,
180 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
182 LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path));
185 lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
188 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
189 return wrap(LTOModule::createInLocalContext(mem, length, Options,
190 sLastErrorString, path));
193 lto_module_t lto_module_create_in_codegen_context(const void *mem,
198 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
199 return wrap(LTOModule::createInContext(mem, length, Options, sLastErrorString,
200 path, &unwrap(cg)->getContext()));
203 void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
205 const char* lto_module_get_target_triple(lto_module_t mod) {
206 return unwrap(mod)->getTargetTriple().c_str();
209 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
210 return unwrap(mod)->setTargetTriple(triple);
213 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
214 return unwrap(mod)->getSymbolCount();
217 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
218 return unwrap(mod)->getSymbolName(index);
221 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
222 unsigned int index) {
223 return unwrap(mod)->getSymbolAttributes(index);
226 const char* lto_module_get_linkeropts(lto_module_t mod) {
227 return unwrap(mod)->getLinkerOpts();
230 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
231 lto_diagnostic_handler_t diag_handler,
233 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
236 static lto_code_gen_t createCodeGen(bool InLocalContext) {
239 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
241 LibLTOCodeGenerator *CodeGen =
242 InLocalContext ? new LibLTOCodeGenerator(make_unique<LLVMContext>())
243 : new LibLTOCodeGenerator();
244 CodeGen->setTargetOptions(Options);
245 return wrap(CodeGen);
248 lto_code_gen_t lto_codegen_create(void) {
249 return createCodeGen(/* InLocalContext */ false);
252 lto_code_gen_t lto_codegen_create_in_local_context(void) {
253 return createCodeGen(/* InLocalContext */ true);
256 void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
258 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
259 return !unwrap(cg)->addModule(unwrap(mod));
262 void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {
263 unwrap(cg)->setModule(unwrap(mod));
266 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
267 unwrap(cg)->setDebugInfo(debug);
271 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
273 case LTO_CODEGEN_PIC_MODEL_STATIC:
274 unwrap(cg)->setCodePICModel(Reloc::Static);
276 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
277 unwrap(cg)->setCodePICModel(Reloc::PIC_);
279 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
280 unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC);
282 case LTO_CODEGEN_PIC_MODEL_DEFAULT:
283 unwrap(cg)->setCodePICModel(Reloc::Default);
286 sLastErrorString = "Unknown PIC model";
290 void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
291 return unwrap(cg)->setCpu(cpu);
294 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
295 // In here only for backwards compatibility. We use MC now.
298 void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
300 // In here only for backwards compatibility. We use MC now.
303 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
304 const char *symbol) {
305 unwrap(cg)->addMustPreserveSymbol(symbol);
308 static void maybeParseOptions(lto_code_gen_t cg) {
309 if (!parsedOptions) {
310 unwrap(cg)->parseCodeGenDebugOptions();
312 parsedOptions = true;
316 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
317 maybeParseOptions(cg);
318 return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
321 const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
322 maybeParseOptions(cg);
323 LibLTOCodeGenerator *CG = unwrap(cg);
324 CG->NativeObjectFile = CG->compile(DisableInline, DisableGVNLoadPRE,
325 DisableLTOVectorization, sLastErrorString);
326 if (!CG->NativeObjectFile)
328 *length = CG->NativeObjectFile->getBufferSize();
329 return CG->NativeObjectFile->getBufferStart();
332 bool lto_codegen_optimize(lto_code_gen_t cg) {
333 maybeParseOptions(cg);
334 return !unwrap(cg)->optimize(DisableInline,
335 DisableGVNLoadPRE, DisableLTOVectorization,
339 const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {
340 maybeParseOptions(cg);
341 LibLTOCodeGenerator *CG = unwrap(cg);
342 CG->NativeObjectFile = CG->compileOptimized(sLastErrorString);
343 if (!CG->NativeObjectFile)
345 *length = CG->NativeObjectFile->getBufferSize();
346 return CG->NativeObjectFile->getBufferStart();
349 bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
350 maybeParseOptions(cg);
351 return !unwrap(cg)->compile_to_file(
352 name, DisableInline, DisableGVNLoadPRE,
353 DisableLTOVectorization, sLastErrorString);
356 void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
357 unwrap(cg)->setCodeGenDebugOptions(opt);
360 unsigned int lto_api_version() { return LTO_API_VERSION; }
362 void lto_codegen_set_should_internalize(lto_code_gen_t cg,
363 bool ShouldInternalize) {
364 unwrap(cg)->setShouldInternalize(ShouldInternalize);
367 void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
368 lto_bool_t ShouldEmbedUselists) {
369 unwrap(cg)->setShouldEmbedUselists(ShouldEmbedUselists);