blob: 2a6585fc6069fd68b354f4f1d4720d7cb4036009 [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"
Mehdi Aminie75aa6f2016-07-11 23:10:18 +000017#include "llvm/Bitcode/ReaderWriter.h"
Rafael Espindola0b385c72013-09-30 16:39:19 +000018#include "llvm/CodeGen/CommandFlags.h"
Rafael Espindolaa7612b42015-12-04 16:14:31 +000019#include "llvm/IR/DiagnosticInfo.h"
20#include "llvm/IR/DiagnosticPrinter.h"
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +000021#include "llvm/IR/LLVMContext.h"
Peter Collingbourne5c732202016-07-14 21:21:16 +000022#include "llvm/LTO/legacy/LTOCodeGenerator.h"
23#include "llvm/LTO/legacy/LTOModule.h"
24#include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
Alp Tokerac903802014-07-04 00:58:41 +000025#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer50a20c02015-01-29 17:20:41 +000026#include "llvm/Support/Signals.h"
Rafael Espindola77c50d22014-06-19 19:11:22 +000027#include "llvm/Support/TargetSelect.h"
Reid Kleckner7c6b5e12016-01-29 00:03:34 +000028#include "llvm/Support/raw_ostream.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000029
Rafael Espindola0b385c72013-09-30 16:39:19 +000030// extra command-line flags needed for LTOCodeGenerator
Peter Collingbourne070843d2015-03-19 22:01:00 +000031static cl::opt<char>
32OptLevel("O",
33 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
34 "(default = '-O2')"),
35 cl::Prefix,
36 cl::ZeroOrMore,
37 cl::init('2'));
Rafael Espindola0b385c72013-09-30 16:39:19 +000038
39static cl::opt<bool>
40DisableInline("disable-inlining", cl::init(false),
41 cl::desc("Do not run the inliner pass"));
42
43static cl::opt<bool>
44DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
45 cl::desc("Do not run the GVN load PRE pass"));
Nick Kledzik07b4a622008-02-26 20:26:43 +000046
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +000047static cl::opt<bool>
48DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
49 cl::desc("Do not run loop or slp vectorization during LTO"));
50
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +000051#ifdef NDEBUG
52static bool VerifyByDefault = false;
53#else
54static bool VerifyByDefault = true;
55#endif
56
57static cl::opt<bool> DisableVerify(
58 "disable-llvm-verifier", cl::init(!VerifyByDefault),
59 cl::desc("Don't run the LLVM verifier during the optimization pipeline"));
60
Bill Wendling36cbf032012-03-30 10:29:38 +000061// Holds most recent error string.
62// *** Not thread safe ***
Nick Kledzik07b4a622008-02-26 20:26:43 +000063static std::string sLastErrorString;
64
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000065// Holds the initialization state of the LTO module.
66// *** Not thread safe ***
67static bool initialized = false;
68
Rafael Espindolaefa02d52013-10-02 14:36:23 +000069// Holds the command-line option parsing state of the LTO module.
70static bool parsedOptions = false;
71
Rafael Espindolaa7612b42015-12-04 16:14:31 +000072static LLVMContext *LTOContext = nullptr;
73
74static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
75 if (DI.getSeverity() != DS_Error) {
76 DiagnosticPrinterRawOStream DP(errs());
77 DI.print(DP);
78 errs() << '\n';
79 return;
80 }
81 sLastErrorString = "";
82 {
83 raw_string_ostream Stream(sLastErrorString);
84 DiagnosticPrinterRawOStream DP(Stream);
85 DI.print(DP);
86 }
Rafael Espindolaa7612b42015-12-04 16:14:31 +000087}
88
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000089// Initialize the configured targets if they have not been initialized.
90static void lto_initialize() {
91 if (!initialized) {
Michael J. Spencer50a20c02015-01-29 17:20:41 +000092#ifdef LLVM_ON_WIN32
93 // Dialog box on crash disabling doesn't work across DLL boundaries, so do
94 // it here.
95 llvm::sys::DisableSystemDialogsOnCrash();
96#endif
97
Rafael Espindola77c50d22014-06-19 19:11:22 +000098 InitializeAllTargetInfos();
99 InitializeAllTargets();
100 InitializeAllTargetMCs();
101 InitializeAllAsmParsers();
102 InitializeAllAsmPrinters();
103 InitializeAllDisassemblers();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000104
Mehdi Amini03b42e42016-04-14 21:59:01 +0000105 static LLVMContext Context;
106 LTOContext = &Context;
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000107 LTOContext->setDiagnosticHandler(diagnosticHandler, nullptr, true);
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000108 initialized = true;
109 }
110}
111
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000112namespace {
113
Yunzhong Gaoea7b3a22015-11-11 19:59:08 +0000114static void handleLibLTODiagnostic(lto_codegen_diagnostic_severity_t Severity,
115 const char *Msg, void *) {
116 sLastErrorString = Msg;
Yunzhong Gaoea7b3a22015-11-11 19:59:08 +0000117}
118
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000119// This derived class owns the native object file. This helps implement the
120// libLTO API semantics, which require that the code generator owns the object
121// file.
122struct LibLTOCodeGenerator : LTOCodeGenerator {
Duncan P. N. Exon Smith247e6362016-04-16 22:25:36 +0000123 LibLTOCodeGenerator() : LTOCodeGenerator(*LTOContext) { init(); }
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000124 LibLTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
Rafael Espindola7b8a24e2015-12-04 02:42:28 +0000125 : LTOCodeGenerator(*Context), OwnedContext(std::move(Context)) {
Duncan P. N. Exon Smith247e6362016-04-16 22:25:36 +0000126 init();
127 }
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000128
Steven Wub5104b52015-12-09 03:37:51 +0000129 // Reset the module first in case MergedModule is created in OwnedContext.
130 // Module must be destructed before its context gets destructed.
131 ~LibLTOCodeGenerator() { resetMergedModule(); }
132
Duncan P. N. Exon Smith247e6362016-04-16 22:25:36 +0000133 void init() { setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
134
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000135 std::unique_ptr<MemoryBuffer> NativeObjectFile;
Rafael Espindola7b8a24e2015-12-04 02:42:28 +0000136 std::unique_ptr<LLVMContext> OwnedContext;
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000137};
138
139}
140
141DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LibLTOCodeGenerator, lto_code_gen_t)
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000142DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThinLTOCodeGenerator, thinlto_code_gen_t)
Patrik Hagglund9be9d872014-05-05 12:24:08 +0000143DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000144
Tom Roederfd1bc602014-04-25 21:46:51 +0000145// Convert the subtarget features into a string to pass to LTOCodeGenerator.
146static void lto_add_attrs(lto_code_gen_t cg) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000147 LTOCodeGenerator *CG = unwrap(cg);
Tom Roederfd1bc602014-04-25 21:46:51 +0000148 if (MAttrs.size()) {
149 std::string attrs;
150 for (unsigned i = 0; i < MAttrs.size(); ++i) {
151 if (i > 0)
152 attrs.append(",");
153 attrs.append(MAttrs[i]);
154 }
155
Malcolm Parsons06ac79c2016-11-02 16:43:50 +0000156 CG->setAttr(attrs);
Tom Roederfd1bc602014-04-25 21:46:51 +0000157 }
Peter Collingbourne070843d2015-03-19 22:01:00 +0000158
159 if (OptLevel < '0' || OptLevel > '3')
160 report_fatal_error("Optimization level must be between 0 and 3");
161 CG->setOptLevel(OptLevel - '0');
Tom Roederfd1bc602014-04-25 21:46:51 +0000162}
163
Bill Wendling36cbf032012-03-30 10:29:38 +0000164extern const char* lto_get_version() {
165 return LTOCodeGenerator::getVersionString();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000166}
167
Bill Wendling36cbf032012-03-30 10:29:38 +0000168const char* lto_get_error_message() {
169 return sLastErrorString.c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000170}
171
Reid Klecknerddac1512013-10-24 22:26:04 +0000172bool lto_module_is_object_file(const char* path) {
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000173 return LTOModule::isBitcodeFile(StringRef(path));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000174}
175
Reid Klecknerddac1512013-10-24 22:26:04 +0000176bool lto_module_is_object_file_for_target(const char* path,
Bill Wendling36cbf032012-03-30 10:29:38 +0000177 const char* target_triplet_prefix) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000178 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
179 if (!Buffer)
Alp Tokerac903802014-07-04 00:58:41 +0000180 return false;
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000181 return LTOModule::isBitcodeForTarget(Buffer->get(),
182 StringRef(target_triplet_prefix));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000183}
184
Mehdi Aminie75aa6f2016-07-11 23:10:18 +0000185bool lto_module_has_objc_category(const void *mem, size_t length) {
186 std::unique_ptr<MemoryBuffer> Buffer(LTOModule::makeBuffer(mem, length));
187 if (!Buffer)
188 return false;
189 LLVMContext Ctx;
190 return llvm::isBitcodeContainingObjCCategory(*Buffer, Ctx);
191}
192
Reid Klecknerddac1512013-10-24 22:26:04 +0000193bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
Bill Wendling36cbf032012-03-30 10:29:38 +0000194 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000195}
196
Reid Klecknerddac1512013-10-24 22:26:04 +0000197bool
Bill Wendling36cbf032012-03-30 10:29:38 +0000198lto_module_is_object_file_in_memory_for_target(const void* mem,
199 size_t length,
200 const char* target_triplet_prefix) {
Alp Tokerac903802014-07-04 00:58:41 +0000201 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
202 if (!buffer)
203 return false;
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000204 return LTOModule::isBitcodeForTarget(buffer.get(),
205 StringRef(target_triplet_prefix));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000206}
207
Bill Wendling36cbf032012-03-30 10:29:38 +0000208lto_module_t lto_module_create(const char* path) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000209 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000210 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000211 ErrorOr<std::unique_ptr<LTOModule>> M =
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000212 LTOModule::createFromFile(*LTOContext, StringRef(path), Options);
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000213 if (!M)
214 return nullptr;
215 return wrap(M->release());
Nick Kledzik07b4a622008-02-26 20:26:43 +0000216}
217
Bill Wendling36cbf032012-03-30 10:29:38 +0000218lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000219 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000220 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000221 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromOpenFile(
222 *LTOContext, fd, StringRef(path), size, Options);
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000223 if (!M)
224 return nullptr;
225 return wrap(M->release());
Rafael Espindola56e41f72011-02-08 22:40:47 +0000226}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000227
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000228lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
229 size_t file_size,
230 size_t map_size,
Bill Wendling36cbf032012-03-30 10:29:38 +0000231 off_t offset) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000232 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000233 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000234 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromOpenFileSlice(
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000235 *LTOContext, fd, StringRef(path), map_size, offset, Options);
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000236 if (!M)
237 return nullptr;
238 return wrap(M->release());
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000239}
240
Bill Wendling36cbf032012-03-30 10:29:38 +0000241lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000242 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000243 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000244 ErrorOr<std::unique_ptr<LTOModule>> M =
245 LTOModule::createFromBuffer(*LTOContext, mem, length, Options);
246 if (!M)
247 return nullptr;
248 return wrap(M->release());
Nick Kledzik07b4a622008-02-26 20:26:43 +0000249}
250
Manman Ren03456a12014-02-10 23:26:14 +0000251lto_module_t lto_module_create_from_memory_with_path(const void* mem,
252 size_t length,
253 const char *path) {
254 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000255 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000256 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromBuffer(
257 *LTOContext, mem, length, Options, StringRef(path));
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000258 if (!M)
259 return nullptr;
260 return wrap(M->release());
Manman Ren03456a12014-02-10 23:26:14 +0000261}
262
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000263lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
264 const char *path) {
265 lto_initialize();
266 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Petr Pavlu7ad9ec92016-03-01 13:13:49 +0000267
268 // Create a local context. Ownership will be transfered to LTOModule.
269 std::unique_ptr<LLVMContext> Context = llvm::make_unique<LLVMContext>();
270 Context->setDiagnosticHandler(diagnosticHandler, nullptr, true);
271
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000272 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createInLocalContext(
273 std::move(Context), mem, length, Options, StringRef(path));
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000274 if (!M)
275 return nullptr;
276 return wrap(M->release());
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000277}
278
279lto_module_t lto_module_create_in_codegen_context(const void *mem,
280 size_t length,
281 const char *path,
282 lto_code_gen_t cg) {
283 lto_initialize();
284 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Petr Pavlu7ad9ec92016-03-01 13:13:49 +0000285 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromBuffer(
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000286 unwrap(cg)->getContext(), mem, length, Options, StringRef(path));
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000287 return wrap(M->release());
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000288}
289
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000290void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000291
Bill Wendling36cbf032012-03-30 10:29:38 +0000292const char* lto_module_get_target_triple(lto_module_t mod) {
Rafael Espindolad749fb52014-07-04 14:19:41 +0000293 return unwrap(mod)->getTargetTriple().c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000294}
295
Bill Wendling36cbf032012-03-30 10:29:38 +0000296void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000297 return unwrap(mod)->setTargetTriple(StringRef(triple));
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000298}
299
Bill Wendling36cbf032012-03-30 10:29:38 +0000300unsigned int lto_module_get_num_symbols(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000301 return unwrap(mod)->getSymbolCount();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000302}
303
Bill Wendling36cbf032012-03-30 10:29:38 +0000304const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000305 return unwrap(mod)->getSymbolName(index).data();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000306}
307
Bill Wendling36cbf032012-03-30 10:29:38 +0000308lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
309 unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000310 return unwrap(mod)->getSymbolAttributes(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000311}
312
Peter Collingbourne5d7dffb2015-06-29 23:09:12 +0000313const char* lto_module_get_linkeropts(lto_module_t mod) {
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000314 return unwrap(mod)->getLinkerOpts().data();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000315}
316
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000317void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
318 lto_diagnostic_handler_t diag_handler,
319 void *ctxt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000320 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000321}
322
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000323static lto_code_gen_t createCodeGen(bool InLocalContext) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000324 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000325
Eli Benderskyf0f21002014-02-19 17:09:35 +0000326 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000327
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000328 LibLTOCodeGenerator *CodeGen =
329 InLocalContext ? new LibLTOCodeGenerator(make_unique<LLVMContext>())
330 : new LibLTOCodeGenerator();
331 CodeGen->setTargetOptions(Options);
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000332 return wrap(CodeGen);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000333}
334
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000335lto_code_gen_t lto_codegen_create(void) {
336 return createCodeGen(/* InLocalContext */ false);
337}
338
339lto_code_gen_t lto_codegen_create_in_local_context(void) {
340 return createCodeGen(/* InLocalContext */ true);
341}
342
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000343void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000344
Reid Klecknerddac1512013-10-24 22:26:04 +0000345bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000346 return !unwrap(cg)->addModule(unwrap(mod));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000347}
348
Manman Ren6487ce92015-02-24 00:45:56 +0000349void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {
Peter Collingbourne9c8909d2015-08-24 22:22:53 +0000350 unwrap(cg)->setModule(std::unique_ptr<LTOModule>(unwrap(mod)));
Manman Ren6487ce92015-02-24 00:45:56 +0000351}
352
Reid Klecknerddac1512013-10-24 22:26:04 +0000353bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000354 unwrap(cg)->setDebugInfo(debug);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000355 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000356}
357
Reid Klecknerddac1512013-10-24 22:26:04 +0000358bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Peter Collingbourne44ee84e2015-08-21 22:57:17 +0000359 switch (model) {
360 case LTO_CODEGEN_PIC_MODEL_STATIC:
361 unwrap(cg)->setCodePICModel(Reloc::Static);
362 return false;
363 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
364 unwrap(cg)->setCodePICModel(Reloc::PIC_);
365 return false;
366 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
367 unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC);
368 return false;
369 case LTO_CODEGEN_PIC_MODEL_DEFAULT:
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000370 unwrap(cg)->setCodePICModel(None);
Peter Collingbourne44ee84e2015-08-21 22:57:17 +0000371 return false;
372 }
373 sLastErrorString = "Unknown PIC model";
374 return true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000375}
376
Bill Wendling152e4732012-03-31 10:44:20 +0000377void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000378 return unwrap(cg)->setCpu(cpu);
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000379}
380
Bill Wendling36cbf032012-03-30 10:29:38 +0000381void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000382 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcac8c8a2009-06-04 00:28:45 +0000383}
384
Bill Wendling152e4732012-03-31 10:44:20 +0000385void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling36cbf032012-03-30 10:29:38 +0000386 int nargs) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000387 // In here only for backwards compatibility. We use MC now.
Rafael Espindola00456462010-08-10 18:55:09 +0000388}
389
Bill Wendling36cbf032012-03-30 10:29:38 +0000390void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling152e4732012-03-31 10:44:20 +0000391 const char *symbol) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000392 unwrap(cg)->addMustPreserveSymbol(symbol);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000393}
394
Peter Collingbourne78240e02015-03-19 22:12:08 +0000395static void maybeParseOptions(lto_code_gen_t cg) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000396 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000397 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000398 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000399 parsedOptions = true;
400 }
Peter Collingbourne070843d2015-03-19 22:01:00 +0000401}
402
403bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000404 maybeParseOptions(cg);
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000405 return !unwrap(cg)->writeMergedModules(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000406}
407
Bill Wendling152e4732012-03-31 10:44:20 +0000408const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000409 maybeParseOptions(cg);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000410 LibLTOCodeGenerator *CG = unwrap(cg);
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000411 CG->NativeObjectFile =
412 CG->compile(DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000413 DisableLTOVectorization);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000414 if (!CG->NativeObjectFile)
415 return nullptr;
416 *length = CG->NativeObjectFile->getBufferSize();
417 return CG->NativeObjectFile->getBufferStart();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000418}
419
Manman Ren8121e1d2015-02-03 18:39:15 +0000420bool lto_codegen_optimize(lto_code_gen_t cg) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000421 maybeParseOptions(cg);
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000422 return !unwrap(cg)->optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000423 DisableLTOVectorization);
Manman Ren8121e1d2015-02-03 18:39:15 +0000424}
425
426const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000427 maybeParseOptions(cg);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000428 LibLTOCodeGenerator *CG = unwrap(cg);
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000429 CG->NativeObjectFile = CG->compileOptimized();
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000430 if (!CG->NativeObjectFile)
431 return nullptr;
432 *length = CG->NativeObjectFile->getBufferSize();
433 return CG->NativeObjectFile->getBufferStart();
Manman Ren8121e1d2015-02-03 18:39:15 +0000434}
435
Reid Klecknerddac1512013-10-24 22:26:04 +0000436bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000437 maybeParseOptions(cg);
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000438 return !unwrap(cg)->compile_to_file(
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000439 name, DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000440 DisableLTOVectorization);
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000441}
442
Bill Wendling152e4732012-03-31 10:44:20 +0000443void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000444 unwrap(cg)->setCodeGenDebugOptions(opt);
Duncan Sands31554ab2009-07-03 15:38:01 +0000445}
Rafael Espindolaa5ef4902015-02-03 19:25:53 +0000446
447unsigned int lto_api_version() { return LTO_API_VERSION; }
Manman Rence0a0662015-04-17 17:10:09 +0000448
449void lto_codegen_set_should_internalize(lto_code_gen_t cg,
450 bool ShouldInternalize) {
451 unwrap(cg)->setShouldInternalize(ShouldInternalize);
452}
Duncan P. N. Exon Smith5a490d02015-04-27 23:38:54 +0000453
454void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
455 lto_bool_t ShouldEmbedUselists) {
456 unwrap(cg)->setShouldEmbedUselists(ShouldEmbedUselists);
457}
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000458
459// ThinLTO API below
460
Mehdi Amini3ed41d62016-03-09 02:36:09 +0000461thinlto_code_gen_t thinlto_create_codegen(void) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000462 lto_initialize();
463 ThinLTOCodeGenerator *CodeGen = new ThinLTOCodeGenerator();
464 CodeGen->setTargetOptions(InitTargetOptionsFromCodeGenFlags());
465
466 return wrap(CodeGen);
467}
468
469void thinlto_codegen_dispose(thinlto_code_gen_t cg) { delete unwrap(cg); }
470
471void thinlto_codegen_add_module(thinlto_code_gen_t cg, const char *Identifier,
472 const char *Data, int Length) {
473 unwrap(cg)->addModule(Identifier, StringRef(Data, Length));
474}
475
476void thinlto_codegen_process(thinlto_code_gen_t cg) { unwrap(cg)->run(); }
477
478unsigned int thinlto_module_get_num_objects(thinlto_code_gen_t cg) {
479 return unwrap(cg)->getProducedBinaries().size();
480}
481LTOObjectBuffer thinlto_module_get_object(thinlto_code_gen_t cg,
482 unsigned int index) {
483 assert(index < unwrap(cg)->getProducedBinaries().size() && "Index overflow");
484 auto &MemBuffer = unwrap(cg)->getProducedBinaries()[index];
Mehdi Aminic286b9f2016-03-19 21:28:18 +0000485 return LTOObjectBuffer{MemBuffer->getBufferStart(),
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000486 MemBuffer->getBufferSize()};
487}
488
Mehdi Amini43b657b2016-04-01 06:47:02 +0000489void thinlto_codegen_disable_codegen(thinlto_code_gen_t cg,
490 lto_bool_t disable) {
491 unwrap(cg)->disableCodeGen(disable);
492}
493
494void thinlto_codegen_set_codegen_only(thinlto_code_gen_t cg,
495 lto_bool_t CodeGenOnly) {
496 unwrap(cg)->setCodeGenOnly(CodeGenOnly);
497}
498
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000499void thinlto_debug_options(const char *const *options, int number) {
500 // if options were requested, set them
501 if (number && options) {
502 std::vector<const char *> CodegenArgv(1, "libLTO");
503 for (auto Arg : ArrayRef<const char *>(options, number))
504 CodegenArgv.push_back(Arg);
505 cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data());
506 }
507}
508
Mehdi Amini43b657b2016-04-01 06:47:02 +0000509lto_bool_t lto_module_is_thinlto(lto_module_t mod) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000510 return unwrap(mod)->isThinLTO();
511}
512
513void thinlto_codegen_add_must_preserve_symbol(thinlto_code_gen_t cg,
514 const char *Name, int Length) {
515 unwrap(cg)->preserveSymbol(StringRef(Name, Length));
516}
517
518void thinlto_codegen_add_cross_referenced_symbol(thinlto_code_gen_t cg,
519 const char *Name, int Length) {
520 unwrap(cg)->crossReferenceSymbol(StringRef(Name, Length));
521}
522
523void thinlto_codegen_set_cpu(thinlto_code_gen_t cg, const char *cpu) {
524 return unwrap(cg)->setCpu(cpu);
525}
526
527void thinlto_codegen_set_cache_dir(thinlto_code_gen_t cg,
528 const char *cache_dir) {
529 return unwrap(cg)->setCacheDir(cache_dir);
530}
531
532void thinlto_codegen_set_cache_pruning_interval(thinlto_code_gen_t cg,
533 int interval) {
534 return unwrap(cg)->setCachePruningInterval(interval);
535}
536
537void thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg,
538 unsigned expiration) {
539 return unwrap(cg)->setCacheEntryExpiration(expiration);
540}
541
542void thinlto_codegen_set_final_cache_size_relative_to_available_space(
543 thinlto_code_gen_t cg, unsigned Percentage) {
544 return unwrap(cg)->setMaxCacheSizeRelativeToAvailableSpace(Percentage);
545}
546
547void thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg,
548 const char *save_temps_dir) {
549 return unwrap(cg)->setSaveTempsDir(save_temps_dir);
550}
551
552lto_bool_t thinlto_codegen_set_pic_model(thinlto_code_gen_t cg,
553 lto_codegen_model model) {
554 switch (model) {
555 case LTO_CODEGEN_PIC_MODEL_STATIC:
556 unwrap(cg)->setCodePICModel(Reloc::Static);
557 return false;
558 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
559 unwrap(cg)->setCodePICModel(Reloc::PIC_);
560 return false;
561 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
562 unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC);
563 return false;
564 case LTO_CODEGEN_PIC_MODEL_DEFAULT:
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000565 unwrap(cg)->setCodePICModel(None);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000566 return false;
567 }
568 sLastErrorString = "Unknown PIC model";
569 return true;
570}