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 LLVM_LTO_LTOMODULE_H
     15 #define LLVM_LTO_LTOMODULE_H
     16 
     17 #include "llvm-c/lto.h"
     18 #include "llvm/ADT/StringMap.h"
     19 #include "llvm/ADT/StringSet.h"
     20 #include "llvm/IR/Module.h"
     21 #include "llvm/MC/MCContext.h"
     22 #include "llvm/MC/MCObjectFileInfo.h"
     23 #include "llvm/Object/IRObjectFile.h"
     24 #include "llvm/Target/TargetMachine.h"
     25 #include <string>
     26 #include <vector>
     27 
     28 // Forward references to llvm classes.
     29 namespace llvm {
     30   class Function;
     31   class GlobalValue;
     32   class MemoryBuffer;
     33   class TargetOptions;
     34   class Value;
     35 
     36 //===----------------------------------------------------------------------===//
     37 /// C++ class which implements the opaque lto_module_t type.
     38 ///
     39 struct LTOModule {
     40 private:
     41   struct NameAndAttributes {
     42     const char        *name;
     43     uint32_t           attributes;
     44     bool               isFunction;
     45     const GlobalValue *symbol;
     46   };
     47 
     48   std::unique_ptr<LLVMContext> OwnedContext;
     49 
     50   std::string LinkerOpts;
     51 
     52   std::unique_ptr<object::IRObjectFile> IRFile;
     53   std::unique_ptr<TargetMachine> _target;
     54   std::vector<NameAndAttributes> _symbols;
     55 
     56   // _defines and _undefines only needed to disambiguate tentative definitions
     57   StringSet<>                             _defines;
     58   StringMap<NameAndAttributes> _undefines;
     59   std::vector<const char*>                _asm_undefines;
     60 
     61   LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM);
     62   LTOModule(std::unique_ptr<object::IRObjectFile> Obj, TargetMachine *TM,
     63             std::unique_ptr<LLVMContext> Context);
     64 
     65 public:
     66   ~LTOModule();
     67 
     68   /// Returns 'true' if the file or memory contents is LLVM bitcode.
     69   static bool isBitcodeFile(const void *mem, size_t length);
     70   static bool isBitcodeFile(const char *path);
     71 
     72   /// Returns 'true' if the memory buffer is LLVM bitcode for the specified
     73   /// triple.
     74   static bool isBitcodeForTarget(MemoryBuffer *memBuffer,
     75                                  StringRef triplePrefix);
     76 
     77   /// Returns a string representing the producer identification stored in the
     78   /// bitcode, or "" if the bitcode does not contains any.
     79   ///
     80   static std::string getProducerString(MemoryBuffer *Buffer);
     81 
     82   /// Create a MemoryBuffer from a memory range with an optional name.
     83   static std::unique_ptr<MemoryBuffer>
     84   makeBuffer(const void *mem, size_t length, StringRef name = "");
     85 
     86   /// Create an LTOModule. N.B. These methods take ownership of the buffer. The
     87   /// caller must have initialized the Targets, the TargetMCs, the AsmPrinters,
     88   /// and the AsmParsers by calling:
     89   ///
     90   /// InitializeAllTargets();
     91   /// InitializeAllTargetMCs();
     92   /// InitializeAllAsmPrinters();
     93   /// InitializeAllAsmParsers();
     94   static ErrorOr<std::unique_ptr<LTOModule>>
     95   createFromFile(LLVMContext &Context, const char *path, TargetOptions options);
     96   static ErrorOr<std::unique_ptr<LTOModule>>
     97   createFromOpenFile(LLVMContext &Context, int fd, const char *path,
     98                      size_t size, TargetOptions options);
     99   static ErrorOr<std::unique_ptr<LTOModule>>
    100   createFromOpenFileSlice(LLVMContext &Context, int fd, const char *path,
    101                           size_t map_size, off_t offset, TargetOptions options);
    102   static ErrorOr<std::unique_ptr<LTOModule>>
    103   createFromBuffer(LLVMContext &Context, const void *mem, size_t length,
    104                    TargetOptions options, StringRef path = "");
    105 
    106   static ErrorOr<std::unique_ptr<LTOModule>>
    107   createInLocalContext(const void *mem, size_t length, TargetOptions options,
    108                        StringRef path);
    109   static ErrorOr<std::unique_ptr<LTOModule>>
    110   createInContext(const void *mem, size_t length, TargetOptions options,
    111                   StringRef path, LLVMContext *Context);
    112 
    113   const Module &getModule() const {
    114     return const_cast<LTOModule*>(this)->getModule();
    115   }
    116   Module &getModule() {
    117     return IRFile->getModule();
    118   }
    119 
    120   std::unique_ptr<Module> takeModule() { return IRFile->takeModule(); }
    121 
    122   /// Return the Module's target triple.
    123   const std::string &getTargetTriple() {
    124     return getModule().getTargetTriple();
    125   }
    126 
    127   /// Set the Module's target triple.
    128   void setTargetTriple(StringRef Triple) {
    129     getModule().setTargetTriple(Triple);
    130   }
    131 
    132   /// Get the number of symbols
    133   uint32_t getSymbolCount() {
    134     return _symbols.size();
    135   }
    136 
    137   /// Get the attributes for a symbol at the specified index.
    138   lto_symbol_attributes getSymbolAttributes(uint32_t index) {
    139     if (index < _symbols.size())
    140       return lto_symbol_attributes(_symbols[index].attributes);
    141     return lto_symbol_attributes(0);
    142   }
    143 
    144   /// Get the name of the symbol at the specified index.
    145   const char *getSymbolName(uint32_t index) {
    146     if (index < _symbols.size())
    147       return _symbols[index].name;
    148     return nullptr;
    149   }
    150 
    151   const GlobalValue *getSymbolGV(uint32_t index) {
    152     if (index < _symbols.size())
    153       return _symbols[index].symbol;
    154     return nullptr;
    155   }
    156 
    157   const char *getLinkerOpts() {
    158     return LinkerOpts.c_str();
    159   }
    160 
    161   const std::vector<const char*> &getAsmUndefinedRefs() {
    162     return _asm_undefines;
    163   }
    164 
    165 private:
    166   /// Parse metadata from the module
    167   // FIXME: it only parses "Linker Options" metadata at the moment
    168   void parseMetadata();
    169 
    170   /// Parse the symbols from the module and model-level ASM and add them to
    171   /// either the defined or undefined lists.
    172   void parseSymbols();
    173 
    174   /// Add a symbol which isn't defined just yet to a list to be resolved later.
    175   void addPotentialUndefinedSymbol(const object::BasicSymbolRef &Sym,
    176                                    bool isFunc);
    177 
    178   /// Add a defined symbol to the list.
    179   void addDefinedSymbol(const char *Name, const GlobalValue *def,
    180                         bool isFunction);
    181 
    182   /// Add a data symbol as defined to the list.
    183   void addDefinedDataSymbol(const object::BasicSymbolRef &Sym);
    184   void addDefinedDataSymbol(const char*Name, const GlobalValue *v);
    185 
    186   /// Add a function symbol as defined to the list.
    187   void addDefinedFunctionSymbol(const object::BasicSymbolRef &Sym);
    188   void addDefinedFunctionSymbol(const char *Name, const Function *F);
    189 
    190   /// Add a global symbol from module-level ASM to the defined list.
    191   void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
    192 
    193   /// Add a global symbol from module-level ASM to the undefined list.
    194   void addAsmGlobalSymbolUndef(const char *);
    195 
    196   /// Parse i386/ppc ObjC class data structure.
    197   void addObjCClass(const GlobalVariable *clgv);
    198 
    199   /// Parse i386/ppc ObjC category data structure.
    200   void addObjCCategory(const GlobalVariable *clgv);
    201 
    202   /// Parse i386/ppc ObjC class list data structure.
    203   void addObjCClassRef(const GlobalVariable *clgv);
    204 
    205   /// Get string that the data pointer points to.
    206   bool objcClassNameFromExpression(const Constant *c, std::string &name);
    207 
    208   /// Create an LTOModule (private version).
    209   static ErrorOr<std::unique_ptr<LTOModule>>
    210   makeLTOModule(MemoryBufferRef Buffer, TargetOptions options,
    211                 LLVMContext *Context);
    212 };
    213 }
    214 #endif
    215