LLVM  12.0.0git
LTOCodeGenerator.h
Go to the documentation of this file.
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"
45 #include "llvm/Support/Error.h"
49 #include <string>
50 #include <vector>
51 
52 /// Enable global value internalization in LTO.
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 ///
71  static const char *getVersionString();
72 
73  LTOCodeGenerator(LLVMContext &Context);
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);
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 setAttr(StringRef MAttr) { this->MAttr = std::string(MAttr); }
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.
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().
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().
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, bool DisableVerify,
149  bool DisableInline, bool DisableGVNLoadPRE,
150  bool DisableVectorization);
151 
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
157  /// successful.
158  std::unique_ptr<MemoryBuffer> compile(bool DisableVerify, bool DisableInline,
159  bool DisableGVNLoadPRE,
160  bool DisableVectorization);
161 
162  /// Optimizes the merged module. Returns true on success.
163  ///
164  /// Calls \a verifyMergedModuleOnce().
165  bool optimize(bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE,
166  bool DisableVectorization);
167 
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();
172 
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
177  /// success.
178  ///
179  /// Calls \a verifyMergedModuleOnce().
181 
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; }
185 
187 
188  LLVMContext &getContext() { return Context; }
189 
190  void resetMergedModule() { MergedModule.reset(); }
191  void DiagnosticHandler(const DiagnosticInfo &DI);
192 
193 private:
194  void initializeLTOPasses();
195 
196  /// Verify the merged module on first call.
197  ///
198  /// Sets \a HasVerifiedInput on first call and doesn't run again on the same
199  /// input.
200  void verifyMergedModuleOnce();
201 
202  bool compileOptimizedToFile(const char **Name);
203  void restoreLinkageForExternals();
204  void applyScopeRestrictions();
205  void preserveDiscardableGVs(
206  Module &TheModule,
208 
209  bool determineTarget();
210  std::unique_ptr<TargetMachine> createTargetMachine();
211 
212  void emitError(const std::string &ErrMsg);
213  void emitWarning(const std::string &ErrMsg);
214 
215  void finishOptimizationRemarks();
216 
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;
230  std::string MCpu;
231  std::string MAttr;
232  std::string NativeObjectPath;
233  TargetOptions Options;
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;
247 };
248 }
249 #endif
This class represents lattice values for constants.
Definition: AllocatorList.h:23
LLVMContext & getContext()
std::unique_ptr< MemoryBuffer > compileOptimized()
Compiles the merged optimized module into a single output file.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:67
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:176
bool addModule(struct LTOModule *)
Merge given module.
bool optimize(bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE, bool DisableVectorization)
Optimizes the merged module.
void setShouldRestoreGlobalsLinkage(bool Value)
Restore linkage of globals.
void setAttr(StringRef MAttr)
void setCodeGenDebugOptions(ArrayRef< StringRef > Opts)
Pass options to the driver and optimization passes.
std::unique_ptr< MemoryBuffer > compile(bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE, bool DisableVectorization)
As with compile_to_file(), this function compiles the merged module into single output file...
bool writeMergedModules(StringRef Path)
Write the merged module to the file specified by the given path.
static const char * getVersionString()
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
lto_debug_model
Definition: lto.h:79
void setShouldEmbedUselists(bool Value)
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit...
Definition: CodeGen.h:63
void setCodePICModel(Optional< Reloc::Model > Model)
This is the base abstract class for diagnostic reporting in the backend.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
void setOptLevel(unsigned OptLevel)
void setTargetOptions(const TargetOptions &Options)
void setShouldInternalize(bool Value)
C++ class which implements the opaque lto_module_t type.
Definition: LTOModule.h:38
llvm::cl::opt< bool > EnableLTOInternalization
Enable global value internalization in LTO.
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:33
C++ class which implements the opaque lto_code_gen_t type.
void(* lto_diagnostic_handler_t)(lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt)
Diagnostic handler type.
Definition: lto.h:336
void setCpu(StringRef MCpu)
Module.h This file contains the declarations for the Module class.
static bool Enabled
Definition: Statistic.cpp:50
Target - Wrapper for Target specific information.
void addMustPreserveSymbol(StringRef Sym)
void DiagnosticHandler(const DiagnosticInfo &DI)
bool compile_to_file(const char **Name, bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE, bool DisableVectorization)
Compile the merged module into a single output file; the path to output file is returned to the calle...
static bool mustPreserveGV(const GlobalValue &GV)
Predicate for Internalize pass.
void setAsmUndefinedRefs(struct LTOModule *)
LLVM Value Representation.
Definition: Value.h:74
LTOCodeGenerator(LLVMContext &Context)
void setFileType(CodeGenFileType FT)
Set the file type to be emitted (assembly or object code).
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:22
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:57
void parseCodeGenDebugOptions()
Parse the options set in setCodeGenDebugOptions.
void setModule(std::unique_ptr< LTOModule > M)
Set the destination module.
void setFreestanding(bool Enabled)
Enable the Freestanding mode: indicate that the optimizer should not assume builtins are present on t...
void setDiagnosticHandler(lto_diagnostic_handler_t, void *)
void setDebugInfo(lto_debug_model)