Move LTO support library to a component, allowing it to be tested
[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/SmallPtrSet.h"
40 #include "llvm/ADT/StringMap.h"
41 #include "llvm/Linker.h"
42 #include <string>
43 #include <vector>
44
45 namespace llvm {
46   class LLVMContext;
47   class GlobalValue;
48   class Mangler;
49   class MemoryBuffer;
50   class TargetMachine;
51   class raw_ostream;
52 }
53
54 //===----------------------------------------------------------------------===//
55 /// LTOCodeGenerator - C++ class which implements the opaque lto_code_gen_t
56 /// type.
57 ///
58 struct LTOCodeGenerator {
59   static const char *getVersionString();
60
61   LTOCodeGenerator();
62   ~LTOCodeGenerator();
63
64   // Merge given module, return true on success.
65   bool addModule(struct LTOModule*, std::string &errMsg);
66
67   void setDebugInfo(lto_debug_model);
68   void setCodePICModel(lto_codegen_model);
69
70   void setCpu(const char *mCpu) { MCpu = mCpu; }
71
72   void addMustPreserveSymbol(const char *sym) { MustPreserveSymbols[sym] = 1; }
73
74   // To pass options to the driver and optimization passes. These options are
75   // not necessarily for debugging purpose (The function name is misleading).
76   // This function should be called before LTOCodeGenerator::compilexxx(),
77   // and LTOCodeGenerator::writeMergedModules().
78   //
79   void setCodeGenDebugOptions(const char *opts);
80
81   // Write the merged module to the file specified by the given path.
82   // Return true on success.
83   bool writeMergedModules(const char *path, std::string &errMsg);
84
85   // Compile the merged module into a *single* object file; the path to object
86   // file is returned to the caller via argument "name". Return true on
87   // success.
88   //
89   // NOTE that it is up to the linker to remove the intermediate object file.
90   //  Do not try to remove the object file in LTOCodeGenerator's destructor
91   //  as we don't who (LTOCodeGenerator or the obj file) will last longer.
92   // 
93   bool compile_to_file(const char **name, std::string &errMsg);
94
95   // As with compile_to_file(), this function compiles the merged module into
96   // single object file. Instead of returning the object-file-path to the caller
97   // (linker), it brings the object to a buffer, and return the buffer to the
98   // caller. This function should delete intermediate object file once its content
99   // is brought to memory. Return NULL if the compilation was not successful. 
100   //
101   const void *compile(size_t *length, std::string &errMsg);
102
103 private:
104   void initializeLTOPasses();
105
106   bool generateObjectFile(llvm::raw_ostream &out, std::string &errMsg);
107   void applyScopeRestrictions();
108   void applyRestriction(llvm::GlobalValue &GV,
109                         std::vector<const char*> &MustPreserveList,
110                         llvm::SmallPtrSet<llvm::GlobalValue*, 8> &AsmUsed,
111                         llvm::Mangler &Mangler);
112   bool determineTarget(std::string &errMsg);
113
114   typedef llvm::StringMap<uint8_t> StringSet;
115
116   llvm::LLVMContext &Context;
117   llvm::Linker Linker;
118   llvm::TargetMachine *TargetMach;
119   bool EmitDwarfDebugInfo;
120   bool ScopeRestrictionsDone;
121   lto_codegen_model CodeModel;
122   StringSet MustPreserveSymbols;
123   StringSet AsmUndefinedRefs;
124   llvm::MemoryBuffer *NativeObjectFile;
125   std::vector<char *> CodegenOptions;
126   std::string MCpu;
127   std::string NativeObjectPath;
128 };
129
130 #endif // LTO_CODE_GENERATOR_H