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