1 //===- llvm/Bitcode/BitcodeAnalyzer.h - Bitcode analyzer --------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This header defines interfaces to analyze LLVM bitcode files/streams.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_BITCODE_BITCODEANALYZER_H
14 #define LLVM_BITCODE_BITCODEANALYZER_H
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Bitstream/BitstreamReader.h"
19 #include "llvm/Support/Error.h"
28 /// CurStreamTypeType - A type for CurStreamType
29 enum CurStreamTypeType {
32 ClangSerializedASTBitstream,
33 ClangSerializedDiagnosticsBitstream,
37 struct BCDumpOptions {
40 /// Print per-code histogram.
41 bool Histogram = false;
42 /// Don't emit numeric info in dump if symbolic info is available.
43 bool Symbolic = false;
44 /// Print binary blobs using hex escapes.
45 bool ShowBinaryBlobs = false;
46 /// Print BLOCKINFO block details.
47 bool DumpBlockinfo = false;
49 BCDumpOptions(raw_ostream &OS) : OS(OS) {}
52 class BitcodeAnalyzer {
53 BitstreamCursor Stream;
54 BitstreamBlockInfo BlockInfo;
55 CurStreamTypeType CurStreamType;
56 std::optional<BitstreamCursor> BlockInfoStream;
57 unsigned NumTopBlocks = 0;
59 struct PerRecordStats {
60 unsigned NumInstances = 0;
61 unsigned NumAbbrev = 0;
62 uint64_t TotalBits = 0;
63 PerRecordStats() = default;
66 struct PerBlockIDStats {
67 /// NumInstances - This the number of times this block ID has been seen.
68 unsigned NumInstances = 0;
69 /// NumBits - The total size in bits of all of these blocks.
71 /// NumSubBlocks - The total number of blocks these blocks contain.
72 unsigned NumSubBlocks = 0;
73 /// NumAbbrevs - The total number of abbreviations.
74 unsigned NumAbbrevs = 0;
75 /// NumRecords - The total number of records these blocks contain, and the
76 /// number that are abbreviated.
77 unsigned NumRecords = 0, NumAbbreviatedRecords = 0;
78 /// CodeFreq - Keep track of the number of times we see each code.
79 std::vector<PerRecordStats> CodeFreq;
80 PerBlockIDStats() = default;
83 std::map<unsigned, PerBlockIDStats> BlockIDStats;
86 BitcodeAnalyzer(StringRef Buffer,
87 std::optional<StringRef> BlockInfoBuffer = std::nullopt);
88 /// Analyze the bitcode file.
89 Error analyze(std::optional<BCDumpOptions> O = std::nullopt,
90 std::optional<StringRef> CheckHash = std::nullopt);
91 /// Print stats about the bitcode file.
92 void printStats(BCDumpOptions O,
93 std::optional<StringRef> Filename = std::nullopt);
96 /// Read a block, updating statistics, etc.
97 Error parseBlock(unsigned BlockID, unsigned IndentLevel,
98 std::optional<BCDumpOptions> O = std::nullopt,
99 std::optional<StringRef> CheckHash = std::nullopt);
101 Error decodeMetadataStringsBlob(StringRef Indent, ArrayRef<uint64_t> Record,
102 StringRef Blob, raw_ostream &OS);
105 } // end namespace llvm
107 #endif // LLVM_BITCODE_BITCODEANALYZER_H