blob: ef37c90ba3b4350fa109838fc1fd65622fabd1c6 [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"
Alp Tokerac903802014-07-04 00:58:41 +000019#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola77c50d22014-06-19 19:11:22 +000020#include "llvm/Support/TargetSelect.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000021
Rafael Espindola0b385c72013-09-30 16:39:19 +000022// extra command-line flags needed for LTOCodeGenerator
23static cl::opt<bool>
24DisableOpt("disable-opt", cl::init(false),
25 cl::desc("Do not run any optimization passes"));
26
27static cl::opt<bool>
28DisableInline("disable-inlining", cl::init(false),
29 cl::desc("Do not run the inliner pass"));
30
31static cl::opt<bool>
32DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
33 cl::desc("Do not run the GVN load PRE pass"));
Nick Kledzik07b4a622008-02-26 20:26:43 +000034
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +000035static cl::opt<bool>
36DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
37 cl::desc("Do not run loop or slp vectorization during LTO"));
38
Bill Wendling36cbf032012-03-30 10:29:38 +000039// Holds most recent error string.
40// *** Not thread safe ***
Nick Kledzik07b4a622008-02-26 20:26:43 +000041static std::string sLastErrorString;
42
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000043// Holds the initialization state of the LTO module.
44// *** Not thread safe ***
45static bool initialized = false;
46
Rafael Espindolaefa02d52013-10-02 14:36:23 +000047// Holds the command-line option parsing state of the LTO module.
48static bool parsedOptions = false;
49
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000050// Initialize the configured targets if they have not been initialized.
51static void lto_initialize() {
52 if (!initialized) {
Rafael Espindola77c50d22014-06-19 19:11:22 +000053 InitializeAllTargetInfos();
54 InitializeAllTargets();
55 InitializeAllTargetMCs();
56 InitializeAllAsmParsers();
57 InitializeAllAsmPrinters();
58 InitializeAllDisassemblers();
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000059 initialized = true;
60 }
61}
62
Patrik Hagglund9be9d872014-05-05 12:24:08 +000063DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOCodeGenerator, lto_code_gen_t)
64DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000065
Tom Roederfd1bc602014-04-25 21:46:51 +000066// Convert the subtarget features into a string to pass to LTOCodeGenerator.
67static void lto_add_attrs(lto_code_gen_t cg) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000068 LTOCodeGenerator *CG = unwrap(cg);
Tom Roederfd1bc602014-04-25 21:46:51 +000069 if (MAttrs.size()) {
70 std::string attrs;
71 for (unsigned i = 0; i < MAttrs.size(); ++i) {
72 if (i > 0)
73 attrs.append(",");
74 attrs.append(MAttrs[i]);
75 }
76
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000077 CG->setAttr(attrs.c_str());
Tom Roederfd1bc602014-04-25 21:46:51 +000078 }
79}
80
Bill Wendling36cbf032012-03-30 10:29:38 +000081extern const char* lto_get_version() {
82 return LTOCodeGenerator::getVersionString();
Nick Kledzik07b4a622008-02-26 20:26:43 +000083}
84
Bill Wendling36cbf032012-03-30 10:29:38 +000085const char* lto_get_error_message() {
86 return sLastErrorString.c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +000087}
88
Reid Klecknerddac1512013-10-24 22:26:04 +000089bool lto_module_is_object_file(const char* path) {
Bill Wendling36cbf032012-03-30 10:29:38 +000090 return LTOModule::isBitcodeFile(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +000091}
92
Reid Klecknerddac1512013-10-24 22:26:04 +000093bool lto_module_is_object_file_for_target(const char* path,
Bill Wendling36cbf032012-03-30 10:29:38 +000094 const char* target_triplet_prefix) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +000095 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
96 if (!Buffer)
Alp Tokerac903802014-07-04 00:58:41 +000097 return false;
Rafael Espindolaadf21f22014-07-06 17:43:13 +000098 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +000099}
100
Reid Klecknerddac1512013-10-24 22:26:04 +0000101bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
Bill Wendling36cbf032012-03-30 10:29:38 +0000102 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000103}
104
Reid Klecknerddac1512013-10-24 22:26:04 +0000105bool
Bill Wendling36cbf032012-03-30 10:29:38 +0000106lto_module_is_object_file_in_memory_for_target(const void* mem,
107 size_t length,
108 const char* target_triplet_prefix) {
Alp Tokerac903802014-07-04 00:58:41 +0000109 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
110 if (!buffer)
111 return false;
112 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000113}
114
Bill Wendling36cbf032012-03-30 10:29:38 +0000115lto_module_t lto_module_create(const char* path) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000116 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000117 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000118 return wrap(LTOModule::createFromFile(path, Options, sLastErrorString));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000119}
120
Bill Wendling36cbf032012-03-30 10:29:38 +0000121lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000122 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000123 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000124 return wrap(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000125 LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString));
Rafael Espindola56e41f72011-02-08 22:40:47 +0000126}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000127
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000128lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
129 size_t file_size,
130 size_t map_size,
Bill Wendling36cbf032012-03-30 10:29:38 +0000131 off_t offset) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000132 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000133 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000134 return wrap(LTOModule::createFromOpenFileSlice(fd, path, map_size, offset,
135 Options, sLastErrorString));
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000136}
137
Bill Wendling36cbf032012-03-30 10:29:38 +0000138lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000139 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000140 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000141 return wrap(LTOModule::createFromBuffer(mem, length, Options, sLastErrorString));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000142}
143
Manman Ren03456a12014-02-10 23:26:14 +0000144lto_module_t lto_module_create_from_memory_with_path(const void* mem,
145 size_t length,
146 const char *path) {
147 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000148 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000149 return wrap(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000150 LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path));
Manman Ren03456a12014-02-10 23:26:14 +0000151}
152
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000153lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
154 const char *path) {
155 lto_initialize();
156 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
157 return wrap(LTOModule::createInLocalContext(mem, length, Options,
158 sLastErrorString, path));
159}
160
161lto_module_t lto_module_create_in_codegen_context(const void *mem,
162 size_t length,
163 const char *path,
164 lto_code_gen_t cg) {
165 lto_initialize();
166 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
167 return wrap(LTOModule::createInContext(mem, length, Options, sLastErrorString,
168 path, &unwrap(cg)->getContext()));
169}
170
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000171void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000172
Bill Wendling36cbf032012-03-30 10:29:38 +0000173const char* lto_module_get_target_triple(lto_module_t mod) {
Rafael Espindolad749fb52014-07-04 14:19:41 +0000174 return unwrap(mod)->getTargetTriple().c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000175}
176
Bill Wendling36cbf032012-03-30 10:29:38 +0000177void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000178 return unwrap(mod)->setTargetTriple(triple);
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000179}
180
Bill Wendling36cbf032012-03-30 10:29:38 +0000181unsigned int lto_module_get_num_symbols(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000182 return unwrap(mod)->getSymbolCount();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000183}
184
Bill Wendling36cbf032012-03-30 10:29:38 +0000185const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000186 return unwrap(mod)->getSymbolName(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000187}
188
Bill Wendling36cbf032012-03-30 10:29:38 +0000189lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
190 unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000191 return unwrap(mod)->getSymbolAttributes(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000192}
193
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000194unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000195 return unwrap(mod)->getDependentLibraryCount();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000196}
197
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000198const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000199 return unwrap(mod)->getDependentLibrary(index);
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000200}
201
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000202unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000203 return unwrap(mod)->getLinkerOptCount();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000204}
205
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000206const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000207 return unwrap(mod)->getLinkerOpt(index);
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000208}
209
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000210void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
211 lto_diagnostic_handler_t diag_handler,
212 void *ctxt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000213 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000214}
215
Bill Wendling36cbf032012-03-30 10:29:38 +0000216lto_code_gen_t lto_codegen_create(void) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000217 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000218
Eli Benderskyf0f21002014-02-19 17:09:35 +0000219 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000220
221 LTOCodeGenerator *CodeGen = new LTOCodeGenerator();
222 if (CodeGen)
223 CodeGen->setTargetOptions(Options);
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000224 return wrap(CodeGen);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000225}
226
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000227void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000228
Reid Klecknerddac1512013-10-24 22:26:04 +0000229bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000230 return !unwrap(cg)->addModule(unwrap(mod));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000231}
232
Reid Klecknerddac1512013-10-24 22:26:04 +0000233bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000234 unwrap(cg)->setDebugInfo(debug);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000235 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000236}
237
Reid Klecknerddac1512013-10-24 22:26:04 +0000238bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000239 unwrap(cg)->setCodePICModel(model);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000240 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000241}
242
Bill Wendling152e4732012-03-31 10:44:20 +0000243void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000244 return unwrap(cg)->setCpu(cpu);
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000245}
246
Bill Wendling36cbf032012-03-30 10:29:38 +0000247void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000248 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcac8c8a2009-06-04 00:28:45 +0000249}
250
Bill Wendling152e4732012-03-31 10:44:20 +0000251void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling36cbf032012-03-30 10:29:38 +0000252 int nargs) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000253 // In here only for backwards compatibility. We use MC now.
Rafael Espindola00456462010-08-10 18:55:09 +0000254}
255
Bill Wendling36cbf032012-03-30 10:29:38 +0000256void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling152e4732012-03-31 10:44:20 +0000257 const char *symbol) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000258 unwrap(cg)->addMustPreserveSymbol(symbol);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000259}
260
Reid Klecknerddac1512013-10-24 22:26:04 +0000261bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000262 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000263 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000264 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000265 parsedOptions = true;
266 }
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000267 return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000268}
269
Bill Wendling152e4732012-03-31 10:44:20 +0000270const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000271 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000272 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000273 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000274 parsedOptions = true;
275 }
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000276 return unwrap(cg)->compile(length, DisableOpt, DisableInline,
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000277 DisableGVNLoadPRE, DisableLTOVectorization,
278 sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000279}
280
Reid Klecknerddac1512013-10-24 22:26:04 +0000281bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000282 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000283 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000284 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000285 parsedOptions = true;
286 }
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000287 return !unwrap(cg)->compile_to_file(
288 name, DisableOpt, DisableInline, DisableGVNLoadPRE,
289 DisableLTOVectorization, sLastErrorString);
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000290}
291
Bill Wendling152e4732012-03-31 10:44:20 +0000292void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000293 unwrap(cg)->setCodeGenDebugOptions(opt);
Duncan Sands31554ab2009-07-03 15:38:01 +0000294}