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/StringMap.h"
19 #include "llvm/IR/Mangler.h"
20 #include "llvm/IR/Module.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCObjectFileInfo.h"
23 #include "llvm/Target/TargetMachine.h"
27 // Forward references to llvm classes.
36 //===----------------------------------------------------------------------===//
37 /// C++ class which implements the opaque lto_module_t type.
41 typedef llvm::StringMap<uint8_t> StringSet;
43 struct NameAndAttributes {
47 const llvm::GlobalValue *symbol;
50 std::unique_ptr<llvm::Module> _module;
51 std::unique_ptr<llvm::TargetMachine> _target;
52 llvm::MCObjectFileInfo ObjFileInfo;
53 StringSet _linkeropt_strings;
54 std::vector<const char *> _deplibs;
55 std::vector<const char *> _linkeropts;
56 std::vector<NameAndAttributes> _symbols;
58 // _defines and _undefines only needed to disambiguate tentative definitions
60 llvm::StringMap<NameAndAttributes> _undefines;
61 std::vector<const char*> _asm_undefines;
62 llvm::MCContext _context;
64 // Use mangler to add GlobalPrefix to names to match linker names.
65 llvm::Mangler _mangler;
67 LTOModule(llvm::Module *m, llvm::TargetMachine *t);
69 /// Returns 'true' if the file or memory contents is LLVM bitcode.
70 static bool isBitcodeFile(const void *mem, size_t length);
71 static bool isBitcodeFile(const char *path);
73 /// Returns 'true' if the file or memory contents is LLVM bitcode for the
75 static bool isBitcodeFileForTarget(const void *mem,
77 const char *triplePrefix);
78 static bool isBitcodeFileForTarget(const char *path,
79 const char *triplePrefix);
81 /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
82 /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
83 /// and the AsmParsers by calling:
85 /// InitializeAllTargets();
86 /// InitializeAllTargetMCs();
87 /// InitializeAllAsmPrinters();
88 /// InitializeAllAsmParsers();
89 static LTOModule *makeLTOModule(const char* path,
90 llvm::TargetOptions options,
92 static LTOModule *makeLTOModule(int fd, const char *path,
93 size_t size, llvm::TargetOptions options,
95 static LTOModule *makeLTOModule(int fd, const char *path,
97 off_t offset, llvm::TargetOptions options,
99 static LTOModule *makeLTOModule(const void *mem, size_t length,
100 llvm::TargetOptions options,
102 llvm::StringRef path = "");
104 /// Return the Module's target triple.
105 const char *getTargetTriple() {
106 return _module->getTargetTriple().c_str();
109 /// Set the Module's target triple.
110 void setTargetTriple(const char *triple) {
111 _module->setTargetTriple(triple);
114 /// Get the number of symbols
115 uint32_t getSymbolCount() {
116 return _symbols.size();
119 /// Get the attributes for a symbol at the specified index.
120 lto_symbol_attributes getSymbolAttributes(uint32_t index) {
121 if (index < _symbols.size())
122 return lto_symbol_attributes(_symbols[index].attributes);
123 return lto_symbol_attributes(0);
126 /// Get the name of the symbol at the specified index.
127 const char *getSymbolName(uint32_t index) {
128 if (index < _symbols.size())
129 return _symbols[index].name;
133 /// Get the number of dependent libraries
134 uint32_t getDependentLibraryCount() {
135 return _deplibs.size();
138 /// Get the dependent library at the specified index.
139 const char *getDependentLibrary(uint32_t index) {
140 if (index < _deplibs.size())
141 return _deplibs[index];
145 /// Get the number of linker options
146 uint32_t getLinkerOptCount() {
147 return _linkeropts.size();
150 /// Get the linker option at the specified index.
151 const char *getLinkerOpt(uint32_t index) {
152 if (index < _linkeropts.size())
153 return _linkeropts[index];
157 /// Return the Module.
158 llvm::Module *getLLVVMModule() { return _module.get(); }
160 const std::vector<const char*> &getAsmUndefinedRefs() {
161 return _asm_undefines;
165 /// Parse metadata from the module
166 // FIXME: it only parses "Linker Options" metadata at the moment
167 void parseMetadata();
169 /// Parse the symbols from the module and model-level ASM and add them to
170 /// either the defined or undefined lists.
171 bool parseSymbols(std::string &errMsg);
173 /// Add a symbol which isn't defined just yet to a list to be resolved later.
174 void addPotentialUndefinedSymbol(const llvm::GlobalValue *dcl, bool isFunc);
176 /// Add a defined symbol to the list.
177 void addDefinedSymbol(const llvm::GlobalValue *def, bool isFunction);
179 /// Add a function symbol as defined to the list.
180 void addDefinedFunctionSymbol(const llvm::Function *f);
182 /// Add a data symbol as defined to the list.
183 void addDefinedDataSymbol(const llvm::GlobalValue *v);
185 /// Add global symbols from module-level ASM to the defined or undefined
187 bool addAsmGlobalSymbols(std::string &errMsg);
189 /// Add a global symbol from module-level ASM to the defined list.
190 void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
192 /// Add a global symbol from module-level ASM to the undefined list.
193 void addAsmGlobalSymbolUndef(const char *);
195 /// Parse i386/ppc ObjC class data structure.
196 void addObjCClass(const llvm::GlobalVariable *clgv);
198 /// Parse i386/ppc ObjC category data structure.
199 void addObjCCategory(const llvm::GlobalVariable *clgv);
201 /// Parse i386/ppc ObjC class list data structure.
202 void addObjCClassRef(const llvm::GlobalVariable *clgv);
204 /// Get string that the data pointer points to.
205 bool objcClassNameFromExpression(const llvm::Constant* c, std::string &name);
207 /// Returns 'true' if the memory buffer is for the specified target triple.
208 static bool isTargetMatch(llvm::MemoryBuffer *memBuffer,
209 const char *triplePrefix);
211 /// Create an LTOModule (private version). N.B. This method takes ownership of
213 static LTOModule *makeLTOModule(llvm::MemoryBuffer *buffer,
214 llvm::TargetOptions options,
215 std::string &errMsg);
217 /// Create a MemoryBuffer from a memory range with an optional name.
218 static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length,
219 llvm::StringRef name = "");
222 #endif // LTO_MODULE_H