simplify trivial function
[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
28   enum LTOStatus {
29     LTO_UNKNOWN,
30     LTO_OPT_SUCCESS,
31     LTO_READ_SUCCESS,
32     LTO_READ_FAILURE,
33     LTO_WRITE_FAILURE,
34     LTO_NO_TARGET,
35     LTO_NO_WORK,
36     LTO_MODULE_MERGE_FAILURE,
37     LTO_ASM_FAILURE
38   };
39  
40   enum LTOLinkageTypes {
41     LTOExternalLinkage, // Externally visible function
42     LTOLinkOnceLinkage, // Keep one copy of named function when linking (inline)
43     LTOWeakLinkage,     // Keep one copy of named function when linking (weak)
44     LTOInternalLinkage  // Rename collisions when linking (static functions)
45   };
46
47   /// This class represents LLVM symbol information without exposing details
48   /// of LLVM global values. It encapsulates symbol linkage information. This
49   /// is typically used in hash_map where associated name identifies the 
50   /// the symbol name.
51   class LLVMSymbol {
52
53   public:
54
55     LTOLinkageTypes getLinkage() const { return linkage; }
56     void mayBeNotUsed();
57
58     LLVMSymbol (enum LTOLinkageTypes lt, GlobalValue *g, const std::string &n, 
59                 const std::string &m) : linkage(lt), gv(g), name(n), 
60                                         mangledName(m) {}
61
62     const char *getName() { return name.c_str(); }
63     const char *getMangledName() { return mangledName.c_str(); }
64
65   private:
66     enum LTOLinkageTypes linkage;
67     GlobalValue *gv;
68     std::string name;
69     std::string mangledName;
70   };
71
72   class string_compare {
73   public:
74     bool operator()(const char* left, const char* right) const { 
75       return (strcmp(left, right) == 0); 
76     }
77   };
78
79   /// This is abstract class to facilitate dlopen() interface.
80   /// See LTO below for more info.
81   class LinkTimeOptimizer {
82   public:
83     typedef hash_map<const char*, LLVMSymbol*, hash<const char*>, 
84                      string_compare> NameToSymbolMap;
85     typedef hash_map<const char*, Module*, hash<const char*>, 
86                      string_compare> NameToModuleMap;
87     virtual enum LTOStatus readLLVMObjectFile(const std::string &,
88                                               NameToSymbolMap &,
89                                               std::set<std::string> &) = 0;
90     virtual enum LTOStatus optimizeModules(const std::string &,
91                                    std::vector<const char*> &,
92                                    std::string &) = 0;
93     virtual void getTargetTriple(const std::string &, std::string &) = 0;
94     virtual ~LinkTimeOptimizer() = 0;
95   };
96
97   /// This is the main link time optimization class. It exposes simple API
98   /// to perform link time optimization using LLVM intermodular optimizer.
99   class LTO : public LinkTimeOptimizer {
100
101   public:
102     typedef hash_map<const char*, LLVMSymbol*, hash<const char*>, 
103                      string_compare> NameToSymbolMap;
104     typedef hash_map<const char*, Module*, hash<const char*>, 
105                      string_compare> NameToModuleMap;
106
107     enum LTOStatus readLLVMObjectFile(const std::string &InputFilename,
108                                       NameToSymbolMap &symbols,
109                                       std::set<std::string> &references);
110     enum LTOStatus optimizeModules(const std::string &OutputFilename,
111                                    std::vector<const char*> &exportList,
112                                    std::string &targetTriple);
113     void getTargetTriple(const std::string &InputFilename, std::string &targetTriple);
114
115   private:
116     Module *getModule (const std::string &InputFilename);
117
118   private:
119     std::vector<Module *> modules;
120     NameToSymbolMap allSymbols;
121     NameToModuleMap allModules;
122   };
123
124 } // End llvm namespace
125
126 /// This provides C interface to initialize link time optimizer. This allows
127 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
128 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
129 extern "C"
130 llvm::LinkTimeOptimizer *createLLVMOptimizer();
131
132 #endif