LLVM  5.0.0
BitcodeWriter.h
Go to the documentation of this file.
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 
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 
31 
32  // Owns any strings created by the irsymtab writer until we create the
33  // string table.
34  BumpPtrAllocator Alloc;
35 
36  bool WroteStrtab = false, WroteSymtab = false;
37 
38  void writeBlob(unsigned Block, unsigned Record, StringRef Blob);
39 
40  std::vector<Module *> Mods;
41 
42  public:
43  /// Create a BitcodeWriter that writes to Buffer.
45 
47 
48  /// Attempt to write a symbol table to the bitcode file. This must be called
49  /// at most once after all modules have been written.
50  ///
51  /// A reader does not require a symbol table to interpret a bitcode file;
52  /// the symbol table is needed only to improve link-time performance. So
53  /// this function may decide not to write a symbol table. It may so decide
54  /// if, for example, the target is unregistered or the IR is malformed.
55  void writeSymtab();
56 
57  /// Write the bitcode file's string table. This must be called exactly once
58  /// after all modules and the optional symbol table have been written.
59  void writeStrtab();
60 
61  /// Copy the string table for another module into this bitcode file. This
62  /// should be called after copying the module itself into the bitcode file.
63  void copyStrtab(StringRef Strtab);
64 
65  /// Write the specified module to the buffer specified at construction time.
66  ///
67  /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
68  /// Value in \c M. These will be reconstructed exactly when \a M is
69  /// deserialized.
70  ///
71  /// If \c Index is supplied, the bitcode will contain the summary index
72  /// (currently for use in ThinLTO optimization).
73  ///
74  /// \p GenerateHash enables hashing the Module and including the hash in the
75  /// bitcode (currently for use in ThinLTO incremental build).
76  ///
77  /// If \p ModHash is non-null, when GenerateHash is true, the resulting
78  /// hash is written into ModHash. When GenerateHash is false, that value
79  /// is used as the hash instead of computing from the generated bitcode.
80  /// Can be used to produce the same module hash for a minimized bitcode
81  /// used just for the thin link as in the regular full bitcode that will
82  /// be used in the backend.
83  void writeModule(const Module *M, bool ShouldPreserveUseListOrder = false,
84  const ModuleSummaryIndex *Index = nullptr,
85  bool GenerateHash = false, ModuleHash *ModHash = nullptr);
86 
87  void writeIndex(
88  const ModuleSummaryIndex *Index,
89  const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex);
90  };
91 
92  /// \brief Write the specified module to the specified raw output stream.
93  ///
94  /// For streams where it matters, the given stream should be in "binary"
95  /// mode.
96  ///
97  /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
98  /// Value in \c M. These will be reconstructed exactly when \a M is
99  /// deserialized.
100  ///
101  /// If \c Index is supplied, the bitcode will contain the summary index
102  /// (currently for use in ThinLTO optimization).
103  ///
104  /// \p GenerateHash enables hashing the Module and including the hash in the
105  /// bitcode (currently for use in ThinLTO incremental build).
106  ///
107  /// If \p ModHash is non-null, when GenerateHash is true, the resulting
108  /// hash is written into ModHash. When GenerateHash is false, that value
109  /// is used as the hash instead of computing from the generated bitcode.
110  /// Can be used to produce the same module hash for a minimized bitcode
111  /// used just for the thin link as in the regular full bitcode that will
112  /// be used in the backend.
113  void WriteBitcodeToFile(const Module *M, raw_ostream &Out,
114  bool ShouldPreserveUseListOrder = false,
115  const ModuleSummaryIndex *Index = nullptr,
116  bool GenerateHash = false,
117  ModuleHash *ModHash = nullptr);
118 
119  /// Write the specified module summary index to the given raw output stream,
120  /// where it will be written in a new bitcode block. This is used when
121  /// writing the combined index file for ThinLTO. When writing a subset of the
122  /// index for a distributed backend, provide the \p ModuleToSummariesForIndex
123  /// map.
124  void WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out,
125  const std::map<std::string, GVSummaryMapTy>
126  *ModuleToSummariesForIndex = nullptr);
127 } // End llvm namespace
128 
129 #endif
void writeIndex(const ModuleSummaryIndex *Index, const std::map< std::string, GVSummaryMapTy > *ModuleToSummariesForIndex)
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
void writeSymtab()
Attempt to write a symbol table to the bitcode file.
std::array< uint32_t, 5 > ModuleHash
160 bits SHA1
Utility for building string tables with deduplicated suffixes.
Class to hold module path string table and global value map, and encapsulate methods for operating on...
void WriteBitcodeToFile(const Module *M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the specified raw output stream.
BitcodeWriter(SmallVectorImpl< char > &Buffer)
Create a BitcodeWriter that writes to Buffer.
Allocate memory in an ever growing pool, as if by bump-pointer.
Definition: Allocator.h:138
void writeModule(const Module *M, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the buffer specified at construction time.
void WriteIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out, const std::map< std::string, GVSummaryMapTy > *ModuleToSummariesForIndex=nullptr)
Write the specified module summary index to the given raw output stream, where it will be written in ...
void copyStrtab(StringRef Strtab)
Copy the string table for another module into this bitcode file.
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void writeStrtab()
Write the bitcode file's string table.