From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 799F83858D3C; Mon, 15 Apr 2024 06:49:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 799F83858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1713163761; bh=fh+wfnotqApTv5smOPWoEXcCN0I00jROiv85OE5549k=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Ow7m2uzEqQJ8+xujmbs46QjPHqNohTL2kbeSrM9FJtIFVrWFD7qnineeQ0sogaywH h2suh4M0HwoMLmzI2TGOI7gswvuejYTB/R6oxG0akT6zalFcXJYZCLV67K8LFJEe5W 1rvRe0WvaCbiVZ1swj3PffRubpxTmMfMBUHXq3YU= From: "jakub at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug middle-end/114700] middle-end optimization generates causes -fsanitize=undefined not to happen in some cases Date: Mon, 15 Apr 2024 06:49:20 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: middle-end X-Bugzilla-Version: unknown X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: jakub at gcc dot gnu.org X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D114700 --- Comment #18 from Jakub Jelinek --- (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 pres= erve > > > all overflow traps. Because it doesn't work, we use -fsanitize=3Dunde= fined > > > instead of it. > > >=20 > > > refer: Gcc's trapv is known not always to work correctly. > >=20 > > No, -ftrapv isn't a debugging tool. There is no overflow in the expres= sion > > that GCC actually evaluates (into which the expression has been optimiz= ed). > > If you have overflow in an expression that is never used, GCC with -ftr= apv > > will also > > eliminate it as unused and won't diagnose the trap. > > -fsanitize=3Dundefined 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=3Dundefined to get as many cases of UB in t= he > > program diagnosed as possible. >=20 > 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)).=20 Why? Just !TYPE_OVERFLOW_SANITIZED (type). > > When a pattern already has one if, can't you just add that to the preex= isting if rather than adding yet another one. >=20 > 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)))) >=20 > I can't just modify the preexisting if, the optimization shouldn't be used > with -fsanitize=3Dundefined. 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.=