OLD | NEW |
---|---|
1 //===-- pnacl-bcanalyzer.cpp - Bitcode Analyzer -------------------------===// | 1 //===-- pnacl-bcanalyzer.cpp - Bitcode Analyzer -------------------------===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // This tool may be invoked in the following manner: | 10 // This tool may be invoked in the following manner: |
(...skipping 27 matching lines...) Expand all Loading... | |
38 #include "llvm/Support/ManagedStatic.h" | 38 #include "llvm/Support/ManagedStatic.h" |
39 #include "llvm/Support/MemoryBuffer.h" | 39 #include "llvm/Support/MemoryBuffer.h" |
40 #include "llvm/Support/PrettyStackTrace.h" | 40 #include "llvm/Support/PrettyStackTrace.h" |
41 #include "llvm/Support/Signals.h" | 41 #include "llvm/Support/Signals.h" |
42 #include "llvm/Support/raw_ostream.h" | 42 #include "llvm/Support/raw_ostream.h" |
43 #include "llvm/Support/system_error.h" | 43 #include "llvm/Support/system_error.h" |
44 #include <algorithm> | 44 #include <algorithm> |
45 #include <map> | 45 #include <map> |
46 using namespace llvm; | 46 using namespace llvm; |
47 | 47 |
48 /* To turn on debugging for this file, change value of NACL_DEBUGGING to 1. | |
49 */ | |
50 #define NACL_DEBUGGING 0 | |
51 #include "llvm/Bitcode/NaCl/NaClDebugging.h" | |
52 | |
48 static cl::opt<std::string> | 53 static cl::opt<std::string> |
49 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); | 54 InputFilename(cl::Positional, cl::desc("<input bitcode>"), cl::init("-")); |
50 | 55 |
51 static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace")); | 56 static cl::opt<bool> Dump("dump", cl::desc("Dump low level bitcode trace")); |
52 | 57 |
53 //===----------------------------------------------------------------------===// | 58 //===----------------------------------------------------------------------===// |
54 // Bitcode specific analysis. | 59 // Bitcode specific analysis. |
55 //===----------------------------------------------------------------------===// | 60 //===----------------------------------------------------------------------===// |
56 | 61 |
57 static cl::opt<bool> NoHistogram("disable-histogram", | 62 static cl::opt<bool> NoHistogram("disable-histogram", |
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
318 /// good place to breakpoint if debugging. | 323 /// good place to breakpoint if debugging. |
319 static bool Error(const std::string &Err) { | 324 static bool Error(const std::string &Err) { |
320 errs() << Err << "\n"; | 325 errs() << Err << "\n"; |
321 return true; | 326 return true; |
322 } | 327 } |
323 | 328 |
324 /// ParseBlock - Read a block, updating statistics, etc. | 329 /// ParseBlock - Read a block, updating statistics, etc. |
325 static bool ParseBlock(NaClBitstreamCursor &Stream, unsigned BlockID, | 330 static bool ParseBlock(NaClBitstreamCursor &Stream, unsigned BlockID, |
326 unsigned IndentLevel) { | 331 unsigned IndentLevel) { |
327 std::string Indent(IndentLevel*2, ' '); | 332 std::string Indent(IndentLevel*2, ' '); |
333 NACL_DEBUG(outs() << Indent << "-> ParseBlock(" << BlockID << ")\n"); | |
jvoung (off chromium)
2013/05/14 23:47:14
Not sure you want to use "outs()" since that will
Karl
2013/05/17 20:52:18
Fixing to use llvm debug.h
| |
328 uint64_t BlockBitStart = Stream.GetCurrentBitNo(); | 334 uint64_t BlockBitStart = Stream.GetCurrentBitNo(); |
329 | 335 |
330 // Get the statistics for this BlockID. | 336 // Get the statistics for this BlockID. |
331 PerBlockIDStats &BlockStats = BlockIDStats[BlockID]; | 337 PerBlockIDStats &BlockStats = BlockIDStats[BlockID]; |
332 | 338 |
333 BlockStats.NumInstances++; | 339 BlockStats.NumInstances++; |
334 | 340 |
335 // BLOCKINFO is a special part of the stream. | 341 // BLOCKINFO is a special part of the stream. |
336 if (BlockID == naclbitc::BLOCKINFO_BLOCK_ID) { | 342 if (BlockID == naclbitc::BLOCKINFO_BLOCK_ID) { |
337 if (Dump) outs() << Indent << "<BLOCKINFO_BLOCK/>\n"; | 343 if (Dump) outs() << Indent << "<BLOCKINFO_BLOCK/>\n"; |
338 if (Stream.ReadBlockInfoBlock()) | 344 if (Stream.ReadBlockInfoBlock()) |
339 return Error("Malformed BlockInfoBlock"); | 345 return Error("Malformed BlockInfoBlock"); |
340 uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); | 346 uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); |
341 BlockStats.NumBits += BlockBitEnd-BlockBitStart; | 347 BlockStats.NumBits += BlockBitEnd-BlockBitStart; |
348 NACL_DEBUG(outs() << Indent << "<- ParseBlock\n"); | |
342 return false; | 349 return false; |
343 } | 350 } |
344 | 351 |
345 unsigned NumWords = 0; | 352 unsigned NumWords = 0; |
346 if (Stream.EnterSubBlock(BlockID, &NumWords)) | 353 if (Stream.EnterSubBlock(BlockID, &NumWords)) |
347 return Error("Malformed block record"); | 354 return Error("Malformed block record"); |
348 | 355 |
349 const char *BlockName = 0; | 356 const char *BlockName = 0; |
350 if (Dump) { | 357 if (Dump) { |
351 outs() << Indent << "<"; | 358 outs() << Indent << "<"; |
(...skipping 27 matching lines...) Expand all Loading... | |
379 case NaClBitstreamEntry::EndBlock: { | 386 case NaClBitstreamEntry::EndBlock: { |
380 uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); | 387 uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); |
381 BlockStats.NumBits += BlockBitEnd-BlockBitStart; | 388 BlockStats.NumBits += BlockBitEnd-BlockBitStart; |
382 if (Dump) { | 389 if (Dump) { |
383 outs() << Indent << "</"; | 390 outs() << Indent << "</"; |
384 if (BlockName) | 391 if (BlockName) |
385 outs() << BlockName << ">\n"; | 392 outs() << BlockName << ">\n"; |
386 else | 393 else |
387 outs() << "UnknownBlock" << BlockID << ">\n"; | 394 outs() << "UnknownBlock" << BlockID << ">\n"; |
388 } | 395 } |
396 NACL_DEBUG(outs() << Indent << "<- ParseBlock\n"); | |
389 return false; | 397 return false; |
390 } | 398 } |
391 | 399 |
392 case NaClBitstreamEntry::SubBlock: { | 400 case NaClBitstreamEntry::SubBlock: { |
393 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo(); | 401 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo(); |
394 if (ParseBlock(Stream, Entry.ID, IndentLevel+1)) | 402 if (ParseBlock(Stream, Entry.ID, IndentLevel+1)) |
395 return true; | 403 return true; |
396 ++BlockStats.NumSubBlocks; | 404 ++BlockStats.NumSubBlocks; |
397 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo(); | 405 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo(); |
398 | 406 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
471 outs() << format("%.2f/%.2fB/%luW", Bits, Bits/8,(unsigned long)(Bits/32)); | 479 outs() << format("%.2f/%.2fB/%luW", Bits, Bits/8,(unsigned long)(Bits/32)); |
472 } | 480 } |
473 static void PrintSize(uint64_t Bits) { | 481 static void PrintSize(uint64_t Bits) { |
474 outs() << format("%lub/%.2fB/%luW", (unsigned long)Bits, | 482 outs() << format("%lub/%.2fB/%luW", (unsigned long)Bits, |
475 (double)Bits/8, (unsigned long)(Bits/32)); | 483 (double)Bits/8, (unsigned long)(Bits/32)); |
476 } | 484 } |
477 | 485 |
478 | 486 |
479 /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename. | 487 /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename. |
480 static int AnalyzeBitcode() { | 488 static int AnalyzeBitcode() { |
489 NACL_DEBUG(outs() << "-> AnalyzeBitcode\n"); | |
481 // Read the input file. | 490 // Read the input file. |
482 OwningPtr<MemoryBuffer> MemBuf; | 491 OwningPtr<MemoryBuffer> MemBuf; |
483 | 492 |
484 if (error_code ec = | 493 if (error_code ec = |
485 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), MemBuf)) | 494 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), MemBuf)) |
486 return Error("Error reading '" + InputFilename + "': " + ec.message()); | 495 return Error("Error reading '" + InputFilename + "': " + ec.message()); |
487 | 496 |
488 if (MemBuf->getBufferSize() & 3) | 497 if (MemBuf->getBufferSize() & 3) |
489 return Error("Bitcode stream should be a multiple of 4 bytes in length"); | 498 return Error("Bitcode stream should be a multiple of 4 bytes in length"); |
490 | 499 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
613 if (const char *CodeName = | 622 if (const char *CodeName = |
614 GetCodeName(FreqPairs[i].second, I->first, StreamFile)) | 623 GetCodeName(FreqPairs[i].second, I->first, StreamFile)) |
615 outs() << CodeName << "\n"; | 624 outs() << CodeName << "\n"; |
616 else | 625 else |
617 outs() << "UnknownCode" << FreqPairs[i].second << "\n"; | 626 outs() << "UnknownCode" << FreqPairs[i].second << "\n"; |
618 } | 627 } |
619 outs() << "\n"; | 628 outs() << "\n"; |
620 | 629 |
621 } | 630 } |
622 } | 631 } |
632 NACL_DEBUG(outs() << "<- AnalyzeBitcode\n"); | |
623 return 0; | 633 return 0; |
624 } | 634 } |
625 | 635 |
626 | 636 |
627 int main(int argc, char **argv) { | 637 int main(int argc, char **argv) { |
628 // Print a stack trace if we signal out. | 638 // Print a stack trace if we signal out. |
629 sys::PrintStackTraceOnErrorSignal(); | 639 sys::PrintStackTraceOnErrorSignal(); |
630 PrettyStackTraceProgram X(argc, argv); | 640 PrettyStackTraceProgram X(argc, argv); |
631 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. | 641 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
632 cl::ParseCommandLineOptions(argc, argv, "pnacl-bcanalyzer file analyzer\n"); | 642 cl::ParseCommandLineOptions(argc, argv, "pnacl-bcanalyzer file analyzer\n"); |
633 | 643 |
634 return AnalyzeBitcode(); | 644 return AnalyzeBitcode(); |
635 } | 645 } |
OLD | NEW |