File indexing completed on 2026-05-10 08:43:22
0001
0002
0003
0004
0005
0006
0007
0008
0009
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
0029 enum CurStreamTypeType {
0030 UnknownBitstream,
0031 LLVMIRBitstream,
0032 ClangSerializedASTBitstream,
0033 ClangSerializedDiagnosticsBitstream,
0034 LLVMBitstreamRemarks
0035 };
0036
0037 struct BCDumpOptions {
0038
0039 raw_ostream &OS;
0040
0041 bool Histogram = false;
0042
0043 bool Symbolic = false;
0044
0045 bool ShowBinaryBlobs = false;
0046
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
0068 unsigned NumInstances = 0;
0069
0070 uint64_t NumBits = 0;
0071
0072 unsigned NumSubBlocks = 0;
0073
0074 unsigned NumAbbrevs = 0;
0075
0076
0077 unsigned NumRecords = 0, NumAbbreviatedRecords = 0;
0078
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
0089 Error analyze(std::optional<BCDumpOptions> O = std::nullopt,
0090 std::optional<StringRef> CheckHash = std::nullopt);
0091
0092 void printStats(BCDumpOptions O,
0093 std::optional<StringRef> Filename = std::nullopt);
0094
0095 private:
0096
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 }
0106
0107 #endif