aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2022-10-19 11:28:42 +0200
committerJakub Jelinek <jakub@redhat.com>2022-10-19 11:28:42 +0200
commit07cc4c1da1046f0ffda241d59df796417c122ff5 (patch)
treef7b7a3344104fcc6fee5defb8058dfb014ed351e /gcc
parentba281da28d34f9a78a07f6ee56ad2c754447966e (diff)
downloadgcc-07cc4c1da1046f0ffda241d59df796417c122ff5.zip
gcc-07cc4c1da1046f0ffda241d59df796417c122ff5.tar.gz
gcc-07cc4c1da1046f0ffda241d59df796417c122ff5.tar.bz2
match.pd: Add 2 TYPE_OVERFLOW_SANITIZED checks [PR106990]
As requested in the PR, this adds 2 TYPE_OVERFLOW_SANITIZED checks and corresponding testcase. 2022-10-19 Jakub Jelinek <jakub@redhat.com> PR tree-optimization/106990 * match.pd ((~X - ~Y) -> Y - X, -x & 1 -> x & 1): Guard with !TYPE_OVERFLOW_SANITIZED (type). * c-c++-common/ubsan/pr106990.c: New test.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/match.pd10
-rw-r--r--gcc/testsuite/c-c++-common/ubsan/pr106990.c29
2 files changed, 35 insertions, 4 deletions
diff --git a/gcc/match.pd b/gcc/match.pd
index fd64ad7..d07c0e4 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1331,8 +1331,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
/* (~X - ~Y) -> Y - X. */
(simplify
(minus (bit_not @0) (bit_not @1))
- (with { tree utype = unsigned_type_for (type); }
- (convert (minus (convert:utype @1) (convert:utype @0)))))
+ (if (!TYPE_OVERFLOW_SANITIZED (type))
+ (with { tree utype = unsigned_type_for (type); }
+ (convert (minus (convert:utype @1) (convert:utype @0))))))
/* ~(X - Y) -> ~X + Y. */
(simplify
@@ -8176,5 +8177,6 @@ and,
/* -x & 1 -> x & 1. */
(simplify
- (bit_and (negate @0) integer_onep@1)
- (bit_and @0 @1))
+ (bit_and (negate @0) integer_onep@1)
+ (if (!TYPE_OVERFLOW_SANITIZED (type))
+ (bit_and @0 @1)))
diff --git a/gcc/testsuite/c-c++-common/ubsan/pr106990.c b/gcc/testsuite/c-c++-common/ubsan/pr106990.c
new file mode 100644
index 0000000..5bd46af
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/ubsan/pr106990.c
@@ -0,0 +1,29 @@
+/* PR tree-optimization/106990 */
+/* { dg-do run { target int32 } } */
+/* { dg-options "-fsanitize=signed-integer-overflow" } */
+
+__attribute__((noipa)) int
+foo (void)
+{
+ int x = -1956816001;
+ int y = 1999200512;
+ return ~x - ~y;
+}
+
+__attribute__((noipa)) int
+bar (void)
+{
+ int x = -__INT_MAX__ - 1;
+ return -x & 1;
+}
+
+int
+main ()
+{
+ foo ();
+ bar ();
+ return 0;
+}
+
+/* { dg-output "signed integer overflow: 1956816000 - -1999200513 cannot be represented in type 'int'\[^\n\r]*(\n|\r\n|\r)" } */
+/* { dg-output "\[^\n\r]*negation of -2147483648 cannot be represented in type 'int'; cast to an unsigned type to negate this value to itself" } */