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