9de02a2a4e28cbeabf66138a3af0d2ff1873c037
[oota-llvm.git] / tools / lto / LTOModule.h
1 //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the LTOModule class. 
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LTO_MODULE_H
15 #define LTO_MODULE_H
16
17 #include "llvm/Module.h"
18 #include "llvm/ADT/OwningPtr.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/ADT/StringMap.h"
21
22 #include "llvm-c/lto.h"
23
24 #include <vector>
25 #include <string>
26
27
28 // forward references to llvm classes
29 namespace llvm {
30     class Mangler;
31     class MemoryBuffer;
32     class GlobalValue;
33     class Value;
34     class Function;
35 }
36
37
38 //
39 // C++ class which implements the opaque lto_module_t
40 //
41 class LTOModule {
42 public:
43
44     static bool              isBitcodeFile(const void* mem, size_t length);
45     static bool              isBitcodeFile(const char* path);
46
47     static bool              isBitcodeFileForTarget(const void* mem, 
48                                     size_t length, const char* triplePrefix);
49
50     static bool              isBitcodeFileForTarget(const char* path, 
51                                                     const char* triplePrefix);
52
53     static LTOModule*        makeLTOModule(const char* path, std::string& errMsg);
54     static LTOModule*        makeLTOModule(const void* mem, size_t length,
55                                                             std::string& errMsg);
56
57     const char*              getTargetTriple();
58     uint32_t                 getSymbolCount();
59     lto_symbol_attributes    getSymbolAttributes(uint32_t index);
60     const char*              getSymbolName(uint32_t index);
61     
62     llvm::Module *           getLLVVMModule() { return _module.get(); }
63
64 private:
65                             LTOModule(llvm::Module* m, llvm::TargetMachine* t);
66
67     void                    lazyParseSymbols();
68     void                    addDefinedSymbol(llvm::GlobalValue* def, 
69                                                     llvm::Mangler& mangler, 
70                                                     bool isFunction);
71     void                    addPotentialUndefinedSymbol(llvm::GlobalValue* decl, 
72                                                         llvm::Mangler &mangler);
73     void                    findExternalRefs(llvm::Value* value, 
74                                                 llvm::Mangler& mangler);
75     void                    addDefinedFunctionSymbol(llvm::Function* f, 
76                                                         llvm::Mangler &mangler);
77     void                    addDefinedDataSymbol(llvm::GlobalValue* v, 
78                                                         llvm::Mangler &mangler);
79     void                    addAsmGlobalSymbol(const char *);
80     void                    addObjCClass(llvm::GlobalVariable* clgv);
81     void                    addObjCCategory(llvm::GlobalVariable* clgv);
82     void                    addObjCClassRef(llvm::GlobalVariable* clgv);
83     bool                    objcClassNameFromExpression(llvm::Constant* c, 
84                                                     std::string& name);
85
86     static bool             isTargetMatch(llvm::MemoryBuffer* memBuffer, 
87                                                     const char* triplePrefix);
88
89     static LTOModule*       makeLTOModule(llvm::MemoryBuffer* buffer, 
90                                                         std::string& errMsg);
91     static llvm::MemoryBuffer* makeBuffer(const void* mem, size_t length);
92
93     typedef llvm::StringMap<uint8_t> StringSet;
94     
95     struct NameAndAttributes { 
96         const char*            name; 
97         lto_symbol_attributes  attributes; 
98     };
99
100     llvm::OwningPtr<llvm::Module>           _module;
101     llvm::OwningPtr<llvm::TargetMachine>    _target;
102     bool                                    _symbolsParsed;
103     std::vector<NameAndAttributes>          _symbols;
104     // _defines and _undefines only needed to disambiguate tentative definitions
105     StringSet                               _defines;    
106     llvm::StringMap<NameAndAttributes>      _undefines;
107 };
108
109 extern std::string getFeatureString(const char *TargetTriple);
110
111 #endif // LTO_MODULE_H
112