From patchwork Tue May 16 01:36: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: 94412 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp99795vqo; Mon, 15 May 2023 18:37:54 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5rKdCeWwDACJtCqrdZT+UqvqSwUXaomqCguXt9K5lt1OqGETqUV1QAu66fJXjimvpTxPMG X-Received: by 2002:a05:6402:51:b0:50d:bd2e:6dfa with SMTP id f17-20020a056402005100b0050dbd2e6dfamr14931518edu.17.1684201074240; Mon, 15 May 2023 18:37:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684201074; cv=none; d=google.com; s=arc-20160816; b=IiXrrLtJ2He6bDfsfDSFAaj73XMh70aNiOQthaTC0aFD/a/pU8Ie+JIFYbQ8zrN5qb 5iXKza2z02+N3S6dV+telXCeS5QsqOqHrO2UALu8qO5oS1QoDaW3i3jJ2FaK+yVNBokP 01XdmZrWaCtCeUhKvYzC5gZq+o7SxsXE4PIcU+VasIcmOW3lBsbG4s4zz/RASZcS2Ln0 vNEvUBnk6MCbvsXG607ML/iYGNO57xyAzPqMVfvA1zcc3gs6NxB4IzdagZRJ1UvCbt/7 SjGU4Va6SbcGWJdeTgmGyjggiKRMMRXrPZbG5M+093Kbms9hw14IYzCOSALyBUt1cU47 04MQ== 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=m1g5bNzfdb2i6RXQXSpb9/Z6fYLbP2djBKXaze0Y/eg=; b=Yzw6B/rYPQYzaVmzYgi8KKs7KZoZrnZAXqWB4iz5+E08CJshJ1/4m6FzOlLHotJ70T nLklMBFgnV/8ZaGfsqq7GTe+xcPQEufVuUhR17yr6vq7ERPVL8Jf1TYZZFvZf3oIFMO4 arv+ejGz+htQn9aSxQs8xMySEWccvtQufBzyL68Dy1WPAGvcMjD1TajLujmSh6T+TsmB ciWU/BWVfrdIemHXmJaWndq2dTCfRXFegsYu27U69mUVtjenG8WXtI/hBkaN3kWpfBLP WnwBmJTUrDD/VIseQaMIYN6oqGdDpayrFj+HvTEk1ncQeGDZuLR+d4cZH7egQQwF+dKO zeQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=mz3LJmQG; 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 sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id y10-20020a056402134a00b0050d8302152esi11836845edw.261.2023.05.15.18.37.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 May 2023 18:37:54 -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=mz3LJmQG; 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 117D9385771B for ; Tue, 16 May 2023 01:37:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 117D9385771B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684201073; bh=m1g5bNzfdb2i6RXQXSpb9/Z6fYLbP2djBKXaze0Y/eg=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=mz3LJmQGlwIkwR2OWU4HsFxlH7VBulU1PoORM4FzaXwXhRTkSZoNZzAewsLNOtdfQ Wx0+0ZlZeVy4vJCi5E0dkDhpGMid939/E2Xkt6vZn5cZ9P4i0+kp8sUFDXRlwwjWo5 fULjr8q/khpdikywp7uHA6+9sYIK/CmrheSbW/fY= 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 D86623858439 for ; Tue, 16 May 2023 01:37:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D86623858439 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 34FL5SDp020571 for ; Mon, 15 May 2023 18:37:06 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3qkvbmgvat-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Mon, 15 May 2023 18:37:06 -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; Mon, 15 May 2023 18:37:04 -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; Mon, 15 May 2023 18:37:04 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 7A4913F7092; Mon, 15 May 2023 18:37:04 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: [PR109424] Simplify min/max of boolean arguments Date: Mon, 15 May 2023 18:36:55 -0700 Message-ID: <20230516013655.1660657-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: D66n4AjMV3K__Y0Xr_CbRi4MIQ_XmzED X-Proofpoint-ORIG-GUID: D66n4AjMV3K__Y0Xr_CbRi4MIQ_XmzED X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-05-15_21,2023-05-05_01,2023-02-09_01 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, KAM_SHORT, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, 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 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766012825946661115?= X-GMAIL-MSGID: =?utf-8?q?1766012825946661115?= This is version 2 of https://gcc.gnu.org/pipermail/gcc-patches/2021-August/577394.html which does not depend on adding gimple_truth_valued_p at this point. Instead will use zero_one_valued_p which is already used for mult simplifications to make sure that we only have [0,1] rather having the mistake of maybe having [-1,0] as the range for signed bools. This shows up in a few places in GCC itself but only at -O1, we miss the min/max conversion because of PR 107888 (which I will be testing seperately). OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. Thanks, Andrew Pinski PR tree-optimization/109424 gcc/ChangeLog: * match.pd: Add patterns for min/max of zero_one_valued values to `&`/`|`. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/bool-12.c: New test. * gcc.dg/tree-ssa/bool-13.c: New test. * gcc.dg/tree-ssa/minmax-20.c: New test. * gcc.dg/tree-ssa/minmax-21.c: New test. --- gcc/match.pd | 8 +++++ gcc/testsuite/gcc.dg/tree-ssa/bool-12.c | 44 +++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/bool-13.c | 38 ++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c | 27 ++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/minmax-21.c | 28 +++++++++++++++ 5 files changed, 145 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-12.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/bool-13.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/minmax-21.c diff --git a/gcc/match.pd b/gcc/match.pd index b025fb8facf..30ffdfcf8bb 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -7439,6 +7439,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && TREE_CODE (@0) != INTEGER_CST) (op @0 (ext @1 @2))))) +/* Max -> bool0 | bool1 + Min -> bool0 & bool1 */ +(for op (max min) + logic (bit_ior bit_and) + (simplify + (op zero_one_valued_p@0 zero_one_valued_p@1) + (logic @0 @1))) + /* signbit(x) != 0 ? -x : x -> abs(x) signbit(x) == 0 ? -x : x -> -abs(x) */ (for sign (SIGNBIT) diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c new file mode 100644 index 00000000000..e62594e1dad --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-12.c @@ -0,0 +1,44 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-original -fdump-tree-phiopt1 -fdump-tree-forwprop2" } */ +#define bool _Bool +int maxbool(bool ab, bool bb) +{ + int a = ab; + int b = bb; + int c; + if (a > b) + c = a; + else + c = b; + return c; +} +int minbool(bool ab, bool bb) +{ + int a = ab; + int b = bb; + int c; + if (a < b) + c = a; + else + c = b; + return c; +} +/* In Original, we should still have the if form as that is what is written. */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "original" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "original" } } */ +/* { dg-final { scan-tree-dump-times "if " 2 "original" } } */ + +/* PHI-OPT1 should have converted it into min/max */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "phiopt1" } } */ + +/* Forwprop2 (after ccp) will convert it into &\| */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "forwprop2" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "forwprop2" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "forwprop2" } } */ + +/* By optimize there should be no min/max nor if */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c b/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c new file mode 100644 index 00000000000..438f15a484a --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/bool-13.c @@ -0,0 +1,38 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized -fdump-tree-original -fdump-tree-phiopt1 -fdump-tree-forwprop2" } */ +#define bool _Bool +int maxbool(bool ab, bool bb) +{ + int a = ab; + int b = bb; + int c; + c = a > b ? a : b; + return c; +} +int minbool(bool ab, bool bb) +{ + int a = ab; + int b = bb; + int c; + c = a < b ? a : b; + return c; +} +/* In Original, we should still have the min/max form as that is what is written. */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "original" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "original" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "original" } } */ + +/* PHI-OPT1 should have kept it as min/max. */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "phiopt1" } } */ + +/* Forwprop2 (after ccp) will convert it into &\| */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "forwprop2" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "forwprop2" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "forwprop2" } } */ + +/* By optimize there should be no min/max nor if */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "if " 0 "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c new file mode 100644 index 00000000000..ab640337d69 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-20.c @@ -0,0 +1,27 @@ +/* PR tree-optimization/109424 */ +/* { dg-do compile } */ +/* Need -O2 for early non-zero */ +/* { dg-options "-O2 -fdump-tree-forwprop1-raw" } */ + +#define bool _Bool +int maxbool(bool ab, bool bb) +{ + int a = ab; + int b = bb; + int c; + c = (a > b)?a : b; + return c; +} +int minbool(bool ab, bool bb) +{ + int a = ab; + int b = bb; + int c; + c = (a < b)?a : b; + return c; +} + +/* { dg-final { scan-tree-dump-not "max_expr, " "forwprop1"} } */ +/* { dg-final { scan-tree-dump-not "min_expr, " "forwprop1"} } */ +/* { dg-final { scan-tree-dump-times "bit_and_expr, " 1 "forwprop1"} } */ +/* { dg-final { scan-tree-dump-times "bit_ior_expr, " 1 "forwprop1"} } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-21.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-21.c new file mode 100644 index 00000000000..5f2b8afc0de --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-21.c @@ -0,0 +1,28 @@ +/* PR tree-optimization/109424 */ +/* { dg-do compile } */ +/* Need -O2 for early non-zero bits */ +/* { dg-options "-O2 -fdump-tree-forwprop1-raw -fdump-tree-optimized-raw" } */ + + +int maxbool(int ab, int bb) +{ + int a = ab & 1; + int b = bb & 1; + int c; + c = (a > b)?a : b; + return c; +} +int minbool(int ab, int bb) +{ + int a = ab & 1; + int b = bb & 1; + int c; + c = (a < b)?a : b; + return c; +} + +/* { dg-final { scan-tree-dump-not "max_expr, " "forwprop1"} } */ +/* { dg-final { scan-tree-dump-not "min_expr, " "forwprop1"} } */ +/* There should only be 3 total bit_and_expr, one &1 for each function and one & for minbool. */ +/* { dg-final { scan-tree-dump-times "bit_and_expr, " 3 "optimized"} } */ +/* { dg-final { scan-tree-dump-times "bit_ior_expr, " 1 "optimized"} } */