https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118660
Bug ID: 118660 Summary: [14/15 Regression] VRP gets in the way sometimes Product: gcc Version: 15.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: tree-optimization Assignee: pinskia at gcc dot gnu.org Reporter: pinskia at gcc dot gnu.org Target Milestone: --- Take: ``` unsigned f(unsigned a) { unsigned t = a; if ((3 & ~a) == 0) return t & 1; return t & 1; } unsigned f1(unsigned a) { unsigned t = a; if ((a & 3) != 3) return t & 1; return t & 1; } ``` Both of these should just be optimized to `t & 1` but VRP changes the second `t & 1` to 1 which then confuses other stuff. Don't worry LLVM has the same issue starting LLVM 18 too. For GCC, it should be a simple match pattern for: ``` _1 = a_3(D) & 3; if (_1 != 3) goto <bb 3>; [INV] else goto <bb 4>; [INV] <bb 3> : _6 = a_3(D) & 1; // predicted unlikely by early return (on trees) predictor. <bb 4> : # _2 = PHI <_6(3), 1(2)> ``` That is: ``` (simplify (cond (ne (bit_and @0 INTEGER_CST@1) INTEGER_CST@1) (bit_and@3 @0 INTEGER_CST@2) @2) (if ((@2 & @1) == @2) @3)) ``` And the other one requires us to canonicalize `(3 & ~a) == 0` to `(3 & a) == 3` (since that is simplier in gimple.