210 changes: 127 additions & 83 deletions clang/lib/CodeGen/CoverageMappingGen.cpp

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions clang/lib/CodeGen/CoverageMappingGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class CoverageSourceInfo : public PPCallbacks,
namespace CodeGen {

class CodeGenModule;
class CounterPair;

namespace MCDC {
struct State;
Expand Down Expand Up @@ -158,7 +159,7 @@ class CoverageMappingGen {
CoverageMappingModuleGen &CVM;
SourceManager &SM;
const LangOptions &LangOpts;
llvm::DenseMap<const Stmt *, unsigned> *CounterMap;
llvm::DenseMap<const Stmt *, CounterPair> *CounterMap;
MCDC::State *MCDCState;

public:
Expand All @@ -169,7 +170,7 @@ class CoverageMappingGen {

CoverageMappingGen(CoverageMappingModuleGen &CVM, SourceManager &SM,
const LangOptions &LangOpts,
llvm::DenseMap<const Stmt *, unsigned> *CounterMap,
llvm::DenseMap<const Stmt *, CounterPair> *CounterMap,
MCDC::State *MCDCState)
: CVM(CVM), SM(SM), LangOpts(LangOpts), CounterMap(CounterMap),
MCDCState(MCDCState) {}
Expand Down
4 changes: 4 additions & 0 deletions llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <cassert>
#include <cstdint>
#include <iterator>
#include <map>
#include <memory>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -213,6 +214,9 @@ class CounterExpressionBuilder {
/// Return a counter that represents the expression that subtracts RHS from
/// LHS.
Counter subtract(Counter LHS, Counter RHS, bool Simplify = true);

using ReplaceMap = std::map<Counter, Counter>;
Counter replace(Counter C, const ReplaceMap &Map);
};

using LineColPair = std::pair<unsigned, unsigned>;
Expand Down
30 changes: 30 additions & 0 deletions llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,32 @@ Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS,
return Simplify ? simplify(Cnt) : Cnt;
}

Counter CounterExpressionBuilder::replace(Counter C, const ReplaceMap &Map) {
auto I = Map.find(C);
if (I != Map.end())
return I->second;

if (!C.isExpression())
return C;

auto CE = Expressions[C.getExpressionID()];
auto NewLHS = replace(CE.LHS, Map);
auto NewRHS = replace(CE.RHS, Map);
switch (CE.Kind) {
case CounterExpression::Subtract:
C = subtract(NewLHS, NewRHS);
break;
case CounterExpression::Add:
C = add(NewLHS, NewRHS);
break;
}

if ((I = Map.find(C)) != Map.end())
return I->second;

return C;
}

void CounterMappingContext::dump(const Counter &C, raw_ostream &OS) const {
switch (C.getKind()) {
case Counter::Zero:
Expand Down Expand Up @@ -608,6 +634,10 @@ static unsigned getMaxCounterID(const CounterMappingContext &Ctx,
unsigned MaxCounterID = 0;
for (const auto &Region : Record.MappingRegions) {
MaxCounterID = std::max(MaxCounterID, Ctx.getMaxCounterID(Region.Count));
if (Region.Kind == CounterMappingRegion::BranchRegion ||
Region.Kind == CounterMappingRegion::MCDCBranchRegion)
MaxCounterID =
std::max(MaxCounterID, Ctx.getMaxCounterID(Region.FalseCount));
}
return MaxCounterID;
}
Expand Down