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

            Bug ID: 125735
           Summary: non-positive x in `1/x should be converted to `x == 1`
                    early
           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 (int b) {
  if (b < 1) return 0;
  return 1 / b;
}
```

This does not get optimized to:
`b== 1`

This is because the pattern:
```
(simplify
 (trunc_div integer_onep@0 @1)
 (if (INTEGRAL_TYPE_P (type)
      && TYPE_PRECISION (type) > 1
      && !integer_zerop (@1)
      && (!flag_non_call_exceptions || tree_expr_nonzero_p (@1)))
  (if (TYPE_UNSIGNED (type))
   (convert (eq:boolean_type_node @1 { build_one_cst (type); }))
   (if (fold_before_rtl_expansion_p ())
    (with { tree utype = unsigned_type_for (type); }
     (cond (le (plus (convert:utype @1) { build_one_cst (utype); })
                { build_int_cst (utype, 2); })
      @1 { build_zero_cst (type); }))))))
```

Only checks TYPE_UNSIGNED rather than nonnegative of `@1`.

Reply via email to