LLVM  16.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 // FIXME: populateLTOPassManager no longer exists.
28 //
29 // The "LTOCodeGenerator" is the driver for the IPO and Post-IPO stages.
30 // The "CodeGenerator" here is bit confusing. Don't confuse the "CodeGenerator"
31 // with the machine specific code generator.
32 //
33 //===----------------------------------------------------------------------===//
34 
35 #ifndef LLVM_LTO_LEGACY_LTOCODEGENERATOR_H
36 #define LLVM_LTO_LEGACY_LTOCODEGENERATOR_H
37 
38 #include "llvm-c/lto.h"
39 #include "llvm/ADT/ArrayRef.h"
40 #include "llvm/ADT/SmallPtrSet.h"
41 #include "llvm/ADT/StringMap.h"
42 #include "llvm/ADT/StringSet.h"
43 #include "llvm/IR/GlobalValue.h"
44 #include "llvm/IR/Module.h"
45 #include "llvm/LTO/Config.h"
46 #include "llvm/LTO/LTO.h"
48 #include "llvm/Support/Error.h"
52 #include <string>
53 #include <vector>
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 /// Enable global value internalization in LTO.
68 extern cl::opt<bool> EnableLTOInternalization;
69 
70 //===----------------------------------------------------------------------===//
71 /// C++ class which implements the opaque lto_code_gen_t type.
72 ///
74  static const char *getVersionString();
75 
76  LTOCodeGenerator(LLVMContext &Context);
78 
79  /// Merge given module. Return true on success.
80  ///
81  /// Resets \a HasVerifiedInput.
82  bool addModule(struct LTOModule *);
83 
84  /// Set the destination module.
85  ///
86  /// Resets \a HasVerifiedInput.
87  void setModule(std::unique_ptr<LTOModule> M);
88 
89  void setAsmUndefinedRefs(struct LTOModule *);
93  Config.RelocModel = Model;
94  }
95 
96  /// Set the file type to be emitted (assembly or object code).
97  /// The default is CGFT_ObjectFile.
98  void setFileType(CodeGenFileType FT) { Config.CGFileType = FT; }
99 
100  void setCpu(StringRef MCpu) { Config.CPU = std::string(MCpu); }
101  void setAttrs(std::vector<std::string> MAttrs) { Config.MAttrs = MAttrs; }
102  void setOptLevel(unsigned OptLevel);
103 
104  void setShouldInternalize(bool Value) { ShouldInternalize = Value; }
105  void setShouldEmbedUselists(bool Value) { ShouldEmbedUselists = Value; }
106  void setSaveIRBeforeOptPath(std::string Value) {
107  SaveIRBeforeOptPath = Value;
108  }
109 
110  /// Restore linkage of globals
111  ///
112  /// When set, the linkage of globals will be restored prior to code
113  /// generation. That is, a global symbol that had external linkage prior to
114  /// LTO will be emitted with external linkage again; and a local will remain
115  /// local. Note that this option only affects the end result - globals may
116  /// still be internalized in the process of LTO and may be modified and/or
117  /// deleted where legal.
118  ///
119  /// The default behavior will internalize globals (unless on the preserve
120  /// list) and, if parallel code generation is enabled, will externalize
121  /// all locals.
123  ShouldRestoreGlobalsLinkage = Value;
124  }
125 
126  void addMustPreserveSymbol(StringRef Sym) { MustPreserveSymbols.insert(Sym); }
127 
128  /// Pass options to the driver and optimization passes.
129  ///
130  /// These options are not necessarily for debugging purpose (the function
131  /// name is misleading). This function should be called before
132  /// LTOCodeGenerator::compilexxx(), and
133  /// LTOCodeGenerator::writeMergedModules().
135 
136  /// Parse the options set in setCodeGenDebugOptions.
137  ///
138  /// Like \a setCodeGenDebugOptions(), this must be called before
139  /// LTOCodeGenerator::compilexxx() and
140  /// LTOCodeGenerator::writeMergedModules().
142 
143  /// Write the merged module to the file specified by the given path. Return
144  /// true on success.
145  ///
146  /// Calls \a verifyMergedModuleOnce().
147  bool writeMergedModules(StringRef Path);
148 
149  /// Compile the merged module into a *single* output file; the path to output
150  /// file is returned to the caller via argument "name". Return true on
151  /// success.
152  ///
153  /// \note It is up to the linker to remove the intermediate output file. Do
154  /// not try to remove the object file in LTOCodeGenerator's destructor as we
155  /// don't who (LTOCodeGenerator or the output file) will last longer.
156  bool compile_to_file(const char **Name);
157 
158  /// As with compile_to_file(), this function compiles the merged module into
159  /// single output file. Instead of returning the output file path to the
160  /// caller (linker), it brings the output to a buffer, and returns the buffer
161  /// to the caller. This function should delete the intermediate file once
162  /// its content is brought to memory. Return NULL if the compilation was not
163  /// successful.
164  std::unique_ptr<MemoryBuffer> compile();
165 
166  /// Optimizes the merged module. Returns true on success.
167  ///
168  /// Calls \a verifyMergedModuleOnce().
169  bool optimize();
170 
171  /// Compiles the merged optimized module into a single output file. It brings
172  /// the output to a buffer, and returns the buffer to the caller. Return NULL
173  /// if the compilation was not successful.
174  std::unique_ptr<MemoryBuffer> compileOptimized();
175 
176  /// Compile the merged optimized module \p ParallelismLevel output files each
177  /// representing a linkable partition of the module. If out contains more
178  /// than one element, code generation is done in parallel with \p
179  /// ParallelismLevel threads. Output files will be written to the streams
180  /// created using the \p AddStream callback. Returns true on success.
181  ///
182  /// Calls \a verifyMergedModuleOnce().
183  bool compileOptimized(AddStreamFn AddStream, unsigned ParallelismLevel);
184 
185  /// Enable the Freestanding mode: indicate that the optimizer should not
186  /// assume builtins are present on the target.
187  void setFreestanding(bool Enabled) { Config.Freestanding = Enabled; }
188 
189  void setDisableVerify(bool Value) { Config.DisableVerify = Value; }
190 
192 
194 
196 
197  void resetMergedModule() { MergedModule.reset(); }
198  void DiagnosticHandler(const DiagnosticInfo &DI);
199 
200 private:
201  /// Verify the merged module on first call.
202  ///
203  /// Sets \a HasVerifiedInput on first call and doesn't run again on the same
204  /// input.
205  void verifyMergedModuleOnce();
206 
207  bool compileOptimizedToFile(const char **Name);
208  void restoreLinkageForExternals();
209  void applyScopeRestrictions();
210  void preserveDiscardableGVs(
211  Module &TheModule,
213 
214  bool determineTarget();
215  std::unique_ptr<TargetMachine> createTargetMachine();
216 
217  bool useAIXSystemAssembler();
218  bool runAIXSystemAssembler(SmallString<128> &AssemblyFile);
219 
220  void emitError(const std::string &ErrMsg);
221  void emitWarning(const std::string &ErrMsg);
222 
223  void finishOptimizationRemarks();
224 
226  std::unique_ptr<Module> MergedModule;
227  std::unique_ptr<Linker> TheLinker;
228  std::unique_ptr<TargetMachine> TargetMach;
229  bool EmitDwarfDebugInfo = false;
230  bool ScopeRestrictionsDone = false;
231  bool HasVerifiedInput = false;
232  StringSet<> MustPreserveSymbols;
233  StringSet<> AsmUndefinedRefs;
234  StringMap<GlobalValue::LinkageTypes> ExternalSymbols;
235  std::vector<std::string> CodegenOptions;
236  std::string FeatureStr;
237  std::string NativeObjectPath;
238  const Target *MArch = nullptr;
239  std::string TripleStr;
240  lto_diagnostic_handler_t DiagHandler = nullptr;
241  void *DiagContext = nullptr;
242  bool ShouldInternalize = EnableLTOInternalization;
243  bool ShouldEmbedUselists = false;
244  bool ShouldRestoreGlobalsLinkage = false;
245  std::unique_ptr<ToolOutputFile> DiagnosticOutputFile;
246  std::unique_ptr<ToolOutputFile> StatsFile = nullptr;
247  std::string SaveIRBeforeOptPath;
248 
249  lto::Config Config;
250 };
251 
252 /// A convenience function that calls cl::ParseCommandLineOptions on the given
253 /// set of options.
254 void parseCommandLineOptions(std::vector<std::string> &Options);
255 }
256 #endif
llvm::LTOCodeGenerator::getContext
LLVMContext & getContext()
Definition: LTOCodeGenerator.h:195
llvm::LTOCodeGenerator::setCodeGenDebugOptions
void setCodeGenDebugOptions(ArrayRef< StringRef > Opts)
Pass options to the driver and optimization passes.
Definition: LTOCodeGenerator.cpp:693
llvm::LTOCodeGenerator::setDiagnosticHandler
void setDiagnosticHandler(lto_diagnostic_handler_t, void *)
Definition: LTOCodeGenerator.cpp:756
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
mustPreserveGV
static bool mustPreserveGV(const GlobalValue &GV)
Predicate for Internalize pass.
Definition: AMDGPUTargetMachine.cpp:568
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::LTOCodeGenerator::parseCodeGenDebugOptions
void parseCodeGenDebugOptions()
Parse the options set in setCodeGenDebugOptions.
Definition: LTOCodeGenerator.cpp:698
llvm::TargetOptions
Definition: TargetOptions.h:124
llvm::EnableLTOInternalization
cl::opt< bool > EnableLTOInternalization
Enable global value internalization in LTO.
llvm::lto::Config::DisableVerify
bool DisableVerify
Definition: Config.h:59
llvm::Target
Target - Wrapper for Target specific information.
Definition: TargetRegistry.h:149
llvm::LTOCodeGenerator::LTOCodeGenerator
LTOCodeGenerator(LLVMContext &Context)
Definition: LTOCodeGenerator.cpp:131
Error.h
llvm::LTOCodeGenerator::setDebugInfo
void setDebugInfo(lto_debug_model)
Definition: LTOCodeGenerator.cpp:185
llvm::LTOCodeGenerator::setTargetOptions
void setTargetOptions(const TargetOptions &Options)
Definition: LTOCodeGenerator.cpp:181
Module.h
llvm::lto::Config
LTO configuration.
Definition: Config.h:40
llvm::Optional< Reloc::Model >
llvm::lto::Config::Freestanding
bool Freestanding
Flag to indicate that the optimizer should not assume builtins are present on the target.
Definition: Config.h:66
llvm::LTOCodeGenerator::compile_to_file
bool compile_to_file(const char **Name)
Compile the merged module into a single output file; the path to output file is returned to the calle...
Definition: LTOCodeGenerator.cpp:383
llvm::LTOCodeGenerator::setShouldEmbedUselists
void setShouldEmbedUselists(bool Value)
Definition: LTOCodeGenerator.h:105
llvm::StringSet::insert
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:34
llvm::LTOCodeGenerator::setOptLevel
void setOptLevel(unsigned OptLevel)
Definition: LTOCodeGenerator.cpp:198
llvm::LTOCodeGenerator::addModule
bool addModule(struct LTOModule *)
Merge given module.
Definition: LTOCodeGenerator.cpp:154
llvm::Reloc::Model
Model
Definition: CodeGen.h:22
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
CommandLine.h
GlobalValue.h
llvm::LTOCodeGenerator::compile
std::unique_ptr< MemoryBuffer > compile()
As with compile_to_file(), this function compiles the merged module into single output file.
Definition: LTOCodeGenerator.cpp:390
TargetMachine.h
llvm::LTOCodeGenerator::setCodePICModel
void setCodePICModel(Optional< Reloc::Model > Model)
Definition: LTOCodeGenerator.h:92
llvm::LTOCodeGenerator::compileOptimized
std::unique_ptr< MemoryBuffer > compileOptimized()
Compiles the merged optimized module into a single output file.
Definition: LTOCodeGenerator.cpp:363
Options
const char LLVMTargetMachineRef LLVMPassBuilderOptionsRef Options
Definition: PassBuilderBindings.cpp:48
SmallPtrSet.h
llvm::LTOCodeGenerator::optimize
bool optimize()
Optimizes the merged module.
Definition: LTOCodeGenerator.cpp:590
llvm::DiagnosticInfo
This is the base abstract class for diagnostic reporting in the backend.
Definition: DiagnosticInfo.h:108
llvm::LTOCodeGenerator::setAttrs
void setAttrs(std::vector< std::string > MAttrs)
Definition: LTOCodeGenerator.h:101
llvm::lto::Config::DebugPassManager
bool DebugPassManager
Whether to emit the pass manager debuggging informations.
Definition: Config.h:161
lto.h
llvm::LTOCodeGenerator::setShouldInternalize
void setShouldInternalize(bool Value)
Definition: LTOCodeGenerator.h:104
lto_debug_model
lto_debug_model
Definition: lto.h:79
llvm::StringMap< GlobalValue::LinkageTypes >
llvm::SmallString< 128 >
llvm::LTOCodeGenerator::DiagnosticHandler
void DiagnosticHandler(const DiagnosticInfo &DI)
Definition: LTOCodeGenerator.cpp:713
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLFunctionalExtras.h:36
llvm::LTOCodeGenerator::getVersionString
static const char * getVersionString()
Definition: LTOCodeGenerator.cpp:68
llvm::parseCommandLineOptions
void parseCommandLineOptions(std::vector< std::string > &Options)
A convenience function that calls cl::ParseCommandLineOptions on the given set of options.
Definition: LTOCodeGenerator.cpp:703
llvm::GlobalValue
Definition: GlobalValue.h:44
llvm::LTOCodeGenerator::~LTOCodeGenerator
~LTOCodeGenerator()
llvm::LTOCodeGenerator::setDisableVerify
void setDisableVerify(bool Value)
Definition: LTOCodeGenerator.h:189
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
llvm::LTOCodeGenerator
C++ class which implements the opaque lto_code_gen_t type.
Definition: LTOCodeGenerator.h:73
llvm::LTOCodeGenerator::setFileType
void setFileType(CodeGenFileType FT)
Set the file type to be emitted (assembly or object code).
Definition: LTOCodeGenerator.h:98
ArrayRef.h
ToolOutputFile.h
llvm::AddStreamFn
std::function< Expected< std::unique_ptr< CachedFileStream > >(unsigned Task, const Twine &ModuleName)> AddStreamFn
This type defines the callback to add a file that is generated on the fly.
Definition: Caching.h:42
lto_diagnostic_handler_t
void(* lto_diagnostic_handler_t)(lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt)
Diagnostic handler type.
Definition: lto.h:346
llvm::lto::Config::MAttrs
std::vector< std::string > MAttrs
Definition: Config.h:49
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
TargetOptions.h
llvm::StringSet
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:23
llvm::LTOCodeGenerator::setModule
void setModule(std::unique_ptr< LTOModule > M)
Set the destination module.
Definition: LTOCodeGenerator.cpp:167
llvm::lto::Config::RelocModel
Optional< Reloc::Model > RelocModel
Definition: Config.h:54
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::CodeGenFileType
CodeGenFileType
These enums are meant to be passed into addPassesToEmitFile to indicate what type of file to emit,...
Definition: CodeGen.h:63
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Config.h
llvm::LTOCodeGenerator::addMustPreserveSymbol
void addMustPreserveSymbol(StringRef Sym)
Definition: LTOCodeGenerator.h:126
llvm::LTOCodeGenerator::resetMergedModule
void resetMergedModule()
Definition: LTOCodeGenerator.h:197
StringSet.h
llvm::LTOCodeGenerator::writeMergedModules
bool writeMergedModules(StringRef Path)
Write the merged module to the file specified by the given path.
Definition: LTOCodeGenerator.cpp:219
llvm::LTOCodeGenerator::setFreestanding
void setFreestanding(bool Enabled)
Enable the Freestanding mode: indicate that the optimizer should not assume builtins are present on t...
Definition: LTOCodeGenerator.h:187
Enabled
static bool Enabled
Definition: Statistic.cpp:46
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::lto::Config::CGFileType
CodeGenFileType CGFileType
Definition: Config.h:57
LTO.h
llvm::LTOCodeGenerator::setCpu
void setCpu(StringRef MCpu)
Definition: LTOCodeGenerator.h:100
llvm::LTOCodeGenerator::setDebugPassManager
void setDebugPassManager(bool Enabled)
Definition: LTOCodeGenerator.h:191
llvm::LTOCodeGenerator::setShouldRestoreGlobalsLinkage
void setShouldRestoreGlobalsLinkage(bool Value)
Restore linkage of globals.
Definition: LTOCodeGenerator.h:122
llvm::lto::Config::CPU
std::string CPU
Definition: Config.h:47
StringMap.h
llvm::LTOCodeGenerator::setAsmUndefinedRefs
void setAsmUndefinedRefs(struct LTOModule *)
Definition: LTOCodeGenerator.cpp:149
llvm::LTOCodeGenerator::setSaveIRBeforeOptPath
void setSaveIRBeforeOptPath(std::string Value)
Definition: LTOCodeGenerator.h:106
llvm::LTOModule
C++ class which implements the opaque lto_module_t type.
Definition: LTOModule.h:38
llvm::Value
LLVM Value Representation.
Definition: Value.h:74