diff options
Diffstat (limited to 'llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp')
| -rw-r--r-- | llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp | 88 |
1 files changed, 41 insertions, 47 deletions
diff --git a/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp b/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp index fec11e35d50..e4ca893cc2b 100644 --- a/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp +++ b/llvm/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp @@ -84,7 +84,7 @@ enum CurStreamTypeType { /// GetBlockName - Return a symbolic block name if known, otherwise return /// null. static const char *GetBlockName(unsigned BlockID, - const BitstreamReader &StreamFile, + const BitstreamBlockInfo &BlockInfo, CurStreamTypeType CurStreamType) { // Standard blocks for all bitcode files. if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) { @@ -94,8 +94,8 @@ static const char *GetBlockName(unsigned BlockID, } // Check to see if we have a blockinfo record for this block, with a name. - if (const BitstreamReader::BlockInfo *Info = - StreamFile.getBlockInfo(BlockID)) { + if (const BitstreamBlockInfo::BlockInfo *Info = + BlockInfo.getBlockInfo(BlockID)) { if (!Info->Name.empty()) return Info->Name.c_str(); } @@ -128,7 +128,7 @@ static const char *GetBlockName(unsigned BlockID, /// GetCodeName - Return a symbolic code name if known, otherwise return /// null. static const char *GetCodeName(unsigned CodeID, unsigned BlockID, - const BitstreamReader &StreamFile, + const BitstreamBlockInfo &BlockInfo, CurStreamTypeType CurStreamType) { // Standard blocks for all bitcode files. if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) { @@ -144,8 +144,8 @@ static const char *GetCodeName(unsigned CodeID, unsigned BlockID, } // Check to see if we have a blockinfo record for this record, with a name. - if (const BitstreamReader::BlockInfo *Info = - StreamFile.getBlockInfo(BlockID)) { + if (const BitstreamBlockInfo::BlockInfo *Info = + BlockInfo.getBlockInfo(BlockID)) { for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i) if (Info->RecordNames[i].first == CodeID) return Info->RecordNames[i].second.c_str(); @@ -419,7 +419,7 @@ static bool ReportError(const Twine &Err) { return true; } -static bool decodeMetadataStringsBlob(BitstreamReader &Reader, StringRef Indent, +static bool decodeMetadataStringsBlob(StringRef Indent, ArrayRef<uint64_t> Record, StringRef Blob) { if (Blob.empty()) @@ -433,9 +433,7 @@ static bool decodeMetadataStringsBlob(BitstreamReader &Reader, StringRef Indent, outs() << " num-strings = " << NumStrings << " {\n"; StringRef Lengths = Blob.slice(0, StringsOffset); - SimpleBitstreamCursor R(Reader); - R.jumpToPointer(Lengths.begin()); - + SimpleBitstreamCursor R(Lengths); StringRef Strings = Blob.drop_front(StringsOffset); do { if (R.AtEndOfStream()) @@ -455,20 +453,20 @@ static bool decodeMetadataStringsBlob(BitstreamReader &Reader, StringRef Indent, return false; } -static bool decodeBlob(unsigned Code, unsigned BlockID, BitstreamReader &Reader, - StringRef Indent, ArrayRef<uint64_t> Record, - StringRef Blob) { +static bool decodeBlob(unsigned Code, unsigned BlockID, StringRef Indent, + ArrayRef<uint64_t> Record, StringRef Blob) { if (BlockID != bitc::METADATA_BLOCK_ID) return true; if (Code != bitc::METADATA_STRINGS) return true; - return decodeMetadataStringsBlob(Reader, Indent, Record, Blob); + return decodeMetadataStringsBlob(Indent, Record, Blob); } /// ParseBlock - Read a block, updating statistics, etc. -static bool ParseBlock(BitstreamCursor &Stream, unsigned BlockID, - unsigned IndentLevel, CurStreamTypeType CurStreamType) { +static bool ParseBlock(BitstreamCursor &Stream, BitstreamBlockInfo &BlockInfo, + unsigned BlockID, unsigned IndentLevel, + CurStreamTypeType CurStreamType) { std::string Indent(IndentLevel*2, ' '); uint64_t BlockBitStart = Stream.GetCurrentBitNo(); @@ -481,8 +479,12 @@ static bool ParseBlock(BitstreamCursor &Stream, unsigned BlockID, bool DumpRecords = Dump; if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { if (Dump) outs() << Indent << "<BLOCKINFO_BLOCK/>\n"; - if (BitstreamCursor(Stream).ReadBlockInfoBlock()) + Optional<BitstreamBlockInfo> NewBlockInfo = + Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true); + if (!NewBlockInfo) return ReportError("Malformed BlockInfoBlock"); + BlockInfo = std::move(*NewBlockInfo); + Stream.JumpToBit(BlockBitStart); // It's not really interesting to dump the contents of the blockinfo block. DumpRecords = false; } @@ -497,8 +499,7 @@ static bool ParseBlock(BitstreamCursor &Stream, unsigned BlockID, const char *BlockName = nullptr; if (DumpRecords) { outs() << Indent << "<"; - if ((BlockName = GetBlockName(BlockID, *Stream.getBitStreamReader(), - CurStreamType))) + if ((BlockName = GetBlockName(BlockID, BlockInfo, CurStreamType))) outs() << BlockName; else outs() << "UnknownBlock" << BlockID; @@ -540,7 +541,8 @@ static bool ParseBlock(BitstreamCursor &Stream, unsigned BlockID, case BitstreamEntry::SubBlock: { uint64_t SubBlockBitStart = Stream.GetCurrentBitNo(); - if (ParseBlock(Stream, Entry.ID, IndentLevel+1, CurStreamType)) + if (ParseBlock(Stream, BlockInfo, Entry.ID, IndentLevel + 1, + CurStreamType)) return true; ++BlockStats.NumSubBlocks; uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo(); @@ -582,14 +584,11 @@ static bool ParseBlock(BitstreamCursor &Stream, unsigned BlockID, if (DumpRecords) { outs() << Indent << " <"; if (const char *CodeName = - GetCodeName(Code, BlockID, *Stream.getBitStreamReader(), - CurStreamType)) + GetCodeName(Code, BlockID, BlockInfo, CurStreamType)) outs() << CodeName; else outs() << "UnknownCode" << Code; - if (NonSymbolic && - GetCodeName(Code, BlockID, *Stream.getBitStreamReader(), - CurStreamType)) + if (NonSymbolic && GetCodeName(Code, BlockID, BlockInfo, CurStreamType)) outs() << " codeid=" << Code; const BitCodeAbbrev *Abbv = nullptr; if (Entry.ID != bitc::UNABBREV_RECORD) { @@ -654,8 +653,7 @@ static bool ParseBlock(BitstreamCursor &Stream, unsigned BlockID, } } - if (Blob.data() && decodeBlob(Code, BlockID, *Stream.getBitStreamReader(), - Indent, Record, Blob)) { + if (Blob.data() && decodeBlob(Code, BlockID, Indent, Record, Blob)) { outs() << " blob data = "; if (ShowBinaryBlobs) { outs() << "'"; @@ -690,7 +688,6 @@ static void PrintSize(uint64_t Bits) { static bool openBitcodeFile(StringRef Path, std::unique_ptr<MemoryBuffer> &MemBuf, - BitstreamReader &StreamFile, BitstreamCursor &Stream, CurStreamTypeType &CurStreamType) { // Read the input file. @@ -731,9 +728,7 @@ static bool openBitcodeFile(StringRef Path, return ReportError("Invalid bitcode wrapper header"); } - StreamFile = BitstreamReader(ArrayRef<uint8_t>(BufPtr, EndBufPtr)); - Stream = BitstreamCursor(StreamFile); - StreamFile.CollectBlockInfoNames(); + Stream = BitstreamCursor(ArrayRef<uint8_t>(BufPtr, EndBufPtr)); // Read the stream signature. char Signature[6]; @@ -757,22 +752,21 @@ static bool openBitcodeFile(StringRef Path, /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename. static int AnalyzeBitcode() { std::unique_ptr<MemoryBuffer> StreamBuffer; - BitstreamReader StreamFile; BitstreamCursor Stream; + BitstreamBlockInfo BlockInfo; CurStreamTypeType CurStreamType; - if (openBitcodeFile(InputFilename, StreamBuffer, StreamFile, Stream, - CurStreamType)) + if (openBitcodeFile(InputFilename, StreamBuffer, Stream, CurStreamType)) return true; + Stream.setBlockInfo(&BlockInfo); // Read block info from BlockInfoFilename, if specified. // The block info must be a top-level block. if (!BlockInfoFilename.empty()) { std::unique_ptr<MemoryBuffer> BlockInfoBuffer; - BitstreamReader BlockInfoFile; BitstreamCursor BlockInfoCursor; CurStreamTypeType BlockInfoStreamType; - if (openBitcodeFile(BlockInfoFilename, BlockInfoBuffer, BlockInfoFile, - BlockInfoCursor, BlockInfoStreamType)) + if (openBitcodeFile(BlockInfoFilename, BlockInfoBuffer, BlockInfoCursor, + BlockInfoStreamType)) return true; while (!BlockInfoCursor.AtEndOfStream()) { @@ -782,15 +776,16 @@ static int AnalyzeBitcode() { unsigned BlockID = BlockInfoCursor.ReadSubBlockID(); if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { - if (BlockInfoCursor.ReadBlockInfoBlock()) + Optional<BitstreamBlockInfo> NewBlockInfo = + BlockInfoCursor.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true); + if (!NewBlockInfo) return ReportError("Malformed BlockInfoBlock in block info file"); + BlockInfo = std::move(*NewBlockInfo); break; } BlockInfoCursor.SkipBlock(); } - - StreamFile.takeBlockInfo(std::move(BlockInfoFile)); } unsigned NumTopBlocks = 0; @@ -803,14 +798,14 @@ static int AnalyzeBitcode() { unsigned BlockID = Stream.ReadSubBlockID(); - if (ParseBlock(Stream, BlockID, 0, CurStreamType)) + if (ParseBlock(Stream, BlockInfo, BlockID, 0, CurStreamType)) return true; ++NumTopBlocks; } if (Dump) outs() << "\n\n"; - uint64_t BufferSizeBits = StreamFile.getBitcodeBytes().size() * CHAR_BIT; + uint64_t BufferSizeBits = Stream.getBitcodeBytes().size() * CHAR_BIT; // Print a summary of the read file. outs() << "Summary of " << InputFilename << ":\n"; outs() << " Total size: "; @@ -829,8 +824,8 @@ static int AnalyzeBitcode() { for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(), E = BlockIDStats.end(); I != E; ++I) { outs() << " Block ID #" << I->first; - if (const char *BlockName = GetBlockName(I->first, StreamFile, - CurStreamType)) + if (const char *BlockName = + GetBlockName(I->first, BlockInfo, CurStreamType)) outs() << " (" << BlockName << ")"; outs() << ":\n"; @@ -894,9 +889,8 @@ static int AnalyzeBitcode() { outs() << " "; outs() << " "; - if (const char *CodeName = - GetCodeName(FreqPairs[i].second, I->first, StreamFile, - CurStreamType)) + if (const char *CodeName = GetCodeName(FreqPairs[i].second, I->first, + BlockInfo, CurStreamType)) outs() << CodeName << "\n"; else outs() << "UnknownCode" << FreqPairs[i].second << "\n"; |

