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_BITCODE_ANALYZER_H
14 #define LLVM_BITCODE_BITCODE_ANALYZER_H
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Bitstream/BitstreamReader.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/raw_ostream.h"
27 /// CurStreamTypeType - A type for CurStreamType
28 enum CurStreamTypeType
{
31 ClangSerializedASTBitstream
,
32 ClangSerializedDiagnosticsBitstream
,
35 struct BCDumpOptions
{
38 /// Print per-code histogram.
39 bool Histogram
= false;
40 /// Don't emit numeric info in dump if symbolic info is available.
41 bool Symbolic
= false;
42 /// Print binary blobs using hex escapes.
43 bool ShowBinaryBlobs
= false;
45 BCDumpOptions(raw_ostream
&OS
) : OS(OS
) {}
48 class BitcodeAnalyzer
{
49 BitstreamCursor Stream
;
50 BitstreamBlockInfo BlockInfo
;
51 CurStreamTypeType CurStreamType
;
52 Optional
<BitstreamCursor
> BlockInfoStream
;
53 unsigned NumTopBlocks
= 0;
55 struct PerRecordStats
{
56 unsigned NumInstances
;
59 PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
62 struct PerBlockIDStats
{
63 /// NumInstances - This the number of times this block ID has been seen.
64 unsigned NumInstances
;
65 /// NumBits - The total size in bits of all of these blocks.
67 /// NumSubBlocks - The total number of blocks these blocks contain.
68 unsigned NumSubBlocks
;
69 /// NumAbbrevs - The total number of abbreviations.
71 /// NumRecords - The total number of records these blocks contain, and the
72 /// number that are abbreviated.
73 unsigned NumRecords
, NumAbbreviatedRecords
;
74 /// CodeFreq - Keep track of the number of times we see each code.
75 std::vector
<PerRecordStats
> CodeFreq
;
77 : NumInstances(0), NumBits(0), NumSubBlocks(0), NumAbbrevs(0),
78 NumRecords(0), NumAbbreviatedRecords(0) {}
81 std::map
<unsigned, PerBlockIDStats
> BlockIDStats
;
84 BitcodeAnalyzer(StringRef Buffer
, Optional
<StringRef
> BlockInfoBuffer
= None
);
85 /// Analyze the bitcode file.
86 Error
analyze(Optional
<BCDumpOptions
> O
= None
,
87 Optional
<StringRef
> CheckHash
= None
);
88 /// Print stats about the bitcode file.
89 void printStats(BCDumpOptions O
, Optional
<StringRef
> Filename
= None
);
92 /// Read a block, updating statistics, etc.
93 Error
parseBlock(unsigned BlockID
, unsigned IndentLevel
,
94 Optional
<BCDumpOptions
> O
= None
,
95 Optional
<StringRef
> CheckHash
= None
);
97 Error
decodeMetadataStringsBlob(StringRef Indent
, ArrayRef
<uint64_t> Record
,
98 StringRef Blob
, raw_ostream
&OS
);
101 } // end namespace llvm
103 #endif // LLVM_BITCODE_BITCODE_ANALYZER_H