[PATCH] llvm-cov: Added -c option for branch counts.
Justin Bogner
mail at justinbogner.com
Thu Dec 12 18:43:02 PST 2013
Yuchen Wu <yuchenericwu at hotmail.com> writes:
> From f23dca16abbd5bff01df70346cdc2286613125d6 Mon Sep 17 00:00:00 2001
> From: Yuchen Wu <yuchen_wu at apple.com>
> Date: Thu, 12 Dec 2013 12:18:49 -0800
> Subject: [PATCH 2/5] llvm-cov: Added -c option for branch counts.
>
> This will cause llvm-cov to output branch counts instead of branch
> probabilities. -b must be enabled.
>
> Also updated tests.
> ---
> include/llvm/Support/GCOV.h | 7 +-
> lib/IR/GCOV.cpp | 33 +++--
> .../llvm-cov/Inputs/test_-a_-b_-c_-u.cpp.gcov | 160 +++++++++++++++++++++
> test/tools/llvm-cov/Inputs/test_-a_-b_-c_-u.h.gcov | 14 ++
> test/tools/llvm-cov/llvm-cov.test | 4 +
> tools/llvm-cov/llvm-cov.cpp | 10 +-
> 6 files changed, 213 insertions(+), 15 deletions(-)
> create mode 100644 test/tools/llvm-cov/Inputs/test_-a_-b_-c_-u.cpp.gcov
> create mode 100644 test/tools/llvm-cov/Inputs/test_-a_-b_-c_-u.h.gcov
>
> diff --git a/include/llvm/Support/GCOV.h b/include/llvm/Support/GCOV.h
> index 2e73bd9..c3018f1 100644
> --- a/include/llvm/Support/GCOV.h
> +++ b/include/llvm/Support/GCOV.h
> @@ -36,11 +36,12 @@ namespace GCOV {
>
> /// GCOVOptions - A struct for passing gcov options between functions.
> struct GCOVOptions {
> - GCOVOptions(bool A, bool B, bool U) :
> - AllBlocks(A), BranchProb(B), UncondBranch(U) {}
> + GCOVOptions(bool A, bool B, bool C, bool U) :
> + AllBlocks(A), BranchInfo(B), BranchCount(C), UncondBranch(U) {}
>
> bool AllBlocks;
> - bool BranchProb;
> + bool BranchInfo;
> + bool BranchCount;
> bool UncondBranch;
> };
>
> diff --git a/lib/IR/GCOV.cpp b/lib/IR/GCOV.cpp
> index 7e9dc3d..7902f04 100644
> --- a/lib/IR/GCOV.cpp
> +++ b/lib/IR/GCOV.cpp
> @@ -431,7 +431,7 @@ void FileInfo::print(StringRef GCNOFile, StringRef GCDAFile) const {
>
> const LineData &Line = I->second;
> for (uint32_t LineIndex = 0; !AllLines.empty(); ++LineIndex) {
> - if (Options.BranchProb) {
> + if (Options.BranchInfo) {
> FunctionLines::const_iterator FuncsIt = Line.Functions.find(LineIndex);
> if (FuncsIt != Line.Functions.end())
> printFunctionSummary(OS, FuncsIt->second);
> @@ -481,7 +481,7 @@ void FileInfo::print(StringRef GCNOFile, StringRef GCDAFile) const {
> continue;
> if (Options.AllBlocks)
> printBlockInfo(OS, *Block, LineIndex, BlockNo);
> - if (Options.BranchProb)
> + if (Options.BranchInfo)
> printBranchInfo(OS, *Block, LineIndex, EdgeNo);
> }
> }
> @@ -538,17 +538,32 @@ void FileInfo::printBranchInfo(raw_fd_ostream &OS, const GCOVBlock &Block,
>
> for (SmallVectorImpl<uint64_t>::const_iterator I = BranchCounts.begin(),
> E = BranchCounts.end(); I != E; ++I) {
> - if (TotalCounts)
> - OS << format("branch %2u taken %u%%\n", EdgeNo++,
> - branchDiv(*I, TotalCounts));
> - else
> + if (TotalCounts) {
> + if (Options.BranchCount) {
> + // Print branch counts
> + OS << format("branch %2u taken ", EdgeNo++) << *I << "\n";
> + } else {
> + // Print branch probabilities
> + OS << format("branch %2u taken %u%%\n", EdgeNo++,
> + branchDiv(*I, TotalCounts));
> + }
> + } else {
> OS << format("branch %2u never executed\n", EdgeNo++);
> + }
> }
> } else if (Options.UncondBranch && NumEdges == 1) {
> // Print unconditional branch info.
> - if ((*Block.dst_begin())->Count)
> - OS << format("unconditional %2u taken 100%%\n", EdgeNo++);
> - else
> + uint64_t Count = (*Block.dst_begin())->Count;
> + if (Count) {
> + if (Options.BranchCount) {
> + // Print branch counts
> + OS << format("unconditional %2u taken ", EdgeNo++) << Count << "\n";
> + } else {
> + // Print branch probabilities
> + OS << format("unconditional %2u taken 100%%\n", EdgeNo++);
> + }
> + } else {
> OS << format("unconditional %2u never executed\n", EdgeNo++);
> + }
These two blocks are a bit redundant and they're starting to get a bit
ugly with the chain of conditions. Would it be better if we hid the
conditions and wrote something like these?
OS << format("branch %2u ", EdgeNo++) <<
formatBranchInfo(*I, TotalCounts) << "\n";
// ...
OS << format("unconditional %2u ", EdgeNo++) <<
formatBranchInfo(Count, Count) << "\n";
> }
> }
> diff --git a/test/tools/llvm-cov/Inputs/test_-a_-b_-c_-u.cpp.gcov b/test/tools/llvm-cov/Inputs/test_-a_-b_-c_-u.cpp.gcov
> new file mode 100644
> index 0000000..cc5940f
> --- /dev/null
> +++ b/test/tools/llvm-cov/Inputs/test_-a_-b_-c_-u.cpp.gcov
> @@ -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 8589934592
> + -: 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 2
> + -: 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 8
> + -: 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 2
> + 6: 33-block 1
> +branch 1 taken 4
> +branch 2 taken 2
> + 4: 33-block 2
> +unconditional 3 taken 4
> + 12: 34: for (int jj = 0; jj < 2; jj++)
> + 4: 34-block 0
> +unconditional 0 taken 4
> + 12: 34-block 1
> +branch 1 taken 8
> +branch 2 taken 4
> + 8: 34-block 2
> +unconditional 3 taken 8
> + 8: 35: assign(ii, jj);
> + 8: 35-block 0
> +unconditional 0 taken 8
> + 4: 35-block 1
> +unconditional 1 taken 4
> + 2: 36:}
> + 2: 36-block 0
> +unconditional 0 taken 2
> + -: 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 2
> +branch 1 taken 0
> + 2: 44: foo();
> + 2: 45: ++a;
> + 2: 46: } else {
> + 2: 46-block 0
> +unconditional 0 taken 2
> + #####: 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 2
> + 22: 51-block 1
> +branch 1 taken 20
> +branch 2 taken 2
> + 20: 51-block 2
> +unconditional 3 taken 20
> + 20: 52: switch (rand() % 5) {
> + 20: 52-block 0
> +branch 0 taken 4
> +branch 1 taken 0
> +branch 2 taken 2
> +branch 3 taken 6
> +branch 4 taken 8
> + -: 53: case 0:
> + 4: 54: a += rand();
> + 4: 55: break;
> + 4: 55-block 0
> +unconditional 0 taken 4
> + -: 56: case 1:
> + -: 57: case 2:
> + 2: 58: a += rand() / rand();
> + 2: 59: break;
> + 2: 59-block 0
> +unconditional 0 taken 2
> + -: 60: case 3:
> + 6: 61: a -= rand();
> + 6: 62: break;
> + 6: 62-block 0
> +unconditional 0 taken 6
> + -: 63: default:
> + 8: 64: a = -1;
> + 8: 65: }
> + 8: 65-block 0
> +unconditional 0 taken 8
> + 20: 66: }
> + 20: 66-block 0
> +unconditional 0 taken 20
> + -: 67:
> + 2: 68: A thing;
> +8589934594: 69: for (uint64_t ii = 0; ii < 4294967296; ++ii)
> + 2: 69-block 0
> +unconditional 0 taken 2
> +8589934594: 69-block 1
> +branch 1 taken 8589934592
> +branch 2 taken 2
> +8589934592: 69-block 2
> +unconditional 3 taken 8589934592
> +8589934592: 70: thing.B();
> +8589934592: 70-block 0
> +unconditional 0 taken 8589934592
> + -: 71:
> + 2: 72: return a + 8 + grid[2][3] + len;
> + 2: 72-block 0
> +unconditional 0 taken 2
> + -: 73: return more_useless();
> + -: 74:}
> diff --git a/test/tools/llvm-cov/Inputs/test_-a_-b_-c_-u.h.gcov b/test/tools/llvm-cov/Inputs/test_-a_-b_-c_-u.h.gcov
> new file mode 100644
> index 0000000..840324e
> --- /dev/null
> +++ b/test/tools/llvm-cov/Inputs/test_-a_-b_-c_-u.h.gcov
> @@ -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 2
> + 2: 1-block 1
> +unconditional 1 taken 2
> + -: 2: virtual void B();
> + -: 3:};
> diff --git a/test/tools/llvm-cov/llvm-cov.test b/test/tools/llvm-cov/llvm-cov.test
> index c436c1a..02b8a3d 100644
> --- a/test/tools/llvm-cov/llvm-cov.test
> +++ b/test/tools/llvm-cov/llvm-cov.test
> @@ -21,6 +21,10 @@ 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: llvm-cov -gcno=test.gcno -gcda=test.gcda -a -b -c -u
> +RUN: diff -aub test_-a_-b_-c_-u.cpp.gcov test.cpp.gcov
> +RUN: diff -aub test_-a_-b_-c_-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
> diff --git a/tools/llvm-cov/llvm-cov.cpp b/tools/llvm-cov/llvm-cov.cpp
> index cd6ba18..5372317 100644
> --- a/tools/llvm-cov/llvm-cov.cpp
> +++ b/tools/llvm-cov/llvm-cov.cpp
> @@ -34,11 +34,15 @@ static cl::opt<bool>
> AllBlocks("a", cl::init(false), cl::desc("display all block info"));
>
> static cl::opt<bool>
> -BranchProb("b", cl::init(false), cl::desc("display branch info"));
> +BranchInfo("b", cl::init(false), cl::desc("display branch info"));
> +
> +static cl::opt<bool>
> +BranchCount("c", cl::init(false), cl::desc("display branch counts instead of \
> + probabilities (requires -b)"));
>
> static cl::opt<bool>
> UncondBranch("u", cl::init(false), cl::desc("display unconditional branch info \
> - (only works with -b)"));
> + (requires -b)"));
>
> //===----------------------------------------------------------------------===//
> int main(int argc, char **argv) {
> @@ -80,7 +84,7 @@ int main(int argc, char **argv) {
> if (DumpGCOV)
> GF.dump();
>
> - GCOVOptions Options(AllBlocks, BranchProb, UncondBranch);
> + GCOVOptions Options(AllBlocks, BranchInfo, BranchCount, UncondBranch);
> FileInfo FI(Options);
> GF.collectLineCounts(FI);
> FI.print(InputGCNO, InputGCDA);
More information about the llvm-commits
mailing list