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