re PR middle-end/81088 (UBSAN: false positive as a result of reassosiation)
authorRichard Biener <rguenther@suse.de>
Wed, 14 Jun 2017 11:40:20 +0000 (11:40 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Wed, 14 Jun 2017 11:40:20 +0000 (11:40 +0000)
2017-06-14  Richard Biener  <rguenther@suse.de>

PR middle-end/81088
* fold-const.c (split_tree): Drop TREE_OVERFLOW flag from
literal constants.
(fold_binary_loc): When associating do not treat pre-existing
TREE_OVERFLOW on literal constants as a reason to allow
TREE_OVERFLOW on associated literal constants.

* c-c++-common/ubsan/pr81088.c: New testcase.

From-SVN: r249192

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/c-c++-common/ubsan/pr81088.c [new file with mode: 0644]

index 11e3533d4f9b9470a0d9bcf426a9e1cb491be6ca..09a1b983584b18c6aa9606648d36ba5ea673dc8e 100644 (file)
@@ -1,3 +1,12 @@
+2017-06-14  Richard Biener  <rguenther@suse.de>
+
+       PR middle-end/81088
+       * fold-const.c (split_tree): Drop TREE_OVERFLOW flag from
+       literal constants.
+       (fold_binary_loc): When associating do not treat pre-existing
+       TREE_OVERFLOW on literal constants as a reason to allow
+       TREE_OVERFLOW on associated literal constants.
+
 2017-06-14  Eric Botcazou  <ebotcazou@adacore.com>
 
        * config/sparc/sparc.h (MASK_ISA): Add MASK_LEON and MASK_LEON3.
index 74bbdb07ffb4fdaee2c90fd7a13b814245576828..8559b1d073188844704fda8a917a90d3749d856e 100644 (file)
@@ -880,6 +880,13 @@ split_tree (location_t loc, tree in, tree type, enum tree_code code,
        }
     }
 
+  if (*litp
+      && TREE_OVERFLOW_P (*litp))
+    *litp = drop_tree_overflow (*litp);
+  if (*minus_litp
+      && TREE_OVERFLOW_P (*minus_litp))
+    *minus_litp = drop_tree_overflow (*minus_litp);
+
   return var;
 }
 
@@ -9703,11 +9710,6 @@ fold_binary_loc (location_t loc,
                       + (lit0 != 0) + (lit1 != 0)
                       + (minus_lit0 != 0) + (minus_lit1 != 0))))
            {
-             bool any_overflows = false;
-             if (lit0) any_overflows |= TREE_OVERFLOW (lit0);
-             if (lit1) any_overflows |= TREE_OVERFLOW (lit1);
-             if (minus_lit0) any_overflows |= TREE_OVERFLOW (minus_lit0);
-             if (minus_lit1) any_overflows |= TREE_OVERFLOW (minus_lit1);
              var0 = associate_trees (loc, var0, var1, code, atype);
              con0 = associate_trees (loc, con0, con1, code, atype);
              lit0 = associate_trees (loc, lit0, lit1, code, atype);
@@ -9738,9 +9740,8 @@ fold_binary_loc (location_t loc,
                }
 
              /* Don't introduce overflows through reassociation.  */
-             if (!any_overflows
-                 && ((lit0 && TREE_OVERFLOW_P (lit0))
-                     || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0))))
+             if ((lit0 && TREE_OVERFLOW_P (lit0))
+                 || (minus_lit0 && TREE_OVERFLOW_P (minus_lit0)))
                return NULL_TREE;
 
              if (minus_lit0)
index d6fdd845dd2d500e30239c0c7936190e00f17e89..bd241de477962c1bb029715f0872db35e4ac1c7e 100644 (file)
@@ -1,3 +1,8 @@
+2017-06-14  Richard Biener  <rguenther@suse.de>
+
+       PR middle-end/81088
+       * c-c++-common/ubsan/pr81088.c: New testcase.
+
 2017-06-14  Eric Botcazou  <ebotcazou@adacore.com>
 
        * gcc.target/sparc/overflow-4.c: Add -mno-vis3.
diff --git a/gcc/testsuite/c-c++-common/ubsan/pr81088.c b/gcc/testsuite/c-c++-common/ubsan/pr81088.c
new file mode 100644 (file)
index 0000000..6753d77
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do run } */
+/* { dg-options "-fsanitize=undefined -fsanitize-undefined-trap-on-error" } */
+
+short s = 2;
+short y = 1;
+int i;
+int main()
+{
+  i = -(s + (int)(~(unsigned)(0 / y))) + 0x7fffffff;
+  return 0;
+}