Home | History | Annotate | Download | only in lto
      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 ***
     24 static std::string sLastErrorString;
     25 
     26 
     27 
     28 //
     29 // returns a printable string
     30 //
     31 extern 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 //
     39 const 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 //
     49 bool 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 //
     58 bool 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 //
     68 bool 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 //
     77 bool 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 //
     89 lto_module_t lto_module_create(const char* path)
     90 {
     91      return LTOModule::makeLTOModule(path, sLastErrorString);
     92 }
     93 
     94 //
     95 // loads an object file from disk
     96 // returns NULL on error (check lto_get_error_message() for details)
     97 //
     98 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size)
     99 {
    100      return LTOModule::makeLTOModule(fd, path, size, sLastErrorString);
    101 }
    102 
    103 //
    104 // loads an object file from disk
    105 // returns NULL on error (check lto_get_error_message() for details)
    106 //
    107 lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
    108                                                  size_t file_size,
    109                                                  size_t map_size,
    110                                                  off_t offset)
    111 {
    112      return LTOModule::makeLTOModule(fd, path, file_size, map_size,
    113                                      offset, sLastErrorString);
    114 }
    115 
    116 //
    117 // loads an object file from memory
    118 // returns NULL on error (check lto_get_error_message() for details)
    119 //
    120 lto_module_t lto_module_create_from_memory(const void* mem, size_t length)
    121 {
    122      return LTOModule::makeLTOModule(mem, length, sLastErrorString);
    123 }
    124 
    125 
    126 //
    127 // frees all memory for a module
    128 // upon return the lto_module_t is no longer valid
    129 //
    130 void lto_module_dispose(lto_module_t mod)
    131 {
    132     delete mod;
    133 }
    134 
    135 
    136 //
    137 // returns triplet string which the object module was compiled under
    138 //
    139 const char* lto_module_get_target_triple(lto_module_t mod)
    140 {
    141     return mod->getTargetTriple();
    142 }
    143 
    144 //
    145 // sets triple string with which the object will be codegened.
    146 //
    147 void lto_module_set_target_triple(lto_module_t mod, const char *triple)
    148 {
    149     return mod->setTargetTriple(triple);
    150 }
    151 
    152 
    153 //
    154 // returns the number of symbols in the object module
    155 //
    156 unsigned int lto_module_get_num_symbols(lto_module_t mod)
    157 {
    158     return mod->getSymbolCount();
    159 }
    160 
    161 //
    162 // returns the name of the ith symbol in the object module
    163 //
    164 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index)
    165 {
    166     return mod->getSymbolName(index);
    167 }
    168 
    169 
    170 //
    171 // returns the attributes of the ith symbol in the object module
    172 //
    173 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
    174                                                       unsigned int index)
    175 {
    176     return mod->getSymbolAttributes(index);
    177 }
    178 
    179 
    180 
    181 
    182 
    183 //
    184 // instantiates a code generator
    185 // returns NULL if there is an error
    186 //
    187 lto_code_gen_t lto_codegen_create(void)
    188 {
    189      return new LTOCodeGenerator();
    190 }
    191 
    192 
    193 
    194 //
    195 // frees all memory for a code generator
    196 // upon return the lto_code_gen_t is no longer valid
    197 //
    198 void lto_codegen_dispose(lto_code_gen_t cg)
    199 {
    200     delete cg;
    201 }
    202 
    203 
    204 
    205 //
    206 // add an object module to the set of modules for which code will be generated
    207 // returns true on error (check lto_get_error_message() for details)
    208 //
    209 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod)
    210 {
    211     return cg->addModule(mod, sLastErrorString);
    212 }
    213 
    214 
    215 //
    216 // sets what if any format of debug info should be generated
    217 // returns true on error (check lto_get_error_message() for details)
    218 //
    219 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug)
    220 {
    221     return cg->setDebugInfo(debug, sLastErrorString);
    222 }
    223 
    224 
    225 //
    226 // sets what code model to generated
    227 // returns true on error (check lto_get_error_message() for details)
    228 //
    229 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model)
    230 {
    231   return cg->setCodePICModel(model, sLastErrorString);
    232 }
    233 
    234 //
    235 // sets the cpu to generate code for
    236 //
    237 void lto_codegen_set_cpu(lto_code_gen_t cg, const char* cpu)
    238 {
    239   return cg->setCpu(cpu);
    240 }
    241 
    242 //
    243 // sets the path to the assembler tool
    244 //
    245 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char* path)
    246 {
    247   // In here only for backwards compatibility. We use MC now.
    248 }
    249 
    250 
    251 //
    252 // sets extra arguments that libLTO should pass to the assembler
    253 //
    254 void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char** args,
    255                                     int nargs)
    256 {
    257   // In here only for backwards compatibility. We use MC now.
    258 }
    259 
    260 //
    261 // adds to a list of all global symbols that must exist in the final
    262 // generated code.  If a function is not listed there, it might be
    263 // inlined into every usage and optimized away.
    264 //
    265 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const char* symbol)
    266 {
    267   cg->addMustPreserveSymbol(symbol);
    268 }
    269 
    270 
    271 //
    272 // writes a new file at the specified path that contains the
    273 // merged contents of all modules added so far.
    274 // returns true on error (check lto_get_error_message() for details)
    275 //
    276 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path)
    277 {
    278   return cg->writeMergedModules(path, sLastErrorString);
    279 }
    280 
    281 
    282 //
    283 // Generates code for all added modules into one native object file.
    284 // On success returns a pointer to a generated mach-o/ELF buffer and
    285 // length set to the buffer size.  The buffer is owned by the
    286 // lto_code_gen_t and will be freed when lto_codegen_dispose()
    287 // is called, or lto_codegen_compile() is called again.
    288 // On failure, returns NULL (check lto_get_error_message() for details).
    289 //
    290 extern const void*
    291 lto_codegen_compile(lto_code_gen_t cg, size_t* length)
    292 {
    293   return cg->compile(length, sLastErrorString);
    294 }
    295 
    296 extern bool
    297 lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name)
    298 {
    299   return cg->compile_to_file(name, sLastErrorString);
    300 }
    301 
    302 
    303 //
    304 // Used to pass extra options to the code generator
    305 //
    306 extern void
    307 lto_codegen_debug_options(lto_code_gen_t cg, const char * opt)
    308 {
    309   cg->setCodeGenDebugOptions(opt);
    310 }
    311