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"
17 #include "LTOModule.h"
18 #include "LTOCodeGenerator.h"
21 // holds most recent error string
22 // *** not thread safe ***
23 static std::string sLastErrorString
;
28 // returns a printable string
30 extern const char* lto_get_version()
32 return LTOCodeGenerator::getVersionString();
36 // returns the last error string or NULL if last operation was successful
38 const char* lto_get_error_message()
40 return sLastErrorString
.c_str();
46 // validates if a file is a loadable object file
48 bool lto_module_is_object_file(const char* path
)
50 return LTOModule::isBitcodeFile(path
);
55 // validates if a file is a loadable object file compilable for requested target
57 bool lto_module_is_object_file_for_target(const char* path
,
58 const char* target_triplet_prefix
)
60 return LTOModule::isBitcodeFileForTarget(path
, target_triplet_prefix
);
65 // validates if a buffer is a loadable object file
67 bool lto_module_is_object_file_in_memory(const void* mem
, size_t length
)
69 return LTOModule::isBitcodeFile(mem
, length
);
74 // validates if a buffer is a loadable object file compilable for the target
76 bool lto_module_is_object_file_in_memory_for_target(const void* mem
,
77 size_t length
, const char* target_triplet_prefix
)
79 return LTOModule::isBitcodeFileForTarget(mem
, length
, target_triplet_prefix
);
85 // loads an object file from disk
86 // returns NULL on error (check lto_get_error_message() for details)
88 lto_module_t
lto_module_create(const char* path
)
90 return LTOModule::makeLTOModule(path
, sLastErrorString
);
95 // loads an object file from memory
96 // returns NULL on error (check lto_get_error_message() for details)
98 lto_module_t
lto_module_create_from_memory(const void* mem
, size_t length
)
100 return LTOModule::makeLTOModule(mem
, length
, sLastErrorString
);
105 // frees all memory for a module
106 // upon return the lto_module_t is no longer valid
108 void lto_module_dispose(lto_module_t mod
)
115 // returns triplet string which the object module was compiled under
117 const char* lto_module_get_target_triple(lto_module_t mod
)
119 return mod
->getTargetTriple();
124 // returns the number of symbols in the object module
126 uint32_t lto_module_get_num_symbols(lto_module_t mod
)
128 return mod
->getSymbolCount();
132 // returns the name of the ith symbol in the object module
134 const char* lto_module_get_symbol_name(lto_module_t mod
, uint32_t index
)
136 return mod
->getSymbolName(index
);
141 // returns the attributes of the ith symbol in the object module
143 lto_symbol_attributes
lto_module_get_symbol_attribute(lto_module_t mod
,
146 return mod
->getSymbolAttributes(index
);
154 // instantiates a code generator
155 // returns NULL if there is an error
157 lto_code_gen_t
lto_codegen_create()
159 return new LTOCodeGenerator();
165 // frees all memory for a code generator
166 // upon return the lto_code_gen_t is no longer valid
168 void lto_codegen_dispose(lto_code_gen_t cg
)
176 // add an object module to the set of modules for which code will be generated
177 // returns true on error (check lto_get_error_message() for details)
179 bool lto_codegen_add_module(lto_code_gen_t cg
, lto_module_t mod
)
181 return cg
->addModule(mod
, sLastErrorString
);
186 // sets what if any format of debug info should be generated
187 // returns true on error (check lto_get_error_message() for details)
189 bool lto_codegen_set_debug_model(lto_code_gen_t cg
, lto_debug_model debug
)
191 return cg
->setDebugInfo(debug
, sLastErrorString
);
196 // sets what code model to generated
197 // returns true on error (check lto_get_error_message() for details)
199 bool lto_codegen_set_pic_model(lto_code_gen_t cg
, lto_codegen_model model
)
201 return cg
->setCodePICModel(model
, sLastErrorString
);
205 // adds to a list of all global symbols that must exist in the final
206 // generated code. If a function is not listed there, it might be
207 // inlined into every usage and optimized away.
209 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg
, const char* symbol
)
211 cg
->addMustPreserveSymbol(symbol
);
216 // writes a new file at the specified path that contains the
217 // merged contents of all modules added so far.
218 // returns true on error (check lto_get_error_message() for details)
220 bool lto_codegen_write_merged_modules(lto_code_gen_t cg
, const char* path
)
222 return cg
->writeMergedModules(path
, sLastErrorString
);
227 // Generates code for all added modules into one native object file.
228 // On sucess returns a pointer to a generated mach-o/ELF buffer and
229 // length set to the buffer size. The buffer is owned by the
230 // lto_code_gen_t and will be freed when lto_codegen_dispose()
231 // is called, or lto_codegen_compile() is called again.
232 // On failure, returns NULL (check lto_get_error_message() for details).
235 lto_codegen_compile(lto_code_gen_t cg
, size_t* length
)
237 return cg
->compile(length
, sLastErrorString
);
242 // Used to pass extra options to the code generator
245 lto_codegen_debug_options(lto_code_gen_t cg
, const char * opt
)
247 cg
->setCodeGenDebugOptions(opt
);