llvm/llvm/tools/lto/lto.cpp

//===-lto.cpp - LLVM Link Time Optimizer ----------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements the Link Time Optimization library. This library is
// intended to be used by linker to optimize code at link time.
//
//===----------------------------------------------------------------------===//

#include "llvm-c/lto.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/CodeGen/CommandFlags.h"
#include "llvm/IR/DiagnosticInfo.h"
#include "llvm/IR/DiagnosticPrinter.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/LTO/LTO.h"
#include "llvm/LTO/legacy/LTOCodeGenerator.h"
#include "llvm/LTO/legacy/LTOModule.h"
#include "llvm/LTO/legacy/ThinLTOCodeGenerator.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"

usingnamespacellvm;

static codegen::RegisterCodeGenFlags CGF;

// extra command-line flags needed for LTOCodeGenerator
static cl::opt<char>
    OptLevel("O",
             cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
                      "(default = '-O2')"),
             cl::Prefix, cl::init('2'));

static cl::opt<bool> EnableFreestanding(
    "lto-freestanding", cl::init(false),
    cl::desc("Enable Freestanding (disable builtins / TLI) during LTO"));

#ifdef NDEBUG
static bool VerifyByDefault =;
#else
static bool VerifyByDefault = true;
#endif

static cl::opt<bool> DisableVerify(
    "disable-llvm-verifier", cl::init(!VerifyByDefault),
    cl::desc("Don't run the LLVM verifier during the optimization pipeline"));

// Holds most recent error string.
// *** Not thread safe ***
static std::string sLastErrorString;

// Holds the initialization state of the LTO module.
// *** Not thread safe ***
static bool initialized =;

// Represent the state of parsing command line debug options.
static enum class OptParsingState {} optionParsingState =;

static LLVMContext *LTOContext =;

struct LTOToolDiagnosticHandler : public DiagnosticHandler {};

static SmallVector<const char *> RuntimeLibcallSymbols;

// Initialize the configured targets if they have not been initialized.
static void lto_initialize() {}

namespace {

static void handleLibLTODiagnostic(lto_codegen_diagnostic_severity_t Severity,
                                   const char *Msg, void *) {}

// This derived class owns the native object file. This helps implement the
// libLTO API semantics, which require that the code generator owns the object
// file.
struct LibLTOCodeGenerator : LTOCodeGenerator {};

}

DEFINE_SIMPLE_CONVERSION_FUNCTIONS()
DEFINE_SIMPLE_CONVERSION_FUNCTIONS()
DEFINE_SIMPLE_CONVERSION_FUNCTIONS()

// Convert the subtarget features into a string to pass to LTOCodeGenerator.
static void lto_add_attrs(lto_code_gen_t cg) {}

extern const char* lto_get_version() {}

const char* lto_get_error_message() {}

bool lto_module_is_object_file(const char* path) {}

bool lto_module_is_object_file_for_target(const char* path,
                                          const char* target_triplet_prefix) {}

bool lto_module_has_objc_category(const void *mem, size_t length) {}

bool lto_module_is_object_file_in_memory(const void* mem, size_t length) {}

bool
lto_module_is_object_file_in_memory_for_target(const void* mem,
                                            size_t length,
                                            const char* target_triplet_prefix) {}

lto_module_t lto_module_create(const char* path) {}

lto_module_t lto_module_create_from_fd(int fd, const char *path, size_t size) {}

lto_module_t lto_module_create_from_fd_at_offset(int fd, const char *path,
                                                 size_t file_size,
                                                 size_t map_size,
                                                 off_t offset) {}

lto_module_t lto_module_create_from_memory(const void* mem, size_t length) {}

lto_module_t lto_module_create_from_memory_with_path(const void* mem,
                                                     size_t length,
                                                     const char *path) {}

lto_module_t lto_module_create_in_local_context(const void *mem, size_t length,
                                                const char *path) {}

lto_module_t lto_module_create_in_codegen_context(const void *mem,
                                                  size_t length,
                                                  const char *path,
                                                  lto_code_gen_t cg) {}

void lto_module_dispose(lto_module_t mod) {}

const char* lto_module_get_target_triple(lto_module_t mod) {}

void lto_module_set_target_triple(lto_module_t mod, const char *triple) {}

unsigned int lto_module_get_num_symbols(lto_module_t mod) {}

const char* lto_module_get_symbol_name(lto_module_t mod, unsigned int index) {}

lto_symbol_attributes lto_module_get_symbol_attribute(lto_module_t mod,
                                                      unsigned int index) {}

const char* lto_module_get_linkeropts(lto_module_t mod) {}

lto_bool_t lto_module_get_macho_cputype(lto_module_t mod,
                                        unsigned int *out_cputype,
                                        unsigned int *out_cpusubtype) {}

void lto_codegen_set_diagnostic_handler(lto_code_gen_t cg,
                                        lto_diagnostic_handler_t diag_handler,
                                        void *ctxt) {}

static lto_code_gen_t createCodeGen(bool InLocalContext) {}

lto_code_gen_t lto_codegen_create(void) {}

