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

            Bug ID: 126318
           Summary: Match non-negative t >> p to t / (1 << p)
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: kaelfandrew at gmail dot com
  Target Milestone: ---

While working on PR126294,

I noticed that my patch does not work when C is a power of 2:

```
#define C 8

unsigned f(unsigned short b)
{
  unsigned t = b;
  return t * (C + 1) / C;
}
```

This is because gcc/match.pd has:

/* Canonicalize unsigned t / 4 to t >> 2.  */
#if GIMPLE
(for div (trunc_div floor_div exact_div)
 (simplify
  (div @0 integer_pow2p@1)
  (if (INTEGRAL_TYPE_P (type)
       && (TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0)))
   (rshift @0 { build_int_cst (integer_type_node,
                               wi::exact_log2 (wi::to_wide (@1))); }))))
#endif

Reply via email to