blob: b84c4a83dee459fbe6a703f7365412c4843c2c45 [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";
124 case bitc::MODULE_STRTAB_BLOCK_ID: return "MODULE_STRTAB_BLOCK";
Chris Lattner1684cee2007-04-29 20:00:02 +0000125 }
126}
127
Chris Lattner3543caa2007-04-29 21:48:19 +0000128/// GetCodeName - Return a symbolic code name if known, otherwise return
129/// null.
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000130static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000131 const BitstreamBlockInfo &BlockInfo,
Jordan Rose88eb5342014-08-30 17:07:55 +0000132 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +0000133 // Standard blocks for all bitcode files.
134 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
135 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
136 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000137 default: return nullptr;
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000138 case bitc::BLOCKINFO_CODE_SETBID: return "SETBID";
139 case bitc::BLOCKINFO_CODE_BLOCKNAME: return "BLOCKNAME";
140 case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME";
Chris Lattner9181ddf2007-05-05 00:17:42 +0000141 }
142 }
Craig Toppere6cb63e2014-04-25 04:24:47 +0000143 return nullptr;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000144 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000145
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000146 // Check to see if we have a blockinfo record for this record, with a name.
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000147 if (const BitstreamBlockInfo::BlockInfo *Info =
148 BlockInfo.getBlockInfo(BlockID)) {
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000149 for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i)
150 if (Info->RecordNames[i].first == CodeID)
151 return Info->RecordNames[i].second.c_str();
152 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000153
154
Craig Toppere6cb63e2014-04-25 04:24:47 +0000155 if (CurStreamType != LLVMIRBitstream) return nullptr;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000156
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000157#define STRINGIFY_CODE(PREFIX, CODE) \
158 case bitc::PREFIX##_##CODE: \
159 return #CODE;
Chris Lattner3543caa2007-04-29 21:48:19 +0000160 switch (BlockID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000161 default: return nullptr;
Chris Lattner3543caa2007-04-29 21:48:19 +0000162 case bitc::MODULE_BLOCK_ID:
163 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000164 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000165 STRINGIFY_CODE(MODULE_CODE, VERSION)
166 STRINGIFY_CODE(MODULE_CODE, TRIPLE)
167 STRINGIFY_CODE(MODULE_CODE, DATALAYOUT)
168 STRINGIFY_CODE(MODULE_CODE, ASM)
169 STRINGIFY_CODE(MODULE_CODE, SECTIONNAME)
170 STRINGIFY_CODE(MODULE_CODE, DEPLIB) // FIXME: Remove in 4.0
171 STRINGIFY_CODE(MODULE_CODE, GLOBALVAR)
172 STRINGIFY_CODE(MODULE_CODE, FUNCTION)
173 STRINGIFY_CODE(MODULE_CODE, ALIAS)
174 STRINGIFY_CODE(MODULE_CODE, PURGEVALS)
175 STRINGIFY_CODE(MODULE_CODE, GCNAME)
Teresa Johnsonff642b92015-09-17 20:12:00 +0000176 STRINGIFY_CODE(MODULE_CODE, VSTOFFSET)
Duncan P. N. Exon Smith68f56242016-03-25 01:29:50 +0000177 STRINGIFY_CODE(MODULE_CODE, METADATA_VALUES_UNUSED)
Teresa Johnsone1164de2016-02-10 21:55:02 +0000178 STRINGIFY_CODE(MODULE_CODE, SOURCE_FILENAME)
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000179 STRINGIFY_CODE(MODULE_CODE, HASH)
Chris Lattner3543caa2007-04-29 21:48:19 +0000180 }
Mehdi Amini5d303282015-10-26 18:37:00 +0000181 case bitc::IDENTIFICATION_BLOCK_ID:
182 switch (CodeID) {
183 default:
184 return nullptr;
185 STRINGIFY_CODE(IDENTIFICATION_CODE, STRING)
186 STRINGIFY_CODE(IDENTIFICATION_CODE, EPOCH)
187 }
Chris Lattner0b7c5122007-05-04 03:01:41 +0000188 case bitc::PARAMATTR_BLOCK_ID:
189 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000190 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000191 // FIXME: Should these be different?
Bill Wendlingd7e05d62013-02-10 23:17:10 +0000192 case bitc::PARAMATTR_CODE_ENTRY_OLD: return "ENTRY";
193 case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY";
Justin Bogner68b28d02016-03-15 22:37:25 +0000194 }
195 case bitc::PARAMATTR_GROUP_BLOCK_ID:
196 switch (CodeID) {
197 default: return nullptr;
Bill Wendlingd7e05d62013-02-10 23:17:10 +0000198 case bitc::PARAMATTR_GRP_CODE_ENTRY: return "ENTRY";
Chris Lattner0b7c5122007-05-04 03:01:41 +0000199 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000200 case bitc::TYPE_BLOCK_ID_NEW:
Chris Lattner3543caa2007-04-29 21:48:19 +0000201 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000202 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000203 STRINGIFY_CODE(TYPE_CODE, NUMENTRY)
204 STRINGIFY_CODE(TYPE_CODE, VOID)
205 STRINGIFY_CODE(TYPE_CODE, FLOAT)
206 STRINGIFY_CODE(TYPE_CODE, DOUBLE)
207 STRINGIFY_CODE(TYPE_CODE, LABEL)
208 STRINGIFY_CODE(TYPE_CODE, OPAQUE)
209 STRINGIFY_CODE(TYPE_CODE, INTEGER)
210 STRINGIFY_CODE(TYPE_CODE, POINTER)
211 STRINGIFY_CODE(TYPE_CODE, ARRAY)
212 STRINGIFY_CODE(TYPE_CODE, VECTOR)
213 STRINGIFY_CODE(TYPE_CODE, X86_FP80)
214 STRINGIFY_CODE(TYPE_CODE, FP128)
215 STRINGIFY_CODE(TYPE_CODE, PPC_FP128)
216 STRINGIFY_CODE(TYPE_CODE, METADATA)
217 STRINGIFY_CODE(TYPE_CODE, STRUCT_ANON)
218 STRINGIFY_CODE(TYPE_CODE, STRUCT_NAME)
219 STRINGIFY_CODE(TYPE_CODE, STRUCT_NAMED)
220 STRINGIFY_CODE(TYPE_CODE, FUNCTION)
Chris Lattner3543caa2007-04-29 21:48:19 +0000221 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000222
Chris Lattner3543caa2007-04-29 21:48:19 +0000223 case bitc::CONSTANTS_BLOCK_ID:
224 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000225 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000226 STRINGIFY_CODE(CST_CODE, SETTYPE)
227 STRINGIFY_CODE(CST_CODE, NULL)
228 STRINGIFY_CODE(CST_CODE, UNDEF)
229 STRINGIFY_CODE(CST_CODE, INTEGER)
230 STRINGIFY_CODE(CST_CODE, WIDE_INTEGER)
231 STRINGIFY_CODE(CST_CODE, FLOAT)
232 STRINGIFY_CODE(CST_CODE, AGGREGATE)
233 STRINGIFY_CODE(CST_CODE, STRING)
234 STRINGIFY_CODE(CST_CODE, CSTRING)
235 STRINGIFY_CODE(CST_CODE, CE_BINOP)
236 STRINGIFY_CODE(CST_CODE, CE_CAST)
237 STRINGIFY_CODE(CST_CODE, CE_GEP)
238 STRINGIFY_CODE(CST_CODE, CE_INBOUNDS_GEP)
239 STRINGIFY_CODE(CST_CODE, CE_SELECT)
240 STRINGIFY_CODE(CST_CODE, CE_EXTRACTELT)
241 STRINGIFY_CODE(CST_CODE, CE_INSERTELT)
242 STRINGIFY_CODE(CST_CODE, CE_SHUFFLEVEC)
243 STRINGIFY_CODE(CST_CODE, CE_CMP)
244 STRINGIFY_CODE(CST_CODE, INLINEASM)
245 STRINGIFY_CODE(CST_CODE, CE_SHUFVEC_EX)
Chris Lattner372dd1e2012-01-30 00:51:16 +0000246 case bitc::CST_CODE_BLOCKADDRESS: return "CST_CODE_BLOCKADDRESS";
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000247 STRINGIFY_CODE(CST_CODE, DATA)
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000248 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000249 case bitc::FUNCTION_BLOCK_ID:
250 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000251 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000252 STRINGIFY_CODE(FUNC_CODE, DECLAREBLOCKS)
253 STRINGIFY_CODE(FUNC_CODE, INST_BINOP)
254 STRINGIFY_CODE(FUNC_CODE, INST_CAST)
255 STRINGIFY_CODE(FUNC_CODE, INST_GEP_OLD)
256 STRINGIFY_CODE(FUNC_CODE, INST_INBOUNDS_GEP_OLD)
257 STRINGIFY_CODE(FUNC_CODE, INST_SELECT)
258 STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTELT)
259 STRINGIFY_CODE(FUNC_CODE, INST_INSERTELT)
260 STRINGIFY_CODE(FUNC_CODE, INST_SHUFFLEVEC)
261 STRINGIFY_CODE(FUNC_CODE, INST_CMP)
262 STRINGIFY_CODE(FUNC_CODE, INST_RET)
263 STRINGIFY_CODE(FUNC_CODE, INST_BR)
264 STRINGIFY_CODE(FUNC_CODE, INST_SWITCH)
265 STRINGIFY_CODE(FUNC_CODE, INST_INVOKE)
266 STRINGIFY_CODE(FUNC_CODE, INST_UNREACHABLE)
David Majnemer654e1302015-07-31 17:58:14 +0000267 STRINGIFY_CODE(FUNC_CODE, INST_CLEANUPRET)
268 STRINGIFY_CODE(FUNC_CODE, INST_CATCHRET)
269 STRINGIFY_CODE(FUNC_CODE, INST_CATCHPAD)
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000270 STRINGIFY_CODE(FUNC_CODE, INST_PHI)
271 STRINGIFY_CODE(FUNC_CODE, INST_ALLOCA)
272 STRINGIFY_CODE(FUNC_CODE, INST_LOAD)
273 STRINGIFY_CODE(FUNC_CODE, INST_VAARG)
274 STRINGIFY_CODE(FUNC_CODE, INST_STORE)
275 STRINGIFY_CODE(FUNC_CODE, INST_EXTRACTVAL)
276 STRINGIFY_CODE(FUNC_CODE, INST_INSERTVAL)
277 STRINGIFY_CODE(FUNC_CODE, INST_CMP2)
278 STRINGIFY_CODE(FUNC_CODE, INST_VSELECT)
279 STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC_AGAIN)
280 STRINGIFY_CODE(FUNC_CODE, INST_CALL)
281 STRINGIFY_CODE(FUNC_CODE, DEBUG_LOC)
282 STRINGIFY_CODE(FUNC_CODE, INST_GEP)
Sanjoy Das51df5fa2016-04-26 05:59:08 +0000283 STRINGIFY_CODE(FUNC_CODE, OPERAND_BUNDLE)
Chris Lattner3543caa2007-04-29 21:48:19 +0000284 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000285 case bitc::VALUE_SYMTAB_BLOCK_ID:
286 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000287 default: return nullptr;
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000288 STRINGIFY_CODE(VST_CODE, ENTRY)
289 STRINGIFY_CODE(VST_CODE, BBENTRY)
Teresa Johnsonff642b92015-09-17 20:12:00 +0000290 STRINGIFY_CODE(VST_CODE, FNENTRY)
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000291 STRINGIFY_CODE(VST_CODE, COMBINED_ENTRY)
Teresa Johnson403a7872015-10-04 14:33:43 +0000292 }
293 case bitc::MODULE_STRTAB_BLOCK_ID:
294 switch (CodeID) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000295 default:
296 return nullptr;
297 STRINGIFY_CODE(MST_CODE, ENTRY)
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000298 STRINGIFY_CODE(MST_CODE, HASH)
Teresa Johnson403a7872015-10-04 14:33:43 +0000299 }
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000300 case bitc::GLOBALVAL_SUMMARY_BLOCK_ID:
Teresa Johnson403a7872015-10-04 14:33:43 +0000301 switch (CodeID) {
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000302 default:
303 return nullptr;
Teresa Johnson76a1c1d2016-03-11 18:52:24 +0000304 STRINGIFY_CODE(FS, PERMODULE)
305 STRINGIFY_CODE(FS, PERMODULE_PROFILE)
306 STRINGIFY_CODE(FS, PERMODULE_GLOBALVAR_INIT_REFS)
307 STRINGIFY_CODE(FS, COMBINED)
308 STRINGIFY_CODE(FS, COMBINED_PROFILE)
309 STRINGIFY_CODE(FS, COMBINED_GLOBALVAR_INIT_REFS)
Mehdi Amini2d28f7a2016-04-16 06:56:44 +0000310 STRINGIFY_CODE(FS, ALIAS)
311 STRINGIFY_CODE(FS, COMBINED_ALIAS)
Mehdi Aminiae64eaf2016-04-23 23:38:17 +0000312 STRINGIFY_CODE(FS, COMBINED_ORIGINAL_NAME)
Mehdi Amini8fe69362016-04-24 03:18:11 +0000313 STRINGIFY_CODE(FS, VERSION)
Peter Collingbourne1b4137a72016-12-21 23:03:45 +0000314 STRINGIFY_CODE(FS, TYPE_TESTS)
Chris Lattner3543caa2007-04-29 21:48:19 +0000315 }
Devang Patelaf206b82009-09-18 19:26:43 +0000316 case bitc::METADATA_ATTACHMENT_ID:
317 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000318 default:return nullptr;
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000319 STRINGIFY_CODE(METADATA, ATTACHMENT)
Devang Patelaf206b82009-09-18 19:26:43 +0000320 }
Devang Patel7428d8a2009-07-22 17:43:22 +0000321 case bitc::METADATA_BLOCK_ID:
322 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000323 default:return nullptr;
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000324 STRINGIFY_CODE(METADATA, STRING_OLD)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000325 STRINGIFY_CODE(METADATA, VALUE)
Adrian Prantl36daf632017-01-03 19:17:49 +0000326 STRINGIFY_CODE(METADATA, NODE)
327 STRINGIFY_CODE(METADATA, NAME)
328 STRINGIFY_CODE(METADATA, DISTINCT_NODE)
329 STRINGIFY_CODE(METADATA, KIND) // Older bitcode has it in a MODULE_BLOCK
330 STRINGIFY_CODE(METADATA, LOCATION)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000331 STRINGIFY_CODE(METADATA, OLD_NODE)
332 STRINGIFY_CODE(METADATA, OLD_FN_NODE)
333 STRINGIFY_CODE(METADATA, NAMED_NODE)
Duncan P. N. Exon Smith706b80d2015-06-29 22:50:35 +0000334 STRINGIFY_CODE(METADATA, GENERIC_DEBUG)
335 STRINGIFY_CODE(METADATA, SUBRANGE)
336 STRINGIFY_CODE(METADATA, ENUMERATOR)
337 STRINGIFY_CODE(METADATA, BASIC_TYPE)
338 STRINGIFY_CODE(METADATA, FILE)
339 STRINGIFY_CODE(METADATA, DERIVED_TYPE)
340 STRINGIFY_CODE(METADATA, COMPOSITE_TYPE)
341 STRINGIFY_CODE(METADATA, SUBROUTINE_TYPE)
342 STRINGIFY_CODE(METADATA, COMPILE_UNIT)
343 STRINGIFY_CODE(METADATA, SUBPROGRAM)
344 STRINGIFY_CODE(METADATA, LEXICAL_BLOCK)
345 STRINGIFY_CODE(METADATA, LEXICAL_BLOCK_FILE)
346 STRINGIFY_CODE(METADATA, NAMESPACE)
347 STRINGIFY_CODE(METADATA, TEMPLATE_TYPE)
348 STRINGIFY_CODE(METADATA, TEMPLATE_VALUE)
349 STRINGIFY_CODE(METADATA, GLOBAL_VAR)
350 STRINGIFY_CODE(METADATA, LOCAL_VAR)
351 STRINGIFY_CODE(METADATA, EXPRESSION)
352 STRINGIFY_CODE(METADATA, OBJC_PROPERTY)
353 STRINGIFY_CODE(METADATA, IMPORTED_ENTITY)
Adrian Prantla7ad09d2015-06-30 00:25:41 +0000354 STRINGIFY_CODE(METADATA, MODULE)
Adrian Prantl36daf632017-01-03 19:17:49 +0000355 STRINGIFY_CODE(METADATA, MACRO)
356 STRINGIFY_CODE(METADATA, MACRO_FILE)
357 STRINGIFY_CODE(METADATA, STRINGS)
358 STRINGIFY_CODE(METADATA, GLOBAL_DECL_ATTACHMENT)
359 STRINGIFY_CODE(METADATA, GLOBAL_VAR_EXPR)
Mehdi Aminie98f9252016-12-28 22:30:28 +0000360 STRINGIFY_CODE(METADATA, INDEX_OFFSET)
361 STRINGIFY_CODE(METADATA, INDEX)
Devang Patel7428d8a2009-07-22 17:43:22 +0000362 }
Teresa Johnson12545072015-11-15 02:00:09 +0000363 case bitc::METADATA_KIND_BLOCK_ID:
364 switch (CodeID) {
365 default:
366 return nullptr;
367 STRINGIFY_CODE(METADATA, KIND)
368 }
Chad Rosierdd4ffae2011-12-07 21:45:13 +0000369 case bitc::USELIST_BLOCK_ID:
370 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000371 default:return nullptr;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000372 case bitc::USELIST_CODE_DEFAULT: return "USELIST_CODE_DEFAULT";
373 case bitc::USELIST_CODE_BB: return "USELIST_CODE_BB";
Chad Rosierdd4ffae2011-12-07 21:45:13 +0000374 }
Sanjoy Das51df5fa2016-04-26 05:59:08 +0000375
376 case bitc::OPERAND_BUNDLE_TAGS_BLOCK_ID:
377 switch(CodeID) {
378 default: return nullptr;
379 case bitc::OPERAND_BUNDLE_TAG: return "OPERAND_BUNDLE_TAG";
380 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000381 }
Duncan P. N. Exon Smithcb8ee002015-06-29 22:50:32 +0000382#undef STRINGIFY_CODE
Chris Lattner3543caa2007-04-29 21:48:19 +0000383}
384
Chris Lattnerbf419a92009-04-27 17:59:34 +0000385struct PerRecordStats {
386 unsigned NumInstances;
Chris Lattner1cf80692009-04-27 18:15:27 +0000387 unsigned NumAbbrev;
388 uint64_t TotalBits;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000389
Mehdi Aminid2638562015-10-21 06:10:55 +0000390 PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
Chris Lattnerbf419a92009-04-27 17:59:34 +0000391};
Chris Lattner1684cee2007-04-29 20:00:02 +0000392
393struct PerBlockIDStats {
394 /// NumInstances - This the number of times this block ID has been seen.
395 unsigned NumInstances;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000396
Chris Lattner1684cee2007-04-29 20:00:02 +0000397 /// NumBits - The total size in bits of all of these blocks.
398 uint64_t NumBits;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000399
Chris Lattner1684cee2007-04-29 20:00:02 +0000400 /// NumSubBlocks - The total number of blocks these blocks contain.
401 unsigned NumSubBlocks;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000402
Chris Lattner1684cee2007-04-29 20:00:02 +0000403 /// NumAbbrevs - The total number of abbreviations.
404 unsigned NumAbbrevs;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000405
406 /// NumRecords - The total number of records these blocks contain, and the
Chris Lattner1684cee2007-04-29 20:00:02 +0000407 /// number that are abbreviated.
408 unsigned NumRecords, NumAbbreviatedRecords;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000409
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000410 /// CodeFreq - Keep track of the number of times we see each code.
Chris Lattnerbf419a92009-04-27 17:59:34 +0000411 std::vector<PerRecordStats> CodeFreq;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000412
Chris Lattner1684cee2007-04-29 20:00:02 +0000413 PerBlockIDStats()
414 : NumInstances(0), NumBits(0),
415 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
416};
417
418static std::map<unsigned, PerBlockIDStats> BlockIDStats;
419
420
421
Zachary Turner2ee505e2016-10-03 18:17:18 +0000422/// ReportError - All bitcode analysis errors go through this function, making this a
Chris Lattnerca0ea542007-04-29 08:31:14 +0000423/// good place to breakpoint if debugging.
Zachary Turner2ee505e2016-10-03 18:17:18 +0000424static bool ReportError(const Twine &Err) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000425 errs() << Err << "\n";
Chris Lattnerca0ea542007-04-29 08:31:14 +0000426 return true;
427}
428
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000429static bool decodeMetadataStringsBlob(StringRef Indent,
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000430 ArrayRef<uint64_t> Record,
431 StringRef Blob) {
432 if (Blob.empty())
433 return true;
434
435 if (Record.size() != 2)
436 return true;
437
438 unsigned NumStrings = Record[0];
439 unsigned StringsOffset = Record[1];
440 outs() << " num-strings = " << NumStrings << " {\n";
441
442 StringRef Lengths = Blob.slice(0, StringsOffset);
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000443 SimpleBitstreamCursor R(Lengths);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000444 StringRef Strings = Blob.drop_front(StringsOffset);
445 do {
446 if (R.AtEndOfStream())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000447 return ReportError("bad length");
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000448
449 unsigned Size = R.ReadVBR(6);
450 if (Strings.size() < Size)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000451 return ReportError("truncated chars");
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000452
453 outs() << Indent << " '";
454 outs().write_escaped(Strings.slice(0, Size), /*hex=*/true);
455 outs() << "'\n";
456 Strings = Strings.drop_front(Size);
457 } while (--NumStrings);
458
459 outs() << Indent << " }";
460 return false;
461}
462
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000463static bool decodeBlob(unsigned Code, unsigned BlockID, StringRef Indent,
464 ArrayRef<uint64_t> Record, StringRef Blob) {
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000465 if (BlockID != bitc::METADATA_BLOCK_ID)
466 return true;
467 if (Code != bitc::METADATA_STRINGS)
468 return true;
469
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000470 return decodeMetadataStringsBlob(Indent, Record, Blob);
Duncan P. N. Exon Smith6565a0d2016-03-27 23:17:54 +0000471}
472
Chris Lattnerca0ea542007-04-29 08:31:14 +0000473/// ParseBlock - Read a block, updating statistics, etc.
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000474static bool ParseBlock(BitstreamCursor &Stream, BitstreamBlockInfo &BlockInfo,
475 unsigned BlockID, unsigned IndentLevel,
476 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +0000477 std::string Indent(IndentLevel*2, ' ');
Chris Lattner1684cee2007-04-29 20:00:02 +0000478 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
Chris Lattner3543caa2007-04-29 21:48:19 +0000479
Chris Lattner1684cee2007-04-29 20:00:02 +0000480 // Get the statistics for this BlockID.
481 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000482
Chris Lattner1684cee2007-04-29 20:00:02 +0000483 BlockStats.NumInstances++;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000484
Chris Lattner9181ddf2007-05-05 00:17:42 +0000485 // BLOCKINFO is a special part of the stream.
Richard Smithdc1414b2016-02-06 00:46:09 +0000486 bool DumpRecords = Dump;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000487 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Chris Lattner633ab162012-03-19 23:40:48 +0000488 if (Dump) outs() << Indent << "<BLOCKINFO_BLOCK/>\n";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000489 Optional<BitstreamBlockInfo> NewBlockInfo =
490 Stream.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
491 if (!NewBlockInfo)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000492 return ReportError("Malformed BlockInfoBlock");
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000493 BlockInfo = std::move(*NewBlockInfo);
494 Stream.JumpToBit(BlockBitStart);
Richard Smithdc1414b2016-02-06 00:46:09 +0000495 // It's not really interesting to dump the contents of the blockinfo block.
496 DumpRecords = false;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000497 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000498
Chris Lattner3543caa2007-04-29 21:48:19 +0000499 unsigned NumWords = 0;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000500 if (Stream.EnterSubBlock(BlockID, &NumWords))
Zachary Turner2ee505e2016-10-03 18:17:18 +0000501 return ReportError("Malformed block record");
Chris Lattnerca0ea542007-04-29 08:31:14 +0000502
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000503 // Keep it for later, when we see a MODULE_HASH record
504 uint64_t BlockEntryPos = Stream.getCurrentByteNo();
505
Craig Toppere6cb63e2014-04-25 04:24:47 +0000506 const char *BlockName = nullptr;
Richard Smithdc1414b2016-02-06 00:46:09 +0000507 if (DumpRecords) {
Chris Lattner633ab162012-03-19 23:40:48 +0000508 outs() << Indent << "<";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000509 if ((BlockName = GetBlockName(BlockID, BlockInfo, CurStreamType)))
Chris Lattner633ab162012-03-19 23:40:48 +0000510 outs() << BlockName;
Chris Lattner3543caa2007-04-29 21:48:19 +0000511 else
Chris Lattner633ab162012-03-19 23:40:48 +0000512 outs() << "UnknownBlock" << BlockID;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000513
Chris Lattner3543caa2007-04-29 21:48:19 +0000514 if (NonSymbolic && BlockName)
Chris Lattner633ab162012-03-19 23:40:48 +0000515 outs() << " BlockID=" << BlockID;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000516
Chris Lattner633ab162012-03-19 23:40:48 +0000517 outs() << " NumWords=" << NumWords
Chris Lattner3fa323d2013-01-19 21:37:14 +0000518 << " BlockCodeSize=" << Stream.getAbbrevIDWidth() << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000519 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000520
Chris Lattnerca0ea542007-04-29 08:31:14 +0000521 SmallVector<uint64_t, 64> Record;
522
Mehdi Aminie98f9252016-12-28 22:30:28 +0000523 // Keep the offset to the metadata index if seen.
524 uint64_t MetadataIndexOffset = 0;
525
Chris Lattnerca0ea542007-04-29 08:31:14 +0000526 // Read all the records for this block.
527 while (1) {
528 if (Stream.AtEndOfStream())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000529 return ReportError("Premature end of bitstream");
Chris Lattnerca0ea542007-04-29 08:31:14 +0000530
Chris Lattner1cf80692009-04-27 18:15:27 +0000531 uint64_t RecordStartBit = Stream.GetCurrentBitNo();
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000532
Chris Lattner0271af82013-01-20 02:50:32 +0000533 BitstreamEntry Entry =
534 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
535
536 switch (Entry.Kind) {
537 case BitstreamEntry::Error:
Zachary Turner2ee505e2016-10-03 18:17:18 +0000538 return ReportError("malformed bitcode file");
Chris Lattner0271af82013-01-20 02:50:32 +0000539 case BitstreamEntry::EndBlock: {
Chris Lattner1684cee2007-04-29 20:00:02 +0000540 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
541 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
Richard Smithdc1414b2016-02-06 00:46:09 +0000542 if (DumpRecords) {
Chris Lattner633ab162012-03-19 23:40:48 +0000543 outs() << Indent << "</";
Chris Lattner3543caa2007-04-29 21:48:19 +0000544 if (BlockName)
Chris Lattner633ab162012-03-19 23:40:48 +0000545 outs() << BlockName << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000546 else
Chris Lattner633ab162012-03-19 23:40:48 +0000547 outs() << "UnknownBlock" << BlockID << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000548 }
Chris Lattnerca0ea542007-04-29 08:31:14 +0000549 return false;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000550 }
Chris Lattner0271af82013-01-20 02:50:32 +0000551
552 case BitstreamEntry::SubBlock: {
Chris Lattner9e808cd2007-05-05 01:29:31 +0000553 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000554 if (ParseBlock(Stream, BlockInfo, Entry.ID, IndentLevel + 1,
555 CurStreamType))
Chris Lattnerca0ea542007-04-29 08:31:14 +0000556 return true;
Chris Lattner1684cee2007-04-29 20:00:02 +0000557 ++BlockStats.NumSubBlocks;
Chris Lattner9e808cd2007-05-05 01:29:31 +0000558 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
Chris Lattner0271af82013-01-20 02:50:32 +0000559
Chris Lattner9e808cd2007-05-05 01:29:31 +0000560 // Don't include subblock sizes in the size of this block.
561 BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
Chris Lattner0271af82013-01-20 02:50:32 +0000562 continue;
563 }
564 case BitstreamEntry::Record:
565 // The interesting case.
Chris Lattnerca0ea542007-04-29 08:31:14 +0000566 break;
Chris Lattner9e808cd2007-05-05 01:29:31 +0000567 }
Chris Lattner0271af82013-01-20 02:50:32 +0000568
569 if (Entry.ID == bitc::DEFINE_ABBREV) {
Chris Lattnerca0ea542007-04-29 08:31:14 +0000570 Stream.ReadAbbrevRecord();
Chris Lattner1684cee2007-04-29 20:00:02 +0000571 ++BlockStats.NumAbbrevs;
Chris Lattner0271af82013-01-20 02:50:32 +0000572 continue;
573 }
574
575 Record.clear();
Chris Lattner2ed6a202009-04-06 22:44:40 +0000576
Chris Lattner0271af82013-01-20 02:50:32 +0000577 ++BlockStats.NumRecords;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000578
Chris Lattner0271af82013-01-20 02:50:32 +0000579 StringRef Blob;
Peter Collingbournecf2750a2016-12-01 05:47:58 +0000580 unsigned CurrentRecordPos = Stream.GetCurrentBitNo();
Chris Lattner0271af82013-01-20 02:50:32 +0000581 unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000582
Chris Lattner0271af82013-01-20 02:50:32 +0000583 // Increment the # occurrences of this code.
584 if (BlockStats.CodeFreq.size() <= Code)
585 BlockStats.CodeFreq.resize(Code+1);
586 BlockStats.CodeFreq[Code].NumInstances++;
587 BlockStats.CodeFreq[Code].TotalBits +=
588 Stream.GetCurrentBitNo()-RecordStartBit;
589 if (Entry.ID != bitc::UNABBREV_RECORD) {
590 BlockStats.CodeFreq[Code].NumAbbrev++;
591 ++BlockStats.NumAbbreviatedRecords;
592 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000593
Richard Smithdc1414b2016-02-06 00:46:09 +0000594 if (DumpRecords) {
Chris Lattner0271af82013-01-20 02:50:32 +0000595 outs() << Indent << " <";
596 if (const char *CodeName =
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000597 GetCodeName(Code, BlockID, BlockInfo, CurStreamType))
Chris Lattner0271af82013-01-20 02:50:32 +0000598 outs() << CodeName;
599 else
600 outs() << "UnknownCode" << Code;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000601 if (NonSymbolic && GetCodeName(Code, BlockID, BlockInfo, CurStreamType))
Chris Lattner0271af82013-01-20 02:50:32 +0000602 outs() << " codeid=" << Code;
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000603 const BitCodeAbbrev *Abbv = nullptr;
604 if (Entry.ID != bitc::UNABBREV_RECORD) {
605 Abbv = Stream.getAbbrev(Entry.ID);
Chris Lattner0271af82013-01-20 02:50:32 +0000606 outs() << " abbrevid=" << Entry.ID;
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000607 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000608
Chris Lattner0271af82013-01-20 02:50:32 +0000609 for (unsigned i = 0, e = Record.size(); i != e; ++i)
610 outs() << " op" << i << "=" << (int64_t)Record[i];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000611
Mehdi Aminie98f9252016-12-28 22:30:28 +0000612 // If we found a metadata index, let's verify that we had an offset before
613 // and validate its forward reference offset was correct!
614 if (BlockID == bitc::METADATA_BLOCK_ID) {
615 if (Code == bitc::METADATA_INDEX_OFFSET) {
Mehdi Amini5022bb72016-12-28 23:45:54 +0000616 if (Record.size() != 2)
617 outs() << "(Invalid record)";
618 else {
619 auto Offset = Record[0] + (Record[1] << 32);
620 MetadataIndexOffset = Stream.GetCurrentBitNo() + Offset;
621 }
Mehdi Aminie98f9252016-12-28 22:30:28 +0000622 }
623 if (Code == bitc::METADATA_INDEX) {
624 outs() << " (offset ";
625 if (MetadataIndexOffset == RecordStartBit)
626 outs() << "match)";
627 else
628 outs() << "mismatch: " << MetadataIndexOffset << " vs "
629 << RecordStartBit << ")";
630 }
631 }
632
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000633 // If we found a module hash, let's verify that it matches!
634 if (BlockID == bitc::MODULE_BLOCK_ID && Code == bitc::MODULE_CODE_HASH) {
635 if (Record.size() != 5)
636 outs() << " (invalid)";
637 else {
638 // Recompute the hash and compare it to the one in the bitcode
639 SHA1 Hasher;
640 StringRef Hash;
641 {
Peter Collingbournecf2750a2016-12-01 05:47:58 +0000642 int BlockSize = (CurrentRecordPos / 8) - BlockEntryPos;
Mehdi Aminid7ad2212016-04-01 05:33:11 +0000643 auto Ptr = Stream.getPointerToByte(BlockEntryPos, BlockSize);
644 Hasher.update(ArrayRef<uint8_t>(Ptr, BlockSize));
645 Hash = Hasher.result();
646 }
647 SmallString<20> RecordedHash;
648 RecordedHash.resize(20);
649 int Pos = 0;
650 for (auto &Val : Record) {
651 assert(!(Val >> 32) && "Unexpected high bits set");
652 RecordedHash[Pos++] = (Val >> 24) & 0xFF;
653 RecordedHash[Pos++] = (Val >> 16) & 0xFF;
654 RecordedHash[Pos++] = (Val >> 8) & 0xFF;
655 RecordedHash[Pos++] = (Val >> 0) & 0xFF;
656 }
657 if (Hash == RecordedHash)
658 outs() << " (match)";
659 else
660 outs() << " (!mismatch!)";
661 }
662 }
663
Chris Lattner0271af82013-01-20 02:50:32 +0000664 outs() << "/>";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000665
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000666 if (Abbv) {
667 for (unsigned i = 1, e = Abbv->getNumOperandInfos(); i != e; ++i) {
668 const BitCodeAbbrevOp &Op = Abbv->getOperandInfo(i);
669 if (!Op.isEncoding() || Op.getEncoding() != BitCodeAbbrevOp::Array)
670 continue;
671 assert(i + 2 == e && "Array op not second to last");
672 std::string Str;
673 bool ArrayIsPrintable = true;
674 for (unsigned j = i - 1, je = Record.size(); j != je; ++j) {
675 if (!isprint(static_cast<unsigned char>(Record[j]))) {
676 ArrayIsPrintable = false;
677 break;
678 }
679 Str += (char)Record[j];
680 }
Teresa Johnsonf72278f2015-11-02 18:02:11 +0000681 if (ArrayIsPrintable)
682 outs() << " record string = '" << Str << "'";
Teresa Johnsonb1cfcd42015-10-08 15:56:24 +0000683 break;
684 }
685 }
686
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000687 if (Blob.data() && decodeBlob(Code, BlockID, Indent, Record, Blob)) {
Chris Lattner0271af82013-01-20 02:50:32 +0000688 outs() << " blob data = ";
Jordan Rose0fa38b82015-05-13 18:51:49 +0000689 if (ShowBinaryBlobs) {
690 outs() << "'";
691 outs().write_escaped(Blob, /*hex=*/true) << "'";
692 } else {
693 bool BlobIsPrintable = true;
694 for (unsigned i = 0, e = Blob.size(); i != e; ++i)
695 if (!isprint(static_cast<unsigned char>(Blob[i]))) {
696 BlobIsPrintable = false;
697 break;
698 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000699
Jordan Rose0fa38b82015-05-13 18:51:49 +0000700 if (BlobIsPrintable)
701 outs() << "'" << Blob << "'";
702 else
703 outs() << "unprintable, " << Blob.size() << " bytes.";
704 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000705 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000706
Chris Lattner0271af82013-01-20 02:50:32 +0000707 outs() << "\n";
Chris Lattnerca0ea542007-04-29 08:31:14 +0000708 }
Peter Collingbournecf2750a2016-12-01 05:47:58 +0000709
710 // Make sure that we can skip the current record.
711 Stream.JumpToBit(CurrentRecordPos);
712 Stream.skipRecord(Entry.ID);
Chris Lattnerca0ea542007-04-29 08:31:14 +0000713 }
714}
715
Chris Lattner1684cee2007-04-29 20:00:02 +0000716static void PrintSize(double Bits) {
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000717 outs() << format("%.2f/%.2fB/%luW", Bits, Bits/8,(unsigned long)(Bits/32));
Chris Lattnerbf419a92009-04-27 17:59:34 +0000718}
719static void PrintSize(uint64_t Bits) {
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000720 outs() << format("%lub/%.2fB/%luW", (unsigned long)Bits,
721 (double)Bits/8, (unsigned long)(Bits/32));
Chris Lattner1684cee2007-04-29 20:00:02 +0000722}
723
Jordan Rose88eb5342014-08-30 17:07:55 +0000724static bool openBitcodeFile(StringRef Path,
725 std::unique_ptr<MemoryBuffer> &MemBuf,
Jordan Rose88eb5342014-08-30 17:07:55 +0000726 BitstreamCursor &Stream,
727 CurStreamTypeType &CurStreamType) {
Chris Lattner03997582007-04-29 08:12:22 +0000728 // Read the input file.
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000729 ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr =
Jordan Rose88eb5342014-08-30 17:07:55 +0000730 MemoryBuffer::getFileOrSTDIN(Path);
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000731 if (std::error_code EC = MemBufOrErr.getError())
Zachary Turner2ee505e2016-10-03 18:17:18 +0000732 return ReportError(Twine("ReportError reading '") + Path + "': " + EC.message());
Jordan Rose88eb5342014-08-30 17:07:55 +0000733 MemBuf = std::move(MemBufOrErr.get());
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000734
Jordan Rose88eb5342014-08-30 17:07:55 +0000735 if (MemBuf->getBufferSize() & 3)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000736 return ReportError("Bitcode stream should be a multiple of 4 bytes in length");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000737
Jordan Rose88eb5342014-08-30 17:07:55 +0000738 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart();
739 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize();
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000740
Chris Lattnerb9e07fd2009-04-06 20:54:32 +0000741 // If we have a wrapper header, parse it and ignore the non-bc file contents.
742 // The magic number is 0x0B17C0DE stored in little endian.
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000743 if (isBitcodeWrapper(BufPtr, EndBufPtr)) {
Mehdi Aminieed26932016-04-01 05:19:14 +0000744 if (MemBuf->getBufferSize() < BWH_HeaderSize)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000745 return ReportError("Invalid bitcode wrapper header");
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000746
747 if (Dump) {
748 unsigned Magic = support::endian::read32le(&BufPtr[BWH_MagicField]);
749 unsigned Version = support::endian::read32le(&BufPtr[BWH_VersionField]);
750 unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]);
751 unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]);
752 unsigned CPUType = support::endian::read32le(&BufPtr[BWH_CPUTypeField]);
753
754 outs() << "<BITCODE_WRAPPER_HEADER"
755 << " Magic=" << format_hex(Magic, 10)
756 << " Version=" << format_hex(Version, 10)
757 << " Offset=" << format_hex(Offset, 10)
758 << " Size=" << format_hex(Size, 10)
759 << " CPUType=" << format_hex(CPUType, 10) << "/>\n";
760 }
761
Derek Schuff8b2dcad2012-02-06 22:30:29 +0000762 if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr, true))
Zachary Turner2ee505e2016-10-03 18:17:18 +0000763 return ReportError("Invalid bitcode wrapper header");
Akira Hatanaka4f472a882016-01-29 05:55:09 +0000764 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000765
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000766 Stream = BitstreamCursor(ArrayRef<uint8_t>(BufPtr, EndBufPtr));
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000767
Chris Lattner03997582007-04-29 08:12:22 +0000768 // Read the stream signature.
769 char Signature[6];
770 Signature[0] = Stream.Read(8);
771 Signature[1] = Stream.Read(8);
772 Signature[2] = Stream.Read(4);
773 Signature[3] = Stream.Read(4);
774 Signature[4] = Stream.Read(4);
775 Signature[5] = Stream.Read(4);
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000776
Chris Lattnerca0ea542007-04-29 08:31:14 +0000777 // Autodetect the file contents, if it is one we know.
Chris Lattner03997582007-04-29 08:12:22 +0000778 CurStreamType = UnknownBitstream;
779 if (Signature[0] == 'B' && Signature[1] == 'C' &&
780 Signature[2] == 0x0 && Signature[3] == 0xC &&
781 Signature[4] == 0xE && Signature[5] == 0xD)
782 CurStreamType = LLVMIRBitstream;
783
Jordan Rose88eb5342014-08-30 17:07:55 +0000784 return false;
785}
786
787/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
788static int AnalyzeBitcode() {
789 std::unique_ptr<MemoryBuffer> StreamBuffer;
Jordan Rose88eb5342014-08-30 17:07:55 +0000790 BitstreamCursor Stream;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000791 BitstreamBlockInfo BlockInfo;
Jordan Rose88eb5342014-08-30 17:07:55 +0000792 CurStreamTypeType CurStreamType;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000793 if (openBitcodeFile(InputFilename, StreamBuffer, Stream, CurStreamType))
Jordan Rose88eb5342014-08-30 17:07:55 +0000794 return true;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000795 Stream.setBlockInfo(&BlockInfo);
Jordan Rose88eb5342014-08-30 17:07:55 +0000796
797 // Read block info from BlockInfoFilename, if specified.
798 // The block info must be a top-level block.
799 if (!BlockInfoFilename.empty()) {
800 std::unique_ptr<MemoryBuffer> BlockInfoBuffer;
Jordan Rose88eb5342014-08-30 17:07:55 +0000801 BitstreamCursor BlockInfoCursor;
802 CurStreamTypeType BlockInfoStreamType;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000803 if (openBitcodeFile(BlockInfoFilename, BlockInfoBuffer, BlockInfoCursor,
804 BlockInfoStreamType))
Jordan Rose88eb5342014-08-30 17:07:55 +0000805 return true;
806
807 while (!BlockInfoCursor.AtEndOfStream()) {
808 unsigned Code = BlockInfoCursor.ReadCode();
809 if (Code != bitc::ENTER_SUBBLOCK)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000810 return ReportError("Invalid record at top-level in block info file");
Jordan Rose88eb5342014-08-30 17:07:55 +0000811
812 unsigned BlockID = BlockInfoCursor.ReadSubBlockID();
813 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000814 Optional<BitstreamBlockInfo> NewBlockInfo =
815 BlockInfoCursor.ReadBlockInfoBlock(/*ReadBlockInfoNames=*/true);
816 if (!NewBlockInfo)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000817 return ReportError("Malformed BlockInfoBlock in block info file");
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000818 BlockInfo = std::move(*NewBlockInfo);
Jordan Rose88eb5342014-08-30 17:07:55 +0000819 break;
820 }
821
822 BlockInfoCursor.SkipBlock();
823 }
Jordan Rose88eb5342014-08-30 17:07:55 +0000824 }
825
Chris Lattner1684cee2007-04-29 20:00:02 +0000826 unsigned NumTopBlocks = 0;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000827
Chris Lattnerca0ea542007-04-29 08:31:14 +0000828 // Parse the top-level structure. We only allow blocks at the top-level.
829 while (!Stream.AtEndOfStream()) {
830 unsigned Code = Stream.ReadCode();
831 if (Code != bitc::ENTER_SUBBLOCK)
Zachary Turner2ee505e2016-10-03 18:17:18 +0000832 return ReportError("Invalid record at top-level");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000833
Chris Lattner0271af82013-01-20 02:50:32 +0000834 unsigned BlockID = Stream.ReadSubBlockID();
835
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000836 if (ParseBlock(Stream, BlockInfo, BlockID, 0, CurStreamType))
Chris Lattnerca0ea542007-04-29 08:31:14 +0000837 return true;
Chris Lattner1684cee2007-04-29 20:00:02 +0000838 ++NumTopBlocks;
Chris Lattnerca0ea542007-04-29 08:31:14 +0000839 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000840
Chris Lattner633ab162012-03-19 23:40:48 +0000841 if (Dump) outs() << "\n\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000842
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000843 uint64_t BufferSizeBits = Stream.getBitcodeBytes().size() * CHAR_BIT;
Chris Lattnerca0ea542007-04-29 08:31:14 +0000844 // Print a summary of the read file.
Chris Lattner633ab162012-03-19 23:40:48 +0000845 outs() << "Summary of " << InputFilename << ":\n";
846 outs() << " Total size: ";
Chris Lattner5fab65d2007-05-01 02:43:46 +0000847 PrintSize(BufferSizeBits);
Chris Lattner633ab162012-03-19 23:40:48 +0000848 outs() << "\n";
849 outs() << " Stream type: ";
Chris Lattner03997582007-04-29 08:12:22 +0000850 switch (CurStreamType) {
Chris Lattner633ab162012-03-19 23:40:48 +0000851 case UnknownBitstream: outs() << "unknown\n"; break;
852 case LLVMIRBitstream: outs() << "LLVM IR\n"; break;
Chris Lattner03997582007-04-29 08:12:22 +0000853 }
Chris Lattner633ab162012-03-19 23:40:48 +0000854 outs() << " # Toplevel Blocks: " << NumTopBlocks << "\n";
855 outs() << "\n";
Chris Lattner1684cee2007-04-29 20:00:02 +0000856
857 // Emit per-block stats.
Chris Lattner633ab162012-03-19 23:40:48 +0000858 outs() << "Per-block Summary:\n";
Chris Lattner1684cee2007-04-29 20:00:02 +0000859 for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
860 E = BlockIDStats.end(); I != E; ++I) {
Chris Lattner633ab162012-03-19 23:40:48 +0000861 outs() << " Block ID #" << I->first;
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000862 if (const char *BlockName =
863 GetBlockName(I->first, BlockInfo, CurStreamType))
Chris Lattner633ab162012-03-19 23:40:48 +0000864 outs() << " (" << BlockName << ")";
865 outs() << ":\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000866
Chris Lattner1684cee2007-04-29 20:00:02 +0000867 const PerBlockIDStats &Stats = I->second;
Chris Lattner633ab162012-03-19 23:40:48 +0000868 outs() << " Num Instances: " << Stats.NumInstances << "\n";
869 outs() << " Total Size: ";
Chris Lattner1684cee2007-04-29 20:00:02 +0000870 PrintSize(Stats.NumBits);
Chris Lattner633ab162012-03-19 23:40:48 +0000871 outs() << "\n";
Daniel Dunbare813b222009-09-25 16:04:21 +0000872 double pct = (Stats.NumBits * 100.0) / BufferSizeBits;
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000873 outs() << " Percent of file: " << format("%2.4f%%", pct) << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000874 if (Stats.NumInstances > 1) {
Chris Lattner633ab162012-03-19 23:40:48 +0000875 outs() << " Average Size: ";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000876 PrintSize(Stats.NumBits/(double)Stats.NumInstances);
Chris Lattner633ab162012-03-19 23:40:48 +0000877 outs() << "\n";
878 outs() << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000879 << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
Chris Lattner633ab162012-03-19 23:40:48 +0000880 outs() << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000881 << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
Chris Lattner633ab162012-03-19 23:40:48 +0000882 outs() << " Tot/Avg Records: " << Stats.NumRecords << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000883 << Stats.NumRecords/(double)Stats.NumInstances << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000884 } else {
Chris Lattner633ab162012-03-19 23:40:48 +0000885 outs() << " Num SubBlocks: " << Stats.NumSubBlocks << "\n";
886 outs() << " Num Abbrevs: " << Stats.NumAbbrevs << "\n";
887 outs() << " Num Records: " << Stats.NumRecords << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000888 }
Daniel Dunbare813b222009-09-25 16:04:21 +0000889 if (Stats.NumRecords) {
890 double pct = (Stats.NumAbbreviatedRecords * 100.0) / Stats.NumRecords;
Chris Lattner633ab162012-03-19 23:40:48 +0000891 outs() << " Percent Abbrevs: " << format("%2.4f%%", pct) << "\n";
Daniel Dunbare813b222009-09-25 16:04:21 +0000892 }
Chris Lattner633ab162012-03-19 23:40:48 +0000893 outs() << "\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000894
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000895 // Print a histogram of the codes we see.
896 if (!NoHistogram && !Stats.CodeFreq.empty()) {
Mehdi Aminid2638562015-10-21 06:10:55 +0000897 std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code>
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000898 for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
Mehdi Aminid2638562015-10-21 06:10:55 +0000899 if (unsigned Freq = Stats.CodeFreq[i].NumInstances)
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000900 FreqPairs.push_back(std::make_pair(Freq, i));
901 std::stable_sort(FreqPairs.begin(), FreqPairs.end());
902 std::reverse(FreqPairs.begin(), FreqPairs.end());
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000903
Chris Lattner633ab162012-03-19 23:40:48 +0000904 outs() << "\tRecord Histogram:\n";
Richard Smithdc1414b2016-02-06 00:46:09 +0000905 outs() << "\t\t Count # Bits b/Rec % Abv Record Kind\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000906 for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
Chris Lattner1cf80692009-04-27 18:15:27 +0000907 const PerRecordStats &RecStats = Stats.CodeFreq[FreqPairs[i].second];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000908
Mehdi Aminid2638562015-10-21 06:10:55 +0000909 outs() << format("\t\t%7d %9lu",
Jan Wen Voung05ff5702012-09-05 20:55:57 +0000910 RecStats.NumInstances,
911 (unsigned long)RecStats.TotalBits);
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000912
Richard Smithdc1414b2016-02-06 00:46:09 +0000913 if (RecStats.NumInstances > 1)
914 outs() << format(" %9.1f",
915 (double)RecStats.TotalBits/RecStats.NumInstances);
916 else
917 outs() << " ";
918
Chris Lattner1cf80692009-04-27 18:15:27 +0000919 if (RecStats.NumAbbrev)
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000920 outs() <<
Richard Smithdc1414b2016-02-06 00:46:09 +0000921 format(" %7.2f",
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000922 (double)RecStats.NumAbbrev/RecStats.NumInstances*100);
Chris Lattner1cf80692009-04-27 18:15:27 +0000923 else
Richard Smithdc1414b2016-02-06 00:46:09 +0000924 outs() << " ";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000925
Richard Smithdc1414b2016-02-06 00:46:09 +0000926 outs() << " ";
Peter Collingbourne77c89b62016-11-08 04:17:11 +0000927 if (const char *CodeName = GetCodeName(FreqPairs[i].second, I->first,
928 BlockInfo, CurStreamType))
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000929 outs() << CodeName << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000930 else
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000931 outs() << "UnknownCode" << FreqPairs[i].second << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000932 }
Chris Lattner633ab162012-03-19 23:40:48 +0000933 outs() << "\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000934
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000935 }
Chris Lattner1684cee2007-04-29 20:00:02 +0000936 }
Chris Lattner03997582007-04-29 08:12:22 +0000937 return 0;
938}
Reid Spencerdb5c86d2004-06-07 17:53:43 +0000939
Chris Lattnerca0ea542007-04-29 08:31:14 +0000940
Chris Lattner76d46322006-12-06 01:18:01 +0000941int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000942 // Print a stack trace if we signal out.
Richard Smith2ad6d482016-06-09 00:53:21 +0000943 sys::PrintStackTraceOnErrorSignal(argv[0]);
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000944 PrettyStackTraceProgram X(argc, argv);
945 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
946 cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000947
Chris Lattner6d80e212007-05-06 09:29:57 +0000948 return AnalyzeBitcode();
Reid Spencerdb5c86d2004-06-07 17:53:43 +0000949}