public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix associate_plusminus A + ~A optimization (PR tree-optimization/48717)
@ 2011-04-22 14:49 Jakub Jelinek
  2011-04-22 15:21 ` Richard Guenther
  0 siblings, 1 reply; 3+ messages in thread
From: Jakub Jelinek @ 2011-04-22 14:49 UTC (permalink / raw)
  To: gcc-patches

Hi!

On the following testcase associate_plusminus optimizes
A + ~A into INTEGER_CST { -1, -1 } with type unsigned short, which
confuses enough following passes on (int) cast of that into
assuming it is -1 instead of 65535.
Fixed by using build_int_cst_type, which is what e.g. fold-const.c
uses when optimizing X + ~X.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk
and 4.6 (where the bug is just latent)?  4.5 didn't have
associate_plusminus.

2011-04-22  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/48717
	* tree-ssa-forwprop.c (associate_plusminus): For A + ~A and
	~A + A optimizations use build_int_cst_type instead of build_int_cst.

	* gcc.c-torture/execute/pr48717.c: New test.

--- gcc/tree-ssa-forwprop.c.jj	2011-04-01 23:09:21.000000000 +0200
+++ gcc/tree-ssa-forwprop.c	2011-04-22 11:57:10.000000000 +0200
@@ -1815,7 +1815,7 @@ associate_plusminus (gimple stmt)
 		{
 		  /* ~A + A -> -1.  */
 		  code = INTEGER_CST;
-		  rhs1 = build_int_cst (TREE_TYPE (rhs2), -1);
+		  rhs1 = build_int_cst_type (TREE_TYPE (rhs2), -1);
 		  rhs2 = NULL_TREE;
 		  gimple_assign_set_rhs_with_ops (&gsi, code, rhs1, NULL_TREE);
 		  gcc_assert (gsi_stmt (gsi) == stmt);
@@ -1915,7 +1915,7 @@ associate_plusminus (gimple stmt)
 		{
 		  /* A + ~A -> -1.  */
 		  code = INTEGER_CST;
-		  rhs1 = build_int_cst (TREE_TYPE (rhs1), -1);
+		  rhs1 = build_int_cst_type (TREE_TYPE (rhs1), -1);
 		  rhs2 = NULL_TREE;
 		  gimple_assign_set_rhs_with_ops (&gsi, code, rhs1, NULL_TREE);
 		  gcc_assert (gsi_stmt (gsi) == stmt);
--- gcc/testsuite/gcc.c-torture/execute/pr48717.c.jj	2011-04-22 11:59:23.000000000 +0200
+++ gcc/testsuite/gcc.c-torture/execute/pr48717.c	2011-04-22 11:58:48.000000000 +0200
@@ -0,0 +1,26 @@
+/* PR tree-optimization/48717 */
+
+extern void abort (void);
+
+int v = 1, w;
+
+unsigned short
+foo (unsigned short x, unsigned short y)
+{
+  return x + y;
+}
+
+void
+bar (void)
+{
+  v = foo (~w, w);
+}
+
+int
+main ()
+{
+  bar ();
+  if (v != (unsigned short) -1)
+    abort ();
+  return 0;
+}

	Jakub

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

* Re: [PATCH] Fix associate_plusminus A + ~A optimization (PR tree-optimization/48717)
  2011-04-22 14:49 [PATCH] Fix associate_plusminus A + ~A optimization (PR tree-optimization/48717) Jakub Jelinek
@ 2011-04-22 15:21 ` Richard Guenther
  2011-04-22 19:26   ` Jakub Jelinek
  0 siblings, 1 reply; 3+ messages in thread
From: Richard Guenther @ 2011-04-22 15:21 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: gcc-patches

On Fri, Apr 22, 2011 at 3:41 PM, Jakub Jelinek <jakub@redhat.com> wrote:
> Hi!
>
> On the following testcase associate_plusminus optimizes
> A + ~A into INTEGER_CST { -1, -1 } with type unsigned short, which
> confuses enough following passes on (int) cast of that into
> assuming it is -1 instead of 65535.

Ick.

> Fixed by using build_int_cst_type, which is what e.g. fold-const.c
> uses when optimizing X + ~X.
>
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk
> and 4.6 (where the bug is just latent)?  4.5 didn't have
> associate_plusminus.

Ok for both.

build_int_cst looks seriously broken, I think we want to make it
an alias of build_int_cst_type, or rather call that from
build_int_cst.

Thanks,
Richard.

> 2011-04-22  Jakub Jelinek  <jakub@redhat.com>
>
>        PR tree-optimization/48717
>        * tree-ssa-forwprop.c (associate_plusminus): For A + ~A and
>        ~A + A optimizations use build_int_cst_type instead of build_int_cst.
>
>        * gcc.c-torture/execute/pr48717.c: New test.
>
> --- gcc/tree-ssa-forwprop.c.jj  2011-04-01 23:09:21.000000000 +0200
> +++ gcc/tree-ssa-forwprop.c     2011-04-22 11:57:10.000000000 +0200
> @@ -1815,7 +1815,7 @@ associate_plusminus (gimple stmt)
>                {
>                  /* ~A + A -> -1.  */
>                  code = INTEGER_CST;
> -                 rhs1 = build_int_cst (TREE_TYPE (rhs2), -1);
> +                 rhs1 = build_int_cst_type (TREE_TYPE (rhs2), -1);
>                  rhs2 = NULL_TREE;
>                  gimple_assign_set_rhs_with_ops (&gsi, code, rhs1, NULL_TREE);
>                  gcc_assert (gsi_stmt (gsi) == stmt);
> @@ -1915,7 +1915,7 @@ associate_plusminus (gimple stmt)
>                {
>                  /* A + ~A -> -1.  */
>                  code = INTEGER_CST;
> -                 rhs1 = build_int_cst (TREE_TYPE (rhs1), -1);
> +                 rhs1 = build_int_cst_type (TREE_TYPE (rhs1), -1);
>                  rhs2 = NULL_TREE;
>                  gimple_assign_set_rhs_with_ops (&gsi, code, rhs1, NULL_TREE);
>                  gcc_assert (gsi_stmt (gsi) == stmt);
> --- gcc/testsuite/gcc.c-torture/execute/pr48717.c.jj    2011-04-22 11:59:23.000000000 +0200
> +++ gcc/testsuite/gcc.c-torture/execute/pr48717.c       2011-04-22 11:58:48.000000000 +0200
> @@ -0,0 +1,26 @@
> +/* PR tree-optimization/48717 */
> +
> +extern void abort (void);
> +
> +int v = 1, w;
> +
> +unsigned short
> +foo (unsigned short x, unsigned short y)
> +{
> +  return x + y;
> +}
> +
> +void
> +bar (void)
> +{
> +  v = foo (~w, w);
> +}
> +
> +int
> +main ()
> +{
> +  bar ();
> +  if (v != (unsigned short) -1)
> +    abort ();
> +  return 0;
> +}
>
>        Jakub
>

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

* Re: [PATCH] Fix associate_plusminus A + ~A optimization (PR tree-optimization/48717)
  2011-04-22 15:21 ` Richard Guenther
@ 2011-04-22 19:26   ` Jakub Jelinek
  0 siblings, 0 replies; 3+ messages in thread
From: Jakub Jelinek @ 2011-04-22 19:26 UTC (permalink / raw)
  To: Richard Guenther; +Cc: gcc-patches

On Fri, Apr 22, 2011 at 04:50:26PM +0200, Richard Guenther wrote:
> build_int_cst looks seriously broken, I think we want to make it
> an alias of build_int_cst_type, or rather call that from
> build_int_cst.

The comment above build_int_cst_type talks about it:

"We cannot however make this a default behavior of build_int_cst without
more intrusive changes, since there are parts of gcc that rely on the
extra precision of the integer constants."

	Jakub

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

end of thread, other threads:[~2011-04-22 18:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-04-22 14:49 [PATCH] Fix associate_plusminus A + ~A optimization (PR tree-optimization/48717) Jakub Jelinek
2011-04-22 15:21 ` Richard Guenther
2011-04-22 19:26   ` Jakub Jelinek

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