From patchwork Wed Jul 31 22:30:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1967354 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=quicinc.com header.i=@quicinc.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=e2ylPuKM; 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 4WZ6Hp4vkkz1yYq for ; Thu, 1 Aug 2024 08:30:44 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 472B43858408 for ; Wed, 31 Jul 2024 22:30:40 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by sourceware.org (Postfix) with ESMTPS id 53E913858C56 for ; Wed, 31 Jul 2024 22:30:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 53E913858C56 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=quicinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 53E913858C56 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722465022; cv=none; b=KrDKac62UH9gS3sp3qcMV9jxpgiFh682NwnjE5AC2RkKqfWqa7ojmbOcf0oSBWLg1Bd922rcxyH72OmKsIBt+6uqqXEjtzAilA/FXk4l+yPIRkySib8hcuVYmBd9xqTxucAFN6wzGrad8vA2mDAsVpT13OTIjkR5NWWKH4+wWis= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722465022; c=relaxed/simple; bh=hIYXDZlayRoVuZZ0tO23LZAh+z5bFIckhbZYVzh/hkY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=LZuMIZiXuAY/IdhQdREz8M/3XlQ9M1x/VhlBZNHgujNMl2xDBnto564PnK16EI/88B6GBLfr3Tju/FO/2uATlSIlksDJMI4QnrWXwp09UulXdfw0psuTNjox3NTy5pCLp/Fcd7wNvZaNiIzQ+FeuTZ+YeYHqfbEl/1WNORVBjos= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279866.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 46VAwgNm030520 for ; Wed, 31 Jul 2024 22:30:18 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:message-id :mime-version:subject:to; s=qcppdkim1; bh=jyM3JT0e0zsHoBjcW956Aj KZmXu++go0pr7XZVn6nDY=; b=e2ylPuKMqYFUfjqinNxJrVsqfi85FJeUmFudcW JQ45Q4/QDWDIYNhJVgIdY8MPLei270ngMefb0Fj1sxiqI2gqTpmXM12KQvX98LzI +AFggYYU0w+McS6yjpoLUoqCVJQREiK7Bx9qoAe7pSdh5TaXTBR3BQMOc3CM4OWZ ETmhEul59RbKJQSedASCfo0ZnWFInB02GscN5OA17OIklFsbt8SLaeHCzoKBUyaY NzNueuD/iU2wJ40vUgEKPc7KRWeGmZv7pq7s+DWsev1NZsfAUmEmD3hRM3fiSMww J66h+Cy6uPOdKUH5AF0zgZz7vNK40KHU5lymZm+lABOLuPkA== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 40qm081qvf-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 31 Jul 2024 22:30:18 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA04.qualcomm.com (8.17.1.19/8.17.1.19) with ESMTPS id 46VMUHtc017935 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 31 Jul 2024 22:30:17 GMT Received: from hu-apinski-lv.qualcomm.com (10.49.16.6) by nasanex01c.na.qualcomm.com (10.45.79.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Wed, 31 Jul 2024 15:30:17 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH v2] match: Fix wrong code due to `(a ? e : f) !=/== (b ? e : f)` patterns [PR116120] Date: Wed, 31 Jul 2024 15:30:07 -0700 Message-ID: <20240731223007.1084404-1-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01a.na.qualcomm.com (10.47.209.196) To nasanex01c.na.qualcomm.com (10.45.79.139) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-GUID: qE71Cnd-VYOkgwTdz900A6-fGsm35cGu X-Proofpoint-ORIG-GUID: qE71Cnd-VYOkgwTdz900A6-fGsm35cGu X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-07-31_10,2024-07-31_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 adultscore=0 bulkscore=0 mlxscore=0 priorityscore=1501 mlxlogscore=999 spamscore=0 clxscore=1015 malwarescore=0 impostorscore=0 phishscore=0 lowpriorityscore=0 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2407110000 definitions=main-2407310158 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, PLING_QUERY, SPF_HELO_NONE, SPF_NONE, 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org When this pattern was converted from being only dealing with 0/-1, we missed that if `e == f` is true then the optimization is wrong and needs an extra check for that. This changes the patterns to be: /* (a ? x : y) != (b ? x : y) --> (a^b & (x != y)) ? TRUE : FALSE */ /* (a ? x : y) == (b ? x : y) --> (a^b & (x != y)) ? FALSE : TRUE */ /* (a ? x : y) != (b ? y : x) --> (a^b | (x == y)) ? FALSE : TRUE */ /* (a ? x : y) == (b ? y : x) --> (a^b | (x == y)) ? TRUE : FALSE */ Also this can't be done if the X can be a NaNs either. Since that changes the value there too. This still produces better code than the original case and in many cases (x != y) will still reduce to either false or true. With this change we also need to make sure `a`, `b` and the resulting types are all the same for the same reason as the previous patch. I updated (well added) to the testcases to make sure there are the right amount of comparisons left. Changes since v1: * v2: Fixed the testcase names and fixed dg-run to be `dg-do run`. Added a check for HONORS_NANS too. Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/116120 gcc/ChangeLog: * match.pd (`(a ? x : y) eq/ne (b ? x : y)`): Add test for `x != y` in result. (`(a ? x : y) eq/ne (b ? y : x)`): Add test for `x == y` in result. gcc/testsuite/ChangeLog: * g++.dg/tree-ssa/pr111150.C: Add extra checks on the test. * gcc.dg/tree-ssa/pr111150-1.c: Likewise. * gcc.dg/tree-ssa/pr111150.c: Likewise. * g++.dg/torture/pr116120-1.C: New test. * g++.dg/torture/pr116120-2.C: New test. Signed-off-by: Andrew Pinski --- gcc/match.pd | 23 ++++++---- gcc/testsuite/g++.dg/torture/pr116120-1.C | 32 +++++++++++++ gcc/testsuite/g++.dg/torture/pr116120-2.C | 53 ++++++++++++++++++++++ gcc/testsuite/g++.dg/tree-ssa/pr111150.C | 10 ++++ gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c | 9 ++++ gcc/testsuite/gcc.dg/tree-ssa/pr111150.c | 1 + 6 files changed, 120 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/g++.dg/torture/pr116120-1.C create mode 100644 gcc/testsuite/g++.dg/torture/pr116120-2.C diff --git a/gcc/match.pd b/gcc/match.pd index 881a827860f..c9c8478d286 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5632,21 +5632,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (vec_cond (bit_and (bit_not @0) @1) @2 @3))) #endif -/* (a ? x : y) != (b ? x : y) --> (a^b) ? TRUE : FALSE */ -/* (a ? x : y) == (b ? x : y) --> (a^b) ? FALSE : TRUE */ -/* (a ? x : y) != (b ? y : x) --> (a^b) ? FALSE : TRUE */ -/* (a ? x : y) == (b ? y : x) --> (a^b) ? TRUE : FALSE */ +/* (a ? x : y) != (b ? x : y) --> (a^b & (x != y)) ? TRUE : FALSE */ +/* (a ? x : y) == (b ? x : y) --> (a^b & (x != y)) ? FALSE : TRUE */ +/* (a ? x : y) != (b ? y : x) --> (a^b | (x == y)) ? FALSE : TRUE */ +/* (a ? x : y) == (b ? y : x) --> (a^b | (x == y)) ? TRUE : FALSE */ +/* These are only valid if x and y don't have NaNs. */ (for cnd (cond vec_cond) (for eqne (eq ne) (simplify (eqne:c (cnd @0 @1 @2) (cnd @3 @1 @2)) - (if (types_match (TREE_TYPE (@0), TREE_TYPE (@3))) - (cnd (bit_xor @0 @3) { constant_boolean_node (eqne == NE_EXPR, type); } + (if (!HONOR_NANS (@1) + && types_match (TREE_TYPE (@0), TREE_TYPE (@3)) + && types_match (type, TREE_TYPE (@0))) + (cnd (bit_and (bit_xor @0 @3) (ne:type @1 @2)) + { constant_boolean_node (eqne == NE_EXPR, type); } { constant_boolean_node (eqne != NE_EXPR, type); }))) (simplify (eqne:c (cnd @0 @1 @2) (cnd @3 @2 @1)) - (if (types_match (TREE_TYPE (@0), TREE_TYPE (@3))) - (cnd (bit_xor @0 @3) { constant_boolean_node (eqne != NE_EXPR, type); } + (if (!HONOR_NANS (@1) + && types_match (TREE_TYPE (@0), TREE_TYPE (@3)) + && types_match (type, TREE_TYPE (@0))) + (cnd (bit_ior (bit_xor @0 @3) (eq:type @1 @2)) + { constant_boolean_node (eqne != NE_EXPR, type); } { constant_boolean_node (eqne == NE_EXPR, type); }))))) /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask diff --git a/gcc/testsuite/g++.dg/torture/pr116120-1.C b/gcc/testsuite/g++.dg/torture/pr116120-1.C new file mode 100644 index 00000000000..209946f17a4 --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/pr116120-1.C @@ -0,0 +1,32 @@ +// { dg-do run } +// PR tree-optimization/116120 + +// The optimization for `(a ? x : y) != (b ? x : y)` +// missed that x and y could be the same value. + +typedef int v4si __attribute((__vector_size__(1 * sizeof(int)))); +v4si f1(v4si a, v4si b, v4si c, v4si d, v4si e, v4si f) { + v4si X = a == b ? e : f; + v4si Y = c == d ? e : f; + return (X != Y); // ~(X == Y ? -1 : 0) (x ^ Y) +} + +int f2(int a, int b, int c, int d, int e, int f) { + int X = a == b ? e : f; + int Y = c == d ? e : f; + return (X != Y) ? -1 : 0; // ~(X == Y ? -1 : 0) (x ^ Y) +} + +int main() +{ + v4si a = {0}; + v4si b = {0}; // a == b, true + v4si c = {2}; + v4si d = {3}; // c == b, false + v4si e = {0}; + v4si f = e; + v4si r = f1(a,b,c,d,e, f); + int r1 = f2(a[0], b[0], c[0], d[0], e[0], f[0]); + if (r[0] != r1) + __builtin_abort(); +} diff --git a/gcc/testsuite/g++.dg/torture/pr116120-2.C b/gcc/testsuite/g++.dg/torture/pr116120-2.C new file mode 100644 index 00000000000..e9fee17a262 --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/pr116120-2.C @@ -0,0 +1,53 @@ +// { dg-do run } +// PR tree-optimization/116120 + +// The optimization for `(a ? x : y) == (b ? x : y)` +// missed that x and y could be the same value +// This can't be done if x and y support NaNs since `NaN == NaN` is always false +// And dominates the whole expression rather than supporting it. + +typedef int v1si __attribute((__vector_size__(1 * sizeof(int)))); +typedef float v1sf __attribute((__vector_size__(1 * sizeof(float)))); + +v1si f1(v1si a, v1si b, v1si c, v1si d, v1sf e, v1sf f) __attribute__((noinline)); +v1si f1(v1si a, v1si b, v1si c, v1si d, v1sf e, v1sf f) { + v1sf X = a == b ? e : f; + v1sf Y = c == d ? f : e; + return (X == Y); // ~(X == Y ? -1 : 0) (x ^ Y) +} + +int f2(int a, int b, int c, int d, float e, float f) __attribute__((noinline)); +int f2(int a, int b, int c, int d, float e, float f) { + float X = a == b ? e : f; + float Y = c == d ? f : e; + return (X == Y) ? -1 : 0; // ~(X == Y ? -1 : 0) (x ^ Y) +} + +int main() +{ + v1si a = {0}; + v1si b = {0}; // a == b, true + v1si c = {2}; + v1si d = {3}; // c == b, false + v1sf e; + v1sf f; + + /* Test signed 0s. */ + e = (v1sf){0.0}; + f = -e; + v1si r = f1(a,b,c,d,e, f); + int r1 = f2(a[0], b[0], c[0], d[0], e[0], f[0]); + if (r[0] != r1) + __builtin_abort(); + + /* Test NaNs */ +#if __FLT_HAS_QUIET_NAN__ + e = (v1sf){__builtin_nanf("")}; + f = e; + /* Test NaNs */ + r = f1(a,b,c,d,e, f); + r1 = f2(a[0], b[0], c[0], d[0], e[0], f[0]); + if (r[0] != r1) + __builtin_abort(); +#endif +} diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr111150.C b/gcc/testsuite/g++.dg/tree-ssa/pr111150.C index ac5d3ef15d8..82af8f96878 100644 --- a/gcc/testsuite/g++.dg/tree-ssa/pr111150.C +++ b/gcc/testsuite/g++.dg/tree-ssa/pr111150.C @@ -32,3 +32,13 @@ v4si f4_(v4si a, v4si b, v4si c, v4si d, v4si e, v4si f) { /* For each testcase, should produce only one VEC_COND_EXPR for X^Y. */ /* { dg-final { scan-tree-dump-times " VEC_COND_EXPR " 4 "forwprop1" } } */ +/* 2 IOR, one each for f1 and f2. + 2 AND, one each for f3 and f4. */ +/* { dg-final { scan-tree-dump-times " & " 2 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times " \\| " 2 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times " \\^ " 4 "forwprop1" } } */ +/* 8 eq comparisons from each of `a == b`/`c == d`. + 2 more to check that `e == f` + 2 ne comparisons to check that `e != f`. */ +/* { dg-final { scan-tree-dump-times " == " 10 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times " != " 2 "forwprop1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c index 6f4b21ac6bc..1970f7fe496 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c @@ -69,4 +69,13 @@ _Bool f4_(int a, int b, int c, int d, int e, int f) { /* Should generate one bit_xor_expr for each testcase. */ /* { dg-final { scan-tree-dump-not "cond_expr, " "forwprop1" } } */ +/* 2 IOR, one each for f1 and f2. + 2 AND, one each for f3 and f4. */ +/* { dg-final { scan-tree-dump-times "bit_ior_expr, " 2 "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times "bit_and_expr, " 2 "forwprop1" } } */ /* { dg-final { scan-tree-dump-times "bit_xor_expr, " 4 "forwprop1" } } */ +/* 8 eq comparisons from each of `a == b`/`c == d`. + 2 more to check that `e == f` + 2 ne comparisons to check that `e != f`. */ +/* { dg-final { scan-tree-dump-times "