Skip to content

Commit 618e639

Browse files
committed
Introduce CounterExpressionBuilder::replace(C, Map)
This return a counter for each term in the expression replaced by ReplaceMap. At the moment, this doesn't update the Map, so Map is marked as `const`.
1 parent 6c331e5 commit 618e639

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <cassert>
3535
#include <cstdint>
3636
#include <iterator>
37+
#include <map>
3738
#include <memory>
3839
#include <sstream>
3940
#include <string>
@@ -213,6 +214,11 @@ class CounterExpressionBuilder {
213214
/// Return a counter that represents the expression that subtracts RHS from
214215
/// LHS.
215216
Counter subtract(Counter LHS, Counter RHS, bool Simplify = true);
217+
218+
using ReplaceMap = std::map<Counter, Counter>;
219+
220+
/// Return a counter for each term in the expression replaced by ReplaceMap.
221+
Counter replace(Counter C, const ReplaceMap &Map);
216222
};
217223

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

llvm/lib/ProfileData/Coverage/CoverageMapping.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,38 @@ Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS,
135135
return Simplify ? simplify(Cnt) : Cnt;
136136
}
137137

138+
Counter CounterExpressionBuilder::replace(Counter C, const ReplaceMap &Map) {
139+
auto I = Map.find(C);
140+
141+
// Replace C with the Map even if C is Expression.
142+
if (I != Map.end())
143+
return I->second;
144+
145+
// Traverse only Expression.
146+
if (!C.isExpression())
147+
return C;
148+
149+
auto CE = Expressions[C.getExpressionID()];
150+
auto NewLHS = replace(CE.LHS, Map);
151+
auto NewRHS = replace(CE.RHS, Map);
152+
153+
// Reconstruct Expression with induced subexpressions.
154+
switch (CE.Kind) {
155+
case CounterExpression::Add:
156+
C = add(NewLHS, NewRHS);
157+
break;
158+
case CounterExpression::Subtract:
159+
C = subtract(NewLHS, NewRHS);
160+
break;
161+
}
162+
163+
// Reconfirm if the reconstructed expression would hit the Map.
164+
if ((I = Map.find(C)) != Map.end())
165+
return I->second;
166+
167+
return C;
168+
}
169+
138170
void CounterMappingContext::dump(const Counter &C, raw_ostream &OS) const {
139171
switch (C.getKind()) {
140172
case Counter::Zero:

0 commit comments

Comments
 (0)