blob: 971ef5cd4e6f4c6753af8c40a06ba9f7aaacf5d1 [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>
Reid Spencerdac69c82004-06-07 17:53:43 +000044using namespace llvm;
45
46static cl::opt<std::string>
47 InputFilename(cl::Positional, cl::desc("<input bytecode>"), cl::init("-"));
48
Reid Spencer75937452004-08-21 21:00:24 +000049static cl::opt<std::string>
50 OutputFilename("-o", cl::init("-"), cl::desc("<output file>"));
51
Chris Lattner63db4852007-04-29 05:51:00 +000052static cl::opt<bool> NoDetails("nodetails", cl::desc("Skip detailed output"));
53static cl::opt<bool> Dump("dump", cl::desc("Dump low level bytecode trace"));
54static cl::opt<bool> Verify("verify", cl::desc("Progressively verify module"));
Chris Lattner32de6332007-04-29 08:31:14 +000055
56//===----------------------------------------------------------------------===//
57// Bitcode specific analysis.
58//===----------------------------------------------------------------------===//
59
Chris Lattner45e0f892007-04-29 08:12:22 +000060static cl::opt<bool> Bitcode("bitcode", cl::desc("Read a bitcode file"));
61
Chris Lattnerb30c9252007-04-29 21:48:19 +000062static cl::opt<bool>
63NonSymbolic("non-symbolic",
64 cl::desc("Emit numberic info in dump even if"
65 " symbolic info is available"));
66
Chris Lattner45e0f892007-04-29 08:12:22 +000067/// CurStreamType - If we can sniff the flavor of this stream, we can produce
68/// better dump info.
69static enum {
70 UnknownBitstream,
71 LLVMIRBitstream
72} CurStreamType;
73
Chris Lattner4238d472007-04-29 20:00:02 +000074
75/// GetBlockName - Return a symbolic block name if known, otherwise return
Chris Lattnerb30c9252007-04-29 21:48:19 +000076/// null.
Chris Lattner4238d472007-04-29 20:00:02 +000077static const char *GetBlockName(unsigned BlockID) {
Chris Lattner1772b122007-05-05 00:17:42 +000078 // Standard blocks for all bitcode files.
79 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
80 if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
81 return "BLOCKINFO_BLOCK";
82 return 0;
83 }
84
Chris Lattnerb30c9252007-04-29 21:48:19 +000085 if (CurStreamType != LLVMIRBitstream) return 0;
Chris Lattner4238d472007-04-29 20:00:02 +000086
87 switch (BlockID) {
Chris Lattnerb30c9252007-04-29 21:48:19 +000088 default: return 0;
Chris Lattner4238d472007-04-29 20:00:02 +000089 case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK";
Chris Lattnercd5b7d72007-05-04 03:01:41 +000090 case bitc::PARAMATTR_BLOCK_ID: return "PARAMATTR_BLOCK";
Chris Lattner4238d472007-04-29 20:00:02 +000091 case bitc::TYPE_BLOCK_ID: return "TYPE_BLOCK";
92 case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK";
93 case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK";
94 case bitc::TYPE_SYMTAB_BLOCK_ID: return "TYPE_SYMTAB";
95 case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB";
96 }
97}
98
Chris Lattnerb30c9252007-04-29 21:48:19 +000099/// GetCodeName - Return a symbolic code name if known, otherwise return
100/// null.
101static const char *GetCodeName(unsigned CodeID, unsigned BlockID) {
Chris Lattner1772b122007-05-05 00:17:42 +0000102 // Standard blocks for all bitcode files.
103 if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
104 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
105 switch (CodeID) {
106 default: return 0;
107 case bitc::MODULE_CODE_VERSION: return "VERSION";
108 }
109 }
110 return 0;
111 }
112
113
Chris Lattnerb30c9252007-04-29 21:48:19 +0000114 if (CurStreamType != LLVMIRBitstream) return 0;
115
116 switch (BlockID) {
117 default: return 0;
118 case bitc::MODULE_BLOCK_ID:
119 switch (CodeID) {
120 default: return 0;
121 case bitc::MODULE_CODE_VERSION: return "VERSION";
122 case bitc::MODULE_CODE_TRIPLE: return "TRIPLE";
123 case bitc::MODULE_CODE_DATALAYOUT: return "DATALAYOUT";
124 case bitc::MODULE_CODE_ASM: return "ASM";
125 case bitc::MODULE_CODE_SECTIONNAME: return "SECTIONNAME";
126 case bitc::MODULE_CODE_DEPLIB: return "DEPLIB";
127 case bitc::MODULE_CODE_GLOBALVAR: return "GLOBALVAR";
128 case bitc::MODULE_CODE_FUNCTION: return "FUNCTION";
129 case bitc::MODULE_CODE_ALIAS: return "ALIAS";
130 case bitc::MODULE_CODE_PURGEVALS: return "PURGEVALS";
131 }
Chris Lattnercd5b7d72007-05-04 03:01:41 +0000132 case bitc::PARAMATTR_BLOCK_ID:
133 switch (CodeID) {
134 default: return 0;
135 case bitc::PARAMATTR_CODE_ENTRY: return "ENTRY";
136 }
Chris Lattnerb30c9252007-04-29 21:48:19 +0000137 case bitc::TYPE_BLOCK_ID:
138 switch (CodeID) {
139 default: return 0;
140 case bitc::TYPE_CODE_NUMENTRY: return "NUMENTRY";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000141 case bitc::TYPE_CODE_VOID: return "VOID";
142 case bitc::TYPE_CODE_FLOAT: return "FLOAT";
143 case bitc::TYPE_CODE_DOUBLE: return "DOUBLE";
144 case bitc::TYPE_CODE_LABEL: return "LABEL";
145 case bitc::TYPE_CODE_OPAQUE: return "OPAQUE";
146 case bitc::TYPE_CODE_INTEGER: return "INTEGER";
147 case bitc::TYPE_CODE_POINTER: return "POINTER";
148 case bitc::TYPE_CODE_FUNCTION: return "FUNCTION";
149 case bitc::TYPE_CODE_STRUCT: return "STRUCT";
150 case bitc::TYPE_CODE_ARRAY: return "ARRAY";
151 case bitc::TYPE_CODE_VECTOR: return "VECTOR";
152 }
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";
164 case bitc::CST_CODE_CE_BINOP: return "CE_BINOP";
165 case bitc::CST_CODE_CE_CAST: return "CE_CAST";
166 case bitc::CST_CODE_CE_GEP: return "CE_GEP";
167 case bitc::CST_CODE_CE_SELECT: return "CE_SELECT";
168 case bitc::CST_CODE_CE_EXTRACTELT: return "CE_EXTRACTELT";
169 case bitc::CST_CODE_CE_INSERTELT: return "CE_INSERTELT";
170 case bitc::CST_CODE_CE_SHUFFLEVEC: return "CE_SHUFFLEVEC";
171 case bitc::CST_CODE_CE_CMP: return "CE_CMP";
172 }
173 case bitc::FUNCTION_BLOCK_ID:
174 switch (CodeID) {
175 default: return 0;
176 case bitc::FUNC_CODE_DECLAREBLOCKS: return "DECLAREBLOCKS";
177
178 case bitc::FUNC_CODE_INST_BINOP: return "INST_BINOP";
179 case bitc::FUNC_CODE_INST_CAST: return "INST_CAST";
180 case bitc::FUNC_CODE_INST_GEP: return "INST_GEP";
181 case bitc::FUNC_CODE_INST_SELECT: return "INST_SELECT";
182 case bitc::FUNC_CODE_INST_EXTRACTELT: return "INST_EXTRACTELT";
183 case bitc::FUNC_CODE_INST_INSERTELT: return "INST_INSERTELT";
184 case bitc::FUNC_CODE_INST_SHUFFLEVEC: return "INST_SHUFFLEVEC";
185 case bitc::FUNC_CODE_INST_CMP: return "INST_CMP";
186
187 case bitc::FUNC_CODE_INST_RET: return "INST_RET";
188 case bitc::FUNC_CODE_INST_BR: return "INST_BR";
189 case bitc::FUNC_CODE_INST_SWITCH: return "INST_SWITCH";
190 case bitc::FUNC_CODE_INST_INVOKE: return "INST_INVOKE";
191 case bitc::FUNC_CODE_INST_UNWIND: return "INST_UNWIND";
192 case bitc::FUNC_CODE_INST_UNREACHABLE: return "INST_UNREACHABLE";
193
Chris Lattner8f926682007-05-01 02:43:46 +0000194 case bitc::FUNC_CODE_INST_PHI: return "INST_PHI";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000195 case bitc::FUNC_CODE_INST_MALLOC: return "INST_MALLOC";
196 case bitc::FUNC_CODE_INST_FREE: return "INST_FREE";
197 case bitc::FUNC_CODE_INST_ALLOCA: return "INST_ALLOCA";
198 case bitc::FUNC_CODE_INST_LOAD: return "INST_LOAD";
199 case bitc::FUNC_CODE_INST_STORE: return "INST_STORE";
200 case bitc::FUNC_CODE_INST_CALL: return "INST_CALL";
201 case bitc::FUNC_CODE_INST_VAARG: return "INST_VAARG";
202 }
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
235 PerBlockIDStats()
236 : NumInstances(0), NumBits(0),
237 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
238};
239
240static std::map<unsigned, PerBlockIDStats> BlockIDStats;
241
242
243
Chris Lattner32de6332007-04-29 08:31:14 +0000244/// Error - All bitcode analysis errors go through this function, making this a
245/// good place to breakpoint if debugging.
246static bool Error(const std::string &Err) {
247 std::cerr << Err << "\n";
248 return true;
249}
250
251/// ParseBlock - Read a block, updating statistics, etc.
Chris Lattnerb30c9252007-04-29 21:48:19 +0000252static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) {
Chris Lattner1772b122007-05-05 00:17:42 +0000253 std::string Indent(IndentLevel*2, ' ');
Chris Lattner4238d472007-04-29 20:00:02 +0000254 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
Chris Lattner32de6332007-04-29 08:31:14 +0000255 unsigned BlockID = Stream.ReadSubBlockID();
Chris Lattnerb30c9252007-04-29 21:48:19 +0000256
Chris Lattner4238d472007-04-29 20:00:02 +0000257 // Get the statistics for this BlockID.
258 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
259
260 BlockStats.NumInstances++;
Chris Lattner32de6332007-04-29 08:31:14 +0000261
Chris Lattner1772b122007-05-05 00:17:42 +0000262 // BLOCKINFO is a special part of the stream.
263 if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
264 if (Dump) std::cerr << Indent << "<BLOCKINFO_BLOCK/>\n";
265 if (Stream.ReadBlockInfoBlock())
266 return Error("Malformed BlockInfoBlock");
267 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
268 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
269 return false;
270 }
271
Chris Lattnerb30c9252007-04-29 21:48:19 +0000272 unsigned NumWords = 0;
Chris Lattner1772b122007-05-05 00:17:42 +0000273 if (Stream.EnterSubBlock(BlockID, &NumWords))
Chris Lattner32de6332007-04-29 08:31:14 +0000274 return Error("Malformed block record");
275
Chris Lattnerb30c9252007-04-29 21:48:19 +0000276 const char *BlockName = 0;
277 if (Dump) {
278 std::cerr << Indent << "<";
279 if ((BlockName = GetBlockName(BlockID)))
280 std::cerr << BlockName;
281 else
282 std::cerr << "UnknownBlock" << BlockID;
283
284 if (NonSymbolic && BlockName)
285 std::cerr << " BlockID=" << BlockID;
286
287 std::cerr << " NumWords=" << NumWords
288 << " BlockCodeSize=" << Stream.GetAbbrevIDWidth() << ">\n";
289 }
290
Chris Lattner32de6332007-04-29 08:31:14 +0000291 SmallVector<uint64_t, 64> Record;
292
293 // Read all the records for this block.
294 while (1) {
295 if (Stream.AtEndOfStream())
296 return Error("Premature end of bitstream");
297
298 // Read the code for this record.
299 unsigned AbbrevID = Stream.ReadCode();
300 switch (AbbrevID) {
Chris Lattner4238d472007-04-29 20:00:02 +0000301 case bitc::END_BLOCK: {
Chris Lattner32de6332007-04-29 08:31:14 +0000302 if (Stream.ReadBlockEnd())
303 return Error("Error at end of block");
Chris Lattner4238d472007-04-29 20:00:02 +0000304 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
305 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000306 if (Dump) {
307 std::cerr << Indent << "</";
308 if (BlockName)
309 std::cerr << BlockName << ">\n";
310 else
311 std::cerr << "UnknownBlock" << BlockID << ">\n";
312 }
Chris Lattner32de6332007-04-29 08:31:14 +0000313 return false;
Chris Lattner4238d472007-04-29 20:00:02 +0000314 }
Chris Lattner44b0f102007-05-05 01:29:31 +0000315 case bitc::ENTER_SUBBLOCK: {
316 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
Chris Lattnerb30c9252007-04-29 21:48:19 +0000317 if (ParseBlock(Stream, IndentLevel+1))
Chris Lattner32de6332007-04-29 08:31:14 +0000318 return true;
Chris Lattner4238d472007-04-29 20:00:02 +0000319 ++BlockStats.NumSubBlocks;
Chris Lattner44b0f102007-05-05 01:29:31 +0000320 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
321
322 // Don't include subblock sizes in the size of this block.
323 BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
Chris Lattner32de6332007-04-29 08:31:14 +0000324 break;
Chris Lattner44b0f102007-05-05 01:29:31 +0000325 }
Chris Lattner32de6332007-04-29 08:31:14 +0000326 case bitc::DEFINE_ABBREV:
327 Stream.ReadAbbrevRecord();
Chris Lattner4238d472007-04-29 20:00:02 +0000328 ++BlockStats.NumAbbrevs;
Chris Lattner32de6332007-04-29 08:31:14 +0000329 break;
330 default:
Chris Lattner4238d472007-04-29 20:00:02 +0000331 ++BlockStats.NumRecords;
332 if (AbbrevID != bitc::UNABBREV_RECORD)
333 ++BlockStats.NumAbbreviatedRecords;
334
Chris Lattner32de6332007-04-29 08:31:14 +0000335 Record.clear();
336 unsigned Code = Stream.ReadRecord(AbbrevID, Record);
337 // TODO: Compute per-blockid/code stats.
Chris Lattnerb30c9252007-04-29 21:48:19 +0000338
339 if (Dump) {
340 std::cerr << Indent << " <";
341 if (const char *CodeName = GetCodeName(Code, BlockID))
342 std::cerr << CodeName;
343 else
344 std::cerr << "UnknownCode" << Code;
345 if (NonSymbolic && GetCodeName(Code, BlockID))
346 std::cerr << " codeid=" << Code;
347 if (AbbrevID != bitc::UNABBREV_RECORD)
348 std::cerr << " abbrevid=" << AbbrevID;
349
350 for (unsigned i = 0, e = Record.size(); i != e; ++i)
351 std::cerr << " op" << i << "=" << (int64_t)Record[i];
352
Chris Lattner1772b122007-05-05 00:17:42 +0000353 std::cerr << "/>\n";
Chris Lattnerb30c9252007-04-29 21:48:19 +0000354 }
355
Chris Lattner32de6332007-04-29 08:31:14 +0000356 break;
357 }
358 }
359}
360
Chris Lattner4238d472007-04-29 20:00:02 +0000361static void PrintSize(double Bits) {
362 std::cerr << Bits << "b/" << Bits/8 << "B/" << Bits/32 << "W";
363}
364
365
Chris Lattner45e0f892007-04-29 08:12:22 +0000366/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
367static int AnalyzeBitcode() {
368 // Read the input file.
369 MemoryBuffer *Buffer;
370 if (InputFilename == "-")
371 Buffer = MemoryBuffer::getSTDIN();
372 else
373 Buffer = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size());
374
Chris Lattner32de6332007-04-29 08:31:14 +0000375 if (Buffer == 0)
376 return Error("Error reading '" + InputFilename + "'.");
Chris Lattner45e0f892007-04-29 08:12:22 +0000377
Chris Lattner32de6332007-04-29 08:31:14 +0000378 if (Buffer->getBufferSize() & 3)
379 return Error("Bitcode stream should be a multiple of 4 bytes in length");
Chris Lattner45e0f892007-04-29 08:12:22 +0000380
381 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
382 BitstreamReader Stream(BufPtr, BufPtr+Buffer->getBufferSize());
383
384
385 // Read the stream signature.
386 char Signature[6];
387 Signature[0] = Stream.Read(8);
388 Signature[1] = Stream.Read(8);
389 Signature[2] = Stream.Read(4);
390 Signature[3] = Stream.Read(4);
391 Signature[4] = Stream.Read(4);
392 Signature[5] = Stream.Read(4);
393
Chris Lattner32de6332007-04-29 08:31:14 +0000394 // Autodetect the file contents, if it is one we know.
Chris Lattner45e0f892007-04-29 08:12:22 +0000395 CurStreamType = UnknownBitstream;
396 if (Signature[0] == 'B' && Signature[1] == 'C' &&
397 Signature[2] == 0x0 && Signature[3] == 0xC &&
398 Signature[4] == 0xE && Signature[5] == 0xD)
399 CurStreamType = LLVMIRBitstream;
400
Chris Lattner4238d472007-04-29 20:00:02 +0000401 unsigned NumTopBlocks = 0;
402
Chris Lattner32de6332007-04-29 08:31:14 +0000403 // Parse the top-level structure. We only allow blocks at the top-level.
404 while (!Stream.AtEndOfStream()) {
405 unsigned Code = Stream.ReadCode();
406 if (Code != bitc::ENTER_SUBBLOCK)
407 return Error("Invalid record at top-level");
408
Chris Lattnerb30c9252007-04-29 21:48:19 +0000409 if (ParseBlock(Stream, 0))
Chris Lattner32de6332007-04-29 08:31:14 +0000410 return true;
Chris Lattner4238d472007-04-29 20:00:02 +0000411 ++NumTopBlocks;
Chris Lattner32de6332007-04-29 08:31:14 +0000412 }
413
Chris Lattnerb30c9252007-04-29 21:48:19 +0000414 if (Dump) std::cerr << "\n\n";
415
Chris Lattner8f926682007-05-01 02:43:46 +0000416 uint64_t BufferSizeBits = Buffer->getBufferSize()*8;
Chris Lattner32de6332007-04-29 08:31:14 +0000417 // Print a summary of the read file.
Chris Lattner45e0f892007-04-29 08:12:22 +0000418 std::cerr << "Summary of " << InputFilename << ":\n";
Chris Lattner4238d472007-04-29 20:00:02 +0000419 std::cerr << " Total size: ";
Chris Lattner8f926682007-05-01 02:43:46 +0000420 PrintSize(BufferSizeBits);
Chris Lattner4238d472007-04-29 20:00:02 +0000421 std::cerr << "\n";
422 std::cerr << " Stream type: ";
Chris Lattner45e0f892007-04-29 08:12:22 +0000423 switch (CurStreamType) {
424 default: assert(0 && "Unknown bitstream type");
425 case UnknownBitstream: std::cerr << "unknown\n"; break;
426 case LLVMIRBitstream: std::cerr << "LLVM IR\n"; break;
427 }
Chris Lattner4238d472007-04-29 20:00:02 +0000428 std::cerr << " # Toplevel Blocks: " << NumTopBlocks << "\n";
429 std::cerr << "\n";
430
431 // Emit per-block stats.
432 std::cerr << "Per-block Summary:\n";
433 for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
434 E = BlockIDStats.end(); I != E; ++I) {
435 std::cerr << " Block ID #" << I->first;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000436 if (const char *BlockName = GetBlockName(I->first))
Chris Lattner4238d472007-04-29 20:00:02 +0000437 std::cerr << " (" << BlockName << ")";
438 std::cerr << ":\n";
439
440 const PerBlockIDStats &Stats = I->second;
441 std::cerr << " Num Instances: " << Stats.NumInstances << "\n";
442 std::cerr << " Total Size: ";
443 PrintSize(Stats.NumBits);
444 std::cerr << "\n";
445 std::cerr << " Average Size: ";
446 PrintSize(Stats.NumBits/(double)Stats.NumInstances);
447 std::cerr << "\n";
Chris Lattner8f926682007-05-01 02:43:46 +0000448 std::cerr << " % of file: "
449 << Stats.NumBits/(double)BufferSizeBits*100 << "\n";
Chris Lattner4238d472007-04-29 20:00:02 +0000450 std::cerr << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
451 << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
452 std::cerr << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
453 << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
454 std::cerr << " Tot/Avg Records: " << Stats.NumRecords << "/"
455 << Stats.NumRecords/(double)Stats.NumInstances << "\n";
Chris Lattner1772b122007-05-05 00:17:42 +0000456 if (Stats.NumRecords)
457 std::cerr << " % Abbrev Recs: " << (Stats.NumAbbreviatedRecords/
458 (double)Stats.NumRecords)*100 << "\n";
Chris Lattner4238d472007-04-29 20:00:02 +0000459 std::cerr << "\n";
460 }
Chris Lattner45e0f892007-04-29 08:12:22 +0000461 return 0;
462}
Reid Spencerdac69c82004-06-07 17:53:43 +0000463
Chris Lattner32de6332007-04-29 08:31:14 +0000464
465//===----------------------------------------------------------------------===//
466// Bytecode specific analysis.
467//===----------------------------------------------------------------------===//
468
Chris Lattnerc30598b2006-12-06 01:18:01 +0000469int main(int argc, char **argv) {
470 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Chris Lattner45e0f892007-04-29 08:12:22 +0000471 cl::ParseCommandLineOptions(argc, argv, " llvm-bcanalyzer file analyzer\n");
472
473 sys::PrintStackTraceOnErrorSignal();
474
475 if (Bitcode)
476 return AnalyzeBitcode();
477
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000478 try {
Chris Lattner63db4852007-04-29 05:51:00 +0000479 std::ostream *Out = &std::cout; // Default to printing to stdout...
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000480 std::string ErrorMessage;
481 BytecodeAnalysis bca;
Reid Spencerdac69c82004-06-07 17:53:43 +0000482
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000483 /// Determine what to generate
484 bca.detailedResults = !NoDetails;
485 bca.progressiveVerify = Verify;
Reid Spencer96684ef2004-06-08 05:56:58 +0000486
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000487 /// Analyze the bytecode file
Chris Lattnerf2e292c2007-02-07 21:41:02 +0000488 Module* M = AnalyzeBytecodeFile(InputFilename, bca,
489 Compressor::decompressToNewBuffer,
490 &ErrorMessage, (Dump?Out:0));
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000491
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000492 // All that bcanalyzer does is write the gathered statistics to the output
493 PrintBytecodeAnalysis(bca,*Out);
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000494
Chris Lattner45e0f892007-04-29 08:12:22 +0000495 if (M && Verify) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000496 std::string verificationMsg;
Chris Lattner05ac92c2006-07-06 18:02:27 +0000497 if (verifyModule(*M, ReturnStatusAction, &verificationMsg))
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000498 std::cerr << "Final Verification Message: " << verificationMsg << "\n";
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000499 }
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000500
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000501 if (Out != &std::cout) {
502 ((std::ofstream*)Out)->close();
503 delete Out;
504 }
505 return 0;
506 } catch (const std::string& msg) {
507 std::cerr << argv[0] << ": " << msg << "\n";
508 } catch (...) {
509 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000510 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000511 return 1;
Reid Spencerdac69c82004-06-07 17:53:43 +0000512}