File: | include/llvm/Bitcode/BitstreamReader.h |
Warning: | line 210, column 39 The result of the right shift is undefined due to shifting by '64', which is greater or equal to the width of type 'llvm::SimpleBitstreamCursor::word_t' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===-- llvm-bcanalyzer.cpp - Bitcode Analyzer --------------------------===// | |||
2 | // | |||
3 | // The LLVM Compiler Infrastructure | |||
4 | // | |||
5 | // This file is distributed under the University of Illinois Open Source | |||
6 | // License. See LICENSE.TXT for details. | |||
7 | // | |||
8 | //===----------------------------------------------------------------------===// | |||
9 | // | |||
10 | // This tool may be invoked in the following manner: | |||
11 | // llvm-bcanalyzer [options] - Read LLVM bitcode from stdin | |||
12 | // llvm-bcanalyzer [options] x.bc - Read LLVM bitcode from the x.bc file | |||
13 | // | |||
14 | // Options: | |||
15 | // --help - Output information about command line switches | |||
16 | // --dump - Dump low-level bitcode structure in readable format | |||
17 | // | |||
18 | // This tool provides analytical information about a bitcode file. It is | |||
19 | // intended as an aid to developers of bitcode reading and writing software. It | |||
20 | // produces on std::out a summary of the bitcode file that shows various | |||
21 | // statistics about the contents of the file. By default this information is | |||
22 | // detailed and contains information about individual bitcode blocks and the | |||
23 | // functions in the module. | |||
24 | // The tool is also able to print a bitcode file in a straight forward text | |||
25 | // format that shows the containment and relationships of the information in | |||
26 | // the bitcode file (-dump option). | |||
27 | // | |||
28 | //===----------------------------------------------------------------------===// | |||
29 | ||||
30 | #include "llvm/ADT/StringExtras.h" | |||
31 | #include "llvm/Bitcode/BitcodeReader.h" | |||
32 | #include "llvm/Bitcode/BitstreamReader.h" | |||
33 | #include "llvm/Bitcode/LLVMBitCodes.h" | |||
34 | #include "llvm/Support/CommandLine.h" | |||
35 | #include "llvm/Support/Format.h" | |||
36 | #include "llvm/Support/InitLLVM.h" | |||
37 | #include "llvm/Support/ManagedStatic.h" | |||
38 | #include "llvm/Support/MemoryBuffer.h" | |||
39 | #include "llvm/Support/SHA1.h" | |||
40 | #include "llvm/Support/WithColor.h" | |||
41 | #include "llvm/Support/raw_ostream.h" | |||
42 | using namespace llvm; | |||
43 | ||||
44 | static cl::opt<std::string> | |||
45 | InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); | |||
46 | ||||
47 | static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace")); | |||
48 | ||||
49 | //===----------------------------------------------------------------------===// | |||
50 | // Bitcode specific analysis. | |||
51 | //===----------------------------------------------------------------------===// | |||
52 | ||||
53 | static cl::opt<bool> NoHistogram("disable-histogram", | |||
54 | cl::desc("Do not print per-code histogram")); | |||
55 | ||||
56 | static cl::opt<bool> | |||
57 | NonSymbolic("non-symbolic", | |||
58 | cl::desc("Emit numeric info in dump even if" | |||
59 | " symbolic info is available")); | |||
60 | ||||
61 | static cl::opt<std::string> | |||
62 | BlockInfoFilename("block-info", | |||
63 | cl::desc("Use the BLOCK_INFO from the given file")); | |||
64 | ||||
65 | static cl::opt<bool> | |||
66 | ShowBinaryBlobs("show-binary-blobs", | |||
67 | cl::desc("Print binary blobs using hex escapes")); | |||
68 | ||||
69 | static cl::opt<std::string> CheckHash( | |||
70 | "check-hash", | |||
71 | cl::desc("Check module hash using the argument as a string table")); | |||
72 | ||||
73 | namespace { | |||
74 | ||||
75 | /// CurStreamTypeType - A type for CurStreamType | |||
76 | enum CurStreamTypeType { | |||
77 | UnknownBitstream, | |||
78 | LLVMIRBitstream, | |||
79 | ClangSerializedASTBitstream, | |||
80 | ClangSerializedDiagnosticsBitstream, | |||
81 | }; | |||
82 | ||||
83 | } | |||
84 | ||||
85 | /// GetBlockName - Return a symbolic block name if known, otherwise return | |||
86 | /// null. | |||
87 | static const char *GetBlockName(unsigned BlockID, | |||
88 | const BitstreamBlockInfo &BlockInfo, | |||
89 | CurStreamTypeType CurStreamType) { | |||
90 | // Standard blocks for all bitcode files. | |||
91 | if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) { | |||
92 | if (BlockID == bitc::BLOCKINFO_BLOCK_ID) | |||
93 | return "BLOCKINFO_BLOCK"; | |||
94 | return nullptr; | |||
95 | } | |||
96 | ||||
97 | // Check to see if we have a blockinfo record for this block, with a name. | |||
98 | if (const BitstreamBlockInfo::BlockInfo *Info = | |||
99 | BlockInfo.getBlockInfo(BlockID)) { | |||
100 | if (!Info->Name.empty()) | |||
101 | return Info->Name.c_str(); | |||
102 | } | |||
103 | ||||
104 | ||||
105 | if (CurStreamType != LLVMIRBitstream) return nullptr; | |||
106 | ||||
107 | switch (BlockID) { | |||
108 | default: return nullptr; | |||
109 | case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: return "OPERAND_BUNDLE_TAGS_BLOCK"; | |||
110 | case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK"; | |||
111 | case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK"; | |||
112 | case bitc::PARAMATTR_GROUP_BLOCK_ID: return "PARAMATTR_GROUP_BLOCK_ID"; | |||
113 | case bitc::TYPE_BLOCK_ID_NEW: return "TYPE_BLOCK_ID"; | |||
114 | case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK"; | |||
115 | case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK"; | |||
116 | case bitc::IDENTIFICATION_BLOCK_ID: | |||
117 | return "IDENTIFICATION_BLOCK_ID"; | |||
118 | case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB"; | |||
119 | case bitc::METADATA_BLOCK_ID: return "METADATA_BLOCK"; | |||
120 | case bitc::METADATA_KIND_BLOCK_ID: return "METADATA_KIND_BLOCK"; | |||
121 | case bitc::METADATA_ATTACHMENT_ID: return "METADATA_ATTACHMENT_BLOCK"; | |||
122 | case bitc::USELIST_BLOCK_ID: return "USELIST_BLOCK_ID"; | |||
123 | case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: | |||
124 | return "GLOBALVAL_SUMMARY_BLOCK"; | |||
125 | case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID: | |||
126 | return "FULL_LTO_GLOBALVAL_SUMMARY_BLOCK"; | |||
127 | case bitc::MODULE_STRTAB_BLOCK_ID: return "MODULE_STRTAB_BLOCK"; | |||
128 | case bitc::STRTAB_BLOCK_ID: return "STRTAB_BLOCK"; | |||
129 | case bitc::SYMTAB_BLOCK_ID: return "SYMTAB_BLOCK"; | |||
130 | } | |||
131 | } | |||
132 | ||||
133 | /// GetCodeName - Return a symbolic code name if known, otherwise return | |||
134 | /// null. | |||
135 | static const char *GetCodeName(unsigned CodeID, unsigned BlockID, | |||
136 | const BitstreamBlockInfo &BlockInfo, | |||
137 | CurStreamTypeType CurStreamType) { | |||
138 | // Standard blocks for all bitcode files. | |||
139 | if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) { | |||
140 | if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { | |||
141 | switch (CodeID) { | |||
142 | default: return nullptr; | |||
143 | case bitc::BLOCKINFO_CODE_SETBID: return "SETBID"; | |||
144 | case bitc::BLOCKINFO_CODE_BLOCKNAME: return "BLOCKNAME"; | |||
145 | case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME"; | |||
146 | } | |||
147 | } | |||
148 | return nullptr; | |||
149 | } | |||
150 | ||||
151 | // Check to see if we have a blockinfo record for this record, with a name. | |||
152 | if (const BitstreamBlockInfo::BlockInfo *Info = | |||
153 | BlockInfo.getBlockInfo(BlockID)) { | |||
154 | for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i) | |||
155 | if (Info->RecordNames[i].first == CodeID) | |||
156 | return Info->RecordNames[i].second.c_str(); | |||
157 | } | |||
158 | ||||
159 | ||||
160 | if (CurStreamType != LLVMIRBitstream) return nullptr; | |||
161 | ||||
162 | #define STRINGIFY_CODE(PREFIX, CODE) \ | |||
163 | case bitc::PREFIX##_##CODE: \ | |||
164 | return #CODE; | |||
165 | switch (BlockID) { | |||
166 | default: return nullptr; | |||
167 | case bitc::MODULE_BLOCK_ID: | |||
168 | switch (CodeID) { | |||
169 | default: return nullptr; | |||
170 | STRINGIFY_CODE(MODULE_CODE, VERSION) | |||
171 | STRINGIFY_CODE(MODULE_CODE, TRIPLE) | |||
172 | STRINGIFY_CODE(MODULE_CODE, DATALAYOUT) | |||
173 | STRINGIFY_CODE(MODULE_CODE, ASM) | |||
174 | STRINGIFY_CODE(MODULE_CODE, SECTIONNAME) | |||
175 | STRINGIFY_CODE(MODULE_CODE, DEPLIB) // FIXME: Remove in 4.0 | |||
176 | STRINGIFY_CODE(MODULE_CODE, GLOBALVAR) | |||
177 | STRINGIFY_CODE(MODULE_CODE, FUNCTION) | |||
178 | STRINGIFY_CODE(MODULE_CODE, ALIAS) | |||
179 | STRINGIFY_CODE(MODULE_CODE, GCNAME) | |||
180 | STRINGIFY_CODE(MODULE_CODE, VSTOFFSET) | |||
181 | STRINGIFY_CODE(MODULE_CODE, METADATA_VALUES_UNUSED) | |||
182 | STRINGIFY_CODE(MODULE_CODE, SOURCE_FILENAME) | |||
183 | STRINGIFY_CODE(MODULE_CODE, HASH) | |||
184 | } | |||
185 | case bitc::IDENTIFICATION_BLOCK_ID: | |||
186 | switch (CodeID) { | |||
187 | default: | |||
188 | return nullptr; | |||
189 | STRINGIFY_CODE(IDENTIFICATION_CODE, STRING) | |||
190 | STRINGIFY_CODE(IDENTIFICATION_CODE, EPOCH) | |||
191 | } | |||
192 | case bitc::PARAMATTR_BLOCK_ID: | |||
193 | switch (CodeID) { | |||
194 | default: return nullptr; | |||
195 | // FIXME: Should these be different? | |||
196 | case bitc::PARAMATTR_CODE_ENTRY_OLD: return "ENTRY"; | |||
197 | case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY"; | |||
198 | } | |||
199 | case bitc::PARAMATTR_GROUP_BLOCK_ID: | |||
200 | switch (CodeID) { | |||
201 | default: return nullptr; | |||
202 | case bitc::PARAMATTR_GRP_CODE_ENTRY: return "ENTRY"; | |||
203 | } | |||
204 | case bitc::TYPE_BLOCK_ID_NEW: | |||
205 | switch (CodeID) { | |||
206 | default: return nullptr; | |||
207 | STRINGIFY_CODE(TYPE_CODE, NUMENTRY) | |||
208 | STRINGIFY_CODE(TYPE_CODE, VOID) | |||
209 | STRINGIFY_CODE(TYPE_CODE, FLOAT) | |||
210 | STRINGIFY_CODE(TYPE_CODE, DOUBLE) | |||
211 | STRINGIFY_CODE(TYPE_CODE, LABEL) | |||
212 | STRINGIFY_CODE(TYPE_CODE, OPAQUE) | |||
213 | STRINGIFY_CODE(TYPE_CODE, INTEGER) | |||
214 | STRINGIFY_CODE(TYPE_CODE, POINTER) | |||
215 | STRINGIFY_CODE(TYPE_CODE, ARRAY) | |||
216 | STRINGIFY_CODE(TYPE_CODE, VECTOR) | |||
217 | STRINGIFY_CODE(TYPE_CODE, X86_FP80) | |||
218 | STRINGIFY_CODE(TYPE_CODE, FP128) | |||
219 | STRINGIFY_CODE(TYPE_CODE, PPC_FP128) | |||
220 | STRINGIFY_CODE(TYPE_CODE, METADATA) | |||
221 | STRINGIFY_CODE(TYPE_CODE, STRUCT_ANON) | |||
222 | STRINGIFY_CODE(TYPE_CODE, STRUCT_NAME) | |||
223 | STRINGIFY_CODE(TYPE_CODE, STRUCT_NAMED) | |||
224 | STRINGIFY_CODE(TYPE_CODE, FUNCTION) | |||
225 | } | |||
226 | ||||
227 | case bitc::CONSTANTS_BLOCK_ID: | |||
228 | switch (CodeID) { | |||
229 | default: return nullptr; | |||
230 | STRINGIFY_CODE(CST_CODE, SETTYPE) | |||
231 | STRINGIFY_CODE(CST_CODE, NULL__null) | |||
232 | STRINGIFY_CODE(CST_CODE, UNDEF) | |||
233 | STRINGIFY_CODE(CST_CODE, INTEGER) | |||
234 | STRINGIFY_CODE(CST_CODE, WIDE_INTEGER) | |||
235 | STRINGIFY_CODE(CST_CODE, FLOAT) | |||
236 | STRINGIFY_CODE(CST_CODE, AGGREGATE) | |||
237 | STRINGIFY_CODE(CST_CODE, STRING) | |||
238 | STRINGIFY_CODE(CST_CODE, CSTRING) | |||
239 | STRINGIFY_CODE(CST_CODE, CE_BINOP) | |||
240 | STRINGIFY_CODE(CST_CODE, CE_CAST) | |||
241 | STRINGIFY_CODE(CST_CODE, CE_GEP) | |||
242 | STRINGIFY_CODE(CST_CODE, CE_INBOUNDS_GEP) | |||
243 | STRINGIFY_CODE(CST_CODE, CE_SELECT) | |||
244 | STRINGIFY_CODE(CST_CODE, CE_EXTRACTELT) | |||
245 | STRINGIFY_CODE(CST_CODE, CE_INSERTELT) | |||
246 | STRINGIFY_CODE(CST_CODE, CE_SHUFFLEVEC) | |||
247 | STRINGIFY_CODE(CST_CODE, CE_CMP) | |||
248 | STRINGIFY_CODE(CST_CODE, INLINEASM) | |||
249 | STRINGIFY_CODE(CST_CODE, CE_SHUFVEC_EX) | |||
250 | case bitc::CST_CODE_BLOCKADDRESS: return "CST_CODE_BLOCKADDRESS"; | |||
251 | STRINGIFY_CODE(CST_CODE, DATA) | |||
252 | } | |||
253 | case bitc::FUNCTION_BLOCK_ID: | |||
254 | switch (CodeID) { | |||
255 | default: return nullptr; | |||
256 | STRINGIFY_CODE(FUNC_CODE, DECLAREBLOCKS) | |||
257 | STRINGIFY_CODE(FUNC_CODE, INST_BINOP) | |||
258 | STRINGIFY_CODE(FUNC_CODE, INST_CAST) | |||
259 | STRINGIFY_CODE(FUNC_CODE, INST_GEP_OLD) | |||
260 | STRINGIFY_CODE(FUNC_CODE, INST_INBOUNDS_GEP_OLD) | |||
261 | STRINGIFY_CODE(FUNC_CODE, INST_SELECT) | |||
262 | STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTELT) | |||
263 | STRINGIFY_CODE(FUNC_CODE, INST_INSERTELT) | |||
264 | STRINGIFY_CODE(FUNC_CODE, INST_SHUFFLEVEC) | |||
265 | STRINGIFY_CODE(FUNC_CODE, INST_CMP) | |||
266 | STRINGIFY_CODE(FUNC_CODE, INST_RET) | |||
267 | STRINGIFY_CODE(FUNC_CODE, INST_BR) | |||
268 | STRINGIFY_CODE(FUNC_CODE, INST_SWITCH) | |||
269 | STRINGIFY_CODE(FUNC_CODE, INST_INVOKE) | |||
270 | STRINGIFY_CODE(FUNC_CODE, INST_UNREACHABLE) | |||
271 | STRINGIFY_CODE(FUNC_CODE, INST_CLEANUPRET) | |||
272 | STRINGIFY_CODE(FUNC_CODE, INST_CATCHRET) | |||
273 | STRINGIFY_CODE(FUNC_CODE, INST_CATCHPAD) | |||
274 | STRINGIFY_CODE(FUNC_CODE, INST_PHI) | |||
275 | STRINGIFY_CODE(FUNC_CODE, INST_ALLOCA) | |||
276 | STRINGIFY_CODE(FUNC_CODE, INST_LOAD) | |||
277 | STRINGIFY_CODE(FUNC_CODE, INST_VAARG) | |||
278 | STRINGIFY_CODE(FUNC_CODE, INST_STORE) | |||
279 | STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTVAL) | |||
280 | STRINGIFY_CODE(FUNC_CODE, INST_INSERTVAL) | |||
281 | STRINGIFY_CODE(FUNC_CODE, INST_CMP2) | |||
282 | STRINGIFY_CODE(FUNC_CODE, INST_VSELECT) | |||
283 | STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC_AGAIN) | |||
284 | STRINGIFY_CODE(FUNC_CODE, INST_CALL) | |||
285 | STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC) | |||
286 | STRINGIFY_CODE(FUNC_CODE, INST_GEP) | |||
287 | STRINGIFY_CODE(FUNC_CODE, OPERAND_BUNDLE) | |||
288 | STRINGIFY_CODE(FUNC_CODE, INST_FENCE) | |||
289 | STRINGIFY_CODE(FUNC_CODE, INST_ATOMICRMW) | |||
290 | STRINGIFY_CODE(FUNC_CODE, INST_LOADATOMIC) | |||
291 | STRINGIFY_CODE(FUNC_CODE, INST_STOREATOMIC) | |||
292 | STRINGIFY_CODE(FUNC_CODE, INST_CMPXCHG) | |||
293 | } | |||
294 | case bitc::VALUE_SYMTAB_BLOCK_ID: | |||
295 | switch (CodeID) { | |||
296 | default: return nullptr; | |||
297 | STRINGIFY_CODE(VST_CODE, ENTRY) | |||
298 | STRINGIFY_CODE(VST_CODE, BBENTRY) | |||
299 | STRINGIFY_CODE(VST_CODE, FNENTRY) | |||
300 | STRINGIFY_CODE(VST_CODE, COMBINED_ENTRY) | |||
301 | } | |||
302 | case bitc::MODULE_STRTAB_BLOCK_ID: | |||
303 | switch (CodeID) { | |||
304 | default: | |||
305 | return nullptr; | |||
306 | STRINGIFY_CODE(MST_CODE, ENTRY) | |||
307 | STRINGIFY_CODE(MST_CODE, HASH) | |||
308 | } | |||
309 | case bitc::GLOBALVAL_SUMMARY_BLOCK_ID: | |||
310 | case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID: | |||
311 | switch (CodeID) { | |||
312 | default: | |||
313 | return nullptr; | |||
314 | STRINGIFY_CODE(FS, PERMODULE) | |||
315 | STRINGIFY_CODE(FS, PERMODULE_PROFILE) | |||
316 | STRINGIFY_CODE(FS, PERMODULE_RELBF) | |||
317 | STRINGIFY_CODE(FS, PERMODULE_GLOBALVAR_INIT_REFS) | |||
318 | STRINGIFY_CODE(FS, COMBINED) | |||
319 | STRINGIFY_CODE(FS, COMBINED_PROFILE) | |||
320 | STRINGIFY_CODE(FS, COMBINED_GLOBALVAR_INIT_REFS) | |||
321 | STRINGIFY_CODE(FS, ALIAS) | |||
322 | STRINGIFY_CODE(FS, COMBINED_ALIAS) | |||
323 | STRINGIFY_CODE(FS, COMBINED_ORIGINAL_NAME) | |||
324 | STRINGIFY_CODE(FS, VERSION) | |||
325 | STRINGIFY_CODE(FS, FLAGS) | |||
326 | STRINGIFY_CODE(FS, TYPE_TESTS) | |||
327 | STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_VCALLS) | |||
328 | STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_VCALLS) | |||
329 | STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_CONST_VCALL) | |||
330 | STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_CONST_VCALL) | |||
331 | STRINGIFY_CODE(FS, VALUE_GUID) | |||
332 | STRINGIFY_CODE(FS, CFI_FUNCTION_DEFS) | |||
333 | STRINGIFY_CODE(FS, CFI_FUNCTION_DECLS) | |||
334 | STRINGIFY_CODE(FS, TYPE_ID) | |||
335 | } | |||
336 | case bitc::METADATA_ATTACHMENT_ID: | |||
337 | switch(CodeID) { | |||
338 | default:return nullptr; | |||
339 | STRINGIFY_CODE(METADATA, ATTACHMENT) | |||
340 | } | |||
341 | case bitc::METADATA_BLOCK_ID: | |||
342 | switch(CodeID) { | |||
343 | default:return nullptr; | |||
344 | STRINGIFY_CODE(METADATA, STRING_OLD) | |||
345 | STRINGIFY_CODE(METADATA, VALUE) | |||
346 | STRINGIFY_CODE(METADATA, NODE) | |||
347 | STRINGIFY_CODE(METADATA, NAME) | |||
348 | STRINGIFY_CODE(METADATA, DISTINCT_NODE) | |||
349 | STRINGIFY_CODE(METADATA, KIND) // Older bitcode has it in a MODULE_BLOCK | |||
350 | STRINGIFY_CODE(METADATA, LOCATION) | |||
351 | STRINGIFY_CODE(METADATA, OLD_NODE) | |||
352 | STRINGIFY_CODE(METADATA, OLD_FN_NODE) | |||
353 | STRINGIFY_CODE(METADATA, NAMED_NODE) | |||
354 | STRINGIFY_CODE(METADATA, GENERIC_DEBUG) | |||
355 | STRINGIFY_CODE(METADATA, SUBRANGE) | |||
356 | STRINGIFY_CODE(METADATA, ENUMERATOR) | |||
357 | STRINGIFY_CODE(METADATA, BASIC_TYPE) | |||
358 | STRINGIFY_CODE(METADATA, FILE) | |||
359 | STRINGIFY_CODE(METADATA, DERIVED_TYPE) | |||
360 | STRINGIFY_CODE(METADATA, COMPOSITE_TYPE) | |||
361 | STRINGIFY_CODE(METADATA, SUBROUTINE_TYPE) | |||
362 | STRINGIFY_CODE(METADATA, COMPILE_UNIT) | |||
363 | STRINGIFY_CODE(METADATA, SUBPROGRAM) | |||
364 | STRINGIFY_CODE(METADATA, LEXICAL_BLOCK) | |||
365 | STRINGIFY_CODE(METADATA, LEXICAL_BLOCK_FILE) | |||
366 | STRINGIFY_CODE(METADATA, NAMESPACE) | |||
367 | STRINGIFY_CODE(METADATA, TEMPLATE_TYPE) | |||
368 | STRINGIFY_CODE(METADATA, TEMPLATE_VALUE) | |||
369 | STRINGIFY_CODE(METADATA, GLOBAL_VAR) | |||
370 | STRINGIFY_CODE(METADATA, LOCAL_VAR) | |||
371 | STRINGIFY_CODE(METADATA, EXPRESSION) | |||
372 | STRINGIFY_CODE(METADATA, OBJC_PROPERTY) | |||
373 | STRINGIFY_CODE(METADATA, IMPORTED_ENTITY) | |||
374 | STRINGIFY_CODE(METADATA, MODULE) | |||
375 | STRINGIFY_CODE(METADATA, MACRO) | |||
376 | STRINGIFY_CODE(METADATA, MACRO_FILE) | |||
377 | STRINGIFY_CODE(METADATA, STRINGS) | |||
378 | STRINGIFY_CODE(METADATA, GLOBAL_DECL_ATTACHMENT) | |||
379 | STRINGIFY_CODE(METADATA, GLOBAL_VAR_EXPR) | |||
380 | STRINGIFY_CODE(METADATA, INDEX_OFFSET) | |||
381 | STRINGIFY_CODE(METADATA, INDEX) | |||
382 | } | |||
383 | case bitc::METADATA_KIND_BLOCK_ID: | |||
384 | switch (CodeID) { | |||
385 | default: | |||
386 | return nullptr; | |||
387 | STRINGIFY_CODE(METADATA, KIND) | |||
388 | } | |||
389 | case bitc::USELIST_BLOCK_ID: | |||
390 | switch(CodeID) { | |||
391 | default:return nullptr; | |||
392 | case bitc::USELIST_CODE_DEFAULT: return "USELIST_CODE_DEFAULT"; | |||
393 | case bitc::USELIST_CODE_BB: return "USELIST_CODE_BB"; | |||
394 | } | |||
395 | ||||
396 | case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: | |||
397 | switch(CodeID) { | |||
398 | default: return nullptr; | |||
399 | case bitc::OPERAND_BUNDLE_TAG: return "OPERAND_BUNDLE_TAG"; | |||
400 | } | |||
401 | case bitc::STRTAB_BLOCK_ID: | |||
402 | switch(CodeID) { | |||
403 | default: return nullptr; | |||
404 | case bitc::STRTAB_BLOB: return "BLOB"; | |||
405 | } | |||
406 | case bitc::SYMTAB_BLOCK_ID: | |||
407 | switch(CodeID) { | |||
408 | default: return nullptr; | |||
409 | case bitc::SYMTAB_BLOB: return "BLOB"; | |||
410 | } | |||
411 | } | |||
412 | #undef STRINGIFY_CODE | |||
413 | } | |||
414 | ||||
415 | struct PerRecordStats { | |||
416 | unsigned NumInstances; | |||
417 | unsigned NumAbbrev; | |||
418 | uint64_t TotalBits; | |||
419 | ||||
420 | PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {} | |||
421 | }; | |||
422 | ||||
423 | struct PerBlockIDStats { | |||
424 | /// NumInstances - This the number of times this block ID has been seen. | |||
425 | unsigned NumInstances; | |||
426 | ||||
427 | /// NumBits - The total size in bits of all of these blocks. | |||
428 | uint64_t NumBits; | |||
429 | ||||
430 | /// NumSubBlocks - The total number of blocks these blocks contain. | |||
431 | unsigned NumSubBlocks; | |||
432 | ||||
433 | /// NumAbbrevs - The total number of abbreviations. | |||
434 | unsigned NumAbbrevs; | |||
435 | ||||
436 | /// NumRecords - The total number of records these blocks contain, and the | |||
437 | /// number that are abbreviated. | |||
438 | unsigned NumRecords, NumAbbreviatedRecords; | |||
439 | ||||
440 | /// CodeFreq - Keep track of the number of times we see each code. | |||
441 | std::vector<PerRecordStats> CodeFreq; | |||
442 | ||||
443 | PerBlockIDStats() | |||
444 | : NumInstances(0), NumBits(0), | |||
445 | NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {} | |||
446 | }; | |||
447 | ||||
448 | static std::map<unsigned, PerBlockIDStats> BlockIDStats; | |||
449 | ||||
450 | ||||
451 | ||||
452 | /// ReportError - All bitcode analysis errors go through this function, making this a | |||
453 | /// good place to breakpoint if debugging. | |||
454 | static bool ReportError(const Twine &Err) { | |||
455 | WithColor::error() << Err << "\n"; | |||
456 | return true; | |||
457 | } | |||
458 | ||||
459 | static bool decodeMetadataStringsBlob(StringRef Indent, | |||
460 | ArrayRef<uint64_t> Record, | |||
461 | StringRef Blob) { | |||
462 | if (Blob.empty()) | |||
463 | return true; | |||
464 | ||||
465 | if (Record.size() != 2) | |||
466 | return true; | |||
467 | ||||
468 | unsigned NumStrings = Record[0]; | |||
469 | unsigned StringsOffset = Record[1]; | |||
470 | outs() << " num-strings = " << NumStrings << " {\n"; | |||
471 | ||||
472 | StringRef Lengths = Blob.slice(0, StringsOffset); | |||
473 | SimpleBitstreamCursor R(Lengths); | |||
474 | StringRef Strings = Blob.drop_front(StringsOffset); | |||
475 | do { | |||
476 | if (R.AtEndOfStream()) | |||
477 | return ReportError("bad length"); | |||
478 | ||||
479 | unsigned Size = R.ReadVBR(6); | |||
480 | if (Strings.size() < Size) | |||
481 | return ReportError("truncated chars"); | |||
482 | ||||
483 | outs() << Indent << " '"; | |||
484 | outs().write_escaped(Strings.slice(0, Size), /*hex=*/true); | |||
485 | outs() << "'\n"; | |||
486 | Strings = Strings.drop_front(Size); | |||
487 | } while (--NumStrings); | |||
488 | ||||
489 | outs() << Indent << " }"; | |||
490 | return false; | |||
491 | } | |||
492 | ||||
493 | static bool decodeBlob(unsigned Code, unsigned BlockID, StringRef Indent, | |||
494 | ArrayRef<uint64_t> Record, StringRef Blob) { | |||
495 | if (BlockID != bitc::METADATA_BLOCK_ID) | |||
496 | return true; | |||
497 | if (Code != bitc::METADATA_STRINGS) | |||
498 | return true; | |||
499 | ||||
500 | return decodeMetadataStringsBlob(Indent, Record, Blob); | |||
501 | } | |||
502 | ||||
503 | /// ParseBlock - Read a block, updating statistics, etc. | |||
504 | static bool ParseBlock(BitstreamCursor &Stream, BitstreamBlockInfo &BlockInfo, | |||
505 | unsigned BlockID, unsigned IndentLevel, | |||
506 | CurStreamTypeType CurStreamType) { | |||
507 | std::string Indent(IndentLevel*2, ' '); | |||
508 | uint64_t BlockBitStart = Stream.GetCurrentBitNo(); | |||
509 | ||||
510 | // Get the statistics for this BlockID. | |||
511 | PerBlockIDStats &BlockStats = BlockIDStats[BlockID]; | |||
512 | ||||
513 | BlockStats.NumInstances++; | |||
514 | ||||
515 | // BLOCKINFO is a special part of the stream. | |||
516 | bool DumpRecords = Dump; | |||
517 | if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { | |||
518 | if (Dump) outs() << Indent << "<BLOCKINFO_BLOCK/>\n"; | |||
519 | Optional<BitstreamBlockInfo> NewBlockInfo = | |||
520 | Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true); | |||
521 | if (!NewBlockInfo) | |||
522 | return ReportError("Malformed BlockInfoBlock"); | |||
523 | BlockInfo = std::move(*NewBlockInfo); | |||
524 | Stream.JumpToBit(BlockBitStart); | |||
525 | // It's not really interesting to dump the contents of the blockinfo block. | |||
526 | DumpRecords = false; | |||
527 | } | |||
528 | ||||
529 | unsigned NumWords = 0; | |||
530 | if (Stream.EnterSubBlock(BlockID, &NumWords)) | |||
531 | return ReportError("Malformed block record"); | |||
532 | ||||
533 | // Keep it for later, when we see a MODULE_HASH record | |||
534 | uint64_t BlockEntryPos = Stream.getCurrentByteNo(); | |||
535 | ||||
536 | const char *BlockName = nullptr; | |||
537 | if (DumpRecords) { | |||
538 | outs() << Indent << "<"; | |||
539 | if ((BlockName = GetBlockName(BlockID, BlockInfo, CurStreamType))) | |||
540 | outs() << BlockName; | |||
541 | else | |||
542 | outs() << "UnknownBlock" << BlockID; | |||
543 | ||||
544 | if (NonSymbolic && BlockName) | |||
545 | outs() << " BlockID=" << BlockID; | |||
546 | ||||
547 | outs() << " NumWords=" << NumWords | |||
548 | << " BlockCodeSize=" << Stream.getAbbrevIDWidth() << ">\n"; | |||
549 | } | |||
550 | ||||
551 | SmallVector<uint64_t, 64> Record; | |||
552 | ||||
553 | // Keep the offset to the metadata index if seen. | |||
554 | uint64_t MetadataIndexOffset = 0; | |||
555 | ||||
556 | // Read all the records for this block. | |||
557 | while (1) { | |||
558 | if (Stream.AtEndOfStream()) | |||
559 | return ReportError("Premature end of bitstream"); | |||
560 | ||||
561 | uint64_t RecordStartBit = Stream.GetCurrentBitNo(); | |||
562 | ||||
563 | BitstreamEntry Entry = | |||
564 | Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs); | |||
565 | ||||
566 | switch (Entry.Kind) { | |||
567 | case BitstreamEntry::Error: | |||
568 | return ReportError("malformed bitcode file"); | |||
569 | case BitstreamEntry::EndBlock: { | |||
570 | uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); | |||
571 | BlockStats.NumBits += BlockBitEnd-BlockBitStart; | |||
572 | if (DumpRecords) { | |||
573 | outs() << Indent << "</"; | |||
574 | if (BlockName) | |||
575 | outs() << BlockName << ">\n"; | |||
576 | else | |||
577 | outs() << "UnknownBlock" << BlockID << ">\n"; | |||
578 | } | |||
579 | return false; | |||
580 | } | |||
581 | ||||
582 | case BitstreamEntry::SubBlock: { | |||
583 | uint64_t SubBlockBitStart = Stream.GetCurrentBitNo(); | |||
584 | if (ParseBlock(Stream, BlockInfo, Entry.ID, IndentLevel + 1, | |||
585 | CurStreamType)) | |||
586 | return true; | |||
587 | ++BlockStats.NumSubBlocks; | |||
588 | uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo(); | |||
589 | ||||
590 | // Don't include subblock sizes in the size of this block. | |||
591 | BlockBitStart += SubBlockBitEnd-SubBlockBitStart; | |||
592 | continue; | |||
593 | } | |||
594 | case BitstreamEntry::Record: | |||
595 | // The interesting case. | |||
596 | break; | |||
597 | } | |||
598 | ||||
599 | if (Entry.ID == bitc::DEFINE_ABBREV) { | |||
600 | Stream.ReadAbbrevRecord(); | |||
601 | ++BlockStats.NumAbbrevs; | |||
602 | continue; | |||
603 | } | |||
604 | ||||
605 | Record.clear(); | |||
606 | ||||
607 | ++BlockStats.NumRecords; | |||
608 | ||||
609 | StringRef Blob; | |||
610 | uint64_t CurrentRecordPos = Stream.GetCurrentBitNo(); | |||
611 | unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob); | |||
612 | ||||
613 | // Increment the # occurrences of this code. | |||
614 | if (BlockStats.CodeFreq.size() <= Code) | |||
615 | BlockStats.CodeFreq.resize(Code+1); | |||
616 | BlockStats.CodeFreq[Code].NumInstances++; | |||
617 | BlockStats.CodeFreq[Code].TotalBits += | |||
618 | Stream.GetCurrentBitNo()-RecordStartBit; | |||
619 | if (Entry.ID != bitc::UNABBREV_RECORD) { | |||
620 | BlockStats.CodeFreq[Code].NumAbbrev++; | |||
621 | ++BlockStats.NumAbbreviatedRecords; | |||
622 | } | |||
623 | ||||
624 | if (DumpRecords) { | |||
625 | outs() << Indent << " <"; | |||
626 | if (const char *CodeName = | |||
627 | GetCodeName(Code, BlockID, BlockInfo, CurStreamType)) | |||
628 | outs() << CodeName; | |||
629 | else | |||
630 | outs() << "UnknownCode" << Code; | |||
631 | if (NonSymbolic && GetCodeName(Code, BlockID, BlockInfo, CurStreamType)) | |||
632 | outs() << " codeid=" << Code; | |||
633 | const BitCodeAbbrev *Abbv = nullptr; | |||
634 | if (Entry.ID != bitc::UNABBREV_RECORD) { | |||
635 | Abbv = Stream.getAbbrev(Entry.ID); | |||
636 | outs() << " abbrevid=" << Entry.ID; | |||
637 | } | |||
638 | ||||
639 | for (unsigned i = 0, e = Record.size(); i != e; ++i) | |||
640 | outs() << " op" << i << "=" << (int64_t)Record[i]; | |||
641 | ||||
642 | // If we found a metadata index, let's verify that we had an offset before | |||
643 | // and validate its forward reference offset was correct! | |||
644 | if (BlockID == bitc::METADATA_BLOCK_ID) { | |||
645 | if (Code == bitc::METADATA_INDEX_OFFSET) { | |||
646 | if (Record.size() != 2) | |||
647 | outs() << "(Invalid record)"; | |||
648 | else { | |||
649 | auto Offset = Record[0] + (Record[1] << 32); | |||
650 | MetadataIndexOffset = Stream.GetCurrentBitNo() + Offset; | |||
651 | } | |||
652 | } | |||
653 | if (Code == bitc::METADATA_INDEX) { | |||
654 | outs() << " (offset "; | |||
655 | if (MetadataIndexOffset == RecordStartBit) | |||
656 | outs() << "match)"; | |||
657 | else | |||
658 | outs() << "mismatch: " << MetadataIndexOffset << " vs " | |||
659 | << RecordStartBit << ")"; | |||
660 | } | |||
661 | } | |||
662 | ||||
663 | // If we found a module hash, let's verify that it matches! | |||
664 | if (BlockID == bitc::MODULE_BLOCK_ID && Code == bitc::MODULE_CODE_HASH && | |||
665 | !CheckHash.empty()) { | |||
666 | if (Record.size() != 5) | |||
667 | outs() << " (invalid)"; | |||
668 | else { | |||
669 | // Recompute the hash and compare it to the one in the bitcode | |||
670 | SHA1 Hasher; | |||
671 | StringRef Hash; | |||
672 | Hasher.update(CheckHash); | |||
673 | { | |||
674 | int BlockSize = (CurrentRecordPos / 8) - BlockEntryPos; | |||
675 | auto Ptr = Stream.getPointerToByte(BlockEntryPos, BlockSize); | |||
676 | Hasher.update(ArrayRef<uint8_t>(Ptr, BlockSize)); | |||
677 | Hash = Hasher.result(); | |||
678 | } | |||
679 | SmallString<20> RecordedHash; | |||
680 | RecordedHash.resize(20); | |||
681 | int Pos = 0; | |||
682 | for (auto &Val : Record) { | |||
683 | assert(!(Val >> 32) && "Unexpected high bits set")((!(Val >> 32) && "Unexpected high bits set") ? static_cast<void> (0) : __assert_fail ("!(Val >> 32) && \"Unexpected high bits set\"" , "/build/llvm-toolchain-snapshot-8~svn345461/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp" , 683, __PRETTY_FUNCTION__)); | |||
684 | RecordedHash[Pos++] = (Val >> 24) & 0xFF; | |||
685 | RecordedHash[Pos++] = (Val >> 16) & 0xFF; | |||
686 | RecordedHash[Pos++] = (Val >> 8) & 0xFF; | |||
687 | RecordedHash[Pos++] = (Val >> 0) & 0xFF; | |||
688 | } | |||
689 | if (Hash == RecordedHash) | |||
690 | outs() << " (match)"; | |||
691 | else | |||
692 | outs() << " (!mismatch!)"; | |||
693 | } | |||
694 | } | |||
695 | ||||
696 | outs() << "/>"; | |||
697 | ||||
698 | if (Abbv) { | |||
699 | for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) { | |||
700 | const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i); | |||
701 | if (!Op.isEncoding() || Op.getEncoding() != BitCodeAbbrevOp::Array) | |||
702 | continue; | |||
703 | assert(i + 2 == e && "Array op not second to last")((i + 2 == e && "Array op not second to last") ? static_cast <void> (0) : __assert_fail ("i + 2 == e && \"Array op not second to last\"" , "/build/llvm-toolchain-snapshot-8~svn345461/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp" , 703, __PRETTY_FUNCTION__)); | |||
704 | std::string Str; | |||
705 | bool ArrayIsPrintable = true; | |||
706 | for (unsigned j = i - 1, je = Record.size(); j != je; ++j) { | |||
707 | if (!isPrint(static_cast<unsigned char>(Record[j]))) { | |||
708 | ArrayIsPrintable = false; | |||
709 | break; | |||
710 | } | |||
711 | Str += (char)Record[j]; | |||
712 | } | |||
713 | if (ArrayIsPrintable) | |||
714 | outs() << " record string = '" << Str << "'"; | |||
715 | break; | |||
716 | } | |||
717 | } | |||
718 | ||||
719 | if (Blob.data() && decodeBlob(Code, BlockID, Indent, Record, Blob)) { | |||
720 | outs() << " blob data = "; | |||
721 | if (ShowBinaryBlobs) { | |||
722 | outs() << "'"; | |||
723 | outs().write_escaped(Blob, /*hex=*/true) << "'"; | |||
724 | } else { | |||
725 | bool BlobIsPrintable = true; | |||
726 | for (unsigned i = 0, e = Blob.size(); i != e; ++i) | |||
727 | if (!isPrint(static_cast<unsigned char>(Blob[i]))) { | |||
728 | BlobIsPrintable = false; | |||
729 | break; | |||
730 | } | |||
731 | ||||
732 | if (BlobIsPrintable) | |||
733 | outs() << "'" << Blob << "'"; | |||
734 | else | |||
735 | outs() << "unprintable, " << Blob.size() << " bytes."; | |||
736 | } | |||
737 | } | |||
738 | ||||
739 | outs() << "\n"; | |||
740 | } | |||
741 | ||||
742 | // Make sure that we can skip the current record. | |||
743 | Stream.JumpToBit(CurrentRecordPos); | |||
744 | Stream.skipRecord(Entry.ID); | |||
745 | } | |||
746 | } | |||
747 | ||||
748 | static void PrintSize(double Bits) { | |||
749 | outs() << format("%.2f/%.2fB/%luW", Bits, Bits/8,(unsigned long)(Bits/32)); | |||
750 | } | |||
751 | static void PrintSize(uint64_t Bits) { | |||
752 | outs() << format("%lub/%.2fB/%luW", (unsigned long)Bits, | |||
753 | (double)Bits/8, (unsigned long)(Bits/32)); | |||
754 | } | |||
755 | ||||
756 | static CurStreamTypeType ReadSignature(BitstreamCursor &Stream) { | |||
757 | char Signature[6]; | |||
758 | Signature[0] = Stream.Read(8); | |||
759 | Signature[1] = Stream.Read(8); | |||
760 | ||||
761 | // Autodetect the file contents, if it is one we know. | |||
762 | if (Signature[0] == 'C' && Signature[1] == 'P') { | |||
763 | Signature[2] = Stream.Read(8); | |||
764 | Signature[3] = Stream.Read(8); | |||
765 | if (Signature[2] == 'C' && Signature[3] == 'H') | |||
766 | return ClangSerializedASTBitstream; | |||
767 | } else if (Signature[0] == 'D' && Signature[1] == 'I') { | |||
768 | Signature[2] = Stream.Read(8); | |||
769 | Signature[3] = Stream.Read(8); | |||
770 | if (Signature[2] == 'A' && Signature[3] == 'G') | |||
771 | return ClangSerializedDiagnosticsBitstream; | |||
772 | } else { | |||
773 | Signature[2] = Stream.Read(4); | |||
774 | Signature[3] = Stream.Read(4); | |||
775 | Signature[4] = Stream.Read(4); | |||
776 | Signature[5] = Stream.Read(4); | |||
777 | if (Signature[0] == 'B' && Signature[1] == 'C' && | |||
778 | Signature[2] == 0x0 && Signature[3] == 0xC && | |||
779 | Signature[4] == 0xE && Signature[5] == 0xD) | |||
780 | return LLVMIRBitstream; | |||
781 | } | |||
782 | return UnknownBitstream; | |||
783 | } | |||
784 | ||||
785 | static bool openBitcodeFile(StringRef Path, | |||
786 | std::unique_ptr<MemoryBuffer> &MemBuf, | |||
787 | BitstreamCursor &Stream, | |||
788 | CurStreamTypeType &CurStreamType) { | |||
789 | // Read the input file. | |||
790 | ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr = | |||
791 | MemoryBuffer::getFileOrSTDIN(Path); | |||
792 | if (std::error_code EC = MemBufOrErr.getError()) | |||
793 | return ReportError(Twine("ReportError reading '") + Path + "': " + EC.message()); | |||
794 | MemBuf = std::move(MemBufOrErr.get()); | |||
795 | ||||
796 | if (MemBuf->getBufferSize() & 3) | |||
797 | return ReportError("Bitcode stream should be a multiple of 4 bytes in length"); | |||
798 | ||||
799 | const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart(); | |||
800 | const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize(); | |||
801 | ||||
802 | // If we have a wrapper header, parse it and ignore the non-bc file contents. | |||
803 | // The magic number is 0x0B17C0DE stored in little endian. | |||
804 | if (isBitcodeWrapper(BufPtr, EndBufPtr)) { | |||
805 | if (MemBuf->getBufferSize() < BWH_HeaderSize) | |||
806 | return ReportError("Invalid bitcode wrapper header"); | |||
807 | ||||
808 | if (Dump) { | |||
809 | unsigned Magic = support::endian::read32le(&BufPtr[BWH_MagicField]); | |||
810 | unsigned Version = support::endian::read32le(&BufPtr[BWH_VersionField]); | |||
811 | unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]); | |||
812 | unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]); | |||
813 | unsigned CPUType = support::endian::read32le(&BufPtr[BWH_CPUTypeField]); | |||
814 | ||||
815 | outs() << "<BITCODE_WRAPPER_HEADER" | |||
816 | << " Magic=" << format_hex(Magic, 10) | |||
817 | << " Version=" << format_hex(Version, 10) | |||
818 | << " Offset=" << format_hex(Offset, 10) | |||
819 | << " Size=" << format_hex(Size, 10) | |||
820 | << " CPUType=" << format_hex(CPUType, 10) << "/>\n"; | |||
821 | } | |||
822 | ||||
823 | if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr, true)) | |||
824 | return ReportError("Invalid bitcode wrapper header"); | |||
825 | } | |||
826 | ||||
827 | Stream = BitstreamCursor(ArrayRef<uint8_t>(BufPtr, EndBufPtr)); | |||
828 | CurStreamType = ReadSignature(Stream); | |||
829 | ||||
830 | return false; | |||
831 | } | |||
832 | ||||
833 | /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename. | |||
834 | static int AnalyzeBitcode() { | |||
835 | std::unique_ptr<MemoryBuffer> StreamBuffer; | |||
836 | BitstreamCursor Stream; | |||
837 | BitstreamBlockInfo BlockInfo; | |||
838 | CurStreamTypeType CurStreamType; | |||
839 | if (openBitcodeFile(InputFilename, StreamBuffer, Stream, CurStreamType)) | |||
840 | return true; | |||
841 | Stream.setBlockInfo(&BlockInfo); | |||
842 | ||||
843 | // Read block info from BlockInfoFilename, if specified. | |||
844 | // The block info must be a top-level block. | |||
845 | if (!BlockInfoFilename.empty()) { | |||
846 | std::unique_ptr<MemoryBuffer> BlockInfoBuffer; | |||
847 | BitstreamCursor BlockInfoCursor; | |||
848 | CurStreamTypeType BlockInfoStreamType; | |||
849 | if (openBitcodeFile(BlockInfoFilename, BlockInfoBuffer, BlockInfoCursor, | |||
850 | BlockInfoStreamType)) | |||
851 | return true; | |||
852 | ||||
853 | while (!BlockInfoCursor.AtEndOfStream()) { | |||
854 | unsigned Code = BlockInfoCursor.ReadCode(); | |||
855 | if (Code != bitc::ENTER_SUBBLOCK) | |||
856 | return ReportError("Invalid record at top-level in block info file"); | |||
857 | ||||
858 | unsigned BlockID = BlockInfoCursor.ReadSubBlockID(); | |||
859 | if (BlockID == bitc::BLOCKINFO_BLOCK_ID) { | |||
860 | Optional<BitstreamBlockInfo> NewBlockInfo = | |||
861 | BlockInfoCursor.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true); | |||
862 | if (!NewBlockInfo) | |||
863 | return ReportError("Malformed BlockInfoBlock in block info file"); | |||
864 | BlockInfo = std::move(*NewBlockInfo); | |||
865 | break; | |||
866 | } | |||
867 | ||||
868 | BlockInfoCursor.SkipBlock(); | |||
869 | } | |||
870 | } | |||
871 | ||||
872 | unsigned NumTopBlocks = 0; | |||
873 | ||||
874 | // Parse the top-level structure. We only allow blocks at the top-level. | |||
875 | while (!Stream.AtEndOfStream()) { | |||
876 | unsigned Code = Stream.ReadCode(); | |||
877 | if (Code != bitc::ENTER_SUBBLOCK) | |||
878 | return ReportError("Invalid record at top-level"); | |||
879 | ||||
880 | unsigned BlockID = Stream.ReadSubBlockID(); | |||
881 | ||||
882 | if (ParseBlock(Stream, BlockInfo, BlockID, 0, CurStreamType)) | |||
883 | return true; | |||
884 | ++NumTopBlocks; | |||
885 | } | |||
886 | ||||
887 | if (Dump) outs() << "\n\n"; | |||
888 | ||||
889 | uint64_t BufferSizeBits = Stream.getBitcodeBytes().size() * CHAR_BIT8; | |||
890 | // Print a summary of the read file. | |||
891 | outs() << "Summary of " << InputFilename << ":\n"; | |||
892 | outs() << " Total size: "; | |||
893 | PrintSize(BufferSizeBits); | |||
894 | outs() << "\n"; | |||
895 | outs() << " Stream type: "; | |||
896 | switch (CurStreamType) { | |||
897 | case UnknownBitstream: | |||
898 | outs() << "unknown\n"; | |||
899 | break; | |||
900 | case LLVMIRBitstream: | |||
901 | outs() << "LLVM IR\n"; | |||
902 | break; | |||
903 | case ClangSerializedASTBitstream: | |||
904 | outs() << "Clang Serialized AST\n"; | |||
905 | break; | |||
906 | case ClangSerializedDiagnosticsBitstream: | |||
907 | outs() << "Clang Serialized Diagnostics\n"; | |||
908 | break; | |||
909 | } | |||
910 | outs() << " # Toplevel Blocks: " << NumTopBlocks << "\n"; | |||
911 | outs() << "\n"; | |||
912 | ||||
913 | // Emit per-block stats. | |||
914 | outs() << "Per-block Summary:\n"; | |||
915 | for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(), | |||
916 | E = BlockIDStats.end(); I != E; ++I) { | |||
917 | outs() << " Block ID #" << I->first; | |||
918 | if (const char *BlockName = | |||
919 | GetBlockName(I->first, BlockInfo, CurStreamType)) | |||
920 | outs() << " (" << BlockName << ")"; | |||
921 | outs() << ":\n"; | |||
922 | ||||
923 | const PerBlockIDStats &Stats = I->second; | |||
924 | outs() << " Num Instances: " << Stats.NumInstances << "\n"; | |||
925 | outs() << " Total Size: "; | |||
926 | PrintSize(Stats.NumBits); | |||
927 | outs() << "\n"; | |||
928 | double pct = (Stats.NumBits * 100.0) / BufferSizeBits; | |||
929 | outs() << " Percent of file: " << format("%2.4f%%", pct) << "\n"; | |||
930 | if (Stats.NumInstances > 1) { | |||
931 | outs() << " Average Size: "; | |||
932 | PrintSize(Stats.NumBits/(double)Stats.NumInstances); | |||
933 | outs() << "\n"; | |||
934 | outs() << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/" | |||
935 | << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n"; | |||
936 | outs() << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/" | |||
937 | << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n"; | |||
938 | outs() << " Tot/Avg Records: " << Stats.NumRecords << "/" | |||
939 | << Stats.NumRecords/(double)Stats.NumInstances << "\n"; | |||
940 | } else { | |||
941 | outs() << " Num SubBlocks: " << Stats.NumSubBlocks << "\n"; | |||
942 | outs() << " Num Abbrevs: " << Stats.NumAbbrevs << "\n"; | |||
943 | outs() << " Num Records: " << Stats.NumRecords << "\n"; | |||
944 | } | |||
945 | if (Stats.NumRecords) { | |||
946 | double pct = (Stats.NumAbbreviatedRecords * 100.0) / Stats.NumRecords; | |||
947 | outs() << " Percent Abbrevs: " << format("%2.4f%%", pct) << "\n"; | |||
948 | } | |||
949 | outs() << "\n"; | |||
950 | ||||
951 | // Print a histogram of the codes we see. | |||
952 | if (!NoHistogram && !Stats.CodeFreq.empty()) { | |||
953 | std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code> | |||
954 | for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i) | |||
955 | if (unsigned Freq = Stats.CodeFreq[i].NumInstances) | |||
956 | FreqPairs.push_back(std::make_pair(Freq, i)); | |||
957 | std::stable_sort(FreqPairs.begin(), FreqPairs.end()); | |||
958 | std::reverse(FreqPairs.begin(), FreqPairs.end()); | |||
959 | ||||
960 | outs() << "\tRecord Histogram:\n"; | |||
961 | outs() << "\t\t Count # Bits b/Rec % Abv Record Kind\n"; | |||
962 | for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) { | |||
963 | const PerRecordStats &RecStats = Stats.CodeFreq[FreqPairs[i].second]; | |||
964 | ||||
965 | outs() << format("\t\t%7d %9lu", | |||
966 | RecStats.NumInstances, | |||
967 | (unsigned long)RecStats.TotalBits); | |||
968 | ||||
969 | if (RecStats.NumInstances > 1) | |||
970 | outs() << format(" %9.1f", | |||
971 | (double)RecStats.TotalBits/RecStats.NumInstances); | |||
972 | else | |||
973 | outs() << " "; | |||
974 | ||||
975 | if (RecStats.NumAbbrev) | |||
976 | outs() << | |||
977 | format(" %7.2f", | |||
978 | (double)RecStats.NumAbbrev/RecStats.NumInstances*100); | |||
979 | else | |||
980 | outs() << " "; | |||
981 | ||||
982 | outs() << " "; | |||
983 | if (const char *CodeName = GetCodeName(FreqPairs[i].second, I->first, | |||
984 | BlockInfo, CurStreamType)) | |||
985 | outs() << CodeName << "\n"; | |||
986 | else | |||
987 | outs() << "UnknownCode" << FreqPairs[i].second << "\n"; | |||
988 | } | |||
989 | outs() << "\n"; | |||
990 | ||||
991 | } | |||
992 | } | |||
993 | return 0; | |||
994 | } | |||
995 | ||||
996 | ||||
997 | int main(int argc, char **argv) { | |||
998 | InitLLVM X(argc, argv); | |||
999 | cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n"); | |||
1000 | return AnalyzeBitcode(); | |||
| ||||
1001 | } |
1 | //===- BitstreamReader.h - Low-level bitstream reader interface -*- C++ -*-===// | |||
2 | // | |||
3 | // The LLVM Compiler Infrastructure | |||
4 | // | |||
5 | // This file is distributed under the University of Illinois Open Source | |||
6 | // License. See LICENSE.TXT for details. | |||
7 | // | |||
8 | //===----------------------------------------------------------------------===// | |||
9 | // | |||
10 | // This header defines the BitstreamReader class. This class can be used to | |||
11 | // read an arbitrary bitstream, regardless of its contents. | |||
12 | // | |||
13 | //===----------------------------------------------------------------------===// | |||
14 | ||||
15 | #ifndef LLVM_BITCODE_BITSTREAMREADER_H | |||
16 | #define LLVM_BITCODE_BITSTREAMREADER_H | |||
17 | ||||
18 | #include "llvm/ADT/ArrayRef.h" | |||
19 | #include "llvm/ADT/SmallVector.h" | |||
20 | #include "llvm/Bitcode/BitCodes.h" | |||
21 | #include "llvm/Support/Endian.h" | |||
22 | #include "llvm/Support/ErrorHandling.h" | |||
23 | #include "llvm/Support/MathExtras.h" | |||
24 | #include "llvm/Support/MemoryBuffer.h" | |||
25 | #include <algorithm> | |||
26 | #include <cassert> | |||
27 | #include <climits> | |||
28 | #include <cstddef> | |||
29 | #include <cstdint> | |||
30 | #include <memory> | |||
31 | #include <string> | |||
32 | #include <utility> | |||
33 | #include <vector> | |||
34 | ||||
35 | namespace llvm { | |||
36 | ||||
37 | /// This class maintains the abbreviations read from a block info block. | |||
38 | class BitstreamBlockInfo { | |||
39 | public: | |||
40 | /// This contains information emitted to BLOCKINFO_BLOCK blocks. These | |||
41 | /// describe abbreviations that all blocks of the specified ID inherit. | |||
42 | struct BlockInfo { | |||
43 | unsigned BlockID; | |||
44 | std::vector<std::shared_ptr<BitCodeAbbrev>> Abbrevs; | |||
45 | std::string Name; | |||
46 | std::vector<std::pair<unsigned, std::string>> RecordNames; | |||
47 | }; | |||
48 | ||||
49 | private: | |||
50 | std::vector<BlockInfo> BlockInfoRecords; | |||
51 | ||||
52 | public: | |||
53 | /// If there is block info for the specified ID, return it, otherwise return | |||
54 | /// null. | |||
55 | const BlockInfo *getBlockInfo(unsigned BlockID) const { | |||
56 | // Common case, the most recent entry matches BlockID. | |||
57 | if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID) | |||
58 | return &BlockInfoRecords.back(); | |||
59 | ||||
60 | for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size()); | |||
61 | i != e; ++i) | |||
62 | if (BlockInfoRecords[i].BlockID == BlockID) | |||
63 | return &BlockInfoRecords[i]; | |||
64 | return nullptr; | |||
65 | } | |||
66 | ||||
67 | BlockInfo &getOrCreateBlockInfo(unsigned BlockID) { | |||
68 | if (const BlockInfo *BI = getBlockInfo(BlockID)) | |||
69 | return *const_cast<BlockInfo*>(BI); | |||
70 | ||||
71 | // Otherwise, add a new record. | |||
72 | BlockInfoRecords.emplace_back(); | |||
73 | BlockInfoRecords.back().BlockID = BlockID; | |||
74 | return BlockInfoRecords.back(); | |||
75 | } | |||
76 | }; | |||
77 | ||||
78 | /// This represents a position within a bitstream. There may be multiple | |||
79 | /// independent cursors reading within one bitstream, each maintaining their | |||
80 | /// own local state. | |||
81 | class SimpleBitstreamCursor { | |||
82 | ArrayRef<uint8_t> BitcodeBytes; | |||
83 | size_t NextChar = 0; | |||
84 | ||||
85 | public: | |||
86 | /// This is the current data we have pulled from the stream but have not | |||
87 | /// returned to the client. This is specifically and intentionally defined to | |||
88 | /// follow the word size of the host machine for efficiency. We use word_t in | |||
89 | /// places that are aware of this to make it perfectly explicit what is going | |||
90 | /// on. | |||
91 | using word_t = size_t; | |||
92 | ||||
93 | private: | |||
94 | word_t CurWord = 0; | |||
95 | ||||
96 | /// This is the number of bits in CurWord that are valid. This is always from | |||
97 | /// [0...bits_of(size_t)-1] inclusive. | |||
98 | unsigned BitsInCurWord = 0; | |||
99 | ||||
100 | public: | |||
101 | static const size_t MaxChunkSize = sizeof(word_t) * 8; | |||
102 | ||||
103 | SimpleBitstreamCursor() = default; | |||
104 | explicit SimpleBitstreamCursor(ArrayRef<uint8_t> BitcodeBytes) | |||
105 | : BitcodeBytes(BitcodeBytes) {} | |||
106 | explicit SimpleBitstreamCursor(StringRef BitcodeBytes) | |||
107 | : BitcodeBytes(reinterpret_cast<const uint8_t *>(BitcodeBytes.data()), | |||
108 | BitcodeBytes.size()) {} | |||
109 | explicit SimpleBitstreamCursor(MemoryBufferRef BitcodeBytes) | |||
110 | : SimpleBitstreamCursor(BitcodeBytes.getBuffer()) {} | |||
111 | ||||
112 | bool canSkipToPos(size_t pos) const { | |||
113 | // pos can be skipped to if it is a valid address or one byte past the end. | |||
114 | return pos <= BitcodeBytes.size(); | |||
115 | } | |||
116 | ||||
117 | bool AtEndOfStream() { | |||
118 | return BitsInCurWord == 0 && BitcodeBytes.size() <= NextChar; | |||
119 | } | |||
120 | ||||
121 | /// Return the bit # of the bit we are reading. | |||
122 | uint64_t GetCurrentBitNo() const { | |||
123 | return NextChar*CHAR_BIT8 - BitsInCurWord; | |||
124 | } | |||
125 | ||||
126 | // Return the byte # of the current bit. | |||
127 | uint64_t getCurrentByteNo() const { return GetCurrentBitNo() / 8; } | |||
128 | ||||
129 | ArrayRef<uint8_t> getBitcodeBytes() const { return BitcodeBytes; } | |||
130 | ||||
131 | /// Reset the stream to the specified bit number. | |||
132 | void JumpToBit(uint64_t BitNo) { | |||
133 | size_t ByteNo = size_t(BitNo/8) & ~(sizeof(word_t)-1); | |||
134 | unsigned WordBitNo = unsigned(BitNo & (sizeof(word_t)*8-1)); | |||
135 | assert(canSkipToPos(ByteNo) && "Invalid location")((canSkipToPos(ByteNo) && "Invalid location") ? static_cast <void> (0) : __assert_fail ("canSkipToPos(ByteNo) && \"Invalid location\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Bitcode/BitstreamReader.h" , 135, __PRETTY_FUNCTION__)); | |||
136 | ||||
137 | // Move the cursor to the right word. | |||
138 | NextChar = ByteNo; | |||
139 | BitsInCurWord = 0; | |||
140 | ||||
141 | // Skip over any bits that are already consumed. | |||
142 | if (WordBitNo) | |||
143 | Read(WordBitNo); | |||
144 | } | |||
145 | ||||
146 | /// Get a pointer into the bitstream at the specified byte offset. | |||
147 | const uint8_t *getPointerToByte(uint64_t ByteNo, uint64_t NumBytes) { | |||
148 | return BitcodeBytes.data() + ByteNo; | |||
149 | } | |||
150 | ||||
151 | /// Get a pointer into the bitstream at the specified bit offset. | |||
152 | /// | |||
153 | /// The bit offset must be on a byte boundary. | |||
154 | const uint8_t *getPointerToBit(uint64_t BitNo, uint64_t NumBytes) { | |||
155 | assert(!(BitNo % 8) && "Expected bit on byte boundary")((!(BitNo % 8) && "Expected bit on byte boundary") ? static_cast <void> (0) : __assert_fail ("!(BitNo % 8) && \"Expected bit on byte boundary\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Bitcode/BitstreamReader.h" , 155, __PRETTY_FUNCTION__)); | |||
156 | return getPointerToByte(BitNo / 8, NumBytes); | |||
157 | } | |||
158 | ||||
159 | void fillCurWord() { | |||
160 | if (NextChar >= BitcodeBytes.size()) | |||
161 | report_fatal_error("Unexpected end of file"); | |||
162 | ||||
163 | // Read the next word from the stream. | |||
164 | const uint8_t *NextCharPtr = BitcodeBytes.data() + NextChar; | |||
165 | unsigned BytesRead; | |||
166 | if (BitcodeBytes.size() >= NextChar + sizeof(word_t)) { | |||
167 | BytesRead = sizeof(word_t); | |||
168 | CurWord = | |||
169 | support::endian::read<word_t, support::little, support::unaligned>( | |||
170 | NextCharPtr); | |||
171 | } else { | |||
172 | // Short read. | |||
173 | BytesRead = BitcodeBytes.size() - NextChar; | |||
174 | CurWord = 0; | |||
175 | for (unsigned B = 0; B != BytesRead; ++B) | |||
176 | CurWord |= uint64_t(NextCharPtr[B]) << (B * 8); | |||
177 | } | |||
178 | NextChar += BytesRead; | |||
179 | BitsInCurWord = BytesRead * 8; | |||
180 | } | |||
181 | ||||
182 | word_t Read(unsigned NumBits) { | |||
183 | static const unsigned BitsInWord = MaxChunkSize; | |||
184 | ||||
185 | assert(NumBits && NumBits <= BitsInWord &&((NumBits && NumBits <= BitsInWord && "Cannot return zero or more than BitsInWord bits!" ) ? static_cast<void> (0) : __assert_fail ("NumBits && NumBits <= BitsInWord && \"Cannot return zero or more than BitsInWord bits!\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Bitcode/BitstreamReader.h" , 186, __PRETTY_FUNCTION__)) | |||
186 | "Cannot return zero or more than BitsInWord bits!")((NumBits && NumBits <= BitsInWord && "Cannot return zero or more than BitsInWord bits!" ) ? static_cast<void> (0) : __assert_fail ("NumBits && NumBits <= BitsInWord && \"Cannot return zero or more than BitsInWord bits!\"" , "/build/llvm-toolchain-snapshot-8~svn345461/include/llvm/Bitcode/BitstreamReader.h" , 186, __PRETTY_FUNCTION__)); | |||
187 | ||||
188 | static const unsigned Mask = sizeof(word_t) > 4 ? 0x3f : 0x1f; | |||
189 | ||||
190 | // If the field is fully contained by CurWord, return it quickly. | |||
191 | if (BitsInCurWord >= NumBits) { | |||
192 | word_t R = CurWord & (~word_t(0) >> (BitsInWord - NumBits)); | |||
193 | ||||
194 | // Use a mask to avoid undefined behavior. | |||
195 | CurWord >>= (NumBits & Mask); | |||
196 | ||||
197 | BitsInCurWord -= NumBits; | |||
198 | return R; | |||
199 | } | |||
200 | ||||
201 | word_t R = BitsInCurWord ? CurWord : 0; | |||
202 | unsigned BitsLeft = NumBits - BitsInCurWord; | |||
203 | ||||
204 | fillCurWord(); | |||
205 | ||||
206 | // If we run out of data, abort. | |||
207 | if (BitsLeft > BitsInCurWord) | |||
208 | report_fatal_error("Unexpected end of file"); | |||
209 | ||||
210 | word_t R2 = CurWord & (~word_t(0) >> (BitsInWord - BitsLeft)); | |||
| ||||
211 | ||||
212 | // Use a mask to avoid undefined behavior. | |||
213 | CurWord >>= (BitsLeft & Mask); | |||
214 | ||||
215 | BitsInCurWord -= BitsLeft; | |||
216 | ||||
217 | R |= R2 << (NumBits - BitsLeft); | |||
218 | ||||
219 | return R; | |||
220 | } | |||
221 | ||||
222 | uint32_t ReadVBR(unsigned NumBits) { | |||
223 | uint32_t Piece = Read(NumBits); | |||
224 | if ((Piece & (1U << (NumBits-1))) == 0) | |||
225 | return Piece; | |||
226 | ||||
227 | uint32_t Result = 0; | |||
228 | unsigned NextBit = 0; | |||
229 | while (true) { | |||
230 | Result |= (Piece & ((1U << (NumBits-1))-1)) << NextBit; | |||
231 | ||||
232 | if ((Piece & (1U << (NumBits-1))) == 0) | |||
233 | return Result; | |||
234 | ||||
235 | NextBit += NumBits-1; | |||
236 | Piece = Read(NumBits); | |||
237 | } | |||
238 | } | |||
239 | ||||
240 | // Read a VBR that may have a value up to 64-bits in size. The chunk size of | |||
241 | // the VBR must still be <= 32 bits though. | |||
242 | uint64_t ReadVBR64(unsigned NumBits) { | |||
243 | uint32_t Piece = Read(NumBits); | |||
244 | if ((Piece & (1U << (NumBits-1))) == 0) | |||
245 | return uint64_t(Piece); | |||
246 | ||||
247 | uint64_t Result = 0; | |||
248 | unsigned NextBit = 0; | |||
249 | while (true) { | |||
250 | Result |= uint64_t(Piece & ((1U << (NumBits-1))-1)) << NextBit; | |||
251 | ||||
252 | if ((Piece & (1U << (NumBits-1))) == 0) | |||
253 | return Result; | |||
254 | ||||
255 | NextBit += NumBits-1; | |||
256 | Piece = Read(NumBits); | |||
257 | } | |||
258 | } | |||
259 | ||||
260 | void SkipToFourByteBoundary() { | |||
261 | // If word_t is 64-bits and if we've read less than 32 bits, just dump | |||
262 | // the bits we have up to the next 32-bit boundary. | |||
263 | if (sizeof(word_t) > 4 && | |||
264 | BitsInCurWord >= 32) { | |||
265 | CurWord >>= BitsInCurWord-32; | |||
266 | BitsInCurWord = 32; | |||
267 | return; | |||
268 | } | |||
269 | ||||
270 | BitsInCurWord = 0; | |||
271 | } | |||
272 | ||||
273 | /// Skip to the end of the file. | |||
274 | void skipToEnd() { NextChar = BitcodeBytes.size(); } | |||
275 | }; | |||
276 | ||||
277 | /// When advancing through a bitstream cursor, each advance can discover a few | |||
278 | /// different kinds of entries: | |||
279 | struct BitstreamEntry { | |||
280 | enum { | |||
281 | Error, // Malformed bitcode was found. | |||
282 | EndBlock, // We've reached the end of the current block, (or the end of the | |||
283 | // file, which is treated like a series of EndBlock records. | |||
284 | SubBlock, // This is the start of a new subblock of a specific ID. | |||
285 | Record // This is a record with a specific AbbrevID. | |||
286 | } Kind; | |||
287 | ||||
288 | unsigned ID; | |||
289 | ||||
290 | static BitstreamEntry getError() { | |||
291 | BitstreamEntry E; E.Kind = Error; return E; | |||
292 | } | |||
293 | ||||
294 | static BitstreamEntry getEndBlock() { | |||
295 | BitstreamEntry E; E.Kind = EndBlock; return E; | |||
296 | } | |||
297 | ||||
298 | static BitstreamEntry getSubBlock(unsigned ID) { | |||
299 | BitstreamEntry E; E.Kind = SubBlock; E.ID = ID; return E; | |||
300 | } | |||
301 | ||||
302 | static BitstreamEntry getRecord(unsigned AbbrevID) { | |||
303 | BitstreamEntry E; E.Kind = Record; E.ID = AbbrevID; return E; | |||
304 | } | |||
305 | }; | |||
306 | ||||
307 | /// This represents a position within a bitcode file, implemented on top of a | |||
308 | /// SimpleBitstreamCursor. | |||
309 | /// | |||
310 | /// Unlike iterators, BitstreamCursors are heavy-weight objects that should not | |||
311 | /// be passed by value. | |||
312 | class BitstreamCursor : SimpleBitstreamCursor { | |||
313 | // This is the declared size of code values used for the current block, in | |||
314 | // bits. | |||
315 | unsigned CurCodeSize = 2; | |||
316 | ||||
317 | /// Abbrevs installed at in this block. | |||
318 | std::vector<std::shared_ptr<BitCodeAbbrev>> CurAbbrevs; | |||
319 | ||||
320 | struct Block { | |||
321 | unsigned PrevCodeSize; | |||
322 | std::vector<std::shared_ptr<BitCodeAbbrev>> PrevAbbrevs; | |||
323 | ||||
324 | explicit Block(unsigned PCS) : PrevCodeSize(PCS) {} | |||
325 | }; | |||
326 | ||||
327 | /// This tracks the codesize of parent blocks. | |||
328 | SmallVector<Block, 8> BlockScope; | |||
329 | ||||
330 | BitstreamBlockInfo *BlockInfo = nullptr; | |||
331 | ||||
332 | public: | |||
333 | static const size_t MaxChunkSize = sizeof(word_t) * 8; | |||
334 | ||||
335 | BitstreamCursor() = default; | |||
336 | explicit BitstreamCursor(ArrayRef<uint8_t> BitcodeBytes) | |||
337 | : SimpleBitstreamCursor(BitcodeBytes) {} | |||
338 | explicit BitstreamCursor(StringRef BitcodeBytes) | |||
339 | : SimpleBitstreamCursor(BitcodeBytes) {} | |||
340 | explicit BitstreamCursor(MemoryBufferRef BitcodeBytes) | |||
341 | : SimpleBitstreamCursor(BitcodeBytes) {} | |||
342 | ||||
343 | using SimpleBitstreamCursor::canSkipToPos; | |||
344 | using SimpleBitstreamCursor::AtEndOfStream; | |||
345 | using SimpleBitstreamCursor::getBitcodeBytes; | |||
346 | using SimpleBitstreamCursor::GetCurrentBitNo; | |||
347 | using SimpleBitstreamCursor::getCurrentByteNo; | |||
348 | using SimpleBitstreamCursor::getPointerToByte; | |||
349 | using SimpleBitstreamCursor::JumpToBit; | |||
350 | using SimpleBitstreamCursor::fillCurWord; | |||
351 | using SimpleBitstreamCursor::Read; | |||
352 | using SimpleBitstreamCursor::ReadVBR; | |||
353 | using SimpleBitstreamCursor::ReadVBR64; | |||
354 | ||||
355 | /// Return the number of bits used to encode an abbrev #. | |||
356 | unsigned getAbbrevIDWidth() const { return CurCodeSize; } | |||
357 | ||||
358 | /// Flags that modify the behavior of advance(). | |||
359 | enum { | |||
360 | /// If this flag is used, the advance() method does not automatically pop | |||
361 | /// the block scope when the end of a block is reached. | |||
362 | AF_DontPopBlockAtEnd = 1, | |||
363 | ||||
364 | /// If this flag is used, abbrev entries are returned just like normal | |||
365 | /// records. | |||
366 | AF_DontAutoprocessAbbrevs = 2 | |||
367 | }; | |||
368 | ||||
369 | /// Advance the current bitstream, returning the next entry in the stream. | |||
370 | BitstreamEntry advance(unsigned Flags = 0) { | |||
371 | while (true) { | |||
372 | if (AtEndOfStream()) | |||
373 | return BitstreamEntry::getError(); | |||
374 | ||||
375 | unsigned Code = ReadCode(); | |||
376 | if (Code == bitc::END_BLOCK) { | |||
377 | // Pop the end of the block unless Flags tells us not to. | |||
378 | if (!(Flags & AF_DontPopBlockAtEnd) && ReadBlockEnd()) | |||
379 | return BitstreamEntry::getError(); | |||
380 | return BitstreamEntry::getEndBlock(); | |||
381 | } | |||
382 | ||||
383 | if (Code == bitc::ENTER_SUBBLOCK) | |||
384 | return BitstreamEntry::getSubBlock(ReadSubBlockID()); | |||
385 | ||||
386 | if (Code == bitc::DEFINE_ABBREV && | |||
387 | !(Flags & AF_DontAutoprocessAbbrevs)) { | |||
388 | // We read and accumulate abbrev's, the client can't do anything with | |||
389 | // them anyway. | |||
390 | ReadAbbrevRecord(); | |||
391 | continue; | |||
392 | } | |||
393 | ||||
394 | return BitstreamEntry::getRecord(Code); | |||
395 | } | |||
396 | } | |||
397 | ||||
398 | /// This is a convenience function for clients that don't expect any | |||
399 | /// subblocks. This just skips over them automatically. | |||
400 | BitstreamEntry advanceSkippingSubblocks(unsigned Flags = 0) { | |||
401 | while (true) { | |||
402 | // If we found a normal entry, return it. | |||
403 | BitstreamEntry Entry = advance(Flags); | |||
404 | if (Entry.Kind != BitstreamEntry::SubBlock) | |||
405 | return Entry; | |||
406 | ||||
407 | // If we found a sub-block, just skip over it and check the next entry. | |||
408 | if (SkipBlock()) | |||
409 | return BitstreamEntry::getError(); | |||
410 | } | |||
411 | } | |||
412 | ||||
413 | unsigned ReadCode() { | |||
414 | return Read(CurCodeSize); | |||
415 | } | |||
416 | ||||
417 | // Block header: | |||
418 | // [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen] | |||
419 | ||||
420 | /// Having read the ENTER_SUBBLOCK code, read the BlockID for the block. | |||
421 | unsigned ReadSubBlockID() { | |||
422 | return ReadVBR(bitc::BlockIDWidth); | |||
423 | } | |||
424 | ||||
425 | /// Having read the ENTER_SUBBLOCK abbrevid and a BlockID, skip over the body | |||
426 | /// of this block. If the block record is malformed, return true. | |||
427 | bool SkipBlock() { | |||
428 | // Read and ignore the codelen value. Since we are skipping this block, we | |||
429 | // don't care what code widths are used inside of it. | |||
430 | ReadVBR(bitc::CodeLenWidth); | |||
431 | SkipToFourByteBoundary(); | |||
432 | size_t NumFourBytes = Read(bitc::BlockSizeWidth); | |||
433 | ||||
434 | // Check that the block wasn't partially defined, and that the offset isn't | |||
435 | // bogus. | |||
436 | size_t SkipTo = GetCurrentBitNo() + NumFourBytes*4*8; | |||
437 | if (AtEndOfStream() || !canSkipToPos(SkipTo/8)) | |||
438 | return true; | |||
439 | ||||
440 | JumpToBit(SkipTo); | |||
441 | return false; | |||
442 | } | |||
443 | ||||
444 | /// Having read the ENTER_SUBBLOCK abbrevid, enter the block, and return true | |||
445 | /// if the block has an error. | |||
446 | bool EnterSubBlock(unsigned BlockID, unsigned *NumWordsP = nullptr); | |||
447 | ||||
448 | bool ReadBlockEnd() { | |||
449 | if (BlockScope.empty()) return true; | |||
450 | ||||
451 | // Block tail: | |||
452 | // [END_BLOCK, <align4bytes>] | |||
453 | SkipToFourByteBoundary(); | |||
454 | ||||
455 | popBlockScope(); | |||
456 | return false; | |||
457 | } | |||
458 | ||||
459 | private: | |||
460 | void popBlockScope() { | |||
461 | CurCodeSize = BlockScope.back().PrevCodeSize; | |||
462 | ||||
463 | CurAbbrevs = std::move(BlockScope.back().PrevAbbrevs); | |||
464 | BlockScope.pop_back(); | |||
465 | } | |||
466 | ||||
467 | //===--------------------------------------------------------------------===// | |||
468 | // Record Processing | |||
469 | //===--------------------------------------------------------------------===// | |||
470 | ||||
471 | public: | |||
472 | /// Return the abbreviation for the specified AbbrevId. | |||
473 | const BitCodeAbbrev *getAbbrev(unsigned AbbrevID) { | |||
474 | unsigned AbbrevNo = AbbrevID - bitc::FIRST_APPLICATION_ABBREV; | |||
475 | if (AbbrevNo >= CurAbbrevs.size()) | |||
476 | report_fatal_error("Invalid abbrev number"); | |||
477 | return CurAbbrevs[AbbrevNo].get(); | |||
478 | } | |||
479 | ||||
480 | /// Read the current record and discard it, returning the code for the record. | |||
481 | unsigned skipRecord(unsigned AbbrevID); | |||
482 | ||||
483 | unsigned readRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals, | |||
484 | StringRef *Blob = nullptr); | |||
485 | ||||
486 | //===--------------------------------------------------------------------===// | |||
487 | // Abbrev Processing | |||
488 | //===--------------------------------------------------------------------===// | |||
489 | void ReadAbbrevRecord(); | |||
490 | ||||
491 | /// Read and return a block info block from the bitstream. If an error was | |||
492 | /// encountered, return None. | |||
493 | /// | |||
494 | /// \param ReadBlockInfoNames Whether to read block/record name information in | |||
495 | /// the BlockInfo block. Only llvm-bcanalyzer uses this. | |||
496 | Optional<BitstreamBlockInfo> | |||
497 | ReadBlockInfoBlock(bool ReadBlockInfoNames = false); | |||
498 | ||||
499 | /// Set the block info to be used by this BitstreamCursor to interpret | |||
500 | /// abbreviated records. | |||
501 | void setBlockInfo(BitstreamBlockInfo *BI) { BlockInfo = BI; } | |||
502 | }; | |||
503 | ||||
504 | } // end llvm namespace | |||
505 | ||||
506 | #endif // LLVM_BITCODE_BITSTREAMREADER_H |