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 8F30E3858C41 for ; Mon, 31 Jul 2023 05:34:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8F30E3858C41 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 36ULMu29027932 for ; Sun, 30 Jul 2023 22:34:25 -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=ByHX/Tqpz8ibPeV+2r22Km3ubh7gVBgDQj+9YzKD06Q=; b=hCJg5ZMMa7NySl0lhk08+PyHATpORSUkWKvc56H6QSOpDP2Yc0tV0LTyNEdosvkz/i8p 6eoqFmRxKWvgIfWBa+vpNIrtDPv8cjxEserGe7AED6geAwcq6aq/B8WWF6iv3L7/2XFu Mkm9Js1jl6NPqJa6AyKuwj5nJPJ/k7ssXn+b7VGIs4Rggdkv4RLgAHXf1FVs6brG6MQX XnJVVPvxJqTELmLnUFx+rY2CNLCS5r4PlFdNCU1xhvJV1E0286QADGfxtYptLFBCZh5o eB3KdfzgVS6FhJ8qS3A6L3rOCvsLEM4eHJ2OetTKG957tvoA8TByp/GbX58geF+CWJhF pg== Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3s504nmd47-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sun, 30 Jul 2023 22:34:25 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Sun, 30 Jul 2023 22:34:23 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Sun, 30 Jul 2023 22:34:23 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 72B643F7040; Sun, 30 Jul 2023 22:34:23 -0700 (PDT) From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH 1/2] MATCH: PR 106164 : Optimize `(X CMP1 Y) AND/IOR (X CMP2 Y)` Date: Sun, 30 Jul 2023 22:34:11 -0700 Message-ID: <20230731053412.2102672-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-ORIG-GUID: NtSyCfTaYLm3Of4RZwDaD51xaIrcXqPX X-Proofpoint-GUID: NtSyCfTaYLm3Of4RZwDaD51xaIrcXqPX X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-07-27_10,2023-07-26_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,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: I noticed that there are patterns that optimize `(X CMP1 CST1) AND/IOR (X CMP2 CST2)` and we can easily extend them to support the `(X CMP1 Y) AND/IOR (X CMP2 Y)` by saying they compare equal. This allows for this kind of optimization for integral and pointer types (which have the same semantics). OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: PR tree-optimization/106164 * match.pd: Extend the `(X CMP1 CST1) AND/IOR (X CMP2 CST2)` patterns to support `(X CMP1 Y) AND/IOR (X CMP2 Y)`. gcc/testsuite/ChangeLog: PR tree-optimization/106164 * gcc.dg/tree-ssa/cmpbit-1.c: New test. --- gcc/match.pd | 66 +++++++++++++++++++----- gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c | 38 ++++++++++++++ 2 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c diff --git a/gcc/match.pd b/gcc/match.pd index 73eb249f704..00af5d99119 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2799,14 +2799,24 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* Convert (X == CST1) && (X OP2 CST2) to a known value based on CST1 OP2 CST2. Similarly for (X != CST1). */ +/* Convert (X == Y) && (X OP2 Y) to a known value if X is an integral type. + Similarly for (X != Y). */ (for code1 (eq ne) (for code2 (eq ne lt gt le ge) (simplify - (bit_and:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2)) + (bit_and:c (code1@3 @0 @1) (code2@4 @0 @2)) + (if ((TREE_CODE (@1) == INTEGER_CST + && TREE_CODE (@2) == INTEGER_CST) + || ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) + || POINTER_TYPE_P (TREE_TYPE (@1))) + && operand_equal_p (@1, @2))) (with { - int cmp = tree_int_cst_compare (@1, @2); + int cmp = 0; + if (TREE_CODE (@1) == INTEGER_CST + && TREE_CODE (@2) == INTEGER_CST) + cmp = tree_int_cst_compare (@1, @2); bool val; switch (code2) { @@ -2822,17 +2832,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (switch (if (code1 == EQ_EXPR && val) @3) (if (code1 == EQ_EXPR && !val) { constant_boolean_node (false, type); }) - (if (code1 == NE_EXPR && !val) @4)))))) + (if (code1 == NE_EXPR && !val) @4))))))) -/* Convert (X OP1 CST1) && (X OP2 CST2). */ +/* Convert (X OP1 CST1) && (X OP2 CST2). + Convert (X OP1 Y) && (X OP2 Y). */ (for code1 (lt le gt ge) (for code2 (lt le gt ge) (simplify - (bit_and (code1:c@3 @0 INTEGER_CST@1) (code2:c@4 @0 INTEGER_CST@2)) + (bit_and (code1:c@3 @0 @1) (code2:c@4 @0 @2)) + (if ((TREE_CODE (@1) == INTEGER_CST + && TREE_CODE (@2) == INTEGER_CST) + || ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) + || POINTER_TYPE_P (TREE_TYPE (@1))) + && operand_equal_p (@1, @2))) (with { - int cmp = tree_int_cst_compare (@1, @2); + int cmp = 0; + if (TREE_CODE (@1) == INTEGER_CST + && TREE_CODE (@2) == INTEGER_CST) + cmp = tree_int_cst_compare (@1, @2); } (switch /* Choose the more restrictive of two < or <= comparisons. */ @@ -2861,18 +2880,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && (code1 == GT_EXPR || code1 == GE_EXPR) && (code2 == LT_EXPR || code2 == LE_EXPR)) { constant_boolean_node (false, type); }) - ))))) + )))))) /* Convert (X == CST1) || (X OP2 CST2) to a known value based on CST1 OP2 CST2. Similarly for (X != CST1). */ +/* Convert (X == Y) || (X OP2 Y) to a known value if X is an integral type. + Similarly for (X != Y). */ (for code1 (eq ne) (for code2 (eq ne lt gt le ge) (simplify - (bit_ior:c (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2)) + (bit_ior:c (code1@3 @0 @1) (code2@4 @0 @2)) + (if ((TREE_CODE (@1) == INTEGER_CST + && TREE_CODE (@2) == INTEGER_CST) + || ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) + || POINTER_TYPE_P (TREE_TYPE (@1))) + && operand_equal_p (@1, @2))) (with { - int cmp = tree_int_cst_compare (@1, @2); + int cmp = 0; + if (TREE_CODE (@1) == INTEGER_CST + && TREE_CODE (@2) == INTEGER_CST) + cmp = tree_int_cst_compare (@1, @2); bool val; switch (code2) { @@ -2888,17 +2917,26 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (switch (if (code1 == EQ_EXPR && val) @4) (if (code1 == NE_EXPR && val) { constant_boolean_node (true, type); }) - (if (code1 == NE_EXPR && !val) @3)))))) + (if (code1 == NE_EXPR && !val) @3))))))) -/* Convert (X OP1 CST1) || (X OP2 CST2). */ +/* Convert (X OP1 CST1) || (X OP2 CST2). + Convert (X OP1 Y) || (X OP2 Y). */ (for code1 (lt le gt ge) (for code2 (lt le gt ge) (simplify - (bit_ior (code1@3 @0 INTEGER_CST@1) (code2@4 @0 INTEGER_CST@2)) + (bit_ior (code1@3 @0 @1) (code2@4 @0 @2)) + (if ((TREE_CODE (@1) == INTEGER_CST + && TREE_CODE (@2) == INTEGER_CST) + || ((INTEGRAL_TYPE_P (TREE_TYPE (@1)) + || POINTER_TYPE_P (TREE_TYPE (@1))) + && operand_equal_p (@1, @2))) (with { - int cmp = tree_int_cst_compare (@1, @2); + int cmp = 0; + if (TREE_CODE (@1) == INTEGER_CST + && TREE_CODE (@2) == INTEGER_CST) + cmp = tree_int_cst_compare (@1, @2); } (switch /* Choose the more restrictive of two < or <= comparisons. */ @@ -2927,7 +2965,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && (code1 == GT_EXPR || code1 == GE_EXPR) && (code2 == LT_EXPR || code2 == LE_EXPR)) { constant_boolean_node (true, type); }) - ))))) + )))))) /* We can't reassociate at all for saturating types. */ (if (!TYPE_SATURATING (type)) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c new file mode 100644 index 00000000000..c219e047ba8 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-1.c @@ -0,0 +1,38 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fno-tree-reassoc -fdump-tree-optimized-raw" } */ +_Bool f(int a, int b) +{ + _Bool c = a > b; + _Bool d = a >= b; + return c & d; +} + +_Bool f1(int a, int b) +{ + _Bool c = a > b; + _Bool d = a >= b; + return c | d; +} + +_Bool g(int a, int b) +{ + _Bool c = a < b; + _Bool d = a <= b; + return c & d; +} + +_Bool g1(int a, int b) +{ + _Bool c = a < b; + _Bool d = a <= b; + return c | d; +} + + +/* We should be able to optimize these without reassociation too. */ +/* { dg-final { scan-tree-dump-not "bit_and_expr," "optimized" } } */ +/* { dg-final { scan-tree-dump-not "bit_ior_expr," "optimized" } } */ +/* { dg-final { scan-tree-dump-times "gt_expr," 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "ge_expr," 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "lt_expr," 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "le_expr," 1 "optimized" } } */ -- 2.31.1