BitcodeAnalyzer.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_BITCODEANALYZER_H
  18. #define LLVM_BITCODE_BITCODEANALYZER_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. /// Print BLOCKINFO block details.
  46. bool DumpBlockinfo = false;
  47. BCDumpOptions(raw_ostream &OS) : OS(OS) {}
  48. };
  49. class BitcodeAnalyzer {
  50. BitstreamCursor Stream;
  51. BitstreamBlockInfo BlockInfo;
  52. CurStreamTypeType CurStreamType;
  53. Optional<BitstreamCursor> BlockInfoStream;
  54. unsigned NumTopBlocks = 0;
  55. struct PerRecordStats {
  56. unsigned NumInstances;
  57. unsigned NumAbbrev;
  58. uint64_t TotalBits;
  59. PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
  60. };
  61. struct PerBlockIDStats {
  62. /// NumInstances - This the number of times this block ID has been seen.
  63. unsigned NumInstances;
  64. /// NumBits - The total size in bits of all of these blocks.
  65. uint64_t NumBits;
  66. /// NumSubBlocks - The total number of blocks these blocks contain.
  67. unsigned NumSubBlocks;
  68. /// NumAbbrevs - The total number of abbreviations.
  69. unsigned NumAbbrevs;
  70. /// NumRecords - The total number of records these blocks contain, and the
  71. /// number that are abbreviated.
  72. unsigned NumRecords, NumAbbreviatedRecords;
  73. /// CodeFreq - Keep track of the number of times we see each code.
  74. std::vector<PerRecordStats> CodeFreq;
  75. PerBlockIDStats()
  76. : NumInstances(0), NumBits(0), NumSubBlocks(0), NumAbbrevs(0),
  77. NumRecords(0), NumAbbreviatedRecords(0) {}
  78. };
  79. std::map<unsigned, PerBlockIDStats> BlockIDStats;
  80. public:
  81. BitcodeAnalyzer(StringRef Buffer, Optional<StringRef> BlockInfoBuffer = None);
  82. /// Analyze the bitcode file.
  83. Error analyze(Optional<BCDumpOptions> O = None,
  84. Optional<StringRef> CheckHash = None);
  85. /// Print stats about the bitcode file.
  86. void printStats(BCDumpOptions O, Optional<StringRef> Filename = None);
  87. private:
  88. /// Read a block, updating statistics, etc.
  89. Error parseBlock(unsigned BlockID, unsigned IndentLevel,
  90. Optional<BCDumpOptions> O = None,
  91. Optional<StringRef> CheckHash = None);
  92. Error decodeMetadataStringsBlob(StringRef Indent, ArrayRef<uint64_t> Record,
  93. StringRef Blob, raw_ostream &OS);
  94. };
  95. } // end namespace llvm
  96. #endif // LLVM_BITCODE_BITCODEANALYZER_H
  97. #ifdef __GNUC__
  98. #pragma GCC diagnostic pop
  99. #endif