1 //===-- llvm/LinkTimeOptimizer.h - Public Interface ------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Devang Patel and is distributed under
6 // the University of Illinois Open Source 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>
36 LTO_MODULE_MERGE_FAILURE,
40 enum LTOLinkageTypes {
41 LTOExternalLinkage, // Externally visible function
42 LTOLinkOnceLinkage, // Keep one copy of named function when linking (inline)
43 LTOWeakLinkage, // Keep one copy of named function when linking (weak)
44 LTOInternalLinkage // Rename collisions when linking (static functions)
47 /// This class represents LLVM symbol information without exposing details
48 /// of LLVM global values. It encapsulates symbol linkage information. This
49 /// is typically used in hash_map where associated name identifies the
55 LTOLinkageTypes getLinkage() const { return linkage; }
58 LLVMSymbol (enum LTOLinkageTypes lt, GlobalValue *g, const std::string &n,
59 const std::string &m) : linkage(lt), gv(g), name(n),
62 const char *getName() { return name.c_str(); }
63 const char *getMangledName() { return mangledName.c_str(); }
66 enum LTOLinkageTypes linkage;
69 std::string mangledName;
72 class string_compare {
74 bool operator()(const char* left, const char* right) const {
75 return (strcmp(left, right) == 0);
79 /// This is abstract class to facilitate dlopen() interface.
80 /// See LTO below for more info.
81 class LinkTimeOptimizer {
83 typedef hash_map<const char*, LLVMSymbol*, hash<const char*>,
84 string_compare> NameToSymbolMap;
85 typedef hash_map<const char*, Module*, hash<const char*>,
86 string_compare> NameToModuleMap;
87 virtual enum LTOStatus readLLVMObjectFile(const std::string &,
89 std::set<std::string> &) = 0;
90 virtual enum LTOStatus optimizeModules(const std::string &,
91 std::vector<const char*> &,
93 virtual void getTargetTriple(const std::string &, std::string &) = 0;
94 virtual void removeModule (const std::string &InputFilename) = 0;
95 virtual ~LinkTimeOptimizer() = 0;
98 /// This is the main link time optimization class. It exposes simple API
99 /// to perform link time optimization using LLVM intermodular optimizer.
100 class LTO : public LinkTimeOptimizer {
103 typedef hash_map<const char*, LLVMSymbol*, hash<const char*>,
104 string_compare> NameToSymbolMap;
105 typedef hash_map<const char*, Module*, hash<const char*>,
106 string_compare> NameToModuleMap;
108 enum LTOStatus readLLVMObjectFile(const std::string &InputFilename,
109 NameToSymbolMap &symbols,
110 std::set<std::string> &references);
111 enum LTOStatus optimizeModules(const std::string &OutputFilename,
112 std::vector<const char*> &exportList,
113 std::string &targetTriple);
114 void getTargetTriple(const std::string &InputFilename, std::string &targetTriple);
115 void removeModule (const std::string &InputFilename);
118 Module *getModule (const std::string &InputFilename);
121 std::vector<Module *> modules;
122 NameToSymbolMap allSymbols;
123 NameToModuleMap allModules;
126 } // End llvm namespace
128 /// This provides C interface to initialize link time optimizer. This allows
129 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
130 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
132 llvm::LinkTimeOptimizer *createLLVMOptimizer();