blob: 6058fb1197eb4a47b7ccd529332e71fa6400972a [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"
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +000018#include "llvm/IR/LLVMContext.h"
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000019#include "llvm/LTO/LTOCodeGenerator.h"
20#include "llvm/LTO/LTOModule.h"
Alp Tokerac903802014-07-04 00:58:41 +000021#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer50a20c02015-01-29 17:20:41 +000022#include "llvm/Support/Signals.h"
Rafael Espindola77c50d22014-06-19 19:11:22 +000023#include "llvm/Support/TargetSelect.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000024
Rafael Espindola0b385c72013-09-30 16:39:19 +000025// extra command-line flags needed for LTOCodeGenerator
Peter Collingbourne070843d2015-03-19 22:01:00 +000026static cl::opt<char>
27OptLevel("O",
28 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
29 "(default = '-O2')"),
30 cl::Prefix,
31 cl::ZeroOrMore,
32 cl::init('2'));
Rafael Espindola0b385c72013-09-30 16:39:19 +000033
34static cl::opt<bool>
35DisableInline("disable-inlining", cl::init(false),
36 cl::desc("Do not run the inliner pass"));
37
38static cl::opt<bool>
39DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
40 cl::desc("Do not run the GVN load PRE pass"));
Nick Kledzik07b4a622008-02-26 20:26:43 +000041
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +000042static cl::opt<bool>
43DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
44 cl::desc("Do not run loop or slp vectorization during LTO"));
45
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +000046#ifdef NDEBUG
47static bool VerifyByDefault = false;
48#else
49static bool VerifyByDefault = true;
50#endif
51
52static cl::opt<bool> DisableVerify(
53 "disable-llvm-verifier", cl::init(!VerifyByDefault),
54 cl::desc("Don't run the LLVM verifier during the optimization pipeline"));
55
Bill Wendling36cbf032012-03-30 10:29:38 +000056// Holds most recent error string.
57// *** Not thread safe ***
Nick Kledzik07b4a622008-02-26 20:26:43 +000058static std::string sLastErrorString;
59
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000060// Holds the initialization state of the LTO module.
61// *** Not thread safe ***
62static bool initialized = false;
63
Rafael Espindolaefa02d52013-10-02 14:36:23 +000064// Holds the command-line option parsing state of the LTO module.
65static bool parsedOptions = false;
66
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000067// Initialize the configured targets if they have not been initialized.
68static void lto_initialize() {
69 if (!initialized) {
Michael J. Spencer50a20c02015-01-29 17:20:41 +000070#ifdef LLVM_ON_WIN32
71 // Dialog box on crash disabling doesn't work across DLL boundaries, so do
72 // it here.
73 llvm::sys::DisableSystemDialogsOnCrash();
74#endif
75
Rafael Espindola77c50d22014-06-19 19:11:22 +000076 InitializeAllTargetInfos();
77 InitializeAllTargets();
78 InitializeAllTargetMCs();
79 InitializeAllAsmParsers();
80 InitializeAllAsmPrinters();
81 InitializeAllDisassemblers();
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000082 initialized = true;
83 }
84}
85
Peter Collingbourne3cc69d902015-06-01 20:08:30 +000086namespace {
87
Yunzhong Gaoea7b3a22015-11-11 19:59:08 +000088static void handleLibLTODiagnostic(lto_codegen_diagnostic_severity_t Severity,
89 const char *Msg, void *) {
90 sLastErrorString = Msg;
91 sLastErrorString += "\n";
92}
93
Peter Collingbourne3cc69d902015-06-01 20:08:30 +000094// This derived class owns the native object file. This helps implement the
95// libLTO API semantics, which require that the code generator owns the object
96// file.
97struct LibLTOCodeGenerator : LTOCodeGenerator {
Rafael Espindola7b8a24e2015-12-04 02:42:28 +000098 LibLTOCodeGenerator() : LTOCodeGenerator(getGlobalContext()) {
Yunzhong Gaoea7b3a22015-11-11 19:59:08 +000099 setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000100 LibLTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
Rafael Espindola7b8a24e2015-12-04 02:42:28 +0000101 : LTOCodeGenerator(*Context), OwnedContext(std::move(Context)) {
Yunzhong Gaoea7b3a22015-11-11 19:59:08 +0000102 setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000103
104 std::unique_ptr<MemoryBuffer> NativeObjectFile;
Rafael Espindola7b8a24e2015-12-04 02:42:28 +0000105 std::unique_ptr<LLVMContext> OwnedContext;
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000106};
107
108}
109
110DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LibLTOCodeGenerator, lto_code_gen_t)
Patrik Hagglund9be9d872014-05-05 12:24:08 +0000111DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000112
Tom Roederfd1bc602014-04-25 21:46:51 +0000113// Convert the subtarget features into a string to pass to LTOCodeGenerator.
114static void lto_add_attrs(lto_code_gen_t cg) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000115 LTOCodeGenerator *CG = unwrap(cg);
Tom Roederfd1bc602014-04-25 21:46:51 +0000116 if (MAttrs.size()) {
117 std::string attrs;
118 for (unsigned i = 0; i < MAttrs.size(); ++i) {
119 if (i > 0)
120 attrs.append(",");
121 attrs.append(MAttrs[i]);
122 }
123
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000124 CG->setAttr(attrs.c_str());
Tom Roederfd1bc602014-04-25 21:46:51 +0000125 }
Peter Collingbourne070843d2015-03-19 22:01:00 +0000126
127 if (OptLevel < '0' || OptLevel > '3')
128 report_fatal_error("Optimization level must be between 0 and 3");
129 CG->setOptLevel(OptLevel - '0');
Tom Roederfd1bc602014-04-25 21:46:51 +0000130}
131
Bill Wendling36cbf032012-03-30 10:29:38 +0000132extern const char* lto_get_version() {
133 return LTOCodeGenerator::getVersionString();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000134}
135
Bill Wendling36cbf032012-03-30 10:29:38 +0000136const char* lto_get_error_message() {
137 return sLastErrorString.c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000138}
139
Reid Klecknerddac1512013-10-24 22:26:04 +0000140bool lto_module_is_object_file(const char* path) {
Bill Wendling36cbf032012-03-30 10:29:38 +0000141 return LTOModule::isBitcodeFile(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000142}
143
Reid Klecknerddac1512013-10-24 22:26:04 +0000144bool lto_module_is_object_file_for_target(const char* path,
Bill Wendling36cbf032012-03-30 10:29:38 +0000145 const char* target_triplet_prefix) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000146 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
147 if (!Buffer)
Alp Tokerac903802014-07-04 00:58:41 +0000148 return false;
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000149 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000150}
151
Reid Klecknerddac1512013-10-24 22:26:04 +0000152bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
Bill Wendling36cbf032012-03-30 10:29:38 +0000153 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000154}
155
Reid Klecknerddac1512013-10-24 22:26:04 +0000156bool
Bill Wendling36cbf032012-03-30 10:29:38 +0000157lto_module_is_object_file_in_memory_for_target(const void* mem,
158 size_t length,
159 const char* target_triplet_prefix) {
Alp Tokerac903802014-07-04 00:58:41 +0000160 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
161 if (!buffer)
162 return false;
163 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000164}
165
Bill Wendling36cbf032012-03-30 10:29:38 +0000166lto_module_t lto_module_create(const char* path) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000167 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000168 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000169 return wrap(LTOModule::createFromFile(path, Options, sLastErrorString));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000170}
171
Bill Wendling36cbf032012-03-30 10:29:38 +0000172lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000173 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000174 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000175 return wrap(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000176 LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString));
Rafael Espindola56e41f72011-02-08 22:40:47 +0000177}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000178
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000179lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
180 size_t file_size,
181 size_t map_size,
Bill Wendling36cbf032012-03-30 10:29:38 +0000182 off_t offset) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000183 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000184 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000185 return wrap(LTOModule::createFromOpenFileSlice(fd, path, map_size, offset,
186 Options, sLastErrorString));
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000187}
188
Bill Wendling36cbf032012-03-30 10:29:38 +0000189lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000190 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000191 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000192 return wrap(LTOModule::createFromBuffer(mem, length, Options, sLastErrorString));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000193}
194
Manman Ren03456a12014-02-10 23:26:14 +0000195lto_module_t lto_module_create_from_memory_with_path(const void* mem,
196 size_t length,
197 const char *path) {
198 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000199 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000200 return wrap(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000201 LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path));
Manman Ren03456a12014-02-10 23:26:14 +0000202}
203
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000204lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
205 const char *path) {
206 lto_initialize();
207 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
208 return wrap(LTOModule::createInLocalContext(mem, length, Options,
209 sLastErrorString, path));
210}
211
212lto_module_t lto_module_create_in_codegen_context(const void *mem,
213 size_t length,
214 const char *path,
215 lto_code_gen_t cg) {
216 lto_initialize();
217 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
218 return wrap(LTOModule::createInContext(mem, length, Options, sLastErrorString,
219 path, &unwrap(cg)->getContext()));
220}
221
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000222void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000223
Bill Wendling36cbf032012-03-30 10:29:38 +0000224const char* lto_module_get_target_triple(lto_module_t mod) {
Rafael Espindolad749fb52014-07-04 14:19:41 +0000225 return unwrap(mod)->getTargetTriple().c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000226}
227
Bill Wendling36cbf032012-03-30 10:29:38 +0000228void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000229 return unwrap(mod)->setTargetTriple(triple);
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000230}
231
Bill Wendling36cbf032012-03-30 10:29:38 +0000232unsigned int lto_module_get_num_symbols(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000233 return unwrap(mod)->getSymbolCount();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000234}
235
Bill Wendling36cbf032012-03-30 10:29:38 +0000236const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000237 return unwrap(mod)->getSymbolName(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000238}
239
Bill Wendling36cbf032012-03-30 10:29:38 +0000240lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
241 unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000242 return unwrap(mod)->getSymbolAttributes(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000243}
244
Peter Collingbourne5d7dffb2015-06-29 23:09:12 +0000245const char* lto_module_get_linkeropts(lto_module_t mod) {
Peter Collingbourneaef36592015-06-29 22:04:09 +0000246 return unwrap(mod)->getLinkerOpts();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000247}
248
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000249void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
250 lto_diagnostic_handler_t diag_handler,
251 void *ctxt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000252 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000253}
254
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000255static lto_code_gen_t createCodeGen(bool InLocalContext) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000256 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000257
Eli Benderskyf0f21002014-02-19 17:09:35 +0000258 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000259
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000260 LibLTOCodeGenerator *CodeGen =
261 InLocalContext ? new LibLTOCodeGenerator(make_unique<LLVMContext>())
262 : new LibLTOCodeGenerator();
263 CodeGen->setTargetOptions(Options);
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000264 return wrap(CodeGen);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000265}
266
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000267lto_code_gen_t lto_codegen_create(void) {
268 return createCodeGen(/* InLocalContext */ false);
269}
270
271lto_code_gen_t lto_codegen_create_in_local_context(void) {
272 return createCodeGen(/* InLocalContext */ true);
273}
274
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000275void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000276
Reid Klecknerddac1512013-10-24 22:26:04 +0000277bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000278 return !unwrap(cg)->addModule(unwrap(mod));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000279}
280
Manman Ren6487ce92015-02-24 00:45:56 +0000281void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {
Peter Collingbourne9c8909d2015-08-24 22:22:53 +0000282 unwrap(cg)->setModule(std::unique_ptr<LTOModule>(unwrap(mod)));
Manman Ren6487ce92015-02-24 00:45:56 +0000283}
284
Reid Klecknerddac1512013-10-24 22:26:04 +0000285bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000286 unwrap(cg)->setDebugInfo(debug);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000287 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000288}
289
Reid Klecknerddac1512013-10-24 22:26:04 +0000290bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Peter Collingbourne44ee84e2015-08-21 22:57:17 +0000291 switch (model) {
292 case LTO_CODEGEN_PIC_MODEL_STATIC:
293 unwrap(cg)->setCodePICModel(Reloc::Static);
294 return false;
295 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
296 unwrap(cg)->setCodePICModel(Reloc::PIC_);
297 return false;
298 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
299 unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC);
300 return false;
301 case LTO_CODEGEN_PIC_MODEL_DEFAULT:
302 unwrap(cg)->setCodePICModel(Reloc::Default);
303 return false;
304 }
305 sLastErrorString = "Unknown PIC model";
306 return true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000307}
308
Bill Wendling152e4732012-03-31 10:44:20 +0000309void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000310 return unwrap(cg)->setCpu(cpu);
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000311}
312
Bill Wendling36cbf032012-03-30 10:29:38 +0000313void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000314 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcac8c8a2009-06-04 00:28:45 +0000315}
316
Bill Wendling152e4732012-03-31 10:44:20 +0000317void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling36cbf032012-03-30 10:29:38 +0000318 int nargs) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000319 // In here only for backwards compatibility. We use MC now.
Rafael Espindola00456462010-08-10 18:55:09 +0000320}
321
Bill Wendling36cbf032012-03-30 10:29:38 +0000322void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling152e4732012-03-31 10:44:20 +0000323 const char *symbol) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000324 unwrap(cg)->addMustPreserveSymbol(symbol);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000325}
326
Peter Collingbourne78240e02015-03-19 22:12:08 +0000327static void maybeParseOptions(lto_code_gen_t cg) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000328 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000329 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000330 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000331 parsedOptions = true;
332 }
Peter Collingbourne070843d2015-03-19 22:01:00 +0000333}
334
335bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000336 maybeParseOptions(cg);
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000337 return !unwrap(cg)->writeMergedModules(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000338}
339
Bill Wendling152e4732012-03-31 10:44:20 +0000340const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000341 maybeParseOptions(cg);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000342 LibLTOCodeGenerator *CG = unwrap(cg);
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000343 CG->NativeObjectFile =
344 CG->compile(DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000345 DisableLTOVectorization);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000346 if (!CG->NativeObjectFile)
347 return nullptr;
348 *length = CG->NativeObjectFile->getBufferSize();
349 return CG->NativeObjectFile->getBufferStart();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000350}
351
Manman Ren8121e1d2015-02-03 18:39:15 +0000352bool lto_codegen_optimize(lto_code_gen_t cg) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000353 maybeParseOptions(cg);
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000354 return !unwrap(cg)->optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000355 DisableLTOVectorization);
Manman Ren8121e1d2015-02-03 18:39:15 +0000356}
357
358const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000359 maybeParseOptions(cg);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000360 LibLTOCodeGenerator *CG = unwrap(cg);
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000361 CG->NativeObjectFile = CG->compileOptimized();
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000362 if (!CG->NativeObjectFile)
363 return nullptr;
364 *length = CG->NativeObjectFile->getBufferSize();
365 return CG->NativeObjectFile->getBufferStart();
Manman Ren8121e1d2015-02-03 18:39:15 +0000366}
367
Reid Klecknerddac1512013-10-24 22:26:04 +0000368bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000369 maybeParseOptions(cg);
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000370 return !unwrap(cg)->compile_to_file(
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000371 name, DisableVerify, DisableInline, DisableGVNLoadPRE,
Yunzhong Gao8e348cc2015-11-17 19:48:12 +0000372 DisableLTOVectorization);
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000373}
374
Bill Wendling152e4732012-03-31 10:44:20 +0000375void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000376 unwrap(cg)->setCodeGenDebugOptions(opt);
Duncan Sands31554ab2009-07-03 15:38:01 +0000377}
Rafael Espindolaa5ef4902015-02-03 19:25:53 +0000378
379unsigned int lto_api_version() { return LTO_API_VERSION; }
Manman Rence0a0662015-04-17 17:10:09 +0000380
381void lto_codegen_set_should_internalize(lto_code_gen_t cg,
382 bool ShouldInternalize) {
383 unwrap(cg)->setShouldInternalize(ShouldInternalize);
384}
Duncan P. N. Exon Smith5a490d02015-04-27 23:38:54 +0000385
386void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
387 lto_bool_t ShouldEmbedUselists) {
388 unwrap(cg)->setShouldEmbedUselists(ShouldEmbedUselists);
389}