From patchwork Thu Apr 11 08:42:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1922425 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=HMMgDDmG; 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 4VFYBC6htjz1yY8 for ; Thu, 11 Apr 2024 18:43:15 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6411E384AB62 for ; Thu, 11 Apr 2024 08:43:13 +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 0043B385841F for ; Thu, 11 Apr 2024 08:42:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0043B385841F 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 0043B385841F 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=1712824974; cv=none; b=Tdkr25YCl5jsfc5qowFNsXThDE4xlCpTOEF2WfdUCpi7pUq0MVwy8xWtSCJvJHFAIWX8cUeKHFvnIoSn52nl+77tCsHtxHMM2uauR86jKiLVGg23bUenJ8uD0eb3zavFwIGzout8+e72yCwagC5ECMnBSElbVhSCshgnozpU+nc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712824974; c=relaxed/simple; bh=MD3IJ/COnTGt833CosfIau7ytP4gX22ogdzIcGyqJAs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=drzNRXpcIfPorTsZMTd+VLswkhtNNz11k5QFXyd0C7a5/Xcvxl2JbovE52vuUggsK7cxDdvMJmnfjUF0bMb5jbEm92sISca0LzbcnRqnnCDpGFfXuJPMu8PLqtCTCc60YOWrQhN0Ojv0wUXAu6BqMZ/xhisk9hDls8X2VZ8i9mY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279866.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.17.1.24/8.17.1.24) with ESMTP id 43B7Mr74021172 for ; Thu, 11 Apr 2024 08:42:52 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding:content-type; s=qcppdkim1; bh=OGJmPI+ Q+lBmD3ZA+FY6I125aVXvvGAr1EsrQZ/GSaU=; b=HMMgDDmGzaBneldnnZc6jR7 LNm4W1cUNMj2r10MZt7pe4tEvzMCw7zFd9+F2oB2hD6tAtLNe2qcvJTHPvaAiAsW ig8mRCca/HQSyEzAwCaEgdokrlOJiGfJAKd48WJ4Uv1LzZ74JnrWUr2RsIik9vtv chM7g6vHgD6E/wiw1zepo8xfcfOIZHaFKF/fmtOEuJH1NUsBymbnsk+B1IsVTlOR YzgU2lDC9PY5lSdEEECVRKZAlCuRMYyYR8q9QSirluSnqwU8BwBhCxwrobx0Ubt5 ElHhPyTpHcC//s+fXrrwCmUz40dNf8gOzkEWLDLxBbAIMzSlteGT57uqDgPQixw= = Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3xeb628e8v-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 11 Apr 2024 08:42:51 +0000 (GMT) Received: from nasanex01c.na.qualcomm.com (nasanex01c.na.qualcomm.com [10.45.79.139]) by NASANPPMTA01.qualcomm.com (8.17.1.5/8.17.1.5) with ESMTPS id 43B8gpIO030324 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Thu, 11 Apr 2024 08:42:51 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; Thu, 11 Apr 2024 01:42:50 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] match: Fix `!a?b:c` and `a?~t:t` patterns for signed 1 bit types [PR114666] Date: Thu, 11 Apr 2024 01:42:40 -0700 Message-ID: <20240411084240.3358337-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-ORIG-GUID: k-SJ60Wqs6iioQUvYKNo6JfQN0-h0nnE X-Proofpoint-GUID: k-SJ60Wqs6iioQUvYKNo6JfQN0-h0nnE X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.1011,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2024-04-11_02,2024-04-09_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 suspectscore=0 priorityscore=1501 mlxscore=0 mlxlogscore=999 malwarescore=0 phishscore=0 spamscore=0 bulkscore=0 adultscore=0 clxscore=1011 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2404010003 definitions=main-2404110062 X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, PLING_QUERY, SPF_HELO_NONE, SPF_PASS, 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 issue here is that the `a?~t:t` pattern assumed (maybe correctly) that a here was always going to be a unsigned boolean type. This fixes the problem in both patterns to cast the operand to boolean type first. I should note that VRP seems to be keep on wanting to produce `a == 0?1:-2` from `((int)a) ^ 1` is a bit odd and partly is the cause of the issue and there seems to be some disconnect on what should be the canonical form. That will be something to look at for GCC 15. Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/114666 gcc/ChangeLog: * match.pd (`!a?b:c`): Cast `a` to boolean type for cond for gimple. (`a?~t:t`): Cast `a` to boolean type before casting it to the type. gcc/testsuite/ChangeLog: * gcc.c-torture/execute/bitfld-signed1-1.c: New test. Signed-off-by: Andrew Pinski --- gcc/match.pd | 10 +++++++--- .../gcc.c-torture/execute/bitfld-signed1-1.c | 13 +++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/bitfld-signed1-1.c diff --git a/gcc/match.pd b/gcc/match.pd index 15a1e7350d4..ffc928b656a 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -5895,7 +5895,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* !A ? B : C -> A ? C : B. */ (simplify (cnd (logical_inverted_value truth_valued_p@0) @1 @2) - (cnd @0 @2 @1))) + /* For gimple, make sure the operand to COND is a boolean type, + truth_valued_p will match 1bit integers too. */ + (if (GIMPLE && cnd == COND_EXPR) + (cnd (convert:boolean_type_node @0) @2 @1) + (cnd @0 @2 @1)))) /* abs/negative simplifications moved from fold_cond_expr_with_comparison. @@ -7099,8 +7103,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && (!wascmp || TYPE_PRECISION (type) == 1)) (if ((!TYPE_UNSIGNED (type) && TREE_CODE (type) == BOOLEAN_TYPE) || TYPE_PRECISION (type) == 1) - (bit_xor (convert:type @0) @2) - (bit_xor (negate (convert:type @0)) @2))))) + (bit_xor (convert:type (convert:boolean_type_node @0)) @2) + (bit_xor (negate (convert:type (convert:boolean_type_node @0))) @2))))) #endif /* Simplify pointer equality compares using PTA. */ diff --git a/gcc/testsuite/gcc.c-torture/execute/bitfld-signed1-1.c b/gcc/testsuite/gcc.c-torture/execute/bitfld-signed1-1.c new file mode 100644 index 00000000000..b0ff120ea51 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/bitfld-signed1-1.c @@ -0,0 +1,13 @@ +/* PR tree-optimization/114666 */ +/* We used to miscompile this to be always aborting + due to the use of the signed 1bit into the COND_EXPR. */ + +struct { + signed a : 1; +} b = {-1}; +char c; +int main() +{ + if ((b.a ^ 1UL) < 3) + __builtin_abort(); +}