From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 2D69C385B83E; Wed, 24 Aug 2022 07:57:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2D69C385B83E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1661327822; bh=uzst4pwHPyV/+5/y5FpDOjtbd/yb1dlm+aXuLH29JiU=; h=From:To:Subject:Date:In-Reply-To:References:From; b=YnOitPa9013O50Av5OrEQ7wVcNjS3nSAc3OFAWcsPJVYN8eV9ydaPL7rKYG+TWA+h WFuf+HHVsL5zxcI0zW2Jzs9pJhuzdSbYemfLX+PEak4a7MfAl8u0obDhcVpjH3fjlT zylCeAFHXhInQvoZgmUYbUXSPhIXp85NCLfzpywU= From: "rdapp at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/106701] Compiler does not take into account number range limitation to avoid subtract from immediate Date: Wed, 24 Aug 2022 07:57:01 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 11.2.0 X-Bugzilla-Keywords: missed-optimization X-Bugzilla-Severity: normal X-Bugzilla-Who: rdapp 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: cf_gcctarget cc short_desc 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=3D106701 rdapp at gcc dot gnu.org changed: What |Removed |Added ---------------------------------------------------------------------------- Target|s390 |s390 x86_64-linux-gnu CC| |glisse at gcc dot gnu.org, | |rdapp at gcc dot gnu.org, | |rguenth at gcc dot gnu.org Summary|s390: Compiler does not |Compiler does not take into |take into account number |account number range |range limitation to avoid |limitation to avoid |subtract from immediate |subtract from immediate --- Comment #1 from rdapp at gcc dot gnu.org --- Added x86 to targets because we don't seem to optimize this there either (at least I didn't see it on my recent-ish GCC). The following (not regtested) helps on s390 diff --git a/gcc/match.pd b/gcc/match.pd index e486b4be282c..2ebbf68010f9 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -7992,3 +7992,27 @@ and, (match (bitwise_induction_p @0 @2 @3) (bit_not (nop_convert1? (bit_xor@0 (convert2? (lshift integer_onep@1 @2)) @3)))) + +/* cst - a -> cst ^ a if 0 >=3D a <=3D cst and integer_pow2p (cst + 1). */ +#if GIMPLE +(simplify + (minus INTEGER_CST@0 @1) + (with { + wide_int w =3D wi::to_wide (@0) + 1; + value_range vr; + wide_int wmin =3D w; + wide_int wmax =3D w; + if (get_global_range_query ()->range_of_expr (vr, @1) + && vr.kind () =3D=3D VR_RANGE) + { + wmin =3D vr.lower_bound (); + wmax =3D vr.upper_bound (); + } + } + (if (wi::exact_log2 (w) !=3D -1 + && wi::geu_p (wmin, 0) + && wi::leu_p (wmax, w)) + (bit_xor @0 @1)) + ) +) +#endif but it can surely be improved by some match.pd magic still. A second quest= ion would be, do we unconditionally want to simplify this or should it rather be backend dependent?=