1 //===-LTOCodeGenerator.h - LLVM Link Time Optimizer -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the LTOCodeGenerator class.
12 // LTO compilation consists of three phases: Pre-IPO, IPO and Post-IPO.
14 // The Pre-IPO phase compiles source code into bitcode file. The resulting
15 // bitcode files, along with object files and libraries, will be fed to the
16 // linker to through the IPO and Post-IPO phases. By using obj-file extension,
17 // the resulting bitcode file disguises itself as an object file, and therefore
18 // obviates the need of writing a special set of the make-rules only for LTO
21 // The IPO phase perform inter-procedural analyses and optimizations, and
22 // the Post-IPO consists two sub-phases: intra-procedural scalar optimizations
23 // (SOPT), and intra-procedural target-dependent code generator (CG).
25 // As of this writing, we don't separate IPO and the Post-IPO SOPT. They
26 // are intermingled together, and are driven by a single pass manager (see
27 // PassManagerBuilder::populateLTOPassManager()).
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.
33 //===----------------------------------------------------------------------===//
35 #ifndef LLVM_LTO_LTOCODEGENERATOR_H
36 #define LLVM_LTO_LTOCODEGENERATOR_H
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/Linker/Linker.h"
43 #include "llvm/Target/TargetOptions.h"
53 class TargetLibraryInfo;
56 class raw_pwrite_stream;
58 //===----------------------------------------------------------------------===//
59 /// C++ class which implements the opaque lto_code_gen_t type.
61 struct LTOCodeGenerator {
62 static const char *getVersionString();
65 LTOCodeGenerator(std::unique_ptr<LLVMContext> Context);
68 // Merge given module, return true on success.
69 bool addModule(struct LTOModule *);
71 // Set the destination module.
72 void setModule(std::unique_ptr<LTOModule> M);
74 void setTargetOptions(TargetOptions options);
75 void setDebugInfo(lto_debug_model);
76 void setCodePICModel(Reloc::Model model) { RelocModel = model; }
78 void setCpu(const char *mCpu) { MCpu = mCpu; }
79 void setAttr(const char *mAttr) { MAttr = mAttr; }
80 void setOptLevel(unsigned optLevel);
82 void setShouldInternalize(bool Value) { ShouldInternalize = Value; }
83 void setShouldEmbedUselists(bool Value) { ShouldEmbedUselists = Value; }
85 void addMustPreserveSymbol(StringRef sym) { MustPreserveSymbols[sym] = 1; }
87 // To pass options to the driver and optimization passes. These options are
88 // not necessarily for debugging purpose (The function name is misleading).
89 // This function should be called before LTOCodeGenerator::compilexxx(),
90 // and LTOCodeGenerator::writeMergedModules().
91 void setCodeGenDebugOptions(const char *opts);
93 // Parse the options set in setCodeGenDebugOptions. Like
94 // setCodeGenDebugOptions, this must be called before
95 // LTOCodeGenerator::compilexxx() and LTOCodeGenerator::writeMergedModules()
96 void parseCodeGenDebugOptions();
98 // Write the merged module to the file specified by the given path.
99 // Return true on success.
100 bool writeMergedModules(const char *path, std::string &errMsg);
102 // Compile the merged module into a *single* object file; the path to object
103 // file is returned to the caller via argument "name". Return true on
106 // NOTE that it is up to the linker to remove the intermediate object file.
107 // Do not try to remove the object file in LTOCodeGenerator's destructor
108 // as we don't who (LTOCodeGenerator or the obj file) will last longer.
109 bool compile_to_file(const char **name,
111 bool disableGVNLoadPRE,
112 bool disableVectorization,
113 std::string &errMsg);
115 // As with compile_to_file(), this function compiles the merged module into
116 // single object file. Instead of returning the object-file-path to the caller
117 // (linker), it brings the object to a buffer, and return the buffer to the
118 // caller. This function should delete intermediate object file once its content
119 // is brought to memory. Return NULL if the compilation was not successful.
120 std::unique_ptr<MemoryBuffer> compile(bool disableInline,
121 bool disableGVNLoadPRE,
122 bool disableVectorization,
123 std::string &errMsg);
125 // Optimizes the merged module. Returns true on success.
126 bool optimize(bool disableInline,
127 bool disableGVNLoadPRE,
128 bool disableVectorization,
129 std::string &errMsg);
131 // Compiles the merged optimized module into a single object file. It brings
132 // the object to a buffer, and returns the buffer to the caller. Return NULL
133 // if the compilation was not successful.
134 std::unique_ptr<MemoryBuffer> compileOptimized(std::string &errMsg);
136 // Compile the merged optimized module into out.size() object files each
137 // representing a linkable partition of the module. If out contains more than
138 // one element, code generation is done in parallel with out.size() threads.
139 // Object files will be written to members of out. Returns true on success.
140 bool compileOptimized(ArrayRef<raw_pwrite_stream *> out, std::string &errMsg);
142 void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
144 LLVMContext &getContext() { return Context; }
147 void initializeLTOPasses();
149 bool compileOptimizedToFile(const char **name, std::string &errMsg);
150 void applyScopeRestrictions();
151 void applyRestriction(GlobalValue &GV, ArrayRef<StringRef> Libcalls,
152 std::vector<const char *> &MustPreserveList,
153 SmallPtrSetImpl<GlobalValue *> &AsmUsed,
155 bool determineTarget(std::string &errMsg);
157 static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context);
159 void DiagnosticHandler2(const DiagnosticInfo &DI);
161 typedef StringMap<uint8_t> StringSet;
163 std::unique_ptr<LLVMContext> OwnedContext;
164 LLVMContext &Context;
165 std::unique_ptr<Module> MergedModule;
167 std::unique_ptr<TargetMachine> TargetMach;
168 bool EmitDwarfDebugInfo = false;
169 bool ScopeRestrictionsDone = false;
170 Reloc::Model RelocModel = Reloc::Default;
171 StringSet MustPreserveSymbols;
172 StringSet AsmUndefinedRefs;
173 std::vector<std::string> CodegenOptions;
174 std::string FeatureStr;
177 std::string NativeObjectPath;
178 TargetOptions Options;
179 CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
180 unsigned OptLevel = 2;
181 lto_diagnostic_handler_t DiagHandler = nullptr;
182 void *DiagContext = nullptr;
183 bool ShouldInternalize = true;
184 bool ShouldEmbedUselists = false;