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-c/Core.h"
17 #include "llvm-c/Target.h"
18 #include "llvm/CodeGen/CommandFlags.h"
19 #include "llvm/LTO/LTOCodeGenerator.h"
20 #include "llvm/LTO/LTOModule.h"
21 #include "llvm/Support/TargetSelect.h"
23 // extra command-line flags needed for LTOCodeGenerator
25 DisableOpt("disable-opt", cl::init(false),
26 cl::desc("Do not run any optimization passes"));
29 DisableInline("disable-inlining", cl::init(false),
30 cl::desc("Do not run the inliner pass"));
33 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
34 cl::desc("Do not run the GVN load PRE pass"));
36 // Holds most recent error string.
37 // *** Not thread safe ***
38 static std::string sLastErrorString;
40 // Holds the initialization state of the LTO module.
41 // *** Not thread safe ***
42 static bool initialized = false;
44 // Holds the command-line option parsing state of the LTO module.
45 static bool parsedOptions = false;
47 // Initialize the configured targets if they have not been initialized.
48 static void lto_initialize() {
50 InitializeAllTargetInfos();
51 InitializeAllTargets();
52 InitializeAllTargetMCs();
53 InitializeAllAsmParsers();
54 InitializeAllAsmPrinters();
55 InitializeAllDisassemblers();
60 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOCodeGenerator, lto_code_gen_t)
61 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
63 // Convert the subtarget features into a string to pass to LTOCodeGenerator.
64 static void lto_add_attrs(lto_code_gen_t cg) {
65 LTOCodeGenerator *CG = unwrap(cg);
68 for (unsigned i = 0; i < MAttrs.size(); ++i) {
71 attrs.append(MAttrs[i]);
74 CG->setAttr(attrs.c_str());
78 extern const char* lto_get_version() {
79 return LTOCodeGenerator::getVersionString();
82 const char* lto_get_error_message() {
83 return sLastErrorString.c_str();
86 bool lto_module_is_object_file(const char* path) {
87 return LTOModule::isBitcodeFile(path);
90 bool lto_module_is_object_file_for_target(const char* path,
91 const char* target_triplet_prefix) {
92 return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix);
95 bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
96 return LTOModule::isBitcodeFile(mem, length);
100 lto_module_is_object_file_in_memory_for_target(const void* mem,
102 const char* target_triplet_prefix) {
103 return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix);
106 lto_module_t lto_module_create(const char* path) {
108 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
109 return wrap(LTOModule::makeLTOModule(path, Options, sLastErrorString));
112 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
114 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
116 LTOModule::makeLTOModule(fd, path, size, Options, sLastErrorString));
119 lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
124 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
125 return wrap(LTOModule::makeLTOModule(fd, path, map_size, offset, Options,
129 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
131 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
132 return wrap(LTOModule::makeLTOModule(mem, length, Options, sLastErrorString));
135 lto_module_t lto_module_create_from_memory_with_path(const void* mem,
139 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
141 LTOModule::makeLTOModule(mem, length, Options, sLastErrorString, path));
144 void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
146 const char* lto_module_get_target_triple(lto_module_t mod) {
147 return unwrap(mod)->getTargetTriple();
150 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
151 return unwrap(mod)->setTargetTriple(triple);
154 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
155 return unwrap(mod)->getSymbolCount();
158 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
159 return unwrap(mod)->getSymbolName(index);
162 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
163 unsigned int index) {
164 return unwrap(mod)->getSymbolAttributes(index);
167 unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
168 return unwrap(mod)->getDependentLibraryCount();
171 const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
172 return unwrap(mod)->getDependentLibrary(index);
175 unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
176 return unwrap(mod)->getLinkerOptCount();
179 const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
180 return unwrap(mod)->getLinkerOpt(index);
183 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
184 lto_diagnostic_handler_t diag_handler,
186 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
189 lto_code_gen_t lto_codegen_create(void) {
192 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
194 LTOCodeGenerator *CodeGen = new LTOCodeGenerator();
196 CodeGen->setTargetOptions(Options);
197 return wrap(CodeGen);
200 void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
202 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
203 return !unwrap(cg)->addModule(unwrap(mod), sLastErrorString);
206 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
207 unwrap(cg)->setDebugInfo(debug);
211 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
212 unwrap(cg)->setCodePICModel(model);
216 void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
217 return unwrap(cg)->setCpu(cpu);
220 void lto_codegen_set_attr(lto_code_gen_t cg, const char *attr) {
221 return unwrap(cg)->setAttr(attr);
224 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
225 // In here only for backwards compatibility. We use MC now.
228 void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
230 // In here only for backwards compatibility. We use MC now.
233 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
234 const char *symbol) {
235 unwrap(cg)->addMustPreserveSymbol(symbol);
238 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
239 if (!parsedOptions) {
240 unwrap(cg)->parseCodeGenDebugOptions();
242 parsedOptions = true;
244 return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
247 const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
248 if (!parsedOptions) {
249 unwrap(cg)->parseCodeGenDebugOptions();
251 parsedOptions = true;
253 return unwrap(cg)->compile(length, DisableOpt, DisableInline,
254 DisableGVNLoadPRE, sLastErrorString);
257 bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
258 if (!parsedOptions) {
259 unwrap(cg)->parseCodeGenDebugOptions();
261 parsedOptions = true;
263 return !unwrap(cg)->compile_to_file(name, DisableOpt, DisableInline,
264 DisableGVNLoadPRE, sLastErrorString);
267 void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
268 unwrap(cg)->setCodeGenDebugOptions(opt);