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/Compiler.h" 20 #include "llvm/Support/Error.h" 21 #include <map> 22 #include <optional> 23 #include <vector> 24 25 namespace llvm { 26 27 class raw_ostream; 28 29 /// CurStreamTypeType - A type for CurStreamType 30 enum CurStreamTypeType { 31 UnknownBitstream, 32 LLVMIRBitstream, 33 ClangSerializedASTBitstream, 34 ClangSerializedDiagnosticsBitstream, 35 LLVMBitstreamRemarks 36 }; 37 38 struct BCDumpOptions { 39 /// The stream. 40 raw_ostream &OS; 41 /// Print per-code histogram. 42 bool Histogram = false; 43 /// Don't emit numeric info in dump if symbolic info is available. 44 bool Symbolic = false; 45 /// Print binary blobs using hex escapes. 46 bool ShowBinaryBlobs = false; 47 /// Print BLOCKINFO block details. 48 bool DumpBlockinfo = false; 49 BCDumpOptionsBCDumpOptions50 BCDumpOptions(raw_ostream &OS) : OS(OS) {} 51 }; 52 53 class BitcodeAnalyzer { 54 BitstreamCursor Stream; 55 BitstreamBlockInfo BlockInfo; 56 CurStreamTypeType CurStreamType; 57 std::optional<BitstreamCursor> BlockInfoStream; 58 unsigned NumTopBlocks = 0; 59 60 struct PerRecordStats { 61 unsigned NumInstances = 0; 62 unsigned NumAbbrev = 0; 63 uint64_t TotalBits = 0; 64 PerRecordStats() = default; 65 }; 66 67 struct PerBlockIDStats { 68 /// NumInstances - This the number of times this block ID has been seen. 69 unsigned NumInstances = 0; 70 /// NumBits - The total size in bits of all of these blocks. 71 uint64_t NumBits = 0; 72 /// NumSubBlocks - The total number of blocks these blocks contain. 73 unsigned NumSubBlocks = 0; 74 /// NumAbbrevs - The total number of abbreviations. 75 unsigned NumAbbrevs = 0; 76 /// NumRecords - The total number of records these blocks contain, and the 77 /// number that are abbreviated. 78 unsigned NumRecords = 0, NumAbbreviatedRecords = 0; 79 /// CodeFreq - Keep track of the number of times we see each code. 80 std::vector<PerRecordStats> CodeFreq; 81 PerBlockIDStats() = default; 82 }; 83 84 std::map<unsigned, PerBlockIDStats> BlockIDStats; 85 86 public: 87 LLVM_ABI 88 BitcodeAnalyzer(StringRef Buffer, 89 std::optional<StringRef> BlockInfoBuffer = std::nullopt); 90 /// Analyze the bitcode file. 91 LLVM_ABI Error analyze(std::optional<BCDumpOptions> O = std::nullopt, 92 std::optional<StringRef> CheckHash = std::nullopt); 93 /// Print stats about the bitcode file. 94 LLVM_ABI 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