Home | History | Annotate | Download | only in lto
      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-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"
     24 #include <string>
     25 #include <vector>
     26 
     27 // Forward references to llvm classes.
     28 namespace llvm {
     29   class Function;
     30   class GlobalValue;
     31   class MemoryBuffer;
     32   class TargetOptions;
     33   class Value;
     34 }
     35 
     36 //===----------------------------------------------------------------------===//
     37 /// LTOModule - C++ class which implements the opaque lto_module_t type.
     38 ///
     39 struct LTOModule {
     40 private:
     41   typedef llvm::StringMap<uint8_t> StringSet;
     42 
     43   struct NameAndAttributes {
     44     const char        *name;
     45     uint32_t           attributes;
     46     bool               isFunction;
     47     const llvm::GlobalValue *symbol;
     48   };
     49 
     50   llvm::OwningPtr<llvm::Module>           _module;
     51   llvm::OwningPtr<llvm::TargetMachine>    _target;
     52   std::vector<NameAndAttributes>          _symbols;
     53 
     54   // _defines and _undefines only needed to disambiguate tentative definitions
     55   StringSet                               _defines;
     56   llvm::StringMap<NameAndAttributes>      _undefines;
     57   std::vector<const char*>                _asm_undefines;
     58   llvm::MCContext                         _context;
     59 
     60   // Use mangler to add GlobalPrefix to names to match linker names.
     61   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, std::string &errMsg);
     84   static LTOModule *makeLTOModule(int fd, const char *path,
     85                                   size_t file_size,
     86                                   size_t map_size,
     87                                   off_t offset,
     88                                   std::string& errMsg);
     89   static LTOModule *makeLTOModule(const void *mem, size_t length,
     90                                   std::string &errMsg);
     91 
     92   /// getTargetTriple - Return the Module's target triple.
     93   const char *getTargetTriple() {
     94     return _module->getTargetTriple().c_str();
     95   }
     96 
     97   /// setTargetTriple - Set the Module's target triple.
     98   void setTargetTriple(const char *triple) {
     99     _module->setTargetTriple(triple);
    100   }
    101 
    102   /// getSymbolCount - Get the number of symbols
    103   uint32_t getSymbolCount() {
    104     return _symbols.size();
    105   }
    106 
    107   /// getSymbolAttributes - Get the attributes for a symbol at the specified
    108   /// index.
    109   lto_symbol_attributes getSymbolAttributes(uint32_t index) {
    110     if (index < _symbols.size())
    111       return lto_symbol_attributes(_symbols[index].attributes);
    112     return lto_symbol_attributes(0);
    113   }
    114 
    115   /// getSymbolName - Get the name of the symbol at the specified index.
    116   const char *getSymbolName(uint32_t index) {
    117     if (index < _symbols.size())
    118       return _symbols[index].name;
    119     return NULL;
    120   }
    121 
    122   /// getLLVVMModule - Return the Module.
    123   llvm::Module *getLLVVMModule() { return _module.get(); }
    124 
    125   /// getAsmUndefinedRefs -
    126   const std::vector<const char*> &getAsmUndefinedRefs() {
    127     return _asm_undefines;
    128   }
    129 
    130   /// getTargetOptions - Fill the TargetOptions object with the options
    131   /// specified on the command line.
    132   static void getTargetOptions(llvm::TargetOptions &Options);
    133 
    134 private:
    135   /// parseSymbols - Parse the symbols from the module and model-level ASM and
    136   /// add them to either the defined or undefined lists.
    137   bool parseSymbols(std::string &errMsg);
    138 
    139   /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet
    140   /// to a list to be resolved later.
    141   void addPotentialUndefinedSymbol(const llvm::GlobalValue *dcl, bool isFunc);
    142 
    143   /// addDefinedSymbol - Add a defined symbol to the list.
    144   void addDefinedSymbol(const llvm::GlobalValue *def, bool isFunction);
    145 
    146   /// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
    147   void addDefinedFunctionSymbol(const llvm::Function *f);
    148 
    149   /// addDefinedDataSymbol - Add a data symbol as defined to the list.
    150   void addDefinedDataSymbol(const 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 *, lto_symbol_attributes scope);
    159 
    160   /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to
    161   /// the undefined list.
    162   void addAsmGlobalSymbolUndef(const char *);
    163 
    164   /// addObjCClass - Parse i386/ppc ObjC class data structure.
    165   void addObjCClass(const llvm::GlobalVariable *clgv);
    166 
    167   /// addObjCCategory - Parse i386/ppc ObjC category data structure.
    168   void addObjCCategory(const llvm::GlobalVariable *clgv);
    169 
    170   /// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
    171   void addObjCClassRef(const llvm::GlobalVariable *clgv);
    172 
    173   /// objcClassNameFromExpression - Get string that the data pointer points
    174   /// to.
    175   bool objcClassNameFromExpression(const llvm::Constant* c, std::string &name);
    176 
    177   /// isTargetMatch - Returns 'true' if the memory buffer is for the specified
    178   /// target triple.
    179   static bool isTargetMatch(llvm::MemoryBuffer *memBuffer,
    180                             const char *triplePrefix);
    181 
    182   /// makeLTOModule - Create an LTOModule (private version). N.B. This
    183   /// method takes ownership of the buffer.
    184   static LTOModule *makeLTOModule(llvm::MemoryBuffer *buffer,
    185                                   std::string &errMsg);
    186 
    187   /// makeBuffer - Create a MemoryBuffer from a memory range.
    188   static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length);
    189 };
    190 
    191 #endif // LTO_MODULE_H
    192