This is an archive of the discontinued LLVM Phabricator instance.

[SCEVExpander] Allow udiv with isKnownNonZero(RHS) + add vscale case
ClosedPublic

Authored by reames on Jul 13 2022, 5:39 PM.

Details

Summary

Motivation here is to unblock LSRs ability to use ICmpZero uses - the major effect of which is to enable count down IVs. The test changes reflect this goal, but the potential impact is much broader since this isn't a change in LSR at all.

SCEVExpander needs(*) to prove that expanding the expression is safe anywhere the SCEV expression is valid. In general, we can't expand any node which might fault (or exhibit UB) unless we can either a) prove it won't fault, or b) guard the faulting case. We'd been allowing non-zero constants here; this change extends it to non-zero values.

vscale is never zero. This is already implemented in ValueTracking, and this change just adds the same logic in SCEV's range computation (which in turn drives isKnownNonZero). We should common up some logic here, but let's do that in separate changes.

(*) As an aside, "needs" is such an interesting word here. First, we don't actually need to guard this at all; we could choose to emit a select for the RHS of ever udiv and remove this code entirely. Secondly, the property being checked here is way too strong. What the client actually needs is to expand the SCEV at some particular point in some particular loop. In the examples, the original urem dominates that loop and yet we completely ignore that information when analyzing legality. I don't plan to actively pursue either direction, just noting it for future reference.

Diff Detail

Event Timeline

reames created this revision.Jul 13 2022, 5:39 PM
reames requested review of this revision.Jul 13 2022, 5:39 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 13 2022, 5:39 PM
nikic accepted this revision.Jul 14 2022, 12:56 AM

LGTM. No compile-time impact on CTMark at least.

This revision is now accepted and ready to land.Jul 14 2022, 12:56 AM
This revision was landed with ongoing or failed builds.Jul 14 2022, 8:57 AM
This revision was automatically updated to reflect the committed changes.

I wrote an issue about a failure starting to appear with this patch:
https://github.com/llvm/llvm-project/issues/57336