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/LTO/LTOCodeGenerator.h"
18 #include "llvm/LTO/LTOModule.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/TargetSelect.h"
22 // extra command-line flags needed for LTOCodeGenerator
24 DisableOpt("disable-opt", cl::init(false),
25 cl::desc("Do not run any optimization passes"));
28 DisableInline("disable-inlining", cl::init(false),
29 cl::desc("Do not run the inliner pass"));
32 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
33 cl::desc("Do not run the GVN load PRE pass"));
36 DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
37 cl::desc("Do not run loop or slp vectorization during LTO"));
39 // Holds most recent error string.
40 // *** Not thread safe ***
41 static std::string sLastErrorString;
43 // Holds the initialization state of the LTO module.
44 // *** Not thread safe ***
45 static bool initialized = false;
47 // Holds the command-line option parsing state of the LTO module.
48 static bool parsedOptions = false;
50 // Initialize the configured targets if they have not been initialized.
51 static void lto_initialize() {
53 InitializeAllTargetInfos();
54 InitializeAllTargets();
55 InitializeAllTargetMCs();
56 InitializeAllAsmParsers();
57 InitializeAllAsmPrinters();
58 InitializeAllDisassemblers();
63 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOCodeGenerator, lto_code_gen_t)
64 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
66 // Convert the subtarget features into a string to pass to LTOCodeGenerator.
67 static void lto_add_attrs(lto_code_gen_t cg) {
68 LTOCodeGenerator *CG = unwrap(cg);
71 for (unsigned i = 0; i < MAttrs.size(); ++i) {
74 attrs.append(MAttrs[i]);
77 CG->setAttr(attrs.c_str());
81 extern const char* lto_get_version() {
82 return LTOCodeGenerator::getVersionString();
85 const char* lto_get_error_message() {
86 return sLastErrorString.c_str();
89 bool lto_module_is_object_file(const char* path) {
90 return LTOModule::isBitcodeFile(path);
93 bool lto_module_is_object_file_for_target(const char* path,
94 const char* target_triplet_prefix) {
95 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
98 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
101 bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
102 return LTOModule::isBitcodeFile(mem, length);
106 lto_module_is_object_file_in_memory_for_target(const void* mem,
108 const char* target_triplet_prefix) {
109 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
112 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
115 lto_module_t lto_module_create(const char* path) {
117 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
118 return wrap(LTOModule::createFromFile(path, Options, sLastErrorString));
121 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
123 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
125 LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString));
128 lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
133 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
134 return wrap(LTOModule::createFromOpenFileSlice(fd, path, map_size, offset,
135 Options, sLastErrorString));
138 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
140 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
141 return wrap(LTOModule::createFromBuffer(mem, length, Options, sLastErrorString));
144 lto_module_t lto_module_create_from_memory_with_path(const void* mem,
148 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
150 LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path));
153 void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
155 const char* lto_module_get_target_triple(lto_module_t mod) {
156 return unwrap(mod)->getTargetTriple().c_str();
159 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
160 return unwrap(mod)->setTargetTriple(triple);
163 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
164 return unwrap(mod)->getSymbolCount();
167 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
168 return unwrap(mod)->getSymbolName(index);
171 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
172 unsigned int index) {
173 return unwrap(mod)->getSymbolAttributes(index);
176 unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
177 return unwrap(mod)->getDependentLibraryCount();
180 const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
181 return unwrap(mod)->getDependentLibrary(index);
184 unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
185 return unwrap(mod)->getLinkerOptCount();
188 const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
189 return unwrap(mod)->getLinkerOpt(index);
192 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
193 lto_diagnostic_handler_t diag_handler,
195 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
198 lto_code_gen_t lto_codegen_create(void) {
201 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
203 LTOCodeGenerator *CodeGen = new LTOCodeGenerator();
205 CodeGen->setTargetOptions(Options);
206 return wrap(CodeGen);
209 void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
211 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
212 return !unwrap(cg)->addModule(unwrap(mod));
215 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
216 unwrap(cg)->setDebugInfo(debug);
220 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
221 unwrap(cg)->setCodePICModel(model);
225 void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
226 return unwrap(cg)->setCpu(cpu);
229 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
230 // In here only for backwards compatibility. We use MC now.
233 void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
235 // In here only for backwards compatibility. We use MC now.
238 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
239 const char *symbol) {
240 unwrap(cg)->addMustPreserveSymbol(symbol);
243 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
244 if (!parsedOptions) {
245 unwrap(cg)->parseCodeGenDebugOptions();
247 parsedOptions = true;
249 return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
252 const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
253 if (!parsedOptions) {
254 unwrap(cg)->parseCodeGenDebugOptions();
256 parsedOptions = true;
258 return unwrap(cg)->compile(length, DisableOpt, DisableInline,
259 DisableGVNLoadPRE, DisableLTOVectorization,
263 bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
264 if (!parsedOptions) {
265 unwrap(cg)->parseCodeGenDebugOptions();
267 parsedOptions = true;
269 return !unwrap(cg)->compile_to_file(
270 name, DisableOpt, DisableInline, DisableGVNLoadPRE,
271 DisableLTOVectorization, sLastErrorString);
274 void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
275 unwrap(cg)->setCodeGenDebugOptions(opt);