blob: 118da1177354f2587e6e7e9c4d9dd560b551ef6f [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 Lattnerb30c9252007-04-29 21:48:19 +000078 if (CurStreamType != LLVMIRBitstream) return 0;
Chris Lattner4238d472007-04-29 20:00:02 +000079
80 switch (BlockID) {
Chris Lattnerb30c9252007-04-29 21:48:19 +000081 default: return 0;
Chris Lattner4238d472007-04-29 20:00:02 +000082 case bitc::MODULE_BLOCK_ID: return "MODULE_BLOCK";
83 case bitc::TYPE_BLOCK_ID: return "TYPE_BLOCK";
84 case bitc::CONSTANTS_BLOCK_ID: return "CONSTANTS_BLOCK";
85 case bitc::FUNCTION_BLOCK_ID: return "FUNCTION_BLOCK";
86 case bitc::TYPE_SYMTAB_BLOCK_ID: return "TYPE_SYMTAB";
87 case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB";
88 }
89}
90
Chris Lattnerb30c9252007-04-29 21:48:19 +000091/// GetCodeName - Return a symbolic code name if known, otherwise return
92/// null.
93static const char *GetCodeName(unsigned CodeID, unsigned BlockID) {
94 if (CurStreamType != LLVMIRBitstream) return 0;
95
96 switch (BlockID) {
97 default: return 0;
98 case bitc::MODULE_BLOCK_ID:
99 switch (CodeID) {
100 default: return 0;
101 case bitc::MODULE_CODE_VERSION: return "VERSION";
102 case bitc::MODULE_CODE_TRIPLE: return "TRIPLE";
103 case bitc::MODULE_CODE_DATALAYOUT: return "DATALAYOUT";
104 case bitc::MODULE_CODE_ASM: return "ASM";
105 case bitc::MODULE_CODE_SECTIONNAME: return "SECTIONNAME";
106 case bitc::MODULE_CODE_DEPLIB: return "DEPLIB";
107 case bitc::MODULE_CODE_GLOBALVAR: return "GLOBALVAR";
108 case bitc::MODULE_CODE_FUNCTION: return "FUNCTION";
109 case bitc::MODULE_CODE_ALIAS: return "ALIAS";
110 case bitc::MODULE_CODE_PURGEVALS: return "PURGEVALS";
111 }
112 case bitc::TYPE_BLOCK_ID:
113 switch (CodeID) {
114 default: return 0;
115 case bitc::TYPE_CODE_NUMENTRY: return "NUMENTRY";
116 case bitc::TYPE_CODE_META: return "META";
117 case bitc::TYPE_CODE_VOID: return "VOID";
118 case bitc::TYPE_CODE_FLOAT: return "FLOAT";
119 case bitc::TYPE_CODE_DOUBLE: return "DOUBLE";
120 case bitc::TYPE_CODE_LABEL: return "LABEL";
121 case bitc::TYPE_CODE_OPAQUE: return "OPAQUE";
122 case bitc::TYPE_CODE_INTEGER: return "INTEGER";
123 case bitc::TYPE_CODE_POINTER: return "POINTER";
124 case bitc::TYPE_CODE_FUNCTION: return "FUNCTION";
125 case bitc::TYPE_CODE_STRUCT: return "STRUCT";
126 case bitc::TYPE_CODE_ARRAY: return "ARRAY";
127 case bitc::TYPE_CODE_VECTOR: return "VECTOR";
128 }
129
130 case bitc::CONSTANTS_BLOCK_ID:
131 switch (CodeID) {
132 default: return 0;
133 case bitc::CST_CODE_SETTYPE: return "SETTYPE";
134 case bitc::CST_CODE_NULL: return "NULL";
135 case bitc::CST_CODE_UNDEF: return "UNDEF";
136 case bitc::CST_CODE_INTEGER: return "INTEGER";
137 case bitc::CST_CODE_WIDE_INTEGER: return "WIDE_INTEGER";
138 case bitc::CST_CODE_FLOAT: return "FLOAT";
139 case bitc::CST_CODE_AGGREGATE: return "AGGREGATE";
140 case bitc::CST_CODE_CE_BINOP: return "CE_BINOP";
141 case bitc::CST_CODE_CE_CAST: return "CE_CAST";
142 case bitc::CST_CODE_CE_GEP: return "CE_GEP";
143 case bitc::CST_CODE_CE_SELECT: return "CE_SELECT";
144 case bitc::CST_CODE_CE_EXTRACTELT: return "CE_EXTRACTELT";
145 case bitc::CST_CODE_CE_INSERTELT: return "CE_INSERTELT";
146 case bitc::CST_CODE_CE_SHUFFLEVEC: return "CE_SHUFFLEVEC";
147 case bitc::CST_CODE_CE_CMP: return "CE_CMP";
148 }
149 case bitc::FUNCTION_BLOCK_ID:
150 switch (CodeID) {
151 default: return 0;
152 case bitc::FUNC_CODE_DECLAREBLOCKS: return "DECLAREBLOCKS";
153
154 case bitc::FUNC_CODE_INST_BINOP: return "INST_BINOP";
155 case bitc::FUNC_CODE_INST_CAST: return "INST_CAST";
156 case bitc::FUNC_CODE_INST_GEP: return "INST_GEP";
157 case bitc::FUNC_CODE_INST_SELECT: return "INST_SELECT";
158 case bitc::FUNC_CODE_INST_EXTRACTELT: return "INST_EXTRACTELT";
159 case bitc::FUNC_CODE_INST_INSERTELT: return "INST_INSERTELT";
160 case bitc::FUNC_CODE_INST_SHUFFLEVEC: return "INST_SHUFFLEVEC";
161 case bitc::FUNC_CODE_INST_CMP: return "INST_CMP";
162
163 case bitc::FUNC_CODE_INST_RET: return "INST_RET";
164 case bitc::FUNC_CODE_INST_BR: return "INST_BR";
165 case bitc::FUNC_CODE_INST_SWITCH: return "INST_SWITCH";
166 case bitc::FUNC_CODE_INST_INVOKE: return "INST_INVOKE";
167 case bitc::FUNC_CODE_INST_UNWIND: return "INST_UNWIND";
168 case bitc::FUNC_CODE_INST_UNREACHABLE: return "INST_UNREACHABLE";
169
170 case bitc::FUNC_CODE_INST_MALLOC: return "INST_MALLOC";
171 case bitc::FUNC_CODE_INST_FREE: return "INST_FREE";
172 case bitc::FUNC_CODE_INST_ALLOCA: return "INST_ALLOCA";
173 case bitc::FUNC_CODE_INST_LOAD: return "INST_LOAD";
174 case bitc::FUNC_CODE_INST_STORE: return "INST_STORE";
175 case bitc::FUNC_CODE_INST_CALL: return "INST_CALL";
176 case bitc::FUNC_CODE_INST_VAARG: return "INST_VAARG";
177 }
178 case bitc::TYPE_SYMTAB_BLOCK_ID:
179 switch (CodeID) {
180 default: return 0;
181 case bitc::TST_CODE_ENTRY: return "ENTRY";
182 }
183 case bitc::VALUE_SYMTAB_BLOCK_ID:
184 switch (CodeID) {
185 default: return 0;
186 case bitc::VST_CODE_ENTRY: return "ENTRY";
187 }
188 }
189}
190
Chris Lattner4238d472007-04-29 20:00:02 +0000191
192struct PerBlockIDStats {
193 /// NumInstances - This the number of times this block ID has been seen.
194 unsigned NumInstances;
195
196 /// NumBits - The total size in bits of all of these blocks.
197 uint64_t NumBits;
198
199 /// NumSubBlocks - The total number of blocks these blocks contain.
200 unsigned NumSubBlocks;
201
202 /// NumAbbrevs - The total number of abbreviations.
203 unsigned NumAbbrevs;
204
205 /// NumRecords - The total number of records these blocks contain, and the
206 /// number that are abbreviated.
207 unsigned NumRecords, NumAbbreviatedRecords;
208
209 PerBlockIDStats()
210 : NumInstances(0), NumBits(0),
211 NumSubBlocks(0), NumAbbrevs(0), NumRecords(0), NumAbbreviatedRecords(0) {}
212};
213
214static std::map<unsigned, PerBlockIDStats> BlockIDStats;
215
216
217
Chris Lattner32de6332007-04-29 08:31:14 +0000218/// Error - All bitcode analysis errors go through this function, making this a
219/// good place to breakpoint if debugging.
220static bool Error(const std::string &Err) {
221 std::cerr << Err << "\n";
222 return true;
223}
224
225/// ParseBlock - Read a block, updating statistics, etc.
Chris Lattnerb30c9252007-04-29 21:48:19 +0000226static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) {
Chris Lattner4238d472007-04-29 20:00:02 +0000227 uint64_t BlockBitStart = Stream.GetCurrentBitNo();
Chris Lattner32de6332007-04-29 08:31:14 +0000228 unsigned BlockID = Stream.ReadSubBlockID();
Chris Lattnerb30c9252007-04-29 21:48:19 +0000229
Chris Lattner4238d472007-04-29 20:00:02 +0000230 // Get the statistics for this BlockID.
231 PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
232
233 BlockStats.NumInstances++;
Chris Lattner32de6332007-04-29 08:31:14 +0000234
Chris Lattnerb30c9252007-04-29 21:48:19 +0000235 unsigned NumWords = 0;
236 if (Stream.EnterSubBlock(&NumWords))
Chris Lattner32de6332007-04-29 08:31:14 +0000237 return Error("Malformed block record");
238
Chris Lattnerb30c9252007-04-29 21:48:19 +0000239 std::string Indent(IndentLevel*2, ' ');
240 const char *BlockName = 0;
241 if (Dump) {
242 std::cerr << Indent << "<";
243 if ((BlockName = GetBlockName(BlockID)))
244 std::cerr << BlockName;
245 else
246 std::cerr << "UnknownBlock" << BlockID;
247
248 if (NonSymbolic && BlockName)
249 std::cerr << " BlockID=" << BlockID;
250
251 std::cerr << " NumWords=" << NumWords
252 << " BlockCodeSize=" << Stream.GetAbbrevIDWidth() << ">\n";
253 }
254
Chris Lattner32de6332007-04-29 08:31:14 +0000255 SmallVector<uint64_t, 64> Record;
256
257 // Read all the records for this block.
258 while (1) {
259 if (Stream.AtEndOfStream())
260 return Error("Premature end of bitstream");
261
262 // Read the code for this record.
263 unsigned AbbrevID = Stream.ReadCode();
264 switch (AbbrevID) {
Chris Lattner4238d472007-04-29 20:00:02 +0000265 case bitc::END_BLOCK: {
Chris Lattner32de6332007-04-29 08:31:14 +0000266 if (Stream.ReadBlockEnd())
267 return Error("Error at end of block");
Chris Lattner4238d472007-04-29 20:00:02 +0000268 uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
269 BlockStats.NumBits += BlockBitEnd-BlockBitStart;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000270 if (Dump) {
271 std::cerr << Indent << "</";
272 if (BlockName)
273 std::cerr << BlockName << ">\n";
274 else
275 std::cerr << "UnknownBlock" << BlockID << ">\n";
276 }
Chris Lattner32de6332007-04-29 08:31:14 +0000277 return false;
Chris Lattner4238d472007-04-29 20:00:02 +0000278 }
Chris Lattner32de6332007-04-29 08:31:14 +0000279 case bitc::ENTER_SUBBLOCK:
Chris Lattnerb30c9252007-04-29 21:48:19 +0000280 if (ParseBlock(Stream, IndentLevel+1))
Chris Lattner32de6332007-04-29 08:31:14 +0000281 return true;
Chris Lattner4238d472007-04-29 20:00:02 +0000282 ++BlockStats.NumSubBlocks;
Chris Lattner32de6332007-04-29 08:31:14 +0000283 break;
284 case bitc::DEFINE_ABBREV:
285 Stream.ReadAbbrevRecord();
Chris Lattner4238d472007-04-29 20:00:02 +0000286 ++BlockStats.NumAbbrevs;
Chris Lattner32de6332007-04-29 08:31:14 +0000287 break;
288 default:
Chris Lattner4238d472007-04-29 20:00:02 +0000289 ++BlockStats.NumRecords;
290 if (AbbrevID != bitc::UNABBREV_RECORD)
291 ++BlockStats.NumAbbreviatedRecords;
292
Chris Lattner32de6332007-04-29 08:31:14 +0000293 Record.clear();
294 unsigned Code = Stream.ReadRecord(AbbrevID, Record);
295 // TODO: Compute per-blockid/code stats.
Chris Lattnerb30c9252007-04-29 21:48:19 +0000296
297 if (Dump) {
298 std::cerr << Indent << " <";
299 if (const char *CodeName = GetCodeName(Code, BlockID))
300 std::cerr << CodeName;
301 else
302 std::cerr << "UnknownCode" << Code;
303 if (NonSymbolic && GetCodeName(Code, BlockID))
304 std::cerr << " codeid=" << Code;
305 if (AbbrevID != bitc::UNABBREV_RECORD)
306 std::cerr << " abbrevid=" << AbbrevID;
307
308 for (unsigned i = 0, e = Record.size(); i != e; ++i)
309 std::cerr << " op" << i << "=" << (int64_t)Record[i];
310
311 std::cerr << ">\n";
312 }
313
Chris Lattner32de6332007-04-29 08:31:14 +0000314 break;
315 }
316 }
317}
318
Chris Lattner4238d472007-04-29 20:00:02 +0000319static void PrintSize(double Bits) {
320 std::cerr << Bits << "b/" << Bits/8 << "B/" << Bits/32 << "W";
321}
322
323
Chris Lattner45e0f892007-04-29 08:12:22 +0000324/// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
325static int AnalyzeBitcode() {
326 // Read the input file.
327 MemoryBuffer *Buffer;
328 if (InputFilename == "-")
329 Buffer = MemoryBuffer::getSTDIN();
330 else
331 Buffer = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size());
332
Chris Lattner32de6332007-04-29 08:31:14 +0000333 if (Buffer == 0)
334 return Error("Error reading '" + InputFilename + "'.");
Chris Lattner45e0f892007-04-29 08:12:22 +0000335
Chris Lattner32de6332007-04-29 08:31:14 +0000336 if (Buffer->getBufferSize() & 3)
337 return Error("Bitcode stream should be a multiple of 4 bytes in length");
Chris Lattner45e0f892007-04-29 08:12:22 +0000338
339 unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
340 BitstreamReader Stream(BufPtr, BufPtr+Buffer->getBufferSize());
341
342
343 // Read the stream signature.
344 char Signature[6];
345 Signature[0] = Stream.Read(8);
346 Signature[1] = Stream.Read(8);
347 Signature[2] = Stream.Read(4);
348 Signature[3] = Stream.Read(4);
349 Signature[4] = Stream.Read(4);
350 Signature[5] = Stream.Read(4);
351
Chris Lattner32de6332007-04-29 08:31:14 +0000352 // Autodetect the file contents, if it is one we know.
Chris Lattner45e0f892007-04-29 08:12:22 +0000353 CurStreamType = UnknownBitstream;
354 if (Signature[0] == 'B' && Signature[1] == 'C' &&
355 Signature[2] == 0x0 && Signature[3] == 0xC &&
356 Signature[4] == 0xE && Signature[5] == 0xD)
357 CurStreamType = LLVMIRBitstream;
358
Chris Lattner4238d472007-04-29 20:00:02 +0000359 unsigned NumTopBlocks = 0;
360
Chris Lattner32de6332007-04-29 08:31:14 +0000361 // Parse the top-level structure. We only allow blocks at the top-level.
362 while (!Stream.AtEndOfStream()) {
363 unsigned Code = Stream.ReadCode();
364 if (Code != bitc::ENTER_SUBBLOCK)
365 return Error("Invalid record at top-level");
366
Chris Lattnerb30c9252007-04-29 21:48:19 +0000367 if (ParseBlock(Stream, 0))
Chris Lattner32de6332007-04-29 08:31:14 +0000368 return true;
Chris Lattner4238d472007-04-29 20:00:02 +0000369 ++NumTopBlocks;
Chris Lattner32de6332007-04-29 08:31:14 +0000370 }
371
Chris Lattnerb30c9252007-04-29 21:48:19 +0000372 if (Dump) std::cerr << "\n\n";
373
Chris Lattner32de6332007-04-29 08:31:14 +0000374 // Print a summary of the read file.
Chris Lattner45e0f892007-04-29 08:12:22 +0000375 std::cerr << "Summary of " << InputFilename << ":\n";
Chris Lattner4238d472007-04-29 20:00:02 +0000376 std::cerr << " Total size: ";
377 PrintSize(Buffer->getBufferSize()*8);
378 std::cerr << "\n";
379 std::cerr << " Stream type: ";
Chris Lattner45e0f892007-04-29 08:12:22 +0000380 switch (CurStreamType) {
381 default: assert(0 && "Unknown bitstream type");
382 case UnknownBitstream: std::cerr << "unknown\n"; break;
383 case LLVMIRBitstream: std::cerr << "LLVM IR\n"; break;
384 }
Chris Lattner4238d472007-04-29 20:00:02 +0000385 std::cerr << " # Toplevel Blocks: " << NumTopBlocks << "\n";
386 std::cerr << "\n";
387
388 // Emit per-block stats.
389 std::cerr << "Per-block Summary:\n";
390 for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
391 E = BlockIDStats.end(); I != E; ++I) {
392 std::cerr << " Block ID #" << I->first;
Chris Lattnerb30c9252007-04-29 21:48:19 +0000393 if (const char *BlockName = GetBlockName(I->first))
Chris Lattner4238d472007-04-29 20:00:02 +0000394 std::cerr << " (" << BlockName << ")";
395 std::cerr << ":\n";
396
397 const PerBlockIDStats &Stats = I->second;
398 std::cerr << " Num Instances: " << Stats.NumInstances << "\n";
399 std::cerr << " Total Size: ";
400 PrintSize(Stats.NumBits);
401 std::cerr << "\n";
402 std::cerr << " Average Size: ";
403 PrintSize(Stats.NumBits/(double)Stats.NumInstances);
404 std::cerr << "\n";
405 std::cerr << " Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
406 << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
407 std::cerr << " Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
408 << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
409 std::cerr << " Tot/Avg Records: " << Stats.NumRecords << "/"
410 << Stats.NumRecords/(double)Stats.NumInstances << "\n";
411 std::cerr << " % Abbrev Recs: " << (Stats.NumAbbreviatedRecords/
412 (double)Stats.NumRecords)*100 << "\n";
413 std::cerr << "\n";
414 }
Chris Lattner45e0f892007-04-29 08:12:22 +0000415 return 0;
416}
Reid Spencerdac69c82004-06-07 17:53:43 +0000417
Chris Lattner32de6332007-04-29 08:31:14 +0000418
419//===----------------------------------------------------------------------===//
420// Bytecode specific analysis.
421//===----------------------------------------------------------------------===//
422
Chris Lattnerc30598b2006-12-06 01:18:01 +0000423int main(int argc, char **argv) {
424 llvm_shutdown_obj X; // Call llvm_shutdown() on exit.
Chris Lattner45e0f892007-04-29 08:12:22 +0000425 cl::ParseCommandLineOptions(argc, argv, " llvm-bcanalyzer file analyzer\n");
426
427 sys::PrintStackTraceOnErrorSignal();
428
429 if (Bitcode)
430 return AnalyzeBitcode();
431
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000432 try {
Chris Lattner63db4852007-04-29 05:51:00 +0000433 std::ostream *Out = &std::cout; // Default to printing to stdout...
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000434 std::string ErrorMessage;
435 BytecodeAnalysis bca;
Reid Spencerdac69c82004-06-07 17:53:43 +0000436
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000437 /// Determine what to generate
438 bca.detailedResults = !NoDetails;
439 bca.progressiveVerify = Verify;
Reid Spencer96684ef2004-06-08 05:56:58 +0000440
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000441 /// Analyze the bytecode file
Chris Lattnerf2e292c2007-02-07 21:41:02 +0000442 Module* M = AnalyzeBytecodeFile(InputFilename, bca,
443 Compressor::decompressToNewBuffer,
444 &ErrorMessage, (Dump?Out:0));
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000445
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000446 // All that bcanalyzer does is write the gathered statistics to the output
447 PrintBytecodeAnalysis(bca,*Out);
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000448
Chris Lattner45e0f892007-04-29 08:12:22 +0000449 if (M && Verify) {
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000450 std::string verificationMsg;
Chris Lattner05ac92c2006-07-06 18:02:27 +0000451 if (verifyModule(*M, ReturnStatusAction, &verificationMsg))
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000452 std::cerr << "Final Verification Message: " << verificationMsg << "\n";
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000453 }
Reid Spencer86a9a7a2004-06-29 23:34:27 +0000454
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000455 if (Out != &std::cout) {
456 ((std::ofstream*)Out)->close();
457 delete Out;
458 }
459 return 0;
460 } catch (const std::string& msg) {
461 std::cerr << argv[0] << ": " << msg << "\n";
462 } catch (...) {
463 std::cerr << argv[0] << ": Unexpected unknown exception occurred.\n";
Reid Spencerdac69c82004-06-07 17:53:43 +0000464 }
Reid Spencer1ef8bda2004-12-30 05:36:08 +0000465 return 1;
Reid Spencerdac69c82004-06-07 17:53:43 +0000466}