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
,
36 struct BCDumpOptions
{
39 /// Print per-code histogram.
40 bool Histogram
= false;
41 /// Don't emit numeric info in dump if symbolic info is available.
42 bool Symbolic
= false;
43 /// Print binary blobs using hex escapes.
44 bool ShowBinaryBlobs
= false;
46 BCDumpOptions(raw_ostream
&OS
) : OS(OS
) {}
49 class BitcodeAnalyzer
{
50 BitstreamCursor Stream
;
51 BitstreamBlockInfo BlockInfo
;
52 CurStreamTypeType CurStreamType
;
53 Optional
<BitstreamCursor
> BlockInfoStream
;
54 unsigned NumTopBlocks
= 0;
56 struct PerRecordStats
{
57 unsigned NumInstances
;
60 PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
63 struct PerBlockIDStats
{
64 /// NumInstances - This the number of times this block ID has been seen.
65 unsigned NumInstances
;
66 /// NumBits - The total size in bits of all of these blocks.
68 /// NumSubBlocks - The total number of blocks these blocks contain.
69 unsigned NumSubBlocks
;
70 /// NumAbbrevs - The total number of abbreviations.
72 /// NumRecords - The total number of records these blocks contain, and the
73 /// number that are abbreviated.
74 unsigned NumRecords
, NumAbbreviatedRecords
;
75 /// CodeFreq - Keep track of the number of times we see each code.
76 std::vector
<PerRecordStats
> CodeFreq
;
78 : NumInstances(0), NumBits(0), NumSubBlocks(0), NumAbbrevs(0),
79 NumRecords(0), NumAbbreviatedRecords(0) {}
82 std::map
<unsigned, PerBlockIDStats
> BlockIDStats
;
85 BitcodeAnalyzer(StringRef Buffer
, Optional
<StringRef
> BlockInfoBuffer
= None
);
86 /// Analyze the bitcode file.
87 Error
analyze(Optional
<BCDumpOptions
> O
= None
,
88 Optional
<StringRef
> CheckHash
= None
);
89 /// Print stats about the bitcode file.
90 void printStats(BCDumpOptions O
, Optional
<StringRef
> Filename
= None
);
93 /// Read a block, updating statistics, etc.
94 Error
parseBlock(unsigned BlockID
, unsigned IndentLevel
,
95 Optional
<BCDumpOptions
> O
= None
,
96 Optional
<StringRef
> CheckHash
= None
);
98 Error
decodeMetadataStringsBlob(StringRef Indent
, ArrayRef
<uint64_t> Record
,
99 StringRef Blob
, raw_ostream
&OS
);
102 } // end namespace llvm
104 #endif // LLVM_BITCODE_BITCODE_ANALYZER_H