blob: db7147c2bc7e43ee9ca89395e1cb1aa750c139ce [file] [log] [blame]
Nick Kledzik07b4a622008-02-26 20:26:43 +00001//===-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.
Bill Wendling36cbf032012-03-30 10:29:38 +00007//
Nick Kledzik07b4a622008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Bill Wendling36cbf032012-03-30 10:29:38 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik07b4a622008-02-26 20:26:43 +000011// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm-c/lto.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000016#include "LTOCodeGenerator.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000017#include "LTOModule.h"
18#include "llvm-c/Core.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000019
20
Bill Wendling36cbf032012-03-30 10:29:38 +000021// Holds most recent error string.
22// *** Not thread safe ***
Nick Kledzik07b4a622008-02-26 20:26:43 +000023static std::string sLastErrorString;
24
Bill Wendling36cbf032012-03-30 10:29:38 +000025/// lto_get_version - Returns a printable string.
26extern const char* lto_get_version() {
27 return LTOCodeGenerator::getVersionString();
Nick Kledzik07b4a622008-02-26 20:26:43 +000028}
29
Bill Wendling36cbf032012-03-30 10:29:38 +000030/// lto_get_error_message - Returns the last error string or NULL if last
31/// operation was successful.
32const char* lto_get_error_message() {
33 return sLastErrorString.c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +000034}
35
Bill Wendling36cbf032012-03-30 10:29:38 +000036/// lto_module_is_object_file - Validates if a file is a loadable object file.
37bool lto_module_is_object_file(const char* path) {
38 return LTOModule::isBitcodeFile(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +000039}
40
Bill Wendling36cbf032012-03-30 10:29:38 +000041/// lto_module_is_object_file_for_target - Validates if a file is a loadable
42/// object file compilable for requested target.
43bool lto_module_is_object_file_for_target(const char* path,
44 const char* target_triplet_prefix) {
45 return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +000046}
47
Bill Wendling36cbf032012-03-30 10:29:38 +000048/// lto_module_is_object_file_in_memory - Validates if a buffer is a loadable
49/// object file.
50bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
51 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik07b4a622008-02-26 20:26:43 +000052}
53
Bill Wendling36cbf032012-03-30 10:29:38 +000054/// lto_module_is_object_file_in_memory_for_target - Validates if a buffer is a
55/// loadable object file compilable for the target.
56bool
57lto_module_is_object_file_in_memory_for_target(const void* mem,
58 size_t length,
59 const char* target_triplet_prefix) {
60 return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +000061}
62
Bill Wendling36cbf032012-03-30 10:29:38 +000063/// lto_module_create - Loads an object file from disk. Returns NULL on error
64/// (check lto_get_error_message() for details).
65lto_module_t lto_module_create(const char* path) {
66 return LTOModule::makeLTOModule(path, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +000067}
68
Bill Wendling36cbf032012-03-30 10:29:38 +000069/// lto_module_create_from_fd - Loads an object file from disk. Returns NULL on
70/// error (check lto_get_error_message() for details).
71lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
72 return LTOModule::makeLTOModule(fd, path, size, sLastErrorString);
Rafael Espindola56e41f72011-02-08 22:40:47 +000073}
Nick Kledzik07b4a622008-02-26 20:26:43 +000074
Bill Wendling36cbf032012-03-30 10:29:38 +000075/// lto_module_create_from_fd_at_offset - Loads an object file from disk.
76/// Returns NULL on error (check lto_get_error_message() for details).
Rafael Espindolab39c7c72011-03-17 00:36:11 +000077lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
78 size_t file_size,
79 size_t map_size,
Bill Wendling36cbf032012-03-30 10:29:38 +000080 off_t offset) {
Rafael Espindola3d2ac2e2013-07-23 20:25:01 +000081 return LTOModule::makeLTOModule(fd, path, map_size, offset, sLastErrorString);
Rafael Espindolab39c7c72011-03-17 00:36:11 +000082}
83
Bill Wendling36cbf032012-03-30 10:29:38 +000084/// lto_module_create_from_memory - Loads an object file from memory. Returns
85/// NULL on error (check lto_get_error_message() for details).
86lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
87 return LTOModule::makeLTOModule(mem, length, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +000088}
89
Bill Wendling36cbf032012-03-30 10:29:38 +000090/// lto_module_dispose - Frees all memory for a module. Upon return the
91/// lto_module_t is no longer valid.
92void lto_module_dispose(lto_module_t mod) {
93 delete mod;
Nick Kledzik07b4a622008-02-26 20:26:43 +000094}
95
Bill Wendling36cbf032012-03-30 10:29:38 +000096/// lto_module_get_target_triple - Returns triplet string which the object
97/// module was compiled under.
98const char* lto_module_get_target_triple(lto_module_t mod) {
99 return mod->getTargetTriple();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000100}
101
Bill Wendling36cbf032012-03-30 10:29:38 +0000102/// lto_module_set_target_triple - Sets triple string with which the object will
103/// be codegened.
104void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
105 return mod->setTargetTriple(triple);
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000106}
107
Bill Wendling36cbf032012-03-30 10:29:38 +0000108/// lto_module_get_num_symbols - Returns the number of symbols in the object
109/// module.
110unsigned int lto_module_get_num_symbols(lto_module_t mod) {
111 return mod->getSymbolCount();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000112}
113
Bill Wendling36cbf032012-03-30 10:29:38 +0000114/// lto_module_get_symbol_name - Returns the name of the ith symbol in the
115/// object module.
116const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
117 return mod->getSymbolName(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000118}
119
Bill Wendling36cbf032012-03-30 10:29:38 +0000120/// lto_module_get_symbol_attribute - Returns the attributes of the ith symbol
121/// in the object module.
122lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
123 unsigned int index) {
124 return mod->getSymbolAttributes(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000125}
126
Bill Wendling36cbf032012-03-30 10:29:38 +0000127/// lto_codegen_create - Instantiates a code generator. Returns NULL if there
128/// is an error.
129lto_code_gen_t lto_codegen_create(void) {
130 return new LTOCodeGenerator();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000131}
132
Bill Wendling36cbf032012-03-30 10:29:38 +0000133/// lto_codegen_dispose - Frees all memory for a code generator. Upon return the
134/// lto_code_gen_t is no longer valid.
135void lto_codegen_dispose(lto_code_gen_t cg) {
136 delete cg;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000137}
138
Bill Wendling36cbf032012-03-30 10:29:38 +0000139/// lto_codegen_add_module - Add an object module to the set of modules for
140/// which code will be generated. Returns true on error (check
141/// lto_get_error_message() for details).
142bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
Shuxin Yangb6696a92013-08-07 05:19:23 +0000143 return !cg->addModule(mod, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000144}
145
Bill Wendling36cbf032012-03-30 10:29:38 +0000146/// lto_codegen_set_debug_model - Sets what if any format of debug info should
147/// be generated. Returns true on error (check lto_get_error_message() for
148/// details).
149bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
Shuxin Yangb6696a92013-08-07 05:19:23 +0000150 cg->setDebugInfo(debug);
151 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000152}
153
Bill Wendling36cbf032012-03-30 10:29:38 +0000154/// lto_codegen_set_pic_model - Sets what code model to generated. Returns true
155/// on error (check lto_get_error_message() for details).
156bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Shuxin Yangb6696a92013-08-07 05:19:23 +0000157 cg->setCodePICModel(model);
158 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000159}
160
Bill Wendling36cbf032012-03-30 10:29:38 +0000161/// lto_codegen_set_cpu - Sets the cpu to generate code for.
Bill Wendling152e4732012-03-31 10:44:20 +0000162void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000163 return cg->setCpu(cpu);
164}
165
Bill Wendling36cbf032012-03-30 10:29:38 +0000166/// lto_codegen_set_assembler_path - Sets the path to the assembler tool.
167void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000168 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcac8c8a2009-06-04 00:28:45 +0000169}
170
Bill Wendling36cbf032012-03-30 10:29:38 +0000171/// lto_codegen_set_assembler_args - Sets extra arguments that libLTO should
172/// pass to the assembler.
Bill Wendling152e4732012-03-31 10:44:20 +0000173void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling36cbf032012-03-30 10:29:38 +0000174 int nargs) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000175 // In here only for backwards compatibility. We use MC now.
Rafael Espindola00456462010-08-10 18:55:09 +0000176}
177
Bill Wendling36cbf032012-03-30 10:29:38 +0000178/// lto_codegen_add_must_preserve_symbol - Adds to a list of all global symbols
179/// that must exist in the final generated code. If a function is not listed
180/// there, it might be inlined into every usage and optimized away.
181void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling152e4732012-03-31 10:44:20 +0000182 const char *symbol) {
Evan Chengd6460962009-06-26 06:57:16 +0000183 cg->addMustPreserveSymbol(symbol);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000184}
185
Bill Wendling36cbf032012-03-30 10:29:38 +0000186/// lto_codegen_write_merged_modules - Writes a new file at the specified path
187/// that contains the merged contents of all modules added so far. Returns true
188/// on error (check lto_get_error_message() for details).
Bill Wendling152e4732012-03-31 10:44:20 +0000189bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Shuxin Yangb6696a92013-08-07 05:19:23 +0000190 return !cg->writeMergedModules(path, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000191}
192
Bill Wendling36cbf032012-03-30 10:29:38 +0000193/// lto_codegen_compile - Generates code for all added modules into one native
194/// object file. On success returns a pointer to a generated mach-o/ELF buffer
195/// and length set to the buffer size. The buffer is owned by the lto_code_gen_t
196/// object and will be freed when lto_codegen_dispose() is called, or
197/// lto_codegen_compile() is called again. On failure, returns NULL (check
198/// lto_get_error_message() for details).
Bill Wendling152e4732012-03-31 10:44:20 +0000199const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Evan Chengd6460962009-06-26 06:57:16 +0000200 return cg->compile(length, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000201}
202
Bill Wendling152e4732012-03-31 10:44:20 +0000203/// lto_codegen_compile_to_file - Generates code for all added modules into one
204/// native object file. The name of the file is written to name. Returns true on
205/// error.
206bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Shuxin Yangb6696a92013-08-07 05:19:23 +0000207 return !cg->compile_to_file(name, sLastErrorString);
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000208}
209
Bill Wendling36cbf032012-03-30 10:29:38 +0000210/// lto_codegen_debug_options - Used to pass extra options to the code
211/// generator.
Bill Wendling152e4732012-03-31 10:44:20 +0000212void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Devang Patela0e4fb82008-07-03 22:53:14 +0000213 cg->setCodeGenDebugOptions(opt);
Duncan Sands31554ab2009-07-03 15:38:01 +0000214}