30 return "BLOCKINFO_BLOCK";
37 if (!
Info->Name.empty())
38 return Info->Name.c_str();
48 return "OPERAND_BUNDLE_TAGS_BLOCK";
50 return "MODULE_BLOCK";
52 return "PARAMATTR_BLOCK";
54 return "PARAMATTR_GROUP_BLOCK_ID";
56 return "TYPE_BLOCK_ID";
58 return "CONSTANTS_BLOCK";
60 return "FUNCTION_BLOCK";
62 return "IDENTIFICATION_BLOCK_ID";
64 return "VALUE_SYMTAB";
66 return "METADATA_BLOCK";
68 return "METADATA_KIND_BLOCK";
70 return "METADATA_ATTACHMENT_BLOCK";
72 return "USELIST_BLOCK_ID";
74 return "GLOBALVAL_SUMMARY_BLOCK";
76 return "FULL_LTO_GLOBALVAL_SUMMARY_BLOCK";
78 return "MODULE_STRTAB_BLOCK";
80 return "STRTAB_BLOCK";
82 return "SYMTAB_BLOCK";
101 return "SETRECORDNAME";
110 for (
unsigned i = 0,
e =
Info->RecordNames.size(); i !=
e; ++i)
111 if (
Info->RecordNames[i].first == CodeID)
112 return Info->RecordNames[i].second.c_str();
118 #define STRINGIFY_CODE(PREFIX, CODE) \ 119 case bitc::PREFIX##_##CODE: \ 217 return "CST_CODE_BLOCKADDRESS";
369 return "USELIST_CODE_DEFAULT";
371 return "USELIST_CODE_BB";
379 return "OPERAND_BUNDLE_TAG";
396 #undef STRINGIFY_CODE 400 OS <<
format(
"%.2f/%.2fB/%luW", Bits, Bits / 8, (
unsigned long)(Bits / 32));
403 OS <<
format(
"%lub/%.2fB/%luW", (
unsigned long)Bits, (
double)Bits / 8,
404 (
unsigned long)(Bits / 32));
408 auto tryRead = [&Stream](
char &Dest,
size_t size) ->
Error {
410 Dest = MaybeWord.get();
412 return MaybeWord.takeError();
417 if (
Error Err = tryRead(Signature[0], 8))
418 return std::move(Err);
419 if (
Error Err = tryRead(Signature[1], 8))
420 return std::move(Err);
423 if (Signature[0] ==
'C' && Signature[1] ==
'P') {
424 if (
Error Err = tryRead(Signature[2], 8))
425 return std::move(Err);
426 if (
Error Err = tryRead(Signature[3], 8))
427 return std::move(Err);
428 if (Signature[2] ==
'C' && Signature[3] ==
'H')
430 }
else if (Signature[0] ==
'D' && Signature[1] ==
'I') {
431 if (
Error Err = tryRead(Signature[2], 8))
432 return std::move(Err);
433 if (
Error Err = tryRead(Signature[3], 8))
434 return std::move(Err);
435 if (Signature[2] ==
'A' && Signature[3] ==
'G')
437 }
else if (Signature[0] ==
'R' && Signature[1] ==
'M') {
438 if (
Error Err = tryRead(Signature[2], 8))
439 return std::move(Err);
440 if (
Error Err = tryRead(Signature[3], 8))
441 return std::move(Err);
442 if (Signature[2] ==
'R' && Signature[3] ==
'K')
445 if (
Error Err = tryRead(Signature[2], 4))
446 return std::move(Err);
447 if (
Error Err = tryRead(Signature[3], 4))
448 return std::move(Err);
449 if (
Error Err = tryRead(Signature[4], 4))
450 return std::move(Err);
451 if (
Error Err = tryRead(Signature[5], 4))
452 return std::move(Err);
453 if (Signature[0] ==
'B' && Signature[1] ==
'C' && Signature[2] == 0x0 &&
454 Signature[3] == 0xC && Signature[4] == 0xE && Signature[5] == 0xD)
463 const unsigned char *BufPtr = (
const unsigned char *)Bytes.
data();
464 const unsigned char *EndBufPtr = BufPtr + Bytes.
size();
470 return reportError(
"Invalid bitcode wrapper header");
479 O->
OS <<
"<BITCODE_WRAPPER_HEADER" 484 <<
" CPUType=" <<
format_hex(CPUType, 10) <<
"/>\n";
488 return reportError(
"Invalid bitcode wrapper header");
501 Error BitcodeAnalyzer::decodeMetadataStringsBlob(
StringRef Indent,
508 if (Record.
size() != 2)
510 "Decoding metadata strings blob needs two record entries.");
512 unsigned NumStrings = Record[0];
513 unsigned StringsOffset = Record[1];
514 OS <<
" num-strings = " << NumStrings <<
" {\n";
530 OS << Indent <<
" '";
534 }
while (--NumStrings);
536 OS << Indent <<
" }";
544 BlockInfoStream.emplace(*BlockInfoBuffer);
553 CurStreamType = *MaybeType;
559 if (BlockInfoStream) {
570 return reportError(
"Invalid record at top-level in block info file");
578 if (!MaybeNewBlockInfo)
581 std::move(MaybeNewBlockInfo.
get());
583 return reportError(
"Malformed BlockInfoBlock in block info file");
584 BlockInfo = std::move(*NewBlockInfo);
605 if (
Error E = parseBlock(MaybeBlockID.
get(), 0,
O, CheckHash))
619 O.
OS <<
"of " << Filename->
data() <<
":\n";
620 O.
OS <<
" Total size: ";
623 O.
OS <<
" Stream type: ";
624 switch (CurStreamType) {
632 O.
OS <<
"Clang Serialized AST\n";
635 O.
OS <<
"Clang Serialized Diagnostics\n";
638 O.
OS <<
"LLVM Remarks\n";
641 O.
OS <<
" # Toplevel Blocks: " << NumTopBlocks <<
"\n";
645 O.
OS <<
"Per-block Summary:\n";
646 for (std::map<unsigned, PerBlockIDStats>::iterator
I = BlockIDStats.begin(),
647 E = BlockIDStats.end();
649 O.
OS <<
" Block ID #" <<
I->first;
652 O.
OS <<
" (" << *BlockName <<
")";
655 const PerBlockIDStats &
Stats =
I->second;
656 O.
OS <<
" Num Instances: " <<
Stats.NumInstances <<
"\n";
657 O.
OS <<
" Total Size: ";
660 double pct = (
Stats.NumBits * 100.0) / BufferSizeBits;
661 O.
OS <<
" Percent of file: " <<
format(
"%2.4f%%", pct) <<
"\n";
662 if (
Stats.NumInstances > 1) {
663 O.
OS <<
" Average Size: ";
666 O.
OS <<
" Tot/Avg SubBlocks: " <<
Stats.NumSubBlocks <<
"/" 667 <<
Stats.NumSubBlocks / (double)
Stats.NumInstances <<
"\n";
668 O.
OS <<
" Tot/Avg Abbrevs: " <<
Stats.NumAbbrevs <<
"/" 669 <<
Stats.NumAbbrevs / (
double)
Stats.NumInstances <<
"\n";
670 O.
OS <<
" Tot/Avg Records: " <<
Stats.NumRecords <<
"/" 671 <<
Stats.NumRecords / (double)
Stats.NumInstances <<
"\n";
673 O.
OS <<
" Num SubBlocks: " <<
Stats.NumSubBlocks <<
"\n";
674 O.
OS <<
" Num Abbrevs: " <<
Stats.NumAbbrevs <<
"\n";
675 O.
OS <<
" Num Records: " <<
Stats.NumRecords <<
"\n";
677 if (
Stats.NumRecords) {
678 double pct = (
Stats.NumAbbreviatedRecords * 100.0) /
Stats.NumRecords;
679 O.
OS <<
" Percent Abbrevs: " <<
format(
"%2.4f%%", pct) <<
"\n";
685 std::vector<std::pair<unsigned, unsigned>> FreqPairs;
686 for (
unsigned i = 0,
e =
Stats.CodeFreq.size(); i !=
e; ++i)
687 if (
unsigned Freq =
Stats.CodeFreq[i].NumInstances)
688 FreqPairs.push_back(std::make_pair(Freq, i));
692 O.
OS <<
"\tRecord Histogram:\n";
693 O.
OS <<
"\t\t Count # Bits b/Rec % Abv Record Kind\n";
694 for (
unsigned i = 0,
e = FreqPairs.size(); i !=
e; ++i) {
695 const PerRecordStats &RecStats =
Stats.CodeFreq[FreqPairs[i].second];
697 O.
OS <<
format(
"\t\t%7d %9lu", RecStats.NumInstances,
698 (
unsigned long)RecStats.TotalBits);
700 if (RecStats.NumInstances > 1)
702 (
double)RecStats.TotalBits / RecStats.NumInstances);
706 if (RecStats.NumAbbrev)
707 O.
OS <<
format(
" %7.2f", (
double)RecStats.NumAbbrev /
708 RecStats.NumInstances * 100);
714 FreqPairs[i].
second,
I->first, BlockInfo, CurStreamType))
715 O.
OS << *CodeName <<
"\n";
717 O.
OS <<
"UnknownCode" << FreqPairs[i].second <<
"\n";
724 Error BitcodeAnalyzer::parseBlock(
unsigned BlockID,
unsigned IndentLevel,
727 std::string Indent(IndentLevel * 2,
' ');
731 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
733 BlockStats.NumInstances++;
739 O->
OS << Indent <<
"<BLOCKINFO_BLOCK/>\n";
742 if (!MaybeNewBlockInfo)
745 std::move(MaybeNewBlockInfo.
get());
748 BlockInfo = std::move(*NewBlockInfo);
756 unsigned NumWords = 0;
765 O->
OS << Indent <<
"<";
766 if ((BlockName =
GetBlockName(BlockID, BlockInfo, CurStreamType)))
769 O->
OS <<
"UnknownBlock" << BlockID;
772 O->
OS <<
" BlockID=" << BlockID;
774 O->
OS <<
" NumWords=" << NumWords
781 uint64_t MetadataIndexOffset = 0;
796 switch (Entry.
Kind) {
801 BlockStats.NumBits += BlockBitEnd - BlockBitStart;
803 O->
OS << Indent <<
"</";
805 O->
OS << *BlockName <<
">\n";
807 O->
OS <<
"UnknownBlock" << BlockID <<
">\n";
814 if (
Error E = parseBlock(Entry.
ID, IndentLevel + 1, O, CheckHash))
816 ++BlockStats.NumSubBlocks;
820 BlockBitStart += SubBlockBitEnd - SubBlockBitStart;
831 ++BlockStats.NumAbbrevs;
837 ++BlockStats.NumRecords;
844 unsigned Code = MaybeCode.
get();
847 if (BlockStats.CodeFreq.size() <= Code)
848 BlockStats.CodeFreq.resize(Code + 1);
849 BlockStats.CodeFreq[Code].NumInstances++;
850 BlockStats.CodeFreq[Code].TotalBits +=
853 BlockStats.CodeFreq[Code].NumAbbrev++;
854 ++BlockStats.NumAbbreviatedRecords;
858 O->
OS << Indent <<
" <";
860 GetCodeName(Code, BlockID, BlockInfo, CurStreamType);
864 O->
OS <<
"UnknownCode" << Code;
866 O->
OS <<
" codeid=" << Code;
870 O->
OS <<
" abbrevid=" << Entry.
ID;
873 for (
unsigned i = 0,
e = Record.
size(); i !=
e; ++i)
874 O->
OS <<
" op" << i <<
"=" << (int64_t)Record[i];
880 if (Record.
size() != 2)
881 O->
OS <<
"(Invalid record)";
883 auto Offset = Record[0] + (Record[1] << 32);
888 O->
OS <<
" (offset ";
889 if (MetadataIndexOffset == RecordStartBit)
892 O->
OS <<
"mismatch: " << MetadataIndexOffset <<
" vs " 893 << RecordStartBit <<
")";
900 if (Record.
size() != 5)
901 O->
OS <<
" (invalid)";
906 Hasher.update(*CheckHash);
908 int BlockSize = (CurrentRecordPos / 8) - BlockEntryPos;
911 Hash = Hasher.result();
916 for (
auto &Val : Record) {
917 assert(!(Val >> 32) &&
"Unexpected high bits set");
918 RecordedHash[Pos++] = (Val >> 24) & 0xFF;
919 RecordedHash[Pos++] = (Val >> 16) & 0xFF;
920 RecordedHash[Pos++] = (Val >> 8) & 0xFF;
921 RecordedHash[Pos++] = (Val >> 0) & 0xFF;
923 if (Hash == RecordedHash)
926 O->
OS <<
" (!mismatch!)";
933 for (
unsigned i = 1,
e = Abbv->getNumOperandInfos(); i !=
e; ++i) {
937 assert(i + 2 ==
e &&
"Array op not second to last");
939 bool ArrayIsPrintable =
true;
940 for (
unsigned j = i - 1, je = Record.
size(); j != je; ++j) {
941 if (!
isPrint(static_cast<unsigned char>(Record[j]))) {
942 ArrayIsPrintable =
false;
945 Str += (
char)Record[j];
947 if (ArrayIsPrintable)
948 O->
OS <<
" record string = '" << Str <<
"'";
955 if (
Error E = decodeMetadataStringsBlob(Indent, Record, Blob, O->
OS))
958 O->
OS <<
" blob data = ";
963 bool BlobIsPrintable =
true;
964 for (
unsigned i = 0,
e = Blob.
size(); i !=
e; ++i)
965 if (!
isPrint(static_cast<unsigned char>(Blob[i]))) {
966 BlobIsPrintable =
false;
971 O->
OS <<
"'" << Blob <<
"'";
973 O->
OS <<
"unprintable, " << Blob.
size() <<
" bytes.";
987 return Skipped.takeError();
Encoding getEncoding() const
This contains information emitted to BLOCKINFO_BLOCK blocks.
bool ShowBinaryBlobs
Print binary blobs using hex escapes.
const BitCodeAbbrev * getAbbrev(unsigned AbbrevID)
Return the abbreviation for the specified AbbrevId.
BLOCKINFO_BLOCK is used to define metadata about blocks, for example, standard abbrevs that should be...
bool isPrint(char C)
Checks whether character C is printable.
static Expected< CurStreamTypeType > ReadSignature(BitstreamCursor &Stream)
This class represents lattice values for constants.
uint64_t GetCurrentBitNo() const
Return the bit # of the bit we are reading.
FormattedNumber format_hex(uint64_t N, unsigned Width, bool Upper=false)
format_hex - Output N as a fixed width hexadecimal.
BitcodeAnalyzer(StringRef Buffer, Optional< StringRef > BlockInfoBuffer=None)
const uint8_t * getPointerToByte(uint64_t ByteNo, uint64_t NumBytes)
Get a pointer into the bitstream at the specified byte offset.
BitCodeAbbrev - This class represents an abbreviation record.
Expected< uint32_t > ReadVBR(unsigned NumBits)
format_object< Ts... > format(const char *Fmt, const Ts &... Vals)
These are helper functions used to produce formatted output.
#define ENUMERATOR(NodeKind)
bool isBitcodeWrapper(const unsigned char *BufPtr, const unsigned char *BufEnd)
isBitcodeWrapper - Return true if the given bytes are the magic bytes for an LLVM IR bitcode wrapper...
A class that wrap the SHA1 algorithm.
Error takeError()
Take ownership of the stored error.
void setBlockInfo(BitstreamBlockInfo *BI)
Set the block info to be used by this BitstreamCursor to interpret abbreviated records.
static Optional< const char * > GetBlockName(unsigned BlockID, const BitstreamBlockInfo &BlockInfo, CurStreamTypeType CurStreamType)
Return a symbolic block name if known, otherwise return null.
Expected< BitstreamEntry > advance(unsigned Flags=0)
Advance the current bitstream, returning the next entry in the stream.
bool Symbolic
Don't emit numeric info in dump if symbolic info is available.
static Expected< CurStreamTypeType > analyzeHeader(Optional< BCDumpOptions > O, BitstreamCursor &Stream)
LLVM_NODISCARD StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Error SkipBlock()
Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body of this block...
static void printSize(raw_ostream &OS, double Bits)
LLVM_NODISCARD StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Tagged union holding either a T or a Error.
If this flag is used, abbrev entries are returned just like normal records.
block placement Basic Block Placement Stats
static bool canDecodeBlob(unsigned Code, unsigned BlockID)
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
INLINEASM - Represents an inline asm block.
#define ENTRY(ASMNAME, ENUM)
Expected< unsigned > ReadCode()
auto reverse(ContainerTy &&C, typename std::enable_if< has_rbegin< ContainerTy >::value >::type *=nullptr) -> decltype(make_range(C.rbegin(), C.rend()))
Expected< unsigned > skipRecord(unsigned AbbrevID)
Read the current record and discard it, returning the code for the record.
LLVM_NODISCARD size_t size() const
size - Get the string size.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
ArrayRef< uint8_t > getBitcodeBytes() const
Analysis containing CSE Info
#define STRINGIFY_CODE(PREFIX, CODE)
UNDEF - An undefined node.
uint64_t getCurrentByteNo() const
size_t size() const
size - Get the array size.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This represents a position within a bitcode file, implemented on top of a SimpleBitstreamCursor.
BitCodeAbbrevOp - This describes one or more operands in an abbreviation.
Expected< Optional< BitstreamBlockInfo > > ReadBlockInfoBlock(bool ReadBlockInfoNames=false)
Read and return a block info block from the bitstream.
raw_ostream & OS
The stream.
const BlockInfo * getBlockInfo(unsigned BlockID) const
If there is block info for the specified ID, return it, otherwise return null.
unsigned getAbbrevIDWidth() const
Return the number of bits used to encode an abbrev #.
Expected< unsigned > ReadSubBlockID()
Having read the ENTER_SUBBLOCK code, read the BlockID for the block.
Error JumpToBit(uint64_t BitNo)
Reset the stream to the specified bit number.
Expected< unsigned > readRecord(unsigned AbbrevID, SmallVectorImpl< uint64_t > &Vals, StringRef *Blob=nullptr)
static const char *const Magic
static const int BlockSize
raw_ostream & write_escaped(StringRef Str, bool UseHexEscapes=false)
Output Str, turning '\', '', ' ', '"', and anything that doesn't satisfy llvm::isPrint into an escape...
static ErrorSuccess success()
Create a success value.
When advancing through a bitstream cursor, each advance can discover a few different kinds of entries...
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
static Error reportError(StringRef Message)
This class maintains the abbreviations read from a block info block.
reference get()
Returns a reference to the stored T value.
DEFINE_ABBREV - Defines an abbrev for the current block.
bool Histogram
Print per-code histogram.
Error analyze(Optional< BCDumpOptions > O=None, Optional< StringRef > CheckHash=None)
Analyze the bitcode file.
Error EnterSubBlock(unsigned BlockID, unsigned *NumWordsP=nullptr)
Having read the ENTER_SUBBLOCK abbrevid, and enter the block.
bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr, const unsigned char *&BufEnd, bool VerifyBufferSize)
SkipBitcodeWrapperHeader - Some systems wrap bc files with a special header for padding or other reas...
uint32_t read32le(const void *P)
LLVM_NODISCARD const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
void stable_sort(R &&Range)
Lightweight error class with error context and mandatory checking.
void printStats(BCDumpOptions O, Optional< StringRef > Filename=None)
Print stats about the bitcode file.
static Optional< const char * > GetCodeName(unsigned CodeID, unsigned BlockID, const BitstreamBlockInfo &BlockInfo, CurStreamTypeType CurStreamType)
Return a symbolic code name if known, otherwise return null.
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
enum llvm::BitstreamEntry::@159 Kind
CurStreamTypeType
CurStreamTypeType - A type for CurStreamType.
This represents a position within a bitstream.
Expected< word_t > Read(unsigned NumBits)
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.