blob: 6f702b6e5458efb2b4c120d43f51f5bcb7f3d214 [file] [log] [blame]
Gabor Greif0e535c3c2007-07-04 21:55:50 +00001//===-- llvm-bcanalyzer.cpp - Bitcode Analyzer --------------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukman650ba8e2005-04-22 00:00:37 +00006//
Reid Spencerdb5c86d2004-06-07 17:53:43 +00007//===----------------------------------------------------------------------===//
8//
Reid Spencerddc6fb12004-06-08 05:56:58 +00009// This tool may be invoked in the following manner:
Gabor Greif0e535c3c2007-07-04 21:55:50 +000010// llvm-bcanalyzer [options] - Read LLVM bitcode from stdin
11// llvm-bcanalyzer [options] x.bc - Read LLVM bitcode from the x.bc file
Reid Spencerdb5c86d2004-06-07 17:53:43 +000012//
Reid Spencerddc6fb12004-06-08 05:56:58 +000013// Options:
Reid Spencerb3a4e0b2004-06-10 18:38:44 +000014// --help - Output information about command line switches
Gabor Greif0e535c3c2007-07-04 21:55:50 +000015// --dump - Dump low-level bitcode structure in readable format
Reid Spencerddc6fb12004-06-08 05:56:58 +000016//
Gabor Greif0e535c3c2007-07-04 21:55:50 +000017// This tool provides analytical information about a bitcode file. It is
18// intended as an aid to developers of bitcode reading and writing software. It
19// produces on std::out a summary of the bitcode file that shows various
Reid Spencerb3a4e0b2004-06-10 18:38:44 +000020// statistics about the contents of the file. By default this information is
Gabor Greif0e535c3c2007-07-04 21:55:50 +000021// detailed and contains information about individual bitcode blocks and the
Daniel Dunbar75359a7c2009-09-25 16:03:57 +000022// functions in the module.
Gabor Greif0e535c3c2007-07-04 21:55:50 +000023// The tool is also able to print a bitcode file in a straight forward text
Misha Brukman650ba8e2005-04-22 00:00:37 +000024// format that shows the containment and relationships of the information in
Gabor Greif0e535c3c2007-07-04 21:55:50 +000025// the bitcode file (-dump option).
Chris Lattnercc189892007-04-29 05:51:00 +000026//
Reid Spencerdb5c86d2004-06-07 17:53:43 +000027//===----------------------------------------------------------------------===//
28
Mehdi Aminid7ad2212016-04-01 05:33:11 +000029#include "llvm/ADT/StringExtras.h"
Teresa Johnsonad176792016-11-11 05:34:58 +000030#include "llvm/Bitcode/BitcodeReader.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000031#include "llvm/Bitcode/BitstreamReader.h"
Chris Lattner1684cee2007-04-29 20:00:02 +000032#include "llvm/Bitcode/LLVMBitCodes.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000033#include "llvm/Support/CommandLine.h"
Daniel Dunbare813b222009-09-25 16:04:21 +000034#include "llvm/Support/Format.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000035#include "llvm/Support/InitLLVM.h"
Chris Lattner76d46322006-12-06 01:18:01 +000036#include "llvm/Support/ManagedStatic.h"
Chris Lattner03997582007-04-29 08:12:22 +000037#include "llvm/Support/MemoryBuffer.h"
Mehdi Aminid7ad2212016-04-01 05:33:11 +000038#include "llvm/Support/SHA1.h"
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +000039#include "llvm/Support/WithColor.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000040#include "llvm/Support/raw_ostream.h"
Reid Spencerdb5c86d2004-06-07 17:53:43 +000041using namespace llvm;
42
43static cl::opt<std::string>
Gabor Greif0e535c3c2007-07-04 21:55:50 +000044 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Reid Spencerdb5c86d2004-06-07 17:53:43 +000045
Gabor Greif0e535c3c2007-07-04 21:55:50 +000046static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace"));
Chris Lattnerca0ea542007-04-29 08:31:14 +000047
48//===----------------------------------------------------------------------===//
49// Bitcode specific analysis.
50//===----------------------------------------------------------------------===//
51
Chris Lattner4a7ac9f2007-05-05 01:46:49 +000052static cl::opt<bool> NoHistogram("disable-histogram",
53 cl::desc("Do not print per-code histogram"));
Chris Lattner03997582007-04-29 08:12:22 +000054
Chris Lattner3543caa2007-04-29 21:48:19 +000055static cl::opt<bool>
56NonSymbolic("non-symbolic",
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000057 cl::desc("Emit numeric info in dump even if"
Chris Lattner3543caa2007-04-29 21:48:19 +000058 " symbolic info is available"));
59
Jordan Rose88eb5342014-08-30 17:07:55 +000060static cl::opt<std::string>
61 BlockInfoFilename("block-info",
62 cl::desc("Use the BLOCK_INFO from the given file"));
63
Jordan Rose0fa38b82015-05-13 18:51:49 +000064static cl::opt<bool>
65 ShowBinaryBlobs("show-binary-blobs",
66 cl::desc("Print binary blobs using hex escapes"));
67
Peter Collingbournec8556152017-07-06 17:56:01 +000068static cl::opt<std::string> CheckHash(
69 "check-hash",
70 cl::desc("Check module hash using the argument as a string table"));
71
Dan Gohmanf749ad72010-12-09 20:35:40 +000072namespace {
73
74/// CurStreamTypeType - A type for CurStreamType
75enum CurStreamTypeType {
Chris Lattner03997582007-04-29 08:12:22 +000076 UnknownBitstream,
Brian Gesiakb1358892018-04-21 23:52:04 +000077 LLVMIRBitstream,
78 ClangSerializedASTBitstream,
79 ClangSerializedDiagnosticsBitstream,
Dan Gohmanf749ad72010-12-09 20:35:40 +000080};
81
82}
83
Chris Lattner1684cee2007-04-29 20:00:02 +000084/// GetBlockName - Return a symbolic block name if known, otherwise return
Chris Lattner3543caa2007-04-29 21:48:19 +000085/// null.
Chris Lattnera6fdf5a2009-04-26 22:21:57 +000086static const char *GetBlockName(unsigned BlockID,
Peter Collingbourne77c89b62016-11-08 04:17:11 +000087 const BitstreamBlockInfo &BlockInfo,
Jordan Rose88eb5342014-08-30 17:07:55 +000088 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +000089 // Standard blocks for all bitcode files.
90 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
91 if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
92 return "BLOCKINFO_BLOCK";
Craig Toppere6cb63e2014-04-25 04:24:47 +000093 return nullptr;
Chris Lattner9181ddf2007-05-05 00:17:42 +000094 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +000095
Chris Lattnera6fdf5a2009-04-26 22:21:57 +000096 // Check to see if we have a blockinfo record for this block, with a name.
Peter Collingbourne77c89b62016-11-08 04:17:11 +000097 if (const BitstreamBlockInfo::BlockInfo *Info =
98 BlockInfo.getBlockInfo(BlockID)) {
Chris Lattnera6fdf5a2009-04-26 22:21:57 +000099 if (!Info->Name.empty())
100 return Info->Name.c_str();
101 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000102
103
Craig Toppere6cb63e2014-04-25 04:24:47 +0000104 if (CurStreamType != LLVMIRBitstream) return nullptr;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000105
Chris Lattner1684cee2007-04-29 20:00:02 +0000106 switch (BlockID) {
Sanjoy Das65c13322016-04-26 05:59:14 +0000107 default: return nullptr;
Sanjoy Das51df5fa2016-04-26 05:59:08 +0000108 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: return "OPERAND_BUNDLE_TAGS_BLOCK";
Sanjoy Das65c13322016-04-26 05:59:14 +0000109 case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK";
110 case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK";
111 case bitc::PARAMATTR_GROUP_BLOCK_ID: return "PARAMATTR_GROUP_BLOCK_ID";
112 case bitc::TYPE_BLOCK_ID_NEW: return "TYPE_BLOCK_ID";
113 case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK";
114 case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK";
Mehdi Amini5d303282015-10-26 18:37:00 +0000115 case bitc::IDENTIFICATION_BLOCK_ID:
Sanjoy Das65c13322016-04-26 05:59:14 +0000116 return "IDENTIFICATION_BLOCK_ID";
117 case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB";
118 case bitc::METADATA_BLOCK_ID: return "METADATA_BLOCK";
119 case bitc::METADATA_KIND_BLOCK_ID: return "METADATA_KIND_BLOCK";
120 case bitc::METADATA_ATTACHMENT_ID: return "METADATA_ATTACHMENT_BLOCK";
121 case bitc::USELIST_BLOCK_ID: return "USELIST_BLOCK_ID";
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000122 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
Sanjoy Das65c13322016-04-26 05:59:14 +0000123 return "GLOBALVAL_SUMMARY_BLOCK";
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000124 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
125 return "FULL_LTO_GLOBALVAL_SUMMARY_BLOCK";
Sanjoy Das65c13322016-04-26 05:59:14 +0000126 case bitc::MODULE_STRTAB_BLOCK_ID: return "MODULE_STRTAB_BLOCK";
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000127 case bitc::STRTAB_BLOCK_ID: return "STRTAB_BLOCK";
Peter Collingbourne92648c22017-06-27 23:50:11 +0000128 case bitc::SYMTAB_BLOCK_ID: return "SYMTAB_BLOCK";
Chris Lattner1684cee2007-04-29 20:00:02 +0000129 }
130}
131
Chris Lattner3543caa2007-04-29 21:48:19 +0000132/// GetCodeName - Return a symbolic code name if known, otherwise return
133/// null.
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000134static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000135 const BitstreamBlockInfo &BlockInfo,
Jordan Rose88eb5342014-08-30 17:07:55 +0000136 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +0000137 // Standard blocks for all bitcode files.
138 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
139 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
140 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000141 default: return nullptr;
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000142 case bitc::BLOCKINFO_CODE_SETBID: return "SETBID";
143 case bitc::BLOCKINFO_CODE_BLOCKNAME: return "BLOCKNAME";
144 case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME";
Chris Lattner9181ddf2007-05-05 00:17:42 +0000145 }
146 }
Craig Toppere6cb63e2014-04-25 04:24:47 +0000147 return nullptr;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000148 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000149
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000150 // Check to see if we have a blockinfo record for this record, with a name.
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000151 if (const BitstreamBlockInfo::BlockInfo *Info =
152 BlockInfo.getBlockInfo(BlockID)) {
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000153 for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i)
154 if (Info->RecordNames[i].first == CodeID)
155 return Info->RecordNames[i].second.c_str();
156 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000157
158
Craig Toppere6cb63e2014-04-25 04:24:47 +0000159 if (CurStreamType != LLVMIRBitstream) return nullptr;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000160
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000161#define STRINGIFY_CODE(PREFIX, CODE) \
162 case bitc::PREFIX##_##CODE: \
163 return #CODE;
Chris Lattner3543caa2007-04-29 21:48:19 +0000164 switch (BlockID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000165 default: return nullptr;
Chris Lattner3543caa2007-04-29 21:48:19 +0000166 case bitc::MODULE_BLOCK_ID:
167 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000168 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000169 STRINGIFY_CODE(MODULE_CODE, VERSION)
170 STRINGIFY_CODE(MODULE_CODE, TRIPLE)
171 STRINGIFY_CODE(MODULE_CODE, DATALAYOUT)
172 STRINGIFY_CODE(MODULE_CODE, ASM)
173 STRINGIFY_CODE(MODULE_CODE, SECTIONNAME)
174 STRINGIFY_CODE(MODULE_CODE, DEPLIB) // FIXME: Remove in 4.0
175 STRINGIFY_CODE(MODULE_CODE, GLOBALVAR)
176 STRINGIFY_CODE(MODULE_CODE, FUNCTION)
177 STRINGIFY_CODE(MODULE_CODE, ALIAS)
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000178 STRINGIFY_CODE(MODULE_CODE, GCNAME)
Teresa Johnsonff642b92015-09-17 20:12:00 +0000179 STRINGIFY_CODE(MODULE_CODE, VSTOFFSET)
Duncan P. N. Exon Smith68f56242016-03-25 01:29:50 +0000180 STRINGIFY_CODE(MODULE_CODE, METADATA_VALUES_UNUSED)
Teresa Johnsone1164de2016-02-10 21:55:02 +0000181 STRINGIFY_CODE(MODULE_CODE, SOURCE_FILENAME)
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000182 STRINGIFY_CODE(MODULE_CODE, HASH)
Chris Lattner3543caa2007-04-29 21:48:19 +0000183 }
Mehdi Amini5d303282015-10-26 18:37:00 +0000184 case bitc::IDENTIFICATION_BLOCK_ID:
185 switch (CodeID) {
186 default:
187 return nullptr;
188 STRINGIFY_CODE(IDENTIFICATION_CODE, STRING)
189 STRINGIFY_CODE(IDENTIFICATION_CODE, EPOCH)
190 }
Chris Lattner0b7c5122007-05-04 03:01:41 +0000191 case bitc::PARAMATTR_BLOCK_ID:
192 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000193 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000194 // FIXME: Should these be different?
Bill Wendlingd7e05d62013-02-10 23:17:10 +0000195 case bitc::PARAMATTR_CODE_ENTRY_OLD: return "ENTRY";
196 case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY";
Justin Bogner68b28d02016-03-15 22:37:25 +0000197 }
198 case bitc::PARAMATTR_GROUP_BLOCK_ID:
199 switch (CodeID) {
200 default: return nullptr;
Bill Wendlingd7e05d62013-02-10 23:17:10 +0000201 case bitc::PARAMATTR_GRP_CODE_ENTRY: return "ENTRY";
Chris Lattner0b7c5122007-05-04 03:01:41 +0000202 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000203 case bitc::TYPE_BLOCK_ID_NEW:
Chris Lattner3543caa2007-04-29 21:48:19 +0000204 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000205 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000206 STRINGIFY_CODE(TYPE_CODE, NUMENTRY)
207 STRINGIFY_CODE(TYPE_CODE, VOID)
208 STRINGIFY_CODE(TYPE_CODE, FLOAT)
209 STRINGIFY_CODE(TYPE_CODE, DOUBLE)
210 STRINGIFY_CODE(TYPE_CODE, LABEL)
211 STRINGIFY_CODE(TYPE_CODE, OPAQUE)
212 STRINGIFY_CODE(TYPE_CODE, INTEGER)
213 STRINGIFY_CODE(TYPE_CODE, POINTER)
214 STRINGIFY_CODE(TYPE_CODE, ARRAY)
215 STRINGIFY_CODE(TYPE_CODE, VECTOR)
216 STRINGIFY_CODE(TYPE_CODE, X86_FP80)
217 STRINGIFY_CODE(TYPE_CODE, FP128)
218 STRINGIFY_CODE(TYPE_CODE, PPC_FP128)
219 STRINGIFY_CODE(TYPE_CODE, METADATA)
220 STRINGIFY_CODE(TYPE_CODE, STRUCT_ANON)
221 STRINGIFY_CODE(TYPE_CODE, STRUCT_NAME)
222 STRINGIFY_CODE(TYPE_CODE, STRUCT_NAMED)
223 STRINGIFY_CODE(TYPE_CODE, FUNCTION)
Chris Lattner3543caa2007-04-29 21:48:19 +0000224 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000225
Chris Lattner3543caa2007-04-29 21:48:19 +0000226 case bitc::CONSTANTS_BLOCK_ID:
227 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000228 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000229 STRINGIFY_CODE(CST_CODE, SETTYPE)
230 STRINGIFY_CODE(CST_CODE, NULL)
231 STRINGIFY_CODE(CST_CODE, UNDEF)
232 STRINGIFY_CODE(CST_CODE, INTEGER)
233 STRINGIFY_CODE(CST_CODE, WIDE_INTEGER)
234 STRINGIFY_CODE(CST_CODE, FLOAT)
235 STRINGIFY_CODE(CST_CODE, AGGREGATE)
236 STRINGIFY_CODE(CST_CODE, STRING)
237 STRINGIFY_CODE(CST_CODE, CSTRING)
238 STRINGIFY_CODE(CST_CODE, CE_BINOP)
239 STRINGIFY_CODE(CST_CODE, CE_CAST)
240 STRINGIFY_CODE(CST_CODE, CE_GEP)
241 STRINGIFY_CODE(CST_CODE, CE_INBOUNDS_GEP)
242 STRINGIFY_CODE(CST_CODE, CE_SELECT)
243 STRINGIFY_CODE(CST_CODE, CE_EXTRACTELT)
244 STRINGIFY_CODE(CST_CODE, CE_INSERTELT)
245 STRINGIFY_CODE(CST_CODE, CE_SHUFFLEVEC)
246 STRINGIFY_CODE(CST_CODE, CE_CMP)
247 STRINGIFY_CODE(CST_CODE, INLINEASM)
248 STRINGIFY_CODE(CST_CODE, CE_SHUFVEC_EX)
Cameron McInallycbde0d92018-11-13 18:15:47 +0000249 STRINGIFY_CODE(CST_CODE, CE_UNOP)
Chris Lattner372dd1e2012-01-30 00:51:16 +0000250 case bitc::CST_CODE_BLOCKADDRESS: return "CST_CODE_BLOCKADDRESS";
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000251 STRINGIFY_CODE(CST_CODE, DATA)
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000252 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000253 case bitc::FUNCTION_BLOCK_ID:
254 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000255 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000256 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)
Cameron McInallycbde0d92018-11-13 18:15:47 +0000270 STRINGIFY_CODE(FUNC_CODE, INST_UNOP)
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000271 STRINGIFY_CODE(FUNC_CODE, INST_UNREACHABLE)
David Majnemer654e1302015-07-31 17:58:14 +0000272 STRINGIFY_CODE(FUNC_CODE, INST_CLEANUPRET)
273 STRINGIFY_CODE(FUNC_CODE, INST_CATCHRET)
274 STRINGIFY_CODE(FUNC_CODE, INST_CATCHPAD)
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000275 STRINGIFY_CODE(FUNC_CODE, INST_PHI)
276 STRINGIFY_CODE(FUNC_CODE, INST_ALLOCA)
277 STRINGIFY_CODE(FUNC_CODE, INST_LOAD)
278 STRINGIFY_CODE(FUNC_CODE, INST_VAARG)
279 STRINGIFY_CODE(FUNC_CODE, INST_STORE)
280 STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTVAL)
281 STRINGIFY_CODE(FUNC_CODE, INST_INSERTVAL)
282 STRINGIFY_CODE(FUNC_CODE, INST_CMP2)
283 STRINGIFY_CODE(FUNC_CODE, INST_VSELECT)
284 STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC_AGAIN)
285 STRINGIFY_CODE(FUNC_CODE, INST_CALL)
286 STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC)
287 STRINGIFY_CODE(FUNC_CODE, INST_GEP)
Sanjoy Das51df5fa2016-04-26 05:59:08 +0000288 STRINGIFY_CODE(FUNC_CODE, OPERAND_BUNDLE)
Matt Arsenaultb53feca2018-09-24 12:47:17 +0000289 STRINGIFY_CODE(FUNC_CODE, INST_FENCE)
290 STRINGIFY_CODE(FUNC_CODE, INST_ATOMICRMW)
291 STRINGIFY_CODE(FUNC_CODE, INST_LOADATOMIC)
292 STRINGIFY_CODE(FUNC_CODE, INST_STOREATOMIC)
293 STRINGIFY_CODE(FUNC_CODE, INST_CMPXCHG)
Chris Lattner3543caa2007-04-29 21:48:19 +0000294 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000295 case bitc::VALUE_SYMTAB_BLOCK_ID:
296 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000297 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000298 STRINGIFY_CODE(VST_CODE, ENTRY)
299 STRINGIFY_CODE(VST_CODE, BBENTRY)
Teresa Johnsonff642b92015-09-17 20:12:00 +0000300 STRINGIFY_CODE(VST_CODE, FNENTRY)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000301 STRINGIFY_CODE(VST_CODE, COMBINED_ENTRY)
Teresa Johnson403a7872015-10-04 14:33:43 +0000302 }
303 case bitc::MODULE_STRTAB_BLOCK_ID:
304 switch (CodeID) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000305 default:
306 return nullptr;
307 STRINGIFY_CODE(MST_CODE, ENTRY)
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000308 STRINGIFY_CODE(MST_CODE, HASH)
Teresa Johnson403a7872015-10-04 14:33:43 +0000309 }
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000310 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000311 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
Teresa Johnson403a7872015-10-04 14:33:43 +0000312 switch (CodeID) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000313 default:
314 return nullptr;
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000315 STRINGIFY_CODE(FS, PERMODULE)
316 STRINGIFY_CODE(FS, PERMODULE_PROFILE)
Easwaran Ramanc73cec82018-01-25 19:27:17 +0000317 STRINGIFY_CODE(FS, PERMODULE_RELBF)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000318 STRINGIFY_CODE(FS, PERMODULE_GLOBALVAR_INIT_REFS)
319 STRINGIFY_CODE(FS, COMBINED)
320 STRINGIFY_CODE(FS, COMBINED_PROFILE)
321 STRINGIFY_CODE(FS, COMBINED_GLOBALVAR_INIT_REFS)
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000322 STRINGIFY_CODE(FS, ALIAS)
323 STRINGIFY_CODE(FS, COMBINED_ALIAS)
Mehdi Aminiae64eaf2016-04-23 23:38:17 +0000324 STRINGIFY_CODE(FS, COMBINED_ORIGINAL_NAME)
Mehdi Amini8fe69362016-04-24 03:18:11 +0000325 STRINGIFY_CODE(FS, VERSION)
Teresa Johnsonf3681012018-02-07 04:05:59 +0000326 STRINGIFY_CODE(FS, FLAGS)
Peter Collingbourne1b4137a72016-12-21 23:03:45 +0000327 STRINGIFY_CODE(FS, TYPE_TESTS)
Peter Collingbournebe9ffaa2017-02-10 22:29:38 +0000328 STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_VCALLS)
329 STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_VCALLS)
330 STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_CONST_VCALL)
331 STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_CONST_VCALL)
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000332 STRINGIFY_CODE(FS, VALUE_GUID)
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +0000333 STRINGIFY_CODE(FS, CFI_FUNCTION_DEFS)
334 STRINGIFY_CODE(FS, CFI_FUNCTION_DECLS)
Vitaly Buka44396fa2018-02-14 22:41:15 +0000335 STRINGIFY_CODE(FS, TYPE_ID)
Chris Lattner3543caa2007-04-29 21:48:19 +0000336 }
Devang Patelaf206b82009-09-18 19:26:43 +0000337 case bitc::METADATA_ATTACHMENT_ID:
338 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000339 default:return nullptr;
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000340 STRINGIFY_CODE(METADATA, ATTACHMENT)
Devang Patelaf206b82009-09-18 19:26:43 +0000341 }
Devang Patel7428d8a2009-07-22 17:43:22 +0000342 case bitc::METADATA_BLOCK_ID:
343 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000344 default:return nullptr;
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000345 STRINGIFY_CODE(METADATA, STRING_OLD)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000346 STRINGIFY_CODE(METADATA, VALUE)
Adrian Prantl36daf632017-01-03 19:17:49 +0000347 STRINGIFY_CODE(METADATA, NODE)
348 STRINGIFY_CODE(METADATA, NAME)
349 STRINGIFY_CODE(METADATA, DISTINCT_NODE)
350 STRINGIFY_CODE(METADATA, KIND) // Older bitcode has it in a MODULE_BLOCK
351 STRINGIFY_CODE(METADATA, LOCATION)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000352 STRINGIFY_CODE(METADATA, OLD_NODE)
353 STRINGIFY_CODE(METADATA, OLD_FN_NODE)
354 STRINGIFY_CODE(METADATA, NAMED_NODE)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000355 STRINGIFY_CODE(METADATA, GENERIC_DEBUG)
356 STRINGIFY_CODE(METADATA, SUBRANGE)
357 STRINGIFY_CODE(METADATA, ENUMERATOR)
358 STRINGIFY_CODE(METADATA, BASIC_TYPE)
359 STRINGIFY_CODE(METADATA, FILE)
360 STRINGIFY_CODE(METADATA, DERIVED_TYPE)
361 STRINGIFY_CODE(METADATA, COMPOSITE_TYPE)
362 STRINGIFY_CODE(METADATA, SUBROUTINE_TYPE)
363 STRINGIFY_CODE(METADATA, COMPILE_UNIT)
364 STRINGIFY_CODE(METADATA, SUBPROGRAM)
365 STRINGIFY_CODE(METADATA, LEXICAL_BLOCK)
366 STRINGIFY_CODE(METADATA, LEXICAL_BLOCK_FILE)
367 STRINGIFY_CODE(METADATA, NAMESPACE)
368 STRINGIFY_CODE(METADATA, TEMPLATE_TYPE)
369 STRINGIFY_CODE(METADATA, TEMPLATE_VALUE)
370 STRINGIFY_CODE(METADATA, GLOBAL_VAR)
371 STRINGIFY_CODE(METADATA, LOCAL_VAR)
372 STRINGIFY_CODE(METADATA, EXPRESSION)
373 STRINGIFY_CODE(METADATA, OBJC_PROPERTY)
374 STRINGIFY_CODE(METADATA, IMPORTED_ENTITY)
Adrian Prantla7ad09d2015-06-30 00:25:41 +0000375 STRINGIFY_CODE(METADATA, MODULE)
Adrian Prantl36daf632017-01-03 19:17:49 +0000376 STRINGIFY_CODE(METADATA, MACRO)
377 STRINGIFY_CODE(METADATA, MACRO_FILE)
378 STRINGIFY_CODE(METADATA, STRINGS)
379 STRINGIFY_CODE(METADATA, GLOBAL_DECL_ATTACHMENT)
380 STRINGIFY_CODE(METADATA, GLOBAL_VAR_EXPR)
Mehdi Aminie98f9252016-12-28 22:30:28 +0000381 STRINGIFY_CODE(METADATA, INDEX_OFFSET)
382 STRINGIFY_CODE(METADATA, INDEX)
Devang Patel7428d8a2009-07-22 17:43:22 +0000383 }
Teresa Johnson12545072015-11-15 02:00:09 +0000384 case bitc::METADATA_KIND_BLOCK_ID:
385 switch (CodeID) {
386 default:
387 return nullptr;
388 STRINGIFY_CODE(METADATA, KIND)
389 }
Chad Rosierdd4ffae2011-12-07 21:45:13 +0000390 case bitc::USELIST_BLOCK_ID:
391 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000392 default:return nullptr;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000393 case bitc::USELIST_CODE_DEFAULT: return "USELIST_CODE_DEFAULT";
394 case bitc::USELIST_CODE_BB: return "USELIST_CODE_BB";
Chad Rosierdd4ffae2011-12-07 21:45:13 +0000395 }
Sanjoy Das51df5fa2016-04-26 05:59:08 +0000396
397 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
398 switch(CodeID) {
399 default: return nullptr;
400 case bitc::OPERAND_BUNDLE_TAG: return "OPERAND_BUNDLE_TAG";
401 }
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000402 case bitc::STRTAB_BLOCK_ID:
403 switch(CodeID) {
404 default: return nullptr;
405 case bitc::STRTAB_BLOB: return "BLOB";
406 }
Peter Collingbourne92648c22017-06-27 23:50:11 +0000407 case bitc::SYMTAB_BLOCK_ID:
408 switch(CodeID) {
409 default: return nullptr;
410 case bitc::SYMTAB_BLOB: return "BLOB";
411 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000412 }
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000413#undef STRINGIFY_CODE
Chris Lattner3543caa2007-04-29 21:48:19 +0000414}
415
Chris Lattnerbf419a92009-04-27 17:59:34 +0000416struct PerRecordStats {
417 unsigned NumInstances;
Chris Lattner1cf80692009-04-27 18:15:27 +0000418 unsigned NumAbbrev;
419 uint64_t TotalBits;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000420
Mehdi Aminid2638562015-10-21 06:10:55 +0000421 PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
Chris Lattnerbf419a92009-04-27 17:59:34 +0000422};
Chris Lattner1684cee2007-04-29 20:00:02 +0000423
424struct PerBlockIDStats {
425 /// NumInstances - This the number of times this block ID has been seen.
426 unsigned NumInstances;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000427
Chris Lattner1684cee2007-04-29 20:00:02 +0000428 /// NumBits - The total size in bits of all of these blocks.
429 uint64_t NumBits;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000430
Chris Lattner1684cee2007-04-29 20:00:02 +0000431 /// NumSubBlocks - The total number of blocks these blocks contain.
432 unsigned NumSubBlocks;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000433
Chris Lattner1684cee2007-04-29 20:00:02 +0000434 /// NumAbbrevs - The total number of abbreviations.
435 unsigned NumAbbrevs;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000436
437 /// NumRecords - The total number of records these blocks contain, and the
Chris Lattner1684cee2007-04-29 20:00:02 +0000438 /// number that are abbreviated.
439 unsigned NumRecords, NumAbbreviatedRecords;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000440
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000441 /// CodeFreq - Keep track of the number of times we see each code.
Chris Lattnerbf419a92009-04-27 17:59:34 +0000442 std::vector<PerRecordStats> CodeFreq;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000443
Chris Lattner1684cee2007-04-29 20:00:02 +0000444 PerBlockIDStats()
445 : NumInstances(0), NumBits(0),
446 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
447};
448
449static std::map<unsigned, PerBlockIDStats> BlockIDStats;
450
451
452
Zachary Turner2ee505e2016-10-03 18:17:18 +0000453/// ReportError - All bitcode analysis errors go through this function, making this a
Chris Lattnerca0ea542007-04-29 08:31:14 +0000454/// good place to breakpoint if debugging.
Zachary Turner2ee505e2016-10-03 18:17:18 +0000455static bool ReportError(const Twine &Err) {
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000456 WithColor::error() << Err << "\n";
Chris Lattnerca0ea542007-04-29 08:31:14 +0000457 return true;
458}
459
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000460static bool decodeMetadataStringsBlob(StringRef Indent,
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000461 ArrayRef<uint64_t> Record,
462 StringRef Blob) {
463 if (Blob.empty())
464 return true;
465
466 if (Record.size() != 2)
467 return true;
468
469 unsigned NumStrings = Record[0];
470 unsigned StringsOffset = Record[1];
471 outs() << " num-strings = " << NumStrings << " {\n";
472
473 StringRef Lengths = Blob.slice(0, StringsOffset);
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000474 SimpleBitstreamCursor R(Lengths);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000475 StringRef Strings = Blob.drop_front(StringsOffset);
476 do {
477 if (R.AtEndOfStream())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000478 return ReportError("bad length");
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000479
480 unsigned Size = R.ReadVBR(6);
481 if (Strings.size() < Size)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000482 return ReportError("truncated chars");
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000483
484 outs() << Indent << " '";
485 outs().write_escaped(Strings.slice(0, Size), /*hex=*/true);
486 outs() << "'\n";
487 Strings = Strings.drop_front(Size);
488 } while (--NumStrings);
489
490 outs() << Indent << " }";
491 return false;
492}
493
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000494static bool decodeBlob(unsigned Code, unsigned BlockID, StringRef Indent,
495 ArrayRef<uint64_t> Record, StringRef Blob) {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000496 if (BlockID != bitc::METADATA_BLOCK_ID)
497 return true;
498 if (Code != bitc::METADATA_STRINGS)
499 return true;
500
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000501 return decodeMetadataStringsBlob(Indent, Record, Blob);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000502}
503
Chris Lattnerca0ea542007-04-29 08:31:14 +0000504/// ParseBlock - Read a block, updating statistics, etc.
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000505static bool ParseBlock(BitstreamCursor &Stream, BitstreamBlockInfo &BlockInfo,
506 unsigned BlockID, unsigned IndentLevel,
507 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +0000508 std::string Indent(IndentLevel*2, ' ');
Chris Lattner1684cee2007-04-29 20:00:02 +0000509 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
Chris Lattner3543caa2007-04-29 21:48:19 +0000510
Chris Lattner1684cee2007-04-29 20:00:02 +0000511 // Get the statistics for this BlockID.
512 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000513
Chris Lattner1684cee2007-04-29 20:00:02 +0000514 BlockStats.NumInstances++;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000515
Chris Lattner9181ddf2007-05-05 00:17:42 +0000516 // BLOCKINFO is a special part of the stream.
Richard Smithdc1414b2016-02-06 00:46:09 +0000517 bool DumpRecords = Dump;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000518 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Chris Lattner633ab162012-03-19 23:40:48 +0000519 if (Dump) outs() << Indent << "<BLOCKINFO_BLOCK/>\n";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000520 Optional<BitstreamBlockInfo> NewBlockInfo =
521 Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
522 if (!NewBlockInfo)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000523 return ReportError("Malformed BlockInfoBlock");
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000524 BlockInfo = std::move(*NewBlockInfo);
525 Stream.JumpToBit(BlockBitStart);
Richard Smithdc1414b2016-02-06 00:46:09 +0000526 // It's not really interesting to dump the contents of the blockinfo block.
527 DumpRecords = false;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000528 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000529
Chris Lattner3543caa2007-04-29 21:48:19 +0000530 unsigned NumWords = 0;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000531 if (Stream.EnterSubBlock(BlockID, &NumWords))
Zachary Turner2ee505e2016-10-03 18:17:18 +0000532 return ReportError("Malformed block record");
Chris Lattnerca0ea542007-04-29 08:31:14 +0000533
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000534 // Keep it for later, when we see a MODULE_HASH record
535 uint64_t BlockEntryPos = Stream.getCurrentByteNo();
536
Craig Toppere6cb63e2014-04-25 04:24:47 +0000537 const char *BlockName = nullptr;
Richard Smithdc1414b2016-02-06 00:46:09 +0000538 if (DumpRecords) {
Chris Lattner633ab162012-03-19 23:40:48 +0000539 outs() << Indent << "<";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000540 if ((BlockName = GetBlockName(BlockID, BlockInfo, CurStreamType)))
Chris Lattner633ab162012-03-19 23:40:48 +0000541 outs() << BlockName;
Chris Lattner3543caa2007-04-29 21:48:19 +0000542 else
Chris Lattner633ab162012-03-19 23:40:48 +0000543 outs() << "UnknownBlock" << BlockID;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000544
Chris Lattner3543caa2007-04-29 21:48:19 +0000545 if (NonSymbolic && BlockName)
Chris Lattner633ab162012-03-19 23:40:48 +0000546 outs() << " BlockID=" << BlockID;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000547
Chris Lattner633ab162012-03-19 23:40:48 +0000548 outs() << " NumWords=" << NumWords
Chris Lattner3fa323d2013-01-19 21:37:14 +0000549 << " BlockCodeSize=" << Stream.getAbbrevIDWidth() << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000550 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000551
Chris Lattnerca0ea542007-04-29 08:31:14 +0000552 SmallVector<uint64_t, 64> Record;
553
Mehdi Aminie98f9252016-12-28 22:30:28 +0000554 // Keep the offset to the metadata index if seen.
555 uint64_t MetadataIndexOffset = 0;
556
Chris Lattnerca0ea542007-04-29 08:31:14 +0000557 // Read all the records for this block.
558 while (1) {
559 if (Stream.AtEndOfStream())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000560 return ReportError("Premature end of bitstream");
Chris Lattnerca0ea542007-04-29 08:31:14 +0000561
Chris Lattner1cf80692009-04-27 18:15:27 +0000562 uint64_t RecordStartBit = Stream.GetCurrentBitNo();
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000563
Chris Lattner0271af82013-01-20 02:50:32 +0000564 BitstreamEntry Entry =
565 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
566
567 switch (Entry.Kind) {
568 case BitstreamEntry::Error:
Zachary Turner2ee505e2016-10-03 18:17:18 +0000569 return ReportError("malformed bitcode file");
Chris Lattner0271af82013-01-20 02:50:32 +0000570 case BitstreamEntry::EndBlock: {
Chris Lattner1684cee2007-04-29 20:00:02 +0000571 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
572 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
Richard Smithdc1414b2016-02-06 00:46:09 +0000573 if (DumpRecords) {
Chris Lattner633ab162012-03-19 23:40:48 +0000574 outs() << Indent << "</";
Chris Lattner3543caa2007-04-29 21:48:19 +0000575 if (BlockName)
Chris Lattner633ab162012-03-19 23:40:48 +0000576 outs() << BlockName << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000577 else
Chris Lattner633ab162012-03-19 23:40:48 +0000578 outs() << "UnknownBlock" << BlockID << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000579 }
Chris Lattnerca0ea542007-04-29 08:31:14 +0000580 return false;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000581 }
Chris Lattner0271af82013-01-20 02:50:32 +0000582
583 case BitstreamEntry::SubBlock: {
Chris Lattner9e808cd2007-05-05 01:29:31 +0000584 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000585 if (ParseBlock(Stream, BlockInfo, Entry.ID, IndentLevel + 1,
586 CurStreamType))
Chris Lattnerca0ea542007-04-29 08:31:14 +0000587 return true;
Chris Lattner1684cee2007-04-29 20:00:02 +0000588 ++BlockStats.NumSubBlocks;
Chris Lattner9e808cd2007-05-05 01:29:31 +0000589 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
Chris Lattner0271af82013-01-20 02:50:32 +0000590
Chris Lattner9e808cd2007-05-05 01:29:31 +0000591 // Don't include subblock sizes in the size of this block.
592 BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
Chris Lattner0271af82013-01-20 02:50:32 +0000593 continue;
594 }
595 case BitstreamEntry::Record:
596 // The interesting case.
Chris Lattnerca0ea542007-04-29 08:31:14 +0000597 break;
Chris Lattner9e808cd2007-05-05 01:29:31 +0000598 }
Chris Lattner0271af82013-01-20 02:50:32 +0000599
600 if (Entry.ID == bitc::DEFINE_ABBREV) {
Chris Lattnerca0ea542007-04-29 08:31:14 +0000601 Stream.ReadAbbrevRecord();
Chris Lattner1684cee2007-04-29 20:00:02 +0000602 ++BlockStats.NumAbbrevs;
Chris Lattner0271af82013-01-20 02:50:32 +0000603 continue;
604 }
605
606 Record.clear();
Chris Lattner2ed6a202009-04-06 22:44:40 +0000607
Chris Lattner0271af82013-01-20 02:50:32 +0000608 ++BlockStats.NumRecords;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000609
Chris Lattner0271af82013-01-20 02:50:32 +0000610 StringRef Blob;
Teresa Johnson0cff9352018-06-04 19:20:02 +0000611 uint64_t CurrentRecordPos = Stream.GetCurrentBitNo();
Chris Lattner0271af82013-01-20 02:50:32 +0000612 unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000613
Chris Lattner0271af82013-01-20 02:50:32 +0000614 // Increment the # occurrences of this code.
615 if (BlockStats.CodeFreq.size() <= Code)
616 BlockStats.CodeFreq.resize(Code+1);
617 BlockStats.CodeFreq[Code].NumInstances++;
618 BlockStats.CodeFreq[Code].TotalBits +=
619 Stream.GetCurrentBitNo()-RecordStartBit;
620 if (Entry.ID != bitc::UNABBREV_RECORD) {
621 BlockStats.CodeFreq[Code].NumAbbrev++;
622 ++BlockStats.NumAbbreviatedRecords;
623 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000624
Richard Smithdc1414b2016-02-06 00:46:09 +0000625 if (DumpRecords) {
Chris Lattner0271af82013-01-20 02:50:32 +0000626 outs() << Indent << " <";
627 if (const char *CodeName =
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000628 GetCodeName(Code, BlockID, BlockInfo, CurStreamType))
Chris Lattner0271af82013-01-20 02:50:32 +0000629 outs() << CodeName;
630 else
631 outs() << "UnknownCode" << Code;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000632 if (NonSymbolic && GetCodeName(Code, BlockID, BlockInfo, CurStreamType))
Chris Lattner0271af82013-01-20 02:50:32 +0000633 outs() << " codeid=" << Code;
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000634 const BitCodeAbbrev *Abbv = nullptr;
635 if (Entry.ID != bitc::UNABBREV_RECORD) {
636 Abbv = Stream.getAbbrev(Entry.ID);
Chris Lattner0271af82013-01-20 02:50:32 +0000637 outs() << " abbrevid=" << Entry.ID;
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000638 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000639
Chris Lattner0271af82013-01-20 02:50:32 +0000640 for (unsigned i = 0, e = Record.size(); i != e; ++i)
641 outs() << " op" << i << "=" << (int64_t)Record[i];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000642
Mehdi Aminie98f9252016-12-28 22:30:28 +0000643 // If we found a metadata index, let's verify that we had an offset before
644 // and validate its forward reference offset was correct!
645 if (BlockID == bitc::METADATA_BLOCK_ID) {
646 if (Code == bitc::METADATA_INDEX_OFFSET) {
Mehdi Amini5022bb72016-12-28 23:45:54 +0000647 if (Record.size() != 2)
648 outs() << "(Invalid record)";
649 else {
650 auto Offset = Record[0] + (Record[1] << 32);
651 MetadataIndexOffset = Stream.GetCurrentBitNo() + Offset;
652 }
Mehdi Aminie98f9252016-12-28 22:30:28 +0000653 }
654 if (Code == bitc::METADATA_INDEX) {
655 outs() << " (offset ";
656 if (MetadataIndexOffset == RecordStartBit)
657 outs() << "match)";
658 else
659 outs() << "mismatch: " << MetadataIndexOffset << " vs "
660 << RecordStartBit << ")";
661 }
662 }
663
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000664 // If we found a module hash, let's verify that it matches!
Peter Collingbournec8556152017-07-06 17:56:01 +0000665 if (BlockID == bitc::MODULE_BLOCK_ID && Code == bitc::MODULE_CODE_HASH &&
666 !CheckHash.empty()) {
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000667 if (Record.size() != 5)
668 outs() << " (invalid)";
669 else {
670 // Recompute the hash and compare it to the one in the bitcode
671 SHA1 Hasher;
672 StringRef Hash;
Peter Collingbournec8556152017-07-06 17:56:01 +0000673 Hasher.update(CheckHash);
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000674 {
Peter Collingbournecf2750a2016-12-01 05:47:58 +0000675 int BlockSize = (CurrentRecordPos / 8) - BlockEntryPos;
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000676 auto Ptr = Stream.getPointerToByte(BlockEntryPos, BlockSize);
677 Hasher.update(ArrayRef<uint8_t>(Ptr, BlockSize));
678 Hash = Hasher.result();
679 }
680 SmallString<20> RecordedHash;
681 RecordedHash.resize(20);
682 int Pos = 0;
683 for (auto &Val : Record) {
684 assert(!(Val >> 32) && "Unexpected high bits set");
685 RecordedHash[Pos++] = (Val >> 24) & 0xFF;
686 RecordedHash[Pos++] = (Val >> 16) & 0xFF;
687 RecordedHash[Pos++] = (Val >> 8) & 0xFF;
688 RecordedHash[Pos++] = (Val >> 0) & 0xFF;
689 }
690 if (Hash == RecordedHash)
691 outs() << " (match)";
692 else
693 outs() << " (!mismatch!)";
694 }
695 }
696
Chris Lattner0271af82013-01-20 02:50:32 +0000697 outs() << "/>";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000698
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000699 if (Abbv) {
700 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
701 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
702 if (!Op.isEncoding() || Op.getEncoding() != BitCodeAbbrevOp::Array)
703 continue;
704 assert(i + 2 == e && "Array op not second to last");
705 std::string Str;
706 bool ArrayIsPrintable = true;
707 for (unsigned j = i - 1, je = Record.size(); j != je; ++j) {
Michael Kruse6f1da6e2018-07-26 15:31:41 +0000708 if (!isPrint(static_cast<unsigned char>(Record[j]))) {
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000709 ArrayIsPrintable = false;
710 break;
711 }
712 Str += (char)Record[j];
713 }
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000714 if (ArrayIsPrintable)
715 outs() << " record string = '" << Str << "'";
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000716 break;
717 }
718 }
719
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000720 if (Blob.data() && decodeBlob(Code, BlockID, Indent, Record, Blob)) {
Chris Lattner0271af82013-01-20 02:50:32 +0000721 outs() << " blob data = ";
Jordan Rose0fa38b82015-05-13 18:51:49 +0000722 if (ShowBinaryBlobs) {
723 outs() << "'";
724 outs().write_escaped(Blob, /*hex=*/true) << "'";
725 } else {
726 bool BlobIsPrintable = true;
727 for (unsigned i = 0, e = Blob.size(); i != e; ++i)
Michael Kruse6f1da6e2018-07-26 15:31:41 +0000728 if (!isPrint(static_cast<unsigned char>(Blob[i]))) {
Jordan Rose0fa38b82015-05-13 18:51:49 +0000729 BlobIsPrintable = false;
730 break;
731 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000732
Jordan Rose0fa38b82015-05-13 18:51:49 +0000733 if (BlobIsPrintable)
734 outs() << "'" << Blob << "'";
735 else
736 outs() << "unprintable, " << Blob.size() << " bytes.";
737 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000738 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000739
Chris Lattner0271af82013-01-20 02:50:32 +0000740 outs() << "\n";
Chris Lattnerca0ea542007-04-29 08:31:14 +0000741 }
Peter Collingbournecf2750a2016-12-01 05:47:58 +0000742
743 // Make sure that we can skip the current record.
744 Stream.JumpToBit(CurrentRecordPos);
745 Stream.skipRecord(Entry.ID);
Chris Lattnerca0ea542007-04-29 08:31:14 +0000746 }
747}
748
Chris Lattner1684cee2007-04-29 20:00:02 +0000749static void PrintSize(double Bits) {
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000750 outs() << format("%.2f/%.2fB/%luW", Bits, Bits/8,(unsigned long)(Bits/32));
Chris Lattnerbf419a92009-04-27 17:59:34 +0000751}
752static void PrintSize(uint64_t Bits) {
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000753 outs() << format("%lub/%.2fB/%luW", (unsigned long)Bits,
754 (double)Bits/8, (unsigned long)(Bits/32));
Chris Lattner1684cee2007-04-29 20:00:02 +0000755}
756
Brian Gesiakb1358892018-04-21 23:52:04 +0000757static CurStreamTypeType ReadSignature(BitstreamCursor &Stream) {
758 char Signature[6];
759 Signature[0] = Stream.Read(8);
760 Signature[1] = Stream.Read(8);
761
762 // Autodetect the file contents, if it is one we know.
763 if (Signature[0] == 'C' && Signature[1] == 'P') {
764 Signature[2] = Stream.Read(8);
765 Signature[3] = Stream.Read(8);
766 if (Signature[2] == 'C' && Signature[3] == 'H')
767 return ClangSerializedASTBitstream;
768 } else if (Signature[0] == 'D' && Signature[1] == 'I') {
769 Signature[2] = Stream.Read(8);
770 Signature[3] = Stream.Read(8);
771 if (Signature[2] == 'A' && Signature[3] == 'G')
772 return ClangSerializedDiagnosticsBitstream;
773 } else {
774 Signature[2] = Stream.Read(4);
775 Signature[3] = Stream.Read(4);
776 Signature[4] = Stream.Read(4);
777 Signature[5] = Stream.Read(4);
778 if (Signature[0] == 'B' && Signature[1] == 'C' &&
779 Signature[2] == 0x0 && Signature[3] == 0xC &&
780 Signature[4] == 0xE && Signature[5] == 0xD)
781 return LLVMIRBitstream;
782 }
783 return UnknownBitstream;
784}
785
Jordan Rose88eb5342014-08-30 17:07:55 +0000786static bool openBitcodeFile(StringRef Path,
787 std::unique_ptr<MemoryBuffer> &MemBuf,
Jordan Rose88eb5342014-08-30 17:07:55 +0000788 BitstreamCursor &Stream,
789 CurStreamTypeType &CurStreamType) {
Chris Lattner03997582007-04-29 08:12:22 +0000790 // Read the input file.
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000791 ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr =
Jordan Rose88eb5342014-08-30 17:07:55 +0000792 MemoryBuffer::getFileOrSTDIN(Path);
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000793 if (std::error_code EC = MemBufOrErr.getError())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000794 return ReportError(Twine("ReportError reading '") + Path + "': " + EC.message());
Jordan Rose88eb5342014-08-30 17:07:55 +0000795 MemBuf = std::move(MemBufOrErr.get());
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000796
Jordan Rose88eb5342014-08-30 17:07:55 +0000797 if (MemBuf->getBufferSize() & 3)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000798 return ReportError("Bitcode stream should be a multiple of 4 bytes in length");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000799
Jordan Rose88eb5342014-08-30 17:07:55 +0000800 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart();
801 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize();
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000802
Chris Lattnerb9e07fd2009-04-06 20:54:32 +0000803 // If we have a wrapper header, parse it and ignore the non-bc file contents.
804 // The magic number is 0x0B17C0DE stored in little endian.
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000805 if (isBitcodeWrapper(BufPtr, EndBufPtr)) {
Mehdi Aminieed26932016-04-01 05:19:14 +0000806 if (MemBuf->getBufferSize() < BWH_HeaderSize)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000807 return ReportError("Invalid bitcode wrapper header");
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000808
809 if (Dump) {
810 unsigned Magic = support::endian::read32le(&BufPtr[BWH_MagicField]);
811 unsigned Version = support::endian::read32le(&BufPtr[BWH_VersionField]);
812 unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]);
813 unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]);
814 unsigned CPUType = support::endian::read32le(&BufPtr[BWH_CPUTypeField]);
815
816 outs() << "<BITCODE_WRAPPER_HEADER"
817 << " Magic=" << format_hex(Magic, 10)
818 << " Version=" << format_hex(Version, 10)
819 << " Offset=" << format_hex(Offset, 10)
820 << " Size=" << format_hex(Size, 10)
821 << " CPUType=" << format_hex(CPUType, 10) << "/>\n";
822 }
823
Derek Schuff8b2dcad2012-02-06 22:30:29 +0000824 if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr, true))
Zachary Turner2ee505e2016-10-03 18:17:18 +0000825 return ReportError("Invalid bitcode wrapper header");
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000826 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000827
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000828 Stream = BitstreamCursor(ArrayRef<uint8_t>(BufPtr, EndBufPtr));
Brian Gesiakb1358892018-04-21 23:52:04 +0000829 CurStreamType = ReadSignature(Stream);
Chris Lattner03997582007-04-29 08:12:22 +0000830
Jordan Rose88eb5342014-08-30 17:07:55 +0000831 return false;
832}
833
834/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
835static int AnalyzeBitcode() {
836 std::unique_ptr<MemoryBuffer> StreamBuffer;
Jordan Rose88eb5342014-08-30 17:07:55 +0000837 BitstreamCursor Stream;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000838 BitstreamBlockInfo BlockInfo;
Jordan Rose88eb5342014-08-30 17:07:55 +0000839 CurStreamTypeType CurStreamType;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000840 if (openBitcodeFile(InputFilename, StreamBuffer, Stream, CurStreamType))
Jordan Rose88eb5342014-08-30 17:07:55 +0000841 return true;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000842 Stream.setBlockInfo(&BlockInfo);
Jordan Rose88eb5342014-08-30 17:07:55 +0000843
844 // Read block info from BlockInfoFilename, if specified.
845 // The block info must be a top-level block.
846 if (!BlockInfoFilename.empty()) {
847 std::unique_ptr<MemoryBuffer> BlockInfoBuffer;
Jordan Rose88eb5342014-08-30 17:07:55 +0000848 BitstreamCursor BlockInfoCursor;
849 CurStreamTypeType BlockInfoStreamType;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000850 if (openBitcodeFile(BlockInfoFilename, BlockInfoBuffer, BlockInfoCursor,
851 BlockInfoStreamType))
Jordan Rose88eb5342014-08-30 17:07:55 +0000852 return true;
853
854 while (!BlockInfoCursor.AtEndOfStream()) {
855 unsigned Code = BlockInfoCursor.ReadCode();
856 if (Code != bitc::ENTER_SUBBLOCK)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000857 return ReportError("Invalid record at top-level in block info file");
Jordan Rose88eb5342014-08-30 17:07:55 +0000858
859 unsigned BlockID = BlockInfoCursor.ReadSubBlockID();
860 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000861 Optional<BitstreamBlockInfo> NewBlockInfo =
862 BlockInfoCursor.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
863 if (!NewBlockInfo)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000864 return ReportError("Malformed BlockInfoBlock in block info file");
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000865 BlockInfo = std::move(*NewBlockInfo);
Jordan Rose88eb5342014-08-30 17:07:55 +0000866 break;
867 }
868
869 BlockInfoCursor.SkipBlock();
870 }
Jordan Rose88eb5342014-08-30 17:07:55 +0000871 }
872
Chris Lattner1684cee2007-04-29 20:00:02 +0000873 unsigned NumTopBlocks = 0;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000874
Chris Lattnerca0ea542007-04-29 08:31:14 +0000875 // Parse the top-level structure. We only allow blocks at the top-level.
876 while (!Stream.AtEndOfStream()) {
877 unsigned Code = Stream.ReadCode();
878 if (Code != bitc::ENTER_SUBBLOCK)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000879 return ReportError("Invalid record at top-level");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000880
Chris Lattner0271af82013-01-20 02:50:32 +0000881 unsigned BlockID = Stream.ReadSubBlockID();
882
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000883 if (ParseBlock(Stream, BlockInfo, BlockID, 0, CurStreamType))
Chris Lattnerca0ea542007-04-29 08:31:14 +0000884 return true;
Chris Lattner1684cee2007-04-29 20:00:02 +0000885 ++NumTopBlocks;
Chris Lattnerca0ea542007-04-29 08:31:14 +0000886 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000887
Chris Lattner633ab162012-03-19 23:40:48 +0000888 if (Dump) outs() << "\n\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000889
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000890 uint64_t BufferSizeBits = Stream.getBitcodeBytes().size() * CHAR_BIT;
Chris Lattnerca0ea542007-04-29 08:31:14 +0000891 // Print a summary of the read file.
Chris Lattner633ab162012-03-19 23:40:48 +0000892 outs() << "Summary of " << InputFilename << ":\n";
893 outs() << " Total size: ";
Chris Lattner5fab65d2007-05-01 02:43:46 +0000894 PrintSize(BufferSizeBits);
Chris Lattner633ab162012-03-19 23:40:48 +0000895 outs() << "\n";
896 outs() << " Stream type: ";
Chris Lattner03997582007-04-29 08:12:22 +0000897 switch (CurStreamType) {
Brian Gesiakb1358892018-04-21 23:52:04 +0000898 case UnknownBitstream:
899 outs() << "unknown\n";
900 break;
901 case LLVMIRBitstream:
902 outs() << "LLVM IR\n";
903 break;
904 case ClangSerializedASTBitstream:
905 outs() << "Clang Serialized AST\n";
906 break;
907 case ClangSerializedDiagnosticsBitstream:
908 outs() << "Clang Serialized Diagnostics\n";
909 break;
Chris Lattner03997582007-04-29 08:12:22 +0000910 }
Chris Lattner633ab162012-03-19 23:40:48 +0000911 outs() << " # Toplevel Blocks: " << NumTopBlocks << "\n";
912 outs() << "\n";
Chris Lattner1684cee2007-04-29 20:00:02 +0000913
914 // Emit per-block stats.
Chris Lattner633ab162012-03-19 23:40:48 +0000915 outs() << "Per-block Summary:\n";
Chris Lattner1684cee2007-04-29 20:00:02 +0000916 for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
917 E = BlockIDStats.end(); I != E; ++I) {
Chris Lattner633ab162012-03-19 23:40:48 +0000918 outs() << " Block ID #" << I->first;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000919 if (const char *BlockName =
920 GetBlockName(I->first, BlockInfo, CurStreamType))
Chris Lattner633ab162012-03-19 23:40:48 +0000921 outs() << " (" << BlockName << ")";
922 outs() << ":\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000923
Chris Lattner1684cee2007-04-29 20:00:02 +0000924 const PerBlockIDStats &Stats = I->second;
Chris Lattner633ab162012-03-19 23:40:48 +0000925 outs() << " Num Instances: " << Stats.NumInstances << "\n";
926 outs() << " Total Size: ";
Chris Lattner1684cee2007-04-29 20:00:02 +0000927 PrintSize(Stats.NumBits);
Chris Lattner633ab162012-03-19 23:40:48 +0000928 outs() << "\n";
Daniel Dunbare813b222009-09-25 16:04:21 +0000929 double pct = (Stats.NumBits * 100.0) / BufferSizeBits;
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000930 outs() << " Percent of file: " << format("%2.4f%%", pct) << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000931 if (Stats.NumInstances > 1) {
Chris Lattner633ab162012-03-19 23:40:48 +0000932 outs() << " Average Size: ";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000933 PrintSize(Stats.NumBits/(double)Stats.NumInstances);
Chris Lattner633ab162012-03-19 23:40:48 +0000934 outs() << "\n";
935 outs() << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000936 << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
Chris Lattner633ab162012-03-19 23:40:48 +0000937 outs() << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000938 << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
Chris Lattner633ab162012-03-19 23:40:48 +0000939 outs() << " Tot/Avg Records: " << Stats.NumRecords << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000940 << Stats.NumRecords/(double)Stats.NumInstances << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000941 } else {
Chris Lattner633ab162012-03-19 23:40:48 +0000942 outs() << " Num SubBlocks: " << Stats.NumSubBlocks << "\n";
943 outs() << " Num Abbrevs: " << Stats.NumAbbrevs << "\n";
944 outs() << " Num Records: " << Stats.NumRecords << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000945 }
Daniel Dunbare813b222009-09-25 16:04:21 +0000946 if (Stats.NumRecords) {
947 double pct = (Stats.NumAbbreviatedRecords * 100.0) / Stats.NumRecords;
Chris Lattner633ab162012-03-19 23:40:48 +0000948 outs() << " Percent Abbrevs: " << format("%2.4f%%", pct) << "\n";
Daniel Dunbare813b222009-09-25 16:04:21 +0000949 }
Chris Lattner633ab162012-03-19 23:40:48 +0000950 outs() << "\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000951
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000952 // Print a histogram of the codes we see.
953 if (!NoHistogram && !Stats.CodeFreq.empty()) {
Mehdi Aminid2638562015-10-21 06:10:55 +0000954 std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code>
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000955 for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
Mehdi Aminid2638562015-10-21 06:10:55 +0000956 if (unsigned Freq = Stats.CodeFreq[i].NumInstances)
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000957 FreqPairs.push_back(std::make_pair(Freq, i));
958 std::stable_sort(FreqPairs.begin(), FreqPairs.end());
959 std::reverse(FreqPairs.begin(), FreqPairs.end());
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000960
Chris Lattner633ab162012-03-19 23:40:48 +0000961 outs() << "\tRecord Histogram:\n";
Richard Smithdc1414b2016-02-06 00:46:09 +0000962 outs() << "\t\t Count # Bits b/Rec % Abv Record Kind\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000963 for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
Chris Lattner1cf80692009-04-27 18:15:27 +0000964 const PerRecordStats &RecStats = Stats.CodeFreq[FreqPairs[i].second];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000965
Mehdi Aminid2638562015-10-21 06:10:55 +0000966 outs() << format("\t\t%7d %9lu",
Jan Wen Voung05ff5702012-09-05 20:55:57 +0000967 RecStats.NumInstances,
968 (unsigned long)RecStats.TotalBits);
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000969
Richard Smithdc1414b2016-02-06 00:46:09 +0000970 if (RecStats.NumInstances > 1)
971 outs() << format(" %9.1f",
972 (double)RecStats.TotalBits/RecStats.NumInstances);
973 else
974 outs() << " ";
975
Chris Lattner1cf80692009-04-27 18:15:27 +0000976 if (RecStats.NumAbbrev)
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000977 outs() <<
Richard Smithdc1414b2016-02-06 00:46:09 +0000978 format(" %7.2f",
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000979 (double)RecStats.NumAbbrev/RecStats.NumInstances*100);
Chris Lattner1cf80692009-04-27 18:15:27 +0000980 else
Richard Smithdc1414b2016-02-06 00:46:09 +0000981 outs() << " ";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000982
Richard Smithdc1414b2016-02-06 00:46:09 +0000983 outs() << " ";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000984 if (const char *CodeName = GetCodeName(FreqPairs[i].second, I->first,
985 BlockInfo, CurStreamType))
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000986 outs() << CodeName << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000987 else
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000988 outs() << "UnknownCode" << FreqPairs[i].second << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000989 }
Chris Lattner633ab162012-03-19 23:40:48 +0000990 outs() << "\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000991
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000992 }
Chris Lattner1684cee2007-04-29 20:00:02 +0000993 }
Chris Lattner03997582007-04-29 08:12:22 +0000994 return 0;
995}
Reid Spencerdb5c86d2004-06-07 17:53:43 +0000996
Chris Lattnerca0ea542007-04-29 08:31:14 +0000997
Chris Lattner76d46322006-12-06 01:18:01 +0000998int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000999 InitLLVM X(argc, argv);
Chris Lattnere3fc2d12009-03-06 05:34:10 +00001000 cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
Chris Lattner6d80e212007-05-06 09:29:57 +00001001 return AnalyzeBitcode();
Reid Spencerdb5c86d2004-06-07 17:53:43 +00001002}