Use ELFOSABI_NONE instead of ELFOSABI_LINUX.
[oota-llvm.git] / include / llvm / LTO / LTOCodeGenerator.h
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"
43 #include "llvm/Target/TargetOptions.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 ///
61 struct LTOCodeGenerator {
62   static const char *getVersionString();
63
64   LTOCodeGenerator();
65   LTOCodeGenerator(std::unique_ptr<LLVMContext> Context);
66   ~LTOCodeGenerator();
67
68   /// Merge given module.  Return true on success.
69   bool addModule(struct LTOModule *);
70
71   /// Set the destination module.
72   void setModule(std::unique_ptr<LTOModule> M);
73
74   void setTargetOptions(TargetOptions Options);
75   void setDebugInfo(lto_debug_model);
76   void setCodePICModel(Reloc::Model Model) { RelocModel = Model; }
77
78   void setCpu(const char *MCpu) { this->MCpu = MCpu; }
79   void setAttr(const char *MAttr) { this->MAttr = MAttr; }
80   void setOptLevel(unsigned 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   /// Pass options to the driver and optimization passes.
88   ///
89   /// These options are not necessarily for debugging purpose (the function
90   /// name is misleading).  This function should be called before
91   /// LTOCodeGenerator::compilexxx(), and
92   /// LTOCodeGenerator::writeMergedModules().
93   void setCodeGenDebugOptions(const char *Opts);
94
95   /// Parse the options set in setCodeGenDebugOptions.
96   ///
97   /// Like \a setCodeGenDebugOptions(), this must be called before
98   /// LTOCodeGenerator::compilexxx() and
99   /// LTOCodeGenerator::writeMergedModules().
100   void parseCodeGenDebugOptions();
101
102   /// Write the merged module to the file specified by the given path.  Return
103   /// true on success.
104   bool writeMergedModules(const char *Path, std::string &ErrMsg);
105
106   /// Compile the merged module into a *single* object file; the path to object
107   /// file is returned to the caller via argument "name". Return true on
108   /// success.
109   ///
110   /// \note It is up to the linker to remove the intermediate object file.  Do
111   /// not try to remove the object file in LTOCodeGenerator's destructor as we
112   /// don't who (LTOCodeGenerator or the obj file) will last longer.
113   bool compile_to_file(const char **Name, bool DisableVerify,
114                        bool DisableInline, bool DisableGVNLoadPRE,
115                        bool DisableVectorization, std::string &ErrMsg);
116
117   /// As with compile_to_file(), this function compiles the merged module into
118   /// single object file. Instead of returning the object-file-path to the
119   /// caller (linker), it brings the object to a buffer, and return the buffer
120   /// to the caller. This function should delete intermediate object file once
121   /// its content is brought to memory. Return NULL if the compilation was not
122   /// successful.
123   std::unique_ptr<MemoryBuffer> compile(bool DisableVerify, bool DisableInline,
124                                         bool DisableGVNLoadPRE,
125                                         bool DisableVectorization,
126                                         std::string &errMsg);
127
128   /// Optimizes the merged module.  Returns true on success.
129   bool optimize(bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE,
130                 bool DisableVectorization, std::string &ErrMsg);
131
132   /// Compiles the merged optimized module into a single object file. It brings
133   /// the object to a buffer, and returns the buffer to the caller. Return NULL
134   /// if the compilation was not successful.
135   std::unique_ptr<MemoryBuffer> compileOptimized(std::string &ErrMsg);
136
137   /// Compile the merged optimized module into out.size() object files each
138   /// representing a linkable partition of the module. If out contains more
139   /// than one element, code generation is done in parallel with out.size()
140   /// threads.  Object files will be written to members of out. Returns true on
141   /// success.
142   bool compileOptimized(ArrayRef<raw_pwrite_stream *> Out, std::string &ErrMsg);
143
144   void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
145
146   LLVMContext &getContext() { return Context; }
147
148 private:
149   void initializeLTOPasses();
150
151   bool compileOptimizedToFile(const char **Name, std::string &ErrMsg);
152   void applyScopeRestrictions();
153   void applyRestriction(GlobalValue &GV, ArrayRef<StringRef> Libcalls,
154                         std::vector<const char *> &MustPreserveList,
155                         SmallPtrSetImpl<GlobalValue *> &AsmUsed,
156                         Mangler &Mangler);
157   bool determineTarget(std::string &ErrMsg);
158
159   static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context);
160
161   void DiagnosticHandler2(const DiagnosticInfo &DI);
162
163   typedef StringMap<uint8_t> StringSet;
164
165   std::unique_ptr<LLVMContext> OwnedContext;
166   LLVMContext &Context;
167   std::unique_ptr<Module> MergedModule;
168   Linker IRLinker;
169   std::unique_ptr<TargetMachine> TargetMach;
170   bool EmitDwarfDebugInfo = false;
171   bool ScopeRestrictionsDone = false;
172   Reloc::Model RelocModel = Reloc::Default;
173   StringSet MustPreserveSymbols;
174   StringSet AsmUndefinedRefs;
175   std::vector<std::string> CodegenOptions;
176   std::string FeatureStr;
177   std::string MCpu;
178   std::string MAttr;
179   std::string NativeObjectPath;
180   TargetOptions Options;
181   CodeGenOpt::Level CGOptLevel = CodeGenOpt::Default;
182   unsigned OptLevel = 2;
183   lto_diagnostic_handler_t DiagHandler = nullptr;
184   void *DiagContext = nullptr;
185   bool ShouldInternalize = true;
186   bool ShouldEmbedUselists = false;
187 };
188 }
189 #endif