LLVM  10.0.0svn
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_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"
20 #include "llvm/Support/Error.h"
22 #include <map>
23 #include <vector>
24 
25 namespace llvm {
26 
27 /// CurStreamTypeType - A type for CurStreamType
34 };
35 
36 struct BCDumpOptions {
37  /// The stream.
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 
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.
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,
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
bool ShowBinaryBlobs
Print binary blobs using hex escapes.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static Error parseBlock(T &ParserHelper, unsigned BlockID, const char *BlockName)
bool Symbolic
Don&#39;t emit numeric info in dump if symbolic info is available.
This represents a position within a bitcode file, implemented on top of a SimpleBitstreamCursor.
raw_ostream & OS
The stream.
This class maintains the abbreviations read from a block info block.
BCDumpOptions(raw_ostream &OS)
bool Histogram
Print per-code histogram.
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
CurStreamTypeType
CurStreamTypeType - A type for CurStreamType.