blob: cbac047c752e70572240ddd423178cae5be54a00 [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//
Rafael Espindolaf21b1052011-03-17 00:36:11 +000098lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size)
Rafael Espindolab4cc0312011-02-08 22:40:47 +000099{
100 return LTOModule::makeLTOModule(fd, path, size, sLastErrorString);
101}
Nick Kledzik77595fc2008-02-26 20:26:43 +0000102
103//
Rafael Espindolaf21b1052011-03-17 00:36:11 +0000104// loads an object file from disk
105// returns NULL on error (check lto_get_error_message() for details)
106//
107lto_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//
Nick Kledzik77595fc2008-02-26 20:26:43 +0000117// loads an object file from memory
118// returns NULL on error (check lto_get_error_message() for details)
119//
Owen Anderson0e7a5462009-07-02 00:31:14 +0000120lto_module_t lto_module_create_from_memory(const void* mem, size_t length)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000121{
Owen Anderson0e7a5462009-07-02 00:31:14 +0000122 return LTOModule::makeLTOModule(mem, length, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000123}
124
125
126//
127// frees all memory for a module
128// upon return the lto_module_t is no longer valid
129//
130void 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//
139const char* lto_module_get_target_triple(lto_module_t mod)
140{
141 return mod->getTargetTriple();
142}
143
Rafael Espindolacbb170d2010-08-09 21:09:46 +0000144//
145// sets triple string with which the object will be codegened.
146//
147void lto_module_set_target_triple(lto_module_t mod, const char *triple)
148{
149 return mod->setTargetTriple(triple);
150}
151
Nick Kledzik77595fc2008-02-26 20:26:43 +0000152
153//
154// returns the number of symbols in the object module
155//
Devang Patel6a6623c2011-01-07 22:26:25 +0000156unsigned int lto_module_get_num_symbols(lto_module_t mod)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000157{
158 return mod->getSymbolCount();
159}
160
161//
162// returns the name of the ith symbol in the object module
163//
Devang Patel6a6623c2011-01-07 22:26:25 +0000164const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000165{
166 return mod->getSymbolName(index);
167}
168
169
170//
171// returns the attributes of the ith symbol in the object module
172//
173lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
Devang Patel6a6623c2011-01-07 22:26:25 +0000174 unsigned int index)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000175{
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//
Owen Anderson0e7a5462009-07-02 00:31:14 +0000187lto_code_gen_t lto_codegen_create(void)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000188{
Owen Anderson0e7a5462009-07-02 00:31:14 +0000189 return new LTOCodeGenerator();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000190}
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//
198void 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//
209bool 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//
219bool 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//
229bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model)
230{
Evan Cheng855a1682009-06-26 06:57:16 +0000231 return cg->setCodePICModel(model, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000232}
233
234//
Rafael Espindola2d643ef2010-08-11 00:15:13 +0000235// sets the cpu to generate code for
236//
237void lto_codegen_set_cpu(lto_code_gen_t cg, const char* cpu)
238{
239 return cg->setCpu(cpu);
240}
241
242//
Nick Kledzikcbad5862009-06-04 00:28:45 +0000243// sets the path to the assembler tool
244//
245void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char* path)
246{
Rafael Espindolae9efea12011-02-24 21:04:06 +0000247 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcbad5862009-06-04 00:28:45 +0000248}
249
Rafael Espindola98197e52010-08-10 18:55:09 +0000250
251//
252// sets extra arguments that libLTO should pass to the assembler
253//
254void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char** args,
255 int nargs)
256{
Rafael Espindolae9efea12011-02-24 21:04:06 +0000257 // In here only for backwards compatibility. We use MC now.
Rafael Espindola98197e52010-08-10 18:55:09 +0000258}
259
Nick Kledzikcbad5862009-06-04 00:28:45 +0000260//
Nick Kledzik77595fc2008-02-26 20:26:43 +0000261// 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//
265void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const char* symbol)
266{
Evan Cheng855a1682009-06-26 06:57:16 +0000267 cg->addMustPreserveSymbol(symbol);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000268}
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//
276bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path)
277{
Evan Cheng855a1682009-06-26 06:57:16 +0000278 return cg->writeMergedModules(path, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000279}
280
281
282//
Nick Kledzikef194ed2008-02-27 22:25:36 +0000283// Generates code for all added modules into one native object file.
284// On sucess 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).
Nick Kledzik77595fc2008-02-26 20:26:43 +0000289//
Nick Kledzikef194ed2008-02-27 22:25:36 +0000290extern const void*
Nick Kledzik77595fc2008-02-26 20:26:43 +0000291lto_codegen_compile(lto_code_gen_t cg, size_t* length)
292{
Evan Cheng855a1682009-06-26 06:57:16 +0000293 return cg->compile(length, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000294}
295
Nick Kledzik920ae982008-07-08 21:14:10 +0000296
297//
298// Used to pass extra options to the code generator
299//
Devang Patela93ae712008-07-03 22:53:14 +0000300extern void
301lto_codegen_debug_options(lto_code_gen_t cg, const char * opt)
302{
303 cg->setCodeGenDebugOptions(opt);
Duncan Sandsd44d4bf2009-07-03 15:38:01 +0000304}