1 //===-- llvm/LinkTimeOptimizer.h - Public Interface ------------*- C++ -*-===//
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 header provides public interface to use LLVM link time optimization
11 // library. This is intended to be used by linker to do link time optimization.
13 //===----------------------------------------------------------------------===//
21 #include <llvm/ADT/hash_map>
24 #define LLVM_LTO_VERSION 2
40 LTO_MODULE_MERGE_FAILURE,
44 enum LTOLinkageTypes {
45 LTOExternalLinkage, // Externally visible function
46 LTOLinkOnceLinkage, // Keep one copy of named function when linking (inline)
47 LTOWeakLinkage, // Keep one copy of named function when linking (weak)
48 LTOInternalLinkage, // Rename collisions when linking (static functions)
49 LTOCommonLinkage // tentative definitions (usually equivalent to weak)
52 enum LTOVisibilityTypes {
53 LTODefaultVisibility = 0, ///< The GV is visible
54 LTOHiddenVisibility, ///< The GV is hidden
55 LTOProtectedVisibility ///< The GV is protected
59 enum LTOCodeGenModel {
65 /// This class represents LLVM symbol information without exposing details
66 /// of LLVM global values. It encapsulates symbol linkage information. This
67 /// is typically used in hash_map where associated name identifies the
73 LTOLinkageTypes getLinkage() const { return linkage; }
74 LTOVisibilityTypes getVisibility() const { return visibility; }
77 LLVMSymbol (enum LTOLinkageTypes lt, enum LTOVisibilityTypes vis,
78 GlobalValue *g, const std::string &n,
79 const std::string &m, int a) : linkage(lt), visibility(vis),
81 mangledName(m), alignment(a) {}
83 const char *getName() { return name.c_str(); }
84 const char *getMangledName() { return mangledName.c_str(); }
85 int getAlignment() { return alignment; }
88 enum LTOLinkageTypes linkage;
89 enum LTOVisibilityTypes visibility;
92 std::string mangledName;
96 class string_compare {
98 bool operator()(const char* left, const char* right) const {
99 return (strcmp(left, right) == 0);
103 /// This is abstract class to facilitate dlopen() interface.
104 /// See LTO below for more info.
105 class LinkTimeOptimizer {
107 typedef hash_map<const char*, LLVMSymbol*, hash<const char*>,
108 string_compare> NameToSymbolMap;
109 typedef hash_map<const char*, Module*, hash<const char*>,
110 string_compare> NameToModuleMap;
111 virtual enum LTOStatus readLLVMObjectFile(const std::string &,
113 std::set<std::string> &) = 0;
114 virtual enum LTOStatus optimizeModules(const std::string &,
115 std::vector<const char*> &exportList,
116 std::string &targetTriple,
117 bool saveTemps, const char *) = 0;
118 virtual void getTargetTriple(const std::string &, std::string &) = 0;
119 virtual void removeModule (const std::string &InputFilename) = 0;
120 virtual void setCodeGenModel(LTOCodeGenModel CGM) = 0;
121 virtual void printVersion () = 0;
122 virtual ~LinkTimeOptimizer() = 0;
125 /// This is the main link time optimization class. It exposes simple API
126 /// to perform link time optimization using LLVM intermodular optimizer.
127 class LTO : public LinkTimeOptimizer {
130 typedef hash_map<const char*, LLVMSymbol*, hash<const char*>,
131 string_compare> NameToSymbolMap;
132 typedef hash_map<const char*, Module*, hash<const char*>,
133 string_compare> NameToModuleMap;
135 enum LTOStatus readLLVMObjectFile(const std::string &InputFilename,
136 NameToSymbolMap &symbols,
137 std::set<std::string> &references);
138 enum LTOStatus optimizeModules(const std::string &OutputFilename,
139 std::vector<const char*> &exportList,
140 std::string &targetTriple,
141 bool saveTemps, const char *);
142 void getTargetTriple(const std::string &InputFilename,
143 std::string &targetTriple);
144 void removeModule (const std::string &InputFilename);
147 void setCodeGenModel(LTOCodeGenModel CGM) {
151 // Constructors and destructors
152 LTO() : Target(NULL), CGModel(LTO_CGM_Dynamic) {
153 /// TODO: Use Target info, it is available at this time.
158 Module *getModule (const std::string &InputFilename);
159 enum LTOStatus optimize(Module *, std::ostream &,
160 std::vector<const char *> &);
161 void getTarget(Module *);
164 std::vector<Module *> modules;
165 NameToSymbolMap allSymbols;
166 NameToModuleMap allModules;
167 TargetMachine *Target;
168 LTOCodeGenModel CGModel;
171 } // End llvm namespace
173 /// This provides C interface to initialize link time optimizer. This allows
174 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
175 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
177 llvm::LinkTimeOptimizer *createLLVMOptimizer(unsigned VERSION = LLVM_LTO_VERSION);