blob: 1252ed5df8a8a02a1d51f1b9015dccc601668997 [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,
78 LLVMIRBitstream
Dan Gohmanf749ad72010-12-09 20:35:40 +000079};
80
81}
82
Chris Lattner1684cee2007-04-29 20:00:02 +000083/// GetBlockName - Return a symbolic block name if known, otherwise return
Chris Lattner3543caa2007-04-29 21:48:19 +000084/// null.
Chris Lattnera6fdf5a2009-04-26 22:21:57 +000085static const char *GetBlockName(unsigned BlockID,
Peter Collingbourne77c89b62016-11-08 04:17:11 +000086 const BitstreamBlockInfo &BlockInfo,
Jordan Rose88eb5342014-08-30 17:07:55 +000087 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +000088 // Standard blocks for all bitcode files.
89 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
90 if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
91 return "BLOCKINFO_BLOCK";
Craig Toppere6cb63e2014-04-25 04:24:47 +000092 return nullptr;
Chris Lattner9181ddf2007-05-05 00:17:42 +000093 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +000094
Chris Lattnera6fdf5a2009-04-26 22:21:57 +000095 // Check to see if we have a blockinfo record for this block, with a name.
Peter Collingbourne77c89b62016-11-08 04:17:11 +000096 if (const BitstreamBlockInfo::BlockInfo *Info =
97 BlockInfo.getBlockInfo(BlockID)) {
Chris Lattnera6fdf5a2009-04-26 22:21:57 +000098 if (!Info->Name.empty())
99 return Info->Name.c_str();
100 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000101
102
Craig Toppere6cb63e2014-04-25 04:24:47 +0000103 if (CurStreamType != LLVMIRBitstream) return nullptr;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000104
Chris Lattner1684cee2007-04-29 20:00:02 +0000105 switch (BlockID) {
Sanjoy Das65c13322016-04-26 05:59:14 +0000106 default: return nullptr;
Sanjoy Das51df5fa2016-04-26 05:59:08 +0000107 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: return "OPERAND_BUNDLE_TAGS_BLOCK";
Sanjoy Das65c13322016-04-26 05:59:14 +0000108 case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK";
109 case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK";
110 case bitc::PARAMATTR_GROUP_BLOCK_ID: return "PARAMATTR_GROUP_BLOCK_ID";
111 case bitc::TYPE_BLOCK_ID_NEW: return "TYPE_BLOCK_ID";
112 case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK";
113 case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK";
Mehdi Amini5d303282015-10-26 18:37:00 +0000114 case bitc::IDENTIFICATION_BLOCK_ID:
Sanjoy Das65c13322016-04-26 05:59:14 +0000115 return "IDENTIFICATION_BLOCK_ID";
116 case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB";
117 case bitc::METADATA_BLOCK_ID: return "METADATA_BLOCK";
118 case bitc::METADATA_KIND_BLOCK_ID: return "METADATA_KIND_BLOCK";
119 case bitc::METADATA_ATTACHMENT_ID: return "METADATA_ATTACHMENT_BLOCK";
120 case bitc::USELIST_BLOCK_ID: return "USELIST_BLOCK_ID";
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000121 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
Sanjoy Das65c13322016-04-26 05:59:14 +0000122 return "GLOBALVAL_SUMMARY_BLOCK";
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000123 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
124 return "FULL_LTO_GLOBALVAL_SUMMARY_BLOCK";
Sanjoy Das65c13322016-04-26 05:59:14 +0000125 case bitc::MODULE_STRTAB_BLOCK_ID: return "MODULE_STRTAB_BLOCK";
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000126 case bitc::STRTAB_BLOCK_ID: return "STRTAB_BLOCK";
Peter Collingbourne92648c22017-06-27 23:50:11 +0000127 case bitc::SYMTAB_BLOCK_ID: return "SYMTAB_BLOCK";
Chris Lattner1684cee2007-04-29 20:00:02 +0000128 }
129}
130
Chris Lattner3543caa2007-04-29 21:48:19 +0000131/// GetCodeName - Return a symbolic code name if known, otherwise return
132/// null.
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000133static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000134 const BitstreamBlockInfo &BlockInfo,
Jordan Rose88eb5342014-08-30 17:07:55 +0000135 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +0000136 // Standard blocks for all bitcode files.
137 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
138 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
139 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000140 default: return nullptr;
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000141 case bitc::BLOCKINFO_CODE_SETBID: return "SETBID";
142 case bitc::BLOCKINFO_CODE_BLOCKNAME: return "BLOCKNAME";
143 case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME";
Chris Lattner9181ddf2007-05-05 00:17:42 +0000144 }
145 }
Craig Toppere6cb63e2014-04-25 04:24:47 +0000146 return nullptr;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000147 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000148
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000149 // Check to see if we have a blockinfo record for this record, with a name.
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000150 if (const BitstreamBlockInfo::BlockInfo *Info =
151 BlockInfo.getBlockInfo(BlockID)) {
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000152 for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i)
153 if (Info->RecordNames[i].first == CodeID)
154 return Info->RecordNames[i].second.c_str();
155 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000156
157
Craig Toppere6cb63e2014-04-25 04:24:47 +0000158 if (CurStreamType != LLVMIRBitstream) return nullptr;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000159
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000160#define STRINGIFY_CODE(PREFIX, CODE) \
161 case bitc::PREFIX##_##CODE: \
162 return #CODE;
Chris Lattner3543caa2007-04-29 21:48:19 +0000163 switch (BlockID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000164 default: return nullptr;
Chris Lattner3543caa2007-04-29 21:48:19 +0000165 case bitc::MODULE_BLOCK_ID:
166 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000167 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000168 STRINGIFY_CODE(MODULE_CODE, VERSION)
169 STRINGIFY_CODE(MODULE_CODE, TRIPLE)
170 STRINGIFY_CODE(MODULE_CODE, DATALAYOUT)
171 STRINGIFY_CODE(MODULE_CODE, ASM)
172 STRINGIFY_CODE(MODULE_CODE, SECTIONNAME)
173 STRINGIFY_CODE(MODULE_CODE, DEPLIB) // FIXME: Remove in 4.0
174 STRINGIFY_CODE(MODULE_CODE, GLOBALVAR)
175 STRINGIFY_CODE(MODULE_CODE, FUNCTION)
176 STRINGIFY_CODE(MODULE_CODE, ALIAS)
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000177 STRINGIFY_CODE(MODULE_CODE, GCNAME)
Teresa Johnsonff642b92015-09-17 20:12:00 +0000178 STRINGIFY_CODE(MODULE_CODE, VSTOFFSET)
Duncan P. N. Exon Smith68f56242016-03-25 01:29:50 +0000179 STRINGIFY_CODE(MODULE_CODE, METADATA_VALUES_UNUSED)
Teresa Johnsone1164de2016-02-10 21:55:02 +0000180 STRINGIFY_CODE(MODULE_CODE, SOURCE_FILENAME)
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000181 STRINGIFY_CODE(MODULE_CODE, HASH)
Chris Lattner3543caa2007-04-29 21:48:19 +0000182 }
Mehdi Amini5d303282015-10-26 18:37:00 +0000183 case bitc::IDENTIFICATION_BLOCK_ID:
184 switch (CodeID) {
185 default:
186 return nullptr;
187 STRINGIFY_CODE(IDENTIFICATION_CODE, STRING)
188 STRINGIFY_CODE(IDENTIFICATION_CODE, EPOCH)
189 }
Chris Lattner0b7c5122007-05-04 03:01:41 +0000190 case bitc::PARAMATTR_BLOCK_ID:
191 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000192 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000193 // FIXME: Should these be different?
Bill Wendlingd7e05d62013-02-10 23:17:10 +0000194 case bitc::PARAMATTR_CODE_ENTRY_OLD: return "ENTRY";
195 case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY";
Justin Bogner68b28d02016-03-15 22:37:25 +0000196 }
197 case bitc::PARAMATTR_GROUP_BLOCK_ID:
198 switch (CodeID) {
199 default: return nullptr;
Bill Wendlingd7e05d62013-02-10 23:17:10 +0000200 case bitc::PARAMATTR_GRP_CODE_ENTRY: return "ENTRY";
Chris Lattner0b7c5122007-05-04 03:01:41 +0000201 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000202 case bitc::TYPE_BLOCK_ID_NEW:
Chris Lattner3543caa2007-04-29 21:48:19 +0000203 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000204 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000205 STRINGIFY_CODE(TYPE_CODE, NUMENTRY)
206 STRINGIFY_CODE(TYPE_CODE, VOID)
207 STRINGIFY_CODE(TYPE_CODE, FLOAT)
208 STRINGIFY_CODE(TYPE_CODE, DOUBLE)
209 STRINGIFY_CODE(TYPE_CODE, LABEL)
210 STRINGIFY_CODE(TYPE_CODE, OPAQUE)
211 STRINGIFY_CODE(TYPE_CODE, INTEGER)
212 STRINGIFY_CODE(TYPE_CODE, POINTER)
213 STRINGIFY_CODE(TYPE_CODE, ARRAY)
214 STRINGIFY_CODE(TYPE_CODE, VECTOR)
215 STRINGIFY_CODE(TYPE_CODE, X86_FP80)
216 STRINGIFY_CODE(TYPE_CODE, FP128)
217 STRINGIFY_CODE(TYPE_CODE, PPC_FP128)
218 STRINGIFY_CODE(TYPE_CODE, METADATA)
219 STRINGIFY_CODE(TYPE_CODE, STRUCT_ANON)
220 STRINGIFY_CODE(TYPE_CODE, STRUCT_NAME)
221 STRINGIFY_CODE(TYPE_CODE, STRUCT_NAMED)
222 STRINGIFY_CODE(TYPE_CODE, FUNCTION)
Chris Lattner3543caa2007-04-29 21:48:19 +0000223 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000224
Chris Lattner3543caa2007-04-29 21:48:19 +0000225 case bitc::CONSTANTS_BLOCK_ID:
226 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000227 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000228 STRINGIFY_CODE(CST_CODE, SETTYPE)
229 STRINGIFY_CODE(CST_CODE, NULL)
230 STRINGIFY_CODE(CST_CODE, UNDEF)
231 STRINGIFY_CODE(CST_CODE, INTEGER)
232 STRINGIFY_CODE(CST_CODE, WIDE_INTEGER)
233 STRINGIFY_CODE(CST_CODE, FLOAT)
234 STRINGIFY_CODE(CST_CODE, AGGREGATE)
235 STRINGIFY_CODE(CST_CODE, STRING)
236 STRINGIFY_CODE(CST_CODE, CSTRING)
237 STRINGIFY_CODE(CST_CODE, CE_BINOP)
238 STRINGIFY_CODE(CST_CODE, CE_CAST)
239 STRINGIFY_CODE(CST_CODE, CE_GEP)
240 STRINGIFY_CODE(CST_CODE, CE_INBOUNDS_GEP)
241 STRINGIFY_CODE(CST_CODE, CE_SELECT)
242 STRINGIFY_CODE(CST_CODE, CE_EXTRACTELT)
243 STRINGIFY_CODE(CST_CODE, CE_INSERTELT)
244 STRINGIFY_CODE(CST_CODE, CE_SHUFFLEVEC)
245 STRINGIFY_CODE(CST_CODE, CE_CMP)
246 STRINGIFY_CODE(CST_CODE, INLINEASM)
247 STRINGIFY_CODE(CST_CODE, CE_SHUFVEC_EX)
Chris Lattner372dd1e2012-01-30 00:51:16 +0000248 case bitc::CST_CODE_BLOCKADDRESS: return "CST_CODE_BLOCKADDRESS";
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000249 STRINGIFY_CODE(CST_CODE, DATA)
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000250 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000251 case bitc::FUNCTION_BLOCK_ID:
252 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000253 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000254 STRINGIFY_CODE(FUNC_CODE, DECLAREBLOCKS)
255 STRINGIFY_CODE(FUNC_CODE, INST_BINOP)
256 STRINGIFY_CODE(FUNC_CODE, INST_CAST)
257 STRINGIFY_CODE(FUNC_CODE, INST_GEP_OLD)
258 STRINGIFY_CODE(FUNC_CODE, INST_INBOUNDS_GEP_OLD)
259 STRINGIFY_CODE(FUNC_CODE, INST_SELECT)
260 STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTELT)
261 STRINGIFY_CODE(FUNC_CODE, INST_INSERTELT)
262 STRINGIFY_CODE(FUNC_CODE, INST_SHUFFLEVEC)
263 STRINGIFY_CODE(FUNC_CODE, INST_CMP)
264 STRINGIFY_CODE(FUNC_CODE, INST_RET)
265 STRINGIFY_CODE(FUNC_CODE, INST_BR)
266 STRINGIFY_CODE(FUNC_CODE, INST_SWITCH)
267 STRINGIFY_CODE(FUNC_CODE, INST_INVOKE)
268 STRINGIFY_CODE(FUNC_CODE, INST_UNREACHABLE)
David Majnemer654e1302015-07-31 17:58:14 +0000269 STRINGIFY_CODE(FUNC_CODE, INST_CLEANUPRET)
270 STRINGIFY_CODE(FUNC_CODE, INST_CATCHRET)
271 STRINGIFY_CODE(FUNC_CODE, INST_CATCHPAD)
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000272 STRINGIFY_CODE(FUNC_CODE, INST_PHI)
273 STRINGIFY_CODE(FUNC_CODE, INST_ALLOCA)
274 STRINGIFY_CODE(FUNC_CODE, INST_LOAD)
275 STRINGIFY_CODE(FUNC_CODE, INST_VAARG)
276 STRINGIFY_CODE(FUNC_CODE, INST_STORE)
277 STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTVAL)
278 STRINGIFY_CODE(FUNC_CODE, INST_INSERTVAL)
279 STRINGIFY_CODE(FUNC_CODE, INST_CMP2)
280 STRINGIFY_CODE(FUNC_CODE, INST_VSELECT)
281 STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC_AGAIN)
282 STRINGIFY_CODE(FUNC_CODE, INST_CALL)
283 STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC)
284 STRINGIFY_CODE(FUNC_CODE, INST_GEP)
Sanjoy Das51df5fa2016-04-26 05:59:08 +0000285 STRINGIFY_CODE(FUNC_CODE, OPERAND_BUNDLE)
Chris Lattner3543caa2007-04-29 21:48:19 +0000286 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000287 case bitc::VALUE_SYMTAB_BLOCK_ID:
288 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000289 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000290 STRINGIFY_CODE(VST_CODE, ENTRY)
291 STRINGIFY_CODE(VST_CODE, BBENTRY)
Teresa Johnsonff642b92015-09-17 20:12:00 +0000292 STRINGIFY_CODE(VST_CODE, FNENTRY)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000293 STRINGIFY_CODE(VST_CODE, COMBINED_ENTRY)
Teresa Johnson403a7872015-10-04 14:33:43 +0000294 }
295 case bitc::MODULE_STRTAB_BLOCK_ID:
296 switch (CodeID) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000297 default:
298 return nullptr;
299 STRINGIFY_CODE(MST_CODE, ENTRY)
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000300 STRINGIFY_CODE(MST_CODE, HASH)
Teresa Johnson403a7872015-10-04 14:33:43 +0000301 }
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000302 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000303 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
Teresa Johnson403a7872015-10-04 14:33:43 +0000304 switch (CodeID) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000305 default:
306 return nullptr;
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000307 STRINGIFY_CODE(FS, PERMODULE)
308 STRINGIFY_CODE(FS, PERMODULE_PROFILE)
Easwaran Ramanc73cec82018-01-25 19:27:17 +0000309 STRINGIFY_CODE(FS, PERMODULE_RELBF)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000310 STRINGIFY_CODE(FS, PERMODULE_GLOBALVAR_INIT_REFS)
311 STRINGIFY_CODE(FS, COMBINED)
312 STRINGIFY_CODE(FS, COMBINED_PROFILE)
313 STRINGIFY_CODE(FS, COMBINED_GLOBALVAR_INIT_REFS)
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000314 STRINGIFY_CODE(FS, ALIAS)
315 STRINGIFY_CODE(FS, COMBINED_ALIAS)
Mehdi Aminiae64eaf2016-04-23 23:38:17 +0000316 STRINGIFY_CODE(FS, COMBINED_ORIGINAL_NAME)
Mehdi Amini8fe69362016-04-24 03:18:11 +0000317 STRINGIFY_CODE(FS, VERSION)
Teresa Johnsonf3681012018-02-07 04:05:59 +0000318 STRINGIFY_CODE(FS, FLAGS)
Peter Collingbourne1b4137a72016-12-21 23:03:45 +0000319 STRINGIFY_CODE(FS, TYPE_TESTS)
Peter Collingbournebe9ffaa2017-02-10 22:29:38 +0000320 STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_VCALLS)
321 STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_VCALLS)
322 STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_CONST_VCALL)
323 STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_CONST_VCALL)
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000324 STRINGIFY_CODE(FS, VALUE_GUID)
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +0000325 STRINGIFY_CODE(FS, CFI_FUNCTION_DEFS)
326 STRINGIFY_CODE(FS, CFI_FUNCTION_DECLS)
Vitaly Buka44396fa2018-02-14 22:41:15 +0000327 STRINGIFY_CODE(FS, TYPE_ID)
Chris Lattner3543caa2007-04-29 21:48:19 +0000328 }
Devang Patelaf206b82009-09-18 19:26:43 +0000329 case bitc::METADATA_ATTACHMENT_ID:
330 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000331 default:return nullptr;
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000332 STRINGIFY_CODE(METADATA, ATTACHMENT)
Devang Patelaf206b82009-09-18 19:26:43 +0000333 }
Devang Patel7428d8a2009-07-22 17:43:22 +0000334 case bitc::METADATA_BLOCK_ID:
335 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000336 default:return nullptr;
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000337 STRINGIFY_CODE(METADATA, STRING_OLD)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000338 STRINGIFY_CODE(METADATA, VALUE)
Adrian Prantl36daf632017-01-03 19:17:49 +0000339 STRINGIFY_CODE(METADATA, NODE)
340 STRINGIFY_CODE(METADATA, NAME)
341 STRINGIFY_CODE(METADATA, DISTINCT_NODE)
342 STRINGIFY_CODE(METADATA, KIND) // Older bitcode has it in a MODULE_BLOCK
343 STRINGIFY_CODE(METADATA, LOCATION)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000344 STRINGIFY_CODE(METADATA, OLD_NODE)
345 STRINGIFY_CODE(METADATA, OLD_FN_NODE)
346 STRINGIFY_CODE(METADATA, NAMED_NODE)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000347 STRINGIFY_CODE(METADATA, GENERIC_DEBUG)
348 STRINGIFY_CODE(METADATA, SUBRANGE)
349 STRINGIFY_CODE(METADATA, ENUMERATOR)
350 STRINGIFY_CODE(METADATA, BASIC_TYPE)
351 STRINGIFY_CODE(METADATA, FILE)
352 STRINGIFY_CODE(METADATA, DERIVED_TYPE)
353 STRINGIFY_CODE(METADATA, COMPOSITE_TYPE)
354 STRINGIFY_CODE(METADATA, SUBROUTINE_TYPE)
355 STRINGIFY_CODE(METADATA, COMPILE_UNIT)
356 STRINGIFY_CODE(METADATA, SUBPROGRAM)
357 STRINGIFY_CODE(METADATA, LEXICAL_BLOCK)
358 STRINGIFY_CODE(METADATA, LEXICAL_BLOCK_FILE)
359 STRINGIFY_CODE(METADATA, NAMESPACE)
360 STRINGIFY_CODE(METADATA, TEMPLATE_TYPE)
361 STRINGIFY_CODE(METADATA, TEMPLATE_VALUE)
362 STRINGIFY_CODE(METADATA, GLOBAL_VAR)
363 STRINGIFY_CODE(METADATA, LOCAL_VAR)
364 STRINGIFY_CODE(METADATA, EXPRESSION)
365 STRINGIFY_CODE(METADATA, OBJC_PROPERTY)
366 STRINGIFY_CODE(METADATA, IMPORTED_ENTITY)
Adrian Prantla7ad09d2015-06-30 00:25:41 +0000367 STRINGIFY_CODE(METADATA, MODULE)
Adrian Prantl36daf632017-01-03 19:17:49 +0000368 STRINGIFY_CODE(METADATA, MACRO)
369 STRINGIFY_CODE(METADATA, MACRO_FILE)
370 STRINGIFY_CODE(METADATA, STRINGS)
371 STRINGIFY_CODE(METADATA, GLOBAL_DECL_ATTACHMENT)
372 STRINGIFY_CODE(METADATA, GLOBAL_VAR_EXPR)
Mehdi Aminie98f9252016-12-28 22:30:28 +0000373 STRINGIFY_CODE(METADATA, INDEX_OFFSET)
374 STRINGIFY_CODE(METADATA, INDEX)
Devang Patel7428d8a2009-07-22 17:43:22 +0000375 }
Teresa Johnson12545072015-11-15 02:00:09 +0000376 case bitc::METADATA_KIND_BLOCK_ID:
377 switch (CodeID) {
378 default:
379 return nullptr;
380 STRINGIFY_CODE(METADATA, KIND)
381 }
Chad Rosierdd4ffae2011-12-07 21:45:13 +0000382 case bitc::USELIST_BLOCK_ID:
383 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000384 default:return nullptr;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000385 case bitc::USELIST_CODE_DEFAULT: return "USELIST_CODE_DEFAULT";
386 case bitc::USELIST_CODE_BB: return "USELIST_CODE_BB";
Chad Rosierdd4ffae2011-12-07 21:45:13 +0000387 }
Sanjoy Das51df5fa2016-04-26 05:59:08 +0000388
389 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
390 switch(CodeID) {
391 default: return nullptr;
392 case bitc::OPERAND_BUNDLE_TAG: return "OPERAND_BUNDLE_TAG";
393 }
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000394 case bitc::STRTAB_BLOCK_ID:
395 switch(CodeID) {
396 default: return nullptr;
397 case bitc::STRTAB_BLOB: return "BLOB";
398 }
Peter Collingbourne92648c22017-06-27 23:50:11 +0000399 case bitc::SYMTAB_BLOCK_ID:
400 switch(CodeID) {
401 default: return nullptr;
402 case bitc::SYMTAB_BLOB: return "BLOB";
403 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000404 }
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000405#undef STRINGIFY_CODE
Chris Lattner3543caa2007-04-29 21:48:19 +0000406}
407
Chris Lattnerbf419a92009-04-27 17:59:34 +0000408struct PerRecordStats {
409 unsigned NumInstances;
Chris Lattner1cf80692009-04-27 18:15:27 +0000410 unsigned NumAbbrev;
411 uint64_t TotalBits;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000412
Mehdi Aminid2638562015-10-21 06:10:55 +0000413 PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
Chris Lattnerbf419a92009-04-27 17:59:34 +0000414};
Chris Lattner1684cee2007-04-29 20:00:02 +0000415
416struct PerBlockIDStats {
417 /// NumInstances - This the number of times this block ID has been seen.
418 unsigned NumInstances;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000419
Chris Lattner1684cee2007-04-29 20:00:02 +0000420 /// NumBits - The total size in bits of all of these blocks.
421 uint64_t NumBits;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000422
Chris Lattner1684cee2007-04-29 20:00:02 +0000423 /// NumSubBlocks - The total number of blocks these blocks contain.
424 unsigned NumSubBlocks;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000425
Chris Lattner1684cee2007-04-29 20:00:02 +0000426 /// NumAbbrevs - The total number of abbreviations.
427 unsigned NumAbbrevs;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000428
429 /// NumRecords - The total number of records these blocks contain, and the
Chris Lattner1684cee2007-04-29 20:00:02 +0000430 /// number that are abbreviated.
431 unsigned NumRecords, NumAbbreviatedRecords;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000432
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000433 /// CodeFreq - Keep track of the number of times we see each code.
Chris Lattnerbf419a92009-04-27 17:59:34 +0000434 std::vector<PerRecordStats> CodeFreq;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000435
Chris Lattner1684cee2007-04-29 20:00:02 +0000436 PerBlockIDStats()
437 : NumInstances(0), NumBits(0),
438 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
439};
440
441static std::map<unsigned, PerBlockIDStats> BlockIDStats;
442
443
444
Zachary Turner2ee505e2016-10-03 18:17:18 +0000445/// ReportError - All bitcode analysis errors go through this function, making this a
Chris Lattnerca0ea542007-04-29 08:31:14 +0000446/// good place to breakpoint if debugging.
Zachary Turner2ee505e2016-10-03 18:17:18 +0000447static bool ReportError(const Twine &Err) {
Jonas Devlieghere2cd41eb2018-04-21 21:11:59 +0000448 WithColor::error() << Err << "\n";
Chris Lattnerca0ea542007-04-29 08:31:14 +0000449 return true;
450}
451
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000452static bool decodeMetadataStringsBlob(StringRef Indent,
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000453 ArrayRef<uint64_t> Record,
454 StringRef Blob) {
455 if (Blob.empty())
456 return true;
457
458 if (Record.size() != 2)
459 return true;
460
461 unsigned NumStrings = Record[0];
462 unsigned StringsOffset = Record[1];
463 outs() << " num-strings = " << NumStrings << " {\n";
464
465 StringRef Lengths = Blob.slice(0, StringsOffset);
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000466 SimpleBitstreamCursor R(Lengths);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000467 StringRef Strings = Blob.drop_front(StringsOffset);
468 do {
469 if (R.AtEndOfStream())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000470 return ReportError("bad length");
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000471
472 unsigned Size = R.ReadVBR(6);
473 if (Strings.size() < Size)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000474 return ReportError("truncated chars");
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000475
476 outs() << Indent << " '";
477 outs().write_escaped(Strings.slice(0, Size), /*hex=*/true);
478 outs() << "'\n";
479 Strings = Strings.drop_front(Size);
480 } while (--NumStrings);
481
482 outs() << Indent << " }";
483 return false;
484}
485
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000486static bool decodeBlob(unsigned Code, unsigned BlockID, StringRef Indent,
487 ArrayRef<uint64_t> Record, StringRef Blob) {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000488 if (BlockID != bitc::METADATA_BLOCK_ID)
489 return true;
490 if (Code != bitc::METADATA_STRINGS)
491 return true;
492
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000493 return decodeMetadataStringsBlob(Indent, Record, Blob);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000494}
495
Chris Lattnerca0ea542007-04-29 08:31:14 +0000496/// ParseBlock - Read a block, updating statistics, etc.
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000497static bool ParseBlock(BitstreamCursor &Stream, BitstreamBlockInfo &BlockInfo,
498 unsigned BlockID, unsigned IndentLevel,
499 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +0000500 std::string Indent(IndentLevel*2, ' ');
Chris Lattner1684cee2007-04-29 20:00:02 +0000501 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
Chris Lattner3543caa2007-04-29 21:48:19 +0000502
Chris Lattner1684cee2007-04-29 20:00:02 +0000503 // Get the statistics for this BlockID.
504 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000505
Chris Lattner1684cee2007-04-29 20:00:02 +0000506 BlockStats.NumInstances++;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000507
Chris Lattner9181ddf2007-05-05 00:17:42 +0000508 // BLOCKINFO is a special part of the stream.
Richard Smithdc1414b2016-02-06 00:46:09 +0000509 bool DumpRecords = Dump;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000510 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Chris Lattner633ab162012-03-19 23:40:48 +0000511 if (Dump) outs() << Indent << "<BLOCKINFO_BLOCK/>\n";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000512 Optional<BitstreamBlockInfo> NewBlockInfo =
513 Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
514 if (!NewBlockInfo)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000515 return ReportError("Malformed BlockInfoBlock");
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000516 BlockInfo = std::move(*NewBlockInfo);
517 Stream.JumpToBit(BlockBitStart);
Richard Smithdc1414b2016-02-06 00:46:09 +0000518 // It's not really interesting to dump the contents of the blockinfo block.
519 DumpRecords = false;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000520 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000521
Chris Lattner3543caa2007-04-29 21:48:19 +0000522 unsigned NumWords = 0;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000523 if (Stream.EnterSubBlock(BlockID, &NumWords))
Zachary Turner2ee505e2016-10-03 18:17:18 +0000524 return ReportError("Malformed block record");
Chris Lattnerca0ea542007-04-29 08:31:14 +0000525
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000526 // Keep it for later, when we see a MODULE_HASH record
527 uint64_t BlockEntryPos = Stream.getCurrentByteNo();
528
Craig Toppere6cb63e2014-04-25 04:24:47 +0000529 const char *BlockName = nullptr;
Richard Smithdc1414b2016-02-06 00:46:09 +0000530 if (DumpRecords) {
Chris Lattner633ab162012-03-19 23:40:48 +0000531 outs() << Indent << "<";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000532 if ((BlockName = GetBlockName(BlockID, BlockInfo, CurStreamType)))
Chris Lattner633ab162012-03-19 23:40:48 +0000533 outs() << BlockName;
Chris Lattner3543caa2007-04-29 21:48:19 +0000534 else
Chris Lattner633ab162012-03-19 23:40:48 +0000535 outs() << "UnknownBlock" << BlockID;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000536
Chris Lattner3543caa2007-04-29 21:48:19 +0000537 if (NonSymbolic && BlockName)
Chris Lattner633ab162012-03-19 23:40:48 +0000538 outs() << " BlockID=" << BlockID;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000539
Chris Lattner633ab162012-03-19 23:40:48 +0000540 outs() << " NumWords=" << NumWords
Chris Lattner3fa323d2013-01-19 21:37:14 +0000541 << " BlockCodeSize=" << Stream.getAbbrevIDWidth() << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000542 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000543
Chris Lattnerca0ea542007-04-29 08:31:14 +0000544 SmallVector<uint64_t, 64> Record;
545
Mehdi Aminie98f9252016-12-28 22:30:28 +0000546 // Keep the offset to the metadata index if seen.
547 uint64_t MetadataIndexOffset = 0;
548
Chris Lattnerca0ea542007-04-29 08:31:14 +0000549 // Read all the records for this block.
550 while (1) {
551 if (Stream.AtEndOfStream())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000552 return ReportError("Premature end of bitstream");
Chris Lattnerca0ea542007-04-29 08:31:14 +0000553
Chris Lattner1cf80692009-04-27 18:15:27 +0000554 uint64_t RecordStartBit = Stream.GetCurrentBitNo();
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000555
Chris Lattner0271af82013-01-20 02:50:32 +0000556 BitstreamEntry Entry =
557 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
558
559 switch (Entry.Kind) {
560 case BitstreamEntry::Error:
Zachary Turner2ee505e2016-10-03 18:17:18 +0000561 return ReportError("malformed bitcode file");
Chris Lattner0271af82013-01-20 02:50:32 +0000562 case BitstreamEntry::EndBlock: {
Chris Lattner1684cee2007-04-29 20:00:02 +0000563 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
564 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
Richard Smithdc1414b2016-02-06 00:46:09 +0000565 if (DumpRecords) {
Chris Lattner633ab162012-03-19 23:40:48 +0000566 outs() << Indent << "</";
Chris Lattner3543caa2007-04-29 21:48:19 +0000567 if (BlockName)
Chris Lattner633ab162012-03-19 23:40:48 +0000568 outs() << BlockName << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000569 else
Chris Lattner633ab162012-03-19 23:40:48 +0000570 outs() << "UnknownBlock" << BlockID << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000571 }
Chris Lattnerca0ea542007-04-29 08:31:14 +0000572 return false;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000573 }
Chris Lattner0271af82013-01-20 02:50:32 +0000574
575 case BitstreamEntry::SubBlock: {
Chris Lattner9e808cd2007-05-05 01:29:31 +0000576 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000577 if (ParseBlock(Stream, BlockInfo, Entry.ID, IndentLevel + 1,
578 CurStreamType))
Chris Lattnerca0ea542007-04-29 08:31:14 +0000579 return true;
Chris Lattner1684cee2007-04-29 20:00:02 +0000580 ++BlockStats.NumSubBlocks;
Chris Lattner9e808cd2007-05-05 01:29:31 +0000581 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
Chris Lattner0271af82013-01-20 02:50:32 +0000582
Chris Lattner9e808cd2007-05-05 01:29:31 +0000583 // Don't include subblock sizes in the size of this block.
584 BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
Chris Lattner0271af82013-01-20 02:50:32 +0000585 continue;
586 }
587 case BitstreamEntry::Record:
588 // The interesting case.
Chris Lattnerca0ea542007-04-29 08:31:14 +0000589 break;
Chris Lattner9e808cd2007-05-05 01:29:31 +0000590 }
Chris Lattner0271af82013-01-20 02:50:32 +0000591
592 if (Entry.ID == bitc::DEFINE_ABBREV) {
Chris Lattnerca0ea542007-04-29 08:31:14 +0000593 Stream.ReadAbbrevRecord();
Chris Lattner1684cee2007-04-29 20:00:02 +0000594 ++BlockStats.NumAbbrevs;
Chris Lattner0271af82013-01-20 02:50:32 +0000595 continue;
596 }
597
598 Record.clear();
Chris Lattner2ed6a202009-04-06 22:44:40 +0000599
Chris Lattner0271af82013-01-20 02:50:32 +0000600 ++BlockStats.NumRecords;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000601
Chris Lattner0271af82013-01-20 02:50:32 +0000602 StringRef Blob;
Peter Collingbournecf2750a2016-12-01 05:47:58 +0000603 unsigned CurrentRecordPos = Stream.GetCurrentBitNo();
Chris Lattner0271af82013-01-20 02:50:32 +0000604 unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000605
Chris Lattner0271af82013-01-20 02:50:32 +0000606 // Increment the # occurrences of this code.
607 if (BlockStats.CodeFreq.size() <= Code)
608 BlockStats.CodeFreq.resize(Code+1);
609 BlockStats.CodeFreq[Code].NumInstances++;
610 BlockStats.CodeFreq[Code].TotalBits +=
611 Stream.GetCurrentBitNo()-RecordStartBit;
612 if (Entry.ID != bitc::UNABBREV_RECORD) {
613 BlockStats.CodeFreq[Code].NumAbbrev++;
614 ++BlockStats.NumAbbreviatedRecords;
615 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000616
Richard Smithdc1414b2016-02-06 00:46:09 +0000617 if (DumpRecords) {
Chris Lattner0271af82013-01-20 02:50:32 +0000618 outs() << Indent << " <";
619 if (const char *CodeName =
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000620 GetCodeName(Code, BlockID, BlockInfo, CurStreamType))
Chris Lattner0271af82013-01-20 02:50:32 +0000621 outs() << CodeName;
622 else
623 outs() << "UnknownCode" << Code;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000624 if (NonSymbolic && GetCodeName(Code, BlockID, BlockInfo, CurStreamType))
Chris Lattner0271af82013-01-20 02:50:32 +0000625 outs() << " codeid=" << Code;
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000626 const BitCodeAbbrev *Abbv = nullptr;
627 if (Entry.ID != bitc::UNABBREV_RECORD) {
628 Abbv = Stream.getAbbrev(Entry.ID);
Chris Lattner0271af82013-01-20 02:50:32 +0000629 outs() << " abbrevid=" << Entry.ID;
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000630 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000631
Chris Lattner0271af82013-01-20 02:50:32 +0000632 for (unsigned i = 0, e = Record.size(); i != e; ++i)
633 outs() << " op" << i << "=" << (int64_t)Record[i];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000634
Mehdi Aminie98f9252016-12-28 22:30:28 +0000635 // If we found a metadata index, let's verify that we had an offset before
636 // and validate its forward reference offset was correct!
637 if (BlockID == bitc::METADATA_BLOCK_ID) {
638 if (Code == bitc::METADATA_INDEX_OFFSET) {
Mehdi Amini5022bb72016-12-28 23:45:54 +0000639 if (Record.size() != 2)
640 outs() << "(Invalid record)";
641 else {
642 auto Offset = Record[0] + (Record[1] << 32);
643 MetadataIndexOffset = Stream.GetCurrentBitNo() + Offset;
644 }
Mehdi Aminie98f9252016-12-28 22:30:28 +0000645 }
646 if (Code == bitc::METADATA_INDEX) {
647 outs() << " (offset ";
648 if (MetadataIndexOffset == RecordStartBit)
649 outs() << "match)";
650 else
651 outs() << "mismatch: " << MetadataIndexOffset << " vs "
652 << RecordStartBit << ")";
653 }
654 }
655
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000656 // If we found a module hash, let's verify that it matches!
Peter Collingbournec8556152017-07-06 17:56:01 +0000657 if (BlockID == bitc::MODULE_BLOCK_ID && Code == bitc::MODULE_CODE_HASH &&
658 !CheckHash.empty()) {
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000659 if (Record.size() != 5)
660 outs() << " (invalid)";
661 else {
662 // Recompute the hash and compare it to the one in the bitcode
663 SHA1 Hasher;
664 StringRef Hash;
Peter Collingbournec8556152017-07-06 17:56:01 +0000665 Hasher.update(CheckHash);
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000666 {
Peter Collingbournecf2750a2016-12-01 05:47:58 +0000667 int BlockSize = (CurrentRecordPos / 8) - BlockEntryPos;
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000668 auto Ptr = Stream.getPointerToByte(BlockEntryPos, BlockSize);
669 Hasher.update(ArrayRef<uint8_t>(Ptr, BlockSize));
670 Hash = Hasher.result();
671 }
672 SmallString<20> RecordedHash;
673 RecordedHash.resize(20);
674 int Pos = 0;
675 for (auto &Val : Record) {
676 assert(!(Val >> 32) && "Unexpected high bits set");
677 RecordedHash[Pos++] = (Val >> 24) & 0xFF;
678 RecordedHash[Pos++] = (Val >> 16) & 0xFF;
679 RecordedHash[Pos++] = (Val >> 8) & 0xFF;
680 RecordedHash[Pos++] = (Val >> 0) & 0xFF;
681 }
682 if (Hash == RecordedHash)
683 outs() << " (match)";
684 else
685 outs() << " (!mismatch!)";
686 }
687 }
688
Chris Lattner0271af82013-01-20 02:50:32 +0000689 outs() << "/>";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000690
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000691 if (Abbv) {
692 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
693 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
694 if (!Op.isEncoding() || Op.getEncoding() != BitCodeAbbrevOp::Array)
695 continue;
696 assert(i + 2 == e && "Array op not second to last");
697 std::string Str;
698 bool ArrayIsPrintable = true;
699 for (unsigned j = i - 1, je = Record.size(); j != je; ++j) {
700 if (!isprint(static_cast<unsigned char>(Record[j]))) {
701 ArrayIsPrintable = false;
702 break;
703 }
704 Str += (char)Record[j];
705 }
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000706 if (ArrayIsPrintable)
707 outs() << " record string = '" << Str << "'";
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000708 break;
709 }
710 }
711
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000712 if (Blob.data() && decodeBlob(Code, BlockID, Indent, Record, Blob)) {
Chris Lattner0271af82013-01-20 02:50:32 +0000713 outs() << " blob data = ";
Jordan Rose0fa38b82015-05-13 18:51:49 +0000714 if (ShowBinaryBlobs) {
715 outs() << "'";
716 outs().write_escaped(Blob, /*hex=*/true) << "'";
717 } else {
718 bool BlobIsPrintable = true;
719 for (unsigned i = 0, e = Blob.size(); i != e; ++i)
720 if (!isprint(static_cast<unsigned char>(Blob[i]))) {
721 BlobIsPrintable = false;
722 break;
723 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000724
Jordan Rose0fa38b82015-05-13 18:51:49 +0000725 if (BlobIsPrintable)
726 outs() << "'" << Blob << "'";
727 else
728 outs() << "unprintable, " << Blob.size() << " bytes.";
729 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000730 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000731
Chris Lattner0271af82013-01-20 02:50:32 +0000732 outs() << "\n";
Chris Lattnerca0ea542007-04-29 08:31:14 +0000733 }
Peter Collingbournecf2750a2016-12-01 05:47:58 +0000734
735 // Make sure that we can skip the current record.
736 Stream.JumpToBit(CurrentRecordPos);
737 Stream.skipRecord(Entry.ID);
Chris Lattnerca0ea542007-04-29 08:31:14 +0000738 }
739}
740
Chris Lattner1684cee2007-04-29 20:00:02 +0000741static void PrintSize(double Bits) {
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000742 outs() << format("%.2f/%.2fB/%luW", Bits, Bits/8,(unsigned long)(Bits/32));
Chris Lattnerbf419a92009-04-27 17:59:34 +0000743}
744static void PrintSize(uint64_t Bits) {
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000745 outs() << format("%lub/%.2fB/%luW", (unsigned long)Bits,
746 (double)Bits/8, (unsigned long)(Bits/32));
Chris Lattner1684cee2007-04-29 20:00:02 +0000747}
748
Jordan Rose88eb5342014-08-30 17:07:55 +0000749static bool openBitcodeFile(StringRef Path,
750 std::unique_ptr<MemoryBuffer> &MemBuf,
Jordan Rose88eb5342014-08-30 17:07:55 +0000751 BitstreamCursor &Stream,
752 CurStreamTypeType &CurStreamType) {
Chris Lattner03997582007-04-29 08:12:22 +0000753 // Read the input file.
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000754 ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr =
Jordan Rose88eb5342014-08-30 17:07:55 +0000755 MemoryBuffer::getFileOrSTDIN(Path);
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000756 if (std::error_code EC = MemBufOrErr.getError())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000757 return ReportError(Twine("ReportError reading '") + Path + "': " + EC.message());
Jordan Rose88eb5342014-08-30 17:07:55 +0000758 MemBuf = std::move(MemBufOrErr.get());
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000759
Jordan Rose88eb5342014-08-30 17:07:55 +0000760 if (MemBuf->getBufferSize() & 3)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000761 return ReportError("Bitcode stream should be a multiple of 4 bytes in length");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000762
Jordan Rose88eb5342014-08-30 17:07:55 +0000763 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart();
764 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize();
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000765
Chris Lattnerb9e07fd2009-04-06 20:54:32 +0000766 // If we have a wrapper header, parse it and ignore the non-bc file contents.
767 // The magic number is 0x0B17C0DE stored in little endian.
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000768 if (isBitcodeWrapper(BufPtr, EndBufPtr)) {
Mehdi Aminieed26932016-04-01 05:19:14 +0000769 if (MemBuf->getBufferSize() < BWH_HeaderSize)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000770 return ReportError("Invalid bitcode wrapper header");
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000771
772 if (Dump) {
773 unsigned Magic = support::endian::read32le(&BufPtr[BWH_MagicField]);
774 unsigned Version = support::endian::read32le(&BufPtr[BWH_VersionField]);
775 unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]);
776 unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]);
777 unsigned CPUType = support::endian::read32le(&BufPtr[BWH_CPUTypeField]);
778
779 outs() << "<BITCODE_WRAPPER_HEADER"
780 << " Magic=" << format_hex(Magic, 10)
781 << " Version=" << format_hex(Version, 10)
782 << " Offset=" << format_hex(Offset, 10)
783 << " Size=" << format_hex(Size, 10)
784 << " CPUType=" << format_hex(CPUType, 10) << "/>\n";
785 }
786
Derek Schuff8b2dcad2012-02-06 22:30:29 +0000787 if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr, true))
Zachary Turner2ee505e2016-10-03 18:17:18 +0000788 return ReportError("Invalid bitcode wrapper header");
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000789 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000790
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000791 Stream = BitstreamCursor(ArrayRef<uint8_t>(BufPtr, EndBufPtr));
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000792
Chris Lattner03997582007-04-29 08:12:22 +0000793 // Read the stream signature.
794 char Signature[6];
795 Signature[0] = Stream.Read(8);
796 Signature[1] = Stream.Read(8);
797 Signature[2] = Stream.Read(4);
798 Signature[3] = Stream.Read(4);
799 Signature[4] = Stream.Read(4);
800 Signature[5] = Stream.Read(4);
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000801
Chris Lattnerca0ea542007-04-29 08:31:14 +0000802 // Autodetect the file contents, if it is one we know.
Chris Lattner03997582007-04-29 08:12:22 +0000803 CurStreamType = UnknownBitstream;
804 if (Signature[0] == 'B' && Signature[1] == 'C' &&
805 Signature[2] == 0x0 && Signature[3] == 0xC &&
806 Signature[4] == 0xE && Signature[5] == 0xD)
807 CurStreamType = LLVMIRBitstream;
808
Jordan Rose88eb5342014-08-30 17:07:55 +0000809 return false;
810}
811
812/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
813static int AnalyzeBitcode() {
814 std::unique_ptr<MemoryBuffer> StreamBuffer;
Jordan Rose88eb5342014-08-30 17:07:55 +0000815 BitstreamCursor Stream;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000816 BitstreamBlockInfo BlockInfo;
Jordan Rose88eb5342014-08-30 17:07:55 +0000817 CurStreamTypeType CurStreamType;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000818 if (openBitcodeFile(InputFilename, StreamBuffer, Stream, CurStreamType))
Jordan Rose88eb5342014-08-30 17:07:55 +0000819 return true;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000820 Stream.setBlockInfo(&BlockInfo);
Jordan Rose88eb5342014-08-30 17:07:55 +0000821
822 // Read block info from BlockInfoFilename, if specified.
823 // The block info must be a top-level block.
824 if (!BlockInfoFilename.empty()) {
825 std::unique_ptr<MemoryBuffer> BlockInfoBuffer;
Jordan Rose88eb5342014-08-30 17:07:55 +0000826 BitstreamCursor BlockInfoCursor;
827 CurStreamTypeType BlockInfoStreamType;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000828 if (openBitcodeFile(BlockInfoFilename, BlockInfoBuffer, BlockInfoCursor,
829 BlockInfoStreamType))
Jordan Rose88eb5342014-08-30 17:07:55 +0000830 return true;
831
832 while (!BlockInfoCursor.AtEndOfStream()) {
833 unsigned Code = BlockInfoCursor.ReadCode();
834 if (Code != bitc::ENTER_SUBBLOCK)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000835 return ReportError("Invalid record at top-level in block info file");
Jordan Rose88eb5342014-08-30 17:07:55 +0000836
837 unsigned BlockID = BlockInfoCursor.ReadSubBlockID();
838 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000839 Optional<BitstreamBlockInfo> NewBlockInfo =
840 BlockInfoCursor.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
841 if (!NewBlockInfo)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000842 return ReportError("Malformed BlockInfoBlock in block info file");
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000843 BlockInfo = std::move(*NewBlockInfo);
Jordan Rose88eb5342014-08-30 17:07:55 +0000844 break;
845 }
846
847 BlockInfoCursor.SkipBlock();
848 }
Jordan Rose88eb5342014-08-30 17:07:55 +0000849 }
850
Chris Lattner1684cee2007-04-29 20:00:02 +0000851 unsigned NumTopBlocks = 0;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000852
Chris Lattnerca0ea542007-04-29 08:31:14 +0000853 // Parse the top-level structure. We only allow blocks at the top-level.
854 while (!Stream.AtEndOfStream()) {
855 unsigned Code = Stream.ReadCode();
856 if (Code != bitc::ENTER_SUBBLOCK)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000857 return ReportError("Invalid record at top-level");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000858
Chris Lattner0271af82013-01-20 02:50:32 +0000859 unsigned BlockID = Stream.ReadSubBlockID();
860
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000861 if (ParseBlock(Stream, BlockInfo, BlockID, 0, CurStreamType))
Chris Lattnerca0ea542007-04-29 08:31:14 +0000862 return true;
Chris Lattner1684cee2007-04-29 20:00:02 +0000863 ++NumTopBlocks;
Chris Lattnerca0ea542007-04-29 08:31:14 +0000864 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000865
Chris Lattner633ab162012-03-19 23:40:48 +0000866 if (Dump) outs() << "\n\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000867
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000868 uint64_t BufferSizeBits = Stream.getBitcodeBytes().size() * CHAR_BIT;
Chris Lattnerca0ea542007-04-29 08:31:14 +0000869 // Print a summary of the read file.
Chris Lattner633ab162012-03-19 23:40:48 +0000870 outs() << "Summary of " << InputFilename << ":\n";
871 outs() << " Total size: ";
Chris Lattner5fab65d2007-05-01 02:43:46 +0000872 PrintSize(BufferSizeBits);
Chris Lattner633ab162012-03-19 23:40:48 +0000873 outs() << "\n";
874 outs() << " Stream type: ";
Chris Lattner03997582007-04-29 08:12:22 +0000875 switch (CurStreamType) {
Chris Lattner633ab162012-03-19 23:40:48 +0000876 case UnknownBitstream: outs() << "unknown\n"; break;
877 case LLVMIRBitstream: outs() << "LLVM IR\n"; break;
Chris Lattner03997582007-04-29 08:12:22 +0000878 }
Chris Lattner633ab162012-03-19 23:40:48 +0000879 outs() << " # Toplevel Blocks: " << NumTopBlocks << "\n";
880 outs() << "\n";
Chris Lattner1684cee2007-04-29 20:00:02 +0000881
882 // Emit per-block stats.
Chris Lattner633ab162012-03-19 23:40:48 +0000883 outs() << "Per-block Summary:\n";
Chris Lattner1684cee2007-04-29 20:00:02 +0000884 for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
885 E = BlockIDStats.end(); I != E; ++I) {
Chris Lattner633ab162012-03-19 23:40:48 +0000886 outs() << " Block ID #" << I->first;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000887 if (const char *BlockName =
888 GetBlockName(I->first, BlockInfo, CurStreamType))
Chris Lattner633ab162012-03-19 23:40:48 +0000889 outs() << " (" << BlockName << ")";
890 outs() << ":\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000891
Chris Lattner1684cee2007-04-29 20:00:02 +0000892 const PerBlockIDStats &Stats = I->second;
Chris Lattner633ab162012-03-19 23:40:48 +0000893 outs() << " Num Instances: " << Stats.NumInstances << "\n";
894 outs() << " Total Size: ";
Chris Lattner1684cee2007-04-29 20:00:02 +0000895 PrintSize(Stats.NumBits);
Chris Lattner633ab162012-03-19 23:40:48 +0000896 outs() << "\n";
Daniel Dunbare813b222009-09-25 16:04:21 +0000897 double pct = (Stats.NumBits * 100.0) / BufferSizeBits;
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000898 outs() << " Percent of file: " << format("%2.4f%%", pct) << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000899 if (Stats.NumInstances > 1) {
Chris Lattner633ab162012-03-19 23:40:48 +0000900 outs() << " Average Size: ";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000901 PrintSize(Stats.NumBits/(double)Stats.NumInstances);
Chris Lattner633ab162012-03-19 23:40:48 +0000902 outs() << "\n";
903 outs() << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000904 << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
Chris Lattner633ab162012-03-19 23:40:48 +0000905 outs() << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000906 << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
Chris Lattner633ab162012-03-19 23:40:48 +0000907 outs() << " Tot/Avg Records: " << Stats.NumRecords << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000908 << Stats.NumRecords/(double)Stats.NumInstances << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000909 } else {
Chris Lattner633ab162012-03-19 23:40:48 +0000910 outs() << " Num SubBlocks: " << Stats.NumSubBlocks << "\n";
911 outs() << " Num Abbrevs: " << Stats.NumAbbrevs << "\n";
912 outs() << " Num Records: " << Stats.NumRecords << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000913 }
Daniel Dunbare813b222009-09-25 16:04:21 +0000914 if (Stats.NumRecords) {
915 double pct = (Stats.NumAbbreviatedRecords * 100.0) / Stats.NumRecords;
Chris Lattner633ab162012-03-19 23:40:48 +0000916 outs() << " Percent Abbrevs: " << format("%2.4f%%", pct) << "\n";
Daniel Dunbare813b222009-09-25 16:04:21 +0000917 }
Chris Lattner633ab162012-03-19 23:40:48 +0000918 outs() << "\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000919
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000920 // Print a histogram of the codes we see.
921 if (!NoHistogram && !Stats.CodeFreq.empty()) {
Mehdi Aminid2638562015-10-21 06:10:55 +0000922 std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code>
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000923 for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
Mehdi Aminid2638562015-10-21 06:10:55 +0000924 if (unsigned Freq = Stats.CodeFreq[i].NumInstances)
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000925 FreqPairs.push_back(std::make_pair(Freq, i));
926 std::stable_sort(FreqPairs.begin(), FreqPairs.end());
927 std::reverse(FreqPairs.begin(), FreqPairs.end());
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000928
Chris Lattner633ab162012-03-19 23:40:48 +0000929 outs() << "\tRecord Histogram:\n";
Richard Smithdc1414b2016-02-06 00:46:09 +0000930 outs() << "\t\t Count # Bits b/Rec % Abv Record Kind\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000931 for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
Chris Lattner1cf80692009-04-27 18:15:27 +0000932 const PerRecordStats &RecStats = Stats.CodeFreq[FreqPairs[i].second];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000933
Mehdi Aminid2638562015-10-21 06:10:55 +0000934 outs() << format("\t\t%7d %9lu",
Jan Wen Voung05ff5702012-09-05 20:55:57 +0000935 RecStats.NumInstances,
936 (unsigned long)RecStats.TotalBits);
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000937
Richard Smithdc1414b2016-02-06 00:46:09 +0000938 if (RecStats.NumInstances > 1)
939 outs() << format(" %9.1f",
940 (double)RecStats.TotalBits/RecStats.NumInstances);
941 else
942 outs() << " ";
943
Chris Lattner1cf80692009-04-27 18:15:27 +0000944 if (RecStats.NumAbbrev)
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000945 outs() <<
Richard Smithdc1414b2016-02-06 00:46:09 +0000946 format(" %7.2f",
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000947 (double)RecStats.NumAbbrev/RecStats.NumInstances*100);
Chris Lattner1cf80692009-04-27 18:15:27 +0000948 else
Richard Smithdc1414b2016-02-06 00:46:09 +0000949 outs() << " ";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000950
Richard Smithdc1414b2016-02-06 00:46:09 +0000951 outs() << " ";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000952 if (const char *CodeName = GetCodeName(FreqPairs[i].second, I->first,
953 BlockInfo, CurStreamType))
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000954 outs() << CodeName << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000955 else
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000956 outs() << "UnknownCode" << FreqPairs[i].second << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000957 }
Chris Lattner633ab162012-03-19 23:40:48 +0000958 outs() << "\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000959
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000960 }
Chris Lattner1684cee2007-04-29 20:00:02 +0000961 }
Chris Lattner03997582007-04-29 08:12:22 +0000962 return 0;
963}
Reid Spencerdb5c86d2004-06-07 17:53:43 +0000964
Chris Lattnerca0ea542007-04-29 08:31:14 +0000965
Chris Lattner76d46322006-12-06 01:18:01 +0000966int main(int argc, char **argv) {
Rui Ueyama197194b2018-04-13 18:26:06 +0000967 InitLLVM X(argc, argv);
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000968 cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
Chris Lattner6d80e212007-05-06 09:29:57 +0000969 return AnalyzeBitcode();
Reid Spencerdb5c86d2004-06-07 17:53:43 +0000970}