blob: 5f021244249e431a0d89689eb22a20208adfe37c [file] [log] [blame]
Nick Kledzik07b4a622008-02-26 20:26:43 +00001//===-lto.cpp - LLVM Link Time Optimizer ----------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Bill Wendling36cbf032012-03-30 10:29:38 +00007//
Nick Kledzik07b4a622008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Bill Wendling36cbf032012-03-30 10:29:38 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik07b4a622008-02-26 20:26:43 +000011// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm-c/lto.h"
Rafael Espindola0b385c72013-09-30 16:39:19 +000016#include "llvm/CodeGen/CommandFlags.h"
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000017#include "llvm/LTO/LTOCodeGenerator.h"
18#include "llvm/LTO/LTOModule.h"
Rafael Espindola77c50d22014-06-19 19:11:22 +000019#include "llvm/Support/TargetSelect.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000020
Rafael Espindola0b385c72013-09-30 16:39:19 +000021// extra command-line flags needed for LTOCodeGenerator
22static cl::opt<bool>
23DisableOpt("disable-opt", cl::init(false),
24 cl::desc("Do not run any optimization passes"));
25
26static cl::opt<bool>
27DisableInline("disable-inlining", cl::init(false),
28 cl::desc("Do not run the inliner pass"));
29
30static cl::opt<bool>
31DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
32 cl::desc("Do not run the GVN load PRE pass"));
Nick Kledzik07b4a622008-02-26 20:26:43 +000033
Bill Wendling36cbf032012-03-30 10:29:38 +000034// Holds most recent error string.
35// *** Not thread safe ***
Nick Kledzik07b4a622008-02-26 20:26:43 +000036static std::string sLastErrorString;
37
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000038// Holds the initialization state of the LTO module.
39// *** Not thread safe ***
40static bool initialized = false;
41
Rafael Espindolaefa02d52013-10-02 14:36:23 +000042// Holds the command-line option parsing state of the LTO module.
43static bool parsedOptions = false;
44
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000045// Initialize the configured targets if they have not been initialized.
46static void lto_initialize() {
47 if (!initialized) {
Rafael Espindola77c50d22014-06-19 19:11:22 +000048 InitializeAllTargetInfos();
49 InitializeAllTargets();
50 InitializeAllTargetMCs();
51 InitializeAllAsmParsers();
52 InitializeAllAsmPrinters();
53 InitializeAllDisassemblers();
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000054 initialized = true;
55 }
56}
57
Patrik Hagglund9be9d872014-05-05 12:24:08 +000058DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOCodeGenerator, lto_code_gen_t)
59DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000060
Tom Roederfd1bc602014-04-25 21:46:51 +000061// Convert the subtarget features into a string to pass to LTOCodeGenerator.
62static void lto_add_attrs(lto_code_gen_t cg) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000063 LTOCodeGenerator *CG = unwrap(cg);
Tom Roederfd1bc602014-04-25 21:46:51 +000064 if (MAttrs.size()) {
65 std::string attrs;
66 for (unsigned i = 0; i < MAttrs.size(); ++i) {
67 if (i > 0)
68 attrs.append(",");
69 attrs.append(MAttrs[i]);
70 }
71
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000072 CG->setAttr(attrs.c_str());
Tom Roederfd1bc602014-04-25 21:46:51 +000073 }
74}
75
Bill Wendling36cbf032012-03-30 10:29:38 +000076extern const char* lto_get_version() {
77 return LTOCodeGenerator::getVersionString();
Nick Kledzik07b4a622008-02-26 20:26:43 +000078}
79
Bill Wendling36cbf032012-03-30 10:29:38 +000080const char* lto_get_error_message() {
81 return sLastErrorString.c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +000082}
83
Reid Klecknerddac1512013-10-24 22:26:04 +000084bool lto_module_is_object_file(const char* path) {
Bill Wendling36cbf032012-03-30 10:29:38 +000085 return LTOModule::isBitcodeFile(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +000086}
87
Reid Klecknerddac1512013-10-24 22:26:04 +000088bool lto_module_is_object_file_for_target(const char* path,
Bill Wendling36cbf032012-03-30 10:29:38 +000089 const char* target_triplet_prefix) {
90 return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +000091}
92
Reid Klecknerddac1512013-10-24 22:26:04 +000093bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
Bill Wendling36cbf032012-03-30 10:29:38 +000094 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik07b4a622008-02-26 20:26:43 +000095}
96
Reid Klecknerddac1512013-10-24 22:26:04 +000097bool
Bill Wendling36cbf032012-03-30 10:29:38 +000098lto_module_is_object_file_in_memory_for_target(const void* mem,
99 size_t length,
100 const char* target_triplet_prefix) {
101 return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000102}
103
Bill Wendling36cbf032012-03-30 10:29:38 +0000104lto_module_t lto_module_create(const char* path) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000105 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000106 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000107 return wrap(LTOModule::createFromFile(path, Options, sLastErrorString));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000108}
109
Bill Wendling36cbf032012-03-30 10:29:38 +0000110lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000111 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000112 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000113 return wrap(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000114 LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString));
Rafael Espindola56e41f72011-02-08 22:40:47 +0000115}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000116
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000117lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
118 size_t file_size,
119 size_t map_size,
Bill Wendling36cbf032012-03-30 10:29:38 +0000120 off_t offset) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000121 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000122 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000123 return wrap(LTOModule::createFromOpenFileSlice(fd, path, map_size, offset,
124 Options, sLastErrorString));
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000125}
126
Bill Wendling36cbf032012-03-30 10:29:38 +0000127lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000128 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000129 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000130 return wrap(LTOModule::createFromBuffer(mem, length, Options, sLastErrorString));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000131}
132
Manman Ren03456a12014-02-10 23:26:14 +0000133lto_module_t lto_module_create_from_memory_with_path(const void* mem,
134 size_t length,
135 const char *path) {
136 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000137 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000138 return wrap(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000139 LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path));
Manman Ren03456a12014-02-10 23:26:14 +0000140}
141
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000142void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000143
Bill Wendling36cbf032012-03-30 10:29:38 +0000144const char* lto_module_get_target_triple(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000145 return unwrap(mod)->getTargetTriple();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000146}
147
Bill Wendling36cbf032012-03-30 10:29:38 +0000148void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000149 return unwrap(mod)->setTargetTriple(triple);
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000150}
151
Bill Wendling36cbf032012-03-30 10:29:38 +0000152unsigned int lto_module_get_num_symbols(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000153 return unwrap(mod)->getSymbolCount();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000154}
155
Bill Wendling36cbf032012-03-30 10:29:38 +0000156const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000157 return unwrap(mod)->getSymbolName(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000158}
159
Bill Wendling36cbf032012-03-30 10:29:38 +0000160lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
161 unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000162 return unwrap(mod)->getSymbolAttributes(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000163}
164
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000165unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000166 return unwrap(mod)->getDependentLibraryCount();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000167}
168
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000169const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000170 return unwrap(mod)->getDependentLibrary(index);
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000171}
172
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000173unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000174 return unwrap(mod)->getLinkerOptCount();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000175}
176
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000177const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000178 return unwrap(mod)->getLinkerOpt(index);
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000179}
180
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000181void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
182 lto_diagnostic_handler_t diag_handler,
183 void *ctxt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000184 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000185}
186
Bill Wendling36cbf032012-03-30 10:29:38 +0000187lto_code_gen_t lto_codegen_create(void) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000188 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000189
Eli Benderskyf0f21002014-02-19 17:09:35 +0000190 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000191
192 LTOCodeGenerator *CodeGen = new LTOCodeGenerator();
193 if (CodeGen)
194 CodeGen->setTargetOptions(Options);
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000195 return wrap(CodeGen);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000196}
197
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000198void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000199
Reid Klecknerddac1512013-10-24 22:26:04 +0000200bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000201 return !unwrap(cg)->addModule(unwrap(mod), sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000202}
203
Reid Klecknerddac1512013-10-24 22:26:04 +0000204bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000205 unwrap(cg)->setDebugInfo(debug);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000206 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000207}
208
Reid Klecknerddac1512013-10-24 22:26:04 +0000209bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000210 unwrap(cg)->setCodePICModel(model);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000211 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000212}
213
Bill Wendling152e4732012-03-31 10:44:20 +0000214void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000215 return unwrap(cg)->setCpu(cpu);
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000216}
217
Tom Roederfd1bc602014-04-25 21:46:51 +0000218void lto_codegen_set_attr(lto_code_gen_t cg, const char *attr) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000219 return unwrap(cg)->setAttr(attr);
Tom Roederfd1bc602014-04-25 21:46:51 +0000220}
221
Bill Wendling36cbf032012-03-30 10:29:38 +0000222void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000223 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcac8c8a2009-06-04 00:28:45 +0000224}
225
Bill Wendling152e4732012-03-31 10:44:20 +0000226void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling36cbf032012-03-30 10:29:38 +0000227 int nargs) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000228 // In here only for backwards compatibility. We use MC now.
Rafael Espindola00456462010-08-10 18:55:09 +0000229}
230
Bill Wendling36cbf032012-03-30 10:29:38 +0000231void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling152e4732012-03-31 10:44:20 +0000232 const char *symbol) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000233 unwrap(cg)->addMustPreserveSymbol(symbol);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000234}
235
Reid Klecknerddac1512013-10-24 22:26:04 +0000236bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000237 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000238 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000239 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000240 parsedOptions = true;
241 }
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000242 return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000243}
244
Bill Wendling152e4732012-03-31 10:44:20 +0000245const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000246 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000247 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000248 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000249 parsedOptions = true;
250 }
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000251 return unwrap(cg)->compile(length, DisableOpt, DisableInline,
252 DisableGVNLoadPRE, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000253}
254
Reid Klecknerddac1512013-10-24 22:26:04 +0000255bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000256 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000257 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000258 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000259 parsedOptions = true;
260 }
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000261 return !unwrap(cg)->compile_to_file(name, DisableOpt, DisableInline,
262 DisableGVNLoadPRE, sLastErrorString);
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000263}
264
Bill Wendling152e4732012-03-31 10:44:20 +0000265void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000266 unwrap(cg)->setCodeGenDebugOptions(opt);
Duncan Sands31554ab2009-07-03 15:38:01 +0000267}