From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id E15CA3858003 for ; Thu, 28 Oct 2021 02:20:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E15CA3858003 Received: from pps.filterd (m0098410.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 19S1povD027490; Thu, 28 Oct 2021 02:20:01 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com with ESMTP id 3byjjvgpsp-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 28 Oct 2021 02:20:00 +0000 Received: from m0098410.ppops.net (m0098410.ppops.net [127.0.0.1]) by pps.reinject (8.16.0.43/8.16.0.43) with SMTP id 19S2DtZM024446; Thu, 28 Oct 2021 02:20:00 GMT Received: from ppma03wdc.us.ibm.com (ba.79.3fa9.ip4.static.sl-reverse.com [169.63.121.186]) by mx0a-001b2d01.pphosted.com with ESMTP id 3byjjvgps4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 28 Oct 2021 02:20:00 +0000 Received: from pps.filterd (ppma03wdc.us.ibm.com [127.0.0.1]) by ppma03wdc.us.ibm.com (8.16.1.2/8.16.1.2) with SMTP id 19S2B0kA026300; Thu, 28 Oct 2021 02:19:59 GMT Received: from b03cxnp08025.gho.boulder.ibm.com (b03cxnp08025.gho.boulder.ibm.com [9.17.130.17]) by ppma03wdc.us.ibm.com with ESMTP id 3bx4f0xv30-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Thu, 28 Oct 2021 02:19:59 +0000 Received: from b03ledav003.gho.boulder.ibm.com (b03ledav003.gho.boulder.ibm.com [9.17.130.234]) by b03cxnp08025.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 19S2JwMo40305028 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 28 Oct 2021 02:19:58 GMT Received: from b03ledav003.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3C3D66A04D; Thu, 28 Oct 2021 02:19:58 +0000 (GMT) Received: from b03ledav003.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B57846A051; Thu, 28 Oct 2021 02:19:57 +0000 (GMT) Received: from ltc.linux.ibm.com (unknown [9.10.229.42]) by b03ledav003.gho.boulder.ibm.com (Postfix) with ESMTP; Thu, 28 Oct 2021 02:19:57 +0000 (GMT) Content-Type: text/plain; charset=US-ASCII; format=flowed Date: Thu, 28 Oct 2021 10:19:57 +0800 From: guojiufu To: gcc-patches@gcc.gnu.org Cc: amker.cheng@gmail.com, rguenther@suse.de, wschmidt@linux.ibm.com, segher@kernel.crashing.org, dje.gcc@gmail.com, jlaw@tachyum.com Subject: Re: [RFC] Overflow check in simplifying exit cond comparing two IVs. In-Reply-To: <20211018133757.3960-1-guojiufu@linux.ibm.com> References: <20211018133757.3960-1-guojiufu@linux.ibm.com> Message-ID: <4ffe7dc6f3f9ced9eeb729448379a978@imap.linux.ibm.com> X-Sender: guojiufu@linux.ibm.com User-Agent: Roundcube Webmail/1.1.12 X-TM-AS-GCONF: 00 X-Proofpoint-GUID: VzD0L_fwPL8YhFiixr5X-Q0sDetkaCmR X-Proofpoint-ORIG-GUID: ZFMLK3iXX_cjvlpJYD_4Dd_YgnrXiNUy Content-Transfer-Encoding: 7bit X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.182.1,Aquarius:18.0.790,Hydra:6.0.425,FMLib:17.0.607.475 definitions=2021-10-27_07,2021-10-26_01,2020-04-07_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 impostorscore=0 suspectscore=0 malwarescore=0 phishscore=0 mlxscore=0 adultscore=0 clxscore=1015 bulkscore=0 mlxlogscore=999 lowpriorityscore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2110280009 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 28 Oct 2021 02:20:06 -0000 I just had a test on ppc64le, this patch pass bootstrap and regtest. Is this patch OK for trunk? Thanks for any comments. BR, Jiufu On 2021-10-18 21:37, Jiufu Guo wrote: > With reference the discussions in: > https://gcc.gnu.org/pipermail/gcc-patches/2021-July/574334.html > https://gcc.gnu.org/pipermail/gcc-patches/2021-June/572006.html > https://gcc.gnu.org/pipermail/gcc-patches/2021-September/578672.html > > Base on the patches in above discussion, we may draft a patch to fix > the > issue. > > In this patch, to make sure it is ok to change '{b0,s0} op {b1,s1}' to > '{b0,s0-s1} op {b1,0}', we also compute the condition which could > assume > both 2 ivs are not overflow/wrap: the niter "of '{b0,s0-s1} op {b1,0}'" > < the niter "of untill wrap for iv0 or iv1". > > Does this patch make sense? > > BR, > Jiufu Guo > > gcc/ChangeLog: > > PR tree-optimization/100740 > * tree-ssa-loop-niter.c (number_of_iterations_cond): Add > assume condition for combining of two IVs > > gcc/testsuite/ChangeLog: > > * gcc.c-torture/execute/pr100740.c: New test. > --- > gcc/tree-ssa-loop-niter.c | 103 +++++++++++++++--- > .../gcc.c-torture/execute/pr100740.c | 11 ++ > 2 files changed, 99 insertions(+), 15 deletions(-) > create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr100740.c > > diff --git a/gcc/tree-ssa-loop-niter.c b/gcc/tree-ssa-loop-niter.c > index 75109407124..f2987a4448d 100644 > --- a/gcc/tree-ssa-loop-niter.c > +++ b/gcc/tree-ssa-loop-niter.c > @@ -1863,29 +1863,102 @@ number_of_iterations_cond (class loop *loop, > > provided that either below condition is satisfied: > > - a) the test is NE_EXPR; > - b) iv0.step - iv1.step is integer and iv0/iv1 don't overflow. > + a) iv0.step - iv1.step is integer and iv0/iv1 don't overflow. > + b) assumptions in below table also need to be satisfied. > + > + | iv0 | iv1 | assum (iv0 + |---------+---------+---------------------+---------------------| > + | (b0,2) | (b1,1) | before iv1 overflow | before iv1 overflow | > + | (b0,2) | (b1,-1) | true | true | > + | (b0,-1) | (b1,-2) | before iv0 overflow | before iv0 overflow | > + | | | | | > + | (b0,1) | (b1,2) | false | before iv0 overflow | > + | (b0,-1) | (b1,2) | false | true | > + | (b0,-2) | (b1,-1) | false | before iv1 overflow | > + 'true' in above table means no need additional condition. > + 'false' means this case can not satify the transform. > + The first three rows: iv0->step > iv1->step; > + The second three rows: iv0->step < iv1->step. > > This rarely occurs in practice, but it is simple enough to > manage. */ > if (!integer_zerop (iv0->step) && !integer_zerop (iv1->step)) > { > + if (TREE_CODE (iv0->step) != INTEGER_CST > + || TREE_CODE (iv1->step) != INTEGER_CST) > + return false; > + if (!iv0->no_overflow || !iv1->no_overflow) > + return false; > + > tree step_type = POINTER_TYPE_P (type) ? sizetype : type; > - tree step = fold_binary_to_constant (MINUS_EXPR, step_type, > - iv0->step, iv1->step); > - > - /* No need to check sign of the new step since below code takes > care > - of this well. */ > - if (code != NE_EXPR > - && (TREE_CODE (step) != INTEGER_CST > - || !iv0->no_overflow || !iv1->no_overflow)) > + tree step > + = fold_binary_to_constant (MINUS_EXPR, step_type, iv0->step, > iv1->step); > + > + if (code != NE_EXPR && tree_int_cst_sign_bit (step)) > return false; > > - iv0->step = step; > - if (!POINTER_TYPE_P (type)) > - iv0->no_overflow = false; > + bool positive0 = !tree_int_cst_sign_bit (iv0->step); > + bool positive1 = !tree_int_cst_sign_bit (iv1->step); > > - iv1->step = build_int_cst (step_type, 0); > - iv1->no_overflow = true; > + /* Cases in rows 2 and 4 of above table. */ > + if ((positive0 && !positive1) || (!positive0 && positive1)) > + { > + iv0->step = step; > + iv1->step = build_int_cst (step_type, 0); > + return number_of_iterations_cond (loop, type, iv0, code, iv1, > + niter, only_exit, every_iteration); > + } > + > + affine_iv i_0, i_1; > + class tree_niter_desc num; > + i_0 = *iv0; > + i_1 = *iv1; > + i_0.step = step; > + i_1.step = build_int_cst (step_type, 0); > + if (!number_of_iterations_cond (loop, type, &i_0, code, &i_1, > &num, > + only_exit, every_iteration)) > + return false; > + > + affine_iv i0, i1; > + class tree_niter_desc num_wrap; > + i0 = *iv0; > + i1 = *iv1; > + > + /* Reset iv0 and iv1 to calculate the niter which cause > overflow. */ > + if (tree_int_cst_lt (i1.step, i0.step)) > + { > + if (positive0 && positive1) > + i0.step = build_int_cst (step_type, 0); > + else if (!positive0 && !positive1) > + i1.step = build_int_cst (step_type, 0); > + if (code == NE_EXPR) > + code = LT_EXPR; > + } > + else > + { > + if (positive0 && positive1) > + i1.step = build_int_cst (step_type, 0); > + else if (!positive0 && !positive1) > + i0.step = build_int_cst (step_type, 0); > + gcc_assert (code == NE_EXPR); > + code = GT_EXPR; > + } > + > + /* Calculate the niter which cause overflow. */ > + if (!number_of_iterations_cond (loop, type, &i0, code, &i1, > &num_wrap, > + only_exit, every_iteration)) > + return false; > + > + /* Make assumption there is no overflow. */ > + tree assum > + = fold_build2 (LE_EXPR, boolean_type_node, num.niter, > + fold_convert (TREE_TYPE (num.niter), num_wrap.niter)); > + num.assumptions = fold_build2 (TRUTH_AND_EXPR, > boolean_type_node, > + num.assumptions, assum); > + > + *iv0 = i_0; > + *iv1 = i_1; > + *niter = num; > + return true; > } > > /* If the result of the comparison is a constant, the loop is > weird. More > diff --git a/gcc/testsuite/gcc.c-torture/execute/pr100740.c > b/gcc/testsuite/gcc.c-torture/execute/pr100740.c > new file mode 100644 > index 00000000000..8fcdaffef3b > --- /dev/null > +++ b/gcc/testsuite/gcc.c-torture/execute/pr100740.c > @@ -0,0 +1,11 @@ > +/* PR tree-optimization/100740 */ > + > +unsigned a, b; > +int main() { > + unsigned c = 0; > + for (a = 0; a < 2; a++) > + for (b = 0; b < 2; b++) > + if (++c < a) > + __builtin_abort (); > + return 0; > +}