1 //===-LTOCodeGenerator.h - LLVM Link Time Optimizer -----------------------===// 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 declares the LTOCodeGenerator class. 10 // 11 // LTO compilation consists of three phases: Pre-IPO, IPO and Post-IPO. 12 // 13 // The Pre-IPO phase compiles source code into bitcode file. The resulting 14 // bitcode files, along with object files and libraries, will be fed to the 15 // linker to through the IPO and Post-IPO phases. By using obj-file extension, 16 // the resulting bitcode file disguises itself as an object file, and therefore 17 // obviates the need of writing a special set of the make-rules only for LTO 18 // compilation. 19 // 20 // The IPO phase perform inter-procedural analyses and optimizations, and 21 // the Post-IPO consists two sub-phases: intra-procedural scalar optimizations 22 // (SOPT), and intra-procedural target-dependent code generator (CG). 23 // 24 // As of this writing, we don't separate IPO and the Post-IPO SOPT. They 25 // are intermingled together, and are driven by a single pass manager (see 26 // PassManagerBuilder::populateLTOPassManager()). 27 // 28 // The "LTOCodeGenerator" is the driver for the IPO and Post-IPO stages. 29 // The "CodeGenerator" here is bit confusing. Don't confuse the "CodeGenerator" 30 // with the machine specific code generator. 31 // 32 //===----------------------------------------------------------------------===// 33 34 #ifndef LLVM_LTO_LTOCODEGENERATOR_H 35 #define LLVM_LTO_LTOCODEGENERATOR_H 36 37 #include "llvm-c/lto.h" 38 #include "llvm/ADT/ArrayRef.h" 39 #include "llvm/ADT/SmallPtrSet.h" 40 #include "llvm/ADT/StringMap.h" 41 #include "llvm/ADT/StringSet.h" 42 #include "llvm/IR/GlobalValue.h" 43 #include "llvm/IR/Module.h" 44 #include "llvm/Support/CommandLine.h" 45 #include "llvm/Support/Error.h" 46 #include "llvm/Support/ToolOutputFile.h" 47 #include "llvm/Target/TargetMachine.h" 48 #include "llvm/Target/TargetOptions.h" 49 #include <string> 50 #include <vector> 51 52 /// Enable global value internalization in LTO. 53 extern llvm::cl::opt<bool> EnableLTOInternalization; 54 55 namespace llvm { 56 template <typename T> class ArrayRef; 57 class LLVMContext; 58 class DiagnosticInfo; 59 class Linker; 60 class Mangler; 61 class MemoryBuffer; 62 class TargetLibraryInfo; 63 class TargetMachine; 64 class raw_ostream; 65 class raw_pwrite_stream; 66 67 //===----------------------------------------------------------------------===// 68 /// C++ class which implements the opaque lto_code_gen_t type. 69 /// 70 struct LTOCodeGenerator { 71 static const char *getVersionString(); 72 73 LTOCodeGenerator(LLVMContext &Context); 74 ~LTOCodeGenerator(); 75 76 /// Merge given module. Return true on success. 77 /// 78 /// Resets \a HasVerifiedInput. 79 bool addModule(struct LTOModule *); 80 81 /// Set the destination module. 82 /// 83 /// Resets \a HasVerifiedInput. 84 void setModule(std::unique_ptr<LTOModule> M); 85 86 void setAsmUndefinedRefs(struct LTOModule *); 87 void setTargetOptions(const TargetOptions &Options); 88 void setDebugInfo(lto_debug_model); 89 void setCodePICModel(Optional<Reloc::Model> Model) { RelocModel = Model; } 90 91 /// Set the file type to be emitted (assembly or object code). 92 /// The default is CGFT_ObjectFile. 93 void setFileType(CodeGenFileType FT) { FileType = FT; } 94 95 void setCpu(StringRef MCpu) { this->MCpu = std::string(MCpu); } 96 void setAttrs(std::vector<std::string> MAttrs) { this->MAttrs = MAttrs; } 97 void setOptLevel(unsigned OptLevel); 98 99 void setShouldInternalize(bool Value) { ShouldInternalize = Value; } 100 void setShouldEmbedUselists(bool Value) { ShouldEmbedUselists = Value; } 101 102 /// Restore linkage of globals 103 /// 104 /// When set, the linkage of globals will be restored prior to code 105 /// generation. That is, a global symbol that had external linkage prior to 106 /// LTO will be emitted with external linkage again; and a local will remain 107 /// local. Note that this option only affects the end result - globals may 108 /// still be internalized in the process of LTO and may be modified and/or 109 /// deleted where legal. 110 /// 111 /// The default behavior will internalize globals (unless on the preserve 112 /// list) and, if parallel code generation is enabled, will externalize 113 /// all locals. 114 void setShouldRestoreGlobalsLinkage(bool Value) { 115 ShouldRestoreGlobalsLinkage = Value; 116 } 117 118 void addMustPreserveSymbol(StringRef Sym) { MustPreserveSymbols.insert(Sym); } 119 120 /// Pass options to the driver and optimization passes. 121 /// 122 /// These options are not necessarily for debugging purpose (the function 123 /// name is misleading). This function should be called before 124 /// LTOCodeGenerator::compilexxx(), and 125 /// LTOCodeGenerator::writeMergedModules(). 126 void setCodeGenDebugOptions(ArrayRef<StringRef> Opts); 127 128 /// Parse the options set in setCodeGenDebugOptions. 129 /// 130 /// Like \a setCodeGenDebugOptions(), this must be called before 131 /// LTOCodeGenerator::compilexxx() and 132 /// LTOCodeGenerator::writeMergedModules(). 133 void parseCodeGenDebugOptions(); 134 135 /// Write the merged module to the file specified by the given path. Return 136 /// true on success. 137 /// 138 /// Calls \a verifyMergedModuleOnce(). 139 bool writeMergedModules(StringRef Path); 140 141 /// Compile the merged module into a *single* output file; the path to output 142 /// file is returned to the caller via argument "name". Return true on 143 /// success. 144 /// 145 /// \note It is up to the linker to remove the intermediate output file. Do 146 /// not try to remove the object file in LTOCodeGenerator's destructor as we 147 /// don't who (LTOCodeGenerator or the output file) will last longer. 148 bool compile_to_file(const char **Name); 149 150 /// As with compile_to_file(), this function compiles the merged module into 151 /// single output file. Instead of returning the output file path to the 152 /// caller (linker), it brings the output to a buffer, and returns the buffer 153 /// to the caller. This function should delete the intermediate file once 154 /// its content is brought to memory. Return NULL if the compilation was not 155 /// successful. 156 std::unique_ptr<MemoryBuffer> compile(); 157 158 /// Optimizes the merged module. Returns true on success. 159 /// 160 /// Calls \a verifyMergedModuleOnce(). 161 bool optimize(); 162 163 /// Compiles the merged optimized module into a single output file. It brings 164 /// the output to a buffer, and returns the buffer to the caller. Return NULL 165 /// if the compilation was not successful. 166 std::unique_ptr<MemoryBuffer> compileOptimized(); 167 168 /// Compile the merged optimized module into out.size() output files each 169 /// representing a linkable partition of the module. If out contains more 170 /// than one element, code generation is done in parallel with out.size() 171 /// threads. Output files will be written to members of out. Returns true on 172 /// success. 173 /// 174 /// Calls \a verifyMergedModuleOnce(). 175 bool compileOptimized(ArrayRef<raw_pwrite_stream *> Out); 176 177 /// Enable the Freestanding mode: indicate that the optimizer should not 178 /// assume builtins are present on the target. 179 void setFreestanding(bool Enabled) { Freestanding = Enabled; } 180 181 void setDisableVerify(bool Value) { DisableVerify = Value; } 182 183 void setDiagnosticHandler(lto_diagnostic_handler_t, void *); 184 185 LLVMContext &getContext() { return Context; } 186 187 void resetMergedModule() { MergedModule.reset(); } 188 void DiagnosticHandler(const DiagnosticInfo &DI); 189 190 private: 191 void initializeLTOPasses(); 192 193 /// Verify the merged module on first call. 194 /// 195 /// Sets \a HasVerifiedInput on first call and doesn't run again on the same 196 /// input. 197 void verifyMergedModuleOnce(); 198 199 bool compileOptimizedToFile(const char **Name); 200 void restoreLinkageForExternals(); 201 void applyScopeRestrictions(); 202 void preserveDiscardableGVs( 203 Module &TheModule, 204 llvm::function_ref<bool(const GlobalValue &)> mustPreserveGV); 205 206 bool determineTarget(); 207 std::unique_ptr<TargetMachine> createTargetMachine(); 208 209 void emitError(const std::string &ErrMsg); 210 void emitWarning(const std::string &ErrMsg); 211 212 void finishOptimizationRemarks(); 213 214 LLVMContext &Context; 215 std::unique_ptr<Module> MergedModule; 216 std::unique_ptr<Linker> TheLinker; 217 std::unique_ptr<TargetMachine> TargetMach; 218 bool EmitDwarfDebugInfo = false; 219 bool ScopeRestrictionsDone = false; 220 bool HasVerifiedInput = false; 221 Optional<Reloc::Model> RelocModel; 222 StringSet<> MustPreserveSymbols; 223 StringSet<> AsmUndefinedRefs; 224 StringMap<GlobalValue::LinkageTypes> ExternalSymbols; 225 std::vector<std::string> CodegenOptions; 226 std::string FeatureStr; 227 std::string MCpu; 228 std::vector<std::string> MAttrs; 229 std::string NativeObjectPath; 230 TargetOptions Options; 231 CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default; 232 const Target *MArch = nullptr; 233 std::string TripleStr; 234 unsigned OptLevel = 2; 235 lto_diagnostic_handler_t DiagHandler = nullptr; 236 void *DiagContext = nullptr; 237 bool ShouldInternalize = EnableLTOInternalization; 238 bool ShouldEmbedUselists = false; 239 bool ShouldRestoreGlobalsLinkage = false; 240 CodeGenFileType FileType = CGFT_ObjectFile; 241 std::unique_ptr<ToolOutputFile> DiagnosticOutputFile; 242 bool Freestanding = false; 243 std::unique_ptr<ToolOutputFile> StatsFile = nullptr; 244 bool DisableVerify = false; 245 }; 246 } 247 #endif 248