BitcodeAnalyzer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/Bitcode/BitcodeAnalyzer.h - Bitcode analyzer --------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This header defines interfaces to analyze LLVM bitcode files/streams.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_BITCODE_BITCODE_ANALYZER_H
  18. #define LLVM_BITCODE_BITCODE_ANALYZER_H
  19. #include "llvm/ADT/ArrayRef.h"
  20. #include "llvm/ADT/Optional.h"
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/Bitstream/BitstreamReader.h"
  23. #include "llvm/Support/Error.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include <map>
  26. #include <vector>
  27. namespace llvm {
  28. /// CurStreamTypeType - A type for CurStreamType
  29. enum CurStreamTypeType {
  30. UnknownBitstream,
  31. LLVMIRBitstream,
  32. ClangSerializedASTBitstream,
  33. ClangSerializedDiagnosticsBitstream,
  34. LLVMBitstreamRemarks
  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. BCDumpOptions(raw_ostream &OS) : OS(OS) {}
  46. };
  47. class BitcodeAnalyzer {
  48. BitstreamCursor Stream;
  49. BitstreamBlockInfo BlockInfo;
  50. CurStreamTypeType CurStreamType;
  51. Optional<BitstreamCursor> BlockInfoStream;
  52. unsigned NumTopBlocks = 0;
  53. struct PerRecordStats {
  54. unsigned NumInstances;
  55. unsigned NumAbbrev;
  56. uint64_t TotalBits;
  57. PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
  58. };
  59. struct PerBlockIDStats {
  60. /// NumInstances - This the number of times this block ID has been seen.
  61. unsigned NumInstances;
  62. /// NumBits - The total size in bits of all of these blocks.
  63. uint64_t NumBits;
  64. /// NumSubBlocks - The total number of blocks these blocks contain.
  65. unsigned NumSubBlocks;
  66. /// NumAbbrevs - The total number of abbreviations.
  67. unsigned NumAbbrevs;
  68. /// NumRecords - The total number of records these blocks contain, and the
  69. /// number that are abbreviated.
  70. unsigned NumRecords, NumAbbreviatedRecords;
  71. /// CodeFreq - Keep track of the number of times we see each code.
  72. std::vector<PerRecordStats> CodeFreq;
  73. PerBlockIDStats()
  74. : NumInstances(0), NumBits(0), NumSubBlocks(0), NumAbbrevs(0),
  75. NumRecords(0), NumAbbreviatedRecords(0) {}
  76. };
  77. std::map<unsigned, PerBlockIDStats> BlockIDStats;
  78. public:
  79. BitcodeAnalyzer(StringRef Buffer, Optional<StringRef> BlockInfoBuffer = None);
  80. /// Analyze the bitcode file.
  81. Error analyze(Optional<BCDumpOptions> O = None,
  82. Optional<StringRef> CheckHash = None);
  83. /// Print stats about the bitcode file.
  84. void printStats(BCDumpOptions O, Optional<StringRef> Filename = None);
  85. private:
  86. /// Read a block, updating statistics, etc.
  87. Error parseBlock(unsigned BlockID, unsigned IndentLevel,
  88. Optional<BCDumpOptions> O = None,
  89. Optional<StringRef> CheckHash = None);
  90. Error decodeMetadataStringsBlob(StringRef Indent, ArrayRef<uint64_t> Record,
  91. StringRef Blob, raw_ostream &OS);
  92. };
  93. } // end namespace llvm
  94. #endif // LLVM_BITCODE_BITCODE_ANALYZER_H
  95. #ifdef __GNUC__
  96. #pragma GCC diagnostic pop
  97. #endif