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/Module.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/Target/Mangler.h"
23 #include "llvm/Target/TargetMachine.h"
27 // Forward references to llvm classes.
36 //===----------------------------------------------------------------------===//
37 /// LTOModule - 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 llvm::OwningPtr<llvm::Module> _module;
51 llvm::OwningPtr<llvm::TargetMachine> _target;
52 std::vector<NameAndAttributes> _symbols;
54 // _defines and _undefines only needed to disambiguate tentative definitions
56 llvm::StringMap<NameAndAttributes> _undefines;
57 std::vector<const char*> _asm_undefines;
58 llvm::MCContext _context;
60 // Use mangler to add GlobalPrefix to names to match linker names.
61 llvm::Mangler _mangler;
63 LTOModule(llvm::Module *m, llvm::TargetMachine *t);
65 /// isBitcodeFile - Returns 'true' if the file or memory contents is LLVM
67 static bool isBitcodeFile(const void *mem, size_t length);
68 static bool isBitcodeFile(const char *path);
70 /// isBitcodeFileForTarget - Returns 'true' if the file or memory contents
71 /// is LLVM bitcode for the specified triple.
72 static bool isBitcodeFileForTarget(const void *mem,
74 const char *triplePrefix);
75 static bool isBitcodeFileForTarget(const char *path,
76 const char *triplePrefix);
78 /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership
79 /// of the buffer. The caller must have initialized the Targets, the
80 /// TargetMCs, the AsmPrinters, and the AsmParsers by calling:
82 /// InitializeAllTargets();
83 /// InitializeAllTargetMCs();
84 /// InitializeAllAsmPrinters();
85 /// InitializeAllAsmParsers();
86 static LTOModule *makeLTOModule(const char* path,
88 static LTOModule *makeLTOModule(int fd, const char *path,
89 size_t size, std::string &errMsg);
90 static LTOModule *makeLTOModule(int fd, const char *path,
94 static LTOModule *makeLTOModule(const void *mem, size_t length,
97 /// getTargetTriple - Return the Module's target triple.
98 const char *getTargetTriple() {
99 return _module->getTargetTriple().c_str();
102 /// setTargetTriple - Set the Module's target triple.
103 void setTargetTriple(const char *triple) {
104 _module->setTargetTriple(triple);
107 /// getSymbolCount - Get the number of symbols
108 uint32_t getSymbolCount() {
109 return _symbols.size();
112 /// getSymbolAttributes - Get the attributes for a symbol at the specified
114 lto_symbol_attributes getSymbolAttributes(uint32_t index) {
115 if (index < _symbols.size())
116 return lto_symbol_attributes(_symbols[index].attributes);
117 return lto_symbol_attributes(0);
120 /// getSymbolName - Get the name of the symbol at the specified index.
121 const char *getSymbolName(uint32_t index) {
122 if (index < _symbols.size())
123 return _symbols[index].name;
127 /// getLLVVMModule - Return the Module.
128 llvm::Module *getLLVVMModule() { return _module.get(); }
130 /// getAsmUndefinedRefs -
131 const std::vector<const char*> &getAsmUndefinedRefs() {
132 return _asm_undefines;
135 /// getTargetOptions - Fill the TargetOptions object with the options
136 /// specified on the command line.
137 static void getTargetOptions(llvm::TargetOptions &Options);
140 /// parseSymbols - Parse the symbols from the module and model-level ASM and
141 /// add them to either the defined or undefined lists.
142 bool parseSymbols(std::string &errMsg);
144 /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet
145 /// to a list to be resolved later.
146 void addPotentialUndefinedSymbol(const llvm::GlobalValue *dcl, bool isFunc);
148 /// addDefinedSymbol - Add a defined symbol to the list.
149 void addDefinedSymbol(const llvm::GlobalValue *def, bool isFunction);
151 /// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
152 void addDefinedFunctionSymbol(const llvm::Function *f);
154 /// addDefinedDataSymbol - Add a data symbol as defined to the list.
155 void addDefinedDataSymbol(const llvm::GlobalValue *v);
157 /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
158 /// defined or undefined lists.
159 bool addAsmGlobalSymbols(std::string &errMsg);
161 /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
163 void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
165 /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to
166 /// the undefined list.
167 void addAsmGlobalSymbolUndef(const char *);
169 /// addObjCClass - Parse i386/ppc ObjC class data structure.
170 void addObjCClass(const llvm::GlobalVariable *clgv);
172 /// addObjCCategory - Parse i386/ppc ObjC category data structure.
173 void addObjCCategory(const llvm::GlobalVariable *clgv);
175 /// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
176 void addObjCClassRef(const llvm::GlobalVariable *clgv);
178 /// objcClassNameFromExpression - Get string that the data pointer points
180 bool objcClassNameFromExpression(const llvm::Constant* c, std::string &name);
182 /// isTargetMatch - Returns 'true' if the memory buffer is for the specified
184 static bool isTargetMatch(llvm::MemoryBuffer *memBuffer,
185 const char *triplePrefix);
187 /// makeLTOModule - Create an LTOModule (private version). N.B. This
188 /// method takes ownership of the buffer.
189 static LTOModule *makeLTOModule(llvm::MemoryBuffer *buffer,
190 std::string &errMsg);
192 /// makeBuffer - Create a MemoryBuffer from a memory range.
193 static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length);
196 #endif // LTO_MODULE_H