blob: 4551718344c12d6df9a0b2fa4260bfdf5bdda199 [file] [log] [blame]
Gabor Greif0e535c3c2007-07-04 21:55:50 +00001//===-- llvm-bcanalyzer.cpp - Bitcode Analyzer --------------------------===//
Misha Brukman650ba8e2005-04-22 00:00:37 +00002//
Reid Spencerdb5c86d2004-06-07 17:53:43 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman650ba8e2005-04-22 00:00:37 +00007//
Reid Spencerdb5c86d2004-06-07 17:53:43 +00008//===----------------------------------------------------------------------===//
9//
Reid Spencerddc6fb12004-06-08 05:56:58 +000010// This tool may be invoked in the following manner:
Gabor Greif0e535c3c2007-07-04 21:55:50 +000011// llvm-bcanalyzer [options] - Read LLVM bitcode from stdin
12// llvm-bcanalyzer [options] x.bc - Read LLVM bitcode from the x.bc file
Reid Spencerdb5c86d2004-06-07 17:53:43 +000013//
Reid Spencerddc6fb12004-06-08 05:56:58 +000014// Options:
Reid Spencerb3a4e0b2004-06-10 18:38:44 +000015// --help - Output information about command line switches
Gabor Greif0e535c3c2007-07-04 21:55:50 +000016// --dump - Dump low-level bitcode structure in readable format
Reid Spencerddc6fb12004-06-08 05:56:58 +000017//
Gabor Greif0e535c3c2007-07-04 21:55:50 +000018// 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
Reid Spencerb3a4e0b2004-06-10 18:38:44 +000021// statistics about the contents of the file. By default this information is
Gabor Greif0e535c3c2007-07-04 21:55:50 +000022// detailed and contains information about individual bitcode blocks and the
Daniel Dunbar75359a7c2009-09-25 16:03:57 +000023// functions in the module.
Gabor Greif0e535c3c2007-07-04 21:55:50 +000024// The tool is also able to print a bitcode file in a straight forward text
Misha Brukman650ba8e2005-04-22 00:00:37 +000025// format that shows the containment and relationships of the information in
Gabor Greif0e535c3c2007-07-04 21:55:50 +000026// the bitcode file (-dump option).
Chris Lattnercc189892007-04-29 05:51:00 +000027//
Reid Spencerdb5c86d2004-06-07 17:53:43 +000028//===----------------------------------------------------------------------===//
29
Mehdi Aminid7ad2212016-04-01 05:33:11 +000030#include "llvm/ADT/StringExtras.h"
Teresa Johnsonad176792016-11-11 05:34:58 +000031#include "llvm/Bitcode/BitcodeReader.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000032#include "llvm/Bitcode/BitstreamReader.h"
Chris Lattner1684cee2007-04-29 20:00:02 +000033#include "llvm/Bitcode/LLVMBitCodes.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000034#include "llvm/Support/CommandLine.h"
Daniel Dunbare813b222009-09-25 16:04:21 +000035#include "llvm/Support/Format.h"
Rui Ueyama197194b2018-04-13 18:26:06 +000036#include "llvm/Support/InitLLVM.h"
Chris Lattner76d46322006-12-06 01:18:01 +000037#include "llvm/Support/ManagedStatic.h"
Chris Lattner03997582007-04-29 08:12:22 +000038#include "llvm/Support/MemoryBuffer.h"
Mehdi Aminid7ad2212016-04-01 05:33:11 +000039#include "llvm/Support/SHA1.h"
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +000040#include "llvm/Support/WithColor.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000041#include "llvm/Support/raw_ostream.h"
Reid Spencerdb5c86d2004-06-07 17:53:43 +000042using namespace llvm;
43
44static cl::opt<std::string>
Gabor Greif0e535c3c2007-07-04 21:55:50 +000045 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Reid Spencerdb5c86d2004-06-07 17:53:43 +000046
Gabor Greif0e535c3c2007-07-04 21:55:50 +000047static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace"));
Chris Lattnerca0ea542007-04-29 08:31:14 +000048
49//===----------------------------------------------------------------------===//
50// Bitcode specific analysis.
51//===----------------------------------------------------------------------===//
52
Chris Lattner4a7ac9f2007-05-05 01:46:49 +000053static cl::opt<bool> NoHistogram("disable-histogram",
54 cl::desc("Do not print per-code histogram"));
Chris Lattner03997582007-04-29 08:12:22 +000055
Chris Lattner3543caa2007-04-29 21:48:19 +000056static cl::opt<bool>
57NonSymbolic("non-symbolic",
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000058 cl::desc("Emit numeric info in dump even if"
Chris Lattner3543caa2007-04-29 21:48:19 +000059 " symbolic info is available"));
60
Jordan Rose88eb5342014-08-30 17:07:55 +000061static cl::opt<std::string>
62 BlockInfoFilename("block-info",
63 cl::desc("Use the BLOCK_INFO from the given file"));
64
Jordan Rose0fa38b82015-05-13 18:51:49 +000065static cl::opt<bool>
66 ShowBinaryBlobs("show-binary-blobs",
67 cl::desc("Print binary blobs using hex escapes"));
68
Peter Collingbournec8556152017-07-06 17:56:01 +000069static cl::opt<std::string> CheckHash(
70 "check-hash",
71 cl::desc("Check module hash using the argument as a string table"));
72
Dan Gohmanf749ad72010-12-09 20:35:40 +000073namespace {
74
75/// CurStreamTypeType - A type for CurStreamType
76enum CurStreamTypeType {
Chris Lattner03997582007-04-29 08:12:22 +000077 UnknownBitstream,
Brian Gesiakb1358892018-04-21 23:52:04 +000078 LLVMIRBitstream,
79 ClangSerializedASTBitstream,
80 ClangSerializedDiagnosticsBitstream,
Dan Gohmanf749ad72010-12-09 20:35:40 +000081};
82
83}
84
Chris Lattner1684cee2007-04-29 20:00:02 +000085/// GetBlockName - Return a symbolic block name if known, otherwise return
Chris Lattner3543caa2007-04-29 21:48:19 +000086/// null.
Chris Lattnera6fdf5a2009-04-26 22:21:57 +000087static const char *GetBlockName(unsigned BlockID,
Peter Collingbourne77c89b62016-11-08 04:17:11 +000088 const BitstreamBlockInfo &BlockInfo,
Jordan Rose88eb5342014-08-30 17:07:55 +000089 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +000090 // Standard blocks for all bitcode files.
91 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
92 if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
93 return "BLOCKINFO_BLOCK";
Craig Toppere6cb63e2014-04-25 04:24:47 +000094 return nullptr;
Chris Lattner9181ddf2007-05-05 00:17:42 +000095 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +000096
Chris Lattnera6fdf5a2009-04-26 22:21:57 +000097 // Check to see if we have a blockinfo record for this block, with a name.
Peter Collingbourne77c89b62016-11-08 04:17:11 +000098 if (const BitstreamBlockInfo::BlockInfo *Info =
99 BlockInfo.getBlockInfo(BlockID)) {
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000100 if (!Info->Name.empty())
101 return Info->Name.c_str();
102 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000103
104
Craig Toppere6cb63e2014-04-25 04:24:47 +0000105 if (CurStreamType != LLVMIRBitstream) return nullptr;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000106
Chris Lattner1684cee2007-04-29 20:00:02 +0000107 switch (BlockID) {
Sanjoy Das65c13322016-04-26 05:59:14 +0000108 default: return nullptr;
Sanjoy Das51df5fa2016-04-26 05:59:08 +0000109 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: return "OPERAND_BUNDLE_TAGS_BLOCK";
Sanjoy Das65c13322016-04-26 05:59:14 +0000110 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";
Mehdi Amini5d303282015-10-26 18:37:00 +0000116 case bitc::IDENTIFICATION_BLOCK_ID:
Sanjoy Das65c13322016-04-26 05:59:14 +0000117 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";
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000123 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
Sanjoy Das65c13322016-04-26 05:59:14 +0000124 return "GLOBALVAL_SUMMARY_BLOCK";
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000125 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
126 return "FULL_LTO_GLOBALVAL_SUMMARY_BLOCK";
Sanjoy Das65c13322016-04-26 05:59:14 +0000127 case bitc::MODULE_STRTAB_BLOCK_ID: return "MODULE_STRTAB_BLOCK";
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000128 case bitc::STRTAB_BLOCK_ID: return "STRTAB_BLOCK";
Peter Collingbourne92648c22017-06-27 23:50:11 +0000129 case bitc::SYMTAB_BLOCK_ID: return "SYMTAB_BLOCK";
Chris Lattner1684cee2007-04-29 20:00:02 +0000130 }
131}
132
Chris Lattner3543caa2007-04-29 21:48:19 +0000133/// GetCodeName - Return a symbolic code name if known, otherwise return
134/// null.
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000135static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000136 const BitstreamBlockInfo &BlockInfo,
Jordan Rose88eb5342014-08-30 17:07:55 +0000137 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +0000138 // Standard blocks for all bitcode files.
139 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
140 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
141 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000142 default: return nullptr;
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000143 case bitc::BLOCKINFO_CODE_SETBID: return "SETBID";
144 case bitc::BLOCKINFO_CODE_BLOCKNAME: return "BLOCKNAME";
145 case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME";
Chris Lattner9181ddf2007-05-05 00:17:42 +0000146 }
147 }
Craig Toppere6cb63e2014-04-25 04:24:47 +0000148 return nullptr;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000149 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000150
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000151 // Check to see if we have a blockinfo record for this record, with a name.
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000152 if (const BitstreamBlockInfo::BlockInfo *Info =
153 BlockInfo.getBlockInfo(BlockID)) {
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000154 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 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000158
159
Craig Toppere6cb63e2014-04-25 04:24:47 +0000160 if (CurStreamType != LLVMIRBitstream) return nullptr;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000161
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000162#define STRINGIFY_CODE(PREFIX, CODE) \
163 case bitc::PREFIX##_##CODE: \
164 return #CODE;
Chris Lattner3543caa2007-04-29 21:48:19 +0000165 switch (BlockID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000166 default: return nullptr;
Chris Lattner3543caa2007-04-29 21:48:19 +0000167 case bitc::MODULE_BLOCK_ID:
168 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000169 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000170 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)
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000179 STRINGIFY_CODE(MODULE_CODE, GCNAME)
Teresa Johnsonff642b92015-09-17 20:12:00 +0000180 STRINGIFY_CODE(MODULE_CODE, VSTOFFSET)
Duncan P. N. Exon Smith68f56242016-03-25 01:29:50 +0000181 STRINGIFY_CODE(MODULE_CODE, METADATA_VALUES_UNUSED)
Teresa Johnsone1164de2016-02-10 21:55:02 +0000182 STRINGIFY_CODE(MODULE_CODE, SOURCE_FILENAME)
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000183 STRINGIFY_CODE(MODULE_CODE, HASH)
Chris Lattner3543caa2007-04-29 21:48:19 +0000184 }
Mehdi Amini5d303282015-10-26 18:37:00 +0000185 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 }
Chris Lattner0b7c5122007-05-04 03:01:41 +0000192 case bitc::PARAMATTR_BLOCK_ID:
193 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000194 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000195 // FIXME: Should these be different?
Bill Wendlingd7e05d62013-02-10 23:17:10 +0000196 case bitc::PARAMATTR_CODE_ENTRY_OLD: return "ENTRY";
197 case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY";
Justin Bogner68b28d02016-03-15 22:37:25 +0000198 }
199 case bitc::PARAMATTR_GROUP_BLOCK_ID:
200 switch (CodeID) {
201 default: return nullptr;
Bill Wendlingd7e05d62013-02-10 23:17:10 +0000202 case bitc::PARAMATTR_GRP_CODE_ENTRY: return "ENTRY";
Chris Lattner0b7c5122007-05-04 03:01:41 +0000203 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000204 case bitc::TYPE_BLOCK_ID_NEW:
Chris Lattner3543caa2007-04-29 21:48:19 +0000205 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000206 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000207 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)
Chris Lattner3543caa2007-04-29 21:48:19 +0000225 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000226
Chris Lattner3543caa2007-04-29 21:48:19 +0000227 case bitc::CONSTANTS_BLOCK_ID:
228 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000229 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000230 STRINGIFY_CODE(CST_CODE, SETTYPE)
231 STRINGIFY_CODE(CST_CODE, 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)
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)
270 STRINGIFY_CODE(FUNC_CODE, INST_UNREACHABLE)
David Majnemer654e1302015-07-31 17:58:14 +0000271 STRINGIFY_CODE(FUNC_CODE, INST_CLEANUPRET)
272 STRINGIFY_CODE(FUNC_CODE, INST_CATCHRET)
273 STRINGIFY_CODE(FUNC_CODE, INST_CATCHPAD)
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000274 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)
Sanjoy Das51df5fa2016-04-26 05:59:08 +0000287 STRINGIFY_CODE(FUNC_CODE, OPERAND_BUNDLE)
Chris Lattner3543caa2007-04-29 21:48:19 +0000288 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000289 case bitc::VALUE_SYMTAB_BLOCK_ID:
290 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000291 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000292 STRINGIFY_CODE(VST_CODE, ENTRY)
293 STRINGIFY_CODE(VST_CODE, BBENTRY)
Teresa Johnsonff642b92015-09-17 20:12:00 +0000294 STRINGIFY_CODE(VST_CODE, FNENTRY)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000295 STRINGIFY_CODE(VST_CODE, COMBINED_ENTRY)
Teresa Johnson403a7872015-10-04 14:33:43 +0000296 }
297 case bitc::MODULE_STRTAB_BLOCK_ID:
298 switch (CodeID) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000299 default:
300 return nullptr;
301 STRINGIFY_CODE(MST_CODE, ENTRY)
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000302 STRINGIFY_CODE(MST_CODE, HASH)
Teresa Johnson403a7872015-10-04 14:33:43 +0000303 }
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000304 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000305 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
Teresa Johnson403a7872015-10-04 14:33:43 +0000306 switch (CodeID) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000307 default:
308 return nullptr;
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000309 STRINGIFY_CODE(FS, PERMODULE)
310 STRINGIFY_CODE(FS, PERMODULE_PROFILE)
Easwaran Ramanc73cec82018-01-25 19:27:17 +0000311 STRINGIFY_CODE(FS, PERMODULE_RELBF)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000312 STRINGIFY_CODE(FS, PERMODULE_GLOBALVAR_INIT_REFS)
313 STRINGIFY_CODE(FS, COMBINED)
314 STRINGIFY_CODE(FS, COMBINED_PROFILE)
315 STRINGIFY_CODE(FS, COMBINED_GLOBALVAR_INIT_REFS)
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000316 STRINGIFY_CODE(FS, ALIAS)
317 STRINGIFY_CODE(FS, COMBINED_ALIAS)
Mehdi Aminiae64eaf2016-04-23 23:38:17 +0000318 STRINGIFY_CODE(FS, COMBINED_ORIGINAL_NAME)
Mehdi Amini8fe69362016-04-24 03:18:11 +0000319 STRINGIFY_CODE(FS, VERSION)
Teresa Johnsonf3681012018-02-07 04:05:59 +0000320 STRINGIFY_CODE(FS, FLAGS)
Peter Collingbourne1b4137a72016-12-21 23:03:45 +0000321 STRINGIFY_CODE(FS, TYPE_TESTS)
Peter Collingbournebe9ffaa2017-02-10 22:29:38 +0000322 STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_VCALLS)
323 STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_VCALLS)
324 STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_CONST_VCALL)
325 STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_CONST_VCALL)
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000326 STRINGIFY_CODE(FS, VALUE_GUID)
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +0000327 STRINGIFY_CODE(FS, CFI_FUNCTION_DEFS)
328 STRINGIFY_CODE(FS, CFI_FUNCTION_DECLS)
Vitaly Buka44396fa2018-02-14 22:41:15 +0000329 STRINGIFY_CODE(FS, TYPE_ID)
Chris Lattner3543caa2007-04-29 21:48:19 +0000330 }
Devang Patelaf206b82009-09-18 19:26:43 +0000331 case bitc::METADATA_ATTACHMENT_ID:
332 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000333 default:return nullptr;
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000334 STRINGIFY_CODE(METADATA, ATTACHMENT)
Devang Patelaf206b82009-09-18 19:26:43 +0000335 }
Devang Patel7428d8a2009-07-22 17:43:22 +0000336 case bitc::METADATA_BLOCK_ID:
337 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000338 default:return nullptr;
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000339 STRINGIFY_CODE(METADATA, STRING_OLD)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000340 STRINGIFY_CODE(METADATA, VALUE)
Adrian Prantl36daf632017-01-03 19:17:49 +0000341 STRINGIFY_CODE(METADATA, NODE)
342 STRINGIFY_CODE(METADATA, NAME)
343 STRINGIFY_CODE(METADATA, DISTINCT_NODE)
344 STRINGIFY_CODE(METADATA, KIND) // Older bitcode has it in a MODULE_BLOCK
345 STRINGIFY_CODE(METADATA, LOCATION)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000346 STRINGIFY_CODE(METADATA, OLD_NODE)
347 STRINGIFY_CODE(METADATA, OLD_FN_NODE)
348 STRINGIFY_CODE(METADATA, NAMED_NODE)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000349 STRINGIFY_CODE(METADATA, GENERIC_DEBUG)
350 STRINGIFY_CODE(METADATA, SUBRANGE)
351 STRINGIFY_CODE(METADATA, ENUMERATOR)
352 STRINGIFY_CODE(METADATA, BASIC_TYPE)
353 STRINGIFY_CODE(METADATA, FILE)
354 STRINGIFY_CODE(METADATA, DERIVED_TYPE)
355 STRINGIFY_CODE(METADATA, COMPOSITE_TYPE)
356 STRINGIFY_CODE(METADATA, SUBROUTINE_TYPE)
357 STRINGIFY_CODE(METADATA, COMPILE_UNIT)
358 STRINGIFY_CODE(METADATA, SUBPROGRAM)
359 STRINGIFY_CODE(METADATA, LEXICAL_BLOCK)
360 STRINGIFY_CODE(METADATA, LEXICAL_BLOCK_FILE)
361 STRINGIFY_CODE(METADATA, NAMESPACE)
362 STRINGIFY_CODE(METADATA, TEMPLATE_TYPE)
363 STRINGIFY_CODE(METADATA, TEMPLATE_VALUE)
364 STRINGIFY_CODE(METADATA, GLOBAL_VAR)
365 STRINGIFY_CODE(METADATA, LOCAL_VAR)
366 STRINGIFY_CODE(METADATA, EXPRESSION)
367 STRINGIFY_CODE(METADATA, OBJC_PROPERTY)
368 STRINGIFY_CODE(METADATA, IMPORTED_ENTITY)
Adrian Prantla7ad09d2015-06-30 00:25:41 +0000369 STRINGIFY_CODE(METADATA, MODULE)
Adrian Prantl36daf632017-01-03 19:17:49 +0000370 STRINGIFY_CODE(METADATA, MACRO)
371 STRINGIFY_CODE(METADATA, MACRO_FILE)
372 STRINGIFY_CODE(METADATA, STRINGS)
373 STRINGIFY_CODE(METADATA, GLOBAL_DECL_ATTACHMENT)
374 STRINGIFY_CODE(METADATA, GLOBAL_VAR_EXPR)
Mehdi Aminie98f9252016-12-28 22:30:28 +0000375 STRINGIFY_CODE(METADATA, INDEX_OFFSET)
376 STRINGIFY_CODE(METADATA, INDEX)
Devang Patel7428d8a2009-07-22 17:43:22 +0000377 }
Teresa Johnson12545072015-11-15 02:00:09 +0000378 case bitc::METADATA_KIND_BLOCK_ID:
379 switch (CodeID) {
380 default:
381 return nullptr;
382 STRINGIFY_CODE(METADATA, KIND)
383 }
Chad Rosierdd4ffae2011-12-07 21:45:13 +0000384 case bitc::USELIST_BLOCK_ID:
385 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000386 default:return nullptr;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000387 case bitc::USELIST_CODE_DEFAULT: return "USELIST_CODE_DEFAULT";
388 case bitc::USELIST_CODE_BB: return "USELIST_CODE_BB";
Chad Rosierdd4ffae2011-12-07 21:45:13 +0000389 }
Sanjoy Das51df5fa2016-04-26 05:59:08 +0000390
391 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
392 switch(CodeID) {
393 default: return nullptr;
394 case bitc::OPERAND_BUNDLE_TAG: return "OPERAND_BUNDLE_TAG";
395 }
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000396 case bitc::STRTAB_BLOCK_ID:
397 switch(CodeID) {
398 default: return nullptr;
399 case bitc::STRTAB_BLOB: return "BLOB";
400 }
Peter Collingbourne92648c22017-06-27 23:50:11 +0000401 case bitc::SYMTAB_BLOCK_ID:
402 switch(CodeID) {
403 default: return nullptr;
404 case bitc::SYMTAB_BLOB: return "BLOB";
405 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000406 }
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000407#undef STRINGIFY_CODE
Chris Lattner3543caa2007-04-29 21:48:19 +0000408}
409
Chris Lattnerbf419a92009-04-27 17:59:34 +0000410struct PerRecordStats {
411 unsigned NumInstances;
Chris Lattner1cf80692009-04-27 18:15:27 +0000412 unsigned NumAbbrev;
413 uint64_t TotalBits;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000414
Mehdi Aminid2638562015-10-21 06:10:55 +0000415 PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
Chris Lattnerbf419a92009-04-27 17:59:34 +0000416};
Chris Lattner1684cee2007-04-29 20:00:02 +0000417
418struct PerBlockIDStats {
419 /// NumInstances - This the number of times this block ID has been seen.
420 unsigned NumInstances;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000421
Chris Lattner1684cee2007-04-29 20:00:02 +0000422 /// NumBits - The total size in bits of all of these blocks.
423 uint64_t NumBits;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000424
Chris Lattner1684cee2007-04-29 20:00:02 +0000425 /// NumSubBlocks - The total number of blocks these blocks contain.
426 unsigned NumSubBlocks;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000427
Chris Lattner1684cee2007-04-29 20:00:02 +0000428 /// NumAbbrevs - The total number of abbreviations.
429 unsigned NumAbbrevs;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000430
431 /// NumRecords - The total number of records these blocks contain, and the
Chris Lattner1684cee2007-04-29 20:00:02 +0000432 /// number that are abbreviated.
433 unsigned NumRecords, NumAbbreviatedRecords;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000434
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000435 /// CodeFreq - Keep track of the number of times we see each code.
Chris Lattnerbf419a92009-04-27 17:59:34 +0000436 std::vector<PerRecordStats> CodeFreq;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000437
Chris Lattner1684cee2007-04-29 20:00:02 +0000438 PerBlockIDStats()
439 : NumInstances(0), NumBits(0),
440 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
441};
442
443static std::map<unsigned, PerBlockIDStats> BlockIDStats;
444
445
446
Zachary Turner2ee505e2016-10-03 18:17:18 +0000447/// ReportError - All bitcode analysis errors go through this function, making this a
Chris Lattnerca0ea542007-04-29 08:31:14 +0000448/// good place to breakpoint if debugging.
Zachary Turner2ee505e2016-10-03 18:17:18 +0000449static bool ReportError(const Twine &Err) {
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000450 WithColor::error() << Err << "\n";
Chris Lattnerca0ea542007-04-29 08:31:14 +0000451 return true;
452}
453
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000454static bool decodeMetadataStringsBlob(StringRef Indent,
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000455 ArrayRef<uint64_t> Record,
456 StringRef Blob) {
457 if (Blob.empty())
458 return true;
459
460 if (Record.size() != 2)
461 return true;
462
463 unsigned NumStrings = Record[0];
464 unsigned StringsOffset = Record[1];
465 outs() << " num-strings = " << NumStrings << " {\n";
466
467 StringRef Lengths = Blob.slice(0, StringsOffset);
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000468 SimpleBitstreamCursor R(Lengths);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000469 StringRef Strings = Blob.drop_front(StringsOffset);
470 do {
471 if (R.AtEndOfStream())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000472 return ReportError("bad length");
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000473
474 unsigned Size = R.ReadVBR(6);
475 if (Strings.size() < Size)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000476 return ReportError("truncated chars");
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000477
478 outs() << Indent << " '";
479 outs().write_escaped(Strings.slice(0, Size), /*hex=*/true);
480 outs() << "'\n";
481 Strings = Strings.drop_front(Size);
482 } while (--NumStrings);
483
484 outs() << Indent << " }";
485 return false;
486}
487
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000488static bool decodeBlob(unsigned Code, unsigned BlockID, StringRef Indent,
489 ArrayRef<uint64_t> Record, StringRef Blob) {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000490 if (BlockID != bitc::METADATA_BLOCK_ID)
491 return true;
492 if (Code != bitc::METADATA_STRINGS)
493 return true;
494
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000495 return decodeMetadataStringsBlob(Indent, Record, Blob);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000496}
497
Chris Lattnerca0ea542007-04-29 08:31:14 +0000498/// ParseBlock - Read a block, updating statistics, etc.
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000499static bool ParseBlock(BitstreamCursor &Stream, BitstreamBlockInfo &BlockInfo,
500 unsigned BlockID, unsigned IndentLevel,
501 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +0000502 std::string Indent(IndentLevel*2, ' ');
Chris Lattner1684cee2007-04-29 20:00:02 +0000503 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
Chris Lattner3543caa2007-04-29 21:48:19 +0000504
Chris Lattner1684cee2007-04-29 20:00:02 +0000505 // Get the statistics for this BlockID.
506 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000507
Chris Lattner1684cee2007-04-29 20:00:02 +0000508 BlockStats.NumInstances++;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000509
Chris Lattner9181ddf2007-05-05 00:17:42 +0000510 // BLOCKINFO is a special part of the stream.
Richard Smithdc1414b2016-02-06 00:46:09 +0000511 bool DumpRecords = Dump;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000512 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Chris Lattner633ab162012-03-19 23:40:48 +0000513 if (Dump) outs() << Indent << "<BLOCKINFO_BLOCK/>\n";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000514 Optional<BitstreamBlockInfo> NewBlockInfo =
515 Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
516 if (!NewBlockInfo)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000517 return ReportError("Malformed BlockInfoBlock");
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000518 BlockInfo = std::move(*NewBlockInfo);
519 Stream.JumpToBit(BlockBitStart);
Richard Smithdc1414b2016-02-06 00:46:09 +0000520 // It's not really interesting to dump the contents of the blockinfo block.
521 DumpRecords = false;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000522 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000523
Chris Lattner3543caa2007-04-29 21:48:19 +0000524 unsigned NumWords = 0;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000525 if (Stream.EnterSubBlock(BlockID, &NumWords))
Zachary Turner2ee505e2016-10-03 18:17:18 +0000526 return ReportError("Malformed block record");
Chris Lattnerca0ea542007-04-29 08:31:14 +0000527
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000528 // Keep it for later, when we see a MODULE_HASH record
529 uint64_t BlockEntryPos = Stream.getCurrentByteNo();
530
Craig Toppere6cb63e2014-04-25 04:24:47 +0000531 const char *BlockName = nullptr;
Richard Smithdc1414b2016-02-06 00:46:09 +0000532 if (DumpRecords) {
Chris Lattner633ab162012-03-19 23:40:48 +0000533 outs() << Indent << "<";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000534 if ((BlockName = GetBlockName(BlockID, BlockInfo, CurStreamType)))
Chris Lattner633ab162012-03-19 23:40:48 +0000535 outs() << BlockName;
Chris Lattner3543caa2007-04-29 21:48:19 +0000536 else
Chris Lattner633ab162012-03-19 23:40:48 +0000537 outs() << "UnknownBlock" << BlockID;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000538
Chris Lattner3543caa2007-04-29 21:48:19 +0000539 if (NonSymbolic && BlockName)
Chris Lattner633ab162012-03-19 23:40:48 +0000540 outs() << " BlockID=" << BlockID;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000541
Chris Lattner633ab162012-03-19 23:40:48 +0000542 outs() << " NumWords=" << NumWords
Chris Lattner3fa323d2013-01-19 21:37:14 +0000543 << " BlockCodeSize=" << Stream.getAbbrevIDWidth() << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000544 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000545
Chris Lattnerca0ea542007-04-29 08:31:14 +0000546 SmallVector<uint64_t, 64> Record;
547
Mehdi Aminie98f9252016-12-28 22:30:28 +0000548 // Keep the offset to the metadata index if seen.
549 uint64_t MetadataIndexOffset = 0;
550
Chris Lattnerca0ea542007-04-29 08:31:14 +0000551 // Read all the records for this block.
552 while (1) {
553 if (Stream.AtEndOfStream())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000554 return ReportError("Premature end of bitstream");
Chris Lattnerca0ea542007-04-29 08:31:14 +0000555
Chris Lattner1cf80692009-04-27 18:15:27 +0000556 uint64_t RecordStartBit = Stream.GetCurrentBitNo();
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000557
Chris Lattner0271af82013-01-20 02:50:32 +0000558 BitstreamEntry Entry =
559 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
560
561 switch (Entry.Kind) {
562 case BitstreamEntry::Error:
Zachary Turner2ee505e2016-10-03 18:17:18 +0000563 return ReportError("malformed bitcode file");
Chris Lattner0271af82013-01-20 02:50:32 +0000564 case BitstreamEntry::EndBlock: {
Chris Lattner1684cee2007-04-29 20:00:02 +0000565 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
566 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
Richard Smithdc1414b2016-02-06 00:46:09 +0000567 if (DumpRecords) {
Chris Lattner633ab162012-03-19 23:40:48 +0000568 outs() << Indent << "</";
Chris Lattner3543caa2007-04-29 21:48:19 +0000569 if (BlockName)
Chris Lattner633ab162012-03-19 23:40:48 +0000570 outs() << BlockName << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000571 else
Chris Lattner633ab162012-03-19 23:40:48 +0000572 outs() << "UnknownBlock" << BlockID << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000573 }
Chris Lattnerca0ea542007-04-29 08:31:14 +0000574 return false;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000575 }
Chris Lattner0271af82013-01-20 02:50:32 +0000576
577 case BitstreamEntry::SubBlock: {
Chris Lattner9e808cd2007-05-05 01:29:31 +0000578 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000579 if (ParseBlock(Stream, BlockInfo, Entry.ID, IndentLevel + 1,
580 CurStreamType))
Chris Lattnerca0ea542007-04-29 08:31:14 +0000581 return true;
Chris Lattner1684cee2007-04-29 20:00:02 +0000582 ++BlockStats.NumSubBlocks;
Chris Lattner9e808cd2007-05-05 01:29:31 +0000583 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
Chris Lattner0271af82013-01-20 02:50:32 +0000584
Chris Lattner9e808cd2007-05-05 01:29:31 +0000585 // Don't include subblock sizes in the size of this block.
586 BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
Chris Lattner0271af82013-01-20 02:50:32 +0000587 continue;
588 }
589 case BitstreamEntry::Record:
590 // The interesting case.
Chris Lattnerca0ea542007-04-29 08:31:14 +0000591 break;
Chris Lattner9e808cd2007-05-05 01:29:31 +0000592 }
Chris Lattner0271af82013-01-20 02:50:32 +0000593
594 if (Entry.ID == bitc::DEFINE_ABBREV) {
Chris Lattnerca0ea542007-04-29 08:31:14 +0000595 Stream.ReadAbbrevRecord();
Chris Lattner1684cee2007-04-29 20:00:02 +0000596 ++BlockStats.NumAbbrevs;
Chris Lattner0271af82013-01-20 02:50:32 +0000597 continue;
598 }
599
600 Record.clear();
Chris Lattner2ed6a202009-04-06 22:44:40 +0000601
Chris Lattner0271af82013-01-20 02:50:32 +0000602 ++BlockStats.NumRecords;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000603
Chris Lattner0271af82013-01-20 02:50:32 +0000604 StringRef Blob;
Teresa Johnson0cff9352018-06-04 19:20:02 +0000605 uint64_t CurrentRecordPos = Stream.GetCurrentBitNo();
Chris Lattner0271af82013-01-20 02:50:32 +0000606 unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000607
Chris Lattner0271af82013-01-20 02:50:32 +0000608 // Increment the # occurrences of this code.
609 if (BlockStats.CodeFreq.size() <= Code)
610 BlockStats.CodeFreq.resize(Code+1);
611 BlockStats.CodeFreq[Code].NumInstances++;
612 BlockStats.CodeFreq[Code].TotalBits +=
613 Stream.GetCurrentBitNo()-RecordStartBit;
614 if (Entry.ID != bitc::UNABBREV_RECORD) {
615 BlockStats.CodeFreq[Code].NumAbbrev++;
616 ++BlockStats.NumAbbreviatedRecords;
617 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000618
Richard Smithdc1414b2016-02-06 00:46:09 +0000619 if (DumpRecords) {
Chris Lattner0271af82013-01-20 02:50:32 +0000620 outs() << Indent << " <";
621 if (const char *CodeName =
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000622 GetCodeName(Code, BlockID, BlockInfo, CurStreamType))
Chris Lattner0271af82013-01-20 02:50:32 +0000623 outs() << CodeName;
624 else
625 outs() << "UnknownCode" << Code;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000626 if (NonSymbolic && GetCodeName(Code, BlockID, BlockInfo, CurStreamType))
Chris Lattner0271af82013-01-20 02:50:32 +0000627 outs() << " codeid=" << Code;
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000628 const BitCodeAbbrev *Abbv = nullptr;
629 if (Entry.ID != bitc::UNABBREV_RECORD) {
630 Abbv = Stream.getAbbrev(Entry.ID);
Chris Lattner0271af82013-01-20 02:50:32 +0000631 outs() << " abbrevid=" << Entry.ID;
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000632 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000633
Chris Lattner0271af82013-01-20 02:50:32 +0000634 for (unsigned i = 0, e = Record.size(); i != e; ++i)
635 outs() << " op" << i << "=" << (int64_t)Record[i];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000636
Mehdi Aminie98f9252016-12-28 22:30:28 +0000637 // If we found a metadata index, let's verify that we had an offset before
638 // and validate its forward reference offset was correct!
639 if (BlockID == bitc::METADATA_BLOCK_ID) {
640 if (Code == bitc::METADATA_INDEX_OFFSET) {
Mehdi Amini5022bb72016-12-28 23:45:54 +0000641 if (Record.size() != 2)
642 outs() << "(Invalid record)";
643 else {
644 auto Offset = Record[0] + (Record[1] << 32);
645 MetadataIndexOffset = Stream.GetCurrentBitNo() + Offset;
646 }
Mehdi Aminie98f9252016-12-28 22:30:28 +0000647 }
648 if (Code == bitc::METADATA_INDEX) {
649 outs() << " (offset ";
650 if (MetadataIndexOffset == RecordStartBit)
651 outs() << "match)";
652 else
653 outs() << "mismatch: " << MetadataIndexOffset << " vs "
654 << RecordStartBit << ")";
655 }
656 }
657
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000658 // If we found a module hash, let's verify that it matches!
Peter Collingbournec8556152017-07-06 17:56:01 +0000659 if (BlockID == bitc::MODULE_BLOCK_ID && Code == bitc::MODULE_CODE_HASH &&
660 !CheckHash.empty()) {
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000661 if (Record.size() != 5)
662 outs() << " (invalid)";
663 else {
664 // Recompute the hash and compare it to the one in the bitcode
665 SHA1 Hasher;
666 StringRef Hash;
Peter Collingbournec8556152017-07-06 17:56:01 +0000667 Hasher.update(CheckHash);
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000668 {
Peter Collingbournecf2750a2016-12-01 05:47:58 +0000669 int BlockSize = (CurrentRecordPos / 8) - BlockEntryPos;
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000670 auto Ptr = Stream.getPointerToByte(BlockEntryPos, BlockSize);
671 Hasher.update(ArrayRef<uint8_t>(Ptr, BlockSize));
672 Hash = Hasher.result();
673 }
674 SmallString<20> RecordedHash;
675 RecordedHash.resize(20);
676 int Pos = 0;
677 for (auto &Val : Record) {
678 assert(!(Val >> 32) && "Unexpected high bits set");
679 RecordedHash[Pos++] = (Val >> 24) & 0xFF;
680 RecordedHash[Pos++] = (Val >> 16) & 0xFF;
681 RecordedHash[Pos++] = (Val >> 8) & 0xFF;
682 RecordedHash[Pos++] = (Val >> 0) & 0xFF;
683 }
684 if (Hash == RecordedHash)
685 outs() << " (match)";
686 else
687 outs() << " (!mismatch!)";
688 }
689 }
690
Chris Lattner0271af82013-01-20 02:50:32 +0000691 outs() << "/>";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000692
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000693 if (Abbv) {
694 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
695 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
696 if (!Op.isEncoding() || Op.getEncoding() != BitCodeAbbrevOp::Array)
697 continue;
698 assert(i + 2 == e && "Array op not second to last");
699 std::string Str;
700 bool ArrayIsPrintable = true;
701 for (unsigned j = i - 1, je = Record.size(); j != je; ++j) {
702 if (!isprint(static_cast<unsigned char>(Record[j]))) {
703 ArrayIsPrintable = false;
704 break;
705 }
706 Str += (char)Record[j];
707 }
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000708 if (ArrayIsPrintable)
709 outs() << " record string = '" << Str << "'";
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000710 break;
711 }
712 }
713
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000714 if (Blob.data() && decodeBlob(Code, BlockID, Indent, Record, Blob)) {
Chris Lattner0271af82013-01-20 02:50:32 +0000715 outs() << " blob data = ";
Jordan Rose0fa38b82015-05-13 18:51:49 +0000716 if (ShowBinaryBlobs) {
717 outs() << "'";
718 outs().write_escaped(Blob, /*hex=*/true) << "'";
719 } else {
720 bool BlobIsPrintable = true;
721 for (unsigned i = 0, e = Blob.size(); i != e; ++i)
722 if (!isprint(static_cast<unsigned char>(Blob[i]))) {
723 BlobIsPrintable = false;
724 break;
725 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000726
Jordan Rose0fa38b82015-05-13 18:51:49 +0000727 if (BlobIsPrintable)
728 outs() << "'" << Blob << "'";
729 else
730 outs() << "unprintable, " << Blob.size() << " bytes.";
731 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000732 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000733
Chris Lattner0271af82013-01-20 02:50:32 +0000734 outs() << "\n";
Chris Lattnerca0ea542007-04-29 08:31:14 +0000735 }
Peter Collingbournecf2750a2016-12-01 05:47:58 +0000736
737 // Make sure that we can skip the current record.
738 Stream.JumpToBit(CurrentRecordPos);
739 Stream.skipRecord(Entry.ID);
Chris Lattnerca0ea542007-04-29 08:31:14 +0000740 }
741}
742
Chris Lattner1684cee2007-04-29 20:00:02 +0000743static void PrintSize(double Bits) {
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000744 outs() << format("%.2f/%.2fB/%luW", Bits, Bits/8,(unsigned long)(Bits/32));
Chris Lattnerbf419a92009-04-27 17:59:34 +0000745}
746static void PrintSize(uint64_t Bits) {
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000747 outs() << format("%lub/%.2fB/%luW", (unsigned long)Bits,
748 (double)Bits/8, (unsigned long)(Bits/32));
Chris Lattner1684cee2007-04-29 20:00:02 +0000749}
750
Brian Gesiakb1358892018-04-21 23:52:04 +0000751static CurStreamTypeType ReadSignature(BitstreamCursor &Stream) {
752 char Signature[6];
753 Signature[0] = Stream.Read(8);
754 Signature[1] = Stream.Read(8);
755
756 // Autodetect the file contents, if it is one we know.
757 if (Signature[0] == 'C' && Signature[1] == 'P') {
758 Signature[2] = Stream.Read(8);
759 Signature[3] = Stream.Read(8);
760 if (Signature[2] == 'C' && Signature[3] == 'H')
761 return ClangSerializedASTBitstream;
762 } else if (Signature[0] == 'D' && Signature[1] == 'I') {
763 Signature[2] = Stream.Read(8);
764 Signature[3] = Stream.Read(8);
765 if (Signature[2] == 'A' && Signature[3] == 'G')
766 return ClangSerializedDiagnosticsBitstream;
767 } else {
768 Signature[2] = Stream.Read(4);
769 Signature[3] = Stream.Read(4);
770 Signature[4] = Stream.Read(4);
771 Signature[5] = Stream.Read(4);
772 if (Signature[0] == 'B' && Signature[1] == 'C' &&
773 Signature[2] == 0x0 && Signature[3] == 0xC &&
774 Signature[4] == 0xE && Signature[5] == 0xD)
775 return LLVMIRBitstream;
776 }
777 return UnknownBitstream;
778}
779
Jordan Rose88eb5342014-08-30 17:07:55 +0000780static bool openBitcodeFile(StringRef Path,
781 std::unique_ptr<MemoryBuffer> &MemBuf,
Jordan Rose88eb5342014-08-30 17:07:55 +0000782 BitstreamCursor &Stream,
783 CurStreamTypeType &CurStreamType) {
Chris Lattner03997582007-04-29 08:12:22 +0000784 // Read the input file.
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000785 ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr =
Jordan Rose88eb5342014-08-30 17:07:55 +0000786 MemoryBuffer::getFileOrSTDIN(Path);
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000787 if (std::error_code EC = MemBufOrErr.getError())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000788 return ReportError(Twine("ReportError reading '") + Path + "': " + EC.message());
Jordan Rose88eb5342014-08-30 17:07:55 +0000789 MemBuf = std::move(MemBufOrErr.get());
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000790
Jordan Rose88eb5342014-08-30 17:07:55 +0000791 if (MemBuf->getBufferSize() & 3)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000792 return ReportError("Bitcode stream should be a multiple of 4 bytes in length");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000793
Jordan Rose88eb5342014-08-30 17:07:55 +0000794 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart();
795 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize();
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000796
Chris Lattnerb9e07fd2009-04-06 20:54:32 +0000797 // If we have a wrapper header, parse it and ignore the non-bc file contents.
798 // The magic number is 0x0B17C0DE stored in little endian.
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000799 if (isBitcodeWrapper(BufPtr, EndBufPtr)) {
Mehdi Aminieed26932016-04-01 05:19:14 +0000800 if (MemBuf->getBufferSize() < BWH_HeaderSize)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000801 return ReportError("Invalid bitcode wrapper header");
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000802
803 if (Dump) {
804 unsigned Magic = support::endian::read32le(&BufPtr[BWH_MagicField]);
805 unsigned Version = support::endian::read32le(&BufPtr[BWH_VersionField]);
806 unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]);
807 unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]);
808 unsigned CPUType = support::endian::read32le(&BufPtr[BWH_CPUTypeField]);
809
810 outs() << "<BITCODE_WRAPPER_HEADER"
811 << " Magic=" << format_hex(Magic, 10)
812 << " Version=" << format_hex(Version, 10)
813 << " Offset=" << format_hex(Offset, 10)
814 << " Size=" << format_hex(Size, 10)
815 << " CPUType=" << format_hex(CPUType, 10) << "/>\n";
816 }
817
Derek Schuff8b2dcad2012-02-06 22:30:29 +0000818 if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr, true))
Zachary Turner2ee505e2016-10-03 18:17:18 +0000819 return ReportError("Invalid bitcode wrapper header");
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000820 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000821
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000822 Stream = BitstreamCursor(ArrayRef<uint8_t>(BufPtr, EndBufPtr));
Brian Gesiakb1358892018-04-21 23:52:04 +0000823 CurStreamType = ReadSignature(Stream);
Chris Lattner03997582007-04-29 08:12:22 +0000824
Jordan Rose88eb5342014-08-30 17:07:55 +0000825 return false;
826}
827
828/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
829static int AnalyzeBitcode() {
830 std::unique_ptr<MemoryBuffer> StreamBuffer;
Jordan Rose88eb5342014-08-30 17:07:55 +0000831 BitstreamCursor Stream;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000832 BitstreamBlockInfo BlockInfo;
Jordan Rose88eb5342014-08-30 17:07:55 +0000833 CurStreamTypeType CurStreamType;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000834 if (openBitcodeFile(InputFilename, StreamBuffer, Stream, CurStreamType))
Jordan Rose88eb5342014-08-30 17:07:55 +0000835 return true;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000836 Stream.setBlockInfo(&BlockInfo);
Jordan Rose88eb5342014-08-30 17:07:55 +0000837
838 // Read block info from BlockInfoFilename, if specified.
839 // The block info must be a top-level block.
840 if (!BlockInfoFilename.empty()) {
841 std::unique_ptr<MemoryBuffer> BlockInfoBuffer;
Jordan Rose88eb5342014-08-30 17:07:55 +0000842 BitstreamCursor BlockInfoCursor;
843 CurStreamTypeType BlockInfoStreamType;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000844 if (openBitcodeFile(BlockInfoFilename, BlockInfoBuffer, BlockInfoCursor,
845 BlockInfoStreamType))
Jordan Rose88eb5342014-08-30 17:07:55 +0000846 return true;
847
848 while (!BlockInfoCursor.AtEndOfStream()) {
849 unsigned Code = BlockInfoCursor.ReadCode();
850 if (Code != bitc::ENTER_SUBBLOCK)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000851 return ReportError("Invalid record at top-level in block info file");
Jordan Rose88eb5342014-08-30 17:07:55 +0000852
853 unsigned BlockID = BlockInfoCursor.ReadSubBlockID();
854 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000855 Optional<BitstreamBlockInfo> NewBlockInfo =
856 BlockInfoCursor.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
857 if (!NewBlockInfo)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000858 return ReportError("Malformed BlockInfoBlock in block info file");
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000859 BlockInfo = std::move(*NewBlockInfo);
Jordan Rose88eb5342014-08-30 17:07:55 +0000860 break;
861 }
862
863 BlockInfoCursor.SkipBlock();
864 }
Jordan Rose88eb5342014-08-30 17:07:55 +0000865 }
866
Chris Lattner1684cee2007-04-29 20:00:02 +0000867 unsigned NumTopBlocks = 0;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000868
Chris Lattnerca0ea542007-04-29 08:31:14 +0000869 // Parse the top-level structure. We only allow blocks at the top-level.
870 while (!Stream.AtEndOfStream()) {
871 unsigned Code = Stream.ReadCode();
872 if (Code != bitc::ENTER_SUBBLOCK)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000873 return ReportError("Invalid record at top-level");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000874
Chris Lattner0271af82013-01-20 02:50:32 +0000875 unsigned BlockID = Stream.ReadSubBlockID();
876
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000877 if (ParseBlock(Stream, BlockInfo, BlockID, 0, CurStreamType))
Chris Lattnerca0ea542007-04-29 08:31:14 +0000878 return true;
Chris Lattner1684cee2007-04-29 20:00:02 +0000879 ++NumTopBlocks;
Chris Lattnerca0ea542007-04-29 08:31:14 +0000880 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000881
Chris Lattner633ab162012-03-19 23:40:48 +0000882 if (Dump) outs() << "\n\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000883
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000884 uint64_t BufferSizeBits = Stream.getBitcodeBytes().size() * CHAR_BIT;
Chris Lattnerca0ea542007-04-29 08:31:14 +0000885 // Print a summary of the read file.
Chris Lattner633ab162012-03-19 23:40:48 +0000886 outs() << "Summary of " << InputFilename << ":\n";
887 outs() << " Total size: ";
Chris Lattner5fab65d2007-05-01 02:43:46 +0000888 PrintSize(BufferSizeBits);
Chris Lattner633ab162012-03-19 23:40:48 +0000889 outs() << "\n";
890 outs() << " Stream type: ";
Chris Lattner03997582007-04-29 08:12:22 +0000891 switch (CurStreamType) {
Brian Gesiakb1358892018-04-21 23:52:04 +0000892 case UnknownBitstream:
893 outs() << "unknown\n";
894 break;
895 case LLVMIRBitstream:
896 outs() << "LLVM IR\n";
897 break;
898 case ClangSerializedASTBitstream:
899 outs() << "Clang Serialized AST\n";
900 break;
901 case ClangSerializedDiagnosticsBitstream:
902 outs() << "Clang Serialized Diagnostics\n";
903 break;
Chris Lattner03997582007-04-29 08:12:22 +0000904 }
Chris Lattner633ab162012-03-19 23:40:48 +0000905 outs() << " # Toplevel Blocks: " << NumTopBlocks << "\n";
906 outs() << "\n";
Chris Lattner1684cee2007-04-29 20:00:02 +0000907
908 // Emit per-block stats.
Chris Lattner633ab162012-03-19 23:40:48 +0000909 outs() << "Per-block Summary:\n";
Chris Lattner1684cee2007-04-29 20:00:02 +0000910 for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
911 E = BlockIDStats.end(); I != E; ++I) {
Chris Lattner633ab162012-03-19 23:40:48 +0000912 outs() << " Block ID #" << I->first;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000913 if (const char *BlockName =
914 GetBlockName(I->first, BlockInfo, CurStreamType))
Chris Lattner633ab162012-03-19 23:40:48 +0000915 outs() << " (" << BlockName << ")";
916 outs() << ":\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000917
Chris Lattner1684cee2007-04-29 20:00:02 +0000918 const PerBlockIDStats &Stats = I->second;
Chris Lattner633ab162012-03-19 23:40:48 +0000919 outs() << " Num Instances: " << Stats.NumInstances << "\n";
920 outs() << " Total Size: ";
Chris Lattner1684cee2007-04-29 20:00:02 +0000921 PrintSize(Stats.NumBits);
Chris Lattner633ab162012-03-19 23:40:48 +0000922 outs() << "\n";
Daniel Dunbare813b222009-09-25 16:04:21 +0000923 double pct = (Stats.NumBits * 100.0) / BufferSizeBits;
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000924 outs() << " Percent of file: " << format("%2.4f%%", pct) << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000925 if (Stats.NumInstances > 1) {
Chris Lattner633ab162012-03-19 23:40:48 +0000926 outs() << " Average Size: ";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000927 PrintSize(Stats.NumBits/(double)Stats.NumInstances);
Chris Lattner633ab162012-03-19 23:40:48 +0000928 outs() << "\n";
929 outs() << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000930 << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
Chris Lattner633ab162012-03-19 23:40:48 +0000931 outs() << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000932 << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
Chris Lattner633ab162012-03-19 23:40:48 +0000933 outs() << " Tot/Avg Records: " << Stats.NumRecords << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000934 << Stats.NumRecords/(double)Stats.NumInstances << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000935 } else {
Chris Lattner633ab162012-03-19 23:40:48 +0000936 outs() << " Num SubBlocks: " << Stats.NumSubBlocks << "\n";
937 outs() << " Num Abbrevs: " << Stats.NumAbbrevs << "\n";
938 outs() << " Num Records: " << Stats.NumRecords << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000939 }
Daniel Dunbare813b222009-09-25 16:04:21 +0000940 if (Stats.NumRecords) {
941 double pct = (Stats.NumAbbreviatedRecords * 100.0) / Stats.NumRecords;
Chris Lattner633ab162012-03-19 23:40:48 +0000942 outs() << " Percent Abbrevs: " << format("%2.4f%%", pct) << "\n";
Daniel Dunbare813b222009-09-25 16:04:21 +0000943 }
Chris Lattner633ab162012-03-19 23:40:48 +0000944 outs() << "\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000945
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000946 // Print a histogram of the codes we see.
947 if (!NoHistogram && !Stats.CodeFreq.empty()) {
Mehdi Aminid2638562015-10-21 06:10:55 +0000948 std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code>
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000949 for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
Mehdi Aminid2638562015-10-21 06:10:55 +0000950 if (unsigned Freq = Stats.CodeFreq[i].NumInstances)
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000951 FreqPairs.push_back(std::make_pair(Freq, i));
952 std::stable_sort(FreqPairs.begin(), FreqPairs.end());
953 std::reverse(FreqPairs.begin(), FreqPairs.end());
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000954
Chris Lattner633ab162012-03-19 23:40:48 +0000955 outs() << "\tRecord Histogram:\n";
Richard Smithdc1414b2016-02-06 00:46:09 +0000956 outs() << "\t\t Count # Bits b/Rec % Abv Record Kind\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000957 for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
Chris Lattner1cf80692009-04-27 18:15:27 +0000958 const PerRecordStats &RecStats = Stats.CodeFreq[FreqPairs[i].second];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000959
Mehdi Aminid2638562015-10-21 06:10:55 +0000960 outs() << format("\t\t%7d %9lu",
Jan Wen Voung05ff5702012-09-05 20:55:57 +0000961 RecStats.NumInstances,
962 (unsigned long)RecStats.TotalBits);
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000963
Richard Smithdc1414b2016-02-06 00:46:09 +0000964 if (RecStats.NumInstances > 1)
965 outs() << format(" %9.1f",
966 (double)RecStats.TotalBits/RecStats.NumInstances);
967 else
968 outs() << " ";
969
Chris Lattner1cf80692009-04-27 18:15:27 +0000970 if (RecStats.NumAbbrev)
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000971 outs() <<
Richard Smithdc1414b2016-02-06 00:46:09 +0000972 format(" %7.2f",
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000973 (double)RecStats.NumAbbrev/RecStats.NumInstances*100);
Chris Lattner1cf80692009-04-27 18:15:27 +0000974 else
Richard Smithdc1414b2016-02-06 00:46:09 +0000975 outs() << " ";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000976
Richard Smithdc1414b2016-02-06 00:46:09 +0000977 outs() << " ";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000978 if (const char *CodeName = GetCodeName(FreqPairs[i].second, I->first,
979 BlockInfo, CurStreamType))
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000980 outs() << CodeName << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000981 else
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000982 outs() << "UnknownCode" << FreqPairs[i].second << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000983 }
Chris Lattner633ab162012-03-19 23:40:48 +0000984 outs() << "\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000985
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000986 }
Chris Lattner1684cee2007-04-29 20:00:02 +0000987 }
Chris Lattner03997582007-04-29 08:12:22 +0000988 return 0;
989}
Reid Spencerdb5c86d2004-06-07 17:53:43 +0000990
Chris Lattnerca0ea542007-04-29 08:31:14 +0000991
Chris Lattner76d46322006-12-06 01:18:01 +0000992int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000993 InitLLVM X(argc, argv);
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000994 cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
Chris Lattner6d80e212007-05-06 09:29:57 +0000995 return AnalyzeBitcode();
Reid Spencerdb5c86d2004-06-07 17:53:43 +0000996}