Skip to content

[NFC] [Coverage] Do not use recursion for getMaxCounterID #67870

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

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 52 additions & 12 deletions llvm/lib/ProfileData/Coverage/CoverageMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,19 +223,59 @@ Expected<int64_t> CounterMappingContext::evaluate(const Counter &C) const {
}

unsigned CounterMappingContext::getMaxCounterID(const Counter &C) const {
switch (C.getKind()) {
case Counter::Zero:
return 0;
case Counter::CounterValueReference:
return C.getCounterID();
case Counter::Expression: {
if (C.getExpressionID() >= Expressions.size())
return 0;
const auto &E = Expressions[C.getExpressionID()];
return std::max(getMaxCounterID(E.LHS), getMaxCounterID(E.RHS));
}
struct StackElem {
Counter ICounter;
int64_t LHS = 0;
enum {
KNeverVisited = 0,
KVisitedOnce = 1,
KVisitedTwice = 2,
} VisitCount = KNeverVisited;
};

std::stack<StackElem> CounterStack;
CounterStack.push({C});

int64_t LastPoppedValue;

while (!CounterStack.empty()) {
StackElem &Current = CounterStack.top();

switch (Current.ICounter.getKind()) {
case Counter::Zero:
LastPoppedValue = 0;
CounterStack.pop();
break;
case Counter::CounterValueReference:
LastPoppedValue = Current.ICounter.getCounterID();
CounterStack.pop();
break;
case Counter::Expression: {
if (Current.ICounter.getExpressionID() >= Expressions.size()) {
LastPoppedValue = 0;
CounterStack.pop();
} else {
const auto &E = Expressions[Current.ICounter.getExpressionID()];
if (Current.VisitCount == StackElem::KNeverVisited) {
CounterStack.push(StackElem{E.LHS});
Current.VisitCount = StackElem::KVisitedOnce;
} else if (Current.VisitCount == StackElem::KVisitedOnce) {
Current.LHS = LastPoppedValue;
CounterStack.push(StackElem{E.RHS});
Current.VisitCount = StackElem::KVisitedTwice;
} else {
int64_t LHS = Current.LHS;
int64_t RHS = LastPoppedValue;
LastPoppedValue = std::max(LHS, RHS);
CounterStack.pop();
}
}
break;
}
}
}
llvm_unreachable("Unhandled CounterKind");

return LastPoppedValue;
}

void FunctionRecordIterator::skipOtherFiles() {
Expand Down