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

            Bug ID: 125737
           Summary: `a%b < b` should be true if a and b are non-negative
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Keywords: easyhack, missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
int
f (unsigned short a, unsigned short b) {
  int aa = a;
  int bb = b;
  int c = aa % bb;
  return c < bb;
}
```
This should be just `return 1`.
It is not because the pattern:
```
/* X % Y is smaller than Y.  */
(for cmp (lt ge)
 (simplify
  (cmp:c (trunc_mod @0 @1) @1)
  (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
   { constant_boolean_node (cmp == LT_EXPR, type); })))
```
Only checks TYPE_UNSIGNED but it is true when @0 and @1 are non-negative
values.

Reply via email to