blob: 74e0dbe5e377e0b09562b3d4ed3eea030425e7d5 [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"
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +000017#include "llvm/IR/LLVMContext.h"
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000018#include "llvm/LTO/LTOCodeGenerator.h"
19#include "llvm/LTO/LTOModule.h"
Alp Tokerac903802014-07-04 00:58:41 +000020#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer50a20c02015-01-29 17:20:41 +000021#include "llvm/Support/Signals.h"
Rafael Espindola77c50d22014-06-19 19:11:22 +000022#include "llvm/Support/TargetSelect.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000023
Rafael Espindola0b385c72013-09-30 16:39:19 +000024// extra command-line flags needed for LTOCodeGenerator
25static cl::opt<bool>
26DisableOpt("disable-opt", cl::init(false),
27 cl::desc("Do not run any optimization passes"));
28
29static cl::opt<bool>
30DisableInline("disable-inlining", cl::init(false),
31 cl::desc("Do not run the inliner pass"));
32
33static cl::opt<bool>
34DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
35 cl::desc("Do not run the GVN load PRE pass"));
Nick Kledzik07b4a622008-02-26 20:26:43 +000036
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +000037static cl::opt<bool>
38DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
39 cl::desc("Do not run loop or slp vectorization during LTO"));
40
Bill Wendling36cbf032012-03-30 10:29:38 +000041// Holds most recent error string.
42// *** Not thread safe ***
Nick Kledzik07b4a622008-02-26 20:26:43 +000043static std::string sLastErrorString;
44
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000045// Holds the initialization state of the LTO module.
46// *** Not thread safe ***
47static bool initialized = false;
48
Rafael Espindolaefa02d52013-10-02 14:36:23 +000049// Holds the command-line option parsing state of the LTO module.
50static bool parsedOptions = false;
51
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000052// Initialize the configured targets if they have not been initialized.
53static void lto_initialize() {
54 if (!initialized) {
Michael J. Spencer50a20c02015-01-29 17:20:41 +000055#ifdef LLVM_ON_WIN32
56 // Dialog box on crash disabling doesn't work across DLL boundaries, so do
57 // it here.
58 llvm::sys::DisableSystemDialogsOnCrash();
59#endif
60
Rafael Espindola77c50d22014-06-19 19:11:22 +000061 InitializeAllTargetInfos();
62 InitializeAllTargets();
63 InitializeAllTargetMCs();
64 InitializeAllAsmParsers();
65 InitializeAllAsmPrinters();
66 InitializeAllDisassemblers();
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000067 initialized = true;
68 }
69}
70
Patrik Hagglund9be9d872014-05-05 12:24:08 +000071DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOCodeGenerator, lto_code_gen_t)
72DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000073
Tom Roederfd1bc602014-04-25 21:46:51 +000074// Convert the subtarget features into a string to pass to LTOCodeGenerator.
75static void lto_add_attrs(lto_code_gen_t cg) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000076 LTOCodeGenerator *CG = unwrap(cg);
Tom Roederfd1bc602014-04-25 21:46:51 +000077 if (MAttrs.size()) {
78 std::string attrs;
79 for (unsigned i = 0; i < MAttrs.size(); ++i) {
80 if (i > 0)
81 attrs.append(",");
82 attrs.append(MAttrs[i]);
83 }
84
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000085 CG->setAttr(attrs.c_str());
Tom Roederfd1bc602014-04-25 21:46:51 +000086 }
87}
88
Bill Wendling36cbf032012-03-30 10:29:38 +000089extern const char* lto_get_version() {
90 return LTOCodeGenerator::getVersionString();
Nick Kledzik07b4a622008-02-26 20:26:43 +000091}
92
Bill Wendling36cbf032012-03-30 10:29:38 +000093const char* lto_get_error_message() {
94 return sLastErrorString.c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +000095}
96
Reid Klecknerddac1512013-10-24 22:26:04 +000097bool lto_module_is_object_file(const char* path) {
Bill Wendling36cbf032012-03-30 10:29:38 +000098 return LTOModule::isBitcodeFile(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +000099}
100
Reid Klecknerddac1512013-10-24 22:26:04 +0000101bool lto_module_is_object_file_for_target(const char* path,
Bill Wendling36cbf032012-03-30 10:29:38 +0000102 const char* target_triplet_prefix) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000103 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
104 if (!Buffer)
Alp Tokerac903802014-07-04 00:58:41 +0000105 return false;
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000106 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000107}
108
Reid Klecknerddac1512013-10-24 22:26:04 +0000109bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
Bill Wendling36cbf032012-03-30 10:29:38 +0000110 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000111}
112
Reid Klecknerddac1512013-10-24 22:26:04 +0000113bool
Bill Wendling36cbf032012-03-30 10:29:38 +0000114lto_module_is_object_file_in_memory_for_target(const void* mem,
115 size_t length,
116 const char* target_triplet_prefix) {
Alp Tokerac903802014-07-04 00:58:41 +0000117 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
118 if (!buffer)
119 return false;
120 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000121}
122
Bill Wendling36cbf032012-03-30 10:29:38 +0000123lto_module_t lto_module_create(const char* path) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000124 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000125 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000126 return wrap(LTOModule::createFromFile(path, Options, sLastErrorString));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000127}
128
Bill Wendling36cbf032012-03-30 10:29:38 +0000129lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000130 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000131 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000132 return wrap(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000133 LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString));
Rafael Espindola56e41f72011-02-08 22:40:47 +0000134}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000135
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000136lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
137 size_t file_size,
138 size_t map_size,
Bill Wendling36cbf032012-03-30 10:29:38 +0000139 off_t offset) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000140 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000141 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000142 return wrap(LTOModule::createFromOpenFileSlice(fd, path, map_size, offset,
143 Options, sLastErrorString));
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000144}
145
Bill Wendling36cbf032012-03-30 10:29:38 +0000146lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000147 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000148 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000149 return wrap(LTOModule::createFromBuffer(mem, length, Options, sLastErrorString));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000150}
151
Manman Ren03456a12014-02-10 23:26:14 +0000152lto_module_t lto_module_create_from_memory_with_path(const void* mem,
153 size_t length,
154 const char *path) {
155 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000156 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000157 return wrap(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000158 LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path));
Manman Ren03456a12014-02-10 23:26:14 +0000159}
160
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000161lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
162 const char *path) {
163 lto_initialize();
164 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
165 return wrap(LTOModule::createInLocalContext(mem, length, Options,
166 sLastErrorString, path));
167}
168
169lto_module_t lto_module_create_in_codegen_context(const void *mem,
170 size_t length,
171 const char *path,
172 lto_code_gen_t cg) {
173 lto_initialize();
174 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
175 return wrap(LTOModule::createInContext(mem, length, Options, sLastErrorString,
176 path, &unwrap(cg)->getContext()));
177}
178
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000179void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000180
Bill Wendling36cbf032012-03-30 10:29:38 +0000181const char* lto_module_get_target_triple(lto_module_t mod) {
Rafael Espindolad749fb52014-07-04 14:19:41 +0000182 return unwrap(mod)->getTargetTriple().c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000183}
184
Bill Wendling36cbf032012-03-30 10:29:38 +0000185void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000186 return unwrap(mod)->setTargetTriple(triple);
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000187}
188
Bill Wendling36cbf032012-03-30 10:29:38 +0000189unsigned int lto_module_get_num_symbols(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000190 return unwrap(mod)->getSymbolCount();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000191}
192
Bill Wendling36cbf032012-03-30 10:29:38 +0000193const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000194 return unwrap(mod)->getSymbolName(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000195}
196
Bill Wendling36cbf032012-03-30 10:29:38 +0000197lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
198 unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000199 return unwrap(mod)->getSymbolAttributes(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000200}
201
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000202unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000203 return unwrap(mod)->getDependentLibraryCount();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000204}
205
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000206const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000207 return unwrap(mod)->getDependentLibrary(index);
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000208}
209
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000210unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000211 return unwrap(mod)->getLinkerOptCount();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000212}
213
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000214const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000215 return unwrap(mod)->getLinkerOpt(index);
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000216}
217
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000218void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
219 lto_diagnostic_handler_t diag_handler,
220 void *ctxt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000221 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000222}
223
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000224static lto_code_gen_t createCodeGen(bool InLocalContext) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000225 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000226
Eli Benderskyf0f21002014-02-19 17:09:35 +0000227 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000228
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000229 LTOCodeGenerator *CodeGen =
230 InLocalContext ? new LTOCodeGenerator(make_unique<LLVMContext>())
231 : new LTOCodeGenerator();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000232 if (CodeGen)
233 CodeGen->setTargetOptions(Options);
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000234 return wrap(CodeGen);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000235}
236
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000237lto_code_gen_t lto_codegen_create(void) {
238 return createCodeGen(/* InLocalContext */ false);
239}
240
241lto_code_gen_t lto_codegen_create_in_local_context(void) {
242 return createCodeGen(/* InLocalContext */ true);
243}
244
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000245void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000246
Reid Klecknerddac1512013-10-24 22:26:04 +0000247bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000248 return !unwrap(cg)->addModule(unwrap(mod));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000249}
250
Reid Klecknerddac1512013-10-24 22:26:04 +0000251bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000252 unwrap(cg)->setDebugInfo(debug);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000253 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000254}
255
Reid Klecknerddac1512013-10-24 22:26:04 +0000256bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000257 unwrap(cg)->setCodePICModel(model);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000258 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000259}
260
Bill Wendling152e4732012-03-31 10:44:20 +0000261void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000262 return unwrap(cg)->setCpu(cpu);
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000263}
264
Bill Wendling36cbf032012-03-30 10:29:38 +0000265void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000266 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcac8c8a2009-06-04 00:28:45 +0000267}
268
Bill Wendling152e4732012-03-31 10:44:20 +0000269void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling36cbf032012-03-30 10:29:38 +0000270 int nargs) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000271 // In here only for backwards compatibility. We use MC now.
Rafael Espindola00456462010-08-10 18:55:09 +0000272}
273
Bill Wendling36cbf032012-03-30 10:29:38 +0000274void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling152e4732012-03-31 10:44:20 +0000275 const char *symbol) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000276 unwrap(cg)->addMustPreserveSymbol(symbol);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000277}
278
Reid Klecknerddac1512013-10-24 22:26:04 +0000279bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000280 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000281 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000282 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000283 parsedOptions = true;
284 }
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000285 return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000286}
287
Bill Wendling152e4732012-03-31 10:44:20 +0000288const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000289 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000290 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000291 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000292 parsedOptions = true;
293 }
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000294 return unwrap(cg)->compile(length, DisableOpt, DisableInline,
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000295 DisableGVNLoadPRE, DisableLTOVectorization,
296 sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000297}
298
Reid Klecknerddac1512013-10-24 22:26:04 +0000299bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000300 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000301 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000302 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000303 parsedOptions = true;
304 }
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000305 return !unwrap(cg)->compile_to_file(
306 name, DisableOpt, DisableInline, DisableGVNLoadPRE,
307 DisableLTOVectorization, sLastErrorString);
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000308}
309
Bill Wendling152e4732012-03-31 10:44:20 +0000310void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000311 unwrap(cg)->setCodeGenDebugOptions(opt);
Duncan Sands31554ab2009-07-03 15:38:01 +0000312}