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