1 //===-lto.cpp - LLVM Link Time Optimizer ----------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm-c/lto.h"
16 #include "llvm/LTO/LTOCodeGenerator.h"
17 #include "llvm/LTO/LTOModule.h"
18 #include "llvm-c/Core.h"
19 #include "llvm-c/Target.h"
22 // Holds most recent error string.
23 // *** Not thread safe ***
24 static std::string sLastErrorString;
26 // Holds the initialization state of the LTO module.
27 // *** Not thread safe ***
28 static bool initialized = false;
30 // Initialize the configured targets if they have not been initialized.
31 static void lto_initialize() {
33 LLVMInitializeAllTargetInfos();
34 LLVMInitializeAllTargets();
35 LLVMInitializeAllTargetMCs();
36 LLVMInitializeAllAsmParsers();
37 LLVMInitializeAllAsmPrinters();
38 LLVMInitializeAllDisassemblers();
43 /// lto_get_version - Returns a printable string.
44 extern const char* lto_get_version() {
45 return LTOCodeGenerator::getVersionString();
48 /// lto_get_error_message - Returns the last error string or NULL if last
49 /// operation was successful.
50 const char* lto_get_error_message() {
51 return sLastErrorString.c_str();
54 /// lto_module_is_object_file - Validates if a file is a loadable object file.
55 bool lto_module_is_object_file(const char* path) {
56 return LTOModule::isBitcodeFile(path);
59 /// lto_module_is_object_file_for_target - Validates if a file is a loadable
60 /// object file compilable for requested target.
61 bool lto_module_is_object_file_for_target(const char* path,
62 const char* target_triplet_prefix) {
63 return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix);
66 /// lto_module_is_object_file_in_memory - Validates if a buffer is a loadable
68 bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
69 return LTOModule::isBitcodeFile(mem, length);
72 /// lto_module_is_object_file_in_memory_for_target - Validates if a buffer is a
73 /// loadable object file compilable for the target.
75 lto_module_is_object_file_in_memory_for_target(const void* mem,
77 const char* target_triplet_prefix) {
78 return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix);
81 /// lto_module_create - Loads an object file from disk. Returns NULL on error
82 /// (check lto_get_error_message() for details).
83 lto_module_t lto_module_create(const char* path) {
85 return LTOModule::makeLTOModule(path, sLastErrorString);
88 /// lto_module_create_from_fd - Loads an object file from disk. Returns NULL on
89 /// error (check lto_get_error_message() for details).
90 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
92 return LTOModule::makeLTOModule(fd, path, size, sLastErrorString);
95 /// 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).
97 lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
102 return LTOModule::makeLTOModule(fd, path, map_size, offset, sLastErrorString);
105 /// lto_module_create_from_memory - Loads an object file from memory. Returns
106 /// NULL on error (check lto_get_error_message() for details).
107 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
109 return LTOModule::makeLTOModule(mem, length, sLastErrorString);
112 /// lto_module_dispose - Frees all memory for a module. Upon return the
113 /// lto_module_t is no longer valid.
114 void lto_module_dispose(lto_module_t mod) {
118 /// lto_module_get_target_triple - Returns triplet string which the object
119 /// module was compiled under.
120 const char* lto_module_get_target_triple(lto_module_t mod) {
121 return mod->getTargetTriple();
124 /// lto_module_set_target_triple - Sets triple string with which the object will
126 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
127 return mod->setTargetTriple(triple);
130 /// lto_module_get_num_symbols - Returns the number of symbols in the object
132 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
133 return mod->getSymbolCount();
136 /// lto_module_get_symbol_name - Returns the name of the ith symbol in the
138 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
139 return mod->getSymbolName(index);
142 /// lto_module_get_symbol_attribute - Returns the attributes of the ith symbol
143 /// in the object module.
144 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
145 unsigned int index) {
146 return mod->getSymbolAttributes(index);
149 /// lto_codegen_create - Instantiates a code generator. Returns NULL if there
151 lto_code_gen_t lto_codegen_create(void) {
153 return new LTOCodeGenerator();
156 /// lto_codegen_dispose - Frees all memory for a code generator. Upon return the
157 /// lto_code_gen_t is no longer valid.
158 void lto_codegen_dispose(lto_code_gen_t cg) {
162 /// 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).
165 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
166 return !cg->addModule(mod, sLastErrorString);
169 /// 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
172 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
173 cg->setDebugInfo(debug);
177 /// lto_codegen_set_pic_model - Sets what code model to generated. Returns true
178 /// on error (check lto_get_error_message() for details).
179 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
180 cg->setCodePICModel(model);
184 /// lto_codegen_set_cpu - Sets the cpu to generate code for.
185 void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
186 return cg->setCpu(cpu);
189 /// lto_codegen_set_assembler_path - Sets the path to the assembler tool.
190 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
191 // In here only for backwards compatibility. We use MC now.
194 /// lto_codegen_set_assembler_args - Sets extra arguments that libLTO should
195 /// pass to the assembler.
196 void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
198 // In here only for backwards compatibility. We use MC now.
201 /// 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.
204 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
205 const char *symbol) {
206 cg->addMustPreserveSymbol(symbol);
209 /// 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).
212 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
213 return !cg->writeMergedModules(path, sLastErrorString);
216 /// 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).
222 const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
223 return cg->compile(length, sLastErrorString);
226 /// 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
229 bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
230 return !cg->compile_to_file(name, sLastErrorString);
233 /// lto_codegen_debug_options - Used to pass extra options to the code
235 void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
236 cg->setCodeGenDebugOptions(opt);