llvm.org GIT mirror llvm / 62cf01e
Reformat the LTOModule code to be more inline with LLVM's coding standards. Add a bunch of comments for the various functions. No intended functionality change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@153595 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 7 years ago
2 changed file(s) with 180 addition(s) and 115 deletion(s). Raw diff Collapse all Expand all
1212 //===----------------------------------------------------------------------===//
1313
1414 #include "LTOModule.h"
15
1615 #include "llvm/Constants.h"
1716 #include "llvm/LLVMContext.h"
1817 #include "llvm/Module.h"
4039 #include "llvm/MC/MCTargetAsmParser.h"
4140 #include "llvm/Target/TargetMachine.h"
4241 #include "llvm/Target/TargetRegisterInfo.h"
43
4442 using namespace llvm;
4543
44 LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t)
45 : _module(m), _target(t),
46 _context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(), NULL),
47 _mangler(_context, *_target->getTargetData()) {}
48
49 /// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM
50 /// bitcode.
4651 bool LTOModule::isBitcodeFile(const void *mem, size_t length) {
4752 return llvm::sys::IdentifyFileType((char*)mem, length)
4853 == llvm::sys::Bitcode_FileType;
5257 return llvm::sys::Path(path).isBitcodeFile();
5358 }
5459
60 /// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is
61 /// LLVM bitcode for the specified triple.
5562 bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length,
5663 const char *triplePrefix) {
5764 MemoryBuffer *buffer = makeBuffer(mem, length);
6875 return isTargetMatch(buffer.take(), triplePrefix);
6976 }
7077
71 // Takes ownership of buffer.
78 /// isTargetMatch - Returns 'true' if the memory buffer is for the specified
79 /// target triple.
7280 bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) {
7381 std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext());
7482 delete buffer;
7583 return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0;
7684 }
7785
78
79 LTOModule::LTOModule(Module *m, TargetMachine *t)
80 : _module(m), _target(t),
81 _context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(), NULL),
82 _mangler(_context, *_target->getTargetData())
83 {
84 }
85
86 LTOModule *LTOModule::makeLTOModule(const char *path,
87 std::string &errMsg) {
86 /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of
87 /// the buffer.
88 LTOModule *LTOModule::makeLTOModule(const char *path, std::string &errMsg) {
8889 OwningPtr buffer;
8990 if (error_code ec = MemoryBuffer::getFile(path, buffer)) {
9091 errMsg = ec.message();
110111 return NULL;
111112 }
112113 return makeLTOModule(buffer.take(), errMsg);
113 }
114
115 /// makeBuffer - Create a MemoryBuffer from a memory range.
116 MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
117 const char *startPtr = (char*)mem;
118 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
119114 }
120115
121116 LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length,
170165 return Ret;
171166 }
172167
173 const char *LTOModule::getTargetTriple() {
174 return _module->getTargetTriple().c_str();
175 }
176
177 void LTOModule::setTargetTriple(const char *triple) {
178 _module->setTargetTriple(triple);
179 }
180
181 void LTOModule::addDefinedFunctionSymbol(Function *f) {
182 // add to list of defined symbols
183 addDefinedSymbol(f, true);
184 }
185
186 // Get string that data pointer points to.
168 /// makeBuffer - Create a MemoryBuffer from a memory range.
169 MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) {
170 const char *startPtr = (char*)mem;
171 return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false);
172 }
173
174 /// objcClassNameFromExpression - Get string that the data pointer points to.
187175 bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) {
188176 if (ConstantExpr *ce = dyn_cast(c)) {
189177 Constant *op = ce->getOperand(0);
200188 return false;
201189 }
202190
203 // Parse i386/ppc ObjC class data structure.
191 /// addObjCClass - Parse i386/ppc ObjC class data structure.
204192 void LTOModule::addObjCClass(GlobalVariable *clgv) {
205193 ConstantStruct *c = dyn_cast(clgv->getInitializer());
206194 if (!c) return;
233221 }
234222 }
235223
236
237 // Parse i386/ppc ObjC category data structure.
224 /// addObjCCategory - Parse i386/ppc ObjC category data structure.
238225 void LTOModule::addObjCCategory(GlobalVariable *clgv) {
239226 ConstantStruct *c = dyn_cast(clgv->getInitializer());
240227 if (!c) return;
257244 entry.setValue(info);
258245 }
259246
260
261 // Parse i386/ppc ObjC class list data structure.
247 /// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
262248 void LTOModule::addObjCClassRef(GlobalVariable *clgv) {
263249 std::string targetclassName;
264250 if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName))
276262 entry.setValue(info);
277263 }
278264
279
265 /// addDefinedDataSymbol - Add a data symbol as defined to the list.
280266 void LTOModule::addDefinedDataSymbol(GlobalValue *v) {
281267 // Add to list of defined symbols.
282268 addDefinedSymbol(v, false);
324310 }
325311 }
326312
313 /// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
314 void LTOModule::addDefinedFunctionSymbol(Function *f) {
315 // add to list of defined symbols
316 addDefinedSymbol(f, true);
317 }
318
319 /// addDefinedSymbol - Add a defined symbol to the list.
327320 void LTOModule::addDefinedSymbol(GlobalValue *def, bool isFunction) {
328321 // ignore all llvm.* symbols
329322 if (def->getName().startswith("llvm."))
384377 _symbols.push_back(info);
385378 }
386379
380 /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
381 /// defined list.
387382 void LTOModule::addAsmGlobalSymbol(const char *name,
388383 lto_symbol_attributes scope) {
389384 StringSet::value_type &entry = _defines.GetOrCreateValue(name);
402397 _symbols.push_back(info);
403398 }
404399
400 /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the
401 /// undefined list.
405402 void LTOModule::addAsmGlobalSymbolUndef(const char *name) {
406403 StringMap::value_type &entry =
407404 _undefines.GetOrCreateValue(name);
421418 entry.setValue(info);
422419 }
423420
421 /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a
422 /// list to be resolved later.
424423 void LTOModule::addPotentialUndefinedSymbol(GlobalValue *decl) {
425424 // ignore all llvm.* symbols
426425 if (decl->getName().startswith("llvm."))
443442 NameAndAttributes info;
444443
445444 info.name = entry.getKey().data();
445
446446 if (decl->hasExternalWeakLinkage())
447447 info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF;
448448 else
606606 };
607607 }
608608
609 /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
610 /// defined or undefined lists.
609611 bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) {
610612 const std::string &inlineAsm = _module->getModuleInlineAsm();
611613 if (inlineAsm.empty())
650652 return false;
651653 }
652654
655 /// isDeclaration - Return 'true' if the global value is a declaration.
653656 static bool isDeclaration(const GlobalValue &V) {
654657 if (V.hasAvailableExternallyLinkage())
655658 return true;
658661 return V.isDeclaration();
659662 }
660663
664 /// isAliasToDeclaration - Return 'true' if the global value is an alias to a
665 /// declaration.
661666 static bool isAliasToDeclaration(const GlobalAlias &V) {
662667 return isDeclaration(*V.getAliasedGlobal());
663668 }
664669
670 /// ParseSymbols - Parse the symbols from the module and model-level ASM and add
671 /// them to either the defined or undefined lists.
665672 bool LTOModule::ParseSymbols(std::string &errMsg) {
666673 // add functions
667674 for (Module::iterator f = _module->begin(); f != _module->end(); ++f) {
705712 }
706713 return false;
707714 }
708
709 uint32_t LTOModule::getSymbolCount() {
710 return _symbols.size();
711 }
712
713 lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index) {
714 if (index < _symbols.size())
715 return _symbols[index].attributes;
716 else
717 return lto_symbol_attributes(0);
718 }
719
720 const char *LTOModule::getSymbolName(uint32_t index) {
721 if (index < _symbols.size())
722 return _symbols[index].name;
723 else
724 return NULL;
725 }
2828
2929 // forward references to llvm classes
3030 namespace llvm {
31 class Function;
32 class GlobalValue;
3133 class MemoryBuffer;
32 class GlobalValue;
3334 class Value;
34 class Function;
3535 }
3636
3737 //
3838 // C++ class which implements the opaque lto_module_t
3939 //
4040 struct LTOModule {
41
42 static bool isBitcodeFile(const void* mem, size_t length);
43 static bool isBitcodeFile(const char* path);
44 static bool isBitcodeFileForTarget(const void* mem,
45 size_t length,
46 const char* triplePrefix);
47 static bool isBitcodeFileForTarget(const char* path,
48 const char* triplePrefix);
49 static LTOModule* makeLTOModule(const char* path,
50 std::string& errMsg);
51 static LTOModule* makeLTOModule(int fd, const char *path,
52 size_t size,
53 std::string& errMsg);
54 static LTOModule* makeLTOModule(int fd, const char *path,
55 size_t file_size,
56 size_t map_size,
57 off_t offset,
58 std::string& errMsg);
59 static LTOModule* makeLTOModule(const void* mem, size_t length,
60 std::string& errMsg);
61 const char* getTargetTriple();
62 void setTargetTriple(const char*);
63 uint32_t getSymbolCount();
64 lto_symbol_attributes getSymbolAttributes(uint32_t index);
65 const char* getSymbolName(uint32_t index);
66 llvm::Module * getLLVVMModule() { return _module.get(); }
67 const std::vector &getAsmUndefinedRefs() {
68 return _asm_undefines;
69 }
70
7141 private:
72 LTOModule(llvm::Module* m, llvm::TargetMachine* t);
73
74 bool ParseSymbols(std::string &errMsg);
75 void addDefinedSymbol(llvm::GlobalValue* def,
76 bool isFunction);
77 void addPotentialUndefinedSymbol(llvm::GlobalValue* decl);
78 void addDefinedFunctionSymbol(llvm::Function* f);
79 void addDefinedDataSymbol(llvm::GlobalValue* v);
80 bool addAsmGlobalSymbols(std::string &errMsg);
81 void addAsmGlobalSymbol(const char *,
82 lto_symbol_attributes scope);
83 void addAsmGlobalSymbolUndef(const char *);
84 void addObjCClass(llvm::GlobalVariable* clgv);
85 void addObjCCategory(llvm::GlobalVariable* clgv);
86 void addObjCClassRef(llvm::GlobalVariable* clgv);
87 bool objcClassNameFromExpression(llvm::Constant* c,
88 std::string& name);
89
90 static bool isTargetMatch(llvm::MemoryBuffer* memBuffer,
91 const char* triplePrefix);
92 static LTOModule* makeLTOModule(llvm::MemoryBuffer* buffer,
93 std::string& errMsg);
94 static llvm::MemoryBuffer* makeBuffer(const void* mem, size_t length);
95
9642 typedef llvm::StringMap StringSet;
9743
9844 struct NameAndAttributes {
99 const char* name;
100 lto_symbol_attributes attributes;
45 enum name_type { IsFunction, IsData };
46 const char* name;
47 lto_symbol_attributes attributes;
10148 };
10249
10350 llvm::OwningPtr _module;
11259
11360 // Use mangler to add GlobalPrefix to names to match linker names.
11461 llvm::Mangler _mangler;
62
63 LTOModule(llvm::Module *m, llvm::TargetMachine *t);
64 public:
65 /// isBitcodeFile - Returns 'true' if the file or memory contents is LLVM
66 /// bitcode.
67 static bool isBitcodeFile(const void *mem, size_t length);
68 static bool isBitcodeFile(const char *path);
69
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,
73 size_t length,
74 const char *triplePrefix);
75 static bool isBitcodeFileForTarget(const char *path,
76 const char *triplePrefix);
77
78 /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership
79 /// of the buffer.
80 static LTOModule* makeLTOModule(const char* path,
81 std::string &errMsg);
82 static LTOModule* makeLTOModule(int fd, const char *path,
83 size_t size,
84 std::string &errMsg);
85 static LTOModule* makeLTOModule(int fd, const char *path,
86 size_t file_size,
87 size_t map_size,
88 off_t offset,
89 std::string& errMsg);
90 static LTOModule* makeLTOModule(const void *mem, size_t length,
91 std::string &errMsg);
92
93 /// getTargetTriple - Return the Module's target triple.
94 const char* getTargetTriple() {
95 return _module->getTargetTriple().c_str();
96 }
97
98 /// setTargetTriple - Set the Module's target triple.
99 void setTargetTriple(const char *triple) {
100 _module->setTargetTriple(triple);
101 }
102
103 /// getSymbolCount - Get the number of symbols
104 uint32_t getSymbolCount() {
105 return _symbols.size();
106 }
107
108 /// getSymbolAttributes - Get the attributes for a symbol at the specified
109 /// index.
110 lto_symbol_attributes getSymbolAttributes(uint32_t index) {
111 if (index < _symbols.size())
112 return _symbols[index].attributes;
113 else
114 return lto_symbol_attributes(0);
115 }
116
117 /// getSymbolName - Get the name of the symbol at the specified index.
118 const char* getSymbolName(uint32_t index) {
119 if (index < _symbols.size())
120 return _symbols[index].name;
121 else
122 return NULL;
123 }
124
125 /// getLLVVMModule - Return the Module.
126 llvm::Module * getLLVVMModule() { return _module.get(); }
127
128 /// getAsmUndefinedRefs -
129 const std::vector &getAsmUndefinedRefs() {
130 return _asm_undefines;
131 }
132
133 private:
134 /// ParseSymbols - Parse the symbols from the module and model-level ASM and
135 /// add them to either the defined or undefined lists.
136 bool ParseSymbols(std::string &errMsg);
137
138 /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet
139 /// to a list to be resolved later.
140 void addPotentialUndefinedSymbol(llvm::GlobalValue *dcl);
141
142 /// addDefinedSymbol - Add a defined symbol to the list.
143 void addDefinedSymbol(llvm::GlobalValue *def,
144 bool isFunction);
145
146 /// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
147 void addDefinedFunctionSymbol(llvm::Function *f);
148
149 /// addDefinedDataSymbol - Add a data symbol as defined to the list.
150 void addDefinedDataSymbol(llvm::GlobalValue *v);
151
152 /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
153 /// defined or undefined lists.
154 bool addAsmGlobalSymbols(std::string &errMsg);
155
156 /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
157 /// defined list.
158 void addAsmGlobalSymbol(const char *,
159 lto_symbol_attributes scope);
160
161 /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to
162 /// the undefined list.
163 void addAsmGlobalSymbolUndef(const char *);
164
165 /// addObjCClass - Parse i386/ppc ObjC class data structure.
166 void addObjCClass(llvm::GlobalVariable *clgv);
167
168 /// addObjCCategory - Parse i386/ppc ObjC category data structure.
169 void addObjCCategory(llvm::GlobalVariable *clgv);
170
171 /// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
172 void addObjCClassRef(llvm::GlobalVariable *clgv);
173
174 /// objcClassNameFromExpression - Get string that the data pointer points
175 /// to.
176 bool objcClassNameFromExpression(llvm::Constant* c,
177 std::string &name);
178
179 /// isTargetMatch - Returns 'true' if the memory buffer is for the specified
180 /// target triple.
181 static bool isTargetMatch(llvm::MemoryBuffer *memBuffer,
182 const char *triplePrefix);
183
184 /// makeLTOModule - Create an LTOModule (private version). N.B. This
185 /// method takes ownership of the buffer.
186 static LTOModule* makeLTOModule(llvm::MemoryBuffer *buffer,
187 std::string &errMsg);
188
189 /// makeBuffer - Create a MemoryBuffer from a memory range.
190 static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length);
115191 };
116192
117193 #endif // LTO_MODULE_H