lto.cpp revision 70c7e485453fdbc228406715556f9447bc9f9fd8
1//===-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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Link Time Optimization library. This library is
11// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm-c/lto.h"
16#include "LTOCodeGenerator.h"
17#include "LTOModule.h"
18#include "llvm-c/Core.h"
19
20
21// Holds most recent error string.
22// *** Not thread safe ***
23static std::string sLastErrorString;
24
25/// lto_get_version - Returns a printable string.
26extern const char* lto_get_version() {
27  return LTOCodeGenerator::getVersionString();
28}
29
30/// 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();
34}
35
36/// 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);
39}
40
41/// 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);
46}
47
48/// 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);
52}
53
54/// 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);
61}
62
63/// 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);
67}
68
69/// 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);
73}
74
75/// 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).
77lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
78                                                 size_t file_size,
79                                                 size_t map_size,
80                                                 off_t offset) {
81  return LTOModule::makeLTOModule(fd, path, map_size, offset, sLastErrorString);
82}
83
84/// lto_module_create_from_memory - Loads an object file from memory. Returns
85/// NULL on error (check lto_get_error_message() for details).
86lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
87  return LTOModule::makeLTOModule(mem, length, sLastErrorString);
88}
89
90/// lto_module_dispose - Frees all memory for a module. Upon return the
91/// lto_module_t is no longer valid.
92void lto_module_dispose(lto_module_t mod) {
93  delete mod;
94}
95
96/// lto_module_get_target_triple - Returns triplet string which the object
97/// module was compiled under.
98const char* lto_module_get_target_triple(lto_module_t mod) {
99  return mod->getTargetTriple();
100}
101
102/// lto_module_set_target_triple - Sets triple string with which the object will
103/// be codegened.
104void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
105  return mod->setTargetTriple(triple);
106}
107
108/// lto_module_get_num_symbols - Returns the number of symbols in the object
109/// module.
110unsigned int lto_module_get_num_symbols(lto_module_t mod) {
111  return mod->getSymbolCount();
112}
113
114/// lto_module_get_symbol_name - Returns the name of the ith symbol in the
115/// object module.
116const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
117  return mod->getSymbolName(index);
118}
119
120/// lto_module_get_symbol_attribute - Returns the attributes of the ith symbol
121/// in the object module.
122lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
123                                                      unsigned int index) {
124  return mod->getSymbolAttributes(index);
125}
126
127/// lto_codegen_create - Instantiates a code generator. Returns NULL if there
128/// is an error.
129lto_code_gen_t lto_codegen_create(void) {
130  return new LTOCodeGenerator();
131}
132
133/// lto_codegen_dispose - Frees all memory for a code generator. Upon return the
134/// lto_code_gen_t is no longer valid.
135void lto_codegen_dispose(lto_code_gen_t cg) {
136  delete cg;
137}
138
139/// lto_codegen_add_module - Add an object module to the set of modules for
140/// which code will be generated. Returns true on error (check
141/// lto_get_error_message() for details).
142bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
143  return cg->addModule(mod, sLastErrorString);
144}
145
146/// lto_codegen_set_debug_model - Sets what if any format of debug info should
147/// be generated. Returns true on error (check lto_get_error_message() for
148/// details).
149bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
150  return cg->setDebugInfo(debug, sLastErrorString);
151}
152
153/// lto_codegen_set_pic_model - Sets what code model to generated. Returns true
154/// on error (check lto_get_error_message() for details).
155bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
156  return cg->setCodePICModel(model, sLastErrorString);
157}
158
159/// lto_codegen_set_cpu - Sets the cpu to generate code for.
160void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
161  return cg->setCpu(cpu);
162}
163
164/// lto_codegen_set_assembler_path - Sets the path to the assembler tool.
165void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
166  // In here only for backwards compatibility. We use MC now.
167}
168
169/// lto_codegen_set_assembler_args - Sets extra arguments that libLTO should
170/// pass to the assembler.
171void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
172                                    int nargs) {
173  // In here only for backwards compatibility. We use MC now.
174}
175
176/// lto_codegen_add_must_preserve_symbol - Adds to a list of all global symbols
177/// that must exist in the final generated code. If a function is not listed
178/// there, it might be inlined into every usage and optimized away.
179void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
180                                          const char *symbol) {
181  cg->addMustPreserveSymbol(symbol);
182}
183
184/// lto_codegen_write_merged_modules - Writes a new file at the specified path
185/// that contains the merged contents of all modules added so far. Returns true
186/// on error (check lto_get_error_message() for details).
187bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
188  return cg->writeMergedModules(path, sLastErrorString);
189}
190
191/// lto_codegen_compile - Generates code for all added modules into one native
192/// object file. On success returns a pointer to a generated mach-o/ELF buffer
193/// and length set to the buffer size. The buffer is owned by the lto_code_gen_t
194/// object and will be freed when lto_codegen_dispose() is called, or
195/// lto_codegen_compile() is called again. On failure, returns NULL (check
196/// lto_get_error_message() for details).
197const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
198  return cg->compile(length, sLastErrorString);
199}
200
201/// lto_codegen_compile_to_file - Generates code for all added modules into one
202/// native object file. The name of the file is written to name. Returns true on
203/// error.
204bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
205  return cg->compile_to_file(name, sLastErrorString);
206}
207
208/// lto_codegen_debug_options - Used to pass extra options to the code
209/// generator.
210void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
211  cg->setCodeGenDebugOptions(opt);
212}
213