80dca6b4d46b1139aaae381bdd1abc7c56a055cf
[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/CodeGen/CommandFlags.h"
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/LTO/LTOCodeGenerator.h"
19 #include "llvm/LTO/LTOModule.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/Signals.h"
22 #include "llvm/Support/TargetSelect.h"
23
24 // extra command-line flags needed for LTOCodeGenerator
25 static cl::opt<bool>
26 DisableOpt("disable-opt", cl::init(false),
27   cl::desc("Do not run any optimization passes"));
28
29 static cl::opt<bool>
30 DisableInline("disable-inlining", cl::init(false),
31   cl::desc("Do not run the inliner pass"));
32
33 static cl::opt<bool>
34 DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
35   cl::desc("Do not run the GVN load PRE pass"));
36
37 static cl::opt<bool>
38 DisableLTOVectorization("disable-lto-vectorization", cl::init(false),
39   cl::desc("Do not run loop or slp vectorization during LTO"));
40
41 // Holds most recent error string.
42 // *** Not thread safe ***
43 static std::string sLastErrorString;
44
45 // Holds the initialization state of the LTO module.
46 // *** Not thread safe ***
47 static bool initialized = false;
48
49 // Holds the command-line option parsing state of the LTO module.
50 static bool parsedOptions = false;
51
52 // Initialize the configured targets if they have not been initialized.
53 static void lto_initialize() {
54   if (!initialized) {
55 #ifdef LLVM_ON_WIN32
56     // Dialog box on crash disabling doesn't work across DLL boundaries, so do
57     // it here.
58     llvm::sys::DisableSystemDialogsOnCrash();
59 #endif
60
61     InitializeAllTargetInfos();
62     InitializeAllTargets();
63     InitializeAllTargetMCs();
64     InitializeAllAsmParsers();
65     InitializeAllAsmPrinters();
66     InitializeAllDisassemblers();
67     initialized = true;
68   }
69 }
70
71 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOCodeGenerator, lto_code_gen_t)
72 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(LTOModule, lto_module_t)
73
74 // Convert the subtarget features into a string to pass to LTOCodeGenerator.
75 static void lto_add_attrs(lto_code_gen_t cg) {
76   LTOCodeGenerator *CG = unwrap(cg);
77   if (MAttrs.size()) {
78     std::string attrs;
79     for (unsigned i = 0; i < MAttrs.size(); ++i) {
80       if (i > 0)
81         attrs.append(",");
82       attrs.append(MAttrs[i]);
83     }
84
85     CG->setAttr(attrs.c_str());
86   }
87 }
88
89 extern const char* lto_get_version() {
90   return LTOCodeGenerator::getVersionString();
91 }
92
93 const char* lto_get_error_message() {
94   return sLastErrorString.c_str();
95 }
96
97 bool lto_module_is_object_file(const char* path) {
98   return LTOModule::isBitcodeFile(path);
99 }
100
101 bool lto_module_is_object_file_for_target(const char* path,
102                                           const char* target_triplet_prefix) {
103   ErrorOr<std::unique_ptr<MemoryBuffer>> Buffer = MemoryBuffer::getFile(path);
104   if (!Buffer)
105     return false;
106   return LTOModule::isBitcodeForTarget(Buffer->get(), target_triplet_prefix);
107 }
108
109 bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
110   return LTOModule::isBitcodeFile(mem, length);
111 }
112
113 bool
114 lto_module_is_object_file_in_memory_for_target(const void* mem,
115                                             size_t length,
116                                             const char* target_triplet_prefix) {
117   std::unique_ptr<MemoryBuffer> buffer(LTOModule::makeBuffer(mem, length));
118   if (!buffer)
119     return false;
120   return LTOModule::isBitcodeForTarget(buffer.get(), target_triplet_prefix);
121 }
122
123 lto_module_t lto_module_create(const char* path) {
124   lto_initialize();
125   llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
126   return wrap(LTOModule::createFromFile(path, Options, sLastErrorString));
127 }
128
129 lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
130   lto_initialize();
131   llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
132   return wrap(
133       LTOModule::createFromOpenFile(fd, path, size, Options, sLastErrorString));
134 }
135
136 lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
137                                                  size_t file_size,
138                                                  size_t map_size,
139                                                  off_t offset) {
140   lto_initialize();
141   llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
142   return wrap(LTOModule::createFromOpenFileSlice(fd, path, map_size, offset,
143                                                  Options, sLastErrorString));
144 }
145
146 lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
147   lto_initialize();
148   llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
149   return wrap(LTOModule::createFromBuffer(mem, length, Options, sLastErrorString));
150 }
151
152 lto_module_t lto_module_create_from_memory_with_path(const void* mem,
153                                                      size_t length,
154                                                      const char *path) {
155   lto_initialize();
156   llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
157   return wrap(
158       LTOModule::createFromBuffer(mem, length, Options, sLastErrorString, path));
159 }
160
161 lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
162                                                 const char *path) {
163   lto_initialize();
164   llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
165   return wrap(LTOModule::createInLocalContext(mem, length, Options,
166                                               sLastErrorString, path));
167 }
168
169 lto_module_t lto_module_create_in_codegen_context(const void *mem,
170                                                   size_t length,
171                                                   const char *path,
172                                                   lto_code_gen_t cg) {
173   lto_initialize();
174   llvm::TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
175   return wrap(LTOModule::createInContext(mem, length, Options, sLastErrorString,
176                                          path, &unwrap(cg)->getContext()));
177 }
178
179 void lto_module_dispose(lto_module_t mod) { delete unwrap(mod); }
180
181 const char* lto_module_get_target_triple(lto_module_t mod) {
182   return unwrap(mod)->getTargetTriple().c_str();
183 }
184
185 void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
186   return unwrap(mod)->setTargetTriple(triple);
187 }
188
189 unsigned int lto_module_get_num_symbols(lto_module_t mod) {
190   return unwrap(mod)->getSymbolCount();
191 }
192
193 const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
194   return unwrap(mod)->getSymbolName(index);
195 }
196
197 lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
198                                                       unsigned int index) {
199   return unwrap(mod)->getSymbolAttributes(index);
200 }
201
202 unsigned int lto_module_get_num_deplibs(lto_module_t mod) {
203   return unwrap(mod)->getDependentLibraryCount();
204 }
205
206 const char* lto_module_get_deplib(lto_module_t mod, unsigned int index) {
207   return unwrap(mod)->getDependentLibrary(index);
208 }
209
210 unsigned int lto_module_get_num_linkeropts(lto_module_t mod) {
211   return unwrap(mod)->getLinkerOptCount();
212 }
213
214 const char* lto_module_get_linkeropt(lto_module_t mod, unsigned int index) {
215   return unwrap(mod)->getLinkerOpt(index);
216 }
217
218 void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
219                                         lto_diagnostic_handler_t diag_handler,
220                                         void *ctxt) {
221   unwrap(cg)->setDiagnosticHandler(diag_handler, ctxt);
222 }
223
224 static lto_code_gen_t createCodeGen(bool InLocalContext) {
225   lto_initialize();
226
227   TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
228
229   LTOCodeGenerator *CodeGen =
230       InLocalContext ? new LTOCodeGenerator(make_unique<LLVMContext>())
231                      : new LTOCodeGenerator();
232   if (CodeGen)
233     CodeGen->setTargetOptions(Options);
234   return wrap(CodeGen);
235 }
236
237 lto_code_gen_t lto_codegen_create(void) {
238   return createCodeGen(/* InLocalContext */ false);
239 }
240
241 lto_code_gen_t lto_codegen_create_in_local_context(void) {
242   return createCodeGen(/* InLocalContext */ true);
243 }
244
245 void lto_codegen_dispose(lto_code_gen_t cg) { delete unwrap(cg); }
246
247 bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
248   return !unwrap(cg)->addModule(unwrap(mod));
249 }
250
251 bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
252   unwrap(cg)->setDebugInfo(debug);
253   return false;
254 }
255
256 bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
257   unwrap(cg)->setCodePICModel(model);
258   return false;
259 }
260
261 void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
262   return unwrap(cg)->setCpu(cpu);
263 }
264
265 void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
266   // In here only for backwards compatibility. We use MC now.
267 }
268
269 void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
270                                     int nargs) {
271   // In here only for backwards compatibility. We use MC now.
272 }
273
274 void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
275                                           const char *symbol) {
276   unwrap(cg)->addMustPreserveSymbol(symbol);
277 }
278
279 bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
280   if (!parsedOptions) {
281     unwrap(cg)->parseCodeGenDebugOptions();
282     lto_add_attrs(cg);
283     parsedOptions = true;
284   }
285   return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
286 }
287
288 const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
289   if (!parsedOptions) {
290     unwrap(cg)->parseCodeGenDebugOptions();
291     lto_add_attrs(cg);
292     parsedOptions = true;
293   }
294   return unwrap(cg)->compile(length, DisableOpt, DisableInline,
295                              DisableGVNLoadPRE, DisableLTOVectorization,
296                              sLastErrorString);
297 }
298
299 bool lto_codegen_optimize(lto_code_gen_t cg) {
300   if (!parsedOptions) {
301     unwrap(cg)->parseCodeGenDebugOptions();
302     lto_add_attrs(cg);
303     parsedOptions = true;
304   }
305   return !unwrap(cg)->optimize(DisableOpt, DisableInline,
306                                DisableGVNLoadPRE, DisableLTOVectorization,
307                                sLastErrorString);
308 }
309
310 const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {
311   if (!parsedOptions) {
312     unwrap(cg)->parseCodeGenDebugOptions();
313     lto_add_attrs(cg);
314     parsedOptions = true;
315   }
316   return unwrap(cg)->compileOptimized(length, sLastErrorString);
317 }
318
319 bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
320   if (!parsedOptions) {
321     unwrap(cg)->parseCodeGenDebugOptions();
322     lto_add_attrs(cg);
323     parsedOptions = true;
324   }
325   return !unwrap(cg)->compile_to_file(
326       name, DisableOpt, DisableInline, DisableGVNLoadPRE,
327       DisableLTOVectorization, sLastErrorString);
328 }
329
330 void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
331   unwrap(cg)->setCodeGenDebugOptions(opt);
332 }