9770757339b5879b5200a7fd7e3f1b248632327c
[oota-llvm.git] / tools / lto2 / 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/GlobalValue.h"
19 #include "llvm/Constants.h"
20 #include "llvm/Support/Mangler.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/ADT/StringMap.h"
23
24 #include "llvm-c/lto.h"
25
26 #include <vector>
27
28
29 //
30 // C++ class which implements the opaque lto_module_t
31 //
32 class LTOModule {
33 public:
34
35     static bool              isBitcodeFile(const void* mem, size_t length);
36     static bool              isBitcodeFile(const char* path);
37
38     static bool              isBitcodeFileForTarget(const void* mem, 
39                                     size_t length, const char* triplePrefix);
40
41     static bool              isBitcodeFileForTarget(const char* path, 
42                                                     const char* triplePrefix);
43
44     static LTOModule*        makeLTOModule(const char* path, std::string& errMsg);
45     static LTOModule*        makeLTOModule(const void* mem, size_t length,
46                                                             std::string& errMsg);
47                             ~LTOModule();
48
49     const char*              getTargetTriple();
50     uint32_t                 getSymbolCount();
51     lto_symbol_attributes    getSymbolAttributes(uint32_t index);
52     const char*              getSymbolName(uint32_t index);
53     
54     llvm::Module *           getLLVVMModule() { return _module; }
55     bool                     targetSupported() { return (_target !=  NULL); }
56
57 private:
58                             LTOModule(llvm::Module* m, llvm::TargetMachine* t);
59
60     void                    addDefinedSymbol(llvm::GlobalValue* def, 
61                                                     llvm::Mangler& mangler, 
62                                                     bool isFunction);
63     void                    addUndefinedSymbol(const char* name);
64     void                    findExternalRefs(llvm::Value* value, 
65                                                 llvm::Mangler& mangler);
66     
67     typedef llvm::StringMap<uint8_t> StringSet;
68     
69     struct NameAndAttributes { 
70         const char*            name; 
71         lto_symbol_attributes  attributes; 
72     };
73
74     llvm::Module *                   _module;
75     llvm::TargetMachine *            _target;
76     bool                             _symbolsParsed;
77     std::vector<NameAndAttributes>   _symbols;
78     StringSet                        _defines;    // only needed to disambiguate tentative definitions
79     StringSet                        _undefines;  // only needed to disambiguate tentative definitions
80 };
81
82 #endif // LTO_MODULE_H
83