https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126953

            Bug ID: 126953
           Summary: popcount pattern should use with_possible_nonzero_bits
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: internal-improvement
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

```
/* Common POPCOUNT/PARITY simplifications.  */
/* popcount(X&C1) is (X>>C2)&1 when C1 == 1<<C2.  Same for parity(X&C1).  */
(for pfun (POPCOUNT PARITY)
  (simplify
    (pfun @0)
    (if (INTEGRAL_TYPE_P (type))
     (with { wide_int nz = tree_nonzero_bits (@0); }
       (switch
         (if (nz == 1)
           (convert @0))
         (if (wi::popcount (nz) == 1)
           (with { tree utype = unsigned_type_for (TREE_TYPE (@0)); }
             (convert (rshift:utype (convert:utype @0)
                                    { build_int_cst (integer_type_node,
                                                     wi::ctz (nz)); })))))))))

```

Instead of the integral type check here, with_possible_nonzero_bits should be
used as a predicate. For gimple, this does not change much. For generic, this
will do some simple extra checks before calling tree_nonzero_bits .

Reply via email to