blob: db9b1312f01690b69964210a11a30b5aea70cffa [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";
Devang Patel0c0a6ca2009-07-22 17:43:22 +0000101 case bitc::METADATA_BLOCK_ID: return "METADATA_BLOCK";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 }
103}
104
105/// GetCodeName - Return a symbolic code name if known, otherwise return
106/// null.
Chris Lattner71101322009-04-26 22:21:57 +0000107static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
108 const BitstreamReader &StreamFile) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 // Standard blocks for all bitcode files.
110 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
111 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
112 switch (CodeID) {
113 default: return 0;
Chris Lattner71101322009-04-26 22:21:57 +0000114 case bitc::BLOCKINFO_CODE_SETBID: return "SETBID";
115 case bitc::BLOCKINFO_CODE_BLOCKNAME: return "BLOCKNAME";
116 case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 }
118 }
119 return 0;
120 }
121
Chris Lattner71101322009-04-26 22:21:57 +0000122 // Check to see if we have a blockinfo record for this record, with a name.
123 if (const BitstreamReader::BlockInfo *Info =
124 StreamFile.getBlockInfo(BlockID)) {
125 for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i)
126 if (Info->RecordNames[i].first == CodeID)
127 return Info->RecordNames[i].second.c_str();
128 }
129
130
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131 if (CurStreamType != LLVMIRBitstream) return 0;
132
133 switch (BlockID) {
134 default: return 0;
135 case bitc::MODULE_BLOCK_ID:
136 switch (CodeID) {
137 default: return 0;
138 case bitc::MODULE_CODE_VERSION: return "VERSION";
139 case bitc::MODULE_CODE_TRIPLE: return "TRIPLE";
140 case bitc::MODULE_CODE_DATALAYOUT: return "DATALAYOUT";
141 case bitc::MODULE_CODE_ASM: return "ASM";
142 case bitc::MODULE_CODE_SECTIONNAME: return "SECTIONNAME";
143 case bitc::MODULE_CODE_DEPLIB: return "DEPLIB";
144 case bitc::MODULE_CODE_GLOBALVAR: return "GLOBALVAR";
145 case bitc::MODULE_CODE_FUNCTION: return "FUNCTION";
146 case bitc::MODULE_CODE_ALIAS: return "ALIAS";
147 case bitc::MODULE_CODE_PURGEVALS: return "PURGEVALS";
Nick Lewyckyb90b8412008-11-07 14:52:51 +0000148 case bitc::MODULE_CODE_GCNAME: return "GCNAME";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149 }
150 case bitc::PARAMATTR_BLOCK_ID:
151 switch (CodeID) {
152 default: return 0;
153 case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY";
154 }
155 case bitc::TYPE_BLOCK_ID:
156 switch (CodeID) {
157 default: return 0;
Nick Lewyckyb90b8412008-11-07 14:52:51 +0000158 case bitc::TYPE_CODE_NUMENTRY: return "NUMENTRY";
159 case bitc::TYPE_CODE_VOID: return "VOID";
160 case bitc::TYPE_CODE_FLOAT: return "FLOAT";
161 case bitc::TYPE_CODE_DOUBLE: return "DOUBLE";
162 case bitc::TYPE_CODE_LABEL: return "LABEL";
163 case bitc::TYPE_CODE_OPAQUE: return "OPAQUE";
164 case bitc::TYPE_CODE_INTEGER: return "INTEGER";
165 case bitc::TYPE_CODE_POINTER: return "POINTER";
166 case bitc::TYPE_CODE_FUNCTION: return "FUNCTION";
167 case bitc::TYPE_CODE_STRUCT: return "STRUCT";
168 case bitc::TYPE_CODE_ARRAY: return "ARRAY";
169 case bitc::TYPE_CODE_VECTOR: return "VECTOR";
170 case bitc::TYPE_CODE_X86_FP80: return "X86_FP80";
171 case bitc::TYPE_CODE_FP128: return "FP128";
172 case bitc::TYPE_CODE_PPC_FP128: return "PPC_FP128";
Nick Lewycky15714342009-06-01 04:41:03 +0000173 case bitc::TYPE_CODE_METADATA: return "METADATA";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 }
175
176 case bitc::CONSTANTS_BLOCK_ID:
177 switch (CodeID) {
178 default: return 0;
179 case bitc::CST_CODE_SETTYPE: return "SETTYPE";
180 case bitc::CST_CODE_NULL: return "NULL";
181 case bitc::CST_CODE_UNDEF: return "UNDEF";
182 case bitc::CST_CODE_INTEGER: return "INTEGER";
183 case bitc::CST_CODE_WIDE_INTEGER: return "WIDE_INTEGER";
184 case bitc::CST_CODE_FLOAT: return "FLOAT";
185 case bitc::CST_CODE_AGGREGATE: return "AGGREGATE";
186 case bitc::CST_CODE_STRING: return "STRING";
187 case bitc::CST_CODE_CSTRING: return "CSTRING";
188 case bitc::CST_CODE_CE_BINOP: return "CE_BINOP";
189 case bitc::CST_CODE_CE_CAST: return "CE_CAST";
190 case bitc::CST_CODE_CE_GEP: return "CE_GEP";
191 case bitc::CST_CODE_CE_SELECT: return "CE_SELECT";
192 case bitc::CST_CODE_CE_EXTRACTELT: return "CE_EXTRACTELT";
193 case bitc::CST_CODE_CE_INSERTELT: return "CE_INSERTELT";
194 case bitc::CST_CODE_CE_SHUFFLEVEC: return "CE_SHUFFLEVEC";
195 case bitc::CST_CODE_CE_CMP: return "CE_CMP";
196 case bitc::CST_CODE_INLINEASM: return "INLINEASM";
Nick Lewycky15714342009-06-01 04:41:03 +0000197 case bitc::CST_CODE_CE_SHUFVEC_EX: return "CE_SHUFVEC_EX";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000198 }
199 case bitc::FUNCTION_BLOCK_ID:
200 switch (CodeID) {
201 default: return 0;
202 case bitc::FUNC_CODE_DECLAREBLOCKS: return "DECLAREBLOCKS";
203
204 case bitc::FUNC_CODE_INST_BINOP: return "INST_BINOP";
205 case bitc::FUNC_CODE_INST_CAST: return "INST_CAST";
206 case bitc::FUNC_CODE_INST_GEP: return "INST_GEP";
207 case bitc::FUNC_CODE_INST_SELECT: return "INST_SELECT";
208 case bitc::FUNC_CODE_INST_EXTRACTELT: return "INST_EXTRACTELT";
209 case bitc::FUNC_CODE_INST_INSERTELT: return "INST_INSERTELT";
210 case bitc::FUNC_CODE_INST_SHUFFLEVEC: return "INST_SHUFFLEVEC";
211 case bitc::FUNC_CODE_INST_CMP: return "INST_CMP";
212
213 case bitc::FUNC_CODE_INST_RET: return "INST_RET";
214 case bitc::FUNC_CODE_INST_BR: return "INST_BR";
215 case bitc::FUNC_CODE_INST_SWITCH: return "INST_SWITCH";
216 case bitc::FUNC_CODE_INST_INVOKE: return "INST_INVOKE";
217 case bitc::FUNC_CODE_INST_UNWIND: return "INST_UNWIND";
218 case bitc::FUNC_CODE_INST_UNREACHABLE: return "INST_UNREACHABLE";
219
220 case bitc::FUNC_CODE_INST_PHI: return "INST_PHI";
221 case bitc::FUNC_CODE_INST_MALLOC: return "INST_MALLOC";
222 case bitc::FUNC_CODE_INST_FREE: return "INST_FREE";
223 case bitc::FUNC_CODE_INST_ALLOCA: return "INST_ALLOCA";
224 case bitc::FUNC_CODE_INST_LOAD: return "INST_LOAD";
225 case bitc::FUNC_CODE_INST_STORE: return "INST_STORE";
226 case bitc::FUNC_CODE_INST_CALL: return "INST_CALL";
227 case bitc::FUNC_CODE_INST_VAARG: return "INST_VAARG";
Christopher Lamb44d62f62007-12-11 08:59:05 +0000228 case bitc::FUNC_CODE_INST_STORE2: return "INST_STORE2";
Nick Lewycky2c3eced2008-03-01 21:47:06 +0000229 case bitc::FUNC_CODE_INST_GETRESULT: return "INST_GETRESULT";
Nick Lewyckyb90b8412008-11-07 14:52:51 +0000230 case bitc::FUNC_CODE_INST_EXTRACTVAL: return "INST_EXTRACTVAL";
231 case bitc::FUNC_CODE_INST_INSERTVAL: return "INST_INSERTVAL";
232 case bitc::FUNC_CODE_INST_CMP2: return "INST_CMP2";
233 case bitc::FUNC_CODE_INST_VSELECT: return "INST_VSELECT";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 }
235 case bitc::TYPE_SYMTAB_BLOCK_ID:
236 switch (CodeID) {
237 default: return 0;
238 case bitc::TST_CODE_ENTRY: return "ENTRY";
239 }
240 case bitc::VALUE_SYMTAB_BLOCK_ID:
241 switch (CodeID) {
242 default: return 0;
243 case bitc::VST_CODE_ENTRY: return "ENTRY";
244 case bitc::VST_CODE_BBENTRY: return "BBENTRY";
245 }
Devang Patel0c0a6ca2009-07-22 17:43:22 +0000246 case bitc::METADATA_BLOCK_ID:
247 switch(CodeID) {
248 default:return 0;
249 case bitc::METADATA_STRING: return "MDSTRING";
Devang Patel54199ef2009-07-23 01:07:34 +0000250 case bitc::METADATA_NODE: return "MDNODE";
Devang Patel5288eae2009-07-30 23:03:19 +0000251 case bitc::METADATA_NAME: return "METADATA_NAME";
252 case bitc::METADATA_NAMED_NODE: return "NAMEDMDNODE";
Devang Patel0c0a6ca2009-07-22 17:43:22 +0000253 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 }
255}
256
Chris Lattnere4775482009-04-27 17:59:34 +0000257struct PerRecordStats {
258 unsigned NumInstances;
Chris Lattnerc40ab842009-04-27 18:15:27 +0000259 unsigned NumAbbrev;
260 uint64_t TotalBits;
261
262 PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
Chris Lattnere4775482009-04-27 17:59:34 +0000263};
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264
265struct PerBlockIDStats {
266 /// NumInstances - This the number of times this block ID has been seen.
267 unsigned NumInstances;
268
269 /// NumBits - The total size in bits of all of these blocks.
270 uint64_t NumBits;
271
272 /// NumSubBlocks - The total number of blocks these blocks contain.
273 unsigned NumSubBlocks;
274
275 /// NumAbbrevs - The total number of abbreviations.
276 unsigned NumAbbrevs;
277
278 /// NumRecords - The total number of records these blocks contain, and the
279 /// number that are abbreviated.
280 unsigned NumRecords, NumAbbreviatedRecords;
281
282 /// CodeFreq - Keep track of the number of times we see each code.
Chris Lattnere4775482009-04-27 17:59:34 +0000283 std::vector<PerRecordStats> CodeFreq;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284
285 PerBlockIDStats()
286 : NumInstances(0), NumBits(0),
287 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
288};
289
290static std::map<unsigned, PerBlockIDStats> BlockIDStats;
291
292
293
294/// Error - All bitcode analysis errors go through this function, making this a
295/// good place to breakpoint if debugging.
296static bool Error(const std::string &Err) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000297 errs() << Err << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000298 return true;
299}
300
301/// ParseBlock - Read a block, updating statistics, etc.
Chris Lattner25a6abf2009-04-26 20:59:02 +0000302static bool ParseBlock(BitstreamCursor &Stream, unsigned IndentLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 std::string Indent(IndentLevel*2, ' ');
304 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
305 unsigned BlockID = Stream.ReadSubBlockID();
306
307 // Get the statistics for this BlockID.
308 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
309
310 BlockStats.NumInstances++;
311
312 // BLOCKINFO is a special part of the stream.
313 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000314 if (Dump) errs() << Indent << "<BLOCKINFO_BLOCK/>\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315 if (Stream.ReadBlockInfoBlock())
316 return Error("Malformed BlockInfoBlock");
317 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
318 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
319 return false;
320 }
321
322 unsigned NumWords = 0;
323 if (Stream.EnterSubBlock(BlockID, &NumWords))
324 return Error("Malformed block record");
325
326 const char *BlockName = 0;
327 if (Dump) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000328 errs() << Indent << "<";
Chris Lattner71101322009-04-26 22:21:57 +0000329 if ((BlockName = GetBlockName(BlockID, *Stream.getBitStreamReader())))
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000330 errs() << BlockName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000331 else
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000332 errs() << "UnknownBlock" << BlockID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000333
334 if (NonSymbolic && BlockName)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000335 errs() << " BlockID=" << BlockID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000336
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000337 errs() << " NumWords=" << NumWords
338 << " BlockCodeSize=" << Stream.GetAbbrevIDWidth() << ">\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000339 }
340
341 SmallVector<uint64_t, 64> Record;
342
343 // Read all the records for this block.
344 while (1) {
345 if (Stream.AtEndOfStream())
346 return Error("Premature end of bitstream");
347
Chris Lattnerc40ab842009-04-27 18:15:27 +0000348 uint64_t RecordStartBit = Stream.GetCurrentBitNo();
349
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 // Read the code for this record.
351 unsigned AbbrevID = Stream.ReadCode();
352 switch (AbbrevID) {
353 case bitc::END_BLOCK: {
354 if (Stream.ReadBlockEnd())
355 return Error("Error at end of block");
356 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
357 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
358 if (Dump) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000359 errs() << Indent << "</";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000360 if (BlockName)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000361 errs() << BlockName << ">\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 else
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000363 errs() << "UnknownBlock" << BlockID << ">\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 }
365 return false;
366 }
367 case bitc::ENTER_SUBBLOCK: {
368 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
369 if (ParseBlock(Stream, IndentLevel+1))
370 return true;
371 ++BlockStats.NumSubBlocks;
372 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
373
374 // Don't include subblock sizes in the size of this block.
375 BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
376 break;
377 }
378 case bitc::DEFINE_ABBREV:
379 Stream.ReadAbbrevRecord();
380 ++BlockStats.NumAbbrevs;
381 break;
382 default:
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 Record.clear();
Chris Lattner7cbe0072009-04-06 22:44:40 +0000384
385 ++BlockStats.NumRecords;
Chris Lattnerc1894162009-04-07 02:56:46 +0000386 if (AbbrevID != bitc::UNABBREV_RECORD)
Chris Lattner7cbe0072009-04-06 22:44:40 +0000387 ++BlockStats.NumAbbreviatedRecords;
Chris Lattner7cbe0072009-04-06 22:44:40 +0000388
Chris Lattner7cbe0072009-04-06 22:44:40 +0000389 const char *BlobStart = 0;
390 unsigned BlobLen = 0;
Chris Lattnerc1894162009-04-07 02:56:46 +0000391 unsigned Code = Stream.ReadRecord(AbbrevID, Record, BlobStart, BlobLen);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392
Chris Lattnerc40ab842009-04-27 18:15:27 +0000393
394
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000395 // Increment the # occurrences of this code.
396 if (BlockStats.CodeFreq.size() <= Code)
397 BlockStats.CodeFreq.resize(Code+1);
Chris Lattnere4775482009-04-27 17:59:34 +0000398 BlockStats.CodeFreq[Code].NumInstances++;
Chris Lattnerc40ab842009-04-27 18:15:27 +0000399 BlockStats.CodeFreq[Code].TotalBits +=
400 Stream.GetCurrentBitNo()-RecordStartBit;
401 if (AbbrevID != bitc::UNABBREV_RECORD)
402 BlockStats.CodeFreq[Code].NumAbbrev++;
403
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000404 if (Dump) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000405 errs() << Indent << " <";
Chris Lattner71101322009-04-26 22:21:57 +0000406 if (const char *CodeName =
407 GetCodeName(Code, BlockID, *Stream.getBitStreamReader()))
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000408 errs() << CodeName;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 else
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000410 errs() << "UnknownCode" << Code;
Chris Lattner71101322009-04-26 22:21:57 +0000411 if (NonSymbolic &&
412 GetCodeName(Code, BlockID, *Stream.getBitStreamReader()))
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000413 errs() << " codeid=" << Code;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 if (AbbrevID != bitc::UNABBREV_RECORD)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000415 errs() << " abbrevid=" << AbbrevID;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416
417 for (unsigned i = 0, e = Record.size(); i != e; ++i)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000418 errs() << " op" << i << "=" << (int64_t)Record[i];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000419
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000420 errs() << "/>";
Chris Lattnerc1894162009-04-07 02:56:46 +0000421
422 if (BlobStart) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000423 errs() << " blob data = ";
Chris Lattnerc1894162009-04-07 02:56:46 +0000424 bool BlobIsPrintable = true;
425 for (unsigned i = 0; i != BlobLen; ++i)
426 if (!isprint(BlobStart[i])) {
427 BlobIsPrintable = false;
428 break;
429 }
430
431 if (BlobIsPrintable)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000432 errs() << "'" << std::string(BlobStart, BlobStart+BlobLen) <<"'";
Chris Lattnerc1894162009-04-07 02:56:46 +0000433 else
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000434 errs() << "unprintable, " << BlobLen << " bytes.";
Chris Lattnerc1894162009-04-07 02:56:46 +0000435 }
436
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000437 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 }
439
440 break;
441 }
442 }
443}
444
445static void PrintSize(double Bits) {
Chris Lattnere4775482009-04-27 17:59:34 +0000446 fprintf(stderr, "%.2f/%.2fB/%lluW", Bits, Bits/8,(unsigned long long)Bits/32);
447}
448static void PrintSize(uint64_t Bits) {
449 fprintf(stderr, "%llub/%.2fB/%lluW", (unsigned long long)Bits,
450 (double)Bits/8, (unsigned long long)Bits/32);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000451}
452
453
454/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
455static int AnalyzeBitcode() {
456 // Read the input file.
Chris Lattnerabd0e442009-04-06 20:54:32 +0000457 MemoryBuffer *MemBuf = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000458
Chris Lattnerabd0e442009-04-06 20:54:32 +0000459 if (MemBuf == 0)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000460 return Error("Error reading '" + InputFilename + "'.");
461
Chris Lattnerabd0e442009-04-06 20:54:32 +0000462 if (MemBuf->getBufferSize() & 3)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 return Error("Bitcode stream should be a multiple of 4 bytes in length");
464
Chris Lattnerabd0e442009-04-06 20:54:32 +0000465 unsigned char *BufPtr = (unsigned char *)MemBuf->getBufferStart();
466 unsigned char *EndBufPtr = BufPtr+MemBuf->getBufferSize();
467
468 // If we have a wrapper header, parse it and ignore the non-bc file contents.
469 // The magic number is 0x0B17C0DE stored in little endian.
470 if (isBitcodeWrapper(BufPtr, EndBufPtr))
471 if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr))
472 return Error("Invalid bitcode wrapper header");
473
Chris Lattner25a6abf2009-04-26 20:59:02 +0000474 BitstreamReader StreamFile(BufPtr, EndBufPtr);
475 BitstreamCursor Stream(StreamFile);
Chris Lattnerfe9f9c12009-04-27 20:04:08 +0000476 StreamFile.CollectBlockInfoNames();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000477
478 // Read the stream signature.
479 char Signature[6];
480 Signature[0] = Stream.Read(8);
481 Signature[1] = Stream.Read(8);
482 Signature[2] = Stream.Read(4);
483 Signature[3] = Stream.Read(4);
484 Signature[4] = Stream.Read(4);
485 Signature[5] = Stream.Read(4);
486
487 // Autodetect the file contents, if it is one we know.
488 CurStreamType = UnknownBitstream;
489 if (Signature[0] == 'B' && Signature[1] == 'C' &&
490 Signature[2] == 0x0 && Signature[3] == 0xC &&
491 Signature[4] == 0xE && Signature[5] == 0xD)
492 CurStreamType = LLVMIRBitstream;
493
494 unsigned NumTopBlocks = 0;
495
496 // Parse the top-level structure. We only allow blocks at the top-level.
497 while (!Stream.AtEndOfStream()) {
498 unsigned Code = Stream.ReadCode();
499 if (Code != bitc::ENTER_SUBBLOCK)
500 return Error("Invalid record at top-level");
501
502 if (ParseBlock(Stream, 0))
503 return true;
504 ++NumTopBlocks;
505 }
506
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000507 if (Dump) errs() << "\n\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000508
Chris Lattnerabd0e442009-04-06 20:54:32 +0000509 uint64_t BufferSizeBits = (EndBufPtr-BufPtr)*CHAR_BIT;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 // Print a summary of the read file.
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000511 errs() << "Summary of " << InputFilename << ":\n";
512 errs() << " Total size: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 PrintSize(BufferSizeBits);
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000514 errs() << "\n";
515 errs() << " Stream type: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000516 switch (CurStreamType) {
517 default: assert(0 && "Unknown bitstream type");
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000518 case UnknownBitstream: errs() << "unknown\n"; break;
519 case LLVMIRBitstream: errs() << "LLVM IR\n"; break;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 }
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000521 errs() << " # Toplevel Blocks: " << NumTopBlocks << "\n";
522 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000523
524 // Emit per-block stats.
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000525 errs() << "Per-block Summary:\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526 for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
527 E = BlockIDStats.end(); I != E; ++I) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000528 errs() << " Block ID #" << I->first;
Chris Lattner71101322009-04-26 22:21:57 +0000529 if (const char *BlockName = GetBlockName(I->first, StreamFile))
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000530 errs() << " (" << BlockName << ")";
531 errs() << ":\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000532
533 const PerBlockIDStats &Stats = I->second;
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000534 errs() << " Num Instances: " << Stats.NumInstances << "\n";
535 errs() << " Total Size: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000536 PrintSize(Stats.NumBits);
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000537 errs() << "\n";
538 errs() << " % of file: "
539 << Stats.NumBits/(double)BufferSizeBits*100 << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000540 if (Stats.NumInstances > 1) {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000541 errs() << " Average Size: ";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000542 PrintSize(Stats.NumBits/(double)Stats.NumInstances);
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000543 errs() << "\n";
544 errs() << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
545 << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
546 errs() << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
547 << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
548 errs() << " Tot/Avg Records: " << Stats.NumRecords << "/"
549 << Stats.NumRecords/(double)Stats.NumInstances << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 } else {
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000551 errs() << " Num SubBlocks: " << Stats.NumSubBlocks << "\n";
552 errs() << " Num Abbrevs: " << Stats.NumAbbrevs << "\n";
553 errs() << " Num Records: " << Stats.NumRecords << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 }
555 if (Stats.NumRecords)
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000556 errs() << " % Abbrev Recs: " << (Stats.NumAbbreviatedRecords/
557 (double)Stats.NumRecords)*100 << "\n";
558 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000559
560 // Print a histogram of the codes we see.
561 if (!NoHistogram && !Stats.CodeFreq.empty()) {
562 std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code>
563 for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
Chris Lattnere4775482009-04-27 17:59:34 +0000564 if (unsigned Freq = Stats.CodeFreq[i].NumInstances)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000565 FreqPairs.push_back(std::make_pair(Freq, i));
566 std::stable_sort(FreqPairs.begin(), FreqPairs.end());
567 std::reverse(FreqPairs.begin(), FreqPairs.end());
568
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000569 errs() << "\tRecord Histogram:\n";
Chris Lattnerc40ab842009-04-27 18:15:27 +0000570 fprintf(stderr, "\t\t Count # Bits %% Abv Record Kind\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000571 for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
Chris Lattnerc40ab842009-04-27 18:15:27 +0000572 const PerRecordStats &RecStats = Stats.CodeFreq[FreqPairs[i].second];
573
574 fprintf(stderr, "\t\t%7d %9llu ", RecStats.NumInstances,
Dan Gohmand31c4192009-05-01 16:33:33 +0000575 (unsigned long long)RecStats.TotalBits);
Chris Lattnerc40ab842009-04-27 18:15:27 +0000576
577 if (RecStats.NumAbbrev)
578 fprintf(stderr, "%7.2f ",
579 (double)RecStats.NumAbbrev/RecStats.NumInstances*100);
580 else
581 fprintf(stderr, " ");
Chris Lattnere4775482009-04-27 17:59:34 +0000582
Chris Lattner71101322009-04-26 22:21:57 +0000583 if (const char *CodeName =
584 GetCodeName(FreqPairs[i].second, I->first, StreamFile))
Chris Lattnere4775482009-04-27 17:59:34 +0000585 fprintf(stderr, "%s\n", CodeName);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000586 else
Chris Lattnere4775482009-04-27 17:59:34 +0000587 fprintf(stderr, "UnknownCode%d\n", FreqPairs[i].second);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000588 }
Dan Gohmanf8b81bf2009-07-15 16:35:29 +0000589 errs() << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000590
591 }
592 }
593 return 0;
594}
595
596
597int main(int argc, char **argv) {
Chris Lattnere6012df2009-03-06 05:34:10 +0000598 // Print a stack trace if we signal out.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000599 sys::PrintStackTraceOnErrorSignal();
Chris Lattnere6012df2009-03-06 05:34:10 +0000600 PrettyStackTraceProgram X(argc, argv);
601 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
602 cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000603
604 return AnalyzeBitcode();
605}