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