-
Notifications
You must be signed in to change notification settings - Fork 11.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce CounterExpressionBuilder::replace(C, Map) #112698
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,6 +135,38 @@ Counter CounterExpressionBuilder::subtract(Counter LHS, Counter RHS, | |
return Simplify ? simplify(Cnt) : Cnt; | ||
} | ||
|
||
Counter CounterExpressionBuilder::replace(Counter C, const ReplaceMap &Map) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this have a more descriptive name? This is replacing each term with an existing term, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought its name may be simple here, since it is declared in the class. "This replaces each term (or possibly recognizable expression) with an existing expression." |
||
auto I = Map.find(C); | ||
|
||
// Replace C with the Map even if C is Expression. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
"replace C with the value found in Map..."? |
||
if (I != Map.end()) | ||
return I->second; | ||
|
||
// Traverse only Expression. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because others (Zero or CounterRef) are not traversable :) Is this line too naive to you? I could eliminate. |
||
if (!C.isExpression()) | ||
return C; | ||
|
||
auto CE = Expressions[C.getExpressionID()]; | ||
auto NewLHS = replace(CE.LHS, Map); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How deep can this recurse? Should there be a depth limit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose at most the number of terms, unless the expression is made buggy with loops. (Then, other CounterExpression stuff would hang or crash, I guess) Would we really check if the expr would not be buggy? If we should introduce such a check, it may be not depth limit but the set of |
||
auto NewRHS = replace(CE.RHS, Map); | ||
|
||
// Reconstruct Expression with induced subexpressions. | ||
switch (CE.Kind) { | ||
case CounterExpression::Add: | ||
C = add(NewLHS, NewRHS); | ||
break; | ||
case CounterExpression::Subtract: | ||
C = subtract(NewLHS, NewRHS); | ||
break; | ||
} | ||
|
||
// Reconfirm if the reconstructed expression would hit the Map. | ||
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: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why std::map instead of e.g. DenseMap?
https://llvm.org/docs/ProgrammersManual.html#map
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am so lazy to add hash helper into
llvm::Counter
. I have takenstd::map
sinceCounter
has provided enough operators.The map may grow up to hundreds of keys. Could we make
Counter
ready for hash maps?