blob: 8e10418e21f9da971c25b6d0f5449c13936ec93e [file] [log] [blame]
Reid Spencer8a542ae2004-07-02 03:22:53 +00001//===-- llvm-bcanalyzer.cpp - Byte Code Analyzer --------------------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
Reid Spencerdac69c82004-06-07 17:53:43 +00003// The LLVM Compiler Infrastructure
4//
Misha Brukman3da94ae2005-04-22 00:00:37 +00005// This file was developed by Reid Spencer and is distributed under the
Reid Spencer96684ef2004-06-08 05:56:58 +00006// University of Illinois Open Source 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:
Reid Spencer8a542ae2004-07-02 03:22:53 +000011// llvm-bcanalyzer [options] - Read LLVM bytecode from stdin
12// llvm-bcanalyzer [options] x.bc - Read LLVM bytecode 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
Misha Brukman3da94ae2005-04-22 00:00:37 +000016// --nodetails - Don't print out detailed informaton about individual
Reid Spencer23d46e72004-06-10 18:38:44 +000017// blocks and functions
18// --dump - Dump low-level bytecode structure in readable format
Reid Spencer96684ef2004-06-08 05:56:58 +000019//
20// This tool provides analytical information about a bytecode file. It is
21// intended as an aid to developers of bytecode reading and writing software. It
Misha Brukman3da94ae2005-04-22 00:00:37 +000022// produces on std::out a summary of the bytecode file that shows various
Reid Spencer23d46e72004-06-10 18:38:44 +000023// statistics about the contents of the file. By default this information is
24// detailed and contains information about individual bytecode blocks and the
Misha Brukman3da94ae2005-04-22 00:00:37 +000025// functions in the module. To avoid this more detailed output, use the
Reid Spencer23d46e72004-06-10 18:38:44 +000026// -nodetails option to limit the output to just module level information.
Misha Brukman3da94ae2005-04-22 00:00:37 +000027// The tool is also able to print a bytecode file in a straight forward text
28// format that shows the containment and relationships of the information in
29// the bytecode file (-dump option).
Chris Lattner63db4852007-04-29 05:51:00 +000030//
Reid Spencerdac69c82004-06-07 17:53:43 +000031//===----------------------------------------------------------------------===//
32
Reid Spencer86a9a7a2004-06-29 23:34:27 +000033#include "llvm/Analysis/Verifier.h"
Chris Lattner45e0f892007-04-29 08:12:22 +000034#include "llvm/Bitcode/BitstreamReader.h"
Chris Lattner4238d472007-04-29 20:00:02 +000035#include "llvm/Bitcode/LLVMBitCodes.h"
Reid Spencerdac69c82004-06-07 17:53:43 +000036#include "llvm/Bytecode/Analyzer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000037#include "llvm/Support/CommandLine.h"
Chris Lattnerf2e292c2007-02-07 21:41:02 +000038#include "llvm/Support/Compressor.h"
Chris Lattnerc30598b2006-12-06 01:18:01 +000039#include "llvm/Support/ManagedStatic.h"
Chris Lattner45e0f892007-04-29 08:12:22 +000040#include "llvm/Support/MemoryBuffer.h"
Reid Spencerdac69c82004-06-07 17:53:43 +000041#include "llvm/System/Signals.h"
42#include <fstream>
43#include <iostream>
Chris Lattnerb1e85b52007-05-05 01:46:49 +000044#include <algorithm>
Reid Spencerdac69c82004-06-07 17:53:43 +000045using namespace llvm;
46
47static cl::opt<std::string>
48 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
49
Reid Spencer75937452004-08-21 21:00:24 +000050static cl::opt<std::string>
51 OutputFilename("-o", cl::init("-"), cl::desc("<output file>"));
52
Chris Lattner63db4852007-04-29 05:51:00 +000053static cl::opt<bool> NoDetails("nodetails", cl::desc("Skip detailed output"));
54static cl::opt<bool> Dump("dump", cl::desc("Dump low level bytecode trace"));
55static cl::opt<bool> Verify("verify", cl::desc("Progressively verify module"));
Chris Lattner32de6332007-04-29 08:31:14 +000056
57//===----------------------------------------------------------------------===//
58// Bitcode specific analysis.
59//===----------------------------------------------------------------------===//
60
Chris Lattner45e0f892007-04-29 08:12:22 +000061static cl::opt<bool> Bitcode("bitcode", cl::desc("Read a bitcode file"));
Chris Lattnerb1e85b52007-05-05 01:46:49 +000062static cl::opt<bool> NoHistogram("disable-histogram",
63 cl::desc("Do not print per-code histogram"));
Chris Lattner45e0f892007-04-29 08:12:22 +000064
Chris Lattnerb30c9252007-04-29 21:48:19 +000065static cl::opt<bool>
66NonSymbolic("non-symbolic",
67 cl::desc("Emit numberic info in dump even if"
68 " symbolic info is available"));
69
Chris Lattner45e0f892007-04-29 08:12:22 +000070/// CurStreamType - If we can sniff the flavor of this stream, we can produce
71/// better dump info.
72static enum {
73 UnknownBitstream,
74 LLVMIRBitstream
75} CurStreamType;
76
Chris Lattner4238d472007-04-29 20:00:02 +000077
78/// GetBlockName - Return a symbolic block name if known, otherwise return
Chris Lattnerb30c9252007-04-29 21:48:19 +000079/// null.
Chris Lattner4238d472007-04-29 20:00:02 +000080static const char *GetBlockName(unsigned BlockID) {
Chris Lattner1772b122007-05-05 00:17:42 +000081 // Standard blocks for all bitcode files.
82 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
83 if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
84 return "BLOCKINFO_BLOCK";
85 return 0;
86 }
87
Chris Lattnerb30c9252007-04-29 21:48:19 +000088 if (CurStreamType != LLVMIRBitstream) return 0;
Chris Lattner4238d472007-04-29 20:00:02 +000089
90 switch (BlockID) {
Chris Lattnerb30c9252007-04-29 21:48:19 +000091 default: return 0;
Chris Lattner4238d472007-04-29 20:00:02 +000092 case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK";
Chris Lattnercd5b7d72007-05-04 03:01:41 +000093 case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK";
Chris Lattner4238d472007-04-29 20:00:02 +000094 case bitc::TYPE_BLOCK_ID: return "TYPE_BLOCK";
95 case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK";
96 case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK";
97 case bitc::TYPE_SYMTAB_BLOCK_ID: return "TYPE_SYMTAB";
98 case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB";
99 }
100}
101
Chris Lattnerb30c9252007-04-29 21:48:19 +0000102/// GetCodeName - Return a symbolic code name if known, otherwise return
103/// null.
104static const char *GetCodeName(unsigned CodeID, unsigned BlockID) {
Chris Lattner1772b122007-05-05 00:17:42 +0000105 // Standard blocks for all bitcode files.
106 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
107 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
108 switch (CodeID) {
109 default: return 0;
110 case bitc::MODULE_CODE_VERSION: return "VERSION";
111 }
112 }
113 return 0;
114 }
115
Chris Lattnerb30c9252007-04-29 21:48:19 +0000116 if (CurStreamType != LLVMIRBitstream) return 0;
117
118 switch (BlockID) {
119 default: return 0;
120 case bitc::MODULE_BLOCK_ID:
121 switch (CodeID) {
122 default: return 0;
123 case bitc::MODULE_CODE_VERSION: return "VERSION";
124 case bitc::MODULE_CODE_TRIPLE: return "TRIPLE";
125 case bitc::MODULE_CODE_DATALAYOUT: return "DATALAYOUT";
126 case bitc::MODULE_CODE_ASM: return "ASM";
127 case bitc::MODULE_CODE_SECTIONNAME: return "SECTIONNAME";
128 case bitc::MODULE_CODE_DEPLIB: return "DEPLIB";
129 case bitc::MODULE_CODE_GLOBALVAR: return "GLOBALVAR";
130 case bitc::MODULE_CODE_FUNCTION: return "FUNCTION";
131 case bitc::MODULE_CODE_ALIAS: return "ALIAS";
132 case bitc::MODULE_CODE_PURGEVALS: return "PURGEVALS";
133 }
Chris Lattnercd5b7d72007-05-04 03:01:41 +0000134 case bitc::PARAMATTR_BLOCK_ID:
135 switch (CodeID) {
136 default: return 0;
137 case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY";
138 }
Chris Lattnerb30c9252007-04-29 21:48:19 +0000139 case bitc::TYPE_BLOCK_ID:
140 switch (CodeID) {
141 default: return 0;
142 case bitc::TYPE_CODE_NUMENTRY: return "NUMENTRY";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000143 case bitc::TYPE_CODE_VOID: return "VOID";
144 case bitc::TYPE_CODE_FLOAT: return "FLOAT";
145 case bitc::TYPE_CODE_DOUBLE: return "DOUBLE";
146 case bitc::TYPE_CODE_LABEL: return "LABEL";
147 case bitc::TYPE_CODE_OPAQUE: return "OPAQUE";
148 case bitc::TYPE_CODE_INTEGER: return "INTEGER";
149 case bitc::TYPE_CODE_POINTER: return "POINTER";
150 case bitc::TYPE_CODE_FUNCTION: return "FUNCTION";
151 case bitc::TYPE_CODE_STRUCT: return "STRUCT";
152 case bitc::TYPE_CODE_ARRAY: return "ARRAY";
153 case bitc::TYPE_CODE_VECTOR: return "VECTOR";
154 }
155
156 case bitc::CONSTANTS_BLOCK_ID:
157 switch (CodeID) {
158 default: return 0;
159 case bitc::CST_CODE_SETTYPE: return "SETTYPE";
160 case bitc::CST_CODE_NULL: return "NULL";
161 case bitc::CST_CODE_UNDEF: return "UNDEF";
162 case bitc::CST_CODE_INTEGER: return "INTEGER";
163 case bitc::CST_CODE_WIDE_INTEGER: return "WIDE_INTEGER";
164 case bitc::CST_CODE_FLOAT: return "FLOAT";
165 case bitc::CST_CODE_AGGREGATE: return "AGGREGATE";
Chris Lattnercb3d91b2007-05-06 00:53:07 +0000166 case bitc::CST_CODE_STRING: return "STRING";
167 case bitc::CST_CODE_CSTRING: return "CSTRING";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000168 case bitc::CST_CODE_CE_BINOP: return "CE_BINOP";
169 case bitc::CST_CODE_CE_CAST: return "CE_CAST";
170 case bitc::CST_CODE_CE_GEP: return "CE_GEP";
171 case bitc::CST_CODE_CE_SELECT: return "CE_SELECT";
172 case bitc::CST_CODE_CE_EXTRACTELT: return "CE_EXTRACTELT";
173 case bitc::CST_CODE_CE_INSERTELT: return "CE_INSERTELT";
174 case bitc::CST_CODE_CE_SHUFFLEVEC: return "CE_SHUFFLEVEC";
175 case bitc::CST_CODE_CE_CMP: return "CE_CMP";
176 }
177 case bitc::FUNCTION_BLOCK_ID:
178 switch (CodeID) {
179 default: return 0;
180 case bitc::FUNC_CODE_DECLAREBLOCKS: return "DECLAREBLOCKS";
181
182 case bitc::FUNC_CODE_INST_BINOP: return "INST_BINOP";
183 case bitc::FUNC_CODE_INST_CAST: return "INST_CAST";
184 case bitc::FUNC_CODE_INST_GEP: return "INST_GEP";
185 case bitc::FUNC_CODE_INST_SELECT: return "INST_SELECT";
186 case bitc::FUNC_CODE_INST_EXTRACTELT: return "INST_EXTRACTELT";
187 case bitc::FUNC_CODE_INST_INSERTELT: return "INST_INSERTELT";
188 case bitc::FUNC_CODE_INST_SHUFFLEVEC: return "INST_SHUFFLEVEC";
189 case bitc::FUNC_CODE_INST_CMP: return "INST_CMP";
190
191 case bitc::FUNC_CODE_INST_RET: return "INST_RET";
192 case bitc::FUNC_CODE_INST_BR: return "INST_BR";
193 case bitc::FUNC_CODE_INST_SWITCH: return "INST_SWITCH";
194 case bitc::FUNC_CODE_INST_INVOKE: return "INST_INVOKE";
195 case bitc::FUNC_CODE_INST_UNWIND: return "INST_UNWIND";
196 case bitc::FUNC_CODE_INST_UNREACHABLE: return "INST_UNREACHABLE";
197
Chris Lattner8f926682007-05-01 02:43:46 +0000198 case bitc::FUNC_CODE_INST_PHI: return "INST_PHI";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000199 case bitc::FUNC_CODE_INST_MALLOC: return "INST_MALLOC";
200 case bitc::FUNC_CODE_INST_FREE: return "INST_FREE";
201 case bitc::FUNC_CODE_INST_ALLOCA: return "INST_ALLOCA";
202 case bitc::FUNC_CODE_INST_LOAD: return "INST_LOAD";
203 case bitc::FUNC_CODE_INST_STORE: return "INST_STORE";
204 case bitc::FUNC_CODE_INST_CALL: return "INST_CALL";
205 case bitc::FUNC_CODE_INST_VAARG: return "INST_VAARG";
206 }
207 case bitc::TYPE_SYMTAB_BLOCK_ID:
208 switch (CodeID) {
209 default: return 0;
210 case bitc::TST_CODE_ENTRY: return "ENTRY";
211 }
212 case bitc::VALUE_SYMTAB_BLOCK_ID:
213 switch (CodeID) {
214 default: return 0;
215 case bitc::VST_CODE_ENTRY: return "ENTRY";
Chris Lattnercd5b7d72007-05-04 03:01:41 +0000216 case bitc::VST_CODE_BBENTRY: return "BBENTRY";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000217 }
218 }
219}
220
Chris Lattner4238d472007-04-29 20:00:02 +0000221
222struct PerBlockIDStats {
223 /// NumInstances - This the number of times this block ID has been seen.
224 unsigned NumInstances;
225
226 /// NumBits - The total size in bits of all of these blocks.
227 uint64_t NumBits;
228
229 /// NumSubBlocks - The total number of blocks these blocks contain.
230 unsigned NumSubBlocks;
231
232 /// NumAbbrevs - The total number of abbreviations.
233 unsigned NumAbbrevs;
234
235 /// NumRecords - The total number of records these blocks contain, and the
236 /// number that are abbreviated.
237 unsigned NumRecords, NumAbbreviatedRecords;
238
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000239 /// CodeFreq - Keep track of the number of times we see each code.
240 std::vector<unsigned> CodeFreq;
241
Chris Lattner4238d472007-04-29 20:00:02 +0000242 PerBlockIDStats()
243 : NumInstances(0), NumBits(0),
244 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
245};
246
247static std::map<unsigned, PerBlockIDStats> BlockIDStats;
248
249
250
Chris Lattner32de6332007-04-29 08:31:14 +0000251/// Error - All bitcode analysis errors go through this function, making this a
252/// good place to breakpoint if debugging.
253static bool Error(const std::string &Err) {
254 std::cerr << Err << "\n";
255 return true;
256}
257
258/// ParseBlock - Read a block, updating statistics, etc.
Chris Lattnerb30c9252007-04-29 21:48:19 +0000259static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) {
Chris Lattner1772b122007-05-05 00:17:42 +0000260 std::string Indent(IndentLevel*2, ' ');
Chris Lattner4238d472007-04-29 20:00:02 +0000261 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
Chris Lattner32de6332007-04-29 08:31:14 +0000262 unsigned BlockID = Stream.ReadSubBlockID();
Chris Lattnerb30c9252007-04-29 21:48:19 +0000263
Chris Lattner4238d472007-04-29 20:00:02 +0000264 // Get the statistics for this BlockID.
265 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
266
267 BlockStats.NumInstances++;
Chris Lattner32de6332007-04-29 08:31:14 +0000268
Chris Lattner1772b122007-05-05 00:17:42 +0000269 // BLOCKINFO is a special part of the stream.
270 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
271 if (Dump) std::cerr << Indent << "<BLOCKINFO_BLOCK/>\n";
272 if (Stream.ReadBlockInfoBlock())
273 return Error("Malformed BlockInfoBlock");
274 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
275 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
276 return false;
277 }
278
Chris Lattnerb30c9252007-04-29 21:48:19 +0000279 unsigned NumWords = 0;
Chris Lattner1772b122007-05-05 00:17:42 +0000280 if (Stream.EnterSubBlock(BlockID, &NumWords))
Chris Lattner32de6332007-04-29 08:31:14 +0000281 return Error("Malformed block record");
282
Chris Lattnerb30c9252007-04-29 21:48:19 +0000283 const char *BlockName = 0;
284 if (Dump) {
285 std::cerr << Indent << "<";
286 if ((BlockName = GetBlockName(BlockID)))
287 std::cerr << BlockName;
288 else
289 std::cerr << "UnknownBlock" << BlockID;
290
291 if (NonSymbolic && BlockName)
292 std::cerr << " BlockID=" << BlockID;
293
294 std::cerr << " NumWords=" << NumWords
295 << " BlockCodeSize=" << Stream.GetAbbrevIDWidth() << ">\n";
296 }
297
Chris Lattner32de6332007-04-29 08:31:14 +0000298 SmallVector<uint64_t, 64> Record;
299
300 // Read all the records for this block.
301 while (1) {
302 if (Stream.AtEndOfStream())
303 return Error("Premature end of bitstream");
304
305 // Read the code for this record.
306 unsigned AbbrevID = Stream.ReadCode();
307 switch (AbbrevID) {
Chris Lattner4238d472007-04-29 20:00:02 +0000308 case bitc::END_BLOCK: {
Chris Lattner32de6332007-04-29 08:31:14 +0000309 if (Stream.ReadBlockEnd())
310 return Error("Error at end of block");
Chris Lattner4238d472007-04-29 20:00:02 +0000311 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
312 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000313 if (Dump) {
314 std::cerr << Indent << "</";
315 if (BlockName)
316 std::cerr << BlockName << ">\n";
317 else
318 std::cerr << "UnknownBlock" << BlockID << ">\n";
319 }
Chris Lattner32de6332007-04-29 08:31:14 +0000320 return false;
Chris Lattner4238d472007-04-29 20:00:02 +0000321 }
Chris Lattner44b0f102007-05-05 01:29:31 +0000322 case bitc::ENTER_SUBBLOCK: {
323 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
Chris Lattnerb30c9252007-04-29 21:48:19 +0000324 if (ParseBlock(Stream, IndentLevel+1))
Chris Lattner32de6332007-04-29 08:31:14 +0000325 return true;
Chris Lattner4238d472007-04-29 20:00:02 +0000326 ++BlockStats.NumSubBlocks;
Chris Lattner44b0f102007-05-05 01:29:31 +0000327 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
328
329 // Don't include subblock sizes in the size of this block.
330 BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
Chris Lattner32de6332007-04-29 08:31:14 +0000331 break;
Chris Lattner44b0f102007-05-05 01:29:31 +0000332 }
Chris Lattner32de6332007-04-29 08:31:14 +0000333 case bitc::DEFINE_ABBREV:
334 Stream.ReadAbbrevRecord();
Chris Lattner4238d472007-04-29 20:00:02 +0000335 ++BlockStats.NumAbbrevs;
Chris Lattner32de6332007-04-29 08:31:14 +0000336 break;
337 default:
Chris Lattner4238d472007-04-29 20:00:02 +0000338 ++BlockStats.NumRecords;
339 if (AbbrevID != bitc::UNABBREV_RECORD)
340 ++BlockStats.NumAbbreviatedRecords;
341
Chris Lattner32de6332007-04-29 08:31:14 +0000342 Record.clear();
343 unsigned Code = Stream.ReadRecord(AbbrevID, Record);
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000344
345 // Increment the # occurrences of this code.
346 if (BlockStats.CodeFreq.size() <= Code)
347 BlockStats.CodeFreq.resize(Code+1);
348 BlockStats.CodeFreq[Code]++;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000349
350 if (Dump) {
351 std::cerr << Indent << " <";
352 if (const char *CodeName = GetCodeName(Code, BlockID))
353 std::cerr << CodeName;
354 else
355 std::cerr << "UnknownCode" << Code;
356 if (NonSymbolic && GetCodeName(Code, BlockID))
357 std::cerr << " codeid=" << Code;
358 if (AbbrevID != bitc::UNABBREV_RECORD)
359 std::cerr << " abbrevid=" << AbbrevID;
360
361 for (unsigned i = 0, e = Record.size(); i != e; ++i)
362 std::cerr << " op" << i << "=" << (int64_t)Record[i];
363
Chris Lattner1772b122007-05-05 00:17:42 +0000364 std::cerr << "/>\n";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000365 }
366
Chris Lattner32de6332007-04-29 08:31:14 +0000367 break;
368 }
369 }
370}
371
Chris Lattner4238d472007-04-29 20:00:02 +0000372static void PrintSize(double Bits) {
373 std::cerr << Bits << "b/" << Bits/8 << "B/" << Bits/32 << "W";
374}
375
376
Chris Lattner45e0f892007-04-29 08:12:22 +0000377/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
378static int AnalyzeBitcode() {
379 // Read the input file.
380 MemoryBuffer *Buffer;
381 if (InputFilename == "-")
382 Buffer = MemoryBuffer::getSTDIN();
383 else
384 Buffer = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size());
385
Chris Lattner32de6332007-04-29 08:31:14 +0000386 if (Buffer == 0)
387 return Error("Error reading '" + InputFilename + "'.");
Chris Lattner45e0f892007-04-29 08:12:22 +0000388
Chris Lattner32de6332007-04-29 08:31:14 +0000389 if (Buffer->getBufferSize() & 3)
390 return Error("Bitcode stream should be a multiple of 4 bytes in length");
Chris Lattner45e0f892007-04-29 08:12:22 +0000391
392 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
393 BitstreamReader Stream(BufPtr, BufPtr+Buffer->getBufferSize());
394
395
396 // Read the stream signature.
397 char Signature[6];
398 Signature[0] = Stream.Read(8);
399 Signature[1] = Stream.Read(8);
400 Signature[2] = Stream.Read(4);
401 Signature[3] = Stream.Read(4);
402 Signature[4] = Stream.Read(4);
403 Signature[5] = Stream.Read(4);
404
Chris Lattner32de6332007-04-29 08:31:14 +0000405 // Autodetect the file contents, if it is one we know.
Chris Lattner45e0f892007-04-29 08:12:22 +0000406 CurStreamType = UnknownBitstream;
407 if (Signature[0] == 'B' && Signature[1] == 'C' &&
408 Signature[2] == 0x0 && Signature[3] == 0xC &&
409 Signature[4] == 0xE && Signature[5] == 0xD)
410 CurStreamType = LLVMIRBitstream;
411
Chris Lattner4238d472007-04-29 20:00:02 +0000412 unsigned NumTopBlocks = 0;
413
Chris Lattner32de6332007-04-29 08:31:14 +0000414 // Parse the top-level structure. We only allow blocks at the top-level.
415 while (!Stream.AtEndOfStream()) {
416 unsigned Code = Stream.ReadCode();
417 if (Code != bitc::ENTER_SUBBLOCK)
418 return Error("Invalid record at top-level");
419
Chris Lattnerb30c9252007-04-29 21:48:19 +0000420 if (ParseBlock(Stream, 0))
Chris Lattner32de6332007-04-29 08:31:14 +0000421 return true;
Chris Lattner4238d472007-04-29 20:00:02 +0000422 ++NumTopBlocks;
Chris Lattner32de6332007-04-29 08:31:14 +0000423 }
424
Chris Lattnerb30c9252007-04-29 21:48:19 +0000425 if (Dump) std::cerr << "\n\n";
426
Chris Lattner8f926682007-05-01 02:43:46 +0000427 uint64_t BufferSizeBits = Buffer->getBufferSize()*8;
Chris Lattner32de6332007-04-29 08:31:14 +0000428 // Print a summary of the read file.
Chris Lattner45e0f892007-04-29 08:12:22 +0000429 std::cerr << "Summary of " << InputFilename << ":\n";
Chris Lattner4238d472007-04-29 20:00:02 +0000430 std::cerr << " Total size: ";
Chris Lattner8f926682007-05-01 02:43:46 +0000431 PrintSize(BufferSizeBits);
Chris Lattner4238d472007-04-29 20:00:02 +0000432 std::cerr << "\n";
433 std::cerr << " Stream type: ";
Chris Lattner45e0f892007-04-29 08:12:22 +0000434 switch (CurStreamType) {
435 default: assert(0 && "Unknown bitstream type");
436 case UnknownBitstream: std::cerr << "unknown\n"; break;
437 case LLVMIRBitstream: std::cerr << "LLVM IR\n"; break;
438 }
Chris Lattner4238d472007-04-29 20:00:02 +0000439 std::cerr << " # Toplevel Blocks: " << NumTopBlocks << "\n";
440 std::cerr << "\n";
441
442 // Emit per-block stats.
443 std::cerr << "Per-block Summary:\n";
444 for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
445 E = BlockIDStats.end(); I != E; ++I) {
446 std::cerr << " Block ID #" << I->first;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000447 if (const char *BlockName = GetBlockName(I->first))
Chris Lattner4238d472007-04-29 20:00:02 +0000448 std::cerr << " (" << BlockName << ")";
449 std::cerr << ":\n";
450
451 const PerBlockIDStats &Stats = I->second;
452 std::cerr << " Num Instances: " << Stats.NumInstances << "\n";
453 std::cerr << " Total Size: ";
454 PrintSize(Stats.NumBits);
455 std::cerr << "\n";
Chris Lattner8f926682007-05-01 02:43:46 +0000456 std::cerr << " % of file: "
457 << Stats.NumBits/(double)BufferSizeBits*100 << "\n";
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000458 if (Stats.NumInstances > 1) {
459 std::cerr << " Average Size: ";
460 PrintSize(Stats.NumBits/(double)Stats.NumInstances);
461 std::cerr << "\n";
462 std::cerr << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
463 << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
464 std::cerr << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
465 << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
466 std::cerr << " Tot/Avg Records: " << Stats.NumRecords << "/"
467 << Stats.NumRecords/(double)Stats.NumInstances << "\n";
468 } else {
469 std::cerr << " Num SubBlocks: " << Stats.NumSubBlocks << "\n";
470 std::cerr << " Num Abbrevs: " << Stats.NumAbbrevs << "\n";
471 std::cerr << " Num Records: " << Stats.NumRecords << "\n";
472 }
Chris Lattner1772b122007-05-05 00:17:42 +0000473 if (Stats.NumRecords)
474 std::cerr << " % Abbrev Recs: " << (Stats.NumAbbreviatedRecords/
475 (double)Stats.NumRecords)*100 << "\n";
Chris Lattner4238d472007-04-29 20:00:02 +0000476 std::cerr << "\n";
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000477
478 // Print a histogram of the codes we see.
479 if (!NoHistogram && !Stats.CodeFreq.empty()) {
480 std::vector<std::pair<unsigned, unsigned> > FreqPairs; // <freq,code>
481 for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
482 if (unsigned Freq = Stats.CodeFreq[i])
483 FreqPairs.push_back(std::make_pair(Freq, i));
484 std::stable_sort(FreqPairs.begin(), FreqPairs.end());
485 std::reverse(FreqPairs.begin(), FreqPairs.end());
486
487 std::cerr << "\tCode Histogram:\n";
488 for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
489 std::cerr << "\t\t" << FreqPairs[i].first << "\t";
490 if (const char *CodeName = GetCodeName(FreqPairs[i].second, I->first))
491 std::cerr << CodeName << "\n";
492 else
493 std::cerr << "UnknownCode" << FreqPairs[i].second << "\n";
494 }
495 std::cerr << "\n";
496
497 }
Chris Lattner4238d472007-04-29 20:00:02 +0000498 }
Chris Lattner45e0f892007-04-29 08:12:22 +0000499 return 0;
500}
Reid Spencerdac69c82004-06-07 17:53:43 +0000501
Chris Lattner32de6332007-04-29 08:31:14 +0000502
503//===----------------------------------------------------------------------===//
504// Bytecode specific analysis.
505//===----------------------------------------------------------------------===//
506
Chris Lattnerc30598b2006-12-06 01:18:01 +0000507int main(int argc, char **argv) {
508 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Chris Lattner45e0f892007-04-29 08:12:22 +0000509 cl::ParseCommandLineOptions(argc, argv, " llvm-bcanalyzer file analyzer\n");
510
511 sys::PrintStackTraceOnErrorSignal();
512
513 if (Bitcode)
514 return AnalyzeBitcode();
515
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000516 try {
Chris Lattner63db4852007-04-29 05:51:00 +0000517 std::ostream *Out = &std::cout; // Default to printing to stdout...
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000518 std::string ErrorMessage;
519 BytecodeAnalysis bca;
Reid Spencerdac69c82004-06-07 17:53:43 +0000520
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000521 /// Determine what to generate
522 bca.detailedResults = !NoDetails;
523 bca.progressiveVerify = Verify;
Reid Spencer96684ef2004-06-08 05:56:58 +0000524
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000525 /// Analyze the bytecode file
Chris Lattnerf2e292c2007-02-07 21:41:02 +0000526 Module* M = AnalyzeBytecodeFile(InputFilename, bca,
527 Compressor::decompressToNewBuffer,
528 &ErrorMessage, (Dump?Out:0));
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000529
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000530 // All that bcanalyzer does is write the gathered statistics to the output
531 PrintBytecodeAnalysis(bca,*Out);
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000532
Chris Lattner45e0f892007-04-29 08:12:22 +0000533 if (M && Verify) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000534 std::string verificationMsg;
Chris Lattner05ac92c2006-07-06 18:02:27 +0000535 if (verifyModule(*M, ReturnStatusAction, &verificationMsg))
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000536 std::cerr << "Final Verification Message: " << verificationMsg << "\n";
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000537 }
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000538
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000539 if (Out != &std::cout) {
540 ((std::ofstream*)Out)->close();
541 delete Out;
542 }
543 return 0;
544 } catch (const std::string& msg) {
545 std::cerr << argv[0] << ": " << msg << "\n";
546 } catch (...) {
547 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000548 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000549 return 1;
Reid Spencerdac69c82004-06-07 17:53:43 +0000550}