From patchwork Tue Jul 16 13:35:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eikansh Gupta X-Patchwork-Id: 93987 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 19E1A3861807 for ; Tue, 16 Jul 2024 13:36:08 +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 3012A3858C33 for ; Tue, 16 Jul 2024 13:35:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3012A3858C33 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 3012A3858C33 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=1721136942; cv=none; b=hG/j5Nr75A9MvvbyqVrloftVrcp4SSbYxYuODxuqRW6wmktFZcQa6g7FyV/Kgc4ekHSIE1xrTceA+vtdNev534kXwkKCvLt/YL9ctp+TFpWyXb1Hu42grLjg3XsQc5pQlIrw7RMFmf/jSZ4vU9N3pRVWWoRT+B+wJ1t3TZ5wmCU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721136942; c=relaxed/simple; bh=w6gDw8vEiF7AMqsywcCtStygOTsR3KEAkVnmEYCLHjY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=YpuyFb6eOiw9LNkidbSZ0IcDSIJcWBHtnWXW/oaWbeNYvlLcXU+dwCKt94yOi3bPV4PXqBJaSXLr2yAJbF11zCz88aWpPn8wFD9ZwiOdnw1bgxuwcmCpNvIXstKiUcP+f2hD9NWy9dwa9mNGb9zBIYU6UEEUXYKTQYAzk6iUxNc= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279867.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 46GDVbO0016233 for ; Tue, 16 Jul 2024 13:35:39 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-type:date:from:message-id:mime-version:subject:to; s= qcppdkim1; bh=clxyVdBRGDWb9JttFo/rZu7m1Xk307nDL4s7dMVyP1I=; b=Tx GwS+JWzX8f9P0cgl7K0uLSeVpx1ijDRwNr6jMWtvEQ2/CGqk/LT5TCb/yURvX6fg mmQiH4Ah9/6mlgGVVagIXzNmynsUkm32Uoagxh93IAB7M1+g/tLICnv8FYKTdCp6 mu72vVX4nT+/DXB+aub9VOcBwodr1ul58gQZo33e9x5S2QuBe4aHlGGthY9kRAwd hjpkfJmoXP3WKMBD6MAUWpjqPliQhTmlRdiO7TT+3dqwsFtKEgxPIFrZdBeDUuRC oze0iIdnIsjZcODCsQNpHMmYjNGMAiWLzB2DXfbuZ6AEzAQtIK6rQxxWlcLJGvgF o0YdXDtX9JnrG170Sc8g== Received: from nalasppmta04.qualcomm.com (Global_NAT1.qualcomm.com [129.46.96.20]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 40bf9efc52-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 16 Jul 2024 13:35:38 +0000 (GMT) Received: from nalasex01b.na.qualcomm.com (nalasex01b.na.qualcomm.com [10.47.209.197]) by NALASPPMTA04.qualcomm.com (8.17.1.19/8.17.1.19) with ESMTPS id 46GDZcFu015777 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 16 Jul 2024 13:35:38 GMT Received: from hu-eikagupt-hyd.qualcomm.com (10.80.80.8) by nalasex01b.na.qualcomm.com (10.47.209.197) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Tue, 16 Jul 2024 06:35:36 -0700 From: Eikansh Gupta To: CC: Eikansh Gupta Subject: [PATCH v2] MATCH: Simplify (a ? x : y) eq/ne (b ? x : y) [PR111150] Date: Tue, 16 Jul 2024 19:05:18 +0530 Message-ID: <20240716133518.628-1-quic_eikagupt@quicinc.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-Originating-IP: [10.80.80.8] X-ClientProxiedBy: nasanex01a.na.qualcomm.com (10.52.223.231) To nalasex01b.na.qualcomm.com (10.47.209.197) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: i9jYZXRVzYaMeYKYzlByhfmehpWVfcyN X-Proofpoint-GUID: i9jYZXRVzYaMeYKYzlByhfmehpWVfcyN 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-15_19,2024-07-16_02,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1011 priorityscore=1501 bulkscore=0 lowpriorityscore=0 suspectscore=0 impostorscore=0 spamscore=0 phishscore=0 mlxlogscore=949 mlxscore=0 adultscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2406140001 definitions=main-2407160101 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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~patchwork=sourceware.org@gcc.gnu.org This patch adds match pattern for `(a ? x : y) eq/ne (b ? x : y)`. In forwprop1 pass, depending on the type of `a` and `b`, GCC produces `vec_cond` or `cond_expr`. Based on the observation that `(x != y)` is TRUE, the pattern can be optimized to produce `(a^b ? TRUE : FALSE)`. The patch adds match pattern for a, b: (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) ? TRUE : FALSE (a ? x : y) == (b ? y : x) --> (a^b) ? FALSE : TRUE PR tree-optimization/111150 gcc/ChangeLog: * match.pd (`(a ? x : y) eq/ne (b ? x : y)`): New pattern. (`(a ? x : y) eq/ne (b ? y : x)`): New pattern. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr111150.c: New test. * gcc.dg/tree-ssa/pr111150-1.c: New test. * g++.dg/tree-ssa/pr111150.C: New test. Signed-off-by: Eikansh Gupta --- gcc/match.pd | 15 +++++ gcc/testsuite/g++.dg/tree-ssa/pr111150.C | 33 ++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c | 72 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr111150.c | 22 +++++++ 4 files changed, 142 insertions(+) create mode 100644 gcc/testsuite/g++.dg/tree-ssa/pr111150.C create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr111150.c diff --git a/gcc/match.pd b/gcc/match.pd index 3759c64d461..7c125255ea3 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5577,6 +5577,21 @@ 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 */ +(for cnd (cond vec_cond) + (for eqne (eq ne) + (simplify + (eqne:c (cnd @0 @1 @2) (cnd @3 @1 @2)) + (cnd (bit_xor @0 @3) { 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)) + (cnd (bit_xor @0 @3) { constant_boolean_node (eqne != NE_EXPR, type); } + { constant_boolean_node (eqne == NE_EXPR, type); })))) + /* Canonicalize mask ? { 0, ... } : { -1, ...} to ~mask if the mask types are compatible. */ (simplify diff --git a/gcc/testsuite/g++.dg/tree-ssa/pr111150.C b/gcc/testsuite/g++.dg/tree-ssa/pr111150.C new file mode 100644 index 00000000000..ca02d8dc51e --- /dev/null +++ b/gcc/testsuite/g++.dg/tree-ssa/pr111150.C @@ -0,0 +1,33 @@ +/* PR tree-optimization/111150 */ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-forwprop1" } */ +typedef int v4si __attribute((__vector_size__(4 * sizeof(int)))); + +/* Before the patch, VEC_COND_EXPR was generated for each statement in the + function. This resulted in 3 VEC_COND_EXPR. */ +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); +} + +v4si f2_(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); +} + +v4si f3_(v4si a, v4si b, v4si c, v4si d, v4si e, v4si f) { + v4si X = a == b ? e : f; + v4si Y = c == d ? f : e; + return (X != Y); +} + +v4si f4_(v4si a, v4si b, v4si c, v4si d, v4si e, v4si f) { + v4si X = a == b ? e : f; + v4si Y = c == d ? f : e; + return (X == Y); +} + +/* For each testcase, should produce only one VEC_COND_EXPR for X^Y. */ +/* { dg-final { scan-tree-dump-times " VEC_COND_EXPR " 4 "forwprop1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c new file mode 100644 index 00000000000..6f4b21ac6bc --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr111150-1.c @@ -0,0 +1,72 @@ +/* PR tree-optimization/111150 */ +/* { dg-do compile } */ +/* { dg-options "-O1 -fgimple -fdump-tree-forwprop1-raw" } */ + +/* Checks if pattern (X ? e : f) == (Y ? e : f) gets optimized. */ +__GIMPLE() +_Bool f1_(int a, int b, int c, int d, int e, int f) { + _Bool X; + _Bool Y; + _Bool t; + int t1; + int t2; + X = a == b; + Y = c == d; + /* Before the patch cond_expr was generated for these 2 statements. */ + t1 = X ? e : f; + t2 = Y ? e : f; + t = t1 == t2; + return t; +} + +/* Checks if pattern (X ? e : f) != (Y ? e : f) gets optimized. */ +__GIMPLE() +_Bool f2_(int a, int b, int c, int d, int e, int f) { + _Bool X; + _Bool Y; + _Bool t; + int t1; + int t2; + X = a == b; + Y = c == d; + t1 = X ? e : f; + t2 = Y ? e : f; + t = t1 != t2; + return t; +} + +/* Checks if pattern (X ? e : f) == (Y ? f : e) gets optimized. */ +__GIMPLE() +_Bool f3_(int a, int b, int c, int d, int e, int f) { + _Bool X; + _Bool Y; + _Bool t; + int t1; + int t2; + X = a == b; + Y = c == d; + t1 = X ? e : f; + t2 = Y ? f : e; + t = t1 == t2; + return t; +} + +/* Checks if pattern (X ? e : f) != (Y ? f : e) gets optimized. */ +__GIMPLE() +_Bool f4_(int a, int b, int c, int d, int e, int f) { + _Bool X; + _Bool Y; + _Bool t; + int t1; + int t2; + X = a == b; + Y = c == d; + t1 = X ? e : f; + t2 = Y ? f : e; + t = t1 != t2; + return t; +} + +/* Should generate one bit_xor_expr for each testcase. */ +/* { dg-final { scan-tree-dump-not "cond_expr, " "forwprop1" } } */ +/* { dg-final { scan-tree-dump-times "bit_xor_expr, " 4 "forwprop1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr111150.c b/gcc/testsuite/gcc.dg/tree-ssa/pr111150.c new file mode 100644 index 00000000000..cf25c5d758c --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr111150.c @@ -0,0 +1,22 @@ +/* PR tree-optimization/111150 */ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-forwprop1" } */ + +typedef int v4si __attribute((__vector_size__(4 * sizeof(int)))); + +/* Before the patch, VEC_COND_EXPR was generated for each statement in the + function. This resulted in 3 VEC_COND_EXPR. */ +v4si f1_(v4si a, v4si b, v4si c, v4si d) { + v4si X = a == b; + v4si Y = c == d; + return (X != Y); +} + +v4si f2_(v4si a, v4si b, v4si c, v4si d) { + v4si X = a == b; + v4si Y = c == d; + return (X == Y); +} + +/* For each testcase, should produce only one VEC_COND_EXPR for X^Y. */ +/* { dg-final { scan-tree-dump-times " VEC_COND_EXPR " 2 "forwprop1" } } */