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