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