Do not require a Context to extract the FunctionIndex from Bitcode (NFC)
[oota-llvm.git] / include / llvm / Bitcode / ReaderWriter.h
1 //===-- llvm/Bitcode/ReaderWriter.h - Bitcode reader/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 read and write LLVM bitcode files/streams.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_BITCODE_READERWRITER_H
15 #define LLVM_BITCODE_READERWRITER_H
16
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"
22 #include <memory>
23 #include <string>
24
25 namespace llvm {
26   class BitstreamWriter;
27   class DataStreamer;
28   class LLVMContext;
29   class Module;
30   class ModulePass;
31   class raw_ostream;
32
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,
39                        LLVMContext &Context,
40                        DiagnosticHandlerFunction DiagnosticHandler = nullptr,
41                        bool ShouldLazyLoadMetadata = false);
42
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,
47       LLVMContext &Context,
48       DiagnosticHandlerFunction DiagnosticHandler = nullptr);
49
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
52   /// returns "".
53   std::string
54   getBitcodeTargetTriple(MemoryBufferRef Buffer, LLVMContext &Context,
55                          DiagnosticHandlerFunction DiagnosticHandler = nullptr);
56
57   /// Read the header of the specified bitcode buffer and extract just the
58   /// producer string information. If successful, this returns a string. On
59   /// error, this returns "".
60   std::string getBitcodeProducerString(
61       MemoryBufferRef Buffer, LLVMContext &Context,
62       DiagnosticHandlerFunction DiagnosticHandler = nullptr);
63
64   /// Read the specified bitcode file, returning the module.
65   ErrorOr<std::unique_ptr<Module>>
66   parseBitcodeFile(MemoryBufferRef Buffer, LLVMContext &Context,
67                    DiagnosticHandlerFunction DiagnosticHandler = nullptr);
68
69   /// Check if the given bitcode buffer contains a function summary block.
70   bool hasFunctionSummary(MemoryBufferRef Buffer,
71                           DiagnosticHandlerFunction DiagnosticHandler);
72
73   /// Parse the specified bitcode buffer, returning the function info index.
74   /// If ExportingModule is true, check for functions in the index from this
75   /// module when the combined index is built during parsing and set flag.
76   /// If IsLazy is true, parse the entire function summary into
77   /// the index. Otherwise skip the function summary section, and only create
78   /// an index object with a map from function name to function summary offset.
79   /// The index is used to perform lazy function summary reading later.
80   ErrorOr<std::unique_ptr<FunctionInfoIndex>> getFunctionInfoIndex(
81       MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
82       const Module *ExportingModule = nullptr, bool IsLazy = false);
83
84   /// This method supports lazy reading of function summary data from the
85   /// combined index during function importing. When reading the combined index
86   /// file, getFunctionInfoIndex is first invoked with IsLazy=true.
87   /// Then this method is called for each function considered for importing,
88   /// to parse the summary information for the given function name into
89   /// the index.
90   std::error_code readFunctionSummary(
91       MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
92       StringRef FunctionName, std::unique_ptr<FunctionInfoIndex> Index);
93
94   /// \brief Write the specified module to the specified raw output stream.
95   ///
96   /// For streams where it matters, the given stream should be in "binary"
97   /// mode.
98   ///
99   /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
100   /// Value in \c M.  These will be reconstructed exactly when \a M is
101   /// deserialized.
102   ///
103   /// If \c EmitFunctionSummary, emit the function summary index (currently
104   /// for use in ThinLTO optimization).
105   void WriteBitcodeToFile(const Module *M, raw_ostream &Out,
106                           bool ShouldPreserveUseListOrder = false,
107                           bool EmitFunctionSummary = false);
108
109   /// Write the specified function summary index to the given raw output stream,
110   /// where it will be written in a new bitcode block. This is used when
111   /// writing the combined index file for ThinLTO.
112   void WriteFunctionSummaryToFile(const FunctionInfoIndex &Index,
113                                   raw_ostream &Out);
114
115   /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
116   /// for an LLVM IR bitcode wrapper.
117   ///
118   inline bool isBitcodeWrapper(const unsigned char *BufPtr,
119                                const unsigned char *BufEnd) {
120     // See if you can find the hidden message in the magic bytes :-).
121     // (Hint: it's a little-endian encoding.)
122     return BufPtr != BufEnd &&
123            BufPtr[0] == 0xDE &&
124            BufPtr[1] == 0xC0 &&
125            BufPtr[2] == 0x17 &&
126            BufPtr[3] == 0x0B;
127   }
128
129   /// isRawBitcode - Return true if the given bytes are the magic bytes for
130   /// raw LLVM IR bitcode (without a wrapper).
131   ///
132   inline bool isRawBitcode(const unsigned char *BufPtr,
133                            const unsigned char *BufEnd) {
134     // These bytes sort of have a hidden message, but it's not in
135     // little-endian this time, and it's a little redundant.
136     return BufPtr != BufEnd &&
137            BufPtr[0] == 'B' &&
138            BufPtr[1] == 'C' &&
139            BufPtr[2] == 0xc0 &&
140            BufPtr[3] == 0xde;
141   }
142
143   /// isBitcode - Return true if the given bytes are the magic bytes for
144   /// LLVM IR bitcode, either with or without a wrapper.
145   ///
146   inline bool isBitcode(const unsigned char *BufPtr,
147                         const unsigned char *BufEnd) {
148     return isBitcodeWrapper(BufPtr, BufEnd) ||
149            isRawBitcode(BufPtr, BufEnd);
150   }
151
152   /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
153   /// header for padding or other reasons.  The format of this header is:
154   ///
155   /// struct bc_header {
156   ///   uint32_t Magic;         // 0x0B17C0DE
157   ///   uint32_t Version;       // Version, currently always 0.
158   ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
159   ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
160   ///   ... potentially other gunk ...
161   /// };
162   ///
163   /// This function is called when we find a file with a matching magic number.
164   /// In this case, skip down to the subsection of the file that is actually a
165   /// BC file.
166   /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
167   /// contain the whole bitcode file.
168   inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
169                                        const unsigned char *&BufEnd,
170                                        bool VerifyBufferSize) {
171     enum {
172       KnownHeaderSize = 4*4,  // Size of header we read.
173       OffsetField = 2*4,      // Offset in bytes to Offset field.
174       SizeField = 3*4         // Offset in bytes to Size field.
175     };
176
177     // Must contain the header!
178     if (BufEnd-BufPtr < KnownHeaderSize) return true;
179
180     unsigned Offset = support::endian::read32le(&BufPtr[OffsetField]);
181     unsigned Size = support::endian::read32le(&BufPtr[SizeField]);
182
183     // Verify that Offset+Size fits in the file.
184     if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))
185       return true;
186     BufPtr += Offset;
187     BufEnd = BufPtr+Size;
188     return false;
189   }
190
191   const std::error_category &BitcodeErrorCategory();
192   enum class BitcodeError { InvalidBitcodeSignature = 1, CorruptedBitcode };
193   inline std::error_code make_error_code(BitcodeError E) {
194     return std::error_code(static_cast<int>(E), BitcodeErrorCategory());
195   }
196
197   class BitcodeDiagnosticInfo : public DiagnosticInfo {
198     const Twine &Msg;
199     std::error_code EC;
200
201   public:
202     BitcodeDiagnosticInfo(std::error_code EC, DiagnosticSeverity Severity,
203                           const Twine &Msg);
204     void print(DiagnosticPrinter &DP) const override;
205     std::error_code getError() const { return EC; }
206
207     static bool classof(const DiagnosticInfo *DI) {
208       return DI->getKind() == DK_Bitcode;
209     }
210   };
211
212 } // End llvm namespace
213
214 namespace std {
215 template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {};
216 }
217
218 #endif