1259698Sdim//===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===// 2259698Sdim// 3259698Sdim// The LLVM Compiler Infrastructure 4259698Sdim// 5259698Sdim// This file is distributed under the University of Illinois Open Source 6259698Sdim// License. See LICENSE.TXT for details. 7259698Sdim// 8259698Sdim//===----------------------------------------------------------------------===// 9259698Sdim// 10259698Sdim// This file declares the LTOModule class. 11259698Sdim// 12259698Sdim//===----------------------------------------------------------------------===// 13259698Sdim 14259698Sdim#ifndef LTO_MODULE_H 15259698Sdim#define LTO_MODULE_H 16259698Sdim 17259698Sdim#include "llvm-c/lto.h" 18259698Sdim#include "llvm/ADT/OwningPtr.h" 19259698Sdim#include "llvm/ADT/StringMap.h" 20259698Sdim#include "llvm/IR/Module.h" 21259698Sdim#include "llvm/MC/MCContext.h" 22259698Sdim#include "llvm/Target/Mangler.h" 23259698Sdim#include "llvm/Target/TargetMachine.h" 24259698Sdim#include <string> 25259698Sdim#include <vector> 26259698Sdim 27259698Sdim// Forward references to llvm classes. 28259698Sdimnamespace llvm { 29259698Sdim class Function; 30259698Sdim class GlobalValue; 31259698Sdim class MemoryBuffer; 32259698Sdim class TargetOptions; 33259698Sdim class Value; 34259698Sdim} 35259698Sdim 36259698Sdim//===----------------------------------------------------------------------===// 37259698Sdim/// LTOModule - C++ class which implements the opaque lto_module_t type. 38259698Sdim/// 39259698Sdimstruct LTOModule { 40259698Sdimprivate: 41259698Sdim typedef llvm::StringMap<uint8_t> StringSet; 42259698Sdim 43259698Sdim struct NameAndAttributes { 44259698Sdim const char *name; 45259698Sdim uint32_t attributes; 46259698Sdim bool isFunction; 47259698Sdim const llvm::GlobalValue *symbol; 48259698Sdim }; 49259698Sdim 50259698Sdim llvm::OwningPtr<llvm::Module> _module; 51259698Sdim llvm::OwningPtr<llvm::TargetMachine> _target; 52259698Sdim std::vector<NameAndAttributes> _symbols; 53259698Sdim 54259698Sdim // _defines and _undefines only needed to disambiguate tentative definitions 55259698Sdim StringSet _defines; 56259698Sdim llvm::StringMap<NameAndAttributes> _undefines; 57259698Sdim std::vector<const char*> _asm_undefines; 58259698Sdim llvm::MCContext _context; 59259698Sdim 60259698Sdim // Use mangler to add GlobalPrefix to names to match linker names. 61259698Sdim llvm::Mangler _mangler; 62259698Sdim 63259698Sdim LTOModule(llvm::Module *m, llvm::TargetMachine *t); 64259698Sdimpublic: 65259698Sdim /// isBitcodeFile - Returns 'true' if the file or memory contents is LLVM 66259698Sdim /// bitcode. 67259698Sdim static bool isBitcodeFile(const void *mem, size_t length); 68259698Sdim static bool isBitcodeFile(const char *path); 69259698Sdim 70259698Sdim /// isBitcodeFileForTarget - Returns 'true' if the file or memory contents 71259698Sdim /// is LLVM bitcode for the specified triple. 72259698Sdim static bool isBitcodeFileForTarget(const void *mem, 73259698Sdim size_t length, 74259698Sdim const char *triplePrefix); 75259698Sdim static bool isBitcodeFileForTarget(const char *path, 76259698Sdim const char *triplePrefix); 77259698Sdim 78259698Sdim /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership 79259698Sdim /// of the buffer. The caller must have initialized the Targets, the 80259698Sdim /// TargetMCs, the AsmPrinters, and the AsmParsers by calling: 81259698Sdim /// 82259698Sdim /// InitializeAllTargets(); 83259698Sdim /// InitializeAllTargetMCs(); 84259698Sdim /// InitializeAllAsmPrinters(); 85259698Sdim /// InitializeAllAsmParsers(); 86259698Sdim static LTOModule *makeLTOModule(const char* path, 87259698Sdim llvm::TargetOptions options, 88259698Sdim std::string &errMsg); 89259698Sdim static LTOModule *makeLTOModule(int fd, const char *path, 90259698Sdim size_t size, llvm::TargetOptions options, 91259698Sdim std::string &errMsg); 92259698Sdim static LTOModule *makeLTOModule(int fd, const char *path, 93259698Sdim size_t map_size, 94259698Sdim off_t offset, llvm::TargetOptions options, 95259698Sdim std::string& errMsg); 96259698Sdim static LTOModule *makeLTOModule(const void *mem, size_t length, 97259698Sdim llvm::TargetOptions options, 98259698Sdim std::string &errMsg); 99259698Sdim 100259698Sdim /// getTargetTriple - Return the Module's target triple. 101259698Sdim const char *getTargetTriple() { 102259698Sdim return _module->getTargetTriple().c_str(); 103259698Sdim } 104259698Sdim 105259698Sdim /// setTargetTriple - Set the Module's target triple. 106259698Sdim void setTargetTriple(const char *triple) { 107259698Sdim _module->setTargetTriple(triple); 108259698Sdim } 109259698Sdim 110259698Sdim /// getSymbolCount - Get the number of symbols 111259698Sdim uint32_t getSymbolCount() { 112259698Sdim return _symbols.size(); 113259698Sdim } 114259698Sdim 115259698Sdim /// getSymbolAttributes - Get the attributes for a symbol at the specified 116259698Sdim /// index. 117259698Sdim lto_symbol_attributes getSymbolAttributes(uint32_t index) { 118259698Sdim if (index < _symbols.size()) 119259698Sdim return lto_symbol_attributes(_symbols[index].attributes); 120259698Sdim return lto_symbol_attributes(0); 121259698Sdim } 122259698Sdim 123259698Sdim /// getSymbolName - Get the name of the symbol at the specified index. 124259698Sdim const char *getSymbolName(uint32_t index) { 125259698Sdim if (index < _symbols.size()) 126259698Sdim return _symbols[index].name; 127259698Sdim return NULL; 128259698Sdim } 129259698Sdim 130259698Sdim /// getLLVVMModule - Return the Module. 131259698Sdim llvm::Module *getLLVVMModule() { return _module.get(); } 132259698Sdim 133259698Sdim /// getAsmUndefinedRefs - 134259698Sdim const std::vector<const char*> &getAsmUndefinedRefs() { 135259698Sdim return _asm_undefines; 136259698Sdim } 137259698Sdim 138259698Sdimprivate: 139259698Sdim /// parseSymbols - Parse the symbols from the module and model-level ASM and 140259698Sdim /// add them to either the defined or undefined lists. 141259698Sdim bool parseSymbols(std::string &errMsg); 142259698Sdim 143259698Sdim /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet 144259698Sdim /// to a list to be resolved later. 145259698Sdim void addPotentialUndefinedSymbol(const llvm::GlobalValue *dcl, bool isFunc); 146259698Sdim 147259698Sdim /// addDefinedSymbol - Add a defined symbol to the list. 148259698Sdim void addDefinedSymbol(const llvm::GlobalValue *def, bool isFunction); 149259698Sdim 150259698Sdim /// addDefinedFunctionSymbol - Add a function symbol as defined to the list. 151259698Sdim void addDefinedFunctionSymbol(const llvm::Function *f); 152259698Sdim 153259698Sdim /// addDefinedDataSymbol - Add a data symbol as defined to the list. 154259698Sdim void addDefinedDataSymbol(const llvm::GlobalValue *v); 155259698Sdim 156259698Sdim /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the 157259698Sdim /// defined or undefined lists. 158259698Sdim bool addAsmGlobalSymbols(std::string &errMsg); 159259698Sdim 160259698Sdim /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the 161259698Sdim /// defined list. 162259698Sdim void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope); 163259698Sdim 164259698Sdim /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to 165259698Sdim /// the undefined list. 166259698Sdim void addAsmGlobalSymbolUndef(const char *); 167259698Sdim 168259698Sdim /// addObjCClass - Parse i386/ppc ObjC class data structure. 169259698Sdim void addObjCClass(const llvm::GlobalVariable *clgv); 170259698Sdim 171259698Sdim /// addObjCCategory - Parse i386/ppc ObjC category data structure. 172259698Sdim void addObjCCategory(const llvm::GlobalVariable *clgv); 173259698Sdim 174259698Sdim /// addObjCClassRef - Parse i386/ppc ObjC class list data structure. 175259698Sdim void addObjCClassRef(const llvm::GlobalVariable *clgv); 176259698Sdim 177259698Sdim /// objcClassNameFromExpression - Get string that the data pointer points 178259698Sdim /// to. 179259698Sdim bool objcClassNameFromExpression(const llvm::Constant* c, std::string &name); 180259698Sdim 181259698Sdim /// isTargetMatch - Returns 'true' if the memory buffer is for the specified 182259698Sdim /// target triple. 183259698Sdim static bool isTargetMatch(llvm::MemoryBuffer *memBuffer, 184259698Sdim const char *triplePrefix); 185259698Sdim 186259698Sdim /// makeLTOModule - Create an LTOModule (private version). N.B. This 187259698Sdim /// method takes ownership of the buffer. 188259698Sdim static LTOModule *makeLTOModule(llvm::MemoryBuffer *buffer, 189259698Sdim llvm::TargetOptions options, 190259698Sdim std::string &errMsg); 191259698Sdim 192259698Sdim /// makeBuffer - Create a MemoryBuffer from a memory range. 193259698Sdim static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length); 194259698Sdim}; 195259698Sdim 196259698Sdim#endif // LTO_MODULE_H 197