Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

Commit 89452cf

Browse files
committed
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@197432 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent e774912 commit 89452cf

File tree

6 files changed

+208
-11
lines changed

6 files changed

+208
-11
lines changed

Diff for: include/llvm/Support/GCOV.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ namespace GCOV {
3636

3737
/// GCOVOptions - A struct for passing gcov options between functions.
3838
struct GCOVOptions {
39-
GCOVOptions(bool A, bool B): AllBlocks(A), BranchProb(B) {}
39+
GCOVOptions(bool A, bool B, bool U) :
40+
AllBlocks(A), BranchProb(B), UncondBranch(U) {}
41+
4042
bool AllBlocks;
4143
bool BranchProb;
44+
bool UncondBranch;
4245
};
4346

4447
/// GCOVBuffer - A wrapper around MemoryBuffer to provide GCOV specific
@@ -365,7 +368,9 @@ class FileInfo {
365368
void printBlockInfo(raw_fd_ostream &OS, const GCOVBlock &Block,
366369
uint32_t LineIndex, uint32_t &BlockNo) const;
367370
void printBranchInfo(raw_fd_ostream &OS, const GCOVBlock &Block,
368-
uint32_t LineIndex, uint32_t &EdgeNo) const;
371+
uint32_t &EdgeNo) const;
372+
void printUncondBranchInfo(raw_fd_ostream &OS, uint32_t &EdgeNo,
373+
uint64_t Count) const;
369374
private:
370375
const GCOVOptions &Options;
371376
StringMap<LineData> LineInfo;

Diff for: lib/IR/GCOV.cpp

+18-8
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,13 @@ void FileInfo::print(StringRef GCNOFile, StringRef GCDAFile) const {
481481
continue;
482482
if (Options.AllBlocks)
483483
printBlockInfo(OS, *Block, LineIndex, BlockNo);
484-
if (Options.BranchProb)
485-
printBranchInfo(OS, *Block, LineIndex, EdgeNo);
484+
if (Options.BranchProb) {
485+
size_t NumEdges = Block->getNumDstEdges();
486+
if (NumEdges > 1)
487+
printBranchInfo(OS, *Block, EdgeNo);
488+
else if (Options.UncondBranch && NumEdges == 1)
489+
printUncondBranchInfo(OS, EdgeNo, (*Block->dst_begin())->Count);
490+
}
486491
}
487492
}
488493
}
@@ -521,13 +526,9 @@ void FileInfo::printBlockInfo(raw_fd_ostream &OS, const GCOVBlock &Block,
521526
OS << format("%5u-block %2u\n", LineIndex+1, BlockNo++);
522527
}
523528

