blob: 1cd23679edd3fafea7ad0c1180334cca864934e8 [file] [log] [blame]
Gabor Greif8ff70c22007-07-04 21:55:50 +00001//===-- llvm-bcanalyzer.cpp - Bitcode Analyzer --------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Reid Spencerdac69c82004-06-07 17:53:43 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
Reid Spencerdac69c82004-06-07 17:53:43 +00008//===----------------------------------------------------------------------===//
9//
Reid Spencer96684ef2004-06-08 05:56:58 +000010// This tool may be invoked in the following manner:
Gabor Greif8ff70c22007-07-04 21:55:50 +000011// llvm-bcanalyzer [options] - Read LLVM bitcode from stdin
12// llvm-bcanalyzer [options] x.bc - Read LLVM bitcode from the x.bc file
Reid Spencerdac69c82004-06-07 17:53:43 +000013//
Reid Spencer96684ef2004-06-08 05:56:58 +000014// Options:
Reid Spencer23d46e72004-06-10 18:38:44 +000015// --help - Output information about command line switches
Gabor Greif8ff70c22007-07-04 21:55:50 +000016// --dump - Dump low-level bitcode structure in readable format
Reid Spencer96684ef2004-06-08 05:56:58 +000017//
Gabor Greif8ff70c22007-07-04 21:55:50 +000018// This tool provides analytical information about a bitcode file. It is
19// intended as an aid to developers of bitcode reading and writing software. It
20// produces on std::out a summary of the bitcode file that shows various
Reid Spencer23d46e72004-06-10 18:38:44 +000021// statistics about the contents of the file. By default this information is
Gabor Greif8ff70c22007-07-04 21:55:50 +000022// detailed and contains information about individual bitcode blocks and the
Chris Lattner44dadff2007-05-06 09:29:57 +000023// functions in the module.
Gabor Greif8ff70c22007-07-04 21:55:50 +000024// The tool is also able to print a bitcode file in a straight forward text
Misha Brukman3da94ae2005-04-22 00:00:37 +000025// format that shows the containment and relationships of the information in
Gabor Greif8ff70c22007-07-04 21:55:50 +000026// the bitcode file (-dump option).
Chris Lattner63db4852007-04-29 05:51:00 +000027//
Reid Spencerdac69c82004-06-07 17:53:43 +000028//===----------------------------------------------------------------------===//
29
Reid Spencer86a9a7a2004-06-29 23:34:27 +000030#include "llvm/Analysis/Verifier.h"
Chris Lattner45e0f892007-04-29 08:12:22 +000031#include "llvm/Bitcode/BitstreamReader.h"
Chris Lattner4238d472007-04-29 20:00:02 +000032#include "llvm/Bitcode/LLVMBitCodes.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/Support/CommandLine.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000034#include "llvm/Support/ManagedStatic.h"
Chris Lattner45e0f892007-04-29 08:12:22 +000035#include "llvm/Support/MemoryBuffer.h"
Reid Spencerdac69c82004-06-07 17:53:43 +000036#include "llvm/System/Signals.h"
Chris Lattner44dadff2007-05-06 09:29:57 +000037#include <map>
Reid Spencerdac69c82004-06-07 17:53:43 +000038#include <fstream>
39#include <iostream>
Chris Lattnerb1e85b52007-05-05 01:46:49 +000040#include <algorithm>
Reid Spencerdac69c82004-06-07 17:53:43 +000041using namespace llvm;
42
43static cl::opt<std::string>
Gabor Greif8ff70c22007-07-04 21:55:50 +000044 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Reid Spencerdac69c82004-06-07 17:53:43 +000045
Reid Spencer75937452004-08-21 21:00:24 +000046static cl::opt<std::string>
47 OutputFilename("-o", cl::init("-"), cl::desc("<output file>"));
48
Gabor Greif8ff70c22007-07-04 21:55:50 +000049static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace"));
Chris Lattner32de6332007-04-29 08:31:14 +000050
51//===----------------------------------------------------------------------===//
52// Bitcode specific analysis.
53//===----------------------------------------------------------------------===//
54
Chris Lattnerb1e85b52007-05-05 01:46:49 +000055static cl::opt<bool> NoHistogram("disable-histogram",
56 cl::desc("Do not print per-code histogram"));
Chris Lattner45e0f892007-04-29 08:12:22 +000057
Chris Lattnerb30c9252007-04-29 21:48:19 +000058static cl::opt<bool>
59NonSymbolic("non-symbolic",
60 cl::desc("Emit numberic info in dump even if"
61 " symbolic info is available"));
62
Chris Lattner45e0f892007-04-29 08:12:22 +000063/// 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
Chris Lattner4238d472007-04-29 20:00:02 +000070
71/// GetBlockName - Return a symbolic block name if known, otherwise return
Chris Lattnerb30c9252007-04-29 21:48:19 +000072/// null.
Chris Lattner4238d472007-04-29 20:00:02 +000073static const char *GetBlockName(unsigned BlockID) {
Chris Lattner1772b122007-05-05 00:17:42 +000074 // Standard blocks for all bitcode files.
75 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
76 if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
77 return "BLOCKINFO_BLOCK";
78 return 0;
79 }
80
Chris Lattnerb30c9252007-04-29 21:48:19 +000081 if (CurStreamType != LLVMIRBitstream) return 0;
Chris Lattner4238d472007-04-29 20:00:02 +000082
83 switch (BlockID) {
Chris Lattnerb30c9252007-04-29 21:48:19 +000084 default: return 0;
Chris Lattner4238d472007-04-29 20:00:02 +000085 case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK";
Chris Lattnercd5b7d72007-05-04 03:01:41 +000086 case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK";
Chris Lattner4238d472007-04-29 20:00:02 +000087 case bitc::TYPE_BLOCK_ID: return "TYPE_BLOCK";
88 case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK";
89 case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK";
90 case bitc::TYPE_SYMTAB_BLOCK_ID: return "TYPE_SYMTAB";
91 case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB";
92 }
93}
94
Chris Lattnerb30c9252007-04-29 21:48:19 +000095/// GetCodeName - Return a symbolic code name if known, otherwise return
96/// null.
97static const char *GetCodeName(unsigned CodeID, unsigned BlockID) {
Chris Lattner1772b122007-05-05 00:17:42 +000098 // Standard blocks for all bitcode files.
99 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
100 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
101 switch (CodeID) {
102 default: return 0;
103 case bitc::MODULE_CODE_VERSION: return "VERSION";
104 }
105 }
106 return 0;
107 }
108
Chris Lattnerb30c9252007-04-29 21:48:19 +0000109 if (CurStreamType != LLVMIRBitstream) return 0;
110
111 switch (BlockID) {
112 default: return 0;
113 case bitc::MODULE_BLOCK_ID:
114 switch (CodeID) {
115 default: return 0;
116 case bitc::MODULE_CODE_VERSION: return "VERSION";
117 case bitc::MODULE_CODE_TRIPLE: return "TRIPLE";
118 case bitc::MODULE_CODE_DATALAYOUT: return "DATALAYOUT";
119 case bitc::MODULE_CODE_ASM: return "ASM";
120 case bitc::MODULE_CODE_SECTIONNAME: return "SECTIONNAME";
121 case bitc::MODULE_CODE_DEPLIB: return "DEPLIB";
122 case bitc::MODULE_CODE_GLOBALVAR: return "GLOBALVAR";
123 case bitc::MODULE_CODE_FUNCTION: return "FUNCTION";
124 case bitc::MODULE_CODE_ALIAS: return "ALIAS";
125 case bitc::MODULE_CODE_PURGEVALS: return "PURGEVALS";
126 }
Chris Lattnercd5b7d72007-05-04 03:01:41 +0000127 case bitc::PARAMATTR_BLOCK_ID:
128 switch (CodeID) {
129 default: return 0;
130 case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY";
131 }
Chris Lattnerb30c9252007-04-29 21:48:19 +0000132 case bitc::TYPE_BLOCK_ID:
133 switch (CodeID) {
134 default: return 0;
135 case bitc::TYPE_CODE_NUMENTRY: return "NUMENTRY";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000136 case bitc::TYPE_CODE_VOID: return "VOID";
137 case bitc::TYPE_CODE_FLOAT: return "FLOAT";
138 case bitc::TYPE_CODE_DOUBLE: return "DOUBLE";
139 case bitc::TYPE_CODE_LABEL: return "LABEL";
140 case bitc::TYPE_CODE_OPAQUE: return "OPAQUE";
141 case bitc::TYPE_CODE_INTEGER: return "INTEGER";
142 case bitc::TYPE_CODE_POINTER: return "POINTER";
143 case bitc::TYPE_CODE_FUNCTION: return "FUNCTION";
144 case bitc::TYPE_CODE_STRUCT: return "STRUCT";
145 case bitc::TYPE_CODE_ARRAY: return "ARRAY";
146 case bitc::TYPE_CODE_VECTOR: return "VECTOR";
147 }
148
149 case bitc::CONSTANTS_BLOCK_ID:
150 switch (CodeID) {
151 default: return 0;
152 case bitc::CST_CODE_SETTYPE: return "SETTYPE";
153 case bitc::CST_CODE_NULL: return "NULL";
154 case bitc::CST_CODE_UNDEF: return "UNDEF";
155 case bitc::CST_CODE_INTEGER: return "INTEGER";
156 case bitc::CST_CODE_WIDE_INTEGER: return "WIDE_INTEGER";
157 case bitc::CST_CODE_FLOAT: return "FLOAT";
158 case bitc::CST_CODE_AGGREGATE: return "AGGREGATE";
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000159 case bitc::CST_CODE_STRING: return "STRING";
160 case bitc::CST_CODE_CSTRING: return "CSTRING";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000161 case bitc::CST_CODE_CE_BINOP: return "CE_BINOP";
162 case bitc::CST_CODE_CE_CAST: return "CE_CAST";
163 case bitc::CST_CODE_CE_GEP: return "CE_GEP";
164 case bitc::CST_CODE_CE_SELECT: return "CE_SELECT";
165 case bitc::CST_CODE_CE_EXTRACTELT: return "CE_EXTRACTELT";
166 case bitc::CST_CODE_CE_INSERTELT: return "CE_INSERTELT";
167 case bitc::CST_CODE_CE_SHUFFLEVEC: return "CE_SHUFFLEVEC";
168 case bitc::CST_CODE_CE_CMP: return "CE_CMP";
Chris Lattnerc5ff2cc2007-05-06 01:50:11 +0000169 case bitc::CST_CODE_INLINEASM: return "INLINEASM";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000170 }
171 case bitc::FUNCTION_BLOCK_ID:
172 switch (CodeID) {
173 default: return 0;
174 case bitc::FUNC_CODE_DECLAREBLOCKS: return "DECLAREBLOCKS";
175
176 case bitc::FUNC_CODE_INST_BINOP: return "INST_BINOP";
177 case bitc::FUNC_CODE_INST_CAST: return "INST_CAST";
178 case bitc::FUNC_CODE_INST_GEP: return "INST_GEP";
179 case bitc::FUNC_CODE_INST_SELECT: return "INST_SELECT";
180 case bitc::FUNC_CODE_INST_EXTRACTELT: return "INST_EXTRACTELT";
181 case bitc::FUNC_CODE_INST_INSERTELT: return "INST_INSERTELT";
182 case bitc::FUNC_CODE_INST_SHUFFLEVEC: return "INST_SHUFFLEVEC";
183 case bitc::FUNC_CODE_INST_CMP: return "INST_CMP";
184
185 case bitc::FUNC_CODE_INST_RET: return "INST_RET";
186 case bitc::FUNC_CODE_INST_BR: return "INST_BR";
187 case bitc::FUNC_CODE_INST_SWITCH: return "INST_SWITCH";
188 case bitc::FUNC_CODE_INST_INVOKE: return "INST_INVOKE";
189 case bitc::FUNC_CODE_INST_UNWIND: return "INST_UNWIND";
190 case bitc::FUNC_CODE_INST_UNREACHABLE: return "INST_UNREACHABLE";
191
Chris Lattner8f926682007-05-01 02:43:46 +0000192 case bitc::FUNC_CODE_INST_PHI: return "INST_PHI";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000193 case bitc::FUNC_CODE_INST_MALLOC: return "INST_MALLOC";
194 case bitc::FUNC_CODE_INST_FREE: return "INST_FREE";
195 case bitc::FUNC_CODE_INST_ALLOCA: return "INST_ALLOCA";
196 case bitc::FUNC_CODE_INST_LOAD: return "INST_LOAD";
197 case bitc::FUNC_CODE_INST_STORE: return "INST_STORE";
198 case bitc::FUNC_CODE_INST_CALL: return "INST_CALL";
199 case bitc::FUNC_CODE_INST_VAARG: return "INST_VAARG";
Christopher Lambfe63fb92007-12-11 08:59:05 +0000200 case bitc::FUNC_CODE_INST_STORE2: return "INST_STORE2";
Nick Lewycky33a834a2008-03-01 21:47:06 +0000201 case bitc::FUNC_CODE_INST_GETRESULT: return "INST_GETRESULT";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000202 }
203 case bitc::TYPE_SYMTAB_BLOCK_ID:
204 switch (CodeID) {
205 default: return 0;
206 case bitc::TST_CODE_ENTRY: return "ENTRY";
207 }
208 case bitc::VALUE_SYMTAB_BLOCK_ID:
209 switch (CodeID) {
210 default: return 0;
211 case bitc::VST_CODE_ENTRY: return "ENTRY";
Chris Lattnercd5b7d72007-05-04 03:01:41 +0000212 case bitc::VST_CODE_BBENTRY: return "BBENTRY";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000213 }
214 }
215}
216
Chris Lattner4238d472007-04-29 20:00:02 +0000217
218struct PerBlockIDStats {
219 /// NumInstances - This the number of times this block ID has been seen.
220 unsigned NumInstances;
221
222 /// NumBits - The total size in bits of all of these blocks.
223 uint64_t NumBits;
224
225 /// NumSubBlocks - The total number of blocks these blocks contain.
226 unsigned NumSubBlocks;
227
228 /// NumAbbrevs - The total number of abbreviations.
229 unsigned NumAbbrevs;
230
231 /// NumRecords - The total number of records these blocks contain, and the
232 /// number that are abbreviated.
233 unsigned NumRecords, NumAbbreviatedRecords;
234
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000235 /// CodeFreq - Keep track of the number of times we see each code.
236 std::vector<unsigned> CodeFreq;
237
Chris Lattner4238d472007-04-29 20:00:02 +0000238 PerBlockIDStats()
239 : NumInstances(0), NumBits(0),
240 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
241};
242
243static std::map<unsigned, PerBlockIDStats> BlockIDStats;
244
245
246
Chris Lattner32de6332007-04-29 08:31:14 +0000247/// Error - All bitcode analysis errors go through this function, making this a
248/// good place to breakpoint if debugging.
249static bool Error(const std::string &Err) {
250 std::cerr << Err << "\n";
251 return true;
252}
253
254/// ParseBlock - Read a block, updating statistics, etc.
Chris Lattnerb30c9252007-04-29 21:48:19 +0000255static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) {
Chris Lattner1772b122007-05-05 00:17:42 +0000256 std::string Indent(IndentLevel*2, ' ');
Chris Lattner4238d472007-04-29 20:00:02 +0000257 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
Chris Lattner32de6332007-04-29 08:31:14 +0000258 unsigned BlockID = Stream.ReadSubBlockID();
Chris Lattnerb30c9252007-04-29 21:48:19 +0000259
Chris Lattner4238d472007-04-29 20:00:02 +0000260 // Get the statistics for this BlockID.
261 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
262
263 BlockStats.NumInstances++;
Chris Lattner32de6332007-04-29 08:31:14 +0000264
Chris Lattner1772b122007-05-05 00:17:42 +0000265 // BLOCKINFO is a special part of the stream.
266 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
267 if (Dump) std::cerr << Indent << "<BLOCKINFO_BLOCK/>\n";
268 if (Stream.ReadBlockInfoBlock())
269 return Error("Malformed BlockInfoBlock");
270 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
271 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
272 return false;
273 }
274
Chris Lattnerb30c9252007-04-29 21:48:19 +0000275 unsigned NumWords = 0;
Chris Lattner1772b122007-05-05 00:17:42 +0000276 if (Stream.EnterSubBlock(BlockID, &NumWords))
Chris Lattner32de6332007-04-29 08:31:14 +0000277 return Error("Malformed block record");
278
Chris Lattnerb30c9252007-04-29 21:48:19 +0000279 const char *BlockName = 0;
280 if (Dump) {
281 std::cerr << Indent << "<";
282 if ((BlockName = GetBlockName(BlockID)))
283 std::cerr << BlockName;
284 else
285 std::cerr << "UnknownBlock" << BlockID;
286
287 if (NonSymbolic && BlockName)
288 std::cerr << " BlockID=" << BlockID;
289
290 std::cerr << " NumWords=" << NumWords
291 << " BlockCodeSize=" << Stream.GetAbbrevIDWidth() << ">\n";
292 }
293
Chris Lattner32de6332007-04-29 08:31:14 +0000294 SmallVector<uint64_t, 64> Record;
295
296 // Read all the records for this block.
297 while (1) {
298 if (Stream.AtEndOfStream())
299 return Error("Premature end of bitstream");
300
301 // Read the code for this record.
302 unsigned AbbrevID = Stream.ReadCode();
303 switch (AbbrevID) {
Chris Lattner4238d472007-04-29 20:00:02 +0000304 case bitc::END_BLOCK: {
Chris Lattner32de6332007-04-29 08:31:14 +0000305 if (Stream.ReadBlockEnd())
306 return Error("Error at end of block");
Chris Lattner4238d472007-04-29 20:00:02 +0000307 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
308 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000309 if (Dump) {
310 std::cerr << Indent << "</";
311 if (BlockName)
312 std::cerr << BlockName << ">\n";
313 else
314 std::cerr << "UnknownBlock" << BlockID << ">\n";
315 }
Chris Lattner32de6332007-04-29 08:31:14 +0000316 return false;
Chris Lattner4238d472007-04-29 20:00:02 +0000317 }
Chris Lattner44b0f102007-05-05 01:29:31 +0000318 case bitc::ENTER_SUBBLOCK: {
319 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
Chris Lattnerb30c9252007-04-29 21:48:19 +0000320 if (ParseBlock(Stream, IndentLevel+1))
Chris Lattner32de6332007-04-29 08:31:14 +0000321 return true;
Chris Lattner4238d472007-04-29 20:00:02 +0000322 ++BlockStats.NumSubBlocks;
Chris Lattner44b0f102007-05-05 01:29:31 +0000323 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
324
325 // Don't include subblock sizes in the size of this block.
326 BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
Chris Lattner32de6332007-04-29 08:31:14 +0000327 break;
Chris Lattner44b0f102007-05-05 01:29:31 +0000328 }
Chris Lattner32de6332007-04-29 08:31:14 +0000329 case bitc::DEFINE_ABBREV:
330 Stream.ReadAbbrevRecord();
Chris Lattner4238d472007-04-29 20:00:02 +0000331 ++BlockStats.NumAbbrevs;
Chris Lattner32de6332007-04-29 08:31:14 +0000332 break;
333 default:
Chris Lattner4238d472007-04-29 20:00:02 +0000334 ++BlockStats.NumRecords;
335 if (AbbrevID != bitc::UNABBREV_RECORD)
336 ++BlockStats.NumAbbreviatedRecords;
337
Chris Lattner32de6332007-04-29 08:31:14 +0000338 Record.clear();
339 unsigned Code = Stream.ReadRecord(AbbrevID, Record);
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000340
341 // Increment the # occurrences of this code.
342 if (BlockStats.CodeFreq.size() <= Code)
343 BlockStats.CodeFreq.resize(Code+1);
344 BlockStats.CodeFreq[Code]++;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000345
346 if (Dump) {
347 std::cerr << Indent << " <";
348 if (const char *CodeName = GetCodeName(Code, BlockID))
349 std::cerr << CodeName;
350 else
351 std::cerr << "UnknownCode" << Code;
352 if (NonSymbolic && GetCodeName(Code, BlockID))
353 std::cerr << " codeid=" << Code;
354 if (AbbrevID != bitc::UNABBREV_RECORD)
355 std::cerr << " abbrevid=" << AbbrevID;
356
357 for (unsigned i = 0, e = Record.size(); i != e; ++i)
358 std::cerr << " op" << i << "=" << (int64_t)Record[i];
359
Chris Lattner1772b122007-05-05 00:17:42 +0000360 std::cerr << "/>\n";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000361 }
362
Chris Lattner32de6332007-04-29 08:31:14 +0000363 break;
364 }
365 }
366}
367
Chris Lattner4238d472007-04-29 20:00:02 +0000368static void PrintSize(double Bits) {
369 std::cerr << Bits << "b/" << Bits/8 << "B/" << Bits/32 << "W";
370}
371
372
Chris Lattner45e0f892007-04-29 08:12:22 +0000373/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
374static int AnalyzeBitcode() {
375 // Read the input file.
Chris Lattner038112a2008-04-01 18:04:03 +0000376 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str());
Chris Lattner45e0f892007-04-29 08:12:22 +0000377
Chris Lattner32de6332007-04-29 08:31:14 +0000378 if (Buffer == 0)
379 return Error("Error reading '" + InputFilename + "'.");
Chris Lattner45e0f892007-04-29 08:12:22 +0000380
Chris Lattner32de6332007-04-29 08:31:14 +0000381 if (Buffer->getBufferSize() & 3)
382 return Error("Bitcode stream should be a multiple of 4 bytes in length");
Chris Lattner45e0f892007-04-29 08:12:22 +0000383
384 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
385 BitstreamReader Stream(BufPtr, BufPtr+Buffer->getBufferSize());
386
387
388 // Read the stream signature.
389 char Signature[6];
390 Signature[0] = Stream.Read(8);
391 Signature[1] = Stream.Read(8);
392 Signature[2] = Stream.Read(4);
393 Signature[3] = Stream.Read(4);
394 Signature[4] = Stream.Read(4);
395 Signature[5] = Stream.Read(4);
396
Chris Lattner32de6332007-04-29 08:31:14 +0000397 // Autodetect the file contents, if it is one we know.
Chris Lattner45e0f892007-04-29 08:12:22 +0000398 CurStreamType = UnknownBitstream;
399 if (Signature[0] == 'B' && Signature[1] == 'C' &&
400 Signature[2] == 0x0 && Signature[3] == 0xC &&
401 Signature[4] == 0xE && Signature[5] == 0xD)
402 CurStreamType = LLVMIRBitstream;
403
Chris Lattner4238d472007-04-29 20:00:02 +0000404 unsigned NumTopBlocks = 0;
405
Chris Lattner32de6332007-04-29 08:31:14 +0000406 // Parse the top-level structure. We only allow blocks at the top-level.
407 while (!Stream.AtEndOfStream()) {
408 unsigned Code = Stream.ReadCode();
409 if (Code != bitc::ENTER_SUBBLOCK)
410 return Error("Invalid record at top-level");
411
Chris Lattnerb30c9252007-04-29 21:48:19 +0000412 if (ParseBlock(Stream, 0))
Chris Lattner32de6332007-04-29 08:31:14 +0000413 return true;
Chris Lattner4238d472007-04-29 20:00:02 +0000414 ++NumTopBlocks;
Chris Lattner32de6332007-04-29 08:31:14 +0000415 }
416
Chris Lattnerb30c9252007-04-29 21:48:19 +0000417 if (Dump) std::cerr << "\n\n";
418
Chris Lattner8f926682007-05-01 02:43:46 +0000419 uint64_t BufferSizeBits = Buffer->getBufferSize()*8;
Chris Lattner32de6332007-04-29 08:31:14 +0000420 // Print a summary of the read file.
Chris Lattner45e0f892007-04-29 08:12:22 +0000421 std::cerr << "Summary of " << InputFilename << ":\n";
Chris Lattner4238d472007-04-29 20:00:02 +0000422 std::cerr << " Total size: ";
Chris Lattner8f926682007-05-01 02:43:46 +0000423 PrintSize(BufferSizeBits);
Chris Lattner4238d472007-04-29 20:00:02 +0000424 std::cerr << "\n";
425 std::cerr << " Stream type: ";
Chris Lattner45e0f892007-04-29 08:12:22 +0000426 switch (CurStreamType) {
427 default: assert(0 && "Unknown bitstream type");
428 case UnknownBitstream: std::cerr << "unknown\n"; break;
429 case LLVMIRBitstream: std::cerr << "LLVM IR\n"; break;
430 }
Chris Lattner4238d472007-04-29 20:00:02 +0000431 std::cerr << " # Toplevel Blocks: " << NumTopBlocks << "\n";
432 std::cerr << "\n";
433
434 // Emit per-block stats.
435 std::cerr << "Per-block Summary:\n";
436 for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
437 E = BlockIDStats.end(); I != E; ++I) {
438 std::cerr << " Block ID #" << I->first;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000439 if (const char *BlockName = GetBlockName(I->first))
Chris Lattner4238d472007-04-29 20:00:02 +0000440 std::cerr << " (" << BlockName << ")";
441 std::cerr << ":\n";
442
443 const PerBlockIDStats &Stats = I->second;
444 std::cerr << " Num Instances: " << Stats.NumInstances << "\n";
445 std::cerr << " Total Size: ";
446 PrintSize(Stats.NumBits);
447 std::cerr << "\n";
Chris Lattner8f926682007-05-01 02:43:46 +0000448 std::cerr << " % of file: "
449 << Stats.NumBits/(double)BufferSizeBits*100 << "\n";
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000450 if (Stats.NumInstances > 1) {
451 std::cerr << " Average Size: ";
452 PrintSize(Stats.NumBits/(double)Stats.NumInstances);
453 std::cerr << "\n";
454 std::cerr << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
455 << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
456 std::cerr << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
457 << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
458 std::cerr << " Tot/Avg Records: " << Stats.NumRecords << "/"
459 << Stats.NumRecords/(double)Stats.NumInstances << "\n";
460 } else {
461 std::cerr << " Num SubBlocks: " << Stats.NumSubBlocks << "\n";
462 std::cerr << " Num Abbrevs: " << Stats.NumAbbrevs << "\n";
463 std::cerr << " Num Records: " << Stats.NumRecords << "\n";
464 }
Chris Lattner1772b122007-05-05 00:17:42 +0000465 if (Stats.NumRecords)
466 std::cerr << " % Abbrev Recs: " << (Stats.NumAbbreviatedRecords/
467 (double)Stats.NumRecords)*100 << "\n";
Chris Lattner4238d472007-04-29 20:00:02 +0000468 std::cerr << "\n";
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000469
470 // Print a histogram of the codes we see.
471 if (!NoHistogram && !Stats.CodeFreq.empty()) {
472 std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code>
473 for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
474 if (unsigned Freq = Stats.CodeFreq[i])
475 FreqPairs.push_back(std::make_pair(Freq, i));
476 std::stable_sort(FreqPairs.begin(), FreqPairs.end());
477 std::reverse(FreqPairs.begin(), FreqPairs.end());
478
479 std::cerr << "\tCode Histogram:\n";
480 for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
481 std::cerr << "\t\t" << FreqPairs[i].first << "\t";
482 if (const char *CodeName = GetCodeName(FreqPairs[i].second, I->first))
483 std::cerr << CodeName << "\n";
484 else
485 std::cerr << "UnknownCode" << FreqPairs[i].second << "\n";
486 }
487 std::cerr << "\n";
488
489 }
Chris Lattner4238d472007-04-29 20:00:02 +0000490 }
Chris Lattner45e0f892007-04-29 08:12:22 +0000491 return 0;
492}
Reid Spencerdac69c82004-06-07 17:53:43 +0000493
Chris Lattner32de6332007-04-29 08:31:14 +0000494
Chris Lattnerc30598b2006-12-06 01:18:01 +0000495int main(int argc, char **argv) {
496 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Dan Gohman82a13c92007-10-08 15:45:12 +0000497 cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
Chris Lattner45e0f892007-04-29 08:12:22 +0000498
499 sys::PrintStackTraceOnErrorSignal();
500
Chris Lattner44dadff2007-05-06 09:29:57 +0000501 return AnalyzeBitcode();
Reid Spencerdac69c82004-06-07 17:53:43 +0000502}