lto_code_gen_t lto_codegen_create_in_local_context(void) {}

void lto_codegen_dispose(lto_code_gen_t cg) {}

bool lto_codegen_add_module(lto_code_gen_t cg, lto_module_t mod) {}

void lto_codegen_set_module(lto_code_gen_t cg, lto_module_t mod) {}

bool lto_codegen_set_debug_model(lto_code_gen_t cg, lto_debug_model debug) {}

bool lto_codegen_set_pic_model(lto_code_gen_t cg, lto_codegen_model model) {}

void lto_codegen_set_cpu(lto_code_gen_t cg, const char *cpu) {}

void lto_codegen_set_assembler_path(lto_code_gen_t cg, const char *path) {}

void lto_codegen_set_assembler_args(lto_code_gen_t cg, const char **args,
                                    int nargs) {}

void lto_codegen_add_must_preserve_symbol(lto_code_gen_t cg,
                                          const char *symbol) {}

static void maybeParseOptions(lto_code_gen_t cg) {}

bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {}

const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {}

bool lto_codegen_optimize(lto_code_gen_t cg) {}

const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {}

bool lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) {}

void lto_set_debug_options(const char *const *options, int number) {}

void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {}

void lto_codegen_debug_options_array(lto_code_gen_t cg,
                                     const char *const *options, int number) {}

unsigned int lto_api_version() {}

void lto_codegen_set_should_internalize(lto_code_gen_t cg,
                                        bool ShouldInternalize) {}

void lto_codegen_set_should_embed_uselists(lto_code_gen_t cg,
                                           lto_bool_t ShouldEmbedUselists) {}

lto_bool_t lto_module_has_ctor_dtor(lto_module_t mod) {}

// ThinLTO API below

thinlto_code_gen_t thinlto_create_codegen(void) {}

void thinlto_codegen_dispose(thinlto_code_gen_t cg) {}

void thinlto_codegen_add_module(thinlto_code_gen_t cg, const char *Identifier,
                                const char *Data, int Length) {}

void thinlto_codegen_process(thinlto_code_gen_t cg) {}

unsigned int thinlto_module_get_num_objects(thinlto_code_gen_t cg) {}
LTOObjectBuffer thinlto_module_get_object(thinlto_code_gen_t cg,
                                          unsigned int index) {}

unsigned int thinlto_module_get_num_object_files(thinlto_code_gen_t cg) {}
const char *thinlto_module_get_object_file(thinlto_code_gen_t cg,
                                           unsigned int index) {}

void thinlto_codegen_disable_codegen(thinlto_code_gen_t cg,
                                     lto_bool_t disable) {}

void thinlto_codegen_set_codegen_only(thinlto_code_gen_t cg,
                                      lto_bool_t CodeGenOnly) {}

void thinlto_debug_options(const char *const *options, int number) {}

lto_bool_t lto_module_is_thinlto(lto_module_t mod) {}

void thinlto_codegen_add_must_preserve_symbol(thinlto_code_gen_t cg,
                                              const char *Name, int Length) {}

void thinlto_codegen_add_cross_referenced_symbol(thinlto_code_gen_t cg,
                                                 const char *Name, int Length) {}

void thinlto_codegen_set_cpu(thinlto_code_gen_t cg, const char *cpu) {}

void thinlto_codegen_set_cache_dir(thinlto_code_gen_t cg,
                                   const char *cache_dir) {}

void thinlto_codegen_set_cache_pruning_interval(thinlto_code_gen_t cg,
                                                int interval) {}

void thinlto_codegen_set_cache_entry_expiration(thinlto_code_gen_t cg,
                                                unsigned expiration) {}

void thinlto_codegen_set_final_cache_size_relative_to_available_space(
    thinlto_code_gen_t cg, unsigned Percentage) {}

void thinlto_codegen_set_cache_size_bytes(
    thinlto_code_gen_t cg, unsigned MaxSizeBytes) {}

void thinlto_codegen_set_cache_size_megabytes(
    thinlto_code_gen_t cg, unsigned MaxSizeMegabytes) {}

void thinlto_codegen_set_cache_size_files(
    thinlto_code_gen_t cg, unsigned MaxSizeFiles) {}

void thinlto_codegen_set_savetemps_dir(thinlto_code_gen_t cg,
                                       const char *save_temps_dir) {}

void thinlto_set_generated_objects_dir(thinlto_code_gen_t cg,
                                       const char *save_temps_dir) {}

lto_bool_t thinlto_codegen_set_pic_model(thinlto_code_gen_t cg,
                                         lto_codegen_model model) {}

DEFINE_SIMPLE_CONVERSION_FUNCTIONS()

lto_input_t lto_input_create(const void *buffer, size_t buffer_size, const char *path) {}

void lto_input_dispose(lto_input_t input) {}

extern unsigned lto_input_get_num_dependent_libraries(lto_input_t input) {}

extern const char *lto_input_get_dependent_library(lto_input_t input,
                                                   size_t index,
                                                   size_t *size) {}

extern const char *const *lto_runtime_lib_symbols_list(size_t *size) {}