From patchwork Sat Sep 23 00:54:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 143754 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:910f:0:b0:403:3b70:6f57 with SMTP id r15csp12075vqg; Fri, 22 Sep 2023 17:55:31 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF6KuvJD6y5tzRiLy5SqYkkXr+XJMJF9zWf6jKQOvbWDVpnkbzRoYBAYF1l/Y1NleITwix6 X-Received: by 2002:a17:906:8a51:b0:9a1:d67c:b4eb with SMTP id gx17-20020a1709068a5100b009a1d67cb4ebmr679175ejc.48.1695430531709; Fri, 22 Sep 2023 17:55:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695430531; cv=none; d=google.com; s=arc-20160816; b=MUF8bPkNlpcr9Uzw9VuUbN5+KAnGCBqCnOFCHf9nOPC+J5sFGnsedbuXY3fy+zRjrT C/RryI6mKMHVdVQXHk7weULkunElRWh0mkDL+qVC2gPq6jK+38rj8tyfNmoxDXqKUXW4 g6UKxNbKKFQQUezoIyZSpa5uyTTPHRPelaLoChxiBL1nWmgl2OFCUj/fkNbml0RQHp0h HlgO7BaDUWIMwmY1byfCfQAxjzzLLM6b3KJF+Nri/0fL/X9dnqlWGlzln2KO+V+7vbji F3yd6Fyru9dc5O13nk8fZp/LwM/B9VgP09X6s6i8Jk2W/OnGSLj4eeq1w1Z+CDfZTgNu 9ktg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :dmarc-filter:delivered-to; bh=Eci/kwPRemvhjSCyxwJAAe2MYIYUvRM/4P1R3HPYLoA=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=0bPrkytiBMTu18gZfPvr9j/AODtOpHkPXsFPsxEjaQ8BD0iCsj/EwuErqX2SVvF2/z WzI1LJU9m/Q3X6BVOLo91HFlnobVyNu0HUPokT5q0ZbtqrqLLOFki3TMezIeYfvuXdIc /oi2cei4ZgRvNEmq00F2h2Mgpf44C/uV4Kz9B75twqGKp81WTqraEBVnek2Qd1dJsPfQ P38Gm+v7wsKhkDzvNoV9TCAl0AgwsNPxXxHYzoSIwgtbdvY6FBeBe8lJ5jheQ0XXTnRz HGYhX287WAcIL2n5NjR59a2HayBY5pDQGIwRYnaMosFB+4gKUBWXYdrNN27qfu4/PlRk gxsA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@marvell.com header.s=pfpt0220 header.b=XC9N7JWS; 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=REJECT dis=NONE) header.from=marvell.com Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id va25-20020a17090711d900b009ad806095e7si4306399ejb.595.2023.09.22.17.55.31 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 17:55:31 -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=@marvell.com header.s=pfpt0220 header.b=XC9N7JWS; 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=REJECT dis=NONE) header.from=marvell.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A18413858281 for ; Sat, 23 Sep 2023 00:55:30 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by sourceware.org (Postfix) with ESMTPS id D6F593858D28 for ; Sat, 23 Sep 2023 00:54:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D6F593858D28 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 38MIf1km017754 for ; Fri, 22 Sep 2023 17:54:54 -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=Eci/kwPRemvhjSCyxwJAAe2MYIYUvRM/4P1R3HPYLoA=; b=XC9N7JWSgmJgYeNLJyLyDLpxwaKpqH8bx1DVf0mfJp9YO/fIKhlLgJmFkxFEmz3GJKbU 0Nd8O9W5Dnq19n8rM7L1VUHWgeZzF2LR31zgvpW6Ee/nvp5oJ1aGLzUnbcok2qDcLqr8 9+dLh6q9GJ3Pdm6MOjSk6ljFKRoZbROVgfEvInYuX5Xrvx8vHgmvD/MHJFPigu6grMPo b4ZL3oYSjYB5Hs454BKkWReUEcuCAdTg1havEAZrE4zEsZIZ9ja7Dt3uq/7oGwz9LPGg skrRhBY9025+8RGS31ZaudhGe7vXcICYFw5/vvjM5GhXzX09G0owtRb2pfazFcsamsXF /Q== Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3t8trb504k-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 22 Sep 2023 17:54:54 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Fri, 22 Sep 2023 17:54:41 -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; Fri, 22 Sep 2023 17:54:40 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id CB2743F705C; Fri, 22 Sep 2023 17:54:40 -0700 (PDT) From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] PHIOPT: Fix minmax_replacement for three way Date: Fri, 22 Sep 2023 17:54:27 -0700 Message-ID: <20230923005427.2053950-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: NIJnVKVKTlkYzN3Iqrlbr3KfDEveDs3k X-Proofpoint-GUID: NIJnVKVKTlkYzN3Iqrlbr3KfDEveDs3k X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-09-22_21,2023-09-21_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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777787765265742289 X-GMAIL-MSGID: 1777787765265742289 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. Changes made: v2: Fix test for `(a <= u) b = MAX(a, d) else b = u`. 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 | 9 ++++- 2 files changed, 45 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..312a6f9082b 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,8 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_ return true; } - else + else if (!threeway_p + || empty_block_p (alt_middle_bb)) { /* Recognize the following case, assuming d <= u: @@ -2182,6 +2185,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;