blob: e28c7aaf526f7c267e46aeba3efe8ade27d64c0a [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"
Teresa Johnsonad176792016-11-11 05:34:58 +000017#include "llvm/Bitcode/BitcodeReader.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
Mehdi Aminib5a46c12017-03-28 18:55:44 +000047static cl::opt<bool> DisableLTOVectorization(
48 "disable-lto-vectorization", cl::init(false),
49 cl::desc("Do not run loop or slp vectorization during LTO"));
50
51static cl::opt<bool> EnableFreestanding(
52 "lto-freestanding", cl::init(false),
53 cl::desc("Enable Freestanding (disable builtins / TLI) during LTO"));
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +000054
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +000055#ifdef NDEBUG
56static bool VerifyByDefault = false;
57#else
58static bool VerifyByDefault = true;
59#endif
60
61static cl::opt<bool> DisableVerify(
62 "disable-llvm-verifier", cl::init(!VerifyByDefault),
63 cl::desc("Don't run the LLVM verifier during the optimization pipeline"));
64
Bill Wendling36cbf032012-03-30 10:29:38 +000065// Holds most recent error string.
66// *** Not thread safe ***
Nick Kledzik07b4a622008-02-26 20:26:43 +000067static std::string sLastErrorString;
68
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000069// Holds the initialization state of the LTO module.
70// *** Not thread safe ***
71static bool initialized = false;
72
Rafael Espindolaefa02d52013-10-02 14:36:23 +000073// Holds the command-line option parsing state of the LTO module.
74static bool parsedOptions = false;
75
Rafael Espindolaa7612b42015-12-04 16:14:31 +000076static LLVMContext *LTOContext = nullptr;
77
78static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
79 if (DI.getSeverity() != DS_Error) {
80 DiagnosticPrinterRawOStream DP(errs());
81 DI.print(DP);
82 errs() << '\n';
83 return;
84 }
85 sLastErrorString = "";
86 {
87 raw_string_ostream Stream(sLastErrorString);
88 DiagnosticPrinterRawOStream DP(Stream);
89 DI.print(DP);
90 }
Rafael Espindolaa7612b42015-12-04 16:14:31 +000091}
92
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000093// Initialize the configured targets if they have not been initialized.
94static void lto_initialize() {
95 if (!initialized) {
Michael J. Spencer50a20c02015-01-29 17:20:41 +000096#ifdef LLVM_ON_WIN32
97 // Dialog box on crash disabling doesn't work across DLL boundaries, so do
98 // it here.
99 llvm::sys::DisableSystemDialogsOnCrash();
100#endif
101
Rafael Espindola77c50d22014-06-19 19:11:22 +0000102 InitializeAllTargetInfos();
103 InitializeAllTargets();
104 InitializeAllTargetMCs();
105 InitializeAllAsmParsers();
106 InitializeAllAsmPrinters();
107 InitializeAllDisassemblers();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000108
Mehdi Amini03b42e42016-04-14 21:59:01 +0000109 static LLVMContext Context;
110 LTOContext = &Context;
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000111 LTOContext->setDiagnosticHandler(diagnosticHandler, nullptr, true);
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000112 initialized = true;
113 }
114}
115
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000116namespace {
117
Yunzhong Gaoea7b3a22015-11-11 19:59:08 +0000118static void handleLibLTODiagnostic(lto_codegen_diagnostic_severity_t Severity,
119 const char *Msg, void *) {
120 sLastErrorString = Msg;
Yunzhong Gaoea7b3a22015-11-11 19:59:08 +0000121}
122
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000123// This derived class owns the native object file. This helps implement the
124// libLTO API semantics, which require that the code generator owns the object
125// file.
126struct LibLTOCodeGenerator : LTOCodeGenerator {
Duncan P. N. Exon Smith247e6362016-04-16 22:25:36 +0000127 LibLTOCodeGenerator() : LTOCodeGenerator(*LTOContext) { init(); }
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000128 LibLTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
Rafael Espindola7b8a24e2015-12-04 02:42:28 +0000129 : LTOCodeGenerator(*Context), OwnedContext(std::move(Context)) {
Duncan P. N. Exon Smith247e6362016-04-16 22:25:36 +0000130 init();
131 }
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000132
Steven Wub5104b52015-12-09 03:37:51 +0000133 // Reset the module first in case MergedModule is created in OwnedContext.
134 // Module must be destructed before its context gets destructed.
135 ~LibLTOCodeGenerator() { resetMergedModule(); }
136
Duncan P. N. Exon Smith247e6362016-04-16 22:25:36 +0000137 void init() { setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
138
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000139 std::unique_ptr<MemoryBuffer> NativeObjectFile;
Rafael Espindola7b8a24e2015-12-04 02:42:28 +0000140 std::unique_ptr<LLVMContext> OwnedContext;
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000141};
142
143}
144
145DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LibLTOCodeGenerator, lto_code_gen_t)
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000146DEFINE_SIMPLE_CONVERSION_FUNCTIONS(ThinLTOCodeGenerator, thinlto_code_gen_t)
Patrik Hagglund9be9d872014-05-05 12:24:08 +0000147DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000148
Tom Roederfd1bc602014-04-25 21:46:51 +0000149// Convert the subtarget features into a string to pass to LTOCodeGenerator.
150static void lto_add_attrs(lto_code_gen_t cg) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000151 LTOCodeGenerator *CG = unwrap(cg);
Tom Roederfd1bc602014-04-25 21:46:51 +0000152 if (MAttrs.size()) {
153 std::string attrs;
154 for (unsigned i = 0; i < MAttrs.size(); ++i) {
155 if (i > 0)
156 attrs.append(",");
157 attrs.append(MAttrs[i]);
158 }
159
Malcolm Parsons06ac79c2016-11-02 16:43:50 +0000160 CG->setAttr(attrs);
Tom Roederfd1bc602014-04-25 21:46:51 +0000161 }
Peter Collingbourne070843d2015-03-19 22:01:00 +0000162
163 if (OptLevel < '0' || OptLevel > '3')
164 report_fatal_error("Optimization level must be between 0 and 3");
165 CG->setOptLevel(OptLevel - '0');
Mehdi Aminib5a46c12017-03-28 18:55:44 +0000166 CG->setFreestanding(EnableFreestanding);
Tom Roederfd1bc602014-04-25 21:46:51 +0000167}
168
Bill Wendling36cbf032012-03-30 10:29:38 +0000169extern const char* lto_get_version() {
170 return LTOCodeGenerator::getVersionString();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000171}
172
Bill Wendling36cbf032012-03-30 10:29:38 +0000173const char* lto_get_error_message() {
174 return sLastErrorString.c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000175}
176
Reid Klecknerddac1512013-10-24 22:26:04 +0000177bool lto_module_is_object_file(const char* path) {
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000178 return LTOModule::isBitcodeFile(StringRef(path));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000179}
180
Reid Klecknerddac1512013-10-24 22:26:04 +0000181bool lto_module_is_object_file_for_target(const char* path,
Bill Wendling36cbf032012-03-30 10:29:38 +0000182 const char* target_triplet_prefix) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000183 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
184 if (!Buffer)
Alp Tokerac903802014-07-04 00:58:41 +0000185 return false;
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000186 return LTOModule::isBitcodeForTarget(Buffer->get(),
187 StringRef(target_triplet_prefix));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000188}
189
Mehdi Aminie75aa6f2016-07-11 23:10:18 +0000190bool lto_module_has_objc_category(const void *mem, size_t length) {
191 std::unique_ptr<MemoryBuffer> Buffer(LTOModule::makeBuffer(mem, length));
192 if (!Buffer)
193 return false;
194 LLVMContext Ctx;
Peter Collingbournecd513a42016-11-11 19:50:24 +0000195 ErrorOr<bool> Result = expectedToErrorOrAndEmitErrors(
196 Ctx, llvm::isBitcodeContainingObjCCategory(*Buffer));
197 return Result && *Result;
Mehdi Aminie75aa6f2016-07-11 23:10:18 +0000198}
199
Reid Klecknerddac1512013-10-24 22:26:04 +0000200bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
Bill Wendling36cbf032012-03-30 10:29:38 +0000201 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000202}
203
Reid Klecknerddac1512013-10-24 22:26:04 +0000204bool
Bill Wendling36cbf032012-03-30 10:29:38 +0000205lto_module_is_object_file_in_memory_for_target(const void* mem,
206 size_t length,
207 const char* target_triplet_prefix) {
Alp Tokerac903802014-07-04 00:58:41 +0000208 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
209 if (!buffer)
210 return false;
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000211 return LTOModule::isBitcodeForTarget(buffer.get(),
212 StringRef(target_triplet_prefix));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000213}
214
Bill Wendling36cbf032012-03-30 10:29:38 +0000215lto_module_t lto_module_create(const char* path) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000216 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000217 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000218 ErrorOr<std::unique_ptr<LTOModule>> M =
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000219 LTOModule::createFromFile(*LTOContext, StringRef(path), Options);
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000220 if (!M)
221 return nullptr;
222 return wrap(M->release());
Nick Kledzik07b4a622008-02-26 20:26:43 +0000223}
224
Bill Wendling36cbf032012-03-30 10:29:38 +0000225lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000226 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000227 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000228 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromOpenFile(
229 *LTOContext, fd, StringRef(path), size, Options);
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000230 if (!M)
231 return nullptr;
232 return wrap(M->release());
Rafael Espindola56e41f72011-02-08 22:40:47 +0000233}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000234
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000235lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
236 size_t file_size,
237 size_t map_size,
Bill Wendling36cbf032012-03-30 10:29:38 +0000238 off_t offset) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000239 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000240 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000241 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromOpenFileSlice(
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000242 *LTOContext, fd, StringRef(path), map_size, offset, Options);
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000243 if (!M)
244 return nullptr;
245 return wrap(M->release());
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000246}
247
Bill Wendling36cbf032012-03-30 10:29:38 +0000248lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000249 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000250 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000251 ErrorOr<std::unique_ptr<LTOModule>> M =
252 LTOModule::createFromBuffer(*LTOContext, mem, length, Options);
253 if (!M)
254 return nullptr;
255 return wrap(M->release());
Nick Kledzik07b4a622008-02-26 20:26:43 +0000256}
257
Manman Ren03456a12014-02-10 23:26:14 +0000258lto_module_t lto_module_create_from_memory_with_path(const void* mem,
259 size_t length,
260 const char *path) {
261 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000262 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000263 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromBuffer(
264 *LTOContext, mem, length, Options, StringRef(path));
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000265 if (!M)
266 return nullptr;
267 return wrap(M->release());
Manman Ren03456a12014-02-10 23:26:14 +0000268}
269
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000270lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
271 const char *path) {
272 lto_initialize();
273 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Petr Pavlu7ad9ec92016-03-01 13:13:49 +0000274
275 // Create a local context. Ownership will be transfered to LTOModule.
276 std::unique_ptr<LLVMContext> Context = llvm::make_unique<LLVMContext>();
277 Context->setDiagnosticHandler(diagnosticHandler, nullptr, true);
278
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000279 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createInLocalContext(
280 std::move(Context), mem, length, Options, StringRef(path));
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000281 if (!M)
282 return nullptr;
283 return wrap(M->release());
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000284}
285
286lto_module_t lto_module_create_in_codegen_context(const void *mem,
287 size_t length,
288 const char *path,
289 lto_code_gen_t cg) {
290 lto_initialize();
291 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Petr Pavlu7ad9ec92016-03-01 13:13:49 +0000292 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromBuffer(
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000293 unwrap(cg)->getContext(), mem, length, Options, StringRef(path));
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000294 return wrap(M->release());
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000295}
296
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000297void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000298
Bill Wendling36cbf032012-03-30 10:29:38 +0000299const char* lto_module_get_target_triple(lto_module_t mod) {
Rafael Espindolad749fb52014-07-04 14:19:41 +0000300 return unwrap(mod)->getTargetTriple().c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000301}
302
Bill Wendling36cbf032012-03-30 10:29:38 +0000303void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000304 return unwrap(mod)->setTargetTriple(StringRef(triple));
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000305}
306
Bill Wendling36cbf032012-03-30 10:29:38 +0000307unsigned int lto_module_get_num_symbols(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000308 return unwrap(mod)->getSymbolCount();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000309}
310
Bill Wendling36cbf032012-03-30 10:29:38 +0000311const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000312 return unwrap(mod)->getSymbolName(index).data();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000313}
314
Bill Wendling36cbf032012-03-30 10:29:38 +0000315lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
316 unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000317 return unwrap(mod)->getSymbolAttributes(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000318}
319
Peter Collingbourne5d7dffb2015-06-29 23:09:12 +0000320const char* lto_module_get_linkeropts(lto_module_t mod) {
Mehdi Aminidc5a5072016-10-07 19:05:14 +0000321 return unwrap(mod)->getLinkerOpts().data();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000322}
323
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000324void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
325 lto_diagnostic_handler_t diag_handler,
326 void *ctxt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000327 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000328}
329
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000330static lto_code_gen_t createCodeGen(bool InLocalContext) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000331 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000332
Eli Benderskyf0f21002014-02-19 17:09:35 +0000333 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000334
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000335 LibLTOCodeGenerator *CodeGen =
336 InLocalContext ? new LibLTOCodeGenerator(make_unique<LLVMContext>())
337 : new LibLTOCodeGenerator();
338 CodeGen->setTargetOptions(Options);
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000339 return wrap(CodeGen);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000340}
341
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000342lto_code_gen_t lto_codegen_create(void) {
343 return createCodeGen(/* InLocalContext */ false);
344}
345
346lto_code_gen_t lto_codegen_create_in_local_context(void) {
347 return createCodeGen(/* InLocalContext */ true);
348}
349
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000350void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000351
Reid Klecknerddac1512013-10-24 22:26:04 +0000352bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000353 return !unwrap(cg)->addModule(unwrap(mod));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000354}
355
Manman Ren6487ce92015-02-24 00:45:56 +0000356void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {
Peter Collingbourne9c8909d2015-08-24 22:22:53 +0000357 unwrap(cg)->setModule(std::unique_ptr<LTOModule>(unwrap(mod)));
Manman Ren6487ce92015-02-24 00:45:56 +0000358}
359
Reid Klecknerddac1512013-10-24 22:26:04 +0000360bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000361 unwrap(cg)->setDebugInfo(debug);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000362 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000363}
364
Reid Klecknerddac1512013-10-24 22:26:04 +0000365bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Peter Collingbourne44ee84e2015-08-21 22:57:17 +0000366 switch (model) {
367 case LTO_CODEGEN_PIC_MODEL_STATIC:
368 unwrap(cg)->setCodePICModel(Reloc::Static);
369 return false;
370 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
371 unwrap(cg)->setCodePICModel(Reloc::PIC_);
372 return false;
373 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
374 unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC);
375 return false;
376 case LTO_CODEGEN_PIC_MODEL_DEFAULT:
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000377 unwrap(cg)->setCodePICModel(None);
Peter Collingbourne44ee84e2015-08-21 22:57:17 +0000378 return false;
379 }
380 sLastErrorString = "Unknown PIC model";
381 return true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000382}
383
Bill Wendling152e4732012-03-31 10:44:20 +0000384void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000385 return unwrap(cg)->setCpu(cpu);
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000386}
387
Bill Wendling36cbf032012-03-30 10:29:38 +0000388void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000389 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcac8c8a2009-06-04 00:28:45 +0000390}
391
Bill Wendling152e4732012-03-31 10:44:20 +0000392void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling36cbf032012-03-30 10:29:38 +0000393 int nargs) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000394 // In here only for backwards compatibility. We use MC now.
Rafael Espindola00456462010-08-10 18:55:09 +0000395}
396
Bill Wendling36cbf032012-03-30 10:29:38 +0000397void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling152e4732012-03-31 10:44:20 +0000398 const char *symbol) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000399 unwrap(cg)->addMustPreserveSymbol(symbol);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000400}
401
Peter Collingbourne78240e02015-03-19 22:12:08 +0000402static void maybeParseOptions(lto_code_gen_t cg) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000403 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000404 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000405 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000406 parsedOptions = true;
407 }
Peter Collingbourne070843d2015-03-19 22:01:00 +0000408}
409
410bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000411 maybeParseOptions(cg);
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000412 return !unwrap(cg)->writeMergedModules(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000413}
414
Bill Wendling152e4732012-03-31 10:44:20 +0000415const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000416 maybeParseOptions(cg);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000417 LibLTOCodeGenerator *CG = unwrap(cg);
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000418 CG->NativeObjectFile =
419 CG->compile(DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000420 DisableLTOVectorization);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000421 if (!CG->NativeObjectFile)
422 return nullptr;
423 *length = CG->NativeObjectFile->getBufferSize();
424 return CG->NativeObjectFile->getBufferStart();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000425}
426
Manman Ren8121e1d2015-02-03 18:39:15 +0000427bool lto_codegen_optimize(lto_code_gen_t cg) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000428 maybeParseOptions(cg);
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000429 return !unwrap(cg)->optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000430 DisableLTOVectorization);
Manman Ren8121e1d2015-02-03 18:39:15 +0000431}
432
433const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000434 maybeParseOptions(cg);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000435 LibLTOCodeGenerator *CG = unwrap(cg);
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000436 CG->NativeObjectFile = CG->compileOptimized();
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000437 if (!CG->NativeObjectFile)
438 return nullptr;
439 *length = CG->NativeObjectFile->getBufferSize();
440 return CG->NativeObjectFile->getBufferStart();
Manman Ren8121e1d2015-02-03 18:39:15 +0000441}
442
Reid Klecknerddac1512013-10-24 22:26:04 +0000443bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000444 maybeParseOptions(cg);
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000445 return !unwrap(cg)->compile_to_file(
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000446 name, DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000447 DisableLTOVectorization);
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000448}
449
Bill Wendling152e4732012-03-31 10:44:20 +0000450void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000451 unwrap(cg)->setCodeGenDebugOptions(opt);
Duncan Sands31554ab2009-07-03 15:38:01 +0000452}
Rafael Espindolaa5ef4902015-02-03 19:25:53 +0000453
454unsigned int lto_api_version() { return LTO_API_VERSION; }
Manman Rence0a0662015-04-17 17:10:09 +0000455
456void lto_codegen_set_should_internalize(lto_code_gen_t cg,
457 bool ShouldInternalize) {
458 unwrap(cg)->setShouldInternalize(ShouldInternalize);
459}
Duncan P. N. Exon Smith5a490d02015-04-27 23:38:54 +0000460
461void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
462 lto_bool_t ShouldEmbedUselists) {
463 unwrap(cg)->setShouldEmbedUselists(ShouldEmbedUselists);
464}
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000465
466// ThinLTO API below
467
Mehdi Amini3ed41d62016-03-09 02:36:09 +0000468thinlto_code_gen_t thinlto_create_codegen(void) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000469 lto_initialize();
470 ThinLTOCodeGenerator *CodeGen = new ThinLTOCodeGenerator();
471 CodeGen->setTargetOptions(InitTargetOptionsFromCodeGenFlags());
Mehdi Aminib5a46c12017-03-28 18:55:44 +0000472 CodeGen->setFreestanding(EnableFreestanding);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000473
Mehdi Aminicc7fbf72016-12-28 19:37:16 +0000474 if (OptLevel.getNumOccurrences()) {
475 if (OptLevel < '0' || OptLevel > '3')
476 report_fatal_error("Optimization level must be between 0 and 3");
477 CodeGen->setOptLevel(OptLevel - '0');
478 switch (OptLevel) {
479 case '0':
480 CodeGen->setCodeGenOptLevel(CodeGenOpt::None);
481 break;
482 case '1':
483 CodeGen->setCodeGenOptLevel(CodeGenOpt::Less);
484 break;
485 case '2':
486 CodeGen->setCodeGenOptLevel(CodeGenOpt::Default);
487 break;
488 case '3':
489 CodeGen->setCodeGenOptLevel(CodeGenOpt::Aggressive);
490 break;
491 }
492 }
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000493 return wrap(CodeGen);
494}
495
496void thinlto_codegen_dispose(thinlto_code_gen_t cg) { delete unwrap(cg); }
497
498void thinlto_codegen_add_module(thinlto_code_gen_t cg, const char *Identifier,
499 const char *Data, int Length) {
500 unwrap(cg)->addModule(Identifier, StringRef(Data, Length));
501}
502
503void thinlto_codegen_process(thinlto_code_gen_t cg) { unwrap(cg)->run(); }
504
505unsigned int thinlto_module_get_num_objects(thinlto_code_gen_t cg) {
506 return unwrap(cg)->getProducedBinaries().size();
507}
508LTOObjectBuffer thinlto_module_get_object(thinlto_code_gen_t cg,
509 unsigned int index) {
510 assert(index < unwrap(cg)->getProducedBinaries().size() && "Index overflow");
511 auto &MemBuffer = unwrap(cg)->getProducedBinaries()[index];
Mehdi Aminic286b9f2016-03-19 21:28:18 +0000512 return LTOObjectBuffer{MemBuffer->getBufferStart(),
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000513 MemBuffer->getBufferSize()};
514}
515
Mehdi Amini8e13bc42016-12-14 04:56:42 +0000516unsigned int thinlto_module_get_num_object_files(thinlto_code_gen_t cg) {
517 return unwrap(cg)->getProducedBinaryFiles().size();
518}
519const char *thinlto_module_get_object_file(thinlto_code_gen_t cg,
520 unsigned int index) {
521 assert(index < unwrap(cg)->getProducedBinaryFiles().size() &&
522 "Index overflow");
523 return unwrap(cg)->getProducedBinaryFiles()[index].c_str();
524}
525
Mehdi Amini43b657b2016-04-01 06:47:02 +0000526void thinlto_codegen_disable_codegen(thinlto_code_gen_t cg,
527 lto_bool_t disable) {
528 unwrap(cg)->disableCodeGen(disable);
529}
530
531void thinlto_codegen_set_codegen_only(thinlto_code_gen_t cg,
532 lto_bool_t CodeGenOnly) {
533 unwrap(cg)->setCodeGenOnly(CodeGenOnly);
534}
535
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000536void thinlto_debug_options(const char *const *options, int number) {
537 // if options were requested, set them
538 if (number && options) {
539 std::vector<const char *> CodegenArgv(1, "libLTO");
540 for (auto Arg : ArrayRef<const char *>(options, number))
541 CodegenArgv.push_back(Arg);
542 cl::ParseCommandLineOptions(CodegenArgv.size(), CodegenArgv.data());
543 }
544}
545
Mehdi Amini43b657b2016-04-01 06:47:02 +0000546lto_bool_t lto_module_is_thinlto(lto_module_t mod) {
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000547 return unwrap(mod)->isThinLTO();
548}
549
550void thinlto_codegen_add_must_preserve_symbol(thinlto_code_gen_t cg,
551 const char *Name, int Length) {
552 unwrap(cg)->preserveSymbol(StringRef(Name, Length));
553}
554
555void thinlto_codegen_add_cross_referenced_symbol(thinlto_code_gen_t cg,
556 const char *Name, int Length) {
557 unwrap(cg)->crossReferenceSymbol(StringRef(Name, Length));
558}
559
560void thinlto_codegen_set_cpu(thinlto_code_gen_t cg, const char *cpu) {
561 return unwrap(cg)->setCpu(cpu);
562}
563
564void thinlto_codegen_set_cache_dir(thinlto_code_gen_t cg,
565 const char *cache_dir) {
566 return unwrap(cg)->setCacheDir(cache_dir);
567}
568
569void thinlto_codegen_set_cache_pruning_interval(thinlto_code_gen_t cg,
570 int interval) {
571 return unwrap(cg)->setCachePruningInterval(interval);
572}
573
574void thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg,
575 unsigned expiration) {
576 return unwrap(cg)->setCacheEntryExpiration(expiration);
577}
578
579void thinlto_codegen_set_final_cache_size_relative_to_available_space(
580 thinlto_code_gen_t cg, unsigned Percentage) {
581 return unwrap(cg)->setMaxCacheSizeRelativeToAvailableSpace(Percentage);
582}
583
584void thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg,
585 const char *save_temps_dir) {
586 return unwrap(cg)->setSaveTempsDir(save_temps_dir);
587}
588
Mehdi Amini8e13bc42016-12-14 04:56:42 +0000589void thinlto_set_generated_objects_dir(thinlto_code_gen_t cg,
590 const char *save_temps_dir) {
591 unwrap(cg)->setGeneratedObjectsDirectory(save_temps_dir);
592}
593
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000594lto_bool_t thinlto_codegen_set_pic_model(thinlto_code_gen_t cg,
595 lto_codegen_model model) {
596 switch (model) {
597 case LTO_CODEGEN_PIC_MODEL_STATIC:
598 unwrap(cg)->setCodePICModel(Reloc::Static);
599 return false;
600 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
601 unwrap(cg)->setCodePICModel(Reloc::PIC_);
602 return false;
603 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
604 unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC);
605 return false;
606 case LTO_CODEGEN_PIC_MODEL_DEFAULT:
Rafael Espindola8c34dd82016-05-18 22:04:49 +0000607 unwrap(cg)->setCodePICModel(None);
Mehdi Amini7c4a1a82016-03-09 01:37:22 +0000608 return false;
609 }
610 sLastErrorString = "Unknown PIC model";
611 return true;
612}