From patchwork Sat Aug 31 00:26:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1979240 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=MyklkUE8; 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 4WwbSv4Fm1z1yfK for ; Sat, 31 Aug 2024 10:27:41 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2AD693864C64 for ; Sat, 31 Aug 2024 00:27:39 +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 730C2384F032 for ; Sat, 31 Aug 2024 00:27:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 730C2384F032 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 730C2384F032 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=1725064036; cv=none; b=Rp1Qq7iEM5oHQizxk/4UYswDiRH9CsIBTiPvHibQAVp6WUuMFsd+yayFY7enlnzyJS0cGRvW4C+B63Ny8jGmRxFIlzmr/bZIe8E4o74NwjZFQpDzixGoFsZaNTUwe5s3klLrLSyNhP3TQrcdR6jMK7DjS/e2FWPZcj9YM69QE04= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1725064036; c=relaxed/simple; bh=gqPi1aYtqKPw7wKDUGX46zZfE3SW0w8O3F0gCqCJ2Iw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=RX/38G3iM5tE2WGFnWOu0a1zvsjKjJaz5Q4CdtP/qHxa8aV/QnfWcsB1wnV/+NBVkFFJc6mLlmnSPK2cSNpMiRTK+1ArN1UgF5aZjNn8uPVZKfLfLOatq2YEI0xYcn1dhz+jgT3y0JX0MwyFsC96BlV+C7RRDiQM26LUZu6XAwo= 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 47UEipXn030353 for ; Sat, 31 Aug 2024 00:27:12 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= 2O6nDeOHNakAQITLHynj95n+S51cLxVN3G5aRKXlmSQ=; b=MyklkUE8T8XujPYa ZzKqRCKcHxJtEIggcUAzSmX9+Fmjzpu5oAj8/X8jQgBSUlRfI4Sd7pR6L3mpwjzM A2jF4NgGWrqP1V/ckCUIMQTPdPvJi+T6Fk9+oyZm4MKVSfszYDoVY21s3lJT3U9/ tUZt/U+Hz6YrWSjr+mbxLlNdLbvF+p6q6bGzaLBZxbryNh5Kl4tYhQXxulbRIJkJ jF+U6RRjw5C9TBOphf9tokDyo7TycDLVCgANcA72MLboO4ZKR2PCe6x1BmZ69Tlj 3ddjSXX2xpgUmN2aaJGxF/uYm5iCAhe2AtwDjmlyBp0ysYPaeiG2+x/wvcYiSXyz gHjb+Q== Received: from nasanppmta03.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 419puujcur-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Sat, 31 Aug 2024 00:27:12 +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 47V0RBIn031437 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Sat, 31 Aug 2024 00:27:11 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, 30 Aug 2024 17:27:10 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH 2/2] phiopt: Ignore some nop statements in heursics [PR116098] Date: Fri, 30 Aug 2024 17:26:58 -0700 Message-ID: <20240831002658.685004-2-quic_apinski@quicinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240831002658.685004-1-quic_apinski@quicinc.com> References: <20240831002658.685004-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-GUID: HCS7qgn5k_PvDAFQPZn4rY-sW1VoBqnE X-Proofpoint-ORIG-GUID: HCS7qgn5k_PvDAFQPZn4rY-sW1VoBqnE X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-30_12,2024-08-30_01,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 impostorscore=0 adultscore=0 malwarescore=0 suspectscore=0 bulkscore=0 clxscore=1015 mlxlogscore=986 mlxscore=0 phishscore=0 lowpriorityscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2407110000 definitions=main-2408310002 X-Spam-Status: No, score=-13.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, 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.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 heurstics that was added for PR71016, try to search to see if the conversion was being moved away from its definition. The problem is the heurstics would stop if there was a non GIMPLE_ASSIGN (and already ignores debug statements) and in this case we would have a GIMPLE_LABEL that was not being ignored. So we should need to ignore GIMPLE_NOP, GIMPLE_LABEL and GIMPLE_PREDICT. Note this is now similar to how gimple_empty_block_p behaves. Note this fixes the wrong code that was reported by moving the VCE (conversion) out before the phiopt/match could convert it into an bit_ior and move the VCE out with the VCE being conditionally valid. Bootstrapped and tested on x86_64-linux-gnu. Also built and tested for aarch64-linux-gnu. PR tree-optimization/116098 gcc/ChangeLog: * tree-ssa-phiopt.cc (factor_out_conditional_operation): Ignore nops, labels and predicts for heuristic for conversion with a constant. gcc/testsuite/ChangeLog: * c-c++-common/torture/pr116098-1.c: New test. * gcc.target/aarch64/csel-1.c: New test. Signed-off-by: Andrew Pinski --- .../c-c++-common/torture/pr116098-1.c | 84 +++++++++++++++++++ gcc/testsuite/gcc.target/aarch64/csel-1.c | 28 +++++++ gcc/tree-ssa-phiopt.cc | 9 +- 3 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/torture/pr116098-1.c create mode 100644 gcc/testsuite/gcc.target/aarch64/csel-1.c diff --git a/gcc/testsuite/c-c++-common/torture/pr116098-1.c b/gcc/testsuite/c-c++-common/torture/pr116098-1.c new file mode 100644 index 00000000000..b9d9a342305 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/pr116098-1.c @@ -0,0 +1,84 @@ +/* { dg-do run } */ +/* PR tree-optimization/116098 */ +/* truthy was being miscompiled where the VCE was not being pulled out + of the if statement by factor_out_conditional_operation before the rest of + phiopt would happen which assumed VCE would be correct. */ +/* The unused label was causing truthy to have different code generation than truthy_1. */ + + +#ifndef __cplusplus +#define bool _Bool +#endif + +enum ValueType { + VALUE_BOOLEAN, + VALUE_NUM, +}; + +struct Value { + enum ValueType type; + union { + bool boolean; + int num; + }; +}; + +static struct Value s_value; +static bool s_b; + + +bool truthy_1(void) __attribute__((noinline)); +bool +truthy_1(void) +{ + struct Value value = s_value; + if (s_b) s_b = 0; + enum ValueType t = value.type; + if (t != VALUE_BOOLEAN) + return 1; + return value.boolean; +} +bool truthy(void) __attribute__((noinline)); +bool +truthy(void) +{ + struct Value value = s_value; + if (s_b) s_b = 0; + enum ValueType t = value.type; + if (t != VALUE_BOOLEAN) + return 1; + /* This unused label should not cause any difference in code generation. */ +a: __attribute__((unused)); + return value.boolean; +} + +int +main(void) +{ + s_b = 0; + s_value = (struct Value) { + .type = VALUE_NUM, + .num = 2, + }; + s_value = (struct Value) { + .type = VALUE_BOOLEAN, + .boolean = !truthy_1(), + }; + bool b = truthy_1(); + if (b) + __builtin_abort(); + + s_b = 0; + s_value = (struct Value) { + .type = VALUE_NUM, + .num = 2, + }; + s_value = (struct Value) { + .type = VALUE_BOOLEAN, + .boolean = !truthy(), + }; + b = truthy(); + if (b) + __builtin_abort(); +} + diff --git a/gcc/testsuite/gcc.target/aarch64/csel-1.c b/gcc/testsuite/gcc.target/aarch64/csel-1.c new file mode 100644 index 00000000000..a20d39ea375 --- /dev/null +++ b/gcc/testsuite/gcc.target/aarch64/csel-1.c @@ -0,0 +1,28 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +/* These 2 functions should be the same; even though there is a label in f1. + The label should not make a difference in code generation. + There sign extend should be removed as it is not needed. */ +void f(int t, int a, short *b) +{ + short t1 = 1; + if (a) + { + t1 = t; + } + *b = t1; +} + +void f1(int t, int a, short *b) +{ + short t1 = 1; + if (a) + { + label1: __attribute__((unused)) + t1 = t; + } + *b = t1; +} + +/* { dg-final { scan-assembler-not "sxth\t" } } */ diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index 9a009e187ee..271a5d51f09 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -324,8 +324,13 @@ factor_out_conditional_operation (edge e0, edge e1, gphi *phi, gsi_prev_nondebug (&gsi); if (!gsi_end_p (gsi)) { - if (gassign *assign - = dyn_cast (gsi_stmt (gsi))) + gimple *stmt = gsi_stmt (gsi); + /* Ignore nops, predicates and labels. */ + if (gimple_code (stmt) == GIMPLE_NOP + || gimple_code (stmt) == GIMPLE_PREDICT + || gimple_code (stmt) == GIMPLE_LABEL) + ; + else if (gassign *assign = dyn_cast (stmt)) { tree lhs = gimple_assign_lhs (assign); enum tree_code ass_code