Create a new class, MemOperand, for describing memory references
[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
23 #define LLVM_LTO_VERSION 2
24
25 namespace llvm {
26
27   class Module;
28   class GlobalValue;
29   class TargetMachine;
30
31   enum LTOStatus {
32     LTO_UNKNOWN,
33     LTO_OPT_SUCCESS,
34     LTO_READ_SUCCESS,
35     LTO_READ_FAILURE,
36     LTO_WRITE_FAILURE,
37     LTO_NO_TARGET,
38     LTO_NO_WORK,
39     LTO_MODULE_MERGE_FAILURE,
40     LTO_ASM_FAILURE
41   };
42  
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)
48   };
49
50   enum LTOVisibilityTypes {
51     LTODefaultVisibility = 0,  ///< The GV is visible
52     LTOHiddenVisibility,       ///< The GV is hidden
53     LTOProtectedVisibility     ///< The GV is protected
54   };
55
56
57   enum LTOCodeGenModel {
58     LTO_CGM_Static,
59     LTO_CGM_Dynamic,
60     LTO_CGM_DynamicNoPIC
61   };
62
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 
66   /// the symbol name.
67   class LLVMSymbol {
68
69   public:
70
71     LTOLinkageTypes getLinkage() const { return linkage; }
72     LTOVisibilityTypes getVisibility() const { return visibility; }
73     void mayBeNotUsed();
74
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),
78                                                gv(g), name(n), 
79                                                mangledName(m), alignment(a) {}
80
81     const char *getName() { return name.c_str(); }
82     const char *getMangledName() { return mangledName.c_str(); }
83     int getAlignment() { return alignment; }
84
85   private:
86     enum LTOLinkageTypes linkage;
87     enum LTOVisibilityTypes visibility;
88     GlobalValue *gv;
89     std::string name;
90     std::string mangledName;
91     int alignment;
92   };
93
94   class string_compare {
95   public:
96     bool operator()(const char* left, const char* right) const { 
97       return (strcmp(left, right) == 0); 
98     }
99   };
100
101   /// This is abstract class to facilitate dlopen() interface.
102   /// See LTO below for more info.
103   class LinkTimeOptimizer {
104   public:
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 &,
110                                               NameToSymbolMap &,
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;
121   };
122
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 {
126
127   public:
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;
132
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);
143     void printVersion();
144
145     void setCodeGenModel(LTOCodeGenModel CGM) {
146       CGModel = CGM;
147     }
148
149     // Constructors and destructors
150     LTO() : Target(NULL), CGModel(LTO_CGM_Dynamic) {
151       /// TODO: Use Target info, it is available at this time.
152     }
153     ~LTO();
154
155   private:
156     Module *getModule (const std::string &InputFilename);
157     enum LTOStatus optimize(Module *, std::ostream &, 
158                             std::vector<const char *> &);
159     void getTarget(Module *);
160
161   private:
162     std::vector<Module *> modules;
163     NameToSymbolMap allSymbols;
164     NameToModuleMap allModules;
165     TargetMachine *Target;
166     LTOCodeGenModel CGModel;
167   };
168
169 } // End llvm namespace
170
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.
174 extern "C"
175 llvm::LinkTimeOptimizer *createLLVMOptimizer(unsigned VERSION = LLVM_LTO_VERSION);
176
177 #endif