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