blob: 623bcaeae7b525f407ec42cd10e2b5e6848dcdc2 [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"
Chris Lattnercc14d252009-03-06 05:34:10 +000036#include "llvm/Support/PrettyStackTrace.h"
Reid Spencerdac69c82004-06-07 17:53:43 +000037#include "llvm/System/Signals.h"
Chris Lattner44dadff2007-05-06 09:29:57 +000038#include <map>
Reid Spencerdac69c82004-06-07 17:53:43 +000039#include <fstream>
40#include <iostream>
Chris Lattnerb1e85b52007-05-05 01:46:49 +000041#include <algorithm>
Reid Spencerdac69c82004-06-07 17:53:43 +000042using namespace llvm;
43
44static cl::opt<std::string>
Gabor Greif8ff70c22007-07-04 21:55:50 +000045 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-"));
Reid Spencerdac69c82004-06-07 17:53:43 +000046
Reid Spencer75937452004-08-21 21:00:24 +000047static cl::opt<std::string>
48 OutputFilename("-o", cl::init("-"), cl::desc("<output file>"));
49
Gabor Greif8ff70c22007-07-04 21:55:50 +000050static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace"));
Chris Lattner32de6332007-04-29 08:31:14 +000051
52//===----------------------------------------------------------------------===//
53// Bitcode specific analysis.
54//===----------------------------------------------------------------------===//
55
Chris Lattnerb1e85b52007-05-05 01:46:49 +000056static cl::opt<bool> NoHistogram("disable-histogram",
57 cl::desc("Do not print per-code histogram"));
Chris Lattner45e0f892007-04-29 08:12:22 +000058
Chris Lattnerb30c9252007-04-29 21:48:19 +000059static cl::opt<bool>
60NonSymbolic("non-symbolic",
61 cl::desc("Emit numberic info in dump even if"
62 " symbolic info is available"));
63
Chris Lattner45e0f892007-04-29 08:12:22 +000064/// CurStreamType - If we can sniff the flavor of this stream, we can produce
65/// better dump info.
66static enum {
67 UnknownBitstream,
68 LLVMIRBitstream
69} CurStreamType;
70
Chris Lattner4238d472007-04-29 20:00:02 +000071
72/// GetBlockName - Return a symbolic block name if known, otherwise return
Chris Lattnerb30c9252007-04-29 21:48:19 +000073/// null.
Chris Lattner4238d472007-04-29 20:00:02 +000074static const char *GetBlockName(unsigned BlockID) {
Chris Lattner1772b122007-05-05 00:17:42 +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 Lattnerb30c9252007-04-29 21:48:19 +000082 if (CurStreamType != LLVMIRBitstream) return 0;
Chris Lattner4238d472007-04-29 20:00:02 +000083
84 switch (BlockID) {
Chris Lattnerb30c9252007-04-29 21:48:19 +000085 default: return 0;
Chris Lattner4238d472007-04-29 20:00:02 +000086 case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK";
Chris Lattnercd5b7d72007-05-04 03:01:41 +000087 case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK";
Chris Lattner4238d472007-04-29 20:00:02 +000088 case bitc::TYPE_BLOCK_ID: return "TYPE_BLOCK";
89 case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK";
90 case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK";
91 case bitc::TYPE_SYMTAB_BLOCK_ID: return "TYPE_SYMTAB";
92 case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB";
93 }
94}
95
Chris Lattnerb30c9252007-04-29 21:48:19 +000096/// GetCodeName - Return a symbolic code name if known, otherwise return
97/// null.
98static const char *GetCodeName(unsigned CodeID, unsigned BlockID) {
Chris Lattner1772b122007-05-05 00:17:42 +000099 // Standard blocks for all bitcode files.
100 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
101 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
102 switch (CodeID) {
103 default: return 0;
104 case bitc::MODULE_CODE_VERSION: return "VERSION";
105 }
106 }
107 return 0;
108 }
109
Chris Lattnerb30c9252007-04-29 21:48:19 +0000110 if (CurStreamType != LLVMIRBitstream) return 0;
111
112 switch (BlockID) {
113 default: return 0;
114 case bitc::MODULE_BLOCK_ID:
115 switch (CodeID) {
116 default: return 0;
117 case bitc::MODULE_CODE_VERSION: return "VERSION";
118 case bitc::MODULE_CODE_TRIPLE: return "TRIPLE";
119 case bitc::MODULE_CODE_DATALAYOUT: return "DATALAYOUT";
120 case bitc::MODULE_CODE_ASM: return "ASM";
121 case bitc::MODULE_CODE_SECTIONNAME: return "SECTIONNAME";
122 case bitc::MODULE_CODE_DEPLIB: return "DEPLIB";
123 case bitc::MODULE_CODE_GLOBALVAR: return "GLOBALVAR";
124 case bitc::MODULE_CODE_FUNCTION: return "FUNCTION";
125 case bitc::MODULE_CODE_ALIAS: return "ALIAS";
126 case bitc::MODULE_CODE_PURGEVALS: return "PURGEVALS";
Nick Lewycky82b80d92008-11-07 14:52:51 +0000127 case bitc::MODULE_CODE_GCNAME: return "GCNAME";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000128 }
Chris Lattnercd5b7d72007-05-04 03:01:41 +0000129 case bitc::PARAMATTR_BLOCK_ID:
130 switch (CodeID) {
131 default: return 0;
132 case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY";
133 }
Chris Lattnerb30c9252007-04-29 21:48:19 +0000134 case bitc::TYPE_BLOCK_ID:
135 switch (CodeID) {
136 default: return 0;
Nick Lewycky82b80d92008-11-07 14:52:51 +0000137 case bitc::TYPE_CODE_NUMENTRY: return "NUMENTRY";
138 case bitc::TYPE_CODE_VOID: return "VOID";
139 case bitc::TYPE_CODE_FLOAT: return "FLOAT";
140 case bitc::TYPE_CODE_DOUBLE: return "DOUBLE";
141 case bitc::TYPE_CODE_LABEL: return "LABEL";
142 case bitc::TYPE_CODE_OPAQUE: return "OPAQUE";
143 case bitc::TYPE_CODE_INTEGER: return "INTEGER";
144 case bitc::TYPE_CODE_POINTER: return "POINTER";
145 case bitc::TYPE_CODE_FUNCTION: return "FUNCTION";
146 case bitc::TYPE_CODE_STRUCT: return "STRUCT";
147 case bitc::TYPE_CODE_ARRAY: return "ARRAY";
148 case bitc::TYPE_CODE_VECTOR: return "VECTOR";
149 case bitc::TYPE_CODE_X86_FP80: return "X86_FP80";
150 case bitc::TYPE_CODE_FP128: return "FP128";
151 case bitc::TYPE_CODE_PPC_FP128: return "PPC_FP128";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000152 }
153
154 case bitc::CONSTANTS_BLOCK_ID:
155 switch (CodeID) {
156 default: return 0;
157 case bitc::CST_CODE_SETTYPE: return "SETTYPE";
158 case bitc::CST_CODE_NULL: return "NULL";
159 case bitc::CST_CODE_UNDEF: return "UNDEF";
160 case bitc::CST_CODE_INTEGER: return "INTEGER";
161 case bitc::CST_CODE_WIDE_INTEGER: return "WIDE_INTEGER";
162 case bitc::CST_CODE_FLOAT: return "FLOAT";
163 case bitc::CST_CODE_AGGREGATE: return "AGGREGATE";
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000164 case bitc::CST_CODE_STRING: return "STRING";
165 case bitc::CST_CODE_CSTRING: return "CSTRING";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000166 case bitc::CST_CODE_CE_BINOP: return "CE_BINOP";
167 case bitc::CST_CODE_CE_CAST: return "CE_CAST";
168 case bitc::CST_CODE_CE_GEP: return "CE_GEP";
169 case bitc::CST_CODE_CE_SELECT: return "CE_SELECT";
170 case bitc::CST_CODE_CE_EXTRACTELT: return "CE_EXTRACTELT";
171 case bitc::CST_CODE_CE_INSERTELT: return "CE_INSERTELT";
172 case bitc::CST_CODE_CE_SHUFFLEVEC: return "CE_SHUFFLEVEC";
173 case bitc::CST_CODE_CE_CMP: return "CE_CMP";
Chris Lattnerc5ff2cc2007-05-06 01:50:11 +0000174 case bitc::CST_CODE_INLINEASM: return "INLINEASM";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000175 }
176 case bitc::FUNCTION_BLOCK_ID:
177 switch (CodeID) {
178 default: return 0;
179 case bitc::FUNC_CODE_DECLAREBLOCKS: return "DECLAREBLOCKS";
180
181 case bitc::FUNC_CODE_INST_BINOP: return "INST_BINOP";
182 case bitc::FUNC_CODE_INST_CAST: return "INST_CAST";
183 case bitc::FUNC_CODE_INST_GEP: return "INST_GEP";
184 case bitc::FUNC_CODE_INST_SELECT: return "INST_SELECT";
185 case bitc::FUNC_CODE_INST_EXTRACTELT: return "INST_EXTRACTELT";
186 case bitc::FUNC_CODE_INST_INSERTELT: return "INST_INSERTELT";
187 case bitc::FUNC_CODE_INST_SHUFFLEVEC: return "INST_SHUFFLEVEC";
188 case bitc::FUNC_CODE_INST_CMP: return "INST_CMP";
189
190 case bitc::FUNC_CODE_INST_RET: return "INST_RET";
191 case bitc::FUNC_CODE_INST_BR: return "INST_BR";
192 case bitc::FUNC_CODE_INST_SWITCH: return "INST_SWITCH";
193 case bitc::FUNC_CODE_INST_INVOKE: return "INST_INVOKE";
194 case bitc::FUNC_CODE_INST_UNWIND: return "INST_UNWIND";
195 case bitc::FUNC_CODE_INST_UNREACHABLE: return "INST_UNREACHABLE";
196
Chris Lattner8f926682007-05-01 02:43:46 +0000197 case bitc::FUNC_CODE_INST_PHI: return "INST_PHI";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000198 case bitc::FUNC_CODE_INST_MALLOC: return "INST_MALLOC";
199 case bitc::FUNC_CODE_INST_FREE: return "INST_FREE";
200 case bitc::FUNC_CODE_INST_ALLOCA: return "INST_ALLOCA";
201 case bitc::FUNC_CODE_INST_LOAD: return "INST_LOAD";
202 case bitc::FUNC_CODE_INST_STORE: return "INST_STORE";
203 case bitc::FUNC_CODE_INST_CALL: return "INST_CALL";
204 case bitc::FUNC_CODE_INST_VAARG: return "INST_VAARG";
Christopher Lambfe63fb92007-12-11 08:59:05 +0000205 case bitc::FUNC_CODE_INST_STORE2: return "INST_STORE2";
Nick Lewycky33a834a2008-03-01 21:47:06 +0000206 case bitc::FUNC_CODE_INST_GETRESULT: return "INST_GETRESULT";
Nick Lewycky82b80d92008-11-07 14:52:51 +0000207 case bitc::FUNC_CODE_INST_EXTRACTVAL: return "INST_EXTRACTVAL";
208 case bitc::FUNC_CODE_INST_INSERTVAL: return "INST_INSERTVAL";
209 case bitc::FUNC_CODE_INST_CMP2: return "INST_CMP2";
210 case bitc::FUNC_CODE_INST_VSELECT: return "INST_VSELECT";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000211 }
212 case bitc::TYPE_SYMTAB_BLOCK_ID:
213 switch (CodeID) {
214 default: return 0;
215 case bitc::TST_CODE_ENTRY: return "ENTRY";
216 }
217 case bitc::VALUE_SYMTAB_BLOCK_ID:
218 switch (CodeID) {
219 default: return 0;
220 case bitc::VST_CODE_ENTRY: return "ENTRY";
Chris Lattnercd5b7d72007-05-04 03:01:41 +0000221 case bitc::VST_CODE_BBENTRY: return "BBENTRY";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000222 }
223 }
224}
225
Chris Lattner4238d472007-04-29 20:00:02 +0000226
227struct PerBlockIDStats {
228 /// NumInstances - This the number of times this block ID has been seen.
229 unsigned NumInstances;
230
231 /// NumBits - The total size in bits of all of these blocks.
232 uint64_t NumBits;
233
234 /// NumSubBlocks - The total number of blocks these blocks contain.
235 unsigned NumSubBlocks;
236
237 /// NumAbbrevs - The total number of abbreviations.
238 unsigned NumAbbrevs;
239
240 /// NumRecords - The total number of records these blocks contain, and the
241 /// number that are abbreviated.
242 unsigned NumRecords, NumAbbreviatedRecords;
243
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000244 /// CodeFreq - Keep track of the number of times we see each code.
245 std::vector<unsigned> CodeFreq;
246
Chris Lattner4238d472007-04-29 20:00:02 +0000247 PerBlockIDStats()
248 : NumInstances(0), NumBits(0),
249 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
250};
251
252static std::map<unsigned, PerBlockIDStats> BlockIDStats;
253
254
255
Chris Lattner32de6332007-04-29 08:31:14 +0000256/// Error - All bitcode analysis errors go through this function, making this a
257/// good place to breakpoint if debugging.
258static bool Error(const std::string &Err) {
259 std::cerr << Err << "\n";
260 return true;
261}
262
263/// ParseBlock - Read a block, updating statistics, etc.
Chris Lattnerb30c9252007-04-29 21:48:19 +0000264static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) {
Chris Lattner1772b122007-05-05 00:17:42 +0000265 std::string Indent(IndentLevel*2, ' ');
Chris Lattner4238d472007-04-29 20:00:02 +0000266 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
Chris Lattner32de6332007-04-29 08:31:14 +0000267 unsigned BlockID = Stream.ReadSubBlockID();
Chris Lattnerb30c9252007-04-29 21:48:19 +0000268
Chris Lattner4238d472007-04-29 20:00:02 +0000269 // Get the statistics for this BlockID.
270 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
271
272 BlockStats.NumInstances++;
Chris Lattner32de6332007-04-29 08:31:14 +0000273
Chris Lattner1772b122007-05-05 00:17:42 +0000274 // BLOCKINFO is a special part of the stream.
275 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
276 if (Dump) std::cerr << Indent << "<BLOCKINFO_BLOCK/>\n";
277 if (Stream.ReadBlockInfoBlock())
278 return Error("Malformed BlockInfoBlock");
279 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
280 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
281 return false;
282 }
283
Chris Lattnerb30c9252007-04-29 21:48:19 +0000284 unsigned NumWords = 0;
Chris Lattner1772b122007-05-05 00:17:42 +0000285 if (Stream.EnterSubBlock(BlockID, &NumWords))
Chris Lattner32de6332007-04-29 08:31:14 +0000286 return Error("Malformed block record");
287
Chris Lattnerb30c9252007-04-29 21:48:19 +0000288 const char *BlockName = 0;
289 if (Dump) {
290 std::cerr << Indent << "<";
291 if ((BlockName = GetBlockName(BlockID)))
292 std::cerr << BlockName;
293 else
294 std::cerr << "UnknownBlock" << BlockID;
295
296 if (NonSymbolic && BlockName)
297 std::cerr << " BlockID=" << BlockID;
298
299 std::cerr << " NumWords=" << NumWords
300 << " BlockCodeSize=" << Stream.GetAbbrevIDWidth() << ">\n";
301 }
302
Chris Lattner32de6332007-04-29 08:31:14 +0000303 SmallVector<uint64_t, 64> Record;
304
305 // Read all the records for this block.
306 while (1) {
307 if (Stream.AtEndOfStream())
308 return Error("Premature end of bitstream");
309
310 // Read the code for this record.
311 unsigned AbbrevID = Stream.ReadCode();
312 switch (AbbrevID) {
Chris Lattner4238d472007-04-29 20:00:02 +0000313 case bitc::END_BLOCK: {
Chris Lattner32de6332007-04-29 08:31:14 +0000314 if (Stream.ReadBlockEnd())
315 return Error("Error at end of block");
Chris Lattner4238d472007-04-29 20:00:02 +0000316 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
317 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000318 if (Dump) {
319 std::cerr << Indent << "</";
320 if (BlockName)
321 std::cerr << BlockName << ">\n";
322 else
323 std::cerr << "UnknownBlock" << BlockID << ">\n";
324 }
Chris Lattner32de6332007-04-29 08:31:14 +0000325 return false;
Chris Lattner4238d472007-04-29 20:00:02 +0000326 }
Chris Lattner44b0f102007-05-05 01:29:31 +0000327 case bitc::ENTER_SUBBLOCK: {
328 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
Chris Lattnerb30c9252007-04-29 21:48:19 +0000329 if (ParseBlock(Stream, IndentLevel+1))
Chris Lattner32de6332007-04-29 08:31:14 +0000330 return true;
Chris Lattner4238d472007-04-29 20:00:02 +0000331 ++BlockStats.NumSubBlocks;
Chris Lattner44b0f102007-05-05 01:29:31 +0000332 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
333
334 // Don't include subblock sizes in the size of this block.
335 BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
Chris Lattner32de6332007-04-29 08:31:14 +0000336 break;
Chris Lattner44b0f102007-05-05 01:29:31 +0000337 }
Chris Lattner32de6332007-04-29 08:31:14 +0000338 case bitc::DEFINE_ABBREV:
339 Stream.ReadAbbrevRecord();
Chris Lattner4238d472007-04-29 20:00:02 +0000340 ++BlockStats.NumAbbrevs;
Chris Lattner32de6332007-04-29 08:31:14 +0000341 break;
342 default:
Chris Lattner4238d472007-04-29 20:00:02 +0000343 ++BlockStats.NumRecords;
344 if (AbbrevID != bitc::UNABBREV_RECORD)
345 ++BlockStats.NumAbbreviatedRecords;
346
Chris Lattner32de6332007-04-29 08:31:14 +0000347 Record.clear();
348 unsigned Code = Stream.ReadRecord(AbbrevID, Record);
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000349
350 // Increment the # occurrences of this code.
351 if (BlockStats.CodeFreq.size() <= Code)
352 BlockStats.CodeFreq.resize(Code+1);
353 BlockStats.CodeFreq[Code]++;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000354
355 if (Dump) {
356 std::cerr << Indent << " <";
357 if (const char *CodeName = GetCodeName(Code, BlockID))
358 std::cerr << CodeName;
359 else
360 std::cerr << "UnknownCode" << Code;
361 if (NonSymbolic && GetCodeName(Code, BlockID))
362 std::cerr << " codeid=" << Code;
363 if (AbbrevID != bitc::UNABBREV_RECORD)
364 std::cerr << " abbrevid=" << AbbrevID;
365
366 for (unsigned i = 0, e = Record.size(); i != e; ++i)
367 std::cerr << " op" << i << "=" << (int64_t)Record[i];
368
Chris Lattner1772b122007-05-05 00:17:42 +0000369 std::cerr << "/>\n";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000370 }
371
Chris Lattner32de6332007-04-29 08:31:14 +0000372 break;
373 }
374 }
375}
376
Chris Lattner4238d472007-04-29 20:00:02 +0000377static void PrintSize(double Bits) {
378 std::cerr << Bits << "b/" << Bits/8 << "B/" << Bits/32 << "W";
379}
380
381
Chris Lattner45e0f892007-04-29 08:12:22 +0000382/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
383static int AnalyzeBitcode() {
384 // Read the input file.
Chris Lattner038112a2008-04-01 18:04:03 +0000385 MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str());
Chris Lattner45e0f892007-04-29 08:12:22 +0000386
Chris Lattner32de6332007-04-29 08:31:14 +0000387 if (Buffer == 0)
388 return Error("Error reading '" + InputFilename + "'.");
Chris Lattner45e0f892007-04-29 08:12:22 +0000389
Chris Lattner32de6332007-04-29 08:31:14 +0000390 if (Buffer->getBufferSize() & 3)
391 return Error("Bitcode stream should be a multiple of 4 bytes in length");
Chris Lattner45e0f892007-04-29 08:12:22 +0000392
393 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
394 BitstreamReader Stream(BufPtr, BufPtr+Buffer->getBufferSize());
395
396
397 // Read the stream signature.
398 char Signature[6];
399 Signature[0] = Stream.Read(8);
400 Signature[1] = Stream.Read(8);
401 Signature[2] = Stream.Read(4);
402 Signature[3] = Stream.Read(4);
403 Signature[4] = Stream.Read(4);
404 Signature[5] = Stream.Read(4);
405
Chris Lattner32de6332007-04-29 08:31:14 +0000406 // Autodetect the file contents, if it is one we know.
Chris Lattner45e0f892007-04-29 08:12:22 +0000407 CurStreamType = UnknownBitstream;
408 if (Signature[0] == 'B' && Signature[1] == 'C' &&
409 Signature[2] == 0x0 && Signature[3] == 0xC &&
410 Signature[4] == 0xE && Signature[5] == 0xD)
411 CurStreamType = LLVMIRBitstream;
412
Chris Lattner4238d472007-04-29 20:00:02 +0000413 unsigned NumTopBlocks = 0;
414
Chris Lattner32de6332007-04-29 08:31:14 +0000415 // Parse the top-level structure. We only allow blocks at the top-level.
416 while (!Stream.AtEndOfStream()) {
417 unsigned Code = Stream.ReadCode();
418 if (Code != bitc::ENTER_SUBBLOCK)
419 return Error("Invalid record at top-level");
420
Chris Lattnerb30c9252007-04-29 21:48:19 +0000421 if (ParseBlock(Stream, 0))
Chris Lattner32de6332007-04-29 08:31:14 +0000422 return true;
Chris Lattner4238d472007-04-29 20:00:02 +0000423 ++NumTopBlocks;
Chris Lattner32de6332007-04-29 08:31:14 +0000424 }
425
Chris Lattnerb30c9252007-04-29 21:48:19 +0000426 if (Dump) std::cerr << "\n\n";
427
Chris Lattner8f926682007-05-01 02:43:46 +0000428 uint64_t BufferSizeBits = Buffer->getBufferSize()*8;
Chris Lattner32de6332007-04-29 08:31:14 +0000429 // Print a summary of the read file.
Chris Lattner45e0f892007-04-29 08:12:22 +0000430 std::cerr << "Summary of " << InputFilename << ":\n";
Chris Lattner4238d472007-04-29 20:00:02 +0000431 std::cerr << " Total size: ";
Chris Lattner8f926682007-05-01 02:43:46 +0000432 PrintSize(BufferSizeBits);
Chris Lattner4238d472007-04-29 20:00:02 +0000433 std::cerr << "\n";
434 std::cerr << " Stream type: ";
Chris Lattner45e0f892007-04-29 08:12:22 +0000435 switch (CurStreamType) {
436 default: assert(0 && "Unknown bitstream type");
437 case UnknownBitstream: std::cerr << "unknown\n"; break;
438 case LLVMIRBitstream: std::cerr << "LLVM IR\n"; break;
439 }
Chris Lattner4238d472007-04-29 20:00:02 +0000440 std::cerr << " # Toplevel Blocks: " << NumTopBlocks << "\n";
441 std::cerr << "\n";
442
443 // Emit per-block stats.
444 std::cerr << "Per-block Summary:\n";
445 for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
446 E = BlockIDStats.end(); I != E; ++I) {
447 std::cerr << " Block ID #" << I->first;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000448 if (const char *BlockName = GetBlockName(I->first))
Chris Lattner4238d472007-04-29 20:00:02 +0000449 std::cerr << " (" << BlockName << ")";
450 std::cerr << ":\n";
451
452 const PerBlockIDStats &Stats = I->second;
453 std::cerr << " Num Instances: " << Stats.NumInstances << "\n";
454 std::cerr << " Total Size: ";
455 PrintSize(Stats.NumBits);
456 std::cerr << "\n";
Chris Lattner8f926682007-05-01 02:43:46 +0000457 std::cerr << " % of file: "
458 << Stats.NumBits/(double)BufferSizeBits*100 << "\n";
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000459 if (Stats.NumInstances > 1) {
460 std::cerr << " Average Size: ";
461 PrintSize(Stats.NumBits/(double)Stats.NumInstances);
462 std::cerr << "\n";
463 std::cerr << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
464 << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
465 std::cerr << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
466 << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
467 std::cerr << " Tot/Avg Records: " << Stats.NumRecords << "/"
468 << Stats.NumRecords/(double)Stats.NumInstances << "\n";
469 } else {
470 std::cerr << " Num SubBlocks: " << Stats.NumSubBlocks << "\n";
471 std::cerr << " Num Abbrevs: " << Stats.NumAbbrevs << "\n";
472 std::cerr << " Num Records: " << Stats.NumRecords << "\n";
473 }
Chris Lattner1772b122007-05-05 00:17:42 +0000474 if (Stats.NumRecords)
475 std::cerr << " % Abbrev Recs: " << (Stats.NumAbbreviatedRecords/
476 (double)Stats.NumRecords)*100 << "\n";
Chris Lattner4238d472007-04-29 20:00:02 +0000477 std::cerr << "\n";
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000478
479 // Print a histogram of the codes we see.
480 if (!NoHistogram && !Stats.CodeFreq.empty()) {
481 std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code>
482 for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
483 if (unsigned Freq = Stats.CodeFreq[i])
484 FreqPairs.push_back(std::make_pair(Freq, i));
485 std::stable_sort(FreqPairs.begin(), FreqPairs.end());
486 std::reverse(FreqPairs.begin(), FreqPairs.end());
487
488 std::cerr << "\tCode Histogram:\n";
489 for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
490 std::cerr << "\t\t" << FreqPairs[i].first << "\t";
491 if (const char *CodeName = GetCodeName(FreqPairs[i].second, I->first))
492 std::cerr << CodeName << "\n";
493 else
494 std::cerr << "UnknownCode" << FreqPairs[i].second << "\n";
495 }
496 std::cerr << "\n";
497
498 }
Chris Lattner4238d472007-04-29 20:00:02 +0000499 }
Chris Lattner45e0f892007-04-29 08:12:22 +0000500 return 0;
501}
Reid Spencerdac69c82004-06-07 17:53:43 +0000502
Chris Lattner32de6332007-04-29 08:31:14 +0000503
Chris Lattnerc30598b2006-12-06 01:18:01 +0000504int main(int argc, char **argv) {
Chris Lattnercc14d252009-03-06 05:34:10 +0000505 // Print a stack trace if we signal out.
Chris Lattner45e0f892007-04-29 08:12:22 +0000506 sys::PrintStackTraceOnErrorSignal();
Chris Lattnercc14d252009-03-06 05:34:10 +0000507 PrettyStackTraceProgram X(argc, argv);
508 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
509 cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
Chris Lattner45e0f892007-04-29 08:12:22 +0000510
Chris Lattner44dadff2007-05-06 09:29:57 +0000511 return AnalyzeBitcode();
Reid Spencerdac69c82004-06-07 17:53:43 +0000512}