tree-optimization/112706 - missed simplification of condition

We lack a match.pd pattern recognizing ptr + o ==/!= ptr + o'.
The following extends handling we have for integral types to
pointers.

	PR tree-optimization/112706
	* match.pd (ptr + o ==/!=/- ptr + o'): New patterns.

	* gcc.dg/tree-ssa/pr112706.c: New testcase.
This commit is contained in:
Richard Biener 2023-11-27 09:45:04 +01:00
parent 8e4db85eca
commit d9abaa8d58
2 changed files with 24 additions and 0 deletions

View file

@ -2596,6 +2596,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
&& (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
|| TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
(op @0 @1))))
/* And similar for pointers. */
(for op (eq ne)
(simplify
(op (pointer_plus @0 @1) (pointer_plus @0 @2))
(op @1 @2)))
(simplify
(pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
(if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
(convert (minus @1 @2))))
/* X - Z < Y - Z is the same as X < Y when there is no overflow. */
(for op (lt le ge gt)

View file

@ -0,0 +1,15 @@
/* { dg-do compile } */
/* { dg-options "-O -fdump-tree-fre1" } */
int *ptr;
void link_error ();
void
test ()
{
int *ptr1 = ptr + 10;
int *ptr2 = ptr + 20;
if (ptr1 == ptr2)
link_error ();
}
/* { dg-final { scan-tree-dump-not "if" "fre1" } } */