| 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 10 matching lines...) Expand all Loading... |
| 21 // produces on std::out a summary of the bitcode file that shows various | 21 // produces on std::out a summary of the bitcode file that shows various |
| 22 // statistics about the contents of the file. By default this information is | 22 // statistics about the contents of the file. By default this information is |
| 23 // detailed and contains information about individual bitcode blocks and the | 23 // detailed and contains information about individual bitcode blocks and the |
| 24 // functions in the module. | 24 // functions in the module. |
| 25 // The tool is also able to print a bitcode file in a straight forward text | 25 // The tool is also able to print a bitcode file in a straight forward text |
| 26 // format that shows the containment and relationships of the information in | 26 // format that shows the containment and relationships of the information in |
| 27 // the bitcode file (-dump option). | 27 // the bitcode file (-dump option). |
| 28 // | 28 // |
| 29 //===----------------------------------------------------------------------===// | 29 //===----------------------------------------------------------------------===// |
| 30 | 30 |
| 31 #define DEBUG_TYPE "pnacl-bcanalyzer" |
| 32 |
| 31 #include "llvm/ADT/OwningPtr.h" | 33 #include "llvm/ADT/OwningPtr.h" |
| 32 #include "llvm/Analysis/Verifier.h" | 34 #include "llvm/Analysis/Verifier.h" |
| 33 #include "llvm/Bitcode/NaCl/NaClBitstreamReader.h" | 35 #include "llvm/Bitcode/NaCl/NaClBitstreamReader.h" |
| 34 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h" | 36 #include "llvm/Bitcode/NaCl/NaClLLVMBitCodes.h" |
| 35 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" | 37 #include "llvm/Bitcode/NaCl/NaClReaderWriter.h" |
| 36 #include "llvm/Support/CommandLine.h" | 38 #include "llvm/Support/CommandLine.h" |
| 39 #include "llvm/Support/Debug.h" |
| 37 #include "llvm/Support/Format.h" | 40 #include "llvm/Support/Format.h" |
| 38 #include "llvm/Support/ManagedStatic.h" | 41 #include "llvm/Support/ManagedStatic.h" |
| 39 #include "llvm/Support/MemoryBuffer.h" | 42 #include "llvm/Support/MemoryBuffer.h" |
| 40 #include "llvm/Support/PrettyStackTrace.h" | 43 #include "llvm/Support/PrettyStackTrace.h" |
| 41 #include "llvm/Support/Signals.h" | 44 #include "llvm/Support/Signals.h" |
| 42 #include "llvm/Support/raw_ostream.h" | 45 #include "llvm/Support/raw_ostream.h" |
| 43 #include "llvm/Support/system_error.h" | 46 #include "llvm/Support/system_error.h" |
| 44 #include <algorithm> | 47 #include <algorithm> |
| 45 #include <map> | 48 #include <map> |
| 46 using namespace llvm; | 49 using namespace llvm; |
| (...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 318 /// good place to breakpoint if debugging. | 321 /// good place to breakpoint if debugging. |
| 319 static bool Error(const std::string &Err) { | 322 static bool Error(const std::string &Err) { |
| 320 errs() << Err << "\n"; | 323 errs() << Err << "\n"; |
| 321 return true; | 324 return true; |
| 322 } | 325 } |
| 323 | 326 |
| 324 /// ParseBlock - Read a block, updating statistics, etc. | 327 /// ParseBlock - Read a block, updating statistics, etc. |
| 325 static bool ParseBlock(NaClBitstreamCursor &Stream, unsigned BlockID, | 328 static bool ParseBlock(NaClBitstreamCursor &Stream, unsigned BlockID, |
| 326 unsigned IndentLevel) { | 329 unsigned IndentLevel) { |
| 327 std::string Indent(IndentLevel*2, ' '); | 330 std::string Indent(IndentLevel*2, ' '); |
| 331 DEBUG(dbgs() << Indent << "-> ParseBlock(" << BlockID << ")\n"); |
| 328 uint64_t BlockBitStart = Stream.GetCurrentBitNo(); | 332 uint64_t BlockBitStart = Stream.GetCurrentBitNo(); |
| 329 | 333 |
| 330 // Get the statistics for this BlockID. | 334 // Get the statistics for this BlockID. |
| 331 PerBlockIDStats &BlockStats = BlockIDStats[BlockID]; | 335 PerBlockIDStats &BlockStats = BlockIDStats[BlockID]; |
| 332 | 336 |
| 333 BlockStats.NumInstances++; | 337 BlockStats.NumInstances++; |
| 334 | 338 |
| 335 // BLOCKINFO is a special part of the stream. | 339 // BLOCKINFO is a special part of the stream. |
| 336 if (BlockID == naclbitc::BLOCKINFO_BLOCK_ID) { | 340 if (BlockID == naclbitc::BLOCKINFO_BLOCK_ID) { |
| 337 if (Dump) outs() << Indent << "<BLOCKINFO_BLOCK/>\n"; | 341 if (Dump) outs() << Indent << "<BLOCKINFO_BLOCK/>\n"; |
| 338 if (Stream.ReadBlockInfoBlock()) | 342 if (Stream.ReadBlockInfoBlock()) |
| 339 return Error("Malformed BlockInfoBlock"); | 343 return Error("Malformed BlockInfoBlock"); |
| 340 uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); | 344 uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); |
| 341 BlockStats.NumBits += BlockBitEnd-BlockBitStart; | 345 BlockStats.NumBits += BlockBitEnd-BlockBitStart; |
| 346 DEBUG(dbgs() << Indent << "<- ParseBlock\n"); |
| 342 return false; | 347 return false; |
| 343 } | 348 } |
| 344 | 349 |
| 345 unsigned NumWords = 0; | 350 unsigned NumWords = 0; |
| 346 if (Stream.EnterSubBlock(BlockID, &NumWords)) | 351 if (Stream.EnterSubBlock(BlockID, &NumWords)) |
| 347 return Error("Malformed block record"); | 352 return Error("Malformed block record"); |
| 348 | 353 |
| 349 const char *BlockName = 0; | 354 const char *BlockName = 0; |
| 350 if (Dump) { | 355 if (Dump) { |
| 351 outs() << Indent << "<"; | 356 outs() << Indent << "<"; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 379 case NaClBitstreamEntry::EndBlock: { | 384 case NaClBitstreamEntry::EndBlock: { |
| 380 uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); | 385 uint64_t BlockBitEnd = Stream.GetCurrentBitNo(); |
| 381 BlockStats.NumBits += BlockBitEnd-BlockBitStart; | 386 BlockStats.NumBits += BlockBitEnd-BlockBitStart; |
| 382 if (Dump) { | 387 if (Dump) { |
| 383 outs() << Indent << "</"; | 388 outs() << Indent << "</"; |
| 384 if (BlockName) | 389 if (BlockName) |
| 385 outs() << BlockName << ">\n"; | 390 outs() << BlockName << ">\n"; |
| 386 else | 391 else |
| 387 outs() << "UnknownBlock" << BlockID << ">\n"; | 392 outs() << "UnknownBlock" << BlockID << ">\n"; |
| 388 } | 393 } |
| 394 DEBUG(dbgs() << Indent << "<- ParseBlock\n"); |
| 389 return false; | 395 return false; |
| 390 } | 396 } |
| 391 | 397 |
| 392 case NaClBitstreamEntry::SubBlock: { | 398 case NaClBitstreamEntry::SubBlock: { |
| 393 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo(); | 399 uint64_t SubBlockBitStart = Stream.GetCurrentBitNo(); |
| 394 if (ParseBlock(Stream, Entry.ID, IndentLevel+1)) | 400 if (ParseBlock(Stream, Entry.ID, IndentLevel+1)) |
| 395 return true; | 401 return true; |
| 396 ++BlockStats.NumSubBlocks; | 402 ++BlockStats.NumSubBlocks; |
| 397 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo(); | 403 uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo(); |
| 398 | 404 |
| (...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)); | 477 outs() << format("%.2f/%.2fB/%luW", Bits, Bits/8,(unsigned long)(Bits/32)); |
| 472 } | 478 } |
| 473 static void PrintSize(uint64_t Bits) { | 479 static void PrintSize(uint64_t Bits) { |
| 474 outs() << format("%lub/%.2fB/%luW", (unsigned long)Bits, | 480 outs() << format("%lub/%.2fB/%luW", (unsigned long)Bits, |
| 475 (double)Bits/8, (unsigned long)(Bits/32)); | 481 (double)Bits/8, (unsigned long)(Bits/32)); |
| 476 } | 482 } |
| 477 | 483 |
| 478 | 484 |
| 479 /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename. | 485 /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename. |
| 480 static int AnalyzeBitcode() { | 486 static int AnalyzeBitcode() { |
| 487 DEBUG(dbgs() << "-> AnalyzeBitcode\n"); |
| 481 // Read the input file. | 488 // Read the input file. |
| 482 OwningPtr<MemoryBuffer> MemBuf; | 489 OwningPtr<MemoryBuffer> MemBuf; |
| 483 | 490 |
| 484 if (error_code ec = | 491 if (error_code ec = |
| 485 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), MemBuf)) | 492 MemoryBuffer::getFileOrSTDIN(InputFilename.c_str(), MemBuf)) |
| 486 return Error("Error reading '" + InputFilename + "': " + ec.message()); | 493 return Error("Error reading '" + InputFilename + "': " + ec.message()); |
| 487 | 494 |
| 488 if (MemBuf->getBufferSize() & 3) | 495 if (MemBuf->getBufferSize() & 3) |
| 489 return Error("Bitcode stream should be a multiple of 4 bytes in length"); | 496 return Error("Bitcode stream should be a multiple of 4 bytes in length"); |
| 490 | 497 |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 613 if (const char *CodeName = | 620 if (const char *CodeName = |
| 614 GetCodeName(FreqPairs[i].second, I->first, StreamFile)) | 621 GetCodeName(FreqPairs[i].second, I->first, StreamFile)) |
| 615 outs() << CodeName << "\n"; | 622 outs() << CodeName << "\n"; |
| 616 else | 623 else |
| 617 outs() << "UnknownCode" << FreqPairs[i].second << "\n"; | 624 outs() << "UnknownCode" << FreqPairs[i].second << "\n"; |
| 618 } | 625 } |
| 619 outs() << "\n"; | 626 outs() << "\n"; |
| 620 | 627 |
| 621 } | 628 } |
| 622 } | 629 } |
| 630 DEBUG(dbgs() << "<- AnalyzeBitcode\n"); |
| 623 return 0; | 631 return 0; |
| 624 } | 632 } |
| 625 | 633 |
| 626 | 634 |
| 627 int main(int argc, char **argv) { | 635 int main(int argc, char **argv) { |
| 628 // Print a stack trace if we signal out. | 636 // Print a stack trace if we signal out. |
| 629 sys::PrintStackTraceOnErrorSignal(); | 637 sys::PrintStackTraceOnErrorSignal(); |
| 630 PrettyStackTraceProgram X(argc, argv); | 638 PrettyStackTraceProgram X(argc, argv); |
| 631 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. | 639 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
| 632 cl::ParseCommandLineOptions(argc, argv, "pnacl-bcanalyzer file analyzer\n"); | 640 cl::ParseCommandLineOptions(argc, argv, "pnacl-bcanalyzer file analyzer\n"); |
| 633 | 641 |
| 634 return AnalyzeBitcode(); | 642 return AnalyzeBitcode(); |
| 635 } | 643 } |
| OLD | NEW |