[llvm] r193299 - Fixed llvm-cov to count edges instead of blocks.

Yuchen Wu yuchenericwu at hotmail.com
Wed Oct 23 18:51:04 PDT 2013


Author: ywu
Date: Wed Oct 23 20:51:04 2013
New Revision: 193299

URL: http://llvm.org/viewvc/llvm-project?rev=193299&view=rev
Log:
Fixed llvm-cov to count edges instead of blocks.

This was a fundamental flaw in llvm-cov where it treated the values in
the GCDA files as block counts instead of edge counts. This created
incorrect line counts when branching was present. Instead, the edge
counts should be summed to obtain the correct block count.

The fix was tested using custom test files as well as single source
files from the test-suite directory. The behaviour can be verified by
reading the GCOV documentation that describes the GCDA spec ("ARC_COUNTS
gives the counter values for those arcs that are instrumented") and the
header description provided by GCOVProfiling.cpp ("instruments the code
that runs to records (sic) the edges between blocks that run and emit a
complementary "gcda" file on exit").

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=193299&r1=193298&r2=193299&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/GCOV.h (original)
+++ llvm/trunk/include/llvm/Support/GCOV.h Wed Oct 23 20:51:04 2013
@@ -191,7 +191,8 @@ public:
   ~GCOVBlock();
   void addEdge(uint32_t N) { Edges.push_back(N); }
   void addLine(StringRef Filename, uint32_t LineNo);
-  void addCount(uint64_t N) { Counter = N; }
+  void addCount(uint64_t N) { Counter += N; }
+  size_t getNumEdges() { return Edges.size(); }
   void dump();
   void collectLineCounts(FileInfo &FI);
 private:
@@ -217,7 +218,7 @@ typedef DenseMap<uint32_t, uint64_t> Lin
 class FileInfo {
 public:
   void addLineCount(StringRef Filename, uint32_t Line, uint64_t Count) {
-    LineInfo[Filename][Line-1] = Count;
+    LineInfo[Filename][Line-1] += Count;
   }
   void print(StringRef gcnoFile, StringRef gcdaFile);
 private:

Modified: llvm/trunk/lib/IR/GCOV.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/GCOV.cpp?rev=193299&r1=193298&r2=193299&view=diff
==============================================================================
--- llvm/trunk/lib/IR/GCOV.cpp (original)
+++ llvm/trunk/lib/IR/GCOV.cpp Wed Oct 23 20:51:04 2013
@@ -103,9 +103,18 @@ bool GCOVFunction::read(GCOVBuffer &Buff
 
   if (Format == GCOV::GCDA_402 || Format == GCOV::GCDA_404) {
     Buff.readArcTag();
+    uint32_t i = 0;
     uint32_t Count = Buff.readInt() / 2;
-    for (unsigned i = 0, e = Count; i != e; ++i) {
-      Blocks[i]->addCount(Buff.readInt64());
+
+    // This for loop adds the counts for each block. A second nested loop is
+    // required to combine the edge counts that are contained in the GCDA file.
+    for (uint32_t Line = 0; i < Count; ++Line) {
+      GCOVBlock &Block = *Blocks[Line];
+      for (size_t Edge = 0, End = Block.getNumEdges(); Edge < End; ++Edge) {
+        assert(i < Count && "Unexpected number of Edges!");
+        Block.addCount(Buff.readInt64());
+        ++i;
+      }
     }
     return true;
   }





More information about the llvm-commits mailing list