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