xref: /freebsd/contrib/llvm-project/llvm/include/llvm-c/lto.h (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
10b57cec5SDimitry Andric /*===-- llvm-c/lto.h - LTO Public C Interface ---------------------*- C -*-===*\
20b57cec5SDimitry Andric |*                                                                            *|
30b57cec5SDimitry Andric |* Part of the LLVM Project, under the Apache License v2.0 with LLVM          *|
40b57cec5SDimitry Andric |* Exceptions.                                                                *|
50b57cec5SDimitry Andric |* See https://llvm.org/LICENSE.txt for license information.                  *|
60b57cec5SDimitry Andric |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception                    *|
70b57cec5SDimitry Andric |*                                                                            *|
80b57cec5SDimitry Andric |*===----------------------------------------------------------------------===*|
90b57cec5SDimitry Andric |*                                                                            *|
100b57cec5SDimitry Andric |* This header provides public interface to an abstract link time optimization*|
110b57cec5SDimitry Andric |* library.  LLVM provides an implementation of this interface for use with   *|
120b57cec5SDimitry Andric |* llvm bitcode files.                                                        *|
130b57cec5SDimitry Andric |*                                                                            *|
140b57cec5SDimitry Andric \*===----------------------------------------------------------------------===*/
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric #ifndef LLVM_C_LTO_H
170b57cec5SDimitry Andric #define LLVM_C_LTO_H
180b57cec5SDimitry Andric 
19480093f4SDimitry Andric #include "llvm-c/ExternC.h"
20480093f4SDimitry Andric 
210b57cec5SDimitry Andric #ifdef __cplusplus
220b57cec5SDimitry Andric #include <cstddef>
230b57cec5SDimitry Andric #else
240b57cec5SDimitry Andric #include <stddef.h>
250b57cec5SDimitry Andric #endif
260b57cec5SDimitry Andric #include <sys/types.h>
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric #ifndef __cplusplus
290b57cec5SDimitry Andric #if !defined(_MSC_VER)
300b57cec5SDimitry Andric #include <stdbool.h>
310b57cec5SDimitry Andric typedef bool lto_bool_t;
320b57cec5SDimitry Andric #else
330b57cec5SDimitry Andric /* MSVC in particular does not have anything like _Bool or bool in C, but we can
340b57cec5SDimitry Andric    at least make sure the type is the same size.  The implementation side will
350b57cec5SDimitry Andric    use C++ bool. */
360b57cec5SDimitry Andric typedef unsigned char lto_bool_t;
370b57cec5SDimitry Andric #endif
380b57cec5SDimitry Andric #else
390b57cec5SDimitry Andric typedef bool lto_bool_t;
400b57cec5SDimitry Andric #endif
410b57cec5SDimitry Andric 
420b57cec5SDimitry Andric /**
430b57cec5SDimitry Andric  * @defgroup LLVMCLTO LTO
440b57cec5SDimitry Andric  * @ingroup LLVMC
450b57cec5SDimitry Andric  *
460b57cec5SDimitry Andric  * @{
470b57cec5SDimitry Andric  */
480b57cec5SDimitry Andric 
49*5ffd83dbSDimitry Andric #define LTO_API_VERSION 27
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric /**
520b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
530b57cec5SDimitry Andric  */
540b57cec5SDimitry Andric typedef enum {
550b57cec5SDimitry Andric     LTO_SYMBOL_ALIGNMENT_MASK              = 0x0000001F, /* log2 of alignment */
560b57cec5SDimitry Andric     LTO_SYMBOL_PERMISSIONS_MASK            = 0x000000E0,
570b57cec5SDimitry Andric     LTO_SYMBOL_PERMISSIONS_CODE            = 0x000000A0,
580b57cec5SDimitry Andric     LTO_SYMBOL_PERMISSIONS_DATA            = 0x000000C0,
590b57cec5SDimitry Andric     LTO_SYMBOL_PERMISSIONS_RODATA          = 0x00000080,
600b57cec5SDimitry Andric     LTO_SYMBOL_DEFINITION_MASK             = 0x00000700,
610b57cec5SDimitry Andric     LTO_SYMBOL_DEFINITION_REGULAR          = 0x00000100,
620b57cec5SDimitry Andric     LTO_SYMBOL_DEFINITION_TENTATIVE        = 0x00000200,
630b57cec5SDimitry Andric     LTO_SYMBOL_DEFINITION_WEAK             = 0x00000300,
640b57cec5SDimitry Andric     LTO_SYMBOL_DEFINITION_UNDEFINED        = 0x00000400,
650b57cec5SDimitry Andric     LTO_SYMBOL_DEFINITION_WEAKUNDEF        = 0x00000500,
660b57cec5SDimitry Andric     LTO_SYMBOL_SCOPE_MASK                  = 0x00003800,
670b57cec5SDimitry Andric     LTO_SYMBOL_SCOPE_INTERNAL              = 0x00000800,
680b57cec5SDimitry Andric     LTO_SYMBOL_SCOPE_HIDDEN                = 0x00001000,
690b57cec5SDimitry Andric     LTO_SYMBOL_SCOPE_PROTECTED             = 0x00002000,
700b57cec5SDimitry Andric     LTO_SYMBOL_SCOPE_DEFAULT               = 0x00001800,
710b57cec5SDimitry Andric     LTO_SYMBOL_SCOPE_DEFAULT_CAN_BE_HIDDEN = 0x00002800,
720b57cec5SDimitry Andric     LTO_SYMBOL_COMDAT                      = 0x00004000,
730b57cec5SDimitry Andric     LTO_SYMBOL_ALIAS                       = 0x00008000
740b57cec5SDimitry Andric } lto_symbol_attributes;
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric /**
770b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
780b57cec5SDimitry Andric  */
790b57cec5SDimitry Andric typedef enum {
800b57cec5SDimitry Andric     LTO_DEBUG_MODEL_NONE         = 0,
810b57cec5SDimitry Andric     LTO_DEBUG_MODEL_DWARF        = 1
820b57cec5SDimitry Andric } lto_debug_model;
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric /**
850b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
860b57cec5SDimitry Andric  */
870b57cec5SDimitry Andric typedef enum {
880b57cec5SDimitry Andric     LTO_CODEGEN_PIC_MODEL_STATIC         = 0,
890b57cec5SDimitry Andric     LTO_CODEGEN_PIC_MODEL_DYNAMIC        = 1,
900b57cec5SDimitry Andric     LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC = 2,
910b57cec5SDimitry Andric     LTO_CODEGEN_PIC_MODEL_DEFAULT        = 3
920b57cec5SDimitry Andric } lto_codegen_model;
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric /** opaque reference to a loaded object module */
950b57cec5SDimitry Andric typedef struct LLVMOpaqueLTOModule *lto_module_t;
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric /** opaque reference to a code generator */
980b57cec5SDimitry Andric typedef struct LLVMOpaqueLTOCodeGenerator *lto_code_gen_t;
990b57cec5SDimitry Andric 
1000b57cec5SDimitry Andric /** opaque reference to a thin code generator */
1010b57cec5SDimitry Andric typedef struct LLVMOpaqueThinLTOCodeGenerator *thinlto_code_gen_t;
1020b57cec5SDimitry Andric 
103480093f4SDimitry Andric LLVM_C_EXTERN_C_BEGIN
1040b57cec5SDimitry Andric 
1050b57cec5SDimitry Andric /**
1060b57cec5SDimitry Andric  * Returns a printable string.
1070b57cec5SDimitry Andric  *
1080b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
1090b57cec5SDimitry Andric  */
1100b57cec5SDimitry Andric extern const char*
1110b57cec5SDimitry Andric lto_get_version(void);
1120b57cec5SDimitry Andric 
1130b57cec5SDimitry Andric /**
1140b57cec5SDimitry Andric  * Returns the last error string or NULL if last operation was successful.
1150b57cec5SDimitry Andric  *
1160b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
1170b57cec5SDimitry Andric  */
1180b57cec5SDimitry Andric extern const char*
1190b57cec5SDimitry Andric lto_get_error_message(void);
1200b57cec5SDimitry Andric 
1210b57cec5SDimitry Andric /**
1220b57cec5SDimitry Andric  * Checks if a file is a loadable object file.
1230b57cec5SDimitry Andric  *
1240b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
1250b57cec5SDimitry Andric  */
1260b57cec5SDimitry Andric extern lto_bool_t
1270b57cec5SDimitry Andric lto_module_is_object_file(const char* path);
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric /**
1300b57cec5SDimitry Andric  * Checks if a file is a loadable object compiled for requested target.
1310b57cec5SDimitry Andric  *
1320b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
1330b57cec5SDimitry Andric  */
1340b57cec5SDimitry Andric extern lto_bool_t
1350b57cec5SDimitry Andric lto_module_is_object_file_for_target(const char* path,
1360b57cec5SDimitry Andric                                      const char* target_triple_prefix);
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric /**
1390b57cec5SDimitry Andric  * Return true if \p Buffer contains a bitcode file with ObjC code (category
1400b57cec5SDimitry Andric  * or class) in it.
1410b57cec5SDimitry Andric  *
1420b57cec5SDimitry Andric  * \since LTO_API_VERSION=20
1430b57cec5SDimitry Andric  */
1440b57cec5SDimitry Andric extern lto_bool_t
1450b57cec5SDimitry Andric lto_module_has_objc_category(const void *mem, size_t length);
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric /**
1480b57cec5SDimitry Andric  * Checks if a buffer is a loadable object file.
1490b57cec5SDimitry Andric  *
1500b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
1510b57cec5SDimitry Andric  */
1520b57cec5SDimitry Andric extern lto_bool_t lto_module_is_object_file_in_memory(const void *mem,
1530b57cec5SDimitry Andric                                                       size_t length);
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric /**
1560b57cec5SDimitry Andric  * Checks if a buffer is a loadable object compiled for requested target.
1570b57cec5SDimitry Andric  *
1580b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
1590b57cec5SDimitry Andric  */
1600b57cec5SDimitry Andric extern lto_bool_t
1610b57cec5SDimitry Andric lto_module_is_object_file_in_memory_for_target(const void* mem, size_t length,
1620b57cec5SDimitry Andric                                               const char* target_triple_prefix);
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric /**
1650b57cec5SDimitry Andric  * Loads an object file from disk.
1660b57cec5SDimitry Andric  * Returns NULL on error (check lto_get_error_message() for details).
1670b57cec5SDimitry Andric  *
1680b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
1690b57cec5SDimitry Andric  */
1700b57cec5SDimitry Andric extern lto_module_t
1710b57cec5SDimitry Andric lto_module_create(const char* path);
1720b57cec5SDimitry Andric 
1730b57cec5SDimitry Andric /**
1740b57cec5SDimitry Andric  * Loads an object file from memory.
1750b57cec5SDimitry Andric  * Returns NULL on error (check lto_get_error_message() for details).
1760b57cec5SDimitry Andric  *
1770b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
1780b57cec5SDimitry Andric  */
1790b57cec5SDimitry Andric extern lto_module_t
1800b57cec5SDimitry Andric lto_module_create_from_memory(const void* mem, size_t length);
1810b57cec5SDimitry Andric 
1820b57cec5SDimitry Andric /**
1830b57cec5SDimitry Andric  * Loads an object file from memory with an extra path argument.
1840b57cec5SDimitry Andric  * Returns NULL on error (check lto_get_error_message() for details).
1850b57cec5SDimitry Andric  *
1860b57cec5SDimitry Andric  * \since LTO_API_VERSION=9
1870b57cec5SDimitry Andric  */
1880b57cec5SDimitry Andric extern lto_module_t
1890b57cec5SDimitry Andric lto_module_create_from_memory_with_path(const void* mem, size_t length,
1900b57cec5SDimitry Andric                                         const char *path);
1910b57cec5SDimitry Andric 
1920b57cec5SDimitry Andric /**
1930b57cec5SDimitry Andric  * Loads an object file in its own context.
1940b57cec5SDimitry Andric  *
1950b57cec5SDimitry Andric  * Loads an object file in its own LLVMContext.  This function call is
1960b57cec5SDimitry Andric  * thread-safe.  However, modules created this way should not be merged into an
1970b57cec5SDimitry Andric  * lto_code_gen_t using \a lto_codegen_add_module().
1980b57cec5SDimitry Andric  *
1990b57cec5SDimitry Andric  * Returns NULL on error (check lto_get_error_message() for details).
2000b57cec5SDimitry Andric  *
2010b57cec5SDimitry Andric  * \since LTO_API_VERSION=11
2020b57cec5SDimitry Andric  */
2030b57cec5SDimitry Andric extern lto_module_t
2040b57cec5SDimitry Andric lto_module_create_in_local_context(const void *mem, size_t length,
2050b57cec5SDimitry Andric                                    const char *path);
2060b57cec5SDimitry Andric 
2070b57cec5SDimitry Andric /**
2080b57cec5SDimitry Andric  * Loads an object file in the codegen context.
2090b57cec5SDimitry Andric  *
2100b57cec5SDimitry Andric  * Loads an object file into the same context as \c cg.  The module is safe to
2110b57cec5SDimitry Andric  * add using \a lto_codegen_add_module().
2120b57cec5SDimitry Andric  *
2130b57cec5SDimitry Andric  * Returns NULL on error (check lto_get_error_message() for details).
2140b57cec5SDimitry Andric  *
2150b57cec5SDimitry Andric  * \since LTO_API_VERSION=11
2160b57cec5SDimitry Andric  */
2170b57cec5SDimitry Andric extern lto_module_t
2180b57cec5SDimitry Andric lto_module_create_in_codegen_context(const void *mem, size_t length,
2190b57cec5SDimitry Andric                                      const char *path, lto_code_gen_t cg);
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric /**
2220b57cec5SDimitry Andric  * Loads an object file from disk. The seek point of fd is not preserved.
2230b57cec5SDimitry Andric  * Returns NULL on error (check lto_get_error_message() for details).
2240b57cec5SDimitry Andric  *
2250b57cec5SDimitry Andric  * \since LTO_API_VERSION=5
2260b57cec5SDimitry Andric  */
2270b57cec5SDimitry Andric extern lto_module_t
2280b57cec5SDimitry Andric lto_module_create_from_fd(int fd, const char *path, size_t file_size);
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric /**
2310b57cec5SDimitry Andric  * Loads an object file from disk. The seek point of fd is not preserved.
2320b57cec5SDimitry Andric  * Returns NULL on error (check lto_get_error_message() for details).
2330b57cec5SDimitry Andric  *
2340b57cec5SDimitry Andric  * \since LTO_API_VERSION=5
2350b57cec5SDimitry Andric  */
2360b57cec5SDimitry Andric extern lto_module_t
2370b57cec5SDimitry Andric lto_module_create_from_fd_at_offset(int fd, const char *path, size_t file_size,
2380b57cec5SDimitry Andric                                     size_t map_size, off_t offset);
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric /**
2410b57cec5SDimitry Andric  * Frees all memory internally allocated by the module.
2420b57cec5SDimitry Andric  * Upon return the lto_module_t is no longer valid.
2430b57cec5SDimitry Andric  *
2440b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
2450b57cec5SDimitry Andric  */
2460b57cec5SDimitry Andric extern void
2470b57cec5SDimitry Andric lto_module_dispose(lto_module_t mod);
2480b57cec5SDimitry Andric 
2490b57cec5SDimitry Andric /**
2500b57cec5SDimitry Andric  * Returns triple string which the object module was compiled under.
2510b57cec5SDimitry Andric  *
2520b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
2530b57cec5SDimitry Andric  */
2540b57cec5SDimitry Andric extern const char*
2550b57cec5SDimitry Andric lto_module_get_target_triple(lto_module_t mod);
2560b57cec5SDimitry Andric 
2570b57cec5SDimitry Andric /**
2580b57cec5SDimitry Andric  * Sets triple string with which the object will be codegened.
2590b57cec5SDimitry Andric  *
2600b57cec5SDimitry Andric  * \since LTO_API_VERSION=4
2610b57cec5SDimitry Andric  */
2620b57cec5SDimitry Andric extern void
2630b57cec5SDimitry Andric lto_module_set_target_triple(lto_module_t mod, const char *triple);
2640b57cec5SDimitry Andric 
2650b57cec5SDimitry Andric /**
2660b57cec5SDimitry Andric  * Returns the number of symbols in the object module.
2670b57cec5SDimitry Andric  *
2680b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
2690b57cec5SDimitry Andric  */
2700b57cec5SDimitry Andric extern unsigned int
2710b57cec5SDimitry Andric lto_module_get_num_symbols(lto_module_t mod);
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric /**
2740b57cec5SDimitry Andric  * Returns the name of the ith symbol in the object module.
2750b57cec5SDimitry Andric  *
2760b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
2770b57cec5SDimitry Andric  */
2780b57cec5SDimitry Andric extern const char*
2790b57cec5SDimitry Andric lto_module_get_symbol_name(lto_module_t mod, unsigned int index);
2800b57cec5SDimitry Andric 
2810b57cec5SDimitry Andric /**
2820b57cec5SDimitry Andric  * Returns the attributes of the ith symbol in the object module.
2830b57cec5SDimitry Andric  *
2840b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
2850b57cec5SDimitry Andric  */
2860b57cec5SDimitry Andric extern lto_symbol_attributes
2870b57cec5SDimitry Andric lto_module_get_symbol_attribute(lto_module_t mod, unsigned int index);
2880b57cec5SDimitry Andric 
2890b57cec5SDimitry Andric /**
2900b57cec5SDimitry Andric  * Returns the module's linker options.
2910b57cec5SDimitry Andric  *
2920b57cec5SDimitry Andric  * The linker options may consist of multiple flags. It is the linker's
2930b57cec5SDimitry Andric  * responsibility to split the flags using a platform-specific mechanism.
2940b57cec5SDimitry Andric  *
2950b57cec5SDimitry Andric  * \since LTO_API_VERSION=16
2960b57cec5SDimitry Andric  */
2970b57cec5SDimitry Andric extern const char*
2980b57cec5SDimitry Andric lto_module_get_linkeropts(lto_module_t mod);
2990b57cec5SDimitry Andric 
3000b57cec5SDimitry Andric /**
301*5ffd83dbSDimitry Andric  * If targeting mach-o on darwin, this function gets the CPU type and subtype
302*5ffd83dbSDimitry Andric  * that will end up being encoded in the mach-o header. These are the values
303*5ffd83dbSDimitry Andric  * that can be found in mach/machine.h.
304*5ffd83dbSDimitry Andric  *
305*5ffd83dbSDimitry Andric  * \p out_cputype and \p out_cpusubtype must be non-NULL.
306*5ffd83dbSDimitry Andric  *
307*5ffd83dbSDimitry Andric  * Returns true on error (check lto_get_error_message() for details).
308*5ffd83dbSDimitry Andric  *
309*5ffd83dbSDimitry Andric  * \since LTO_API_VERSION=27
310*5ffd83dbSDimitry Andric  */
311*5ffd83dbSDimitry Andric extern lto_bool_t lto_module_get_macho_cputype(lto_module_t mod,
312*5ffd83dbSDimitry Andric                                                unsigned int *out_cputype,
313*5ffd83dbSDimitry Andric                                                unsigned int *out_cpusubtype);
314*5ffd83dbSDimitry Andric 
315*5ffd83dbSDimitry Andric /**
3160b57cec5SDimitry Andric  * Diagnostic severity.
3170b57cec5SDimitry Andric  *
3180b57cec5SDimitry Andric  * \since LTO_API_VERSION=7
3190b57cec5SDimitry Andric  */
3200b57cec5SDimitry Andric typedef enum {
3210b57cec5SDimitry Andric   LTO_DS_ERROR = 0,
3220b57cec5SDimitry Andric   LTO_DS_WARNING = 1,
3230b57cec5SDimitry Andric   LTO_DS_REMARK = 3, // Added in LTO_API_VERSION=10.
3240b57cec5SDimitry Andric   LTO_DS_NOTE = 2
3250b57cec5SDimitry Andric } lto_codegen_diagnostic_severity_t;
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric /**
3280b57cec5SDimitry Andric  * Diagnostic handler type.
3290b57cec5SDimitry Andric  * \p severity defines the severity.
3300b57cec5SDimitry Andric  * \p diag is the actual diagnostic.
3310b57cec5SDimitry Andric  * The diagnostic is not prefixed by any of severity keyword, e.g., 'error: '.
3320b57cec5SDimitry Andric  * \p ctxt is used to pass the context set with the diagnostic handler.
3330b57cec5SDimitry Andric  *
3340b57cec5SDimitry Andric  * \since LTO_API_VERSION=7
3350b57cec5SDimitry Andric  */
3360b57cec5SDimitry Andric typedef void (*lto_diagnostic_handler_t)(
3370b57cec5SDimitry Andric     lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt);
3380b57cec5SDimitry Andric 
3390b57cec5SDimitry Andric /**
3400b57cec5SDimitry Andric  * Set a diagnostic handler and the related context (void *).
3410b57cec5SDimitry Andric  * This is more general than lto_get_error_message, as the diagnostic handler
3420b57cec5SDimitry Andric  * can be called at anytime within lto.
3430b57cec5SDimitry Andric  *
3440b57cec5SDimitry Andric  * \since LTO_API_VERSION=7
3450b57cec5SDimitry Andric  */
3460b57cec5SDimitry Andric extern void lto_codegen_set_diagnostic_handler(lto_code_gen_t,
3470b57cec5SDimitry Andric                                                lto_diagnostic_handler_t,
3480b57cec5SDimitry Andric                                                void *);
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric /**
3510b57cec5SDimitry Andric  * Instantiates a code generator.
3520b57cec5SDimitry Andric  * Returns NULL on error (check lto_get_error_message() for details).
3530b57cec5SDimitry Andric  *
3540b57cec5SDimitry Andric  * All modules added using \a lto_codegen_add_module() must have been created
3550b57cec5SDimitry Andric  * in the same context as the codegen.
3560b57cec5SDimitry Andric  *
3570b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
3580b57cec5SDimitry Andric  */
3590b57cec5SDimitry Andric extern lto_code_gen_t
3600b57cec5SDimitry Andric lto_codegen_create(void);
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric /**
3630b57cec5SDimitry Andric  * Instantiate a code generator in its own context.
3640b57cec5SDimitry Andric  *
3650b57cec5SDimitry Andric  * Instantiates a code generator in its own context.  Modules added via \a
3660b57cec5SDimitry Andric  * lto_codegen_add_module() must have all been created in the same context,
3670b57cec5SDimitry Andric  * using \a lto_module_create_in_codegen_context().
3680b57cec5SDimitry Andric  *
3690b57cec5SDimitry Andric  * \since LTO_API_VERSION=11
3700b57cec5SDimitry Andric  */
3710b57cec5SDimitry Andric extern lto_code_gen_t
3720b57cec5SDimitry Andric lto_codegen_create_in_local_context(void);
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric /**
3750b57cec5SDimitry Andric  * Frees all code generator and all memory it internally allocated.
3760b57cec5SDimitry Andric  * Upon return the lto_code_gen_t is no longer valid.
3770b57cec5SDimitry Andric  *
3780b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
3790b57cec5SDimitry Andric  */
3800b57cec5SDimitry Andric extern void
3810b57cec5SDimitry Andric lto_codegen_dispose(lto_code_gen_t);
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric /**
3840b57cec5SDimitry Andric  * Add an object module to the set of modules for which code will be generated.
3850b57cec5SDimitry Andric  * Returns true on error (check lto_get_error_message() for details).
3860b57cec5SDimitry Andric  *
3870b57cec5SDimitry Andric  * \c cg and \c mod must both be in the same context.  See \a
3880b57cec5SDimitry Andric  * lto_codegen_create_in_local_context() and \a
3890b57cec5SDimitry Andric  * lto_module_create_in_codegen_context().
3900b57cec5SDimitry Andric  *
3910b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
3920b57cec5SDimitry Andric  */
3930b57cec5SDimitry Andric extern lto_bool_t
3940b57cec5SDimitry Andric lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod);
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric /**
3970b57cec5SDimitry Andric  * Sets the object module for code generation. This will transfer the ownership
3980b57cec5SDimitry Andric  * of the module to the code generator.
3990b57cec5SDimitry Andric  *
4000b57cec5SDimitry Andric  * \c cg and \c mod must both be in the same context.
4010b57cec5SDimitry Andric  *
4020b57cec5SDimitry Andric  * \since LTO_API_VERSION=13
4030b57cec5SDimitry Andric  */
4040b57cec5SDimitry Andric extern void
4050b57cec5SDimitry Andric lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod);
4060b57cec5SDimitry Andric 
4070b57cec5SDimitry Andric /**
4080b57cec5SDimitry Andric  * Sets if debug info should be generated.
4090b57cec5SDimitry Andric  * Returns true on error (check lto_get_error_message() for details).
4100b57cec5SDimitry Andric  *
4110b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
4120b57cec5SDimitry Andric  */
4130b57cec5SDimitry Andric extern lto_bool_t
4140b57cec5SDimitry Andric lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model);
4150b57cec5SDimitry Andric 
4160b57cec5SDimitry Andric /**
4170b57cec5SDimitry Andric  * Sets which PIC code model to generated.
4180b57cec5SDimitry Andric  * Returns true on error (check lto_get_error_message() for details).
4190b57cec5SDimitry Andric  *
4200b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
4210b57cec5SDimitry Andric  */
4220b57cec5SDimitry Andric extern lto_bool_t
4230b57cec5SDimitry Andric lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model);
4240b57cec5SDimitry Andric 
4250b57cec5SDimitry Andric /**
4260b57cec5SDimitry Andric  * Sets the cpu to generate code for.
4270b57cec5SDimitry Andric  *
4280b57cec5SDimitry Andric  * \since LTO_API_VERSION=4
4290b57cec5SDimitry Andric  */
4300b57cec5SDimitry Andric extern void
4310b57cec5SDimitry Andric lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu);
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric /**
4340b57cec5SDimitry Andric  * Sets the location of the assembler tool to run. If not set, libLTO
4350b57cec5SDimitry Andric  * will use gcc to invoke the assembler.
4360b57cec5SDimitry Andric  *
4370b57cec5SDimitry Andric  * \since LTO_API_VERSION=3
4380b57cec5SDimitry Andric  */
4390b57cec5SDimitry Andric extern void
4400b57cec5SDimitry Andric lto_codegen_set_assembler_path(lto_code_gen_t cg, const char* path);
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric /**
4430b57cec5SDimitry Andric  * Sets extra arguments that libLTO should pass to the assembler.
4440b57cec5SDimitry Andric  *
4450b57cec5SDimitry Andric  * \since LTO_API_VERSION=4
4460b57cec5SDimitry Andric  */
4470b57cec5SDimitry Andric extern void
4480b57cec5SDimitry Andric lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
4490b57cec5SDimitry Andric                                int nargs);
4500b57cec5SDimitry Andric 
4510b57cec5SDimitry Andric /**
4520b57cec5SDimitry Andric  * Adds to a list of all global symbols that must exist in the final generated
4530b57cec5SDimitry Andric  * code. If a function is not listed there, it might be inlined into every usage
4540b57cec5SDimitry Andric  * and optimized away.
4550b57cec5SDimitry Andric  *
4560b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
4570b57cec5SDimitry Andric  */
4580b57cec5SDimitry Andric extern void
4590b57cec5SDimitry Andric lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg, const char* symbol);
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric /**
4620b57cec5SDimitry Andric  * Writes a new object file at the specified path that contains the
4630b57cec5SDimitry Andric  * merged contents of all modules added so far.
4640b57cec5SDimitry Andric  * Returns true on error (check lto_get_error_message() for details).
4650b57cec5SDimitry Andric  *
4660b57cec5SDimitry Andric  * \since LTO_API_VERSION=5
4670b57cec5SDimitry Andric  */
4680b57cec5SDimitry Andric extern lto_bool_t
4690b57cec5SDimitry Andric lto_codegen_write_merged_modules(lto_code_gen_t cg, const char* path);
4700b57cec5SDimitry Andric 
4710b57cec5SDimitry Andric /**
4720b57cec5SDimitry Andric  * Generates code for all added modules into one native object file.
4730b57cec5SDimitry Andric  * This calls lto_codegen_optimize then lto_codegen_compile_optimized.
4740b57cec5SDimitry Andric  *
4750b57cec5SDimitry Andric  * On success returns a pointer to a generated mach-o/ELF buffer and
4760b57cec5SDimitry Andric  * length set to the buffer size.  The buffer is owned by the
4770b57cec5SDimitry Andric  * lto_code_gen_t and will be freed when lto_codegen_dispose()
4780b57cec5SDimitry Andric  * is called, or lto_codegen_compile() is called again.
4790b57cec5SDimitry Andric  * On failure, returns NULL (check lto_get_error_message() for details).
4800b57cec5SDimitry Andric  *
4810b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
4820b57cec5SDimitry Andric  */
4830b57cec5SDimitry Andric extern const void*
4840b57cec5SDimitry Andric lto_codegen_compile(lto_code_gen_t cg, size_t* length);
4850b57cec5SDimitry Andric 
4860b57cec5SDimitry Andric /**
4870b57cec5SDimitry Andric  * Generates code for all added modules into one native object file.
4880b57cec5SDimitry Andric  * This calls lto_codegen_optimize then lto_codegen_compile_optimized (instead
4890b57cec5SDimitry Andric  * of returning a generated mach-o/ELF buffer, it writes to a file).
4900b57cec5SDimitry Andric  *
4910b57cec5SDimitry Andric  * The name of the file is written to name. Returns true on error.
4920b57cec5SDimitry Andric  *
4930b57cec5SDimitry Andric  * \since LTO_API_VERSION=5
4940b57cec5SDimitry Andric  */
4950b57cec5SDimitry Andric extern lto_bool_t
4960b57cec5SDimitry Andric lto_codegen_compile_to_file(lto_code_gen_t cg, const char** name);
4970b57cec5SDimitry Andric 
4980b57cec5SDimitry Andric /**
4990b57cec5SDimitry Andric  * Runs optimization for the merged module. Returns true on error.
5000b57cec5SDimitry Andric  *
5010b57cec5SDimitry Andric  * \since LTO_API_VERSION=12
5020b57cec5SDimitry Andric  */
5030b57cec5SDimitry Andric extern lto_bool_t
5040b57cec5SDimitry Andric lto_codegen_optimize(lto_code_gen_t cg);
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric /**
5070b57cec5SDimitry Andric  * Generates code for the optimized merged module into one native object file.
5080b57cec5SDimitry Andric  * It will not run any IR optimizations on the merged module.
5090b57cec5SDimitry Andric  *
5100b57cec5SDimitry Andric  * On success returns a pointer to a generated mach-o/ELF buffer and length set
5110b57cec5SDimitry Andric  * to the buffer size.  The buffer is owned by the lto_code_gen_t and will be
5120b57cec5SDimitry Andric  * freed when lto_codegen_dispose() is called, or
5130b57cec5SDimitry Andric  * lto_codegen_compile_optimized() is called again. On failure, returns NULL
5140b57cec5SDimitry Andric  * (check lto_get_error_message() for details).
5150b57cec5SDimitry Andric  *
5160b57cec5SDimitry Andric  * \since LTO_API_VERSION=12
5170b57cec5SDimitry Andric  */
5180b57cec5SDimitry Andric extern const void*
5190b57cec5SDimitry Andric lto_codegen_compile_optimized(lto_code_gen_t cg, size_t* length);
5200b57cec5SDimitry Andric 
5210b57cec5SDimitry Andric /**
5220b57cec5SDimitry Andric  * Returns the runtime API version.
5230b57cec5SDimitry Andric  *
5240b57cec5SDimitry Andric  * \since LTO_API_VERSION=12
5250b57cec5SDimitry Andric  */
5260b57cec5SDimitry Andric extern unsigned int
5270b57cec5SDimitry Andric lto_api_version(void);
5280b57cec5SDimitry Andric 
5290b57cec5SDimitry Andric /**
5300b57cec5SDimitry Andric  * Sets options to help debug codegen bugs.
5310b57cec5SDimitry Andric  *
532480093f4SDimitry Andric  * This function takes one or more options separated by spaces.
533480093f4SDimitry Andric  * Warning: passing file paths through this function may confuse the argument
534480093f4SDimitry Andric  * parser if the paths contain spaces.
535480093f4SDimitry Andric  *
5360b57cec5SDimitry Andric  * \since prior to LTO_API_VERSION=3
5370b57cec5SDimitry Andric  */
5380b57cec5SDimitry Andric extern void
5390b57cec5SDimitry Andric lto_codegen_debug_options(lto_code_gen_t cg, const char *);
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric /**
542480093f4SDimitry Andric  * Same as the previous function, but takes every option separately through an
543480093f4SDimitry Andric  * array.
544480093f4SDimitry Andric  *
545480093f4SDimitry Andric  * \since prior to LTO_API_VERSION=26
546480093f4SDimitry Andric  */
547480093f4SDimitry Andric extern void lto_codegen_debug_options_array(lto_code_gen_t cg,
548480093f4SDimitry Andric                                             const char *const *, int number);
549480093f4SDimitry Andric 
550480093f4SDimitry Andric /**
5510b57cec5SDimitry Andric  * Initializes LLVM disassemblers.
5520b57cec5SDimitry Andric  * FIXME: This doesn't really belong here.
5530b57cec5SDimitry Andric  *
5540b57cec5SDimitry Andric  * \since LTO_API_VERSION=5
5550b57cec5SDimitry Andric  */
5560b57cec5SDimitry Andric extern void
5570b57cec5SDimitry Andric lto_initialize_disassembler(void);
5580b57cec5SDimitry Andric 
5590b57cec5SDimitry Andric /**
5600b57cec5SDimitry Andric  * Sets if we should run internalize pass during optimization and code
5610b57cec5SDimitry Andric  * generation.
5620b57cec5SDimitry Andric  *
5630b57cec5SDimitry Andric  * \since LTO_API_VERSION=14
5640b57cec5SDimitry Andric  */
5650b57cec5SDimitry Andric extern void
5660b57cec5SDimitry Andric lto_codegen_set_should_internalize(lto_code_gen_t cg,
5670b57cec5SDimitry Andric                                    lto_bool_t ShouldInternalize);
5680b57cec5SDimitry Andric 
5690b57cec5SDimitry Andric /**
5700b57cec5SDimitry Andric  * Set whether to embed uselists in bitcode.
5710b57cec5SDimitry Andric  *
5720b57cec5SDimitry Andric  * Sets whether \a lto_codegen_write_merged_modules() should embed uselists in
5730b57cec5SDimitry Andric  * output bitcode.  This should be turned on for all -save-temps output.
5740b57cec5SDimitry Andric  *
5750b57cec5SDimitry Andric  * \since LTO_API_VERSION=15
5760b57cec5SDimitry Andric  */
5770b57cec5SDimitry Andric extern void
5780b57cec5SDimitry Andric lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
5790b57cec5SDimitry Andric                                       lto_bool_t ShouldEmbedUselists);
5800b57cec5SDimitry Andric 
5818bcb0991SDimitry Andric /** Opaque reference to an LTO input file */
5828bcb0991SDimitry Andric typedef struct LLVMOpaqueLTOInput *lto_input_t;
5838bcb0991SDimitry Andric 
5848bcb0991SDimitry Andric /**
5858bcb0991SDimitry Andric   * Creates an LTO input file from a buffer. The path
5868bcb0991SDimitry Andric   * argument is used for diagnotics as this function
5878bcb0991SDimitry Andric   * otherwise does not know which file the given buffer
5888bcb0991SDimitry Andric   * is associated with.
5898bcb0991SDimitry Andric   *
5908bcb0991SDimitry Andric   * \since LTO_API_VERSION=24
5918bcb0991SDimitry Andric   */
5928bcb0991SDimitry Andric extern lto_input_t lto_input_create(const void *buffer,
5938bcb0991SDimitry Andric                                     size_t buffer_size,
5948bcb0991SDimitry Andric                                     const char *path);
5958bcb0991SDimitry Andric 
5968bcb0991SDimitry Andric /**
5978bcb0991SDimitry Andric   * Frees all memory internally allocated by the LTO input file.
5988bcb0991SDimitry Andric   * Upon return the lto_module_t is no longer valid.
5998bcb0991SDimitry Andric   *
6008bcb0991SDimitry Andric   * \since LTO_API_VERSION=24
6018bcb0991SDimitry Andric   */
6028bcb0991SDimitry Andric extern void lto_input_dispose(lto_input_t input);
6038bcb0991SDimitry Andric 
6048bcb0991SDimitry Andric /**
6058bcb0991SDimitry Andric   * Returns the number of dependent library specifiers
6068bcb0991SDimitry Andric   * for the given LTO input file.
6078bcb0991SDimitry Andric   *
6088bcb0991SDimitry Andric   * \since LTO_API_VERSION=24
6098bcb0991SDimitry Andric   */
6108bcb0991SDimitry Andric extern unsigned lto_input_get_num_dependent_libraries(lto_input_t input);
6118bcb0991SDimitry Andric 
6128bcb0991SDimitry Andric /**
6138bcb0991SDimitry Andric   * Returns the ith dependent library specifier
6148bcb0991SDimitry Andric   * for the given LTO input file. The returned
6158bcb0991SDimitry Andric   * string is not null-terminated.
6168bcb0991SDimitry Andric   *
6178bcb0991SDimitry Andric   * \since LTO_API_VERSION=24
6188bcb0991SDimitry Andric   */
6198bcb0991SDimitry Andric extern const char * lto_input_get_dependent_library(lto_input_t input,
6208bcb0991SDimitry Andric                                                     size_t index,
6218bcb0991SDimitry Andric                                                     size_t *size);
6228bcb0991SDimitry Andric 
6238bcb0991SDimitry Andric /**
6248bcb0991SDimitry Andric  * Returns the list of libcall symbols that can be generated by LTO
6258bcb0991SDimitry Andric  * that might not be visible from the symbol table of bitcode files.
6268bcb0991SDimitry Andric  *
6278bcb0991SDimitry Andric  * \since prior to LTO_API_VERSION=25
6288bcb0991SDimitry Andric  */
6298bcb0991SDimitry Andric extern const char *const *lto_runtime_lib_symbols_list(size_t *size);
6308bcb0991SDimitry Andric 
6310b57cec5SDimitry Andric /**
6320b57cec5SDimitry Andric  * @} // endgoup LLVMCLTO
6330b57cec5SDimitry Andric  * @defgroup LLVMCTLTO ThinLTO
6340b57cec5SDimitry Andric  * @ingroup LLVMC
6350b57cec5SDimitry Andric  *
6360b57cec5SDimitry Andric  * @{
6370b57cec5SDimitry Andric  */
6380b57cec5SDimitry Andric 
6390b57cec5SDimitry Andric /**
6400b57cec5SDimitry Andric  * Type to wrap a single object returned by ThinLTO.
6410b57cec5SDimitry Andric  *
6420b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
6430b57cec5SDimitry Andric  */
6440b57cec5SDimitry Andric typedef struct {
6450b57cec5SDimitry Andric   const char *Buffer;
6460b57cec5SDimitry Andric   size_t Size;
6470b57cec5SDimitry Andric } LTOObjectBuffer;
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric /**
6500b57cec5SDimitry Andric  * Instantiates a ThinLTO code generator.
6510b57cec5SDimitry Andric  * Returns NULL on error (check lto_get_error_message() for details).
6520b57cec5SDimitry Andric  *
6530b57cec5SDimitry Andric  *
6540b57cec5SDimitry Andric  * The ThinLTOCodeGenerator is not intended to be reuse for multiple
6550b57cec5SDimitry Andric  * compilation: the model is that the client adds modules to the generator and
6560b57cec5SDimitry Andric  * ask to perform the ThinLTO optimizations / codegen, and finally destroys the
6570b57cec5SDimitry Andric  * codegenerator.
6580b57cec5SDimitry Andric  *
6590b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
6600b57cec5SDimitry Andric  */
6610b57cec5SDimitry Andric extern thinlto_code_gen_t thinlto_create_codegen(void);
6620b57cec5SDimitry Andric 
6630b57cec5SDimitry Andric /**
6640b57cec5SDimitry Andric  * Frees the generator and all memory it internally allocated.
6650b57cec5SDimitry Andric  * Upon return the thinlto_code_gen_t is no longer valid.
6660b57cec5SDimitry Andric  *
6670b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
6680b57cec5SDimitry Andric  */
6690b57cec5SDimitry Andric extern void thinlto_codegen_dispose(thinlto_code_gen_t cg);
6700b57cec5SDimitry Andric 
6710b57cec5SDimitry Andric /**
6720b57cec5SDimitry Andric  * Add a module to a ThinLTO code generator. Identifier has to be unique among
6730b57cec5SDimitry Andric  * all the modules in a code generator. The data buffer stays owned by the
6740b57cec5SDimitry Andric  * client, and is expected to be available for the entire lifetime of the
6750b57cec5SDimitry Andric  * thinlto_code_gen_t it is added to.
6760b57cec5SDimitry Andric  *
6770b57cec5SDimitry Andric  * On failure, returns NULL (check lto_get_error_message() for details).
6780b57cec5SDimitry Andric  *
6790b57cec5SDimitry Andric  *
6800b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
6810b57cec5SDimitry Andric  */
6820b57cec5SDimitry Andric extern void thinlto_codegen_add_module(thinlto_code_gen_t cg,
6830b57cec5SDimitry Andric                                        const char *identifier, const char *data,
6840b57cec5SDimitry Andric                                        int length);
6850b57cec5SDimitry Andric 
6860b57cec5SDimitry Andric /**
6870b57cec5SDimitry Andric  * Optimize and codegen all the modules added to the codegenerator using
6880b57cec5SDimitry Andric  * ThinLTO. Resulting objects are accessible using thinlto_module_get_object().
6890b57cec5SDimitry Andric  *
6900b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
6910b57cec5SDimitry Andric  */
6920b57cec5SDimitry Andric extern void thinlto_codegen_process(thinlto_code_gen_t cg);
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric /**
6950b57cec5SDimitry Andric  * Returns the number of object files produced by the ThinLTO CodeGenerator.
6960b57cec5SDimitry Andric  *
6970b57cec5SDimitry Andric  * It usually matches the number of input files, but this is not a guarantee of
6980b57cec5SDimitry Andric  * the API and may change in future implementation, so the client should not
6990b57cec5SDimitry Andric  * assume it.
7000b57cec5SDimitry Andric  *
7010b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
7020b57cec5SDimitry Andric  */
7030b57cec5SDimitry Andric extern unsigned int thinlto_module_get_num_objects(thinlto_code_gen_t cg);
7040b57cec5SDimitry Andric 
7050b57cec5SDimitry Andric /**
7060b57cec5SDimitry Andric  * Returns a reference to the ith object file produced by the ThinLTO
7070b57cec5SDimitry Andric  * CodeGenerator.
7080b57cec5SDimitry Andric  *
7090b57cec5SDimitry Andric  * Client should use \p thinlto_module_get_num_objects() to get the number of
7100b57cec5SDimitry Andric  * available objects.
7110b57cec5SDimitry Andric  *
7120b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
7130b57cec5SDimitry Andric  */
7140b57cec5SDimitry Andric extern LTOObjectBuffer thinlto_module_get_object(thinlto_code_gen_t cg,
7150b57cec5SDimitry Andric                                                  unsigned int index);
7160b57cec5SDimitry Andric 
7170b57cec5SDimitry Andric /**
7180b57cec5SDimitry Andric  * Returns the number of object files produced by the ThinLTO CodeGenerator.
7190b57cec5SDimitry Andric  *
7200b57cec5SDimitry Andric  * It usually matches the number of input files, but this is not a guarantee of
7210b57cec5SDimitry Andric  * the API and may change in future implementation, so the client should not
7220b57cec5SDimitry Andric  * assume it.
7230b57cec5SDimitry Andric  *
7240b57cec5SDimitry Andric  * \since LTO_API_VERSION=21
7250b57cec5SDimitry Andric  */
7260b57cec5SDimitry Andric unsigned int thinlto_module_get_num_object_files(thinlto_code_gen_t cg);
7270b57cec5SDimitry Andric 
7280b57cec5SDimitry Andric /**
7290b57cec5SDimitry Andric  * Returns the path to the ith object file produced by the ThinLTO
7300b57cec5SDimitry Andric  * CodeGenerator.
7310b57cec5SDimitry Andric  *
7320b57cec5SDimitry Andric  * Client should use \p thinlto_module_get_num_object_files() to get the number
7330b57cec5SDimitry Andric  * of available objects.
7340b57cec5SDimitry Andric  *
7350b57cec5SDimitry Andric  * \since LTO_API_VERSION=21
7360b57cec5SDimitry Andric  */
7370b57cec5SDimitry Andric const char *thinlto_module_get_object_file(thinlto_code_gen_t cg,
7380b57cec5SDimitry Andric                                            unsigned int index);
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric /**
7410b57cec5SDimitry Andric  * Sets which PIC code model to generate.
7420b57cec5SDimitry Andric  * Returns true on error (check lto_get_error_message() for details).
7430b57cec5SDimitry Andric  *
7440b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
7450b57cec5SDimitry Andric  */
7460b57cec5SDimitry Andric extern lto_bool_t thinlto_codegen_set_pic_model(thinlto_code_gen_t cg,
7470b57cec5SDimitry Andric                                                 lto_codegen_model);
7480b57cec5SDimitry Andric 
7490b57cec5SDimitry Andric /**
7500b57cec5SDimitry Andric  * Sets the path to a directory to use as a storage for temporary bitcode files.
7510b57cec5SDimitry Andric  * The intention is to make the bitcode files available for debugging at various
7520b57cec5SDimitry Andric  * stage of the pipeline.
7530b57cec5SDimitry Andric  *
7540b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
7550b57cec5SDimitry Andric  */
7560b57cec5SDimitry Andric extern void thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg,
7570b57cec5SDimitry Andric                                               const char *save_temps_dir);
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric /**
7600b57cec5SDimitry Andric  * Set the path to a directory where to save generated object files. This
7610b57cec5SDimitry Andric  * path can be used by a linker to request on-disk files instead of in-memory
7620b57cec5SDimitry Andric  * buffers. When set, results are available through
7630b57cec5SDimitry Andric  * thinlto_module_get_object_file() instead of thinlto_module_get_object().
7640b57cec5SDimitry Andric  *
7650b57cec5SDimitry Andric  * \since LTO_API_VERSION=21
7660b57cec5SDimitry Andric  */
7670b57cec5SDimitry Andric void thinlto_set_generated_objects_dir(thinlto_code_gen_t cg,
7680b57cec5SDimitry Andric                                        const char *save_temps_dir);
7690b57cec5SDimitry Andric 
7700b57cec5SDimitry Andric /**
7710b57cec5SDimitry Andric  * Sets the cpu to generate code for.
7720b57cec5SDimitry Andric  *
7730b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
7740b57cec5SDimitry Andric  */
7750b57cec5SDimitry Andric extern void thinlto_codegen_set_cpu(thinlto_code_gen_t cg, const char *cpu);
7760b57cec5SDimitry Andric 
7770b57cec5SDimitry Andric /**
7780b57cec5SDimitry Andric  * Disable CodeGen, only run the stages till codegen and stop. The output will
7790b57cec5SDimitry Andric  * be bitcode.
7800b57cec5SDimitry Andric  *
7810b57cec5SDimitry Andric  * \since LTO_API_VERSION=19
7820b57cec5SDimitry Andric  */
7830b57cec5SDimitry Andric extern void thinlto_codegen_disable_codegen(thinlto_code_gen_t cg,
7840b57cec5SDimitry Andric                                             lto_bool_t disable);
7850b57cec5SDimitry Andric 
7860b57cec5SDimitry Andric /**
7870b57cec5SDimitry Andric  * Perform CodeGen only: disable all other stages.
7880b57cec5SDimitry Andric  *
7890b57cec5SDimitry Andric  * \since LTO_API_VERSION=19
7900b57cec5SDimitry Andric  */
7910b57cec5SDimitry Andric extern void thinlto_codegen_set_codegen_only(thinlto_code_gen_t cg,
7920b57cec5SDimitry Andric                                              lto_bool_t codegen_only);
7930b57cec5SDimitry Andric 
7940b57cec5SDimitry Andric /**
7950b57cec5SDimitry Andric  * Parse -mllvm style debug options.
7960b57cec5SDimitry Andric  *
7970b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
7980b57cec5SDimitry Andric  */
7990b57cec5SDimitry Andric extern void thinlto_debug_options(const char *const *options, int number);
8000b57cec5SDimitry Andric 
8010b57cec5SDimitry Andric /**
8020b57cec5SDimitry Andric  * Test if a module has support for ThinLTO linking.
8030b57cec5SDimitry Andric  *
8040b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
8050b57cec5SDimitry Andric  */
8060b57cec5SDimitry Andric extern lto_bool_t lto_module_is_thinlto(lto_module_t mod);
8070b57cec5SDimitry Andric 
8080b57cec5SDimitry Andric /**
8090b57cec5SDimitry Andric  * Adds a symbol to the list of global symbols that must exist in the final
8100b57cec5SDimitry Andric  * generated code. If a function is not listed there, it might be inlined into
8110b57cec5SDimitry Andric  * every usage and optimized away. For every single module, the functions
8120b57cec5SDimitry Andric  * referenced from code outside of the ThinLTO modules need to be added here.
8130b57cec5SDimitry Andric  *
8140b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
8150b57cec5SDimitry Andric  */
8160b57cec5SDimitry Andric extern void thinlto_codegen_add_must_preserve_symbol(thinlto_code_gen_t cg,
8170b57cec5SDimitry Andric                                                      const char *name,
8180b57cec5SDimitry Andric                                                      int length);
8190b57cec5SDimitry Andric 
8200b57cec5SDimitry Andric /**
8210b57cec5SDimitry Andric  * Adds a symbol to the list of global symbols that are cross-referenced between
8220b57cec5SDimitry Andric  * ThinLTO files. If the ThinLTO CodeGenerator can ensure that every
8230b57cec5SDimitry Andric  * references from a ThinLTO module to this symbol is optimized away, then
8240b57cec5SDimitry Andric  * the symbol can be discarded.
8250b57cec5SDimitry Andric  *
8260b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
8270b57cec5SDimitry Andric  */
8280b57cec5SDimitry Andric extern void thinlto_codegen_add_cross_referenced_symbol(thinlto_code_gen_t cg,
8290b57cec5SDimitry Andric                                                         const char *name,
8300b57cec5SDimitry Andric                                                         int length);
8310b57cec5SDimitry Andric 
8320b57cec5SDimitry Andric /**
8330b57cec5SDimitry Andric  * @} // endgoup LLVMCTLTO
8340b57cec5SDimitry Andric  * @defgroup LLVMCTLTO_CACHING ThinLTO Cache Control
8350b57cec5SDimitry Andric  * @ingroup LLVMCTLTO
8360b57cec5SDimitry Andric  *
8370b57cec5SDimitry Andric  * These entry points control the ThinLTO cache. The cache is intended to
8380b57cec5SDimitry Andric  * support incremental builds, and thus needs to be persistent across builds.
8390b57cec5SDimitry Andric  * The client enables the cache by supplying a path to an existing directory.
8400b57cec5SDimitry Andric  * The code generator will use this to store objects files that may be reused
8410b57cec5SDimitry Andric  * during a subsequent build.
8420b57cec5SDimitry Andric  * To avoid filling the disk space, a few knobs are provided:
8430b57cec5SDimitry Andric  *  - The pruning interval limits the frequency at which the garbage collector
8440b57cec5SDimitry Andric  *    will try to scan the cache directory to prune expired entries.
8450b57cec5SDimitry Andric  *    Setting to a negative number disables the pruning.
8460b57cec5SDimitry Andric  *  - The pruning expiration time indicates to the garbage collector how old an
8470b57cec5SDimitry Andric  *    entry needs to be to be removed.
8480b57cec5SDimitry Andric  *  - Finally, the garbage collector can be instructed to prune the cache until
8490b57cec5SDimitry Andric  *    the occupied space goes below a threshold.
8500b57cec5SDimitry Andric  * @{
8510b57cec5SDimitry Andric  */
8520b57cec5SDimitry Andric 
8530b57cec5SDimitry Andric /**
8540b57cec5SDimitry Andric  * Sets the path to a directory to use as a cache storage for incremental build.
8550b57cec5SDimitry Andric  * Setting this activates caching.
8560b57cec5SDimitry Andric  *
8570b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
8580b57cec5SDimitry Andric  */
8590b57cec5SDimitry Andric extern void thinlto_codegen_set_cache_dir(thinlto_code_gen_t cg,
8600b57cec5SDimitry Andric                                           const char *cache_dir);
8610b57cec5SDimitry Andric 
8620b57cec5SDimitry Andric /**
8630b57cec5SDimitry Andric  * Sets the cache pruning interval (in seconds). A negative value disables the
8640b57cec5SDimitry Andric  * pruning. An unspecified default value will be applied, and a value of 0 will
8650b57cec5SDimitry Andric  * force prunning to occur.
8660b57cec5SDimitry Andric  *
8670b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
8680b57cec5SDimitry Andric  */
8690b57cec5SDimitry Andric extern void thinlto_codegen_set_cache_pruning_interval(thinlto_code_gen_t cg,
8700b57cec5SDimitry Andric                                                        int interval);
8710b57cec5SDimitry Andric 
8720b57cec5SDimitry Andric /**
8730b57cec5SDimitry Andric  * Sets the maximum cache size that can be persistent across build, in terms of
8740b57cec5SDimitry Andric  * percentage of the available space on the disk. Set to 100 to indicate
8750b57cec5SDimitry Andric  * no limit, 50 to indicate that the cache size will not be left over half the
8760b57cec5SDimitry Andric  * available space. A value over 100 will be reduced to 100, a value of 0 will
8770b57cec5SDimitry Andric  * be ignored. An unspecified default value will be applied.
8780b57cec5SDimitry Andric  *
8790b57cec5SDimitry Andric  * The formula looks like:
8800b57cec5SDimitry Andric  *  AvailableSpace = FreeSpace + ExistingCacheSize
8810b57cec5SDimitry Andric  *  NewCacheSize = AvailableSpace * P/100
8820b57cec5SDimitry Andric  *
8830b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
8840b57cec5SDimitry Andric  */
8850b57cec5SDimitry Andric extern void thinlto_codegen_set_final_cache_size_relative_to_available_space(
8860b57cec5SDimitry Andric     thinlto_code_gen_t cg, unsigned percentage);
8870b57cec5SDimitry Andric 
8880b57cec5SDimitry Andric /**
8890b57cec5SDimitry Andric  * Sets the expiration (in seconds) for an entry in the cache. An unspecified
8900b57cec5SDimitry Andric  * default value will be applied. A value of 0 will be ignored.
8910b57cec5SDimitry Andric  *
8920b57cec5SDimitry Andric  * \since LTO_API_VERSION=18
8930b57cec5SDimitry Andric  */
8940b57cec5SDimitry Andric extern void thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg,
8950b57cec5SDimitry Andric                                                        unsigned expiration);
8960b57cec5SDimitry Andric 
8970b57cec5SDimitry Andric /**
8980b57cec5SDimitry Andric  * Sets the maximum size of the cache directory (in bytes). A value over the
8990b57cec5SDimitry Andric  * amount of available space on the disk will be reduced to the amount of
9000b57cec5SDimitry Andric  * available space. An unspecified default value will be applied. A value of 0
9010b57cec5SDimitry Andric  * will be ignored.
9020b57cec5SDimitry Andric  *
9030b57cec5SDimitry Andric  * \since LTO_API_VERSION=22
9040b57cec5SDimitry Andric  */
9050b57cec5SDimitry Andric extern void thinlto_codegen_set_cache_size_bytes(thinlto_code_gen_t cg,
9060b57cec5SDimitry Andric                                                  unsigned max_size_bytes);
9070b57cec5SDimitry Andric 
9080b57cec5SDimitry Andric /**
9090b57cec5SDimitry Andric  * Same as thinlto_codegen_set_cache_size_bytes, except the maximum size is in
9100b57cec5SDimitry Andric  * megabytes (2^20 bytes).
9110b57cec5SDimitry Andric  *
9120b57cec5SDimitry Andric  * \since LTO_API_VERSION=23
9130b57cec5SDimitry Andric  */
9140b57cec5SDimitry Andric extern void
9150b57cec5SDimitry Andric thinlto_codegen_set_cache_size_megabytes(thinlto_code_gen_t cg,
9160b57cec5SDimitry Andric                                          unsigned max_size_megabytes);
9170b57cec5SDimitry Andric 
9180b57cec5SDimitry Andric /**
9190b57cec5SDimitry Andric  * Sets the maximum number of files in the cache directory. An unspecified
9200b57cec5SDimitry Andric  * default value will be applied. A value of 0 will be ignored.
9210b57cec5SDimitry Andric  *
9220b57cec5SDimitry Andric  * \since LTO_API_VERSION=22
9230b57cec5SDimitry Andric  */
9240b57cec5SDimitry Andric extern void thinlto_codegen_set_cache_size_files(thinlto_code_gen_t cg,
9250b57cec5SDimitry Andric                                                  unsigned max_size_files);
9260b57cec5SDimitry Andric 
9270b57cec5SDimitry Andric /**
9280b57cec5SDimitry Andric  * @} // endgroup LLVMCTLTO_CACHING
9290b57cec5SDimitry Andric  */
9300b57cec5SDimitry Andric 
931480093f4SDimitry Andric LLVM_C_EXTERN_C_END
9320b57cec5SDimitry Andric 
9330b57cec5SDimitry Andric #endif /* LLVM_C_LTO_H */
934