LTOModule.h revision 30fe94ea43bd122fb4fd26b84bcf62f8096d4293
1//===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file declares the LTOModule class. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LTO_MODULE_H 15#define LTO_MODULE_H 16 17#include "llvm/Module.h" 18#include "llvm/MC/MCContext.h" 19#include "llvm/Target/Mangler.h" 20#include "llvm/Target/TargetMachine.h" 21#include "llvm/ADT/OwningPtr.h" 22#include "llvm/ADT/StringMap.h" 23 24#include "llvm-c/lto.h" 25 26#include <vector> 27#include <string> 28 29 30// forward references to llvm classes 31namespace llvm { 32 class MemoryBuffer; 33 class GlobalValue; 34 class Value; 35 class Function; 36} 37 38// 39// C++ class which implements the opaque lto_module_t 40// 41struct LTOModule { 42 43 static bool isBitcodeFile(const void* mem, size_t length); 44 static bool isBitcodeFile(const char* path); 45 static bool isBitcodeFileForTarget(const void* mem, 46 size_t length, 47 const char* triplePrefix); 48 static bool isBitcodeFileForTarget(const char* path, 49 const char* triplePrefix); 50 static LTOModule* makeLTOModule(const char* path, 51 std::string& errMsg); 52 static LTOModule* makeLTOModule(int fd, const char *path, 53 size_t size, 54 std::string& errMsg); 55 static LTOModule* makeLTOModule(int fd, const char *path, 56 size_t file_size, 57 size_t map_size, 58 off_t offset, 59 std::string& errMsg); 60 static LTOModule* makeLTOModule(const void* mem, size_t length, 61 std::string& errMsg); 62 const char* getTargetTriple(); 63 void setTargetTriple(const char*); 64 uint32_t getSymbolCount(); 65 lto_symbol_attributes getSymbolAttributes(uint32_t index); 66 const char* getSymbolName(uint32_t index); 67 llvm::Module * getLLVVMModule() { return _module.get(); } 68 const std::vector<const char*> &getAsmUndefinedRefs() { 69 return _asm_undefines; 70 } 71 72private: 73 LTOModule(llvm::Module* m, llvm::TargetMachine* t); 74 75 bool ParseSymbols(std::string &errMsg); 76 void addDefinedSymbol(llvm::GlobalValue* def, 77 bool isFunction); 78 void addPotentialUndefinedSymbol(llvm::GlobalValue* decl); 79 void addDefinedFunctionSymbol(llvm::Function* f); 80 void addDefinedDataSymbol(llvm::GlobalValue* v); 81 bool addAsmGlobalSymbols(std::string &errMsg); 82 void addAsmGlobalSymbol(const char *, 83 lto_symbol_attributes scope); 84 void addAsmGlobalSymbolUndef(const char *); 85 void addObjCClass(llvm::GlobalVariable* clgv); 86 void addObjCCategory(llvm::GlobalVariable* clgv); 87 void addObjCClassRef(llvm::GlobalVariable* clgv); 88 bool objcClassNameFromExpression(llvm::Constant* c, 89 std::string& name); 90 91 static bool isTargetMatch(llvm::MemoryBuffer* memBuffer, 92 const char* triplePrefix); 93 static LTOModule* makeLTOModule(llvm::MemoryBuffer* buffer, 94 std::string& errMsg); 95 static llvm::MemoryBuffer* makeBuffer(const void* mem, size_t length); 96 97 typedef llvm::StringMap<uint8_t> StringSet; 98 99 struct NameAndAttributes { 100 const char* name; 101 lto_symbol_attributes attributes; 102 }; 103 104 llvm::OwningPtr<llvm::Module> _module; 105 llvm::OwningPtr<llvm::TargetMachine> _target; 106 std::vector<NameAndAttributes> _symbols; 107 108 // _defines and _undefines only needed to disambiguate tentative definitions 109 StringSet _defines; 110 llvm::StringMap<NameAndAttributes> _undefines; 111 std::vector<const char*> _asm_undefines; 112 llvm::MCContext _context; 113 114 // Use mangler to add GlobalPrefix to names to match linker names. 115 llvm::Mangler _mangler; 116}; 117 118#endif // LTO_MODULE_H 119