367ad1026f012e2c605eeba3770ccbb68961bf40
[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     class LLVMContext;
36 }
37
38
39 //
40 // C++ class which implements the opaque lto_module_t
41 //
42 class LTOModule {
43 public:
44
45     static bool              isBitcodeFile(const void* mem, size_t length);
46     static bool              isBitcodeFile(const char* path);
47
48     static bool              isBitcodeFileForTarget(const void* mem, 
49                                     size_t length, const char* triplePrefix);
50
51     static bool              isBitcodeFileForTarget(const char* path, 
52                                                     const char* triplePrefix);
53
54     static LTOModule*        makeLTOModule(const char* path,
55                                           llvm::LLVMContext& Context,
56                                           std::string& errMsg);
57     static LTOModule*        makeLTOModule(const void* mem, size_t length,
58                                            llvm::LLVMContext& Context,
59                                            std::string& errMsg);
60
61     const char*              getTargetTriple();
62     uint32_t                 getSymbolCount();
63     lto_symbol_attributes    getSymbolAttributes(uint32_t index);
64     const char*              getSymbolName(uint32_t index);
65     
66     llvm::Module *           getLLVVMModule() { return _module.get(); }
67
68 private:
69                             LTOModule(llvm::Module* m, llvm::TargetMachine* t);
70
71     void                    lazyParseSymbols();
72     void                    addDefinedSymbol(llvm::GlobalValue* def, 
73                                                     llvm::Mangler& mangler, 
74                                                     bool isFunction);
75     void                    addPotentialUndefinedSymbol(llvm::GlobalValue* decl, 
76                                                         llvm::Mangler &mangler);
77     void                    findExternalRefs(llvm::Value* value, 
78                                                 llvm::Mangler& mangler);
79     void                    addDefinedFunctionSymbol(llvm::Function* f, 
80                                                         llvm::Mangler &mangler);
81     void                    addDefinedDataSymbol(llvm::GlobalValue* v, 
82                                                         llvm::Mangler &mangler);
83     void                    addAsmGlobalSymbol(const char *);
84     void                    addObjCClass(llvm::GlobalVariable* clgv);
85     void                    addObjCCategory(llvm::GlobalVariable* clgv);
86     void                    addObjCClassRef(llvm::GlobalVariable* clgv);
87     bool                    objcClassNameFromExpression(llvm::Constant* c, 
88                                                     std::string& name);
89
90     static bool             isTargetMatch(llvm::MemoryBuffer* memBuffer,
91                                                     const char* triplePrefix);
92
93     static LTOModule*       makeLTOModule(llvm::MemoryBuffer* buffer,
94                                           llvm::LLVMContext& Context,
95                                                         std::string& errMsg);
96     static llvm::MemoryBuffer* makeBuffer(const void* mem, size_t length);
97
98     typedef llvm::StringMap<uint8_t> StringSet;
99     
100     struct NameAndAttributes { 
101         const char*            name; 
102         lto_symbol_attributes  attributes; 
103     };
104
105     llvm::OwningPtr<llvm::Module>           _module;
106     llvm::OwningPtr<llvm::TargetMachine>    _target;
107     bool                                    _symbolsParsed;
108     std::vector<NameAndAttributes>          _symbols;
109     // _defines and _undefines only needed to disambiguate tentative definitions
110     StringSet                               _defines;    
111     llvm::StringMap<NameAndAttributes>      _undefines;
112 };
113
114 extern std::string getFeatureString(const char *TargetTriple);
115
116 #endif // LTO_MODULE_H
117