Re: [PATCH] match: fold a remainder compared with its dividend
Kyrylo Tkachov <[email protected]> Thu, 6 Aug 2026 05:06:31 +0000
| Newsgroups | gmane.comp.gcc.patches |
|---|---|
| Message-ID | <[email protected]> |
> On 6 Aug 2026, at 06:58, Jeffrey Law <[email protected]> wrote: > > > > On 8/4/2026 4:06 AM, [email protected] wrote: >> From: Kyrylo Tkachov <[email protected]> >> >> A remainder of non-negative operands equals its dividend exactly when the >> dividend is smaller than the divisor. Comparing the two therefore does >> not need the division at all. This sits next to the (X / Y) == 0 rule, >> which has the same shape and the same non-negativity requirement. >> >> int f (unsigned x, unsigned y) { return x % y == x; } >> >> aarch64 -O2: >> >> before after >> udiv w2, w0, w1 cmp w0, w1 >> msub w2, w2, w1, w0 cset w0, cc >> cmp w2, w0 >> cset w0, eq >> >> The :s marker cannot reject this flat replacement. Remove the inactive >> marker. >> >> A zero divisor can raise a non-call exception. Keep the division or >> remainder when the divisor might be zero and non-call exceptions are >> enabled. Also keep an explicit zero divisor for diagnostics. >> >> Bootstrapped and tested on aarch64-none-linux-gnu. >> Ok for trunk? >> Thanks, >> Kyrill >> >> gcc/ChangeLog: >> >> * match.pd ((X / Y) ==/!= 0): Preserve a possible zero-divisor >> exception. >> ((X % Y) ==/!= X): New simplification. Preserve a possible >> zero-divisor exception. >> >> gcc/testsuite/ChangeLog: >> >> * gcc.dg/tree-ssa/modcmp-1.c: New test. >> * gcc.dg/tree-ssa/modcmp-noncall-1.c: Likewise. > Like most of these, I'm curious where this showed up :-) I asked an AI agent to mine SPEC2026 and a few other workloads that are of interest to me to find missing folds. It turned out to be a quite productive exercise. I can recommend it as a way to constructively use these agents for things other than writing GCC code. I don’t think this particular fold is on a hot path or anything, but it is obviously a beneficial one anyway (it reduces the complexity of the IR, as match.pd folds intend) > > It looks correct to me. OK for the trunk. Note that your cover indicates you removed a :s tag, but I don't see that in the patch. Probably best to resolve that one way or another before committing. Yes, will do. Thanks, Kyrill > > Thanks, > > jeff