public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Remove introduction of undefined overflow in emit_case_bit_test.
@ 2012-08-05 23:32 Tom de Vries
  2012-08-06  8:17 ` Richard Guenther
  0 siblings, 1 reply; 2+ messages in thread
From: Tom de Vries @ 2012-08-05 23:32 UTC (permalink / raw)
  To: Richard Guenther; +Cc: gcc-patches, Steven Bosscher

[-- Attachment #1: Type: text/plain, Size: 603 bytes --]

Richard,

the code in emit_case_bit_tests currently introduces a MINUS_EXPR in signed type
(without checking if signed types wrap or not), which could mean introduction of
undefined overflow.

This patch fixes this problem by performing the MINUS_EXPR in an unsigned type.

Doing so also enables the vrp optimization for the test-case.

Bootstrapped and reg-tested (ada inclusive) on x86_64.

OK for trunk?

Thanks,
- Tom


2012-08-06  Tom de Vries  <tom@codesourcery.com>

	* tree-switch-conversion.c (emit_case_bit_tests): Generate MINUS_EXPR in
	unsigned type.

	* gcc.dg/tree-ssa/vrp78.c: New test.

[-- Attachment #2: tree-switch-conversion-fix-undefined-overflow-introduction.patch --]
[-- Type: text/x-patch, Size: 1525 bytes --]

Index: gcc/tree-switch-conversion.c
===================================================================
--- gcc/tree-switch-conversion.c (revision 190139)
+++ gcc/tree-switch-conversion.c (working copy)
@@ -384,10 +384,10 @@ emit_case_bit_tests (gimple swtch, tree
 
   gsi = gsi_last_bb (switch_bb);
 
-  /* idx = (unsigned) (x - minval) */
-  idx = fold_build2 (MINUS_EXPR, index_type, index_expr,
-		     fold_convert (index_type, minval));
-  idx = fold_convert (unsigned_index_type, idx);
+  /* idx = (unsigned)x - minval */
+  idx = fold_convert (unsigned_index_type, index_expr);
+  idx = fold_build2 (MINUS_EXPR, unsigned_index_type, idx,
+		     fold_convert (unsigned_index_type, minval));
   idx = force_gimple_operand_gsi (&gsi, idx,
 				  /*simple=*/true, NULL_TREE,
 				  /*before=*/true, GSI_SAME_STMT);
Index: gcc/testsuite/gcc.dg/tree-ssa/vrp78.c
===================================================================
--- /dev/null (new file)
+++ gcc/testsuite/gcc.dg/tree-ssa/vrp78.c (revision 0)
@@ -0,0 +1,34 @@
+/* { dg-do link } */
+/* { dg-options "-O2" } */
+
+/* Based on f3 from vrp63.c, but with switch instead of if-chain.  */
+
+extern void link_error (void);
+
+void
+f3 (int s)
+{
+  if (s >> 3 == -2)
+    /* s in range [ -16, -9].  */
+    ;
+  else
+    {
+      /* s in range ~[-16, -9], so none of the case labels can be taken.  */
+      switch (s)
+	{
+	case -16:
+	case -12:
+	case -9:
+	  link_error ();
+	  break;
+	default:
+	  break;
+	}
+    }
+}
+
+int
+main ()
+{
+  return 0;
+}

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

* Re: [PATCH] Remove introduction of undefined overflow in emit_case_bit_test.
  2012-08-05 23:32 [PATCH] Remove introduction of undefined overflow in emit_case_bit_test Tom de Vries
@ 2012-08-06  8:17 ` Richard Guenther
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Guenther @ 2012-08-06  8:17 UTC (permalink / raw)
  To: Tom de Vries; +Cc: gcc-patches, Steven Bosscher

On Mon, Aug 6, 2012 at 1:31 AM, Tom de Vries <Tom_deVries@mentor.com> wrote:
> Richard,
>
> the code in emit_case_bit_tests currently introduces a MINUS_EXPR in signed type
> (without checking if signed types wrap or not), which could mean introduction of
> undefined overflow.
>
> This patch fixes this problem by performing the MINUS_EXPR in an unsigned type.
>
> Doing so also enables the vrp optimization for the test-case.
>
> Bootstrapped and reg-tested (ada inclusive) on x86_64.
>
> OK for trunk?

Ok.

Thanks,
Richard.

> Thanks,
> - Tom
>
>
> 2012-08-06  Tom de Vries  <tom@codesourcery.com>
>
>         * tree-switch-conversion.c (emit_case_bit_tests): Generate MINUS_EXPR in
>         unsigned type.
>
>         * gcc.dg/tree-ssa/vrp78.c: New test.

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

end of thread, other threads:[~2012-08-06  8:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-05 23:32 [PATCH] Remove introduction of undefined overflow in emit_case_bit_test Tom de Vries
2012-08-06  8:17 ` Richard Guenther

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).