blob: 5732996a160685b20516d8e524c5ed7c6c2a2eb1 [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
Bill Wendling36cbf032012-03-30 10:29:38 +000035// Holds most recent error string.
36// *** Not thread safe ***
Nick Kledzik07b4a622008-02-26 20:26:43 +000037static std::string sLastErrorString;
38
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000039// Holds the initialization state of the LTO module.
40// *** Not thread safe ***
41static bool initialized = false;
42
Rafael Espindolaefa02d52013-10-02 14:36:23 +000043// Holds the command-line option parsing state of the LTO module.
44static bool parsedOptions = false;
45
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000046// Initialize the configured targets if they have not been initialized.
47static void lto_initialize() {
48 if (!initialized) {
Rafael Espindola77c50d22014-06-19 19:11:22 +000049 InitializeAllTargetInfos();
50 InitializeAllTargets();
51 InitializeAllTargetMCs();
52 InitializeAllAsmParsers();
53 InitializeAllAsmPrinters();
54 InitializeAllDisassemblers();
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000055 initialized = true;
56 }
57}
58
Patrik Hagglund9be9d872014-05-05 12:24:08 +000059DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOCodeGenerator, lto_code_gen_t)
60DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000061
Tom Roederfd1bc602014-04-25 21:46:51 +000062// Convert the subtarget features into a string to pass to LTOCodeGenerator.
63static void lto_add_attrs(lto_code_gen_t cg) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000064 LTOCodeGenerator *CG = unwrap(cg);
Tom Roederfd1bc602014-04-25 21:46:51 +000065 if (MAttrs.size()) {
66 std::string attrs;
67 for (unsigned i = 0; i < MAttrs.size(); ++i) {
68 if (i > 0)
69 attrs.append(",");
70 attrs.append(MAttrs[i]);
71 }
72
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000073 CG->setAttr(attrs.c_str());
Tom Roederfd1bc602014-04-25 21:46:51 +000074 }
75}
76
Bill Wendling36cbf032012-03-30 10:29:38 +000077extern const char* lto_get_version() {
78 return LTOCodeGenerator::getVersionString();
Nick Kledzik07b4a622008-02-26 20:26:43 +000079}
80
Bill Wendling36cbf032012-03-30 10:29:38 +000081const char* lto_get_error_message() {
82 return sLastErrorString.c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +000083}
84
Reid Klecknerddac1512013-10-24 22:26:04 +000085bool lto_module_is_object_file(const char* path) {
Bill Wendling36cbf032012-03-30 10:29:38 +000086 return LTOModule::isBitcodeFile(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +000087}
88
Reid Klecknerddac1512013-10-24 22:26:04 +000089bool lto_module_is_object_file_for_target(const char* path,
Bill Wendling36cbf032012-03-30 10:29:38 +000090 const char* target_triplet_prefix) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +000091 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
92 if (!Buffer)
Alp Tokerac903802014-07-04 00:58:41 +000093 return false;
Rafael Espindolaadf21f22014-07-06 17:43:13 +000094 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +000095}
96
Reid Klecknerddac1512013-10-24 22:26:04 +000097bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
Bill Wendling36cbf032012-03-30 10:29:38 +000098 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik07b4a622008-02-26 20:26:43 +000099}
100
Reid Klecknerddac1512013-10-24 22:26:04 +0000101bool
Bill Wendling36cbf032012-03-30 10:29:38 +0000102lto_module_is_object_file_in_memory_for_target(const void* mem,
103 size_t length,
104 const char* target_triplet_prefix) {
Alp Tokerac903802014-07-04 00:58:41 +0000105 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
106 if (!buffer)
107 return false;
108 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000109}
110
Bill Wendling36cbf032012-03-30 10:29:38 +0000111lto_module_t lto_module_create(const char* path) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000112 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000113 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000114 return wrap(LTOModule::createFromFile(path, Options, sLastErrorString));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000115}
116
Bill Wendling36cbf032012-03-30 10:29:38 +0000117lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000118 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000119 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000120 return wrap(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000121 LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString));
Rafael Espindola56e41f72011-02-08 22:40:47 +0000122}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000123
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000124lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
125 size_t file_size,
126 size_t map_size,
Bill Wendling36cbf032012-03-30 10:29:38 +0000127 off_t offset) {
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::createFromOpenFileSlice(fd, path, map_size, offset,
131 Options, sLastErrorString));
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000132}
133
Bill Wendling36cbf032012-03-30 10:29:38 +0000134lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000135 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000136 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000137 return wrap(LTOModule::createFromBuffer(mem, length, Options, sLastErrorString));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000138}
139
Manman Ren03456a12014-02-10 23:26:14 +0000140lto_module_t lto_module_create_from_memory_with_path(const void* mem,
141 size_t length,
142 const char *path) {
143 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000144 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000145 return wrap(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000146 LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path));
Manman Ren03456a12014-02-10 23:26:14 +0000147}
148
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000149void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000150
Bill Wendling36cbf032012-03-30 10:29:38 +0000151const char* lto_module_get_target_triple(lto_module_t mod) {
Rafael Espindolad749fb52014-07-04 14:19:41 +0000152 return unwrap(mod)->getTargetTriple().c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000153}
154
Bill Wendling36cbf032012-03-30 10:29:38 +0000155void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000156 return unwrap(mod)->setTargetTriple(triple);
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000157}
158
Bill Wendling36cbf032012-03-30 10:29:38 +0000159unsigned int lto_module_get_num_symbols(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000160 return unwrap(mod)->getSymbolCount();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000161}
162
Bill Wendling36cbf032012-03-30 10:29:38 +0000163const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000164 return unwrap(mod)->getSymbolName(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000165}
166
Bill Wendling36cbf032012-03-30 10:29:38 +0000167lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
168 unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000169 return unwrap(mod)->getSymbolAttributes(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000170}
171
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000172unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000173 return unwrap(mod)->getDependentLibraryCount();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000174}
175
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000176const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000177 return unwrap(mod)->getDependentLibrary(index);
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000178}
179
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000180unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000181 return unwrap(mod)->getLinkerOptCount();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000182}
183
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000184const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000185 return unwrap(mod)->getLinkerOpt(index);
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000186}
187
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000188void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
189 lto_diagnostic_handler_t diag_handler,
190 void *ctxt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000191 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000192}
193
Bill Wendling36cbf032012-03-30 10:29:38 +0000194lto_code_gen_t lto_codegen_create(void) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000195 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000196
Eli Benderskyf0f21002014-02-19 17:09:35 +0000197 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000198
199 LTOCodeGenerator *CodeGen = new LTOCodeGenerator();
200 if (CodeGen)
201 CodeGen->setTargetOptions(Options);
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000202 return wrap(CodeGen);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000203}
204
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000205void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000206
Reid Klecknerddac1512013-10-24 22:26:04 +0000207bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000208 return !unwrap(cg)->addModule(unwrap(mod), sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000209}
210
Reid Klecknerddac1512013-10-24 22:26:04 +0000211bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000212 unwrap(cg)->setDebugInfo(debug);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000213 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000214}
215
Reid Klecknerddac1512013-10-24 22:26:04 +0000216bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000217 unwrap(cg)->setCodePICModel(model);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000218 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000219}
220
Bill Wendling152e4732012-03-31 10:44:20 +0000221void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000222 return unwrap(cg)->setCpu(cpu);
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000223}
224
Bill Wendling36cbf032012-03-30 10:29:38 +0000225void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000226 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcac8c8a2009-06-04 00:28:45 +0000227}
228
Bill Wendling152e4732012-03-31 10:44:20 +0000229void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling36cbf032012-03-30 10:29:38 +0000230 int nargs) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000231 // In here only for backwards compatibility. We use MC now.
Rafael Espindola00456462010-08-10 18:55:09 +0000232}
233
Bill Wendling36cbf032012-03-30 10:29:38 +0000234void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling152e4732012-03-31 10:44:20 +0000235 const char *symbol) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000236 unwrap(cg)->addMustPreserveSymbol(symbol);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000237}
238
Reid Klecknerddac1512013-10-24 22:26:04 +0000239bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000240 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000241 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000242 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000243 parsedOptions = true;
244 }
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000245 return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000246}
247
Bill Wendling152e4732012-03-31 10:44:20 +0000248const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000249 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000250 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000251 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000252 parsedOptions = true;
253 }
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000254 return unwrap(cg)->compile(length, DisableOpt, DisableInline,
255 DisableGVNLoadPRE, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000256}
257
Reid Klecknerddac1512013-10-24 22:26:04 +0000258bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000259 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000260 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000261 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000262 parsedOptions = true;
263 }
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000264 return !unwrap(cg)->compile_to_file(name, DisableOpt, DisableInline,
265 DisableGVNLoadPRE, sLastErrorString);
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000266}
267
Bill Wendling152e4732012-03-31 10:44:20 +0000268void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000269 unwrap(cg)->setCodeGenDebugOptions(opt);
Duncan Sands31554ab2009-07-03 15:38:01 +0000270}