blob: ec0372ea56076e626f198f6c5162fde6b6a6cad2 [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"
Rafael Espindola77c50d22014-06-19 19:11:22 +000021#include "llvm/Support/TargetSelect.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000022
Rafael Espindola0b385c72013-09-30 16:39:19 +000023// extra command-line flags needed for LTOCodeGenerator
24static cl::opt<bool>
25DisableOpt("disable-opt", cl::init(false),
26 cl::desc("Do not run any optimization passes"));
27
28static cl::opt<bool>
29DisableInline("disable-inlining", cl::init(false),
30 cl::desc("Do not run the inliner pass"));
31
32static cl::opt<bool>
33DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
34 cl::desc("Do not run the GVN load PRE pass"));
Nick Kledzik07b4a622008-02-26 20:26:43 +000035
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +000036static cl::opt<bool>
37DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
38 cl::desc("Do not run loop or slp vectorization during LTO"));
39
Bill Wendling36cbf032012-03-30 10:29:38 +000040// Holds most recent error string.
41// *** Not thread safe ***
Nick Kledzik07b4a622008-02-26 20:26:43 +000042static std::string sLastErrorString;
43
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000044// Holds the initialization state of the LTO module.
45// *** Not thread safe ***
46static bool initialized = false;
47
Rafael Espindolaefa02d52013-10-02 14:36:23 +000048// Holds the command-line option parsing state of the LTO module.
49static bool parsedOptions = false;
50
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000051// Initialize the configured targets if they have not been initialized.
52static void lto_initialize() {
53 if (!initialized) {
Rafael Espindola77c50d22014-06-19 19:11:22 +000054 InitializeAllTargetInfos();
55 InitializeAllTargets();
56 InitializeAllTargetMCs();
57 InitializeAllAsmParsers();
58 InitializeAllAsmPrinters();
59 InitializeAllDisassemblers();
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000060 initialized = true;
61 }
62}
63
Patrik Hagglund9be9d872014-05-05 12:24:08 +000064DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOCodeGenerator, lto_code_gen_t)
65DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000066
Tom Roederfd1bc602014-04-25 21:46:51 +000067// Convert the subtarget features into a string to pass to LTOCodeGenerator.
68static void lto_add_attrs(lto_code_gen_t cg) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000069 LTOCodeGenerator *CG = unwrap(cg);
Tom Roederfd1bc602014-04-25 21:46:51 +000070 if (MAttrs.size()) {
71 std::string attrs;
72 for (unsigned i = 0; i < MAttrs.size(); ++i) {
73 if (i > 0)
74 attrs.append(",");
75 attrs.append(MAttrs[i]);
76 }
77
Rafael Espindola83ceb8e2014-05-03 14:59:52 +000078 CG->setAttr(attrs.c_str());
Tom Roederfd1bc602014-04-25 21:46:51 +000079 }
80}
81
Bill Wendling36cbf032012-03-30 10:29:38 +000082extern const char* lto_get_version() {
83 return LTOCodeGenerator::getVersionString();
Nick Kledzik07b4a622008-02-26 20:26:43 +000084}
85
Bill Wendling36cbf032012-03-30 10:29:38 +000086const char* lto_get_error_message() {
87 return sLastErrorString.c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +000088}
89
Reid Klecknerddac1512013-10-24 22:26:04 +000090bool lto_module_is_object_file(const char* path) {
Bill Wendling36cbf032012-03-30 10:29:38 +000091 return LTOModule::isBitcodeFile(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +000092}
93
Reid Klecknerddac1512013-10-24 22:26:04 +000094bool lto_module_is_object_file_for_target(const char* path,
Bill Wendling36cbf032012-03-30 10:29:38 +000095 const char* target_triplet_prefix) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +000096 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
97 if (!Buffer)
Alp Tokerac903802014-07-04 00:58:41 +000098 return false;
Rafael Espindolaadf21f22014-07-06 17:43:13 +000099 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000100}
101
Reid Klecknerddac1512013-10-24 22:26:04 +0000102bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
Bill Wendling36cbf032012-03-30 10:29:38 +0000103 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000104}
105
Reid Klecknerddac1512013-10-24 22:26:04 +0000106bool
Bill Wendling36cbf032012-03-30 10:29:38 +0000107lto_module_is_object_file_in_memory_for_target(const void* mem,
108 size_t length,
109 const char* target_triplet_prefix) {
Alp Tokerac903802014-07-04 00:58:41 +0000110 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
111 if (!buffer)
112 return false;
113 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000114}
115
Bill Wendling36cbf032012-03-30 10:29:38 +0000116lto_module_t lto_module_create(const char* path) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000117 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000118 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000119 return wrap(LTOModule::createFromFile(path, Options, sLastErrorString));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000120}
121
Bill Wendling36cbf032012-03-30 10:29:38 +0000122lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000123 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000124 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000125 return wrap(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000126 LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString));
Rafael Espindola56e41f72011-02-08 22:40:47 +0000127}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000128
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000129lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
130 size_t file_size,
131 size_t map_size,
Bill Wendling36cbf032012-03-30 10:29:38 +0000132 off_t offset) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000133 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000134 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000135 return wrap(LTOModule::createFromOpenFileSlice(fd, path, map_size, offset,
136 Options, sLastErrorString));
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000137}
138
Bill Wendling36cbf032012-03-30 10:29:38 +0000139lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
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::createFromBuffer(mem, length, Options, sLastErrorString));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000143}
144
Manman Ren03456a12014-02-10 23:26:14 +0000145lto_module_t lto_module_create_from_memory_with_path(const void* mem,
146 size_t length,
147 const char *path) {
148 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000149 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000150 return wrap(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000151 LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path));
Manman Ren03456a12014-02-10 23:26:14 +0000152}
153
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000154lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
155 const char *path) {
156 lto_initialize();
157 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
158 return wrap(LTOModule::createInLocalContext(mem, length, Options,
159 sLastErrorString, path));
160}
161
162lto_module_t lto_module_create_in_codegen_context(const void *mem,
163 size_t length,
164 const char *path,
165 lto_code_gen_t cg) {
166 lto_initialize();
167 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
168 return wrap(LTOModule::createInContext(mem, length, Options, sLastErrorString,
169 path, &unwrap(cg)->getContext()));
170}
171
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000172void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000173
Bill Wendling36cbf032012-03-30 10:29:38 +0000174const char* lto_module_get_target_triple(lto_module_t mod) {
Rafael Espindolad749fb52014-07-04 14:19:41 +0000175 return unwrap(mod)->getTargetTriple().c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000176}
177
Bill Wendling36cbf032012-03-30 10:29:38 +0000178void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000179 return unwrap(mod)->setTargetTriple(triple);
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000180}
181
Bill Wendling36cbf032012-03-30 10:29:38 +0000182unsigned int lto_module_get_num_symbols(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000183 return unwrap(mod)->getSymbolCount();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000184}
185
Bill Wendling36cbf032012-03-30 10:29:38 +0000186const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000187 return unwrap(mod)->getSymbolName(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000188}
189
Bill Wendling36cbf032012-03-30 10:29:38 +0000190lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
191 unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000192 return unwrap(mod)->getSymbolAttributes(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000193}
194
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000195unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000196 return unwrap(mod)->getDependentLibraryCount();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000197}
198
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000199const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000200 return unwrap(mod)->getDependentLibrary(index);
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000201}
202
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000203unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000204 return unwrap(mod)->getLinkerOptCount();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000205}
206
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000207const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000208 return unwrap(mod)->getLinkerOpt(index);
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000209}
210
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000211void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
212 lto_diagnostic_handler_t diag_handler,
213 void *ctxt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000214 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000215}
216
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000217static lto_code_gen_t createCodeGen(bool InLocalContext) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000218 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000219
Eli Benderskyf0f21002014-02-19 17:09:35 +0000220 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000221
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000222 LTOCodeGenerator *CodeGen =
223 InLocalContext ? new LTOCodeGenerator(make_unique<LLVMContext>())
224 : new LTOCodeGenerator();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000225 if (CodeGen)
226 CodeGen->setTargetOptions(Options);
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000227 return wrap(CodeGen);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000228}
229
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000230lto_code_gen_t lto_codegen_create(void) {
231 return createCodeGen(/* InLocalContext */ false);
232}
233
234lto_code_gen_t lto_codegen_create_in_local_context(void) {
235 return createCodeGen(/* InLocalContext */ true);
236}
237
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000238void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000239
Reid Klecknerddac1512013-10-24 22:26:04 +0000240bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000241 return !unwrap(cg)->addModule(unwrap(mod));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000242}
243
Reid Klecknerddac1512013-10-24 22:26:04 +0000244bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000245 unwrap(cg)->setDebugInfo(debug);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000246 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000247}
248
Reid Klecknerddac1512013-10-24 22:26:04 +0000249bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000250 unwrap(cg)->setCodePICModel(model);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000251 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000252}
253
Bill Wendling152e4732012-03-31 10:44:20 +0000254void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000255 return unwrap(cg)->setCpu(cpu);
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000256}
257
Bill Wendling36cbf032012-03-30 10:29:38 +0000258void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000259 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcac8c8a2009-06-04 00:28:45 +0000260}
261
Bill Wendling152e4732012-03-31 10:44:20 +0000262void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling36cbf032012-03-30 10:29:38 +0000263 int nargs) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000264 // In here only for backwards compatibility. We use MC now.
Rafael Espindola00456462010-08-10 18:55:09 +0000265}
266
Bill Wendling36cbf032012-03-30 10:29:38 +0000267void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling152e4732012-03-31 10:44:20 +0000268 const char *symbol) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000269 unwrap(cg)->addMustPreserveSymbol(symbol);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000270}
271
Reid Klecknerddac1512013-10-24 22:26:04 +0000272bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000273 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000274 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000275 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000276 parsedOptions = true;
277 }
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000278 return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000279}
280
Bill Wendling152e4732012-03-31 10:44:20 +0000281const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
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 }
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000287 return unwrap(cg)->compile(length, DisableOpt, DisableInline,
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000288 DisableGVNLoadPRE, DisableLTOVectorization,
289 sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000290}
291
Reid Klecknerddac1512013-10-24 22:26:04 +0000292bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000293 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000294 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000295 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000296 parsedOptions = true;
297 }
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000298 return !unwrap(cg)->compile_to_file(
299 name, DisableOpt, DisableInline, DisableGVNLoadPRE,
300 DisableLTOVectorization, sLastErrorString);
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000301}
302
Bill Wendling152e4732012-03-31 10:44:20 +0000303void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000304 unwrap(cg)->setCodeGenDebugOptions(opt);
Duncan Sands31554ab2009-07-03 15:38:01 +0000305}