1 //===-- llvm/Bitcode/ReaderWriter.h - Bitcode reader/writers ----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This header defines interfaces to read and write LLVM bitcode files/streams.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_BITCODE_READERWRITER_H
15 #define LLVM_BITCODE_READERWRITER_H
17 #include "llvm/IR/DiagnosticInfo.h"
18 #include "llvm/IR/FunctionInfo.h"
19 #include "llvm/Support/Endian.h"
20 #include "llvm/Support/ErrorOr.h"
21 #include "llvm/Support/MemoryBuffer.h"
26 class BitstreamWriter;
33 /// Read the header of the specified bitcode buffer and prepare for lazy
34 /// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
35 /// lazily load metadata as well. If successful, this moves Buffer. On
36 /// error, this *does not* move Buffer.
37 ErrorOr<std::unique_ptr<Module>>
38 getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
40 bool ShouldLazyLoadMetadata = false);
42 /// Read the header of the specified stream and prepare for lazy
43 /// deserialization and streaming of function bodies.
44 ErrorOr<std::unique_ptr<Module>>
45 getStreamedBitcodeModule(StringRef Name,
46 std::unique_ptr<DataStreamer> Streamer,
47 LLVMContext &Context);
49 /// Read the header of the specified bitcode buffer and extract just the
50 /// triple information. If successful, this returns a string. On error, this
52 std::string getBitcodeTargetTriple(MemoryBufferRef Buffer,
53 LLVMContext &Context);
55 /// Read the header of the specified bitcode buffer and extract just the
56 /// producer string information. If successful, this returns a string. On
57 /// error, this returns "".
58 std::string getBitcodeProducerString(MemoryBufferRef Buffer,
59 LLVMContext &Context);
61 /// Read the specified bitcode file, returning the module.
62 ErrorOr<std::unique_ptr<Module>> parseBitcodeFile(MemoryBufferRef Buffer,
63 LLVMContext &Context);
65 /// Check if the given bitcode buffer contains a function summary block.
66 bool hasFunctionSummary(MemoryBufferRef Buffer,
67 DiagnosticHandlerFunction DiagnosticHandler);
69 /// Parse the specified bitcode buffer, returning the function info index.
70 /// If IsLazy is true, parse the entire function summary into
71 /// the index. Otherwise skip the function summary section, and only create
72 /// an index object with a map from function name to function summary offset.
73 /// The index is used to perform lazy function summary reading later.
74 ErrorOr<std::unique_ptr<FunctionInfoIndex>>
75 getFunctionInfoIndex(MemoryBufferRef Buffer,
76 DiagnosticHandlerFunction DiagnosticHandler,
79 /// This method supports lazy reading of function summary data from the
80 /// combined index during function importing. When reading the combined index
81 /// file, getFunctionInfoIndex is first invoked with IsLazy=true.
82 /// Then this method is called for each function considered for importing,
83 /// to parse the summary information for the given function name into
85 std::error_code readFunctionSummary(
86 MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
87 StringRef FunctionName, std::unique_ptr<FunctionInfoIndex> Index);
89 /// \brief Write the specified module to the specified raw output stream.
91 /// For streams where it matters, the given stream should be in "binary"
94 /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
95 /// Value in \c M. These will be reconstructed exactly when \a M is
98 /// If \c EmitFunctionSummary, emit the function summary index (currently
99 /// for use in ThinLTO optimization).
100 void WriteBitcodeToFile(const Module *M, raw_ostream &Out,
101 bool ShouldPreserveUseListOrder = false,
102 bool EmitFunctionSummary = false);
104 /// Write the specified function summary index to the given raw output stream,
105 /// where it will be written in a new bitcode block. This is used when
106 /// writing the combined index file for ThinLTO.
107 void WriteFunctionSummaryToFile(const FunctionInfoIndex &Index,
110 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
111 /// for an LLVM IR bitcode wrapper.
113 inline bool isBitcodeWrapper(const unsigned char *BufPtr,
114 const unsigned char *BufEnd) {
115 // See if you can find the hidden message in the magic bytes :-).
116 // (Hint: it's a little-endian encoding.)
117 return BufPtr != BufEnd &&
124 /// isRawBitcode - Return true if the given bytes are the magic bytes for
125 /// raw LLVM IR bitcode (without a wrapper).
127 inline bool isRawBitcode(const unsigned char *BufPtr,
128 const unsigned char *BufEnd) {
129 // These bytes sort of have a hidden message, but it's not in
130 // little-endian this time, and it's a little redundant.
131 return BufPtr != BufEnd &&
138 /// isBitcode - Return true if the given bytes are the magic bytes for
139 /// LLVM IR bitcode, either with or without a wrapper.
141 inline bool isBitcode(const unsigned char *BufPtr,
142 const unsigned char *BufEnd) {
143 return isBitcodeWrapper(BufPtr, BufEnd) ||
144 isRawBitcode(BufPtr, BufEnd);
147 /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
148 /// header for padding or other reasons. The format of this header is:
150 /// struct bc_header {
151 /// uint32_t Magic; // 0x0B17C0DE
152 /// uint32_t Version; // Version, currently always 0.
153 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
154 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
155 /// ... potentially other gunk ...
158 /// This function is called when we find a file with a matching magic number.
159 /// In this case, skip down to the subsection of the file that is actually a
161 /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
162 /// contain the whole bitcode file.
163 inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
164 const unsigned char *&BufEnd,
165 bool VerifyBufferSize) {
167 KnownHeaderSize = 4*4, // Size of header we read.
168 OffsetField = 2*4, // Offset in bytes to Offset field.
169 SizeField = 3*4 // Offset in bytes to Size field.
172 // Must contain the header!
173 if (BufEnd-BufPtr < KnownHeaderSize) return true;
175 unsigned Offset = support::endian::read32le(&BufPtr[OffsetField]);
176 unsigned Size = support::endian::read32le(&BufPtr[SizeField]);
178 // Verify that Offset+Size fits in the file.
179 if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))
182 BufEnd = BufPtr+Size;
186 const std::error_category &BitcodeErrorCategory();
187 enum class BitcodeError { InvalidBitcodeSignature = 1, CorruptedBitcode };
188 inline std::error_code make_error_code(BitcodeError E) {
189 return std::error_code(static_cast<int>(E), BitcodeErrorCategory());
192 class BitcodeDiagnosticInfo : public DiagnosticInfo {
197 BitcodeDiagnosticInfo(std::error_code EC, DiagnosticSeverity Severity,
199 void print(DiagnosticPrinter &DP) const override;
200 std::error_code getError() const { return EC; }
202 static bool classof(const DiagnosticInfo *DI) {
203 return DI->getKind() == DK_Bitcode;
207 } // End llvm namespace
210 template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {};