In Clang's lib/CodeGen/CodeGenAction.cpp, a timer is used to measure
the amount of time spent generating LLVM IR. The timer is started and
stopped in a method, BackendConsumer::HandleTopLevelDecl, that is called
recursively. llvm::Timer does not allow starting a timer twice, so to
prevent a runtime assertion that would occur if the timer is started
a second time as the method recurses, it's guarded by a "reference
count" variable: if the reference count is incremented from 0 to 1, the
timer is started. Subsequent calls to start the timer increment the
reference count, but do not call startTimer(). stopTimer() is called when
the reference count goes from 1 to 0.
This pattern is useful when timing any recursive method, such as in
https://reviews.llvm.org/D36492.
Add startReentrantTimer and stopReentrantTimer methods that can be used for
situations such as these. Unlike startTimer/stopTimer, multiple calls to
startReentrantTimer are fine, as long as stopReentrantTimer is called an
equal number of times.
Maybe this should be called StartCount instead of RefCount? It should be possible to get rid of the Running member at that point and check StartCount > 0 instead.