To find the zero check we look for a branch whose condition is
a zero compare with the same Value* passed to the phi for the loop.
InstCombine knows some tricks that can break this. The one I observed
is when the input to the loop is a llvm.abs intrinsic. InstCombine
rewrites icmp eq (llvm.abs X), 0 to icmp eq X, 0 since the absolute
value doesn't change 0 or produce 0 for any other inputs. I believe
the original motivating case for this tranform featured an absolute
value before the loop so we should make this work.
I didn't want to teach this pass to also look through llvm.abs as
it seemed like we'd inevitably need to teach it more things to look
through. So instead I've inserted a new zero check for this case to
select the count value the loop should produce if there was no
zero check before the loop. This new zero check will hopefully be
optimized by InstCombine and eventually determined to be unneeded
and removed. If not, it's probably still cheaper than the loop.