blob: a7e633d14b9dc264957bc471273bda004a0a6e6f [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"
Owen Anderson8b477ed2009-07-01 16:58:40 +000016#include "llvm-c/Core.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000017
18#include "LTOModule.h"
19#include "LTOCodeGenerator.h"
20
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
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000026/// lto_get_version - Returns a printable string.
27extern const char* lto_get_version() {
28 return LTOCodeGenerator::getVersionString();
Nick Kledzik77595fc2008-02-26 20:26:43 +000029}
30
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000031/// lto_get_error_message - Returns the last error string or NULL if last
32/// operation was successful.
33const char* lto_get_error_message() {
34 return sLastErrorString.c_str();
Nick Kledzik77595fc2008-02-26 20:26:43 +000035}
36
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000037/// lto_module_is_object_file - Validates if a file is a loadable object file.
38bool lto_module_is_object_file(const char* path) {
39 return LTOModule::isBitcodeFile(path);
Nick Kledzik77595fc2008-02-26 20:26:43 +000040}
41
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000042/// lto_module_is_object_file_for_target - Validates if a file is a loadable
43/// object file compilable for requested target.
44bool lto_module_is_object_file_for_target(const char* path,
45 const char* target_triplet_prefix) {
46 return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix);
Nick Kledzik77595fc2008-02-26 20:26:43 +000047}
48
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000049/// lto_module_is_object_file_in_memory - Validates if a buffer is a loadable
50/// object file.
51bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
52 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik77595fc2008-02-26 20:26:43 +000053}
54
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000055/// lto_module_is_object_file_in_memory_for_target - Validates if a buffer is a
56/// loadable object file compilable for the target.
57bool
58lto_module_is_object_file_in_memory_for_target(const void* mem,
59 size_t length,
60 const char* target_triplet_prefix) {
61 return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix);
Nick Kledzik77595fc2008-02-26 20:26:43 +000062}
63
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000064/// lto_module_create - Loads an object file from disk. Returns NULL on error
65/// (check lto_get_error_message() for details).
66lto_module_t lto_module_create(const char* path) {
67 return LTOModule::makeLTOModule(path, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +000068}
69
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000070/// lto_module_create_from_fd - Loads an object file from disk. Returns NULL on
71/// error (check lto_get_error_message() for details).
72lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
73 return LTOModule::makeLTOModule(fd, path, size, sLastErrorString);
Rafael Espindolab4cc0312011-02-08 22:40:47 +000074}
Nick Kledzik77595fc2008-02-26 20:26:43 +000075
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000076/// lto_module_create_from_fd_at_offset - Loads an object file from disk.
77/// Returns NULL on error (check lto_get_error_message() for details).
Rafael Espindolaf21b1052011-03-17 00:36:11 +000078lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
79 size_t file_size,
80 size_t map_size,
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000081 off_t offset) {
82 return LTOModule::makeLTOModule(fd, path, file_size, map_size,
83 offset, sLastErrorString);
Rafael Espindolaf21b1052011-03-17 00:36:11 +000084}
85
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000086/// lto_module_create_from_memory - Loads an object file from memory. Returns
87/// NULL on error (check lto_get_error_message() for details).
88lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
89 return LTOModule::makeLTOModule(mem, length, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +000090}
91
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000092/// lto_module_dispose - Frees all memory for a module. Upon return the
93/// lto_module_t is no longer valid.
94void lto_module_dispose(lto_module_t mod) {
95 delete mod;
Nick Kledzik77595fc2008-02-26 20:26:43 +000096}
97
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000098/// lto_module_get_target_triple - Returns triplet string which the object
99/// module was compiled under.
100const char* lto_module_get_target_triple(lto_module_t mod) {
101 return mod->getTargetTriple();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000102}
103
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000104/// lto_module_set_target_triple - Sets triple string with which the object will
105/// be codegened.
106void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
107 return mod->setTargetTriple(triple);
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000108}
109
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000110/// lto_module_get_num_symbols - Returns the number of symbols in the object
111/// module.
112unsigned int lto_module_get_num_symbols(lto_module_t mod) {
113 return mod->getSymbolCount();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000114}
115
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000116/// lto_module_get_symbol_name - Returns the name of the ith symbol in the
117/// object module.
118const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
119 return mod->getSymbolName(index);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000120}
121
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000122/// lto_module_get_symbol_attribute - Returns the attributes of the ith symbol
123/// in the object module.
124lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
125 unsigned int index) {
126 return mod->getSymbolAttributes(index);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000127}
128
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000129/// lto_codegen_create - Instantiates a code generator. Returns NULL if there
130/// is an error.
131lto_code_gen_t lto_codegen_create(void) {
132 return new LTOCodeGenerator();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000133}
134
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000135/// lto_codegen_dispose - Frees all memory for a code generator. Upon return the
136/// lto_code_gen_t is no longer valid.
137void lto_codegen_dispose(lto_code_gen_t cg) {
138 delete cg;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000139}
140
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000141/// lto_codegen_add_module - Add an object module to the set of modules for
142/// which code will be generated. Returns true on error (check
143/// lto_get_error_message() for details).
144bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
145 return cg->addModule(mod, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000146}
147
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000148/// lto_codegen_set_debug_model - Sets what if any format of debug info should
149/// be generated. Returns true on error (check lto_get_error_message() for
150/// details).
151bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
152 return cg->setDebugInfo(debug, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000153}
154
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000155/// lto_codegen_set_pic_model - Sets what code model to generated. Returns true
156/// on error (check lto_get_error_message() for details).
157bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Evan Cheng855a1682009-06-26 06:57:16 +0000158 return cg->setCodePICModel(model, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000159}
160
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000161/// lto_codegen_set_cpu - Sets the cpu to generate code for.
Bill Wendling168f1422012-03-31 10:44:20 +0000162void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000163 return cg->setCpu(cpu);
164}
165
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000166/// lto_codegen_set_assembler_path - Sets the path to the assembler tool.
167void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolae9efea12011-02-24 21:04:06 +0000168 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcbad5862009-06-04 00:28:45 +0000169}
170
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000171/// lto_codegen_set_assembler_args - Sets extra arguments that libLTO should
172/// pass to the assembler.
Bill Wendling168f1422012-03-31 10:44:20 +0000173void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000174 int nargs) {
Rafael Espindolae9efea12011-02-24 21:04:06 +0000175 // In here only for backwards compatibility. We use MC now.
Rafael Espindola98197e52010-08-10 18:55:09 +0000176}
177
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000178/// lto_codegen_add_must_preserve_symbol - Adds to a list of all global symbols
179/// that must exist in the final generated code. If a function is not listed
180/// there, it might be inlined into every usage and optimized away.
181void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling168f1422012-03-31 10:44:20 +0000182 const char *symbol) {
Evan Cheng855a1682009-06-26 06:57:16 +0000183 cg->addMustPreserveSymbol(symbol);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000184}
185
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000186/// lto_codegen_write_merged_modules - Writes a new file at the specified path
187/// that contains the merged contents of all modules added so far. Returns true
188/// on error (check lto_get_error_message() for details).
Bill Wendling168f1422012-03-31 10:44:20 +0000189bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Evan Cheng855a1682009-06-26 06:57:16 +0000190 return cg->writeMergedModules(path, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000191}
192
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000193/// lto_codegen_compile - Generates code for all added modules into one native
194/// object file. On success returns a pointer to a generated mach-o/ELF buffer
195/// and length set to the buffer size. The buffer is owned by the lto_code_gen_t
196/// object and will be freed when lto_codegen_dispose() is called, or
197/// lto_codegen_compile() is called again. On failure, returns NULL (check
198/// lto_get_error_message() for details).
Bill Wendling168f1422012-03-31 10:44:20 +0000199const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Evan Cheng855a1682009-06-26 06:57:16 +0000200 return cg->compile(length, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000201}
202
Bill Wendling168f1422012-03-31 10:44:20 +0000203/// lto_codegen_compile_to_file - Generates code for all added modules into one
204/// native object file. The name of the file is written to name. Returns true on
205/// error.
206bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000207 return cg->compile_to_file(name, sLastErrorString);
208}
209
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000210/// lto_codegen_debug_options - Used to pass extra options to the code
211/// generator.
Bill Wendling168f1422012-03-31 10:44:20 +0000212void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Devang Patela93ae712008-07-03 22:53:14 +0000213 cg->setCodeGenDebugOptions(opt);
Duncan Sandsd44d4bf2009-07-03 15:38:01 +0000214}