1 //===-LTOCodeGenerator.h - LLVM Link Time Optimizer -----------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file declares the LTOCodeGenerator class.
11 // LTO compilation consists of three phases: Pre-IPO, IPO and Post-IPO.
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
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).
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()).
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.
32 //===----------------------------------------------------------------------===//
34 #ifndef LLVM_LTO_LTOCODEGENERATOR_H
35 #define LLVM_LTO_LTOCODEGENERATOR_H
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"
52 /// Enable global value internalization in LTO.
53 extern llvm::cl::opt<bool> EnableLTOInternalization;
56 template <typename T> class ArrayRef;
62 class TargetLibraryInfo;
65 class raw_pwrite_stream;
67 //===----------------------------------------------------------------------===//
68 /// C++ class which implements the opaque lto_code_gen_t type.
70 struct LTOCodeGenerator {
71 static const char *getVersionString();
73 LTOCodeGenerator(LLVMContext &Context);
76 /// Merge given module. Return true on success.
78 /// Resets \a HasVerifiedInput.
79 bool addModule(struct LTOModule *);
81 /// Set the destination module.
83 /// Resets \a HasVerifiedInput.
84 void setModule(std::unique_ptr<LTOModule> M);
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; }
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; }
95 void setCpu(StringRef MCpu) { this->MCpu = std::string(MCpu); }
96 void setAttr(StringRef MAttr) { this->MAttr = std::string(MAttr); }
97 void setOptLevel(unsigned OptLevel);
99 void setShouldInternalize(bool Value) { ShouldInternalize = Value; }
100 void setShouldEmbedUselists(bool Value) { ShouldEmbedUselists = Value; }
102 /// Restore linkage of globals
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.
111 /// The default behavior will internalize globals (unless on the preserve
112 /// list) and, if parallel code generation is enabled, will externalize
114 void setShouldRestoreGlobalsLinkage(bool Value) {
115 ShouldRestoreGlobalsLinkage = Value;
118 void addMustPreserveSymbol(StringRef Sym) { MustPreserveSymbols.insert(Sym); }
120 /// Pass options to the driver and optimization passes.
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);
128 /// Parse the options set in setCodeGenDebugOptions.
130 /// Like \a setCodeGenDebugOptions(), this must be called before
131 /// LTOCodeGenerator::compilexxx() and
132 /// LTOCodeGenerator::writeMergedModules().
133 void parseCodeGenDebugOptions();
135 /// Write the merged module to the file specified by the given path. Return
138 /// Calls \a verifyMergedModuleOnce().
139 bool writeMergedModules(StringRef Path);
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
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, bool DisableVerify,
149 bool DisableInline, bool DisableGVNLoadPRE,
150 bool DisableVectorization);
152 /// As with compile_to_file(), this function compiles the merged module into
153 /// single output file. Instead of returning the output file path to the
154 /// caller (linker), it brings the output to a buffer, and returns the buffer
155 /// to the caller. This function should delete the intermediate file once
156 /// its content is brought to memory. Return NULL if the compilation was not
158 std::unique_ptr<MemoryBuffer> compile(bool DisableVerify, bool DisableInline,
159 bool DisableGVNLoadPRE,
160 bool DisableVectorization);
162 /// Optimizes the merged module. Returns true on success.
164 /// Calls \a verifyMergedModuleOnce().
165 bool optimize(bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE,
166 bool DisableVectorization);
168 /// Compiles the merged optimized module into a single output file. It brings
169 /// the output to a buffer, and returns the buffer to the caller. Return NULL
170 /// if the compilation was not successful.
171 std::unique_ptr<MemoryBuffer> compileOptimized();
173 /// Compile the merged optimized module into out.size() output files each
174 /// representing a linkable partition of the module. If out contains more
175 /// than one element, code generation is done in parallel with out.size()
176 /// threads. Output files will be written to members of out. Returns true on
179 /// Calls \a verifyMergedModuleOnce().
180 bool compileOptimized(ArrayRef<raw_pwrite_stream *> Out);
182 /// Enable the Freestanding mode: indicate that the optimizer should not
183 /// assume builtins are present on the target.
184 void setFreestanding(bool Enabled) { Freestanding = Enabled; }
186 void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
188 LLVMContext &getContext() { return Context; }
190 void resetMergedModule() { MergedModule.reset(); }
191 void DiagnosticHandler(const DiagnosticInfo &DI);
194 void initializeLTOPasses();
196 /// Verify the merged module on first call.
198 /// Sets \a HasVerifiedInput on first call and doesn't run again on the same
200 void verifyMergedModuleOnce();
202 bool compileOptimizedToFile(const char **Name);
203 void restoreLinkageForExternals();
204 void applyScopeRestrictions();
205 void preserveDiscardableGVs(
207 llvm::function_ref<bool(const GlobalValue &)> mustPreserveGV);
209 bool determineTarget();
210 std::unique_ptr<TargetMachine> createTargetMachine();
212 void emitError(const std::string &ErrMsg);
213 void emitWarning(const std::string &ErrMsg);
215 void finishOptimizationRemarks();
217 LLVMContext &Context;
218 std::unique_ptr<Module> MergedModule;
219 std::unique_ptr<Linker> TheLinker;
220 std::unique_ptr<TargetMachine> TargetMach;
221 bool EmitDwarfDebugInfo = false;
222 bool ScopeRestrictionsDone = false;
223 bool HasVerifiedInput = false;
224 Optional<Reloc::Model> RelocModel;
225 StringSet<> MustPreserveSymbols;
226 StringSet<> AsmUndefinedRefs;
227 StringMap<GlobalValue::LinkageTypes> ExternalSymbols;
228 std::vector<std::string> CodegenOptions;
229 std::string FeatureStr;
232 std::string NativeObjectPath;
233 TargetOptions Options;
234 CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
235 const Target *MArch = nullptr;
236 std::string TripleStr;
237 unsigned OptLevel = 2;
238 lto_diagnostic_handler_t DiagHandler = nullptr;
239 void *DiagContext = nullptr;
240 bool ShouldInternalize = EnableLTOInternalization;
241 bool ShouldEmbedUselists = false;
242 bool ShouldRestoreGlobalsLinkage = false;
243 CodeGenFileType FileType = CGFT_ObjectFile;
244 std::unique_ptr<ToolOutputFile> DiagnosticOutputFile;
245 bool Freestanding = false;
246 std::unique_ptr<ToolOutputFile> StatsFile = nullptr;