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