From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x52a.google.com (mail-ed1-x52a.google.com [IPv6:2a00:1450:4864:20::52a]) by sourceware.org (Postfix) with ESMTPS id ACBF53858D28 for ; Sun, 10 Oct 2021 08:03:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org ACBF53858D28 Received: by mail-ed1-x52a.google.com with SMTP id r18so54266379edv.12 for ; Sun, 10 Oct 2021 01:03:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:user-agent:in-reply-to :references:message-id:mime-version:content-transfer-encoding; bh=Tll5CoK8BDsHa1GuLgLORmRF/6Ud150uMoup8idU3Mw=; b=tyJ7wLI4eKRM53SEbiYLFU7TqbDqtRbeI/PB2v+8FeHY0Zo4a3/dUm+TPGZhTx58Bh Ggu7U2jOIDPo1xbLJNq5dv+7+4ggw5sZU0N3f53XjaHWArdu7wyVLmhVJAv0IsGdQ06N 8OBSRiexcRXFIfw3o03G125m07yV8T2TB7AIP7fx5X8Hy0FZwbQmYRFLImup27mZ7YQO pM7izE5W3pUnNblk1HMyFYdEqahnIbufGgOUn4kloM/IVSR0bl17jDZ1qyEVsgfIGB61 gVawhlePlDU8oQDP6Yz/DmMQjqpyevXqmfH3uFSbbEXSs62EXXRkWYoPx4OjqD5x/U/f 6vuQ== X-Gm-Message-State: AOAM532/GbPlHO3ogzosEZV7M+h2WWsNfQDxscrp5rxly2hUskdi7yor qrqiFe4HsW2Rcf8cjFhTFlcMiJrpN2A= X-Google-Smtp-Source: ABdhPJzTJMDu5Wmqu8EzC+Kupf54Txk+GilgiFXDJEL8/6GOGMOIEWse3iAGKA6r6b8SQC71NvljcA== X-Received: by 2002:a50:e145:: with SMTP id i5mr30442276edl.16.1633852998629; Sun, 10 Oct 2021 01:03:18 -0700 (PDT) Received: from [127.0.0.1] (dynamic-095-114-030-002.95.114.pool.telefonica.de. [95.114.30.2]) by smtp.gmail.com with ESMTPSA id u16sm1808684ejy.14.2021.10.10.01.03.17 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 10 Oct 2021 01:03:18 -0700 (PDT) Date: Sun, 10 Oct 2021 10:03:15 +0200 From: Richard Biener To: apinski@marvell.com, apinski--- via Gcc-patches , gcc-patches@gcc.gnu.org CC: Andrew Pinski Subject: =?US-ASCII?Q?Re=3A_=5BPATCH=5D_tree-optimiz?= =?US-ASCII?Q?ation=3A_=5BPR102622=5D=3A_wrong?= =?US-ASCII?Q?_code_due_to_signed_one_bit_integer_and_=22a=3F-1=3A0=22?= User-Agent: K-9 Mail for Android In-Reply-To: <1633844539-26219-1-git-send-email-apinski@marvell.com> References: <1633844539-26219-1-git-send-email-apinski@marvell.com> Message-ID: <7A6A6B75-83F7-4E7D-A559-7A08E352E0FC@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 10 Oct 2021 08:03:21 -0000 On October 10, 2021 7:42:19 AM GMT+02:00, apinski--- via Gcc-patches wrote: >From: Andrew Pinski > >So it turns out this is kinda of a latent bug but not really latent=2E >In GCC 9 and 10, phi-opt would transform a?-1:0 (even for signed 1-bit in= teger) >to -(type)a but the type is an one bit integer which means the negation i= s >undefined=2E GCC 11 fixed the problem by checking for a?pow2cst:0 transfo= rmation >before a?-1:0 transformation=2E > >When I added the transformations to match=2Epd, I had swapped the order n= ot paying >attention and I didn't expect anything of it=2E Because there was no test= case failing >due to this=2E >Anyways this fixes the problem on the trunk by swapping the order in matc= h=2Epd and >adding a comment of why the order is this way=2E > >I will try to come up with a patch for GCC 9 and 10 series later on which= fixes >the problem there too=2E > >Note I didn't include the original testcase which requires the vectorizer= and AVX-512f >as I can't figure out the right dg options to restrict it to avx-512f but= I did come up >with a testcase which shows the problem and even more shows the problem w= ith the 9/10 >series as mentioned=2E > >OK? Bootstrapped and tested on x86_64-linux-gnu=2E OK=2E=20 Richard=2E=20 > PR tree-optimization/102622 > >gcc/ChangeLog: > > * match=2Epd: Swap the order of a?pow2cst:0 and a?-1:0 transformations= =2E > Swap the order of a?0:pow2cst and a?0:-1 transformations=2E > >gcc/testsuite/ChangeLog: > > * gcc=2Ec-torture/execute/bitfld-10=2Ec: New test=2E >--- > gcc/match=2Epd | 26 ++++++++++++------- > =2E=2E=2E/gcc=2Ec-torture/execute/bitfld-10=2Ec | 24 +++++++++++= ++++++ > 2 files changed, 41 insertions(+), 9 deletions(-) > create mode 100644 gcc/testsuite/gcc=2Ec-torture/execute/bitfld-10=2Ec > >diff --git a/gcc/match=2Epd b/gcc/match=2Epd >index 9d7c1ac637f=2E=2Ec153e9a6e98 100644 >--- a/gcc/match=2Epd >+++ b/gcc/match=2Epd >@@ -3949,15 +3949,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > /* a ? 1 : 0 -> a if 0 and 1 are integral types=2E */ > (if (integer_onep (@1)) > (convert (convert:boolean_type_node @0))) >- /* a ? -1 : 0 -> -a=2E */ >- (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1)) >- (negate (convert (convert:boolean_type_node @0)))) > /* a ? powerof2cst : 0 -> a << (log2(powerof2cst)) */ > (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@1)) > (with { > tree shift =3D build_int_cst (integer_type_node, tree_log2 (@1)); > } >- (lshift (convert (convert:boolean_type_node @0)) { shift; }))))) >+ (lshift (convert (convert:boolean_type_node @0)) { shift; }))) >+ /* a ? -1 : 0 -> -a=2E No need to check the TYPE_PRECISION not bein= g 1 >+ here as the powerof2cst case above will handle that case correctl= y=2E */ >+ (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1)) >+ (negate (convert (convert:boolean_type_node @0)))))) > (if (integer_zerop (@1)) > (with { > tree booltrue =3D constant_boolean_node (true, boolean_type_node); >@@ -3966,16 +3967,23 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) > /* a ? 0 : 1 -> !a=2E */ > (if (integer_onep (@2)) > (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))) >- /* a ? -1 : 0 -> -(!a)=2E */ >- (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2)) >- (negate (convert (bit_xor (convert:boolean_type_node @0) { booltru= e; } )))) > /* a ? powerof2cst : 0 -> (!a) << (log2(powerof2cst)) */ >- (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2)) >+ (if (INTEGRAL_TYPE_P (type) && integer_pow2p (@2) >+ && TYPE_PRECISION (type) !=3D 1) > (with { > tree shift =3D build_int_cst (integer_type_node, tree_log2 (@2)); > } > (lshift (convert (bit_xor (convert:boolean_type_node @0) { booltr= ue; } )) >- { shift; })))))))) >+ { shift; }))) >+ /* a ? -1 : 0 -> -(!a)=2E No need to check the TYPE_PRECISION not = being 1 >+ here as the powerof2cst case above will handle that case correctl= y=2E */ >+ (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2)) >+ (negate (convert (bit_xor (convert:boolean_type_node @0) { booltru= e; } )))) >+ ) >+ ) >+ ) >+ ) >+) > #endif >=20 > /* Simplification moved from fold_cond_expr_with_comparison=2E It may a= lso >diff --git a/gcc/testsuite/gcc=2Ec-torture/execute/bitfld-10=2Ec b/gcc/te= stsuite/gcc=2Ec-torture/execute/bitfld-10=2Ec >new file mode 100644 >index 00000000000=2E=2Ebdbf5733ce7 >--- /dev/null >+++ b/gcc/testsuite/gcc=2Ec-torture/execute/bitfld-10=2Ec >@@ -0,0 +1,24 @@ >+/* PR tree-optimization/102622 */ >+/* Wrong code introduced due to phi-opt >+ introducing undefined signed interger overflow >+ with one bit signed integer negation=2E */ >+ >+struct f{signed t:1;}; >+int g(struct f *a, int t) __attribute__((noipa)); >+int g(struct f *a, int t) >+{ >+ if (t) >+ a->t =3D -1; >+ else >+ a->t =3D 0; >+ int t1 =3D a->t; >+ if (t1) return 1; >+ return t1; >+} >+ >+int main(void) >+{ >+ struct f a; >+ if (!g(&a, 1)) __builtin_abort(); >+ return 0; >+}