LLVM  mainline
ReaderWriter.h
Go to the documentation of this file.
00001 //===-- llvm/Bitcode/ReaderWriter.h - Bitcode reader/writers ----*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This header defines interfaces to read and write LLVM bitcode files/streams.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_BITCODE_READERWRITER_H
00015 #define LLVM_BITCODE_READERWRITER_H
00016 
00017 #include "llvm/IR/DiagnosticInfo.h"
00018 #include "llvm/IR/FunctionInfo.h"
00019 #include "llvm/Support/Endian.h"
00020 #include "llvm/Support/ErrorOr.h"
00021 #include "llvm/Support/MemoryBuffer.h"
00022 #include <memory>
00023 #include <string>
00024 
00025 namespace llvm {
00026   class BitstreamWriter;
00027   class DataStreamer;
00028   class LLVMContext;
00029   class Module;
00030   class ModulePass;
00031   class raw_ostream;
00032 
00033   /// Read the header of the specified bitcode buffer and prepare for lazy
00034   /// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
00035   /// lazily load metadata as well. If successful, this moves Buffer. On
00036   /// error, this *does not* move Buffer.
00037   ErrorOr<std::unique_ptr<Module>>
00038   getLazyBitcodeModule(std::unique_ptr<MemoryBuffer> &&Buffer,
00039                        LLVMContext &Context,
00040                        bool ShouldLazyLoadMetadata = false);
00041 
00042   /// Read the header of the specified stream and prepare for lazy
00043   /// deserialization and streaming of function bodies.
00044   ErrorOr<std::unique_ptr<Module>>
00045   getStreamedBitcodeModule(StringRef Name,
00046                            std::unique_ptr<DataStreamer> Streamer,
00047                            LLVMContext &Context);
00048 
00049   /// Read the header of the specified bitcode buffer and extract just the
00050   /// triple information. If successful, this returns a string. On error, this
00051   /// returns "".
00052   std::string getBitcodeTargetTriple(MemoryBufferRef Buffer,
00053                                      LLVMContext &Context);
00054 
00055   /// Read the header of the specified bitcode buffer and extract just the
00056   /// producer string information. If successful, this returns a string. On
00057   /// error, this returns "".
00058   std::string getBitcodeProducerString(MemoryBufferRef Buffer,
00059                                        LLVMContext &Context);
00060 
00061   /// Read the specified bitcode file, returning the module.
00062   ErrorOr<std::unique_ptr<Module>> parseBitcodeFile(MemoryBufferRef Buffer,
00063                                                     LLVMContext &Context);
00064 
00065   /// Check if the given bitcode buffer contains a function summary block.
00066   bool hasFunctionSummary(MemoryBufferRef Buffer,
00067                           DiagnosticHandlerFunction DiagnosticHandler);
00068 
00069   /// Parse the specified bitcode buffer, returning the function info index.
00070   /// If IsLazy is true, parse the entire function summary into
00071   /// the index. Otherwise skip the function summary section, and only create
00072   /// an index object with a map from function name to function summary offset.
00073   /// The index is used to perform lazy function summary reading later.
00074   ErrorOr<std::unique_ptr<FunctionInfoIndex>>
00075   getFunctionInfoIndex(MemoryBufferRef Buffer,
00076                        DiagnosticHandlerFunction DiagnosticHandler,
00077                        bool IsLazy = false);
00078 
00079   /// This method supports lazy reading of function summary data from the
00080   /// combined index during function importing. When reading the combined index
00081   /// file, getFunctionInfoIndex is first invoked with IsLazy=true.
00082   /// Then this method is called for each function considered for importing,
00083   /// to parse the summary information for the given function name into
00084   /// the index.
00085   std::error_code readFunctionSummary(
00086       MemoryBufferRef Buffer, DiagnosticHandlerFunction DiagnosticHandler,
00087       StringRef FunctionName, std::unique_ptr<FunctionInfoIndex> Index);
00088 
00089   /// \brief Write the specified module to the specified raw output stream.
00090   ///
00091   /// For streams where it matters, the given stream should be in "binary"
00092   /// mode.
00093   ///
00094   /// If \c ShouldPreserveUseListOrder, encode the use-list order for each \a
00095   /// Value in \c M.  These will be reconstructed exactly when \a M is
00096   /// deserialized.
00097   ///
00098   /// If \c EmitFunctionSummary, emit the function summary index (currently
00099   /// for use in ThinLTO optimization).
00100   void WriteBitcodeToFile(const Module *M, raw_ostream &Out,
00101                           bool ShouldPreserveUseListOrder = false,
00102                           bool EmitFunctionSummary = false);
00103 
00104   /// Write the specified function summary index to the given raw output stream,
00105   /// where it will be written in a new bitcode block. This is used when
00106   /// writing the combined index file for ThinLTO.
00107   void WriteFunctionSummaryToFile(const FunctionInfoIndex &Index,
00108                                   raw_ostream &Out);
00109 
00110   /// isBitcodeWrapper - Return true if the given bytes are the magic bytes
00111   /// for an LLVM IR bitcode wrapper.
00112   ///
00113   inline bool isBitcodeWrapper(const unsigned char *BufPtr,
00114                                const unsigned char *BufEnd) {
00115     // See if you can find the hidden message in the magic bytes :-).
00116     // (Hint: it's a little-endian encoding.)
00117     return BufPtr != BufEnd &&
00118            BufPtr[0] == 0xDE &&
00119            BufPtr[1] == 0xC0 &&
00120            BufPtr[2] == 0x17 &&
00121            BufPtr[3] == 0x0B;
00122   }
00123 
00124   /// isRawBitcode - Return true if the given bytes are the magic bytes for
00125   /// raw LLVM IR bitcode (without a wrapper).
00126   ///
00127   inline bool isRawBitcode(const unsigned char *BufPtr,
00128                            const unsigned char *BufEnd) {
00129     // These bytes sort of have a hidden message, but it's not in
00130     // little-endian this time, and it's a little redundant.
00131     return BufPtr != BufEnd &&
00132            BufPtr[0] == 'B' &&
00133            BufPtr[1] == 'C' &&
00134            BufPtr[2] == 0xc0 &&
00135            BufPtr[3] == 0xde;
00136   }
00137 
00138   /// isBitcode - Return true if the given bytes are the magic bytes for
00139   /// LLVM IR bitcode, either with or without a wrapper.
00140   ///
00141   inline bool isBitcode(const unsigned char *BufPtr,
00142                         const unsigned char *BufEnd) {
00143     return isBitcodeWrapper(BufPtr, BufEnd) ||
00144            isRawBitcode(BufPtr, BufEnd);
00145   }
00146 
00147   /// SkipBitcodeWrapperHeader - Some systems wrap bc files with a special
00148   /// header for padding or other reasons.  The format of this header is:
00149   ///
00150   /// struct bc_header {
00151   ///   uint32_t Magic;         // 0x0B17C0DE
00152   ///   uint32_t Version;       // Version, currently always 0.
00153   ///   uint32_t BitcodeOffset; // Offset to traditional bitcode file.
00154   ///   uint32_t BitcodeSize;   // Size of traditional bitcode file.
00155   ///   ... potentially other gunk ...
00156   /// };
00157   ///
00158   /// This function is called when we find a file with a matching magic number.
00159   /// In this case, skip down to the subsection of the file that is actually a
00160   /// BC file.
00161   /// If 'VerifyBufferSize' is true, check that the buffer is large enough to
00162   /// contain the whole bitcode file.
00163   inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
00164                                        const unsigned char *&BufEnd,
00165                                        bool VerifyBufferSize) {
00166     enum {
00167       KnownHeaderSize = 4*4,  // Size of header we read.
00168       OffsetField = 2*4,      // Offset in bytes to Offset field.
00169       SizeField = 3*4         // Offset in bytes to Size field.
00170     };
00171 
00172     // Must contain the header!
00173     if (BufEnd-BufPtr < KnownHeaderSize) return true;
00174 
00175     unsigned Offset = support::endian::read32le(&BufPtr[OffsetField]);
00176     unsigned Size = support::endian::read32le(&BufPtr[SizeField]);
00177 
00178     // Verify that Offset+Size fits in the file.
00179     if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))
00180       return true;
00181     BufPtr += Offset;
00182     BufEnd = BufPtr+Size;
00183     return false;
00184   }
00185 
00186   const std::error_category &BitcodeErrorCategory();
00187   enum class BitcodeError { InvalidBitcodeSignature = 1, CorruptedBitcode };
00188   inline std::error_code make_error_code(BitcodeError E) {
00189     return std::error_code(static_cast<int>(E), BitcodeErrorCategory());
00190   }
00191 
00192   class BitcodeDiagnosticInfo : public DiagnosticInfo {
00193     const Twine &Msg;
00194     std::error_code EC;
00195 
00196   public:
00197     BitcodeDiagnosticInfo(std::error_code EC, DiagnosticSeverity Severity,
00198                           const Twine &Msg);
00199     void print(DiagnosticPrinter &DP) const override;
00200     std::error_code getError() const { return EC; }
00201 
00202     static bool classof(const DiagnosticInfo *DI) {
00203       return DI->getKind() == DK_Bitcode;
00204     }
00205   };
00206 
00207 } // End llvm namespace
00208 
00209 namespace std {
00210 template <> struct is_error_code_enum<llvm::BitcodeError> : std::true_type {};
00211 }
00212 
00213 #endif