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
27 DisableOpt("disable-opt", cl::init(false),
28 cl::desc("Do not run any optimization passes"));
31 DisableInline("disable-inlining", cl::init(false),
32 cl::desc("Do not run the inliner pass"));
35 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
36 cl::desc("Do not run the GVN load PRE pass"));
39 DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
40 cl::desc("Do not run loop or slp vectorization during LTO"));
42 // Holds most recent error string.
43 // *** Not thread safe ***
44 static std::string sLastErrorString;
46 // Holds the initialization state of the LTO module.
47 // *** Not thread safe ***
48 static bool initialized = false;
50 // Holds the command-line option parsing state of the LTO module.
51 static bool parsedOptions = false;
53 // Initialize the configured targets if they have not been initialized.
54 static void lto_initialize() {
57 // Dialog box on crash disabling doesn't work across DLL boundaries, so do
59 llvm::sys::DisableSystemDialogsOnCrash();
62 InitializeAllTargetInfos();
63 InitializeAllTargets();
64 InitializeAllTargetMCs();
65 InitializeAllAsmParsers();
66 InitializeAllAsmPrinters();
67 InitializeAllDisassemblers();
72 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOCodeGenerator, lto_code_gen_t)
73 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
75 // Convert the subtarget features into a string to pass to LTOCodeGenerator.
76 static void lto_add_attrs(lto_code_gen_t cg) {
77 LTOCodeGenerator *CG = unwrap(cg);
80 for (unsigned i = 0; i < MAttrs.size(); ++i) {
83 attrs.append(MAttrs[i]);
86 CG->setAttr(attrs.c_str());
90 extern const char* lto_get_version() {
91 return LTOCodeGenerator::getVersionString();
94 const char* lto_get_error_message() {
95 return sLastErrorString.c_str();
98 bool lto_module_is_object_file(const char* path) {
99 return LTOModule::isBitcodeFile(path);
102 bool lto_module_is_object_file_for_target(const char* path,
103 const char* target_triplet_prefix) {
104 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
107 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
110 bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
111 return LTOModule::isBitcodeFile(mem, length);
115 lto_module_is_object_file_in_memory_for_target(const void* mem,
117 const char* target_triplet_prefix) {
118 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
121 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
124 lto_module_t lto_module_create(const char* path) {
126 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
127 return wrap(LTOModule::createFromFile(path, Options, sLastErrorString));
130 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
132 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
134 LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString));
137 lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
142 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
143 return wrap(LTOModule::createFromOpenFileSlice(fd, path, map_size, offset,
144 Options, sLastErrorString));
147 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
149 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
150 return wrap(LTOModule::createFromBuffer(mem, length, Options, sLastErrorString));
153 lto_module_t lto_module_create_from_memory_with_path(const void* mem,
157 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
159 LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path));
162 lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
165 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
166 return wrap(LTOModule::createInLocalContext(mem, length, Options,
167 sLastErrorString, path));
170 lto_module_t lto_module_create_in_codegen_context(const void *mem,
175 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
176 return wrap(LTOModule::createInContext(mem, length, Options, sLastErrorString,
177 path, &unwrap(cg)->getContext()));
180 void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
182 const char* lto_module_get_target_triple(lto_module_t mod) {
183 return unwrap(mod)->getTargetTriple().c_str();
186 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
187 return unwrap(mod)->setTargetTriple(triple);
190 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
191 return unwrap(mod)->getSymbolCount();
194 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
195 return unwrap(mod)->getSymbolName(index);
198 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
199 unsigned int index) {
200 return unwrap(mod)->getSymbolAttributes(index);
203 unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
204 return unwrap(mod)->getDependentLibraryCount();
207 const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
208 return unwrap(mod)->getDependentLibrary(index);
211 unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
212 return unwrap(mod)->getLinkerOptCount();
215 const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
216 return unwrap(mod)->getLinkerOpt(index);
219 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
220 lto_diagnostic_handler_t diag_handler,
222 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
225 static lto_code_gen_t createCodeGen(bool InLocalContext) {
228 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
230 LTOCodeGenerator *CodeGen =
231 InLocalContext ? new LTOCodeGenerator(make_unique<LLVMContext>())
232 : new LTOCodeGenerator();
234 CodeGen->setTargetOptions(Options);
235 return wrap(CodeGen);
238 lto_code_gen_t lto_codegen_create(void) {
239 return createCodeGen(/* InLocalContext */ false);
242 lto_code_gen_t lto_codegen_create_in_local_context(void) {
243 return createCodeGen(/* InLocalContext */ true);
246 void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
248 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
249 return !unwrap(cg)->addModule(unwrap(mod));
252 void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {
253 unwrap(cg)->setModule(unwrap(mod));
256 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
257 unwrap(cg)->setDebugInfo(debug);
261 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
262 unwrap(cg)->setCodePICModel(model);
266 void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
267 return unwrap(cg)->setCpu(cpu);
270 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
271 // In here only for backwards compatibility. We use MC now.
274 void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
276 // In here only for backwards compatibility. We use MC now.
279 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
280 const char *symbol) {
281 unwrap(cg)->addMustPreserveSymbol(symbol);
284 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
285 if (!parsedOptions) {
286 unwrap(cg)->parseCodeGenDebugOptions();
288 parsedOptions = true;
290 return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
293 const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
294 if (!parsedOptions) {
295 unwrap(cg)->parseCodeGenDebugOptions();
297 parsedOptions = true;
299 return unwrap(cg)->compile(length, DisableOpt, DisableInline,
300 DisableGVNLoadPRE, DisableLTOVectorization,
304 bool lto_codegen_optimize(lto_code_gen_t cg) {
305 if (!parsedOptions) {
306 unwrap(cg)->parseCodeGenDebugOptions();
308 parsedOptions = true;
310 return !unwrap(cg)->optimize(DisableOpt, DisableInline,
311 DisableGVNLoadPRE, DisableLTOVectorization,
315 const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {
316 if (!parsedOptions) {
317 unwrap(cg)->parseCodeGenDebugOptions();
319 parsedOptions = true;
321 return unwrap(cg)->compileOptimized(length, sLastErrorString);
324 bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
325 if (!parsedOptions) {
326 unwrap(cg)->parseCodeGenDebugOptions();
328 parsedOptions = true;
330 return !unwrap(cg)->compile_to_file(
331 name, DisableOpt, DisableInline, DisableGVNLoadPRE,
332 DisableLTOVectorization, sLastErrorString);
335 void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
336 unwrap(cg)->setCodeGenDebugOptions(opt);
339 unsigned int lto_api_version() { return LTO_API_VERSION; }