xref: /openbsd-src/gnu/llvm/llvm/include/llvm/Bitcode/BitcodeAnalyzer.h (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
1 //===- llvm/Bitcode/BitcodeAnalyzer.h - Bitcode analyzer --------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This header defines interfaces to analyze LLVM bitcode files/streams.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_BITCODE_BITCODEANALYZER_H
14 #define LLVM_BITCODE_BITCODEANALYZER_H
15 
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Bitstream/BitstreamReader.h"
19 #include "llvm/Support/Error.h"
20 #include <map>
21 #include <optional>
22 #include <vector>
23 
24 namespace llvm {
25 
26 class raw_ostream;
27 
28 /// CurStreamTypeType - A type for CurStreamType
29 enum CurStreamTypeType {
30   UnknownBitstream,
31   LLVMIRBitstream,
32   ClangSerializedASTBitstream,
33   ClangSerializedDiagnosticsBitstream,
34   LLVMBitstreamRemarks
35 };
36 
37 struct BCDumpOptions {
38   /// The stream.
39   raw_ostream &OS;
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;
48 
BCDumpOptionsBCDumpOptions49   BCDumpOptions(raw_ostream &OS) : OS(OS) {}
50 };
51 
52 class BitcodeAnalyzer {
53   BitstreamCursor Stream;
54   BitstreamBlockInfo BlockInfo;
55   CurStreamTypeType CurStreamType;
56   std::optional<BitstreamCursor> BlockInfoStream;
57   unsigned NumTopBlocks = 0;
58 
59   struct PerRecordStats {
60     unsigned NumInstances;
61     unsigned NumAbbrev;
62     uint64_t TotalBits;
PerRecordStatsPerRecordStats63     PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
64   };
65 
66   struct PerBlockIDStats {
67     /// NumInstances - This the number of times this block ID has been seen.
68     unsigned NumInstances;
69     /// NumBits - The total size in bits of all of these blocks.
70     uint64_t NumBits;
71     /// NumSubBlocks - The total number of blocks these blocks contain.
72     unsigned NumSubBlocks;
73     /// NumAbbrevs - The total number of abbreviations.
74     unsigned NumAbbrevs;
75     /// NumRecords - The total number of records these blocks contain, and the
76     /// number that are abbreviated.
77     unsigned NumRecords, NumAbbreviatedRecords;
78     /// CodeFreq - Keep track of the number of times we see each code.
79     std::vector<PerRecordStats> CodeFreq;
PerBlockIDStatsPerBlockIDStats80     PerBlockIDStats()
81         : NumInstances(0), NumBits(0), NumSubBlocks(0), NumAbbrevs(0),
82           NumRecords(0), NumAbbreviatedRecords(0) {}
83   };
84 
85   std::map<unsigned, PerBlockIDStats> BlockIDStats;
86 
87 public:
88   BitcodeAnalyzer(StringRef Buffer,
89                   std::optional<StringRef> BlockInfoBuffer = std::nullopt);
90   /// Analyze the bitcode file.
91   Error analyze(std::optional<BCDumpOptions> O = std::nullopt,
92                 std::optional<StringRef> CheckHash = std::nullopt);
93   /// Print stats about the bitcode file.
94   void printStats(BCDumpOptions O,
95                   std::optional<StringRef> Filename = std::nullopt);
96 
97 private:
98   /// Read a block, updating statistics, etc.
99   Error parseBlock(unsigned BlockID, unsigned IndentLevel,
100                    std::optional<BCDumpOptions> O = std::nullopt,
101                    std::optional<StringRef> CheckHash = std::nullopt);
102 
103   Error decodeMetadataStringsBlob(StringRef Indent, ArrayRef<uint64_t> Record,
104                                   StringRef Blob, raw_ostream &OS);
105 };
106 
107 } // end namespace llvm
108 
109 #endif // LLVM_BITCODE_BITCODEANALYZER_H
110