blob: 648279834fa71d496ceb2099c84632cf1216aa78 [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
94
95//
96// loads an object file from memory
97// returns NULL on error (check lto_get_error_message() for details)
98//
Owen Anderson0e7a5462009-07-02 00:31:14 +000099lto_module_t lto_module_create_from_memory(const void* mem, size_t length)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000100{
Owen Anderson0e7a5462009-07-02 00:31:14 +0000101 return LTOModule::makeLTOModule(mem, length, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000102}
103
104
105//
106// frees all memory for a module
107// upon return the lto_module_t is no longer valid
108//
109void lto_module_dispose(lto_module_t mod)
110{
111 delete mod;
112}
113
114
115//
116// returns triplet string which the object module was compiled under
117//
118const char* lto_module_get_target_triple(lto_module_t mod)
119{
120 return mod->getTargetTriple();
121}
122
123
124//
125// returns the number of symbols in the object module
126//
127uint32_t lto_module_get_num_symbols(lto_module_t mod)
128{
129 return mod->getSymbolCount();
130}
131
132//
133// returns the name of the ith symbol in the object module
134//
135const char* lto_module_get_symbol_name(lto_module_t mod, uint32_t index)
136{
137 return mod->getSymbolName(index);
138}
139
140
141//
142// returns the attributes of the ith symbol in the object module
143//
144lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
145 uint32_t index)
146{
147 return mod->getSymbolAttributes(index);
148}
149
150
151
152
153
154//
155// instantiates a code generator
156// returns NULL if there is an error
157//
Owen Anderson0e7a5462009-07-02 00:31:14 +0000158lto_code_gen_t lto_codegen_create(void)
Nick Kledzik77595fc2008-02-26 20:26:43 +0000159{
Owen Anderson0e7a5462009-07-02 00:31:14 +0000160 return new LTOCodeGenerator();
Nick Kledzik77595fc2008-02-26 20:26:43 +0000161}
162
163
164
165//
166// frees all memory for a code generator
167// upon return the lto_code_gen_t is no longer valid
168//
169void lto_codegen_dispose(lto_code_gen_t cg)
170{
171 delete cg;
172}
173
174
175
176//
177// add an object module to the set of modules for which code will be generated
178// returns true on error (check lto_get_error_message() for details)
179//
180bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod)
181{
182 return cg->addModule(mod, sLastErrorString);
183}
184
185
186//
187// sets what if any format of debug info should be generated
188// returns true on error (check lto_get_error_message() for details)
189//
190bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug)
191{
192 return cg->setDebugInfo(debug, sLastErrorString);
193}
194
195
196//
197// sets what code model to generated
198// returns true on error (check lto_get_error_message() for details)
199//
200bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model)
201{
Evan Cheng855a1682009-06-26 06:57:16 +0000202 return cg->setCodePICModel(model, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000203}
204
205//
Nick Lewycky195bea32009-04-30 15:24:09 +0000206// sets the path to gcc
207//
208void lto_codegen_set_gcc_path(lto_code_gen_t cg, const char* path)
209{
Evan Cheng855a1682009-06-26 06:57:16 +0000210 cg->setGccPath(path);
Nick Lewycky195bea32009-04-30 15:24:09 +0000211}
212
213//
Nick Kledzikcbad5862009-06-04 00:28:45 +0000214// sets the path to the assembler tool
215//
216void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char* path)
217{
218 cg->setAssemblerPath(path);
219}
220
221//
Nick Kledzik77595fc2008-02-26 20:26:43 +0000222// adds to a list of all global symbols that must exist in the final
223// generated code. If a function is not listed there, it might be
224// inlined into every usage and optimized away.
225//
226void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const char* symbol)
227{
Evan Cheng855a1682009-06-26 06:57:16 +0000228 cg->addMustPreserveSymbol(symbol);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000229}
230
231
232//
233// writes a new file at the specified path that contains the
234// merged contents of all modules added so far.
235// returns true on error (check lto_get_error_message() for details)
236//
237bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path)
238{
Evan Cheng855a1682009-06-26 06:57:16 +0000239 return cg->writeMergedModules(path, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000240}
241
242
243//
Nick Kledzikef194ed2008-02-27 22:25:36 +0000244// Generates code for all added modules into one native object file.
245// On sucess returns a pointer to a generated mach-o/ELF buffer and
246// length set to the buffer size. The buffer is owned by the
247// lto_code_gen_t and will be freed when lto_codegen_dispose()
248// is called, or lto_codegen_compile() is called again.
249// On failure, returns NULL (check lto_get_error_message() for details).
Nick Kledzik77595fc2008-02-26 20:26:43 +0000250//
Nick Kledzikef194ed2008-02-27 22:25:36 +0000251extern const void*
Nick Kledzik77595fc2008-02-26 20:26:43 +0000252lto_codegen_compile(lto_code_gen_t cg, size_t* length)
253{
Evan Cheng855a1682009-06-26 06:57:16 +0000254 return cg->compile(length, sLastErrorString);
Nick Kledzik77595fc2008-02-26 20:26:43 +0000255}
256
Nick Kledzik920ae982008-07-08 21:14:10 +0000257
258//
259// Used to pass extra options to the code generator
260//
Devang Patela93ae712008-07-03 22:53:14 +0000261extern void
262lto_codegen_debug_options(lto_code_gen_t cg, const char * opt)
263{
264 cg->setCodeGenDebugOptions(opt);
Owen Anderson31895e72009-07-01 21:22:36 +0000265}