gcov computes the line execution count as the sum of (a) counts from
predecessors on other lines and (b) the sum of loop execution counts of blocks
on the same line (think of loops on one line).
For (b), we use Donald B. Johnson's cycle enumeration algorithm and perform
cycle cancelling for each cycle. This number of candidate cycles were
exponential and D93036 made it polynomial by skipping zero count cycles. The
time complexity is high (O(V*E^2) (it could be O(E^2) but the linear Blocks
check made it higher) and the implementation is complex.
We could just find all back edges (each corresponds to a natural loop) and sum
their counts. However, this requires a dominator tree construction which is more
complex. The time complexity can be almost linear, though.
This patch just performs cycle cancelling iteratively. Add two members
traversable and incoming to GCOVArc. There are 3 states:
- !traversable: blocks not on this line or explored blocks
- traversable && incoming == nullptr: unexplored blocks
- traversable && incoming != nullptr: blocks which are being explored (on the stack)
If an arc points to a block being explored, a cycle has been found.
Let E be the number of arcs. Every time a cycle is found, at least one arc is
saturated (edgeCount reduced to 0), so there are at most E cycles. Finding one
cycle takes O(E) time, so the overall time complexity is O(E^2). Note that we
always augment through a back edge and never need to augment its reverse edge so
reverse edges in traditional flow networks are not needed.
nit: you can define stack as a local variable. e.g.
std::vector<std::pair<GCOVBlock *, size_t>> stack = {{src, 0}};
making it an argument probably is more efficient, but I think the performance benefit is marginal - leaving it to you.