From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 76A033858D37; Wed, 30 Nov 2022 10:46:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 76A033858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1669805179; bh=SSixW6jFiD/gqRGuLFOCGi/pl8syvpIV/kVkSo6+1rg=; h=From:To:Subject:Date:From; b=q/DD/4HWrsRvPsyzdSlgFdNf1Dlru7k0iebdkE7iqufq4qvp47DHchE0AVeeIyLKZ pnkyqdz0ldB3AUJi802xOsuCyeAB3/UDwOYX95Bt40dupjE3as3ovJaDd+2pAn/SAw pEw0Iy39Ku0Su9H2T/mGHP/yxvsgiG757Y0y0170= MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-4405] tree-chrec: Fix up ICE on pointer multiplication [PR107835] X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/master X-Git-Oldrev: e0b95c2e8b771b53876321a6a0a9497619af73cd X-Git-Newrev: 7716ee1e90412f785cba20acffd59fc7461302cb Message-Id: <20221130104619.76A033858D37@sourceware.org> Date: Wed, 30 Nov 2022 10:46:19 +0000 (GMT) List-Id: https://gcc.gnu.org/g:7716ee1e90412f785cba20acffd59fc7461302cb commit r13-4405-g7716ee1e90412f785cba20acffd59fc7461302cb Author: Jakub Jelinek Date: Wed Nov 30 11:44:27 2022 +0100 tree-chrec: Fix up ICE on pointer multiplication [PR107835] r13-254-gdd3c7873a61019e9 added an optimization for {a, +, a} (x-1), but as can be seen on the following testcase, the way it is written where chrec_fold_multiply is called with type doesn't work for pointers:              res = build_int_cst (TREE_TYPE (x), 1);              res = chrec_fold_plus (TREE_TYPE (x), x, res);              res = chrec_convert_rhs (type, res, NULL);              res = chrec_fold_multiply (type, chrecr, res); while what we were doing before and what is still used if the condition doesn't match is fine:              res = chrec_convert_rhs (TREE_TYPE (chrecr), x, NULL);              res = chrec_fold_multiply (TREE_TYPE (chrecr), chrecr, res);              res = chrec_fold_plus (type, CHREC_LEFT (chrec), res); because it performs chrec_fold_multiply on TREE_TYPE (chrecr) and converts only afterwards. I think the easiest fix is to ignore the new path for pointer types. 2022-11-30 Jakub Jelinek PR tree-optimization/107835 * tree-chrec.cc (chrec_apply): Don't handle "{a, +, a} (x-1)" as "a*x" if type is a pointer type. * gcc.c-torture/compile/pr107835.c: New test. Diff: --- gcc/testsuite/gcc.c-torture/compile/pr107835.c | 11 +++++++++++ gcc/tree-chrec.cc | 3 ++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/gcc/testsuite/gcc.c-torture/compile/pr107835.c b/gcc/testsuite/gcc.c-torture/compile/pr107835.c new file mode 100644 index 00000000000..122beff2f11 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/compile/pr107835.c @@ -0,0 +1,11 @@ +/* PR tree-optimization/107835 */ + +int * +foo (void) +{ + int *x = 0; + unsigned n = n; + for (; n; --n, ++x) + ; + return x; +} diff --git a/gcc/tree-chrec.cc b/gcc/tree-chrec.cc index 7321fb9d282..dcf26cbae84 100644 --- a/gcc/tree-chrec.cc +++ b/gcc/tree-chrec.cc @@ -622,7 +622,8 @@ chrec_apply (unsigned var, /* "{a, +, b} (x)" -> "a + b*x". */ else if (operand_equal_p (CHREC_LEFT (chrec), chrecr) && TREE_CODE (x) == PLUS_EXPR - && integer_all_onesp (TREE_OPERAND (x, 1))) + && integer_all_onesp (TREE_OPERAND (x, 1)) + && !POINTER_TYPE_P (type)) { /* We know the number of iterations can't be negative. So {a, +, a} (x-1) -> "a*x". */