libLTO, llvm-lto, gold: Introduce flag for controlling optimization level.
[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
57 //===----------------------------------------------------------------------===//
58 /// C++ class which implements the opaque lto_code_gen_t type.
59 ///
60 struct LTOCodeGenerator {
61   static const char *getVersionString();
62
63   LTOCodeGenerator();
64   LTOCodeGenerator(std::unique_ptr<LLVMContext> Context);
65   ~LTOCodeGenerator();
66
67   // Merge given module, return true on success.
68   bool addModule(struct LTOModule *);
69
70   // Set the destination module.
71   void setModule(struct LTOModule *);
72
73   void setTargetOptions(TargetOptions options);
74   void setDebugInfo(lto_debug_model);
75   void setCodePICModel(lto_codegen_model);
76
77   void setCpu(const char *mCpu) { MCpu = mCpu; }
78   void setAttr(const char *mAttr) { MAttr = mAttr; }
79   void setOptLevel(unsigned optLevel) { OptLevel = optLevel; }
80
81   void addMustPreserveSymbol(const char *sym) { MustPreserveSymbols[sym] = 1; }
82
83   // To pass options to the driver and optimization passes. These options are
84   // not necessarily for debugging purpose (The function name is misleading).
85   // This function should be called before LTOCodeGenerator::compilexxx(),
86   // and LTOCodeGenerator::writeMergedModules().
87   void setCodeGenDebugOptions(const char *opts);
88
89   // Parse the options set in setCodeGenDebugOptions. Like
90   // setCodeGenDebugOptions, this must be called before
91   // LTOCodeGenerator::compilexxx() and LTOCodeGenerator::writeMergedModules()
92   void parseCodeGenDebugOptions();
93
94   // Write the merged module to the file specified by the given path.
95   // Return true on success.
96   bool writeMergedModules(const char *path, std::string &errMsg);
97
98   // Compile the merged module into a *single* object file; the path to object
99   // file is returned to the caller via argument "name". Return true on
100   // success.
101   //
102   // NOTE that it is up to the linker to remove the intermediate object file.
103   //  Do not try to remove the object file in LTOCodeGenerator's destructor
104   //  as we don't who (LTOCodeGenerator or the obj file) will last longer.
105   bool compile_to_file(const char **name,
106                        bool disableInline,
107                        bool disableGVNLoadPRE,
108                        bool disableVectorization,
109                        std::string &errMsg);
110
111   // As with compile_to_file(), this function compiles the merged module into
112   // single object file. Instead of returning the object-file-path to the caller
113   // (linker), it brings the object to a buffer, and return the buffer to the
114   // caller. This function should delete intermediate object file once its content
115   // is brought to memory. Return NULL if the compilation was not successful.
116   const void *compile(size_t *length,
117                       bool disableInline,
118                       bool disableGVNLoadPRE,
119                       bool disableVectorization,
120                       std::string &errMsg);
121
122   // Optimizes the merged module. Returns true on success.
123   bool optimize(bool disableInline,
124                 bool disableGVNLoadPRE,
125                 bool disableVectorization,
126                 std::string &errMsg);
127
128   // Compiles the merged optimized module into a single object file. It brings
129   // the object to a buffer, and returns the buffer to the caller. Return NULL
130   // if the compilation was not successful.
131   const void *compileOptimized(size_t *length, std::string &errMsg);
132
133   void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
134
135   LLVMContext &getContext() { return Context; }
136
137 private:
138   void initializeLTOPasses();
139
140   bool compileOptimized(raw_ostream &out, std::string &errMsg);
141   bool compileOptimizedToFile(const char **name, std::string &errMsg);
142   void applyScopeRestrictions();
143   void applyRestriction(GlobalValue &GV, ArrayRef<StringRef> Libcalls,
144                         std::vector<const char *> &MustPreserveList,
145                         SmallPtrSetImpl<GlobalValue *> &AsmUsed,
146                         Mangler &Mangler);
147   bool determineTarget(std::string &errMsg);
148
149   static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context);
150
151   void DiagnosticHandler2(const DiagnosticInfo &DI);
152
153   typedef StringMap<uint8_t> StringSet;
154
155   void initialize();
156   void destroyMergedModule();
157   std::unique_ptr<LLVMContext> OwnedContext;
158   LLVMContext &Context;
159   Linker IRLinker;
160   TargetMachine *TargetMach;
161   bool EmitDwarfDebugInfo;
162   bool ScopeRestrictionsDone;
163   lto_codegen_model CodeModel;
164   StringSet MustPreserveSymbols;
165   StringSet AsmUndefinedRefs;
166   std::unique_ptr<MemoryBuffer> NativeObjectFile;
167   std::vector<char *> CodegenOptions;
168   std::string MCpu;
169   std::string MAttr;
170   std::string NativeObjectPath;
171   TargetOptions Options;
172   unsigned OptLevel;
173   lto_diagnostic_handler_t DiagHandler;
174   void *DiagContext;
175   LTOModule *OwnedModule;
176 };
177 }
178 #endif