[llvm] r197432 - llvm-cov: Added -u option for unconditional branch info.

Yuchen Wu yuchenericwu at hotmail.com
Mon Dec 16 14:14:03 PST 2013


Author: ywu
Date: Mon Dec 16 16:14:02 2013
New Revision: 197432

URL: http://llvm.org/viewvc/llvm-project?rev=197432&view=rev
Log:
llvm-cov: Added -u option for unconditional branch info.

Outputs branch information for unconditional branches in addition to
conditional branches. -b option must be enabled.

Also updated tests.

Added:
    llvm/trunk/test/tools/llvm-cov/Inputs/test_-a_-b_-u.cpp.gcov
    llvm/trunk/test/tools/llvm-cov/Inputs/test_-a_-b_-u.h.gcov
Modified:
    llvm/trunk/include/llvm/Support/GCOV.h
    llvm/trunk/lib/IR/GCOV.cpp
    llvm/trunk/test/tools/llvm-cov/llvm-cov.test
    llvm/trunk/tools/llvm-cov/llvm-cov.cpp

Modified: llvm/trunk/include/llvm/Support/GCOV.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/GCOV.h?rev=197432&r1=197431&r2=197432&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/GCOV.h (original)
+++ llvm/trunk/include/llvm/Support/GCOV.h Mon Dec 16 16:14:02 2013
@@ -36,9 +36,12 @@ namespace GCOV {
 
 /// GCOVOptions - A struct for passing gcov options between functions.
 struct GCOVOptions {
-  GCOVOptions(bool A, bool B): AllBlocks(A), BranchProb(B) {}
+  GCOVOptions(bool A, bool B, bool U) :
+    AllBlocks(A), BranchProb(B), UncondBranch(U) {}
+
   bool AllBlocks;
   bool BranchProb;
+  bool UncondBranch;
 };
 
 /// GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific
@@ -365,7 +368,9 @@ public:
   void printBlockInfo(raw_fd_ostream &OS, const GCOVBlock &Block,
                       uint32_t LineIndex, uint32_t &BlockNo) const;
   void printBranchInfo(raw_fd_ostream &OS, const GCOVBlock &Block,
-                       uint32_t LineIndex, uint32_t &EdgeNo) const;
+                       uint32_t &EdgeNo) const;
+  void printUncondBranchInfo(raw_fd_ostream &OS, uint32_t &EdgeNo,
+                             uint64_t Count) const;
 private:
   const GCOVOptions &Options;
   StringMap<LineData> LineInfo;

Modified: llvm/trunk/lib/IR/GCOV.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/GCOV.cpp?rev=197432&r1=197431&r2=197432&view=diff
==============================================================================
--- llvm/trunk/lib/IR/GCOV.cpp (original)
+++ llvm/trunk/lib/IR/GCOV.cpp Mon Dec 16 16:14:02 2013
@@ -481,8 +481,13 @@ void FileInfo::print(StringRef GCNOFile,
             continue;
           if (Options.AllBlocks)
             printBlockInfo(OS, *Block, LineIndex, BlockNo);
-          if (Options.BranchProb)
-            printBranchInfo(OS, *Block, LineIndex, EdgeNo);
+          if (Options.BranchProb) {
+            size_t NumEdges = Block->getNumDstEdges();
+            if (NumEdges > 1)
+              printBranchInfo(OS, *Block, EdgeNo);
+            else if (Options.UncondBranch && NumEdges == 1)
+              printUncondBranchInfo(OS, EdgeNo, (*Block->dst_begin())->Count);
+          }
         }
       }
     }
@@ -521,13 +526,9 @@ void FileInfo::printBlockInfo(raw_fd_ost
   OS << format("%5u-block %2u\n", LineIndex+1, BlockNo++);
 }
 
