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)
51 enum LTOVisibilityTypes {
52 LTODefaultVisibility = 0, ///< The GV is visible
53 LTOHiddenVisibility, ///< The GV is hidden
54 LTOProtectedVisibility ///< The GV is protected
58 enum LTOCodeGenModel {
64 /// This class represents LLVM symbol information without exposing details
65 /// of LLVM global values. It encapsulates symbol linkage information. This
66 /// is typically used in hash_map where associated name identifies the
72 LTOLinkageTypes getLinkage() const { return linkage; }
73 LTOVisibilityTypes getVisibility() const { return visibility; }
76 LLVMSymbol (enum LTOLinkageTypes lt, enum LTOVisibilityTypes vis,
77 GlobalValue *g, const std::string &n,
78 const std::string &m, int a) : linkage(lt), visibility(vis),
80 mangledName(m), alignment(a) {}
82 const char *getName() { return name.c_str(); }
83 const char *getMangledName() { return mangledName.c_str(); }
84 int getAlignment() { return alignment; }
87 enum LTOLinkageTypes linkage;
88 enum LTOVisibilityTypes visibility;
91 std::string mangledName;
95 class string_compare {
97 bool operator()(const char* left, const char* right) const {
98 return (strcmp(left, right) == 0);
102 /// This is abstract class to facilitate dlopen() interface.
103 /// See LTO below for more info.
104 class LinkTimeOptimizer {
106 typedef hash_map<const char*, LLVMSymbol*, hash<const char*>,
107 string_compare> NameToSymbolMap;
108 typedef hash_map<const char*, Module*, hash<const char*>,
109 string_compare> NameToModuleMap;
110 virtual enum LTOStatus readLLVMObjectFile(const std::string &,
112 std::set<std::string> &) = 0;
113 virtual enum LTOStatus optimizeModules(const std::string &,
114 std::vector<const char*> &exportList,
115 std::string &targetTriple,
116 bool saveTemps, const char *) = 0;
117 virtual void getTargetTriple(const std::string &, std::string &) = 0;
118 virtual void removeModule (const std::string &InputFilename) = 0;
119 virtual void setCodeGenModel(LTOCodeGenModel CGM) = 0;
120 virtual void printVersion () = 0;
121 virtual ~LinkTimeOptimizer() = 0;
124 /// This is the main link time optimization class. It exposes simple API
125 /// to perform link time optimization using LLVM intermodular optimizer.
126 class LTO : public LinkTimeOptimizer {
129 typedef hash_map<const char*, LLVMSymbol*, hash<const char*>,
130 string_compare> NameToSymbolMap;
131 typedef hash_map<const char*, Module*, hash<const char*>,
132 string_compare> NameToModuleMap;
134 enum LTOStatus readLLVMObjectFile(const std::string &InputFilename,
135 NameToSymbolMap &symbols,
136 std::set<std::string> &references);
137 enum LTOStatus optimizeModules(const std::string &OutputFilename,
138 std::vector<const char*> &exportList,
139 std::string &targetTriple,
140 bool saveTemps, const char *);
141 void getTargetTriple(const std::string &InputFilename,
142 std::string &targetTriple);
143 void removeModule (const std::string &InputFilename);
146 void setCodeGenModel(LTOCodeGenModel CGM) {
150 // Constructors and destructors
151 LTO() : Target(NULL), CGModel(LTO_CGM_Dynamic) {
152 /// TODO: Use Target info, it is available at this time.
157 Module *getModule (const std::string &InputFilename);
158 enum LTOStatus optimize(Module *, std::ostream &,
159 std::vector<const char *> &);
160 void getTarget(Module *);
163 std::vector<Module *> modules;
164 NameToSymbolMap allSymbols;
165 NameToModuleMap allModules;
166 TargetMachine *Target;
167 LTOCodeGenModel CGModel;
170 } // End llvm namespace
172 /// This provides C interface to initialize link time optimizer. This allows
173 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
174 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
176 llvm::LinkTimeOptimizer *createLLVMOptimizer(unsigned VERSION = LLVM_LTO_VERSION);