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