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/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"));
37 DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
38 cl::desc("Do not run loop or slp vectorization during LTO"));
40 // Holds most recent error string.
41 // *** Not thread safe ***
42 static std::string sLastErrorString;
44 // Holds the initialization state of the LTO module.
45 // *** Not thread safe ***
46 static bool initialized = false;
48 // Holds the command-line option parsing state of the LTO module.
49 static bool parsedOptions = false;
51 // Initialize the configured targets if they have not been initialized.
52 static void lto_initialize() {
54 InitializeAllTargetInfos();
55 InitializeAllTargets();
56 InitializeAllTargetMCs();
57 InitializeAllAsmParsers();
58 InitializeAllAsmPrinters();
59 InitializeAllDisassemblers();
64 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOCodeGenerator, lto_code_gen_t)
65 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
67 // Convert the subtarget features into a string to pass to LTOCodeGenerator.
68 static void lto_add_attrs(lto_code_gen_t cg) {
69 LTOCodeGenerator *CG = unwrap(cg);
72 for (unsigned i = 0; i < MAttrs.size(); ++i) {
75 attrs.append(MAttrs[i]);
78 CG->setAttr(attrs.c_str());
82 extern const char* lto_get_version() {
83 return LTOCodeGenerator::getVersionString();
86 const char* lto_get_error_message() {
87 return sLastErrorString.c_str();
90 bool lto_module_is_object_file(const char* path) {
91 return LTOModule::isBitcodeFile(path);
94 bool lto_module_is_object_file_for_target(const char* path,
95 const char* target_triplet_prefix) {
96 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
99 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
102 bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
103 return LTOModule::isBitcodeFile(mem, length);
107 lto_module_is_object_file_in_memory_for_target(const void* mem,
109 const char* target_triplet_prefix) {
110 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
113 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
116 lto_module_t lto_module_create(const char* path) {
118 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
119 return wrap(LTOModule::createFromFile(path, Options, sLastErrorString));
122 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
124 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
126 LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString));
129 lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
134 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
135 return wrap(LTOModule::createFromOpenFileSlice(fd, path, map_size, offset,
136 Options, sLastErrorString));
139 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
141 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
142 return wrap(LTOModule::createFromBuffer(mem, length, Options, sLastErrorString));
145 lto_module_t lto_module_create_from_memory_with_path(const void* mem,
149 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
151 LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path));
154 lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
157 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
158 return wrap(LTOModule::createInLocalContext(mem, length, Options,
159 sLastErrorString, path));
162 lto_module_t lto_module_create_in_codegen_context(const void *mem,
167 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
168 return wrap(LTOModule::createInContext(mem, length, Options, sLastErrorString,
169 path, &unwrap(cg)->getContext()));
172 void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
174 const char* lto_module_get_target_triple(lto_module_t mod) {
175 return unwrap(mod)->getTargetTriple().c_str();
178 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
179 return unwrap(mod)->setTargetTriple(triple);
182 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
183 return unwrap(mod)->getSymbolCount();
186 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
187 return unwrap(mod)->getSymbolName(index);
190 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
191 unsigned int index) {
192 return unwrap(mod)->getSymbolAttributes(index);
195 unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
196 return unwrap(mod)->getDependentLibraryCount();
199 const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
200 return unwrap(mod)->getDependentLibrary(index);
203 unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
204 return unwrap(mod)->getLinkerOptCount();
207 const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
208 return unwrap(mod)->getLinkerOpt(index);
211 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
212 lto_diagnostic_handler_t diag_handler,
214 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
217 static lto_code_gen_t createCodeGen(bool InLocalContext) {
220 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
222 LTOCodeGenerator *CodeGen =
223 InLocalContext ? new LTOCodeGenerator(make_unique<LLVMContext>())
224 : new LTOCodeGenerator();
226 CodeGen->setTargetOptions(Options);
227 return wrap(CodeGen);
230 lto_code_gen_t lto_codegen_create(void) {
231 return createCodeGen(/* InLocalContext */ false);
234 lto_code_gen_t lto_codegen_create_in_local_context(void) {
235 return createCodeGen(/* InLocalContext */ true);
238 void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
240 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
241 return !unwrap(cg)->addModule(unwrap(mod));
244 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
245 unwrap(cg)->setDebugInfo(debug);
249 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
250 unwrap(cg)->setCodePICModel(model);
254 void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
255 return unwrap(cg)->setCpu(cpu);
258 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
259 // In here only for backwards compatibility. We use MC now.
262 void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
264 // In here only for backwards compatibility. We use MC now.
267 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
268 const char *symbol) {
269 unwrap(cg)->addMustPreserveSymbol(symbol);
272 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
273 if (!parsedOptions) {
274 unwrap(cg)->parseCodeGenDebugOptions();
276 parsedOptions = true;
278 return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
281 const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
282 if (!parsedOptions) {
283 unwrap(cg)->parseCodeGenDebugOptions();
285 parsedOptions = true;
287 return unwrap(cg)->compile(length, DisableOpt, DisableInline,
288 DisableGVNLoadPRE, DisableLTOVectorization,
292 bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
293 if (!parsedOptions) {
294 unwrap(cg)->parseCodeGenDebugOptions();
296 parsedOptions = true;
298 return !unwrap(cg)->compile_to_file(
299 name, DisableOpt, DisableInline, DisableGVNLoadPRE,
300 DisableLTOVectorization, sLastErrorString);
303 void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
304 unwrap(cg)->setCodeGenDebugOptions(opt);