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 DiagnosticHandlerFunction DiagnosticHandler = nullptr,
41 bool ShouldLazyLoadMetadata = false);
43 /// Read the header of the specified stream and prepare for lazy
44 /// deserialization and streaming of function bodies.
45 ErrorOr<std::unique_ptr<Module>> getStreamedBitcodeModule(
46 StringRef Name, std::unique_ptr<DataStreamer> Streamer,
48 DiagnosticHandlerFunction DiagnosticHandler = nullptr);
50 /// Read the header of the specified bitcode buffer and extract just the
51 /// triple information. If successful, this returns a string. On error, this
54 getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context,
55 DiagnosticHandlerFunction DiagnosticHandler = nullptr);
57 /// Read the specified bitcode file, returning the module.
58 ErrorOr<std::unique_ptr<Module>>
59 parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
60 DiagnosticHandlerFunction DiagnosticHandler = nullptr);
62 /// Check if the given bitcode buffer contains a function summary block.
63 bool hasFunctionSummary(MemoryBufferRef Buffer, LLVMContext &Context,
64 DiagnosticHandlerFunction DiagnosticHandler);
66 /// Parse the specified bitcode buffer, returning the function info index.
67 /// If IsLazy is true, parse the entire function summary into
68 /// the index. Otherwise skip the function summary section, and only create
69 /// an index object with a map from function name to function summary offset.
70 /// The index is used to perform lazy function summary reading later.
71 ErrorOr<std::unique_ptr<FunctionInfoIndex>> getFunctionInfoIndex(
72 MemoryBufferRef Buffer, LLVMContext &Context,
73 DiagnosticHandlerFunction DiagnosticHandler, bool IsLazy = false);
75 /// This method supports lazy reading of function summary data from the
76 /// combined index during function importing. When reading the combined index
77 /// file, getFunctionInfoIndex is first invoked with IsLazy=true.
78 /// Then this method is called for each function considered for importing,
79 /// to parse the summary information for the given function name into
81 std::error_code readFunctionSummary(
82 MemoryBufferRef Buffer, LLVMContext &Context,
83 DiagnosticHandlerFunction DiagnosticHandler, StringRef FunctionName,
84 std::unique_ptr<FunctionInfoIndex> Index);
86 /// \brief Write the specified module to the specified raw output stream.
88 /// For streams where it matters, the given stream should be in "binary"
91 /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
92 /// Value in \c M. These will be reconstructed exactly when \a M is
95 /// If \c EmitFunctionSummary, emit the function summary index (currently
96 /// for use in ThinLTO optimization).
97 void WriteBitcodeToFile(const Module *M, raw_ostream &Out,
98 bool ShouldPreserveUseListOrder = false,
99 bool EmitFunctionSummary = false);
101 /// Write the specified function summary index to the given raw output stream,
102 /// where it will be written in a new bitcode block. This is used when
103 /// writing the combined index file for ThinLTO.
104 void WriteFunctionSummaryToFile(const FunctionInfoIndex *Index,
107 /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
108 /// for an LLVM IR bitcode wrapper.
110 inline bool isBitcodeWrapper(const unsigned char *BufPtr,
111 const unsigned char *BufEnd) {
112 // See if you can find the hidden message in the magic bytes :-).
113 // (Hint: it's a little-endian encoding.)
114 return BufPtr != BufEnd &&
121 /// isRawBitcode - Return true if the given bytes are the magic bytes for
122 /// raw LLVM IR bitcode (without a wrapper).
124 inline bool isRawBitcode(const unsigned char *BufPtr,
125 const unsigned char *BufEnd) {
126 // These bytes sort of have a hidden message, but it's not in
127 // little-endian this time, and it's a little redundant.
128 return BufPtr != BufEnd &&
135 /// isBitcode - Return true if the given bytes are the magic bytes for
136 /// LLVM IR bitcode, either with or without a wrapper.
138 inline bool isBitcode(const unsigned char *BufPtr,
139 const unsigned char *BufEnd) {
140 return isBitcodeWrapper(BufPtr, BufEnd) ||
141 isRawBitcode(BufPtr, BufEnd);
144 /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
145 /// header for padding or other reasons. The format of this header is:
147 /// struct bc_header {
148 /// uint32_t Magic; // 0x0B17C0DE
149 /// uint32_t Version; // Version, currently always 0.
150 /// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
151 /// uint32_t BitcodeSize; // Size of traditional bitcode file.
152 /// ... potentially other gunk ...
155 /// This function is called when we find a file with a matching magic number.
156 /// In this case, skip down to the subsection of the file that is actually a
158 /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
159 /// contain the whole bitcode file.
160 inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
161 const unsigned char *&BufEnd,
162 bool VerifyBufferSize) {
164 KnownHeaderSize = 4*4, // Size of header we read.
165 OffsetField = 2*4, // Offset in bytes to Offset field.
166 SizeField = 3*4 // Offset in bytes to Size field.
169 // Must contain the header!
170 if (BufEnd-BufPtr < KnownHeaderSize) return true;
172 unsigned Offset = support::endian::read32le(&BufPtr[OffsetField]);
173 unsigned Size = support::endian::read32le(&BufPtr[SizeField]);
175 // Verify that Offset+Size fits in the file.
176 if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))
179 BufEnd = BufPtr+Size;
183 const std::error_category &BitcodeErrorCategory();
184 enum class BitcodeError { InvalidBitcodeSignature = 1, CorruptedBitcode };
185 inline std::error_code make_error_code(BitcodeError E) {
186 return std::error_code(static_cast<int>(E), BitcodeErrorCategory());
189 class BitcodeDiagnosticInfo : public DiagnosticInfo {
194 BitcodeDiagnosticInfo(std::error_code EC, DiagnosticSeverity Severity,
196 void print(DiagnosticPrinter &DP) const override;
197 std::error_code getError() const { return EC; }
199 static bool classof(const DiagnosticInfo *DI) {
200 return DI->getKind() == DK_Bitcode;
204 } // End llvm namespace
207 template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {};