blob: f48570c1495dc4a83607fb8006bb49d1b4dd205c [file] [log] [blame]
Nick Kledzik77595fc2008-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.
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"
Owen Anderson8b477ed2009-07-01 16:58:40 +000016#include "llvm-c/Core.h"
Nick Kledzik77595fc2008-02-26 20:26:43 +000017
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//
Nick Kledzikef194ed2008-02-27 22:25:36 +000037// returns the last error string or NULL if last operation was successful
Nick Kledzik77595fc2008-02-26 20:26:43 +000038//
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//
Owen Anderson0e7a5462009-07-02 00:31:14 +000089lto_module_t lto_module_create(const char* path)
Nick Kledzik77595fc2008-02-26 20:26:43 +000090{
Owen Anderson0e7a5462009-07-02 00:31:14 +000091 return LTOModule::makeLTOModule(path, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +000092}
93
Rafael Espindolab4cc0312011-02-08 22:40:47 +000094//
95// loads an object file from disk
96// returns NULL on error (check lto_get_error_message() for details)
97//
98lto_module_t lto_module_create_from_fd(int fd, const char *path, off_t size)
99{
100 return LTOModule::makeLTOModule(fd, path, size, sLastErrorString);
101}
Nick Kledzik77595fc2008-02-26 20:26:43 +0000102
103//
104// loads an object file from memory
105// returns NULL on error (check lto_get_error_message() for details)
106//
Owen Anderson0e7a5462009-07-02 00:31:14 +0000107lto_module_t lto_module_create_from_memory(const void* mem, size_t length)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000108{
Owen Anderson0e7a5462009-07-02 00:31:14 +0000109 return LTOModule::makeLTOModule(mem, length, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000110}
111
112
113//
114// frees all memory for a module
115// upon return the lto_module_t is no longer valid
116//
117void lto_module_dispose(lto_module_t mod)
118{
119 delete mod;
120}
121
122
123//
124// returns triplet string which the object module was compiled under
125//
126const char* lto_module_get_target_triple(lto_module_t mod)
127{
128 return mod->getTargetTriple();
129}
130
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000131//
132// sets triple string with which the object will be codegened.
133//
134void lto_module_set_target_triple(lto_module_t mod, const char *triple)
135{
136 return mod->setTargetTriple(triple);
137}
138
Nick Kledzik77595fc2008-02-26 20:26:43 +0000139
140//
141// returns the number of symbols in the object module
142//
Devang Patel6a6623c2011-01-07 22:26:25 +0000143unsigned int lto_module_get_num_symbols(lto_module_t mod)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000144{
145 return mod->getSymbolCount();
146}
147
148//
149// returns the name of the ith symbol in the object module
150//
Devang Patel6a6623c2011-01-07 22:26:25 +0000151const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000152{
153 return mod->getSymbolName(index);
154}
155
156
157//
158// returns the attributes of the ith symbol in the object module
159//
160lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
Devang Patel6a6623c2011-01-07 22:26:25 +0000161 unsigned int index)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000162{
163 return mod->getSymbolAttributes(index);
164}
165
166
167
168
169
170//
171// instantiates a code generator
172// returns NULL if there is an error
173//
Owen Anderson0e7a5462009-07-02 00:31:14 +0000174lto_code_gen_t lto_codegen_create(void)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000175{
Owen Anderson0e7a5462009-07-02 00:31:14 +0000176 return new LTOCodeGenerator();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000177}
178
179
180
181//
182// frees all memory for a code generator
183// upon return the lto_code_gen_t is no longer valid
184//
185void lto_codegen_dispose(lto_code_gen_t cg)
186{
187 delete cg;
188}
189
190
191
192//
193// add an object module to the set of modules for which code will be generated
194// returns true on error (check lto_get_error_message() for details)
195//
196bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod)
197{
198 return cg->addModule(mod, sLastErrorString);
199}
200
201
202//
203// sets what if any format of debug info should be generated
204// returns true on error (check lto_get_error_message() for details)
205//
206bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug)
207{
208 return cg->setDebugInfo(debug, sLastErrorString);
209}
210
211
212//
213// sets what code model to generated
214// returns true on error (check lto_get_error_message() for details)
215//
216bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model)
217{
Evan Cheng855a1682009-06-26 06:57:16 +0000218 return cg->setCodePICModel(model, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000219}
220
221//
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000222// sets the cpu to generate code for
223//
224void lto_codegen_set_cpu(lto_code_gen_t cg, const char* cpu)
225{
226 return cg->setCpu(cpu);
227}
228
229//
Nick Kledzikcbad5862009-06-04 00:28:45 +0000230// sets the path to the assembler tool
231//
232void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char* path)
233{
Rafael Espindolae9efea12011-02-24 21:04:06 +0000234 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcbad5862009-06-04 00:28:45 +0000235}
236
Rafael Espindola98197e52010-08-10 18:55:09 +0000237
238//
239// sets extra arguments that libLTO should pass to the assembler
240//
241void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char** args,
242 int nargs)
243{
Rafael Espindolae9efea12011-02-24 21:04:06 +0000244 // In here only for backwards compatibility. We use MC now.
Rafael Espindola98197e52010-08-10 18:55:09 +0000245}
246
Nick Kledzikcbad5862009-06-04 00:28:45 +0000247//
Nick Kledzik77595fc2008-02-26 20:26:43 +0000248// adds to a list of all global symbols that must exist in the final
249// generated code. If a function is not listed there, it might be
250// inlined into every usage and optimized away.
251//
252void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const char* symbol)
253{
Evan Cheng855a1682009-06-26 06:57:16 +0000254 cg->addMustPreserveSymbol(symbol);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000255}
256
257
258//
259// writes a new file at the specified path that contains the
260// merged contents of all modules added so far.
261// returns true on error (check lto_get_error_message() for details)
262//
263bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path)
264{
Evan Cheng855a1682009-06-26 06:57:16 +0000265 return cg->writeMergedModules(path, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000266}
267
268
269//
Nick Kledzikef194ed2008-02-27 22:25:36 +0000270// Generates code for all added modules into one native object file.
271// On sucess returns a pointer to a generated mach-o/ELF buffer and
272// length set to the buffer size. The buffer is owned by the
273// lto_code_gen_t and will be freed when lto_codegen_dispose()
274// is called, or lto_codegen_compile() is called again.
275// On failure, returns NULL (check lto_get_error_message() for details).
Nick Kledzik77595fc2008-02-26 20:26:43 +0000276//
Nick Kledzikef194ed2008-02-27 22:25:36 +0000277extern const void*
Nick Kledzik77595fc2008-02-26 20:26:43 +0000278lto_codegen_compile(lto_code_gen_t cg, size_t* length)
279{
Evan Cheng855a1682009-06-26 06:57:16 +0000280 return cg->compile(length, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000281}
282
Nick Kledzik920ae982008-07-08 21:14:10 +0000283
284//
285// Used to pass extra options to the code generator
286//
Devang Patela93ae712008-07-03 22:53:14 +0000287extern void
288lto_codegen_debug_options(lto_code_gen_t cg, const char * opt)
289{
290 cg->setCodeGenDebugOptions(opt);
Duncan Sandsd44d4bf2009-07-03 15:38:01 +0000291}