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>
37 LTO_MODULE_MERGE_FAILURE,
41 enum LTOLinkageTypes {
42 LTOExternalLinkage, // Externally visible function
43 LTOLinkOnceLinkage, // Keep one copy of named function when linking (inline)
44 LTOWeakLinkage, // Keep one copy of named function when linking (weak)
45 LTOInternalLinkage // Rename collisions when linking (static functions)
48 /// This class represents LLVM symbol information without exposing details
49 /// of LLVM global values. It encapsulates symbol linkage information. This
50 /// is typically used in hash_map where associated name identifies the
56 LTOLinkageTypes getLinkage() const { return linkage; }
59 LLVMSymbol (enum LTOLinkageTypes lt, GlobalValue *g, const std::string &n,
60 const std::string &m, int a) : linkage(lt), gv(g), name(n),
61 mangledName(m), alignment(a) {}
63 const char *getName() { return name.c_str(); }
64 const char *getMangledName() { return mangledName.c_str(); }
65 int getAlignment() { return alignment; }
68 enum LTOLinkageTypes linkage;
71 std::string mangledName;
75 class string_compare {
77 bool operator()(const char* left, const char* right) const {
78 return (strcmp(left, right) == 0);
82 /// This is abstract class to facilitate dlopen() interface.
83 /// See LTO below for more info.
84 class LinkTimeOptimizer {
86 typedef hash_map<const char*, LLVMSymbol*, hash<const char*>,
87 string_compare> NameToSymbolMap;
88 typedef hash_map<const char*, Module*, hash<const char*>,
89 string_compare> NameToModuleMap;
90 virtual enum LTOStatus readLLVMObjectFile(const std::string &,
92 std::set<std::string> &) = 0;
93 virtual enum LTOStatus optimizeModules(const std::string &,
94 std::vector<const char*> &,
97 virtual void getTargetTriple(const std::string &, std::string &) = 0;
98 virtual void removeModule (const std::string &InputFilename) = 0;
99 virtual void printVersion () = 0;
100 virtual ~LinkTimeOptimizer() = 0;
103 /// This is the main link time optimization class. It exposes simple API
104 /// to perform link time optimization using LLVM intermodular optimizer.
105 class LTO : public LinkTimeOptimizer {
108 typedef hash_map<const char*, LLVMSymbol*, hash<const char*>,
109 string_compare> NameToSymbolMap;
110 typedef hash_map<const char*, Module*, hash<const char*>,
111 string_compare> NameToModuleMap;
113 enum LTOStatus readLLVMObjectFile(const std::string &InputFilename,
114 NameToSymbolMap &symbols,
115 std::set<std::string> &references);
116 enum LTOStatus optimizeModules(const std::string &OutputFilename,
117 std::vector<const char*> &exportList,
118 std::string &targetTriple, bool saveTemps,
120 void getTargetTriple(const std::string &InputFilename,
121 std::string &targetTriple);
122 void removeModule (const std::string &InputFilename);
125 // Constructors and destructors
127 /// TODO: Use Target info, it is available at this time.
133 Module *getModule (const std::string &InputFilename);
134 enum LTOStatus optimize(Module *, std::ostream &,
135 std::vector<const char *> &);
136 void getTarget(Module *);
139 std::vector<Module *> modules;
140 NameToSymbolMap allSymbols;
141 NameToModuleMap allModules;
142 TargetMachine *Target;
145 } // End llvm namespace
147 /// This provides C interface to initialize link time optimizer. This allows
148 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
149 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
151 llvm::LinkTimeOptimizer *createLLVMOptimizer();