Handle NamedMDNode.
[oota-llvm.git] / tools / llvm-bcanalyzer / llvm-bcanalyzer.cpp
index c6518a24f4385c68e032be51910a5e437d6fb9c6..db9b1312f01690b69964210a11a30b5aea70cffa 100644 (file)
 #include "llvm/Analysis/Verifier.h"
 #include "llvm/Bitcode/BitstreamReader.h"
 #include "llvm/Bitcode/LLVMBitCodes.h"
+#include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/System/Signals.h"
 #include <map>
-#include <fstream>
-#include <iostream>
 #include <algorithm>
 using namespace llvm;
 
@@ -70,7 +70,8 @@ static enum {
 
 /// GetBlockName - Return a symbolic block name if known, otherwise return
 /// null.
-static const char *GetBlockName(unsigned BlockID) {
+static const char *GetBlockName(unsigned BlockID,
+                                const BitstreamReader &StreamFile) {
   // Standard blocks for all bitcode files.
   if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
     if (BlockID == bitc::BLOCKINFO_BLOCK_ID)
@@ -78,6 +79,14 @@ static const char *GetBlockName(unsigned BlockID) {
     return 0;
   }
   
+  // Check to see if we have a blockinfo record for this block, with a name.
+  if (const BitstreamReader::BlockInfo *Info =
+        StreamFile.getBlockInfo(BlockID)) {
+    if (!Info->Name.empty())
+      return Info->Name.c_str();
+  }
+  
+  
   if (CurStreamType != LLVMIRBitstream) return 0;
   
   switch (BlockID) {
@@ -89,23 +98,36 @@ static const char *GetBlockName(unsigned BlockID) {
   case bitc::FUNCTION_BLOCK_ID:     return "FUNCTION_BLOCK";
   case bitc::TYPE_SYMTAB_BLOCK_ID:  return "TYPE_SYMTAB";
   case bitc::VALUE_SYMTAB_BLOCK_ID: return "VALUE_SYMTAB";
+  case bitc::METADATA_BLOCK_ID:     return "METADATA_BLOCK";
   }
 }
 
 /// GetCodeName - Return a symbolic code name if known, otherwise return
 /// null.
-static const char *GetCodeName(unsigned CodeID, unsigned BlockID) {
+static const char *GetCodeName(unsigned CodeID, unsigned BlockID,
+                               const BitstreamReader &StreamFile) {
   // Standard blocks for all bitcode files.
   if (BlockID < bitc::FIRST_APPLICATION_BLOCKID) {
     if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
       switch (CodeID) {
       default: return 0;
-      case bitc::MODULE_CODE_VERSION:     return "VERSION";
+      case bitc::BLOCKINFO_CODE_SETBID:        return "SETBID";
+      case bitc::BLOCKINFO_CODE_BLOCKNAME:     return "BLOCKNAME";
+      case bitc::BLOCKINFO_CODE_SETRECORDNAME: return "SETRECORDNAME";
       }
     }
     return 0;
   }
   
+  // Check to see if we have a blockinfo record for this record, with a name.
+  if (const BitstreamReader::BlockInfo *Info =
+        StreamFile.getBlockInfo(BlockID)) {
+    for (unsigned i = 0, e = Info->RecordNames.size(); i != e; ++i)
+      if (Info->RecordNames[i].first == CodeID)
+        return Info->RecordNames[i].second.c_str();
+  }
+  
+  
   if (CurStreamType != LLVMIRBitstream) return 0;
   
   switch (BlockID) {
@@ -123,6 +145,7 @@ static const char *GetCodeName(unsigned CodeID, unsigned BlockID) {
     case bitc::MODULE_CODE_FUNCTION:    return "FUNCTION";
     case bitc::MODULE_CODE_ALIAS:       return "ALIAS";
     case bitc::MODULE_CODE_PURGEVALS:   return "PURGEVALS";
+    case bitc::MODULE_CODE_GCNAME:      return "GCNAME";
     }
   case bitc::PARAMATTR_BLOCK_ID:
     switch (CodeID) {
@@ -132,18 +155,22 @@ static const char *GetCodeName(unsigned CodeID, unsigned BlockID) {
   case bitc::TYPE_BLOCK_ID:
     switch (CodeID) {
     default: return 0;
-    case bitc::TYPE_CODE_NUMENTRY: return "NUMENTRY";
-    case bitc::TYPE_CODE_VOID:     return "VOID";
-    case bitc::TYPE_CODE_FLOAT:    return "FLOAT";
-    case bitc::TYPE_CODE_DOUBLE:   return "DOUBLE";
-    case bitc::TYPE_CODE_LABEL:    return "LABEL";
-    case bitc::TYPE_CODE_OPAQUE:   return "OPAQUE";
-    case bitc::TYPE_CODE_INTEGER:  return "INTEGER";
-    case bitc::TYPE_CODE_POINTER:  return "POINTER";
-    case bitc::TYPE_CODE_FUNCTION: return "FUNCTION";
-    case bitc::TYPE_CODE_STRUCT:   return "STRUCT";
-    case bitc::TYPE_CODE_ARRAY:    return "ARRAY";
-    case bitc::TYPE_CODE_VECTOR:   return "VECTOR";
+    case bitc::TYPE_CODE_NUMENTRY:  return "NUMENTRY";
+    case bitc::TYPE_CODE_VOID:      return "VOID";
+    case bitc::TYPE_CODE_FLOAT:     return "FLOAT";
+    case bitc::TYPE_CODE_DOUBLE:    return "DOUBLE";
+    case bitc::TYPE_CODE_LABEL:     return "LABEL";
+    case bitc::TYPE_CODE_OPAQUE:    return "OPAQUE";
+    case bitc::TYPE_CODE_INTEGER:   return "INTEGER";
+    case bitc::TYPE_CODE_POINTER:   return "POINTER";
+    case bitc::TYPE_CODE_FUNCTION:  return "FUNCTION";
+    case bitc::TYPE_CODE_STRUCT:    return "STRUCT";
+    case bitc::TYPE_CODE_ARRAY:     return "ARRAY";
+    case bitc::TYPE_CODE_VECTOR:    return "VECTOR";
+    case bitc::TYPE_CODE_X86_FP80:  return "X86_FP80";
+    case bitc::TYPE_CODE_FP128:     return "FP128";
+    case bitc::TYPE_CODE_PPC_FP128: return "PPC_FP128";
+    case bitc::TYPE_CODE_METADATA:  return "METADATA";
     }
     
   case bitc::CONSTANTS_BLOCK_ID:
@@ -167,12 +194,12 @@ static const char *GetCodeName(unsigned CodeID, unsigned BlockID) {
     case bitc::CST_CODE_CE_SHUFFLEVEC: return "CE_SHUFFLEVEC";
     case bitc::CST_CODE_CE_CMP:        return "CE_CMP";
     case bitc::CST_CODE_INLINEASM:     return "INLINEASM";
+    case bitc::CST_CODE_CE_SHUFVEC_EX: return "CE_SHUFVEC_EX";
     }        
   case bitc::FUNCTION_BLOCK_ID:
     switch (CodeID) {
     default: return 0;
     case bitc::FUNC_CODE_DECLAREBLOCKS: return "DECLAREBLOCKS";
-    case bitc::FUNC_CODE_INST_BB_UNWINDDEST: return "UNWINDDEST";
     
     case bitc::FUNC_CODE_INST_BINOP:       return "INST_BINOP";
     case bitc::FUNC_CODE_INST_CAST:        return "INST_CAST";
@@ -200,6 +227,10 @@ static const char *GetCodeName(unsigned CodeID, unsigned BlockID) {
     case bitc::FUNC_CODE_INST_VAARG:       return "INST_VAARG";
     case bitc::FUNC_CODE_INST_STORE2:      return "INST_STORE2";
     case bitc::FUNC_CODE_INST_GETRESULT:   return "INST_GETRESULT";
+    case bitc::FUNC_CODE_INST_EXTRACTVAL:  return "INST_EXTRACTVAL";
+    case bitc::FUNC_CODE_INST_INSERTVAL:   return "INST_INSERTVAL";
+    case bitc::FUNC_CODE_INST_CMP2:        return "INST_CMP2";
+    case bitc::FUNC_CODE_INST_VSELECT:     return "INST_VSELECT";
     }
   case bitc::TYPE_SYMTAB_BLOCK_ID:
     switch (CodeID) {
@@ -212,9 +243,24 @@ static const char *GetCodeName(unsigned CodeID, unsigned BlockID) {
     case bitc::VST_CODE_ENTRY: return "ENTRY";
     case bitc::VST_CODE_BBENTRY: return "BBENTRY";
     }
+  case bitc::METADATA_BLOCK_ID:
+    switch(CodeID) {
+    default:return 0;
+    case bitc::METADATA_STRING:      return "MDSTRING";
+    case bitc::METADATA_NODE:        return "MDNODE";
+    case bitc::METADATA_NAME:        return "METADATA_NAME";
+    case bitc::METADATA_NAMED_NODE:  return "NAMEDMDNODE";
+    }
   }
 }
 
+struct PerRecordStats {
+  unsigned NumInstances;
+  unsigned NumAbbrev;
+  uint64_t TotalBits;
+  
+  PerRecordStats() : NumInstances(0), NumAbbrev(0), TotalBits(0) {}
+};
 
 struct PerBlockIDStats {
   /// NumInstances - This the number of times this block ID has been seen.
@@ -234,7 +280,7 @@ struct PerBlockIDStats {
   unsigned NumRecords, NumAbbreviatedRecords;
   
   /// CodeFreq - Keep track of the number of times we see each code.
-  std::vector<unsigned> CodeFreq;
+  std::vector<PerRecordStats> CodeFreq;
   
   PerBlockIDStats()
     : NumInstances(0), NumBits(0),
@@ -248,12 +294,12 @@ static std::map<unsigned, PerBlockIDStats> BlockIDStats;
 /// Error - All bitcode analysis errors go through this function, making this a
 /// good place to breakpoint if debugging.
 static bool Error(const std::string &Err) {
-  std::cerr << Err << "\n";
+  errs() << Err << "\n";
   return true;
 }
 
 /// ParseBlock - Read a block, updating statistics, etc.
-static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) {
+static bool ParseBlock(BitstreamCursor &Stream, unsigned IndentLevel) {
   std::string Indent(IndentLevel*2, ' ');
   uint64_t BlockBitStart = Stream.GetCurrentBitNo();
   unsigned BlockID = Stream.ReadSubBlockID();
@@ -265,7 +311,7 @@ static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) {
   
   // BLOCKINFO is a special part of the stream.
   if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
-    if (Dump) std::cerr << Indent << "<BLOCKINFO_BLOCK/>\n";
+    if (Dump) errs() << Indent << "<BLOCKINFO_BLOCK/>\n";
     if (Stream.ReadBlockInfoBlock())
       return Error("Malformed BlockInfoBlock");
     uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
@@ -279,17 +325,17 @@ static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) {
 
   const char *BlockName = 0;
   if (Dump) {
-    std::cerr << Indent << "<";
-    if ((BlockName = GetBlockName(BlockID)))
-      std::cerr << BlockName;
+    errs() << Indent << "<";
+    if ((BlockName = GetBlockName(BlockID, *Stream.getBitStreamReader())))
+      errs() << BlockName;
     else
-      std::cerr << "UnknownBlock" << BlockID;
+      errs() << "UnknownBlock" << BlockID;
     
     if (NonSymbolic && BlockName)
-      std::cerr << " BlockID=" << BlockID;
+      errs() << " BlockID=" << BlockID;
     
-    std::cerr << " NumWords=" << NumWords
-              << " BlockCodeSize=" << Stream.GetAbbrevIDWidth() << ">\n";
+    errs() << " NumWords=" << NumWords
+           << " BlockCodeSize=" << Stream.GetAbbrevIDWidth() << ">\n";
   }
   
   SmallVector<uint64_t, 64> Record;
@@ -299,6 +345,8 @@ static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) {
     if (Stream.AtEndOfStream())
       return Error("Premature end of bitstream");
 
+    uint64_t RecordStartBit = Stream.GetCurrentBitNo();
+    
     // Read the code for this record.
     unsigned AbbrevID = Stream.ReadCode();
     switch (AbbrevID) {
@@ -308,11 +356,11 @@ static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) {
       uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
       BlockStats.NumBits += BlockBitEnd-BlockBitStart;
       if (Dump) {
-        std::cerr << Indent << "</";
+        errs() << Indent << "</";
         if (BlockName)
-          std::cerr << BlockName << ">\n";
+          errs() << BlockName << ">\n";
         else
-          std::cerr << "UnknownBlock" << BlockID << ">\n";
+          errs() << "UnknownBlock" << BlockID << ">\n";
       }
       return false;
     } 
@@ -332,33 +380,61 @@ static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) {
       ++BlockStats.NumAbbrevs;
       break;
     default:
+      Record.clear();
+
       ++BlockStats.NumRecords;
       if (AbbrevID != bitc::UNABBREV_RECORD)
         ++BlockStats.NumAbbreviatedRecords;
-      
-      Record.clear();
-      unsigned Code = Stream.ReadRecord(AbbrevID, Record);
+        
+      const char *BlobStart = 0;
+      unsigned BlobLen = 0;
+      unsigned Code = Stream.ReadRecord(AbbrevID, Record, BlobStart, BlobLen);
 
+        
+        
       // Increment the # occurrences of this code.
       if (BlockStats.CodeFreq.size() <= Code)
         BlockStats.CodeFreq.resize(Code+1);
-      BlockStats.CodeFreq[Code]++;
-      
+      BlockStats.CodeFreq[Code].NumInstances++;
+      BlockStats.CodeFreq[Code].TotalBits +=
+        Stream.GetCurrentBitNo()-RecordStartBit;
+      if (AbbrevID != bitc::UNABBREV_RECORD)
+        BlockStats.CodeFreq[Code].NumAbbrev++;
+        
       if (Dump) {
-        std::cerr << Indent << "  <";
-        if (const char *CodeName = GetCodeName(Code, BlockID))
-          std::cerr << CodeName;
+        errs() << Indent << "  <";
+        if (const char *CodeName =
+              GetCodeName(Code, BlockID, *Stream.getBitStreamReader()))
+          errs() << CodeName;
         else
-          std::cerr << "UnknownCode" << Code;
-        if (NonSymbolic && GetCodeName(Code, BlockID))
-          std::cerr << " codeid=" << Code;
+          errs() << "UnknownCode" << Code;
+        if (NonSymbolic &&
+            GetCodeName(Code, BlockID, *Stream.getBitStreamReader()))
+          errs() << " codeid=" << Code;
         if (AbbrevID != bitc::UNABBREV_RECORD)
-          std::cerr << " abbrevid=" << AbbrevID;
+          errs() << " abbrevid=" << AbbrevID;
 
         for (unsigned i = 0, e = Record.size(); i != e; ++i)
-          std::cerr << " op" << i << "=" << (int64_t)Record[i];
+          errs() << " op" << i << "=" << (int64_t)Record[i];
+        
+        errs() << "/>";
         
-        std::cerr << "/>\n";
+        if (BlobStart) {
+          errs() << " blob data = ";
+          bool BlobIsPrintable = true;
+          for (unsigned i = 0; i != BlobLen; ++i)
+            if (!isprint(BlobStart[i])) {
+              BlobIsPrintable = false;
+              break;
+            }
+          
+          if (BlobIsPrintable)
+            errs() << "'" << std::string(BlobStart, BlobStart+BlobLen) <<"'";
+          else
+            errs() << "unprintable, " << BlobLen << " bytes.";
+        }
+        
+        errs() << "\n";
       }
       
       break;
@@ -367,28 +443,37 @@ static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) {
 }
 
 static void PrintSize(double Bits) {
-  std::cerr << Bits << "b/" << Bits/8 << "B/" << Bits/32 << "W";
+  fprintf(stderr, "%.2f/%.2fB/%lluW", Bits, Bits/8,(unsigned long long)Bits/32);
+}
+static void PrintSize(uint64_t Bits) {
+  fprintf(stderr, "%llub/%.2fB/%lluW", (unsigned long long)Bits,
+          (double)Bits/8, (unsigned long long)Bits/32);
 }
 
 
 /// AnalyzeBitcode - Analyze the bitcode file specified by InputFilename.
 static int AnalyzeBitcode() {
   // Read the input file.
-  MemoryBuffer *Buffer;
-  if (InputFilename == "-")
-    Buffer = MemoryBuffer::getSTDIN();
-  else
-    Buffer = MemoryBuffer::getFile(&InputFilename[0], InputFilename.size());
+  MemoryBuffer *MemBuf = MemoryBuffer::getFileOrSTDIN(InputFilename.c_str());
 
-  if (Buffer == 0)
+  if (MemBuf == 0)
     return Error("Error reading '" + InputFilename + "'.");
   
-  if (Buffer->getBufferSize() & 3)
+  if (MemBuf->getBufferSize() & 3)
     return Error("Bitcode stream should be a multiple of 4 bytes in length");
   
-  unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
-  BitstreamReader Stream(BufPtr, BufPtr+Buffer->getBufferSize());
-
+  unsigned char *BufPtr = (unsigned char *)MemBuf->getBufferStart();
+  unsigned char *EndBufPtr = BufPtr+MemBuf->getBufferSize();
+  
+  // If we have a wrapper header, parse it and ignore the non-bc file contents.
+  // The magic number is 0x0B17C0DE stored in little endian.
+  if (isBitcodeWrapper(BufPtr, EndBufPtr))
+    if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr))
+      return Error("Invalid bitcode wrapper header");
+  
+  BitstreamReader StreamFile(BufPtr, EndBufPtr);
+  BitstreamCursor Stream(StreamFile);
+  StreamFile.CollectBlockInfoNames();
   
   // Read the stream signature.
   char Signature[6];
@@ -419,77 +504,89 @@ static int AnalyzeBitcode() {
     ++NumTopBlocks;
   }
   
-  if (Dump) std::cerr << "\n\n";
+  if (Dump) errs() << "\n\n";
   
-  uint64_t BufferSizeBits = Buffer->getBufferSize()*8;
+  uint64_t BufferSizeBits = (EndBufPtr-BufPtr)*CHAR_BIT;
   // Print a summary of the read file.
-  std::cerr << "Summary of " << InputFilename << ":\n";
-  std::cerr << "         Total size: ";
+  errs() << "Summary of " << InputFilename << ":\n";
+  errs() << "         Total size: ";
   PrintSize(BufferSizeBits);
-  std::cerr << "\n";
-  std::cerr << "        Stream type: ";
+  errs() << "\n";
+  errs() << "        Stream type: ";
   switch (CurStreamType) {
   default: assert(0 && "Unknown bitstream type");
-  case UnknownBitstream: std::cerr << "unknown\n"; break;
-  case LLVMIRBitstream:  std::cerr << "LLVM IR\n"; break;
+  case UnknownBitstream: errs() << "unknown\n"; break;
+  case LLVMIRBitstream:  errs() << "LLVM IR\n"; break;
   }
-  std::cerr << "  # Toplevel Blocks: " << NumTopBlocks << "\n";
-  std::cerr << "\n";
+  errs() << "  # Toplevel Blocks: " << NumTopBlocks << "\n";
+  errs() << "\n";
 
   // Emit per-block stats.
-  std::cerr << "Per-block Summary:\n";
+  errs() << "Per-block Summary:\n";
   for (std::map<unsigned, PerBlockIDStats>::iterator I = BlockIDStats.begin(),
        E = BlockIDStats.end(); I != E; ++I) {
-    std::cerr << "  Block ID #" << I->first;
-    if (const char *BlockName = GetBlockName(I->first))
-      std::cerr << " (" << BlockName << ")";
-    std::cerr << ":\n";
+    errs() << "  Block ID #" << I->first;
+    if (const char *BlockName = GetBlockName(I->first, StreamFile))
+      errs() << " (" << BlockName << ")";
+    errs() << ":\n";
     
     const PerBlockIDStats &Stats = I->second;
-    std::cerr << "      Num Instances: " << Stats.NumInstances << "\n";
-    std::cerr << "         Total Size: ";
+    errs() << "      Num Instances: " << Stats.NumInstances << "\n";
+    errs() << "         Total Size: ";
     PrintSize(Stats.NumBits);
-    std::cerr << "\n";
-    std::cerr << "          % of file: "
-              << Stats.NumBits/(double)BufferSizeBits*100 << "\n";
+    errs() << "\n";
+    errs() << "          % of file: "
+           << Stats.NumBits/(double)BufferSizeBits*100 << "\n";
     if (Stats.NumInstances > 1) {
-      std::cerr << "       Average Size: ";
+      errs() << "       Average Size: ";
       PrintSize(Stats.NumBits/(double)Stats.NumInstances);
-      std::cerr << "\n";
-      std::cerr << "  Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
-                << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
-      std::cerr << "    Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
-                << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
-      std::cerr << "    Tot/Avg Records: " << Stats.NumRecords << "/"
-                << Stats.NumRecords/(double)Stats.NumInstances << "\n";
+      errs() << "\n";
+      errs() << "  Tot/Avg SubBlocks: " << Stats.NumSubBlocks << "/"
+             << Stats.NumSubBlocks/(double)Stats.NumInstances << "\n";
+      errs() << "    Tot/Avg Abbrevs: " << Stats.NumAbbrevs << "/"
+             << Stats.NumAbbrevs/(double)Stats.NumInstances << "\n";
+      errs() << "    Tot/Avg Records: " << Stats.NumRecords << "/"
+             << Stats.NumRecords/(double)Stats.NumInstances << "\n";
     } else {
-      std::cerr << "      Num SubBlocks: " << Stats.NumSubBlocks << "\n";
-      std::cerr << "        Num Abbrevs: " << Stats.NumAbbrevs << "\n";
-      std::cerr << "        Num Records: " << Stats.NumRecords << "\n";
+      errs() << "      Num SubBlocks: " << Stats.NumSubBlocks << "\n";
+      errs() << "        Num Abbrevs: " << Stats.NumAbbrevs << "\n";
+      errs() << "        Num Records: " << Stats.NumRecords << "\n";
     }
     if (Stats.NumRecords)
-      std::cerr << "      % Abbrev Recs: " << (Stats.NumAbbreviatedRecords/
-                   (double)Stats.NumRecords)*100 << "\n";
-    std::cerr << "\n";
+      errs() << "      % Abbrev Recs: " << (Stats.NumAbbreviatedRecords/
+                (double)Stats.NumRecords)*100 << "\n";
+    errs() << "\n";
     
     // Print a histogram of the codes we see.
     if (!NoHistogram && !Stats.CodeFreq.empty()) {
       std::vector<std::pair<unsigned, unsigned> > FreqPairs;  // <freq,code>
       for (unsigned i = 0, e = Stats.CodeFreq.size(); i != e; ++i)
-        if (unsigned Freq = Stats.CodeFreq[i])
+        if (unsigned Freq = Stats.CodeFreq[i].NumInstances)
           FreqPairs.push_back(std::make_pair(Freq, i));
       std::stable_sort(FreqPairs.begin(), FreqPairs.end());
       std::reverse(FreqPairs.begin(), FreqPairs.end());
       
-      std::cerr << "\tCode Histogram:\n";
+      errs() << "\tRecord Histogram:\n";
+      fprintf(stderr, "\t\t  Count    # Bits   %% Abv  Record Kind\n");
       for (unsigned i = 0, e = FreqPairs.size(); i != e; ++i) {
-        std::cerr << "\t\t" << FreqPairs[i].first << "\t";
-        if (const char *CodeName = GetCodeName(FreqPairs[i].second, I->first))
-          std::cerr << CodeName << "\n";
+        const PerRecordStats &RecStats = Stats.CodeFreq[FreqPairs[i].second];
+        
+        fprintf(stderr, "\t\t%7d %9llu ", RecStats.NumInstances,
+                (unsigned long long)RecStats.TotalBits);
+        
+        if (RecStats.NumAbbrev)
+          fprintf(stderr, "%7.2f  ",
+                  (double)RecStats.NumAbbrev/RecStats.NumInstances*100);
+        else
+          fprintf(stderr, "         ");
+        
+        if (const char *CodeName = 
+              GetCodeName(FreqPairs[i].second, I->first, StreamFile))
+          fprintf(stderr, "%s\n", CodeName);
         else
-          std::cerr << "UnknownCode" << FreqPairs[i].second << "\n";
+          fprintf(stderr, "UnknownCode%d\n", FreqPairs[i].second);
       }
-      std::cerr << "\n";
+      errs() << "\n";
       
     }
   }
@@ -498,10 +595,11 @@ static int AnalyzeBitcode() {
 
 
 int main(int argc, char **argv) {
-  llvm_shutdown_obj X;  // Call llvm_shutdown() on exit.
-  cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
-  
+  // Print a stack trace if we signal out.
   sys::PrintStackTraceOnErrorSignal();
+  PrettyStackTraceProgram X(argc, argv);
+  llvm_shutdown_obj Y;  // Call llvm_shutdown() on exit.
+  cl::ParseCommandLineOptions(argc, argv, "llvm-bcanalyzer file analyzer\n");
   
   return AnalyzeBitcode();
 }