xref: /src/contrib/llvm-project/llvm/lib/Bitcode/Reader/BitcodeAnalyzer.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1e6d15924SDimitry Andric //===- BitcodeAnalyzer.cpp - Internal BitcodeAnalyzer implementation ------===//
2e6d15924SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e6d15924SDimitry Andric //
7e6d15924SDimitry Andric //===----------------------------------------------------------------------===//
8e6d15924SDimitry Andric 
9e6d15924SDimitry Andric #include "llvm/Bitcode/BitcodeAnalyzer.h"
10e6d15924SDimitry Andric #include "llvm/Bitcode/BitcodeReader.h"
11e6d15924SDimitry Andric #include "llvm/Bitcode/LLVMBitCodes.h"
12e6d15924SDimitry Andric #include "llvm/Bitstream/BitCodes.h"
13e6d15924SDimitry Andric #include "llvm/Bitstream/BitstreamReader.h"
14e6d15924SDimitry Andric #include "llvm/Support/Format.h"
15e6d15924SDimitry Andric #include "llvm/Support/SHA1.h"
16e3b55780SDimitry Andric #include <optional>
17e6d15924SDimitry Andric 
18e6d15924SDimitry Andric using namespace llvm;
19e6d15924SDimitry Andric 
reportError(StringRef Message)20e6d15924SDimitry Andric static Error reportError(StringRef Message) {
21e6d15924SDimitry Andric   return createStringError(std::errc::illegal_byte_sequence, Message.data());
22e6d15924SDimitry Andric }
23e6d15924SDimitry Andric 
24e6d15924SDimitry Andric /// Return a symbolic block name if known, otherwise return null.
25e3b55780SDimitry Andric static std::optional<const char *>
GetBlockName(unsigned BlockID,const BitstreamBlockInfo & BlockInfo,CurStreamTypeType CurStreamType)26e3b55780SDimitry Andric GetBlockName(unsigned BlockID, const BitstreamBlockInfo &BlockInfo,
27e6d15924SDimitry Andric              CurStreamTypeType CurStreamType) {
28e6d15924SDimitry Andric   // Standard blocks for all bitcode files.
29e6d15924SDimitry Andric   if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
30e6d15924SDimitry Andric     if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
31e6d15924SDimitry Andric       return "BLOCKINFO_BLOCK";
32e3b55780SDimitry Andric     return std::nullopt;
33e6d15924SDimitry Andric   }
34e6d15924SDimitry Andric 
35e6d15924SDimitry Andric   // Check to see if we have a blockinfo record for this block, with a name.
36e6d15924SDimitry Andric   if (const BitstreamBlockInfo::BlockInfo *Info =
37e6d15924SDimitry Andric           BlockInfo.getBlockInfo(BlockID)) {
38e6d15924SDimitry Andric     if (!Info->Name.empty())
39e6d15924SDimitry Andric       return Info->Name.c_str();
40e6d15924SDimitry Andric   }
41e6d15924SDimitry Andric 
42e6d15924SDimitry Andric   if (CurStreamType != LLVMIRBitstream)
43e3b55780SDimitry Andric     return std::nullopt;
44e6d15924SDimitry Andric 
45e6d15924SDimitry Andric   switch (BlockID) {
46e6d15924SDimitry Andric   default:
47e3b55780SDimitry Andric     return std::nullopt;
48e6d15924SDimitry Andric   case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
49e6d15924SDimitry Andric     return "OPERAND_BUNDLE_TAGS_BLOCK";
50e6d15924SDimitry Andric   case bitc::MODULE_BLOCK_ID:
51e6d15924SDimitry Andric     return "MODULE_BLOCK";
52e6d15924SDimitry Andric   case bitc::PARAMATTR_BLOCK_ID:
53e6d15924SDimitry Andric     return "PARAMATTR_BLOCK";
54e6d15924SDimitry Andric   case bitc::PARAMATTR_GROUP_BLOCK_ID:
55e6d15924SDimitry Andric     return "PARAMATTR_GROUP_BLOCK_ID";
56e6d15924SDimitry Andric   case bitc::TYPE_BLOCK_ID_NEW:
57e6d15924SDimitry Andric     return "TYPE_BLOCK_ID";
58e6d15924SDimitry Andric   case bitc::CONSTANTS_BLOCK_ID:
59e6d15924SDimitry Andric     return "CONSTANTS_BLOCK";
60e6d15924SDimitry Andric   case bitc::FUNCTION_BLOCK_ID:
61e6d15924SDimitry Andric     return "FUNCTION_BLOCK";
62e6d15924SDimitry Andric   case bitc::IDENTIFICATION_BLOCK_ID:
63e6d15924SDimitry Andric     return "IDENTIFICATION_BLOCK_ID";
64e6d15924SDimitry Andric   case bitc::VALUE_SYMTAB_BLOCK_ID:
65e6d15924SDimitry Andric     return "VALUE_SYMTAB";
66e6d15924SDimitry Andric   case bitc::METADATA_BLOCK_ID:
67e6d15924SDimitry Andric     return "METADATA_BLOCK";
68e6d15924SDimitry Andric   case bitc::METADATA_KIND_BLOCK_ID:
69e6d15924SDimitry Andric     return "METADATA_KIND_BLOCK";
70e6d15924SDimitry Andric   case bitc::METADATA_ATTACHMENT_ID:
71e6d15924SDimitry Andric     return "METADATA_ATTACHMENT_BLOCK";
72e6d15924SDimitry Andric   case bitc::USELIST_BLOCK_ID:
73e6d15924SDimitry Andric     return "USELIST_BLOCK_ID";
74e6d15924SDimitry Andric   case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
75e6d15924SDimitry Andric     return "GLOBALVAL_SUMMARY_BLOCK";
76e6d15924SDimitry Andric   case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
77e6d15924SDimitry Andric     return "FULL_LTO_GLOBALVAL_SUMMARY_BLOCK";
78e6d15924SDimitry Andric   case bitc::MODULE_STRTAB_BLOCK_ID:
79e6d15924SDimitry Andric     return "MODULE_STRTAB_BLOCK";
80e6d15924SDimitry Andric   case bitc::STRTAB_BLOCK_ID:
81e6d15924SDimitry Andric     return "STRTAB_BLOCK";
82e6d15924SDimitry Andric   case bitc::SYMTAB_BLOCK_ID:
83e6d15924SDimitry Andric     return "SYMTAB_BLOCK";
84e6d15924SDimitry Andric   }
85e6d15924SDimitry Andric }
86e6d15924SDimitry Andric 
87e6d15924SDimitry Andric /// Return a symbolic code name if known, otherwise return null.
88e3b55780SDimitry Andric static std::optional<const char *>
GetCodeName(unsigned CodeID,unsigned BlockID,const BitstreamBlockInfo & BlockInfo,CurStreamTypeType CurStreamType)89e3b55780SDimitry Andric GetCodeName(unsigned CodeID, unsigned BlockID,
90e6d15924SDimitry Andric             const BitstreamBlockInfo &BlockInfo,
91e6d15924SDimitry Andric             CurStreamTypeType CurStreamType) {
92e6d15924SDimitry Andric   // Standard blocks for all bitcode files.
93e6d15924SDimitry Andric   if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
94e6d15924SDimitry Andric     if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
95e6d15924SDimitry Andric       switch (CodeID) {
96e6d15924SDimitry Andric       default:
97e3b55780SDimitry Andric         return std::nullopt;
98e6d15924SDimitry Andric       case bitc::BLOCKINFO_CODE_SETBID:
99e6d15924SDimitry Andric         return "SETBID";
100e6d15924SDimitry Andric       case bitc::BLOCKINFO_CODE_BLOCKNAME:
101e6d15924SDimitry Andric         return "BLOCKNAME";
102e6d15924SDimitry Andric       case bitc::BLOCKINFO_CODE_SETRECORDNAME:
103e6d15924SDimitry Andric         return "SETRECORDNAME";
104e6d15924SDimitry Andric       }
105e6d15924SDimitry Andric     }
106e3b55780SDimitry Andric     return std::nullopt;
107e6d15924SDimitry Andric   }
108e6d15924SDimitry Andric 
109e6d15924SDimitry Andric   // Check to see if we have a blockinfo record for this record, with a name.
110e6d15924SDimitry Andric   if (const BitstreamBlockInfo::BlockInfo *Info =
111e6d15924SDimitry Andric           BlockInfo.getBlockInfo(BlockID)) {
11277fc4c14SDimitry Andric     for (const std::pair<unsigned, std::string> &RN : Info->RecordNames)
11377fc4c14SDimitry Andric       if (RN.first == CodeID)
11477fc4c14SDimitry Andric         return RN.second.c_str();
115e6d15924SDimitry Andric   }
116e6d15924SDimitry Andric 
117e6d15924SDimitry Andric   if (CurStreamType != LLVMIRBitstream)
118e3b55780SDimitry Andric     return std::nullopt;
119e6d15924SDimitry Andric 
120e6d15924SDimitry Andric #define STRINGIFY_CODE(PREFIX, CODE)                                           \
121e6d15924SDimitry Andric   case bitc::PREFIX##_##CODE:                                                  \
122e6d15924SDimitry Andric     return #CODE;
123e6d15924SDimitry Andric   switch (BlockID) {
124e6d15924SDimitry Andric   default:
125e3b55780SDimitry Andric     return std::nullopt;
126e6d15924SDimitry Andric   case bitc::MODULE_BLOCK_ID:
127e6d15924SDimitry Andric     switch (CodeID) {
128e6d15924SDimitry Andric     default:
129e3b55780SDimitry Andric       return std::nullopt;
130e6d15924SDimitry Andric       STRINGIFY_CODE(MODULE_CODE, VERSION)
131e6d15924SDimitry Andric       STRINGIFY_CODE(MODULE_CODE, TRIPLE)
132e6d15924SDimitry Andric       STRINGIFY_CODE(MODULE_CODE, DATALAYOUT)
133e6d15924SDimitry Andric       STRINGIFY_CODE(MODULE_CODE, ASM)
134e6d15924SDimitry Andric       STRINGIFY_CODE(MODULE_CODE, SECTIONNAME)
135cfca06d7SDimitry Andric       STRINGIFY_CODE(MODULE_CODE, DEPLIB) // Deprecated, present in old bitcode
136e6d15924SDimitry Andric       STRINGIFY_CODE(MODULE_CODE, GLOBALVAR)
137e6d15924SDimitry Andric       STRINGIFY_CODE(MODULE_CODE, FUNCTION)
138e6d15924SDimitry Andric       STRINGIFY_CODE(MODULE_CODE, ALIAS)
139e6d15924SDimitry Andric       STRINGIFY_CODE(MODULE_CODE, GCNAME)
140b60736ecSDimitry Andric       STRINGIFY_CODE(MODULE_CODE, COMDAT)
141e6d15924SDimitry Andric       STRINGIFY_CODE(MODULE_CODE, VSTOFFSET)
142e6d15924SDimitry Andric       STRINGIFY_CODE(MODULE_CODE, METADATA_VALUES_UNUSED)
143e6d15924SDimitry Andric       STRINGIFY_CODE(MODULE_CODE, SOURCE_FILENAME)
144e6d15924SDimitry Andric       STRINGIFY_CODE(MODULE_CODE, HASH)
145e6d15924SDimitry Andric     }
146e6d15924SDimitry Andric   case bitc::IDENTIFICATION_BLOCK_ID:
147e6d15924SDimitry Andric     switch (CodeID) {
148e6d15924SDimitry Andric     default:
149e3b55780SDimitry Andric       return std::nullopt;
150e6d15924SDimitry Andric       STRINGIFY_CODE(IDENTIFICATION_CODE, STRING)
151e6d15924SDimitry Andric       STRINGIFY_CODE(IDENTIFICATION_CODE, EPOCH)
152e6d15924SDimitry Andric     }
153e6d15924SDimitry Andric   case bitc::PARAMATTR_BLOCK_ID:
154e6d15924SDimitry Andric     switch (CodeID) {
155e6d15924SDimitry Andric     default:
156e3b55780SDimitry Andric       return std::nullopt;
157e6d15924SDimitry Andric     // FIXME: Should these be different?
158e6d15924SDimitry Andric     case bitc::PARAMATTR_CODE_ENTRY_OLD:
159e6d15924SDimitry Andric       return "ENTRY";
160e6d15924SDimitry Andric     case bitc::PARAMATTR_CODE_ENTRY:
161e6d15924SDimitry Andric       return "ENTRY";
162e6d15924SDimitry Andric     }
163e6d15924SDimitry Andric   case bitc::PARAMATTR_GROUP_BLOCK_ID:
164e6d15924SDimitry Andric     switch (CodeID) {
165e6d15924SDimitry Andric     default:
166e3b55780SDimitry Andric       return std::nullopt;
167e6d15924SDimitry Andric     case bitc::PARAMATTR_GRP_CODE_ENTRY:
168e6d15924SDimitry Andric       return "ENTRY";
169e6d15924SDimitry Andric     }
170e6d15924SDimitry Andric   case bitc::TYPE_BLOCK_ID_NEW:
171e6d15924SDimitry Andric     switch (CodeID) {
172e6d15924SDimitry Andric     default:
173e3b55780SDimitry Andric       return std::nullopt;
174e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, NUMENTRY)
175e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, VOID)
176e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, FLOAT)
177e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, DOUBLE)
178e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, LABEL)
179e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, OPAQUE)
180e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, INTEGER)
181e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, POINTER)
182b60736ecSDimitry Andric       STRINGIFY_CODE(TYPE_CODE, HALF)
183e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, ARRAY)
184e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, VECTOR)
185e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, X86_FP80)
186e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, FP128)
187e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, PPC_FP128)
188e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, METADATA)
189b60736ecSDimitry Andric       STRINGIFY_CODE(TYPE_CODE, X86_MMX)
190e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, STRUCT_ANON)
191e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, STRUCT_NAME)
192e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, STRUCT_NAMED)
193e6d15924SDimitry Andric       STRINGIFY_CODE(TYPE_CODE, FUNCTION)
194b60736ecSDimitry Andric       STRINGIFY_CODE(TYPE_CODE, TOKEN)
195b60736ecSDimitry Andric       STRINGIFY_CODE(TYPE_CODE, BFLOAT)
196e6d15924SDimitry Andric     }
197e6d15924SDimitry Andric 
198e6d15924SDimitry Andric   case bitc::CONSTANTS_BLOCK_ID:
199e6d15924SDimitry Andric     switch (CodeID) {
200e6d15924SDimitry Andric     default:
201e3b55780SDimitry Andric       return std::nullopt;
202e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, SETTYPE)
203e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, NULL)
204e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, UNDEF)
205e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, INTEGER)
206e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, WIDE_INTEGER)
207e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, FLOAT)
208e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, AGGREGATE)
209e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, STRING)
210e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, CSTRING)
211e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, CE_BINOP)
212e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, CE_CAST)
213e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, CE_GEP)
214e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, CE_INBOUNDS_GEP)
215e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, CE_SELECT)
216e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, CE_EXTRACTELT)
217e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, CE_INSERTELT)
218e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, CE_SHUFFLEVEC)
219e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, CE_CMP)
220e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, INLINEASM)
221e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, CE_SHUFVEC_EX)
222e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, CE_UNOP)
223344a3780SDimitry Andric       STRINGIFY_CODE(CST_CODE, DSO_LOCAL_EQUIVALENT)
22477fc4c14SDimitry Andric       STRINGIFY_CODE(CST_CODE, NO_CFI_VALUE)
225ac9a064cSDimitry Andric       STRINGIFY_CODE(CST_CODE, PTRAUTH)
226e6d15924SDimitry Andric     case bitc::CST_CODE_BLOCKADDRESS:
227e6d15924SDimitry Andric       return "CST_CODE_BLOCKADDRESS";
228e6d15924SDimitry Andric       STRINGIFY_CODE(CST_CODE, DATA)
229e6d15924SDimitry Andric     }
230e6d15924SDimitry Andric   case bitc::FUNCTION_BLOCK_ID:
231e6d15924SDimitry Andric     switch (CodeID) {
232e6d15924SDimitry Andric     default:
233e3b55780SDimitry Andric       return std::nullopt;
234e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, DECLAREBLOCKS)
235e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_BINOP)
236e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_CAST)
237e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_GEP_OLD)
238e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_INBOUNDS_GEP_OLD)
239e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_SELECT)
240e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTELT)
241e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_INSERTELT)
242e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_SHUFFLEVEC)
243e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_CMP)
244e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_RET)
245e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_BR)
246e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_SWITCH)
247e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_INVOKE)
248e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_UNOP)
249e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_UNREACHABLE)
250e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_CLEANUPRET)
251e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_CATCHRET)
252e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_CATCHPAD)
253e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_PHI)
254e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_ALLOCA)
255e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_LOAD)
256e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_VAARG)
257e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_STORE)
258e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTVAL)
259e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_INSERTVAL)
260e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_CMP2)
261e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_VSELECT)
262e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC_AGAIN)
263e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_CALL)
264e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC)
265e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_GEP)
266e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, OPERAND_BUNDLE)
267e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_FENCE)
268e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_ATOMICRMW)
269e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_LOADATOMIC)
270e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_STOREATOMIC)
271e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_CMPXCHG)
272e6d15924SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, INST_CALLBR)
273145449b1SDimitry Andric       STRINGIFY_CODE(FUNC_CODE, BLOCKADDR_USERS)
274ac9a064cSDimitry Andric       STRINGIFY_CODE(FUNC_CODE, DEBUG_RECORD_DECLARE)
275ac9a064cSDimitry Andric       STRINGIFY_CODE(FUNC_CODE, DEBUG_RECORD_VALUE)
276ac9a064cSDimitry Andric       STRINGIFY_CODE(FUNC_CODE, DEBUG_RECORD_ASSIGN)
277ac9a064cSDimitry Andric       STRINGIFY_CODE(FUNC_CODE, DEBUG_RECORD_VALUE_SIMPLE)
278ac9a064cSDimitry Andric       STRINGIFY_CODE(FUNC_CODE, DEBUG_RECORD_LABEL)
279e6d15924SDimitry Andric     }
280e6d15924SDimitry Andric   case bitc::VALUE_SYMTAB_BLOCK_ID:
281e6d15924SDimitry Andric     switch (CodeID) {
282e6d15924SDimitry Andric     default:
283e3b55780SDimitry Andric       return std::nullopt;
284e6d15924SDimitry Andric       STRINGIFY_CODE(VST_CODE, ENTRY)
285e6d15924SDimitry Andric       STRINGIFY_CODE(VST_CODE, BBENTRY)
286e6d15924SDimitry Andric       STRINGIFY_CODE(VST_CODE, FNENTRY)
287e6d15924SDimitry Andric       STRINGIFY_CODE(VST_CODE, COMBINED_ENTRY)
288e6d15924SDimitry Andric     }
289e6d15924SDimitry Andric   case bitc::MODULE_STRTAB_BLOCK_ID:
290e6d15924SDimitry Andric     switch (CodeID) {
291e6d15924SDimitry Andric     default:
292e3b55780SDimitry Andric       return std::nullopt;
293e6d15924SDimitry Andric       STRINGIFY_CODE(MST_CODE, ENTRY)
294e6d15924SDimitry Andric       STRINGIFY_CODE(MST_CODE, HASH)
295e6d15924SDimitry Andric     }
296e6d15924SDimitry Andric   case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
297e6d15924SDimitry Andric   case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
298e6d15924SDimitry Andric     switch (CodeID) {
299e6d15924SDimitry Andric     default:
300e3b55780SDimitry Andric       return std::nullopt;
301e6d15924SDimitry Andric       STRINGIFY_CODE(FS, PERMODULE)
302e6d15924SDimitry Andric       STRINGIFY_CODE(FS, PERMODULE_PROFILE)
303e6d15924SDimitry Andric       STRINGIFY_CODE(FS, PERMODULE_RELBF)
304e6d15924SDimitry Andric       STRINGIFY_CODE(FS, PERMODULE_GLOBALVAR_INIT_REFS)
305e6d15924SDimitry Andric       STRINGIFY_CODE(FS, PERMODULE_VTABLE_GLOBALVAR_INIT_REFS)
306e6d15924SDimitry Andric       STRINGIFY_CODE(FS, COMBINED)
307e6d15924SDimitry Andric       STRINGIFY_CODE(FS, COMBINED_PROFILE)
308e6d15924SDimitry Andric       STRINGIFY_CODE(FS, COMBINED_GLOBALVAR_INIT_REFS)
309e6d15924SDimitry Andric       STRINGIFY_CODE(FS, ALIAS)
310e6d15924SDimitry Andric       STRINGIFY_CODE(FS, COMBINED_ALIAS)
311e6d15924SDimitry Andric       STRINGIFY_CODE(FS, COMBINED_ORIGINAL_NAME)
312e6d15924SDimitry Andric       STRINGIFY_CODE(FS, VERSION)
313e6d15924SDimitry Andric       STRINGIFY_CODE(FS, FLAGS)
314e6d15924SDimitry Andric       STRINGIFY_CODE(FS, TYPE_TESTS)
315e6d15924SDimitry Andric       STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_VCALLS)
316e6d15924SDimitry Andric       STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_VCALLS)
317e6d15924SDimitry Andric       STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_CONST_VCALL)
318e6d15924SDimitry Andric       STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_CONST_VCALL)
319e6d15924SDimitry Andric       STRINGIFY_CODE(FS, VALUE_GUID)
320e6d15924SDimitry Andric       STRINGIFY_CODE(FS, CFI_FUNCTION_DEFS)
321e6d15924SDimitry Andric       STRINGIFY_CODE(FS, CFI_FUNCTION_DECLS)
322e6d15924SDimitry Andric       STRINGIFY_CODE(FS, TYPE_ID)
323e6d15924SDimitry Andric       STRINGIFY_CODE(FS, TYPE_ID_METADATA)
324cfca06d7SDimitry Andric       STRINGIFY_CODE(FS, BLOCK_COUNT)
325cfca06d7SDimitry Andric       STRINGIFY_CODE(FS, PARAM_ACCESS)
326e3b55780SDimitry Andric       STRINGIFY_CODE(FS, PERMODULE_CALLSITE_INFO)
327e3b55780SDimitry Andric       STRINGIFY_CODE(FS, PERMODULE_ALLOC_INFO)
328e3b55780SDimitry Andric       STRINGIFY_CODE(FS, COMBINED_CALLSITE_INFO)
329e3b55780SDimitry Andric       STRINGIFY_CODE(FS, COMBINED_ALLOC_INFO)
330e3b55780SDimitry Andric       STRINGIFY_CODE(FS, STACK_IDS)
331e6d15924SDimitry Andric     }
332e6d15924SDimitry Andric   case bitc::METADATA_ATTACHMENT_ID:
333e6d15924SDimitry Andric     switch (CodeID) {
334e6d15924SDimitry Andric     default:
335e3b55780SDimitry Andric       return std::nullopt;
336e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, ATTACHMENT)
337e6d15924SDimitry Andric     }
338e6d15924SDimitry Andric   case bitc::METADATA_BLOCK_ID:
339e6d15924SDimitry Andric     switch (CodeID) {
340e6d15924SDimitry Andric     default:
341e3b55780SDimitry Andric       return std::nullopt;
342e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, STRING_OLD)
343e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, VALUE)
344e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, NODE)
345e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, NAME)
346e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, DISTINCT_NODE)
347e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, KIND) // Older bitcode has it in a MODULE_BLOCK
348e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, LOCATION)
349e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, OLD_NODE)
350e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, OLD_FN_NODE)
351e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, NAMED_NODE)
352e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, GENERIC_DEBUG)
353e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, SUBRANGE)
354e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, ENUMERATOR)
355e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, BASIC_TYPE)
356e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, FILE)
357e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, DERIVED_TYPE)
358e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, COMPOSITE_TYPE)
359e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, SUBROUTINE_TYPE)
360e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, COMPILE_UNIT)
361e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, SUBPROGRAM)
362e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, LEXICAL_BLOCK)
363e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, LEXICAL_BLOCK_FILE)
364e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, NAMESPACE)
365e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, TEMPLATE_TYPE)
366e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, TEMPLATE_VALUE)
367e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, GLOBAL_VAR)
368e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, LOCAL_VAR)
369e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, EXPRESSION)
370e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, OBJC_PROPERTY)
371e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, IMPORTED_ENTITY)
372e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, MODULE)
373e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, MACRO)
374e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, MACRO_FILE)
375e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, STRINGS)
376e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, GLOBAL_DECL_ATTACHMENT)
377e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, GLOBAL_VAR_EXPR)
378e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, INDEX_OFFSET)
379e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, INDEX)
380344a3780SDimitry Andric       STRINGIFY_CODE(METADATA, ARG_LIST)
381e6d15924SDimitry Andric     }
382e6d15924SDimitry Andric   case bitc::METADATA_KIND_BLOCK_ID:
383e6d15924SDimitry Andric     switch (CodeID) {
384e6d15924SDimitry Andric     default:
385e3b55780SDimitry Andric       return std::nullopt;
386e6d15924SDimitry Andric       STRINGIFY_CODE(METADATA, KIND)
387e6d15924SDimitry Andric     }
388e6d15924SDimitry Andric   case bitc::USELIST_BLOCK_ID:
389e6d15924SDimitry Andric     switch (CodeID) {
390e6d15924SDimitry Andric     default:
391e3b55780SDimitry Andric       return std::nullopt;
392e6d15924SDimitry Andric     case bitc::USELIST_CODE_DEFAULT:
393e6d15924SDimitry Andric       return "USELIST_CODE_DEFAULT";
394e6d15924SDimitry Andric     case bitc::USELIST_CODE_BB:
395e6d15924SDimitry Andric       return "USELIST_CODE_BB";
396e6d15924SDimitry Andric     }
397e6d15924SDimitry Andric 
398e6d15924SDimitry Andric   case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
399e6d15924SDimitry Andric     switch (CodeID) {
400e6d15924SDimitry Andric     default:
401e3b55780SDimitry Andric       return std::nullopt;
402e6d15924SDimitry Andric     case bitc::OPERAND_BUNDLE_TAG:
403e6d15924SDimitry Andric       return "OPERAND_BUNDLE_TAG";
404e6d15924SDimitry Andric     }
405e6d15924SDimitry Andric   case bitc::STRTAB_BLOCK_ID:
406e6d15924SDimitry Andric     switch (CodeID) {
407e6d15924SDimitry Andric     default:
408e3b55780SDimitry Andric       return std::nullopt;
409e6d15924SDimitry Andric     case bitc::STRTAB_BLOB:
410e6d15924SDimitry Andric       return "BLOB";
411e6d15924SDimitry Andric     }
412e6d15924SDimitry Andric   case bitc::SYMTAB_BLOCK_ID:
413e6d15924SDimitry Andric     switch (CodeID) {
414e6d15924SDimitry Andric     default:
415e3b55780SDimitry Andric       return std::nullopt;
416e6d15924SDimitry Andric     case bitc::SYMTAB_BLOB:
417e6d15924SDimitry Andric       return "BLOB";
418e6d15924SDimitry Andric     }
419e6d15924SDimitry Andric   }
420e6d15924SDimitry Andric #undef STRINGIFY_CODE
421e6d15924SDimitry Andric }
422e6d15924SDimitry Andric 
printSize(raw_ostream & OS,double Bits)423e6d15924SDimitry Andric static void printSize(raw_ostream &OS, double Bits) {
424e6d15924SDimitry Andric   OS << format("%.2f/%.2fB/%luW", Bits, Bits / 8, (unsigned long)(Bits / 32));
425e6d15924SDimitry Andric }
printSize(raw_ostream & OS,uint64_t Bits)426e6d15924SDimitry Andric static void printSize(raw_ostream &OS, uint64_t Bits) {
427e6d15924SDimitry Andric   OS << format("%lub/%.2fB/%luW", (unsigned long)Bits, (double)Bits / 8,
428e6d15924SDimitry Andric                (unsigned long)(Bits / 32));
429e6d15924SDimitry Andric }
430e6d15924SDimitry Andric 
ReadSignature(BitstreamCursor & Stream)431e6d15924SDimitry Andric static Expected<CurStreamTypeType> ReadSignature(BitstreamCursor &Stream) {
432e6d15924SDimitry Andric   auto tryRead = [&Stream](char &Dest, size_t size) -> Error {
433e6d15924SDimitry Andric     if (Expected<SimpleBitstreamCursor::word_t> MaybeWord = Stream.Read(size))
434e6d15924SDimitry Andric       Dest = MaybeWord.get();
435e6d15924SDimitry Andric     else
436e6d15924SDimitry Andric       return MaybeWord.takeError();
437e6d15924SDimitry Andric     return Error::success();
438e6d15924SDimitry Andric   };
439e6d15924SDimitry Andric 
440e6d15924SDimitry Andric   char Signature[6];
441e6d15924SDimitry Andric   if (Error Err = tryRead(Signature[0], 8))
442e6d15924SDimitry Andric     return std::move(Err);
443e6d15924SDimitry Andric   if (Error Err = tryRead(Signature[1], 8))
444e6d15924SDimitry Andric     return std::move(Err);
445e6d15924SDimitry Andric 
446e6d15924SDimitry Andric   // Autodetect the file contents, if it is one we know.
447e6d15924SDimitry Andric   if (Signature[0] == 'C' && Signature[1] == 'P') {
448e6d15924SDimitry Andric     if (Error Err = tryRead(Signature[2], 8))
449e6d15924SDimitry Andric       return std::move(Err);
450e6d15924SDimitry Andric     if (Error Err = tryRead(Signature[3], 8))
451e6d15924SDimitry Andric       return std::move(Err);
452e6d15924SDimitry Andric     if (Signature[2] == 'C' && Signature[3] == 'H')
453e6d15924SDimitry Andric       return ClangSerializedASTBitstream;
454e6d15924SDimitry Andric   } else if (Signature[0] == 'D' && Signature[1] == 'I') {
455e6d15924SDimitry Andric     if (Error Err = tryRead(Signature[2], 8))
456e6d15924SDimitry Andric       return std::move(Err);
457e6d15924SDimitry Andric     if (Error Err = tryRead(Signature[3], 8))
458e6d15924SDimitry Andric       return std::move(Err);
459e6d15924SDimitry Andric     if (Signature[2] == 'A' && Signature[3] == 'G')
460e6d15924SDimitry Andric       return ClangSerializedDiagnosticsBitstream;
4611d5ae102SDimitry Andric   } else if (Signature[0] == 'R' && Signature[1] == 'M') {
4621d5ae102SDimitry Andric     if (Error Err = tryRead(Signature[2], 8))
4631d5ae102SDimitry Andric       return std::move(Err);
4641d5ae102SDimitry Andric     if (Error Err = tryRead(Signature[3], 8))
4651d5ae102SDimitry Andric       return std::move(Err);
4661d5ae102SDimitry Andric     if (Signature[2] == 'R' && Signature[3] == 'K')
4671d5ae102SDimitry Andric       return LLVMBitstreamRemarks;
468e6d15924SDimitry Andric   } else {
469e6d15924SDimitry Andric     if (Error Err = tryRead(Signature[2], 4))
470e6d15924SDimitry Andric       return std::move(Err);
471e6d15924SDimitry Andric     if (Error Err = tryRead(Signature[3], 4))
472e6d15924SDimitry Andric       return std::move(Err);
473e6d15924SDimitry Andric     if (Error Err = tryRead(Signature[4], 4))
474e6d15924SDimitry Andric       return std::move(Err);
475e6d15924SDimitry Andric     if (Error Err = tryRead(Signature[5], 4))
476e6d15924SDimitry Andric       return std::move(Err);
477e6d15924SDimitry Andric     if (Signature[0] == 'B' && Signature[1] == 'C' && Signature[2] == 0x0 &&
478e6d15924SDimitry Andric         Signature[3] == 0xC && Signature[4] == 0xE && Signature[5] == 0xD)
479e6d15924SDimitry Andric       return LLVMIRBitstream;
480e6d15924SDimitry Andric   }
481e6d15924SDimitry Andric   return UnknownBitstream;
482e6d15924SDimitry Andric }
483e6d15924SDimitry Andric 
analyzeHeader(std::optional<BCDumpOptions> O,BitstreamCursor & Stream)484e3b55780SDimitry Andric static Expected<CurStreamTypeType> analyzeHeader(std::optional<BCDumpOptions> O,
485e6d15924SDimitry Andric                                                  BitstreamCursor &Stream) {
486e6d15924SDimitry Andric   ArrayRef<uint8_t> Bytes = Stream.getBitcodeBytes();
487e6d15924SDimitry Andric   const unsigned char *BufPtr = (const unsigned char *)Bytes.data();
488e6d15924SDimitry Andric   const unsigned char *EndBufPtr = BufPtr + Bytes.size();
489e6d15924SDimitry Andric 
490e6d15924SDimitry Andric   // If we have a wrapper header, parse it and ignore the non-bc file
491e6d15924SDimitry Andric   // contents. The magic number is 0x0B17C0DE stored in little endian.
492e6d15924SDimitry Andric   if (isBitcodeWrapper(BufPtr, EndBufPtr)) {
493e6d15924SDimitry Andric     if (Bytes.size() < BWH_HeaderSize)
494e6d15924SDimitry Andric       return reportError("Invalid bitcode wrapper header");
495e6d15924SDimitry Andric 
496e6d15924SDimitry Andric     if (O) {
497e6d15924SDimitry Andric       unsigned Magic = support::endian::read32le(&BufPtr[BWH_MagicField]);
498e6d15924SDimitry Andric       unsigned Version = support::endian::read32le(&BufPtr[BWH_VersionField]);
499e6d15924SDimitry Andric       unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]);
500e6d15924SDimitry Andric       unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]);
501e6d15924SDimitry Andric       unsigned CPUType = support::endian::read32le(&BufPtr[BWH_CPUTypeField]);
502e6d15924SDimitry Andric 
503e6d15924SDimitry Andric       O->OS << "<BITCODE_WRAPPER_HEADER"
504e6d15924SDimitry Andric             << " Magic=" << format_hex(Magic, 10)
505e6d15924SDimitry Andric             << " Version=" << format_hex(Version, 10)
506e6d15924SDimitry Andric             << " Offset=" << format_hex(Offset, 10)
507e6d15924SDimitry Andric             << " Size=" << format_hex(Size, 10)
508e6d15924SDimitry Andric             << " CPUType=" << format_hex(CPUType, 10) << "/>\n";
509e6d15924SDimitry Andric     }
510e6d15924SDimitry Andric 
511e6d15924SDimitry Andric     if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr, true))
512e6d15924SDimitry Andric       return reportError("Invalid bitcode wrapper header");
513e6d15924SDimitry Andric   }
514e6d15924SDimitry Andric 
515e6d15924SDimitry Andric   // Use the cursor modified by skipping the wrapper header.
516e6d15924SDimitry Andric   Stream = BitstreamCursor(ArrayRef<uint8_t>(BufPtr, EndBufPtr));
517e6d15924SDimitry Andric 
518e6d15924SDimitry Andric   return ReadSignature(Stream);
519e6d15924SDimitry Andric }
520e6d15924SDimitry Andric 
canDecodeBlob(unsigned Code,unsigned BlockID)521e6d15924SDimitry Andric static bool canDecodeBlob(unsigned Code, unsigned BlockID) {
522e6d15924SDimitry Andric   return BlockID == bitc::METADATA_BLOCK_ID && Code == bitc::METADATA_STRINGS;
523e6d15924SDimitry Andric }
524e6d15924SDimitry Andric 
decodeMetadataStringsBlob(StringRef Indent,ArrayRef<uint64_t> Record,StringRef Blob,raw_ostream & OS)525e6d15924SDimitry Andric Error BitcodeAnalyzer::decodeMetadataStringsBlob(StringRef Indent,
526e6d15924SDimitry Andric                                                  ArrayRef<uint64_t> Record,
527e6d15924SDimitry Andric                                                  StringRef Blob,
528e6d15924SDimitry Andric                                                  raw_ostream &OS) {
529e6d15924SDimitry Andric   if (Blob.empty())
530e6d15924SDimitry Andric     return reportError("Cannot decode empty blob.");
531e6d15924SDimitry Andric 
532e6d15924SDimitry Andric   if (Record.size() != 2)
533e6d15924SDimitry Andric     return reportError(
534e6d15924SDimitry Andric         "Decoding metadata strings blob needs two record entries.");
535e6d15924SDimitry Andric 
536e6d15924SDimitry Andric   unsigned NumStrings = Record[0];
537e6d15924SDimitry Andric   unsigned StringsOffset = Record[1];
538e6d15924SDimitry Andric   OS << " num-strings = " << NumStrings << " {\n";
539e6d15924SDimitry Andric 
540e6d15924SDimitry Andric   StringRef Lengths = Blob.slice(0, StringsOffset);
541e6d15924SDimitry Andric   SimpleBitstreamCursor R(Lengths);
542e6d15924SDimitry Andric   StringRef Strings = Blob.drop_front(StringsOffset);
543e6d15924SDimitry Andric   do {
544e6d15924SDimitry Andric     if (R.AtEndOfStream())
545e6d15924SDimitry Andric       return reportError("bad length");
546e6d15924SDimitry Andric 
547c0981da4SDimitry Andric     uint32_t Size;
548c0981da4SDimitry Andric     if (Error E = R.ReadVBR(6).moveInto(Size))
549c0981da4SDimitry Andric       return E;
550e6d15924SDimitry Andric     if (Strings.size() < Size)
551e6d15924SDimitry Andric       return reportError("truncated chars");
552e6d15924SDimitry Andric 
553e6d15924SDimitry Andric     OS << Indent << "    '";
554e6d15924SDimitry Andric     OS.write_escaped(Strings.slice(0, Size), /*hex=*/true);
555e6d15924SDimitry Andric     OS << "'\n";
556e6d15924SDimitry Andric     Strings = Strings.drop_front(Size);
557e6d15924SDimitry Andric   } while (--NumStrings);
558e6d15924SDimitry Andric 
559e6d15924SDimitry Andric   OS << Indent << "  }";
560e6d15924SDimitry Andric   return Error::success();
561e6d15924SDimitry Andric }
562e6d15924SDimitry Andric 
BitcodeAnalyzer(StringRef Buffer,std::optional<StringRef> BlockInfoBuffer)563e6d15924SDimitry Andric BitcodeAnalyzer::BitcodeAnalyzer(StringRef Buffer,
564e3b55780SDimitry Andric                                  std::optional<StringRef> BlockInfoBuffer)
565e6d15924SDimitry Andric     : Stream(Buffer) {
566e6d15924SDimitry Andric   if (BlockInfoBuffer)
567e6d15924SDimitry Andric     BlockInfoStream.emplace(*BlockInfoBuffer);
568e6d15924SDimitry Andric }
569e6d15924SDimitry Andric 
analyze(std::optional<BCDumpOptions> O,std::optional<StringRef> CheckHash)570e3b55780SDimitry Andric Error BitcodeAnalyzer::analyze(std::optional<BCDumpOptions> O,
571e3b55780SDimitry Andric                                std::optional<StringRef> CheckHash) {
572c0981da4SDimitry Andric   if (Error E = analyzeHeader(O, Stream).moveInto(CurStreamType))
573c0981da4SDimitry Andric     return E;
574e6d15924SDimitry Andric 
575e6d15924SDimitry Andric   Stream.setBlockInfo(&BlockInfo);
576e6d15924SDimitry Andric 
577e6d15924SDimitry Andric   // Read block info from BlockInfoStream, if specified.
578e6d15924SDimitry Andric   // The block info must be a top-level block.
579e6d15924SDimitry Andric   if (BlockInfoStream) {
580e6d15924SDimitry Andric     BitstreamCursor BlockInfoCursor(*BlockInfoStream);
581c0981da4SDimitry Andric     if (Error E = analyzeHeader(O, BlockInfoCursor).takeError())
582c0981da4SDimitry Andric       return E;
583e6d15924SDimitry Andric 
584e6d15924SDimitry Andric     while (!BlockInfoCursor.AtEndOfStream()) {
585e6d15924SDimitry Andric       Expected<unsigned> MaybeCode = BlockInfoCursor.ReadCode();
586e6d15924SDimitry Andric       if (!MaybeCode)
587e6d15924SDimitry Andric         return MaybeCode.takeError();
588e6d15924SDimitry Andric       if (MaybeCode.get() != bitc::ENTER_SUBBLOCK)
589e6d15924SDimitry Andric         return reportError("Invalid record at top-level in block info file");
590e6d15924SDimitry Andric 
591e6d15924SDimitry Andric       Expected<unsigned> MaybeBlockID = BlockInfoCursor.ReadSubBlockID();
592e6d15924SDimitry Andric       if (!MaybeBlockID)
593e6d15924SDimitry Andric         return MaybeBlockID.takeError();
594e6d15924SDimitry Andric       if (MaybeBlockID.get() == bitc::BLOCKINFO_BLOCK_ID) {
595e3b55780SDimitry Andric         std::optional<BitstreamBlockInfo> NewBlockInfo;
596c0981da4SDimitry Andric         if (Error E =
597c0981da4SDimitry Andric                 BlockInfoCursor.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true)
598c0981da4SDimitry Andric                     .moveInto(NewBlockInfo))
599c0981da4SDimitry Andric           return E;
600e6d15924SDimitry Andric         if (!NewBlockInfo)
601e6d15924SDimitry Andric           return reportError("Malformed BlockInfoBlock in block info file");
602e6d15924SDimitry Andric         BlockInfo = std::move(*NewBlockInfo);
603e6d15924SDimitry Andric         break;
604e6d15924SDimitry Andric       }
605e6d15924SDimitry Andric 
606e6d15924SDimitry Andric       if (Error Err = BlockInfoCursor.SkipBlock())
607e6d15924SDimitry Andric         return Err;
608e6d15924SDimitry Andric     }
609e6d15924SDimitry Andric   }
610e6d15924SDimitry Andric 
611e6d15924SDimitry Andric   // Parse the top-level structure.  We only allow blocks at the top-level.
612e6d15924SDimitry Andric   while (!Stream.AtEndOfStream()) {
613e6d15924SDimitry Andric     Expected<unsigned> MaybeCode = Stream.ReadCode();
614e6d15924SDimitry Andric     if (!MaybeCode)
615e6d15924SDimitry Andric       return MaybeCode.takeError();
616e6d15924SDimitry Andric     if (MaybeCode.get() != bitc::ENTER_SUBBLOCK)
617e6d15924SDimitry Andric       return reportError("Invalid record at top-level");
618e6d15924SDimitry Andric 
619e6d15924SDimitry Andric     Expected<unsigned> MaybeBlockID = Stream.ReadSubBlockID();
620e6d15924SDimitry Andric     if (!MaybeBlockID)
621e6d15924SDimitry Andric       return MaybeBlockID.takeError();
622e6d15924SDimitry Andric 
623e6d15924SDimitry Andric     if (Error E = parseBlock(MaybeBlockID.get(), 0, O, CheckHash))
624e6d15924SDimitry Andric       return E;
625e6d15924SDimitry Andric     ++NumTopBlocks;
626e6d15924SDimitry Andric   }
627e6d15924SDimitry Andric 
628e6d15924SDimitry Andric   return Error::success();
629e6d15924SDimitry Andric }
630e6d15924SDimitry Andric 
printStats(BCDumpOptions O,std::optional<StringRef> Filename)631e6d15924SDimitry Andric void BitcodeAnalyzer::printStats(BCDumpOptions O,
632e3b55780SDimitry Andric                                  std::optional<StringRef> Filename) {
633e6d15924SDimitry Andric   uint64_t BufferSizeBits = Stream.getBitcodeBytes().size() * CHAR_BIT;
634e6d15924SDimitry Andric   // Print a summary of the read file.
635e6d15924SDimitry Andric   O.OS << "Summary ";
636e6d15924SDimitry Andric   if (Filename)
637e6d15924SDimitry Andric     O.OS << "of " << Filename->data() << ":\n";
638e6d15924SDimitry Andric   O.OS << "         Total size: ";
639e6d15924SDimitry Andric   printSize(O.OS, BufferSizeBits);
640e6d15924SDimitry Andric   O.OS << "\n";
641e6d15924SDimitry Andric   O.OS << "        Stream type: ";
642e6d15924SDimitry Andric   switch (CurStreamType) {
643e6d15924SDimitry Andric   case UnknownBitstream:
644e6d15924SDimitry Andric     O.OS << "unknown\n";
645e6d15924SDimitry Andric     break;
646e6d15924SDimitry Andric   case LLVMIRBitstream:
647e6d15924SDimitry Andric     O.OS << "LLVM IR\n";
648e6d15924SDimitry Andric     break;
649e6d15924SDimitry Andric   case ClangSerializedASTBitstream:
650e6d15924SDimitry Andric     O.OS << "Clang Serialized AST\n";
651e6d15924SDimitry Andric     break;
652e6d15924SDimitry Andric   case ClangSerializedDiagnosticsBitstream:
653e6d15924SDimitry Andric     O.OS << "Clang Serialized Diagnostics\n";
654e6d15924SDimitry Andric     break;
6551d5ae102SDimitry Andric   case LLVMBitstreamRemarks:
6561d5ae102SDimitry Andric     O.OS << "LLVM Remarks\n";
6571d5ae102SDimitry Andric     break;
658e6d15924SDimitry Andric   }
659e6d15924SDimitry Andric   O.OS << "  # Toplevel Blocks: " << NumTopBlocks << "\n";
660e6d15924SDimitry Andric   O.OS << "\n";
661e6d15924SDimitry Andric 
662e6d15924SDimitry Andric   // Emit per-block stats.
663e6d15924SDimitry Andric   O.OS << "Per-block Summary:\n";
66477fc4c14SDimitry Andric   for (const auto &Stat : BlockIDStats) {
66577fc4c14SDimitry Andric     O.OS << "  Block ID #" << Stat.first;
666e3b55780SDimitry Andric     if (std::optional<const char *> BlockName =
66777fc4c14SDimitry Andric             GetBlockName(Stat.first, BlockInfo, CurStreamType))
668e6d15924SDimitry Andric       O.OS << " (" << *BlockName << ")";
669e6d15924SDimitry Andric     O.OS << ":\n";
670e6d15924SDimitry Andric 
67177fc4c14SDimitry Andric     const PerBlockIDStats &Stats = Stat.second;
672e6d15924SDimitry Andric     O.OS << "      Num Instances: " << Stats.NumInstances << "\n";
673e6d15924SDimitry Andric     O.OS << "         Total Size: ";
674e6d15924SDimitry Andric     printSize(O.OS, Stats.NumBits);
675e6d15924SDimitry Andric     O.OS << "\n";
676e6d15924SDimitry Andric     double pct = (Stats.NumBits * 100.0) / BufferSizeBits;
677e6d15924SDimitry Andric     O.OS << "    Percent of file: " << format("%2.4f%%", pct) << "\n";
678e6d15924SDimitry Andric     if (Stats.NumInstances > 1) {
679e6d15924SDimitry Andric       O.OS << "       Average Size: ";
680e6d15924SDimitry Andric       printSize(O.OS, Stats.NumBits / (double)Stats.NumInstances);
681e6d15924SDimitry Andric       O.OS << "\n";
682e6d15924SDimitry Andric       O.OS << "  Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
683e6d15924SDimitry Andric            << Stats.NumSubBlocks / (double)Stats.NumInstances << "\n";
684e6d15924SDimitry Andric       O.OS << "    Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
685e6d15924SDimitry Andric            << Stats.NumAbbrevs / (double)Stats.NumInstances << "\n";
686e6d15924SDimitry Andric       O.OS << "    Tot/Avg Records: " << Stats.NumRecords << "/"
687e6d15924SDimitry Andric            << Stats.NumRecords / (double)Stats.NumInstances << "\n";
688e6d15924SDimitry Andric     } else {
689e6d15924SDimitry Andric       O.OS << "      Num SubBlocks: " << Stats.NumSubBlocks << "\n";
690e6d15924SDimitry Andric       O.OS << "        Num Abbrevs: " << Stats.NumAbbrevs << "\n";
691e6d15924SDimitry Andric       O.OS << "        Num Records: " << Stats.NumRecords << "\n";
692e6d15924SDimitry Andric     }
693e6d15924SDimitry Andric     if (Stats.NumRecords) {
694e6d15924SDimitry Andric       double pct = (Stats.NumAbbreviatedRecords * 100.0) / Stats.NumRecords;
695e6d15924SDimitry Andric       O.OS << "    Percent Abbrevs: " << format("%2.4f%%", pct) << "\n";
696e6d15924SDimitry Andric     }
697e6d15924SDimitry Andric     O.OS << "\n";
698e6d15924SDimitry Andric 
699e6d15924SDimitry Andric     // Print a histogram of the codes we see.
700e6d15924SDimitry Andric     if (O.Histogram && !Stats.CodeFreq.empty()) {
701e6d15924SDimitry Andric       std::vector<std::pair<unsigned, unsigned>> FreqPairs; // <freq,code>
702e6d15924SDimitry Andric       for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
703e6d15924SDimitry Andric         if (unsigned Freq = Stats.CodeFreq[i].NumInstances)
704e6d15924SDimitry Andric           FreqPairs.push_back(std::make_pair(Freq, i));
705e6d15924SDimitry Andric       llvm::stable_sort(FreqPairs);
706e6d15924SDimitry Andric       std::reverse(FreqPairs.begin(), FreqPairs.end());
707e6d15924SDimitry Andric 
708e6d15924SDimitry Andric       O.OS << "\tRecord Histogram:\n";
709e6d15924SDimitry Andric       O.OS << "\t\t  Count    # Bits     b/Rec   % Abv  Record Kind\n";
71077fc4c14SDimitry Andric       for (const auto &FreqPair : FreqPairs) {
71177fc4c14SDimitry Andric         const PerRecordStats &RecStats = Stats.CodeFreq[FreqPair.second];
712e6d15924SDimitry Andric 
713e6d15924SDimitry Andric         O.OS << format("\t\t%7d %9lu", RecStats.NumInstances,
714e6d15924SDimitry Andric                        (unsigned long)RecStats.TotalBits);
715e6d15924SDimitry Andric 
716e6d15924SDimitry Andric         if (RecStats.NumInstances > 1)
717e6d15924SDimitry Andric           O.OS << format(" %9.1f",
718e6d15924SDimitry Andric                          (double)RecStats.TotalBits / RecStats.NumInstances);
719e6d15924SDimitry Andric         else
720e6d15924SDimitry Andric           O.OS << "          ";
721e6d15924SDimitry Andric 
722e6d15924SDimitry Andric         if (RecStats.NumAbbrev)
723e6d15924SDimitry Andric           O.OS << format(" %7.2f", (double)RecStats.NumAbbrev /
724e6d15924SDimitry Andric                                        RecStats.NumInstances * 100);
725e6d15924SDimitry Andric         else
726e6d15924SDimitry Andric           O.OS << "        ";
727e6d15924SDimitry Andric 
728e6d15924SDimitry Andric         O.OS << "  ";
729e3b55780SDimitry Andric         if (std::optional<const char *> CodeName = GetCodeName(
73077fc4c14SDimitry Andric                 FreqPair.second, Stat.first, BlockInfo, CurStreamType))
731e6d15924SDimitry Andric           O.OS << *CodeName << "\n";
732e6d15924SDimitry Andric         else
73377fc4c14SDimitry Andric           O.OS << "UnknownCode" << FreqPair.second << "\n";
734e6d15924SDimitry Andric       }
735e6d15924SDimitry Andric       O.OS << "\n";
736e6d15924SDimitry Andric     }
737e6d15924SDimitry Andric   }
738e6d15924SDimitry Andric }
739e6d15924SDimitry Andric 
parseBlock(unsigned BlockID,unsigned IndentLevel,std::optional<BCDumpOptions> O,std::optional<StringRef> CheckHash)740e6d15924SDimitry Andric Error BitcodeAnalyzer::parseBlock(unsigned BlockID, unsigned IndentLevel,
741e3b55780SDimitry Andric                                   std::optional<BCDumpOptions> O,
742e3b55780SDimitry Andric                                   std::optional<StringRef> CheckHash) {
743e6d15924SDimitry Andric   std::string Indent(IndentLevel * 2, ' ');
744e6d15924SDimitry Andric   uint64_t BlockBitStart = Stream.GetCurrentBitNo();
745e6d15924SDimitry Andric 
746e6d15924SDimitry Andric   // Get the statistics for this BlockID.
747e6d15924SDimitry Andric   PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
748e6d15924SDimitry Andric 
749e6d15924SDimitry Andric   BlockStats.NumInstances++;
750e6d15924SDimitry Andric 
751e6d15924SDimitry Andric   // BLOCKINFO is a special part of the stream.
752145449b1SDimitry Andric   bool DumpRecords = O.has_value();
753e6d15924SDimitry Andric   if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
754c0981da4SDimitry Andric     if (O && !O->DumpBlockinfo)
755e6d15924SDimitry Andric       O->OS << Indent << "<BLOCKINFO_BLOCK/>\n";
756e3b55780SDimitry Andric     std::optional<BitstreamBlockInfo> NewBlockInfo;
757c0981da4SDimitry Andric     if (Error E = Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true)
758c0981da4SDimitry Andric                       .moveInto(NewBlockInfo))
759c0981da4SDimitry Andric       return E;
760e6d15924SDimitry Andric     if (!NewBlockInfo)
761e6d15924SDimitry Andric       return reportError("Malformed BlockInfoBlock");
762e6d15924SDimitry Andric     BlockInfo = std::move(*NewBlockInfo);
763e6d15924SDimitry Andric     if (Error Err = Stream.JumpToBit(BlockBitStart))
764e6d15924SDimitry Andric       return Err;
765e6d15924SDimitry Andric     // It's not really interesting to dump the contents of the blockinfo
766c0981da4SDimitry Andric     // block, so only do it if the user explicitly requests it.
767c0981da4SDimitry Andric     DumpRecords = O && O->DumpBlockinfo;
768e6d15924SDimitry Andric   }
769e6d15924SDimitry Andric 
770e6d15924SDimitry Andric   unsigned NumWords = 0;
771e6d15924SDimitry Andric   if (Error Err = Stream.EnterSubBlock(BlockID, &NumWords))
772e6d15924SDimitry Andric     return Err;
773e6d15924SDimitry Andric 
774e6d15924SDimitry Andric   // Keep it for later, when we see a MODULE_HASH record
775e6d15924SDimitry Andric   uint64_t BlockEntryPos = Stream.getCurrentByteNo();
776e6d15924SDimitry Andric 
777e3b55780SDimitry Andric   std::optional<const char *> BlockName;
778e6d15924SDimitry Andric   if (DumpRecords) {
779e6d15924SDimitry Andric     O->OS << Indent << "<";
780e6d15924SDimitry Andric     if ((BlockName = GetBlockName(BlockID, BlockInfo, CurStreamType)))
781e6d15924SDimitry Andric       O->OS << *BlockName;
782e6d15924SDimitry Andric     else
783e6d15924SDimitry Andric       O->OS << "UnknownBlock" << BlockID;
784e6d15924SDimitry Andric 
785e6d15924SDimitry Andric     if (!O->Symbolic && BlockName)
786e6d15924SDimitry Andric       O->OS << " BlockID=" << BlockID;
787e6d15924SDimitry Andric 
788e6d15924SDimitry Andric     O->OS << " NumWords=" << NumWords
789e6d15924SDimitry Andric           << " BlockCodeSize=" << Stream.getAbbrevIDWidth() << ">\n";
790e6d15924SDimitry Andric   }
791e6d15924SDimitry Andric 
792e6d15924SDimitry Andric   SmallVector<uint64_t, 64> Record;
793e6d15924SDimitry Andric 
794e6d15924SDimitry Andric   // Keep the offset to the metadata index if seen.
795e6d15924SDimitry Andric   uint64_t MetadataIndexOffset = 0;
796e6d15924SDimitry Andric 
797e6d15924SDimitry Andric   // Read all the records for this block.
7986f8fc217SDimitry Andric   while (true) {
799e6d15924SDimitry Andric     if (Stream.AtEndOfStream())
800e6d15924SDimitry Andric       return reportError("Premature end of bitstream");
801e6d15924SDimitry Andric 
802e6d15924SDimitry Andric     uint64_t RecordStartBit = Stream.GetCurrentBitNo();
803e6d15924SDimitry Andric 
804c0981da4SDimitry Andric     BitstreamEntry Entry;
805c0981da4SDimitry Andric     if (Error E = Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs)
806c0981da4SDimitry Andric                       .moveInto(Entry))
807c0981da4SDimitry Andric       return E;
808e6d15924SDimitry Andric 
809e6d15924SDimitry Andric     switch (Entry.Kind) {
810e6d15924SDimitry Andric     case BitstreamEntry::Error:
811e6d15924SDimitry Andric       return reportError("malformed bitcode file");
812e6d15924SDimitry Andric     case BitstreamEntry::EndBlock: {
813e6d15924SDimitry Andric       uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
814e6d15924SDimitry Andric       BlockStats.NumBits += BlockBitEnd - BlockBitStart;
815e6d15924SDimitry Andric       if (DumpRecords) {
816e6d15924SDimitry Andric         O->OS << Indent << "</";
817e6d15924SDimitry Andric         if (BlockName)
818e6d15924SDimitry Andric           O->OS << *BlockName << ">\n";
819e6d15924SDimitry Andric         else
820e6d15924SDimitry Andric           O->OS << "UnknownBlock" << BlockID << ">\n";
821e6d15924SDimitry Andric       }
822e6d15924SDimitry Andric       return Error::success();
823e6d15924SDimitry Andric     }
824e6d15924SDimitry Andric 
825e6d15924SDimitry Andric     case BitstreamEntry::SubBlock: {
826e6d15924SDimitry Andric       uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
827e6d15924SDimitry Andric       if (Error E = parseBlock(Entry.ID, IndentLevel + 1, O, CheckHash))
828e6d15924SDimitry Andric         return E;
829e6d15924SDimitry Andric       ++BlockStats.NumSubBlocks;
830e6d15924SDimitry Andric       uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
831e6d15924SDimitry Andric 
832e6d15924SDimitry Andric       // Don't include subblock sizes in the size of this block.
833e6d15924SDimitry Andric       BlockBitStart += SubBlockBitEnd - SubBlockBitStart;
834e6d15924SDimitry Andric       continue;
835e6d15924SDimitry Andric     }
836e6d15924SDimitry Andric     case BitstreamEntry::Record:
837e6d15924SDimitry Andric       // The interesting case.
838e6d15924SDimitry Andric       break;
839e6d15924SDimitry Andric     }
840e6d15924SDimitry Andric 
841e6d15924SDimitry Andric     if (Entry.ID == bitc::DEFINE_ABBREV) {
842e6d15924SDimitry Andric       if (Error Err = Stream.ReadAbbrevRecord())
843e6d15924SDimitry Andric         return Err;
844e6d15924SDimitry Andric       ++BlockStats.NumAbbrevs;
845e6d15924SDimitry Andric       continue;
846e6d15924SDimitry Andric     }
847e6d15924SDimitry Andric 
848e6d15924SDimitry Andric     Record.clear();
849e6d15924SDimitry Andric 
850e6d15924SDimitry Andric     ++BlockStats.NumRecords;
851e6d15924SDimitry Andric 
852e6d15924SDimitry Andric     StringRef Blob;
853e6d15924SDimitry Andric     uint64_t CurrentRecordPos = Stream.GetCurrentBitNo();
854c0981da4SDimitry Andric     unsigned Code;
855c0981da4SDimitry Andric     if (Error E = Stream.readRecord(Entry.ID, Record, &Blob).moveInto(Code))
856c0981da4SDimitry Andric       return E;
857e6d15924SDimitry Andric 
858e6d15924SDimitry Andric     // Increment the # occurrences of this code.
859e6d15924SDimitry Andric     if (BlockStats.CodeFreq.size() <= Code)
860e6d15924SDimitry Andric       BlockStats.CodeFreq.resize(Code + 1);
861e6d15924SDimitry Andric     BlockStats.CodeFreq[Code].NumInstances++;
862e6d15924SDimitry Andric     BlockStats.CodeFreq[Code].TotalBits +=
863e6d15924SDimitry Andric         Stream.GetCurrentBitNo() - RecordStartBit;
864e6d15924SDimitry Andric     if (Entry.ID != bitc::UNABBREV_RECORD) {
865e6d15924SDimitry Andric       BlockStats.CodeFreq[Code].NumAbbrev++;
866e6d15924SDimitry Andric       ++BlockStats.NumAbbreviatedRecords;
867e6d15924SDimitry Andric     }
868e6d15924SDimitry Andric 
869e6d15924SDimitry Andric     if (DumpRecords) {
870e6d15924SDimitry Andric       O->OS << Indent << "  <";
871e3b55780SDimitry Andric       std::optional<const char *> CodeName =
872e6d15924SDimitry Andric           GetCodeName(Code, BlockID, BlockInfo, CurStreamType);
873e6d15924SDimitry Andric       if (CodeName)
874e6d15924SDimitry Andric         O->OS << *CodeName;
875e6d15924SDimitry Andric       else
876e6d15924SDimitry Andric         O->OS << "UnknownCode" << Code;
877e6d15924SDimitry Andric       if (!O->Symbolic && CodeName)
878e6d15924SDimitry Andric         O->OS << " codeid=" << Code;
879e6d15924SDimitry Andric       const BitCodeAbbrev *Abbv = nullptr;
880e6d15924SDimitry Andric       if (Entry.ID != bitc::UNABBREV_RECORD) {
881145449b1SDimitry Andric         Expected<const BitCodeAbbrev *> MaybeAbbv = Stream.getAbbrev(Entry.ID);
882145449b1SDimitry Andric         if (!MaybeAbbv)
883145449b1SDimitry Andric           return MaybeAbbv.takeError();
884145449b1SDimitry Andric         Abbv = MaybeAbbv.get();
885e6d15924SDimitry Andric         O->OS << " abbrevid=" << Entry.ID;
886e6d15924SDimitry Andric       }
887e6d15924SDimitry Andric 
888e6d15924SDimitry Andric       for (unsigned i = 0, e = Record.size(); i != e; ++i)
889e6d15924SDimitry Andric         O->OS << " op" << i << "=" << (int64_t)Record[i];
890e6d15924SDimitry Andric 
891e6d15924SDimitry Andric       // If we found a metadata index, let's verify that we had an offset
892e6d15924SDimitry Andric       // before and validate its forward reference offset was correct!
893e6d15924SDimitry Andric       if (BlockID == bitc::METADATA_BLOCK_ID) {
894e6d15924SDimitry Andric         if (Code == bitc::METADATA_INDEX_OFFSET) {
895e6d15924SDimitry Andric           if (Record.size() != 2)
896e6d15924SDimitry Andric             O->OS << "(Invalid record)";
897e6d15924SDimitry Andric           else {
898e6d15924SDimitry Andric             auto Offset = Record[0] + (Record[1] << 32);
899e6d15924SDimitry Andric             MetadataIndexOffset = Stream.GetCurrentBitNo() + Offset;
900e6d15924SDimitry Andric           }
901e6d15924SDimitry Andric         }
902e6d15924SDimitry Andric         if (Code == bitc::METADATA_INDEX) {
903e6d15924SDimitry Andric           O->OS << " (offset ";
904e6d15924SDimitry Andric           if (MetadataIndexOffset == RecordStartBit)
905e6d15924SDimitry Andric             O->OS << "match)";
906e6d15924SDimitry Andric           else
907e6d15924SDimitry Andric             O->OS << "mismatch: " << MetadataIndexOffset << " vs "
908e6d15924SDimitry Andric                   << RecordStartBit << ")";
909e6d15924SDimitry Andric         }
910e6d15924SDimitry Andric       }
911e6d15924SDimitry Andric 
912e6d15924SDimitry Andric       // If we found a module hash, let's verify that it matches!
913e6d15924SDimitry Andric       if (BlockID == bitc::MODULE_BLOCK_ID && Code == bitc::MODULE_CODE_HASH &&
914145449b1SDimitry Andric           CheckHash) {
915e6d15924SDimitry Andric         if (Record.size() != 5)
916e6d15924SDimitry Andric           O->OS << " (invalid)";
917e6d15924SDimitry Andric         else {
918e6d15924SDimitry Andric           // Recompute the hash and compare it to the one in the bitcode
919e6d15924SDimitry Andric           SHA1 Hasher;
920145449b1SDimitry Andric           std::array<uint8_t, 20> Hash;
921e6d15924SDimitry Andric           Hasher.update(*CheckHash);
922e6d15924SDimitry Andric           {
923e6d15924SDimitry Andric             int BlockSize = (CurrentRecordPos / 8) - BlockEntryPos;
924e6d15924SDimitry Andric             auto Ptr = Stream.getPointerToByte(BlockEntryPos, BlockSize);
925e6d15924SDimitry Andric             Hasher.update(ArrayRef<uint8_t>(Ptr, BlockSize));
926e6d15924SDimitry Andric             Hash = Hasher.result();
927e6d15924SDimitry Andric           }
928145449b1SDimitry Andric           std::array<uint8_t, 20> RecordedHash;
929e6d15924SDimitry Andric           int Pos = 0;
930e6d15924SDimitry Andric           for (auto &Val : Record) {
931e6d15924SDimitry Andric             assert(!(Val >> 32) && "Unexpected high bits set");
932cfca06d7SDimitry Andric             support::endian::write32be(&RecordedHash[Pos], Val);
933cfca06d7SDimitry Andric             Pos += 4;
934e6d15924SDimitry Andric           }
935145449b1SDimitry Andric           if (Hash == RecordedHash)
936e6d15924SDimitry Andric             O->OS << " (match)";
937e6d15924SDimitry Andric           else
938e6d15924SDimitry Andric             O->OS << " (!mismatch!)";
939e6d15924SDimitry Andric         }
940e6d15924SDimitry Andric       }
941e6d15924SDimitry Andric 
942e6d15924SDimitry Andric       O->OS << "/>";
943e6d15924SDimitry Andric 
944e6d15924SDimitry Andric       if (Abbv) {
945e6d15924SDimitry Andric         for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
946e6d15924SDimitry Andric           const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
947e6d15924SDimitry Andric           if (!Op.isEncoding() || Op.getEncoding() != BitCodeAbbrevOp::Array)
948e6d15924SDimitry Andric             continue;
949e6d15924SDimitry Andric           assert(i + 2 == e && "Array op not second to last");
950e6d15924SDimitry Andric           std::string Str;
951e6d15924SDimitry Andric           bool ArrayIsPrintable = true;
952e6d15924SDimitry Andric           for (unsigned j = i - 1, je = Record.size(); j != je; ++j) {
953e6d15924SDimitry Andric             if (!isPrint(static_cast<unsigned char>(Record[j]))) {
954e6d15924SDimitry Andric               ArrayIsPrintable = false;
955e6d15924SDimitry Andric               break;
956e6d15924SDimitry Andric             }
957e6d15924SDimitry Andric             Str += (char)Record[j];
958e6d15924SDimitry Andric           }
959e6d15924SDimitry Andric           if (ArrayIsPrintable)
960e6d15924SDimitry Andric             O->OS << " record string = '" << Str << "'";
961e6d15924SDimitry Andric           break;
962e6d15924SDimitry Andric         }
963e6d15924SDimitry Andric       }
964e6d15924SDimitry Andric 
965e6d15924SDimitry Andric       if (Blob.data()) {
966e6d15924SDimitry Andric         if (canDecodeBlob(Code, BlockID)) {
967e6d15924SDimitry Andric           if (Error E = decodeMetadataStringsBlob(Indent, Record, Blob, O->OS))
968e6d15924SDimitry Andric             return E;
969e6d15924SDimitry Andric         } else {
970e6d15924SDimitry Andric           O->OS << " blob data = ";
971e6d15924SDimitry Andric           if (O->ShowBinaryBlobs) {
972e6d15924SDimitry Andric             O->OS << "'";
973e6d15924SDimitry Andric             O->OS.write_escaped(Blob, /*hex=*/true) << "'";
974e6d15924SDimitry Andric           } else {
975e6d15924SDimitry Andric             bool BlobIsPrintable = true;
976f65dcba8SDimitry Andric             for (char C : Blob)
977f65dcba8SDimitry Andric               if (!isPrint(static_cast<unsigned char>(C))) {
978e6d15924SDimitry Andric                 BlobIsPrintable = false;
979e6d15924SDimitry Andric                 break;
980e6d15924SDimitry Andric               }
981e6d15924SDimitry Andric 
982e6d15924SDimitry Andric             if (BlobIsPrintable)
983e6d15924SDimitry Andric               O->OS << "'" << Blob << "'";
984e6d15924SDimitry Andric             else
985e6d15924SDimitry Andric               O->OS << "unprintable, " << Blob.size() << " bytes.";
986e6d15924SDimitry Andric           }
987e6d15924SDimitry Andric         }
988e6d15924SDimitry Andric       }
989e6d15924SDimitry Andric 
990e6d15924SDimitry Andric       O->OS << "\n";
991e6d15924SDimitry Andric     }
992e6d15924SDimitry Andric 
993e6d15924SDimitry Andric     // Make sure that we can skip the current record.
994e6d15924SDimitry Andric     if (Error Err = Stream.JumpToBit(CurrentRecordPos))
995e6d15924SDimitry Andric       return Err;
996e6d15924SDimitry Andric     if (Expected<unsigned> Skipped = Stream.skipRecord(Entry.ID))
997e6d15924SDimitry Andric       ; // Do nothing.
998e6d15924SDimitry Andric     else
999e6d15924SDimitry Andric       return Skipped.takeError();
1000e6d15924SDimitry Andric   }
1001e6d15924SDimitry Andric }
1002e6d15924SDimitry Andric 
1003