From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by sourceware.org (Postfix) with ESMTPS id 2CF22385828E for ; Sat, 2 Dec 2023 06:37:43 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2CF22385828E 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 2CF22385828E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701499065; cv=none; b=IE6DR1egi8HHgYnRwlN0yPy3MkIJ3IWbOArd5l00KKpVYWT626URAWzKsgbv1vLz21A7DP5zhyHCHH8sj8E6hgFQ+B5/jH0ebUyYroFaZvmCoTN/9TCEtgKZxf/9ha4sjZd12sblmWAYl75lGI58CPD8tGtlyhveZHCrA8TCyQo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701499065; c=relaxed/simple; bh=xiiLbw00UWDBInwu79heIMFQ5BySHAVx4z0QTWczY4k=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=W3p8C1XjPxoXAEBYIKSYopcltmc2fMo8IyUwb109Jso1Wz0/ASs7Db+GuhaxV7sip5A9PvCXrZpIZYyh4AWv9iFoRpW1owjt3giMRMGnrlZUNCGiBKuTDdSCvpoc2ZQNN+p+M8TOBpfYrd0nen8JCrsCoqOVSEF0+lK0+ko3/OY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279872.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3B26TouL023107 for ; Sat, 2 Dec 2023 06:37:42 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h=from : to : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding : content-type; s=qcppdkim1; bh=djFOzD8d25kgglPl04KgqaMW8ZVZS5PNCzz3hq1+qgI=; b=CZubQ0foT2ZxO5ZcOcxu2r/Er1DP80L4M6RedIIcs7kUPEKQth0viJK03GPFu1d8tpFD 6E9z3rL9Hsn3Y2wy/Uf0UbdSi+ejhgUSPAXeZD9ZC99PCAUq3v7dD9pOy57WyXhsm/EY eXF0P6+Fde0wRAyyvLJ2AZhVa1lKALWqgCTVFhs8oqdyglZHFuH4pfyMZc5LWGSK4OpO lxCtS6XhqPOL69jyjSAPJMIleO3N+7rhqgIkv63d6UYwKKorXeaB4V9bXAzZ3U8/zhl3 NCOPfI/8Vdpz7Wm09ppAgphJsd0jagmfuz3JRuWmViLEEPYtY1dQjwVXHGtv8v61xlhF jQ== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3uqvyer5tp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Sat, 02 Dec 2023 06:37:42 +0000 Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA04.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 3B26bfuK026498 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Sat, 2 Dec 2023 06:37:41 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.1118.40; Fri, 1 Dec 2023 22:37:41 -0800 From: Andrew Pinski To: Subject: [PATCH 3/3] MATCH: (convert)(zero_one !=/== 0/1) for outer type and zero_one type are the same Date: Fri, 1 Dec 2023 22:37:25 -0800 Message-ID: <20231202063725.3405094-4-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231202063725.3405094-1-quic_apinski@quicinc.com> References: <20231202063725.3405094-1-quic_apinski@quicinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain 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-ORIG-GUID: qUscBuWaEo-mUKXn4tui3czXrI1TAMdZ X-Proofpoint-GUID: qUscBuWaEo-mUKXn4tui3czXrI1TAMdZ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.997,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-12-02_03,2023-11-30_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 phishscore=0 impostorscore=0 mlxlogscore=962 bulkscore=0 suspectscore=0 clxscore=1015 spamscore=0 mlxscore=0 malwarescore=0 lowpriorityscore=0 priorityscore=1501 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311060000 definitions=main-2312020047 X-Spam-Status: No, score=-13.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE 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: When I moved two_value to match.pd, I removed the check for the {0,+-1} as I had placed it after the {0,+-1} case for cond in match.pd. In the case of {0,+-1} and non boolean, before we would optmize those case to just `(convert)a` but after we would get `(convert)(a != 0)` which was not handled anyways to just `(convert)a`. So this adds a pattern to match `(convert)(zeroone != 0)` and simplify to `(convert)zeroone`. Also this optimizes (convert)(zeroone == 0) into (zeroone^1) if the type match. This can only be done on the gimple level as if zeroone was defined by (a&1), fold will convert (a&1)^1 back into `(convert)(zeroone == 0)` and an infinite loop will happen. Note the testcase pr69270.c needed a slight update due to not matching exactly a scan pattern, this update makes it more robust and will match before and afterwards and if there are other changes in this area too. Note the testcase gcc.target/i386/pr110790-2.c needs a slight update for better code generation in LP64 bit mode. Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: PR tree-optimization/111972 PR tree-optimization/110637 * match.pd (`(convert)(zeroone !=/== CST)`): Match and simplify to ((convert)zeroone){,^1}. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr110637-1.c: New test. * gcc.dg/tree-ssa/pr110637-2.c: New test. * gcc.dg/tree-ssa/pr110637-3.c: New test. * gcc.dg/tree-ssa/pr111972-1.c: New test. * gcc.dg/tree-ssa/pr69270.c: Update testcase. * gcc.target/i386/pr110790-2.c: Update testcase. Signed-off-by: Andrew Pinski --- gcc/match.pd | 21 +++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c | 10 +++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c | 13 +++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c | 14 +++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c | 34 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/pr69270.c | 4 +-- gcc/testsuite/gcc.target/i386/pr110790-2.c | 16 ++++++++-- 7 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c diff --git a/gcc/match.pd b/gcc/match.pd index 4d554ba4721..656b2c9edda 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3332,6 +3332,27 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0))) (rcmp @0 @1)))) +/* (type)([0,1]@a != 0) -> (type)a + (type)([0,1]@a == 1) -> (type)a + (type)([0,1]@a == 0) -> a ^ 1 + (type)([0,1]@a != 1) -> a ^ 1. */ +(for eqne (eq ne) + (simplify + (convert (eqne zero_one_valued_p@0 INTEGER_CST@1)) + (if ((integer_zerop (@1) || integer_onep (@1))) + (if ((eqne == EQ_EXPR) ^ integer_zerop (@1)) + (convert @0) + /* a^1 can only be produced for gimple as + fold has the exact opposite transformation + for `(X & 1) ^ 1`. + See `Fold ~X & 1 as (X & 1) == 0.` + and `Fold (X ^ 1) & 1 as (X & 1) == 0.` in fold-const.cc. + Only do this if the types match as (type)(a == 0) is + canonical form normally, while `a ^ 1` is canonical when + there is no type change. */ + (if (GIMPLE && types_match (type, TREE_TYPE (@0))) + (bit_xor @0 { build_one_cst (type); } )))))) + /* We can't reassociate at all for saturating types. */ (if (!TYPE_SATURATING (type)) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c new file mode 100644 index 00000000000..3d03b0992a4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ +int f(int a) +{ + int b = (a & 1)!=0; + return b; +} + +/* This should be optimized to just return (a & 1); */ +/* { dg-final { scan-tree-dump-not " == " "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c new file mode 100644 index 00000000000..f1c5b90353a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ +int f(int a) +{ + int b = a & 1; + int c = b == 0; + return c; +} + +/* This should be optimized to just return `(a&1) ^ 1` or `(~a) & 1`. */ +/* { dg-final { scan-tree-dump-not " == " "optimized"} } */ +/* { dg-final { scan-tree-dump-times "~a" 1 "optimized"} } */ +/* { dg-final { scan-tree-dump-times " & 1" 1 "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c new file mode 100644 index 00000000000..ce80146d9df --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c @@ -0,0 +1,14 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ +int f(int a) +{ + int b = a & 1; + int c = b == 0; + int d = ~a; + int e = d & 1; + return c == e; +} + +/* This should be optimized to just `return 1` */ +/* { dg-final { scan-tree-dump-not " == " "optimized"} } */ +/* { dg-final { scan-tree-dump-times "return 1" 1 "optimized"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c new file mode 100644 index 00000000000..0611808ed50 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c @@ -0,0 +1,34 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-phiopt" } */ +double +foo() { + long n3 = 3450000, xtra = 7270; + long i,ix; + long j; + double Check; + + /* Section 3, Conditional jumps */ + j = 0; + { + for (ix=0; ix2) j = 0; + else j = 1; + if(j<1) j = 1; + else j = 0; + } + } + } + Check = Check + (double)j; + return Check; +} + +/* the above if statements in loop should be optimized to just `j ^ 1` + and should not be (type)(j != 1). */ +/* { dg-final { scan-tree-dump-not " != 1" "phiopt2"} } */ +/* { dg-final { scan-tree-dump-times " \\^ 1" 1 "phiopt2"} } */ + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c b/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c index 0d66cc4383f..b08ec9d6ddb 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c @@ -7,8 +7,8 @@ /* { dg-final { scan-tree-dump-times "Replaced .bufferstep_\[0-9\]+. with constant .1." 1 "dom3"} } */ /* And some assignments ought to fold down to constants. */ -/* { dg-final { scan-tree-dump-times "Folded to: _\[0-9\]+ = 1;" 1 "dom3"} } */ -/* { dg-final { scan-tree-dump-times "Folded to: _\[0-9\]+ = 0;" 1 "dom3"} } */ +/* { dg-final { scan-tree-dump-times "Folded to: (?:bufferstep)?_\[0-9\]+ = 1;" 1 "dom3"} } */ +/* { dg-final { scan-tree-dump-times "Folded to: (?:bufferstep)?_\[0-9\]+ = 0;" 1 "dom3"} } */ /* The XOR operations should have been optimized to constants. */ /* { dg-final { scan-tree-dump-not "bit_xor" "dom3"} } */ diff --git a/gcc/testsuite/gcc.target/i386/pr110790-2.c b/gcc/testsuite/gcc.target/i386/pr110790-2.c index 8b9d650c6e9..16c73cb7465 100644 --- a/gcc/testsuite/gcc.target/i386/pr110790-2.c +++ b/gcc/testsuite/gcc.target/i386/pr110790-2.c @@ -9,5 +9,17 @@ refmpn_tstbit_bad (mp_srcptr ptr, unsigned long bit) return (((ptr)[(bit)/(32 - 0)] & (((mp_limb_t) 1L) << ((bit)%(32 - 0)))) != 0); } -/* { dg-final { scan-assembler "bt\[ql\]" } } */ -/* { dg-final { scan-assembler "setc" } } */ +/* 32bit produces: + btl %eax, %edx + setc %al + movzbl %al, %eax + */ +/* { dg-final { scan-assembler "bt\[ql\]" { target { ! lp64 } } } } */ +/* { dg-final { scan-assembler "setc" { target { ! lp64 } } } } */ + +/* 64bit produces: + shrq %cl, %rax + andl $1, %eax + */ +/* { dg-final { scan-assembler-times "shrq" 2 { target { lp64 } } } } */ +/* { dg-final { scan-assembler-times "andl" 2 { target { lp64 } } } } */ -- 2.39.3