On Fri, Nov 20, 2026 at 3:07 PM Daniel Petrovic <[email protected]> wrote: > > Hi Richard, > > thank you for your time and feedback! > > >> A may be a constant or a range limited value, for example a bool, > >> a masked value or a value with a known range. > >> The fold is not done when A may touch the sign bit. > > > Does this supersede some of the existing (cmp (bitop ...)) simplifications? > > I don't see that any of the existing simplifications is completely > superseded, although there is some partial overlap. > > For X|0 and X^0 there are already simpler folds, and the new fold skips that > case. > > There is one partial overlap for this one : > > (for op (min bit_ior) > (for cmp (ge lt) ...) > > which handles (a | b) >= 0 and < 0 for nonnegative b. That one is not > superseded, e.g. it also handles MIN. > That said, I may be missing something, so I'd be glad to be corrected.
Thanks for checking. As said, the patch is OK. Richard. > Regards, > Daniel > ________________________________________ > From: Richard Biener <[email protected]> > Sent: 20 November 2026 12:21 > To: Daniel Petrovic <[email protected]> > Cc: [email protected] <[email protected]>; > [email protected] <[email protected]>; > [email protected] <[email protected]> > Subject: Re: [PATCH] match.pd: Fold (X | A) cmp C and (X ^ A) cmp C into X > cmp C [PR115392] > > On Thu, Oct 29, 2026 at 1:28 PM Daniel Petrovic <[email protected]> wrote: > > > > Hi, > > > > this patch fixes https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115392 > > > > I’d appreciate the review. > > > > If A is known to lie in [0, 2^m-1], then X | A and X ^ A leave the bits > > of X at and above position m unchanged, so the comparison with C is > > decided by the high parts alone when the low m bits of C are: > > > > all ones: fold (X op A) <= C and (X op A) > C to X <= C and X > C, > > all zeros: fold (X op A) < C and (X op A) >= C to X < C and X >= C. > > > > A may be a constant or a range limited value, for example a bool, > > a masked value or a value with a known range. > > The fold is not done when A may touch the sign bit. > > Does this supersede some of the existing (cmp (bitop ...)) simplifications? > > The patch itself looks OK to me. > > Thanks, > Richard. > > > gcc/ChangeLog: > > PR tree-optimization/115392 > > > > match.pd: Fold (X | A) cmp C and (X ^ A) cmp C into X cmp C > > when A is known to be in [0, 2^m-1] and the low m bits of C are > > all ones (le, gt) or all zeros (lt, ge). > > > > gcc/testsuite/ChangeLog: > > PR tree-optimization/115392 > > > > gcc.dg/tree-ssa/pr115392-1.c: New test. > > gcc.dg/tree-ssa/pr115392-2.c: New test. > > gcc.dg/tree-ssa/pr115392-3.c: New test. > > gcc.dg/tree-ssa/pr115392-4.c: New test. > > gcc.dg/tree-ssa/pr115392-5.c: New test. > > gcc.dg/ipa/pr91088.c: Use a constant that the new fold does not > > simplify, so that the XOR is still visible to ipa-cp. > > > > > > Bootstrapped and regtested on x86_64-pc-linux-gnu with no regressions. > > > > Many thanks to Andrew Pinski and Jakub Jelinek for their contribution in > > generalizing the initial report. > > > > > > > > Thanks, > > Daniel > > > >
