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"
42 #include "llvm/Target/TargetOptions.h"
55 //===----------------------------------------------------------------------===//
56 /// LTOCodeGenerator - C++ class which implements the opaque lto_code_gen_t
59 struct LTOCodeGenerator {
60 static const char *getVersionString();
65 // Merge given module, return true on success.
66 bool addModule(struct LTOModule*, std::string &errMsg);
68 void setTargetOptions(llvm::TargetOptions options);
69 void setDebugInfo(lto_debug_model);
70 void setCodePICModel(lto_codegen_model);
72 void setCpu(const char *mCpu) { MCpu = mCpu; }
74 void addMustPreserveSymbol(const char *sym) { MustPreserveSymbols[sym] = 1; }
76 void addDSOSymbol(const char* Sym) {
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().
85 void setCodeGenDebugOptions(const char *opts);
87 // Parse the options set in setCodeGenDebugOptions. Like
88 // setCodeGenDebugOptions, this must be called before
89 // LTOCodeGenerator::compilexxx() and LTOCodeGenerator::writeMergedModules()
90 void parseCodeGenDebugOptions();
92 // Write the merged module to the file specified by the given path.
93 // Return true on success.
94 bool writeMergedModules(const char *path, std::string &errMsg);
96 // Compile the merged module into a *single* object file; the path to object
97 // file is returned to the caller via argument "name". Return true on
100 // NOTE that it is up to the linker to remove the intermediate object file.
101 // Do not try to remove the object file in LTOCodeGenerator's destructor
102 // as we don't who (LTOCodeGenerator or the obj file) will last longer.
104 bool compile_to_file(const char **name,
107 bool disableGVNLoadPRE,
108 std::string &errMsg);
110 // As with compile_to_file(), this function compiles the merged module into
111 // single object file. Instead of returning the object-file-path to the caller
112 // (linker), it brings the object to a buffer, and return the buffer to the
113 // caller. This function should delete intermediate object file once its content
114 // is brought to memory. Return NULL if the compilation was not successful.
116 const void *compile(size_t *length,
119 bool disableGVNLoadPRE,
120 std::string &errMsg);
123 void initializeLTOPasses();
125 bool generateObjectFile(llvm::raw_ostream &out,
128 bool disableGVNLoadPRE,
129 std::string &errMsg);
130 void applyScopeRestrictions();
131 void applyRestriction(llvm::GlobalValue &GV,
132 std::vector<const char*> &MustPreserveList,
133 std::vector<const char*> &SymtabList,
134 llvm::SmallPtrSet<llvm::GlobalValue*, 8> &AsmUsed,
135 llvm::Mangler &Mangler);
136 bool determineTarget(std::string &errMsg);
138 typedef llvm::StringMap<uint8_t> StringSet;
140 llvm::LLVMContext &Context;
142 llvm::TargetMachine *TargetMach;
143 bool EmitDwarfDebugInfo;
144 bool ScopeRestrictionsDone;
145 lto_codegen_model CodeModel;
146 StringSet DSOSymbols;
147 StringSet MustPreserveSymbols;
148 StringSet AsmUndefinedRefs;
149 llvm::MemoryBuffer *NativeObjectFile;
150 std::vector<char *> CodegenOptions;
152 std::string NativeObjectPath;
153 llvm::TargetOptions Options;
156 #endif // LTO_CODE_GENERATOR_H