c126bb23620a1a57055257290a2236819c233435
[oota-llvm.git] / include / llvm / LinkTimeOptimizer.h
1 //===-- llvm/LinkTimeOptimizer.h - Public Interface  ------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
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 namespace llvm {
24
25   class Module;
26   class GlobalValue;
27   class TargetMachine;
28
29   enum LTOStatus {
30     LTO_UNKNOWN,
31     LTO_OPT_SUCCESS,
32     LTO_READ_SUCCESS,
33     LTO_READ_FAILURE,
34     LTO_WRITE_FAILURE,
35     LTO_NO_TARGET,
36     LTO_NO_WORK,
37     LTO_MODULE_MERGE_FAILURE,
38     LTO_ASM_FAILURE
39   };
40  
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)
46   };
47
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 
51   /// the symbol name.
52   class LLVMSymbol {
53
54   public:
55
56     LTOLinkageTypes getLinkage() const { return linkage; }
57     void mayBeNotUsed();
58
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) {}
62
63     const char *getName() { return name.c_str(); }
64     const char *getMangledName() { return mangledName.c_str(); }
65     int getAlignment() { return alignment; }
66
67   private:
68     enum LTOLinkageTypes linkage;
69     GlobalValue *gv;
70     std::string name;
71     std::string mangledName;
72     int alignment;
73   };
74
75   class string_compare {
76   public:
77     bool operator()(const char* left, const char* right) const { 
78       return (strcmp(left, right) == 0); 
79     }
80   };
81
82   /// This is abstract class to facilitate dlopen() interface.
83   /// See LTO below for more info.
84   class LinkTimeOptimizer {
85   public:
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 &,
91                                               NameToSymbolMap &,
92                                               std::set<std::string> &) = 0;
93     virtual enum LTOStatus optimizeModules(const std::string &,
94                                    std::vector<const char*> &,
95                                    std::string &) = 0;
96     virtual void getTargetTriple(const std::string &, std::string &) = 0;
97     virtual void removeModule (const std::string &InputFilename) = 0;
98     virtual ~LinkTimeOptimizer() = 0;
99   };
100
101   /// This is the main link time optimization class. It exposes simple API
102   /// to perform link time optimization using LLVM intermodular optimizer.
103   class LTO : public LinkTimeOptimizer {
104
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
111     enum LTOStatus readLLVMObjectFile(const std::string &InputFilename,
112                                       NameToSymbolMap &symbols,
113                                       std::set<std::string> &references);
114     enum LTOStatus optimizeModules(const std::string &OutputFilename,
115                                    std::vector<const char*> &exportList,
116                                    std::string &targetTriple);
117     void getTargetTriple(const std::string &InputFilename, std::string &targetTriple);
118     void removeModule (const std::string &InputFilename);
119
120     // Constructors and destructors
121     LTO() { 
122       /// TODO: Use Target info, it is available at this time.
123       Target = NULL; 
124     }
125     ~LTO();
126
127   private:
128     Module *getModule (const std::string &InputFilename);
129     enum LTOStatus optimize(Module *, std::ostream &, 
130                             std::vector<const char *> &);
131     void getTarget(Module *);
132
133   private:
134     std::vector<Module *> modules;
135     NameToSymbolMap allSymbols;
136     NameToModuleMap allModules;
137     TargetMachine *Target;
138   };
139
140 } // End llvm namespace
141
142 /// This provides C interface to initialize link time optimizer. This allows
143 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
144 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
145 extern "C"
146 llvm::LinkTimeOptimizer *createLLVMOptimizer();
147
148 #endif