blob: 528247c2dbc3b2d8a517d7a94b9d6d1534a9b13d [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"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000034#include "llvm/IR/Verifier.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000035#include "llvm/Support/CommandLine.h"
Daniel Dunbare813b222009-09-25 16:04:21 +000036#include "llvm/Support/Format.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"
Chris Lattnere3fc2d12009-03-06 05:34:10 +000039#include "llvm/Support/PrettyStackTrace.h"
Mehdi Aminid7ad2212016-04-01 05:33:11 +000040#include "llvm/Support/SHA1.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000041#include "llvm/Support/Signals.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000042#include "llvm/Support/raw_ostream.h"
Chris Lattner4a7ac9f2007-05-05 01:46:49 +000043#include <algorithm>
Will Dietz981af002013-10-12 00:55:57 +000044#include <cctype>
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000045#include <map>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000046#include <system_error>
Reid Spencerdb5c86d2004-06-07 17:53:43 +000047using namespace llvm;
48
49static cl::opt<std::string>
Gabor Greif0e535c3c2007-07-04 21:55:50 +000050 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Reid Spencerdb5c86d2004-06-07 17:53:43 +000051
Gabor Greif0e535c3c2007-07-04 21:55:50 +000052static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace"));
Chris Lattnerca0ea542007-04-29 08:31:14 +000053
54//===----------------------------------------------------------------------===//
55// Bitcode specific analysis.
56//===----------------------------------------------------------------------===//
57
Chris Lattner4a7ac9f2007-05-05 01:46:49 +000058static cl::opt<bool> NoHistogram("disable-histogram",
59 cl::desc("Do not print per-code histogram"));
Chris Lattner03997582007-04-29 08:12:22 +000060
Chris Lattner3543caa2007-04-29 21:48:19 +000061static cl::opt<bool>
62NonSymbolic("non-symbolic",
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000063 cl::desc("Emit numeric info in dump even if"
Chris Lattner3543caa2007-04-29 21:48:19 +000064 " symbolic info is available"));
65
Jordan Rose88eb5342014-08-30 17:07:55 +000066static cl::opt<std::string>
67 BlockInfoFilename("block-info",
68 cl::desc("Use the BLOCK_INFO from the given file"));
69
Jordan Rose0fa38b82015-05-13 18:51:49 +000070static cl::opt<bool>
71 ShowBinaryBlobs("show-binary-blobs",
72 cl::desc("Print binary blobs using hex escapes"));
73
Dan Gohmanf749ad72010-12-09 20:35:40 +000074namespace {
75
76/// CurStreamTypeType - A type for CurStreamType
77enum CurStreamTypeType {
Chris Lattner03997582007-04-29 08:12:22 +000078 UnknownBitstream,
79 LLVMIRBitstream
Dan Gohmanf749ad72010-12-09 20:35:40 +000080};
81
82}
83
Chris Lattner1684cee2007-04-29 20:00:02 +000084/// GetBlockName - Return a symbolic block name if known, otherwise return
Chris Lattner3543caa2007-04-29 21:48:19 +000085/// null.
Chris Lattnera6fdf5a2009-04-26 22:21:57 +000086static const char *GetBlockName(unsigned BlockID,
Peter Collingbourne77c89b62016-11-08 04:17:11 +000087 const BitstreamBlockInfo &BlockInfo,
Jordan Rose88eb5342014-08-30 17:07:55 +000088 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +000089 // Standard blocks for all bitcode files.
90 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
91 if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
92 return "BLOCKINFO_BLOCK";
Craig Toppere6cb63e2014-04-25 04:24:47 +000093 return nullptr;
Chris Lattner9181ddf2007-05-05 00:17:42 +000094 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +000095
Chris Lattnera6fdf5a2009-04-26 22:21:57 +000096 // Check to see if we have a blockinfo record for this block, with a name.
Peter Collingbourne77c89b62016-11-08 04:17:11 +000097 if (const BitstreamBlockInfo::BlockInfo *Info =
98 BlockInfo.getBlockInfo(BlockID)) {
Chris Lattnera6fdf5a2009-04-26 22:21:57 +000099 if (!Info->Name.empty())
100 return Info->Name.c_str();
101 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000102
103
Craig Toppere6cb63e2014-04-25 04:24:47 +0000104 if (CurStreamType != LLVMIRBitstream) return nullptr;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000105
Chris Lattner1684cee2007-04-29 20:00:02 +0000106 switch (BlockID) {
Sanjoy Das65c13322016-04-26 05:59:14 +0000107 default: return nullptr;
Sanjoy Das51df5fa2016-04-26 05:59:08 +0000108 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: return "OPERAND_BUNDLE_TAGS_BLOCK";
Sanjoy Das65c13322016-04-26 05:59:14 +0000109 case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK";
110 case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK";
111 case bitc::PARAMATTR_GROUP_BLOCK_ID: return "PARAMATTR_GROUP_BLOCK_ID";
112 case bitc::TYPE_BLOCK_ID_NEW: return "TYPE_BLOCK_ID";
113 case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK";
114 case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK";
Mehdi Amini5d303282015-10-26 18:37:00 +0000115 case bitc::IDENTIFICATION_BLOCK_ID:
Sanjoy Das65c13322016-04-26 05:59:14 +0000116 return "IDENTIFICATION_BLOCK_ID";
117 case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB";
118 case bitc::METADATA_BLOCK_ID: return "METADATA_BLOCK";
119 case bitc::METADATA_KIND_BLOCK_ID: return "METADATA_KIND_BLOCK";
120 case bitc::METADATA_ATTACHMENT_ID: return "METADATA_ATTACHMENT_BLOCK";
121 case bitc::USELIST_BLOCK_ID: return "USELIST_BLOCK_ID";
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000122 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
Sanjoy Das65c13322016-04-26 05:59:14 +0000123 return "GLOBALVAL_SUMMARY_BLOCK";
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000124 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
125 return "FULL_LTO_GLOBALVAL_SUMMARY_BLOCK";
Sanjoy Das65c13322016-04-26 05:59:14 +0000126 case bitc::MODULE_STRTAB_BLOCK_ID: return "MODULE_STRTAB_BLOCK";
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000127 case bitc::STRTAB_BLOCK_ID: return "STRTAB_BLOCK";
Peter Collingbourne92648c22017-06-27 23:50:11 +0000128 case bitc::SYMTAB_BLOCK_ID: return "SYMTAB_BLOCK";
Chris Lattner1684cee2007-04-29 20:00:02 +0000129 }
130}
131
Chris Lattner3543caa2007-04-29 21:48:19 +0000132/// GetCodeName - Return a symbolic code name if known, otherwise return
133/// null.
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000134static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000135 const BitstreamBlockInfo &BlockInfo,
Jordan Rose88eb5342014-08-30 17:07:55 +0000136 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +0000137 // Standard blocks for all bitcode files.
138 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
139 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
140 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000141 default: return nullptr;
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000142 case bitc::BLOCKINFO_CODE_SETBID: return "SETBID";
143 case bitc::BLOCKINFO_CODE_BLOCKNAME: return "BLOCKNAME";
144 case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME";
Chris Lattner9181ddf2007-05-05 00:17:42 +0000145 }
146 }
Craig Toppere6cb63e2014-04-25 04:24:47 +0000147 return nullptr;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000148 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000149
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000150 // Check to see if we have a blockinfo record for this record, with a name.
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000151 if (const BitstreamBlockInfo::BlockInfo *Info =
152 BlockInfo.getBlockInfo(BlockID)) {
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000153 for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i)
154 if (Info->RecordNames[i].first == CodeID)
155 return Info->RecordNames[i].second.c_str();
156 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000157
158
Craig Toppere6cb63e2014-04-25 04:24:47 +0000159 if (CurStreamType != LLVMIRBitstream) return nullptr;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000160
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000161#define STRINGIFY_CODE(PREFIX, CODE) \
162 case bitc::PREFIX##_##CODE: \
163 return #CODE;
Chris Lattner3543caa2007-04-29 21:48:19 +0000164 switch (BlockID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000165 default: return nullptr;
Chris Lattner3543caa2007-04-29 21:48:19 +0000166 case bitc::MODULE_BLOCK_ID:
167 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000168 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000169 STRINGIFY_CODE(MODULE_CODE, VERSION)
170 STRINGIFY_CODE(MODULE_CODE, TRIPLE)
171 STRINGIFY_CODE(MODULE_CODE, DATALAYOUT)
172 STRINGIFY_CODE(MODULE_CODE, ASM)
173 STRINGIFY_CODE(MODULE_CODE, SECTIONNAME)
174 STRINGIFY_CODE(MODULE_CODE, DEPLIB) // FIXME: Remove in 4.0
175 STRINGIFY_CODE(MODULE_CODE, GLOBALVAR)
176 STRINGIFY_CODE(MODULE_CODE, FUNCTION)
177 STRINGIFY_CODE(MODULE_CODE, ALIAS)
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000178 STRINGIFY_CODE(MODULE_CODE, GCNAME)
Teresa Johnsonff642b92015-09-17 20:12:00 +0000179 STRINGIFY_CODE(MODULE_CODE, VSTOFFSET)
Duncan P. N. Exon Smith68f56242016-03-25 01:29:50 +0000180 STRINGIFY_CODE(MODULE_CODE, METADATA_VALUES_UNUSED)
Teresa Johnsone1164de2016-02-10 21:55:02 +0000181 STRINGIFY_CODE(MODULE_CODE, SOURCE_FILENAME)
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000182 STRINGIFY_CODE(MODULE_CODE, HASH)
Chris Lattner3543caa2007-04-29 21:48:19 +0000183 }
Mehdi Amini5d303282015-10-26 18:37:00 +0000184 case bitc::IDENTIFICATION_BLOCK_ID:
185 switch (CodeID) {
186 default:
187 return nullptr;
188 STRINGIFY_CODE(IDENTIFICATION_CODE, STRING)
189 STRINGIFY_CODE(IDENTIFICATION_CODE, EPOCH)
190 }
Chris Lattner0b7c5122007-05-04 03:01:41 +0000191 case bitc::PARAMATTR_BLOCK_ID:
192 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000193 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000194 // FIXME: Should these be different?
Bill Wendlingd7e05d62013-02-10 23:17:10 +0000195 case bitc::PARAMATTR_CODE_ENTRY_OLD: return "ENTRY";
196 case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY";
Justin Bogner68b28d02016-03-15 22:37:25 +0000197 }
198 case bitc::PARAMATTR_GROUP_BLOCK_ID:
199 switch (CodeID) {
200 default: return nullptr;
Bill Wendlingd7e05d62013-02-10 23:17:10 +0000201 case bitc::PARAMATTR_GRP_CODE_ENTRY: return "ENTRY";
Chris Lattner0b7c5122007-05-04 03:01:41 +0000202 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000203 case bitc::TYPE_BLOCK_ID_NEW:
Chris Lattner3543caa2007-04-29 21:48:19 +0000204 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000205 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000206 STRINGIFY_CODE(TYPE_CODE, NUMENTRY)
207 STRINGIFY_CODE(TYPE_CODE, VOID)
208 STRINGIFY_CODE(TYPE_CODE, FLOAT)
209 STRINGIFY_CODE(TYPE_CODE, DOUBLE)
210 STRINGIFY_CODE(TYPE_CODE, LABEL)
211 STRINGIFY_CODE(TYPE_CODE, OPAQUE)
212 STRINGIFY_CODE(TYPE_CODE, INTEGER)
213 STRINGIFY_CODE(TYPE_CODE, POINTER)
214 STRINGIFY_CODE(TYPE_CODE, ARRAY)
215 STRINGIFY_CODE(TYPE_CODE, VECTOR)
216 STRINGIFY_CODE(TYPE_CODE, X86_FP80)
217 STRINGIFY_CODE(TYPE_CODE, FP128)
218 STRINGIFY_CODE(TYPE_CODE, PPC_FP128)
219 STRINGIFY_CODE(TYPE_CODE, METADATA)
220 STRINGIFY_CODE(TYPE_CODE, STRUCT_ANON)
221 STRINGIFY_CODE(TYPE_CODE, STRUCT_NAME)
222 STRINGIFY_CODE(TYPE_CODE, STRUCT_NAMED)
223 STRINGIFY_CODE(TYPE_CODE, FUNCTION)
Chris Lattner3543caa2007-04-29 21:48:19 +0000224 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000225
Chris Lattner3543caa2007-04-29 21:48:19 +0000226 case bitc::CONSTANTS_BLOCK_ID:
227 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000228 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000229 STRINGIFY_CODE(CST_CODE, SETTYPE)
230 STRINGIFY_CODE(CST_CODE, NULL)
231 STRINGIFY_CODE(CST_CODE, UNDEF)
232 STRINGIFY_CODE(CST_CODE, INTEGER)
233 STRINGIFY_CODE(CST_CODE, WIDE_INTEGER)
234 STRINGIFY_CODE(CST_CODE, FLOAT)
235 STRINGIFY_CODE(CST_CODE, AGGREGATE)
236 STRINGIFY_CODE(CST_CODE, STRING)
237 STRINGIFY_CODE(CST_CODE, CSTRING)
238 STRINGIFY_CODE(CST_CODE, CE_BINOP)
239 STRINGIFY_CODE(CST_CODE, CE_CAST)
240 STRINGIFY_CODE(CST_CODE, CE_GEP)
241 STRINGIFY_CODE(CST_CODE, CE_INBOUNDS_GEP)
242 STRINGIFY_CODE(CST_CODE, CE_SELECT)
243 STRINGIFY_CODE(CST_CODE, CE_EXTRACTELT)
244 STRINGIFY_CODE(CST_CODE, CE_INSERTELT)
245 STRINGIFY_CODE(CST_CODE, CE_SHUFFLEVEC)
246 STRINGIFY_CODE(CST_CODE, CE_CMP)
247 STRINGIFY_CODE(CST_CODE, INLINEASM)
248 STRINGIFY_CODE(CST_CODE, CE_SHUFVEC_EX)
Chris Lattner372dd1e2012-01-30 00:51:16 +0000249 case bitc::CST_CODE_BLOCKADDRESS: return "CST_CODE_BLOCKADDRESS";
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000250 STRINGIFY_CODE(CST_CODE, DATA)
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000251 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000252 case bitc::FUNCTION_BLOCK_ID:
253 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000254 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000255 STRINGIFY_CODE(FUNC_CODE, DECLAREBLOCKS)
256 STRINGIFY_CODE(FUNC_CODE, INST_BINOP)
257 STRINGIFY_CODE(FUNC_CODE, INST_CAST)
258 STRINGIFY_CODE(FUNC_CODE, INST_GEP_OLD)
259 STRINGIFY_CODE(FUNC_CODE, INST_INBOUNDS_GEP_OLD)
260 STRINGIFY_CODE(FUNC_CODE, INST_SELECT)
261 STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTELT)
262 STRINGIFY_CODE(FUNC_CODE, INST_INSERTELT)
263 STRINGIFY_CODE(FUNC_CODE, INST_SHUFFLEVEC)
264 STRINGIFY_CODE(FUNC_CODE, INST_CMP)
265 STRINGIFY_CODE(FUNC_CODE, INST_RET)
266 STRINGIFY_CODE(FUNC_CODE, INST_BR)
267 STRINGIFY_CODE(FUNC_CODE, INST_SWITCH)
268 STRINGIFY_CODE(FUNC_CODE, INST_INVOKE)
269 STRINGIFY_CODE(FUNC_CODE, INST_UNREACHABLE)
David Majnemer654e1302015-07-31 17:58:14 +0000270 STRINGIFY_CODE(FUNC_CODE, INST_CLEANUPRET)
271 STRINGIFY_CODE(FUNC_CODE, INST_CATCHRET)
272 STRINGIFY_CODE(FUNC_CODE, INST_CATCHPAD)
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000273 STRINGIFY_CODE(FUNC_CODE, INST_PHI)
274 STRINGIFY_CODE(FUNC_CODE, INST_ALLOCA)
275 STRINGIFY_CODE(FUNC_CODE, INST_LOAD)
276 STRINGIFY_CODE(FUNC_CODE, INST_VAARG)
277 STRINGIFY_CODE(FUNC_CODE, INST_STORE)
278 STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTVAL)
279 STRINGIFY_CODE(FUNC_CODE, INST_INSERTVAL)
280 STRINGIFY_CODE(FUNC_CODE, INST_CMP2)
281 STRINGIFY_CODE(FUNC_CODE, INST_VSELECT)
282 STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC_AGAIN)
283 STRINGIFY_CODE(FUNC_CODE, INST_CALL)
284 STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC)
285 STRINGIFY_CODE(FUNC_CODE, INST_GEP)
Sanjoy Das51df5fa2016-04-26 05:59:08 +0000286 STRINGIFY_CODE(FUNC_CODE, OPERAND_BUNDLE)
Chris Lattner3543caa2007-04-29 21:48:19 +0000287 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000288 case bitc::VALUE_SYMTAB_BLOCK_ID:
289 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000290 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000291 STRINGIFY_CODE(VST_CODE, ENTRY)
292 STRINGIFY_CODE(VST_CODE, BBENTRY)
Teresa Johnsonff642b92015-09-17 20:12:00 +0000293 STRINGIFY_CODE(VST_CODE, FNENTRY)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000294 STRINGIFY_CODE(VST_CODE, COMBINED_ENTRY)
Teresa Johnson403a7872015-10-04 14:33:43 +0000295 }
296 case bitc::MODULE_STRTAB_BLOCK_ID:
297 switch (CodeID) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000298 default:
299 return nullptr;
300 STRINGIFY_CODE(MST_CODE, ENTRY)
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000301 STRINGIFY_CODE(MST_CODE, HASH)
Teresa Johnson403a7872015-10-04 14:33:43 +0000302 }
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000303 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
Peter Collingbournee357fbd2017-06-08 23:01:49 +0000304 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
Teresa Johnson403a7872015-10-04 14:33:43 +0000305 switch (CodeID) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000306 default:
307 return nullptr;
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000308 STRINGIFY_CODE(FS, PERMODULE)
309 STRINGIFY_CODE(FS, PERMODULE_PROFILE)
310 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)
Peter Collingbourne1b4137a72016-12-21 23:03:45 +0000318 STRINGIFY_CODE(FS, TYPE_TESTS)
Peter Collingbournebe9ffaa2017-02-10 22:29:38 +0000319 STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_VCALLS)
320 STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_VCALLS)
321 STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_CONST_VCALL)
322 STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_CONST_VCALL)
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000323 STRINGIFY_CODE(FS, VALUE_GUID)
Evgeniy Stepanov4d4ee932017-06-16 00:18:29 +0000324 STRINGIFY_CODE(FS, CFI_FUNCTION_DEFS)
325 STRINGIFY_CODE(FS, CFI_FUNCTION_DECLS)
Chris Lattner3543caa2007-04-29 21:48:19 +0000326 }
Devang Patelaf206b82009-09-18 19:26:43 +0000327 case bitc::METADATA_ATTACHMENT_ID:
328 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000329 default:return nullptr;
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000330 STRINGIFY_CODE(METADATA, ATTACHMENT)
Devang Patelaf206b82009-09-18 19:26:43 +0000331 }
Devang Patel7428d8a2009-07-22 17:43:22 +0000332 case bitc::METADATA_BLOCK_ID:
333 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000334 default:return nullptr;
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000335 STRINGIFY_CODE(METADATA, STRING_OLD)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000336 STRINGIFY_CODE(METADATA, VALUE)
Adrian Prantl36daf632017-01-03 19:17:49 +0000337 STRINGIFY_CODE(METADATA, NODE)
338 STRINGIFY_CODE(METADATA, NAME)
339 STRINGIFY_CODE(METADATA, DISTINCT_NODE)
340 STRINGIFY_CODE(METADATA, KIND) // Older bitcode has it in a MODULE_BLOCK
341 STRINGIFY_CODE(METADATA, LOCATION)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000342 STRINGIFY_CODE(METADATA, OLD_NODE)
343 STRINGIFY_CODE(METADATA, OLD_FN_NODE)
344 STRINGIFY_CODE(METADATA, NAMED_NODE)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000345 STRINGIFY_CODE(METADATA, GENERIC_DEBUG)
346 STRINGIFY_CODE(METADATA, SUBRANGE)
347 STRINGIFY_CODE(METADATA, ENUMERATOR)
348 STRINGIFY_CODE(METADATA, BASIC_TYPE)
349 STRINGIFY_CODE(METADATA, FILE)
350 STRINGIFY_CODE(METADATA, DERIVED_TYPE)
351 STRINGIFY_CODE(METADATA, COMPOSITE_TYPE)
352 STRINGIFY_CODE(METADATA, SUBROUTINE_TYPE)
353 STRINGIFY_CODE(METADATA, COMPILE_UNIT)
354 STRINGIFY_CODE(METADATA, SUBPROGRAM)
355 STRINGIFY_CODE(METADATA, LEXICAL_BLOCK)
356 STRINGIFY_CODE(METADATA, LEXICAL_BLOCK_FILE)
357 STRINGIFY_CODE(METADATA, NAMESPACE)
358 STRINGIFY_CODE(METADATA, TEMPLATE_TYPE)
359 STRINGIFY_CODE(METADATA, TEMPLATE_VALUE)
360 STRINGIFY_CODE(METADATA, GLOBAL_VAR)
361 STRINGIFY_CODE(METADATA, LOCAL_VAR)
362 STRINGIFY_CODE(METADATA, EXPRESSION)
363 STRINGIFY_CODE(METADATA, OBJC_PROPERTY)
364 STRINGIFY_CODE(METADATA, IMPORTED_ENTITY)
Adrian Prantla7ad09d2015-06-30 00:25:41 +0000365 STRINGIFY_CODE(METADATA, MODULE)
Adrian Prantl36daf632017-01-03 19:17:49 +0000366 STRINGIFY_CODE(METADATA, MACRO)
367 STRINGIFY_CODE(METADATA, MACRO_FILE)
368 STRINGIFY_CODE(METADATA, STRINGS)
369 STRINGIFY_CODE(METADATA, GLOBAL_DECL_ATTACHMENT)
370 STRINGIFY_CODE(METADATA, GLOBAL_VAR_EXPR)
Mehdi Aminie98f9252016-12-28 22:30:28 +0000371 STRINGIFY_CODE(METADATA, INDEX_OFFSET)
372 STRINGIFY_CODE(METADATA, INDEX)
Devang Patel7428d8a2009-07-22 17:43:22 +0000373 }
Teresa Johnson12545072015-11-15 02:00:09 +0000374 case bitc::METADATA_KIND_BLOCK_ID:
375 switch (CodeID) {
376 default:
377 return nullptr;
378 STRINGIFY_CODE(METADATA, KIND)
379 }
Chad Rosierdd4ffae2011-12-07 21:45:13 +0000380 case bitc::USELIST_BLOCK_ID:
381 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000382 default:return nullptr;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000383 case bitc::USELIST_CODE_DEFAULT: return "USELIST_CODE_DEFAULT";
384 case bitc::USELIST_CODE_BB: return "USELIST_CODE_BB";
Chad Rosierdd4ffae2011-12-07 21:45:13 +0000385 }
Sanjoy Das51df5fa2016-04-26 05:59:08 +0000386
387 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
388 switch(CodeID) {
389 default: return nullptr;
390 case bitc::OPERAND_BUNDLE_TAG: return "OPERAND_BUNDLE_TAG";
391 }
Peter Collingbournea0f371a2017-04-17 17:51:36 +0000392 case bitc::STRTAB_BLOCK_ID:
393 switch(CodeID) {
394 default: return nullptr;
395 case bitc::STRTAB_BLOB: return "BLOB";
396 }
Peter Collingbourne92648c22017-06-27 23:50:11 +0000397 case bitc::SYMTAB_BLOCK_ID:
398 switch(CodeID) {
399 default: return nullptr;
400 case bitc::SYMTAB_BLOB: return "BLOB";
401 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000402 }
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000403#undef STRINGIFY_CODE
Chris Lattner3543caa2007-04-29 21:48:19 +0000404}
405
Chris Lattnerbf419a92009-04-27 17:59:34 +0000406struct PerRecordStats {
407 unsigned NumInstances;
Chris Lattner1cf80692009-04-27 18:15:27 +0000408 unsigned NumAbbrev;
409 uint64_t TotalBits;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000410
Mehdi Aminid2638562015-10-21 06:10:55 +0000411 PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
Chris Lattnerbf419a92009-04-27 17:59:34 +0000412};
Chris Lattner1684cee2007-04-29 20:00:02 +0000413
414struct PerBlockIDStats {
415 /// NumInstances - This the number of times this block ID has been seen.
416 unsigned NumInstances;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000417
Chris Lattner1684cee2007-04-29 20:00:02 +0000418 /// NumBits - The total size in bits of all of these blocks.
419 uint64_t NumBits;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000420
Chris Lattner1684cee2007-04-29 20:00:02 +0000421 /// NumSubBlocks - The total number of blocks these blocks contain.
422 unsigned NumSubBlocks;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000423
Chris Lattner1684cee2007-04-29 20:00:02 +0000424 /// NumAbbrevs - The total number of abbreviations.
425 unsigned NumAbbrevs;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000426
427 /// NumRecords - The total number of records these blocks contain, and the
Chris Lattner1684cee2007-04-29 20:00:02 +0000428 /// number that are abbreviated.
429 unsigned NumRecords, NumAbbreviatedRecords;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000430
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000431 /// CodeFreq - Keep track of the number of times we see each code.
Chris Lattnerbf419a92009-04-27 17:59:34 +0000432 std::vector<PerRecordStats> CodeFreq;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000433
Chris Lattner1684cee2007-04-29 20:00:02 +0000434 PerBlockIDStats()
435 : NumInstances(0), NumBits(0),
436 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
437};
438
439static std::map<unsigned, PerBlockIDStats> BlockIDStats;
440
441
442
Zachary Turner2ee505e2016-10-03 18:17:18 +0000443/// ReportError - All bitcode analysis errors go through this function, making this a
Chris Lattnerca0ea542007-04-29 08:31:14 +0000444/// good place to breakpoint if debugging.
Zachary Turner2ee505e2016-10-03 18:17:18 +0000445static bool ReportError(const Twine &Err) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000446 errs() << Err << "\n";
Chris Lattnerca0ea542007-04-29 08:31:14 +0000447 return true;
448}
449
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000450static bool decodeMetadataStringsBlob(StringRef Indent,
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000451 ArrayRef<uint64_t> Record,
452 StringRef Blob) {
453 if (Blob.empty())
454 return true;
455
456 if (Record.size() != 2)
457 return true;
458
459 unsigned NumStrings = Record[0];
460 unsigned StringsOffset = Record[1];
461 outs() << " num-strings = " << NumStrings << " {\n";
462
463 StringRef Lengths = Blob.slice(0, StringsOffset);
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000464 SimpleBitstreamCursor R(Lengths);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000465 StringRef Strings = Blob.drop_front(StringsOffset);
466 do {
467 if (R.AtEndOfStream())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000468 return ReportError("bad length");
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000469
470 unsigned Size = R.ReadVBR(6);
471 if (Strings.size() < Size)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000472 return ReportError("truncated chars");
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000473
474 outs() << Indent << " '";
475 outs().write_escaped(Strings.slice(0, Size), /*hex=*/true);
476 outs() << "'\n";
477 Strings = Strings.drop_front(Size);
478 } while (--NumStrings);
479
480 outs() << Indent << " }";
481 return false;
482}
483
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000484static bool decodeBlob(unsigned Code, unsigned BlockID, StringRef Indent,
485 ArrayRef<uint64_t> Record, StringRef Blob) {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000486 if (BlockID != bitc::METADATA_BLOCK_ID)
487 return true;
488 if (Code != bitc::METADATA_STRINGS)
489 return true;
490
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000491 return decodeMetadataStringsBlob(Indent, Record, Blob);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000492}
493
Chris Lattnerca0ea542007-04-29 08:31:14 +0000494/// ParseBlock - Read a block, updating statistics, etc.
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000495static bool ParseBlock(BitstreamCursor &Stream, BitstreamBlockInfo &BlockInfo,
496 unsigned BlockID, unsigned IndentLevel,
497 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +0000498 std::string Indent(IndentLevel*2, ' ');
Chris Lattner1684cee2007-04-29 20:00:02 +0000499 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
Chris Lattner3543caa2007-04-29 21:48:19 +0000500
Chris Lattner1684cee2007-04-29 20:00:02 +0000501 // Get the statistics for this BlockID.
502 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000503
Chris Lattner1684cee2007-04-29 20:00:02 +0000504 BlockStats.NumInstances++;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000505
Chris Lattner9181ddf2007-05-05 00:17:42 +0000506 // BLOCKINFO is a special part of the stream.
Richard Smithdc1414b2016-02-06 00:46:09 +0000507 bool DumpRecords = Dump;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000508 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Chris Lattner633ab162012-03-19 23:40:48 +0000509 if (Dump) outs() << Indent << "<BLOCKINFO_BLOCK/>\n";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000510 Optional<BitstreamBlockInfo> NewBlockInfo =
511 Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
512 if (!NewBlockInfo)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000513 return ReportError("Malformed BlockInfoBlock");
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000514 BlockInfo = std::move(*NewBlockInfo);
515 Stream.JumpToBit(BlockBitStart);
Richard Smithdc1414b2016-02-06 00:46:09 +0000516 // It's not really interesting to dump the contents of the blockinfo block.
517 DumpRecords = false;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000518 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000519
Chris Lattner3543caa2007-04-29 21:48:19 +0000520 unsigned NumWords = 0;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000521 if (Stream.EnterSubBlock(BlockID, &NumWords))
Zachary Turner2ee505e2016-10-03 18:17:18 +0000522 return ReportError("Malformed block record");
Chris Lattnerca0ea542007-04-29 08:31:14 +0000523
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000524 // Keep it for later, when we see a MODULE_HASH record
525 uint64_t BlockEntryPos = Stream.getCurrentByteNo();
526
Craig Toppere6cb63e2014-04-25 04:24:47 +0000527 const char *BlockName = nullptr;
Richard Smithdc1414b2016-02-06 00:46:09 +0000528 if (DumpRecords) {
Chris Lattner633ab162012-03-19 23:40:48 +0000529 outs() << Indent << "<";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000530 if ((BlockName = GetBlockName(BlockID, BlockInfo, CurStreamType)))
Chris Lattner633ab162012-03-19 23:40:48 +0000531 outs() << BlockName;
Chris Lattner3543caa2007-04-29 21:48:19 +0000532 else
Chris Lattner633ab162012-03-19 23:40:48 +0000533 outs() << "UnknownBlock" << BlockID;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000534
Chris Lattner3543caa2007-04-29 21:48:19 +0000535 if (NonSymbolic && BlockName)
Chris Lattner633ab162012-03-19 23:40:48 +0000536 outs() << " BlockID=" << BlockID;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000537
Chris Lattner633ab162012-03-19 23:40:48 +0000538 outs() << " NumWords=" << NumWords
Chris Lattner3fa323d2013-01-19 21:37:14 +0000539 << " BlockCodeSize=" << Stream.getAbbrevIDWidth() << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000540 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000541
Chris Lattnerca0ea542007-04-29 08:31:14 +0000542 SmallVector<uint64_t, 64> Record;
543
Mehdi Aminie98f9252016-12-28 22:30:28 +0000544 // Keep the offset to the metadata index if seen.
545 uint64_t MetadataIndexOffset = 0;
546
Chris Lattnerca0ea542007-04-29 08:31:14 +0000547 // Read all the records for this block.
548 while (1) {
549 if (Stream.AtEndOfStream())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000550 return ReportError("Premature end of bitstream");
Chris Lattnerca0ea542007-04-29 08:31:14 +0000551
Chris Lattner1cf80692009-04-27 18:15:27 +0000552 uint64_t RecordStartBit = Stream.GetCurrentBitNo();
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000553
Chris Lattner0271af82013-01-20 02:50:32 +0000554 BitstreamEntry Entry =
555 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
556
557 switch (Entry.Kind) {
558 case BitstreamEntry::Error:
Zachary Turner2ee505e2016-10-03 18:17:18 +0000559 return ReportError("malformed bitcode file");
Chris Lattner0271af82013-01-20 02:50:32 +0000560 case BitstreamEntry::EndBlock: {
Chris Lattner1684cee2007-04-29 20:00:02 +0000561 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
562 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
Richard Smithdc1414b2016-02-06 00:46:09 +0000563 if (DumpRecords) {
Chris Lattner633ab162012-03-19 23:40:48 +0000564 outs() << Indent << "</";
Chris Lattner3543caa2007-04-29 21:48:19 +0000565 if (BlockName)
Chris Lattner633ab162012-03-19 23:40:48 +0000566 outs() << BlockName << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000567 else
Chris Lattner633ab162012-03-19 23:40:48 +0000568 outs() << "UnknownBlock" << BlockID << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000569 }
Chris Lattnerca0ea542007-04-29 08:31:14 +0000570 return false;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000571 }
Chris Lattner0271af82013-01-20 02:50:32 +0000572
573 case BitstreamEntry::SubBlock: {
Chris Lattner9e808cd2007-05-05 01:29:31 +0000574 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000575 if (ParseBlock(Stream, BlockInfo, Entry.ID, IndentLevel + 1,
576 CurStreamType))
Chris Lattnerca0ea542007-04-29 08:31:14 +0000577 return true;
Chris Lattner1684cee2007-04-29 20:00:02 +0000578 ++BlockStats.NumSubBlocks;
Chris Lattner9e808cd2007-05-05 01:29:31 +0000579 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
Chris Lattner0271af82013-01-20 02:50:32 +0000580
Chris Lattner9e808cd2007-05-05 01:29:31 +0000581 // Don't include subblock sizes in the size of this block.
582 BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
Chris Lattner0271af82013-01-20 02:50:32 +0000583 continue;
584 }
585 case BitstreamEntry::Record:
586 // The interesting case.
Chris Lattnerca0ea542007-04-29 08:31:14 +0000587 break;
Chris Lattner9e808cd2007-05-05 01:29:31 +0000588 }
Chris Lattner0271af82013-01-20 02:50:32 +0000589
590 if (Entry.ID == bitc::DEFINE_ABBREV) {
Chris Lattnerca0ea542007-04-29 08:31:14 +0000591 Stream.ReadAbbrevRecord();
Chris Lattner1684cee2007-04-29 20:00:02 +0000592 ++BlockStats.NumAbbrevs;
Chris Lattner0271af82013-01-20 02:50:32 +0000593 continue;
594 }
595
596 Record.clear();
Chris Lattner2ed6a202009-04-06 22:44:40 +0000597
Chris Lattner0271af82013-01-20 02:50:32 +0000598 ++BlockStats.NumRecords;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000599
Chris Lattner0271af82013-01-20 02:50:32 +0000600 StringRef Blob;
Peter Collingbournecf2750a2016-12-01 05:47:58 +0000601 unsigned CurrentRecordPos = Stream.GetCurrentBitNo();
Chris Lattner0271af82013-01-20 02:50:32 +0000602 unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000603
Chris Lattner0271af82013-01-20 02:50:32 +0000604 // Increment the # occurrences of this code.
605 if (BlockStats.CodeFreq.size() <= Code)
606 BlockStats.CodeFreq.resize(Code+1);
607 BlockStats.CodeFreq[Code].NumInstances++;
608 BlockStats.CodeFreq[Code].TotalBits +=
609 Stream.GetCurrentBitNo()-RecordStartBit;
610 if (Entry.ID != bitc::UNABBREV_RECORD) {
611 BlockStats.CodeFreq[Code].NumAbbrev++;
612 ++BlockStats.NumAbbreviatedRecords;
613 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000614
Richard Smithdc1414b2016-02-06 00:46:09 +0000615 if (DumpRecords) {
Chris Lattner0271af82013-01-20 02:50:32 +0000616 outs() << Indent << " <";
617 if (const char *CodeName =
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000618 GetCodeName(Code, BlockID, BlockInfo, CurStreamType))
Chris Lattner0271af82013-01-20 02:50:32 +0000619 outs() << CodeName;
620 else
621 outs() << "UnknownCode" << Code;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000622 if (NonSymbolic && GetCodeName(Code, BlockID, BlockInfo, CurStreamType))
Chris Lattner0271af82013-01-20 02:50:32 +0000623 outs() << " codeid=" << Code;
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000624 const BitCodeAbbrev *Abbv = nullptr;
625 if (Entry.ID != bitc::UNABBREV_RECORD) {
626 Abbv = Stream.getAbbrev(Entry.ID);
Chris Lattner0271af82013-01-20 02:50:32 +0000627 outs() << " abbrevid=" << Entry.ID;
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000628 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000629
Chris Lattner0271af82013-01-20 02:50:32 +0000630 for (unsigned i = 0, e = Record.size(); i != e; ++i)
631 outs() << " op" << i << "=" << (int64_t)Record[i];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000632
Mehdi Aminie98f9252016-12-28 22:30:28 +0000633 // If we found a metadata index, let's verify that we had an offset before
634 // and validate its forward reference offset was correct!
635 if (BlockID == bitc::METADATA_BLOCK_ID) {
636 if (Code == bitc::METADATA_INDEX_OFFSET) {
Mehdi Amini5022bb72016-12-28 23:45:54 +0000637 if (Record.size() != 2)
638 outs() << "(Invalid record)";
639 else {
640 auto Offset = Record[0] + (Record[1] << 32);
641 MetadataIndexOffset = Stream.GetCurrentBitNo() + Offset;
642 }
Mehdi Aminie98f9252016-12-28 22:30:28 +0000643 }
644 if (Code == bitc::METADATA_INDEX) {
645 outs() << " (offset ";
646 if (MetadataIndexOffset == RecordStartBit)
647 outs() << "match)";
648 else
649 outs() << "mismatch: " << MetadataIndexOffset << " vs "
650 << RecordStartBit << ")";
651 }
652 }
653
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000654 // If we found a module hash, let's verify that it matches!
655 if (BlockID == bitc::MODULE_BLOCK_ID && Code == bitc::MODULE_CODE_HASH) {
656 if (Record.size() != 5)
657 outs() << " (invalid)";
658 else {
659 // Recompute the hash and compare it to the one in the bitcode
660 SHA1 Hasher;
661 StringRef Hash;
662 {
Peter Collingbournecf2750a2016-12-01 05:47:58 +0000663 int BlockSize = (CurrentRecordPos / 8) - BlockEntryPos;
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000664 auto Ptr = Stream.getPointerToByte(BlockEntryPos, BlockSize);
665 Hasher.update(ArrayRef<uint8_t>(Ptr, BlockSize));
666 Hash = Hasher.result();
667 }
668 SmallString<20> RecordedHash;
669 RecordedHash.resize(20);
670 int Pos = 0;
671 for (auto &Val : Record) {
672 assert(!(Val >> 32) && "Unexpected high bits set");
673 RecordedHash[Pos++] = (Val >> 24) & 0xFF;
674 RecordedHash[Pos++] = (Val >> 16) & 0xFF;
675 RecordedHash[Pos++] = (Val >> 8) & 0xFF;
676 RecordedHash[Pos++] = (Val >> 0) & 0xFF;
677 }
678 if (Hash == RecordedHash)
679 outs() << " (match)";
680 else
681 outs() << " (!mismatch!)";
682 }
683 }
684
Chris Lattner0271af82013-01-20 02:50:32 +0000685 outs() << "/>";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000686
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000687 if (Abbv) {
688 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
689 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
690 if (!Op.isEncoding() || Op.getEncoding() != BitCodeAbbrevOp::Array)
691 continue;
692 assert(i + 2 == e && "Array op not second to last");
693 std::string Str;
694 bool ArrayIsPrintable = true;
695 for (unsigned j = i - 1, je = Record.size(); j != je; ++j) {
696 if (!isprint(static_cast<unsigned char>(Record[j]))) {
697 ArrayIsPrintable = false;
698 break;
699 }
700 Str += (char)Record[j];
701 }
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000702 if (ArrayIsPrintable)
703 outs() << " record string = '" << Str << "'";
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000704 break;
705 }
706 }
707
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000708 if (Blob.data() && decodeBlob(Code, BlockID, Indent, Record, Blob)) {
Chris Lattner0271af82013-01-20 02:50:32 +0000709 outs() << " blob data = ";
Jordan Rose0fa38b82015-05-13 18:51:49 +0000710 if (ShowBinaryBlobs) {
711 outs() << "'";
712 outs().write_escaped(Blob, /*hex=*/true) << "'";
713 } else {
714 bool BlobIsPrintable = true;
715 for (unsigned i = 0, e = Blob.size(); i != e; ++i)
716 if (!isprint(static_cast<unsigned char>(Blob[i]))) {
717 BlobIsPrintable = false;
718 break;
719 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000720
Jordan Rose0fa38b82015-05-13 18:51:49 +0000721 if (BlobIsPrintable)
722 outs() << "'" << Blob << "'";
723 else
724 outs() << "unprintable, " << Blob.size() << " bytes.";
725 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000726 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000727
Chris Lattner0271af82013-01-20 02:50:32 +0000728 outs() << "\n";
Chris Lattnerca0ea542007-04-29 08:31:14 +0000729 }
Peter Collingbournecf2750a2016-12-01 05:47:58 +0000730
731 // Make sure that we can skip the current record.
732 Stream.JumpToBit(CurrentRecordPos);
733 Stream.skipRecord(Entry.ID);
Chris Lattnerca0ea542007-04-29 08:31:14 +0000734 }
735}
736
Chris Lattner1684cee2007-04-29 20:00:02 +0000737static void PrintSize(double Bits) {
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000738 outs() << format("%.2f/%.2fB/%luW", Bits, Bits/8,(unsigned long)(Bits/32));
Chris Lattnerbf419a92009-04-27 17:59:34 +0000739}
740static void PrintSize(uint64_t Bits) {
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000741 outs() << format("%lub/%.2fB/%luW", (unsigned long)Bits,
742 (double)Bits/8, (unsigned long)(Bits/32));
Chris Lattner1684cee2007-04-29 20:00:02 +0000743}
744
Jordan Rose88eb5342014-08-30 17:07:55 +0000745static bool openBitcodeFile(StringRef Path,
746 std::unique_ptr<MemoryBuffer> &MemBuf,
Jordan Rose88eb5342014-08-30 17:07:55 +0000747 BitstreamCursor &Stream,
748 CurStreamTypeType &CurStreamType) {
Chris Lattner03997582007-04-29 08:12:22 +0000749 // Read the input file.
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000750 ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr =
Jordan Rose88eb5342014-08-30 17:07:55 +0000751 MemoryBuffer::getFileOrSTDIN(Path);
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000752 if (std::error_code EC = MemBufOrErr.getError())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000753 return ReportError(Twine("ReportError reading '") + Path + "': " + EC.message());
Jordan Rose88eb5342014-08-30 17:07:55 +0000754 MemBuf = std::move(MemBufOrErr.get());
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000755
Jordan Rose88eb5342014-08-30 17:07:55 +0000756 if (MemBuf->getBufferSize() & 3)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000757 return ReportError("Bitcode stream should be a multiple of 4 bytes in length");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000758
Jordan Rose88eb5342014-08-30 17:07:55 +0000759 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart();
760 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize();
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000761
Chris Lattnerb9e07fd2009-04-06 20:54:32 +0000762 // If we have a wrapper header, parse it and ignore the non-bc file contents.
763 // The magic number is 0x0B17C0DE stored in little endian.
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000764 if (isBitcodeWrapper(BufPtr, EndBufPtr)) {
Mehdi Aminieed26932016-04-01 05:19:14 +0000765 if (MemBuf->getBufferSize() < BWH_HeaderSize)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000766 return ReportError("Invalid bitcode wrapper header");
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000767
768 if (Dump) {
769 unsigned Magic = support::endian::read32le(&BufPtr[BWH_MagicField]);
770 unsigned Version = support::endian::read32le(&BufPtr[BWH_VersionField]);
771 unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]);
772 unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]);
773 unsigned CPUType = support::endian::read32le(&BufPtr[BWH_CPUTypeField]);
774
775 outs() << "<BITCODE_WRAPPER_HEADER"
776 << " Magic=" << format_hex(Magic, 10)
777 << " Version=" << format_hex(Version, 10)
778 << " Offset=" << format_hex(Offset, 10)
779 << " Size=" << format_hex(Size, 10)
780 << " CPUType=" << format_hex(CPUType, 10) << "/>\n";
781 }
782
Derek Schuff8b2dcad2012-02-06 22:30:29 +0000783 if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr, true))
Zachary Turner2ee505e2016-10-03 18:17:18 +0000784 return ReportError("Invalid bitcode wrapper header");
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000785 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000786
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000787 Stream = BitstreamCursor(ArrayRef<uint8_t>(BufPtr, EndBufPtr));
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000788
Chris Lattner03997582007-04-29 08:12:22 +0000789 // Read the stream signature.
790 char Signature[6];
791 Signature[0] = Stream.Read(8);
792 Signature[1] = Stream.Read(8);
793 Signature[2] = Stream.Read(4);
794 Signature[3] = Stream.Read(4);
795 Signature[4] = Stream.Read(4);
796 Signature[5] = Stream.Read(4);
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000797
Chris Lattnerca0ea542007-04-29 08:31:14 +0000798 // Autodetect the file contents, if it is one we know.
Chris Lattner03997582007-04-29 08:12:22 +0000799 CurStreamType = UnknownBitstream;
800 if (Signature[0] == 'B' && Signature[1] == 'C' &&
801 Signature[2] == 0x0 && Signature[3] == 0xC &&
802 Signature[4] == 0xE && Signature[5] == 0xD)
803 CurStreamType = LLVMIRBitstream;
804
Jordan Rose88eb5342014-08-30 17:07:55 +0000805 return false;
806}
807
808/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
809static int AnalyzeBitcode() {
810 std::unique_ptr<MemoryBuffer> StreamBuffer;
Jordan Rose88eb5342014-08-30 17:07:55 +0000811 BitstreamCursor Stream;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000812 BitstreamBlockInfo BlockInfo;
Jordan Rose88eb5342014-08-30 17:07:55 +0000813 CurStreamTypeType CurStreamType;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000814 if (openBitcodeFile(InputFilename, StreamBuffer, Stream, CurStreamType))
Jordan Rose88eb5342014-08-30 17:07:55 +0000815 return true;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000816 Stream.setBlockInfo(&BlockInfo);
Jordan Rose88eb5342014-08-30 17:07:55 +0000817
818 // Read block info from BlockInfoFilename, if specified.
819 // The block info must be a top-level block.
820 if (!BlockInfoFilename.empty()) {
821 std::unique_ptr<MemoryBuffer> BlockInfoBuffer;
Jordan Rose88eb5342014-08-30 17:07:55 +0000822 BitstreamCursor BlockInfoCursor;
823 CurStreamTypeType BlockInfoStreamType;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000824 if (openBitcodeFile(BlockInfoFilename, BlockInfoBuffer, BlockInfoCursor,
825 BlockInfoStreamType))
Jordan Rose88eb5342014-08-30 17:07:55 +0000826 return true;
827
828 while (!BlockInfoCursor.AtEndOfStream()) {
829 unsigned Code = BlockInfoCursor.ReadCode();
830 if (Code != bitc::ENTER_SUBBLOCK)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000831 return ReportError("Invalid record at top-level in block info file");
Jordan Rose88eb5342014-08-30 17:07:55 +0000832
833 unsigned BlockID = BlockInfoCursor.ReadSubBlockID();
834 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000835 Optional<BitstreamBlockInfo> NewBlockInfo =
836 BlockInfoCursor.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
837 if (!NewBlockInfo)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000838 return ReportError("Malformed BlockInfoBlock in block info file");
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000839 BlockInfo = std::move(*NewBlockInfo);
Jordan Rose88eb5342014-08-30 17:07:55 +0000840 break;
841 }
842
843 BlockInfoCursor.SkipBlock();
844 }
Jordan Rose88eb5342014-08-30 17:07:55 +0000845 }
846
Chris Lattner1684cee2007-04-29 20:00:02 +0000847 unsigned NumTopBlocks = 0;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000848
Chris Lattnerca0ea542007-04-29 08:31:14 +0000849 // Parse the top-level structure. We only allow blocks at the top-level.
850 while (!Stream.AtEndOfStream()) {
851 unsigned Code = Stream.ReadCode();
852 if (Code != bitc::ENTER_SUBBLOCK)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000853 return ReportError("Invalid record at top-level");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000854
Chris Lattner0271af82013-01-20 02:50:32 +0000855 unsigned BlockID = Stream.ReadSubBlockID();
856
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000857 if (ParseBlock(Stream, BlockInfo, BlockID, 0, CurStreamType))
Chris Lattnerca0ea542007-04-29 08:31:14 +0000858 return true;
Chris Lattner1684cee2007-04-29 20:00:02 +0000859 ++NumTopBlocks;
Chris Lattnerca0ea542007-04-29 08:31:14 +0000860 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000861
Chris Lattner633ab162012-03-19 23:40:48 +0000862 if (Dump) outs() << "\n\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000863
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000864 uint64_t BufferSizeBits = Stream.getBitcodeBytes().size() * CHAR_BIT;
Chris Lattnerca0ea542007-04-29 08:31:14 +0000865 // Print a summary of the read file.
Chris Lattner633ab162012-03-19 23:40:48 +0000866 outs() << "Summary of " << InputFilename << ":\n";
867 outs() << " Total size: ";
Chris Lattner5fab65d2007-05-01 02:43:46 +0000868 PrintSize(BufferSizeBits);
Chris Lattner633ab162012-03-19 23:40:48 +0000869 outs() << "\n";
870 outs() << " Stream type: ";
Chris Lattner03997582007-04-29 08:12:22 +0000871 switch (CurStreamType) {
Chris Lattner633ab162012-03-19 23:40:48 +0000872 case UnknownBitstream: outs() << "unknown\n"; break;
873 case LLVMIRBitstream: outs() << "LLVM IR\n"; break;
Chris Lattner03997582007-04-29 08:12:22 +0000874 }
Chris Lattner633ab162012-03-19 23:40:48 +0000875 outs() << " # Toplevel Blocks: " << NumTopBlocks << "\n";
876 outs() << "\n";
Chris Lattner1684cee2007-04-29 20:00:02 +0000877
878 // Emit per-block stats.
Chris Lattner633ab162012-03-19 23:40:48 +0000879 outs() << "Per-block Summary:\n";
Chris Lattner1684cee2007-04-29 20:00:02 +0000880 for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
881 E = BlockIDStats.end(); I != E; ++I) {
Chris Lattner633ab162012-03-19 23:40:48 +0000882 outs() << " Block ID #" << I->first;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000883 if (const char *BlockName =
884 GetBlockName(I->first, BlockInfo, CurStreamType))
Chris Lattner633ab162012-03-19 23:40:48 +0000885 outs() << " (" << BlockName << ")";
886 outs() << ":\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000887
Chris Lattner1684cee2007-04-29 20:00:02 +0000888 const PerBlockIDStats &Stats = I->second;
Chris Lattner633ab162012-03-19 23:40:48 +0000889 outs() << " Num Instances: " << Stats.NumInstances << "\n";
890 outs() << " Total Size: ";
Chris Lattner1684cee2007-04-29 20:00:02 +0000891 PrintSize(Stats.NumBits);
Chris Lattner633ab162012-03-19 23:40:48 +0000892 outs() << "\n";
Daniel Dunbare813b222009-09-25 16:04:21 +0000893 double pct = (Stats.NumBits * 100.0) / BufferSizeBits;
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000894 outs() << " Percent of file: " << format("%2.4f%%", pct) << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000895 if (Stats.NumInstances > 1) {
Chris Lattner633ab162012-03-19 23:40:48 +0000896 outs() << " Average Size: ";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000897 PrintSize(Stats.NumBits/(double)Stats.NumInstances);
Chris Lattner633ab162012-03-19 23:40:48 +0000898 outs() << "\n";
899 outs() << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000900 << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
Chris Lattner633ab162012-03-19 23:40:48 +0000901 outs() << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000902 << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
Chris Lattner633ab162012-03-19 23:40:48 +0000903 outs() << " Tot/Avg Records: " << Stats.NumRecords << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000904 << Stats.NumRecords/(double)Stats.NumInstances << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000905 } else {
Chris Lattner633ab162012-03-19 23:40:48 +0000906 outs() << " Num SubBlocks: " << Stats.NumSubBlocks << "\n";
907 outs() << " Num Abbrevs: " << Stats.NumAbbrevs << "\n";
908 outs() << " Num Records: " << Stats.NumRecords << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000909 }
Daniel Dunbare813b222009-09-25 16:04:21 +0000910 if (Stats.NumRecords) {
911 double pct = (Stats.NumAbbreviatedRecords * 100.0) / Stats.NumRecords;
Chris Lattner633ab162012-03-19 23:40:48 +0000912 outs() << " Percent Abbrevs: " << format("%2.4f%%", pct) << "\n";
Daniel Dunbare813b222009-09-25 16:04:21 +0000913 }
Chris Lattner633ab162012-03-19 23:40:48 +0000914 outs() << "\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000915
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000916 // Print a histogram of the codes we see.
917 if (!NoHistogram && !Stats.CodeFreq.empty()) {
Mehdi Aminid2638562015-10-21 06:10:55 +0000918 std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code>
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000919 for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
Mehdi Aminid2638562015-10-21 06:10:55 +0000920 if (unsigned Freq = Stats.CodeFreq[i].NumInstances)
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000921 FreqPairs.push_back(std::make_pair(Freq, i));
922 std::stable_sort(FreqPairs.begin(), FreqPairs.end());
923 std::reverse(FreqPairs.begin(), FreqPairs.end());
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000924
Chris Lattner633ab162012-03-19 23:40:48 +0000925 outs() << "\tRecord Histogram:\n";
Richard Smithdc1414b2016-02-06 00:46:09 +0000926 outs() << "\t\t Count # Bits b/Rec % Abv Record Kind\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000927 for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
Chris Lattner1cf80692009-04-27 18:15:27 +0000928 const PerRecordStats &RecStats = Stats.CodeFreq[FreqPairs[i].second];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000929
Mehdi Aminid2638562015-10-21 06:10:55 +0000930 outs() << format("\t\t%7d %9lu",
Jan Wen Voung05ff5702012-09-05 20:55:57 +0000931 RecStats.NumInstances,
932 (unsigned long)RecStats.TotalBits);
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000933
Richard Smithdc1414b2016-02-06 00:46:09 +0000934 if (RecStats.NumInstances > 1)
935 outs() << format(" %9.1f",
936 (double)RecStats.TotalBits/RecStats.NumInstances);
937 else
938 outs() << " ";
939
Chris Lattner1cf80692009-04-27 18:15:27 +0000940 if (RecStats.NumAbbrev)
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000941 outs() <<
Richard Smithdc1414b2016-02-06 00:46:09 +0000942 format(" %7.2f",
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000943 (double)RecStats.NumAbbrev/RecStats.NumInstances*100);
Chris Lattner1cf80692009-04-27 18:15:27 +0000944 else
Richard Smithdc1414b2016-02-06 00:46:09 +0000945 outs() << " ";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000946
Richard Smithdc1414b2016-02-06 00:46:09 +0000947 outs() << " ";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000948 if (const char *CodeName = GetCodeName(FreqPairs[i].second, I->first,
949 BlockInfo, CurStreamType))
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000950 outs() << CodeName << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000951 else
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000952 outs() << "UnknownCode" << FreqPairs[i].second << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000953 }
Chris Lattner633ab162012-03-19 23:40:48 +0000954 outs() << "\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000955
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000956 }
Chris Lattner1684cee2007-04-29 20:00:02 +0000957 }
Chris Lattner03997582007-04-29 08:12:22 +0000958 return 0;
959}
Reid Spencerdb5c86d2004-06-07 17:53:43 +0000960
Chris Lattnerca0ea542007-04-29 08:31:14 +0000961
Chris Lattner76d46322006-12-06 01:18:01 +0000962int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000963 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000964 sys::PrintStackTraceOnErrorSignal(argv[0]);
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000965 PrettyStackTraceProgram X(argc, argv);
966 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
967 cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000968
Chris Lattner6d80e212007-05-06 09:29:57 +0000969 return AnalyzeBitcode();
Reid Spencerdb5c86d2004-06-07 17:53:43 +0000970}