BitcodeAnalyzer.h 3.4 KB

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