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>
23 #define LLVM_LTO_VERSION 2
39 LTO_MODULE_MERGE_FAILURE,
43 enum LTOLinkageTypes {
44 LTOExternalLinkage, // Externally visible function
45 LTOLinkOnceLinkage, // Keep one copy of named function when linking (inline)
46 LTOWeakLinkage, // Keep one copy of named function when linking (weak)
47 LTOInternalLinkage // Rename collisions when linking (static functions)
50 enum LTOVisibilityTypes {
51 LTODefaultVisibility = 0, ///< The GV is visible
52 LTOHiddenVisibility, ///< The GV is hidden
53 LTOProtectedVisibility ///< The GV is protected
57 enum LTOCodeGenModel {
63 /// This class represents LLVM symbol information without exposing details
64 /// of LLVM global values. It encapsulates symbol linkage information. This
65 /// is typically used in hash_map where associated name identifies the
71 LTOLinkageTypes getLinkage() const { return linkage; }
72 LTOVisibilityTypes getVisibility() const { return visibility; }
75 LLVMSymbol (enum LTOLinkageTypes lt, enum LTOVisibilityTypes vis,
76 GlobalValue *g, const std::string &n,
77 const std::string &m, int a) : linkage(lt), visibility(vis),
79 mangledName(m), alignment(a) {}
81 const char *getName() { return name.c_str(); }
82 const char *getMangledName() { return mangledName.c_str(); }
83 int getAlignment() { return alignment; }
86 enum LTOLinkageTypes linkage;
87 enum LTOVisibilityTypes visibility;
90 std::string mangledName;
94 class string_compare {
96 bool operator()(const char* left, const char* right) const {
97 return (strcmp(left, right) == 0);
101 /// This is abstract class to facilitate dlopen() interface.
102 /// See LTO below for more info.
103 class LinkTimeOptimizer {
105 typedef hash_map<const char*, LLVMSymbol*, hash<const char*>,
106 string_compare> NameToSymbolMap;
107 typedef hash_map<const char*, Module*, hash<const char*>,
108 string_compare> NameToModuleMap;
109 virtual enum LTOStatus readLLVMObjectFile(const std::string &,
111 std::set<std::string> &) = 0;
112 virtual enum LTOStatus optimizeModules(const std::string &,
113 std::vector<const char*> &exportList,
114 std::string &targetTriple,
115 bool saveTemps, const char *) = 0;
116 virtual void getTargetTriple(const std::string &, std::string &) = 0;
117 virtual void removeModule (const std::string &InputFilename) = 0;
118 virtual void setCodeGenModel(LTOCodeGenModel CGM) = 0;
119 virtual void printVersion () = 0;
120 virtual ~LinkTimeOptimizer() = 0;
123 /// This is the main link time optimization class. It exposes simple API
124 /// to perform link time optimization using LLVM intermodular optimizer.
125 class LTO : public LinkTimeOptimizer {
128 typedef hash_map<const char*, LLVMSymbol*, hash<const char*>,
129 string_compare> NameToSymbolMap;
130 typedef hash_map<const char*, Module*, hash<const char*>,
131 string_compare> NameToModuleMap;
133 enum LTOStatus readLLVMObjectFile(const std::string &InputFilename,
134 NameToSymbolMap &symbols,
135 std::set<std::string> &references);
136 enum LTOStatus optimizeModules(const std::string &OutputFilename,
137 std::vector<const char*> &exportList,
138 std::string &targetTriple,
139 bool saveTemps, const char *);
140 void getTargetTriple(const std::string &InputFilename,
141 std::string &targetTriple);
142 void removeModule (const std::string &InputFilename);
145 void setCodeGenModel(LTOCodeGenModel CGM) {
149 // Constructors and destructors
150 LTO() : Target(NULL), CGModel(LTO_CGM_Dynamic) {
151 /// TODO: Use Target info, it is available at this time.
156 Module *getModule (const std::string &InputFilename);
157 enum LTOStatus optimize(Module *, std::ostream &,
158 std::vector<const char *> &);
159 void getTarget(Module *);
162 std::vector<Module *> modules;
163 NameToSymbolMap allSymbols;
164 NameToModuleMap allModules;
165 TargetMachine *Target;
166 LTOCodeGenModel CGModel;
169 } // End llvm namespace
171 /// This provides C interface to initialize link time optimizer. This allows
172 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
173 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
175 llvm::LinkTimeOptimizer *createLLVMOptimizer(unsigned VERSION = LLVM_LTO_VERSION);