-/// printBranchInfo - Print branch probabilities for blocks that have
-/// conditional branches.
+/// printBranchInfo - Print conditional branch probabilities.
 void FileInfo::printBranchInfo(raw_fd_ostream &OS, const GCOVBlock &Block,
-                               uint32_t LineIndex, uint32_t &EdgeNo) const {
-  if (Block.getNumDstEdges() < 2)
-    return;
-
+                               uint32_t &EdgeNo) const {
   SmallVector<uint64_t, 16> BranchCounts;
   uint64_t TotalCounts = 0;
   for (GCOVBlock::EdgeIterator I = Block.dst_begin(), E = Block.dst_end();
@@ -546,3 +547,12 @@ void FileInfo::printBranchInfo(raw_fd_os
       OS << format("branch %2u never executed\n", EdgeNo++);
   }
 }
+
+/// printUncondBranchInfo - Print unconditional branch probabilities.
+void FileInfo::printUncondBranchInfo(raw_fd_ostream &OS, uint32_t &EdgeNo,
+                                     uint64_t Count) const {
+  if (Count)
+    OS << format("unconditional %2u taken 100%%\n", EdgeNo++);
+  else
+    OS << format("unconditional %2u never executed\n", EdgeNo++);
+}

Added: llvm/trunk/test/tools/llvm-cov/Inputs/test_-a_-b_-u.cpp.gcov
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-cov/Inputs/test_-a_-b_-u.cpp.gcov?rev=197432&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-cov/Inputs/test_-a_-b_-u.cpp.gcov (added)
+++ llvm/trunk/test/tools/llvm-cov/Inputs/test_-a_-b_-u.cpp.gcov Mon Dec 16 16:14:02 2013
@@ -0,0 +1,160 @@
+        -:    0:Source:test.cpp
+        -:    0:Graph:test.gcno
+        -:    0:Data:test.gcda
+        -:    0:Runs:2
+        -:    0:Programs:1
+        -:    1:#include "test.h"
+        -:    2:#include <cstdlib>
+        -:    3:
+        -:    4:bool on = false;
+        -:    5:int len = 42;
+        -:    6:double grid[10][10] = {0};
+        -:    7:const char * hello = "world";
+        -:    8:const char * world = "hello";
+        -:    9:
+function _ZN1A1BEv called 8589934592 returned 100% blocks executed 100%
+8589934592:   10:void A::B() {}
+8589934592:   10-block  0
+unconditional  0 taken 100%
+        -:   11:
+function _Z7uselessv called 0 returned 0% blocks executed 0%
+    #####:   12:void useless() {}
+    $$$$$:   12-block  0
+unconditional  0 never executed
+        -:   13:
+function _Z12more_uselessv called 0 returned 0% blocks executed 0%
+        -:   14:double more_useless() {
+    #####:   15:  return 0;
+    $$$$$:   15-block  0
+unconditional  0 never executed
+        -:   16:}
+        -:   17:
+function _Z3foov called 2 returned 100% blocks executed 100%
+        -:   18:int foo() {
+        2:   19:  on = true;
+        2:   20:  return 3;
+        2:   20-block  0
+unconditional  0 taken 100%
+        -:   21:}
+        -:   22:
+function _Z3barv called 0 returned 0% blocks executed 0%
+        -:   23:int bar() {
+    #####:   24:  len--;
+    #####:   25:  return foo() + 45;
+    $$$$$:   25-block  0
+unconditional  0 never executed
+        -:   26:}
+        -:   27:
+function _Z6assignii called 8 returned 100% blocks executed 100%
+        8:   28:void assign(int ii, int jj) {
+        8:   29:  grid[ii][jj] = (ii+1) * (jj+1);
+        8:   30:}
+        8:   30-block  0
+unconditional  0 taken 100%
+        -:   31:
+function _Z15initialize_gridv called 2 returned 100% blocks executed 100%
+        -:   32:void initialize_grid() {
+        6:   33:  for (int ii = 0; ii < 2; ii++)
+        2:   33-block  0
+unconditional  0 taken 100%
+        6:   33-block  1
+branch  1 taken 67%
+branch  2 taken 33%
+        4:   33-block  2
+unconditional  3 taken 100%
+       12:   34:    for (int jj = 0; jj < 2; jj++)
+        4:   34-block  0
+unconditional  0 taken 100%
+       12:   34-block  1
+branch  1 taken 67%
+branch  2 taken 33%
+        8:   34-block  2
+unconditional  3 taken 100%
+        8:   35:      assign(ii, jj);
+        8:   35-block  0
+unconditional  0 taken 100%
+        4:   35-block  1
+unconditional  1 taken 100%
+        2:   36:}
+        2:   36-block  0
+unconditional  0 taken 100%
+        -:   37:
+function main called 2 returned 100% blocks executed 94%
+        -:   38:int main() {
+        2:   39:  initialize_grid();
+        -:   40:
+        2:   41:  int a = 2;
+        2:   42:  on = rand() % 2;
+        2:   43:  if (on) {
+        2:   43-block  0
+branch  0 taken 100%
+branch  1 taken 0%
+        2:   44:    foo();
+        2:   45:    ++a;
+        2:   46:  } else {
+        2:   46-block  0
+unconditional  0 taken 100%
+    #####:   47:    bar();
+    #####:   48:    a += rand();
+    $$$$$:   48-block  0
+unconditional  0 never executed
+        -:   49:  }
+        -:   50:
+       22:   51:  for (int ii = 0; ii < 10; ++ii) {
+        2:   51-block  0
+unconditional  0 taken 100%
+       22:   51-block  1
+branch  1 taken 91%
+branch  2 taken 9%
+       20:   51-block  2
+unconditional  3 taken 100%
+       20:   52:    switch (rand() % 5) {
+       20:   52-block  0
+branch  0 taken 20%
+branch  1 taken 0%
+branch  2 taken 10%
+branch  3 taken 30%
+branch  4 taken 40%
+        -:   53:      case 0:
+        4:   54:        a += rand();
+        4:   55:        break;
+        4:   55-block  0
+unconditional  0 taken 100%
+        -:   56:      case 1:
+        -:   57:      case 2:
+        2:   58:        a += rand() / rand();
+        2:   59:        break;
+        2:   59-block  0
+unconditional  0 taken 100%
+        -:   60:      case 3:
+        6:   61:        a -= rand();
+        6:   62:        break;
+        6:   62-block  0
+unconditional  0 taken 100%
+        -:   63:      default:
+        8:   64:        a = -1;
+        8:   65:    }
+        8:   65-block  0
+unconditional  0 taken 100%
+       20:   66:  }
+       20:   66-block  0
+unconditional  0 taken 100%
+        -:   67:
+        2:   68:  A thing;
+8589934594:   69:  for (uint64_t ii = 0; ii < 4294967296; ++ii)
+        2:   69-block  0
+unconditional  0 taken 100%
+8589934594:   69-block  1
+branch  1 taken 99%
+branch  2 taken 1%
+8589934592:   69-block  2
+unconditional  3 taken 100%
+8589934592:   70:    thing.B();
+8589934592:   70-block  0
+unconditional  0 taken 100%
+        -:   71:
+        2:   72:  return a + 8 + grid[2][3] + len;
+        2:   72-block  0
+unconditional  0 taken 100%
+        -:   73:  return more_useless();
+        -:   74:}

Added: llvm/trunk/test/tools/llvm-cov/Inputs/test_-a_-b_-u.h.gcov
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-cov/Inputs/test_-a_-b_-u.h.gcov?rev=197432&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-cov/Inputs/test_-a_-b_-u.h.gcov (added)
+++ llvm/trunk/test/tools/llvm-cov/Inputs/test_-a_-b_-u.h.gcov Mon Dec 16 16:14:02 2013
@@ -0,0 +1,14 @@
+        -:    0:Source:./test.h
+        -:    0:Graph:test.gcno
+        -:    0:Data:test.gcda
+        -:    0:Runs:2
+        -:    0:Programs:1
+function _ZN1AC1Ev called 2 returned 100% blocks executed 100%
+function _ZN1AC2Ev called 2 returned 100% blocks executed 100%
+        2:    1:struct A {
+        2:    1-block  0
+unconditional  0 taken 100%
+        2:    1-block  1
+unconditional  1 taken 100%
+        -:    2:  virtual void B();
+        -:    3:};

Modified: llvm/trunk/test/tools/llvm-cov/llvm-cov.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-cov/llvm-cov.test?rev=197432&r1=197431&r2=197432&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-cov/llvm-cov.test (original)
+++ llvm/trunk/test/tools/llvm-cov/llvm-cov.test Mon Dec 16 16:14:02 2013
@@ -17,6 +17,10 @@ RUN: llvm-cov -gcno=test.gcno -gcda=test
 RUN: diff -aub test_-a_-b.cpp.gcov test.cpp.gcov
 RUN: diff -aub test_-a_-b.h.gcov test.h.gcov
 
+RUN: llvm-cov -gcno=test.gcno -gcda=test.gcda -a -b -u
+RUN: diff -aub test_-a_-b_-u.cpp.gcov test.cpp.gcov
+RUN: diff -aub test_-a_-b_-u.h.gcov test.h.gcov
+
 RUN: not llvm-cov -gcno=test_read_fail.gcno -gcda=test.gcda
 
 RUN: not llvm-cov -gcno=test.gcno -gcda=test_file_checksum_fail.gcda

Modified: llvm/trunk/tools/llvm-cov/llvm-cov.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-cov/llvm-cov.cpp?rev=197432&r1=197431&r2=197432&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-cov/llvm-cov.cpp (original)
+++ llvm/trunk/tools/llvm-cov/llvm-cov.cpp Mon Dec 16 16:14:02 2013
@@ -36,6 +36,10 @@ AllBlocks("a", cl::init(false), cl::desc
 static cl::opt<bool>
 BranchProb("b", cl::init(false), cl::desc("display branch info"));
 
+static cl::opt<bool>
+UncondBranch("u", cl::init(false), cl::desc("display unconditional branch info \
+                                             (only works with -b)"));
+
 //===----------------------------------------------------------------------===//
 int main(int argc, char **argv) {
   // Print a stack trace if we signal out.
@@ -76,7 +80,7 @@ int main(int argc, char **argv) {
   if (DumpGCOV)
     GF.dump();
 
-  GCOVOptions Options(AllBlocks, BranchProb);
+  GCOVOptions Options(AllBlocks, BranchProb, UncondBranch);
   FileInfo FI(Options);
   GF.collectLineCounts(FI);
   FI.print(InputGCNO, InputGCDA);





More information about the llvm-commits mailing list