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