LLVM  3.7.0
LTOCodeGenerator.h
Go to the documentation of this file.
1 //===-LTOCodeGenerator.h - LLVM Link Time Optimizer -----------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the LTOCodeGenerator class.
11 //
12 // LTO compilation consists of three phases: Pre-IPO, IPO and Post-IPO.
13 //
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
19 // compilation.
20 //
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).
24 //
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()).
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_LTOCODEGENERATOR_H
36 #define LLVM_LTO_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/Linker/Linker.h"
44 #include <string>
45 #include <vector>
46 
47 namespace llvm {
48  class LLVMContext;
49  class DiagnosticInfo;
50  class GlobalValue;
51  class Mangler;
52  class MemoryBuffer;
53  class TargetLibraryInfo;
54  class TargetMachine;
55  class raw_ostream;
56  class raw_pwrite_stream;
57 
58 //===----------------------------------------------------------------------===//
59 /// C++ class which implements the opaque lto_code_gen_t type.
60 ///
62  static const char *getVersionString();
63 
65  LTOCodeGenerator(std::unique_ptr<LLVMContext> Context);
67 
68  // Merge given module, return true on success.
69  bool addModule(struct LTOModule *);
70 
71  // Set the destination module.
72  void setModule(struct LTOModule *);
73 
74  void setTargetOptions(TargetOptions options);
77 
78  void setCpu(const char *mCpu) { MCpu = mCpu; }
79  void setAttr(const char *mAttr) { MAttr = mAttr; }
80  void setOptLevel(unsigned optLevel) { OptLevel = optLevel; }
81 
82  void setShouldInternalize(bool Value) { ShouldInternalize = Value; }
83  void setShouldEmbedUselists(bool Value) { ShouldEmbedUselists = Value; }
84 
85  void addMustPreserveSymbol(StringRef sym) { MustPreserveSymbols[sym] = 1; }
86 
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);
92 
93  // Parse the options set in setCodeGenDebugOptions. Like
94  // setCodeGenDebugOptions, this must be called before
95  // LTOCodeGenerator::compilexxx() and LTOCodeGenerator::writeMergedModules()
97 
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);
101 
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
104  // success.
105  //
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,
110  bool disableInline,
111  bool disableGVNLoadPRE,
112  bool disableVectorization,
113  std::string &errMsg);
114 
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);
124 
125  // Optimizes the merged module. Returns true on success.
126  bool optimize(bool disableInline,
127  bool disableGVNLoadPRE,
128  bool disableVectorization,
129  std::string &errMsg);
130 
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);
135 
137 
138  LLVMContext &getContext() { return Context; }
139 
140 private:
141  void initializeLTOPasses();
142 
143  bool compileOptimized(raw_pwrite_stream &out, std::string &errMsg);
144  bool compileOptimizedToFile(const char **name, std::string &errMsg);
145  void applyScopeRestrictions();
146  void applyRestriction(GlobalValue &GV, ArrayRef<StringRef> Libcalls,
147  std::vector<const char *> &MustPreserveList,
149  Mangler &Mangler);
150  bool determineTarget(std::string &errMsg);
151 
152  static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context);
153 
154  void DiagnosticHandler2(const DiagnosticInfo &DI);
155 
157 
158  void destroyMergedModule();
159  std::unique_ptr<LLVMContext> OwnedContext;
160  LLVMContext &Context;
161  Linker IRLinker;
162  TargetMachine *TargetMach = nullptr;
163  bool EmitDwarfDebugInfo = false;
164  bool ScopeRestrictionsDone = false;
166  StringSet MustPreserveSymbols;
167  StringSet AsmUndefinedRefs;
168  std::vector<char *> CodegenOptions;
169  std::string MCpu;
170  std::string MAttr;
171  std::string NativeObjectPath;
172  TargetOptions Options;
173  unsigned OptLevel = 2;
174  lto_diagnostic_handler_t DiagHandler = nullptr;
175  void *DiagContext = nullptr;
176  LTOModule *OwnedModule = nullptr;
177  bool ShouldInternalize = true;
178  bool ShouldEmbedUselists = false;
179 };
180 }
181 #endif
void setCpu(const char *mCpu)
LLVMContext & getContext()
bool compile_to_file(const char **name, bool disableInline, bool disableGVNLoadPRE, bool disableVectorization, std::string &errMsg)
bool optimize(bool disableInline, bool disableGVNLoadPRE, bool disableVectorization, std::string &errMsg)
Optimize merged modules using various IPO passes.
peephole opts
bool addModule(struct LTOModule *)
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:242
void addMustPreserveSymbol(StringRef sym)
void setTargetOptions(TargetOptions options)
static const char * getVersionString()
This class provides the core functionality of linking in LLVM.
Definition: Linker/Linker.h:27
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
lto_debug_model
Definition: lto.h:73
void setShouldEmbedUselists(bool Value)
bool writeMergedModules(const char *path, std::string &errMsg)
This is the base abstract class for diagnostic reporting in the backend.
void setCodeGenDebugOptions(const char *opts)
setCodeGenDebugOptions - Set codegen debugging options to aid in debugging LTO problems.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
void setModule(struct LTOModule *)
void setAttr(const char *mAttr)
void setShouldInternalize(bool Value)
C++ class which implements the opaque lto_module_t type.
Definition: LTOModule.h:39
void(* lto_diagnostic_handler_t)(lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt)
Diagnostic handler type.
Definition: lto.h:316
C++ class which implements the opaque lto_code_gen_t type.
std::unique_ptr< MemoryBuffer > compileOptimized(std::string &errMsg)
lto_codegen_model
Definition: lto.h:81
An abstract base class for streams implementations that also support a pwrite operation.
Definition: raw_ostream.h:321
LLVM Value Representation.
Definition: Value.h:69
static const char * name
void setCodePICModel(lto_codegen_model)
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:23
Primary interface to the complete machine description for the target machine.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
std::unique_ptr< MemoryBuffer > compile(bool disableInline, bool disableGVNLoadPRE, bool disableVectorization, std::string &errMsg)
void setDiagnosticHandler(lto_diagnostic_handler_t, void *)
void setOptLevel(unsigned optLevel)
void setDebugInfo(lto_debug_model)