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-c/Core.h"
18 #include "LTOModule.h"
19 #include "LTOCodeGenerator.h"
22 // holds most recent error string
23 // *** not thread safe ***
24 static std::string sLastErrorString
;
29 // returns a printable string
31 extern const char* lto_get_version()
33 return LTOCodeGenerator::getVersionString();
37 // returns the last error string or NULL if last operation was successful
39 const char* lto_get_error_message()
41 return sLastErrorString
.c_str();
47 // validates if a file is a loadable object file
49 bool lto_module_is_object_file(const char* path
)
51 return LTOModule::isBitcodeFile(path
);
56 // validates if a file is a loadable object file compilable for requested target
58 bool lto_module_is_object_file_for_target(const char* path
,
59 const char* target_triplet_prefix
)
61 return LTOModule::isBitcodeFileForTarget(path
, target_triplet_prefix
);
66 // validates if a buffer is a loadable object file
68 bool lto_module_is_object_file_in_memory(const void* mem
, size_t length
)
70 return LTOModule::isBitcodeFile(mem
, length
);
75 // validates if a buffer is a loadable object file compilable for the target
77 bool lto_module_is_object_file_in_memory_for_target(const void* mem
,
78 size_t length
, const char* target_triplet_prefix
)
80 return LTOModule::isBitcodeFileForTarget(mem
, length
, target_triplet_prefix
);
86 // loads an object file from disk
87 // returns NULL on error (check lto_get_error_message() for details)
89 lto_module_t
lto_module_create(const char* path
)
91 return LTOModule::makeLTOModule(path
, sLastErrorString
);
96 // loads an object file from memory
97 // returns NULL on error (check lto_get_error_message() for details)
99 lto_module_t
lto_module_create_from_memory(const void* mem
, size_t length
)
101 return LTOModule::makeLTOModule(mem
, length
, sLastErrorString
);
106 // frees all memory for a module
107 // upon return the lto_module_t is no longer valid
109 void lto_module_dispose(lto_module_t mod
)
116 // returns triplet string which the object module was compiled under
118 const char* lto_module_get_target_triple(lto_module_t mod
)
120 return mod
->getTargetTriple();
125 // returns the number of symbols in the object module
127 uint32_t lto_module_get_num_symbols(lto_module_t mod
)
129 return mod
->getSymbolCount();
133 // returns the name of the ith symbol in the object module
135 const char* lto_module_get_symbol_name(lto_module_t mod
, uint32_t index
)
137 return mod
->getSymbolName(index
);
142 // returns the attributes of the ith symbol in the object module
144 lto_symbol_attributes
lto_module_get_symbol_attribute(lto_module_t mod
,
147 return mod
->getSymbolAttributes(index
);
155 // instantiates a code generator
156 // returns NULL if there is an error
158 lto_code_gen_t
lto_codegen_create(void)
160 return new LTOCodeGenerator();
166 // frees all memory for a code generator
167 // upon return the lto_code_gen_t is no longer valid
169 void lto_codegen_dispose(lto_code_gen_t cg
)
177 // add an object module to the set of modules for which code will be generated
178 // returns true on error (check lto_get_error_message() for details)
180 bool lto_codegen_add_module(lto_code_gen_t cg
, lto_module_t mod
)
182 return cg
->addModule(mod
, sLastErrorString
);
187 // sets what if any format of debug info should be generated
188 // returns true on error (check lto_get_error_message() for details)
190 bool lto_codegen_set_debug_model(lto_code_gen_t cg
, lto_debug_model debug
)
192 return cg
->setDebugInfo(debug
, sLastErrorString
);
197 // sets what code model to generated
198 // returns true on error (check lto_get_error_message() for details)
200 bool lto_codegen_set_pic_model(lto_code_gen_t cg
, lto_codegen_model model
)
202 return cg
->setCodePICModel(model
, sLastErrorString
);
206 // sets the path to the assembler tool
208 void lto_codegen_set_assembler_path(lto_code_gen_t cg
, const char* path
)
210 cg
->setAssemblerPath(path
);
214 // adds to a list of all global symbols that must exist in the final
215 // generated code. If a function is not listed there, it might be
216 // inlined into every usage and optimized away.
218 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg
, const char* symbol
)
220 cg
->addMustPreserveSymbol(symbol
);
225 // writes a new file at the specified path that contains the
226 // merged contents of all modules added so far.
227 // returns true on error (check lto_get_error_message() for details)
229 bool lto_codegen_write_merged_modules(lto_code_gen_t cg
, const char* path
)
231 return cg
->writeMergedModules(path
, sLastErrorString
);
236 // Generates code for all added modules into one native object file.
237 // On sucess returns a pointer to a generated mach-o/ELF buffer and
238 // length set to the buffer size. The buffer is owned by the
239 // lto_code_gen_t and will be freed when lto_codegen_dispose()
240 // is called, or lto_codegen_compile() is called again.
241 // On failure, returns NULL (check lto_get_error_message() for details).
244 lto_codegen_compile(lto_code_gen_t cg
, size_t* length
)
246 return cg
->compile(length
, sLastErrorString
);
251 // Used to pass extra options to the code generator
254 lto_codegen_debug_options(lto_code_gen_t cg
, const char * opt
)
256 cg
->setCodeGenDebugOptions(opt
);