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: 136729 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp739801vqm; Wed, 23 Aug 2023 14:51:11 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGnadtU3cHEpcmNYwUzbTsuV/D2w8WHFHghfvwFCdg28xR85WUuLpmnBymhz9nWnd4THS+f X-Received: by 2002:aa7:c1d3:0:b0:523:d1e0:7079 with SMTP id d19-20020aa7c1d3000000b00523d1e07079mr9724196edp.21.1692827471158; Wed, 23 Aug 2023 14:51:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692827471; cv=none; d=google.com; s=arc-20160816; b=vFkk483yWu3LqOHCurT7vhDNoFG3U5m/mNbvFBVa2ynIAouliALqYq8suytfv0OeLk Fic7II+kE5zFl5RN7jnHb7wnLK3R51eQsQysg71Oiyz8xsHIdeOeQoSnq5EAEFCH4gO5 9+u3jE3PnZfF24M/fGVnx/SDYvOiW6AAbkzA547KO6JUh5H5AZP/xKuG5uuo2sXFybRi +1PQLYDFh/Y2qxvqlMII/CbBTI6n0kNHAE/x09FF9fQh5XiniE7QxRXpVYqhN4f5VbBS blb08Qm+KlJaHRY5yfEx9y6Ahngta60eNkY7OzRO00F+21W4svwVnXvS6JHoRoC9s0WI CeTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=bsNfAp2vjHP+7vAgnp+Xnr+zZqpEqAxj7PcOTRm+r4k=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=IRbuVVoJUe4nSxH7mTLwZE3Tn6MEcg46GASnyqQPbjNMC1SdCwfjF+MVakrLj+SRbZ 0imoj6rWrqE3BOs2/CjTUOEdoPhemT0p0H4toU0/yYyTkuT+pHZoEJOC6Iuiy70gueY8 zfr0OTXZb2DdhAPJ2E9yKY/pjZmQTdEe85c55l+sj5qO1UUSkEjmsVH88XBHutrXa7N2 cMwwp0bwPOE54AT2XDy5XuLkM8T4+0jh0GT0oqkjUKR8R1lQHoThBdm4V73s6KR1+gK4 7Uwe+V0FD1Z89mGDzqPRoabrHsOqJC0mWsNu++l0Z+xjvk+HpUtdZZ0HYzxTZ0yLGP0B /ZqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=c2ZJua2D; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id a17-20020aa7cf11000000b00529f79d033esi7913609edy.262.2023.08.23.14.51.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Aug 2023 14:51:11 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=c2ZJua2D; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 60296385AC09 for ; Wed, 23 Aug 2023 21:51:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 60296385AC09 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1692827466; 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=c2ZJua2DqAD3Er80XLHIxnqUF39gt4eLHbfRiprbcXqx5LGCKxFz0lklyP1kDix1H hzCDa2rmkX/MnYoAqkb7pXIzyZeTHgx79kcPfXVBzP0zl0QA8CTROc2GlAYpif75VZ BOOcJtw5ctOJGVX5jJM78r4yEEped+zQgjEc/bDs= 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775058258409946718 X-GMAIL-MSGID: 1775058258409946718 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 +}