public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] match.pd: Add 2 TYPE_OVERFLOW_SANITIZED checks [PR106990]
@ 2022-10-19  8:02 Jakub Jelinek
  2022-10-19  8:20 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Jakub Jelinek @ 2022-10-19  8:02 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches

Hi!

As requested in the PR, this adds 2 TYPE_OVERFLOW_SANITIZED checks
and corresponding testcase.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

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.

--- gcc/match.pd.jj	2022-10-13 15:51:08.928071208 +0200
+++ gcc/match.pd	2022-10-18 16:01:12.145705958 +0200
@@ -1331,8 +1331,9 @@ (define_operator_list SYNC_FETCH_AND_AND
 /* (~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)))
--- gcc/testsuite/c-c++-common/ubsan/pr106990.c.jj	2022-10-18 15:56:20.260656260 +0200
+++ gcc/testsuite/c-c++-common/ubsan/pr106990.c	2022-10-18 16:17:37.295403933 +0200
@@ -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" } */

	Jakub


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] match.pd: Add 2 TYPE_OVERFLOW_SANITIZED checks [PR106990]
  2022-10-19  8:02 [PATCH] match.pd: Add 2 TYPE_OVERFLOW_SANITIZED checks [PR106990] Jakub Jelinek
@ 2022-10-19  8:20 ` Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2022-10-19  8:20 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches



> Am 19.10.2022 um 10:02 schrieb Jakub Jelinek <jakub@redhat.com>:
> 
> Hi!
> 
> As requested in the PR, this adds 2 TYPE_OVERFLOW_SANITIZED checks
> and corresponding testcase.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

Ok.

Thanks,
Richard 

> 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.
> 
> --- gcc/match.pd.jj    2022-10-13 15:51:08.928071208 +0200
> +++ gcc/match.pd    2022-10-18 16:01:12.145705958 +0200
> @@ -1331,8 +1331,9 @@ (define_operator_list SYNC_FETCH_AND_AND
> /* (~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)))
> --- gcc/testsuite/c-c++-common/ubsan/pr106990.c.jj    2022-10-18 15:56:20.260656260 +0200
> +++ gcc/testsuite/c-c++-common/ubsan/pr106990.c    2022-10-18 16:17:37.295403933 +0200
> @@ -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" } */
> 
>    Jakub
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-10-19  8:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-19  8:02 [PATCH] match.pd: Add 2 TYPE_OVERFLOW_SANITIZED checks [PR106990] Jakub Jelinek
2022-10-19  8:20 ` Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).