LLVM  15.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/Optional.h"
18 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/Error.h"
21 #include <map>
22 #include <vector>
23 
24 namespace llvm {
25 
26 class raw_ostream;
27 
28 /// CurStreamTypeType - A type for CurStreamType
35 };
36 
37 struct BCDumpOptions {
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  Optional<BitstreamCursor> BlockInfoStream;
57  unsigned NumTopBlocks = 0;
58 
59  struct PerRecordStats {
60  unsigned NumInstances;
61  unsigned NumAbbrev;
62  uint64_t TotalBits;
63  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;
80  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, Optional<StringRef> BlockInfoBuffer = None);
89  /// Analyze the bitcode file.
91  Optional<StringRef> CheckHash = None);
92  /// Print stats about the bitcode file.
94 
95 private:
96  /// Read a block, updating statistics, etc.
97  Error parseBlock(unsigned BlockID, unsigned IndentLevel,
99  Optional<StringRef> CheckHash = None);
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
llvm::BitcodeAnalyzer::BitcodeAnalyzer
BitcodeAnalyzer(StringRef Buffer, Optional< StringRef > BlockInfoBuffer=None)
Definition: BitcodeAnalyzer.cpp:550
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
Optional.h
StringRef.h
llvm::BCDumpOptions::OS
raw_ostream & OS
The stream.
Definition: BitcodeAnalyzer.h:39
llvm::BitstreamBlockInfo
This class maintains the abbreviations read from a block info block.
Definition: BitstreamReader.h:36
llvm::BCDumpOptions::BCDumpOptions
BCDumpOptions(raw_ostream &OS)
Definition: BitcodeAnalyzer.h:49
llvm::ClangSerializedDiagnosticsBitstream
@ ClangSerializedDiagnosticsBitstream
Definition: BitcodeAnalyzer.h:33
Error.h
llvm::UnknownBitstream
@ UnknownBitstream
Definition: BitcodeAnalyzer.h:30
llvm::BitstreamCursor
This represents a position within a bitcode file, implemented on top of a SimpleBitstreamCursor.
Definition: BitstreamReader.h:358
llvm::BitcodeAnalyzer
Definition: BitcodeAnalyzer.h:52
llvm::Optional
Definition: APInt.h:33
llvm::BCDumpOptions::Histogram
bool Histogram
Print per-code histogram.
Definition: BitcodeAnalyzer.h:41
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:54
llvm::None
const NoneType None
Definition: None.h:24
BitstreamReader.h
llvm::BCDumpOptions
Definition: BitcodeAnalyzer.h:37
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:239
uint64_t
llvm::BCDumpOptions::ShowBinaryBlobs
bool ShowBinaryBlobs
Print binary blobs using hex escapes.
Definition: BitcodeAnalyzer.h:45
ArrayRef.h
llvm::BCDumpOptions::DumpBlockinfo
bool DumpBlockinfo
Print BLOCKINFO block details.
Definition: BitcodeAnalyzer.h:47
llvm::Record
Definition: Record.h:1508
llvm::ClangSerializedASTBitstream
@ ClangSerializedASTBitstream
Definition: BitcodeAnalyzer.h:32
llvm::BitcodeAnalyzer::analyze
Error analyze(Optional< BCDumpOptions > O=None, Optional< StringRef > CheckHash=None)
Analyze the bitcode file.
Definition: BitcodeAnalyzer.cpp:557
llvm::ArrayRef< uint64_t >
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::LLVMIRBitstream
@ LLVMIRBitstream
Definition: BitcodeAnalyzer.h:31
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::CurStreamTypeType
CurStreamTypeType
CurStreamTypeType - A type for CurStreamType.
Definition: BitcodeAnalyzer.h:29
llvm::BCDumpOptions::Symbolic
bool Symbolic
Don't emit numeric info in dump if symbolic info is available.
Definition: BitcodeAnalyzer.h:43
llvm::BitcodeAnalyzer::printStats
void printStats(BCDumpOptions O, Optional< StringRef > Filename=None)
Print stats about the bitcode file.
Definition: BitcodeAnalyzer.cpp:618
llvm::LLVMBitstreamRemarks
@ LLVMBitstreamRemarks
Definition: BitcodeAnalyzer.h:34