Skip to content

Commit d9abaa8

Browse files
committed
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.
1 parent 8e4db85 commit d9abaa8

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

gcc/match.pd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2596,6 +2596,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
25962596
&& (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))
25972597
|| TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0))))
25982598
(op @0 @1))))
2599+
/* And similar for pointers. */
2600+
(for op (eq ne)
2601+
(simplify
2602+
(op (pointer_plus @0 @1) (pointer_plus @0 @2))
2603+
(op @1 @2)))
2604+
(simplify
2605+
(pointer_diff (pointer_plus @0 @1) (pointer_plus @0 @2))
2606+
(if (TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
2607+
(convert (minus @1 @2))))
25992608

26002609
/* X - Z < Y - Z is the same as X < Y when there is no overflow. */
26012610
(for op (lt le ge gt)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* { dg-do compile } */
2+
/* { dg-options "-O -fdump-tree-fre1" } */
3+
4+
int *ptr;
5+
void link_error ();
6+
void
7+
test ()
8+
{
9+
int *ptr1 = ptr + 10;
10+
int *ptr2 = ptr + 20;
11+
if (ptr1 == ptr2)
12+
link_error ();
13+
}
14+
15+
/* { dg-final { scan-tree-dump-not "if" "fre1" } } */

0 commit comments

Comments
 (0)