From patchwork Wed Nov 6 23:42:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 2007746 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=GMRq2dgE; 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 4XkMFs21JJz1xyd for ; Thu, 7 Nov 2024 10:42:57 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5CACF385843B for ; Wed, 6 Nov 2024 23:42:55 +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 DD44E3858D21 for ; Wed, 6 Nov 2024 23:42:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DD44E3858D21 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 DD44E3858D21 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=1730936557; cv=none; b=E4Yzu3i3E0IVIOQ+9iAdUqP4vr6utzvFUbqwSy++Zj48vq4Y6IH8hFjBXUMyCgSd5JberAY7bAZ5+ozo+e3RtCbbo0m8M+gLGJxXSATVpYKoibwA62bQwgemhaxKh5WDM5yyh68lJ47/u28LxIA7C5hewYtzPLWVzOKIo0M8StQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730936557; c=relaxed/simple; bh=/zQ9f0trFFZloLz3VNi8IFlyBhUCLelgiDXDz4K+CAE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Dy2U+9oHdyhpZ2CkzUqkwZv40fXFrzF4G1NVGG17vPBZKzxTpZspBT82r0Bm0zyumb9zE1Z5CUkMUI/1MoFGyRgSyhduLP1onZk7L0qZsq+ow5d+MyCukMtcWy/rl22m5RsVTrJrlBdMXGpQOZ5zJsMgoFvlFF1yweod4FtSDrQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279862.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4A6AN1wJ004559 for ; Wed, 6 Nov 2024 23:42:31 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= opZ/cvXXz8Rg9xcxCY9lcMwcechDzrUZSGEXuJB8aS0=; b=GMRq2dgEiNAjZ0qx Y+TOozjFWGnMdTOXKswqs0SHx2bS5lHVWNFkbVH/5byf6V+IftaYEgStDCv6+FbS hGM0ZRJ6bObAK1pESqdY8tGd9JemMFfUJpjJYdrFaolmDf7x8AH4mSLSx0bgtQRG S+L1FIOjc2MRRX4F45lksSLyzftyg4ENeodiWkQzMc9JH8zrvW3oh5JWS8IRK71u LVIlg/CujqPAtBgxnvSFOFU8q7+nvUSpgb4/CL6HXEE2GCboAFwUXdsHrCBuNaHm UU1F94qaVEv9I4mL1gJX/zWEwRCrlPV1yuB3zHTc8mZQ4onHTUvuSa+5nfbeKD1I PsDxEQ== Received: from nasanppmta03.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 42qvg3udme-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 06 Nov 2024 23:42:30 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA03.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 4A6NgUeg007900 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 6 Nov 2024 23:42:30 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; Wed, 6 Nov 2024 15:42:29 -0800 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH v2 1/2] VN: Handle `(a | b) !=/== 0` for predicates [PR117414] Date: Wed, 6 Nov 2024 15:42:18 -0800 Message-ID: <20241106234219.1503566-2-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241106234219.1503566-1-quic_apinski@quicinc.com> References: <20241106234219.1503566-1-quic_apinski@quicinc.com> MIME-Version: 1.0 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01b.na.qualcomm.com (10.47.209.197) 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: i8rne55bWqHAryosGxs9NsVE_G9CINlv X-Proofpoint-GUID: i8rne55bWqHAryosGxs9NsVE_G9CINlv 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 mlxlogscore=842 lowpriorityscore=0 priorityscore=1501 clxscore=1015 mlxscore=0 spamscore=0 suspectscore=0 bulkscore=0 phishscore=0 adultscore=0 impostorscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2409260000 definitions=main-2411060183 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. Changes since v1: * v2: Use vn_valueize. Also canonicalize the comparison at the begining of insert_predicates_for_cond for constants to be on the rhs. Return early for non-ssa names on the lhs (after canonicalization). Bootstrapped and tested on x86_64-linux-gnu. PR tree-optimization/117414 gcc/ChangeLog: * tree-ssa-sccvn.cc (insert_predicates_for_cond): Canonicalize the comparison. Don't insert anything if lhs is not a SSA_NAME. Handle `(a | b) !=/== 0`. 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 | 36 +++++++++++++ 3 files changed, 116 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..c6dddd0ba6d 100644 --- a/gcc/tree-ssa-sccvn.cc +++ b/gcc/tree-ssa-sccvn.cc @@ -7901,6 +7901,21 @@ 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; + + /* Canonicalize the comparison so the rhs are constants. */ + if (CONSTANT_CLASS_P (lhs)) + { + std::swap (lhs, rhs); + code = swap_tree_comparison (code); + } + + /* If the lhs is not a ssa name, don't record anything. */ + if (TREE_CODE (lhs) != SSA_NAME) + return; + tree_code icode = invert_tree_comparison (code, HONOR_NANS (lhs)); tree ops[2]; ops[0] = lhs; @@ -7929,6 +7944,27 @@ 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)) + { + 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; + tree nlhs; + + nlhs = vn_valueize (gimple_assign_rhs1 (def_stmt)); + insert_predicates_for_cond (EQ_EXPR, nlhs, rhs, e, nullptr); + + nlhs = vn_valueize (gimple_assign_rhs2 (def_stmt)); + insert_predicates_for_cond (EQ_EXPR, nlhs, rhs, e, nullptr); + } + } } /* Main stmt worker for RPO VN, process BB. */