1 //===-LTOCodeGenerator.h - LLVM Link Time Optimizer -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the LTOCodeGenerator class.
12 // LTO compilation consists of three phases: Pre-IPO, IPO and Post-IPO.
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
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).
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()).
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.
33 //===----------------------------------------------------------------------===//
35 #ifndef LTO_CODE_GENERATOR_H
36 #define LTO_CODE_GENERATOR_H
38 #include "llvm-c/lto.h"
39 #include "llvm/ADT/SmallPtrSet.h"
40 #include "llvm/ADT/StringMap.h"
41 #include "llvm/Linker.h"
54 //===----------------------------------------------------------------------===//
55 /// LTOCodeGenerator - C++ class which implements the opaque lto_code_gen_t
58 struct LTOCodeGenerator {
59 static const char *getVersionString();
64 // Merge given module, return true on success.
65 bool addModule(struct LTOModule*, std::string &errMsg);
67 void setDebugInfo(lto_debug_model);
68 void setCodePICModel(lto_codegen_model);
70 void setCpu(const char* mCpu) { _mCpu = mCpu; }
72 void addMustPreserveSymbol(const char* sym) {
73 _mustPreserveSymbols[sym] = 1;
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().
81 void setCodeGenDebugOptions(const char *opts);
83 // Write the merged module to the file specified by the given path.
84 // Return true on success.
85 bool writeMergedModules(const char *path, std::string &errMsg);
87 // Compile the merged module into a *single* object file; the path to object
88 // file is returned to the caller via argument "name". Return true on
91 // NOTE that it is up to the linker to remove the intermediate object file.
92 // Do not try to remove the object file in LTOCodeGenerator's destructor
93 // as we don't who (LTOCodeGenerator or the obj file) will last longer.
95 bool compile_to_file(const char **name, std::string &errMsg);
97 // As with compile_to_file(), this function compiles the merged module into
98 // single object file. Instead of returning the object-file-path to the caller
99 // (linker), it brings the object to a buffer, and return the buffer to the
100 // caller. This function should delete intermediate object file once its content
101 // is brought to memory. Return NULL is the compilation was not successful.
103 const void *compile(size_t *length, std::string &errMsg);
105 // Corresponding to lto_codegen_compile_parallel() API.
106 // Generates code for merged module into an array of native object files.
107 // On success returns a pointer to an array of NativeObjectFile. The count
108 // parameter returns the number of elements in the array. Each element is
109 // a pointer/length for a generated mach-o/ELF buffer. The buffer is owned
110 // by the lto_code_gen_t and will be freed when lto_codegen_dispose() is
111 // called, or lto_codegen_compile() is called again. On failure, returns
112 // NULL (check lto_get_error_message() for details).
114 NativeObjectFile *compile_parallel(size_t *count, std::string &ErrMsg);
117 void initializeLTOPasses();
119 bool generateObjectFile(llvm::raw_ostream &out, std::string &errMsg);
120 void applyScopeRestrictions();
121 void applyRestriction(llvm::GlobalValue &GV,
122 std::vector<const char*> &mustPreserveList,
123 llvm::SmallPtrSet<llvm::GlobalValue*, 8> &asmUsed,
124 llvm::Mangler &mangler);
125 bool determineTarget(std::string &errMsg);
127 typedef llvm::StringMap<uint8_t> StringSet;
129 llvm::LLVMContext& _context;
130 llvm::Linker _linker;
131 llvm::TargetMachine* _target;
132 bool _emitDwarfDebugInfo;
133 bool _scopeRestrictionsDone;
134 lto_codegen_model _codeModel;
135 StringSet _mustPreserveSymbols;
136 StringSet _asmUndefinedRefs;
137 llvm::MemoryBuffer* _nativeObjectFile;
138 std::vector<char*> _codegenOptions;
140 std::string _nativeObjectPath;
141 NativeObjectFile *ObjBufVect;
144 #endif // LTO_CODE_GENERATOR_H