lto.cpp revision 8b477ed579794ba6d76915d56b3f448a7dd20120
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 "llvm-c/Core.h" 17 18#include "LTOModule.h" 19#include "LTOCodeGenerator.h" 20 21 22// holds most recent error string 23// *** not thread safe *** 24static std::string sLastErrorString; 25 26 27 28// 29// returns a printable string 30// 31extern const char* lto_get_version() 32{ 33 return LTOCodeGenerator::getVersionString(); 34} 35 36// 37// returns the last error string or NULL if last operation was successful 38// 39const char* lto_get_error_message() 40{ 41 return sLastErrorString.c_str(); 42} 43 44 45 46// 47// validates if a file is a loadable object file 48// 49bool lto_module_is_object_file(const char* path) 50{ 51 return LTOModule::isBitcodeFile(path); 52} 53 54 55// 56// validates if a file is a loadable object file compilable for requested target 57// 58bool lto_module_is_object_file_for_target(const char* path, 59 const char* target_triplet_prefix) 60{ 61 return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix); 62} 63 64 65// 66// validates if a buffer is a loadable object file 67// 68bool lto_module_is_object_file_in_memory(const void* mem, size_t length) 69{ 70 return LTOModule::isBitcodeFile(mem, length); 71} 72 73 74// 75// validates if a buffer is a loadable object file compilable for the target 76// 77bool lto_module_is_object_file_in_memory_for_target(const void* mem, 78 size_t length, const char* target_triplet_prefix) 79{ 80 return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix); 81} 82 83 84 85// 86// loads an object file from disk 87// returns NULL on error (check lto_get_error_message() for details) 88// 89lto_module_t lto_module_create(const char* path, LLVMContextRef Ctxt) 90{ 91 return LTOModule::makeLTOModule(path, llvm::unwrap(Ctxt), 92 sLastErrorString); 93} 94 95 96// 97// loads an object file from memory 98// returns NULL on error (check lto_get_error_message() for details) 99// 100lto_module_t lto_module_create_from_memory(const void* mem, size_t length, 101 LLVMContextRef Ctxt) 102{ 103 return LTOModule::makeLTOModule(mem, length, llvm::unwrap(Ctxt), 104 sLastErrorString); 105} 106 107 108// 109// frees all memory for a module 110// upon return the lto_module_t is no longer valid 111// 112void lto_module_dispose(lto_module_t mod) 113{ 114 delete mod; 115} 116 117 118// 119// returns triplet string which the object module was compiled under 120// 121const char* lto_module_get_target_triple(lto_module_t mod) 122{ 123 return mod->getTargetTriple(); 124} 125 126 127// 128// returns the number of symbols in the object module 129// 130uint32_t lto_module_get_num_symbols(lto_module_t mod) 131{ 132 return mod->getSymbolCount(); 133} 134 135// 136// returns the name of the ith symbol in the object module 137// 138const char* lto_module_get_symbol_name(lto_module_t mod, uint32_t index) 139{ 140 return mod->getSymbolName(index); 141} 142 143 144// 145// returns the attributes of the ith symbol in the object module 146// 147lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod, 148 uint32_t index) 149{ 150 return mod->getSymbolAttributes(index); 151} 152 153 154 155 156 157// 158// instantiates a code generator 159// returns NULL if there is an error 160// 161lto_code_gen_t lto_codegen_create() 162{ 163 return new LTOCodeGenerator(); 164} 165 166 167 168// 169// frees all memory for a code generator 170// upon return the lto_code_gen_t is no longer valid 171// 172void lto_codegen_dispose(lto_code_gen_t cg) 173{ 174 delete cg; 175} 176 177 178 179// 180// add an object module to the set of modules for which code will be generated 181// returns true on error (check lto_get_error_message() for details) 182// 183bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) 184{ 185 return cg->addModule(mod, sLastErrorString); 186} 187 188 189// 190// sets what if any format of debug info should be generated 191// returns true on error (check lto_get_error_message() for details) 192// 193bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) 194{ 195 return cg->setDebugInfo(debug, sLastErrorString); 196} 197 198 199// 200// sets what code model to generated 201// returns true on error (check lto_get_error_message() for details) 202// 203bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) 204{ 205 return cg->setCodePICModel(model, sLastErrorString); 206} 207 208// 209// sets the path to gcc 210// 211void lto_codegen_set_gcc_path(lto_code_gen_t cg, const char* path) 212{ 213 cg->setGccPath(path); 214} 215 216// 217// sets the path to the assembler tool 218// 219void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char* path) 220{ 221 cg->setAssemblerPath(path); 222} 223 224// 225// adds to a list of all global symbols that must exist in the final 226// generated code. If a function is not listed there, it might be 227// inlined into every usage and optimized away. 228// 229void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const char* symbol) 230{ 231 cg->addMustPreserveSymbol(symbol); 232} 233 234 235// 236// writes a new file at the specified path that contains the 237// merged contents of all modules added so far. 238// returns true on error (check lto_get_error_message() for details) 239// 240bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path) 241{ 242 return cg->writeMergedModules(path, sLastErrorString); 243} 244 245 246// 247// Generates code for all added modules into one native object file. 248// On sucess returns a pointer to a generated mach-o/ELF buffer and 249// length set to the buffer size. The buffer is owned by the 250// lto_code_gen_t and will be freed when lto_codegen_dispose() 251// is called, or lto_codegen_compile() is called again. 252// On failure, returns NULL (check lto_get_error_message() for details). 253// 254extern const void* 255lto_codegen_compile(lto_code_gen_t cg, size_t* length) 256{ 257 return cg->compile(length, sLastErrorString); 258} 259 260 261// 262// Used to pass extra options to the code generator 263// 264extern void 265lto_codegen_debug_options(lto_code_gen_t cg, const char * opt) 266{ 267 cg->setCodeGenDebugOptions(opt); 268} 269