[llvm-commits] [llvm] r68486 - in /llvm/trunk: include/llvm/Bitcode/BitstreamReader.h tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp

Chris Lattner sabre at nondot.org
Mon Apr 6 19:56:46 PDT 2009


Author: lattner
Date: Mon Apr  6 21:56:46 2009
New Revision: 68486

URL: http://llvm.org/viewvc/llvm-project?rev=68486&view=rev
Log:
Add an API for the bitstream reader to read blobs and return
them by reference, instead of packing each byte into a
smallvector.

Modified:
    llvm/trunk/include/llvm/Bitcode/BitstreamReader.h
    llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp

Modified: llvm/trunk/include/llvm/Bitcode/BitstreamReader.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/BitstreamReader.h?rev=68486&r1=68485&r2=68486&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Bitcode/BitstreamReader.h (original)
+++ llvm/trunk/include/llvm/Bitcode/BitstreamReader.h Mon Apr  6 21:56:46 2009
@@ -371,7 +371,8 @@
     return CurAbbrevs[AbbrevNo];
   }
   
-  unsigned ReadRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals) {
+  unsigned ReadRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals,
+                      const char **BlobStart = 0, unsigned *BlobLen = 0) {
     if (AbbrevID == bitc::UNABBREV_RECORD) {
       unsigned Code = ReadVBR(6);
       unsigned NumElts = ReadVBR(6);
@@ -413,9 +414,15 @@
           break;
         }
         
-        // Otherwise, read the number of bytes.
-        for (; NumElts; ++NextChar, --NumElts)
-          Vals.push_back(*NextChar);
+        // Otherwise, read the number of bytes.  If we can return a reference to
+        // the data, do so to avoid copying it.
+        if (BlobStart) {
+          *BlobStart = (const char*)NextChar;
+          *BlobLen = NumElts;
+        } else {
+          for (; NumElts; ++NextChar, --NumElts)
+            Vals.push_back(*NextChar);
+        }
         // Skip over tail padding.
         NextChar = NewEnd;
       } else {
@@ -428,6 +435,12 @@
     return Code;
   }
 
+  unsigned ReadRecord(unsigned AbbrevID, SmallVectorImpl<uint64_t> &Vals,
+                      const char *&BlobStart, unsigned &BlobLen) {
+    return ReadRecord(AbbrevID, Vals, &BlobStart, &BlobLen);
+  }
+
+  
   //===--------------------------------------------------------------------===//
   // Abbrev Processing
   //===--------------------------------------------------------------------===//

Modified: llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp?rev=68486&r1=68485&r2=68486&view=diff

==============================================================================
--- llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp (original)
+++ llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp Mon Apr  6 21:56:46 2009
@@ -342,32 +342,14 @@
       break;
     default:
       Record.clear();
-      bool HasBlob = false;
 
       ++BlockStats.NumRecords;
-      if (AbbrevID != bitc::UNABBREV_RECORD) {
+      if (AbbrevID != bitc::UNABBREV_RECORD)
         ++BlockStats.NumAbbreviatedRecords;
-        const BitCodeAbbrev *Abbv = Stream.getAbbrev(AbbrevID);
-        if (Abbv->getNumOperandInfos() != 0) {
-          const BitCodeAbbrevOp &LastOp =  
-            Abbv->getOperandInfo(Abbv->getNumOperandInfos()-1);
-          // If the last operand is a blob, then this record has blob data.
-          if (LastOp.isEncoding() && 
-              LastOp.getEncoding() == BitCodeAbbrevOp::Blob)
-            HasBlob = true;
-        }
-      }
         
-      unsigned Code;
       const char *BlobStart = 0;
       unsigned BlobLen = 0;
-      if (!HasBlob)
-        Code = Stream.ReadRecord(AbbrevID, Record);
-      else {
-        Code = Stream.ReadRecord(AbbrevID, Record);
-        BlobStart = BlobStart;
-        BlobLen = BlobLen;
-      }
+      unsigned Code = Stream.ReadRecord(AbbrevID, Record, BlobStart, BlobLen);
 
       // Increment the # occurrences of this code.
       if (BlockStats.CodeFreq.size() <= Code)
@@ -388,7 +370,24 @@
         for (unsigned i = 0, e = Record.size(); i != e; ++i)
           std::cerr << " op" << i << "=" << (int64_t)Record[i];
         
-        std::cerr << "/>\n";
+        std::cerr << "/>";
+        
+        if (BlobStart) {
+          std::cerr << " blob data = ";
+          bool BlobIsPrintable = true;
+          for (unsigned i = 0; i != BlobLen; ++i)
+            if (!isprint(BlobStart[i])) {
+              BlobIsPrintable = false;
+              break;
+            }
+          
+          if (BlobIsPrintable)
+            std::cerr << "'" << std::string(BlobStart, BlobStart+BlobLen) <<"'";
+          else
+            std::cerr << "unprintable, " << BlobLen << " bytes.";
+        }
+        
+        std::cerr << "\n";
       }
       
       break;





More information about the llvm-commits mailing list