blob: 7829dd06d0e80a03cc6330023dc1160658525420 [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"
Benjamin Kramer0a446fd2015-03-01 21:28:53 +000016#include "llvm/ADT/STLExtras.h"
Rafael Espindola0b385c72013-09-30 16:39:19 +000017#include "llvm/CodeGen/CommandFlags.h"
Rafael Espindolaa7612b42015-12-04 16:14:31 +000018#include "llvm/IR/DiagnosticInfo.h"
19#include "llvm/IR/DiagnosticPrinter.h"
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +000020#include "llvm/IR/LLVMContext.h"
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000021#include "llvm/LTO/LTOCodeGenerator.h"
22#include "llvm/LTO/LTOModule.h"
Mehdi Amini7c4a1a82016-03-09 01:37:22 +000023#include "llvm/LTO/ThinLTOCodeGenerator.h"
Alp Tokerac903802014-07-04 00:58:41 +000024#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer50a20c02015-01-29 17:20:41 +000025#include "llvm/Support/Signals.h"
Rafael Espindola77c50d22014-06-19 19:11:22 +000026#include "llvm/Support/TargetSelect.h"
Reid Kleckner7c6b5e12016-01-29 00:03:34 +000027#include "llvm/Support/raw_ostream.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000028
Rafael Espindola0b385c72013-09-30 16:39:19 +000029// extra command-line flags needed for LTOCodeGenerator
Peter Collingbourne070843d2015-03-19 22:01:00 +000030static cl::opt<char>
31OptLevel("O",
32 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
33 "(default = '-O2')"),
34 cl::Prefix,
35 cl::ZeroOrMore,
36 cl::init('2'));
Rafael Espindola0b385c72013-09-30 16:39:19 +000037
38static cl::opt<bool>
39DisableInline("disable-inlining", cl::init(false),
40 cl::desc("Do not run the inliner pass"));
41
42static cl::opt<bool>
43DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
44 cl::desc("Do not run the GVN load PRE pass"));
Nick Kledzik07b4a622008-02-26 20:26:43 +000045
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +000046static cl::opt<bool>
47DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
48 cl::desc("Do not run loop or slp vectorization during LTO"));
49
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +000050#ifdef NDEBUG
51static bool VerifyByDefault = false;
52#else
53static bool VerifyByDefault = true;
54#endif
55
56static cl::opt<bool> DisableVerify(
57 "disable-llvm-verifier", cl::init(!VerifyByDefault),
58 cl::desc("Don't run the LLVM verifier during the optimization pipeline"));
59
Bill Wendling36cbf032012-03-30 10:29:38 +000060// Holds most recent error string.
61// *** Not thread safe ***
Nick Kledzik07b4a622008-02-26 20:26:43 +000062static std::string sLastErrorString;
63
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000064// Holds the initialization state of the LTO module.
65// *** Not thread safe ***
66static bool initialized = false;
67
Rafael Espindolaefa02d52013-10-02 14:36:23 +000068// Holds the command-line option parsing state of the LTO module.
69static bool parsedOptions = false;
70
Rafael Espindolaa7612b42015-12-04 16:14:31 +000071static LLVMContext *LTOContext = nullptr;
72
73static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
74 if (DI.getSeverity() != DS_Error) {
75 DiagnosticPrinterRawOStream DP(errs());
76 DI.print(DP);
77 errs() << '\n';
78 return;
79 }
80 sLastErrorString = "";
81 {
82 raw_string_ostream Stream(sLastErrorString);
83 DiagnosticPrinterRawOStream DP(Stream);
84 DI.print(DP);
85 }
Rafael Espindolaa7612b42015-12-04 16:14:31 +000086}
87
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000088// Initialize the configured targets if they have not been initialized.
89static void lto_initialize() {
90 if (!initialized) {
Michael J. Spencer50a20c02015-01-29 17:20:41 +000091#ifdef LLVM_ON_WIN32
92 // Dialog box on crash disabling doesn't work across DLL boundaries, so do
93 // it here.
94 llvm::sys::DisableSystemDialogsOnCrash();
95#endif
96
Rafael Espindola77c50d22014-06-19 19:11:22 +000097 InitializeAllTargetInfos();
98 InitializeAllTargets();
99 InitializeAllTargetMCs();
100 InitializeAllAsmParsers();
101 InitializeAllAsmPrinters();
102 InitializeAllDisassemblers();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000103
Mehdi Amini03b42e42016-04-14 21:59:01 +0000104 static LLVMContext Context;
105 LTOContext = &Context;
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000106 LTOContext->setDiagnosticHandler(diagnosticHandler, nullptr, true);
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000107 initialized = true;
108 }
109}
110
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000111namespace {
112
Yunzhong Gaoea7b3a22015-11-11 19:59:08 +0000113static void handleLibLTODiagnostic(lto_codegen_diagnostic_severity_t Severity,
114 const char *Msg, void *) {
115 sLastErrorString = Msg;
Yunzhong Gaoea7b3a22015-11-11 19:59:08 +0000116}
117
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000118// This derived class owns the native object file. This helps implement the
119// libLTO API semantics, which require that the code generator owns the object
120// file.
121struct LibLTOCodeGenerator : LTOCodeGenerator {
Duncan P. N. Exon Smith247e6362016-04-16 22:25:36 +0000122 LibLTOCodeGenerator() : LTOCodeGenerator(*LTOContext) { init(); }
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000123 LibLTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
Rafael Espindola7b8a24e2015-12-04 02:42:28 +0000124 : LTOCodeGenerator(*Context), OwnedContext(std::move(Context)) {
Duncan P. N. Exon Smith247e6362016-04-16 22:25:36 +0000125 init();
126 }
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000127
Steven Wub5104b52015-12-09 03:37:51 +0000128 // Reset the module first in case MergedModule is created in OwnedContext.
129 // Module must be destructed before its context gets destructed.
130 ~LibLTOCodeGenerator() { resetMergedModule(); }
131
Duncan P. N. Exon Smith247e6362016-04-16 22:25:36 +0000132 void init() { setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
133
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000134 std::unique_ptr<MemoryBuffer> NativeObjectFile;
Rafael Espindola7b8a24e2015-12-04 02:42:28 +0000135 std::unique_ptr<LLVMContext> OwnedContext;
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000136};
137
138}
139
140DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LibLTOCodeGenerator, lto_code_gen_t)
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000141DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThinLTOCodeGenerator, thinlto_code_gen_t)
Patrik Hagglund9be9d872014-05-05 12:24:08 +0000142DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000143
Tom Roederfd1bc602014-04-25 21:46:51 +0000144// Convert the subtarget features into a string to pass to LTOCodeGenerator.
145static void lto_add_attrs(lto_code_gen_t cg) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000146 LTOCodeGenerator *CG = unwrap(cg);
Tom Roederfd1bc602014-04-25 21:46:51 +0000147 if (MAttrs.size()) {
148 std::string attrs;
149 for (unsigned i = 0; i < MAttrs.size(); ++i) {
150 if (i > 0)
151 attrs.append(",");
152 attrs.append(MAttrs[i]);
153 }
154
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000155 CG->setAttr(attrs.c_str());
Tom Roederfd1bc602014-04-25 21:46:51 +0000156 }
Peter Collingbourne070843d2015-03-19 22:01:00 +0000157
158 if (OptLevel < '0' || OptLevel > '3')
159 report_fatal_error("Optimization level must be between 0 and 3");
160 CG->setOptLevel(OptLevel - '0');
Tom Roederfd1bc602014-04-25 21:46:51 +0000161}
162
Bill Wendling36cbf032012-03-30 10:29:38 +0000163extern const char* lto_get_version() {
164 return LTOCodeGenerator::getVersionString();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000165}
166
Bill Wendling36cbf032012-03-30 10:29:38 +0000167const char* lto_get_error_message() {
168 return sLastErrorString.c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000169}
170
Reid Klecknerddac1512013-10-24 22:26:04 +0000171bool lto_module_is_object_file(const char* path) {
Bill Wendling36cbf032012-03-30 10:29:38 +0000172 return LTOModule::isBitcodeFile(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000173}
174
Reid Klecknerddac1512013-10-24 22:26:04 +0000175bool lto_module_is_object_file_for_target(const char* path,
Bill Wendling36cbf032012-03-30 10:29:38 +0000176 const char* target_triplet_prefix) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000177 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
178 if (!Buffer)
Alp Tokerac903802014-07-04 00:58:41 +0000179 return false;
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000180 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000181}
182
Reid Klecknerddac1512013-10-24 22:26:04 +0000183bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
Bill Wendling36cbf032012-03-30 10:29:38 +0000184 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000185}
186
Reid Klecknerddac1512013-10-24 22:26:04 +0000187bool
Bill Wendling36cbf032012-03-30 10:29:38 +0000188lto_module_is_object_file_in_memory_for_target(const void* mem,
189 size_t length,
190 const char* target_triplet_prefix) {
Alp Tokerac903802014-07-04 00:58:41 +0000191 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
192 if (!buffer)
193 return false;
194 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000195}
196
Bill Wendling36cbf032012-03-30 10:29:38 +0000197lto_module_t lto_module_create(const char* path) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000198 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000199 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000200 ErrorOr<std::unique_ptr<LTOModule>> M =
201 LTOModule::createFromFile(*LTOContext, path, Options);
202 if (!M)
203 return nullptr;
204 return wrap(M->release());
Nick Kledzik07b4a622008-02-26 20:26:43 +0000205}
206
Bill Wendling36cbf032012-03-30 10:29:38 +0000207lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000208 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000209 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000210 ErrorOr<std::unique_ptr<LTOModule>> M =
211 LTOModule::createFromOpenFile(*LTOContext, fd, path, size, Options);
212 if (!M)
213 return nullptr;
214 return wrap(M->release());
Rafael Espindola56e41f72011-02-08 22:40:47 +0000215}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000216
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000217lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
218 size_t file_size,
219 size_t map_size,
Bill Wendling36cbf032012-03-30 10:29:38 +0000220 off_t offset) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000221 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000222 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000223 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromOpenFileSlice(
224 *LTOContext, fd, path, map_size, offset, Options);
225 if (!M)
226 return nullptr;
227 return wrap(M->release());
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000228}
229
Bill Wendling36cbf032012-03-30 10:29:38 +0000230lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000231 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000232 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000233 ErrorOr<std::unique_ptr<LTOModule>> M =
234 LTOModule::createFromBuffer(*LTOContext, mem, length, Options);
235 if (!M)
236 return nullptr;
237 return wrap(M->release());
Nick Kledzik07b4a622008-02-26 20:26:43 +0000238}
239
Manman Ren03456a12014-02-10 23:26:14 +0000240lto_module_t lto_module_create_from_memory_with_path(const void* mem,
241 size_t length,
242 const char *path) {
243 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000244 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000245 ErrorOr<std::unique_ptr<LTOModule>> M =
246 LTOModule::createFromBuffer(*LTOContext, mem, length, Options, path);
247 if (!M)
248 return nullptr;
249 return wrap(M->release());
Manman Ren03456a12014-02-10 23:26:14 +0000250}
251
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000252lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
253 const char *path) {
254 lto_initialize();
255 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Petr Pavlu7ad9ec92016-03-01 13:13:49 +0000256
257 // Create a local context. Ownership will be transfered to LTOModule.
258 std::unique_ptr<LLVMContext> Context = llvm::make_unique<LLVMContext>();
259 Context->setDiagnosticHandler(diagnosticHandler, nullptr, true);
260
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000261 ErrorOr<std::unique_ptr<LTOModule>> M =
Petr Pavlu7ad9ec92016-03-01 13:13:49 +0000262 LTOModule::createInLocalContext(std::move(Context), mem, length, Options,
263 path);
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000264 if (!M)
265 return nullptr;
266 return wrap(M->release());
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000267}
268
269lto_module_t lto_module_create_in_codegen_context(const void *mem,
270 size_t length,
271 const char *path,
272 lto_code_gen_t cg) {
273 lto_initialize();
274 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Petr Pavlu7ad9ec92016-03-01 13:13:49 +0000275 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromBuffer(
276 unwrap(cg)->getContext(), mem, length, Options, path);
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000277 return wrap(M->release());
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000278}
279
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000280void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000281
Bill Wendling36cbf032012-03-30 10:29:38 +0000282const char* lto_module_get_target_triple(lto_module_t mod) {
Rafael Espindolad749fb52014-07-04 14:19:41 +0000283 return unwrap(mod)->getTargetTriple().c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000284}
285
Bill Wendling36cbf032012-03-30 10:29:38 +0000286void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000287 return unwrap(mod)->setTargetTriple(triple);
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000288}
289
Bill Wendling36cbf032012-03-30 10:29:38 +0000290unsigned int lto_module_get_num_symbols(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000291 return unwrap(mod)->getSymbolCount();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000292}
293
Bill Wendling36cbf032012-03-30 10:29:38 +0000294const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000295 return unwrap(mod)->getSymbolName(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000296}
297
Bill Wendling36cbf032012-03-30 10:29:38 +0000298lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
299 unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000300 return unwrap(mod)->getSymbolAttributes(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000301}
302
Peter Collingbourne5d7dffb2015-06-29 23:09:12 +0000303const char* lto_module_get_linkeropts(lto_module_t mod) {
Peter Collingbourneaef36592015-06-29 22:04:09 +0000304 return unwrap(mod)->getLinkerOpts();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000305}
306
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000307void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
308 lto_diagnostic_handler_t diag_handler,
309 void *ctxt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000310 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000311}
312
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000313static lto_code_gen_t createCodeGen(bool InLocalContext) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000314 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000315
Eli Benderskyf0f21002014-02-19 17:09:35 +0000316 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000317
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000318 LibLTOCodeGenerator *CodeGen =
319 InLocalContext ? new LibLTOCodeGenerator(make_unique<LLVMContext>())
320 : new LibLTOCodeGenerator();
321 CodeGen->setTargetOptions(Options);
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000322 return wrap(CodeGen);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000323}
324
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000325lto_code_gen_t lto_codegen_create(void) {
326 return createCodeGen(/* InLocalContext */ false);
327}
328
329lto_code_gen_t lto_codegen_create_in_local_context(void) {
330 return createCodeGen(/* InLocalContext */ true);
331}
332
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000333void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000334
Reid Klecknerddac1512013-10-24 22:26:04 +0000335bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000336 return !unwrap(cg)->addModule(unwrap(mod));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000337}
338
Manman Ren6487ce92015-02-24 00:45:56 +0000339void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {
Peter Collingbourne9c8909d2015-08-24 22:22:53 +0000340 unwrap(cg)->setModule(std::unique_ptr<LTOModule>(unwrap(mod)));
Manman Ren6487ce92015-02-24 00:45:56 +0000341}
342
Reid Klecknerddac1512013-10-24 22:26:04 +0000343bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000344 unwrap(cg)->setDebugInfo(debug);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000345 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000346}
347
Reid Klecknerddac1512013-10-24 22:26:04 +0000348bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Peter Collingbourne44ee84e2015-08-21 22:57:17 +0000349 switch (model) {
350 case LTO_CODEGEN_PIC_MODEL_STATIC:
351 unwrap(cg)->setCodePICModel(Reloc::Static);
352 return false;
353 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
354 unwrap(cg)->setCodePICModel(Reloc::PIC_);
355 return false;
356 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
357 unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC);
358 return false;
359 case LTO_CODEGEN_PIC_MODEL_DEFAULT:
360 unwrap(cg)->setCodePICModel(Reloc::Default);
361 return false;
362 }
363 sLastErrorString = "Unknown PIC model";
364 return true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000365}
366
Bill Wendling152e4732012-03-31 10:44:20 +0000367void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000368 return unwrap(cg)->setCpu(cpu);
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000369}
370
Bill Wendling36cbf032012-03-30 10:29:38 +0000371void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000372 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcac8c8a2009-06-04 00:28:45 +0000373}
374
Bill Wendling152e4732012-03-31 10:44:20 +0000375void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling36cbf032012-03-30 10:29:38 +0000376 int nargs) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000377 // In here only for backwards compatibility. We use MC now.
Rafael Espindola00456462010-08-10 18:55:09 +0000378}
379
Bill Wendling36cbf032012-03-30 10:29:38 +0000380void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling152e4732012-03-31 10:44:20 +0000381 const char *symbol) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000382 unwrap(cg)->addMustPreserveSymbol(symbol);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000383}
384
Peter Collingbourne78240e02015-03-19 22:12:08 +0000385static void maybeParseOptions(lto_code_gen_t cg) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000386 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000387 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000388 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000389 parsedOptions = true;
390 }
Peter Collingbourne070843d2015-03-19 22:01:00 +0000391}
392
393bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000394 maybeParseOptions(cg);
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000395 return !unwrap(cg)->writeMergedModules(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000396}
397
Bill Wendling152e4732012-03-31 10:44:20 +0000398const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000399 maybeParseOptions(cg);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000400 LibLTOCodeGenerator *CG = unwrap(cg);
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000401 CG->NativeObjectFile =
402 CG->compile(DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000403 DisableLTOVectorization);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000404 if (!CG->NativeObjectFile)
405 return nullptr;
406 *length = CG->NativeObjectFile->getBufferSize();
407 return CG->NativeObjectFile->getBufferStart();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000408}
409
Manman Ren8121e1d2015-02-03 18:39:15 +0000410bool lto_codegen_optimize(lto_code_gen_t cg) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000411 maybeParseOptions(cg);
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000412 return !unwrap(cg)->optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000413 DisableLTOVectorization);
Manman Ren8121e1d2015-02-03 18:39:15 +0000414}
415
416const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000417 maybeParseOptions(cg);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000418 LibLTOCodeGenerator *CG = unwrap(cg);
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000419 CG->NativeObjectFile = CG->compileOptimized();
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000420 if (!CG->NativeObjectFile)
421 return nullptr;
422 *length = CG->NativeObjectFile->getBufferSize();
423 return CG->NativeObjectFile->getBufferStart();
Manman Ren8121e1d2015-02-03 18:39:15 +0000424}
425
Reid Klecknerddac1512013-10-24 22:26:04 +0000426bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000427 maybeParseOptions(cg);
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000428 return !unwrap(cg)->compile_to_file(
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000429 name, DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000430 DisableLTOVectorization);
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000431}
432
Bill Wendling152e4732012-03-31 10:44:20 +0000433void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000434 unwrap(cg)->setCodeGenDebugOptions(opt);
Duncan Sands31554ab2009-07-03 15:38:01 +0000435}
Rafael Espindolaa5ef4902015-02-03 19:25:53 +0000436
437unsigned int lto_api_version() { return LTO_API_VERSION; }
Manman Rence0a0662015-04-17 17:10:09 +0000438
439void lto_codegen_set_should_internalize(lto_code_gen_t cg,
440 bool ShouldInternalize) {
441 unwrap(cg)->setShouldInternalize(ShouldInternalize);
442}
Duncan P. N. Exon Smith5a490d02015-04-27 23:38:54 +0000443
444void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
445 lto_bool_t ShouldEmbedUselists) {
446 unwrap(cg)->setShouldEmbedUselists(ShouldEmbedUselists);
447}
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000448
449// ThinLTO API below
450
Mehdi Amini3ed41d62016-03-09 02:36:09 +0000451thinlto_code_gen_t thinlto_create_codegen(void) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000452 lto_initialize();
453 ThinLTOCodeGenerator *CodeGen = new ThinLTOCodeGenerator();
454 CodeGen->setTargetOptions(InitTargetOptionsFromCodeGenFlags());
455
456 return wrap(CodeGen);
457}
458
459void thinlto_codegen_dispose(thinlto_code_gen_t cg) { delete unwrap(cg); }
460
461void thinlto_codegen_add_module(thinlto_code_gen_t cg, const char *Identifier,
462 const char *Data, int Length) {
463 unwrap(cg)->addModule(Identifier, StringRef(Data, Length));
464}
465
466void thinlto_codegen_process(thinlto_code_gen_t cg) { unwrap(cg)->run(); }
467
468unsigned int thinlto_module_get_num_objects(thinlto_code_gen_t cg) {
469 return unwrap(cg)->getProducedBinaries().size();
470}
471LTOObjectBuffer thinlto_module_get_object(thinlto_code_gen_t cg,
472 unsigned int index) {
473 assert(index < unwrap(cg)->getProducedBinaries().size() && "Index overflow");
474 auto &MemBuffer = unwrap(cg)->getProducedBinaries()[index];
Mehdi Aminic286b9f2016-03-19 21:28:18 +0000475 return LTOObjectBuffer{MemBuffer->getBufferStart(),
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000476 MemBuffer->getBufferSize()};
477}
478
Mehdi Amini43b657b2016-04-01 06:47:02 +0000479void thinlto_codegen_disable_codegen(thinlto_code_gen_t cg,
480 lto_bool_t disable) {
481 unwrap(cg)->disableCodeGen(disable);
482}
483
484void thinlto_codegen_set_codegen_only(thinlto_code_gen_t cg,
485 lto_bool_t CodeGenOnly) {
486 unwrap(cg)->setCodeGenOnly(CodeGenOnly);
487}
488
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000489void thinlto_debug_options(const char *const *options, int number) {
490 // if options were requested, set them
491 if (number && options) {
492 std::vector<const char *> CodegenArgv(1, "libLTO");
493 for (auto Arg : ArrayRef<const char *>(options, number))
494 CodegenArgv.push_back(Arg);
495 cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data());
496 }
497}
498
Mehdi Amini43b657b2016-04-01 06:47:02 +0000499lto_bool_t lto_module_is_thinlto(lto_module_t mod) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000500 return unwrap(mod)->isThinLTO();
501}
502
503void thinlto_codegen_add_must_preserve_symbol(thinlto_code_gen_t cg,
504 const char *Name, int Length) {
505 unwrap(cg)->preserveSymbol(StringRef(Name, Length));
506}
507
508void thinlto_codegen_add_cross_referenced_symbol(thinlto_code_gen_t cg,
509 const char *Name, int Length) {
510 unwrap(cg)->crossReferenceSymbol(StringRef(Name, Length));
511}
512
513void thinlto_codegen_set_cpu(thinlto_code_gen_t cg, const char *cpu) {
514 return unwrap(cg)->setCpu(cpu);
515}
516
517void thinlto_codegen_set_cache_dir(thinlto_code_gen_t cg,
518 const char *cache_dir) {
519 return unwrap(cg)->setCacheDir(cache_dir);
520}
521
522void thinlto_codegen_set_cache_pruning_interval(thinlto_code_gen_t cg,
523 int interval) {
524 return unwrap(cg)->setCachePruningInterval(interval);
525}
526
527void thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg,
528 unsigned expiration) {
529 return unwrap(cg)->setCacheEntryExpiration(expiration);
530}
531
532void thinlto_codegen_set_final_cache_size_relative_to_available_space(
533 thinlto_code_gen_t cg, unsigned Percentage) {
534 return unwrap(cg)->setMaxCacheSizeRelativeToAvailableSpace(Percentage);
535}
536
537void thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg,
538 const char *save_temps_dir) {
539 return unwrap(cg)->setSaveTempsDir(save_temps_dir);
540}
541
542lto_bool_t thinlto_codegen_set_pic_model(thinlto_code_gen_t cg,
543 lto_codegen_model model) {
544 switch (model) {
545 case LTO_CODEGEN_PIC_MODEL_STATIC:
546 unwrap(cg)->setCodePICModel(Reloc::Static);
547 return false;
548 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
549 unwrap(cg)->setCodePICModel(Reloc::PIC_);
550 return false;
551 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
552 unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC);
553 return false;
554 case LTO_CODEGEN_PIC_MODEL_DEFAULT:
555 unwrap(cg)->setCodePICModel(Reloc::Default);
556 return false;
557 }
558 sLastErrorString = "Unknown PIC model";
559 return true;
560}