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
39 // C++ class which implements the opaque lto_module_t
43 typedef llvm::StringMap<uint8_t> StringSet;
45 struct NameAndAttributes {
46 enum name_type { IsFunction, IsData };
48 lto_symbol_attributes attributes;
51 llvm::OwningPtr<llvm::Module> _module;
52 llvm::OwningPtr<llvm::TargetMachine> _target;
53 std::vector<NameAndAttributes> _symbols;
55 // _defines and _undefines only needed to disambiguate tentative definitions
57 llvm::StringMap<NameAndAttributes> _undefines;
58 std::vector<const char*> _asm_undefines;
59 llvm::MCContext _context;
61 // Use mangler to add GlobalPrefix to names to match linker names.
62 llvm::Mangler _mangler;
64 LTOModule(llvm::Module *m, llvm::TargetMachine *t);
66 /// isBitcodeFile - Returns 'true' if the file or memory contents is LLVM
68 static bool isBitcodeFile(const void *mem, size_t length);
69 static bool isBitcodeFile(const char *path);
71 /// isBitcodeFileForTarget - Returns 'true' if the file or memory contents
72 /// is LLVM bitcode for the specified triple.
73 static bool isBitcodeFileForTarget(const void *mem,
75 const char *triplePrefix);
76 static bool isBitcodeFileForTarget(const char *path,
77 const char *triplePrefix);
79 /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership
81 static LTOModule* makeLTOModule(const char* path,
83 static LTOModule* makeLTOModule(int fd, const char *path,
86 static LTOModule* makeLTOModule(int fd, const char *path,
91 static LTOModule* makeLTOModule(const void *mem, size_t length,
94 /// getTargetTriple - Return the Module's target triple.
95 const char* getTargetTriple() {
96 return _module->getTargetTriple().c_str();
99 /// setTargetTriple - Set the Module's target triple.
100 void setTargetTriple(const char *triple) {
101 _module->setTargetTriple(triple);
104 /// getSymbolCount - Get the number of symbols
105 uint32_t getSymbolCount() {
106 return _symbols.size();
109 /// getSymbolAttributes - Get the attributes for a symbol at the specified
111 lto_symbol_attributes getSymbolAttributes(uint32_t index) {
112 if (index < _symbols.size())
113 return _symbols[index].attributes;
115 return lto_symbol_attributes(0);
118 /// getSymbolName - Get the name of the symbol at the specified index.
119 const char* getSymbolName(uint32_t index) {
120 if (index < _symbols.size())
121 return _symbols[index].name;
126 /// getLLVVMModule - Return the Module.
127 llvm::Module * getLLVVMModule() { return _module.get(); }
129 /// getAsmUndefinedRefs -
130 const std::vector<const char*> &getAsmUndefinedRefs() {
131 return _asm_undefines;
135 /// ParseSymbols - Parse the symbols from the module and model-level ASM and
136 /// add them to either the defined or undefined lists.
137 bool ParseSymbols(std::string &errMsg);
139 /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet
140 /// to a list to be resolved later.
141 void addPotentialUndefinedSymbol(llvm::GlobalValue *dcl);
143 /// addDefinedSymbol - Add a defined symbol to the list.
144 void addDefinedSymbol(llvm::GlobalValue *def,
147 /// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
148 void addDefinedFunctionSymbol(llvm::Function *f);
150 /// addDefinedDataSymbol - Add a data symbol as defined to the list.
151 void addDefinedDataSymbol(llvm::GlobalValue *v);
153 /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
154 /// defined or undefined lists.
155 bool addAsmGlobalSymbols(std::string &errMsg);
157 /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
159 void addAsmGlobalSymbol(const char *,
160 lto_symbol_attributes scope);
162 /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to
163 /// the undefined list.
164 void addAsmGlobalSymbolUndef(const char *);
166 /// addObjCClass - Parse i386/ppc ObjC class data structure.
167 void addObjCClass(llvm::GlobalVariable *clgv);
169 /// addObjCCategory - Parse i386/ppc ObjC category data structure.
170 void addObjCCategory(llvm::GlobalVariable *clgv);
172 /// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
173 void addObjCClassRef(llvm::GlobalVariable *clgv);
175 /// objcClassNameFromExpression - Get string that the data pointer points
177 bool objcClassNameFromExpression(llvm::Constant* c,
180 /// isTargetMatch - Returns 'true' if the memory buffer is for the specified
182 static bool isTargetMatch(llvm::MemoryBuffer *memBuffer,
183 const char *triplePrefix);
185 /// makeLTOModule - Create an LTOModule (private version). N.B. This
186 /// method takes ownership of the buffer.
187 static LTOModule* makeLTOModule(llvm::MemoryBuffer *buffer,
188 std::string &errMsg);
190 /// makeBuffer - Create a MemoryBuffer from a memory range.
191 static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length);
194 #endif // LTO_MODULE_H