Signed overflow is Undefined Behaviour in ISO C. You can't reliably cause it and then check if it happened.
In the expression (x + y) > y;
, the compiler is allowed to assume that x+y
doesn't overflow (because that would be UB). Therefore, it optimizes down to checking x > 0
. (Yes, really, gcc does this even at -O0
).
This optimization is new in gcc8. It's the same on x86 and AArch64; you must have used different GCC versions on AArch64 and x86. (Even at -O3
, gcc7.x and earlier (intentionally?) miss this optimization. clang7.0 doesn't do it either. They actually do a 32-bit add and compare. They also miss optimizing tadd_ok
to return 1
, or to add
and checking the overflow flag (V
on ARM, OF
on x86). Clang's optimized asm is an interesting mix of >>31
, OR and one XOR operation, but -fwrapv
actually changes that asm so it's probably not doing a full overflow check.)
You could say that gcc8 "breaks" your code, but really it was already broken as far as being legal / portable ISO C. gcc8 just revealed that fact.
To see it more clearly, lets isolate just that expression into one function. gcc -O0
compiles each statement separately anyway, so the information that this only runs when x<0
doesn't affect the -O0
code-gen for this statement in your tadd_ok
function.
// compiles to add and checking the carry flag, or equivalent
int unsigned_overflow_test(unsigned x, unsigned y) {
return (x+y) >= y; // unsigned overflow is well-defined as wrapping.
}
// doesn't work because of UB.
int signed_overflow_expression(int x, int y) {
return (x+y) > y;
}
On the Godbolt compiler explorer with AArch64 GCC8.2 -O0 -fverbose-asm
:
signed_overflow_expression:
sub sp, sp, #16 //,, // make a stack fram
str w0, [sp, 12] // x, x // spill the args
str w1, [sp, 8] // y, y
// end of prologue
// instructions that implement return (x+y) > y; as return x > 0
ldr w0, [sp, 12] // tmp94, x
cmp w0, 0 // tmp94,
cset w0, gt // tmp95, // w0 = (x>0) ? 1 : 0
and w0, w0, 255 // _1, tmp93 // redundant
// epilogue
add sp, sp, 16 //,,
ret
GCC -ftree-dump-original
or -optimized
will even turn its GIMPLE back into C-like code with this optimization done (from the Godbolt link):
;; Function signed_overflow_expression (null)
;; enabled by -tree-original
{
return x > 0;
}
Unfortunately, even with -Wall -Wextra -Wpedantic
, there's no warning about a the comparison. It's not trivially true; it still depends on x
.
The optimized asm is unsurprisingly cmp w0, 0
/ cset w0, gt
/ ret
. The AND with 0xff
is redundant. cset
is an alias of csinc
, using the zero-register as both sources. So it will produce 0 / 1. With other registers, the general case of csinc
is a conditional select and increment of any 2 registers.
Anyway, cset
is AArch64's equivalent of x86 setcc
, for turning a flag condition into a bool
in a register.
If you want your code to work as written, you'd need to compile with -fwrapv
to make it well-defined behaviour in the variant of C that -fwrapv
makes GCC implement. The default is -fstrict-overflow
, like the ISO C standard.
If you want to check for signed overflow in modern C, you need to write checks that detect overflow without actually causing it. This is harder, annoying, and a point of contention between compiler writers and (some) developers. They argue that the language rules around undefined behaviour weren't meant to be used as an excuse to "gratuitously break" code when compiling for target machines where it would make sense in asm. But modern compilers mostly only implement ISO C (with some extensions and extra defined behaviour), even when compiling for target architectures like x86 and ARM where signed integers have no padding (and thus wrap just fine), and don't trap on overflow.
So you could say "shots fired" in that war, with the change in gcc8.x to actually "breaking" unsafe code like this. :P
See Detecting signed overflow in C/C++ and How to check for signed integer overflow in C without undefined behaviour?
Since signed and unsigned addition are the same binary operation in 2's complement, you could maybe just cast to unsigned
for the add, and cast back for a signed compare. That would make a version of your function that's safe on "normal" implementations: 2's complement, and casting between unsigned
and int
is just a reinterpret of the same bits.
This can't have UB, it just won't give the right answer on one's complement or sign/magnitude C implementations.
return (int)((unsigned)x + (unsigned)y) > y;
This compiles (with gcc8.2 -O3 for AArch64) to
add w0, w0, w1 // x+y
cmp w0, w1 // x+y cmp y
cset w0, gt
ret
If you had written int sum = x+y
as a separate C statement from return sum < y
, this UB wouldn't be visible to gcc with optimization disabled. But as part of the same expression, even gcc
with the default -O0
can see it.
Compile-time-visible UB is all kinds of bad. In this case, only certain ranges of inputs would produce UB, so the compiler assumes it doesn't happen. If unconditional UB is seen on a path of execution, an optimizing compiler can assume that path never happens. (In a function with no branching, it could assume the function is never called, and compile it to a single illegal instruction.) See Does the C++ standard allow for an uninitialized bool to crash a program? for more about compile-time-visible UB.
(-O0
doesn't mean "no optimization", it means no extra optimization besides what's already necessary to transform through gcc's internal representations on the way to asm for whatever target platform. @Basile Starynkevitch explains in
Disable all optimization options in GCC)
Some other compilers may "turn their brains off" even more with optimization disabled, and do something closer to transliterating C into asm, but gcc is not like that. For example, gcc still uses a multiplicative inverse for integer division by a constant at -O0
. (Why does GCC use multiplication by a strange number in implementing integer division?) All 3 other major x86 compilers (clang/ICC/MSVC) use div
.