[llvm] r193389 - Support for reading program counts in llvm-cov.

Yuchen Wu yuchenericwu at hotmail.com
Thu Oct 24 19:22:21 PDT 2013


Author: ywu
Date: Thu Oct 24 21:22:21 2013
New Revision: 193389

URL: http://llvm.org/viewvc/llvm-project?rev=193389&view=rev
Log:
Support for reading program counts in llvm-cov.

llvm-cov will now be able to read program counts from the GCDA file and
output it in the same format as gcov. The program summary tag was
identified from gcov-io.h as "\0\0\0\a3".

There is currently a bug in GCOVProfiling.cpp which does not generate
the
run- or program-counting IR, so this change was tested manually by
modifying the GCDA file and comparing the gcov and llvm-cov outputs.

Modified:
    llvm/trunk/include/llvm/Support/GCOV.h
    llvm/trunk/lib/IR/GCOV.cpp

Modified: llvm/trunk/include/llvm/Support/GCOV.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/GCOV.h?rev=193389&r1=193388&r2=193389&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/GCOV.h (original)
+++ llvm/trunk/include/llvm/Support/GCOV.h Thu Oct 24 21:22:21 2013
@@ -126,6 +126,19 @@ public:
     return true;
   }
 
+  /// readProgramTag - If cursor points to a program summary tag then increment
+  /// the cursor and return true otherwise return false.
+  bool readProgramTag() {
+    StringRef Tag = Buffer->getBuffer().slice(Cursor, Cursor+4);
+    if (Tag.empty() ||
+        Tag[0] != '\0' || Tag[1] != '\0' ||
+        Tag[2] != '\0' || Tag[3] != '\xa3') {
+      return false;
+    }
+    Cursor += 4;
+    return true;
+  }
+
   uint32_t readInt() {
     uint32_t Result;
     StringRef Str = Buffer->getBuffer().slice(Cursor, Cursor+4);
@@ -159,13 +172,14 @@ private:
 /// (.gcno and .gcda).
 class GCOVFile {
 public:
-  GCOVFile() {}
+  GCOVFile() : Functions(), ProgramCount(0) {}
   ~GCOVFile();
   bool read(GCOVBuffer &Buffer);
   void dump();
   void collectLineCounts(FileInfo &FI);
 private:
   SmallVector<GCOVFunction *, 16> Functions;
+  uint32_t ProgramCount;
 };
 
 /// GCOVFunction - Collects function information.
@@ -220,9 +234,11 @@ public:
   void addLineCount(StringRef Filename, uint32_t Line, uint64_t Count) {
     LineInfo[Filename][Line-1] += Count;
   }
+  void setProgramCount(uint32_t PC) { ProgramCount = PC; }
   void print(StringRef gcnoFile, StringRef gcdaFile);
 private:
   StringMap<LineCounts> LineInfo;
+  uint32_t ProgramCount;
 };
 
 }

Modified: llvm/trunk/lib/IR/GCOV.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/GCOV.cpp?rev=193389&r1=193388&r2=193389&view=diff
==============================================================================
--- llvm/trunk/lib/IR/GCOV.cpp (original)
+++ llvm/trunk/lib/IR/GCOV.cpp Thu Oct 24 21:22:21 2013
@@ -44,21 +44,24 @@ bool GCOVFile::read(GCOVBuffer &Buffer)
   if (Format == GCOV::InvalidGCOV)
     return false;
 
-  unsigned i = 0;
-  while (1) {
-    GCOVFunction *GFun = NULL;
-    if (isGCDAFile(Format)) {
-      // Use existing function while reading .gcda file.
-      assert(i < Functions.size() && ".gcda data does not match .gcno data");
-      GFun = Functions[i];
-    } else if (isGCNOFile(Format)) {
-      GFun = new GCOVFunction();
+  if (isGCNOFile(Format)) {
+    while (true) {
+      GCOVFunction *GFun = new GCOVFunction();
+      if (!GFun->read(Buffer, Format))
+        break;
       Functions.push_back(GFun);
     }
-    if (!GFun || !GFun->read(Buffer, Format))
-      break;
-    ++i;
   }
+  else if (isGCDAFile(Format)) {
+    for (size_t i = 0, e = Functions.size(); i < e; ++i) {
+      bool ReadGCDA = Functions[i]->read(Buffer, Format);
+      (void)ReadGCDA;
+      assert(ReadGCDA && ".gcda data does not match .gcno data");
+    }
+    while (Buffer.readProgramTag())
+      ++ProgramCount;
+  }
+
   return true;
 }
 
@@ -75,6 +78,7 @@ void GCOVFile::collectLineCounts(FileInf
   for (SmallVectorImpl<GCOVFunction *>::iterator I = Functions.begin(),
          E = Functions.end(); I != E; ++I) 
     (*I)->collectLineCounts(FI);
+  FI.setProgramCount(ProgramCount);
 }
 
 //===----------------------------------------------------------------------===//
@@ -252,6 +256,7 @@ void FileInfo::print(StringRef gcnoFile,
     outs() << "        -:    0:Source:" << Filename << "\n";
     outs() << "        -:    0:Graph:" << gcnoFile << "\n";
     outs() << "        -:    0:Data:" << gcdaFile << "\n";
+    outs() << "        -:    0:Programs:" << ProgramCount << "\n";
     LineCounts &L = LineInfo[Filename];
     OwningPtr<MemoryBuffer> Buff;
     if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, Buff)) {





More information about the llvm-commits mailing list