https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111780
Richard Biener <rguenth at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Keywords| |missed-optimization Status|UNCONFIRMED |NEW Last reconfirmed| |2023-10-12 Ever confirmed|0 |1 --- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> --- Confirmed. Same for int foo (int a, int b, int c) { return 2*c*(a*b) / (a*b); } note when we cannot remove the division like for c*a / d*a we have to watch out for -INT_MIN / -1, but I think the only way this would not invoke undefined behavior before the transform is when the factor is equal to -1 but then c*a cannot be -INT_MIN so it should be safe in general, not only when m and n are constants? Note we cannot re-associate for the transform. We only have /* Simplify (t * 2) / 2) -> t. */ (for div (trunc_div ceil_div floor_div round_div exact_div) (simplify (div (mult:c @0 @1) @1) (if (ANY_INTEGRAL_TYPE_P (type)) (if (TYPE_OVERFLOW_UNDEFINED (type)) @0 #if GIMPLE (with {value_range vr0, vr1;} (if (INTEGRAL_TYPE_P (type) && get_range_query (cfun)->range_of_expr (vr0, @0) && get_range_query (cfun)->range_of_expr (vr1, @1) && range_op_handler (MULT_EXPR).overflow_free_p (vr0, vr1)) @0)) #endif )))) but not the case with two multiplies.