Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:22

0001 //===- llvm/Bitcode/BitcodeAnalyzer.h - Bitcode analyzer --------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This header defines interfaces to analyze LLVM bitcode files/streams.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_BITCODE_BITCODEANALYZER_H
0014 #define LLVM_BITCODE_BITCODEANALYZER_H
0015 
0016 #include "llvm/ADT/ArrayRef.h"
0017 #include "llvm/ADT/StringRef.h"
0018 #include "llvm/Bitstream/BitstreamReader.h"
0019 #include "llvm/Support/Error.h"
0020 #include <map>
0021 #include <optional>
0022 #include <vector>
0023 
0024 namespace llvm {
0025 
0026 class raw_ostream;
0027 
0028 /// CurStreamTypeType - A type for CurStreamType
0029 enum CurStreamTypeType {
0030   UnknownBitstream,
0031   LLVMIRBitstream,
0032   ClangSerializedASTBitstream,
0033   ClangSerializedDiagnosticsBitstream,
0034   LLVMBitstreamRemarks
0035 };
0036 
0037 struct BCDumpOptions {
0038   /// The stream.
0039   raw_ostream &OS;
0040   /// Print per-code histogram.
0041   bool Histogram = false;
0042   /// Don't emit numeric info in dump if symbolic info is available.
0043   bool Symbolic = false;
0044   /// Print binary blobs using hex escapes.
0045   bool ShowBinaryBlobs = false;
0046   /// Print BLOCKINFO block details.
0047   bool DumpBlockinfo = false;
0048 
0049   BCDumpOptions(raw_ostream &OS) : OS(OS) {}
0050 };
0051 
0052 class BitcodeAnalyzer {
0053   BitstreamCursor Stream;
0054   BitstreamBlockInfo BlockInfo;
0055   CurStreamTypeType CurStreamType;
0056   std::optional<BitstreamCursor> BlockInfoStream;
0057   unsigned NumTopBlocks = 0;
0058 
0059   struct PerRecordStats {
0060     unsigned NumInstances = 0;
0061     unsigned NumAbbrev = 0;
0062     uint64_t TotalBits = 0;
0063     PerRecordStats() = default;
0064   };
0065 
0066   struct PerBlockIDStats {
0067     /// NumInstances - This the number of times this block ID has been seen.
0068     unsigned NumInstances = 0;
0069     /// NumBits - The total size in bits of all of these blocks.
0070     uint64_t NumBits = 0;
0071     /// NumSubBlocks - The total number of blocks these blocks contain.
0072     unsigned NumSubBlocks = 0;
0073     /// NumAbbrevs - The total number of abbreviations.
0074     unsigned NumAbbrevs = 0;
0075     /// NumRecords - The total number of records these blocks contain, and the
0076     /// number that are abbreviated.
0077     unsigned NumRecords = 0, NumAbbreviatedRecords = 0;
0078     /// CodeFreq - Keep track of the number of times we see each code.
0079     std::vector<PerRecordStats> CodeFreq;
0080     PerBlockIDStats() = default;
0081   };
0082 
0083   std::map<unsigned, PerBlockIDStats> BlockIDStats;
0084 
0085 public:
0086   BitcodeAnalyzer(StringRef Buffer,
0087                   std::optional<StringRef> BlockInfoBuffer = std::nullopt);
0088   /// Analyze the bitcode file.
0089   Error analyze(std::optional<BCDumpOptions> O = std::nullopt,
0090                 std::optional<StringRef> CheckHash = std::nullopt);
0091   /// Print stats about the bitcode file.
0092   void printStats(BCDumpOptions O,
0093                   std::optional<StringRef> Filename = std::nullopt);
0094 
0095 private:
0096   /// Read a block, updating statistics, etc.
0097   Error parseBlock(unsigned BlockID, unsigned IndentLevel,
0098                    std::optional<BCDumpOptions> O = std::nullopt,
0099                    std::optional<StringRef> CheckHash = std::nullopt);
0100 
0101   Error decodeMetadataStringsBlob(StringRef Indent, ArrayRef<uint64_t> Record,
0102                                   StringRef Blob, raw_ostream &OS);
0103 };
0104 
0105 } // end namespace llvm
0106 
0107 #endif // LLVM_BITCODE_BITCODEANALYZER_H