https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114700
--- Comment #18 from Jakub Jelinek <jakub at gcc dot gnu.org> --- (In reply to Hu Lin from comment #17) > (In reply to Jakub Jelinek from comment #16) > > (In reply to Hu Lin from comment #11) > > > 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. > > > > No, -ftrapv isn't a debugging tool. There is no overflow in the expression > > that GCC actually evaluates (into which the expression has been optimized). > > If you have overflow in an expression that is never used, GCC with -ftrapv > > will also > > eliminate it as unused and won't diagnose the trap. > > -fsanitize=undefined behaves in that case actually the same with -O1 and > > higher (intentionally, to decrease the cost of the sanitization). So, one > > needs to use -O0 -fsanitize=undefined to get as many cases of UB in the > > program diagnosed as possible. > > OK, that look like GCC's -ftrapv is not the same as clang's. Then my added > condition should be (optimize || !TYPE_OVERFLOW_SANITIZED (type)). Why? Just !TYPE_OVERFLOW_SANITIZED (type). > > When a pattern already has one if, can't you just add that to the > > preexisting if rather than adding yet another one. > > I made a mistake on this line, it should be > + (if (!TYPE_OVERFLOW_SANITIZED (type)) > (if (!ANY_INTEGRAL_TYPE_P (type) > || TYPE_OVERFLOW_WRAPS (type)) > (negate (view_convert @1)) > (view_convert (negate @1)))) > > I can't just modify the preexisting if, the optimization shouldn't be used > with -fsanitize=undefined. TYPE_OVERFLOW_SANITIZED is #define TYPE_OVERFLOW_SANITIZED(TYPE) \ (INTEGRAL_TYPE_P (TYPE) \ && !TYPE_OVERFLOW_WRAPS (TYPE) \ && (flag_sanitize & SANITIZE_SI_OVERFLOW)) so, it isn't true for non-integral types, nor for TYPE_OVERFLOW_WRAPS types. So, if you want to avoid the (view_convert (negate @1)), just add (if !TYPE_OVERFLOW_SANITIZED (type)) above the (view_convert (negate @1)). But in each case, you want to be careful which exact type you want to check, type is the type of the outermost expression, otherwise TREE_TYPE (@0) etc.