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>
23 #define LLVM_LTO_VERSION 1
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 /// This class represents LLVM symbol information without exposing details
51 /// of LLVM global values. It encapsulates symbol linkage information. This
52 /// is typically used in hash_map where associated name identifies the
58 LTOLinkageTypes getLinkage() const { return linkage; }
61 LLVMSymbol (enum LTOLinkageTypes lt, GlobalValue *g, const std::string &n,
62 const std::string &m, int a) : linkage(lt), gv(g), name(n),
63 mangledName(m), alignment(a) {}
65 const char *getName() { return name.c_str(); }
66 const char *getMangledName() { return mangledName.c_str(); }
67 int getAlignment() { return alignment; }
70 enum LTOLinkageTypes linkage;
73 std::string mangledName;
77 class string_compare {
79 bool operator()(const char* left, const char* right) const {
80 return (strcmp(left, right) == 0);
84 /// This is abstract class to facilitate dlopen() interface.
85 /// See LTO below for more info.
86 class LinkTimeOptimizer {
88 typedef hash_map<const char*, LLVMSymbol*, hash<const char*>,
89 string_compare> NameToSymbolMap;
90 typedef hash_map<const char*, Module*, hash<const char*>,
91 string_compare> NameToModuleMap;
92 virtual enum LTOStatus readLLVMObjectFile(const std::string &,
94 std::set<std::string> &) = 0;
95 virtual enum LTOStatus optimizeModules(const std::string &,
96 std::vector<const char*> &,
99 virtual void getTargetTriple(const std::string &, std::string &) = 0;
100 virtual void removeModule (const std::string &InputFilename) = 0;
101 virtual void printVersion () = 0;
102 virtual ~LinkTimeOptimizer() = 0;
105 /// This is the main link time optimization class. It exposes simple API
106 /// to perform link time optimization using LLVM intermodular optimizer.
107 class LTO : public LinkTimeOptimizer {
110 typedef hash_map<const char*, LLVMSymbol*, hash<const char*>,
111 string_compare> NameToSymbolMap;
112 typedef hash_map<const char*, Module*, hash<const char*>,
113 string_compare> NameToModuleMap;
115 enum LTOStatus readLLVMObjectFile(const std::string &InputFilename,
116 NameToSymbolMap &symbols,
117 std::set<std::string> &references);
118 enum LTOStatus optimizeModules(const std::string &OutputFilename,
119 std::vector<const char*> &exportList,
120 std::string &targetTriple, bool saveTemps,
122 void getTargetTriple(const std::string &InputFilename,
123 std::string &targetTriple);
124 void removeModule (const std::string &InputFilename);
127 // Constructors and destructors
129 /// TODO: Use Target info, it is available at this time.
135 Module *getModule (const std::string &InputFilename);
136 enum LTOStatus optimize(Module *, std::ostream &,
137 std::vector<const char *> &);
138 void getTarget(Module *);
141 std::vector<Module *> modules;
142 NameToSymbolMap allSymbols;
143 NameToModuleMap allModules;
144 TargetMachine *Target;
147 } // End llvm namespace
149 /// This provides C interface to initialize link time optimizer. This allows
150 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
151 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
153 llvm::LinkTimeOptimizer *createLLVMOptimizer(unsigned VERSION = LLVM_LTO_VERSION);