From patchwork Thu Sep 7 02:02:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jiufu Guo X-Patchwork-Id: 137607 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:ab0a:0:b0:3f2:4152:657d with SMTP id m10csp2715005vqo; Wed, 6 Sep 2023 20:31:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEzFkG2tAwkFky3AjFQFCmGCgngJ1/AfW8zn+PXDlwTAAUgPaeX6HpTdeBhoKJBSWOYrmrV X-Received: by 2002:a17:906:1db2:b0:9a6:4fe0:4552 with SMTP id u18-20020a1709061db200b009a64fe04552mr3769906ejh.46.1694057513763; Wed, 06 Sep 2023 20:31:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694057513; cv=none; d=google.com; s=arc-20160816; b=PorisW4Ci854dpI9G5LxxZtym01r40L/0gEtIKot9R/Wjg2TiDtez/rqHQ8J6Sp6oK pxtL/sgcongTKy5iwTB9njHXZYvIAwJMRMQvNs0/+ffBgH59SHkP5+ka++VC1uRbGxjG 5Y7auYGSblkeZJojFinlCxhTudOSL3EeNcPfF0pxt/USDdzaK8VDdhnINc0OSCueLuo+ HevLxXzyMJzvy/6AsEI4hXvjpXJ4sd3VpBU64Hlbokehtvbmuj6wYFtxIa0a1efLub9W M1sW2QrZe+UFjKGtl3Co9dXx4978iWtEAikjHvU8sxUn+mf+K7XQvg25/1EoW+8XTUeh azsw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=ydis7vGaxoNFf8cBGuBesns1BcN+MhEEcfyY4ywBStQ=; fh=UmUX9czUiMMCVKqbgiUf4TbHn8KnplAAcpnEO35tia4=; b=B9BAEoJ9n52+QpGtPFieceZaMNtA4c0k85LUYzeVjB4RUCEsIXjmQ/WiRjL7SCHLas irPYQLcBwS5bT4zScNfUmLGjMXi69nsyflsueLU+dpsFhfGpawxLgOtI9J8SWG/V50Sa X2kMa69jPgUVNFff9WwQR/eB9c/zQlmHvPEwvre8ZG2p1RqodqLy/Cphjp++NGQOuUaL BEH33h2xLwthS22tqkZhB3p4HBPZZ7gl9KF9GURG+7y2xYTYb7tq2SHNNojLBVFgfjC3 A7RQtGPtWOTIsiUsgJX2v7yFChFYN0trE3zTkPEjK7jyXCaVRnCtMy361jlgKT9hJGOj 2GBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=RENhO0x1; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id y16-20020a1709064b1000b0098642e99c22si9806547eju.604.2023.09.06.20.31.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Sep 2023 20:31:53 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=RENhO0x1; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A7C753858425 for ; Thu, 7 Sep 2023 03:31:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A7C753858425 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1694057512; bh=ydis7vGaxoNFf8cBGuBesns1BcN+MhEEcfyY4ywBStQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=RENhO0x1Y/Mwtfd0UfYHd8pvGOGDnJJTriiWiOdRmTIhyEszo2PLqjeV8tC5ES9nz MmAppsYp2kblYCp7/nM1HM6QRgmRSY/1aW7tYn6dLTpWUlj8BKCRlBaVIOgAAyQ3Zd 8uVjp6t8leWUCamZancu51y0tseN4prYUqgzqcNk= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by sourceware.org (Postfix) with ESMTPS id 393CA3858D35; Thu, 7 Sep 2023 03:31:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 393CA3858D35 Received: from pps.filterd (m0353723.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 38738nCM005219; Thu, 7 Sep 2023 03:31:05 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3sy6410jbr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 07 Sep 2023 03:31:04 +0000 Received: from m0353723.ppops.net (m0353723.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 3873P2EA023573; Thu, 7 Sep 2023 03:31:03 GMT Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3sy6410j8n-13 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 07 Sep 2023 03:31:03 +0000 Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 3870vMgR006622; Thu, 7 Sep 2023 02:02:51 GMT Received: from smtprelay02.fra02v.mail.ibm.com ([9.218.2.226]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3svgvkqyav-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 07 Sep 2023 02:02:50 +0000 Received: from smtpav02.fra02v.mail.ibm.com (smtpav02.fra02v.mail.ibm.com [10.20.54.101]) by smtprelay02.fra02v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 38722lLc25756262 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 7 Sep 2023 02:02:48 GMT Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C439B20043; Thu, 7 Sep 2023 02:02:47 +0000 (GMT) Received: from smtpav02.fra02v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id DA07A20040; Thu, 7 Sep 2023 02:02:45 +0000 (GMT) Received: from genoa.aus.stglabs.ibm.com (unknown [9.40.192.157]) by smtpav02.fra02v.mail.ibm.com (Postfix) with ESMTP; Thu, 7 Sep 2023 02:02:45 +0000 (GMT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] Checking undefined_p before using the vr Date: Thu, 7 Sep 2023 10:02:45 +0800 Message-Id: <20230907020245.2888379-1-guojiufu@linux.ibm.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: qM7UQg0i5un_APRCdu10GZZXCW_Az4pA X-Proofpoint-ORIG-GUID: rLVRI3O_lh21g9Jk1352YYhqplrcLmvq X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-09-06_12,2023-09-05_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 phishscore=0 suspectscore=0 impostorscore=0 spamscore=0 adultscore=0 mlxscore=0 mlxlogscore=962 bulkscore=0 malwarescore=0 clxscore=1015 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2308100000 definitions=main-2309070024 X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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: , X-Patchwork-Original-From: Jiufu Guo via Gcc-patches From: Jiufu Guo Reply-To: Jiufu Guo Cc: rguenther@suse.de, segher@kernel.crashing.org, richard.sandiford@arm.com, bergner@linux.ibm.com, linkw@gcc.gnu.org, dje.gcc@gmail.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776348051711011051 X-GMAIL-MSGID: 1776348051711011051 Hi, As discussed in PR111303: For pattern "(X + C) / N": "div (plus@3 @0 INTEGER_CST@1) INTEGER_CST@2)", Even if "X" has value-range and "X + C" does not overflow, "@3" may still be undefined. Like below example: _3 = _2 + -5; if (0 != 0) goto ; [34.00%] else goto ; [66.00%] ;; succ: 3 ;; 4 ;; basic block 3, loop depth 0 ;; pred: 2 _5 = _3 / 5; ;; succ: 4 The whole pattern "(_2 + -5 ) / 5" is in "bb 3", but "bb 3" would be unreachable (because "if (0 != 0)" is always false). And "get_range_query (cfun)->range_of_expr (vr3, @3)" is checked in "bb 3", "range_of_expr" gets an "undefined vr3". Where "@3" is "_5". So, before using "vr3", it would be safe to check "!vr3.undefined_p ()". Bootstrap & regtest pass on ppc64{,le} and x86_64. Is this ok for trunk? BR, Jeff (Jiufu Guo) PR middle-end/111303 gcc/ChangeLog: * match.pd ((X - N * M) / N): Add undefined_p checking. (X + N * M) / N): Likewise. ((X + C) div_rshift N): Likewise. gcc/testsuite/ChangeLog: * gcc.dg/pr111303.c: New test. --- gcc/match.pd | 3 +++ gcc/testsuite/gcc.dg/pr111303.c | 11 +++++++++++ 2 files changed, 14 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pr111303.c diff --git a/gcc/match.pd b/gcc/match.pd index 801edb128f9..e2583ca7960 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -975,6 +975,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* "X+(N*M)" doesn't overflow. */ && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr3) && get_range_query (cfun)->range_of_expr (vr4, @4) + && !vr4.undefined_p () /* "X+N*M" is not with opposite sign as "X". */ && (TYPE_UNSIGNED (type) || (vr0.nonnegative_p () && vr4.nonnegative_p ()) @@ -995,6 +996,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* "X - (N*M)" doesn't overflow. */ && range_op_handler (MINUS_EXPR).overflow_free_p (vr0, vr3) && get_range_query (cfun)->range_of_expr (vr4, @4) + && !vr4.undefined_p () /* "X-N*M" is not with opposite sign as "X". */ && (TYPE_UNSIGNED (type) || (vr0.nonnegative_p () && vr4.nonnegative_p ()) @@ -1025,6 +1027,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* "X+C" doesn't overflow. */ && range_op_handler (PLUS_EXPR).overflow_free_p (vr0, vr1) && get_range_query (cfun)->range_of_expr (vr3, @3) + && !vr3.undefined_p () /* "X+C" and "X" are not of opposite sign. */ && (TYPE_UNSIGNED (type) || (vr0.nonnegative_p () && vr3.nonnegative_p ()) diff --git a/gcc/testsuite/gcc.dg/pr111303.c b/gcc/testsuite/gcc.dg/pr111303.c new file mode 100644 index 00000000000..eaabe55c105 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr111303.c @@ -0,0 +1,11 @@ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +/* Make sure no ICE. */ +unsigned char a; +int b(int c) { + if (c >= 5000) + return c / 5; +} +void d() { b(a - 5); } +int main() {}