From patchwork Fri Sep 8 12:39:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 137731 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ab0a:0:b0:3f2:4152:657d with SMTP id m10csp514020vqo; Fri, 8 Sep 2023 05:40:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHRUY6MGcbUrmqYdztL6xRFm6kT/Cdq3M4Q3pBJB5yV9Q5hZFy60oMyGUHVeEeR/nPOxUSS X-Received: by 2002:a05:6512:15a3:b0:500:a39a:1b99 with SMTP id bp35-20020a05651215a300b00500a39a1b99mr1987527lfb.38.1694176856025; Fri, 08 Sep 2023 05:40:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694176855; cv=none; d=google.com; s=arc-20160816; b=G/LssbCZXrjhVsXnc/OUylBZNUOL3jmuyHFHSEOt+Kb+V9DAX6wL9o8j5sJJxUAtth NGQPvCS1FIF1jDzZs2OwHh+c28IbI6EKjtvzxektmk15b2qUChbMDEMYtKhTPEBlg6Oy icJsbleQfL4qsEFjcCpZ9vPxW6GDkx7IcAPei5/iulxnSwzRO6OWHcZBqT9xv1PfY45I sdcl8dcjdw7UJ0raUyKdD+Kn+a7Tv7ePsXt2fLblzgq5O6tQUDQt2VYdjNn9lHiFH+3E Ub8Mp25X6oCH89c3F09PcN63lSLDri5oGEIS3pPzp/Moby88S0Bdcv+andU1RiSqMhIi uNvA== 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=WedUY+yH1RuG/vtyzVn5t7XW9QascqAQHvTJ5RK8noA=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=KN4DA3OeuV9jA/5piZ48etsFfnmc8wqtd68IX7EqHKuGM55mcjgNILMgfOWY6DXMmm U0XSTR7AyRsgOOvmafOnOaX0X6NifjUEf1+inB8MV28UYyMpJMdKxKpbgeTxaPJXdrhy iIJTtewdIhDTWtu+XcPU97T7VAkQTHjwc9EAHcAUWrESpapBdLXEdDA/QS7384t/1nXI EAT/i4NBKq0qjzF2o3GpDqlEthKwWgfZBCGkXWxI7XsYBRjFGBrwBIZMgFcjtXk3lmK7 hjtHyXNWUw5tTTAFTeLbNOXNVwV9GWuii4TK/7JcKGSUvntw5D/ocbhfCgEG4j6/WWlB xUYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=gB3M7e2m; 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 q24-20020a170906361800b00996d0a9b50csi1242604ejb.234.2023.09.08.05.40.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Sep 2023 05:40:55 -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=gB3M7e2m; 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 E91DD38582A1 for ; Fri, 8 Sep 2023 12:40:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E91DD38582A1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1694176854; bh=WedUY+yH1RuG/vtyzVn5t7XW9QascqAQHvTJ5RK8noA=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=gB3M7e2mWnuIRE8Rw1Ai1PcQ/iV6WAiDRPdnLnV9Okj7bbD24OMCEOGE4Uyp7DVuP 5baMzfboQB569T59FaUTWrb4obrj79pVdpCq+zmFmmXsY68Bzw2nEJIj+BQeOYuoLo mNSM3XaUR5Vm3xnbANNqDO3leBrINVXSLLerFKig= 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 C76D53858D1E for ; Fri, 8 Sep 2023 12:40:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C76D53858D1E 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 3887k4VS016471 for ; Fri, 8 Sep 2023 05:40:07 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3syygugtav-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 08 Sep 2023 05:40:07 -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; Fri, 8 Sep 2023 05:40:05 -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; Fri, 8 Sep 2023 05:40:05 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 4B9C95B693F; Fri, 8 Sep 2023 05:40:05 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] Fix PR 111331: wrong code for `a > 28 ? MIN : 29` Date: Fri, 8 Sep 2023 05:39:55 -0700 Message-ID: <20230908123955.1196607-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: LMfWelJ3kMwrHOpVnmnpPfHLc2RWmeza X-Proofpoint-ORIG-GUID: LMfWelJ3kMwrHOpVnmnpPfHLc2RWmeza X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-09-08_09,2023-09-05_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: , 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: 1776473190555673049 X-GMAIL-MSGID: 1776473190555673049 The problem here is after r6-7425-ga9fee7cdc3c62d0e51730, the comparison to see if the transformation could be done was using the wrong value. Instead of see if the inner was LE (for MIN and GE for MAX) the outer value, it was comparing the inner to the value used in the comparison which was wrong. The match pattern copied the same logic mistake when they were added in r14-1411-g17cca3c43e2f49 . OK? Bootstrapped and tested on x86_64-linux-gnu. gcc/ChangeLog: PR tree-optimization/111331 * match.pd (`(a CMP CST1) ? max : a`): Fix the LE/GE comparison to the correct value. * tree-ssa-phiopt.cc (minmax_replacement): Fix the LE/GE comparison for the `(a CMP CST1) ? max : a` optimization. gcc/testsuite/ChangeLog: PR tree-optimization/111331 * gcc.c-torture/execute/pr111331-1.c: New test. * gcc.c-torture/execute/pr111331-2.c: New test. * gcc.c-torture/execute/pr111331-3.c: New test. --- gcc/match.pd | 4 ++-- .../gcc.c-torture/execute/pr111331-1.c | 17 +++++++++++++++++ .../gcc.c-torture/execute/pr111331-2.c | 19 +++++++++++++++++++ .../gcc.c-torture/execute/pr111331-3.c | 15 +++++++++++++++ gcc/tree-ssa-phiopt.cc | 8 ++++---- 5 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr111331-1.c create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr111331-2.c create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr111331-3.c diff --git a/gcc/match.pd b/gcc/match.pd index 8c24dae71cd..c7b6db4b543 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5438,11 +5438,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) } (if ((cmp == LT_EXPR || cmp == LE_EXPR) && code == MIN_EXPR - && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @1))) + && integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, @3, @4))) (min @2 @4) (if ((cmp == GT_EXPR || cmp == GE_EXPR) && code == MAX_EXPR - && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @1))) + && integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, @3, @4))) (max @2 @4)))))) #if GIMPLE diff --git a/gcc/testsuite/gcc.c-torture/execute/pr111331-1.c b/gcc/testsuite/gcc.c-torture/execute/pr111331-1.c new file mode 100644 index 00000000000..4c7f4fdbaa9 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr111331-1.c @@ -0,0 +1,17 @@ +int a; +int b; +int c(int d, int e, int f) { + if (d < e) + return e; + if (d > f) + return f; + return d; +} +int main() { + int g = -1; + a = c(b + 30, 29, g + 29); + volatile t = a; + if (t != 28) + __builtin_abort(); + return 0; +} diff --git a/gcc/testsuite/gcc.c-torture/execute/pr111331-2.c b/gcc/testsuite/gcc.c-torture/execute/pr111331-2.c new file mode 100644 index 00000000000..5c677f2caa9 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr111331-2.c @@ -0,0 +1,19 @@ + +int a; +int b; + +int main() { + int d = b+30; + { + int t; + if (d < 29) + t = 29; + else + t = (d > 28) ? 28 : d; + a = t; + } + volatile int t = a; + if (a != 28) + __builtin_abort(); + return 0; +} diff --git a/gcc/testsuite/gcc.c-torture/execute/pr111331-3.c b/gcc/testsuite/gcc.c-torture/execute/pr111331-3.c new file mode 100644 index 00000000000..213d9bdd539 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/pr111331-3.c @@ -0,0 +1,15 @@ +int a; +int b; + +int main() { + int d = b+30; + { + int t; + t = d < 29 ? 29 : ((d > 28) ? 28 : d); + a = t; + } + volatile int t = a; + if (a != 28) + __builtin_abort(); + return 0; +} diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index 9993bbe5b76..9b44ca9758a 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -2073,7 +2073,7 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_ /* We need BOUND <= LARGER. */ if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, - bound, larger))) + bound, arg_false))) return false; } else if (operand_equal_for_phi_arg_p (arg_false, smaller) @@ -2104,7 +2104,7 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_ /* We need BOUND >= SMALLER. */ if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, - bound, smaller))) + bound, arg_false))) return false; } else @@ -2144,7 +2144,7 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_ /* We need BOUND >= LARGER. */ if (!integer_nonzerop (fold_build2 (GE_EXPR, boolean_type_node, - bound, larger))) + bound, arg_true))) return false; } else if (operand_equal_for_phi_arg_p (arg_true, smaller) @@ -2171,7 +2171,7 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_ /* We need BOUND <= SMALLER. */ if (!integer_nonzerop (fold_build2 (LE_EXPR, boolean_type_node, - bound, smaller))) + bound, arg_true))) return false; } else