public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] tree-chrec: Fix up ICE on pointer multiplication [PR107835]
@ 2022-11-30  9:47 Jakub Jelinek
  2022-11-30 10:10 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2022-11-30  9:47 UTC (permalink / raw)
  To: Richard Biener, Roger Sayle; +Cc: gcc-patches

Hi!

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.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2022-11-30  Jakub Jelinek  <jakub@redhat.com>

	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.

--- gcc/tree-chrec.cc.jj	2022-05-10 18:33:14.641029951 +0200
+++ gcc/tree-chrec.cc	2022-11-29 15:24:41.810400368 +0100
@@ -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".  */
--- gcc/testsuite/gcc.c-torture/compile/pr107835.c.jj	2022-11-29 15:31:32.565382590 +0100
+++ gcc/testsuite/gcc.c-torture/compile/pr107835.c	2022-11-29 15:31:15.795628304 +0100
@@ -0,0 +1,11 @@
+/* PR tree-optimization/107835 */
+
+int *
+foo (void)
+{
+  int *x = 0;
+  unsigned n = n;
+  for (; n; --n, ++x)
+    ;
+  return x;
+}

	Jakub


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] tree-chrec: Fix up ICE on pointer multiplication [PR107835]
  2022-11-30  9:47 [PATCH] tree-chrec: Fix up ICE on pointer multiplication [PR107835] Jakub Jelinek
@ 2022-11-30 10:10 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2022-11-30 10:10 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: Roger Sayle, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 2486 bytes --]

On Wed, 30 Nov 2022, Jakub Jelinek wrote:

> Hi!
> 
> 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.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

Thanks,
Richard.

> 2022-11-30  Jakub Jelinek  <jakub@redhat.com>
> 
> 	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.
> 
> --- gcc/tree-chrec.cc.jj	2022-05-10 18:33:14.641029951 +0200
> +++ gcc/tree-chrec.cc	2022-11-29 15:24:41.810400368 +0100
> @@ -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".  */
> --- gcc/testsuite/gcc.c-torture/compile/pr107835.c.jj	2022-11-29 15:31:32.565382590 +0100
> +++ gcc/testsuite/gcc.c-torture/compile/pr107835.c	2022-11-29 15:31:15.795628304 +0100
> @@ -0,0 +1,11 @@
> +/* PR tree-optimization/107835 */
> +
> +int *
> +foo (void)
> +{
> +  int *x = 0;
> +  unsigned n = n;
> +  for (; n; --n, ++x)
> +    ;
> +  return x;
> +}
> 
> 	Jakub
> 
> 

-- 
Richard Biener <rguenther@suse.de>
SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg,
Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman;
HRB 36809 (AG Nuernberg)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-11-30 10:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-30  9:47 [PATCH] tree-chrec: Fix up ICE on pointer multiplication [PR107835] Jakub Jelinek
2022-11-30 10:10 ` Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).