Hi!

EQ/NE comparisons don't really assume that overflow doesn't happen unlike
>/>=/etc., so it is strange that we warn about it.
As the warning happens on simple strcmp uses when using glibc string.h,
it is extra annoying.

Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for
trunk?

2011-03-07  Jakub Jelinek  <ja...@redhat.com>

        PR tree-optimization/48022
        * fold-const.c (fold_comparison): Don't call fold_overflow_warning
        for EQ/NE_EXPR.

        * gcc.dg/pr48022-1.c: New test.
        * gcc.dg/pr48022-2.c: New test.

--- gcc/fold-const.c.jj 2011-03-03 09:11:43.000000000 +0100
+++ gcc/fold-const.c    2011-03-07 17:44:02.000000000 +0100
@@ -8572,10 +8572,11 @@ fold_comparison (location_t loc, enum tr
          && (TREE_CODE (lhs) != INTEGER_CST
              || !TREE_OVERFLOW (lhs)))
        {
-         fold_overflow_warning ("assuming signed overflow does not occur "
-                                 "when changing X +- C1 cmp C2 to "
-                                 "X cmp C1 +- C2",
-                                WARN_STRICT_OVERFLOW_COMPARISON);
+         if (code != EQ_EXPR && code != NE_EXPR)
+           fold_overflow_warning ("assuming signed overflow does not occur "
+                                  "when changing X +- C1 cmp C2 to "
+                                  "X cmp C1 +- C2",
+                                  WARN_STRICT_OVERFLOW_COMPARISON);
          return fold_build2_loc (loc, code, type, variable, lhs);
        }
     }
--- gcc/testsuite/gcc.dg/pr48022-1.c.jj 2011-03-07 17:46:55.000000000 +0100
+++ gcc/testsuite/gcc.dg/pr48022-1.c    2011-03-07 17:47:18.000000000 +0100
@@ -0,0 +1,14 @@
+/* PR tree-optimization/48022 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wstrict-overflow" } */
+
+int
+foo (const char *x)
+{
+  unsigned long l = 1;
+  const unsigned char *s = (const unsigned char *) (const char *) (x);
+  int r = s[0] - ((const unsigned char *) (const char *) ("/"))[0];
+  if (l > 0 && r == 0)
+    r = (s[1] - ((const unsigned char *) (const char *) ("/"))[1]);
+  return r;
+}
--- gcc/testsuite/gcc.dg/pr48022-2.c.jj 2011-03-07 17:47:26.000000000 +0100
+++ gcc/testsuite/gcc.dg/pr48022-2.c    2011-03-07 17:47:45.000000000 +0100
@@ -0,0 +1,11 @@
+/* PR tree-optimization/48022 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -Wstrict-overflow" } */
+
+#include <string.h>
+
+int
+foo (const char *x)
+{
+  return strcmp (x, "/");
+}

        Jakub

Reply via email to