From patchwork Mon May 20 22:02:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1937144 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=cju1/ejM; 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 4Vjs4j1DjQz1ydW for ; Tue, 21 May 2024 08:02:45 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6D59B3858C56 for ; Mon, 20 May 2024 22:02:43 +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 0008A3858D1E for ; Mon, 20 May 2024 22:02:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0008A3858D1E 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 0008A3858D1E 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=1716242545; cv=none; b=IWhT4KhAhdCTsnl22aU6sevUADsIk2I6Z4jjq0xq99Gney2K9vVv7z6CCJIvm62dgKQnBskZP71oVnxV6/hm97ufwXiVmw+z6Or4Kc2mBVIM9hjqHLQbYGB475xryhggHwJXsjup/xZu8w6RzbVRkvtC6q9i1R6uJXSdMfQrgdU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716242545; c=relaxed/simple; bh=6ebfLn9G/k7ITAZc8tSf5PFJwuYWuCqWX4/6WFIraIM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=DwlbX8GoPwiBoxCCnB5eNw9JMaq0Hh3EpgCrWaxgq9Y6PRXJBeu/e/DZTq7iy3b4V8wgNxofSebLI0TwdyB+ErpzbOSa0wAv+ld38ne0lGW0Toh6suoGm7zd83uloP1n4mDT6l8zZ2Aa/EcNPAbAwImy5dRSqK8i8J8fdgZl6NA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279863.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 44KBu1aU002173 for ; Mon, 20 May 2024 22:02:22 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=yEL4bzf bRGEVrINFmxZzpTb4qjv6Hc+wg8SwHCXKGIk=; b=cju1/ejMvz7XXQ5OjUVMg/B NjxyTC84cZiRm7yHy7BmLKaYoqAS+D2D7dO5HbbALJfskoNEcCuILxp4JLd3sItU 9oR/MJE4PEeiQDjRR2Pqh/h3rP5Ir9QzdEQkegSUIEbBlNEDChw5La/NeGZUglL1 S/pUYrPxaKmiWLKGHpm7kv4HyQOpdFYAjNPKJ2b4gBAXRlzKh40UxnrDeFcuPY3c BL/QAm5lmFi/boHTdcpaAzPQFisc68vGSbdoj6kI16MME4V/hU5Y54nG99liQRfT geYcTmxQKiq0bAK1Yptcdxp9ys2BflTW9cMVm2488f3AzqRj7XKOyJqymSWCkDw= = Received: from nasanppmta01.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 3y6pr2mam0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 20 May 2024 22:02:21 +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 44KM2K0e022154 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 20 May 2024 22:02: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, 20 May 2024 15:02:20 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] match: Disable `(type)zero_one_valuep*CST` for 1bit signed types [PR115154] Date: Mon, 20 May 2024 15:02:07 -0700 Message-ID: <20240520220207.1977085-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: nalasex01c.na.qualcomm.com (10.47.97.35) 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: wH-FKu4KnJdJjLX2i3I37HsugIYSKuXg X-Proofpoint-GUID: wH-FKu4KnJdJjLX2i3I37HsugIYSKuXg X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.650,FMLib:17.12.28.16 definitions=2024-05-20_11,2024-05-17_03,2024-05-17_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxlogscore=586 bulkscore=0 malwarescore=0 spamscore=0 priorityscore=1501 impostorscore=0 clxscore=1015 lowpriorityscore=0 adultscore=0 suspectscore=0 mlxscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2405010000 definitions=main-2405200175 X-Spam-Status: No, score=-13.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 problem here is the pattern added in r13-1162-g9991d84d2a8435 assumes that it is well defined to multiply zero_one_valuep by the truncated converted integer constant. It is well defined for all types except for signed 1bit types. Where `a * -1` is produced which is undefined/ So disable this pattern for 1bit signed types. Note the pattern added in r14-3432-gddd64a6ec3b38e is able to workaround the undefinedness except when `-fsanitize=undefined` is turned on, this is why I added a testcase for that. OK for trunk and gcc-14 and gcc-13 branches? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/115154 gcc/ChangeLog: * match.pd (convert (mult zero_one_valued_p@1 INTEGER_CST@2)): Disable for 1bit signed types. gcc/testsuite/ChangeLog: * c-c++-common/ubsan/signed1bitfield-1.c: New test. * gcc.c-torture/execute/signed1bitfield-1.c: New test. Signed-off-by: Andrew Pinski --- gcc/match.pd | 6 +++-- .../c-c++-common/ubsan/signed1bitfield-1.c | 25 +++++++++++++++++++ .../gcc.c-torture/execute/signed1bitfield-1.c | 23 +++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/c-c++-common/ubsan/signed1bitfield-1.c create mode 100644 gcc/testsuite/gcc.c-torture/execute/signed1bitfield-1.c diff --git a/gcc/match.pd b/gcc/match.pd index 0f9c34fa897..35e3d82b131 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2395,12 +2395,14 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (mult (convert @0) @1))) /* Narrow integer multiplication by a zero_one_valued_p operand. - Multiplication by [0,1] is guaranteed not to overflow. */ + Multiplication by [0,1] is guaranteed not to overflow except for + 1bit signed types. */ (simplify (convert (mult@0 zero_one_valued_p@1 INTEGER_CST@2)) (if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (@0)) - && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0))) + && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (@0)) + && (TYPE_UNSIGNED (type) || TYPE_PRECISION (type) > 1)) (mult (convert @1) (convert @2)))) /* (X << C) != 0 can be simplified to X, when C is zero_one_valued_p. diff --git a/gcc/testsuite/c-c++-common/ubsan/signed1bitfield-1.c b/gcc/testsuite/c-c++-common/ubsan/signed1bitfield-1.c new file mode 100644 index 00000000000..2ba8cf4dab0 --- /dev/null +++ b/gcc/testsuite/c-c++-common/ubsan/signed1bitfield-1.c @@ -0,0 +1,25 @@ +/* { dg-do run } */ +/* { dg-options "-O2 -fsanitize=undefined" } */ + +/* PR tree-optimization/115154 */ +/* This was being miscompiled with -fsanitize=undefined due to + `(signed:1)(t*5)` being transformed into `-((signed:1)t)` which + is undefined. */ + +struct s { + signed b : 1; +} f; +int i = 55; +__attribute__((noinline)) +void check(int a) +{ + if (!a) + __builtin_abort(); +} +int main() { + int t = i != 5; + t = t*5; + f.b = t; + int tt = f.b; + check(f.b); +} diff --git a/gcc/testsuite/gcc.c-torture/execute/signed1bitfield-1.c b/gcc/testsuite/gcc.c-torture/execute/signed1bitfield-1.c new file mode 100644 index 00000000000..ab888ca3a04 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/signed1bitfield-1.c @@ -0,0 +1,23 @@ +/* PR tree-optimization/115154 */ +/* This was being miscompiled to `(signed:1)(t*5)` + being transformed into `-((signed:1)t)` which is undefined. + Note there is a pattern which removes the negative in some cases + which works around the issue. */ + +struct { + signed b : 1; +} f; +int i = 55; +__attribute__((noinline)) +void check(int a) +{ + if (!a) + __builtin_abort(); +} +int main() { + int t = i != 5; + t = t*5; + f.b = t; + int tt = f.b; + check(f.b); +}