blob: bb39e07d9042206024fa34c9218eb9c2494775e4 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- llvm-bcanalyzer.cpp - Bitcode Analyzer --------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5f5a5732007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This tool may be invoked in the following manner:
11// llvm-bcanalyzer [options] - Read LLVM bitcode from stdin
12// llvm-bcanalyzer [options] x.bc - Read LLVM bitcode from the x.bc file
13//
14// Options:
15// --help - Output information about command line switches
16// --dump - Dump low-level bitcode structure in readable format
17//
18// 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
21// statistics about the contents of the file. By default this information is
22// detailed and contains information about individual bitcode blocks and the
23// functions in the module.
24// The tool is also able to print a bitcode file in a straight forward text
25// format that shows the containment and relationships of the information in
26// the bitcode file (-dump option).
27//
28//===----------------------------------------------------------------------===//
29
30#include "llvm/Analysis/Verifier.h"
31#include "llvm/Bitcode/BitstreamReader.h"
32#include "llvm/Bitcode/LLVMBitCodes.h"
Chris Lattnerabd0e442009-04-06 20:54:32 +000033#include "llvm/Bitcode/ReaderWriter.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034#include "llvm/Support/CommandLine.h"
35#include "llvm/Support/ManagedStatic.h"
36#include "llvm/Support/MemoryBuffer.h"
Chris Lattnere6012df2009-03-06 05:34:10 +000037#include "llvm/Support/PrettyStackTrace.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038#include "llvm/System/Signals.h"
39#include <map>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040#include <algorithm>
41using namespace llvm;
42
43static cl::opt<std::string>
44 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
45
46static cl::opt<std::string>
47 OutputFilename("-o", cl::init("-"), cl::desc("<output file>"));
48
49static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace"));
50
51//===----------------------------------------------------------------------===//
52// Bitcode specific analysis.
53//===----------------------------------------------------------------------===//
54
55static cl::opt<bool> NoHistogram("disable-histogram",
56 cl::desc("Do not print per-code histogram"));
57
58static cl::opt<bool>
59NonSymbolic("non-symbolic",
60 cl::desc("Emit numberic info in dump even if"
61 " symbolic info is available"));
62
63/// CurStreamType - If we can sniff the flavor of this stream, we can produce
64/// better dump info.
65static enum {
66 UnknownBitstream,
67 LLVMIRBitstream
68} CurStreamType;
69
70
71/// GetBlockName - Return a symbolic block name if known, otherwise return
72/// null.
Chris Lattner71101322009-04-26 22:21:57 +000073static const char *GetBlockName(unsigned BlockID,
74 const BitstreamReader &StreamFile) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 // Standard blocks for all bitcode files.
76 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
77 if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
78 return "BLOCKINFO_BLOCK";
79 return 0;
80 }
81
Chris Lattner71101322009-04-26 22:21:57 +000082 // Check to see if we have a blockinfo record for this block, with a name.
83 if (const BitstreamReader::BlockInfo *Info =
84 StreamFile.getBlockInfo(BlockID)) {
85 if (!Info->Name.empty())
86 return Info->Name.c_str();
87 }
88
89
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 if (CurStreamType != LLVMIRBitstream) return 0;
91
92 switch (BlockID) {
93 default: return 0;
94 case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK";
95 case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK";
96 case bitc::TYPE_BLOCK_ID: return "TYPE_BLOCK";
97 case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK";
98 case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK";
99 case bitc::TYPE_SYMTAB_BLOCK_ID: return "TYPE_SYMTAB";
100 case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB";
101 }
102}
103
104/// GetCodeName - Return a symbolic code name if known, otherwise return
105/// null.
Chris Lattner71101322009-04-26 22:21:57 +0000106static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
107 const BitstreamReader &StreamFile) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108 // Standard blocks for all bitcode files.
109 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
110 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
111 switch (CodeID) {
112 default: return 0;
Chris Lattner71101322009-04-26 22:21:57 +0000113 case bitc::BLOCKINFO_CODE_SETBID: return "SETBID";
114 case bitc::BLOCKINFO_CODE_BLOCKNAME: return "BLOCKNAME";
115 case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 }
117 }
118 return 0;
119 }
120
Chris Lattner71101322009-04-26 22:21:57 +0000121 // Check to see if we have a blockinfo record for this record, with a name.
122 if (const BitstreamReader::BlockInfo *Info =
123 StreamFile.getBlockInfo(BlockID)) {
124 for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i)
125 if (Info->RecordNames[i].first == CodeID)
126 return Info->RecordNames[i].second.c_str();
127 }
128
129
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 if (CurStreamType != LLVMIRBitstream) return 0;
131
132 switch (BlockID) {
133 default: return 0;
134 case bitc::MODULE_BLOCK_ID:
135 switch (CodeID) {
136 default: return 0;
137 case bitc::MODULE_CODE_VERSION: return "VERSION";
138 case bitc::MODULE_CODE_TRIPLE: return "TRIPLE";
139 case bitc::MODULE_CODE_DATALAYOUT: return "DATALAYOUT";
140 case bitc::MODULE_CODE_ASM: return "ASM";
141 case bitc::MODULE_CODE_SECTIONNAME: return "SECTIONNAME";
142 case bitc::MODULE_CODE_DEPLIB: return "DEPLIB";
143 case bitc::MODULE_CODE_GLOBALVAR: return "GLOBALVAR";
144 case bitc::MODULE_CODE_FUNCTION: return "FUNCTION";
145 case bitc::MODULE_CODE_ALIAS: return "ALIAS";
146 case bitc::MODULE_CODE_PURGEVALS: return "PURGEVALS";
Nick Lewyckyb90b8412008-11-07 14:52:51 +0000147 case bitc::MODULE_CODE_GCNAME: return "GCNAME";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 }
149 case bitc::PARAMATTR_BLOCK_ID:
150 switch (CodeID) {
151 default: return 0;
152 case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY";
153 }
154 case bitc::TYPE_BLOCK_ID:
155 switch (CodeID) {
156 default: return 0;
Nick Lewyckyb90b8412008-11-07 14:52:51 +0000157 case bitc::TYPE_CODE_NUMENTRY: return "NUMENTRY";
158 case bitc::TYPE_CODE_VOID: return "VOID";
159 case bitc::TYPE_CODE_FLOAT: return "FLOAT";
160 case bitc::TYPE_CODE_DOUBLE: return "DOUBLE";
161 case bitc::TYPE_CODE_LABEL: return "LABEL";
162 case bitc::TYPE_CODE_OPAQUE: return "OPAQUE";
163 case bitc::TYPE_CODE_INTEGER: return "INTEGER";
164 case bitc::TYPE_CODE_POINTER: return "POINTER";
165 case bitc::TYPE_CODE_FUNCTION: return "FUNCTION";
166 case bitc::TYPE_CODE_STRUCT: return "STRUCT";
167 case bitc::TYPE_CODE_ARRAY: return "ARRAY";
168 case bitc::TYPE_CODE_VECTOR: return "VECTOR";
169 case bitc::TYPE_CODE_X86_FP80: return "X86_FP80";
170 case bitc::TYPE_CODE_FP128: return "FP128";
171 case bitc::TYPE_CODE_PPC_FP128: return "PPC_FP128";
Nick Lewycky15714342009-06-01 04:41:03 +0000172 case bitc::TYPE_CODE_METADATA: return "METADATA";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 }
174
175 case bitc::CONSTANTS_BLOCK_ID:
176 switch (CodeID) {
177 default: return 0;
178 case bitc::CST_CODE_SETTYPE: return "SETTYPE";
179 case bitc::CST_CODE_NULL: return "NULL";
180 case bitc::CST_CODE_UNDEF: return "UNDEF";
181 case bitc::CST_CODE_INTEGER: return "INTEGER";
182 case bitc::CST_CODE_WIDE_INTEGER: return "WIDE_INTEGER";
183 case bitc::CST_CODE_FLOAT: return "FLOAT";
184 case bitc::CST_CODE_AGGREGATE: return "AGGREGATE";
185 case bitc::CST_CODE_STRING: return "STRING";
186 case bitc::CST_CODE_CSTRING: return "CSTRING";
187 case bitc::CST_CODE_CE_BINOP: return "CE_BINOP";
188 case bitc::CST_CODE_CE_CAST: return "CE_CAST";
189 case bitc::CST_CODE_CE_GEP: return "CE_GEP";
190 case bitc::CST_CODE_CE_SELECT: return "CE_SELECT";
191 case bitc::CST_CODE_CE_EXTRACTELT: return "CE_EXTRACTELT";
192 case bitc::CST_CODE_CE_INSERTELT: return "CE_INSERTELT";
193 case bitc::CST_CODE_CE_SHUFFLEVEC: return "CE_SHUFFLEVEC";
194 case bitc::CST_CODE_CE_CMP: return "CE_CMP";
195 case bitc::CST_CODE_INLINEASM: return "INLINEASM";
Nick Lewycky15714342009-06-01 04:41:03 +0000196 case bitc::CST_CODE_CE_SHUFVEC_EX: return "CE_SHUFVEC_EX";
197 case bitc::CST_CODE_MDSTRING: return "MDSTRING";
198 case bitc::CST_CODE_MDNODE: return "MDNODE";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 }
200 case bitc::FUNCTION_BLOCK_ID:
201 switch (CodeID) {
202 default: return 0;
203 case bitc::FUNC_CODE_DECLAREBLOCKS: return "DECLAREBLOCKS";
204
205 case bitc::FUNC_CODE_INST_BINOP: return "INST_BINOP";
206 case bitc::FUNC_CODE_INST_CAST: return "INST_CAST";
207 case bitc::FUNC_CODE_INST_GEP: return "INST_GEP";
208 case bitc::FUNC_CODE_INST_SELECT: return "INST_SELECT";
209 case bitc::FUNC_CODE_INST_EXTRACTELT: return "INST_EXTRACTELT";
210 case bitc::FUNC_CODE_INST_INSERTELT: return "INST_INSERTELT";
211 case bitc::FUNC_CODE_INST_SHUFFLEVEC: return "INST_SHUFFLEVEC";
212 case bitc::FUNC_CODE_INST_CMP: return "INST_CMP";
213
214 case bitc::FUNC_CODE_INST_RET: return "INST_RET";
215 case bitc::FUNC_CODE_INST_BR: return "INST_BR";
216 case bitc::FUNC_CODE_INST_SWITCH: return "INST_SWITCH";
217 case bitc::FUNC_CODE_INST_INVOKE: return "INST_INVOKE";
218 case bitc::FUNC_CODE_INST_UNWIND: return "INST_UNWIND";
219 case bitc::FUNC_CODE_INST_UNREACHABLE: return "INST_UNREACHABLE";
220
221 case bitc::FUNC_CODE_INST_PHI: return "INST_PHI";
222 case bitc::FUNC_CODE_INST_MALLOC: return "INST_MALLOC";
223 case bitc::FUNC_CODE_INST_FREE: return "INST_FREE";
224 case bitc::FUNC_CODE_INST_ALLOCA: return "INST_ALLOCA";
225 case bitc::FUNC_CODE_INST_LOAD: return "INST_LOAD";
226 case bitc::FUNC_CODE_INST_STORE: return "INST_STORE";
227 case bitc::FUNC_CODE_INST_CALL: return "INST_CALL";
228 case bitc::FUNC_CODE_INST_VAARG: return "INST_VAARG";
Christopher Lamb44d62f62007-12-11 08:59:05 +0000229 case bitc::FUNC_CODE_INST_STORE2: return "INST_STORE2";
Nick Lewycky2c3eced2008-03-01 21:47:06 +0000230 case bitc::FUNC_CODE_INST_GETRESULT: return "INST_GETRESULT";
Nick Lewyckyb90b8412008-11-07 14:52:51 +0000231 case bitc::FUNC_CODE_INST_EXTRACTVAL: return "INST_EXTRACTVAL";
232 case bitc::FUNC_CODE_INST_INSERTVAL: return "INST_INSERTVAL";
233 case bitc::FUNC_CODE_INST_CMP2: return "INST_CMP2";
234 case bitc::FUNC_CODE_INST_VSELECT: return "INST_VSELECT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 }
236 case bitc::TYPE_SYMTAB_BLOCK_ID:
237 switch (CodeID) {
238 default: return 0;
239 case bitc::TST_CODE_ENTRY: return "ENTRY";
240 }
241 case bitc::VALUE_SYMTAB_BLOCK_ID:
242 switch (CodeID) {
243 default: return 0;
244 case bitc::VST_CODE_ENTRY: return "ENTRY";
245 case bitc::VST_CODE_BBENTRY: return "BBENTRY";
246 }
247 }
248}
249
Chris Lattnere4775482009-04-27 17:59:34 +0000250struct PerRecordStats {
251 unsigned NumInstances;
Chris Lattnerc40ab842009-04-27 18:15:27 +0000252 unsigned NumAbbrev;
253 uint64_t TotalBits;
254
255 PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
Chris Lattnere4775482009-04-27 17:59:34 +0000256};
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257
258struct PerBlockIDStats {
259 /// NumInstances - This the number of times this block ID has been seen.
260 unsigned NumInstances;
261
262 /// NumBits - The total size in bits of all of these blocks.
263 uint64_t NumBits;
264
265 /// NumSubBlocks - The total number of blocks these blocks contain.
266 unsigned NumSubBlocks;
267
268 /// NumAbbrevs - The total number of abbreviations.
269 unsigned NumAbbrevs;
270
271 /// NumRecords - The total number of records these blocks contain, and the
272 /// number that are abbreviated.
273 unsigned NumRecords, NumAbbreviatedRecords;
274
275 /// CodeFreq - Keep track of the number of times we see each code.
Chris Lattnere4775482009-04-27 17:59:34 +0000276 std::vector<PerRecordStats> CodeFreq;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277
278 PerBlockIDStats()
279 : NumInstances(0), NumBits(0),
280 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
281};
282
283static std::map<unsigned, PerBlockIDStats> BlockIDStats;
284
285
286
287/// Error - All bitcode analysis errors go through this function, making this a
288/// good place to breakpoint if debugging.
289static bool Error(const std::string &Err) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000290 errs() << Err << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 return true;
292}
293
294/// ParseBlock - Read a block, updating statistics, etc.
Chris Lattner25a6abf2009-04-26 20:59:02 +0000295static bool ParseBlock(BitstreamCursor &Stream, unsigned IndentLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 std::string Indent(IndentLevel*2, ' ');
297 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
298 unsigned BlockID = Stream.ReadSubBlockID();
299
300 // Get the statistics for this BlockID.
301 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
302
303 BlockStats.NumInstances++;
304
305 // BLOCKINFO is a special part of the stream.
306 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000307 if (Dump) errs() << Indent << "<BLOCKINFO_BLOCK/>\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 if (Stream.ReadBlockInfoBlock())
309 return Error("Malformed BlockInfoBlock");
310 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
311 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
312 return false;
313 }
314
315 unsigned NumWords = 0;
316 if (Stream.EnterSubBlock(BlockID, &NumWords))
317 return Error("Malformed block record");
318
319 const char *BlockName = 0;
320 if (Dump) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000321 errs() << Indent << "<";
Chris Lattner71101322009-04-26 22:21:57 +0000322 if ((BlockName = GetBlockName(BlockID, *Stream.getBitStreamReader())))
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000323 errs() << BlockName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 else
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000325 errs() << "UnknownBlock" << BlockID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326
327 if (NonSymbolic && BlockName)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000328 errs() << " BlockID=" << BlockID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000330 errs() << " NumWords=" << NumWords
331 << " BlockCodeSize=" << Stream.GetAbbrevIDWidth() << ">\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 }
333
334 SmallVector<uint64_t, 64> Record;
335
336 // Read all the records for this block.
337 while (1) {
338 if (Stream.AtEndOfStream())
339 return Error("Premature end of bitstream");
340
Chris Lattnerc40ab842009-04-27 18:15:27 +0000341 uint64_t RecordStartBit = Stream.GetCurrentBitNo();
342
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 // Read the code for this record.
344 unsigned AbbrevID = Stream.ReadCode();
345 switch (AbbrevID) {
346 case bitc::END_BLOCK: {
347 if (Stream.ReadBlockEnd())
348 return Error("Error at end of block");
349 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
350 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
351 if (Dump) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000352 errs() << Indent << "</";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 if (BlockName)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000354 errs() << BlockName << ">\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 else
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000356 errs() << "UnknownBlock" << BlockID << ">\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 }
358 return false;
359 }
360 case bitc::ENTER_SUBBLOCK: {
361 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
362 if (ParseBlock(Stream, IndentLevel+1))
363 return true;
364 ++BlockStats.NumSubBlocks;
365 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
366
367 // Don't include subblock sizes in the size of this block.
368 BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
369 break;
370 }
371 case bitc::DEFINE_ABBREV:
372 Stream.ReadAbbrevRecord();
373 ++BlockStats.NumAbbrevs;
374 break;
375 default:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 Record.clear();
Chris Lattner7cbe0072009-04-06 22:44:40 +0000377
378 ++BlockStats.NumRecords;
Chris Lattnerc1894162009-04-07 02:56:46 +0000379 if (AbbrevID != bitc::UNABBREV_RECORD)
Chris Lattner7cbe0072009-04-06 22:44:40 +0000380 ++BlockStats.NumAbbreviatedRecords;
Chris Lattner7cbe0072009-04-06 22:44:40 +0000381
Chris Lattner7cbe0072009-04-06 22:44:40 +0000382 const char *BlobStart = 0;
383 unsigned BlobLen = 0;
Chris Lattnerc1894162009-04-07 02:56:46 +0000384 unsigned Code = Stream.ReadRecord(AbbrevID, Record, BlobStart, BlobLen);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385
Chris Lattnerc40ab842009-04-27 18:15:27 +0000386
387
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000388 // Increment the # occurrences of this code.
389 if (BlockStats.CodeFreq.size() <= Code)
390 BlockStats.CodeFreq.resize(Code+1);
Chris Lattnere4775482009-04-27 17:59:34 +0000391 BlockStats.CodeFreq[Code].NumInstances++;
Chris Lattnerc40ab842009-04-27 18:15:27 +0000392 BlockStats.CodeFreq[Code].TotalBits +=
393 Stream.GetCurrentBitNo()-RecordStartBit;
394 if (AbbrevID != bitc::UNABBREV_RECORD)
395 BlockStats.CodeFreq[Code].NumAbbrev++;
396
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000397 if (Dump) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000398 errs() << Indent << " <";
Chris Lattner71101322009-04-26 22:21:57 +0000399 if (const char *CodeName =
400 GetCodeName(Code, BlockID, *Stream.getBitStreamReader()))
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000401 errs() << CodeName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 else
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000403 errs() << "UnknownCode" << Code;
Chris Lattner71101322009-04-26 22:21:57 +0000404 if (NonSymbolic &&
405 GetCodeName(Code, BlockID, *Stream.getBitStreamReader()))
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000406 errs() << " codeid=" << Code;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 if (AbbrevID != bitc::UNABBREV_RECORD)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000408 errs() << " abbrevid=" << AbbrevID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409
410 for (unsigned i = 0, e = Record.size(); i != e; ++i)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000411 errs() << " op" << i << "=" << (int64_t)Record[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000413 errs() << "/>";
Chris Lattnerc1894162009-04-07 02:56:46 +0000414
415 if (BlobStart) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000416 errs() << " blob data = ";
Chris Lattnerc1894162009-04-07 02:56:46 +0000417 bool BlobIsPrintable = true;
418 for (unsigned i = 0; i != BlobLen; ++i)
419 if (!isprint(BlobStart[i])) {
420 BlobIsPrintable = false;
421 break;
422 }
423
424 if (BlobIsPrintable)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000425 errs() << "'" << std::string(BlobStart, BlobStart+BlobLen) <<"'";
Chris Lattnerc1894162009-04-07 02:56:46 +0000426 else
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000427 errs() << "unprintable, " << BlobLen << " bytes.";
Chris Lattnerc1894162009-04-07 02:56:46 +0000428 }
429
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000430 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000431 }
432
433 break;
434 }
435 }
436}
437
438static void PrintSize(double Bits) {
Chris Lattnere4775482009-04-27 17:59:34 +0000439 fprintf(stderr, "%.2f/%.2fB/%lluW", Bits, Bits/8,(unsigned long long)Bits/32);
440}
441static void PrintSize(uint64_t Bits) {
442 fprintf(stderr, "%llub/%.2fB/%lluW", (unsigned long long)Bits,
443 (double)Bits/8, (unsigned long long)Bits/32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444}
445
446
447/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
448static int AnalyzeBitcode() {
449 // Read the input file.
Chris Lattnerabd0e442009-04-06 20:54:32 +0000450 MemoryBuffer *MemBuf = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451
Chris Lattnerabd0e442009-04-06 20:54:32 +0000452 if (MemBuf == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 return Error("Error reading '" + InputFilename + "'.");
454
Chris Lattnerabd0e442009-04-06 20:54:32 +0000455 if (MemBuf->getBufferSize() & 3)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000456 return Error("Bitcode stream should be a multiple of 4 bytes in length");
457
Chris Lattnerabd0e442009-04-06 20:54:32 +0000458 unsigned char *BufPtr = (unsigned char *)MemBuf->getBufferStart();
459 unsigned char *EndBufPtr = BufPtr+MemBuf->getBufferSize();
460
461 // If we have a wrapper header, parse it and ignore the non-bc file contents.
462 // The magic number is 0x0B17C0DE stored in little endian.
463 if (isBitcodeWrapper(BufPtr, EndBufPtr))
464 if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr))
465 return Error("Invalid bitcode wrapper header");
466
Chris Lattner25a6abf2009-04-26 20:59:02 +0000467 BitstreamReader StreamFile(BufPtr, EndBufPtr);
468 BitstreamCursor Stream(StreamFile);
Chris Lattnerfe9f9c12009-04-27 20:04:08 +0000469 StreamFile.CollectBlockInfoNames();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470
471 // Read the stream signature.
472 char Signature[6];
473 Signature[0] = Stream.Read(8);
474 Signature[1] = Stream.Read(8);
475 Signature[2] = Stream.Read(4);
476 Signature[3] = Stream.Read(4);
477 Signature[4] = Stream.Read(4);
478 Signature[5] = Stream.Read(4);
479
480 // Autodetect the file contents, if it is one we know.
481 CurStreamType = UnknownBitstream;
482 if (Signature[0] == 'B' && Signature[1] == 'C' &&
483 Signature[2] == 0x0 && Signature[3] == 0xC &&
484 Signature[4] == 0xE && Signature[5] == 0xD)
485 CurStreamType = LLVMIRBitstream;
486
487 unsigned NumTopBlocks = 0;
488
489 // Parse the top-level structure. We only allow blocks at the top-level.
490 while (!Stream.AtEndOfStream()) {
491 unsigned Code = Stream.ReadCode();
492 if (Code != bitc::ENTER_SUBBLOCK)
493 return Error("Invalid record at top-level");
494
495 if (ParseBlock(Stream, 0))
496 return true;
497 ++NumTopBlocks;
498 }
499
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000500 if (Dump) errs() << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501
Chris Lattnerabd0e442009-04-06 20:54:32 +0000502 uint64_t BufferSizeBits = (EndBufPtr-BufPtr)*CHAR_BIT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000503 // Print a summary of the read file.
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000504 errs() << "Summary of " << InputFilename << ":\n";
505 errs() << " Total size: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 PrintSize(BufferSizeBits);
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000507 errs() << "\n";
508 errs() << " Stream type: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 switch (CurStreamType) {
510 default: assert(0 && "Unknown bitstream type");
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000511 case UnknownBitstream: errs() << "unknown\n"; break;
512 case LLVMIRBitstream: errs() << "LLVM IR\n"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 }
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000514 errs() << " # Toplevel Blocks: " << NumTopBlocks << "\n";
515 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516
517 // Emit per-block stats.
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000518 errs() << "Per-block Summary:\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000519 for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
520 E = BlockIDStats.end(); I != E; ++I) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000521 errs() << " Block ID #" << I->first;
Chris Lattner71101322009-04-26 22:21:57 +0000522 if (const char *BlockName = GetBlockName(I->first, StreamFile))
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000523 errs() << " (" << BlockName << ")";
524 errs() << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525
526 const PerBlockIDStats &Stats = I->second;
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000527 errs() << " Num Instances: " << Stats.NumInstances << "\n";
528 errs() << " Total Size: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000529 PrintSize(Stats.NumBits);
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000530 errs() << "\n";
531 errs() << " % of file: "
532 << Stats.NumBits/(double)BufferSizeBits*100 << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 if (Stats.NumInstances > 1) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000534 errs() << " Average Size: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 PrintSize(Stats.NumBits/(double)Stats.NumInstances);
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000536 errs() << "\n";
537 errs() << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
538 << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
539 errs() << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
540 << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
541 errs() << " Tot/Avg Records: " << Stats.NumRecords << "/"
542 << Stats.NumRecords/(double)Stats.NumInstances << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 } else {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000544 errs() << " Num SubBlocks: " << Stats.NumSubBlocks << "\n";
545 errs() << " Num Abbrevs: " << Stats.NumAbbrevs << "\n";
546 errs() << " Num Records: " << Stats.NumRecords << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 }
548 if (Stats.NumRecords)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000549 errs() << " % Abbrev Recs: " << (Stats.NumAbbreviatedRecords/
550 (double)Stats.NumRecords)*100 << "\n";
551 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000552
553 // Print a histogram of the codes we see.
554 if (!NoHistogram && !Stats.CodeFreq.empty()) {
555 std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code>
556 for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
Chris Lattnere4775482009-04-27 17:59:34 +0000557 if (unsigned Freq = Stats.CodeFreq[i].NumInstances)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000558 FreqPairs.push_back(std::make_pair(Freq, i));
559 std::stable_sort(FreqPairs.begin(), FreqPairs.end());
560 std::reverse(FreqPairs.begin(), FreqPairs.end());
561
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000562 errs() << "\tRecord Histogram:\n";
Chris Lattnerc40ab842009-04-27 18:15:27 +0000563 fprintf(stderr, "\t\t Count # Bits %% Abv Record Kind\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000564 for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
Chris Lattnerc40ab842009-04-27 18:15:27 +0000565 const PerRecordStats &RecStats = Stats.CodeFreq[FreqPairs[i].second];
566
567 fprintf(stderr, "\t\t%7d %9llu ", RecStats.NumInstances,
Dan Gohmand31c4192009-05-01 16:33:33 +0000568 (unsigned long long)RecStats.TotalBits);
Chris Lattnerc40ab842009-04-27 18:15:27 +0000569
570 if (RecStats.NumAbbrev)
571 fprintf(stderr, "%7.2f ",
572 (double)RecStats.NumAbbrev/RecStats.NumInstances*100);
573 else
574 fprintf(stderr, " ");
Chris Lattnere4775482009-04-27 17:59:34 +0000575
Chris Lattner71101322009-04-26 22:21:57 +0000576 if (const char *CodeName =
577 GetCodeName(FreqPairs[i].second, I->first, StreamFile))
Chris Lattnere4775482009-04-27 17:59:34 +0000578 fprintf(stderr, "%s\n", CodeName);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000579 else
Chris Lattnere4775482009-04-27 17:59:34 +0000580 fprintf(stderr, "UnknownCode%d\n", FreqPairs[i].second);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000581 }
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000582 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000583
584 }
585 }
586 return 0;
587}
588
589
590int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +0000591 // Print a stack trace if we signal out.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000592 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere6012df2009-03-06 05:34:10 +0000593 PrettyStackTraceProgram X(argc, argv);
594 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
595 cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000596
597 return AnalyzeBitcode();
598}