From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=marvell.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=marvell.com 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marvell.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=pfpt0220; bh=bsNfAp2vjHP+7vAgnp+Xnr+zZqpEqAxj7PcOTRm+r4k=; b=ivv7iLcCrh50BueCsFmI4NPVAjaqPzcyjDTWy8H6p6ou1JrgJRLandw97jyaSXr4JIU+ LjK7i/2P9xE4GcJKsXnReNRvosfp+QRDBh3PMMwLo9EQF29sAT+i2vRz3NakHFKXcfiD jcK6fpZ9YlxL4sXzt6Xmq1ckUoo/vuPmMgKk7rE9sTM70/VQsHYHMhsV4D2z6RUyNH13 xhAfotzGhrkICDzg4J5cRhZ0dy3Tlrd0L/hYhPZ/46TGkFSKv9XyMRGTCR8WCN72ImEs 5LbTDpRhq76JYSJuIsXy2VC/Ka+gVY6nCcGW7+sJDDsK4dCpCbopFakrzNfl1VjTnRyW xQ== 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) From: Andrew Pinski 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 Content-Transfer-Encoding: 8bit Content-Type: text/plain 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 List-Id: 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 +} -- 2.31.1