Add getTargetTriple() that linker can use to query target architecture.
[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 the main link time optimization class. It exposes simple API
80   /// to perform link time optimization using LLVM intermodular optimizer.
81   class LinkTimeOptimizer {
82
83   public:
84     typedef hash_map<const char*, LLVMSymbol*, hash<const char*>, 
85                      string_compare> NameToSymbolMap;
86     typedef hash_map<const char*, Module*, hash<const char*>, 
87                      string_compare> NameToModuleMap;
88
89     enum LTOStatus readLLVMObjectFile(const std::string &InputFilename,
90                                       NameToSymbolMap &symbols,
91                                       std::set<std::string> &references);
92     enum LTOStatus optimizeModules(const std::string &OutputFilename,
93                                    std::vector<const char*> &exportList,
94                                    std::string &targetTriple);
95     void getTargetTriple(const std::string &InputFilename, std::string &targetTriple);
96
97   private:
98     Module *getModule (const std::string &InputFilename);
99
100   private:
101     std::vector<Module *> modules;
102     NameToSymbolMap allSymbols;
103     NameToModuleMap allModules;
104   };
105
106 } // End llvm namespace
107
108 /// This provides C interface to initialize link time optimizer. This allows
109 /// linker to use dlopen() interface to dynamically load LinkTimeOptimizer.
110 /// extern "C" helps, because dlopen() interface uses name to find the symbol.
111 extern "C"
112 llvm::LinkTimeOptimizer *createLLVMOptimizer();
113
114 #endif