Add methods to StringMap to erase entries by key.
[oota-llvm.git] / include / llvm / LinkTimeOptimizer.h
1 //===-- llvm/LinkTimeOptimizer.h - Public Interface  ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
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.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef __LTO_H__
16 #define __LTO_H__
17
18 #include <string>
19 #include <vector>
20 #include <set>
21 #include <llvm/ADT/hash_map>
22 #include <cstring>
23
24 #define LLVM_LTO_VERSION 2
25
26 namespace llvm {
27
28   class Module;
29   class GlobalValue;
30   class TargetMachine;
31
32   enum LTOStatus {
33     LTO_UNKNOWN,
34     LTO_OPT_SUCCESS,
35     LTO_READ_SUCCESS,
36     LTO_READ_FAILURE,
37     LTO_WRITE_FAILURE,
38     LTO_NO_TARGET,
39     LTO_NO_WORK,
40     LTO_MODULE_MERGE_FAILURE,
41     LTO_ASM_FAILURE
42   };
43  
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)
49     LTOCommonLinkage    // tentative definitions (usually equivalent to weak)
50   };
51
52   enum LTOVisibilityTypes {
53     LTODefaultVisibility = 0,  ///< The GV is visible
54     LTOHiddenVisibility,       ///< The GV is hidden
55     LTOProtectedVisibility     ///< The GV is protected
56   };
57
58
59   enum LTOCodeGenModel {
60     LTO_CGM_Static,
61     LTO_CGM_Dynamic,
62     LTO_CGM_DynamicNoPIC
63   };
64
65   /// This class represents LLVM symbol information without exposing details
66   /// of LLVM global values. It encapsulates symbol linkage information. This
67   /// is typically used in hash_map where associated name identifies the 
68   /// the symbol name.
69   class LLVMSymbol {
70
71   public:
72
73     LTOLinkageTypes getLinkage() const { return linkage; }
74     LTOVisibilityTypes getVisibility() const { return visibility; }
75     void mayBeNotUsed();
76
77     LLVMSymbol (enum LTOLinkageTypes lt, enum LTOVisibilityTypes vis, 
78                 GlobalValue *g, const std::string &n, 
79                 const std::string &m, int a) : linkage(lt), visibility(vis),
80                                                gv(g), name(n), 
81                                                mangledName(m), alignment(a) {}
82
83     const char *getName() { return name.c_str(); }
84     const char *getMangledName() { return mangledName.c_str(); }
85     int getAlignment() { return alignment; }
86
87   private:
88     enum LTOLinkageTypes linkage;
89     enum LTOVisibilityTypes visibility;
90     GlobalValue *gv;
91     std::string name;
92     std::string mangledName;
93     int alignment;
94   };
95
96   class string_compare {
97   public:
98     bool operator()(const char* left, const char* right) const { 
99       return (strcmp(left, right) == 0); 
100     }
101   };
102
103   /// This is abstract class to facilitate dlopen() interface.
104   /// See LTO below for more info.
105   class LinkTimeOptimizer {
106   public:
107     typedef hash_map<const char*, LLVMSymbol*, hash<const char*>, 
108                      string_compare> NameToSymbolMap;
109     typedef hash_map<const char*, Module*, hash<const char*>, 
110                      string_compare> NameToModuleMap;
111     virtual enum LTOStatus readLLVMObjectFile(const std::string &,
112                                               NameToSymbolMap &,
113                                               std::set<std::string> &) = 0;
114     virtual enum LTOStatus optimizeModules(const std::string &,
115                                            std::vector<const char*> &exportList,
116                                            std::string &targetTriple,
117                                            bool saveTemps, const char *) = 0;
118     virtual void getTargetTriple(const std::string &, std::string &) = 0;
119     virtual void removeModule (const std::string &InputFilename) = 0;
120     virtual void setCodeGenModel(LTOCodeGenModel CGM) = 0;
121     virtual void printVersion () = 0;
122     virtual ~LinkTimeOptimizer() = 0;
123   };
124
125   /// This is the main link time optimization class. It exposes simple API
126   /// to perform link time optimization using LLVM intermodular optimizer.
127   class LTO : public LinkTimeOptimizer {
128
129   public:
130     typedef hash_map<const char*, LLVMSymbol*, hash<const char*>, 
131                      string_compare> NameToSymbolMap;
132     typedef hash_map<const char*, Module*, hash<const char*>, 
133                      string_compare> NameToModuleMap;
134
135     enum LTOStatus readLLVMObjectFile(const std::string &InputFilename,
136                                       NameToSymbolMap &symbols,
137                                       std::set<std::string> &references);
138     enum LTOStatus optimizeModules(const std::string &OutputFilename,
139                                    std::vector<const char*> &exportList,
140                                    std::string &targetTriple, 
141                                    bool saveTemps,  const char *);
142     void getTargetTriple(const std::string &InputFilename, 
143                          std::string &targetTriple);
144     void removeModule (const std::string &InputFilename);
145     void printVersion();
146
147     void setCodeGenModel(LTOCodeGenModel CGM) {
148       CGModel = CGM;
149     }
150
151     // Constructors and destructors
152     LTO() : Target(NULL), CGModel(LTO_CGM_Dynamic) {
153       /// TODO: Use Target info, it is available at this time.
154     }
155     ~LTO();
156
157   private:
158     Module *getModule (const std::string &InputFilename);
159     enum LTOStatus optimize(Module *, std::ostream &, 
160                             std::vector<const char *> &);
161     void getTarget(Module *);
162
163   private:
164     std::vector<Module *> modules;
165     NameToSymbolMap allSymbols;
166     NameToModuleMap allModules;
167     TargetMachine *Target;
168     LTOCodeGenModel CGModel;
169   };
170
171 } // End llvm namespace
172
173 /// This provides C interface to initialize link time optimizer. This allows
174 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
175 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
176 extern "C"
177 llvm::LinkTimeOptimizer *createLLVMOptimizer(unsigned VERSION = LLVM_LTO_VERSION);
178
179 #endif