diff options
author | Bill Wendling <isanbard@gmail.com> | 2012-03-28 20:46:54 +0000 |
---|---|---|
committer | Bill Wendling <isanbard@gmail.com> | 2012-03-28 20:46:54 +0000 |
commit | 62cf01e4c5de703e7712c72d12f577153b373cba (patch) | |
tree | c4ef419bb83a92bc73ee4d8b6ca66620776b8359 | |
parent | 8d813de71e18dd73ed98e8d58407caba0265e38e (diff) |
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
-rw-r--r-- | tools/lto/LTOModule.cpp | 99 | ||||
-rw-r--r-- | tools/lto/LTOModule.h | 176 |
2 files changed, 170 insertions, 105 deletions
diff --git a/tools/lto/LTOModule.cpp b/tools/lto/LTOModule.cpp index 8ce8cd2f1a..5e51da0c37 100644 --- a/tools/lto/LTOModule.cpp +++ b/tools/lto/LTOModule.cpp @@ -13,7 +13,6 @@ //===----------------------------------------------------------------------===// #include "LTOModule.h" - #include "llvm/Constants.h" #include "llvm/LLVMContext.h" #include "llvm/Module.h" @@ -41,9 +40,15 @@ #include "llvm/MC/MCTargetAsmParser.h" #include "llvm/Target/TargetMachine.h" #include "llvm/Target/TargetRegisterInfo.h" - using namespace llvm; +LTOModule::LTOModule(llvm::Module *m, llvm::TargetMachine *t) + : _module(m), _target(t), + _context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(), NULL), + _mangler(_context, *_target->getTargetData()) {} + +/// isBitcodeFile - Returns 'true' if the file (or memory contents) is LLVM +/// bitcode. bool LTOModule::isBitcodeFile(const void *mem, size_t length) { return llvm::sys::IdentifyFileType((char*)mem, length) == llvm::sys::Bitcode_FileType; @@ -53,6 +58,8 @@ bool LTOModule::isBitcodeFile(const char *path) { return llvm::sys::Path(path).isBitcodeFile(); } +/// isBitcodeFileForTarget - Returns 'true' if the file (or memory contents) is +/// LLVM bitcode for the specified triple. bool LTOModule::isBitcodeFileForTarget(const void *mem, size_t length, const char *triplePrefix) { MemoryBuffer *buffer = makeBuffer(mem, length); @@ -69,23 +76,17 @@ bool LTOModule::isBitcodeFileForTarget(const char *path, return isTargetMatch(buffer.take(), triplePrefix); } -// Takes ownership of buffer. +/// isTargetMatch - Returns 'true' if the memory buffer is for the specified +/// target triple. bool LTOModule::isTargetMatch(MemoryBuffer *buffer, const char *triplePrefix) { std::string Triple = getBitcodeTargetTriple(buffer, getGlobalContext()); delete buffer; return strncmp(Triple.c_str(), triplePrefix, strlen(triplePrefix)) == 0; } - -LTOModule::LTOModule(Module *m, TargetMachine *t) - : _module(m), _target(t), - _context(*_target->getMCAsmInfo(), *_target->getRegisterInfo(), NULL), - _mangler(_context, *_target->getTargetData()) -{ -} - -LTOModule *LTOModule::makeLTOModule(const char *path, - std::string &errMsg) { +/// makeLTOModule - Create an LTOModule. N.B. These methods take ownership of +/// the buffer. +LTOModule *LTOModule::makeLTOModule(const char *path, std::string &errMsg) { OwningPtr<MemoryBuffer> buffer; if (error_code ec = MemoryBuffer::getFile(path, buffer)) { errMsg = ec.message(); @@ -113,12 +114,6 @@ LTOModule *LTOModule::makeLTOModule(int fd, const char *path, return makeLTOModule(buffer.take(), errMsg); } -/// makeBuffer - Create a MemoryBuffer from a memory range. -MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) { - const char *startPtr = (char*)mem; - return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false); -} - LTOModule *LTOModule::makeLTOModule(const void *mem, size_t length, std::string &errMsg) { OwningPtr<MemoryBuffer> buffer(makeBuffer(mem, length)); @@ -171,20 +166,13 @@ LTOModule *LTOModule::makeLTOModule(MemoryBuffer *buffer, return Ret; } -const char *LTOModule::getTargetTriple() { - return _module->getTargetTriple().c_str(); -} - -void LTOModule::setTargetTriple(const char *triple) { - _module->setTargetTriple(triple); -} - -void LTOModule::addDefinedFunctionSymbol(Function *f) { - // add to list of defined symbols - addDefinedSymbol(f, true); +/// makeBuffer - Create a MemoryBuffer from a memory range. +MemoryBuffer *LTOModule::makeBuffer(const void *mem, size_t length) { + const char *startPtr = (char*)mem; + return MemoryBuffer::getMemBuffer(StringRef(startPtr, length), "", false); } -// Get string that data pointer points to. +/// objcClassNameFromExpression - Get string that the data pointer points to. bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) { if (ConstantExpr *ce = dyn_cast<ConstantExpr>(c)) { Constant *op = ce->getOperand(0); @@ -201,7 +189,7 @@ bool LTOModule::objcClassNameFromExpression(Constant *c, std::string &name) { return false; } -// Parse i386/ppc ObjC class data structure. +/// addObjCClass - Parse i386/ppc ObjC class data structure. void LTOModule::addObjCClass(GlobalVariable *clgv) { ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer()); if (!c) return; @@ -234,8 +222,7 @@ void LTOModule::addObjCClass(GlobalVariable *clgv) { } } - -// Parse i386/ppc ObjC category data structure. +/// addObjCCategory - Parse i386/ppc ObjC category data structure. void LTOModule::addObjCCategory(GlobalVariable *clgv) { ConstantStruct *c = dyn_cast<ConstantStruct>(clgv->getInitializer()); if (!c) return; @@ -258,8 +245,7 @@ void LTOModule::addObjCCategory(GlobalVariable *clgv) { entry.setValue(info); } - -// Parse i386/ppc ObjC class list data structure. +/// addObjCClassRef - Parse i386/ppc ObjC class list data structure. void LTOModule::addObjCClassRef(GlobalVariable *clgv) { std::string targetclassName; if (!objcClassNameFromExpression(clgv->getInitializer(), targetclassName)) @@ -277,7 +263,7 @@ void LTOModule::addObjCClassRef(GlobalVariable *clgv) { entry.setValue(info); } - +/// addDefinedDataSymbol - Add a data symbol as defined to the list. void LTOModule::addDefinedDataSymbol(GlobalValue *v) { // Add to list of defined symbols. addDefinedSymbol(v, false); @@ -325,6 +311,13 @@ void LTOModule::addDefinedDataSymbol(GlobalValue *v) { } } +/// addDefinedFunctionSymbol - Add a function symbol as defined to the list. +void LTOModule::addDefinedFunctionSymbol(Function *f) { + // add to list of defined symbols + addDefinedSymbol(f, true); +} + +/// addDefinedSymbol - Add a defined symbol to the list. void LTOModule::addDefinedSymbol(GlobalValue *def, bool isFunction) { // ignore all llvm.* symbols if (def->getName().startswith("llvm.")) @@ -385,6 +378,8 @@ void LTOModule::addDefinedSymbol(GlobalValue *def, bool isFunction) { _symbols.push_back(info); } +/// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the +/// defined list. void LTOModule::addAsmGlobalSymbol(const char *name, lto_symbol_attributes scope) { StringSet::value_type &entry = _defines.GetOrCreateValue(name); @@ -403,6 +398,8 @@ void LTOModule::addAsmGlobalSymbol(const char *name, _symbols.push_back(info); } +/// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to the +/// undefined list. void LTOModule::addAsmGlobalSymbolUndef(const char *name) { StringMap<NameAndAttributes>::value_type &entry = _undefines.GetOrCreateValue(name); @@ -422,6 +419,8 @@ void LTOModule::addAsmGlobalSymbolUndef(const char *name) { entry.setValue(info); } +/// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet to a +/// list to be resolved later. void LTOModule::addPotentialUndefinedSymbol(GlobalValue *decl) { // ignore all llvm.* symbols if (decl->getName().startswith("llvm.")) @@ -444,6 +443,7 @@ void LTOModule::addPotentialUndefinedSymbol(GlobalValue *decl) { NameAndAttributes info; info.name = entry.getKey().data(); + if (decl->hasExternalWeakLinkage()) info.attributes = LTO_SYMBOL_DEFINITION_WEAKUNDEF; else @@ -607,6 +607,8 @@ namespace { }; } +/// addAsmGlobalSymbols - Add global symbols from module-level ASM to the +/// defined or undefined lists. bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) { const std::string &inlineAsm = _module->getModuleInlineAsm(); if (inlineAsm.empty()) @@ -651,6 +653,7 @@ bool LTOModule::addAsmGlobalSymbols(std::string &errMsg) { return false; } +/// isDeclaration - Return 'true' if the global value is a declaration. static bool isDeclaration(const GlobalValue &V) { if (V.hasAvailableExternallyLinkage()) return true; @@ -659,10 +662,14 @@ static bool isDeclaration(const GlobalValue &V) { return V.isDeclaration(); } +/// isAliasToDeclaration - Return 'true' if the global value is an alias to a +/// declaration. static bool isAliasToDeclaration(const GlobalAlias &V) { return isDeclaration(*V.getAliasedGlobal()); } +/// ParseSymbols - Parse the symbols from the module and model-level ASM and add +/// them to either the defined or undefined lists. bool LTOModule::ParseSymbols(std::string &errMsg) { // add functions for (Module::iterator f = _module->begin(); f != _module->end(); ++f) { @@ -706,21 +713,3 @@ bool LTOModule::ParseSymbols(std::string &errMsg) { } return false; } - -uint32_t LTOModule::getSymbolCount() { - return _symbols.size(); -} - -lto_symbol_attributes LTOModule::getSymbolAttributes(uint32_t index) { - if (index < _symbols.size()) - return _symbols[index].attributes; - else - return lto_symbol_attributes(0); -} - -const char *LTOModule::getSymbolName(uint32_t index) { - if (index < _symbols.size()) - return _symbols[index].name; - else - return NULL; -} diff --git a/tools/lto/LTOModule.h b/tools/lto/LTOModule.h index b7f2d5ede6..22f2c5a1ca 100644 --- a/tools/lto/LTOModule.h +++ b/tools/lto/LTOModule.h @@ -29,90 +29,166 @@ // forward references to llvm classes namespace llvm { - class MemoryBuffer; + class Function; class GlobalValue; + class MemoryBuffer; class Value; - class Function; } // // C++ class which implements the opaque lto_module_t // struct LTOModule { +private: + typedef llvm::StringMap<uint8_t> StringSet; + + struct NameAndAttributes { + enum name_type { IsFunction, IsData }; + const char* name; + lto_symbol_attributes attributes; + }; + + llvm::OwningPtr<llvm::Module> _module; + llvm::OwningPtr<llvm::TargetMachine> _target; + std::vector<NameAndAttributes> _symbols; + + // _defines and _undefines only needed to disambiguate tentative definitions + StringSet _defines; + llvm::StringMap<NameAndAttributes> _undefines; + std::vector<const char*> _asm_undefines; + llvm::MCContext _context; + + // Use mangler to add GlobalPrefix to names to match linker names. + llvm::Mangler _mangler; - static bool isBitcodeFile(const void* mem, size_t length); - static bool isBitcodeFile(const char* path); - static bool isBitcodeFileForTarget(const void* mem, + LTOModule(llvm::Module *m, llvm::TargetMachine *t); +public: + /// isBitcodeFile - Returns 'true' if the file or memory contents is LLVM + /// bitcode. + static bool isBitcodeFile(const void *mem, size_t length); + static bool isBitcodeFile(const char *path); + + /// isBitcodeFileForTarget - Returns 'true' if the file or memory contents + /// is LLVM bitcode for the specified triple. + static bool isBitcodeFileForTarget(const void *mem, size_t length, - const char* triplePrefix); - static bool isBitcodeFileForTarget(const char* path, - const char* triplePrefix); + const char *triplePrefix); + static bool isBitcodeFileForTarget(const char *path, + const char *triplePrefix); + + /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership + /// of the buffer. static LTOModule* makeLTOModule(const char* path, - std::string& errMsg); + std::string &errMsg); static LTOModule* makeLTOModule(int fd, const char *path, size_t size, - std::string& errMsg); + std::string &errMsg); static LTOModule* makeLTOModule(int fd, const char *path, size_t file_size, size_t map_size, off_t offset, std::string& errMsg); - static LTOModule* makeLTOModule(const void* mem, size_t length, - std::string& errMsg); - const char* getTargetTriple(); - void setTargetTriple(const char*); - uint32_t getSymbolCount(); - lto_symbol_attributes getSymbolAttributes(uint32_t index); - const char* getSymbolName(uint32_t index); + static LTOModule* makeLTOModule(const void *mem, size_t length, + std::string &errMsg); + + /// getTargetTriple - Return the Module's target triple. + const char* getTargetTriple() { + return _module->getTargetTriple().c_str(); + } + + /// setTargetTriple - Set the Module's target triple. + void setTargetTriple(const char *triple) { + _module->setTargetTriple(triple); + } + + /// getSymbolCount - Get the number of symbols + uint32_t getSymbolCount() { + return _symbols.size(); + } + + /// getSymbolAttributes - Get the attributes for a symbol at the specified + /// index. + lto_symbol_attributes getSymbolAttributes(uint32_t index) { + if (index < _symbols.size()) + return _symbols[index].attributes; + else + return lto_symbol_attributes(0); + } + + /// getSymbolName - Get the name of the symbol at the specified index. + const char* getSymbolName(uint32_t index) { + if (index < _symbols.size()) + return _symbols[index].name; + else + return NULL; + } + + /// getLLVVMModule - Return the Module. llvm::Module * getLLVVMModule() { return _module.get(); } + + /// getAsmUndefinedRefs - const std::vector<const char*> &getAsmUndefinedRefs() { return _asm_undefines; } private: - LTOModule(llvm::Module* m, llvm::TargetMachine* t); - + /// ParseSymbols - Parse the symbols from the module and model-level ASM and + /// add them to either the defined or undefined lists. bool ParseSymbols(std::string &errMsg); - void addDefinedSymbol(llvm::GlobalValue* def, + + /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet + /// to a list to be resolved later. + void addPotentialUndefinedSymbol(llvm::GlobalValue *dcl); + + /// addDefinedSymbol - Add a defined symbol to the list. + void addDefinedSymbol(llvm::GlobalValue *def, bool isFunction); - void addPotentialUndefinedSymbol(llvm::GlobalValue* decl); - void addDefinedFunctionSymbol(llvm::Function* f); - void addDefinedDataSymbol(llvm::GlobalValue* v); + + /// addDefinedFunctionSymbol - Add a function symbol as defined to the list. + void addDefinedFunctionSymbol(llvm::Function *f); + + /// addDefinedDataSymbol - Add a data symbol as defined to the list. + void addDefinedDataSymbol(llvm::GlobalValue *v); + + /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the + /// defined or undefined lists. bool addAsmGlobalSymbols(std::string &errMsg); + + /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the + /// defined list. void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope); + + /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to + /// the undefined list. void addAsmGlobalSymbolUndef(const char *); - void addObjCClass(llvm::GlobalVariable* clgv); - void addObjCCategory(llvm::GlobalVariable* clgv); - void addObjCClassRef(llvm::GlobalVariable* clgv); - bool objcClassNameFromExpression(llvm::Constant* c, - std::string& name); - - static bool isTargetMatch(llvm::MemoryBuffer* memBuffer, - const char* triplePrefix); - static LTOModule* makeLTOModule(llvm::MemoryBuffer* buffer, - std::string& errMsg); - static llvm::MemoryBuffer* makeBuffer(const void* mem, size_t length); - typedef llvm::StringMap<uint8_t> StringSet; - - struct NameAndAttributes { - const char* name; - lto_symbol_attributes attributes; - }; + /// addObjCClass - Parse i386/ppc ObjC class data structure. + void addObjCClass(llvm::GlobalVariable *clgv); - llvm::OwningPtr<llvm::Module> _module; - llvm::OwningPtr<llvm::TargetMachine> _target; - std::vector<NameAndAttributes> _symbols; + /// addObjCCategory - Parse i386/ppc ObjC category data structure. + void addObjCCategory(llvm::GlobalVariable *clgv); - // _defines and _undefines only needed to disambiguate tentative definitions - StringSet _defines; - llvm::StringMap<NameAndAttributes> _undefines; - std::vector<const char*> _asm_undefines; - llvm::MCContext _context; + /// addObjCClassRef - Parse i386/ppc ObjC class list data structure. + void addObjCClassRef(llvm::GlobalVariable *clgv); - // Use mangler to add GlobalPrefix to names to match linker names. - llvm::Mangler _mangler; + /// objcClassNameFromExpression - Get string that the data pointer points + /// to. + bool objcClassNameFromExpression(llvm::Constant* c, + std::string &name); + + /// isTargetMatch - Returns 'true' if the memory buffer is for the specified + /// target triple. + static bool isTargetMatch(llvm::MemoryBuffer *memBuffer, + const char *triplePrefix); + + /// makeLTOModule - Create an LTOModule (private version). N.B. This + /// method takes ownership of the buffer. + static LTOModule* makeLTOModule(llvm::MemoryBuffer *buffer, + std::string &errMsg); + + /// makeBuffer - Create a MemoryBuffer from a memory range. + static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length); }; #endif // LTO_MODULE_H |