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/TargetSelect.h"
21 // extra command-line flags needed for LTOCodeGenerator
23 DisableOpt("disable-opt", cl::init(false),
24 cl::desc("Do not run any optimization passes"));
27 DisableInline("disable-inlining", cl::init(false),
28 cl::desc("Do not run the inliner pass"));
31 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
32 cl::desc("Do not run the GVN load PRE pass"));
34 // Holds most recent error string.
35 // *** Not thread safe ***
36 static std::string sLastErrorString;
38 // Holds the initialization state of the LTO module.
39 // *** Not thread safe ***
40 static bool initialized = false;
42 // Holds the command-line option parsing state of the LTO module.
43 static bool parsedOptions = false;
45 // Initialize the configured targets if they have not been initialized.
46 static void lto_initialize() {
48 InitializeAllTargetInfos();
49 InitializeAllTargets();
50 InitializeAllTargetMCs();
51 InitializeAllAsmParsers();
52 InitializeAllAsmPrinters();
53 InitializeAllDisassemblers();
58 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOCodeGenerator, lto_code_gen_t)
59 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
61 // Convert the subtarget features into a string to pass to LTOCodeGenerator.
62 static void lto_add_attrs(lto_code_gen_t cg) {
63 LTOCodeGenerator *CG = unwrap(cg);
66 for (unsigned i = 0; i < MAttrs.size(); ++i) {
69 attrs.append(MAttrs[i]);
72 CG->setAttr(attrs.c_str());
76 extern const char* lto_get_version() {
77 return LTOCodeGenerator::getVersionString();
80 const char* lto_get_error_message() {
81 return sLastErrorString.c_str();
84 bool lto_module_is_object_file(const char* path) {
85 return LTOModule::isBitcodeFile(path);
88 bool lto_module_is_object_file_for_target(const char* path,
89 const char* target_triplet_prefix) {
90 return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix);
93 bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
94 return LTOModule::isBitcodeFile(mem, length);
98 lto_module_is_object_file_in_memory_for_target(const void* mem,
100 const char* target_triplet_prefix) {
101 return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix);
104 lto_module_t lto_module_create(const char* path) {
106 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
107 return wrap(LTOModule::createFromFile(path, Options, sLastErrorString));
110 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
112 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
114 LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString));
117 lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
122 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
123 return wrap(LTOModule::createFromOpenFileSlice(fd, path, map_size, offset,
124 Options, sLastErrorString));
127 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
129 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
130 return wrap(LTOModule::createFromBuffer(mem, length, Options, sLastErrorString));
133 lto_module_t lto_module_create_from_memory_with_path(const void* mem,
137 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
139 LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path));
142 void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
144 const char* lto_module_get_target_triple(lto_module_t mod) {
145 return unwrap(mod)->getTargetTriple();
148 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
149 return unwrap(mod)->setTargetTriple(triple);
152 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
153 return unwrap(mod)->getSymbolCount();
156 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
157 return unwrap(mod)->getSymbolName(index);
160 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
161 unsigned int index) {
162 return unwrap(mod)->getSymbolAttributes(index);
165 unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
166 return unwrap(mod)->getDependentLibraryCount();
169 const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
170 return unwrap(mod)->getDependentLibrary(index);
173 unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
174 return unwrap(mod)->getLinkerOptCount();
177 const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
178 return unwrap(mod)->getLinkerOpt(index);
181 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
182 lto_diagnostic_handler_t diag_handler,
184 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
187 lto_code_gen_t lto_codegen_create(void) {
190 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
192 LTOCodeGenerator *CodeGen = new LTOCodeGenerator();
194 CodeGen->setTargetOptions(Options);
195 return wrap(CodeGen);
198 void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
200 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
201 return !unwrap(cg)->addModule(unwrap(mod), sLastErrorString);
204 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
205 unwrap(cg)->setDebugInfo(debug);
209 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
210 unwrap(cg)->setCodePICModel(model);
214 void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
215 return unwrap(cg)->setCpu(cpu);
218 void lto_codegen_set_attr(lto_code_gen_t cg, const char *attr) {
219 return unwrap(cg)->setAttr(attr);
222 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
223 // In here only for backwards compatibility. We use MC now.
226 void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
228 // In here only for backwards compatibility. We use MC now.
231 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
232 const char *symbol) {
233 unwrap(cg)->addMustPreserveSymbol(symbol);
236 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
237 if (!parsedOptions) {
238 unwrap(cg)->parseCodeGenDebugOptions();
240 parsedOptions = true;
242 return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
245 const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
246 if (!parsedOptions) {
247 unwrap(cg)->parseCodeGenDebugOptions();
249 parsedOptions = true;
251 return unwrap(cg)->compile(length, DisableOpt, DisableInline,
252 DisableGVNLoadPRE, sLastErrorString);
255 bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
256 if (!parsedOptions) {
257 unwrap(cg)->parseCodeGenDebugOptions();
259 parsedOptions = true;
261 return !unwrap(cg)->compile_to_file(name, DisableOpt, DisableInline,
262 DisableGVNLoadPRE, sLastErrorString);
265 void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
266 unwrap(cg)->setCodeGenDebugOptions(opt);