https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114700
--- Comment #19 from Hu Lin <lin1.hu at intel dot com> --- (In reply to Jakub Jelinek from comment #18) > (In reply to Hu Lin from comment #17) > > (In reply to Jakub Jelinek from comment #16) > > > > > > 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). > OK, so the part is one of your suggestions on how to test UB in a program. I have another question, -fsanitize=undefined disable this optimization, but you said -ftrapv won't diagnose the trap. Why is the logic here different for these two options? > > 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. Thanks for your advice.