From patchwork Mon Aug 7 14:45:34 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 132115 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1501930vqr; Mon, 7 Aug 2023 07:46:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF4TID2oU9O2MFh2MSq3b4R2Ff7ISjeYv1TpenOQ/0LB0K1dOxiqkon2XcXpse4oQsDNBXf X-Received: by 2002:aa7:d353:0:b0:522:4964:d590 with SMTP id m19-20020aa7d353000000b005224964d590mr8113608edr.10.1691419590118; Mon, 07 Aug 2023 07:46:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691419590; cv=none; d=google.com; s=arc-20160816; b=AZ5zn7DUj+paPCYBlamG7YLTtWXLham4w7XaX2yglQKA0tSuRJFqCOF37sjTF7FLjJ 5YQ5qvvMqDu4EXggbqhJFcT3KhAl1AacbDp7aTUC8vbxNVr42tjbBgOMJ9cO6IZYTtr8 bHVg8Tp9bRa18jGpy3t3NlCNMGCD+iAs/HzYbGmrWNet4a0d0A2PbaPZoNOsM/IvY3ZQ 33PKtEjNx3hoVuq605+TGMJOm8l+t3lWDJ52wcGeHISrIcX8sgaluxBd9xzhFdKHk2qU CoMFMYYZG4uLH8zi9P9BcvYa4SHane8MrZk2Fxxb2+kGvXHVzmAm5KwEX5LY8/16dFfI KTpg== 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=/wN/oA4rW5FPfWzLY9QRlDzcVUsqLFQSwQnmqzfULsg=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=q48UenX7iLQ50cfgMoCT3N7yibi5jrF6Et8R3S85UZAVP6NT/6LGqg0oGiBxb8gFth KMJ1lBOsOthrycMQmdztviujbHhGBJikNdh5QxKug8UlYjRMoFCyidzN/iJmyts072vx flqVhfdlzre32DoGgNBnfWgqILBe0BoXFAwn1pbx693TcbpgsQDbePPUOtGPe2zKUVYs fiISh4/uEcIE5ZuuBuJisRVEGazBk6rnaA9FJPqOZi/aJXqCzvLsKsPA9Ss5hueYrUgG XsXIb+s4+2cW9NQySSYduPFlWIA7Bdj7NHUTLxab84H90AjSqPHqUp0Pd3QWjMYCb/PW GdTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=atsd6Ipj; 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 e15-20020a056402088f00b00523402c3b39si109155edy.7.2023.08.07.07.46.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 07:46:30 -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=atsd6Ipj; 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 F2569385841D for ; Mon, 7 Aug 2023 14:46:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F2569385841D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691419589; bh=/wN/oA4rW5FPfWzLY9QRlDzcVUsqLFQSwQnmqzfULsg=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=atsd6IpjyxFgXMa8THp4He6FAUQu2nKz4aN1zi5hSSdAOuI6rzM+kWgjFZq6z2iN7 W8FHSE6wZIK8W74lEFEpK9/1jWRFntZEShGdPIuRM0LXtkIu8YszLDQayleEbTnfEi 4AdmWJbAu/PgO+LOE2IEqKDGDwR1gup54xbbWRn0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 2DDE23858284 for ; Mon, 7 Aug 2023 14:45:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2DDE23858284 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 377EDiiR008582 for ; Mon, 7 Aug 2023 07:45:44 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3s9nxkvyax-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Mon, 07 Aug 2023 07:45:44 -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; Mon, 7 Aug 2023 07:45:42 -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; Mon, 7 Aug 2023 07:45:42 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id D48843F704B; Mon, 7 Aug 2023 07:45:41 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] VR-VALUES [PR28794]: optimize compare assignments also Date: Mon, 7 Aug 2023 07:45:34 -0700 Message-ID: <20230807144534.2538500-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: FWow2BwIjHCLVoNZyCYDPG5lcMyWuyLX X-Proofpoint-ORIG-GUID: FWow2BwIjHCLVoNZyCYDPG5lcMyWuyLX X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-08-07_16,2023-08-03_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: 1773581987707590762 X-GMAIL-MSGID: 1773581987707590762 This patch fixes the oldish (2006) bug where VRP was not optimizing the comparison for assignments while handling them for GIMPLE_COND only. It just happens to also solves PR 103281 due to allowing to optimize `c < 1` to `c == 0` and then we get `(c == 0) == c` (which was handled by r14-2501-g285c9d04). OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optmization/103281 PR tree-optimization/28794 gcc/ChangeLog: * vr-values.cc (simplify_using_ranges::simplify_cond_using_ranges_1): Split out majority to ... (simplify_using_ranges::simplify_compare_using_ranges_1): Here. (simplify_using_ranges::simplify_casted_cond): Rename to ... (simplify_using_ranges::simplify_casted_compare): This and change arguments to take op0 and op1. (simplify_using_ranges::simplify_compare_assign_using_ranges_1): New method. (simplify_using_ranges::simplify): For tcc_comparison assignments call simplify_compare_assign_using_ranges_1. * vr-values.h (simplify_using_ranges): Add new methods, simplify_compare_using_ranges_1 and simplify_compare_assign_using_ranges_1. Rename simplify_casted_cond and simplify_casted_compare and update argument types. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/pr103281-1.c: New test. * gcc.dg/tree-ssa/vrp-compare-1.c: New test. --- gcc/testsuite/gcc.dg/tree-ssa/pr103281-1.c | 19 +++ gcc/testsuite/gcc.dg/tree-ssa/vrp-compare-1.c | 13 ++ gcc/vr-values.cc | 160 +++++++++++------- gcc/vr-values.h | 4 +- 4 files changed, 134 insertions(+), 62 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr103281-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp-compare-1.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr103281-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr103281-1.c new file mode 100644 index 00000000000..09964d0b46b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr103281-1.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* PR tree-optmization/103281 */ + +void foo(void); + +static unsigned b; + +int main() { + for (; b < 3; b++) { + char c = b; + char a = c ? c : c << 1; + if (!(a < 1 ^ b)) + foo(); + } +} + +/* the call to foo should be optimized away. */ +/* { dg-final { scan-tree-dump-not "foo " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp-compare-1.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp-compare-1.c new file mode 100644 index 00000000000..9889cf34706 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp-compare-1.c @@ -0,0 +1,13 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-evrp-details" } */ +/* PR tree-optimization/28794 */ + +void g(int); +void f1(int x) +{ + if (x < 0) return; + g(x>0); +} + +/* `x > 0` should be optimized to just `x != 0` */ +/* { dg-final { scan-tree-dump-times "Simplified relational" 1 "evrp" } } */ diff --git a/gcc/vr-values.cc b/gcc/vr-values.cc index ac4a83c6097..a4fddd62841 100644 --- a/gcc/vr-values.cc +++ b/gcc/vr-values.cc @@ -1139,6 +1139,87 @@ simplify_using_ranges::simplify_cond_using_ranges_1 (gcond *stmt) if (fold_cond (stmt)) return true; + if (simplify_compare_using_ranges_1 (cond_code, op0, op1, stmt)) + { + if (dump_file) + { + fprintf (dump_file, "Simplified relational "); + print_gimple_stmt (dump_file, stmt, 0); + fprintf (dump_file, " into "); + } + + gimple_cond_set_code (stmt, cond_code); + gimple_cond_set_lhs (stmt, op0); + gimple_cond_set_rhs (stmt, op1); + + update_stmt (stmt); + + if (dump_file) + { + print_gimple_stmt (dump_file, stmt, 0); + fprintf (dump_file, "\n"); + } + return true; + } + return false; +} + +/* Like simplify_cond_using_ranges_1 but for assignments rather + than GIMPLE_COND. */ + +bool +simplify_using_ranges::simplify_compare_assign_using_ranges_1 + (gimple_stmt_iterator *gsi, + gimple *stmt) +{ + enum tree_code code = gimple_assign_rhs_code (stmt); + tree op0 = gimple_assign_rhs1 (stmt); + tree op1 = gimple_assign_rhs2 (stmt); + gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison); + bool happened = false; + + if (simplify_compare_using_ranges_1 (code, op0, op1, stmt)) + { + if (dump_file) + { + fprintf (dump_file, "Simplified relational "); + print_gimple_stmt (dump_file, stmt, 0); + fprintf (dump_file, " into "); + } + + gimple_assign_set_rhs_code (stmt, code); + gimple_assign_set_rhs1 (stmt, op0); + gimple_assign_set_rhs2 (stmt, op1); + + update_stmt (stmt); + + if (dump_file) + { + print_gimple_stmt (dump_file, stmt, 0); + fprintf (dump_file, "\n"); + } + happened = true; + } + + /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity + if the RHS is zero or one, and the LHS are known to be boolean + values. */ + if ((code == EQ_EXPR || code == NE_EXPR) + && INTEGRAL_TYPE_P (TREE_TYPE (op0)) + && simplify_truth_ops_using_ranges (gsi, stmt)) + happened = true; + + return happened; +} + +/* Try to simplify OP0 COND_CODE OP1 using a relational operator to an + equality test if the range information indicates only one value can + satisfy the original conditional. */ + +bool +simplify_using_ranges::simplify_compare_using_ranges_1 (tree_code &cond_code, tree &op0, tree &op1, gimple *stmt) +{ + bool happened = false; if (cond_code != NE_EXPR && cond_code != EQ_EXPR && TREE_CODE (op0) == SSA_NAME @@ -1157,26 +1238,9 @@ simplify_using_ranges::simplify_cond_using_ranges_1 (gcond *stmt) tree new_tree = test_for_singularity (cond_code, op0, op1, &vr); if (new_tree) { - if (dump_file) - { - fprintf (dump_file, "Simplified relational "); - print_gimple_stmt (dump_file, stmt, 0); - fprintf (dump_file, " into "); - } - - gimple_cond_set_code (stmt, EQ_EXPR); - gimple_cond_set_lhs (stmt, op0); - gimple_cond_set_rhs (stmt, new_tree); - - update_stmt (stmt); - - if (dump_file) - { - print_gimple_stmt (dump_file, stmt, 0); - fprintf (dump_file, "\n"); - } - - return true; + cond_code = EQ_EXPR; + op1 = new_tree; + happened = true; } /* Try again after inverting the condition. We only deal @@ -1187,45 +1251,25 @@ simplify_using_ranges::simplify_cond_using_ranges_1 (gcond *stmt) op0, op1, &vr); if (new_tree) { - if (dump_file) - { - fprintf (dump_file, "Simplified relational "); - print_gimple_stmt (dump_file, stmt, 0); - fprintf (dump_file, " into "); - } - - gimple_cond_set_code (stmt, NE_EXPR); - gimple_cond_set_lhs (stmt, op0); - gimple_cond_set_rhs (stmt, new_tree); - - update_stmt (stmt); - - if (dump_file) - { - print_gimple_stmt (dump_file, stmt, 0); - fprintf (dump_file, "\n"); - } - - return true; + cond_code = NE_EXPR; + op1 = new_tree; + happened = true; } } } // Try to simplify casted conditions. - return simplify_casted_cond (stmt); + if (simplify_casted_compare (cond_code, op0, op1)) + happened = true; + return happened; } -/* STMT is a conditional at the end of a basic block. - - If the conditional is of the form SSA_NAME op constant and the SSA_NAME - was set via a type conversion, try to replace the SSA_NAME with the RHS - of the type conversion. Doing so makes the conversion dead which helps - subsequent passes. */ +/* Simplify OP0 code OP1 when OP1 is a constant and OP0 was a SSA_NAME + defined by a type conversion. Replacing OP0 with RHS of the type conversion. + Doing so makes the conversion dead which helps subsequent passes. */ bool -simplify_using_ranges::simplify_casted_cond (gcond *stmt) +simplify_using_ranges::simplify_casted_compare (tree_code &, tree &op0, tree &op1) { - tree op0 = gimple_cond_lhs (stmt); - tree op1 = gimple_cond_rhs (stmt); /* If we have a comparison of an SSA_NAME (OP0) against a constant, see if OP0 was set by a type conversion where the source of @@ -1274,9 +1318,8 @@ simplify_using_ranges::simplify_casted_cond (gcond *stmt) && int_fits_type_p (op1, TREE_TYPE (innerop))) { tree newconst = fold_convert (TREE_TYPE (innerop), op1); - gimple_cond_set_lhs (stmt, innerop); - gimple_cond_set_rhs (stmt, newconst); - update_stmt (stmt); + op0 = innerop; + op1 = newconst; return true; } } @@ -1880,16 +1923,11 @@ simplify_using_ranges::simplify (gimple_stmt_iterator *gsi) } } + if (TREE_CODE_CLASS (rhs_code) == tcc_comparison) + return simplify_compare_assign_using_ranges_1 (gsi, stmt); + switch (rhs_code) { - case EQ_EXPR: - case NE_EXPR: - /* Transform EQ_EXPR, NE_EXPR into BIT_XOR_EXPR or identity - if the RHS is zero or one, and the LHS are known to be boolean - values. */ - if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))) - return simplify_truth_ops_using_ranges (gsi, stmt); - break; /* Transform TRUNC_DIV_EXPR and TRUNC_MOD_EXPR into RSHIFT_EXPR and BIT_AND_EXPR respectively if the first operand is greater diff --git a/gcc/vr-values.h b/gcc/vr-values.h index df79a3a570b..bc17aa1af15 100644 --- a/gcc/vr-values.h +++ b/gcc/vr-values.h @@ -38,13 +38,15 @@ private: void legacy_fold_cond (gcond *, edge *); tree legacy_fold_cond_overflow (gimple *stmt); tree fold_cond_with_ops (tree_code, tree, tree, gimple *s); - bool simplify_casted_cond (gcond *); + bool simplify_casted_compare (tree_code &cond_code, tree &op0, tree &op1); bool simplify_truth_ops_using_ranges (gimple_stmt_iterator *, gimple *); bool simplify_div_or_mod_using_ranges (gimple_stmt_iterator *, gimple *); bool simplify_abs_using_ranges (gimple_stmt_iterator *, gimple *); bool simplify_bit_ops_using_ranges (gimple_stmt_iterator *, gimple *); bool simplify_min_or_max_using_ranges (gimple_stmt_iterator *, gimple *); bool simplify_cond_using_ranges_1 (gcond *); + bool simplify_compare_using_ranges_1 (tree_code &, tree &, tree &, gimple *); + bool simplify_compare_assign_using_ranges_1 (gimple_stmt_iterator *, gimple *); bool simplify_switch_using_ranges (gswitch *); bool simplify_float_conversion_using_ranges (gimple_stmt_iterator *, gimple *);