Skip to content

Commit cb3f959

Browse files
committed
Merge branch 'users/chapuni/cov/single/nextcount' into users/chapuni/cov/single/base
2 parents 2968ea6 + c0785e9 commit cb3f959

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

clang/lib/CodeGen/CoverageMappingGen.cpp

+26-20
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ struct CounterCoverageMappingBuilder
884884
/// The map of statements to count values.
885885
llvm::DenseMap<const Stmt *, CounterPair> &CounterMap;
886886

887-
CounterExpressionBuilder::ReplaceMap MapToExpand;
887+
CounterExpressionBuilder::SubstMap MapToExpand;
888888
unsigned NextCounterNum;
889889

890890
MCDC::State &MCDCState;
@@ -959,20 +959,30 @@ struct CounterCoverageMappingBuilder
959959
return {ExecCnt, SkipCnt};
960960
}
961961

962-
Counter getSwitchImplicitDefaultCounter(const Stmt *Cond, Counter ParentCount,
963-
Counter CaseCountSum) {
964-
return (
965-
llvm::EnableSingleByteCoverage
966-
? Counter::getCounter(CounterMap[Cond].second = NextCounterNum++)
967-
: Builder.subtract(ParentCount, CaseCountSum));
962+
/// Returns {TrueCnt,FalseCnt} for "implicit default".
963+
/// FalseCnt is considered as the False count on SwitchStmt.
964+
std::pair<Counter, Counter>
965+
getSwitchImplicitDefaultCounterPair(const Stmt *Cond, Counter ParentCount,
966+
Counter CaseCountSum) {
967+
if (llvm::EnableSingleByteCoverage)
968+
return {Counter::getZero(), // Folded
969+
Counter::getCounter(CounterMap[Cond].second = NextCounterNum++)};
970+
971+
// Simplify is skipped while building the counters above: it can get
972+
// really slow on top of switches with thousands of cases. Instead,
973+
// trigger simplification by adding zero to the last counter.
974+
CaseCountSum =
975+
addCounters(CaseCountSum, Counter::getZero(), /*Simplify=*/true);
976+
977+
return {CaseCountSum, Builder.subtract(ParentCount, CaseCountSum)};
968978
}
969979

970980
bool IsCounterEqual(Counter OutCount, Counter ParentCount) {
971981
if (OutCount == ParentCount)
972982
return true;
973983

974984
// Try comaparison with pre-replaced expressions.
975-
if (Builder.replace(Builder.subtract(OutCount, ParentCount), MapToExpand)
985+
if (Builder.subst(Builder.subtract(OutCount, ParentCount), MapToExpand)
976986
.isZero())
977987
return true;
978988

@@ -1189,12 +1199,14 @@ struct CounterCoverageMappingBuilder
11891199
/// and add it to the function's SourceRegions.
11901200
/// Returns Counter that corresponds to SC.
11911201
Counter createSwitchCaseRegion(const SwitchCase *SC, Counter ParentCount) {
1202+
Counter TrueCnt = getRegionCounter(SC);
1203+
Counter FalseCnt = (llvm::EnableSingleByteCoverage
1204+
? Counter::getZero() // Folded
1205+
: subtractCounters(ParentCount, TrueCnt));
11921206
// Push region onto RegionStack but immediately pop it (which adds it to
11931207
// the function's SourceRegions) because it doesn't apply to any other
11941208
// source other than the SwitchCase.
1195-
Counter TrueCnt = getRegionCounter(SC);
1196-
popRegions(pushRegion(TrueCnt, getStart(SC), SC->getColonLoc(),
1197-
subtractCounters(ParentCount, TrueCnt)));
1209+
popRegions(pushRegion(TrueCnt, getStart(SC), SC->getColonLoc(), FalseCnt));
11981210
return TrueCnt;
11991211
}
12001212

@@ -1931,15 +1943,9 @@ struct CounterCoverageMappingBuilder
19311943
// the hidden branch, which will be added later by the CodeGen. This region
19321944
// will be associated with the switch statement's condition.
19331945
if (!HasDefaultCase) {
1934-
// Simplify is skipped while building the counters above: it can get
1935-
// really slow on top of switches with thousands of cases. Instead,
1936-
// trigger simplification by adding zero to the last counter.
1937-
CaseCountSum =
1938-
addCounters(CaseCountSum, Counter::getZero(), /*Simplify=*/true);
1939-
1940-
// This is considered as the False count on SwitchStmt.
1941-
Counter SwitchFalse = subtractCounters(ParentCount, CaseCountSum);
1942-
createBranchRegion(S->getCond(), CaseCountSum, SwitchFalse);
1946+
auto Counters = getSwitchImplicitDefaultCounterPair(
1947+
S->getCond(), ParentCount, CaseCountSum);
1948+
createBranchRegion(S->getCond(), Counters.first, Counters.second);
19431949
}
19441950
}
19451951

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,10 @@ class CounterExpressionBuilder {
215215
/// LHS.
216216
Counter subtract(Counter LHS, Counter RHS, bool Simplify = true);
217217

218-
using ReplaceMap = std::map<Counter, Counter>;
218+
using SubstMap = std::map<Counter, Counter>;
219219

220-
/// Return a counter for each term in the expression replaced by ReplaceMap.
221-
Counter replace(Counter C, const ReplaceMap &Map);
220+
/// Return a counter for each term in the expression replaced by SubstMap.
221+
Counter subst(Counter C, const SubstMap &Map);
222222
};
223223

224224
using LineColPair = std::pair<unsigned, unsigned>;

llvm/lib/ProfileData/Coverage/CoverageMapping.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,17 @@ Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS,
135135
return Simplify ? simplify(Cnt) : Cnt;
136136
}
137137

138-
Counter CounterExpressionBuilder::replace(Counter C, const ReplaceMap &Map) {
138+
Counter CounterExpressionBuilder::subst(Counter C, const SubstMap &Map) {
139139
// Replace C with the value found in Map even if C is Expression.
140140
if (auto I = Map.find(C); I != Map.end())
141141
return I->second;
142142

143143
if (!C.isExpression())
144144
return C;
145145

146-
// Traverse both sides of Expression.
147146
auto CE = Expressions[C.getExpressionID()];
148-
auto NewLHS = replace(CE.LHS, Map);
149-
auto NewRHS = replace(CE.RHS, Map);
147+
auto NewLHS = subst(CE.LHS, Map);
148+
auto NewRHS = subst(CE.RHS, Map);
150149

151150
// Reconstruct Expression with induced subexpressions.
152151
switch (CE.Kind) {

0 commit comments

Comments
 (0)