blob: 441bc77352ce57ad35db32a2e54bff56d4bf906f [file] [log] [blame]
Nick Kledzik77595fc2008-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 Wendling8fd3fcd2012-03-30 10:29:38 +00007//
Nick Kledzik77595fc2008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik77595fc2008-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"
Peter Collingbournecc488542013-09-24 23:52:22 +000016#include "llvm/LTO/LTOCodeGenerator.h"
17#include "llvm/LTO/LTOModule.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000018#include "llvm-c/Core.h"
Peter Collingbournecc488542013-09-24 23:52:22 +000019#include "llvm-c/Target.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000020
21
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000022// Holds most recent error string.
23// *** Not thread safe ***
Nick Kledzik77595fc2008-02-26 20:26:43 +000024static std::string sLastErrorString;
25
Peter Collingbournecc488542013-09-24 23:52:22 +000026// Holds the initialization state of the LTO module.
27// *** Not thread safe ***
28static bool initialized = false;
29
30// Initialize the configured targets if they have not been initialized.
31static void lto_initialize() {
32 if (!initialized) {
33 LLVMInitializeAllTargetInfos();
34 LLVMInitializeAllTargets();
35 LLVMInitializeAllTargetMCs();
36 LLVMInitializeAllAsmParsers();
37 LLVMInitializeAllAsmPrinters();
38 LLVMInitializeAllDisassemblers();
39 initialized = true;
40 }
41}
42
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000043/// lto_get_version - Returns a printable string.
44extern const char* lto_get_version() {
45 return LTOCodeGenerator::getVersionString();
Nick Kledzik77595fc2008-02-26 20:26:43 +000046}
47
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000048/// lto_get_error_message - Returns the last error string or NULL if last
49/// operation was successful.
50const char* lto_get_error_message() {
51 return sLastErrorString.c_str();
Nick Kledzik77595fc2008-02-26 20:26:43 +000052}
53
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000054/// lto_module_is_object_file - Validates if a file is a loadable object file.
55bool lto_module_is_object_file(const char* path) {
56 return LTOModule::isBitcodeFile(path);
Nick Kledzik77595fc2008-02-26 20:26:43 +000057}
58
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000059/// lto_module_is_object_file_for_target - Validates if a file is a loadable
60/// object file compilable for requested target.
61bool lto_module_is_object_file_for_target(const char* path,
62 const char* target_triplet_prefix) {
63 return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix);
Nick Kledzik77595fc2008-02-26 20:26:43 +000064}
65
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000066/// lto_module_is_object_file_in_memory - Validates if a buffer is a loadable
67/// object file.
68bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
69 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik77595fc2008-02-26 20:26:43 +000070}
71
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000072/// lto_module_is_object_file_in_memory_for_target - Validates if a buffer is a
73/// loadable object file compilable for the target.
74bool
75lto_module_is_object_file_in_memory_for_target(const void* mem,
76 size_t length,
77 const char* target_triplet_prefix) {
78 return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix);
Nick Kledzik77595fc2008-02-26 20:26:43 +000079}
80
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000081/// lto_module_create - Loads an object file from disk. Returns NULL on error
82/// (check lto_get_error_message() for details).
83lto_module_t lto_module_create(const char* path) {
Peter Collingbournecc488542013-09-24 23:52:22 +000084 lto_initialize();
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000085 return LTOModule::makeLTOModule(path, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +000086}
87
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000088/// lto_module_create_from_fd - Loads an object file from disk. Returns NULL on
89/// error (check lto_get_error_message() for details).
90lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
Peter Collingbournecc488542013-09-24 23:52:22 +000091 lto_initialize();
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000092 return LTOModule::makeLTOModule(fd, path, size, sLastErrorString);
Rafael Espindolab4cc0312011-02-08 22:40:47 +000093}
Nick Kledzik77595fc2008-02-26 20:26:43 +000094
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000095/// lto_module_create_from_fd_at_offset - Loads an object file from disk.
96/// Returns NULL on error (check lto_get_error_message() for details).
Rafael Espindolaf21b1052011-03-17 00:36:11 +000097lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
98 size_t file_size,
99 size_t map_size,
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000100 off_t offset) {
Peter Collingbournecc488542013-09-24 23:52:22 +0000101 lto_initialize();
Rafael Espindola70c7e482013-07-23 20:25:01 +0000102 return LTOModule::makeLTOModule(fd, path, map_size, offset, sLastErrorString);
Rafael Espindolaf21b1052011-03-17 00:36:11 +0000103}
104
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000105/// lto_module_create_from_memory - Loads an object file from memory. Returns
106/// NULL on error (check lto_get_error_message() for details).
107lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
Peter Collingbournecc488542013-09-24 23:52:22 +0000108 lto_initialize();
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000109 return LTOModule::makeLTOModule(mem, length, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000110}
111
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000112/// lto_module_dispose - Frees all memory for a module. Upon return the
113/// lto_module_t is no longer valid.
114void lto_module_dispose(lto_module_t mod) {
115 delete mod;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000116}
117
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000118/// lto_module_get_target_triple - Returns triplet string which the object
119/// module was compiled under.
120const char* lto_module_get_target_triple(lto_module_t mod) {
121 return mod->getTargetTriple();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000122}
123
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000124/// lto_module_set_target_triple - Sets triple string with which the object will
125/// be codegened.
126void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
127 return mod->setTargetTriple(triple);
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000128}
129
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000130/// lto_module_get_num_symbols - Returns the number of symbols in the object
131/// module.
132unsigned int lto_module_get_num_symbols(lto_module_t mod) {
133 return mod->getSymbolCount();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000134}
135
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000136/// lto_module_get_symbol_name - Returns the name of the ith symbol in the
137/// object module.
138const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
139 return mod->getSymbolName(index);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000140}
141
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000142/// lto_module_get_symbol_attribute - Returns the attributes of the ith symbol
143/// in the object module.
144lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
145 unsigned int index) {
146 return mod->getSymbolAttributes(index);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000147}
148
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000149/// lto_codegen_create - Instantiates a code generator. Returns NULL if there
150/// is an error.
151lto_code_gen_t lto_codegen_create(void) {
Peter Collingbournecc488542013-09-24 23:52:22 +0000152 lto_initialize();
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000153 return new LTOCodeGenerator();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000154}
155
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000156/// lto_codegen_dispose - Frees all memory for a code generator. Upon return the
157/// lto_code_gen_t is no longer valid.
158void lto_codegen_dispose(lto_code_gen_t cg) {
159 delete cg;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000160}
161
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000162/// lto_codegen_add_module - Add an object module to the set of modules for
163/// which code will be generated. Returns true on error (check
164/// lto_get_error_message() for details).
165bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
Shuxin Yang235089b2013-08-07 05:19:23 +0000166 return !cg->addModule(mod, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000167}
168
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000169/// lto_codegen_set_debug_model - Sets what if any format of debug info should
170/// be generated. Returns true on error (check lto_get_error_message() for
171/// details).
172bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
Shuxin Yang235089b2013-08-07 05:19:23 +0000173 cg->setDebugInfo(debug);
174 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000175}
176
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000177/// lto_codegen_set_pic_model - Sets what code model to generated. Returns true
178/// on error (check lto_get_error_message() for details).
179bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Shuxin Yang235089b2013-08-07 05:19:23 +0000180 cg->setCodePICModel(model);
181 return false;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000182}
183
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000184/// lto_codegen_set_cpu - Sets the cpu to generate code for.
Bill Wendling168f1422012-03-31 10:44:20 +0000185void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000186 return cg->setCpu(cpu);
187}
188
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000189/// lto_codegen_set_assembler_path - Sets the path to the assembler tool.
190void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolae9efea12011-02-24 21:04:06 +0000191 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcbad5862009-06-04 00:28:45 +0000192}
193
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000194/// lto_codegen_set_assembler_args - Sets extra arguments that libLTO should
195/// pass to the assembler.
Bill Wendling168f1422012-03-31 10:44:20 +0000196void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000197 int nargs) {
Rafael Espindolae9efea12011-02-24 21:04:06 +0000198 // In here only for backwards compatibility. We use MC now.
Rafael Espindola98197e52010-08-10 18:55:09 +0000199}
200
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000201/// lto_codegen_add_must_preserve_symbol - Adds to a list of all global symbols
202/// that must exist in the final generated code. If a function is not listed
203/// there, it might be inlined into every usage and optimized away.
204void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling168f1422012-03-31 10:44:20 +0000205 const char *symbol) {
Evan Cheng855a1682009-06-26 06:57:16 +0000206 cg->addMustPreserveSymbol(symbol);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000207}
208
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000209/// lto_codegen_write_merged_modules - Writes a new file at the specified path
210/// that contains the merged contents of all modules added so far. Returns true
211/// on error (check lto_get_error_message() for details).
Bill Wendling168f1422012-03-31 10:44:20 +0000212bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Shuxin Yang235089b2013-08-07 05:19:23 +0000213 return !cg->writeMergedModules(path, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000214}
215
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000216/// lto_codegen_compile - Generates code for all added modules into one native
217/// object file. On success returns a pointer to a generated mach-o/ELF buffer
218/// and length set to the buffer size. The buffer is owned by the lto_code_gen_t
219/// object and will be freed when lto_codegen_dispose() is called, or
220/// lto_codegen_compile() is called again. On failure, returns NULL (check
221/// lto_get_error_message() for details).
Bill Wendling168f1422012-03-31 10:44:20 +0000222const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Evan Cheng855a1682009-06-26 06:57:16 +0000223 return cg->compile(length, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000224}
225
Bill Wendling168f1422012-03-31 10:44:20 +0000226/// lto_codegen_compile_to_file - Generates code for all added modules into one
227/// native object file. The name of the file is written to name. Returns true on
228/// error.
229bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Shuxin Yang235089b2013-08-07 05:19:23 +0000230 return !cg->compile_to_file(name, sLastErrorString);
Rafael Espindola6421a882011-03-22 20:57:13 +0000231}
232
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000233/// lto_codegen_debug_options - Used to pass extra options to the code
234/// generator.
Bill Wendling168f1422012-03-31 10:44:20 +0000235void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Devang Patela93ae712008-07-03 22:53:14 +0000236 cg->setCodeGenDebugOptions(opt);
Duncan Sandsd44d4bf2009-07-03 15:38:01 +0000237}