Home | History | Annotate | Download | only in Bitcode
      1 //===-- llvm/Bitcode/BitcodeWriter.h - Bitcode writers ----*- C++ -*-===//
      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 header defines interfaces to write LLVM bitcode files/streams.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_BITCODE_BITCODEWRITER_H
     15 #define LLVM_BITCODE_BITCODEWRITER_H
     16 
     17 #include "llvm/IR/ModuleSummaryIndex.h"
     18 #include "llvm/MC/StringTableBuilder.h"
     19 #include <string>
     20 
     21 namespace llvm {
     22   class BitstreamWriter;
     23   class Module;
     24   class raw_ostream;
     25 
     26   class BitcodeWriter {
     27     SmallVectorImpl<char> &Buffer;
     28     std::unique_ptr<BitstreamWriter> Stream;
     29 
     30     StringTableBuilder StrtabBuilder{StringTableBuilder::RAW};
     31     bool WroteStrtab = false;
     32 
     33     void writeBlob(unsigned Block, unsigned Record, StringRef Blob);
     34 
     35    public:
     36     /// Create a BitcodeWriter that writes to Buffer.
     37     BitcodeWriter(SmallVectorImpl<char> &Buffer);
     38 
     39     ~BitcodeWriter();
     40 
     41     /// Write the bitcode file's string table. This must be called exactly once
     42     /// after all modules have been written.
     43     void writeStrtab();
     44 
     45     /// Copy the string table for another module into this bitcode file. This
     46     /// should be called after copying the module itself into the bitcode file.
     47     void copyStrtab(StringRef Strtab);
     48 
     49     /// Write the specified module to the buffer specified at construction time.
     50     ///
     51     /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
     52     /// Value in \c M.  These will be reconstructed exactly when \a M is
     53     /// deserialized.
     54     ///
     55     /// If \c Index is supplied, the bitcode will contain the summary index
     56     /// (currently for use in ThinLTO optimization).
     57     ///
     58     /// \p GenerateHash enables hashing the Module and including the hash in the
     59     /// bitcode (currently for use in ThinLTO incremental build).
     60     ///
     61     /// If \p ModHash is non-null, when GenerateHash is true, the resulting
     62     /// hash is written into ModHash. When GenerateHash is false, that value
     63     /// is used as the hash instead of computing from the generated bitcode.
     64     /// Can be used to produce the same module hash for a minimized bitcode
     65     /// used just for the thin link as in the regular full bitcode that will
     66     /// be used in the backend.
     67     void writeModule(const Module *M, bool ShouldPreserveUseListOrder = false,
     68                      const ModuleSummaryIndex *Index = nullptr,
     69                      bool GenerateHash = false, ModuleHash *ModHash = nullptr);
     70 
     71     void writeIndex(
     72         const ModuleSummaryIndex *Index,
     73         const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex);
     74   };
     75 
     76   /// \brief Write the specified module to the specified raw output stream.
     77   ///
     78   /// For streams where it matters, the given stream should be in "binary"
     79   /// mode.
     80   ///
     81   /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
     82   /// Value in \c M.  These will be reconstructed exactly when \a M is
     83   /// deserialized.
     84   ///
     85   /// If \c Index is supplied, the bitcode will contain the summary index
     86   /// (currently for use in ThinLTO optimization).
     87   ///
     88   /// \p GenerateHash enables hashing the Module and including the hash in the
     89   /// bitcode (currently for use in ThinLTO incremental build).
     90   ///
     91   /// If \p ModHash is non-null, when GenerateHash is true, the resulting
     92   /// hash is written into ModHash. When GenerateHash is false, that value
     93   /// is used as the hash instead of computing from the generated bitcode.
     94   /// Can be used to produce the same module hash for a minimized bitcode
     95   /// used just for the thin link as in the regular full bitcode that will
     96   /// be used in the backend.
     97   void WriteBitcodeToFile(const Module *M, raw_ostream &Out,
     98                           bool ShouldPreserveUseListOrder = false,
     99                           const ModuleSummaryIndex *Index = nullptr,
    100                           bool GenerateHash = false,
    101                           ModuleHash *ModHash = nullptr);
    102 
    103   /// Write the specified module summary index to the given raw output stream,
    104   /// where it will be written in a new bitcode block. This is used when
    105   /// writing the combined index file for ThinLTO. When writing a subset of the
    106   /// index for a distributed backend, provide the \p ModuleToSummariesForIndex
    107   /// map.
    108   void WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out,
    109                         const std::map<std::string, GVSummaryMapTy>
    110                             *ModuleToSummariesForIndex = nullptr);
    111 } // End llvm namespace
    112 
    113 #endif
    114