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/CodeGen/CommandFlags.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/LTO/LTOCodeGenerator.h"
19 #include "llvm/LTO/LTOModule.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/Signals.h"
22 #include "llvm/Support/TargetSelect.h"
24 // extra command-line flags needed for LTOCodeGenerator
26 DisableOpt("disable-opt", cl::init(false),
27 cl::desc("Do not run any optimization passes"));
30 DisableInline("disable-inlining", cl::init(false),
31 cl::desc("Do not run the inliner pass"));
34 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
35 cl::desc("Do not run the GVN load PRE pass"));
38 DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
39 cl::desc("Do not run loop or slp vectorization during LTO"));
41 // Holds most recent error string.
42 // *** Not thread safe ***
43 static std::string sLastErrorString;
45 // Holds the initialization state of the LTO module.
46 // *** Not thread safe ***
47 static bool initialized = false;
49 // Holds the command-line option parsing state of the LTO module.
50 static bool parsedOptions = false;
52 // Initialize the configured targets if they have not been initialized.
53 static void lto_initialize() {
56 // Dialog box on crash disabling doesn't work across DLL boundaries, so do
58 llvm::sys::DisableSystemDialogsOnCrash();
61 InitializeAllTargetInfos();
62 InitializeAllTargets();
63 InitializeAllTargetMCs();
64 InitializeAllAsmParsers();
65 InitializeAllAsmPrinters();
66 InitializeAllDisassemblers();
71 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOCodeGenerator, lto_code_gen_t)
72 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
74 // Convert the subtarget features into a string to pass to LTOCodeGenerator.
75 static void lto_add_attrs(lto_code_gen_t cg) {
76 LTOCodeGenerator *CG = unwrap(cg);
79 for (unsigned i = 0; i < MAttrs.size(); ++i) {
82 attrs.append(MAttrs[i]);
85 CG->setAttr(attrs.c_str());
89 extern const char* lto_get_version() {
90 return LTOCodeGenerator::getVersionString();
93 const char* lto_get_error_message() {
94 return sLastErrorString.c_str();
97 bool lto_module_is_object_file(const char* path) {
98 return LTOModule::isBitcodeFile(path);
101 bool lto_module_is_object_file_for_target(const char* path,
102 const char* target_triplet_prefix) {
103 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
106 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
109 bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
110 return LTOModule::isBitcodeFile(mem, length);
114 lto_module_is_object_file_in_memory_for_target(const void* mem,
116 const char* target_triplet_prefix) {
117 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
120 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
123 lto_module_t lto_module_create(const char* path) {
125 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
126 return wrap(LTOModule::createFromFile(path, Options, sLastErrorString));
129 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
131 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
133 LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString));
136 lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
141 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
142 return wrap(LTOModule::createFromOpenFileSlice(fd, path, map_size, offset,
143 Options, sLastErrorString));
146 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
148 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
149 return wrap(LTOModule::createFromBuffer(mem, length, Options, sLastErrorString));
152 lto_module_t lto_module_create_from_memory_with_path(const void* mem,
156 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
158 LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path));
161 lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
164 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
165 return wrap(LTOModule::createInLocalContext(mem, length, Options,
166 sLastErrorString, path));
169 lto_module_t lto_module_create_in_codegen_context(const void *mem,
174 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
175 return wrap(LTOModule::createInContext(mem, length, Options, sLastErrorString,
176 path, &unwrap(cg)->getContext()));
179 void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
181 const char* lto_module_get_target_triple(lto_module_t mod) {
182 return unwrap(mod)->getTargetTriple().c_str();
185 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
186 return unwrap(mod)->setTargetTriple(triple);
189 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
190 return unwrap(mod)->getSymbolCount();
193 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
194 return unwrap(mod)->getSymbolName(index);
197 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
198 unsigned int index) {
199 return unwrap(mod)->getSymbolAttributes(index);
202 unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
203 return unwrap(mod)->getDependentLibraryCount();
206 const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
207 return unwrap(mod)->getDependentLibrary(index);
210 unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
211 return unwrap(mod)->getLinkerOptCount();
214 const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
215 return unwrap(mod)->getLinkerOpt(index);
218 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
219 lto_diagnostic_handler_t diag_handler,
221 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
224 static lto_code_gen_t createCodeGen(bool InLocalContext) {
227 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
229 LTOCodeGenerator *CodeGen =
230 InLocalContext ? new LTOCodeGenerator(make_unique<LLVMContext>())
231 : new LTOCodeGenerator();
233 CodeGen->setTargetOptions(Options);
234 return wrap(CodeGen);
237 lto_code_gen_t lto_codegen_create(void) {
238 return createCodeGen(/* InLocalContext */ false);
241 lto_code_gen_t lto_codegen_create_in_local_context(void) {
242 return createCodeGen(/* InLocalContext */ true);
245 void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
247 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
248 return !unwrap(cg)->addModule(unwrap(mod));
251 void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {
252 unwrap(cg)->setModule(unwrap(mod));
255 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
256 unwrap(cg)->setDebugInfo(debug);
260 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
261 unwrap(cg)->setCodePICModel(model);
265 void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
266 return unwrap(cg)->setCpu(cpu);
269 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
270 // In here only for backwards compatibility. We use MC now.
273 void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
275 // In here only for backwards compatibility. We use MC now.
278 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
279 const char *symbol) {
280 unwrap(cg)->addMustPreserveSymbol(symbol);
283 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
284 if (!parsedOptions) {
285 unwrap(cg)->parseCodeGenDebugOptions();
287 parsedOptions = true;
289 return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
292 const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
293 if (!parsedOptions) {
294 unwrap(cg)->parseCodeGenDebugOptions();
296 parsedOptions = true;
298 return unwrap(cg)->compile(length, DisableOpt, DisableInline,
299 DisableGVNLoadPRE, DisableLTOVectorization,
303 bool lto_codegen_optimize(lto_code_gen_t cg) {
304 if (!parsedOptions) {
305 unwrap(cg)->parseCodeGenDebugOptions();
307 parsedOptions = true;
309 return !unwrap(cg)->optimize(DisableOpt, DisableInline,
310 DisableGVNLoadPRE, DisableLTOVectorization,
314 const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {
315 if (!parsedOptions) {
316 unwrap(cg)->parseCodeGenDebugOptions();
318 parsedOptions = true;
320 return unwrap(cg)->compileOptimized(length, sLastErrorString);
323 bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
324 if (!parsedOptions) {
325 unwrap(cg)->parseCodeGenDebugOptions();
327 parsedOptions = true;
329 return !unwrap(cg)->compile_to_file(
330 name, DisableOpt, DisableInline, DisableGVNLoadPRE,
331 DisableLTOVectorization, sLastErrorString);
334 void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
335 unwrap(cg)->setCodeGenDebugOptions(opt);
338 unsigned int lto_api_version() { return LTO_API_VERSION; }