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