From patchwork Mon Sep 30 21:50:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1991210 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=Br58VIoJ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4XHZWV1Xs5z1xtY for ; Tue, 1 Oct 2024 07:50:44 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2C2383860768 for ; Mon, 30 Sep 2024 21:50:42 +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 6FBDE3858D26 for ; Mon, 30 Sep 2024 21:50:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6FBDE3858D26 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 6FBDE3858D26 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=1727733024; cv=none; b=xw+qqRNRKfVb1LZhimfHjdrSl61tqHscIWSONZp3p6Zd14+blmbVR7Z/XcK0A9pi+TmDIr8jYEhzSWGdw95vibNpDp/79chbZomnjT2748OsSpm2WAytSm+E5A8d0LydXqLmkNdX4WqyyckzCANENDAFZ3XexXd3l8ImhAMqVpE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727733024; c=relaxed/simple; bh=SzTtFaoVyFDS0puKSCDpQm1ZmD4m19Xyuw4ObxyGA8I=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=F/s8ixCa+whdXkqi1ZkHKCare9aBW17YEv+Ifm/2mQUvmCOntQv2JpJG/Ljucr3OeZ/VrL+exSQ4TywTeJQz87fxJGRuMaMbWwqcEij4NlxtjsdeOxa+IZWl4I8u0eEZBXULHjZdBCEdx5Ij2kem8vka/mFsElkdEsRrrR1xRGg= 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 48UGHNwq014765 for ; Mon, 30 Sep 2024 21:50:21 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:message-id :mime-version:subject:to; s=qcppdkim1; bh=wimexgoIx1j9dqbNHh8PYX 9xNlzhjt9TVp21+Z1yRPU=; b=Br58VIoJLwzOHmBnOoU/xqeuWUYMTAxBG5ohaN 1w27JljSTb/5+VrQhbtT9AMR+OVzTtNCnk8XfVV8MaEk8HJmlsrp6CWKCN4oxoL7 PtEmrBFKgpybqT24I4mKXVHtf/yiZBL6yq1s/uEjOXtSiFzG0yBCToeQJr0okJbP JUFWLJiIjBLG/FZQ+kJ3MOUryX3NwbqjsM0hDGyLBPSnK7hADoulGeocr3aALBmH dvAUZHmuULbWDJoYoNRW8y3H+U7VsNOOQfsV8wZnLIPo3twBkvl97wO2vTSNfwej pTmeVZRsOaJUXST3p7c09ppo+iP8FJirSOq9iyNXptGUMh4g== Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 41yprajbte-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 30 Sep 2024 21:50:21 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA01.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 48ULoKkU009987 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 30 Sep 2024 21:50:20 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; Mon, 30 Sep 2024 14:50:20 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] phi-opt: Improve factor heurstic with constants and conversions from bool [PR116890] Date: Mon, 30 Sep 2024 14:50:10 -0700 Message-ID: <20240930215010.768869-1-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 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-GUID: pCd15mRQeFETDLwaRkZxpORS_zkrYOh7 X-Proofpoint-ORIG-GUID: pCd15mRQeFETDLwaRkZxpORS_zkrYOh7 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 lowpriorityscore=0 malwarescore=0 spamscore=0 mlxscore=0 phishscore=0 impostorscore=0 clxscore=1015 adultscore=0 mlxlogscore=550 bulkscore=0 priorityscore=1501 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2408220000 definitions=main-2409300156 X-Spam-Status: No, score=-13.9 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_NONE, 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~incoming=patchwork.ozlabs.org@gcc.gnu.org Take: ``` if (t_3(D) != 0) goto ; else goto ; _8 = c_4(D) != 0; _9 = (int) _8; # e_2 = PHI <_9(3), 0(2)> ``` We should factor out the conversion here as that will allow a simplfication to `(t_3 != 0) & (c_4 != 0)`. Unlike most other types; `a ? b : CST` will simplify for boolean result type to either `a | b` or `a & b` so allowing this conversion for all operations will be always profitable. Bootstrapped and tested on x86_64-linux-gnu with no regressions. Note on the phi-opt-7.c testcase change, we are now able to optimize this and remove the if due to the factoring out now so this is an improvement. PR tree-optimization/116890 gcc/ChangeLog: * tree-ssa-phiopt.cc (factor_out_conditional_operation): Conversions from bool is also should be considered as wanting to happen. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/phi-opt-7.c: Update testcase for no ifs left. * gcc.dg/tree-ssa/phi-opt-42.c: New test. * gcc.dg/tree-ssa/phi-opt-43.c: New test. Signed-off-by: Andrew Pinski --- gcc/testsuite/gcc.dg/tree-ssa/phi-opt-42.c | 19 +++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/phi-opt-43.c | 19 +++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/phi-opt-7.c | 6 +++--- gcc/tree-ssa-phiopt.cc | 10 +++++++++- 4 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-42.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-43.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-42.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-42.c new file mode 100644 index 00000000000..62556945159 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-42.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ + +/* PR tree-optimization/116890 */ + +int f(int a, int b, int c) +{ + int x; + if (c) x = a == 0; + else x = 0; + return x; +} + + +/* The if should have been removed as the the conversion from bool to int should have been factored out. */ +/* { dg-final { scan-tree-dump-not "if" "optimized" } }*/ +/* { dg-final { scan-tree-dump-times "\[^\r\n\]*_\[0-9\]* = a_\[0-9\]*.D. == 0" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "\[^\r\n\]*_\[0-9\]* = c_\[0-9\]*.D. != 0" 1 "optimized" } } */ + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-43.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-43.c new file mode 100644 index 00000000000..1d16f283f27 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-43.c @@ -0,0 +1,19 @@ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-optimized" } */ + +/* PR tree-optimization/116890 */ + +int f(_Bool a, _Bool b, int c) +{ + int x; + if (c) x = a & b; + else x = 0; + return x; +} + + +/* The if should have been removed as the the conversion from bool to int should have been factored out. */ +/* { dg-final { scan-tree-dump-not "if" "optimized" } }*/ +/* { dg-final { scan-tree-dump-times "\[^\r\n\]*_\[0-9\]* = a_\[0-9\]*.D. & b_\[0-9\]*.D." 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "\[^\r\n\]*_\[0-9\]* = c_\[0-9\]*.D. != 0" 1 "optimized" } } */ + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-7.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-7.c index 51e1f6dfa75..3ee43e55692 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-7.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-7.c @@ -15,8 +15,8 @@ int f(int t, int c) return g(d,e); } -/* There should be one ifs as one of them should be changed into - a conditional and the other should be there still. */ -/* { dg-final { scan-tree-dump-times "if" 1 "optimized" } }*/ +/* There should be no ifs as this is converted into `(t != 0) & (c != 0)`. +/* { dg-final { scan-tree-dump-not "if" "optimized" } }*/ /* { dg-final { scan-tree-dump-times "\[^\r\n\]*_\[0-9\]* = c_\[0-9\]*.D. != 0" 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "\[^\r\n\]*_\[0-9\]* = t_\[0-9\]*.D. != 0" 1 "optimized" } } */ diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index d43832b390b..bd7f9607eb9 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -345,9 +345,17 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi, if (gassign *assign = dyn_cast (stmt)) { tree lhs = gimple_assign_lhs (assign); + tree lhst = TREE_TYPE (lhs); enum tree_code ass_code = gimple_assign_rhs_code (assign); - if (ass_code != MAX_EXPR && ass_code != MIN_EXPR) + if (ass_code != MAX_EXPR && ass_code != MIN_EXPR + /* Conversions from boolean like types is ok + as `a?1:b` and `a?0:b` will always simplify + to `a & b` or `a | b`. + See PR 116890. */ + && !(INTEGRAL_TYPE_P (lhst) + && TYPE_UNSIGNED (lhst) + && TYPE_PRECISION (lhst) == 1)) return NULL; if (lhs != gimple_assign_rhs1 (arg0_def_stmt)) return NULL;