https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101938
Bug ID: 101938 Summary: [12 Regression] Wrong code with -fwrapv since r12-2591-g2e96b5f14e402569 Product: gcc Version: 12.0 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: marxin at gcc dot gnu.org CC: aldyh at gcc dot gnu.org, amacleod at redhat dot com Target Milestone: --- It's a test-case reduced from postgresql package: $ cat postgresql.c #include <stdio.h> typedef long long int int64; #define INT64CONST(x) (x##LL) #define PG_INT64_MIN (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1) static int64 __attribute__((noipa)) int8gcd_internal(int64 arg1, int64 arg2) { int64 swap; int64 a1, a2; /* * Put the greater absolute value in arg1. * * This would happen automatically in the loop below, but avoids an * expensive modulo operation, and simplifies the special-case handling * for INT64_MIN below. * * We do this in negative space in order to handle INT64_MIN. */ a1 = (arg1 < 0) ? arg1 : -arg1; a2 = (arg2 < 0) ? arg2 : -arg2; if (a1 > a2) { swap = arg1; arg1 = arg2; arg2 = swap; } /* Special care needs to be taken with INT64_MIN. See comments above. */ if (arg1 == PG_INT64_MIN) { if (arg2 == 0 || arg2 == PG_INT64_MIN) { __builtin_printf ("ret=out of range\n"); } /* * Some machines throw a floating-point exception for INT64_MIN % -1, * which is a bit silly since the correct answer is perfectly * well-defined, namely zero. Guard against this and just return the * result, gcd(INT64_MIN, -1) = 1. */ if (arg2 == -1) { __builtin_printf ("ret=%d\n", 1); return 1; } } /* Use the Euclidean algorithm to find the GCD */ while (arg2 != 0) { swap = arg2; arg2 = arg1 % arg2; arg1 = swap; } /* * Make sure the result is positive. (We know we don't have INT64_MIN * anymore). */ if (arg1 < 0) arg1 = -arg1; __builtin_printf ("ret=%lld\n", arg1); return arg1; } int main() { int8gcd_internal (-1, -9223372036854775808ULL); return 0; } $ gcc postgresql.c -fwrapv && ./a.out ret=1 $ gcc postgresql.c -O2 -fwrapv && ./a.out Floating point exception (core dumped)