[llvm-branch-commits] [llvm-branch] r70267 - in /llvm/branches/Apple/Dib: docs/BitCodeFormat.html include/llvm/Bitcode/BitCodes.h include/llvm/Bitcode/BitstreamReader.h include/llvm/Bitcode/Deserialize.h lib/Bitcode/Reader/BitcodeReader.cpp lib/Bitcode/Reader/BitcodeReader.h lib/Bitcode/Reader/Deserialize.cpp tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp

Bill Wendling isanbard at gmail.com
Mon Apr 27 15:59:45 PDT 2009


Author: void
Date: Mon Apr 27 17:59:44 2009
New Revision: 70267

URL: http://llvm.org/viewvc/llvm-project?rev=70267&view=rev
Log:
--- Merging r70157 into '.':
U    include/llvm/Bitcode/BitstreamReader.h
U    include/llvm/Bitcode/Deserialize.h
U    tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
U    lib/Bitcode/Reader/Deserialize.cpp
U    lib/Bitcode/Reader/BitcodeReader.h
U    lib/Bitcode/Reader/BitcodeReader.cpp
--- Merging r70159 into '.':
G    include/llvm/Bitcode/BitstreamReader.h
--- Merging r70165 into '.':
U    include/llvm/Bitcode/BitCodes.h
G    include/llvm/Bitcode/BitstreamReader.h
U    docs/BitCodeFormat.html
G    tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
--- Merging r70214 into '.':
G    tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
--- Merging r70215 into '.':
G    tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
--- Merging r70239 into '.':
G    include/llvm/Bitcode/BitstreamReader.h
G    tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp


Modified:
    llvm/branches/Apple/Dib/docs/BitCodeFormat.html
    llvm/branches/Apple/Dib/include/llvm/Bitcode/BitCodes.h
    llvm/branches/Apple/Dib/include/llvm/Bitcode/BitstreamReader.h
    llvm/branches/Apple/Dib/include/llvm/Bitcode/Deserialize.h
    llvm/branches/Apple/Dib/lib/Bitcode/Reader/BitcodeReader.cpp
    llvm/branches/Apple/Dib/lib/Bitcode/Reader/BitcodeReader.h
    llvm/branches/Apple/Dib/lib/Bitcode/Reader/Deserialize.cpp
    llvm/branches/Apple/Dib/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp

