From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id EDFE73858C52 for ; Thu, 21 Sep 2023 08:10:15 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EDFE73858C52 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 (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 38L7PKlY008644 for ; Thu, 21 Sep 2023 01:10:15 -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=Sj7L/PqSMaytjjNt51i7KKl1IOyVvI81diocKD72s34=; b=Vf2uQT4Qt+zklHMVK25W/xa+ql0+D1O2iI2x2X2B4efGJQ5K1KudAT2y6/tbqSXdSSwC DqCdlRBZwC0tdV2JFST2+DyNvN9RX8tAAJoJetYtI4wOIM+bjflBijQ/5k4/L+0KBCk5 QlvLICPvoHtcjMWtIR48vo3w1eKQ4mHth7GhoszaBpuyjf5ayw9Oidg7JcTiNER5K2fw IKb7sY+ImXv96SSk/NlzEo2xIMTuG67mvWSBsGDGey/w64POOF5wR4ttgcEsjCJvPOZY pBPcszNMIKliaGyW+pVjxLm3mfiS7zUPYeKjNgJJfLzH7s+75XYn7xr6bQtlM8k+8TPW SQ== Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3t85pttcfx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Thu, 21 Sep 2023 01:10:15 -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; Thu, 21 Sep 2023 01:09:58 -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; Thu, 21 Sep 2023 01:09:58 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 5D8C55B693B; Thu, 21 Sep 2023 01:09:58 -0700 (PDT) From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] PHIOPT: Fix minmax_replacement for three way Date: Thu, 21 Sep 2023 01:09:31 -0700 Message-ID: <20230921080931.1954219-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-GUID: UWX0sU6Ex0m2qXdZ3yF4coRBWWQsPkhL X-Proofpoint-ORIG-GUID: UWX0sU6Ex0m2qXdZ3yF4coRBWWQsPkhL X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-09-21_06,2023-09-20_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 List-Id: So when diamond bb support was added to minmax_replacement in r13-1950-g9bb19e143cfe, the code was not expecting the alt_middle_bb not to exist if it was empty (for threeway_p). So when factor_out_conditional_conversion was used to factor out conversions, it turns out the assumption for alt_middle_bb to be wrong and we ended up with threeway_p being true but having middle_bb being empty but alt_middle_bb not being empty which causes wrong code in many cases. This patch fixes the issue by adding a test for the 2 cases where the assumption on threeway_p case having the other bb being empty. Note my plan for GCC 15 is remove minmax_replacement as match.pd will catch all cases at that point. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/111469 gcc/ChangeLog: * tree-ssa-phiopt.cc (minmax_replacement): Fix the assumption for the `non-diamond` handling cases of diamond code. gcc/testsuite/ChangeLog: * gcc.c-torture/execute/pr111469-1.c: New test. --- .../gcc.c-torture/execute/pr111469-1.c | 38 +++++++++++++++++++ gcc/tree-ssa-phiopt.cc | 10 ++++- 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr111469-1.c diff --git a/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c b/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c new file mode 100644 index 00000000000..b68d5989eac --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c @@ -0,0 +1,38 @@ +/* PR tree-optimization/111469 */ + +long f; +char *g; +__attribute__((noinline)) +char o() { + char l; + while (f) + ; + l = *g; + return l; +} + +/* factor_out_conditional_conversion is able to remove the casts + from the 2 bbs (correctly) + but then minmax_replacement should not optimize this to a MIN_EXPR + as o has side effects. */ + +__attribute__((noinline)) +unsigned short gg(unsigned short a, unsigned short b) +{ + short d; + if (a > b) + { + d= b; + } + else + { + o(); + d = a; + } + return d; +} + +int main(void) +{ + gg(3, 2); +} diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index 3835d25d08c..96901a40444 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -1823,7 +1823,9 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_ arg_false = arg0; } - if (empty_block_p (middle_bb)) + if (empty_block_p (middle_bb) + && (!threeway_p + || empty_block_p (alt_middle_bb))) { if ((operand_equal_for_phi_arg_p (arg_true, smaller) || (alt_smaller @@ -2006,7 +2008,9 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_ return true; } - else + else if (middle_bb == alt_middle_bb + && (!threeway_p + || empty_block_p (alt_middle_bb))) { /* Recognize the following case, assuming d <= u: @@ -2182,6 +2186,8 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_ SSA_OP_DEF)); gsi_move_before (&gsi_from, &gsi); } + else + return false; /* Emit the statement to compute min/max. */ gimple_seq stmts = NULL; -- 2.31.1