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/ArrayRef.h"
40 #include "llvm/ADT/SmallPtrSet.h"
41 #include "llvm/ADT/StringMap.h"
42 #include "llvm/Linker.h"
43 #include "llvm/Target/TargetOptions.h"
52 class TargetLibraryInfo;
57 //===----------------------------------------------------------------------===//
58 /// LTOCodeGenerator - C++ class which implements the opaque lto_code_gen_t
61 struct LTOCodeGenerator {
62 static const char *getVersionString();
67 // Merge given module, return true on success.
68 bool addModule(struct LTOModule*, std::string &errMsg);
70 void setTargetOptions(llvm::TargetOptions options);
71 void setDebugInfo(lto_debug_model);
72 void setCodePICModel(lto_codegen_model);
73 void setInternalizeStrategy(lto_internalize_strategy);
75 void setCpu(const char *mCpu) { MCpu = mCpu; }
77 void addMustPreserveSymbol(const char *sym) { MustPreserveSymbols[sym] = 1; }
79 // To pass options to the driver and optimization passes. These options are
80 // not necessarily for debugging purpose (The function name is misleading).
81 // This function should be called before LTOCodeGenerator::compilexxx(),
82 // and LTOCodeGenerator::writeMergedModules().
83 void setCodeGenDebugOptions(const char *opts);
85 // Parse the options set in setCodeGenDebugOptions. Like
86 // setCodeGenDebugOptions, this must be called before
87 // LTOCodeGenerator::compilexxx() and LTOCodeGenerator::writeMergedModules()
88 void parseCodeGenDebugOptions();
90 // Write the merged module to the file specified by the given path.
91 // Return true on success.
92 bool writeMergedModules(const char *path, std::string &errMsg);
94 // Compile the merged module into a *single* object file; the path to object
95 // file is returned to the caller via argument "name". Return true on
98 // NOTE that it is up to the linker to remove the intermediate object file.
99 // Do not try to remove the object file in LTOCodeGenerator's destructor
100 // as we don't who (LTOCodeGenerator or the obj file) will last longer.
101 bool compile_to_file(const char **name,
104 bool disableGVNLoadPRE,
105 std::string &errMsg);
107 // As with compile_to_file(), this function compiles the merged module into
108 // single object file. Instead of returning the object-file-path to the caller
109 // (linker), it brings the object to a buffer, and return the buffer to the
110 // caller. This function should delete intermediate object file once its content
111 // is brought to memory. Return NULL if the compilation was not successful.
112 const void *compile(size_t *length,
115 bool disableGVNLoadPRE,
116 std::string &errMsg);
118 bool shouldInternalize() const {
119 return InternalizeStrategy != LTO_INTERNALIZE_NONE;
122 bool shouldOnlyInternalizeHidden() const {
123 return InternalizeStrategy == LTO_INTERNALIZE_HIDDEN;
127 void initializeLTOPasses();
129 bool generateObjectFile(llvm::raw_ostream &out,
132 bool disableGVNLoadPRE,
133 std::string &errMsg);
134 void applyScopeRestrictions();
135 void applyRestriction(llvm::GlobalValue &GV,
136 const llvm::ArrayRef<llvm::StringRef> &Libcalls,
137 std::vector<const char*> &MustPreserveList,
138 llvm::SmallPtrSet<llvm::GlobalValue*, 8> &AsmUsed,
139 llvm::Mangler &Mangler);
140 bool determineTarget(std::string &errMsg);
142 typedef llvm::StringMap<uint8_t> StringSet;
144 llvm::LLVMContext &Context;
146 llvm::TargetMachine *TargetMach;
147 bool EmitDwarfDebugInfo;
148 bool ScopeRestrictionsDone;
149 lto_codegen_model CodeModel;
150 lto_internalize_strategy InternalizeStrategy;
151 StringSet MustPreserveSymbols;
152 StringSet AsmUndefinedRefs;
153 llvm::MemoryBuffer *NativeObjectFile;
154 std::vector<char *> CodegenOptions;
156 std::string NativeObjectPath;
157 llvm::TargetOptions Options;
160 #endif // LTO_CODE_GENERATOR_H