Fix for pr2093: direct operands aren't necessarily addresses, so don't
[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   };
50
51   enum LTOVisibilityTypes {
52     LTODefaultVisibility = 0,  ///< The GV is visible
53     LTOHiddenVisibility,       ///< The GV is hidden
54     LTOProtectedVisibility     ///< The GV is protected
55   };
56
57
58   enum LTOCodeGenModel {
59     LTO_CGM_Static,
60     LTO_CGM_Dynamic,
61     LTO_CGM_DynamicNoPIC
62   };
63
64   /// This class represents LLVM symbol information without exposing details
65   /// of LLVM global values. It encapsulates symbol linkage information. This
66   /// is typically used in hash_map where associated name identifies the 
67   /// the symbol name.
68   class LLVMSymbol {
69
70   public:
71
72     LTOLinkageTypes getLinkage() const { return linkage; }
73     LTOVisibilityTypes getVisibility() const { return visibility; }
74     void mayBeNotUsed();
75
76     LLVMSymbol (enum LTOLinkageTypes lt, enum LTOVisibilityTypes vis, 
77                 GlobalValue *g, const std::string &n, 
78                 const std::string &m, int a) : linkage(lt), visibility(vis),
79                                                gv(g), name(n), 
80                                                mangledName(m), alignment(a) {}
81
82     const char *getName() { return name.c_str(); }
83     const char *getMangledName() { return mangledName.c_str(); }
84     int getAlignment() { return alignment; }
85
86   private:
87     enum LTOLinkageTypes linkage;
88     enum LTOVisibilityTypes visibility;
89     GlobalValue *gv;
90     std::string name;
91     std::string mangledName;
92     int alignment;
93   };
94
95   class string_compare {
96   public:
97     bool operator()(const char* left, const char* right) const { 
98       return (strcmp(left, right) == 0); 
99     }
100   };
101
102   /// This is abstract class to facilitate dlopen() interface.
103   /// See LTO below for more info.
104   class LinkTimeOptimizer {
105   public:
106     typedef hash_map<const char*, LLVMSymbol*, hash<const char*>, 
107                      string_compare> NameToSymbolMap;
108     typedef hash_map<const char*, Module*, hash<const char*>, 
109                      string_compare> NameToModuleMap;
110     virtual enum LTOStatus readLLVMObjectFile(const std::string &,
111                                               NameToSymbolMap &,
112                                               std::set<std::string> &) = 0;
113     virtual enum LTOStatus optimizeModules(const std::string &,
114                                            std::vector<const char*> &exportList,
115                                            std::string &targetTriple,
116                                            bool saveTemps, const char *) = 0;
117     virtual void getTargetTriple(const std::string &, std::string &) = 0;
118     virtual void removeModule (const std::string &InputFilename) = 0;
119     virtual void setCodeGenModel(LTOCodeGenModel CGM) = 0;
120     virtual void printVersion () = 0;
121     virtual ~LinkTimeOptimizer() = 0;
122   };
123
124   /// This is the main link time optimization class. It exposes simple API
125   /// to perform link time optimization using LLVM intermodular optimizer.
126   class LTO : public LinkTimeOptimizer {
127
128   public:
129     typedef hash_map<const char*, LLVMSymbol*, hash<const char*>, 
130                      string_compare> NameToSymbolMap;
131     typedef hash_map<const char*, Module*, hash<const char*>, 
132                      string_compare> NameToModuleMap;
133
134     enum LTOStatus readLLVMObjectFile(const std::string &InputFilename,
135                                       NameToSymbolMap &symbols,
136                                       std::set<std::string> &references);
137     enum LTOStatus optimizeModules(const std::string &OutputFilename,
138                                    std::vector<const char*> &exportList,
139                                    std::string &targetTriple, 
140                                    bool saveTemps,  const char *);
141     void getTargetTriple(const std::string &InputFilename, 
142                          std::string &targetTriple);
143     void removeModule (const std::string &InputFilename);
144     void printVersion();
145
146     void setCodeGenModel(LTOCodeGenModel CGM) {
147       CGModel = CGM;
148     }
149
150     // Constructors and destructors
151     LTO() : Target(NULL), CGModel(LTO_CGM_Dynamic) {
152       /// TODO: Use Target info, it is available at this time.
153     }
154     ~LTO();
155
156   private:
157     Module *getModule (const std::string &InputFilename);
158     enum LTOStatus optimize(Module *, std::ostream &, 
159                             std::vector<const char *> &);
160     void getTarget(Module *);
161
162   private:
163     std::vector<Module *> modules;
164     NameToSymbolMap allSymbols;
165     NameToModuleMap allModules;
166     TargetMachine *Target;
167     LTOCodeGenModel CGModel;
168   };
169
170 } // End llvm namespace
171
172 /// This provides C interface to initialize link time optimizer. This allows
173 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
174 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
175 extern "C"
176 llvm::LinkTimeOptimizer *createLLVMOptimizer(unsigned VERSION = LLVM_LTO_VERSION);
177
178 #endif