Modified: llvm/branches/Apple/Dib/docs/BitCodeFormat.html
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/docs/BitCodeFormat.html?rev=70267&r1=70266&r2=70267&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/docs/BitCodeFormat.html (original)
+++ llvm/branches/Apple/Dib/docs/BitCodeFormat.html Mon Apr 27 17:59:44 2009
@@ -563,6 +563,8 @@
 <pre>
 [SETBID (#1), blockid]
 [DEFINE_ABBREV, ...]
+[BLOCKNAME, ...name...]
+[SETRECORDNAME, RecordID, ...name...]
 </pre>
 </div>
 
@@ -582,6 +584,15 @@
 in <tt><a href="#DEFINE_ABBREV">DEFINE_ABBREV</a></tt>.
 </p>
 
+<p>The <tt>BLOCKNAME</tt> can optionally occur in this block.  The elements of
+the record are the bytes for the string name of the block.  llvm-bcanalyzer uses
+this to dump out bitcode files symbolically.</p>
+
+<p>The <tt>SETRECORDNAME</tt> record can optionally occur in this block.  The
+first entry is a record ID number and the rest of the elements of the record are
+the bytes for the string name of the record.  llvm-bcanalyzer uses
+this to dump out bitcode files symbolically.</p>
+
 <p>
 Note that although the data in <tt>BLOCKINFO</tt> blocks is described as
 "metadata," the abbreviations they contain are essential for parsing records

Modified: llvm/branches/Apple/Dib/include/llvm/Bitcode/BitCodes.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/include/llvm/Bitcode/BitCodes.h?rev=70267&r1=70266&r2=70267&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/include/llvm/Bitcode/BitCodes.h (original)
+++ llvm/branches/Apple/Dib/include/llvm/Bitcode/BitCodes.h Mon Apr 27 17:59:44 2009
@@ -66,10 +66,12 @@
   /// BlockInfoCodes - The blockinfo block contains metadata about user-defined
   /// blocks.
   enum BlockInfoCodes {
-    BLOCKINFO_CODE_SETBID = 1  // SETBID: [blockid#]
     // DEFINE_ABBREV has magic semantics here, applying to the current SETBID'd
     // block, instead of the BlockInfo block.
-    // BLOCKNAME: give string name to block, if desired.
+    
+    BLOCKINFO_CODE_SETBID = 1,       // SETBID: [blockid#]
+    BLOCKINFO_CODE_BLOCKNAME = 2,    // BLOCKNAME: [name]
+    BLOCKINFO_CODE_SETRECORDNAME = 3 // BLOCKINFO_CODE_SETRECORDNAME: [id, name]
   };
 
 } // End bitc namespace

Modified: llvm/branches/Apple/Dib/include/llvm/Bitcode/BitstreamReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/include/llvm/Bitcode/BitstreamReader.h?rev=70267&r1=70266&r2=70267&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/include/llvm/Bitcode/BitstreamReader.h (original)
+++ llvm/branches/Apple/Dib/include/llvm/Bitcode/BitstreamReader.h Mon Apr 27 17:59:44 2009
@@ -24,120 +24,241 @@
   class Deserializer;
 
 class BitstreamReader {
-  const unsigned char *NextChar;
-  const unsigned char *LastChar;
-  friend class Deserializer;
+public:
+  /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks.
+  /// These describe abbreviations that all blocks of the specified ID inherit.
+  struct BlockInfo {
+    unsigned BlockID;
+    std::vector<BitCodeAbbrev*> Abbrevs;
+    std::string Name;
+    
+    std::vector<std::pair<unsigned, std::string> > RecordNames;
+  };
+private:
+  /// FirstChar/LastChar - This remembers the first and last bytes of the
+  /// stream.
+  const unsigned char *FirstChar, *LastChar;
+  
+  std::vector<BlockInfo> BlockInfoRecords;
+
+  /// IgnoreBlockInfoNames - This is set to true if we don't care about the
+  /// block/record name information in the BlockInfo block. Only llvm-bcanalyzer
+  /// uses this.
+  bool IgnoreBlockInfoNames;
+  
+  BitstreamReader(const BitstreamReader&);  // NOT IMPLEMENTED
+  void operator=(const BitstreamReader&);  // NOT IMPLEMENTED
+public:
+  BitstreamReader() : FirstChar(0), LastChar(0), IgnoreBlockInfoNames(true) {
+  }
+
+  BitstreamReader(const unsigned char *Start, const unsigned char *End) {
+    IgnoreBlockInfoNames = true;
+    init(Start, End);
+  }
+
+  void init(const unsigned char *Start, const unsigned char *End) {
+    FirstChar = Start;
+    LastChar = End;
+    assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes");
+  }
+
+  ~BitstreamReader() {
+    // Free the BlockInfoRecords.
+    while (!BlockInfoRecords.empty()) {
+      BlockInfo &Info = BlockInfoRecords.back();
+      // Free blockinfo abbrev info.
+      for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size());
+           i != e; ++i)
+        Info.Abbrevs[i]->dropRef();
+      BlockInfoRecords.pop_back();
+    }
+  }
+  
+  const unsigned char *getFirstChar() const { return FirstChar; }
+  const unsigned char *getLastChar() const { return LastChar; }
+
+  /// CollectBlockInfoNames - This is called by clients that want block/record
+  /// name information.
+  void CollectBlockInfoNames() { IgnoreBlockInfoNames = false; }
+  bool isIgnoringBlockInfoNames() { return IgnoreBlockInfoNames; }
+  
+  //===--------------------------------------------------------------------===//
+  // Block Manipulation
+  //===--------------------------------------------------------------------===//
+
+  /// hasBlockInfoRecords - Return true if we've already read and processed the
+  /// block info block for this Bitstream.  We only process it for the first
+  /// cursor that walks over it.
+  bool hasBlockInfoRecords() const { return !BlockInfoRecords.empty(); }
+  
+  /// getBlockInfo - If there is block info for the specified ID, return it,
+  /// otherwise return null.
+  const BlockInfo *getBlockInfo(unsigned BlockID) const {
+    // Common case, the most recent entry matches BlockID.
+    if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
+      return &BlockInfoRecords.back();
 
+    for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
+         i != e; ++i)
+      if (BlockInfoRecords[i].BlockID == BlockID)
+        return &BlockInfoRecords[i];
+    return 0;
+  }
+
+  BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
+    if (const BlockInfo *BI = getBlockInfo(BlockID))
+      return *const_cast<BlockInfo*>(BI);
+
+    // Otherwise, add a new record.
+    BlockInfoRecords.push_back(BlockInfo());
+    BlockInfoRecords.back().BlockID = BlockID;
+    return BlockInfoRecords.back();
+  }
+
+};
+
+class BitstreamCursor {
+  friend class Deserializer;
+  BitstreamReader *BitStream;
+  const unsigned char *NextChar;
+  
   /// CurWord - This is the current data we have pulled from the stream but have
   /// not returned to the client.
   uint32_t CurWord;
-
+  
   /// BitsInCurWord - This is the number of bits in CurWord that are valid. This
   /// is always from [0...31] inclusive.
   unsigned BitsInCurWord;
-
+  
   // CurCodeSize - This is the declared size of code values used for the current
   // block, in bits.
   unsigned CurCodeSize;
-
+  
   /// CurAbbrevs - Abbrevs installed at in this block.
   std::vector<BitCodeAbbrev*> CurAbbrevs;
-
+  
   struct Block {
     unsigned PrevCodeSize;
     std::vector<BitCodeAbbrev*> PrevAbbrevs;
     explicit Block(unsigned PCS) : PrevCodeSize(PCS) {}
   };
-
+  
   /// BlockScope - This tracks the codesize of parent blocks.
   SmallVector<Block, 8> BlockScope;
-
-  /// BlockInfo - This contains information emitted to BLOCKINFO_BLOCK blocks.
-  /// These describe abbreviations that all blocks of the specified ID inherit.
-  struct BlockInfo {
-    unsigned BlockID;
-    std::vector<BitCodeAbbrev*> Abbrevs;
-  };
-  std::vector<BlockInfo> BlockInfoRecords;
-
-  /// FirstChar - This remembers the first byte of the stream.
-  const unsigned char *FirstChar;
+  
 public:
-  BitstreamReader() {
-    NextChar = FirstChar = LastChar = 0;
+  BitstreamCursor() : BitStream(0), NextChar(0) {
+  }
+  BitstreamCursor(const BitstreamCursor &RHS) : BitStream(0), NextChar(0) {
+    operator=(RHS);
+  }
+  
+  explicit BitstreamCursor(BitstreamReader &R) : BitStream(&R) {
+    NextChar = R.getFirstChar();
+    assert(NextChar && "Bitstream not initialized yet");
     CurWord = 0;
     BitsInCurWord = 0;
-    CurCodeSize = 0;
-  }
-
-  BitstreamReader(const unsigned char *Start, const unsigned char *End) {
-    init(Start, End);
+    CurCodeSize = 2;
   }
-
-  void init(const unsigned char *Start, const unsigned char *End) {
-    NextChar = FirstChar = Start;
-    LastChar = End;
-    assert(((End-Start) & 3) == 0 &&"Bitcode stream not a multiple of 4 bytes");
+  
+  void init(BitstreamReader &R) {
+    freeState();
+    
+    BitStream = &R;
+    NextChar = R.getFirstChar();
+    assert(NextChar && "Bitstream not initialized yet");
     CurWord = 0;
     BitsInCurWord = 0;
     CurCodeSize = 2;
   }
-
-  ~BitstreamReader() {
-    // Abbrevs could still exist if the stream was broken.  If so, don't leak
-    // them.
+  
+  ~BitstreamCursor() {
+    freeState();
+  }
+  
+  void operator=(const BitstreamCursor &RHS) {
+    freeState();
+    
+    BitStream = RHS.BitStream;
+    NextChar = RHS.NextChar;
+    CurWord = RHS.CurWord;
+    BitsInCurWord = RHS.BitsInCurWord;
+    CurCodeSize = RHS.CurCodeSize;
+    
+    // Copy abbreviations, and bump ref counts.
+    CurAbbrevs = RHS.CurAbbrevs;
     for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
          i != e; ++i)
-      CurAbbrevs[i]->dropRef();
-
+      CurAbbrevs[i]->addRef();
+    
+    // Copy block scope and bump ref counts.
     for (unsigned S = 0, e = static_cast<unsigned>(BlockScope.size());
          S != e; ++S) {
       std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
       for (unsigned i = 0, e = static_cast<unsigned>(Abbrevs.size());
            i != e; ++i)
-        Abbrevs[i]->dropRef();
+        Abbrevs[i]->addRef();
     }
-
-    // Free the BlockInfoRecords.
-    while (!BlockInfoRecords.empty()) {
-      BlockInfo &Info = BlockInfoRecords.back();
-      // Free blockinfo abbrev info.
-      for (unsigned i = 0, e = static_cast<unsigned>(Info.Abbrevs.size());
+  }
+  
+  void freeState() {
+    // Free all the Abbrevs.
+    for (unsigned i = 0, e = static_cast<unsigned>(CurAbbrevs.size());
+         i != e; ++i)
+      CurAbbrevs[i]->dropRef();
+    CurAbbrevs.clear();
+    
+    // Free all the Abbrevs in the block scope.
+    for (unsigned S = 0, e = static_cast<unsigned>(BlockScope.size());
+         S != e; ++S) {
+      std::vector<BitCodeAbbrev*> &Abbrevs = BlockScope[S].PrevAbbrevs;
+      for (unsigned i = 0, e = static_cast<unsigned>(Abbrevs.size());
            i != e; ++i)
-        Info.Abbrevs[i]->dropRef();
-      BlockInfoRecords.pop_back();
+        Abbrevs[i]->dropRef();
     }
+    BlockScope.clear();
   }
-
+  
+  /// GetAbbrevIDWidth - Return the number of bits used to encode an abbrev #.
+  unsigned GetAbbrevIDWidth() const { return CurCodeSize; }
+  
   bool AtEndOfStream() const {
-    return NextChar == LastChar && BitsInCurWord == 0;
+    return NextChar == BitStream->getLastChar() && BitsInCurWord == 0;
   }
-
+  
   /// GetCurrentBitNo - Return the bit # of the bit we are reading.
   uint64_t GetCurrentBitNo() const {
-    return (NextChar-FirstChar)*CHAR_BIT - BitsInCurWord;
+    return (NextChar-BitStream->getFirstChar())*CHAR_BIT - BitsInCurWord;
   }
-
+  
+  BitstreamReader *getBitStreamReader() {
+    return BitStream;
+  }
+  const BitstreamReader *getBitStreamReader() const {
+    return BitStream;
+  }
+  
+  
   /// JumpToBit - Reset the stream to the specified bit number.
   void JumpToBit(uint64_t BitNo) {
     uintptr_t ByteNo = uintptr_t(BitNo/8) & ~3;
     uintptr_t WordBitNo = uintptr_t(BitNo) & 31;
-    assert(ByteNo <= (uintptr_t)(LastChar-FirstChar) && "Invalid location");
-
+    assert(ByteNo <= (uintptr_t)(BitStream->getLastChar()-
+                                 BitStream->getFirstChar()) &&
+           "Invalid location");
+    
     // Move the cursor to the right word.
-    NextChar = FirstChar+ByteNo;
+    NextChar = BitStream->getFirstChar()+ByteNo;
     BitsInCurWord = 0;
     CurWord = 0;
-
+    
     // Skip over any bits that are already consumed.
-    if (WordBitNo) {
+    if (WordBitNo)
       Read(static_cast<unsigned>(WordBitNo));
-    }
   }
-
-  /// GetAbbrevIDWidth - Return the number of bits used to encode an abbrev #.
-  unsigned GetAbbrevIDWidth() const { return CurCodeSize; }
-
+  
+  
   uint32_t Read(unsigned NumBits) {
     // If the field is fully contained by CurWord, return it quickly.
     if (BitsInCurWord >= NumBits) {
@@ -148,7 +269,7 @@
     }
 
     // If we run out of data, stop at the end of the stream.
-    if (NextChar == LastChar) {
+    if (NextChar == BitStream->getLastChar()) {
       CurWord = 0;
       BitsInCurWord = 0;
       return 0;
@@ -224,31 +345,10 @@
     CurWord = 0;
   }
 
-
   unsigned ReadCode() {
     return Read(CurCodeSize);
   }
 
-  //===--------------------------------------------------------------------===//
-  // Block Manipulation
-  //===--------------------------------------------------------------------===//
-
-private:
-  /// getBlockInfo - If there is block info for the specified ID, return it,
-  /// otherwise return null.
-  BlockInfo *getBlockInfo(unsigned BlockID) {
-    // Common case, the most recent entry matches BlockID.
-    if (!BlockInfoRecords.empty() && BlockInfoRecords.back().BlockID == BlockID)
-      return &BlockInfoRecords.back();
-
-    for (unsigned i = 0, e = static_cast<unsigned>(BlockInfoRecords.size());
-         i != e; ++i)
-      if (BlockInfoRecords[i].BlockID == BlockID)
-        return &BlockInfoRecords[i];
-    return 0;
-  }
-public:
-
 
   // Block header:
   //    [ENTER_SUBBLOCK, blockid, newcodelen, <align4bytes>, blocklen]
@@ -271,7 +371,7 @@
 
     // Check that the block wasn't partially defined, and that the offset isn't
     // bogus.
-    if (AtEndOfStream() || NextChar+NumWords*4 > LastChar)
+    if (AtEndOfStream() || NextChar+NumWords*4 > BitStream->getLastChar())
       return true;
 
     NextChar += NumWords*4;
@@ -286,7 +386,8 @@
     BlockScope.back().PrevAbbrevs.swap(CurAbbrevs);
 
     // Add the abbrevs specific to this block to the CurAbbrevs list.
-    if (BlockInfo *Info = getBlockInfo(BlockID)) {
+    if (const BitstreamReader::BlockInfo *Info =
+          BitStream->getBlockInfo(BlockID)) {
       for (unsigned i = 0, e = static_cast<unsigned>(Info->Abbrevs.size());
            i != e; ++i) {
         CurAbbrevs.push_back(Info->Abbrevs[i]);
@@ -301,7 +402,8 @@
     if (NumWordsP) *NumWordsP = NumWords;
 
     // Validate that this block is sane.
-    if (CurCodeSize == 0 || AtEndOfStream() || NextChar+NumWords*4 > LastChar)
+    if (CurCodeSize == 0 || AtEndOfStream() ||
+        NextChar+NumWords*4 > BitStream->getLastChar())
       return true;
 
     return false;
@@ -331,7 +433,7 @@
     BlockScope.pop_back();
   }
 
-  //===--------------------------------------------------------------------===//
+ //===--------------------------------------------------------------------===//
   // Record Processing
   //===--------------------------------------------------------------------===//
 
@@ -407,9 +509,9 @@
         
         // If this would read off the end of the bitcode file, just set the
         // record to empty and return.
-        if (NewEnd > LastChar) {
+        if (NewEnd > BitStream->getLastChar()) {
           Vals.append(NumElts, 0);
-          NextChar = LastChar;
+          NextChar = BitStream->getLastChar();
           break;
         }
         
@@ -462,29 +564,18 @@
     }
     CurAbbrevs.push_back(Abbv);
   }
-
-  //===--------------------------------------------------------------------===//
-  // BlockInfo Block Reading
-  //===--------------------------------------------------------------------===//
-
-private:
-  BlockInfo &getOrCreateBlockInfo(unsigned BlockID) {
-    if (BlockInfo *BI = getBlockInfo(BlockID))
-      return *BI;
-
-    // Otherwise, add a new record.
-    BlockInfoRecords.push_back(BlockInfo());
-    BlockInfoRecords.back().BlockID = BlockID;
-    return BlockInfoRecords.back();
-  }
-
+  
 public:
 
   bool ReadBlockInfoBlock() {
+    // If this is the second stream to get to the block info block, skip it.
+    if (BitStream->hasBlockInfoRecords())
+      return SkipBlock();
+    
     if (EnterSubBlock(bitc::BLOCKINFO_BLOCK_ID)) return true;
 
     SmallVector<uint64_t, 64> Record;
-    BlockInfo *CurBlockInfo = 0;
+    BitstreamReader::BlockInfo *CurBlockInfo = 0;
 
     // Read all the records for this module.
     while (1) {
@@ -516,13 +607,32 @@
       default: break;  // Default behavior, ignore unknown content.
       case bitc::BLOCKINFO_CODE_SETBID:
         if (Record.size() < 1) return true;
-        CurBlockInfo = &getOrCreateBlockInfo((unsigned)Record[0]);
+        CurBlockInfo = &BitStream->getOrCreateBlockInfo((unsigned)Record[0]);
+        break;
+      case bitc::BLOCKINFO_CODE_BLOCKNAME: {
+        if (!CurBlockInfo) return true;
+        if (BitStream->isIgnoringBlockInfoNames()) break;  // Ignore name.
+        std::string Name;
+        for (unsigned i = 0, e = Record.size(); i != e; ++i)
+          Name += (char)Record[i];
+        CurBlockInfo->Name = Name;
         break;
       }
+      case bitc::BLOCKINFO_CODE_SETRECORDNAME: {
+        if (!CurBlockInfo) return true;
+        if (BitStream->isIgnoringBlockInfoNames()) break;  // Ignore name.
+        std::string Name;
+        for (unsigned i = 1, e = Record.size(); i != e; ++i)
+          Name += (char)Record[i];
+        CurBlockInfo->RecordNames.push_back(std::make_pair((unsigned)Record[0],
+                                                           Name));
+        break;
+      }
+      }
     }
   }
 };
-
+  
 } // End llvm namespace
 
 #endif

Modified: llvm/branches/Apple/Dib/include/llvm/Bitcode/Deserialize.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/include/llvm/Bitcode/Deserialize.h?rev=70267&r1=70266&r2=70267&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/include/llvm/Bitcode/Deserialize.h (original)
+++ llvm/branches/Apple/Dib/include/llvm/Bitcode/Deserialize.h Mon Apr 27 17:59:44 2009
@@ -116,7 +116,7 @@
   //===----------------------------------------------------------===//
 
 private:
-  BitstreamReader& Stream;
+  BitstreamCursor Stream;
   SmallVector<uint64_t,20> Record;
   unsigned RecIdx;
   BumpPtrAllocator Allocator;
@@ -482,7 +482,7 @@
 
   unsigned getRecordCode();
 
-  BitstreamReader& getStream() { return Stream; }
+  BitstreamCursor &getStream() { return Stream; }
 
 private:
   bool AdvanceStream();

Modified: llvm/branches/Apple/Dib/lib/Bitcode/Reader/BitcodeReader.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/lib/Bitcode/Reader/BitcodeReader.cpp?rev=70267&r1=70266&r2=70267&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/lib/Bitcode/Reader/BitcodeReader.cpp (original)
+++ llvm/branches/Apple/Dib/lib/Bitcode/Reader/BitcodeReader.cpp Mon Apr 27 17:59:44 2009
@@ -1346,7 +1346,8 @@
     if (SkipBitcodeWrapperHeader(BufPtr, BufEnd))
       return Error("Invalid bitcode wrapper header");
   
-  Stream.init(BufPtr, BufEnd);
+  StreamFile.init(BufPtr, BufEnd);
+  Stream.init(StreamFile);
   
   // Sniff for the signature.
   if (Stream.Read(8) != 'B' ||

Modified: llvm/branches/Apple/Dib/lib/Bitcode/Reader/BitcodeReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/lib/Bitcode/Reader/BitcodeReader.h?rev=70267&r1=70266&r2=70267&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/lib/Bitcode/Reader/BitcodeReader.h (original)
+++ llvm/branches/Apple/Dib/lib/Bitcode/Reader/BitcodeReader.h Mon Apr 27 17:59:44 2009
@@ -86,7 +86,8 @@
 
 class BitcodeReader : public ModuleProvider {
   MemoryBuffer *Buffer;
-  BitstreamReader Stream;
+  BitstreamReader StreamFile;
+  BitstreamCursor Stream;
   
   const char *ErrorString;
   

Modified: llvm/branches/Apple/Dib/lib/Bitcode/Reader/Deserialize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/lib/Bitcode/Reader/Deserialize.cpp?rev=70267&r1=70266&r2=70267&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/lib/Bitcode/Reader/Deserialize.cpp (original)
+++ llvm/branches/Apple/Dib/lib/Bitcode/Reader/Deserialize.cpp Mon Apr 27 17:59:44 2009
@@ -22,7 +22,7 @@
 Deserializer::Deserializer(BitstreamReader& stream)
   : Stream(stream), RecIdx(0), FreeList(NULL), AbbrevNo(0), RecordCode(0) {
 
-    StreamStart = Stream.GetCurrentBitNo();
+  StreamStart = Stream.GetCurrentBitNo();
 }
 
 Deserializer::~Deserializer() {

Modified: llvm/branches/Apple/Dib/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/Apple/Dib/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp?rev=70267&r1=70266&r2=70267&view=diff

==============================================================================
--- llvm/branches/Apple/Dib/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp (original)
+++ llvm/branches/Apple/Dib/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp Mon Apr 27 17:59:44 2009
@@ -72,7 +72,8 @@
 
 /// 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)
@@ -80,6 +81,14 @@
     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) {
@@ -96,18 +105,30 @@
 
 /// 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) {
@@ -224,6 +245,13 @@
   }
 }
 
+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.
@@ -243,7 +271,7 @@
   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),
@@ -262,7 +290,7 @@
 }
 
 /// 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();
@@ -289,7 +317,7 @@
   const char *BlockName = 0;
   if (Dump) {
     std::cerr << Indent << "<";
-    if ((BlockName = GetBlockName(BlockID)))
+    if ((BlockName = GetBlockName(BlockID, *Stream.getBitStreamReader())))
       std::cerr << BlockName;
     else
       std::cerr << "UnknownBlock" << BlockID;
@@ -308,6 +336,8 @@
     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) {
@@ -351,18 +381,26 @@
       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))
+        if (const char *CodeName =
+              GetCodeName(Code, BlockID, *Stream.getBitStreamReader()))
           std::cerr << CodeName;
         else
           std::cerr << "UnknownCode" << Code;
-        if (NonSymbolic && GetCodeName(Code, BlockID))
+        if (NonSymbolic &&
+            GetCodeName(Code, BlockID, *Stream.getBitStreamReader()))
           std::cerr << " codeid=" << Code;
         if (AbbrevID != bitc::UNABBREV_RECORD)
           std::cerr << " abbrevid=" << AbbrevID;
@@ -396,7 +434,11 @@
 }
 
 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);
 }
 
 
@@ -420,8 +462,9 @@
     if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr))
       return Error("Invalid bitcode wrapper header");
   
-  BitstreamReader Stream(BufPtr, EndBufPtr);
-
+  BitstreamReader StreamFile(BufPtr, EndBufPtr);
+  BitstreamCursor Stream(StreamFile);
+  StreamFile.CollectBlockInfoNames();
   
   // Read the stream signature.
   char Signature[6];
@@ -474,7 +517,7 @@
   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))
+    if (const char *BlockName = GetBlockName(I->first, StreamFile))
       std::cerr << " (" << BlockName << ")";
     std::cerr << ":\n";
     
@@ -509,18 +552,30 @@
     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";
+      std::cerr << "\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,
+                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";
       





More information about the llvm-branch-commits mailing list