blob: d13de57e830c135c37de060b9f0d42620081e3e9 [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"
Alp Tokerac903802014-07-04 00:58:41 +000023#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer50a20c02015-01-29 17:20:41 +000024#include "llvm/Support/Signals.h"
Rafael Espindola77c50d22014-06-19 19:11:22 +000025#include "llvm/Support/TargetSelect.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000026
Rafael Espindola0b385c72013-09-30 16:39:19 +000027// extra command-line flags needed for LTOCodeGenerator
Peter Collingbourne070843d2015-03-19 22:01:00 +000028static cl::opt<char>
29OptLevel("O",
30 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
31 "(default = '-O2')"),
32 cl::Prefix,
33 cl::ZeroOrMore,
34 cl::init('2'));
Rafael Espindola0b385c72013-09-30 16:39:19 +000035
36static cl::opt<bool>
37DisableInline("disable-inlining", cl::init(false),
38 cl::desc("Do not run the inliner pass"));
39
40static cl::opt<bool>
41DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
42 cl::desc("Do not run the GVN load PRE pass"));
Nick Kledzik07b4a622008-02-26 20:26:43 +000043
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +000044static cl::opt<bool>
45DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
46 cl::desc("Do not run loop or slp vectorization during LTO"));
47
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +000048#ifdef NDEBUG
49static bool VerifyByDefault = false;
50#else
51static bool VerifyByDefault = true;
52#endif
53
54static cl::opt<bool> DisableVerify(
55 "disable-llvm-verifier", cl::init(!VerifyByDefault),
56 cl::desc("Don't run the LLVM verifier during the optimization pipeline"));
57
Bill Wendling36cbf032012-03-30 10:29:38 +000058// Holds most recent error string.
59// *** Not thread safe ***
Nick Kledzik07b4a622008-02-26 20:26:43 +000060static std::string sLastErrorString;
61
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000062// Holds the initialization state of the LTO module.
63// *** Not thread safe ***
64static bool initialized = false;
65
Rafael Espindolaefa02d52013-10-02 14:36:23 +000066// Holds the command-line option parsing state of the LTO module.
67static bool parsedOptions = false;
68
Rafael Espindolaa7612b42015-12-04 16:14:31 +000069static LLVMContext *LTOContext = nullptr;
70
71static void diagnosticHandler(const DiagnosticInfo &DI, void *Context) {
72 if (DI.getSeverity() != DS_Error) {
73 DiagnosticPrinterRawOStream DP(errs());
74 DI.print(DP);
75 errs() << '\n';
76 return;
77 }
78 sLastErrorString = "";
79 {
80 raw_string_ostream Stream(sLastErrorString);
81 DiagnosticPrinterRawOStream DP(Stream);
82 DI.print(DP);
83 }
84 sLastErrorString += '\n';
85}
86
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000087// Initialize the configured targets if they have not been initialized.
88static void lto_initialize() {
89 if (!initialized) {
Michael J. Spencer50a20c02015-01-29 17:20:41 +000090#ifdef LLVM_ON_WIN32
91 // Dialog box on crash disabling doesn't work across DLL boundaries, so do
92 // it here.
93 llvm::sys::DisableSystemDialogsOnCrash();
94#endif
95
Rafael Espindola77c50d22014-06-19 19:11:22 +000096 InitializeAllTargetInfos();
97 InitializeAllTargets();
98 InitializeAllTargetMCs();
99 InitializeAllAsmParsers();
100 InitializeAllAsmPrinters();
101 InitializeAllDisassemblers();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000102
103 LTOContext = &getGlobalContext();
104 LTOContext->setDiagnosticHandler(diagnosticHandler, nullptr, true);
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000105 initialized = true;
106 }
107}
108
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000109namespace {
110
Yunzhong Gaoea7b3a22015-11-11 19:59:08 +0000111static void handleLibLTODiagnostic(lto_codegen_diagnostic_severity_t Severity,
112 const char *Msg, void *) {
113 sLastErrorString = Msg;
114 sLastErrorString += "\n";
115}
116
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000117// This derived class owns the native object file. This helps implement the
118// libLTO API semantics, which require that the code generator owns the object
119// file.
120struct LibLTOCodeGenerator : LTOCodeGenerator {
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000121 LibLTOCodeGenerator() : LTOCodeGenerator(*LTOContext) {
Yunzhong Gaoea7b3a22015-11-11 19:59:08 +0000122 setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
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)) {
Yunzhong Gaoea7b3a22015-11-11 19:59:08 +0000125 setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000126
127 std::unique_ptr<MemoryBuffer> NativeObjectFile;
Rafael Espindola7b8a24e2015-12-04 02:42:28 +0000128 std::unique_ptr<LLVMContext> OwnedContext;
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000129};
130
131}
132
133DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LibLTOCodeGenerator, lto_code_gen_t)
Patrik Hagglund9be9d872014-05-05 12:24:08 +0000134DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000135
Tom Roederfd1bc602014-04-25 21:46:51 +0000136// Convert the subtarget features into a string to pass to LTOCodeGenerator.
137static void lto_add_attrs(lto_code_gen_t cg) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000138 LTOCodeGenerator *CG = unwrap(cg);
Tom Roederfd1bc602014-04-25 21:46:51 +0000139 if (MAttrs.size()) {
140 std::string attrs;
141 for (unsigned i = 0; i < MAttrs.size(); ++i) {
142 if (i > 0)
143 attrs.append(",");
144 attrs.append(MAttrs[i]);
145 }
146
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000147 CG->setAttr(attrs.c_str());
Tom Roederfd1bc602014-04-25 21:46:51 +0000148 }
Peter Collingbourne070843d2015-03-19 22:01:00 +0000149
150 if (OptLevel < '0' || OptLevel > '3')
151 report_fatal_error("Optimization level must be between 0 and 3");
152 CG->setOptLevel(OptLevel - '0');
Tom Roederfd1bc602014-04-25 21:46:51 +0000153}
154
Bill Wendling36cbf032012-03-30 10:29:38 +0000155extern const char* lto_get_version() {
156 return LTOCodeGenerator::getVersionString();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000157}
158
Bill Wendling36cbf032012-03-30 10:29:38 +0000159const char* lto_get_error_message() {
160 return sLastErrorString.c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000161}
162
Reid Klecknerddac1512013-10-24 22:26:04 +0000163bool lto_module_is_object_file(const char* path) {
Bill Wendling36cbf032012-03-30 10:29:38 +0000164 return LTOModule::isBitcodeFile(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000165}
166
Reid Klecknerddac1512013-10-24 22:26:04 +0000167bool lto_module_is_object_file_for_target(const char* path,
Bill Wendling36cbf032012-03-30 10:29:38 +0000168 const char* target_triplet_prefix) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000169 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
170 if (!Buffer)
Alp Tokerac903802014-07-04 00:58:41 +0000171 return false;
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000172 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000173}
174
Reid Klecknerddac1512013-10-24 22:26:04 +0000175bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
Bill Wendling36cbf032012-03-30 10:29:38 +0000176 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000177}
178
Reid Klecknerddac1512013-10-24 22:26:04 +0000179bool
Bill Wendling36cbf032012-03-30 10:29:38 +0000180lto_module_is_object_file_in_memory_for_target(const void* mem,
181 size_t length,
182 const char* target_triplet_prefix) {
Alp Tokerac903802014-07-04 00:58:41 +0000183 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
184 if (!buffer)
185 return false;
186 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000187}
188
Bill Wendling36cbf032012-03-30 10:29:38 +0000189lto_module_t lto_module_create(const char* path) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000190 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000191 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000192 ErrorOr<std::unique_ptr<LTOModule>> M =
193 LTOModule::createFromFile(*LTOContext, path, Options);
194 if (!M)
195 return nullptr;
196 return wrap(M->release());
Nick Kledzik07b4a622008-02-26 20:26:43 +0000197}
198
Bill Wendling36cbf032012-03-30 10:29:38 +0000199lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000200 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000201 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000202 ErrorOr<std::unique_ptr<LTOModule>> M =
203 LTOModule::createFromOpenFile(*LTOContext, fd, path, size, Options);
204 if (!M)
205 return nullptr;
206 return wrap(M->release());
Rafael Espindola56e41f72011-02-08 22:40:47 +0000207}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000208
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000209lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
210 size_t file_size,
211 size_t map_size,
Bill Wendling36cbf032012-03-30 10:29:38 +0000212 off_t offset) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000213 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000214 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000215 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createFromOpenFileSlice(
216 *LTOContext, fd, path, map_size, offset, Options);
217 if (!M)
218 return nullptr;
219 return wrap(M->release());
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000220}
221
Bill Wendling36cbf032012-03-30 10:29:38 +0000222lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000223 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000224 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000225 ErrorOr<std::unique_ptr<LTOModule>> M =
226 LTOModule::createFromBuffer(*LTOContext, mem, length, Options);
227 if (!M)
228 return nullptr;
229 return wrap(M->release());
Nick Kledzik07b4a622008-02-26 20:26:43 +0000230}
231
Manman Ren03456a12014-02-10 23:26:14 +0000232lto_module_t lto_module_create_from_memory_with_path(const void* mem,
233 size_t length,
234 const char *path) {
235 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000236 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000237 ErrorOr<std::unique_ptr<LTOModule>> M =
238 LTOModule::createFromBuffer(*LTOContext, mem, length, Options, path);
239 if (!M)
240 return nullptr;
241 return wrap(M->release());
Manman Ren03456a12014-02-10 23:26:14 +0000242}
243
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000244lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
245 const char *path) {
246 lto_initialize();
247 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000248 ErrorOr<std::unique_ptr<LTOModule>> M =
249 LTOModule::createInLocalContext(mem, length, Options, path);
250 if (!M)
251 return nullptr;
252 return wrap(M->release());
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000253}
254
255lto_module_t lto_module_create_in_codegen_context(const void *mem,
256 size_t length,
257 const char *path,
258 lto_code_gen_t cg) {
259 lto_initialize();
260 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindolaa7612b42015-12-04 16:14:31 +0000261 ErrorOr<std::unique_ptr<LTOModule>> M = LTOModule::createInContext(
262 mem, length, Options, path, &unwrap(cg)->getContext());
263 return wrap(M->release());
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000264}
265
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000266void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000267
Bill Wendling36cbf032012-03-30 10:29:38 +0000268const char* lto_module_get_target_triple(lto_module_t mod) {
Rafael Espindolad749fb52014-07-04 14:19:41 +0000269 return unwrap(mod)->getTargetTriple().c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000270}
271
Bill Wendling36cbf032012-03-30 10:29:38 +0000272void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000273 return unwrap(mod)->setTargetTriple(triple);
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000274}
275
Bill Wendling36cbf032012-03-30 10:29:38 +0000276unsigned int lto_module_get_num_symbols(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000277 return unwrap(mod)->getSymbolCount();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000278}
279
Bill Wendling36cbf032012-03-30 10:29:38 +0000280const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000281 return unwrap(mod)->getSymbolName(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000282}
283
Bill Wendling36cbf032012-03-30 10:29:38 +0000284lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
285 unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000286 return unwrap(mod)->getSymbolAttributes(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000287}
288
Peter Collingbourne5d7dffb2015-06-29 23:09:12 +0000289const char* lto_module_get_linkeropts(lto_module_t mod) {
Peter Collingbourneaef36592015-06-29 22:04:09 +0000290 return unwrap(mod)->getLinkerOpts();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000291}
292
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000293void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
294 lto_diagnostic_handler_t diag_handler,
295 void *ctxt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000296 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000297}
298
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000299static lto_code_gen_t createCodeGen(bool InLocalContext) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000300 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000301
Eli Benderskyf0f21002014-02-19 17:09:35 +0000302 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000303
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000304 LibLTOCodeGenerator *CodeGen =
305 InLocalContext ? new LibLTOCodeGenerator(make_unique<LLVMContext>())
306 : new LibLTOCodeGenerator();
307 CodeGen->setTargetOptions(Options);
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000308 return wrap(CodeGen);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000309}
310
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000311lto_code_gen_t lto_codegen_create(void) {
312 return createCodeGen(/* InLocalContext */ false);
313}
314
315lto_code_gen_t lto_codegen_create_in_local_context(void) {
316 return createCodeGen(/* InLocalContext */ true);
317}
318
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000319void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000320
Reid Klecknerddac1512013-10-24 22:26:04 +0000321bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000322 return !unwrap(cg)->addModule(unwrap(mod));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000323}
324
Manman Ren6487ce92015-02-24 00:45:56 +0000325void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {
Peter Collingbourne9c8909d2015-08-24 22:22:53 +0000326 unwrap(cg)->setModule(std::unique_ptr<LTOModule>(unwrap(mod)));
Manman Ren6487ce92015-02-24 00:45:56 +0000327}
328
Reid Klecknerddac1512013-10-24 22:26:04 +0000329bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000330 unwrap(cg)->setDebugInfo(debug);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000331 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000332}
333
Reid Klecknerddac1512013-10-24 22:26:04 +0000334bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Peter Collingbourne44ee84e2015-08-21 22:57:17 +0000335 switch (model) {
336 case LTO_CODEGEN_PIC_MODEL_STATIC:
337 unwrap(cg)->setCodePICModel(Reloc::Static);
338 return false;
339 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
340 unwrap(cg)->setCodePICModel(Reloc::PIC_);
341 return false;
342 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
343 unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC);
344 return false;
345 case LTO_CODEGEN_PIC_MODEL_DEFAULT:
346 unwrap(cg)->setCodePICModel(Reloc::Default);
347 return false;
348 }
349 sLastErrorString = "Unknown PIC model";
350 return true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000351}
352
Bill Wendling152e4732012-03-31 10:44:20 +0000353void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000354 return unwrap(cg)->setCpu(cpu);
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000355}
356
Bill Wendling36cbf032012-03-30 10:29:38 +0000357void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000358 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcac8c8a2009-06-04 00:28:45 +0000359}
360
Bill Wendling152e4732012-03-31 10:44:20 +0000361void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling36cbf032012-03-30 10:29:38 +0000362 int nargs) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000363 // In here only for backwards compatibility. We use MC now.
Rafael Espindola00456462010-08-10 18:55:09 +0000364}
365
Bill Wendling36cbf032012-03-30 10:29:38 +0000366void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling152e4732012-03-31 10:44:20 +0000367 const char *symbol) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000368 unwrap(cg)->addMustPreserveSymbol(symbol);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000369}
370
Peter Collingbourne78240e02015-03-19 22:12:08 +0000371static void maybeParseOptions(lto_code_gen_t cg) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000372 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000373 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000374 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000375 parsedOptions = true;
376 }
Peter Collingbourne070843d2015-03-19 22:01:00 +0000377}
378
379bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000380 maybeParseOptions(cg);
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000381 return !unwrap(cg)->writeMergedModules(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000382}
383
Bill Wendling152e4732012-03-31 10:44:20 +0000384const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000385 maybeParseOptions(cg);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000386 LibLTOCodeGenerator *CG = unwrap(cg);
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000387 CG->NativeObjectFile =
388 CG->compile(DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000389 DisableLTOVectorization);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000390 if (!CG->NativeObjectFile)
391 return nullptr;
392 *length = CG->NativeObjectFile->getBufferSize();
393 return CG->NativeObjectFile->getBufferStart();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000394}
395
Manman Ren8121e1d2015-02-03 18:39:15 +0000396bool lto_codegen_optimize(lto_code_gen_t cg) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000397 maybeParseOptions(cg);
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000398 return !unwrap(cg)->optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000399 DisableLTOVectorization);
Manman Ren8121e1d2015-02-03 18:39:15 +0000400}
401
402const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000403 maybeParseOptions(cg);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000404 LibLTOCodeGenerator *CG = unwrap(cg);
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000405 CG->NativeObjectFile = CG->compileOptimized();
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000406 if (!CG->NativeObjectFile)
407 return nullptr;
408 *length = CG->NativeObjectFile->getBufferSize();
409 return CG->NativeObjectFile->getBufferStart();
Manman Ren8121e1d2015-02-03 18:39:15 +0000410}
411
Reid Klecknerddac1512013-10-24 22:26:04 +0000412bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000413 maybeParseOptions(cg);
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000414 return !unwrap(cg)->compile_to_file(
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000415 name, DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000416 DisableLTOVectorization);
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000417}
418
Bill Wendling152e4732012-03-31 10:44:20 +0000419void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000420 unwrap(cg)->setCodeGenDebugOptions(opt);
Duncan Sands31554ab2009-07-03 15:38:01 +0000421}
Rafael Espindolaa5ef4902015-02-03 19:25:53 +0000422
423unsigned int lto_api_version() { return LTO_API_VERSION; }
Manman Rence0a0662015-04-17 17:10:09 +0000424
425void lto_codegen_set_should_internalize(lto_code_gen_t cg,
426 bool ShouldInternalize) {
427 unwrap(cg)->setShouldInternalize(ShouldInternalize);
428}
Duncan P. N. Exon Smith5a490d02015-04-27 23:38:54 +0000429
430void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
431 lto_bool_t ShouldEmbedUselists) {
432 unwrap(cg)->setShouldEmbedUselists(ShouldEmbedUselists);
433}