Misc enhancements to LTO:
[oota-llvm.git] / tools / 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 #include "LTOPartition.h"
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 setDebugInfo(lto_debug_model);
69   void setCodePICModel(lto_codegen_model);
70
71   void setCpu(const char* mCpu) { _mCpu = mCpu; }
72
73   void addMustPreserveSymbol(const char* sym) {
74     _mustPreserveSymbols[sym] = 1;
75   }
76
77   // To pass options to the driver and optimization passes. These options are
78   // not necessarily for debugging purpose (The function name is misleading).
79   // This function should be called before LTOCodeGenerator::compilexxx(),
80   // and LTOCodeGenerator::writeMergedModules().
81   //
82   void setCodeGenDebugOptions(const char *opts);
83
84   // Write the merged module to the file specified by the given path.
85   // Return true on success.
86   bool writeMergedModules(const char *path, std::string &errMsg);
87
88   // Compile the merged module into a *single* object file; the path to object
89   // file is returned to the caller via argument "name". Return true on
90   // success.
91   //
92   // NOTE that it is up to the linker to remove the intermediate object file.
93   //  Do not try to remove the object file in LTOCodeGenerator's destructor
94   //  as we don't who (LTOCodeGenerator or the obj file) will last longer.
95   // 
96   bool compile_to_file(const char **name, std::string &errMsg);
97
98   // As with compile_to_file(), this function compiles the merged module into
99   // single object file. Instead of returning the object-file-path to the caller
100   // (linker), it brings the object to a buffer, and return the buffer to the
101   // caller. This function should delete intermediate object file once its content
102   // is brought to memory. Return NULL is the compilation was not successful. 
103   //
104   const void *compile(size_t *length, std::string &errMsg);
105
106   // Return the paths of the intermediate files that linker needs to delete
107   // before it exits. The paths are delimited by a single '\0', and the last
108   // path is ended by double '\0's. The file could be a directory. In that
109   // case, the entire directory should be erased recusively. This function
110   // must be called after the compilexxx() is successfuly called, because
111   // only after that moment, compiler is aware which files need to be removed.
112   // If calling compilexxx() is not successful, it is up to compiler to clean
113   // up all the intermediate files generated during the compilation process.
114   //
115   const char *getFilesNeedToRemove();
116
117 private:
118   void initializeLTOPasses();
119   bool determineTarget(std::string &errMsg);
120   void parseOptions();
121   bool prepareBeforeCompile(std::string &ErrMsg);
122
123   void performIPO(bool PerformPartition, std::string &ErrMsg);
124   bool performPostIPO(std::string &ErrMsg, bool MergeObjs = false,
125                       const char **MergObjPath = 0);
126   bool generateObjectFile(llvm::raw_ostream &out, std::string &errMsg);
127
128   void applyScopeRestrictions();
129   void applyRestriction(llvm::GlobalValue &GV,
130                         std::vector<const char*> &mustPreserveList,
131                         llvm::SmallPtrSet<llvm::GlobalValue*, 8> &asmUsed,
132                         llvm::Mangler &mangler);
133   
134
135   typedef llvm::StringMap<uint8_t> StringSet;
136
137   llvm::LLVMContext&          _context;
138   llvm::Linker                _linker;
139   llvm::TargetMachine*        _target;
140   bool                        _emitDwarfDebugInfo;
141   bool                        _scopeRestrictionsDone;
142   lto_codegen_model           _codeModel;
143   StringSet                   _mustPreserveSymbols;
144   StringSet                   _asmUndefinedRefs;
145   llvm::MemoryBuffer*         _nativeObjectFile;
146   std::vector<char*>          _codegenOptions;
147   std::string                 _mCpu;
148   std::string                 _nativeObjectPath;
149
150   // To manage the partitions. If partition is not enabled, the whole merged
151   // module is considered as a single degenerated partition, and the "manager"
152   // is still active.
153   lto::IPOPartMgr PartitionMgr;
154
155   // To manage the intermediate files during the compilations.
156   lto::IPOFileMgr FileMgr;
157
158   // Sometimes we need to return a vector of strings in a "C" way (to work with
159   // the C-APIs). We encode such C-thinking string vector by concatenating all
160   // strings tegother with a single '\0' as the delimitor, the last string ended
161   // by double '\0's.
162   SmallVector<char, 4> ConcatStrings;
163
164   // Make sure command line is parsed only once. It would otherwise complain
165   // and quite prematurely.
166   bool OptionsParsed;
167 };
168
169 #endif // LTO_CODE_GENERATOR_H