From patchwork Sat Nov 2 15:09:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 2005511 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=quicinc.com header.i=@quicinc.com header.a=rsa-sha256 header.s=qcppdkim1 header.b=OzydQ9FV; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Xgh4F3QYCz1xy4 for ; Sun, 3 Nov 2024 02:10:20 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EB3EE3858283 for ; Sat, 2 Nov 2024 15:10:16 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) by sourceware.org (Postfix) with ESMTPS id 1757B3858D33 for ; Sat, 2 Nov 2024 15:09:38 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1757B3858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=quicinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=quicinc.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1757B3858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.168.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730560181; cv=none; b=uou/h9YfG/LTOE+ruhTQaBi10xP2XTMCUsOeUDv+keMsEAbj7OOKXp3qVHz1jiUGdcM6m89AmxioUUTSlhaX9CxiPm4RVrmYSQpoGt9KzZ1w84rL9BpeEBmqu6h4w85eoUU4Z2W3mT2vQ1tgTJMhavjYizt8wB61YLYTmE8A/lc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730560181; c=relaxed/simple; bh=3OmI/x6d69i1yfbdMBdfnJyjJ8mauv5HYim845O6ltg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=tYjByg1NbfIvIEXqOrgem5ODS1BSC62Kf8KcPCdmts26UjRYmX865SmKndBR/9L/R4oBZq12w2ybyk95wUwBNapEWNvQ1QDxUBEw6oNQeRC+6O8pQ+wKXotVZ3F51JkhX2sJYvuDOxbw5880VGWnX+M5gZSVdi8oLWIjdbBZaiQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279865.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4A2EHhXs012803 for ; Sat, 2 Nov 2024 15:09:37 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= udm+bytJtrMfy6YbYWR+aazmssNBZgY8koAqQUwHzAs=; b=OzydQ9FVyD7P8k/2 ecU5DfTDaUElKLyvla3iBr6HjQjea8kOii70gkLfSwM2tBcc+0uidDmJMRvaVQSR 3zPba1WXP3s27pnSocGAtJ3zeo03uSouX3Ko3c+llRDQgkEYvXQwepl6zneMamj5 i5Mi/6hHaFUdDgDRkDn6iNqtX5ScW+QuUmMoRsL0zCiBFhL+YJxVyxjI9cN5gpMm WOswXXHJMD/e+vDM9BhFWonTtIOMzE3b76BiNdGqSRYK8se9RwuPr2K6FKtj9QBv PzTYwZ1b7OeSy+6UVfS3bLkjSriVm076P8oNiPXb/qdx8BdkJplfHrAGSZno0ZCw +EJVxw== Received: from nasanppmta04.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 42nd5cgu19-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Sat, 02 Nov 2024 15:09:36 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA04.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 4A2F9a8O030303 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Sat, 2 Nov 2024 15:09:36 GMT Received: from hu-apinski-lv.qualcomm.com (10.49.16.6) by nasanex01c.na.qualcomm.com (10.45.79.139) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Sat, 2 Nov 2024 08:09:36 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH 2/4] VN: Handle `(a | b) !=/== 0` for predicates [PR117414] Date: Sat, 2 Nov 2024 08:09:19 -0700 Message-ID: <20241102150921.3319089-2-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241102150921.3319089-1-quic_apinski@quicinc.com> References: <20241102150921.3319089-1-quic_apinski@quicinc.com> MIME-Version: 1.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01a.na.qualcomm.com (10.47.209.196) To nasanex01c.na.qualcomm.com (10.45.79.139) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: hfosgRqAfxTFaEf4dJ9HvX8q58aMfeby X-Proofpoint-GUID: hfosgRqAfxTFaEf4dJ9HvX8q58aMfeby X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-06_09,2024-09-06_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 spamscore=0 mlxscore=0 impostorscore=0 lowpriorityscore=0 bulkscore=0 suspectscore=0 adultscore=0 clxscore=1015 phishscore=0 malwarescore=0 mlxlogscore=780 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2409260000 definitions=main-2411020134 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~incoming=patchwork.ozlabs.org@gcc.gnu.org For `(a | b) == 0`, we can "assert" on the true edge that both `a == 0` and `b == 0` but nothing on the false edge. For `(a | b) != 0`, we can "assert" on the false edge that both `a == 0` and `b == 0` but nothing on the true edge. This adds that predicate and allows us to optimize f0, f1, and f2 in fre-predicated-[12].c. Bootstrapped and tested on x86_64-linux-gnu. PR tree-optimization/117414 gcc/ChangeLog: * tree-ssa-sccvn.cc (insert_predicates_for_cond): Handle `(a | b) !=/== 0` also. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/fre-predicated-1.c: New test. * gcc.dg/tree-ssa/fre-predicated-2.c: New test. Signed-off-by: Andrew Pinski --- .../gcc.dg/tree-ssa/fre-predicated-1.c | 53 +++++++++++++++++++ .../gcc.dg/tree-ssa/fre-predicated-2.c | 27 ++++++++++ gcc/tree-ssa-sccvn.cc | 26 +++++++++ 3 files changed, 106 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-2.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-1.c b/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-1.c new file mode 100644 index 00000000000..d56952f5f24 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-1.c @@ -0,0 +1,53 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +/* PR tree-optimization/117414 */ + +/* Fre1 should figure out that `*aaa != 0` + For f0, f1, and f2. */ + + +void foo(); +int f0(int *aaa, int j, int t) +{ + int b = *aaa; + int c = b != 0; + int d = t != 0; + if (d | c) + return 0; + for(int i = 0; i < j; i++) + { + if (*aaa) foo(); + } + return 0; +} + +int f1(int *aaa, int j, int t) +{ + int b = *aaa; + if (b != 0 || t != 0) + return 0; + for(int i = 0; i < j; i++) + { + if (*aaa) foo(); + } + return 0; +} + + +int f2(int *aaa, int j, int t) +{ + int b = *aaa; + if (b != 0) + return 0; + if (t != 0) + return 0; + for(int i = 0; i < j; i++) + { + if (*aaa) foo(); + } + return 0; +} + +/* { dg-final { scan-tree-dump-not "foo " "optimized" } } */ +/* { dg-final { scan-tree-dump "return 0;" "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-2.c b/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-2.c new file mode 100644 index 00000000000..0123a5b54f7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/fre-predicated-2.c @@ -0,0 +1,27 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +/* PR tree-optimization/117414 */ + +/* Fre1 should figure out that `*aaa != 0` + For f0, f1, and f2. */ + + +void foo(); +int f0(int *aaa, int j, int t) +{ + int b = *aaa; + int d = b | t; + if (d == 0) + ; + else + return 0; + for(int i = 0; i < j; i++) + { + if (*aaa) foo(); + } + return 0; +} + +/* { dg-final { scan-tree-dump-not "foo " "optimized" } } */ +/* { dg-final { scan-tree-dump "return 0;" "optimized" } } */ diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc index a11bf968670..b3e6cd09007 100644 --- a/gcc/tree-ssa-sccvn.cc +++ b/gcc/tree-ssa-sccvn.cc @@ -7901,6 +7901,9 @@ static void insert_predicates_for_cond (tree_code code, tree lhs, tree rhs, edge true_e, edge false_e) { + /* If both edges are null, then there is nothing to be done. */ + if (!true_e && !false_e) + return; tree_code icode = invert_tree_comparison (code, HONOR_NANS (lhs)); tree ops[2]; ops[0] = lhs; @@ -7929,6 +7932,29 @@ insert_predicates_for_cond (tree_code code, tree lhs, tree rhs, if (false_e) insert_related_predicates_on_edge (icode, ops, false_e); } + if (integer_zerop (rhs) + && (code == NE_EXPR || code == EQ_EXPR) + && TREE_CODE (lhs) == SSA_NAME) + { + gimple *def_stmt = SSA_NAME_DEF_STMT (lhs); + /* (a | b) == 0 -> + on true edge assert: a == 0 & b == 0. */ + /* (a | b) != 0 -> + on false edge assert: a == 0 & b == 0. */ + if (is_gimple_assign (def_stmt) + && gimple_assign_rhs_code (def_stmt) == BIT_IOR_EXPR) + { + edge e = code == EQ_EXPR ? true_e : false_e; + insert_predicates_for_cond (EQ_EXPR, + gimple_assign_rhs1 (def_stmt), + rhs, + e, nullptr); + insert_predicates_for_cond (EQ_EXPR, + gimple_assign_rhs2 (def_stmt), + rhs, + e, nullptr); + } + } } /* Main stmt worker for RPO VN, process BB. */