1 //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the LTOModule class.
12 //===----------------------------------------------------------------------===//
17 #include "llvm/Module.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/Target/Mangler.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/ADT/OwningPtr.h"
22 #include "llvm/ADT/StringMap.h"
24 #include "llvm-c/lto.h"
30 // forward references to llvm classes
40 // C++ class which implements the opaque lto_module_t
44 static bool isBitcodeFile(const void* mem, size_t length);
45 static bool isBitcodeFile(const char* path);
47 static bool isBitcodeFileForTarget(const void* mem,
48 size_t length, const char* triplePrefix);
50 static bool isBitcodeFileForTarget(const char* path,
51 const char* triplePrefix);
53 static LTOModule* makeLTOModule(const char* path,
55 static LTOModule* makeLTOModule(int fd, const char *path,
58 static LTOModule* makeLTOModule(int fd, const char *path,
63 static LTOModule* makeLTOModule(const void* mem, size_t length,
66 const char* getTargetTriple();
67 void setTargetTriple(const char*);
68 uint32_t getSymbolCount();
69 lto_symbol_attributes getSymbolAttributes(uint32_t index);
70 const char* getSymbolName(uint32_t index);
72 llvm::Module * getLLVVMModule() { return _module.get(); }
73 const std::vector<const char*> &getAsmUndefinedRefs() {
74 return _asm_undefines;
78 LTOModule(llvm::Module* m, llvm::TargetMachine* t);
80 bool ParseSymbols(std::string &errMsg);
81 void addDefinedSymbol(llvm::GlobalValue* def,
83 void addPotentialUndefinedSymbol(llvm::GlobalValue* decl);
84 void addDefinedFunctionSymbol(llvm::Function* f);
85 void addDefinedDataSymbol(llvm::GlobalValue* v);
86 bool addAsmGlobalSymbols(std::string &errMsg);
87 void addAsmGlobalSymbol(const char *,
88 lto_symbol_attributes scope);
89 void addAsmGlobalSymbolUndef(const char *);
90 void addObjCClass(llvm::GlobalVariable* clgv);
91 void addObjCCategory(llvm::GlobalVariable* clgv);
92 void addObjCClassRef(llvm::GlobalVariable* clgv);
93 bool objcClassNameFromExpression(llvm::Constant* c,
96 static bool isTargetMatch(llvm::MemoryBuffer* memBuffer,
97 const char* triplePrefix);
99 static LTOModule* makeLTOModule(llvm::MemoryBuffer* buffer,
100 std::string& errMsg);
101 static llvm::MemoryBuffer* makeBuffer(const void* mem, size_t length);
103 typedef llvm::StringMap<uint8_t> StringSet;
105 struct NameAndAttributes {
107 lto_symbol_attributes attributes;
110 llvm::OwningPtr<llvm::Module> _module;
111 llvm::OwningPtr<llvm::TargetMachine> _target;
112 std::vector<NameAndAttributes> _symbols;
113 // _defines and _undefines only needed to disambiguate tentative definitions
115 llvm::StringMap<NameAndAttributes> _undefines;
116 std::vector<const char*> _asm_undefines;
117 llvm::MCContext _context;
118 // Use mangler to add GlobalPrefix to names to match linker names.
119 llvm::Mangler _mangler;
122 #endif // LTO_MODULE_H