Refactor TargetOptions initialization into a single place.
[oota-llvm.git] / tools / lto / lto.cpp
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 #include "llvm-c/Target.h"
18 #include "llvm/CodeGen/CommandFlags.h"
19 #include "llvm/LTO/LTOCodeGenerator.h"
20 #include "llvm/LTO/LTOModule.h"
21
22 // extra command-line flags needed for LTOCodeGenerator
23 static cl::opt<bool>
24 DisableOpt("disable-opt", cl::init(false),
25   cl::desc("Do not run any optimization passes"));
26
27 static cl::opt<bool>
28 DisableInline("disable-inlining", cl::init(false),
29   cl::desc("Do not run the inliner pass"));
30
31 static cl::opt<bool>
32 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
33   cl::desc("Do not run the GVN load PRE pass"));
34
35 // Holds most recent error string.
36 // *** Not thread safe ***
37 static std::string sLastErrorString;
38
39 // Holds the initialization state of the LTO module.
40 // *** Not thread safe ***
41 static bool initialized = false;
42
43 // Holds the command-line option parsing state of the LTO module.
44 static bool parsedOptions = false;
45
46 // Initialize the configured targets if they have not been initialized.
47 static void lto_initialize() {
48   if (!initialized) {
49     LLVMInitializeAllTargetInfos();
50     LLVMInitializeAllTargets();
51     LLVMInitializeAllTargetMCs();
52     LLVMInitializeAllAsmParsers();
53     LLVMInitializeAllAsmPrinters();
54     LLVMInitializeAllDisassemblers();
55     initialized = true;
56   }
57 }
58
59 /// lto_get_version - Returns a printable string.
60 extern const char* lto_get_version() {
61   return LTOCodeGenerator::getVersionString();
62 }
63
64 /// lto_get_error_message - Returns the last error string or NULL if last
65 /// operation was successful.
66 const char* lto_get_error_message() {
67   return sLastErrorString.c_str();
68 }
69
70 /// lto_module_is_object_file - Validates if a file is a loadable object file.
71 bool lto_module_is_object_file(const char* path) {
72   return LTOModule::isBitcodeFile(path);
73 }
74
75 /// lto_module_is_object_file_for_target - Validates if a file is a loadable
76 /// object file compilable for requested target.
77 bool lto_module_is_object_file_for_target(const char* path,
78                                           const char* target_triplet_prefix) {
79   return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix);
80 }
81
82 /// lto_module_is_object_file_in_memory - Validates if a buffer is a loadable
83 /// object file.
84 bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
85   return LTOModule::isBitcodeFile(mem, length);
86 }
87
88 /// lto_module_is_object_file_in_memory_for_target - Validates if a buffer is a
89 /// loadable object file compilable for the target.
90 bool
91 lto_module_is_object_file_in_memory_for_target(const void* mem,
92                                             size_t length,
93                                             const char* target_triplet_prefix) {
94   return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix);
95 }
96
97 /// lto_module_create - Loads an object file from disk. Returns NULL on error
98 /// (check lto_get_error_message() for details).
99 lto_module_t lto_module_create(const char* path) {
100   lto_initialize();
101   llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
102   return LTOModule::makeLTOModule(path, Options, sLastErrorString);
103 }
104
105 /// lto_module_create_from_fd - Loads an object file from disk. Returns NULL on
106 /// error (check lto_get_error_message() for details).
107 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
108   lto_initialize();
109   llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
110   return LTOModule::makeLTOModule(fd, path, size, Options, sLastErrorString);
111 }
112
113 /// lto_module_create_from_fd_at_offset - Loads an object file from disk.
114 /// Returns NULL on error (check lto_get_error_message() for details).
115 lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
116                                                  size_t file_size,
117                                                  size_t map_size,
118                                                  off_t offset) {
119   lto_initialize();
120   llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
121   return LTOModule::makeLTOModule(fd, path, map_size, offset, Options,
122                                   sLastErrorString);
123 }
124
125 /// lto_module_create_from_memory - Loads an object file from memory. Returns
126 /// NULL on error (check lto_get_error_message() for details).
127 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
128   lto_initialize();
129   llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
130   return LTOModule::makeLTOModule(mem, length, Options, sLastErrorString);
131 }
132
133 /// Loads an object file from memory with an extra path argument.
134 /// Returns NULL on error (check lto_get_error_message() for details).
135 lto_module_t lto_module_create_from_memory_with_path(const void* mem,
136                                                      size_t length,
137                                                      const char *path) {
138   lto_initialize();
139   llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
140   return LTOModule::makeLTOModule(mem, length, Options, sLastErrorString, path);
141 }
142
143 /// lto_module_dispose - Frees all memory for a module. Upon return the
144 /// lto_module_t is no longer valid.
145 void lto_module_dispose(lto_module_t mod) {
146   delete mod;
147 }
148
149 /// lto_module_get_target_triple - Returns triplet string which the object
150 /// module was compiled under.
151 const char* lto_module_get_target_triple(lto_module_t mod) {
152   return mod->getTargetTriple();
153 }
154
155 /// lto_module_set_target_triple - Sets triple string with which the object will
156 /// be codegened.
157 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
158   return mod->setTargetTriple(triple);
159 }
160
161 /// lto_module_get_num_symbols - Returns the number of symbols in the object
162 /// module.
163 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
164   return mod->getSymbolCount();
165 }
166
167 /// lto_module_get_symbol_name - Returns the name of the ith symbol in the
168 /// object module.
169 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
170   return mod->getSymbolName(index);
171 }
172
173 /// lto_module_get_symbol_attribute - Returns the attributes of the ith symbol
174 /// in the object module.
175 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
176                                                       unsigned int index) {
177   return mod->getSymbolAttributes(index);
178 }
179
180 /// lto_module_get_num_deplibs - Returns the number of dependent libraries in
181 /// the object module.
182 unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
183   return mod->getDependentLibraryCount();
184 }
185
186 /// lto_module_get_deplib - Returns the ith dependent library in the module.
187 const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
188   return mod->getDependentLibrary(index);
189 }
190
191 /// lto_module_get_num_linkeropts - Returns the number of linker options in the
192 /// object module.
193 unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
194   return mod->getLinkerOptCount();
195 }
196
197 /// lto_module_get_linkeropt - Returns the ith linker option in the module.
198 const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
199   return mod->getLinkerOpt(index);
200 }
201
202 /// Set a diagnostic handler.
203 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
204                                         lto_diagnostic_handler_t diag_handler,
205                                         void *ctxt) {
206   cg->setDiagnosticHandler(diag_handler, ctxt);
207 }
208
209 /// lto_codegen_create - Instantiates a code generator. Returns NULL if there
210 /// is an error.
211 lto_code_gen_t lto_codegen_create(void) {
212   lto_initialize();
213
214   TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
215
216   LTOCodeGenerator *CodeGen = new LTOCodeGenerator();
217   if (CodeGen)
218     CodeGen->setTargetOptions(Options);
219   return CodeGen;
220 }
221
222 /// lto_codegen_dispose - Frees all memory for a code generator. Upon return the
223 /// lto_code_gen_t is no longer valid.
224 void lto_codegen_dispose(lto_code_gen_t cg) {
225   delete cg;
226 }
227
228 /// lto_codegen_add_module - Add an object module to the set of modules for
229 /// which code will be generated. Returns true on error (check
230 /// lto_get_error_message() for details).
231 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
232   return !cg->addModule(mod, sLastErrorString);
233 }
234
235 /// lto_codegen_set_debug_model - Sets what if any format of debug info should
236 /// be generated. Returns true on error (check lto_get_error_message() for
237 /// details).
238 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
239   cg->setDebugInfo(debug);
240   return false;
241 }
242
243 /// lto_codegen_set_pic_model - Sets what code model to generated. Returns true
244 /// on error (check lto_get_error_message() for details).
245 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
246   cg->setCodePICModel(model);
247   return false;
248 }
249
250 /// lto_codegen_set_cpu - Sets the cpu to generate code for.
251 void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
252   return cg->setCpu(cpu);
253 }
254
255 /// lto_codegen_set_assembler_path - Sets the path to the assembler tool.
256 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
257   // In here only for backwards compatibility. We use MC now.
258 }
259
260 /// lto_codegen_set_assembler_args - Sets extra arguments that libLTO should
261 /// pass to the assembler.
262 void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
263                                     int nargs) {
264   // In here only for backwards compatibility. We use MC now.
265 }
266
267 /// lto_codegen_set_internalize_strategy - Sets the strategy to use during
268 /// internalize.
269 void lto_codegen_set_internalize_strategy(lto_code_gen_t cg,
270                                           lto_internalize_strategy strategy) {
271   cg->setInternalizeStrategy(strategy);
272 }
273
274 /// lto_codegen_add_must_preserve_symbol - Adds to a list of all global symbols
275 /// that must exist in the final generated code. If a function is not listed
276 /// there, it might be inlined into every usage and optimized away.
277 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
278                                           const char *symbol) {
279   cg->addMustPreserveSymbol(symbol);
280 }
281
282 /// lto_codegen_write_merged_modules - Writes a new file at the specified path
283 /// that contains the merged contents of all modules added so far. Returns true
284 /// on error (check lto_get_error_message() for details).
285 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
286   if (!parsedOptions) {
287     cg->parseCodeGenDebugOptions();
288     parsedOptions = true;
289   }
290   return !cg->writeMergedModules(path, sLastErrorString);
291 }
292
293 /// lto_codegen_compile - Generates code for all added modules into one native
294 /// object file. On success returns a pointer to a generated mach-o/ELF buffer
295 /// and length set to the buffer size. The buffer is owned by the lto_code_gen_t
296 /// object and will be freed when lto_codegen_dispose() is called, or
297 /// lto_codegen_compile() is called again. On failure, returns NULL (check
298 /// lto_get_error_message() for details).
299 const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
300   if (!parsedOptions) {
301     cg->parseCodeGenDebugOptions();
302     parsedOptions = true;
303   }
304   return cg->compile(length, DisableOpt, DisableInline, DisableGVNLoadPRE,
305                      sLastErrorString);
306 }
307
308 /// lto_codegen_compile_to_file - Generates code for all added modules into one
309 /// native object file. The name of the file is written to name. Returns true on
310 /// error.
311 bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
312   if (!parsedOptions) {
313     cg->parseCodeGenDebugOptions();
314     parsedOptions = true;
315   }
316   return !cg->compile_to_file(name, DisableOpt, DisableInline, DisableGVNLoadPRE,
317                               sLastErrorString);
318 }
319
320 /// lto_codegen_debug_options - Used to pass extra options to the code
321 /// generator.
322 void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
323   cg->setCodeGenDebugOptions(opt);
324 }