]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Bitcode/BitcodeAnalyzer.h
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Bitcode / BitcodeAnalyzer.h
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,
33 };
34
35 struct BCDumpOptions {
36   /// The stream.
37   raw_ostream &OS;
38   /// Print per-code histogram.
39   bool Histogram = false;
40   /// Don't emit numeric info in dump if symbolic info is available.
41   bool Symbolic = false;
42   /// Print binary blobs using hex escapes.
43   bool ShowBinaryBlobs = false;
44
45   BCDumpOptions(raw_ostream &OS) : OS(OS) {}
46 };
47
48 class BitcodeAnalyzer {
49   BitstreamCursor Stream;
50   BitstreamBlockInfo BlockInfo;
51   CurStreamTypeType CurStreamType;
52   Optional<BitstreamCursor> BlockInfoStream;
53   unsigned NumTopBlocks = 0;
54
55   struct PerRecordStats {
56     unsigned NumInstances;
57     unsigned NumAbbrev;
58     uint64_t TotalBits;
59     PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
60   };
61
62   struct PerBlockIDStats {
63     /// NumInstances - This the number of times this block ID has been seen.
64     unsigned NumInstances;
65     /// NumBits - The total size in bits of all of these blocks.
66     uint64_t NumBits;
67     /// NumSubBlocks - The total number of blocks these blocks contain.
68     unsigned NumSubBlocks;
69     /// NumAbbrevs - The total number of abbreviations.
70     unsigned NumAbbrevs;
71     /// NumRecords - The total number of records these blocks contain, and the
72     /// number that are abbreviated.
73     unsigned NumRecords, NumAbbreviatedRecords;
74     /// CodeFreq - Keep track of the number of times we see each code.
75     std::vector<PerRecordStats> CodeFreq;
76     PerBlockIDStats()
77         : NumInstances(0), NumBits(0), NumSubBlocks(0), NumAbbrevs(0),
78           NumRecords(0), NumAbbreviatedRecords(0) {}
79   };
80
81   std::map<unsigned, PerBlockIDStats> BlockIDStats;
82
83 public:
84   BitcodeAnalyzer(StringRef Buffer, Optional<StringRef> BlockInfoBuffer = None);
85   /// Analyze the bitcode file.
86   Error analyze(Optional<BCDumpOptions> O = None,
87                 Optional<StringRef> CheckHash = None);
88   /// Print stats about the bitcode file.
89   void printStats(BCDumpOptions O, Optional<StringRef> Filename = None);
90
91 private:
92   /// Read a block, updating statistics, etc.
93   Error parseBlock(unsigned BlockID, unsigned IndentLevel,
94                    Optional<BCDumpOptions> O = None,
95                    Optional<StringRef> CheckHash = None);
96
97   Error decodeMetadataStringsBlob(StringRef Indent, ArrayRef<uint64_t> Record,
98                                   StringRef Blob, raw_ostream &OS);
99 };
100
101 } // end namespace llvm
102
103 #endif // LLVM_BITCODE_BITCODE_ANALYZER_H