blob: 11ad532be896c2f6a77b782f3d006e29c0d66152 [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"
Nick Kledzik77595fc2008-02-26 20:26:43 +000016#include "LTOCodeGenerator.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000017#include "LTOModule.h"
18#include "llvm-c/Core.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000019
20
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000021// Holds most recent error string.
22// *** Not thread safe ***
Nick Kledzik77595fc2008-02-26 20:26:43 +000023static std::string sLastErrorString;
24
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000025/// lto_get_version - Returns a printable string.
26extern const char* lto_get_version() {
27 return LTOCodeGenerator::getVersionString();
Nick Kledzik77595fc2008-02-26 20:26:43 +000028}
29
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000030/// lto_get_error_message - Returns the last error string or NULL if last
31/// operation was successful.
32const char* lto_get_error_message() {
33 return sLastErrorString.c_str();
Nick Kledzik77595fc2008-02-26 20:26:43 +000034}
35
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000036/// lto_module_is_object_file - Validates if a file is a loadable object file.
37bool lto_module_is_object_file(const char* path) {
38 return LTOModule::isBitcodeFile(path);
Nick Kledzik77595fc2008-02-26 20:26:43 +000039}
40
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000041/// lto_module_is_object_file_for_target - Validates if a file is a loadable
42/// object file compilable for requested target.
43bool lto_module_is_object_file_for_target(const char* path,
44 const char* target_triplet_prefix) {
45 return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix);
Nick Kledzik77595fc2008-02-26 20:26:43 +000046}
47
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000048/// lto_module_is_object_file_in_memory - Validates if a buffer is a loadable
49/// object file.
50bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
51 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik77595fc2008-02-26 20:26:43 +000052}
53
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000054/// lto_module_is_object_file_in_memory_for_target - Validates if a buffer is a
55/// loadable object file compilable for the target.
56bool
57lto_module_is_object_file_in_memory_for_target(const void* mem,
58 size_t length,
59 const char* target_triplet_prefix) {
60 return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix);
Nick Kledzik77595fc2008-02-26 20:26:43 +000061}
62
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000063/// lto_module_create - Loads an object file from disk. Returns NULL on error
64/// (check lto_get_error_message() for details).
65lto_module_t lto_module_create(const char* path) {
66 return LTOModule::makeLTOModule(path, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +000067}
68
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000069/// lto_module_create_from_fd - Loads an object file from disk. Returns NULL on
70/// error (check lto_get_error_message() for details).
71lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
72 return LTOModule::makeLTOModule(fd, path, size, sLastErrorString);
Rafael Espindolab4cc0312011-02-08 22:40:47 +000073}
Nick Kledzik77595fc2008-02-26 20:26:43 +000074
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000075/// lto_module_create_from_fd_at_offset - Loads an object file from disk.
76/// Returns NULL on error (check lto_get_error_message() for details).
Rafael Espindolaf21b1052011-03-17 00:36:11 +000077lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
78 size_t file_size,
79 size_t map_size,
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000080 off_t offset) {
81 return LTOModule::makeLTOModule(fd, path, file_size, map_size,
82 offset, sLastErrorString);
Rafael Espindolaf21b1052011-03-17 00:36:11 +000083}
84
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000085/// lto_module_create_from_memory - Loads an object file from memory. Returns
86/// NULL on error (check lto_get_error_message() for details).
87lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
88 return LTOModule::makeLTOModule(mem, length, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +000089}
90
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000091/// lto_module_dispose - Frees all memory for a module. Upon return the
92/// lto_module_t is no longer valid.
93void lto_module_dispose(lto_module_t mod) {
94 delete mod;
Nick Kledzik77595fc2008-02-26 20:26:43 +000095}
96
Bill Wendling8fd3fcd2012-03-30 10:29:38 +000097/// lto_module_get_target_triple - Returns triplet string which the object
98/// module was compiled under.
99const char* lto_module_get_target_triple(lto_module_t mod) {
100 return mod->getTargetTriple();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000101}
102
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000103/// lto_module_set_target_triple - Sets triple string with which the object will
104/// be codegened.
105void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
106 return mod->setTargetTriple(triple);
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000107}
108
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000109/// lto_module_get_num_symbols - Returns the number of symbols in the object
110/// module.
111unsigned int lto_module_get_num_symbols(lto_module_t mod) {
112 return mod->getSymbolCount();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000113}
114
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000115/// lto_module_get_symbol_name - Returns the name of the ith symbol in the
116/// object module.
117const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
118 return mod->getSymbolName(index);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000119}
120
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000121/// lto_module_get_symbol_attribute - Returns the attributes of the ith symbol
122/// in the object module.
123lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
124 unsigned int index) {
125 return mod->getSymbolAttributes(index);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000126}
127
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000128/// lto_codegen_create - Instantiates a code generator. Returns NULL if there
129/// is an error.
130lto_code_gen_t lto_codegen_create(void) {
131 return new LTOCodeGenerator();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000132}
133
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000134/// lto_codegen_dispose - Frees all memory for a code generator. Upon return the
135/// lto_code_gen_t is no longer valid.
136void lto_codegen_dispose(lto_code_gen_t cg) {
137 delete cg;
Nick Kledzik77595fc2008-02-26 20:26:43 +0000138}
139
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000140/// lto_codegen_add_module - Add an object module to the set of modules for
141/// which code will be generated. Returns true on error (check
142/// lto_get_error_message() for details).
143bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
144 return cg->addModule(mod, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000145}
146
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000147/// lto_codegen_set_debug_model - Sets what if any format of debug info should
148/// be generated. Returns true on error (check lto_get_error_message() for
149/// details).
150bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
151 return cg->setDebugInfo(debug, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000152}
153
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000154/// lto_codegen_set_pic_model - Sets what code model to generated. Returns true
155/// on error (check lto_get_error_message() for details).
156bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Evan Cheng855a1682009-06-26 06:57:16 +0000157 return cg->setCodePICModel(model, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000158}
159
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000160/// lto_codegen_set_cpu - Sets the cpu to generate code for.
Bill Wendling168f1422012-03-31 10:44:20 +0000161void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000162 return cg->setCpu(cpu);
163}
164
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000165/// lto_codegen_set_assembler_path - Sets the path to the assembler tool.
166void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolae9efea12011-02-24 21:04:06 +0000167 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcbad5862009-06-04 00:28:45 +0000168}
169
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000170/// lto_codegen_set_assembler_args - Sets extra arguments that libLTO should
171/// pass to the assembler.
Bill Wendling168f1422012-03-31 10:44:20 +0000172void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000173 int nargs) {
Rafael Espindolae9efea12011-02-24 21:04:06 +0000174 // In here only for backwards compatibility. We use MC now.
Rafael Espindola98197e52010-08-10 18:55:09 +0000175}
176
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000177/// lto_codegen_add_must_preserve_symbol - Adds to a list of all global symbols
178/// that must exist in the final generated code. If a function is not listed
179/// there, it might be inlined into every usage and optimized away.
180void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling168f1422012-03-31 10:44:20 +0000181 const char *symbol) {
Evan Cheng855a1682009-06-26 06:57:16 +0000182 cg->addMustPreserveSymbol(symbol);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000183}
184
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000185/// lto_codegen_write_merged_modules - Writes a new file at the specified path
186/// that contains the merged contents of all modules added so far. Returns true
187/// on error (check lto_get_error_message() for details).
Bill Wendling168f1422012-03-31 10:44:20 +0000188bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Evan Cheng855a1682009-06-26 06:57:16 +0000189 return cg->writeMergedModules(path, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000190}
191
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000192/// lto_codegen_compile - Generates code for all added modules into one native
193/// object file. On success returns a pointer to a generated mach-o/ELF buffer
194/// and length set to the buffer size. The buffer is owned by the lto_code_gen_t
195/// object and will be freed when lto_codegen_dispose() is called, or
196/// lto_codegen_compile() is called again. On failure, returns NULL (check
197/// lto_get_error_message() for details).
Bill Wendling168f1422012-03-31 10:44:20 +0000198const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Evan Cheng855a1682009-06-26 06:57:16 +0000199 return cg->compile(length, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000200}
201
Bill Wendling168f1422012-03-31 10:44:20 +0000202/// lto_codegen_compile_to_file - Generates code for all added modules into one
203/// native object file. The name of the file is written to name. Returns true on
204/// error.
205bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Rafael Espindola6421a882011-03-22 20:57:13 +0000206 return cg->compile_to_file(name, sLastErrorString);
207}
208
Bill Wendling8fd3fcd2012-03-30 10:29:38 +0000209/// lto_codegen_debug_options - Used to pass extra options to the code
210/// generator.
Bill Wendling168f1422012-03-31 10:44:20 +0000211void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Devang Patela93ae712008-07-03 22:53:14 +0000212 cg->setCodeGenDebugOptions(opt);
Duncan Sandsd44d4bf2009-07-03 15:38:01 +0000213}