From 8ae438811f695dd025fb1fef254bff920777b494 Mon Sep 17 00:00:00 2001 From: Jakub Jelinek Date: Thu, 18 Jan 2018 09:29:14 +0100 Subject: [PATCH] re PR c/61240 (Incorrect warning "integer overflow in expression" on pointer-pointer subtraction) PR c/61240 * match.pd ((P + A) - P, P - (P + A), (P + A) - (P + B)): For pointer_diff optimizations use view_convert instead of convert. * gcc.dg/pr61240.c: New test. From-SVN: r256838 --- gcc/ChangeLog | 6 ++++++ gcc/match.pd | 16 +++++++++++++--- gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/pr61240.c | 20 ++++++++++++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr61240.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 5e4d6b3..577d19b 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2018-01-18 Jakub Jelinek + + PR c/61240 + * match.pd ((P + A) - P, P - (P + A), (P + A) - (P + B)): For + pointer_diff optimizations use view_convert instead of convert. + 2018-01-17 Bill Schmidt * config/rs6000/rs6000.md (*call_indirect_nonlocal_sysv): diff --git a/gcc/match.pd b/gcc/match.pd index 3f6e009..b288a36 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -1832,7 +1832,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* The second argument of pointer_plus must be interpreted as signed, and thus sign-extended if necessary. */ (with { tree stype = signed_type_for (TREE_TYPE (@1)); } - (convert (convert:stype @1)))) + /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR + second arg is unsigned even when we need to consider it as signed, + we don't want to diagnose overflow here. */ + (convert (view_convert:stype @1)))) /* (T)P - (T)(P + A) -> -(T) A */ (simplify @@ -1876,7 +1879,10 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* The second argument of pointer_plus must be interpreted as signed, and thus sign-extended if necessary. */ (with { tree stype = signed_type_for (TREE_TYPE (@1)); } - (negate (convert (convert:stype @1))))) + /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR + second arg is unsigned even when we need to consider it as signed, + we don't want to diagnose overflow here. */ + (negate (convert (view_convert:stype @1))))) /* (T)(P + A) - (T)(P + B) -> (T)A - (T)B */ (simplify @@ -1927,7 +1933,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) /* The second argument of pointer_plus must be interpreted as signed, and thus sign-extended if necessary. */ (with { tree stype = signed_type_for (TREE_TYPE (@1)); } - (minus (convert (convert:stype @1)) (convert (convert:stype @2))))))) + /* Use view_convert instead of convert here, as POINTER_PLUS_EXPR + second arg is unsigned even when we need to consider it as signed, + we don't want to diagnose overflow here. */ + (minus (convert (view_convert:stype @1)) + (convert (view_convert:stype @2))))))) /* Simplifications of MIN_EXPR, MAX_EXPR, fmin() and fmax(). */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index e166d03..014a15d 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-01-18 Jakub Jelinek + + PR c/61240 + * gcc.dg/pr61240.c: New test. + 2018-01-17 Harald Anlauf PR fortran/83864 diff --git a/gcc/testsuite/gcc.dg/pr61240.c b/gcc/testsuite/gcc.dg/pr61240.c new file mode 100644 index 0000000..6332918 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr61240.c @@ -0,0 +1,20 @@ +/* PR c/61240 */ +/* { dg-do compile } */ + +typedef __PTRDIFF_TYPE__ ptrdiff_t; + +ptrdiff_t +foo (ptrdiff_t a[4]) +{ + int i[4]; + int *p = i + 2; + static ptrdiff_t b = p - (p - 1); /* { dg-bogus "integer overflow in expression" } */ + static ptrdiff_t c = (p - 1) - p; /* { dg-bogus "integer overflow in expression" } */ + static ptrdiff_t d = (p - 2) - (p - 1);/* { dg-bogus "integer overflow in expression" } */ + static ptrdiff_t e = (p - 1) - (p - 2);/* { dg-bogus "integer overflow in expression" } */ + a[0] = p - (p - 1); /* { dg-bogus "integer overflow in expression" } */ + a[1] = (p - 1) - p; /* { dg-bogus "integer overflow in expression" } */ + a[2] = (p - 2) - (p - 1); /* { dg-bogus "integer overflow in expression" } */ + a[3] = (p - 1) - (p - 2); /* { dg-bogus "integer overflow in expression" } */ + return b + c + d + e; +} -- 2.7.4