524-
/// printBranchInfo - Print branch probabilities for blocks that have
525-
/// conditional branches.
529+
/// printBranchInfo - Print conditional branch probabilities.
526530
void FileInfo::printBranchInfo(raw_fd_ostream &OS, const GCOVBlock &Block,
527-
uint32_t LineIndex, uint32_t &EdgeNo) const {
528-
if (Block.getNumDstEdges() < 2)
529-
return;
530-
531+
uint32_t &EdgeNo) const {
531532
SmallVector<uint64_t, 16> BranchCounts;
532533
uint64_t TotalCounts = 0;
533534
for (GCOVBlock::EdgeIterator I = Block.dst_begin(), E = Block.dst_end();
@@ -546,3 +547,12 @@ void FileInfo::printBranchInfo(raw_fd_ostream &OS, const GCOVBlock &Block,
546547
OS << format("branch %2u never executed\n", EdgeNo++);
547548
}
548549
}
550+
551+
/// printUncondBranchInfo - Print unconditional branch probabilities.
552+
void FileInfo::printUncondBranchInfo(raw_fd_ostream &OS, uint32_t &EdgeNo,
553+
uint64_t Count) const {
554+
if (Count)
555+
OS << format("unconditional %2u taken 100%%\n", EdgeNo++);
556+
else
557+
OS << format("unconditional %2u never executed\n", EdgeNo++);
558+
}

Diff for: test/tools/llvm-cov/Inputs/test_-a_-b_-u.cpp.gcov

+160
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
-: 0:Source:test.cpp
2+
-: 0:Graph:test.gcno
3+
-: 0:Data:test.gcda
4+
-: 0:Runs:2
5+
-: 0:Programs:1
6+
-: 1:#include "test.h"
7+
-: 2:#include <cstdlib>
8+
-: 3:
9+
-: 4:bool on = false;
10+
-: 5:int len = 42;
11+
-: 6:double grid[10][10] = {0};
12+
-: 7:const char * hello = "world";
13+
-: 8:const char * world = "hello";
14+
-: 9:
15+
function _ZN1A1BEv called 8589934592 returned 100% blocks executed 100%
16+
8589934592: 10:void A::B() {}
17+
8589934592: 10-block 0
18+
unconditional 0 taken 100%
19+
-: 11:
20+
function _Z7uselessv called 0 returned 0% blocks executed 0%
21+
#####: 12:void useless() {}
22+
$$$$$: 12-block 0
23+
unconditional 0 never executed
24+
-: 13:
25+
function _Z12more_uselessv called 0 returned 0% blocks executed 0%
26+
-: 14:double more_useless() {
27+
#####: 15: return 0;
28+
$$$$$: 15-block 0
29+
unconditional 0 never executed
30+
-: 16:}
31+
-: 17:
32+
function _Z3foov called 2 returned 100% blocks executed 100%
33+
-: 18:int foo() {
34+
2: 19: on = true;
35+
2: 20: return 3;
36+
2: 20-block 0
37+
unconditional 0 taken 100%
38+
-: 21:}
39+
-: 22:
40+
function _Z3barv called 0 returned 0% blocks executed 0%
41+
-: 23:int bar() {
42+
#####: 24: len--;
43+
#####: 25: return foo() + 45;
44+
$$$$$: 25-block 0
45+
unconditional 0 never executed
46+
-: 26:}
47+
-: 27:
48+
function _Z6assignii called 8 returned 100% blocks executed 100%
49+
8: 28:void assign(int ii, int jj) {
50+
8: 29: grid[ii][jj] = (ii+1) * (jj+1);
51+
8: 30:}
52+
8: 30-block 0
53+
unconditional 0 taken 100%
54+
-: 31:
55+
function _Z15initialize_gridv called 2 returned 100% blocks executed 100%
56+
-: 32:void initialize_grid() {
57+
6: 33: for (int ii = 0; ii < 2; ii++)
58+
2: 33-block 0
59+
unconditional 0 taken 100%
60+
6: 33-block 1
61+
branch 1 taken 67%
62+
branch 2 taken 33%
63+
4: 33-block 2
64+
unconditional 3 taken 100%
65+
12: 34: for (int jj = 0; jj < 2; jj++)
66+
4: 34-block 0
67+
unconditional 0 taken 100%
68+
12: 34-block 1
69+
branch 1 taken 67%
70+
branch 2 taken 33%
71+
8: 34-block 2
72+
unconditional 3 taken 100%
73+
8: 35: assign(ii, jj);
74+
8: 35-block 0
75+
unconditional 0 taken 100%
76+
4: 35-block 1
77+
unconditional 1 taken 100%
78+
2: 36:}
79+
2: 36-block 0
80+
unconditional 0 taken 100%
81+
-: 37:
82+
function main called 2 returned 100% blocks executed 94%
83+
-: 38:int main() {
84+
2: 39: initialize_grid();
85+
-: 40:
86+
2: 41: int a = 2;
87+
2: 42: on = rand() % 2;
88+
2: 43: if (on) {
89+
2: 43-block 0
90+
branch 0 taken 100%
91+
branch 1 taken 0%
92+
2: 44: foo();
93+
2: 45: ++a;
94+
2: 46: } else {
95+
2: 46-block 0
96+
unconditional 0 taken 100%
97+
#####: 47: bar();
98+
#####: 48: a += rand();
99+
$$$$$: 48-block 0
100+
unconditional 0 never executed
101+
-: 49: }
102+
-: 50:
103+
22: 51: for (int ii = 0; ii < 10; ++ii) {
104+
2: 51-block 0
105+
unconditional 0 taken 100%
106+
22: 51-block 1
107+
branch 1 taken 91%
108+
branch 2 taken 9%
109+
20: 51-block 2
110+
unconditional 3 taken 100%
111+
20: 52: switch (rand() % 5) {
112+
20: 52-block 0
113+
branch 0 taken 20%
114+
branch 1 taken 0%
115+
branch 2 taken 10%
116+
branch 3 taken 30%
117+
branch 4 taken 40%
118+
-: 53: case 0:
119+
4: 54: a += rand();
120+
4: 55: break;
121+
4: 55-block 0
122+
unconditional 0 taken 100%
123+
-: 56: case 1:
124+
-: 57: case 2:
125+
2: 58: a += rand() / rand();
126+
2: 59: break;
127+
2: 59-block 0
128+
unconditional 0 taken 100%
129+
-: 60: case 3:
130+
6: 61: a -= rand();
131+
6: 62: break;
132+
6: 62-block 0
133+
unconditional 0 taken 100%
134+
-: 63: default:
135+
8: 64: a = -1;
136+
8: 65: }
137+
8: 65-block 0
138+
unconditional 0 taken 100%
139+
20: 66: }
140+
20: 66-block 0
141+
unconditional 0 taken 100%
142+
-: 67:
143+
2: 68: A thing;
144+
8589934594: 69: for (uint64_t ii = 0; ii < 4294967296; ++ii)
145+
2: 69-block 0
146+
unconditional 0 taken 100%
147+
8589934594: 69-block 1
148+
branch 1 taken 99%
149+
branch 2 taken 1%
150+
8589934592: 69-block 2
151+
unconditional 3 taken 100%
152+
8589934592: 70: thing.B();
153+
8589934592: 70-block 0
154+
unconditional 0 taken 100%
155+
-: 71:
156+
2: 72: return a + 8 + grid[2][3] + len;
157+
2: 72-block 0
158+
unconditional 0 taken 100%
159+
-: 73: return more_useless();
160+
-: 74:}

Diff for: test/tools/llvm-cov/Inputs/test_-a_-b_-u.h.gcov

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-: 0:Source:./test.h
2+
-: 0:Graph:test.gcno
3+
-: 0:Data:test.gcda
4+
-: 0:Runs:2
5+
-: 0:Programs:1
6+
function _ZN1AC1Ev called 2 returned 100% blocks executed 100%
7+
function _ZN1AC2Ev called 2 returned 100% blocks executed 100%
8+
2: 1:struct A {
9+
2: 1-block 0
10+
unconditional 0 taken 100%
11+
2: 1-block 1
12+
unconditional 1 taken 100%
13+
-: 2: virtual void B();
14+
-: 3:};

Diff for: test/tools/llvm-cov/llvm-cov.test

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ RUN: llvm-cov -gcno=test.gcno -gcda=test.gcda -a -b
1717
RUN: diff -aub test_-a_-b.cpp.gcov test.cpp.gcov
1818
RUN: diff -aub test_-a_-b.h.gcov test.h.gcov
1919

20+
RUN: llvm-cov -gcno=test.gcno -gcda=test.gcda -a -b -u
21+
RUN: diff -aub test_-a_-b_-u.cpp.gcov test.cpp.gcov
22+
RUN: diff -aub test_-a_-b_-u.h.gcov test.h.gcov
23+
2024
RUN: not llvm-cov -gcno=test_read_fail.gcno -gcda=test.gcda
2125

2226
RUN: not llvm-cov -gcno=test.gcno -gcda=test_file_checksum_fail.gcda

Diff for: tools/llvm-cov/llvm-cov.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ AllBlocks("a", cl::init(false), cl::desc("display all block info"));
3636
static cl::opt<bool>
3737
BranchProb("b", cl::init(false), cl::desc("display branch info"));
3838

39+
static cl::opt<bool>
40+
UncondBranch("u", cl::init(false), cl::desc("display unconditional branch info \
41+
(only works with -b)"));
42+
3943
//===----------------------------------------------------------------------===//
4044
int main(int argc, char **argv) {
4145
// Print a stack trace if we signal out.
@@ -76,7 +80,7 @@ int main(int argc, char **argv) {
7680
if (DumpGCOV)
7781
GF.dump();
7882

79-
GCOVOptions Options(AllBlocks, BranchProb);
83+
GCOVOptions Options(AllBlocks, BranchProb, UncondBranch);
8084
FileInfo FI(Options);
8185
GF.collectLineCounts(FI);
8286
FI.print(InputGCNO, InputGCDA);

0 commit comments

Comments
 (0)