blob: a28257f6ab04f5189cc153f0d84890d79021926b [file] [log] [blame]
Nick Kledzik07b4a622008-02-26 20:26:43 +00001//===-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.
Bill Wendling36cbf032012-03-30 10:29:38 +00007//
Nick Kledzik07b4a622008-02-26 20:26:43 +00008//===----------------------------------------------------------------------===//
9//
Bill Wendling36cbf032012-03-30 10:29:38 +000010// This file implements the Link Time Optimization library. This library is
Nick Kledzik07b4a622008-02-26 20:26:43 +000011// intended to be used by linker to optimize code at link time.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm-c/lto.h"
Chandler Carruth07baed52014-01-13 08:04:33 +000016#include "llvm-c/Core.h"
17#include "llvm-c/Target.h"
Rafael Espindola0b385c72013-09-30 16:39:19 +000018#include "llvm/CodeGen/CommandFlags.h"
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000019#include "llvm/LTO/LTOCodeGenerator.h"
20#include "llvm/LTO/LTOModule.h"
Nick Kledzik07b4a622008-02-26 20:26:43 +000021
Rafael Espindola0b385c72013-09-30 16:39:19 +000022// extra command-line flags needed for LTOCodeGenerator
23static cl::opt<bool>
24DisableOpt("disable-opt", cl::init(false),
25 cl::desc("Do not run any optimization passes"));
26
27static cl::opt<bool>
28DisableInline("disable-inlining", cl::init(false),
29 cl::desc("Do not run the inliner pass"));
30
31static cl::opt<bool>
32DisableGVNLoadPRE("disable-gvn-loadpre", cl::init(false),
33 cl::desc("Do not run the GVN load PRE pass"));
Nick Kledzik07b4a622008-02-26 20:26:43 +000034
Bill Wendling36cbf032012-03-30 10:29:38 +000035// Holds most recent error string.
36// *** Not thread safe ***
Nick Kledzik07b4a622008-02-26 20:26:43 +000037static std::string sLastErrorString;
38
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000039// Holds the initialization state of the LTO module.
40// *** Not thread safe ***
41static bool initialized = false;
42
Rafael Espindolaefa02d52013-10-02 14:36:23 +000043// Holds the command-line option parsing state of the LTO module.
44static bool parsedOptions = false;
45
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +000046// Initialize the configured targets if they have not been initialized.
47static void lto_initialize() {
48 if (!initialized) {
49 LLVMInitializeAllTargetInfos();
50 LLVMInitializeAllTargets();
51 LLVMInitializeAllTargetMCs();
52 LLVMInitializeAllAsmParsers();
53 LLVMInitializeAllAsmPrinters();
54 LLVMInitializeAllDisassemblers();
55 initialized = true;
56 }
57}
58
Rafael Espindola0b385c72013-09-30 16:39:19 +000059static void lto_set_target_options(llvm::TargetOptions &Options) {
60 Options.LessPreciseFPMADOption = EnableFPMAD;
61 Options.NoFramePointerElim = DisableFPElim;
62 Options.AllowFPOpFusion = FuseFPOps;
63 Options.UnsafeFPMath = EnableUnsafeFPMath;
64 Options.NoInfsFPMath = EnableNoInfsFPMath;
65 Options.NoNaNsFPMath = EnableNoNaNsFPMath;
66 Options.HonorSignDependentRoundingFPMathOption =
67 EnableHonorSignDependentRoundingFPMath;
68 Options.UseSoftFloat = GenerateSoftFloatCalls;
69 if (FloatABIForCalls != llvm::FloatABI::Default)
70 Options.FloatABIType = FloatABIForCalls;
71 Options.NoZerosInBSS = DontPlaceZerosInBSS;
72 Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt;
73 Options.DisableTailCalls = DisableTailCalls;
74 Options.StackAlignmentOverride = OverrideStackAlignment;
75 Options.TrapFuncName = TrapFuncName;
76 Options.PositionIndependentExecutable = EnablePIE;
77 Options.EnableSegmentedStacks = SegmentedStacks;
78 Options.UseInitArray = UseInitArray;
79}
80
Bill Wendling36cbf032012-03-30 10:29:38 +000081/// lto_get_version - Returns a printable string.
82extern const char* lto_get_version() {
83 return LTOCodeGenerator::getVersionString();
Nick Kledzik07b4a622008-02-26 20:26:43 +000084}
85
Bill Wendling36cbf032012-03-30 10:29:38 +000086/// lto_get_error_message - Returns the last error string or NULL if last
87/// operation was successful.
88const char* lto_get_error_message() {
89 return sLastErrorString.c_str();
Nick Kledzik07b4a622008-02-26 20:26:43 +000090}
91
Bill Wendling36cbf032012-03-30 10:29:38 +000092/// lto_module_is_object_file - Validates if a file is a loadable object file.
Reid Klecknerddac1512013-10-24 22:26:04 +000093bool lto_module_is_object_file(const char* path) {
Bill Wendling36cbf032012-03-30 10:29:38 +000094 return LTOModule::isBitcodeFile(path);
Nick Kledzik07b4a622008-02-26 20:26:43 +000095}
96
Bill Wendling36cbf032012-03-30 10:29:38 +000097/// lto_module_is_object_file_for_target - Validates if a file is a loadable
98/// object file compilable for requested target.
Reid Klecknerddac1512013-10-24 22:26:04 +000099bool lto_module_is_object_file_for_target(const char* path,
Bill Wendling36cbf032012-03-30 10:29:38 +0000100 const char* target_triplet_prefix) {
101 return LTOModule::isBitcodeFileForTarget(path, target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000102}
103
Bill Wendling36cbf032012-03-30 10:29:38 +0000104/// lto_module_is_object_file_in_memory - Validates if a buffer is a loadable
105/// object file.
Reid Klecknerddac1512013-10-24 22:26:04 +0000106bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {
Bill Wendling36cbf032012-03-30 10:29:38 +0000107 return LTOModule::isBitcodeFile(mem, length);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000108}
109
Bill Wendling36cbf032012-03-30 10:29:38 +0000110/// lto_module_is_object_file_in_memory_for_target - Validates if a buffer is a
111/// loadable object file compilable for the target.
Reid Klecknerddac1512013-10-24 22:26:04 +0000112bool
Bill Wendling36cbf032012-03-30 10:29:38 +0000113lto_module_is_object_file_in_memory_for_target(const void* mem,
114 size_t length,
115 const char* target_triplet_prefix) {
116 return LTOModule::isBitcodeFileForTarget(mem, length, target_triplet_prefix);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000117}
118
Bill Wendling36cbf032012-03-30 10:29:38 +0000119/// lto_module_create - Loads an object file from disk. Returns NULL on error
120/// (check lto_get_error_message() for details).
121lto_module_t lto_module_create(const char* path) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000122 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000123 llvm::TargetOptions Options;
124 lto_set_target_options(Options);
125 return LTOModule::makeLTOModule(path, Options, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000126}
127
Bill Wendling36cbf032012-03-30 10:29:38 +0000128/// lto_module_create_from_fd - Loads an object file from disk. Returns NULL on
129/// error (check lto_get_error_message() for details).
130lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000131 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000132 llvm::TargetOptions Options;
133 lto_set_target_options(Options);
134 return LTOModule::makeLTOModule(fd, path, size, Options, sLastErrorString);
Rafael Espindola56e41f72011-02-08 22:40:47 +0000135}
Nick Kledzik07b4a622008-02-26 20:26:43 +0000136
Bill Wendling36cbf032012-03-30 10:29:38 +0000137/// lto_module_create_from_fd_at_offset - Loads an object file from disk.
138/// Returns NULL on error (check lto_get_error_message() for details).
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000139lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
140 size_t file_size,
141 size_t map_size,
Bill Wendling36cbf032012-03-30 10:29:38 +0000142 off_t offset) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000143 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000144 llvm::TargetOptions Options;
145 lto_set_target_options(Options);
146 return LTOModule::makeLTOModule(fd, path, map_size, offset, Options,
147 sLastErrorString);
Rafael Espindolab39c7c72011-03-17 00:36:11 +0000148}
149
Bill Wendling36cbf032012-03-30 10:29:38 +0000150/// lto_module_create_from_memory - Loads an object file from memory. Returns
151/// NULL on error (check lto_get_error_message() for details).
152lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000153 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000154 llvm::TargetOptions Options;
155 lto_set_target_options(Options);
156 return LTOModule::makeLTOModule(mem, length, Options, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000157}
158
Bill Wendling36cbf032012-03-30 10:29:38 +0000159/// lto_module_dispose - Frees all memory for a module. Upon return the
160/// lto_module_t is no longer valid.
161void lto_module_dispose(lto_module_t mod) {
162 delete mod;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000163}
164
Bill Wendling36cbf032012-03-30 10:29:38 +0000165/// lto_module_get_target_triple - Returns triplet string which the object
166/// module was compiled under.
167const char* lto_module_get_target_triple(lto_module_t mod) {
168 return mod->getTargetTriple();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000169}
170
Bill Wendling36cbf032012-03-30 10:29:38 +0000171/// lto_module_set_target_triple - Sets triple string with which the object will
172/// be codegened.
173void lto_module_set_target_triple(lto_module_t mod, const char *triple) {
174 return mod->setTargetTriple(triple);
Rafael Espindola4ef89f52010-08-09 21:09:46 +0000175}
176
Bill Wendling36cbf032012-03-30 10:29:38 +0000177/// lto_module_get_num_symbols - Returns the number of symbols in the object
178/// module.
179unsigned int lto_module_get_num_symbols(lto_module_t mod) {
180 return mod->getSymbolCount();
Nick Kledzik07b4a622008-02-26 20:26:43 +0000181}
182
Bill Wendling36cbf032012-03-30 10:29:38 +0000183/// lto_module_get_symbol_name - Returns the name of the ith symbol in the
184/// object module.
185const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {
186 return mod->getSymbolName(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000187}
188
Bill Wendling36cbf032012-03-30 10:29:38 +0000189/// lto_module_get_symbol_attribute - Returns the attributes of the ith symbol
190/// in the object module.
191lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
192 unsigned int index) {
193 return mod->getSymbolAttributes(index);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000194}
195
Bill Wendling36cbf032012-03-30 10:29:38 +0000196/// lto_codegen_create - Instantiates a code generator. Returns NULL if there
197/// is an error.
198lto_code_gen_t lto_codegen_create(void) {
Peter Collingbourne4ccf0f12013-09-24 23:52:22 +0000199 lto_initialize();
Rafael Espindola0b385c72013-09-30 16:39:19 +0000200
201 TargetOptions Options;
202 lto_set_target_options(Options);
203
204 LTOCodeGenerator *CodeGen = new LTOCodeGenerator();
205 if (CodeGen)
206 CodeGen->setTargetOptions(Options);
207 return CodeGen;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000208}
209
Bill Wendling36cbf032012-03-30 10:29:38 +0000210/// lto_codegen_dispose - Frees all memory for a code generator. Upon return the
211/// lto_code_gen_t is no longer valid.
212void lto_codegen_dispose(lto_code_gen_t cg) {
213 delete cg;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000214}
215
Bill Wendling36cbf032012-03-30 10:29:38 +0000216/// lto_codegen_add_module - Add an object module to the set of modules for
217/// which code will be generated. Returns true on error (check
218/// lto_get_error_message() for details).
Reid Klecknerddac1512013-10-24 22:26:04 +0000219bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {
Shuxin Yangb6696a92013-08-07 05:19:23 +0000220 return !cg->addModule(mod, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000221}
222
Bill Wendling36cbf032012-03-30 10:29:38 +0000223/// lto_codegen_set_debug_model - Sets what if any format of debug info should
224/// be generated. Returns true on error (check lto_get_error_message() for
225/// details).
Reid Klecknerddac1512013-10-24 22:26:04 +0000226bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {
Shuxin Yangb6696a92013-08-07 05:19:23 +0000227 cg->setDebugInfo(debug);
228 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000229}
230
Bill Wendling36cbf032012-03-30 10:29:38 +0000231/// lto_codegen_set_pic_model - Sets what code model to generated. Returns true
232/// on error (check lto_get_error_message() for details).
Reid Klecknerddac1512013-10-24 22:26:04 +0000233bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {
Shuxin Yangb6696a92013-08-07 05:19:23 +0000234 cg->setCodePICModel(model);
235 return false;
Nick Kledzik07b4a622008-02-26 20:26:43 +0000236}
237
Bill Wendling36cbf032012-03-30 10:29:38 +0000238/// lto_codegen_set_cpu - Sets the cpu to generate code for.
Bill Wendling152e4732012-03-31 10:44:20 +0000239void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {
Rafael Espindolaccab1dd2010-08-11 00:15:13 +0000240 return cg->setCpu(cpu);
241}
242
Bill Wendling36cbf032012-03-30 10:29:38 +0000243/// lto_codegen_set_assembler_path - Sets the path to the assembler tool.
244void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000245 // In here only for backwards compatibility. We use MC now.
Nick Kledzikcac8c8a2009-06-04 00:28:45 +0000246}
247
Bill Wendling36cbf032012-03-30 10:29:38 +0000248/// lto_codegen_set_assembler_args - Sets extra arguments that libLTO should
249/// pass to the assembler.
Bill Wendling152e4732012-03-31 10:44:20 +0000250void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
Bill Wendling36cbf032012-03-30 10:29:38 +0000251 int nargs) {
Rafael Espindolafac373c2011-02-24 21:04:06 +0000252 // In here only for backwards compatibility. We use MC now.
Rafael Espindola00456462010-08-10 18:55:09 +0000253}
254
Duncan P. N. Exon Smith93be7c42014-01-14 18:52:17 +0000255/// lto_codegen_set_internalize_strategy - Sets the strategy to use during
256/// internalize.
257void lto_codegen_set_internalize_strategy(lto_code_gen_t cg,
258 lto_internalize_strategy strategy) {
259 cg->setInternalizeStrategy(strategy);
260}
261
Bill Wendling36cbf032012-03-30 10:29:38 +0000262/// lto_codegen_add_must_preserve_symbol - Adds to a list of all global symbols
263/// that must exist in the final generated code. If a function is not listed
264/// there, it might be inlined into every usage and optimized away.
265void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
Bill Wendling152e4732012-03-31 10:44:20 +0000266 const char *symbol) {
Evan Chengd6460962009-06-26 06:57:16 +0000267 cg->addMustPreserveSymbol(symbol);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000268}
269
Bill Wendling36cbf032012-03-30 10:29:38 +0000270/// lto_codegen_write_merged_modules - Writes a new file at the specified path
271/// that contains the merged contents of all modules added so far. Returns true
272/// on error (check lto_get_error_message() for details).
Reid Klecknerddac1512013-10-24 22:26:04 +0000273bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000274 if (!parsedOptions) {
275 cg->parseCodeGenDebugOptions();
276 parsedOptions = true;
277 }
Shuxin Yangb6696a92013-08-07 05:19:23 +0000278 return !cg->writeMergedModules(path, sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000279}
280
Bill Wendling36cbf032012-03-30 10:29:38 +0000281/// lto_codegen_compile - Generates code for all added modules into one native
282/// object file. On success returns a pointer to a generated mach-o/ELF buffer
283/// and length set to the buffer size. The buffer is owned by the lto_code_gen_t
284/// object and will be freed when lto_codegen_dispose() is called, or
285/// lto_codegen_compile() is called again. On failure, returns NULL (check
286/// lto_get_error_message() for details).
Bill Wendling152e4732012-03-31 10:44:20 +0000287const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000288 if (!parsedOptions) {
289 cg->parseCodeGenDebugOptions();
290 parsedOptions = true;
291 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000292 return cg->compile(length, DisableOpt, DisableInline, DisableGVNLoadPRE,
293 sLastErrorString);
Nick Kledzik07b4a622008-02-26 20:26:43 +0000294}
295
Bill Wendling152e4732012-03-31 10:44:20 +0000296/// lto_codegen_compile_to_file - Generates code for all added modules into one
297/// native object file. The name of the file is written to name. Returns true on
298/// error.
Reid Klecknerddac1512013-10-24 22:26:04 +0000299bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {
Rafael Espindolaefa02d52013-10-02 14:36:23 +0000300 if (!parsedOptions) {
301 cg->parseCodeGenDebugOptions();
302 parsedOptions = true;
303 }
Rafael Espindola0b385c72013-09-30 16:39:19 +0000304 return !cg->compile_to_file(name, DisableOpt, DisableInline, DisableGVNLoadPRE,
305 sLastErrorString);
Rafael Espindola26b57ff2011-03-22 20:57:13 +0000306}
307
Bill Wendling36cbf032012-03-30 10:29:38 +0000308/// lto_codegen_debug_options - Used to pass extra options to the code
309/// generator.
Bill Wendling152e4732012-03-31 10:44:20 +0000310void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
Devang Patela0e4fb82008-07-03 22:53:14 +0000311 cg->setCodeGenDebugOptions(opt);
Duncan Sands31554ab2009-07-03 15:38:01 +0000312}