1 //===-LTOBackend.h - LLVM Link Time Optimizer Backend ---------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the "backend" phase of LTO, i.e. it performs 10 // optimization and code generation on a loaded module. It is generally used 11 // internally by the LTO class but can also be used independently, for example 12 // to implement a standalone ThinLTO backend. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #ifndef LLVM_LTO_LTOBACKEND_H 17 #define LLVM_LTO_LTOBACKEND_H 18 19 #include "llvm/ADT/MapVector.h" 20 #include "llvm/IR/DiagnosticInfo.h" 21 #include "llvm/IR/ModuleSummaryIndex.h" 22 #include "llvm/LTO/LTO.h" 23 #include "llvm/Support/Compiler.h" 24 #include "llvm/Support/MemoryBuffer.h" 25 #include "llvm/Target/TargetOptions.h" 26 #include "llvm/Transforms/IPO/FunctionImport.h" 27 28 namespace llvm { 29 30 class BitcodeModule; 31 class Error; 32 class Module; 33 class Target; 34 35 namespace lto { 36 37 /// Runs middle-end LTO optimizations on \p Mod. 38 LLVM_ABI bool opt(const Config &Conf, TargetMachine *TM, unsigned Task, 39 Module &Mod, bool IsThinLTO, 40 ModuleSummaryIndex *ExportSummary, 41 const ModuleSummaryIndex *ImportSummary, 42 const std::vector<uint8_t> &CmdArgs); 43 44 /// Runs a regular LTO backend. The regular LTO backend can also act as the 45 /// regular LTO phase of ThinLTO, which may need to access the combined index. 46 LLVM_ABI Error backend(const Config &C, AddStreamFn AddStream, 47 unsigned ParallelCodeGenParallelismLevel, Module &M, 48 ModuleSummaryIndex &CombinedIndex); 49 50 /// Runs a ThinLTO backend. 51 /// If \p ModuleMap is not nullptr, all the module files to be imported have 52 /// already been mapped to memory and the corresponding BitcodeModule objects 53 /// are saved in the ModuleMap. If \p ModuleMap is nullptr, module files will 54 /// be mapped to memory on demand and at any given time during importing, only 55 /// one source module will be kept open at the most. If \p CodeGenOnly is true, 56 /// the backend will skip optimization and only perform code generation. If 57 /// \p IRAddStream is not nullptr, it will be called just before code generation 58 /// to serialize the optimized IR. 59 LLVM_ABI Error 60 thinBackend(const Config &C, unsigned Task, AddStreamFn AddStream, Module &M, 61 const ModuleSummaryIndex &CombinedIndex, 62 const FunctionImporter::ImportMapTy &ImportList, 63 const GVSummaryMapTy &DefinedGlobals, 64 MapVector<StringRef, BitcodeModule> *ModuleMap, bool CodeGenOnly, 65 AddStreamFn IRAddStream = nullptr, 66 const std::vector<uint8_t> &CmdArgs = std::vector<uint8_t>()); 67 68 LLVM_ABI Error 69 finalizeOptimizationRemarks(std::unique_ptr<ToolOutputFile> DiagOutputFile); 70 71 /// Returns the BitcodeModule that is ThinLTO. 72 LLVM_ABI BitcodeModule *findThinLTOModule(MutableArrayRef<BitcodeModule> BMs); 73 74 /// Variant of the above. 75 LLVM_ABI Expected<BitcodeModule> findThinLTOModule(MemoryBufferRef MBRef); 76 77 /// Distributed ThinLTO: collect the referenced modules based on 78 /// module summary and initialize ImportList. Returns false if the 79 /// operation failed. 80 LLVM_ABI bool initImportList(const Module &M, 81 const ModuleSummaryIndex &CombinedIndex, 82 FunctionImporter::ImportMapTy &ImportList); 83 } 84 } 85 86 #endif 87