From patchwork Fri Sep 13 23:01:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1985458 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=TliMf1US; 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 4X58w34Tmzz1y2N for ; Sat, 14 Sep 2024 09:02:25 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 739983857349 for ; Fri, 13 Sep 2024 23:02:23 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by sourceware.org (Postfix) with ESMTPS id 8BF833858D26 for ; Fri, 13 Sep 2024 23:02:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8BF833858D26 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 8BF833858D26 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=205.220.180.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726268525; cv=none; b=vAwWN4C490gqLbQPsGjPNDud/qII4+9aLht2cIoDSFqTbHAzJuFnEPjvo8TSSGppaAdCn4q4GRUSDYMhtGjvb343yKGZU73N4j27Wu0rg4lRpvbtDIMDQfqO2I7TTE8902P+QcE8iLoGSeG6qbW0n+TrCh0UGwrQ6c4ZbGRO+qs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726268525; c=relaxed/simple; bh=IA6p+6Tzdbsejc94tI4N21sNwzYKG2pzqwWycnz/p9Y=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QDfaJ0UfR1GiIyyabHAatHKupbtfjwt2f6vnc7DPtrJj+cG7dUEIwyxIr189QSqBrO0R1Yqkl6uZzNCL+cRkRHt+lXhfHR7asIGS3TcQh+nIHFmK4zU5b/lX4QIceCKqX3mEgYnjVvteNyXhOxeiLYtLo884sd6hZZmWnRNEuzE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279870.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 48DAQpAD025477 for ; Fri, 13 Sep 2024 23:02:02 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=hUuJjAlyRvs0GdHNbxqoDG Y0yTNhcb1sbCHl4Uq1rQg=; b=TliMf1USZsZNmhaFE0pcT3Ua0vDnPe4Wu1n6PJ DPzhlvIImshsSHEH5ZS3CjGGpcZnTn7X6EspbxJn2/m7NllpOCTbPcrHeYp6hT1m HKpHxJv8bgUR+GQhHmM6STMOqg1GYaYixyCeb6ARRe18ZybX9VgY1xIkkWpdQvBY Kc/xg8lbYw96IcV+PVDM4mHUP4G4nnCFbhkdSLVnxuVHs84ti+CCDr3DAU6FStoV 4tdxmtIFytfa3NL2dPuKngsMudHnJcOv7uCw8z44jacWOOGOL1ZdEFssvrOkcXlX MOkxG27miqTWSTYEsuX1fIvolxALfg8322d6Q9Iu0ngCuW3Q== Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 41gy8pa7x5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 13 Sep 2024 23:02:02 +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 48DN21gp002653 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 13 Sep 2024 23:02:01 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; Fri, 13 Sep 2024 16:02:00 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] phi-opt: Improve heuristics for factoring out with constant (again) [PR116699] Date: Fri, 13 Sep 2024 16:01:50 -0700 Message-ID: <20240913230150.4071148-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: 3NTjDEkhtYru1WJfnMh2ZGQH_ftw5d0Y X-Proofpoint-ORIG-GUID: 3NTjDEkhtYru1WJfnMh2ZGQH_ftw5d0Y 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 spamscore=0 clxscore=1015 impostorscore=0 mlxscore=0 bulkscore=0 suspectscore=0 priorityscore=1501 mlxlogscore=661 lowpriorityscore=0 adultscore=0 phishscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2408220000 definitions=main-2409130163 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 The heuristics for factoring out with a constant checks that the assignment statement is the last statement of the basic block but sometimes there is a predicate or a nop statement after the assignment. Rejecting this case does not make sense since both predicates and nop statements are removed and don't contribute any instructions. So we should skip over them when checking if the assignment statement was the last statement in the basic block. phi-opt-factor-1.c's f0 is such an example where it should catch it at phiopt1 (before predicates are removed) and should happen in a similar way as f1 (which uses a temporary variable rather than return). Bootstrapped and tested on x86_64-linux-gnu. PR tree-optimization/116699 gcc/ChangeLog: * tree-ssa-phiopt.cc (factor_out_conditional_operation): Skip over nop/predicates for seeing the assignment is the last statement. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/phi-opt-factor-1.c: New test. Signed-off-by: Andrew Pinski --- .../gcc.dg/tree-ssa/phi-opt-factor-1.c | 26 +++++++++++++++++++ gcc/tree-ssa-phiopt.cc | 6 +++++ 2 files changed, 32 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-factor-1.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-factor-1.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-factor-1.c new file mode 100644 index 00000000000..12b846b9337 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-factor-1.c @@ -0,0 +1,26 @@ +/* { dg-options "-O2 -fdump-tree-phiopt" } */ + +/* PR tree-optimization/116699 + Make sure the return PREDICT has no factor in deciding + if we factor out the conversion. */ + +short f0(int a, int b, int c) +{ + int t1 = 4; + if (c < t1) return (c > -1 ? c : -1); + return t1; +} + + +short f1(int a, int b, int c) +{ + int t1 = 4; + short t = t1; + if (c < t1) t = (c > -1 ? c : -1); + return t; +} + +/* Both f1 and f0 should be optimized at phiopt1 to the same thing. */ +/* { dg-final { scan-tree-dump-times "MAX_EXPR " 2 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-times "MIN_EXPR " 2 "phiopt1" } } */ +/* { dg-final { scan-tree-dump-not "if " "phiopt1" } } */ diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index e5413e40572..7b12692237e 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -360,6 +360,12 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi, } gsi = gsi_for_stmt (arg0_def_stmt); gsi_next_nondebug (&gsi); + /* Skip past nops and predicates. */ + while (!gsi_end_p (gsi) + && (gimple_code (gsi_stmt (gsi)) == GIMPLE_NOP + || gimple_code (gsi_stmt (gsi)) == GIMPLE_PREDICT)) + gsi_next_nondebug (&gsi); + /* Reject if the statement was not at the end of the block. */ if (!gsi_end_p (gsi)) return NULL; }