[Profiler] Avoid introducing empty unreachable regions

When computing the counter for the region
following a labeled statement such as `if`, avoid
adding a new empty region if the counter for such
a region is known to be zero, i.e unreachable.
This avoids adding spurious unreachable regions
after an if statements where each branch returns,
throws, or otherwise jumps to a parent statement.

We will however still add the region if any code
follows such a statement, making it non-empty.

rdar://29390569
This commit is contained in:
Hamish Knight
2022-08-16 20:27:30 +01:00
parent c1ae4ed6bf
commit d1eb6f9465
2 changed files with 82 additions and 1 deletions

View File

@@ -814,7 +814,17 @@ private:
if (ControlFlowAdjust)
Count = &createCounter(CounterExpr::Sub(*Count, *ControlFlowAdjust));
RegionStack.emplace_back(ASTNode(), *Count, getEndLoc(Scope), None);
if (Count->isSemanticallyZero()) {
// If the counter is semantically zero, form an 'incomplete' region with
// no starting location. This prevents forming unreachable regions unless
// there is a following statement or expression to extend the region.
RegionStack.emplace_back(ASTNode(), *Count, None, None);
} else {
// Otherwise, we have a non-zero counter, so form a new region starting
// at the end of the previous scope. This ensures the user covers both
// branches of a condition.
RegionStack.emplace_back(ASTNode(), *Count, getEndLoc(Scope), None);
}
}
/// Push a region covering \c Node onto the stack.