From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id E4659388E82D; Mon, 18 May 2020 13:28:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E4659388E82D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1589808481; bh=Aae5WaSxam0kqq4py71BQjkfbwlsQBu7vxdcz0/oy1U=; h=From:To:Subject:Date:In-Reply-To:References:From; b=g2Znl/dSbAQirunKdLWRiUXIwhEcLJOg+nPRiStXA4+AHY1f/Ug/VzaR+RZ23lHpR TciTWfj/KLSm9yyo2OEsOpt6iDYE80DQpXkerJtQKNJC8gTxRnCeT442EwhSx0l47j seoS+2oKIDE98JXOQ5rHl+q2WNhSJclqh9zrgtFQ= From: "rguenth at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/95141] [8/9/10/11 Regression] Incorrect integer overflow warning message for bitand expression Date: Mon, 18 May 2020 13:28:01 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 8.3.0 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: rguenth at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: rguenth at gcc dot gnu.org X-Bugzilla-Target-Milestone: 8.5 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 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 18 May 2020 13:28:02 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D95141 --- Comment #3 from Richard Biener --- (In reply to Richard Biener from comment #2) > So there's already (OVF) at >=20 > ((long unsigned int) IA1 & 158(OVF)) & 1 >=20 > but we only check >=20 > 375 if (TREE_OVERFLOW_P (ret) > 376 && !TREE_OVERFLOW_P (op0) > 377 && !TREE_OVERFLOW_P (op1)) > 378 overflow_warning (EXPR_LOC_OR_LOC (expr, input_location), > ret, expr); >=20 > which doesn't catch the pre-existing overflow on op0 which then propagate= s. >=20 > The original overflow is introduced folding short 158 to signed char -98(= OVF) > via convert.c:do_narrow: >=20 > 437 /* We should do away with all this once we have a proper > 438 type promotion/demotion pass, see PR45397. */ > 439 expr =3D maybe_fold_build2_loc (dofold, loc, ex_form, typex, > 440 convert (typex, arg0), > 441 convert (typex, arg1)); >=20 > specifically the convert (typex, arg1). >=20 > Now TREE_OVERFLOW in general is quite a fragile thing, but it's tempting = to > adjust the overflow_warning guard for this case ... >=20 > The do_narrow code also specifically looks for overflow cases that matter > and does not perform narrowing then so clearing TREE_OVERFLOW there would > also look reasonable. >=20 > Thus like the following? Should be cheaper than adding walk_tree to the > diagnostic guard. >=20 > diff --git a/gcc/convert.c b/gcc/convert.c > index 42509c518a9..ed00ded1a89 100644 > --- a/gcc/convert.c > +++ b/gcc/convert.c > @@ -436,9 +436,16 @@ do_narrow (location_t loc, > } > /* We should do away with all this once we have a proper > type promotion/demotion pass, see PR45397. */ > + /* Above we checked for all cases where overflow matters, avoid > + geneating overflowed constants here which otherwise propagate > + and cause spurious warnings, see PR95141. */ > + tree converted_arg1 =3D convert (typex, arg1); > + if (TREE_OVERFLOW_P (converted_arg1) > + && !TREE_OVERFLOW_P (arg1)) > + converted_arg1 =3D drop_tree_overflow (converted_arg1); > expr =3D maybe_fold_build2_loc (dofold, loc, ex_form, typex, > convert (typex, arg0), > - convert (typex, arg1)); > + converted_arg1); > return convert (type, expr); > } Regresses FAIL: gcc.dg/overflow-warn-5.c (test for warnings, line 6) which looks like a useful warning to preserve. Lame "walk-tree" variant catching this case: diff --git a/gcc/c/c-fold.c b/gcc/c/c-fold.c index 63becfeaf2c..bd21d247051 100644 --- a/gcc/c/c-fold.c +++ b/gcc/c/c-fold.c @@ -374,6 +374,7 @@ c_fully_fold_internal (tree expr, bool in_init, bool *maybe_const_operands, ret =3D fold (expr); if (TREE_OVERFLOW_P (ret) && !TREE_OVERFLOW_P (op0) + && !(BINARY_CLASS_P (op0) && TREE_OVERFLOW_P (TREE_OPERAND (op0, = 1))) && !TREE_OVERFLOW_P (op1)) overflow_warning (EXPR_LOC_OR_LOC (expr, input_location), ret, expr= ); if (code =3D=3D LSHIFT_EXPR=