From patchwork Tue Oct 1 23:10:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1991744 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=PitNv6kw; 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 4XJDGD5xBcz1xsq for ; Wed, 2 Oct 2024 09:11:31 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B5B7A3861826 for ; Tue, 1 Oct 2024 23:11:29 +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 5274F385EC15 for ; Tue, 1 Oct 2024 23:11:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5274F385EC15 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 5274F385EC15 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=1727824270; cv=none; b=pTrIR3J/SOMh/Iyss9rv0GdzUARqyJyPbrfzIEBaZzvb57BorOd7lLZPiccaQv+VaK7+JWR2SOz2ktiftBSYnAsG6u3BGSnEVawLKkl0rI+s0CidjQY6jQLtDoyBv3KB+vixaYAeBKSdpnZgPw0e9GViWmTH46Sd87PDZfAhj5c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1727824270; c=relaxed/simple; bh=gO7zExsw67OD6ciuS2GNQh6BMg+tiEDQRKLHDDkaVPc=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=e/PS0ghyeKaKQJqOFz+YudgMOgZube27MRDF+mzhesP95a8gsT0+kgii/xI//HfiijyCcLNfQSe8Xjpkgz5LnCkJRasYkXJx/966rohFpKjJ4Spa3Eo+LkW89fBsiLoh0tD+OW2pvvX66ECf2I58g2EVpOkGhjfOHjAHjOE80Jk= 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 491F5NN0015065 for ; Tue, 1 Oct 2024 23:11:06 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=JVu4L/Pj3w3kO0KqJROj+I LqMDSs1ka2tlUsUBXxbN8=; b=PitNv6kwy4T4acRvDbyv9JI9ovBX2/heq3Smyl mtqf+zvzACDhBL4ej/n2bTDJM47TJBrxV9rczWzGR45QqttYKSJneBxRcTBvwByZ lqdmST34XLYsgnziUlGZU0olCBZC3OBbGG5cEVmYX3BDKsTN3ca0/ORpzLHbJSu0 /OBQDVLFZHFz7+uOe5Fk1rI7WsJb8k6WeIrAyIBLdETNYKGiBG0FgOFudAY7cxVA tllCMu1KACkzEVMReCAjlBrIjwtPYslXNgaAghARVkA1jsXdErByWEiUehH/V/1Q JaGZ2rY8wUyUq5ILb+Dg3evsb0oyNiJnleHqdoSekBPaFXEA== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 41yprap7u8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 01 Oct 2024 23:11:06 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA05.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 491NB5su024363 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 1 Oct 2024 23:11:05 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; Tue, 1 Oct 2024 16:11:05 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] phiopt: Fix VCE moving by rewriting it into cast [PR116098] Date: Tue, 1 Oct 2024 16:10:54 -0700 Message-ID: <20241001231054.1356313-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: 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-GUID: Em2Rj1kydnCI_N8LrNEwAKcdSl-jL9c0 X-Proofpoint-ORIG-GUID: Em2Rj1kydnCI_N8LrNEwAKcdSl-jL9c0 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=999 bulkscore=0 priorityscore=1501 suspectscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2408220000 definitions=main-2410010154 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, KAM_SHORT, 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 Phiopt match_and_simplify might move a well defined VCE assign statement from being conditional to being uncondtitional; that VCE might no longer being defined. It will need a rewrite into a cast instead. This adds the rewriting code to move_stmt for the VCE case. This is enough to fix the issue at hand. It should also be using rewrite_to_defined_overflow but first I need to move the check to see a rewrite is needed into its own function and that is causing issues (see https://gcc.gnu.org/pipermail/gcc-patches/2024-September/663938.html). Plus this version is easiest to backport. Bootstrapped and tested on x86_64-linux-gnu. PR tree-optimization/116098 gcc/ChangeLog: * tree-ssa-phiopt.cc (move_stmt): Rewrite VCEs from integer to integer types to case. gcc/testsuite/ChangeLog: * c-c++-common/torture/pr116098-2.c: New test. * g++.dg/torture/pr116098-1.C: New test. Signed-off-by: Andrew Pinski --- .../c-c++-common/torture/pr116098-2.c | 46 +++++++++++++++++++ gcc/testsuite/g++.dg/torture/pr116098-1.C | 33 +++++++++++++ gcc/tree-ssa-phiopt.cc | 28 ++++++++++- 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/c-c++-common/torture/pr116098-2.c create mode 100644 gcc/testsuite/g++.dg/torture/pr116098-1.C diff --git a/gcc/testsuite/c-c++-common/torture/pr116098-2.c b/gcc/testsuite/c-c++-common/torture/pr116098-2.c new file mode 100644 index 00000000000..614ed049171 --- /dev/null +++ b/gcc/testsuite/c-c++-common/torture/pr116098-2.c @@ -0,0 +1,46 @@ +/* { dg-do run } */ +/* PR tree-optimization/116098 */ + + +#include + +struct Value { + int type; + union { + bool boolean; + long long t; + }; +}; + +static struct Value s_item_mem; + +/* truthy was being miscompiled for the value.type==2 case, + because we would have a VCE from unsigned char to bool + that went from being conditional in the value.type==1 case + to unconditional when `value.type!=0`. + The move of the VCE from conditional to unconditional, + needs to changed into a convert (NOP_EXPR). */ +static bool truthy(void) __attribute__((noipa)); +static bool +truthy(void) +{ + struct Value value = s_item_mem; + if (value.type == 0) + return 0; + if (value.type == 1) + return value.boolean; + return 1; +} + +int +main(void) +{ + s_item_mem.type = 2; + s_item_mem.t = -1; + bool b1 = !truthy(); + s_item_mem.type = 1; + s_item_mem.boolean = b1; + bool b = truthy(); + if (b1 != b) __builtin_abort(); + if (b) __builtin_abort(); +} diff --git a/gcc/testsuite/g++.dg/torture/pr116098-1.C b/gcc/testsuite/g++.dg/torture/pr116098-1.C new file mode 100644 index 00000000000..90e44a6eeed --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/pr116098-1.C @@ -0,0 +1,33 @@ +// { dg-do run } +/* PR tree-optimization/116098 */ + + +static bool truthy(int type, unsigned char data) __attribute__((noipa)); +/* truthy was being miscompiled for the type==2 case, + because we would have a VCE from unsigned char to bool + that went from being conditional in the type==1 case + to unconditional when `type!=0`. + The move of the VCE from conditional to unconditional, + needs to changed into a convert (NOP_EXPR). */ + +static bool truthy(void) __attribute__((noipa)); +static bool +truthy(int type, unsigned char data) +{ + if (type == 0) + return 0; + if (type == 1) + /* Emulate what SRA does, so this can be + tested without depending on SRA. */ + return __builtin_bit_cast (bool, data); + return 1; +} + +int +main(void) +{ + bool b1 = !truthy(2, -1); + bool b = truthy(1, b1); + if (b1 != b) __builtin_abort(); + if (b) __builtin_abort(); +} diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index bd7f9607eb9..43b65b362a3 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -742,7 +742,8 @@ empty_bb_or_one_feeding_into_p (basic_block bb, } /* Move STMT to before GSI and insert its defining - name into INSERTED_EXPRS bitmap. */ + name into INSERTED_EXPRS bitmap. + Also rewrite its if it might be undefined when unconditionalized. */ static void move_stmt (gimple *stmt, gimple_stmt_iterator *gsi, auto_bitmap &inserted_exprs) { @@ -761,6 +762,31 @@ move_stmt (gimple *stmt, gimple_stmt_iterator *gsi, auto_bitmap &inserted_exprs) gimple_stmt_iterator gsi1 = gsi_for_stmt (stmt); gsi_move_before (&gsi1, gsi); reset_flow_sensitive_info (name); + + /* Rewrite some code which might be undefined when + unconditionalized. */ + if (gimple_assign_single_p (stmt)) + { + tree rhs = gimple_assign_rhs1 (stmt); + /* VCE from integral types to another integral types but with + different precisions need to be changed into casts + to be well defined when unconditional. */ + if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR + && INTEGRAL_TYPE_P (TREE_TYPE (name)) + && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (rhs, 0)))) + { + if (dump_file && (dump_flags & TDF_DETAILS)) + { + fprintf (dump_file, "rewriting stmt with maybe undefined VCE "); + print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); + } + tree new_rhs = TREE_OPERAND (rhs, 0); + gcc_assert (is_gimple_val (new_rhs)); + gimple_assign_set_rhs_code (stmt, NOP_EXPR); + gimple_assign_set_rhs1 (stmt, new_rhs); + update_stmt (stmt); + } + } } /* RAII style class to temporarily remove flow sensitive