blob: 69880a425a1ddc4ed60841f16b306d135f6699f6 [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
Chris Lattner03997582007-04-29 08:12:22 +000030#include "llvm/Bitcode/BitstreamReader.h"
Chris Lattner1684cee2007-04-29 20:00:02 +000031#include "llvm/Bitcode/LLVMBitCodes.h"
Chris Lattnerb9e07fd2009-04-06 20:54:32 +000032#include "llvm/Bitcode/ReaderWriter.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000033#include "llvm/IR/Verifier.h"
Jordan Rose88eb5342014-08-30 17:07:55 +000034#include "llvm/ADT/Optional.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"
Michael J. Spencer447762d2010-11-29 18:16:10 +000040#include "llvm/Support/Signals.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000041#include "llvm/Support/raw_ostream.h"
Chris Lattner4a7ac9f2007-05-05 01:46:49 +000042#include <algorithm>
Will Dietz981af002013-10-12 00:55:57 +000043#include <cctype>
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000044#include <map>
Rafael Espindolaa6e9c3e2014-06-12 17:38:55 +000045#include <system_error>
Reid Spencerdb5c86d2004-06-07 17:53:43 +000046using namespace llvm;
47
48static cl::opt<std::string>
Gabor Greif0e535c3c2007-07-04 21:55:50 +000049 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Reid Spencerdb5c86d2004-06-07 17:53:43 +000050
Gabor Greif0e535c3c2007-07-04 21:55:50 +000051static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace"));
Chris Lattnerca0ea542007-04-29 08:31:14 +000052
53//===----------------------------------------------------------------------===//
54// Bitcode specific analysis.
55//===----------------------------------------------------------------------===//
56
Chris Lattner4a7ac9f2007-05-05 01:46:49 +000057static cl::opt<bool> NoHistogram("disable-histogram",
58 cl::desc("Do not print per-code histogram"));
Chris Lattner03997582007-04-29 08:12:22 +000059
Chris Lattner3543caa2007-04-29 21:48:19 +000060static cl::opt<bool>
61NonSymbolic("non-symbolic",
Michael J. Spencer39a0ffc2010-12-16 03:29:14 +000062 cl::desc("Emit numeric info in dump even if"
Chris Lattner3543caa2007-04-29 21:48:19 +000063 " symbolic info is available"));
64
Jordan Rose88eb5342014-08-30 17:07:55 +000065static cl::opt<std::string>
66 BlockInfoFilename("block-info",
67 cl::desc("Use the BLOCK_INFO from the given file"));
68
Dan Gohmanf749ad72010-12-09 20:35:40 +000069namespace {
70
71/// CurStreamTypeType - A type for CurStreamType
72enum CurStreamTypeType {
Chris Lattner03997582007-04-29 08:12:22 +000073 UnknownBitstream,
74 LLVMIRBitstream
Dan Gohmanf749ad72010-12-09 20:35:40 +000075};
76
77}
78
Chris Lattner1684cee2007-04-29 20:00:02 +000079/// GetBlockName - Return a symbolic block name if known, otherwise return
Chris Lattner3543caa2007-04-29 21:48:19 +000080/// null.
Chris Lattnera6fdf5a2009-04-26 22:21:57 +000081static const char *GetBlockName(unsigned BlockID,
Jordan Rose88eb5342014-08-30 17:07:55 +000082 const BitstreamReader &StreamFile,
83 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +000084 // Standard blocks for all bitcode files.
85 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
86 if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
87 return "BLOCKINFO_BLOCK";
Craig Toppere6cb63e2014-04-25 04:24:47 +000088 return nullptr;
Chris Lattner9181ddf2007-05-05 00:17:42 +000089 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +000090
Chris Lattnera6fdf5a2009-04-26 22:21:57 +000091 // Check to see if we have a blockinfo record for this block, with a name.
92 if (const BitstreamReader::BlockInfo *Info =
93 StreamFile.getBlockInfo(BlockID)) {
94 if (!Info->Name.empty())
95 return Info->Name.c_str();
96 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +000097
98
Craig Toppere6cb63e2014-04-25 04:24:47 +000099 if (CurStreamType != LLVMIRBitstream) return nullptr;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000100
Chris Lattner1684cee2007-04-29 20:00:02 +0000101 switch (BlockID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000102 default: return nullptr;
Bill Wendlingd7e05d62013-02-10 23:17:10 +0000103 case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK";
104 case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK";
105 case bitc::PARAMATTR_GROUP_BLOCK_ID: return "PARAMATTR_GROUP_BLOCK_ID";
106 case bitc::TYPE_BLOCK_ID_NEW: return "TYPE_BLOCK_ID";
107 case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK";
108 case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK";
109 case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB";
110 case bitc::METADATA_BLOCK_ID: return "METADATA_BLOCK";
111 case bitc::METADATA_ATTACHMENT_ID: return "METADATA_ATTACHMENT_BLOCK";
112 case bitc::USELIST_BLOCK_ID: return "USELIST_BLOCK_ID";
Chris Lattner1684cee2007-04-29 20:00:02 +0000113 }
114}
115
Chris Lattner3543caa2007-04-29 21:48:19 +0000116/// GetCodeName - Return a symbolic code name if known, otherwise return
117/// null.
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000118static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
Jordan Rose88eb5342014-08-30 17:07:55 +0000119 const BitstreamReader &StreamFile,
120 CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +0000121 // Standard blocks for all bitcode files.
122 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
123 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
124 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000125 default: return nullptr;
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000126 case bitc::BLOCKINFO_CODE_SETBID: return "SETBID";
127 case bitc::BLOCKINFO_CODE_BLOCKNAME: return "BLOCKNAME";
128 case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME";
Chris Lattner9181ddf2007-05-05 00:17:42 +0000129 }
130 }
Craig Toppere6cb63e2014-04-25 04:24:47 +0000131 return nullptr;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000132 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000133
Chris Lattnera6fdf5a2009-04-26 22:21:57 +0000134 // Check to see if we have a blockinfo record for this record, with a name.
135 if (const BitstreamReader::BlockInfo *Info =
136 StreamFile.getBlockInfo(BlockID)) {
137 for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i)
138 if (Info->RecordNames[i].first == CodeID)
139 return Info->RecordNames[i].second.c_str();
140 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000141
142
Craig Toppere6cb63e2014-04-25 04:24:47 +0000143 if (CurStreamType != LLVMIRBitstream) return nullptr;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000144
Chris Lattner3543caa2007-04-29 21:48:19 +0000145 switch (BlockID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000146 default: return nullptr;
Chris Lattner3543caa2007-04-29 21:48:19 +0000147 case bitc::MODULE_BLOCK_ID:
148 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000149 default: return nullptr;
Chris Lattner3543caa2007-04-29 21:48:19 +0000150 case bitc::MODULE_CODE_VERSION: return "VERSION";
151 case bitc::MODULE_CODE_TRIPLE: return "TRIPLE";
152 case bitc::MODULE_CODE_DATALAYOUT: return "DATALAYOUT";
153 case bitc::MODULE_CODE_ASM: return "ASM";
154 case bitc::MODULE_CODE_SECTIONNAME: return "SECTIONNAME";
Bill Wendling706d3d62012-11-28 08:41:48 +0000155 case bitc::MODULE_CODE_DEPLIB: return "DEPLIB"; // FIXME: Remove in 4.0
Chris Lattner3543caa2007-04-29 21:48:19 +0000156 case bitc::MODULE_CODE_GLOBALVAR: return "GLOBALVAR";
157 case bitc::MODULE_CODE_FUNCTION: return "FUNCTION";
158 case bitc::MODULE_CODE_ALIAS: return "ALIAS";
159 case bitc::MODULE_CODE_PURGEVALS: return "PURGEVALS";
Nick Lewycky3707f1e2008-11-07 14:52:51 +0000160 case bitc::MODULE_CODE_GCNAME: return "GCNAME";
Chris Lattner3543caa2007-04-29 21:48:19 +0000161 }
Chris Lattner0b7c5122007-05-04 03:01:41 +0000162 case bitc::PARAMATTR_BLOCK_ID:
163 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000164 default: return nullptr;
Bill Wendlingd7e05d62013-02-10 23:17:10 +0000165 case bitc::PARAMATTR_CODE_ENTRY_OLD: return "ENTRY";
166 case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY";
167 case bitc::PARAMATTR_GRP_CODE_ENTRY: return "ENTRY";
Chris Lattner0b7c5122007-05-04 03:01:41 +0000168 }
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000169 case bitc::TYPE_BLOCK_ID_NEW:
Chris Lattner3543caa2007-04-29 21:48:19 +0000170 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000171 default: return nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000172 case bitc::TYPE_CODE_NUMENTRY: return "NUMENTRY";
173 case bitc::TYPE_CODE_VOID: return "VOID";
174 case bitc::TYPE_CODE_FLOAT: return "FLOAT";
175 case bitc::TYPE_CODE_DOUBLE: return "DOUBLE";
176 case bitc::TYPE_CODE_LABEL: return "LABEL";
177 case bitc::TYPE_CODE_OPAQUE: return "OPAQUE";
178 case bitc::TYPE_CODE_INTEGER: return "INTEGER";
179 case bitc::TYPE_CODE_POINTER: return "POINTER";
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000180 case bitc::TYPE_CODE_ARRAY: return "ARRAY";
181 case bitc::TYPE_CODE_VECTOR: return "VECTOR";
182 case bitc::TYPE_CODE_X86_FP80: return "X86_FP80";
183 case bitc::TYPE_CODE_FP128: return "FP128";
184 case bitc::TYPE_CODE_PPC_FP128: return "PPC_FP128";
185 case bitc::TYPE_CODE_METADATA: return "METADATA";
186 case bitc::TYPE_CODE_STRUCT_ANON: return "STRUCT_ANON";
187 case bitc::TYPE_CODE_STRUCT_NAME: return "STRUCT_NAME";
188 case bitc::TYPE_CODE_STRUCT_NAMED: return "STRUCT_NAMED";
Chad Rosier95898722011-11-03 00:14:01 +0000189 case bitc::TYPE_CODE_FUNCTION: return "FUNCTION";
Chris Lattner3543caa2007-04-29 21:48:19 +0000190 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000191
Chris Lattner3543caa2007-04-29 21:48:19 +0000192 case bitc::CONSTANTS_BLOCK_ID:
193 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000194 default: return nullptr;
Duncan Sands3cf0cde2009-09-25 12:28:37 +0000195 case bitc::CST_CODE_SETTYPE: return "SETTYPE";
196 case bitc::CST_CODE_NULL: return "NULL";
197 case bitc::CST_CODE_UNDEF: return "UNDEF";
198 case bitc::CST_CODE_INTEGER: return "INTEGER";
199 case bitc::CST_CODE_WIDE_INTEGER: return "WIDE_INTEGER";
200 case bitc::CST_CODE_FLOAT: return "FLOAT";
201 case bitc::CST_CODE_AGGREGATE: return "AGGREGATE";
202 case bitc::CST_CODE_STRING: return "STRING";
203 case bitc::CST_CODE_CSTRING: return "CSTRING";
204 case bitc::CST_CODE_CE_BINOP: return "CE_BINOP";
205 case bitc::CST_CODE_CE_CAST: return "CE_CAST";
206 case bitc::CST_CODE_CE_GEP: return "CE_GEP";
207 case bitc::CST_CODE_CE_INBOUNDS_GEP: return "CE_INBOUNDS_GEP";
208 case bitc::CST_CODE_CE_SELECT: return "CE_SELECT";
209 case bitc::CST_CODE_CE_EXTRACTELT: return "CE_EXTRACTELT";
210 case bitc::CST_CODE_CE_INSERTELT: return "CE_INSERTELT";
211 case bitc::CST_CODE_CE_SHUFFLEVEC: return "CE_SHUFFLEVEC";
212 case bitc::CST_CODE_CE_CMP: return "CE_CMP";
213 case bitc::CST_CODE_INLINEASM: return "INLINEASM";
214 case bitc::CST_CODE_CE_SHUFVEC_EX: return "CE_SHUFVEC_EX";
Chris Lattner372dd1e2012-01-30 00:51:16 +0000215 case bitc::CST_CODE_BLOCKADDRESS: return "CST_CODE_BLOCKADDRESS";
216 case bitc::CST_CODE_DATA: return "DATA";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000217 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000218 case bitc::FUNCTION_BLOCK_ID:
219 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000220 default: return nullptr;
Chris Lattner3543caa2007-04-29 21:48:19 +0000221 case bitc::FUNC_CODE_DECLAREBLOCKS: return "DECLAREBLOCKS";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000222
Duncan Sands3cf0cde2009-09-25 12:28:37 +0000223 case bitc::FUNC_CODE_INST_BINOP: return "INST_BINOP";
224 case bitc::FUNC_CODE_INST_CAST: return "INST_CAST";
225 case bitc::FUNC_CODE_INST_GEP: return "INST_GEP";
226 case bitc::FUNC_CODE_INST_INBOUNDS_GEP: return "INST_INBOUNDS_GEP";
227 case bitc::FUNC_CODE_INST_SELECT: return "INST_SELECT";
228 case bitc::FUNC_CODE_INST_EXTRACTELT: return "INST_EXTRACTELT";
229 case bitc::FUNC_CODE_INST_INSERTELT: return "INST_INSERTELT";
230 case bitc::FUNC_CODE_INST_SHUFFLEVEC: return "INST_SHUFFLEVEC";
231 case bitc::FUNC_CODE_INST_CMP: return "INST_CMP";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000232
Duncan Sands3cf0cde2009-09-25 12:28:37 +0000233 case bitc::FUNC_CODE_INST_RET: return "INST_RET";
234 case bitc::FUNC_CODE_INST_BR: return "INST_BR";
235 case bitc::FUNC_CODE_INST_SWITCH: return "INST_SWITCH";
236 case bitc::FUNC_CODE_INST_INVOKE: return "INST_INVOKE";
Duncan Sands3cf0cde2009-09-25 12:28:37 +0000237 case bitc::FUNC_CODE_INST_UNREACHABLE: return "INST_UNREACHABLE";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000238
Duncan Sands3cf0cde2009-09-25 12:28:37 +0000239 case bitc::FUNC_CODE_INST_PHI: return "INST_PHI";
Duncan Sands3cf0cde2009-09-25 12:28:37 +0000240 case bitc::FUNC_CODE_INST_ALLOCA: return "INST_ALLOCA";
241 case bitc::FUNC_CODE_INST_LOAD: return "INST_LOAD";
Duncan Sands3cf0cde2009-09-25 12:28:37 +0000242 case bitc::FUNC_CODE_INST_VAARG: return "INST_VAARG";
Chris Lattnerc44070802011-06-17 18:17:37 +0000243 case bitc::FUNC_CODE_INST_STORE: return "INST_STORE";
Duncan Sands3cf0cde2009-09-25 12:28:37 +0000244 case bitc::FUNC_CODE_INST_EXTRACTVAL: return "INST_EXTRACTVAL";
245 case bitc::FUNC_CODE_INST_INSERTVAL: return "INST_INSERTVAL";
246 case bitc::FUNC_CODE_INST_CMP2: return "INST_CMP2";
247 case bitc::FUNC_CODE_INST_VSELECT: return "INST_VSELECT";
Chris Lattner07d09ed2010-04-03 02:17:50 +0000248 case bitc::FUNC_CODE_DEBUG_LOC_AGAIN: return "DEBUG_LOC_AGAIN";
Chris Lattnerc44070802011-06-17 18:17:37 +0000249 case bitc::FUNC_CODE_INST_CALL: return "INST_CALL";
Duncan P. N. Exon Smith9ed19662015-01-09 17:53:27 +0000250 case bitc::FUNC_CODE_DEBUG_LOC: return "DEBUG_LOC";
Chris Lattner3543caa2007-04-29 21:48:19 +0000251 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000252 case bitc::VALUE_SYMTAB_BLOCK_ID:
253 switch (CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000254 default: return nullptr;
Chris Lattner3543caa2007-04-29 21:48:19 +0000255 case bitc::VST_CODE_ENTRY: return "ENTRY";
Chris Lattner0b7c5122007-05-04 03:01:41 +0000256 case bitc::VST_CODE_BBENTRY: return "BBENTRY";
Chris Lattner3543caa2007-04-29 21:48:19 +0000257 }
Devang Patelaf206b82009-09-18 19:26:43 +0000258 case bitc::METADATA_ATTACHMENT_ID:
259 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000260 default:return nullptr;
Chris Lattnerb8778552011-06-17 17:50:30 +0000261 case bitc::METADATA_ATTACHMENT: return "METADATA_ATTACHMENT";
Devang Patelaf206b82009-09-18 19:26:43 +0000262 }
Devang Patel7428d8a2009-07-22 17:43:22 +0000263 case bitc::METADATA_BLOCK_ID:
264 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000265 default:return nullptr;
Dan Gohmanaa288012010-07-16 18:28:07 +0000266 case bitc::METADATA_STRING: return "METADATA_STRING";
Devang Patel5ac2f492009-07-30 23:03:19 +0000267 case bitc::METADATA_NAME: return "METADATA_NAME";
Devang Patelaf206b82009-09-18 19:26:43 +0000268 case bitc::METADATA_KIND: return "METADATA_KIND";
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +0000269 case bitc::METADATA_NODE: return "METADATA_NODE";
270 case bitc::METADATA_VALUE: return "METADATA_VALUE";
Duncan P. N. Exon Smith005f9f42014-12-11 22:30:48 +0000271 case bitc::METADATA_OLD_NODE: return "METADATA_OLD_NODE";
272 case bitc::METADATA_OLD_FN_NODE: return "METADATA_OLD_FN_NODE";
Chris Lattnerb8778552011-06-17 17:50:30 +0000273 case bitc::METADATA_NAMED_NODE: return "METADATA_NAMED_NODE";
Devang Patel7428d8a2009-07-22 17:43:22 +0000274 }
Chad Rosierdd4ffae2011-12-07 21:45:13 +0000275 case bitc::USELIST_BLOCK_ID:
276 switch(CodeID) {
Craig Toppere6cb63e2014-04-25 04:24:47 +0000277 default:return nullptr;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000278 case bitc::USELIST_CODE_DEFAULT: return "USELIST_CODE_DEFAULT";
279 case bitc::USELIST_CODE_BB: return "USELIST_CODE_BB";
Chad Rosierdd4ffae2011-12-07 21:45:13 +0000280 }
Chris Lattner3543caa2007-04-29 21:48:19 +0000281 }
282}
283
Chris Lattnerbf419a92009-04-27 17:59:34 +0000284struct PerRecordStats {
285 unsigned NumInstances;
Chris Lattner1cf80692009-04-27 18:15:27 +0000286 unsigned NumAbbrev;
287 uint64_t TotalBits;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000288
Chris Lattner1cf80692009-04-27 18:15:27 +0000289 PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
Chris Lattnerbf419a92009-04-27 17:59:34 +0000290};
Chris Lattner1684cee2007-04-29 20:00:02 +0000291
292struct PerBlockIDStats {
293 /// NumInstances - This the number of times this block ID has been seen.
294 unsigned NumInstances;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000295
Chris Lattner1684cee2007-04-29 20:00:02 +0000296 /// NumBits - The total size in bits of all of these blocks.
297 uint64_t NumBits;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000298
Chris Lattner1684cee2007-04-29 20:00:02 +0000299 /// NumSubBlocks - The total number of blocks these blocks contain.
300 unsigned NumSubBlocks;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000301
Chris Lattner1684cee2007-04-29 20:00:02 +0000302 /// NumAbbrevs - The total number of abbreviations.
303 unsigned NumAbbrevs;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000304
305 /// NumRecords - The total number of records these blocks contain, and the
Chris Lattner1684cee2007-04-29 20:00:02 +0000306 /// number that are abbreviated.
307 unsigned NumRecords, NumAbbreviatedRecords;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000308
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000309 /// CodeFreq - Keep track of the number of times we see each code.
Chris Lattnerbf419a92009-04-27 17:59:34 +0000310 std::vector<PerRecordStats> CodeFreq;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000311
Chris Lattner1684cee2007-04-29 20:00:02 +0000312 PerBlockIDStats()
313 : NumInstances(0), NumBits(0),
314 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
315};
316
317static std::map<unsigned, PerBlockIDStats> BlockIDStats;
318
319
320
Chris Lattnerca0ea542007-04-29 08:31:14 +0000321/// Error - All bitcode analysis errors go through this function, making this a
322/// good place to breakpoint if debugging.
Jordan Rose88eb5342014-08-30 17:07:55 +0000323static bool Error(const Twine &Err) {
Dan Gohmand8db3762009-07-15 16:35:29 +0000324 errs() << Err << "\n";
Chris Lattnerca0ea542007-04-29 08:31:14 +0000325 return true;
326}
327
328/// ParseBlock - Read a block, updating statistics, etc.
Chris Lattner0271af82013-01-20 02:50:32 +0000329static bool ParseBlock(BitstreamCursor &Stream, unsigned BlockID,
Jordan Rose88eb5342014-08-30 17:07:55 +0000330 unsigned IndentLevel, CurStreamTypeType CurStreamType) {
Chris Lattner9181ddf2007-05-05 00:17:42 +0000331 std::string Indent(IndentLevel*2, ' ');
Chris Lattner1684cee2007-04-29 20:00:02 +0000332 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
Chris Lattner3543caa2007-04-29 21:48:19 +0000333
Chris Lattner1684cee2007-04-29 20:00:02 +0000334 // Get the statistics for this BlockID.
335 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000336
Chris Lattner1684cee2007-04-29 20:00:02 +0000337 BlockStats.NumInstances++;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000338
Chris Lattner9181ddf2007-05-05 00:17:42 +0000339 // BLOCKINFO is a special part of the stream.
340 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Chris Lattner633ab162012-03-19 23:40:48 +0000341 if (Dump) outs() << Indent << "<BLOCKINFO_BLOCK/>\n";
Chris Lattner9181ddf2007-05-05 00:17:42 +0000342 if (Stream.ReadBlockInfoBlock())
343 return Error("Malformed BlockInfoBlock");
344 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
345 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
346 return false;
347 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000348
Chris Lattner3543caa2007-04-29 21:48:19 +0000349 unsigned NumWords = 0;
Chris Lattner9181ddf2007-05-05 00:17:42 +0000350 if (Stream.EnterSubBlock(BlockID, &NumWords))
Chris Lattnerca0ea542007-04-29 08:31:14 +0000351 return Error("Malformed block record");
352
Craig Toppere6cb63e2014-04-25 04:24:47 +0000353 const char *BlockName = nullptr;
Chris Lattner3543caa2007-04-29 21:48:19 +0000354 if (Dump) {
Chris Lattner633ab162012-03-19 23:40:48 +0000355 outs() << Indent << "<";
Jordan Rose88eb5342014-08-30 17:07:55 +0000356 if ((BlockName = GetBlockName(BlockID, *Stream.getBitStreamReader(),
357 CurStreamType)))
Chris Lattner633ab162012-03-19 23:40:48 +0000358 outs() << BlockName;
Chris Lattner3543caa2007-04-29 21:48:19 +0000359 else
Chris Lattner633ab162012-03-19 23:40:48 +0000360 outs() << "UnknownBlock" << BlockID;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000361
Chris Lattner3543caa2007-04-29 21:48:19 +0000362 if (NonSymbolic && BlockName)
Chris Lattner633ab162012-03-19 23:40:48 +0000363 outs() << " BlockID=" << BlockID;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000364
Chris Lattner633ab162012-03-19 23:40:48 +0000365 outs() << " NumWords=" << NumWords
Chris Lattner3fa323d2013-01-19 21:37:14 +0000366 << " BlockCodeSize=" << Stream.getAbbrevIDWidth() << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000367 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000368
Chris Lattnerca0ea542007-04-29 08:31:14 +0000369 SmallVector<uint64_t, 64> Record;
370
371 // Read all the records for this block.
372 while (1) {
373 if (Stream.AtEndOfStream())
374 return Error("Premature end of bitstream");
375
Chris Lattner1cf80692009-04-27 18:15:27 +0000376 uint64_t RecordStartBit = Stream.GetCurrentBitNo();
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000377
Chris Lattner0271af82013-01-20 02:50:32 +0000378 BitstreamEntry Entry =
379 Stream.advance(BitstreamCursor::AF_DontAutoprocessAbbrevs);
380
381 switch (Entry.Kind) {
382 case BitstreamEntry::Error:
383 return Error("malformed bitcode file");
384 case BitstreamEntry::EndBlock: {
Chris Lattner1684cee2007-04-29 20:00:02 +0000385 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
386 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
Chris Lattner3543caa2007-04-29 21:48:19 +0000387 if (Dump) {
Chris Lattner633ab162012-03-19 23:40:48 +0000388 outs() << Indent << "</";
Chris Lattner3543caa2007-04-29 21:48:19 +0000389 if (BlockName)
Chris Lattner633ab162012-03-19 23:40:48 +0000390 outs() << BlockName << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000391 else
Chris Lattner633ab162012-03-19 23:40:48 +0000392 outs() << "UnknownBlock" << BlockID << ">\n";
Chris Lattner3543caa2007-04-29 21:48:19 +0000393 }
Chris Lattnerca0ea542007-04-29 08:31:14 +0000394 return false;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000395 }
Chris Lattner0271af82013-01-20 02:50:32 +0000396
397 case BitstreamEntry::SubBlock: {
Chris Lattner9e808cd2007-05-05 01:29:31 +0000398 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
Jordan Rose88eb5342014-08-30 17:07:55 +0000399 if (ParseBlock(Stream, Entry.ID, IndentLevel+1, CurStreamType))
Chris Lattnerca0ea542007-04-29 08:31:14 +0000400 return true;
Chris Lattner1684cee2007-04-29 20:00:02 +0000401 ++BlockStats.NumSubBlocks;
Chris Lattner9e808cd2007-05-05 01:29:31 +0000402 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
Chris Lattner0271af82013-01-20 02:50:32 +0000403
Chris Lattner9e808cd2007-05-05 01:29:31 +0000404 // Don't include subblock sizes in the size of this block.
405 BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
Chris Lattner0271af82013-01-20 02:50:32 +0000406 continue;
407 }
408 case BitstreamEntry::Record:
409 // The interesting case.
Chris Lattnerca0ea542007-04-29 08:31:14 +0000410 break;
Chris Lattner9e808cd2007-05-05 01:29:31 +0000411 }
Chris Lattner0271af82013-01-20 02:50:32 +0000412
413 if (Entry.ID == bitc::DEFINE_ABBREV) {
Chris Lattnerca0ea542007-04-29 08:31:14 +0000414 Stream.ReadAbbrevRecord();
Chris Lattner1684cee2007-04-29 20:00:02 +0000415 ++BlockStats.NumAbbrevs;
Chris Lattner0271af82013-01-20 02:50:32 +0000416 continue;
417 }
418
419 Record.clear();
Chris Lattner2ed6a202009-04-06 22:44:40 +0000420
Chris Lattner0271af82013-01-20 02:50:32 +0000421 ++BlockStats.NumRecords;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000422
Chris Lattner0271af82013-01-20 02:50:32 +0000423 StringRef Blob;
424 unsigned Code = Stream.readRecord(Entry.ID, Record, &Blob);
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000425
Chris Lattner0271af82013-01-20 02:50:32 +0000426 // Increment the # occurrences of this code.
427 if (BlockStats.CodeFreq.size() <= Code)
428 BlockStats.CodeFreq.resize(Code+1);
429 BlockStats.CodeFreq[Code].NumInstances++;
430 BlockStats.CodeFreq[Code].TotalBits +=
431 Stream.GetCurrentBitNo()-RecordStartBit;
432 if (Entry.ID != bitc::UNABBREV_RECORD) {
433 BlockStats.CodeFreq[Code].NumAbbrev++;
434 ++BlockStats.NumAbbreviatedRecords;
435 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000436
Chris Lattner0271af82013-01-20 02:50:32 +0000437 if (Dump) {
438 outs() << Indent << " <";
439 if (const char *CodeName =
Jordan Rose88eb5342014-08-30 17:07:55 +0000440 GetCodeName(Code, BlockID, *Stream.getBitStreamReader(),
441 CurStreamType))
Chris Lattner0271af82013-01-20 02:50:32 +0000442 outs() << CodeName;
443 else
444 outs() << "UnknownCode" << Code;
445 if (NonSymbolic &&
Jordan Rose88eb5342014-08-30 17:07:55 +0000446 GetCodeName(Code, BlockID, *Stream.getBitStreamReader(),
447 CurStreamType))
Chris Lattner0271af82013-01-20 02:50:32 +0000448 outs() << " codeid=" << Code;
449 if (Entry.ID != bitc::UNABBREV_RECORD)
450 outs() << " abbrevid=" << Entry.ID;
Chris Lattner3543caa2007-04-29 21:48:19 +0000451
Chris Lattner0271af82013-01-20 02:50:32 +0000452 for (unsigned i = 0, e = Record.size(); i != e; ++i)
453 outs() << " op" << i << "=" << (int64_t)Record[i];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000454
Chris Lattner0271af82013-01-20 02:50:32 +0000455 outs() << "/>";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000456
Chris Lattner0271af82013-01-20 02:50:32 +0000457 if (Blob.data()) {
458 outs() << " blob data = ";
459 bool BlobIsPrintable = true;
460 for (unsigned i = 0, e = Blob.size(); i != e; ++i)
Guy Benyei83c74e92013-02-12 21:21:59 +0000461 if (!isprint(static_cast<unsigned char>(Blob[i]))) {
Chris Lattner0271af82013-01-20 02:50:32 +0000462 BlobIsPrintable = false;
463 break;
464 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000465
Chris Lattner0271af82013-01-20 02:50:32 +0000466 if (BlobIsPrintable)
467 outs() << "'" << Blob << "'";
468 else
469 outs() << "unprintable, " << Blob.size() << " bytes.";
Chris Lattner3543caa2007-04-29 21:48:19 +0000470 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000471
Chris Lattner0271af82013-01-20 02:50:32 +0000472 outs() << "\n";
Chris Lattnerca0ea542007-04-29 08:31:14 +0000473 }
474 }
475}
476
Chris Lattner1684cee2007-04-29 20:00:02 +0000477static void PrintSize(double Bits) {
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000478 outs() << format("%.2f/%.2fB/%luW", Bits, Bits/8,(unsigned long)(Bits/32));
Chris Lattnerbf419a92009-04-27 17:59:34 +0000479}
480static void PrintSize(uint64_t Bits) {
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000481 outs() << format("%lub/%.2fB/%luW", (unsigned long)Bits,
482 (double)Bits/8, (unsigned long)(Bits/32));
Chris Lattner1684cee2007-04-29 20:00:02 +0000483}
484
Jordan Rose88eb5342014-08-30 17:07:55 +0000485static bool openBitcodeFile(StringRef Path,
486 std::unique_ptr<MemoryBuffer> &MemBuf,
487 BitstreamReader &StreamFile,
488 BitstreamCursor &Stream,
489 CurStreamTypeType &CurStreamType) {
Chris Lattner03997582007-04-29 08:12:22 +0000490 // Read the input file.
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000491 ErrorOr<std::unique_ptr<MemoryBuffer>> MemBufOrErr =
Jordan Rose88eb5342014-08-30 17:07:55 +0000492 MemoryBuffer::getFileOrSTDIN(Path);
Rafael Espindolaadf21f22014-07-06 17:43:13 +0000493 if (std::error_code EC = MemBufOrErr.getError())
Jordan Rose88eb5342014-08-30 17:07:55 +0000494 return Error(Twine("Error reading '") + Path + "': " + EC.message());
495 MemBuf = std::move(MemBufOrErr.get());
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000496
Jordan Rose88eb5342014-08-30 17:07:55 +0000497 if (MemBuf->getBufferSize() & 3)
Chris Lattnerca0ea542007-04-29 08:31:14 +0000498 return Error("Bitcode stream should be a multiple of 4 bytes in length");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000499
Jordan Rose88eb5342014-08-30 17:07:55 +0000500 const unsigned char *BufPtr = (const unsigned char *)MemBuf->getBufferStart();
501 const unsigned char *EndBufPtr = BufPtr + MemBuf->getBufferSize();
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000502
Chris Lattnerb9e07fd2009-04-06 20:54:32 +0000503 // If we have a wrapper header, parse it and ignore the non-bc file contents.
504 // The magic number is 0x0B17C0DE stored in little endian.
505 if (isBitcodeWrapper(BufPtr, EndBufPtr))
Derek Schuff8b2dcad2012-02-06 22:30:29 +0000506 if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr, true))
Chris Lattnerb9e07fd2009-04-06 20:54:32 +0000507 return Error("Invalid bitcode wrapper header");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000508
Jordan Rose88eb5342014-08-30 17:07:55 +0000509 StreamFile = BitstreamReader(BufPtr, EndBufPtr);
510 Stream = BitstreamCursor(StreamFile);
Chris Lattner0e3f50d2009-04-27 20:04:08 +0000511 StreamFile.CollectBlockInfoNames();
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000512
Chris Lattner03997582007-04-29 08:12:22 +0000513 // Read the stream signature.
514 char Signature[6];
515 Signature[0] = Stream.Read(8);
516 Signature[1] = Stream.Read(8);
517 Signature[2] = Stream.Read(4);
518 Signature[3] = Stream.Read(4);
519 Signature[4] = Stream.Read(4);
520 Signature[5] = Stream.Read(4);
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000521
Chris Lattnerca0ea542007-04-29 08:31:14 +0000522 // Autodetect the file contents, if it is one we know.
Chris Lattner03997582007-04-29 08:12:22 +0000523 CurStreamType = UnknownBitstream;
524 if (Signature[0] == 'B' && Signature[1] == 'C' &&
525 Signature[2] == 0x0 && Signature[3] == 0xC &&
526 Signature[4] == 0xE && Signature[5] == 0xD)
527 CurStreamType = LLVMIRBitstream;
528
Jordan Rose88eb5342014-08-30 17:07:55 +0000529 return false;
530}
531
532/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
533static int AnalyzeBitcode() {
534 std::unique_ptr<MemoryBuffer> StreamBuffer;
535 BitstreamReader StreamFile;
536 BitstreamCursor Stream;
537 CurStreamTypeType CurStreamType;
538 if (openBitcodeFile(InputFilename, StreamBuffer, StreamFile, Stream,
539 CurStreamType))
540 return true;
541
542 // Read block info from BlockInfoFilename, if specified.
543 // The block info must be a top-level block.
544 if (!BlockInfoFilename.empty()) {
545 std::unique_ptr<MemoryBuffer> BlockInfoBuffer;
546 BitstreamReader BlockInfoFile;
547 BitstreamCursor BlockInfoCursor;
548 CurStreamTypeType BlockInfoStreamType;
549 if (openBitcodeFile(BlockInfoFilename, BlockInfoBuffer, BlockInfoFile,
550 BlockInfoCursor, BlockInfoStreamType))
551 return true;
552
553 while (!BlockInfoCursor.AtEndOfStream()) {
554 unsigned Code = BlockInfoCursor.ReadCode();
555 if (Code != bitc::ENTER_SUBBLOCK)
556 return Error("Invalid record at top-level in block info file");
557
558 unsigned BlockID = BlockInfoCursor.ReadSubBlockID();
559 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
560 if (BlockInfoCursor.ReadBlockInfoBlock())
561 return Error("Malformed BlockInfoBlock in block info file");
562 break;
563 }
564
565 BlockInfoCursor.SkipBlock();
566 }
567
568 StreamFile.takeBlockInfo(std::move(BlockInfoFile));
569 }
570
Chris Lattner1684cee2007-04-29 20:00:02 +0000571 unsigned NumTopBlocks = 0;
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000572
Chris Lattnerca0ea542007-04-29 08:31:14 +0000573 // Parse the top-level structure. We only allow blocks at the top-level.
574 while (!Stream.AtEndOfStream()) {
575 unsigned Code = Stream.ReadCode();
576 if (Code != bitc::ENTER_SUBBLOCK)
577 return Error("Invalid record at top-level");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000578
Chris Lattner0271af82013-01-20 02:50:32 +0000579 unsigned BlockID = Stream.ReadSubBlockID();
580
Jordan Rose88eb5342014-08-30 17:07:55 +0000581 if (ParseBlock(Stream, BlockID, 0, CurStreamType))
Chris Lattnerca0ea542007-04-29 08:31:14 +0000582 return true;
Chris Lattner1684cee2007-04-29 20:00:02 +0000583 ++NumTopBlocks;
Chris Lattnerca0ea542007-04-29 08:31:14 +0000584 }
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000585
Chris Lattner633ab162012-03-19 23:40:48 +0000586 if (Dump) outs() << "\n\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000587
Jordan Rose88eb5342014-08-30 17:07:55 +0000588 uint64_t BufferSizeBits = StreamFile.getBitcodeBytes().getExtent() * CHAR_BIT;
Chris Lattnerca0ea542007-04-29 08:31:14 +0000589 // Print a summary of the read file.
Chris Lattner633ab162012-03-19 23:40:48 +0000590 outs() << "Summary of " << InputFilename << ":\n";
591 outs() << " Total size: ";
Chris Lattner5fab65d2007-05-01 02:43:46 +0000592 PrintSize(BufferSizeBits);
Chris Lattner633ab162012-03-19 23:40:48 +0000593 outs() << "\n";
594 outs() << " Stream type: ";
Chris Lattner03997582007-04-29 08:12:22 +0000595 switch (CurStreamType) {
Chris Lattner633ab162012-03-19 23:40:48 +0000596 case UnknownBitstream: outs() << "unknown\n"; break;
597 case LLVMIRBitstream: outs() << "LLVM IR\n"; break;
Chris Lattner03997582007-04-29 08:12:22 +0000598 }
Chris Lattner633ab162012-03-19 23:40:48 +0000599 outs() << " # Toplevel Blocks: " << NumTopBlocks << "\n";
600 outs() << "\n";
Chris Lattner1684cee2007-04-29 20:00:02 +0000601
602 // Emit per-block stats.
Chris Lattner633ab162012-03-19 23:40:48 +0000603 outs() << "Per-block Summary:\n";
Chris Lattner1684cee2007-04-29 20:00:02 +0000604 for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
605 E = BlockIDStats.end(); I != E; ++I) {
Chris Lattner633ab162012-03-19 23:40:48 +0000606 outs() << " Block ID #" << I->first;
Jordan Rose88eb5342014-08-30 17:07:55 +0000607 if (const char *BlockName = GetBlockName(I->first, StreamFile,
608 CurStreamType))
Chris Lattner633ab162012-03-19 23:40:48 +0000609 outs() << " (" << BlockName << ")";
610 outs() << ":\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000611
Chris Lattner1684cee2007-04-29 20:00:02 +0000612 const PerBlockIDStats &Stats = I->second;
Chris Lattner633ab162012-03-19 23:40:48 +0000613 outs() << " Num Instances: " << Stats.NumInstances << "\n";
614 outs() << " Total Size: ";
Chris Lattner1684cee2007-04-29 20:00:02 +0000615 PrintSize(Stats.NumBits);
Chris Lattner633ab162012-03-19 23:40:48 +0000616 outs() << "\n";
Daniel Dunbare813b222009-09-25 16:04:21 +0000617 double pct = (Stats.NumBits * 100.0) / BufferSizeBits;
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000618 outs() << " Percent of file: " << format("%2.4f%%", pct) << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000619 if (Stats.NumInstances > 1) {
Chris Lattner633ab162012-03-19 23:40:48 +0000620 outs() << " Average Size: ";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000621 PrintSize(Stats.NumBits/(double)Stats.NumInstances);
Chris Lattner633ab162012-03-19 23:40:48 +0000622 outs() << "\n";
623 outs() << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000624 << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
Chris Lattner633ab162012-03-19 23:40:48 +0000625 outs() << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000626 << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
Chris Lattner633ab162012-03-19 23:40:48 +0000627 outs() << " Tot/Avg Records: " << Stats.NumRecords << "/"
Dan Gohmand8db3762009-07-15 16:35:29 +0000628 << Stats.NumRecords/(double)Stats.NumInstances << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000629 } else {
Chris Lattner633ab162012-03-19 23:40:48 +0000630 outs() << " Num SubBlocks: " << Stats.NumSubBlocks << "\n";
631 outs() << " Num Abbrevs: " << Stats.NumAbbrevs << "\n";
632 outs() << " Num Records: " << Stats.NumRecords << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000633 }
Daniel Dunbare813b222009-09-25 16:04:21 +0000634 if (Stats.NumRecords) {
635 double pct = (Stats.NumAbbreviatedRecords * 100.0) / Stats.NumRecords;
Chris Lattner633ab162012-03-19 23:40:48 +0000636 outs() << " Percent Abbrevs: " << format("%2.4f%%", pct) << "\n";
Daniel Dunbare813b222009-09-25 16:04:21 +0000637 }
Chris Lattner633ab162012-03-19 23:40:48 +0000638 outs() << "\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000639
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000640 // Print a histogram of the codes we see.
641 if (!NoHistogram && !Stats.CodeFreq.empty()) {
642 std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code>
643 for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
Chris Lattnerbf419a92009-04-27 17:59:34 +0000644 if (unsigned Freq = Stats.CodeFreq[i].NumInstances)
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000645 FreqPairs.push_back(std::make_pair(Freq, i));
646 std::stable_sort(FreqPairs.begin(), FreqPairs.end());
647 std::reverse(FreqPairs.begin(), FreqPairs.end());
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000648
Chris Lattner633ab162012-03-19 23:40:48 +0000649 outs() << "\tRecord Histogram:\n";
Jan Wen Voung05ff5702012-09-05 20:55:57 +0000650 outs() << "\t\t Count # Bits %% Abv Record Kind\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000651 for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
Chris Lattner1cf80692009-04-27 18:15:27 +0000652 const PerRecordStats &RecStats = Stats.CodeFreq[FreqPairs[i].second];
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000653
Jan Wen Voung5020dd32012-09-05 20:56:00 +0000654 outs() << format("\t\t%7d %9lu",
Jan Wen Voung05ff5702012-09-05 20:55:57 +0000655 RecStats.NumInstances,
656 (unsigned long)RecStats.TotalBits);
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000657
Chris Lattner1cf80692009-04-27 18:15:27 +0000658 if (RecStats.NumAbbrev)
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000659 outs() <<
660 format("%7.2f ",
661 (double)RecStats.NumAbbrev/RecStats.NumInstances*100);
Chris Lattner1cf80692009-04-27 18:15:27 +0000662 else
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000663 outs() << " ";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000664
665 if (const char *CodeName =
Jordan Rose88eb5342014-08-30 17:07:55 +0000666 GetCodeName(FreqPairs[i].second, I->first, StreamFile,
667 CurStreamType))
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000668 outs() << CodeName << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000669 else
Jan Wen Voung52ad2082012-09-05 20:55:54 +0000670 outs() << "UnknownCode" << FreqPairs[i].second << "\n";
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000671 }
Chris Lattner633ab162012-03-19 23:40:48 +0000672 outs() << "\n";
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000673
Chris Lattner4a7ac9f2007-05-05 01:46:49 +0000674 }
Chris Lattner1684cee2007-04-29 20:00:02 +0000675 }
Chris Lattner03997582007-04-29 08:12:22 +0000676 return 0;
677}
Reid Spencerdb5c86d2004-06-07 17:53:43 +0000678
Chris Lattnerca0ea542007-04-29 08:31:14 +0000679
Chris Lattner76d46322006-12-06 01:18:01 +0000680int main(int argc, char **argv) {
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000681 // Print a stack trace if we signal out.
Chris Lattner03997582007-04-29 08:12:22 +0000682 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere3fc2d12009-03-06 05:34:10 +0000683 PrettyStackTraceProgram X(argc, argv);
684 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
685 cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
Daniel Dunbar75359a7c2009-09-25 16:03:57 +0000686
Chris Lattner6d80e212007-05-06 09:29:57 +0000687 return AnalyzeBitcode();
Reid Spencerdb5c86d2004-06-07 17:53:43 +0000688}