From patchwork Wed Aug 23 21:49:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1824997 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=HMpkMkie; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4RWKdy0KPtz1yZs for ; Thu, 24 Aug 2023 07:50:44 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id ABEF1385DC30 for ; Wed, 23 Aug 2023 21:50:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ABEF1385DC30 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692827442; bh=bsNfAp2vjHP+7vAgnp+Xnr+zZqpEqAxj7PcOTRm+r4k=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=HMpkMkietbzCvfoe81ObB9nML7UbGUXZVjALYN4Tjp6oL5Q+fvqx8yPn+fMVtIdvR 5J/iGArTjNpc/I33PL3cu79dBykYZdMnH8rpjUWp9mwZmQ55cyA/Buf1S3jWlQXAsk PZiQDMjsQwZmJIrv9+b1ZKeAR/F+eFDY2C2AqJto= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by sourceware.org (Postfix) with ESMTPS id E4F593857BB3 for ; Wed, 23 Aug 2023 21:50:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E4F593857BB3 Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 37NJoW4D006141 for ; Wed, 23 Aug 2023 14:50:15 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3snrmcr9jy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Wed, 23 Aug 2023 14:50:15 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 23 Aug 2023 14:50:11 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Wed, 23 Aug 2023 14:50:10 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id C73E05B6933; Wed, 23 Aug 2023 14:50:10 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: [PR111109] Fix bit_ior(cond, cond) when comparisons are fp Date: Wed, 23 Aug 2023 14:49:55 -0700 Message-ID: <20230823214955.3494903-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: 6y1FXvpJIBR653kYWI-saO2RCmOYNjf2 X-Proofpoint-ORIG-GUID: 6y1FXvpJIBR653kYWI-saO2RCmOYNjf2 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-08-23_15,2023-08-22_01,2023-05-22_02 X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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-Patchwork-Original-From: Andrew Pinski via Gcc-patches From: Andrew Pinski Reply-To: Andrew Pinski Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The patterns that were added in r13-4620-g4d9db4bdd458, missed that (a > b) and (a <= b) are not inverse of each other for floating point comparisons (if NaNs are supported). Even though there was a check for intergal types, it was only for the result of the cond rather for the type of what is being compared. The fix is to check to see if cmp and icmp are inverse of each other by using the invert_tree_comparison function. OK for trunk and GCC 13 branch? Bootstrapped and tested on x86_64-linux-gnu with no regressions. I added the testcase to execute/ieee as it requires support for NAN. PR tree-optimization/111109 gcc/ChangeLog: * match.pd (ior(cond,cond), ior(vec_cond,vec_cond)): Add check to make sure cmp and icmp are inverse. gcc/testsuite/ChangeLog: * gcc.c-torture/execute/ieee/fp-cmp-cond-1.c: New test. --- gcc/match.pd | 11 ++- .../execute/ieee/fp-cmp-cond-1.c | 78 +++++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-cond-1.c diff --git a/gcc/match.pd b/gcc/match.pd index 85b7d323a19..b666d73b189 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2087,6 +2087,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (bit_and:c (convert? (cmp@0 @01 @02)) @3) (bit_and:c (convert? (icmp@4 @01 @02)) @5)) (if (INTEGRAL_TYPE_P (type) + && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp /* The scalar version has to be canonicalized after vectorization because it makes unconditional loads conditional ones, which means we lose vectorization because the loads may trap. */ @@ -2101,6 +2102,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (cond (cmp@0 @01 @02) @3 zerop) (cond (icmp@4 @01 @02) @5 zerop)) (if (INTEGRAL_TYPE_P (type) + && invert_tree_comparison (cmp, HONOR_NANS (@01)) == icmp /* The scalar version has to be canonicalized after vectorization because it makes unconditional loads conditional ones, which means we lose vectorization because the loads may trap. */ @@ -2113,13 +2115,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (bit_ior (bit_and:c (vec_cond:s (cmp@0 @6 @7) @4 @5) @2) (bit_and:c (vec_cond:s (icmp@1 @6 @7) @4 @5) @3)) - (if (integer_zerop (@5)) + (if (integer_zerop (@5) + && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp) (switch (if (integer_onep (@4)) (bit_and (vec_cond @0 @2 @3) @4)) (if (integer_minus_onep (@4)) (vec_cond @0 @2 @3))) - (if (integer_zerop (@4)) + (if (integer_zerop (@4) + && invert_tree_comparison (cmp, HONOR_NANS (@6)) == icmp) (switch (if (integer_onep (@5)) (bit_and (vec_cond @0 @3 @2) @5)) @@ -2132,7 +2136,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (bit_ior (vec_cond:s (cmp@0 @4 @5) @2 integer_zerop) (vec_cond:s (icmp@1 @4 @5) @3 integer_zerop)) - (vec_cond @0 @2 @3))) + (if (invert_tree_comparison (cmp, HONOR_NANS (@4)) == icmp) + (vec_cond @0 @2 @3)))) /* Transform X & -Y into X * Y when Y is { 0 or 1 }. */ (simplify diff --git a/gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-cond-1.c b/gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-cond-1.c new file mode 100644 index 00000000000..4a3c4b0eee2 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/ieee/fp-cmp-cond-1.c @@ -0,0 +1,78 @@ +/* PR tree-optimization/111109 */ + +/* + f should return 0 if either fa and fb are a nan. + Rather than the value of a or b. +*/ +__attribute__((noipa)) +int f(int a, int b, float fa, float fb) { + const _Bool c = fa < fb; + const _Bool c1 = fa >= fb; + return (c * a) | (c1 * b); +} + +/* + f1 should return 0 if either fa and fb are a nan. + Rather than the value of a&1 or b&1. +*/ +__attribute__((noipa)) +int f1(int a, int b, float fa, float fb) { + const _Bool c = fa < fb; + const _Bool c1 = fa >= fb; + return (c & a) | (c1 & b); +} + +#if __SIZEOF_INT__ == __SIZEOF_FLOAT__ +typedef int v4si __attribute__ ((vector_size (1*sizeof(int)))); +typedef float v4sf __attribute__ ((vector_size (1*sizeof(float)))); +/* + fvf0 should return {0} if either fa and fb are a nan. + Rather than the value of a or b. +*/ +__attribute__((noipa)) +v4si vf0(v4si a, v4si b, v4sf fa, v4sf fb) { + const v4si c = fa < fb; + const v4si c1 = fa >= fb; + return (c & a) | (c1 & b); +} + + +#endif + +int main(void) +{ + float a = __builtin_nan(""); + + if (f(-1,-1, a, a) != 0) + __builtin_abort(); + if (f(-1,-1, a, 0) != 0) + __builtin_abort(); + if (f(-1,-1, 0, a) != 0) + __builtin_abort(); + if (f(-1,-1, 0, 0) != -1) + __builtin_abort(); + + + if (f1(1,1, a, a) != 0) + __builtin_abort(); + if (f1(1,1, a, 0) != 0) + __builtin_abort(); + if (f1(1,1, 0, a) != 0) + __builtin_abort(); + if (f1(1,1, 0, 0) != 1) + __builtin_abort(); + +#if __SIZEOF_INT__ == __SIZEOF_FLOAT__ + v4si b = {-1}; + v4sf c = {a}; + v4sf d = {0.0}; + if (vf0(b,b, c, c)[0] != 0) + __builtin_abort(); + if (vf0(b,b, c, d)[0] != 0) + __builtin_abort(); + if (vf0(b,b, d, c)[0] != 0) + __builtin_abort(); + if (vf0(b,b, d, d)[0] != b[0]) + __builtin_abort(); +#endif +}