From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by sourceware.org (Postfix) with ESMTPS id 3F8D73858D3C; Wed, 13 Sep 2023 01:42:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3F8D73858D3C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linux.ibm.com Received: from pps.filterd (m0360072.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 38D1Zueb022791; Wed, 13 Sep 2023 01:42:45 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : references : date : in-reply-to : message-id : mime-version : content-type; s=pp1; bh=fdt58OgBHT7k+F4TvsUraNOkPIR5pBTErGyNXmQQGXU=; b=hLq7S9TCl40IHPzNJKWWYex2CP52bvm9+iVt0A2UglX5WUxWl2741gxeJtMbDPChs1Zy FlW6SCx+Np46sOArYEmlvJMFl/swNyeppDJG69mjK0UA7ZQ3WftCKRidQDf0UunSTZMd ztPX7rE/Rjme79wGHrMYuLa3QSME5Ac1tBM+tQ53/lpwqkMu1Hm8X0NceOoKChsdzBTW X75sXKrsZcdGnk5FwSQMeox9FhC5UHfdU4piIa52hP5lODVuVzNDsLjRk+QUL9REy8b6 GM4UTgjmNmES1JPpP9NGmW/BZNjCsb6lBhqhMPF3ZNL0cEU/2MUIITEQQJyfhLUIAhXj /w== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3t3374gj0g-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 13 Sep 2023 01:42:44 +0000 Received: from m0360072.ppops.net (m0360072.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 38D1Zvl3022956; Wed, 13 Sep 2023 01:42:44 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 3t3374gj09-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 13 Sep 2023 01:42:44 +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 38CNg5ur002806; Wed, 13 Sep 2023 01:42:43 GMT Received: from smtprelay03.wdc07v.mail.ibm.com ([172.16.1.70]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3t14hkyg29-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 13 Sep 2023 01:42:43 +0000 Received: from smtpav03.wdc07v.mail.ibm.com (smtpav03.wdc07v.mail.ibm.com [10.39.53.230]) by smtprelay03.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 38D1ghGF6554204 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Wed, 13 Sep 2023 01:42:43 GMT Received: from smtpav03.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 035A15805C; Wed, 13 Sep 2023 01:42:43 +0000 (GMT) Received: from smtpav03.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4EC9358054; Wed, 13 Sep 2023 01:42:42 +0000 (GMT) Received: from genoa (unknown [9.40.192.157]) by smtpav03.wdc07v.mail.ibm.com (Postfix) with ESMTPS; Wed, 13 Sep 2023 01:42:42 +0000 (GMT) From: Jiufu Guo To: Richard Biener Cc: gcc-patches@gcc.gnu.org, jeffreyalaw@gmail.com, richard.sandiford@arm.com, segher@kernel.crashing.org, dje.gcc@gmail.com, linkw@gcc.gnu.org, bergner@linux.ibm.com, amacleod@redhat.com, aldyh@redhat.com Subject: Re: [PATCH] Checking undefined_p before using the vr References: <20230907020245.2888379-1-guojiufu@linux.ibm.com> Date: Wed, 13 Sep 2023 09:42:39 +0800 In-Reply-To: (Richard Biener's message of "Tue, 12 Sep 2023 09:46:30 +0000 (UTC)") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-TM-AS-GCONF: 00 X-Proofpoint-GUID: bDcWV_uunG8j_bGJds6Kko3TIQaPT3iK X-Proofpoint-ORIG-GUID: 2pWn0lQ-EqvizAURG84xlzolAaUZo2I6 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-12_24,2023-09-05_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1011 suspectscore=0 adultscore=0 mlxscore=0 priorityscore=1501 bulkscore=0 impostorscore=0 mlxlogscore=999 spamscore=0 lowpriorityscore=0 malwarescore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2308100000 definitions=main-2309130011 X-Spam-Status: No, score=-10.9 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 List-Id: Hi, Richard Biener writes: > On Thu, 7 Sep 2023, Jiufu Guo wrote: > >> 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? > > OK, but I wonder why ->range_of_expr () doesn't return false for > undefined_p ()? While "undefined" technically means we can treat > it as nonnegative_p (or not, maybe but maybe not both), we seem to > not want to do that. So why expose it at all to ranger users > (yes, internally we in some places want to handle undefined). I guess, currently, it returns true and then lets the user check undefined_p, maybe because it tries to only return false if the type of EXPR is unsupported. Let "range_of_expr" return false for undefined_p would save checking undefined_p again when using the APIs. Committed va r14-3913. BR, Jeff (Jiufu Guo) > > Richard. > >> 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() {} >>