|| !COMPLEX_FLOAT_TYPE_P (type)))
(negate @0)))
-/* Make sure to preserve divisions by zero. This is the reason why
- we don't simplify x / x to 1 or 0 / x to 0. */
+/* X * 1, X / 1 -> X. */
(for op (mult trunc_div ceil_div floor_div round_div exact_div)
(simplify
(op @0 integer_onep)
(non_lvalue @0)))
+/* Preserve explicit divisions by 0: the C++ front-end wants to detect
+ undefined behavior in constexpr evaluation, and assuming that the division
+ traps enables better optimizations than these anyway. */
(for div (trunc_div ceil_div floor_div round_div exact_div)
+ /* 0 / X is always zero. */
+ (simplify
+ (div integer_zerop@0 @1)
+ /* But not for 0 / 0 so that we can get the proper warnings and errors. */
+ (if (!integer_zerop (@1))
+ @0))
/* X / -1 is -X. */
(simplify
(div @0 integer_minus_onep@1)
(if (!TYPE_UNSIGNED (type))
(negate @0)))
+ /* X / X is one. */
+ (simplify
+ (div @0 @0)
+ /* But not for 0 / 0 so that we can get the proper warnings and errors. */
+ (if (!integer_zerop (@0))
+ { build_one_cst (type); }))
/* X / abs (X) is X < 0 ? -1 : 1. */
(simplify
(div:C @0 (abs @0))
(if (inverse)
(mult @0 { inverse; } ))))))))
-/* Same applies to modulo operations, but fold is inconsistent here
- and simplifies 0 % x to 0, only preserving literal 0 % 0. */
(for mod (ceil_mod floor_mod round_mod trunc_mod)
/* 0 % X is always zero. */
(simplify
(mod @0 integer_minus_onep@1)
(if (!TYPE_UNSIGNED (type))
{ build_zero_cst (type); }))
+ /* X % X is zero. */
+ (simplify
+ (mod @0 @0)
+ /* But not for 0 % 0 so that we can get the proper warnings and errors. */
+ (if (!integer_zerop (@0))
+ { build_zero_cst (type); }))
/* (X % Y) % Y is just X % Y. */
(simplify
(mod (mod@2 @0 @1) @1)