From patchwork Fri Sep 20 01:06:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 1987611 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=WeLqevjX; 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 4X8vP14yQJz1y2J for ; Fri, 20 Sep 2024 11:06:57 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 03C9E3858C33 for ; Fri, 20 Sep 2024 01:06:55 +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 3B1993858D29 for ; Fri, 20 Sep 2024 01:06:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3B1993858D29 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 3B1993858D29 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=1726794396; cv=none; b=O/5T5Jk5qKytmzdBlekNSRYGBGdv62PQOgM98OX0XqkBVnm5z8sRAO0j/NLp02OlqwIOzZDIa0oSXaaAhIHXl7Da+R1fM1PzVsBiiDL9pjxtBHB08MFXp6o/W3+iySotmGCXt75DcfinIwZ23KdI/9XuirGqYqEZJiPdWuNKlWQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1726794396; c=relaxed/simple; bh=0efo3nundywi8e/p6fqMLk0D9P4UOHyveDXTLJHMiH8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=x1FapB7QOavu39lFGkAynAOwP43y/gRnbBwB5I7LCbjs/Q8pQZ/QXSmAGgh5yg5JrnSD56J5JTCWcer9Po/VC15wprfsujNyRrqJrXLD5ciahtJrYskP2NN8WzB+2PJOcA1Dy+v7gfLbZa2Gpa4yDtBZGXZeEMe/WYcLi00HYkU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0279872.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 48JJ44La018621 for ; Fri, 20 Sep 2024 01:06:33 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=VaXoK73cQuX1SLpMVHcvED Uodar6mbRaY2xpu/4rosA=; b=WeLqevjXgU4nKLn7Sc47P48IoYefLrooIC1wHB P+82jKMjqWfxwWMX/02NXJKKOwck6H52MjAqUBajnulWJWjc6WQ+MURZjh6jMNM2 oo0MxeEIp4Kyf77PsMGaopE5AJXFgzh4ZloNfhACvJzeF0VjsfoAT/PCyYv+4OrS FNPoE948TUhdAh0aWl68torcvMI7V5seCyeSbMVRN2gYje27yseYcPoNh+P8Mzol JlvmPviAZCTxhf1bLuBF0M6MGo8/b+ZbrGqNNLEq7oz8C+GmlYxf7Skb/4XcRlQc 9Z8V2hiZZG+eW6ii4W43OrnVZZWWC1OZ59tJahmoby6cmMLQ== Received: from nasanppmta05.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 41n4j6ycmt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 20 Sep 2024 01:06:33 +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 48K16WLb028611 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Fri, 20 Sep 2024 01:06:32 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, 19 Sep 2024 18:06:31 -0700 From: Andrew Pinski To: CC: Andrew Pinski Subject: [PATCH] match: Fix `a != 0 ? a * b : 0` patterns for things that trap [PR116772] Date: Thu, 19 Sep 2024 18:06:21 -0700 Message-ID: <20240920010621.3217134-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-ORIG-GUID: KBYXJYhzr4141Kj-F-NFtwFOb3VcP8BZ X-Proofpoint-GUID: KBYXJYhzr4141Kj-F-NFtwFOb3VcP8BZ 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 phishscore=0 mlxlogscore=999 mlxscore=0 malwarescore=0 bulkscore=0 priorityscore=1501 clxscore=1015 adultscore=0 suspectscore=0 lowpriorityscore=0 spamscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2408220000 definitions=main-2409200005 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, PLING_QUERY, 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 For generic, `a != 0 ? a * b : 0` would match where `b` would be an expression which trap (in the case of the testcase, it was an integer division but it could be any). This fixes the issue by adding a condition for `(a != 0 ? expr : 0)` to check for expressions which have side effects or traps. PR middle-end/116772 gcc/ChangeLog: * match.pd (`a != 0 ? a / b : 0`): Add a check to make sure b does not trap or have side effects. (`a != 0 ? a * b : 0`, `a != 0 ? a & b : 0`): Likewise. gcc/testsuite/ChangeLog: * gcc.dg/torture/pr116772-1.c: New test. Signed-off-by: Andrew Pinski --- gcc/match.pd | 12 ++++++++++-- gcc/testsuite/gcc.dg/torture/pr116772-1.c | 24 +++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/torture/pr116772-1.c diff --git a/gcc/match.pd b/gcc/match.pd index fdb59ff0d44..db46f319c5f 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4663,7 +4663,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (simplify (cond (ne @0 integer_zerop) (op@2 @3 @1) integer_zerop ) (if (bitwise_equal_p (@0, @3) - && tree_expr_nonzero_p (@1)) + && tree_expr_nonzero_p (@1) + /* Cannot make a trapping expression or with one with side + effects unconditional. */ + && !generic_expr_could_trap_p (@3) + && (GIMPLE || !TREE_SIDE_EFFECTS (@3))) @2))) /* Note we prefer the != case here @@ -4673,7 +4677,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (for op (mult bit_and) (simplify (cond (ne @0 integer_zerop) (op:c@2 @1 @3) integer_zerop) - (if (bitwise_equal_p (@0, @3)) + (if (bitwise_equal_p (@0, @3) + /* Cannot make a trapping expression or with one with side + effects unconditional. */ + && !generic_expr_could_trap_p (@1) + && (GIMPLE || !TREE_SIDE_EFFECTS (@1))) @2))) /* Simplifications of shift and rotates. */ diff --git a/gcc/testsuite/gcc.dg/torture/pr116772-1.c b/gcc/testsuite/gcc.dg/torture/pr116772-1.c new file mode 100644 index 00000000000..eedd0398af1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/pr116772-1.c @@ -0,0 +1,24 @@ +/* { dg-do run } */ +/* PR middle-end/116772 */ +/* The division by `/b` should not + be made uncondtional. */ + +int mult0(int a,int b) __attribute__((noipa)); + +int mult0(int a,int b){ + return (b!=0 ? (a/b)*b : 0); +} + +int bit_and0(int a,int b) __attribute__((noipa)); + +int bit_and0(int a,int b){ + return (b!=0 ? (a/b)&b : 0); +} + +int main() { + if (mult0(3, 0) != 0) + __builtin_abort(); + if (bit_and0(3, 0) != 0) + __builtin_abort(); + return 0; +}