blob: 789a666cb41a2530c494af443d9843d29ee4d622 [file] [log] [blame]
Gabor Greif8ff70c22007-07-04 21:55:50 +00001//===-- llvm-bcanalyzer.cpp - Bitcode Analyzer --------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Reid Spencerdac69c82004-06-07 17:53:43 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-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 Brukman3da94ae2005-04-22 00:00:37 +00007//
Reid Spencerdac69c82004-06-07 17:53:43 +00008//===----------------------------------------------------------------------===//
9//
Reid Spencer96684ef2004-06-08 05:56:58 +000010// This tool may be invoked in the following manner:
Gabor Greif8ff70c22007-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 Spencerdac69c82004-06-07 17:53:43 +000013//
Reid Spencer96684ef2004-06-08 05:56:58 +000014// Options:
Reid Spencer23d46e72004-06-10 18:38:44 +000015// --help - Output information about command line switches
Gabor Greif8ff70c22007-07-04 21:55:50 +000016// --dump - Dump low-level bitcode structure in readable format
Reid Spencer96684ef2004-06-08 05:56:58 +000017//
Gabor Greif8ff70c22007-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 Spencer23d46e72004-06-10 18:38:44 +000021// statistics about the contents of the file. By default this information is
Gabor Greif8ff70c22007-07-04 21:55:50 +000022// detailed and contains information about individual bitcode blocks and the
Daniel Dunbar80ba3d22009-09-25 16:03:57 +000023// functions in the module.
Gabor Greif8ff70c22007-07-04 21:55:50 +000024// The tool is also able to print a bitcode file in a straight forward text
Misha Brukman3da94ae2005-04-22 00:00:37 +000025// format that shows the containment and relationships of the information in
Gabor Greif8ff70c22007-07-04 21:55:50 +000026// the bitcode file (-dump option).
Chris Lattner63db4852007-04-29 05:51:00 +000027//
Reid Spencerdac69c82004-06-07 17:53:43 +000028//===----------------------------------------------------------------------===//
29
Mehdi Aminid2f47012016-04-01 05:33:11 +000030#include "llvm/ADT/StringExtras.h"
Teresa Johnsona5479192016-11-11 05:34:58 +000031#include "llvm/Bitcode/BitcodeReader.h"
Mehdi Aminif6071e12016-04-18 09:17:29 +000032#include "llvm/Bitcode/BitstreamReader.h"
Chris Lattner4238d472007-04-29 20:00:02 +000033#include "llvm/Bitcode/LLVMBitCodes.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000034#include "llvm/Support/CommandLine.h"
Daniel Dunbar42985bb2009-09-25 16:04:21 +000035#include "llvm/Support/Format.h"
Rui Ueyama0b9d56a2018-04-13 18:26:06 +000036#include "llvm/Support/InitLLVM.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000037#include "llvm/Support/ManagedStatic.h"
Chris Lattner45e0f892007-04-29 08:12:22 +000038#include "llvm/Support/MemoryBuffer.h"
Mehdi Aminid2f47012016-04-01 05:33:11 +000039#include "llvm/Support/SHA1.h"
Jonas Devlieghereae6875e2018-04-21 21:11:59 +000040#include "llvm/Support/WithColor.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000041#include "llvm/Support/raw_ostream.h"
Reid Spencerdac69c82004-06-07 17:53:43 +000042using namespace llvm;
43
44static cl::opt<std::string>
Gabor Greif8ff70c22007-07-04 21:55:50 +000045 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Reid Spencerdac69c82004-06-07 17:53:43 +000046
Gabor Greif8ff70c22007-07-04 21:55:50 +000047static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace"));
Chris Lattner32de6332007-04-29 08:31:14 +000048
49//===----------------------------------------------------------------------===//
50// Bitcode specific analysis.
51//===----------------------------------------------------------------------===//
52
Chris Lattnerb1e85b52007-05-05 01:46:49 +000053static cl::opt<bool> NoHistogram("disable-histogram",
54 cl::desc("Do not print per-code histogram"));
Chris Lattner45e0f892007-04-29 08:12:22 +000055
Chris Lattnerb30c9252007-04-29 21:48:19 +000056static cl::opt<bool>
57NonSymbolic("non-symbolic",
Michael J. Spencer3ff95632010-12-16 03:29:14 +000058 cl::desc("Emit numeric info in dump even if"
Chris Lattnerb30c9252007-04-29 21:48:19 +000059 " symbolic info is available"));
60
Jordan Rosed4210752014-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 Rose18261972015-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 Collingbourne67a3f7f2017-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 Gohmane7e4b512010-12-09 20:35:40 +000073namespace {
74
75/// CurStreamTypeType - A type for CurStreamType
76enum CurStreamTypeType {
Chris Lattner45e0f892007-04-29 08:12:22 +000077 UnknownBitstream,
Brian Gesiak37f13782018-04-21 23:52:04 +000078 LLVMIRBitstream,
79 ClangSerializedASTBitstream,
80 ClangSerializedDiagnosticsBitstream,
Dan Gohmane7e4b512010-12-09 20:35:40 +000081};
82
83}
84
Chris Lattner4238d472007-04-29 20:00:02 +000085/// GetBlockName - Return a symbolic block name if known, otherwise return
Chris Lattnerb30c9252007-04-29 21:48:19 +000086/// null.
Chris Lattnerf9a3ec82009-04-26 22:21:57 +000087static const char *GetBlockName(unsigned BlockID,
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +000088 const BitstreamBlockInfo &BlockInfo,
Jordan Rosed4210752014-08-30 17:07:55 +000089 CurStreamTypeType CurStreamType) {
Chris Lattner1772b12d2007-05-05 00:17:42 +000090 // Standard blocks for all bitcode files.
91 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
92 if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
93 return "BLOCKINFO_BLOCK";
Craig Topper573faec2014-04-25 04:24:47 +000094 return nullptr;
Chris Lattner1772b12d2007-05-05 00:17:42 +000095 }
Daniel Dunbar80ba3d22009-09-25 16:03:57 +000096
Chris Lattnerf9a3ec82009-04-26 22:21:57 +000097 // Check to see if we have a blockinfo record for this block, with a name.
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +000098 if (const BitstreamBlockInfo::BlockInfo *Info =
99 BlockInfo.getBlockInfo(BlockID)) {
Chris Lattnerf9a3ec82009-04-26 22:21:57 +0000100 if (!Info->Name.empty())
101 return Info->Name.c_str();
102 }
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000103
104
Craig Topper573faec2014-04-25 04:24:47 +0000105 if (CurStreamType != LLVMIRBitstream) return nullptr;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000106
Chris Lattner4238d472007-04-29 20:00:02 +0000107 switch (BlockID) {
Sanjoy Das9ad5da12016-04-26 05:59:14 +0000108 default: return nullptr;
Sanjoy Das6588d8d2016-04-26 05:59:08 +0000109 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID: return "OPERAND_BUNDLE_TAGS_BLOCK";
Sanjoy Das9ad5da12016-04-26 05:59:14 +0000110 case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK";
111 case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK";
112 case bitc::PARAMATTR_GROUP_BLOCK_ID: return "PARAMATTR_GROUP_BLOCK_ID";
113 case bitc::TYPE_BLOCK_ID_NEW: return "TYPE_BLOCK_ID";
114 case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK";
115 case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK";
Mehdi Aminib738d342015-10-26 18:37:00 +0000116 case bitc::IDENTIFICATION_BLOCK_ID:
Sanjoy Das9ad5da12016-04-26 05:59:14 +0000117 return "IDENTIFICATION_BLOCK_ID";
118 case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB";
119 case bitc::METADATA_BLOCK_ID: return "METADATA_BLOCK";
120 case bitc::METADATA_KIND_BLOCK_ID: return "METADATA_KIND_BLOCK";
121 case bitc::METADATA_ATTACHMENT_ID: return "METADATA_ATTACHMENT_BLOCK";
122 case bitc::USELIST_BLOCK_ID: return "USELIST_BLOCK_ID";
Teresa Johnsondc6615a2016-03-11 18:52:24 +0000123 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
Sanjoy Das9ad5da12016-04-26 05:59:14 +0000124 return "GLOBALVAL_SUMMARY_BLOCK";
Peter Collingbournee74c64e2017-06-08 23:01:49 +0000125 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
126 return "FULL_LTO_GLOBALVAL_SUMMARY_BLOCK";
Sanjoy Das9ad5da12016-04-26 05:59:14 +0000127 case bitc::MODULE_STRTAB_BLOCK_ID: return "MODULE_STRTAB_BLOCK";
Peter Collingbourne6163b4a2017-04-17 17:51:36 +0000128 case bitc::STRTAB_BLOCK_ID: return "STRTAB_BLOCK";
Peter Collingbournefdc12502017-06-27 23:50:11 +0000129 case bitc::SYMTAB_BLOCK_ID: return "SYMTAB_BLOCK";
Chris Lattner4238d472007-04-29 20:00:02 +0000130 }
131}
132
Chris Lattnerb30c9252007-04-29 21:48:19 +0000133/// GetCodeName - Return a symbolic code name if known, otherwise return
134/// null.
Chris Lattnerf9a3ec82009-04-26 22:21:57 +0000135static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000136 const BitstreamBlockInfo &BlockInfo,
Jordan Rosed4210752014-08-30 17:07:55 +0000137 CurStreamTypeType CurStreamType) {
Chris Lattner1772b12d2007-05-05 00:17:42 +0000138 // Standard blocks for all bitcode files.
139 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
140 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
141 switch (CodeID) {
Craig Topper573faec2014-04-25 04:24:47 +0000142 default: return nullptr;
Chris Lattnerf9a3ec82009-04-26 22:21:57 +0000143 case bitc::BLOCKINFO_CODE_SETBID: return "SETBID";
144 case bitc::BLOCKINFO_CODE_BLOCKNAME: return "BLOCKNAME";
145 case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME";
Chris Lattner1772b12d2007-05-05 00:17:42 +0000146 }
147 }
Craig Topper573faec2014-04-25 04:24:47 +0000148 return nullptr;
Chris Lattner1772b12d2007-05-05 00:17:42 +0000149 }
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000150
Chris Lattnerf9a3ec82009-04-26 22:21:57 +0000151 // Check to see if we have a blockinfo record for this record, with a name.
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000152 if (const BitstreamBlockInfo::BlockInfo *Info =
153 BlockInfo.getBlockInfo(BlockID)) {
Chris Lattnerf9a3ec82009-04-26 22:21:57 +0000154 for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i)
155 if (Info->RecordNames[i].first == CodeID)
156 return Info->RecordNames[i].second.c_str();
157 }
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000158
159
Craig Topper573faec2014-04-25 04:24:47 +0000160 if (CurStreamType != LLVMIRBitstream) return nullptr;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000161
Duncan P. N. Exon Smith70dde5f2015-06-29 22:50:32 +0000162#define STRINGIFY_CODE(PREFIX, CODE) \
163 case bitc::PREFIX##_##CODE: \
164 return #CODE;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000165 switch (BlockID) {
Craig Topper573faec2014-04-25 04:24:47 +0000166 default: return nullptr;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000167 case bitc::MODULE_BLOCK_ID:
168 switch (CodeID) {
Craig Topper573faec2014-04-25 04:24:47 +0000169 default: return nullptr;
Duncan P. N. Exon Smith70dde5f2015-06-29 22:50:32 +0000170 STRINGIFY_CODE(MODULE_CODE, VERSION)
171 STRINGIFY_CODE(MODULE_CODE, TRIPLE)
172 STRINGIFY_CODE(MODULE_CODE, DATALAYOUT)
173 STRINGIFY_CODE(MODULE_CODE, ASM)
174 STRINGIFY_CODE(MODULE_CODE, SECTIONNAME)
175 STRINGIFY_CODE(MODULE_CODE, DEPLIB) // FIXME: Remove in 4.0
176 STRINGIFY_CODE(MODULE_CODE, GLOBALVAR)
177 STRINGIFY_CODE(MODULE_CODE, FUNCTION)
178 STRINGIFY_CODE(MODULE_CODE, ALIAS)
Duncan P. N. Exon Smith70dde5f2015-06-29 22:50:32 +0000179 STRINGIFY_CODE(MODULE_CODE, GCNAME)
Teresa Johnson30110172015-09-17 20:12:00 +0000180 STRINGIFY_CODE(MODULE_CODE, VSTOFFSET)
Duncan P. N. Exon Smithf206a2c2016-03-25 01:29:50 +0000181 STRINGIFY_CODE(MODULE_CODE, METADATA_VALUES_UNUSED)
Teresa Johnson00601602016-02-10 21:55:02 +0000182 STRINGIFY_CODE(MODULE_CODE, SOURCE_FILENAME)
Mehdi Aminid2f47012016-04-01 05:33:11 +0000183 STRINGIFY_CODE(MODULE_CODE, HASH)
Chris Lattnerb30c9252007-04-29 21:48:19 +0000184 }
Mehdi Aminib738d342015-10-26 18:37:00 +0000185 case bitc::IDENTIFICATION_BLOCK_ID:
186 switch (CodeID) {
187 default:
188 return nullptr;
189 STRINGIFY_CODE(IDENTIFICATION_CODE, STRING)
190 STRINGIFY_CODE(IDENTIFICATION_CODE, EPOCH)
191 }
Chris Lattnercd5b7d72007-05-04 03:01:41 +0000192 case bitc::PARAMATTR_BLOCK_ID:
193 switch (CodeID) {
Craig Topper573faec2014-04-25 04:24:47 +0000194 default: return nullptr;
Duncan P. N. Exon Smith70dde5f2015-06-29 22:50:32 +0000195 // FIXME: Should these be different?
Bill Wendling8620bd32013-02-10 23:17:10 +0000196 case bitc::PARAMATTR_CODE_ENTRY_OLD: return "ENTRY";
197 case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY";
Justin Bognera935ab12016-03-15 22:37:25 +0000198 }
199 case bitc::PARAMATTR_GROUP_BLOCK_ID:
200 switch (CodeID) {
201 default: return nullptr;
Bill Wendling8620bd32013-02-10 23:17:10 +0000202 case bitc::PARAMATTR_GRP_CODE_ENTRY: return "ENTRY";
Chris Lattnercd5b7d72007-05-04 03:01:41 +0000203 }
Chris Lattner1afcace2011-07-09 17:41:24 +0000204 case bitc::TYPE_BLOCK_ID_NEW:
Chris Lattnerb30c9252007-04-29 21:48:19 +0000205 switch (CodeID) {
Craig Topper573faec2014-04-25 04:24:47 +0000206 default: return nullptr;
Duncan P. N. Exon Smith70dde5f2015-06-29 22:50:32 +0000207 STRINGIFY_CODE(TYPE_CODE, NUMENTRY)
208 STRINGIFY_CODE(TYPE_CODE, VOID)
209 STRINGIFY_CODE(TYPE_CODE, FLOAT)
210 STRINGIFY_CODE(TYPE_CODE, DOUBLE)
211 STRINGIFY_CODE(TYPE_CODE, LABEL)
212 STRINGIFY_CODE(TYPE_CODE, OPAQUE)
213 STRINGIFY_CODE(TYPE_CODE, INTEGER)
214 STRINGIFY_CODE(TYPE_CODE, POINTER)
215 STRINGIFY_CODE(TYPE_CODE, ARRAY)
216 STRINGIFY_CODE(TYPE_CODE, VECTOR)
217 STRINGIFY_CODE(TYPE_CODE, X86_FP80)
218 STRINGIFY_CODE(TYPE_CODE, FP128)
219 STRINGIFY_CODE(TYPE_CODE, PPC_FP128)
220 STRINGIFY_CODE(TYPE_CODE, METADATA)
221 STRINGIFY_CODE(TYPE_CODE, STRUCT_ANON)
222 STRINGIFY_CODE(TYPE_CODE, STRUCT_NAME)
223 STRINGIFY_CODE(TYPE_CODE, STRUCT_NAMED)
224 STRINGIFY_CODE(TYPE_CODE, FUNCTION)
Chris Lattnerb30c9252007-04-29 21:48:19 +0000225 }
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000226
Chris Lattnerb30c9252007-04-29 21:48:19 +0000227 case bitc::CONSTANTS_BLOCK_ID:
228 switch (CodeID) {
Craig Topper573faec2014-04-25 04:24:47 +0000229 default: return nullptr;
Duncan P. N. Exon Smith70dde5f2015-06-29 22:50:32 +0000230 STRINGIFY_CODE(CST_CODE, SETTYPE)
231 STRINGIFY_CODE(CST_CODE, NULL)
232 STRINGIFY_CODE(CST_CODE, UNDEF)
233 STRINGIFY_CODE(CST_CODE, INTEGER)
234 STRINGIFY_CODE(CST_CODE, WIDE_INTEGER)
235 STRINGIFY_CODE(CST_CODE, FLOAT)
236 STRINGIFY_CODE(CST_CODE, AGGREGATE)
237 STRINGIFY_CODE(CST_CODE, STRING)
238 STRINGIFY_CODE(CST_CODE, CSTRING)
239 STRINGIFY_CODE(CST_CODE, CE_BINOP)
240 STRINGIFY_CODE(CST_CODE, CE_CAST)
241 STRINGIFY_CODE(CST_CODE, CE_GEP)
242 STRINGIFY_CODE(CST_CODE, CE_INBOUNDS_GEP)
243 STRINGIFY_CODE(CST_CODE, CE_SELECT)
244 STRINGIFY_CODE(CST_CODE, CE_EXTRACTELT)
245 STRINGIFY_CODE(CST_CODE, CE_INSERTELT)
246 STRINGIFY_CODE(CST_CODE, CE_SHUFFLEVEC)
247 STRINGIFY_CODE(CST_CODE, CE_CMP)
248 STRINGIFY_CODE(CST_CODE, INLINEASM)
249 STRINGIFY_CODE(CST_CODE, CE_SHUFVEC_EX)
Cameron McInallyca8cb682018-11-13 18:15:47 +0000250 STRINGIFY_CODE(CST_CODE, CE_UNOP)
Chris Lattnerd408f062012-01-30 00:51:16 +0000251 case bitc::CST_CODE_BLOCKADDRESS: return "CST_CODE_BLOCKADDRESS";
Duncan P. N. Exon Smith70dde5f2015-06-29 22:50:32 +0000252 STRINGIFY_CODE(CST_CODE, DATA)
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000253 }
Chris Lattnerb30c9252007-04-29 21:48:19 +0000254 case bitc::FUNCTION_BLOCK_ID:
255 switch (CodeID) {
Craig Topper573faec2014-04-25 04:24:47 +0000256 default: return nullptr;
Duncan P. N. Exon Smith70dde5f2015-06-29 22:50:32 +0000257 STRINGIFY_CODE(FUNC_CODE, DECLAREBLOCKS)
258 STRINGIFY_CODE(FUNC_CODE, INST_BINOP)
259 STRINGIFY_CODE(FUNC_CODE, INST_CAST)
260 STRINGIFY_CODE(FUNC_CODE, INST_GEP_OLD)
261 STRINGIFY_CODE(FUNC_CODE, INST_INBOUNDS_GEP_OLD)
262 STRINGIFY_CODE(FUNC_CODE, INST_SELECT)
263 STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTELT)
264 STRINGIFY_CODE(FUNC_CODE, INST_INSERTELT)
265 STRINGIFY_CODE(FUNC_CODE, INST_SHUFFLEVEC)
266 STRINGIFY_CODE(FUNC_CODE, INST_CMP)
267 STRINGIFY_CODE(FUNC_CODE, INST_RET)
268 STRINGIFY_CODE(FUNC_CODE, INST_BR)
269 STRINGIFY_CODE(FUNC_CODE, INST_SWITCH)
270 STRINGIFY_CODE(FUNC_CODE, INST_INVOKE)
Cameron McInallyca8cb682018-11-13 18:15:47 +0000271 STRINGIFY_CODE(FUNC_CODE, INST_UNOP)
Duncan P. N. Exon Smith70dde5f2015-06-29 22:50:32 +0000272 STRINGIFY_CODE(FUNC_CODE, INST_UNREACHABLE)
David Majnemer4a45f082015-07-31 17:58:14 +0000273 STRINGIFY_CODE(FUNC_CODE, INST_CLEANUPRET)
274 STRINGIFY_CODE(FUNC_CODE, INST_CATCHRET)
275 STRINGIFY_CODE(FUNC_CODE, INST_CATCHPAD)
Duncan P. N. Exon Smith70dde5f2015-06-29 22:50:32 +0000276 STRINGIFY_CODE(FUNC_CODE, INST_PHI)
277 STRINGIFY_CODE(FUNC_CODE, INST_ALLOCA)
278 STRINGIFY_CODE(FUNC_CODE, INST_LOAD)
279 STRINGIFY_CODE(FUNC_CODE, INST_VAARG)
280 STRINGIFY_CODE(FUNC_CODE, INST_STORE)
281 STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTVAL)
282 STRINGIFY_CODE(FUNC_CODE, INST_INSERTVAL)
283 STRINGIFY_CODE(FUNC_CODE, INST_CMP2)
284 STRINGIFY_CODE(FUNC_CODE, INST_VSELECT)
285 STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC_AGAIN)
286 STRINGIFY_CODE(FUNC_CODE, INST_CALL)
287 STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC)
288 STRINGIFY_CODE(FUNC_CODE, INST_GEP)
Sanjoy Das6588d8d2016-04-26 05:59:08 +0000289 STRINGIFY_CODE(FUNC_CODE, OPERAND_BUNDLE)
Matt Arsenault076203b2018-09-24 12:47:17 +0000290 STRINGIFY_CODE(FUNC_CODE, INST_FENCE)
291 STRINGIFY_CODE(FUNC_CODE, INST_ATOMICRMW)
292 STRINGIFY_CODE(FUNC_CODE, INST_LOADATOMIC)
293 STRINGIFY_CODE(FUNC_CODE, INST_STOREATOMIC)
294 STRINGIFY_CODE(FUNC_CODE, INST_CMPXCHG)
Chris Lattnerb30c9252007-04-29 21:48:19 +0000295 }
Chris Lattnerb30c9252007-04-29 21:48:19 +0000296 case bitc::VALUE_SYMTAB_BLOCK_ID:
297 switch (CodeID) {
Craig Topper573faec2014-04-25 04:24:47 +0000298 default: return nullptr;
Duncan P. N. Exon Smith70dde5f2015-06-29 22:50:32 +0000299 STRINGIFY_CODE(VST_CODE, ENTRY)
300 STRINGIFY_CODE(VST_CODE, BBENTRY)
Teresa Johnson30110172015-09-17 20:12:00 +0000301 STRINGIFY_CODE(VST_CODE, FNENTRY)
Teresa Johnsondc6615a2016-03-11 18:52:24 +0000302 STRINGIFY_CODE(VST_CODE, COMBINED_ENTRY)
Teresa Johnsonb97baa52015-10-04 14:33:43 +0000303 }
304 case bitc::MODULE_STRTAB_BLOCK_ID:
305 switch (CodeID) {
Teresa Johnson9dd98c02015-11-02 18:02:11 +0000306 default:
307 return nullptr;
308 STRINGIFY_CODE(MST_CODE, ENTRY)
Mehdi Aminid2f47012016-04-01 05:33:11 +0000309 STRINGIFY_CODE(MST_CODE, HASH)
Teresa Johnsonb97baa52015-10-04 14:33:43 +0000310 }
Teresa Johnsondc6615a2016-03-11 18:52:24 +0000311 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
Peter Collingbournee74c64e2017-06-08 23:01:49 +0000312 case bitc::FULL_LTO_GLOBALVAL_SUMMARY_BLOCK_ID:
Teresa Johnsonb97baa52015-10-04 14:33:43 +0000313 switch (CodeID) {
Teresa Johnson9dd98c02015-11-02 18:02:11 +0000314 default:
315 return nullptr;
Teresa Johnsondc6615a2016-03-11 18:52:24 +0000316 STRINGIFY_CODE(FS, PERMODULE)
317 STRINGIFY_CODE(FS, PERMODULE_PROFILE)
Easwaran Ramanbdb305a2018-01-25 19:27:17 +0000318 STRINGIFY_CODE(FS, PERMODULE_RELBF)
Teresa Johnsondc6615a2016-03-11 18:52:24 +0000319 STRINGIFY_CODE(FS, PERMODULE_GLOBALVAR_INIT_REFS)
320 STRINGIFY_CODE(FS, COMBINED)
321 STRINGIFY_CODE(FS, COMBINED_PROFILE)
322 STRINGIFY_CODE(FS, COMBINED_GLOBALVAR_INIT_REFS)
Mehdi Aminieb79e6e2016-04-16 06:56:44 +0000323 STRINGIFY_CODE(FS, ALIAS)
324 STRINGIFY_CODE(FS, COMBINED_ALIAS)
Mehdi Amini9ab3b372016-04-23 23:38:17 +0000325 STRINGIFY_CODE(FS, COMBINED_ORIGINAL_NAME)
Mehdi Amini3f53abc2016-04-24 03:18:11 +0000326 STRINGIFY_CODE(FS, VERSION)
Teresa Johnsond3f03a62018-02-07 04:05:59 +0000327 STRINGIFY_CODE(FS, FLAGS)
Peter Collingbourneea3f9182016-12-21 23:03:45 +0000328 STRINGIFY_CODE(FS, TYPE_TESTS)
Peter Collingbourne446f8d72017-02-10 22:29:38 +0000329 STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_VCALLS)
330 STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_VCALLS)
331 STRINGIFY_CODE(FS, TYPE_TEST_ASSUME_CONST_VCALL)
332 STRINGIFY_CODE(FS, TYPE_CHECKED_LOAD_CONST_VCALL)
Peter Collingbourne6163b4a2017-04-17 17:51:36 +0000333 STRINGIFY_CODE(FS, VALUE_GUID)
Evgeniy Stepanov3184f8d2017-06-16 00:18:29 +0000334 STRINGIFY_CODE(FS, CFI_FUNCTION_DEFS)
335 STRINGIFY_CODE(FS, CFI_FUNCTION_DECLS)
Vitaly Buka4a81fc62018-02-14 22:41:15 +0000336 STRINGIFY_CODE(FS, TYPE_ID)
Chris Lattnerb30c9252007-04-29 21:48:19 +0000337 }
Devang Patele8e02132009-09-18 19:26:43 +0000338 case bitc::METADATA_ATTACHMENT_ID:
339 switch(CodeID) {
Craig Topper573faec2014-04-25 04:24:47 +0000340 default:return nullptr;
Duncan P. N. Exon Smith88f33c92015-06-29 22:50:35 +0000341 STRINGIFY_CODE(METADATA, ATTACHMENT)
Devang Patele8e02132009-09-18 19:26:43 +0000342 }
Devang Patele54abc92009-07-22 17:43:22 +0000343 case bitc::METADATA_BLOCK_ID:
344 switch(CodeID) {
Craig Topper573faec2014-04-25 04:24:47 +0000345 default:return nullptr;
Duncan P. N. Exon Smitha73e5092016-03-27 23:17:54 +0000346 STRINGIFY_CODE(METADATA, STRING_OLD)
Duncan P. N. Exon Smith88f33c92015-06-29 22:50:35 +0000347 STRINGIFY_CODE(METADATA, VALUE)
Adrian Prantl639592d2017-01-03 19:17:49 +0000348 STRINGIFY_CODE(METADATA, NODE)
349 STRINGIFY_CODE(METADATA, NAME)
350 STRINGIFY_CODE(METADATA, DISTINCT_NODE)
351 STRINGIFY_CODE(METADATA, KIND) // Older bitcode has it in a MODULE_BLOCK
352 STRINGIFY_CODE(METADATA, LOCATION)
Duncan P. N. Exon Smith88f33c92015-06-29 22:50:35 +0000353 STRINGIFY_CODE(METADATA, OLD_NODE)
354 STRINGIFY_CODE(METADATA, OLD_FN_NODE)
355 STRINGIFY_CODE(METADATA, NAMED_NODE)
Duncan P. N. Exon Smith88f33c92015-06-29 22:50:35 +0000356 STRINGIFY_CODE(METADATA, GENERIC_DEBUG)
357 STRINGIFY_CODE(METADATA, SUBRANGE)
358 STRINGIFY_CODE(METADATA, ENUMERATOR)
359 STRINGIFY_CODE(METADATA, BASIC_TYPE)
360 STRINGIFY_CODE(METADATA, FILE)
361 STRINGIFY_CODE(METADATA, DERIVED_TYPE)
362 STRINGIFY_CODE(METADATA, COMPOSITE_TYPE)
363 STRINGIFY_CODE(METADATA, SUBROUTINE_TYPE)
364 STRINGIFY_CODE(METADATA, COMPILE_UNIT)
365 STRINGIFY_CODE(METADATA, SUBPROGRAM)
366 STRINGIFY_CODE(METADATA, LEXICAL_BLOCK)
367 STRINGIFY_CODE(METADATA, LEXICAL_BLOCK_FILE)
368 STRINGIFY_CODE(METADATA, NAMESPACE)
369 STRINGIFY_CODE(METADATA, TEMPLATE_TYPE)
370 STRINGIFY_CODE(METADATA, TEMPLATE_VALUE)
371 STRINGIFY_CODE(METADATA, GLOBAL_VAR)
372 STRINGIFY_CODE(METADATA, LOCAL_VAR)
373 STRINGIFY_CODE(METADATA, EXPRESSION)
374 STRINGIFY_CODE(METADATA, OBJC_PROPERTY)
375 STRINGIFY_CODE(METADATA, IMPORTED_ENTITY)
Adrian Prantl7ce02c12015-06-30 00:25:41 +0000376 STRINGIFY_CODE(METADATA, MODULE)
Adrian Prantl639592d2017-01-03 19:17:49 +0000377 STRINGIFY_CODE(METADATA, MACRO)
378 STRINGIFY_CODE(METADATA, MACRO_FILE)
379 STRINGIFY_CODE(METADATA, STRINGS)
380 STRINGIFY_CODE(METADATA, GLOBAL_DECL_ATTACHMENT)
381 STRINGIFY_CODE(METADATA, GLOBAL_VAR_EXPR)
Mehdi Amini89bf9692016-12-28 22:30:28 +0000382 STRINGIFY_CODE(METADATA, INDEX_OFFSET)
383 STRINGIFY_CODE(METADATA, INDEX)
Devang Patele54abc92009-07-22 17:43:22 +0000384 }
Teresa Johnson778af352015-11-15 02:00:09 +0000385 case bitc::METADATA_KIND_BLOCK_ID:
386 switch (CodeID) {
387 default:
388 return nullptr;
389 STRINGIFY_CODE(METADATA, KIND)
390 }
Chad Rosier837b4e42011-12-07 21:45:13 +0000391 case bitc::USELIST_BLOCK_ID:
392 switch(CodeID) {
Craig Topper573faec2014-04-25 04:24:47 +0000393 default:return nullptr;
Duncan P. N. Exon Smithbd24fe82014-07-28 21:19:41 +0000394 case bitc::USELIST_CODE_DEFAULT: return "USELIST_CODE_DEFAULT";
395 case bitc::USELIST_CODE_BB: return "USELIST_CODE_BB";
Chad Rosier837b4e42011-12-07 21:45:13 +0000396 }
Sanjoy Das6588d8d2016-04-26 05:59:08 +0000397
398 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
399 switch(CodeID) {
400 default: return nullptr;
401 case bitc::OPERAND_BUNDLE_TAG: return "OPERAND_BUNDLE_TAG";
402 }
Peter Collingbourne6163b4a2017-04-17 17:51:36 +0000403 case bitc::STRTAB_BLOCK_ID:
404 switch(CodeID) {
405 default: return nullptr;
406 case bitc::STRTAB_BLOB: return "BLOB";
407 }
Peter Collingbournefdc12502017-06-27 23:50:11 +0000408 case bitc::SYMTAB_BLOCK_ID:
409 switch(CodeID) {
410 default: return nullptr;
411 case bitc::SYMTAB_BLOB: return "BLOB";
412 }
Chris Lattnerb30c9252007-04-29 21:48:19 +0000413 }
Duncan P. N. Exon Smith70dde5f2015-06-29 22:50:32 +0000414#undef STRINGIFY_CODE
Chris Lattnerb30c9252007-04-29 21:48:19 +0000415}
416
Chris Lattner24437472009-04-27 17:59:34 +0000417struct PerRecordStats {
418 unsigned NumInstances;
Chris Lattnerc167cac2009-04-27 18:15:27 +0000419 unsigned NumAbbrev;
420 uint64_t TotalBits;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000421
Mehdi Amini33ee9972015-10-21 06:10:55 +0000422 PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
Chris Lattner24437472009-04-27 17:59:34 +0000423};
Chris Lattner4238d472007-04-29 20:00:02 +0000424
425struct PerBlockIDStats {
426 /// NumInstances - This the number of times this block ID has been seen.
427 unsigned NumInstances;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000428
Chris Lattner4238d472007-04-29 20:00:02 +0000429 /// NumBits - The total size in bits of all of these blocks.
430 uint64_t NumBits;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000431
Chris Lattner4238d472007-04-29 20:00:02 +0000432 /// NumSubBlocks - The total number of blocks these blocks contain.
433 unsigned NumSubBlocks;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000434
Chris Lattner4238d472007-04-29 20:00:02 +0000435 /// NumAbbrevs - The total number of abbreviations.
436 unsigned NumAbbrevs;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000437
438 /// NumRecords - The total number of records these blocks contain, and the
Chris Lattner4238d472007-04-29 20:00:02 +0000439 /// number that are abbreviated.
440 unsigned NumRecords, NumAbbreviatedRecords;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000441
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000442 /// CodeFreq - Keep track of the number of times we see each code.
Chris Lattner24437472009-04-27 17:59:34 +0000443 std::vector<PerRecordStats> CodeFreq;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000444
Chris Lattner4238d472007-04-29 20:00:02 +0000445 PerBlockIDStats()
446 : NumInstances(0), NumBits(0),
447 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
448};
449
450static std::map<unsigned, PerBlockIDStats> BlockIDStats;
451
452
453
Zachary Turner93263082016-10-03 18:17:18 +0000454/// ReportError - All bitcode analysis errors go through this function, making this a
Chris Lattner32de6332007-04-29 08:31:14 +0000455/// good place to breakpoint if debugging.
Zachary Turner93263082016-10-03 18:17:18 +0000456static bool ReportError(const Twine &Err) {
Jonas Devlieghereae6875e2018-04-21 21:11:59 +0000457 WithColor::error() << Err << "\n";
Chris Lattner32de6332007-04-29 08:31:14 +0000458 return true;
459}
460
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000461static bool decodeMetadataStringsBlob(StringRef Indent,
Duncan P. N. Exon Smitha73e5092016-03-27 23:17:54 +0000462 ArrayRef<uint64_t> Record,
463 StringRef Blob) {
464 if (Blob.empty())
465 return true;
466
467 if (Record.size() != 2)
468 return true;
469
470 unsigned NumStrings = Record[0];
471 unsigned StringsOffset = Record[1];
472 outs() << " num-strings = " << NumStrings << " {\n";
473
474 StringRef Lengths = Blob.slice(0, StringsOffset);
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000475 SimpleBitstreamCursor R(Lengths);
Duncan P. N. Exon Smitha73e5092016-03-27 23:17:54 +0000476 StringRef Strings = Blob.drop_front(StringsOffset);
477 do {
478 if (R.AtEndOfStream())
Zachary Turner93263082016-10-03 18:17:18 +0000479 return ReportError("bad length");
Duncan P. N. Exon Smitha73e5092016-03-27 23:17:54 +0000480
481 unsigned Size = R.ReadVBR(6);
482 if (Strings.size() < Size)
Zachary Turner93263082016-10-03 18:17:18 +0000483 return ReportError("truncated chars");
Duncan P. N. Exon Smitha73e5092016-03-27 23:17:54 +0000484
485 outs() << Indent << " '";
486 outs().write_escaped(Strings.slice(0, Size), /*hex=*/true);
487 outs() << "'\n";
488 Strings = Strings.drop_front(Size);
489 } while (--NumStrings);
490
491 outs() << Indent << " }";
492 return false;
493}
494
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000495static bool decodeBlob(unsigned Code, unsigned BlockID, StringRef Indent,
496 ArrayRef<uint64_t> Record, StringRef Blob) {
Duncan P. N. Exon Smitha73e5092016-03-27 23:17:54 +0000497 if (BlockID != bitc::METADATA_BLOCK_ID)
498 return true;
499 if (Code != bitc::METADATA_STRINGS)
500 return true;
501
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000502 return decodeMetadataStringsBlob(Indent, Record, Blob);
Duncan P. N. Exon Smitha73e5092016-03-27 23:17:54 +0000503}
504
Chris Lattner32de6332007-04-29 08:31:14 +0000505/// ParseBlock - Read a block, updating statistics, etc.
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000506static bool ParseBlock(BitstreamCursor &Stream, BitstreamBlockInfo &BlockInfo,
507 unsigned BlockID, unsigned IndentLevel,
508 CurStreamTypeType CurStreamType) {
Chris Lattner1772b12d2007-05-05 00:17:42 +0000509 std::string Indent(IndentLevel*2, ' ');
Chris Lattner4238d472007-04-29 20:00:02 +0000510 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
Chris Lattnerb30c9252007-04-29 21:48:19 +0000511
Chris Lattner4238d472007-04-29 20:00:02 +0000512 // Get the statistics for this BlockID.
513 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000514
Chris Lattner4238d472007-04-29 20:00:02 +0000515 BlockStats.NumInstances++;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000516
Chris Lattner1772b12d2007-05-05 00:17:42 +0000517 // BLOCKINFO is a special part of the stream.
Richard Smith0f73f7a2016-02-06 00:46:09 +0000518 bool DumpRecords = Dump;
Chris Lattner1772b12d2007-05-05 00:17:42 +0000519 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000520 if (Dump) outs() << Indent << "<BLOCKINFO_BLOCK/>\n";
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000521 Optional<BitstreamBlockInfo> NewBlockInfo =
522 Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
523 if (!NewBlockInfo)
Zachary Turner93263082016-10-03 18:17:18 +0000524 return ReportError("Malformed BlockInfoBlock");
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000525 BlockInfo = std::move(*NewBlockInfo);
526 Stream.JumpToBit(BlockBitStart);
Richard Smith0f73f7a2016-02-06 00:46:09 +0000527 // It's not really interesting to dump the contents of the blockinfo block.
528 DumpRecords = false;
Chris Lattner1772b12d2007-05-05 00:17:42 +0000529 }
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000530
Chris Lattnerb30c9252007-04-29 21:48:19 +0000531 unsigned NumWords = 0;
Chris Lattner1772b12d2007-05-05 00:17:42 +0000532 if (Stream.EnterSubBlock(BlockID, &NumWords))
Zachary Turner93263082016-10-03 18:17:18 +0000533 return ReportError("Malformed block record");
Chris Lattner32de6332007-04-29 08:31:14 +0000534
Mehdi Aminid2f47012016-04-01 05:33:11 +0000535 // Keep it for later, when we see a MODULE_HASH record
536 uint64_t BlockEntryPos = Stream.getCurrentByteNo();
537
Craig Topper573faec2014-04-25 04:24:47 +0000538 const char *BlockName = nullptr;
Richard Smith0f73f7a2016-02-06 00:46:09 +0000539 if (DumpRecords) {
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000540 outs() << Indent << "<";
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000541 if ((BlockName = GetBlockName(BlockID, BlockInfo, CurStreamType)))
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000542 outs() << BlockName;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000543 else
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000544 outs() << "UnknownBlock" << BlockID;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000545
Chris Lattnerb30c9252007-04-29 21:48:19 +0000546 if (NonSymbolic && BlockName)
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000547 outs() << " BlockID=" << BlockID;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000548
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000549 outs() << " NumWords=" << NumWords
Chris Lattnerbc837202013-01-19 21:37:14 +0000550 << " BlockCodeSize=" << Stream.getAbbrevIDWidth() << ">\n";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000551 }
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000552
Chris Lattner32de6332007-04-29 08:31:14 +0000553 SmallVector<uint64_t, 64> Record;
554
Mehdi Amini89bf9692016-12-28 22:30:28 +0000555 // Keep the offset to the metadata index if seen.
556 uint64_t MetadataIndexOffset = 0;
557
Chris Lattner32de6332007-04-29 08:31:14 +0000558 // Read all the records for this block.
559 while (1) {
560 if (Stream.AtEndOfStream())
Zachary Turner93263082016-10-03 18:17:18 +0000561 return ReportError("Premature end of bitstream");
Chris Lattner32de6332007-04-29 08:31:14 +0000562
Chris Lattnerc167cac2009-04-27 18:15:27 +0000563 uint64_t RecordStartBit = Stream.GetCurrentBitNo();
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000564
Chris Lattner4156ca72013-01-20 02:50:32 +0000565 BitstreamEntry Entry =
566 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
567
568 switch (Entry.Kind) {
569 case BitstreamEntry::Error:
Zachary Turner93263082016-10-03 18:17:18 +0000570 return ReportError("malformed bitcode file");
Chris Lattner4156ca72013-01-20 02:50:32 +0000571 case BitstreamEntry::EndBlock: {
Chris Lattner4238d472007-04-29 20:00:02 +0000572 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
573 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
Richard Smith0f73f7a2016-02-06 00:46:09 +0000574 if (DumpRecords) {
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000575 outs() << Indent << "</";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000576 if (BlockName)
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000577 outs() << BlockName << ">\n";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000578 else
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000579 outs() << "UnknownBlock" << BlockID << ">\n";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000580 }
Chris Lattner32de6332007-04-29 08:31:14 +0000581 return false;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000582 }
Chris Lattner4156ca72013-01-20 02:50:32 +0000583
584 case BitstreamEntry::SubBlock: {
Chris Lattner44b0f102007-05-05 01:29:31 +0000585 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000586 if (ParseBlock(Stream, BlockInfo, Entry.ID, IndentLevel + 1,
587 CurStreamType))
Chris Lattner32de6332007-04-29 08:31:14 +0000588 return true;
Chris Lattner4238d472007-04-29 20:00:02 +0000589 ++BlockStats.NumSubBlocks;
Chris Lattner44b0f102007-05-05 01:29:31 +0000590 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
Chris Lattner4156ca72013-01-20 02:50:32 +0000591
Chris Lattner44b0f102007-05-05 01:29:31 +0000592 // Don't include subblock sizes in the size of this block.
593 BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
Chris Lattner4156ca72013-01-20 02:50:32 +0000594 continue;
595 }
596 case BitstreamEntry::Record:
597 // The interesting case.
Chris Lattner32de6332007-04-29 08:31:14 +0000598 break;
Chris Lattner44b0f102007-05-05 01:29:31 +0000599 }
Chris Lattner4156ca72013-01-20 02:50:32 +0000600
601 if (Entry.ID == bitc::DEFINE_ABBREV) {
Chris Lattner32de6332007-04-29 08:31:14 +0000602 Stream.ReadAbbrevRecord();
Chris Lattner4238d472007-04-29 20:00:02 +0000603 ++BlockStats.NumAbbrevs;
Chris Lattner4156ca72013-01-20 02:50:32 +0000604 continue;
605 }
606
607 Record.clear();
Chris Lattner3f75d312009-04-06 22:44:40 +0000608
Chris Lattner4156ca72013-01-20 02:50:32 +0000609 ++BlockStats.NumRecords;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000610
Chris Lattner4156ca72013-01-20 02:50:32 +0000611 StringRef Blob;
Teresa Johnson06b75972018-06-04 19:20:02 +0000612 uint64_t CurrentRecordPos = Stream.GetCurrentBitNo();
Chris Lattner4156ca72013-01-20 02:50:32 +0000613 unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000614
Chris Lattner4156ca72013-01-20 02:50:32 +0000615 // Increment the # occurrences of this code.
616 if (BlockStats.CodeFreq.size() <= Code)
617 BlockStats.CodeFreq.resize(Code+1);
618 BlockStats.CodeFreq[Code].NumInstances++;
619 BlockStats.CodeFreq[Code].TotalBits +=
620 Stream.GetCurrentBitNo()-RecordStartBit;
621 if (Entry.ID != bitc::UNABBREV_RECORD) {
622 BlockStats.CodeFreq[Code].NumAbbrev++;
623 ++BlockStats.NumAbbreviatedRecords;
624 }
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000625
Richard Smith0f73f7a2016-02-06 00:46:09 +0000626 if (DumpRecords) {
Chris Lattner4156ca72013-01-20 02:50:32 +0000627 outs() << Indent << " <";
628 if (const char *CodeName =
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000629 GetCodeName(Code, BlockID, BlockInfo, CurStreamType))
Chris Lattner4156ca72013-01-20 02:50:32 +0000630 outs() << CodeName;
631 else
632 outs() << "UnknownCode" << Code;
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000633 if (NonSymbolic && GetCodeName(Code, BlockID, BlockInfo, CurStreamType))
Chris Lattner4156ca72013-01-20 02:50:32 +0000634 outs() << " codeid=" << Code;
Teresa Johnson6b9f46e2015-10-08 15:56:24 +0000635 const BitCodeAbbrev *Abbv = nullptr;
636 if (Entry.ID != bitc::UNABBREV_RECORD) {
637 Abbv = Stream.getAbbrev(Entry.ID);
Chris Lattner4156ca72013-01-20 02:50:32 +0000638 outs() << " abbrevid=" << Entry.ID;
Teresa Johnson6b9f46e2015-10-08 15:56:24 +0000639 }
Chris Lattnerb30c9252007-04-29 21:48:19 +0000640
Chris Lattner4156ca72013-01-20 02:50:32 +0000641 for (unsigned i = 0, e = Record.size(); i != e; ++i)
642 outs() << " op" << i << "=" << (int64_t)Record[i];
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000643
Mehdi Amini89bf9692016-12-28 22:30:28 +0000644 // If we found a metadata index, let's verify that we had an offset before
645 // and validate its forward reference offset was correct!
646 if (BlockID == bitc::METADATA_BLOCK_ID) {
647 if (Code == bitc::METADATA_INDEX_OFFSET) {
Mehdi Amini6fc58a72016-12-28 23:45:54 +0000648 if (Record.size() != 2)
649 outs() << "(Invalid record)";
650 else {
651 auto Offset = Record[0] + (Record[1] << 32);
652 MetadataIndexOffset = Stream.GetCurrentBitNo() + Offset;
653 }
Mehdi Amini89bf9692016-12-28 22:30:28 +0000654 }
655 if (Code == bitc::METADATA_INDEX) {
656 outs() << " (offset ";
657 if (MetadataIndexOffset == RecordStartBit)
658 outs() << "match)";
659 else
660 outs() << "mismatch: " << MetadataIndexOffset << " vs "
661 << RecordStartBit << ")";
662 }
663 }
664
Mehdi Aminid2f47012016-04-01 05:33:11 +0000665 // If we found a module hash, let's verify that it matches!
Peter Collingbourne67a3f7f2017-07-06 17:56:01 +0000666 if (BlockID == bitc::MODULE_BLOCK_ID && Code == bitc::MODULE_CODE_HASH &&
667 !CheckHash.empty()) {
Mehdi Aminid2f47012016-04-01 05:33:11 +0000668 if (Record.size() != 5)
669 outs() << " (invalid)";
670 else {
671 // Recompute the hash and compare it to the one in the bitcode
672 SHA1 Hasher;
673 StringRef Hash;
Peter Collingbourne67a3f7f2017-07-06 17:56:01 +0000674 Hasher.update(CheckHash);
Mehdi Aminid2f47012016-04-01 05:33:11 +0000675 {
Peter Collingbourneca0a5982016-12-01 05:47:58 +0000676 int BlockSize = (CurrentRecordPos / 8) - BlockEntryPos;
Mehdi Aminid2f47012016-04-01 05:33:11 +0000677 auto Ptr = Stream.getPointerToByte(BlockEntryPos, BlockSize);
678 Hasher.update(ArrayRef<uint8_t>(Ptr, BlockSize));
679 Hash = Hasher.result();
680 }
681 SmallString<20> RecordedHash;
682 RecordedHash.resize(20);
683 int Pos = 0;
684 for (auto &Val : Record) {
685 assert(!(Val >> 32) && "Unexpected high bits set");
686 RecordedHash[Pos++] = (Val >> 24) & 0xFF;
687 RecordedHash[Pos++] = (Val >> 16) & 0xFF;
688 RecordedHash[Pos++] = (Val >> 8) & 0xFF;
689 RecordedHash[Pos++] = (Val >> 0) & 0xFF;
690 }
691 if (Hash == RecordedHash)
692 outs() << " (match)";
693 else
694 outs() << " (!mismatch!)";
695 }
696 }
697
Chris Lattner4156ca72013-01-20 02:50:32 +0000698 outs() << "/>";
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000699
Teresa Johnson6b9f46e2015-10-08 15:56:24 +0000700 if (Abbv) {
701 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
702 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
703 if (!Op.isEncoding() || Op.getEncoding() != BitCodeAbbrevOp::Array)
704 continue;
705 assert(i + 2 == e && "Array op not second to last");
706 std::string Str;
707 bool ArrayIsPrintable = true;
708 for (unsigned j = i - 1, je = Record.size(); j != je; ++j) {
Michael Kruse6069e662018-07-26 15:31:41 +0000709 if (!isPrint(static_cast<unsigned char>(Record[j]))) {
Teresa Johnson6b9f46e2015-10-08 15:56:24 +0000710 ArrayIsPrintable = false;
711 break;
712 }
713 Str += (char)Record[j];
714 }
Teresa Johnson9dd98c02015-11-02 18:02:11 +0000715 if (ArrayIsPrintable)
716 outs() << " record string = '" << Str << "'";
Teresa Johnson6b9f46e2015-10-08 15:56:24 +0000717 break;
718 }
719 }
720
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000721 if (Blob.data() && decodeBlob(Code, BlockID, Indent, Record, Blob)) {
Chris Lattner4156ca72013-01-20 02:50:32 +0000722 outs() << " blob data = ";
Jordan Rose18261972015-05-13 18:51:49 +0000723 if (ShowBinaryBlobs) {
724 outs() << "'";
725 outs().write_escaped(Blob, /*hex=*/true) << "'";
726 } else {
727 bool BlobIsPrintable = true;
728 for (unsigned i = 0, e = Blob.size(); i != e; ++i)
Michael Kruse6069e662018-07-26 15:31:41 +0000729 if (!isPrint(static_cast<unsigned char>(Blob[i]))) {
Jordan Rose18261972015-05-13 18:51:49 +0000730 BlobIsPrintable = false;
731 break;
732 }
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000733
Jordan Rose18261972015-05-13 18:51:49 +0000734 if (BlobIsPrintable)
735 outs() << "'" << Blob << "'";
736 else
737 outs() << "unprintable, " << Blob.size() << " bytes.";
738 }
Chris Lattnerb30c9252007-04-29 21:48:19 +0000739 }
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000740
Chris Lattner4156ca72013-01-20 02:50:32 +0000741 outs() << "\n";
Chris Lattner32de6332007-04-29 08:31:14 +0000742 }
Peter Collingbourneca0a5982016-12-01 05:47:58 +0000743
744 // Make sure that we can skip the current record.
745 Stream.JumpToBit(CurrentRecordPos);
746 Stream.skipRecord(Entry.ID);
Chris Lattner32de6332007-04-29 08:31:14 +0000747 }
748}
749
Chris Lattner4238d472007-04-29 20:00:02 +0000750static void PrintSize(double Bits) {
Jan Wen Voung851343c2012-09-05 20:55:54 +0000751 outs() << format("%.2f/%.2fB/%luW", Bits, Bits/8,(unsigned long)(Bits/32));
Chris Lattner24437472009-04-27 17:59:34 +0000752}
753static void PrintSize(uint64_t Bits) {
Jan Wen Voung851343c2012-09-05 20:55:54 +0000754 outs() << format("%lub/%.2fB/%luW", (unsigned long)Bits,
755 (double)Bits/8, (unsigned long)(Bits/32));
Chris Lattner4238d472007-04-29 20:00:02 +0000756}
757
Brian Gesiak37f13782018-04-21 23:52:04 +0000758static CurStreamTypeType ReadSignature(BitstreamCursor &Stream) {
759 char Signature[6];
760 Signature[0] = Stream.Read(8);
761 Signature[1] = Stream.Read(8);
762
763 // Autodetect the file contents, if it is one we know.
764 if (Signature[0] == 'C' && Signature[1] == 'P') {
765 Signature[2] = Stream.Read(8);
766 Signature[3] = Stream.Read(8);
767 if (Signature[2] == 'C' && Signature[3] == 'H')
768 return ClangSerializedASTBitstream;
769 } else if (Signature[0] == 'D' && Signature[1] == 'I') {
770 Signature[2] = Stream.Read(8);
771 Signature[3] = Stream.Read(8);
772 if (Signature[2] == 'A' && Signature[3] == 'G')
773 return ClangSerializedDiagnosticsBitstream;
774 } else {
775 Signature[2] = Stream.Read(4);
776 Signature[3] = Stream.Read(4);
777 Signature[4] = Stream.Read(4);
778 Signature[5] = Stream.Read(4);
779 if (Signature[0] == 'B' && Signature[1] == 'C' &&
780 Signature[2] == 0x0 && Signature[3] == 0xC &&
781 Signature[4] == 0xE && Signature[5] == 0xD)
782 return LLVMIRBitstream;
783 }
784 return UnknownBitstream;
785}
786
Jordan Rosed4210752014-08-30 17:07:55 +0000787static bool openBitcodeFile(StringRef Path,
788 std::unique_ptr<MemoryBuffer> &MemBuf,
Jordan Rosed4210752014-08-30 17:07:55 +0000789 BitstreamCursor &Stream,
790 CurStreamTypeType &CurStreamType) {
Chris Lattner45e0f892007-04-29 08:12:22 +0000791 // Read the input file.
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000792 ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr =
Jordan Rosed4210752014-08-30 17:07:55 +0000793 MemoryBuffer::getFileOrSTDIN(Path);
Rafael Espindola7cba2a92014-07-06 17:43:13 +0000794 if (std::error_code EC = MemBufOrErr.getError())
Zachary Turner93263082016-10-03 18:17:18 +0000795 return ReportError(Twine("ReportError reading '") + Path + "': " + EC.message());
Jordan Rosed4210752014-08-30 17:07:55 +0000796 MemBuf = std::move(MemBufOrErr.get());
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000797
Jordan Rosed4210752014-08-30 17:07:55 +0000798 if (MemBuf->getBufferSize() & 3)
Zachary Turner93263082016-10-03 18:17:18 +0000799 return ReportError("Bitcode stream should be a multiple of 4 bytes in length");
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000800
Jordan Rosed4210752014-08-30 17:07:55 +0000801 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart();
802 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize();
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000803
Chris Lattnere2a466b2009-04-06 20:54:32 +0000804 // If we have a wrapper header, parse it and ignore the non-bc file contents.
805 // The magic number is 0x0B17C0DE stored in little endian.
Akira Hatanaka0dc389d2016-01-29 05:55:09 +0000806 if (isBitcodeWrapper(BufPtr, EndBufPtr)) {
Mehdi Aminid4b10212016-04-01 05:19:14 +0000807 if (MemBuf->getBufferSize() < BWH_HeaderSize)
Zachary Turner93263082016-10-03 18:17:18 +0000808 return ReportError("Invalid bitcode wrapper header");
Akira Hatanaka0dc389d2016-01-29 05:55:09 +0000809
810 if (Dump) {
811 unsigned Magic = support::endian::read32le(&BufPtr[BWH_MagicField]);
812 unsigned Version = support::endian::read32le(&BufPtr[BWH_VersionField]);
813 unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]);
814 unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]);
815 unsigned CPUType = support::endian::read32le(&BufPtr[BWH_CPUTypeField]);
816
817 outs() << "<BITCODE_WRAPPER_HEADER"
818 << " Magic=" << format_hex(Magic, 10)
819 << " Version=" << format_hex(Version, 10)
820 << " Offset=" << format_hex(Offset, 10)
821 << " Size=" << format_hex(Size, 10)
822 << " CPUType=" << format_hex(CPUType, 10) << "/>\n";
823 }
824
Derek Schuff2ea93872012-02-06 22:30:29 +0000825 if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr, true))
Zachary Turner93263082016-10-03 18:17:18 +0000826 return ReportError("Invalid bitcode wrapper header");
Akira Hatanaka0dc389d2016-01-29 05:55:09 +0000827 }
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000828
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000829 Stream = BitstreamCursor(ArrayRef<uint8_t>(BufPtr, EndBufPtr));
Brian Gesiak37f13782018-04-21 23:52:04 +0000830 CurStreamType = ReadSignature(Stream);
Chris Lattner45e0f892007-04-29 08:12:22 +0000831
Jordan Rosed4210752014-08-30 17:07:55 +0000832 return false;
833}
834
835/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
836static int AnalyzeBitcode() {
837 std::unique_ptr<MemoryBuffer> StreamBuffer;
Jordan Rosed4210752014-08-30 17:07:55 +0000838 BitstreamCursor Stream;
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000839 BitstreamBlockInfo BlockInfo;
Jordan Rosed4210752014-08-30 17:07:55 +0000840 CurStreamTypeType CurStreamType;
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000841 if (openBitcodeFile(InputFilename, StreamBuffer, Stream, CurStreamType))
Jordan Rosed4210752014-08-30 17:07:55 +0000842 return true;
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000843 Stream.setBlockInfo(&BlockInfo);
Jordan Rosed4210752014-08-30 17:07:55 +0000844
845 // Read block info from BlockInfoFilename, if specified.
846 // The block info must be a top-level block.
847 if (!BlockInfoFilename.empty()) {
848 std::unique_ptr<MemoryBuffer> BlockInfoBuffer;
Jordan Rosed4210752014-08-30 17:07:55 +0000849 BitstreamCursor BlockInfoCursor;
850 CurStreamTypeType BlockInfoStreamType;
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000851 if (openBitcodeFile(BlockInfoFilename, BlockInfoBuffer, BlockInfoCursor,
852 BlockInfoStreamType))
Jordan Rosed4210752014-08-30 17:07:55 +0000853 return true;
854
855 while (!BlockInfoCursor.AtEndOfStream()) {
856 unsigned Code = BlockInfoCursor.ReadCode();
857 if (Code != bitc::ENTER_SUBBLOCK)
Zachary Turner93263082016-10-03 18:17:18 +0000858 return ReportError("Invalid record at top-level in block info file");
Jordan Rosed4210752014-08-30 17:07:55 +0000859
860 unsigned BlockID = BlockInfoCursor.ReadSubBlockID();
861 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000862 Optional<BitstreamBlockInfo> NewBlockInfo =
863 BlockInfoCursor.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
864 if (!NewBlockInfo)
Zachary Turner93263082016-10-03 18:17:18 +0000865 return ReportError("Malformed BlockInfoBlock in block info file");
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000866 BlockInfo = std::move(*NewBlockInfo);
Jordan Rosed4210752014-08-30 17:07:55 +0000867 break;
868 }
869
870 BlockInfoCursor.SkipBlock();
871 }
Jordan Rosed4210752014-08-30 17:07:55 +0000872 }
873
Chris Lattner4238d472007-04-29 20:00:02 +0000874 unsigned NumTopBlocks = 0;
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000875
Chris Lattner32de6332007-04-29 08:31:14 +0000876 // Parse the top-level structure. We only allow blocks at the top-level.
877 while (!Stream.AtEndOfStream()) {
878 unsigned Code = Stream.ReadCode();
879 if (Code != bitc::ENTER_SUBBLOCK)
Zachary Turner93263082016-10-03 18:17:18 +0000880 return ReportError("Invalid record at top-level");
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000881
Chris Lattner4156ca72013-01-20 02:50:32 +0000882 unsigned BlockID = Stream.ReadSubBlockID();
883
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000884 if (ParseBlock(Stream, BlockInfo, BlockID, 0, CurStreamType))
Chris Lattner32de6332007-04-29 08:31:14 +0000885 return true;
Chris Lattner4238d472007-04-29 20:00:02 +0000886 ++NumTopBlocks;
Chris Lattner32de6332007-04-29 08:31:14 +0000887 }
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000888
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000889 if (Dump) outs() << "\n\n";
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000890
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000891 uint64_t BufferSizeBits = Stream.getBitcodeBytes().size() * CHAR_BIT;
Chris Lattner32de6332007-04-29 08:31:14 +0000892 // Print a summary of the read file.
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000893 outs() << "Summary of " << InputFilename << ":\n";
894 outs() << " Total size: ";
Chris Lattner8f926682007-05-01 02:43:46 +0000895 PrintSize(BufferSizeBits);
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000896 outs() << "\n";
897 outs() << " Stream type: ";
Chris Lattner45e0f892007-04-29 08:12:22 +0000898 switch (CurStreamType) {
Brian Gesiak37f13782018-04-21 23:52:04 +0000899 case UnknownBitstream:
900 outs() << "unknown\n";
901 break;
902 case LLVMIRBitstream:
903 outs() << "LLVM IR\n";
904 break;
905 case ClangSerializedASTBitstream:
906 outs() << "Clang Serialized AST\n";
907 break;
908 case ClangSerializedDiagnosticsBitstream:
909 outs() << "Clang Serialized Diagnostics\n";
910 break;
Chris Lattner45e0f892007-04-29 08:12:22 +0000911 }
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000912 outs() << " # Toplevel Blocks: " << NumTopBlocks << "\n";
913 outs() << "\n";
Chris Lattner4238d472007-04-29 20:00:02 +0000914
915 // Emit per-block stats.
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000916 outs() << "Per-block Summary:\n";
Chris Lattner4238d472007-04-29 20:00:02 +0000917 for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
918 E = BlockIDStats.end(); I != E; ++I) {
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000919 outs() << " Block ID #" << I->first;
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000920 if (const char *BlockName =
921 GetBlockName(I->first, BlockInfo, CurStreamType))
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000922 outs() << " (" << BlockName << ")";
923 outs() << ":\n";
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000924
Chris Lattner4238d472007-04-29 20:00:02 +0000925 const PerBlockIDStats &Stats = I->second;
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000926 outs() << " Num Instances: " << Stats.NumInstances << "\n";
927 outs() << " Total Size: ";
Chris Lattner4238d472007-04-29 20:00:02 +0000928 PrintSize(Stats.NumBits);
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000929 outs() << "\n";
Daniel Dunbar42985bb2009-09-25 16:04:21 +0000930 double pct = (Stats.NumBits * 100.0) / BufferSizeBits;
Jan Wen Voung851343c2012-09-05 20:55:54 +0000931 outs() << " Percent of file: " << format("%2.4f%%", pct) << "\n";
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000932 if (Stats.NumInstances > 1) {
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000933 outs() << " Average Size: ";
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000934 PrintSize(Stats.NumBits/(double)Stats.NumInstances);
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000935 outs() << "\n";
936 outs() << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
Dan Gohman65f57c22009-07-15 16:35:29 +0000937 << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000938 outs() << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
Dan Gohman65f57c22009-07-15 16:35:29 +0000939 << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000940 outs() << " Tot/Avg Records: " << Stats.NumRecords << "/"
Dan Gohman65f57c22009-07-15 16:35:29 +0000941 << Stats.NumRecords/(double)Stats.NumInstances << "\n";
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000942 } else {
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000943 outs() << " Num SubBlocks: " << Stats.NumSubBlocks << "\n";
944 outs() << " Num Abbrevs: " << Stats.NumAbbrevs << "\n";
945 outs() << " Num Records: " << Stats.NumRecords << "\n";
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000946 }
Daniel Dunbar42985bb2009-09-25 16:04:21 +0000947 if (Stats.NumRecords) {
948 double pct = (Stats.NumAbbreviatedRecords * 100.0) / Stats.NumRecords;
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000949 outs() << " Percent Abbrevs: " << format("%2.4f%%", pct) << "\n";
Daniel Dunbar42985bb2009-09-25 16:04:21 +0000950 }
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000951 outs() << "\n";
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000952
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000953 // Print a histogram of the codes we see.
954 if (!NoHistogram && !Stats.CodeFreq.empty()) {
Mehdi Amini33ee9972015-10-21 06:10:55 +0000955 std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code>
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000956 for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
Mehdi Amini33ee9972015-10-21 06:10:55 +0000957 if (unsigned Freq = Stats.CodeFreq[i].NumInstances)
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000958 FreqPairs.push_back(std::make_pair(Freq, i));
959 std::stable_sort(FreqPairs.begin(), FreqPairs.end());
960 std::reverse(FreqPairs.begin(), FreqPairs.end());
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000961
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000962 outs() << "\tRecord Histogram:\n";
Richard Smith0f73f7a2016-02-06 00:46:09 +0000963 outs() << "\t\t Count # Bits b/Rec % Abv Record Kind\n";
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000964 for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
Chris Lattnerc167cac2009-04-27 18:15:27 +0000965 const PerRecordStats &RecStats = Stats.CodeFreq[FreqPairs[i].second];
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000966
Mehdi Amini33ee9972015-10-21 06:10:55 +0000967 outs() << format("\t\t%7d %9lu",
Jan Wen Voung73e562a2012-09-05 20:55:57 +0000968 RecStats.NumInstances,
969 (unsigned long)RecStats.TotalBits);
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000970
Richard Smith0f73f7a2016-02-06 00:46:09 +0000971 if (RecStats.NumInstances > 1)
972 outs() << format(" %9.1f",
973 (double)RecStats.TotalBits/RecStats.NumInstances);
974 else
975 outs() << " ";
976
Chris Lattnerc167cac2009-04-27 18:15:27 +0000977 if (RecStats.NumAbbrev)
Jan Wen Voung851343c2012-09-05 20:55:54 +0000978 outs() <<
Richard Smith0f73f7a2016-02-06 00:46:09 +0000979 format(" %7.2f",
Jan Wen Voung851343c2012-09-05 20:55:54 +0000980 (double)RecStats.NumAbbrev/RecStats.NumInstances*100);
Chris Lattnerc167cac2009-04-27 18:15:27 +0000981 else
Richard Smith0f73f7a2016-02-06 00:46:09 +0000982 outs() << " ";
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000983
Richard Smith0f73f7a2016-02-06 00:46:09 +0000984 outs() << " ";
Peter Collingbourne8fc9b4d2016-11-08 04:17:11 +0000985 if (const char *CodeName = GetCodeName(FreqPairs[i].second, I->first,
986 BlockInfo, CurStreamType))
Jan Wen Voung851343c2012-09-05 20:55:54 +0000987 outs() << CodeName << "\n";
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000988 else
Jan Wen Voung851343c2012-09-05 20:55:54 +0000989 outs() << "UnknownCode" << FreqPairs[i].second << "\n";
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000990 }
Chris Lattnere0ac6f82012-03-19 23:40:48 +0000991 outs() << "\n";
Daniel Dunbar80ba3d22009-09-25 16:03:57 +0000992
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000993 }
Chris Lattner4238d472007-04-29 20:00:02 +0000994 }
Chris Lattner45e0f892007-04-29 08:12:22 +0000995 return 0;
996}
Reid Spencerdac69c82004-06-07 17:53:43 +0000997
Chris Lattner32de6332007-04-29 08:31:14 +0000998
Chris Lattnerc30598b2006-12-06 01:18:01 +0000999int main(int argc, char **argv) {
Rui Ueyama0b9d56a2018-04-13 18:26:06 +00001000 InitLLVM X(argc, argv);
Chris Lattnercc14d252009-03-06 05:34:10 +00001001 cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
Chris Lattner44dadff2007-05-06 09:29:57 +00001002 return AnalyzeBitcode();
Reid Spencerdac69c82004-06-07 17:53:43 +00001003}