https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114700
--- Comment #11 from lin1.hu at intel dot com --- (In reply to Richard Biener from comment #9) > That that GCC doesn't promise that -ftrapv preserves all overflows and > traps, it merely guarantees that all overflows that actually happen trap. > So GCC is fine to contract some expressions where the overall number of > overflows can only > decrease. > > That's not a bug with -ftrapv. > > It is considered a bug with -fsanitize=undefined though. I think it doesn't mean that's not a bug with -ftrapv, it should preserve all overflow traps. Because it doesn't work, we use -fsanitize=undefined instead of it. refer: Gcc's trapv is known not always to work correctly. The current behavior is correct for -fsanitize=undefined, because the integer signed overflow is well-defined, so GCC can eliminate some variables. I just think GCC can optimize `z = c - y - c + a + y - b` to `z = a - b`. But it doesn't mean is a bug for -fsanitize=undefined.