fold-const.c (fold_binary_loc): Don't fold if the result is undefined.
authorMarek Polacek <polacek@redhat.com>
Wed, 12 Nov 2014 14:36:41 +0000 (14:36 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Wed, 12 Nov 2014 14:36:41 +0000 (14:36 +0000)
* fold-const.c (fold_binary_loc): Don't fold if the result
is undefined.
* match.pd (A + (-B) -> A - B, A - (-B) -> A + B,
-(-A) -> A): Likewise.

* c-c++-common/ubsan/overflow-sub-4.c: New test.
* c-c++-common/ubsan/overflow-sub-2.c: Adjust dg-output.
* c-c++-common/ubsan/overflow-int128.c: Likewise.

From-SVN: r217427

gcc/ChangeLog
gcc/fold-const.c
gcc/match.pd
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/ubsan/overflow-int128.c
gcc/testsuite/c-c++-common/ubsan/overflow-sub-2.c
gcc/testsuite/c-c++-common/ubsan/overflow-sub-4.c [new file with mode: 0644]

index 92e704c..428725b 100644 (file)
@@ -1,3 +1,10 @@
+2014-11-12  Marek Polacek  <polacek@redhat.com>
+
+       * fold-const.c (fold_binary_loc): Don't fold if the result
+       is undefined.
+       * match.pd (A + (-B) -> A - B, A - (-B) -> A + B,
+       -(-A) -> A): Likewise.
+
 2014-11-12  Richard Biener  <rguenther@suse.de>
 
        Merge from match-and-simplify branch
index 82e2414..4321b1e 100644 (file)
@@ -10538,6 +10538,9 @@ fold_binary_loc (location_t loc,
 
       /* A - B -> A + (-B) if B is easily negatable.  */
       if (negate_expr_p (arg1)
+         && (!INTEGRAL_TYPE_P (type)
+             || TYPE_OVERFLOW_WRAPS (type)
+             || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
          && ((FLOAT_TYPE_P (type)
                /* Avoid this transformation if B is a positive REAL_CST.  */
               && (TREE_CODE (arg1) != REAL_CST
index 288be4c..d94a8f5 100644 (file)
@@ -285,19 +285,25 @@ along with GCC; see the file COPYING3.  If not see
   /* Apply STRIP_NOPS on @0 and the negate.  */
   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
        && tree_nop_conversion_p (type, TREE_TYPE (@1))
-       && (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0)
+       && (!INTEGRAL_TYPE_P (type)
+          || TYPE_OVERFLOW_WRAPS (type)
+          || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0))
    (minus (convert @0) (convert @1))))
  /* A - (-B) -> A + B */
  (simplify
   (minus (convert1? @0) (convert2? (negate @1)))
   (if (tree_nop_conversion_p (type, TREE_TYPE (@0))
-       && tree_nop_conversion_p (type, TREE_TYPE (@1)))
+       && tree_nop_conversion_p (type, TREE_TYPE (@1))
+       && (!INTEGRAL_TYPE_P (type)
+          || TYPE_OVERFLOW_WRAPS (type)
+          || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0))
    (plus (convert @0) (convert @1))))
  /* -(-A) -> A */
  (simplify
   (negate (convert? (negate @1)))
   (if (tree_nop_conversion_p (type, TREE_TYPE (@1))
-       && (TYPE_OVERFLOW_WRAPS (type)
+       && (!INTEGRAL_TYPE_P (type)
+          || TYPE_OVERFLOW_WRAPS (type)
           || (flag_sanitize & SANITIZE_SI_OVERFLOW) == 0))
    (convert @1)))
 
index 058e32f..cf1107c 100644 (file)
@@ -1,3 +1,9 @@
+2014-11-12  Marek Polacek  <polacek@redhat.com>
+
+       * c-c++-common/ubsan/overflow-sub-4.c: New test.
+       * c-c++-common/ubsan/overflow-sub-2.c: Adjust dg-output.
+       * c-c++-common/ubsan/overflow-int128.c: Likewise.
+
 2014-11-12  Evgeny Stupachenko  <evstupac@gmail.com>
 
        * gcc.target/i386/pr52252-atom-1.c: Extend assembler scan on AVX2 case.
index 125d6bf..4384d7c 100644 (file)
@@ -41,7 +41,7 @@ main (void)
 /* { dg-output "\[^\n\r]*signed integer overflow: 0x7fffffffffffffffffffffffffffff9b \\+ 1024 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
 /* { dg-output "\[^\n\r]*signed integer overflow: -1 \\+ 0x80000000000000000000000000000000 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
 /* { dg-output "\[^\n\r]*signed integer overflow: 0x80000000000000000000000000000000 \\+ -1 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
-/* { dg-output "\[^\n\r]*signed integer overflow: 0x80000000000000000000000000000000 \\+ -1 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: 0x80000000000000000000000000000000 1 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
 /* { dg-output "\[^\n\r]*signed integer overflow: 0x80000000000000000000000000000064 \\+ -1024 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
 /* { dg-output "\[^\n\r]*signed integer overflow: 0x7fffffffffffffffffffffffffffffff \\* 2 cannot be represented in type '__int128'(\n|\r\n|\r)" } */
 /* { dg-output "\[^\n\r]*negation of 0x80000000000000000000000000000000 cannot be represented in type '__int128'; cast to an unsigned type to negate this value to itself\[^\n\r]*(\n|\r\n|\r)" } */
index daf6a54..88c4762 100644 (file)
@@ -43,12 +43,12 @@ main (void)
 }
 
 /* { dg-output "signed integer overflow: -2147483648 - 1 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
-/* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 \\+ -1 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
-/* { dg-output "\[^\n\r]*signed integer overflow: -2147483548 \\+ -1024 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
-/* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 \\+ -1 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
-/* { dg-output "\[^\n\r]*signed integer overflow: -2147482648 \\+ -1048576 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 1 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: -2147483548 1024 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: -2147483648 1 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: -2147482648 1048576 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
 /* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* - 1 cannot be represented in type 'long int'\[^\n\r]*(\n|\r\n|\r)" } */
-/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* \\+ -1 cannot be represented in type 'long int'\[^\n\r]*(\n|\r\n|\r)" } */
-/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* \\+ -1024 cannot be represented in type 'long int'\[^\n\r]*(\n|\r\n|\r)" } */
-/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* \\+ -1 cannot be represented in type 'long int'\[^\n\r]*(\n|\r\n|\r)" } */
-/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* \\+ -1048576 cannot be represented in type 'long int'\[^\n\r]*" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* 1 cannot be represented in type 'long int'\[^\n\r]*(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* 1024 cannot be represented in type 'long int'\[^\n\r]*(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* 1 cannot be represented in type 'long int'\[^\n\r]*(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: -\[^\n\r]* 1048576 cannot be represented in type 'long int'\[^\n\r]*" } */
diff --git a/gcc/testsuite/c-c++-common/ubsan/overflow-sub-4.c b/gcc/testsuite/c-c++-common/ubsan/overflow-sub-4.c
new file mode 100644 (file)
index 0000000..519b7ba
--- /dev/null
@@ -0,0 +1,19 @@
+/* { dg-do run } */
+/* { dg-options "-fsanitize=signed-integer-overflow" } */
+
+#define INT_MIN (-__INT_MAX__ - 1)
+
+int
+main ()
+{
+  int x = INT_MIN;
+  int y = 0;
+  int z;
+  asm ("" : "+g" (y));
+  asm ("" : "+g" (x));
+  z = y - (-x);
+  asm ("" : "+g" (z));
+}
+
+/* { dg-output "negation of -2147483648 cannot be represented in type 'int'\[^\n\r]*; cast to an unsigned type to negate this value to itself\[^\n\r]*(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*signed integer overflow: 0 - -2147483648 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */