Skip to content

Commit 733acf4

Browse files
committed
[coverage] Mark branches with either counter is zero as folded
1 parent 50c8112 commit 733acf4

File tree

9 files changed

+61
-48
lines changed

9 files changed

+61
-48
lines changed

clang/lib/CodeGen/CoverageMappingGen.cpp

+19-8
Original file line numberDiff line numberDiff line change
@@ -1097,9 +1097,13 @@ struct CounterCoverageMappingBuilder
10971097
}
10981098

10991099
/// Determine whether the given condition can be constant folded.
1100-
bool ConditionFoldsToBool(const Expr *Cond) {
1100+
bool ConditionFoldsToBool(const Expr *Cond, bool &ResultBool) {
11011101
Expr::EvalResult Result;
1102-
return (Cond->EvaluateAsInt(Result, CVM.getCodeGenModule().getContext()));
1102+
if (Cond->EvaluateAsInt(Result, CVM.getCodeGenModule().getContext())) {
1103+
ResultBool = Result.Val.getInt().getBoolValue();
1104+
return true;
1105+
}
1106+
return false;
11031107
}
11041108

11051109
/// Create a Branch Region around an instrumentable condition for coverage
@@ -1126,15 +1130,22 @@ struct CounterCoverageMappingBuilder
11261130
BranchParams = mcdc::BranchParameters{ID, Conds};
11271131

11281132
// If a condition can fold to true or false, the corresponding branch
1129-
// will be removed. Create a region with both counters hard-coded to
1130-
// zero. This allows us to visualize them in a special way.
1133+
// will be removed. Create a region with the relative counter hard-coded
1134+
// to zero. This allows us to visualize them in a special way.
11311135
// Alternatively, we can prevent any optimization done via
11321136
// constant-folding by ensuring that ConstantFoldsToSimpleInteger() in
11331137
// CodeGenFunction.c always returns false, but that is very heavy-handed.
1134-
if (ConditionFoldsToBool(C))
1135-
popRegions(pushRegion(Counter::getZero(), getStart(C), getEnd(C),
1136-
Counter::getZero(), BranchParams));
1137-
else
1138+
bool ConstantBool = false;
1139+
if (ConditionFoldsToBool(C, ConstantBool)) {
1140+
if (ConstantBool) {
1141+
popRegions(pushRegion(TrueCnt, getStart(C), getEnd(C),
1142+
Counter::getZero(), BranchParams));
1143+
} else {
1144+
popRegions(pushRegion(Counter::getZero(), getStart(C), getEnd(C),
1145+
FalseCnt, BranchParams));
1146+
}
1147+
1148+
} else
11381149
// Otherwise, create a region with the True counter and False counter.
11391150
popRegions(pushRegion(TrueCnt, getStart(C), getEnd(C), FalseCnt,
11401151
BranchParams));

clang/test/CoverageMapping/branch-constfolded.cpp

+20-20
Original file line numberDiff line numberDiff line change
@@ -5,94 +5,94 @@
55

66
// CHECK-LABEL: _Z6fand_0b:
77
bool fand_0(bool a) { // MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:20 = M:3, C:2
8-
return false && a; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:15 = 0, 0
8+
return false && a; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:15 = 0, (#0 - #1)
99
} // CHECK: Branch,File 0, [[@LINE-1]]:19 -> [[@LINE-1]]:20 = #2, (#1 - #2)
1010

1111
// CHECK-LABEL: _Z6fand_1b:
1212
bool fand_1(bool a) { // MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:19 = M:3, C:2
1313
return a && true; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:11 = #1, (#0 - #1)
14-
} // CHECK: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:19 = 0, 0
14+
} // CHECK: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:19 = #2, 0
1515

1616
// CHECK-LABEL: _Z6fand_2bb:
1717
bool fand_2(bool a, bool b) {// MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:25 = M:4, C:3
18-
return false && a && b; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:15 = 0, 0
18+
return false && a && b; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:15 = 0, (#0 - #3)
1919
} // CHECK: Branch,File 0, [[@LINE-1]]:19 -> [[@LINE-1]]:20 = #4, (#3 - #4)
2020
// CHECK: Branch,File 0, [[@LINE-2]]:24 -> [[@LINE-2]]:25 = #2, (#1 - #2)
2121

2222
// CHECK-LABEL: _Z6fand_3bb:
2323
bool fand_3(bool a, bool b) {// MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:24 = M:4, C:3
2424
return a && true && b; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:11 = #3, (#0 - #3)
25-
} // CHECK: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:19 = 0, 0
25+
} // CHECK: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:19 = #4, 0
2626
// CHECK: Branch,File 0, [[@LINE-2]]:23 -> [[@LINE-2]]:24 = #2, (#1 - #2)
2727

2828
// CHECK-LABEL: _Z6fand_4bb:
2929
bool fand_4(bool a, bool b) {// MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:25 = M:4, C:3
3030
return a && b && false; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:11 = #3, (#0 - #3)
3131
} // CHECK: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:16 = #4, (#3 - #4)
32-
// CHECK: Branch,File 0, [[@LINE-2]]:20 -> [[@LINE-2]]:25 = 0, 0
32+
// CHECK: Branch,File 0, [[@LINE-2]]:20 -> [[@LINE-2]]:25 = 0, (#1 - #2)
3333

3434
// CHECK-LABEL: _Z6fand_5b:
3535
bool fand_5(bool a) { // MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:23 = M:3, C:2
36-
return false && true; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:15 = 0, 0
37-
} // CHECK: Branch,File 0, [[@LINE-1]]:19 -> [[@LINE-1]]:23 = 0, 0
36+
return false && true; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:15 = 0, (#0 - #1)
37+
} // CHECK: Branch,File 0, [[@LINE-1]]:19 -> [[@LINE-1]]:23 = #2, 0
3838

3939
// CHECK-LABEL: _Z6fand_6b:
4040
bool fand_6(bool a) { // MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:19 = M:3, C:2
41-
return true && a; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:14 = 0, 0
41+
return true && a; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:14 = #1, 0
4242
} // CHECK: Branch,File 0, [[@LINE-1]]:18 -> [[@LINE-1]]:19 = #2, (#1 - #2)
4343

4444
// CHECK-LABEL: _Z6fand_7b:
4545
bool fand_7(bool a) { // MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:20 = M:3, C:2
4646
return a && false; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:11 = #1, (#0 - #1)
47-
} // CHECK: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:20 = 0, 0
47+
} // CHECK: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:20 = 0, (#1 - #2)
4848

4949
// CHECK-LABEL: _Z5for_0b:
5050
bool for_0(bool a) { // MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:19 = M:3, C:2
51-
return true || a; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:14 = 0, 0
51+
return true || a; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:14 = (#0 - #1), 0
5252
} // CHECK: Branch,File 0, [[@LINE-1]]:18 -> [[@LINE-1]]:19 = (#1 - #2), #2
5353

5454
// CHECK-LABEL: _Z5for_1b:
5555
bool for_1(bool a) { // MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:20 = M:3, C:2
5656
return a || false; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:11 = (#0 - #1), #1
57-
} // CHECK: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:20 = 0, 0
57+
} // CHECK: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:20 = 0, #2
5858

5959
// CHECK-LABEL: _Z5for_2bb:
6060
bool for_2(bool a, bool b) {// MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:24 = M:4, C:3
61-
return true || a || b; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:14 = 0, 0
61+
return true || a || b; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:14 = (#0 - #3), 0
6262
} // CHECK: Branch,File 0, [[@LINE-1]]:18 -> [[@LINE-1]]:19 = (#3 - #4), #4
6363
// CHECK: Branch,File 0, [[@LINE-2]]:23 -> [[@LINE-2]]:24 = (#1 - #2), #2
6464

6565
// CHECK-LABEL: _Z5for_3bb:
6666
bool for_3(bool a, bool b) {// MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:25 = M:4, C:3
6767
return a || false || b; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:11 = (#0 - #3), #3
68-
} // CHECK: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:20 = 0, 0
68+
} // CHECK: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:20 = 0, #4
6969
// CHECK: Branch,File 0, [[@LINE-2]]:24 -> [[@LINE-2]]:25 = (#1 - #2), #2
7070

7171
// CHECK-LABEL: _Z5for_4bb:
7272
bool for_4(bool a, bool b) {// MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:24 = M:4, C:3
7373
return a || b || true; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:11 = (#0 - #3), #3
7474
} // CHECK: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:16 = (#3 - #4), #4
75-
// CHECK: Branch,File 0, [[@LINE-2]]:20 -> [[@LINE-2]]:24 = 0, 0
75+
// CHECK: Branch,File 0, [[@LINE-2]]:20 -> [[@LINE-2]]:24 = (#1 - #2), 0
7676

7777
// CHECK-LABEL: _Z5for_5b:
7878
bool for_5(bool a) { // MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:23 = M:3, C:2
79-
return true || false; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:14 = 0, 0
80-
} // CHECK: Branch,File 0, [[@LINE-1]]:18 -> [[@LINE-1]]:23 = 0, 0
79+
return true || false; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:14 = (#0 - #1), 0
80+
} // CHECK: Branch,File 0, [[@LINE-1]]:18 -> [[@LINE-1]]:23 = 0, #2
8181

8282
// CHECK-LABEL: _Z5for_6b:
8383
bool for_6(bool a) { // MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:20 = M:3, C:2
84-
return false || a; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:15 = 0, 0
84+
return false || a; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:15 = 0, #1
8585
} // CHECK: Branch,File 0, [[@LINE-1]]:19 -> [[@LINE-1]]:20 = (#1 - #2), #2
8686

8787
// CHECK-LABEL: _Z5for_7b:
8888
bool for_7(bool a) { // MCDC: Decision,File 0, [[@LINE+1]]:10 -> [[@LINE+1]]:19 = M:3, C:2
8989
return a || true; // CHECK: Branch,File 0, [[@LINE]]:10 -> [[@LINE]]:11 = (#0 - #1), #1
90-
} // CHECK: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:19 = 0, 0
90+
} // CHECK: Branch,File 0, [[@LINE-1]]:15 -> [[@LINE-1]]:19 = (#1 - #2), 0
9191

9292
// CHECK-LABEL: _Z5for_8b:
9393
bool for_8(bool a) { // MCDC: Decision,File 0, [[@LINE+3]]:7 -> [[@LINE+3]]:20 = M:3, C:2
94-
// CHECK: Branch,File 0, [[@LINE+2]]:7 -> [[@LINE+2]]:11 = 0, 0
95-
// CHECK: Branch,File 0, [[@LINE+1]]:15 -> [[@LINE+1]]:20 = 0, 0
94+
// CHECK: Branch,File 0, [[@LINE+2]]:7 -> [[@LINE+2]]:11 = #2, 0
95+
// CHECK: Branch,File 0, [[@LINE+1]]:15 -> [[@LINE+1]]:20 = 0, (#2 - #3)
9696
if (true && false)
9797
return true;
9898
else

clang/test/CoverageMapping/if.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ struct S {
1414
// CHECK-LABEL: _Z3foov:
1515
// CHECK-NEXT: [[@LINE+3]]:12 -> [[@LINE+8]]:2 = #0
1616
// CHECK-NEXT: [[@LINE+3]]:15 -> [[@LINE+3]]:19 = #0
17-
// CHECK-NEXT: Branch,File 0, [[@LINE+2]]:15 -> [[@LINE+2]]:19 = 0, 0
17+
// CHECK-NEXT: Branch,File 0, [[@LINE+2]]:15 -> [[@LINE+2]]:19 = #2, 0
1818
void foo() { // CHECK-NEXT: Gap,File 0, [[@LINE+1]]:21 -> [[@LINE+1]]:22 = #2
1919
if (int j = true ? nop() // CHECK-NEXT: [[@LINE]]:22 -> [[@LINE]]:27 = #2
2020
: nop(); // CHECK-NEXT: [[@LINE]]:22 -> [[@LINE]]:27 = (#0 - #2)
@@ -168,7 +168,7 @@ int main() { // CHECK: File 0, [[@LINE]]:12 -> {{[0-9]+}}:2 =
168168
// GH-45481
169169
S s;
170170
s.the_prop = 0? 1 : 2; // CHECK-NEXT: File 0, [[@LINE]]:16 -> [[@LINE]]:17 = #0
171-
// CHECK-NEXT: Branch,File 0, [[@LINE-1]]:16 -> [[@LINE-1]]:17 = 0, 0
171+
// CHECK-NEXT: Branch,File 0, [[@LINE-1]]:16 -> [[@LINE-1]]:17 = 0, (#0 - #7)
172172
// CHECK-NEXT: Gap,File 0, [[@LINE-2]]:18 -> [[@LINE-2]]:19 = #7
173173
// CHECK-NEXT: File 0, [[@LINE-3]]:19 -> [[@LINE-3]]:20 = #7
174174
// CHECK-NEXT: File 0, [[@LINE-4]]:23 -> [[@LINE-4]]:24 = (#0 - #7)

clang/test/CoverageMapping/macro-expansion.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@
44
// CHECK: File 1, [[@LINE+7]]:12 -> [[@LINE+7]]:38 = #0
55
// CHECK-NEXT: File 1, [[@LINE+6]]:15 -> [[@LINE+6]]:28 = (#0 + #2)
66
// CHECK-NEXT: File 1, [[@LINE+5]]:21 -> [[@LINE+5]]:22 = (#0 + #2)
7-
// CHECK: Branch,File 1, [[@LINE+4]]:21 -> [[@LINE+4]]:22 = 0, 0
7+
// CHECK: Branch,File 1, [[@LINE+4]]:21 -> [[@LINE+4]]:22 = 0, ((#0 + #2) - #3)
88
// CHECK-NEXT: File 1, [[@LINE+3]]:24 -> [[@LINE+3]]:26 = #3
99
// CHECK-NEXT: File 1, [[@LINE+2]]:36 -> [[@LINE+2]]:37 = (#0 + #2)
10-
// CHECK-NEXT: Branch,File 1, [[@LINE+1]]:36 -> [[@LINE+1]]:37 = 0, 0
10+
// CHECK-NEXT: Branch,File 1, [[@LINE+1]]:36 -> [[@LINE+1]]:37 = 0, #0
1111
#define M1 do { if (0) {} } while (0)
1212
// CHECK-NEXT: File 2, [[@LINE+12]]:15 -> [[@LINE+12]]:41 = #0
1313
// CHECK-NEXT: File 2, [[@LINE+11]]:18 -> [[@LINE+11]]:31 = (#0 + #4)
1414
// CHECK-NEXT: File 2, [[@LINE+10]]:24 -> [[@LINE+10]]:25 = (#0 + #4)
1515
// CHECK: File 2, [[@LINE+9]]:27 -> [[@LINE+9]]:29 = #5
1616
// CHECK-NEXT: File 2, [[@LINE+8]]:39 -> [[@LINE+8]]:40 = (#0 + #4)
17-
// CHECK-NEXT: Branch,File 2, [[@LINE+7]]:39 -> [[@LINE+7]]:40 = 0, 0
17+
// CHECK-NEXT: Branch,File 2, [[@LINE+7]]:39 -> [[@LINE+7]]:40 = 0, #0
1818
// CHECK-NEXT: File 3, [[@LINE+6]]:15 -> [[@LINE+6]]:41 = #0
1919
// CHECK-NEXT: File 3, [[@LINE+5]]:18 -> [[@LINE+5]]:31 = (#0 + #6)
2020
// CHECK-NEXT: File 3, [[@LINE+4]]:24 -> [[@LINE+4]]:25 = (#0 + #6)
2121
// CHECK: File 3, [[@LINE+3]]:27 -> [[@LINE+3]]:29 = #7
2222
// CHECK-NEXT: File 3, [[@LINE+2]]:39 -> [[@LINE+2]]:40 = (#0 + #6)
23-
// CHECK-NEXT: Branch,File 3, [[@LINE+1]]:39 -> [[@LINE+1]]:40 = 0, 0
23+
// CHECK-NEXT: Branch,File 3, [[@LINE+1]]:39 -> [[@LINE+1]]:40 = 0, #0
2424
#define M2(x) do { if (x) {} } while (0)
2525
// CHECK-NEXT: File 4, [[@LINE+5]]:15 -> [[@LINE+5]]:38 = #0
2626
// CHECK-NEXT: File 4, [[@LINE+4]]:18 -> [[@LINE+4]]:28 = (#0 + #8)
2727
// CHECK-NEXT: Expansion,File 4, [[@LINE+3]]:20 -> [[@LINE+3]]:22 = (#0 + #8)
2828
// CHECK-NEXT: File 4, [[@LINE+2]]:36 -> [[@LINE+2]]:37 = (#0 + #8)
29-
// CHECK-NEXT: Branch,File 4, [[@LINE+1]]:36 -> [[@LINE+1]]:37 = 0, 0
29+
// CHECK-NEXT: Branch,File 4, [[@LINE+1]]:36 -> [[@LINE+1]]:37 = 0, #0
3030
#define M3(x) do { M2(x); } while (0)
3131
// CHECK-NEXT: File 5, [[@LINE+4]]:15 -> [[@LINE+4]]:27 = #0
3232
// CHECK-NEXT: File 5, [[@LINE+3]]:16 -> [[@LINE+3]]:19 = #0

clang/test/CoverageMapping/mcdc-scratch-space.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
// CHECK: builtin_macro0:
44
int builtin_macro0(int a) {
55
// CHECK: Decision,File 0, [[@LINE+1]]:11 -> [[@LINE+2]]:15 = M:3, C:2
6-
return (__LINE__ // CHECK: Branch,File 0, [[@LINE]]:11 -> [[@LINE]]:11 = 0, 0 [1,2,0]
6+
return (__LINE__ // CHECK: Branch,File 0, [[@LINE]]:11 -> [[@LINE]]:11 = #1, 0 [1,2,0]
77
&& a); // CHECK: Branch,File 0, [[@LINE]]:14 -> [[@LINE]]:15 = #2, (#1 - #2) [2,0,0]
88
}
99

1010
// CHECK: builtin_macro1:
1111
int builtin_macro1(int a) {
1212
// CHECK: Decision,File 0, [[@LINE+1]]:11 -> [[@LINE+2]]:22 = M:3, C:2
1313
return (a // CHECK: Branch,File 0, [[@LINE]]:11 -> [[@LINE]]:12 = (#0 - #1), #1 [1,0,2]
14-
|| __LINE__); // CHECK: Branch,File 0, [[@LINE]]:14 -> [[@LINE]]:14 = 0, 0 [2,0,0]
14+
|| __LINE__); // CHECK: Branch,File 0, [[@LINE]]:14 -> [[@LINE]]:14 = (#1 - #2), 0 [2,0,0]
1515
}
1616

1717
#define PRE(x) pre_##x

clang/test/CoverageMapping/mcdc-system-headers.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
int func0(int a) {
1818
// CHECK: Decision,File 0, [[@LINE+3]]:11 -> [[@LINE+3]]:21 = M:3, C:2
1919
// W_SYS: Expansion,File 0, [[@LINE+2]]:11 -> [[@LINE+2]]:16 = #0 (Expanded file = 1)
20-
// X_SYS: Branch,File 0, [[@LINE+1]]:11 -> [[@LINE+1]]:11 = 0, 0 [1,2,0]
20+
// X_SYS: Branch,File 0, [[@LINE+1]]:11 -> [[@LINE+1]]:11 = #1, 0 [1,2,0]
2121
return (CONST && a);
2222
// CHECK: Branch,File 0, [[@LINE-1]]:20 -> [[@LINE-1]]:21 = #2, (#1 - #2) [2,0,0]
23-
// W_SYS: Branch,File 1, [[@LINE-16]]:15 -> [[@LINE-16]]:17 = 0, 0 [1,2,0]
23+
// W_SYS: Branch,File 1, [[@LINE-16]]:15 -> [[@LINE-16]]:17 = #1, 0 [1,2,0]
2424
}
2525

2626
// CHECK: _Z5func1ii:

llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -719,9 +719,9 @@ struct FunctionRecord {
719719
Region.Kind == CounterMappingRegion::MCDCBranchRegion) {
720720
CountedBranchRegions.emplace_back(Region, Count, FalseCount,
721721
HasSingleByteCoverage);
722-
// If both counters are hard-coded to zero, then this region represents a
722+
// If either counter is hard-coded to zero, then this region represents a
723723
// constant-folded branch.
724-
if (Region.Count.isZero() && Region.FalseCount.isZero())
724+
if (Region.Count.isZero() || Region.FalseCount.isZero())
725725
CountedBranchRegions.back().Folded = true;
726726
return;
727727
}

llvm/lib/ProfileData/Coverage/CoverageMapping.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,7 @@ class MCDCRecordProcessor : NextIDsBuilder, mcdc::TVIdxBuilder {
506506
const auto &BranchParams = B->getBranchParams();
507507
PosToID[I] = BranchParams.ID;
508508
CondLoc[I] = B->startLoc();
509-
Folded[I++] = (B->Count.isZero() && B->FalseCount.isZero());
509+
Folded[I++] = (B->Count.isZero() || B->FalseCount.isZero());
510510
}
511511

512512
// Using Profile Bitmap from runtime, mark the executed test vectors.
@@ -611,6 +611,8 @@ static unsigned getMaxCounterID(const CounterMappingContext &Ctx,
611611
unsigned MaxCounterID = 0;
612612
for (const auto &Region : Record.MappingRegions) {
613613
MaxCounterID = std::max(MaxCounterID, Ctx.getMaxCounterID(Region.Count));
614+
MaxCounterID =
615+
std::max(MaxCounterID, Ctx.getMaxCounterID(Region.FalseCount));
614616
}
615617
return MaxCounterID;
616618
}

llvm/test/tools/llvm-cov/branch-c-general.test

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
// CHECK: Branch (103:9): [True: 9, False: 1]
4848

4949
// CHECK: switches()
50-
// CHECK: Branch (113:3): [True: 1, False: 0]
50+
// CHECK: Branch (113:3): [Folded - Ignored]
5151
// CHECK: Branch (117:63): [True: 15, False: 0]
5252
// CHECK: Branch (119:5): [True: 1, False: 14]
5353
// CHECK: Branch (120:11): [True: 0, False: 1]
@@ -57,7 +57,7 @@
5757
// CHECK: Branch (126:11): [True: 3, False: 0]
5858
// CHECK: Branch (128:5): [True: 4, False: 11]
5959
// CHECK: Branch (129:11): [True: 4, False: 0]
60-
// CHECK: Branch (131:7): [True: 4, False: 0]
60+
// CHECK: Branch (131:7): [Folded - Ignored]
6161
// CHECK: Branch (132:13): [True: 4, False: 0]
6262
// CHECK: Branch (136:5): [True: 5, False: 10]
6363
// CHECK: Branch (137:11): [True: 1, False: 4]
@@ -120,7 +120,7 @@
120120
// REPORT-NEXT: conditionals 24 0 100.00% 15 0 100.00% 16 2 87.50%
121121
// REPORT-NEXT: early_exits 20 4 80.00% 25 2 92.00% 16 6 62.50%
122122
// REPORT-NEXT: jumps 39 12 69.23% 48 2 95.83% 26 9 65.38%
123-
// REPORT-NEXT: switches 28 5 82.14% 38 4 89.47% 30 9 70.00%
123+
// REPORT-NEXT: switches 28 5 82.14% 38 4 89.47% 26 7 73.08%
124124
// REPORT-NEXT: big_switch 25 1 96.00% 32 0 100.00% 30 6 80.00%
125125
// REPORT-NEXT: boolean_operators 16 0 100.00% 13 0 100.00% 22 2 90.91%
126126
// REPORT-NEXT: boolop_loops 19 0 100.00% 14 0 100.00% 16 2 87.50%
@@ -129,12 +129,12 @@
129129
// REPORT-NEXT: main 1 0 100.00% 16 0 100.00% 0 0 0.00%
130130
// REPORT-NEXT: c-general.c:static_func 4 0 100.00% 4 0 100.00% 2 0 100.00%
131131
// REPORT-NEXT: ---
132-
// REPORT-NEXT: TOTAL 197 24 87.82% 234 8 96.58% 174 38 78.16%
132+
// REPORT-NEXT: TOTAL 197 24 87.82% 234 8 96.58% 170 36 78.82%
133133

134134
// Test file-level report.
135135
// RUN: llvm-profdata merge %S/Inputs/branch-c-general.proftext -o %t.profdata
136136
// RUN: llvm-cov report %S/Inputs/branch-c-general.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs %S/Inputs/branch-c-general.c | FileCheck %s -check-prefix=FILEREPORT
137-
// FILEREPORT: TOTAL{{.*}}174 38 78.16%
137+
// FILEREPORT: TOTAL{{.*}}170 36 78.82%
138138

139139
// Test color True/False output.
140140
// RUN: llvm-cov show --use-color --show-branches=count %S/Inputs/branch-c-general.o32l -instr-profile %t.profdata -path-equivalence=/tmp,%S/Inputs %S/Inputs/branch-c-general.c | FileCheck %s -check-prefix=USECOLOR
@@ -161,6 +161,6 @@
161161
// HTML-INDEX: <td class='column-entry-yellow'>
162162
// HTML-INDEX: 87.82% (173/197)
163163
// HTML-INDEX: <td class='column-entry-red'>
164-
// HTML-INDEX: 78.16% (136/174)
164+
// HTML-INDEX: 78.82% (134/170)
165165
// HTML-INDEX: <tr class='light-row-bold'>
166166
// HTML-INDEX: Totals

0 commit comments

Comments
 (0)