d4de787154a8143830b8ad511d8ff1d81f450674
[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, std::string n, 
59                 std::string m) : linkage(lt), gv(g), name(n), mangledName(m) {}
60
61     const char *getName() { return name.c_str(); }
62     const char *getMangledName() { return mangledName.c_str(); }
63
64   private:
65     enum LTOLinkageTypes linkage;
66     GlobalValue *gv;
67     std::string name;
68     std::string mangledName;
69   };
70
71   class string_compare {
72   public:
73     bool operator()(const char* left, const char* right) const { 
74       return (strcmp(left, right) == 0); 
75     }
76   };
77   
78   /// This is the main link time optimization class. It exposes simple API
79   /// to perform link time optimization using LLVM intermodular optimizer.
80   class LinkTimeOptimizer {
81
82   public:
83     typedef hash_map<const char*, LLVMSymbol*, hash<const char*>, 
84                      string_compare> NameToSymbolMap;
85
86     enum LTOStatus readLLVMObjectFile(const std::string &InputFilename,
87                                       NameToSymbolMap &symbols,
88                                       std::set<std::string> &references);
89     enum LTOStatus optimizeModules(const std::string &OutputFilename,
90                                    std::vector<const char*> &exportList);
91
92   private:
93     std::vector<Module *> modules;
94     NameToSymbolMap allSymbols;
95   };
96
97 } // End llvm namespace
98
99 /// This provides C interface to initialize link time optimizer. This allows
100 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
101 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
102 extern "C"
103 llvm::LinkTimeOptimizer *createLLVMOptimizer();
104
105 #endif