LLVM 19.0.0git
BitcodeAnalyzer.h
Go to the documentation of this file.
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"
19#include "llvm/Support/Error.h"
20#include <map>
21#include <optional>
22#include <vector>
23
24namespace llvm {
25
26class raw_ostream;
27
28/// CurStreamTypeType - A type for CurStreamType
35};
36
38 /// The stream.
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
50};
51
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 = 0;
61 unsigned NumAbbrev = 0;
62 uint64_t TotalBits = 0;
63 PerRecordStats() = default;
64 };
65
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.
70 uint64_t NumBits = 0;
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;
81 };
82
83 std::map<unsigned, PerBlockIDStats> BlockIDStats;
84
85public:
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.
93 std::optional<StringRef> Filename = std::nullopt);
94
95private:
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);
100
101 Error decodeMetadataStringsBlob(StringRef Indent, ArrayRef<uint64_t> Record,
102 StringRef Blob, raw_ostream &OS);
103};
104
105} // end namespace llvm
106
107#endif // LLVM_BITCODE_BITCODEANALYZER_H
raw_pwrite_stream & OS
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
Error analyze(std::optional< BCDumpOptions > O=std::nullopt, std::optional< StringRef > CheckHash=std::nullopt)
Analyze the bitcode file.
void printStats(BCDumpOptions O, std::optional< StringRef > Filename=std::nullopt)
Print stats about the bitcode file.
This class maintains the abbreviations read from a block info block.
This represents a position within a bitcode file, implemented on top of a SimpleBitstreamCursor.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:160
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
CurStreamTypeType
CurStreamTypeType - A type for CurStreamType.
@ UnknownBitstream
@ ClangSerializedDiagnosticsBitstream
@ LLVMIRBitstream
@ ClangSerializedASTBitstream
@ LLVMBitstreamRemarks
BCDumpOptions(raw_ostream &OS)
raw_ostream & OS
The stream.
bool Histogram
Print per-code histogram.
bool Symbolic
Don't emit numeric info in dump if symbolic info is available.
bool DumpBlockinfo
Print BLOCKINFO block details.
bool ShowBinaryBlobs
Print binary blobs using hex escapes.