blob: 733e4a9e8680870a7df3ea0c092fc39b52b2fce0 [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";
Chris Lattnerc5ff2cc2007-05-06 01:50:11 +0000176 case bitc::CST_CODE_INLINEASM: return "INLINEASM";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000177 }
178 case bitc::FUNCTION_BLOCK_ID:
179 switch (CodeID) {
180 default: return 0;
181 case bitc::FUNC_CODE_DECLAREBLOCKS: return "DECLAREBLOCKS";
182
183 case bitc::FUNC_CODE_INST_BINOP: return "INST_BINOP";
184 case bitc::FUNC_CODE_INST_CAST: return "INST_CAST";
185 case bitc::FUNC_CODE_INST_GEP: return "INST_GEP";
186 case bitc::FUNC_CODE_INST_SELECT: return "INST_SELECT";
187 case bitc::FUNC_CODE_INST_EXTRACTELT: return "INST_EXTRACTELT";
188 case bitc::FUNC_CODE_INST_INSERTELT: return "INST_INSERTELT";
189 case bitc::FUNC_CODE_INST_SHUFFLEVEC: return "INST_SHUFFLEVEC";
190 case bitc::FUNC_CODE_INST_CMP: return "INST_CMP";
191
192 case bitc::FUNC_CODE_INST_RET: return "INST_RET";
193 case bitc::FUNC_CODE_INST_BR: return "INST_BR";
194 case bitc::FUNC_CODE_INST_SWITCH: return "INST_SWITCH";
195 case bitc::FUNC_CODE_INST_INVOKE: return "INST_INVOKE";
196 case bitc::FUNC_CODE_INST_UNWIND: return "INST_UNWIND";
197 case bitc::FUNC_CODE_INST_UNREACHABLE: return "INST_UNREACHABLE";
198
Chris Lattner8f926682007-05-01 02:43:46 +0000199 case bitc::FUNC_CODE_INST_PHI: return "INST_PHI";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000200 case bitc::FUNC_CODE_INST_MALLOC: return "INST_MALLOC";
201 case bitc::FUNC_CODE_INST_FREE: return "INST_FREE";
202 case bitc::FUNC_CODE_INST_ALLOCA: return "INST_ALLOCA";
203 case bitc::FUNC_CODE_INST_LOAD: return "INST_LOAD";
204 case bitc::FUNC_CODE_INST_STORE: return "INST_STORE";
205 case bitc::FUNC_CODE_INST_CALL: return "INST_CALL";
206 case bitc::FUNC_CODE_INST_VAARG: return "INST_VAARG";
207 }
208 case bitc::TYPE_SYMTAB_BLOCK_ID:
209 switch (CodeID) {
210 default: return 0;
211 case bitc::TST_CODE_ENTRY: return "ENTRY";
212 }
213 case bitc::VALUE_SYMTAB_BLOCK_ID:
214 switch (CodeID) {
215 default: return 0;
216 case bitc::VST_CODE_ENTRY: return "ENTRY";
Chris Lattnercd5b7d72007-05-04 03:01:41 +0000217 case bitc::VST_CODE_BBENTRY: return "BBENTRY";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000218 }
219 }
220}
221
Chris Lattner4238d472007-04-29 20:00:02 +0000222
223struct PerBlockIDStats {
224 /// NumInstances - This the number of times this block ID has been seen.
225 unsigned NumInstances;
226
227 /// NumBits - The total size in bits of all of these blocks.
228 uint64_t NumBits;
229
230 /// NumSubBlocks - The total number of blocks these blocks contain.
231 unsigned NumSubBlocks;
232
233 /// NumAbbrevs - The total number of abbreviations.
234 unsigned NumAbbrevs;
235
236 /// NumRecords - The total number of records these blocks contain, and the
237 /// number that are abbreviated.
238 unsigned NumRecords, NumAbbreviatedRecords;
239
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000240 /// CodeFreq - Keep track of the number of times we see each code.
241 std::vector<unsigned> CodeFreq;
242
Chris Lattner4238d472007-04-29 20:00:02 +0000243 PerBlockIDStats()
244 : NumInstances(0), NumBits(0),
245 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
246};
247
248static std::map<unsigned, PerBlockIDStats> BlockIDStats;
249
250
251
Chris Lattner32de6332007-04-29 08:31:14 +0000252/// Error - All bitcode analysis errors go through this function, making this a
253/// good place to breakpoint if debugging.
254static bool Error(const std::string &Err) {
255 std::cerr << Err << "\n";
256 return true;
257}
258
259/// ParseBlock - Read a block, updating statistics, etc.
Chris Lattnerb30c9252007-04-29 21:48:19 +0000260static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) {
Chris Lattner1772b122007-05-05 00:17:42 +0000261 std::string Indent(IndentLevel*2, ' ');
Chris Lattner4238d472007-04-29 20:00:02 +0000262 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
Chris Lattner32de6332007-04-29 08:31:14 +0000263 unsigned BlockID = Stream.ReadSubBlockID();
Chris Lattnerb30c9252007-04-29 21:48:19 +0000264
Chris Lattner4238d472007-04-29 20:00:02 +0000265 // Get the statistics for this BlockID.
266 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
267
268 BlockStats.NumInstances++;
Chris Lattner32de6332007-04-29 08:31:14 +0000269
Chris Lattner1772b122007-05-05 00:17:42 +0000270 // BLOCKINFO is a special part of the stream.
271 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
272 if (Dump) std::cerr << Indent << "<BLOCKINFO_BLOCK/>\n";
273 if (Stream.ReadBlockInfoBlock())
274 return Error("Malformed BlockInfoBlock");
275 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
276 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
277 return false;
278 }
279
Chris Lattnerb30c9252007-04-29 21:48:19 +0000280 unsigned NumWords = 0;
Chris Lattner1772b122007-05-05 00:17:42 +0000281 if (Stream.EnterSubBlock(BlockID, &NumWords))
Chris Lattner32de6332007-04-29 08:31:14 +0000282 return Error("Malformed block record");
283
Chris Lattnerb30c9252007-04-29 21:48:19 +0000284 const char *BlockName = 0;
285 if (Dump) {
286 std::cerr << Indent << "<";
287 if ((BlockName = GetBlockName(BlockID)))
288 std::cerr << BlockName;
289 else
290 std::cerr << "UnknownBlock" << BlockID;
291
292 if (NonSymbolic && BlockName)
293 std::cerr << " BlockID=" << BlockID;
294
295 std::cerr << " NumWords=" << NumWords
296 << " BlockCodeSize=" << Stream.GetAbbrevIDWidth() << ">\n";
297 }
298
Chris Lattner32de6332007-04-29 08:31:14 +0000299 SmallVector<uint64_t, 64> Record;
300
301 // Read all the records for this block.
302 while (1) {
303 if (Stream.AtEndOfStream())
304 return Error("Premature end of bitstream");
305
306 // Read the code for this record.
307 unsigned AbbrevID = Stream.ReadCode();
308 switch (AbbrevID) {
Chris Lattner4238d472007-04-29 20:00:02 +0000309 case bitc::END_BLOCK: {
Chris Lattner32de6332007-04-29 08:31:14 +0000310 if (Stream.ReadBlockEnd())
311 return Error("Error at end of block");
Chris Lattner4238d472007-04-29 20:00:02 +0000312 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
313 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000314 if (Dump) {
315 std::cerr << Indent << "</";
316 if (BlockName)
317 std::cerr << BlockName << ">\n";
318 else
319 std::cerr << "UnknownBlock" << BlockID << ">\n";
320 }
Chris Lattner32de6332007-04-29 08:31:14 +0000321 return false;
Chris Lattner4238d472007-04-29 20:00:02 +0000322 }
Chris Lattner44b0f102007-05-05 01:29:31 +0000323 case bitc::ENTER_SUBBLOCK: {
324 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
Chris Lattnerb30c9252007-04-29 21:48:19 +0000325 if (ParseBlock(Stream, IndentLevel+1))
Chris Lattner32de6332007-04-29 08:31:14 +0000326 return true;
Chris Lattner4238d472007-04-29 20:00:02 +0000327 ++BlockStats.NumSubBlocks;
Chris Lattner44b0f102007-05-05 01:29:31 +0000328 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
329
330 // Don't include subblock sizes in the size of this block.
331 BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
Chris Lattner32de6332007-04-29 08:31:14 +0000332 break;
Chris Lattner44b0f102007-05-05 01:29:31 +0000333 }
Chris Lattner32de6332007-04-29 08:31:14 +0000334 case bitc::DEFINE_ABBREV:
335 Stream.ReadAbbrevRecord();
Chris Lattner4238d472007-04-29 20:00:02 +0000336 ++BlockStats.NumAbbrevs;
Chris Lattner32de6332007-04-29 08:31:14 +0000337 break;
338 default:
Chris Lattner4238d472007-04-29 20:00:02 +0000339 ++BlockStats.NumRecords;
340 if (AbbrevID != bitc::UNABBREV_RECORD)
341 ++BlockStats.NumAbbreviatedRecords;
342
Chris Lattner32de6332007-04-29 08:31:14 +0000343 Record.clear();
344 unsigned Code = Stream.ReadRecord(AbbrevID, Record);
Chris Lattnerb1e85b52007-05-05 01:46:49 +0000345
346 // Increment the # occurrences of this code.
347 if (BlockStats.CodeFreq.size() <= Code)
348 BlockStats.CodeFreq.resize(Code+1);
349 BlockStats.CodeFreq[Code]++;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000350
351 if (Dump) {
352 std::cerr << Indent << " <";
353 if (const char *CodeName = GetCodeName(Code, BlockID))
354 std::cerr << CodeName;
355 else
356 std::cerr << "UnknownCode" << Code;
357 if (NonSymbolic && GetCodeName(Code, BlockID))
358 std::cerr << " codeid=" << Code;
359 if (AbbrevID != bitc::UNABBREV_RECORD)
360 std::cerr << " abbrevid=" << AbbrevID;
361
362 for (unsigned i = 0, e = Record.size(); i != e; ++i)
363 std::cerr << " op" << i << "=" << (int64_t)Record[i];
364
Chris Lattner1772b122007-05-05 00:17:42 +0000365 std::cerr << "/>\n";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000366 }
367
Chris Lattner32de6332007-04-29 08:31:14 +0000368 break;
369 }
370 }
371}
372
Chris Lattner4238d472007-04-29 20:00:02 +0000373static void PrintSize(double Bits) {
374 std::cerr << Bits << "b/" << Bits/8 << "B/" << Bits/32 << "W";
375}
376
377
Chris Lattner45e0f892007-04-29 08:12:22 +0000378/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
379static int AnalyzeBitcode() {
380 // Read the input file.
381 MemoryBuffer *Buffer;
382 if (InputFilename == "-")
383 Buffer = MemoryBuffer::getSTDIN();
384 else
385 Buffer = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size());
386
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
504//===----------------------------------------------------------------------===//
505// Bytecode specific analysis.
506//===----------------------------------------------------------------------===//
507
Chris Lattnerc30598b2006-12-06 01:18:01 +0000508int main(int argc, char **argv) {
509 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Chris Lattner45e0f892007-04-29 08:12:22 +0000510 cl::ParseCommandLineOptions(argc, argv, " llvm-bcanalyzer file analyzer\n");
511
512 sys::PrintStackTraceOnErrorSignal();
513
514 if (Bitcode)
515 return AnalyzeBitcode();
516
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000517 try {
Chris Lattner63db4852007-04-29 05:51:00 +0000518 std::ostream *Out = &std::cout; // Default to printing to stdout...
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000519 std::string ErrorMessage;
520 BytecodeAnalysis bca;
Reid Spencerdac69c82004-06-07 17:53:43 +0000521
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000522 /// Determine what to generate
523 bca.detailedResults = !NoDetails;
524 bca.progressiveVerify = Verify;
Reid Spencer96684ef2004-06-08 05:56:58 +0000525
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000526 /// Analyze the bytecode file
Chris Lattnerf2e292c2007-02-07 21:41:02 +0000527 Module* M = AnalyzeBytecodeFile(InputFilename, bca,
528 Compressor::decompressToNewBuffer,
529 &ErrorMessage, (Dump?Out:0));
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000530
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000531 // All that bcanalyzer does is write the gathered statistics to the output
532 PrintBytecodeAnalysis(bca,*Out);
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000533
Chris Lattner45e0f892007-04-29 08:12:22 +0000534 if (M && Verify) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000535 std::string verificationMsg;
Chris Lattner05ac92c2006-07-06 18:02:27 +0000536 if (verifyModule(*M, ReturnStatusAction, &verificationMsg))
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000537 std::cerr << "Final Verification Message: " << verificationMsg << "\n";
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000538 }
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000539
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000540 if (Out != &std::cout) {
541 ((std::ofstream*)Out)->close();
542 delete Out;
543 }
544 return 0;
545 } catch (const std::string& msg) {
546 std::cerr << argv[0] << ": " << msg << "\n";
547 } catch (...) {
548 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000549 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000550 return 1;
Reid Spencerdac69c82004-06-07 17:53:43 +0000551}