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-c/lto.h"
18 #include "llvm/ADT/OwningPtr.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/IR/Mangler.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCObjectFileInfo.h"
24 #include "llvm/Target/TargetMachine.h"
28 // Forward references to llvm classes.
37 //===----------------------------------------------------------------------===//
38 /// LTOModule - C++ class which implements the opaque lto_module_t type.
42 typedef llvm::StringMap<uint8_t> StringSet;
44 struct NameAndAttributes {
48 const llvm::GlobalValue *symbol;
51 llvm::OwningPtr<llvm::Module> _module;
52 llvm::OwningPtr<llvm::TargetMachine> _target;
53 llvm::MCObjectFileInfo ObjFileInfo;
54 StringSet _linkeropt_strings;
55 std::vector<const char *> _deplibs;
56 std::vector<const char *> _linkeropts;
57 std::vector<NameAndAttributes> _symbols;
59 // _defines and _undefines only needed to disambiguate tentative definitions
61 llvm::StringMap<NameAndAttributes> _undefines;
62 std::vector<const char*> _asm_undefines;
63 llvm::MCContext _context;
65 // Use mangler to add GlobalPrefix to names to match linker names.
66 llvm::Mangler _mangler;
68 LTOModule(llvm::Module *m, llvm::TargetMachine *t);
70 /// isBitcodeFile - Returns 'true' if the file or memory contents is LLVM
72 static bool isBitcodeFile(const void *mem, size_t length);
73 static bool isBitcodeFile(const char *path);
75 /// isBitcodeFileForTarget - Returns 'true' if the file or memory contents
76 /// is LLVM bitcode for the specified triple.
77 static bool isBitcodeFileForTarget(const void *mem,
79 const char *triplePrefix);
80 static bool isBitcodeFileForTarget(const char *path,
81 const char *triplePrefix);
83 /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership
84 /// of the buffer. The caller must have initialized the Targets, the
85 /// TargetMCs, the AsmPrinters, and the AsmParsers by calling:
87 /// InitializeAllTargets();
88 /// InitializeAllTargetMCs();
89 /// InitializeAllAsmPrinters();
90 /// InitializeAllAsmParsers();
91 static LTOModule *makeLTOModule(const char* path,
92 llvm::TargetOptions options,
94 static LTOModule *makeLTOModule(int fd, const char *path,
95 size_t size, llvm::TargetOptions options,
97 static LTOModule *makeLTOModule(int fd, const char *path,
99 off_t offset, llvm::TargetOptions options,
100 std::string& errMsg);
101 static LTOModule *makeLTOModule(const void *mem, size_t length,
102 llvm::TargetOptions options,
104 llvm::StringRef path = "");
106 /// getTargetTriple - Return the Module's target triple.
107 const char *getTargetTriple() {
108 return _module->getTargetTriple().c_str();
111 /// setTargetTriple - Set the Module's target triple.
112 void setTargetTriple(const char *triple) {
113 _module->setTargetTriple(triple);
116 /// getSymbolCount - Get the number of symbols
117 uint32_t getSymbolCount() {
118 return _symbols.size();
121 /// getSymbolAttributes - Get the attributes for a symbol at the specified
123 lto_symbol_attributes getSymbolAttributes(uint32_t index) {
124 if (index < _symbols.size())
125 return lto_symbol_attributes(_symbols[index].attributes);
126 return lto_symbol_attributes(0);
129 /// getSymbolName - Get the name of the symbol at the specified index.
130 const char *getSymbolName(uint32_t index) {
131 if (index < _symbols.size())
132 return _symbols[index].name;
136 /// getDependentLibraryCount - Get the number of dependent libraries
137 uint32_t getDependentLibraryCount() {
138 return _deplibs.size();
141 /// getDependentLibrary - Get the dependent library at the specified index.
142 const char *getDependentLibrary(uint32_t index) {
143 if (index < _deplibs.size())
144 return _deplibs[index];
148 /// getLinkerOptCount - Get the number of linker options
149 uint32_t getLinkerOptCount() {
150 return _linkeropts.size();
153 /// getLinkerOpt - Get the linker option at the specified index.
154 const char *getLinkerOpt(uint32_t index) {
155 if (index < _linkeropts.size())
156 return _linkeropts[index];
160 /// getLLVVMModule - Return the Module.
161 llvm::Module *getLLVVMModule() { return _module.get(); }
163 /// getAsmUndefinedRefs -
164 const std::vector<const char*> &getAsmUndefinedRefs() {
165 return _asm_undefines;
169 /// parseMetadata - Parse metadata from the module
170 // FIXME: it only parses "Linker Options" metadata at the moment
171 void parseMetadata();
173 /// parseSymbols - Parse the symbols from the module and model-level ASM and
174 /// add them to either the defined or undefined lists.
175 bool parseSymbols(std::string &errMsg);
177 /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet
178 /// to a list to be resolved later.
179 void addPotentialUndefinedSymbol(const llvm::GlobalValue *dcl, bool isFunc);
181 /// addDefinedSymbol - Add a defined symbol to the list.
182 void addDefinedSymbol(const llvm::GlobalValue *def, bool isFunction);
184 /// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
185 void addDefinedFunctionSymbol(const llvm::Function *f);
187 /// addDefinedDataSymbol - Add a data symbol as defined to the list.
188 void addDefinedDataSymbol(const llvm::GlobalValue *v);
190 /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
191 /// defined or undefined lists.
192 bool addAsmGlobalSymbols(std::string &errMsg);
194 /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
196 void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
198 /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to
199 /// the undefined list.
200 void addAsmGlobalSymbolUndef(const char *);
202 /// addObjCClass - Parse i386/ppc ObjC class data structure.
203 void addObjCClass(const llvm::GlobalVariable *clgv);
205 /// addObjCCategory - Parse i386/ppc ObjC category data structure.
206 void addObjCCategory(const llvm::GlobalVariable *clgv);
208 /// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
209 void addObjCClassRef(const llvm::GlobalVariable *clgv);
211 /// objcClassNameFromExpression - Get string that the data pointer points
213 bool objcClassNameFromExpression(const llvm::Constant* c, std::string &name);
215 /// isTargetMatch - Returns 'true' if the memory buffer is for the specified
217 static bool isTargetMatch(llvm::MemoryBuffer *memBuffer,
218 const char *triplePrefix);
220 /// makeLTOModule - Create an LTOModule (private version). N.B. This
221 /// method takes ownership of the buffer.
222 static LTOModule *makeLTOModule(llvm::MemoryBuffer *buffer,
223 llvm::TargetOptions options,
224 std::string &errMsg);
226 /// Create a MemoryBuffer from a memory range with an optional name.
227 static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length,
228 llvm::StringRef name = "");
231 #endif // LTO_MODULE_H