Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 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_BITCODE_ANALYZER_H |
| 14 | #define LLVM_BITCODE_BITCODE_ANALYZER_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, |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 33 | LLVMBitstreamRemarks |
Andrew Walbran | 3d2c197 | 2020-04-07 12:24:26 +0100 | [diff] [blame] | 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 | |
| 46 | BCDumpOptions(raw_ostream &OS) : OS(OS) {} |
| 47 | }; |
| 48 | |
| 49 | class BitcodeAnalyzer { |
| 50 | BitstreamCursor Stream; |
| 51 | BitstreamBlockInfo BlockInfo; |
| 52 | CurStreamTypeType CurStreamType; |
| 53 | Optional<BitstreamCursor> BlockInfoStream; |
| 54 | unsigned NumTopBlocks = 0; |
| 55 | |
| 56 | struct PerRecordStats { |
| 57 | unsigned NumInstances; |
| 58 | unsigned NumAbbrev; |
| 59 | uint64_t TotalBits; |
| 60 | PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {} |
| 61 | }; |
| 62 | |
| 63 | struct PerBlockIDStats { |
| 64 | /// NumInstances - This the number of times this block ID has been seen. |
| 65 | unsigned NumInstances; |
| 66 | /// NumBits - The total size in bits of all of these blocks. |
| 67 | uint64_t NumBits; |
| 68 | /// NumSubBlocks - The total number of blocks these blocks contain. |
| 69 | unsigned NumSubBlocks; |
| 70 | /// NumAbbrevs - The total number of abbreviations. |
| 71 | unsigned NumAbbrevs; |
| 72 | /// NumRecords - The total number of records these blocks contain, and the |
| 73 | /// number that are abbreviated. |
| 74 | unsigned NumRecords, NumAbbreviatedRecords; |
| 75 | /// CodeFreq - Keep track of the number of times we see each code. |
| 76 | std::vector<PerRecordStats> CodeFreq; |
| 77 | PerBlockIDStats() |
| 78 | : NumInstances(0), NumBits(0), NumSubBlocks(0), NumAbbrevs(0), |
| 79 | NumRecords(0), NumAbbreviatedRecords(0) {} |
| 80 | }; |
| 81 | |
| 82 | std::map<unsigned, PerBlockIDStats> BlockIDStats; |
| 83 | |
| 84 | public: |
| 85 | BitcodeAnalyzer(StringRef Buffer, Optional<StringRef> BlockInfoBuffer = None); |
| 86 | /// Analyze the bitcode file. |
| 87 | Error analyze(Optional<BCDumpOptions> O = None, |
| 88 | Optional<StringRef> CheckHash = None); |
| 89 | /// Print stats about the bitcode file. |
| 90 | void printStats(BCDumpOptions O, Optional<StringRef> Filename = None); |
| 91 | |
| 92 | private: |
| 93 | /// Read a block, updating statistics, etc. |
| 94 | Error parseBlock(unsigned BlockID, unsigned IndentLevel, |
| 95 | Optional<BCDumpOptions> O = None, |
| 96 | Optional<StringRef> CheckHash = None); |
| 97 | |
| 98 | Error decodeMetadataStringsBlob(StringRef Indent, ArrayRef<uint64_t> Record, |
| 99 | StringRef Blob, raw_ostream &OS); |
| 100 | }; |
| 101 | |
| 102 | } // end namespace llvm |
| 103 | |
| 104 | #endif // LLVM_BITCODE_BITCODE_ANALYZER_H |