blob: 0839a566cc5832ec0461cf18c8ae13f62472998a [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 {
Yunzhong Gaoea7b3a22015-11-11 19:59:08 +000098 LibLTOCodeGenerator() {
99 setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000100 LibLTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
Yunzhong Gaoea7b3a22015-11-11 19:59:08 +0000101 : LTOCodeGenerator(std::move(Context)) {
102 setDiagnosticHandler(handleLibLTODiagnostic, nullptr); }
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000103
104 std::unique_ptr<MemoryBuffer> NativeObjectFile;
105};
106
107}
108
109DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LibLTOCodeGenerator, lto_code_gen_t)
Patrik Hagglund9be9d872014-05-05 12:24:08 +0000110DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000111
Tom Roederfd1bc602014-04-25 21:46:51 +0000112// Convert the subtarget features into a string to pass to LTOCodeGenerator.
113static void lto_add_attrs(lto_code_gen_t cg) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000114 LTOCodeGenerator *CG = unwrap(cg);
Tom Roederfd1bc602014-04-25 21:46:51 +0000115 if (MAttrs.size()) {
116 std::string attrs;
117 for (unsigned i = 0; i < MAttrs.size(); ++i) {
118 if (i > 0)
119 attrs.append(",");
120 attrs.append(MAttrs[i]);
121 }
122
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000123 CG->setAttr(attrs.c_str());
Tom Roederfd1bc602014-04-25 21:46:51 +0000124 }
Peter Collingbourne070843d2015-03-19 22:01:00 +0000125
126 if (OptLevel < '0' || OptLevel > '3')
127 report_fatal_error("Optimization level must be between 0 and 3");
128 CG->setOptLevel(OptLevel - '0');
Tom Roederfd1bc602014-04-25 21:46:51 +0000129}
130
Bill Wendling36cbf032012-03-30 10:29:38 +0000131extern const char* lto_get_version() {
132 return LTOCodeGenerator::getVersionString();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000133}
134
Bill Wendling36cbf032012-03-30 10:29:38 +0000135const char* lto_get_error_message() {
136 return sLastErrorString.c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000137}
138
Reid Klecknerddac1512013-10-24 22:26:04 +0000139bool lto_module_is_object_file(const char* path) {
Bill Wendling36cbf032012-03-30 10:29:38 +0000140 return LTOModule::isBitcodeFile(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000141}
142
Reid Klecknerddac1512013-10-24 22:26:04 +0000143bool lto_module_is_object_file_for_target(const char* path,
Bill Wendling36cbf032012-03-30 10:29:38 +0000144 const char* target_triplet_prefix) {
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000145 ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
146 if (!Buffer)
Alp Tokerac903802014-07-04 00:58:41 +0000147 return false;
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000148 return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000149}
150
Reid Klecknerddac1512013-10-24 22:26:04 +0000151bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
Bill Wendling36cbf032012-03-30 10:29:38 +0000152 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000153}
154
Reid Klecknerddac1512013-10-24 22:26:04 +0000155bool
Bill Wendling36cbf032012-03-30 10:29:38 +0000156lto_module_is_object_file_in_memory_for_target(const void* mem,
157 size_t length,
158 const char* target_triplet_prefix) {
Alp Tokerac903802014-07-04 00:58:41 +0000159 std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
160 if (!buffer)
161 return false;
162 return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000163}
164
Bill Wendling36cbf032012-03-30 10:29:38 +0000165lto_module_t lto_module_create(const char* path) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000166 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000167 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000168 return wrap(LTOModule::createFromFile(path, Options, sLastErrorString));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000169}
170
Bill Wendling36cbf032012-03-30 10:29:38 +0000171lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000172 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000173 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000174 return wrap(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000175 LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString));
Rafael Espindola56e41f72011-02-08 22:40:47 +0000176}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000177
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000178lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
179 size_t file_size,
180 size_t map_size,
Bill Wendling36cbf032012-03-30 10:29:38 +0000181 off_t offset) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000182 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000183 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000184 return wrap(LTOModule::createFromOpenFileSlice(fd, path, map_size, offset,
185 Options, sLastErrorString));
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000186}
187
Bill Wendling36cbf032012-03-30 10:29:38 +0000188lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000189 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000190 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000191 return wrap(LTOModule::createFromBuffer(mem, length, Options, sLastErrorString));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000192}
193
Manman Ren03456a12014-02-10 23:26:14 +0000194lto_module_t lto_module_create_from_memory_with_path(const void* mem,
195 size_t length,
196 const char *path) {
197 lto_initialize();
Eli Benderskyf0f21002014-02-19 17:09:35 +0000198 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000199 return wrap(
Peter Collingbourne63086fe2014-07-03 23:28:00 +0000200 LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path));
Manman Ren03456a12014-02-10 23:26:14 +0000201}
202
Duncan P. N. Exon Smithc5800f62014-11-11 23:19:23 +0000203lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
204 const char *path) {
205 lto_initialize();
206 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
207 return wrap(LTOModule::createInLocalContext(mem, length, Options,
208 sLastErrorString, path));
209}
210
211lto_module_t lto_module_create_in_codegen_context(const void *mem,
212 size_t length,
213 const char *path,
214 lto_code_gen_t cg) {
215 lto_initialize();
216 llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
217 return wrap(LTOModule::createInContext(mem, length, Options, sLastErrorString,
218 path, &unwrap(cg)->getContext()));
219}
220
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000221void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000222
Bill Wendling36cbf032012-03-30 10:29:38 +0000223const char* lto_module_get_target_triple(lto_module_t mod) {
Rafael Espindolad749fb52014-07-04 14:19:41 +0000224 return unwrap(mod)->getTargetTriple().c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000225}
226
Bill Wendling36cbf032012-03-30 10:29:38 +0000227void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000228 return unwrap(mod)->setTargetTriple(triple);
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000229}
230
Bill Wendling36cbf032012-03-30 10:29:38 +0000231unsigned int lto_module_get_num_symbols(lto_module_t mod) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000232 return unwrap(mod)->getSymbolCount();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000233}
234
Bill Wendling36cbf032012-03-30 10:29:38 +0000235const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000236 return unwrap(mod)->getSymbolName(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000237}
238
Bill Wendling36cbf032012-03-30 10:29:38 +0000239lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
240 unsigned int index) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000241 return unwrap(mod)->getSymbolAttributes(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000242}
243
Peter Collingbourne5d7dffb2015-06-29 23:09:12 +0000244const char* lto_module_get_linkeropts(lto_module_t mod) {
Peter Collingbourneaef36592015-06-29 22:04:09 +0000245 return unwrap(mod)->getLinkerOpts();
Yunzhong Gaoa88d7ab2014-01-21 18:31:27 +0000246}
247
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000248void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
249 lto_diagnostic_handler_t diag_handler,
250 void *ctxt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000251 unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
Quentin Colombet5fa1f6f2014-01-15 22:04:35 +0000252}
253
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000254static lto_code_gen_t createCodeGen(bool InLocalContext) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000255 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000256
Eli Benderskyf0f21002014-02-19 17:09:35 +0000257 TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000258
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000259 LibLTOCodeGenerator *CodeGen =
260 InLocalContext ? new LibLTOCodeGenerator(make_unique<LLVMContext>())
261 : new LibLTOCodeGenerator();
262 CodeGen->setTargetOptions(Options);
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000263 return wrap(CodeGen);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000264}
265
Duncan P. N. Exon Smithd34b6132014-12-19 07:19:50 +0000266lto_code_gen_t lto_codegen_create(void) {
267 return createCodeGen(/* InLocalContext */ false);
268}
269
270lto_code_gen_t lto_codegen_create_in_local_context(void) {
271 return createCodeGen(/* InLocalContext */ true);
272}
273
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000274void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
Nick Kledzik07b4a622008-02-26 20:26:43 +0000275
Reid Klecknerddac1512013-10-24 22:26:04 +0000276bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
Rafael Espindolad12b4a32014-10-25 04:06:10 +0000277 return !unwrap(cg)->addModule(unwrap(mod));
Nick Kledzik07b4a622008-02-26 20:26:43 +0000278}
279
Manman Ren6487ce92015-02-24 00:45:56 +0000280void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {
Peter Collingbourne9c8909d2015-08-24 22:22:53 +0000281 unwrap(cg)->setModule(std::unique_ptr<LTOModule>(unwrap(mod)));
Manman Ren6487ce92015-02-24 00:45:56 +0000282}
283
Reid Klecknerddac1512013-10-24 22:26:04 +0000284bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000285 unwrap(cg)->setDebugInfo(debug);
Shuxin Yangb6696a92013-08-07 05:19:23 +0000286 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000287}
288
Reid Klecknerddac1512013-10-24 22:26:04 +0000289bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Peter Collingbourne44ee84e2015-08-21 22:57:17 +0000290 switch (model) {
291 case LTO_CODEGEN_PIC_MODEL_STATIC:
292 unwrap(cg)->setCodePICModel(Reloc::Static);
293 return false;
294 case LTO_CODEGEN_PIC_MODEL_DYNAMIC:
295 unwrap(cg)->setCodePICModel(Reloc::PIC_);
296 return false;
297 case LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC:
298 unwrap(cg)->setCodePICModel(Reloc::DynamicNoPIC);
299 return false;
300 case LTO_CODEGEN_PIC_MODEL_DEFAULT:
301 unwrap(cg)->setCodePICModel(Reloc::Default);
302 return false;
303 }
304 sLastErrorString = "Unknown PIC model";
305 return true;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000306}
307
Bill Wendling152e4732012-03-31 10:44:20 +0000308void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000309 return unwrap(cg)->setCpu(cpu);
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000310}
311
Bill Wendling36cbf032012-03-30 10:29:38 +0000312void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000313 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcac8c8a2009-06-04 00:28:45 +0000314}
315
Bill Wendling152e4732012-03-31 10:44:20 +0000316void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling36cbf032012-03-30 10:29:38 +0000317 int nargs) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000318 // In here only for backwards compatibility. We use MC now.
Rafael Espindola00456462010-08-10 18:55:09 +0000319}
320
Bill Wendling36cbf032012-03-30 10:29:38 +0000321void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling152e4732012-03-31 10:44:20 +0000322 const char *symbol) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000323 unwrap(cg)->addMustPreserveSymbol(symbol);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000324}
325
Peter Collingbourne78240e02015-03-19 22:12:08 +0000326static void maybeParseOptions(lto_code_gen_t cg) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000327 if (!parsedOptions) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000328 unwrap(cg)->parseCodeGenDebugOptions();
Tom Roederfd1bc602014-04-25 21:46:51 +0000329 lto_add_attrs(cg);
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000330 parsedOptions = true;
331 }
Peter Collingbourne070843d2015-03-19 22:01:00 +0000332}
333
334bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000335 maybeParseOptions(cg);
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000336 return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000337}
338
Bill Wendling152e4732012-03-31 10:44:20 +0000339const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000340 maybeParseOptions(cg);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000341 LibLTOCodeGenerator *CG = unwrap(cg);
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000342 CG->NativeObjectFile =
343 CG->compile(DisableVerify, DisableInline, DisableGVNLoadPRE,
344 DisableLTOVectorization, sLastErrorString);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000345 if (!CG->NativeObjectFile)
346 return nullptr;
347 *length = CG->NativeObjectFile->getBufferSize();
348 return CG->NativeObjectFile->getBufferStart();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000349}
350
Manman Ren8121e1d2015-02-03 18:39:15 +0000351bool lto_codegen_optimize(lto_code_gen_t cg) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000352 maybeParseOptions(cg);
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000353 return !unwrap(cg)->optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
354 DisableLTOVectorization, sLastErrorString);
Manman Ren8121e1d2015-02-03 18:39:15 +0000355}
356
357const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000358 maybeParseOptions(cg);
Peter Collingbourne3cc69d902015-06-01 20:08:30 +0000359 LibLTOCodeGenerator *CG = unwrap(cg);
360 CG->NativeObjectFile = CG->compileOptimized(sLastErrorString);
361 if (!CG->NativeObjectFile)
362 return nullptr;
363 *length = CG->NativeObjectFile->getBufferSize();
364 return CG->NativeObjectFile->getBufferStart();
Manman Ren8121e1d2015-02-03 18:39:15 +0000365}
366
Reid Klecknerddac1512013-10-24 22:26:04 +0000367bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Peter Collingbourne78240e02015-03-19 22:12:08 +0000368 maybeParseOptions(cg);
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000369 return !unwrap(cg)->compile_to_file(
Duncan P. N. Exon Smithcff5fef2015-09-15 23:05:59 +0000370 name, DisableVerify, DisableInline, DisableGVNLoadPRE,
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000371 DisableLTOVectorization, sLastErrorString);
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000372}
373
Bill Wendling152e4732012-03-31 10:44:20 +0000374void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Rafael Espindola83ceb8e2014-05-03 14:59:52 +0000375 unwrap(cg)->setCodeGenDebugOptions(opt);
Duncan Sands31554ab2009-07-03 15:38:01 +0000376}
Rafael Espindolaa5ef4902015-02-03 19:25:53 +0000377
378unsigned int lto_api_version() { return LTO_API_VERSION; }
Manman Rence0a0662015-04-17 17:10:09 +0000379
380void lto_codegen_set_should_internalize(lto_code_gen_t cg,
381 bool ShouldInternalize) {
382 unwrap(cg)->setShouldInternalize(ShouldInternalize);
383}
Duncan P. N. Exon Smith5a490d02015-04-27 23:38:54 +0000384
385void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
386 lto_bool_t ShouldEmbedUselists) {
387 unwrap(cg)->setShouldEmbedUselists(ShouldEmbedUselists);
388}