public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ PATCH for c++/53356 (C++11 ICE with new)
@ 2012-05-30 14:45 Jason Merrill
  2012-05-30 21:59 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Jason Merrill @ 2012-05-30 14:45 UTC (permalink / raw)
  To: gcc-patches List

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

The code in build_new_1 already knows how to handle an initializer that 
it was unable to stabilize, but the logic was backwards in a critical 
place.  I'm surprised this typo hasn't been hit before since it was 
introduced in 2006...

Tested x86_64-pc-linux-gnu, applying to trunk and 4.7.

[-- Attachment #2: 53356.patch --]
[-- Type: text/x-patch, Size: 1177 bytes --]

commit b2e577b5a53a4c49bb3eea682e2c1dee86c27316
Author: Jason Merrill <jason@redhat.com>
Date:   Wed May 30 09:29:37 2012 -0400

    	PR c++/53356
    	* tree.c (stabilize_init): Side effects make the init unstable.

diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
index 236180d..897d4d7 100644
--- a/gcc/cp/tree.c
+++ b/gcc/cp/tree.c
@@ -3458,7 +3458,7 @@ stabilize_init (tree init, tree *initp)
 
   /* The initialization is being performed via a bitwise copy -- and
      the item copied may have side effects.  */
-  return TREE_SIDE_EFFECTS (init);
+  return !TREE_SIDE_EFFECTS (init);
 }
 
 /* Like "fold", but should be used whenever we might be processing the
diff --git a/gcc/testsuite/g++.dg/init/new33.C b/gcc/testsuite/g++.dg/init/new33.C
new file mode 100644
index 0000000..18da79e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/init/new33.C
@@ -0,0 +1,11 @@
+// PR c++/53356
+// { dg-do compile }
+
+struct A {};
+struct B { operator const A & () const; };
+struct C { operator const A & () const; C (); };
+struct D { operator const A & () const; D (); ~D (); };
+
+A *foo () { return new A (B ()); }
+A *bar () { return new A (C ()); }
+A *baz () { return new A (D ()); }

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

* Re: C++ PATCH for c++/53356 (C++11 ICE with new)
  2012-05-30 14:45 C++ PATCH for c++/53356 (C++11 ICE with new) Jason Merrill
@ 2012-05-30 21:59 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2012-05-30 21:59 UTC (permalink / raw)
  To: gcc-patches List

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

On 05/30/2012 10:44 AM, Jason Merrill wrote:
> The code in build_new_1 already knows how to handle an initializer that
> it was unable to stabilize, but the logic was backwards in a critical
> place. I'm surprised this typo hasn't been hit before since it was
> introduced in 2006...

...and then this patch fixes stabilize_init to actually stabilize the 
initializer in this case.  And fixes another case I noticed that has 
been ICEing since 4.0.

Tested x86_64-pc-linux-gnu, applying to trunk.  I'm not going to apply 
it to 4.7 because nobody has noticed the other issue.


[-- Attachment #2: 53356-2.patch --]
[-- Type: text/x-patch, Size: 3493 bytes --]

commit 3f176267c61a889926e3f518ccd79cf55c5e7de1
Author: Jason Merrill <jason@redhat.com>
Date:   Wed May 30 17:31:57 2012 -0400

    	PR c++/53356
    	* tree.c (stabilize_init): Handle stabilizing a TARGET_EXPR
    	representing a bitwise copy of a glvalue.

diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
index 4e7056f..2b541cd 100644
--- a/gcc/cp/tree.c
+++ b/gcc/cp/tree.c
@@ -3389,7 +3389,7 @@ stabilize_aggr_init (tree call, tree *initp)
    takes care not to introduce additional temporaries.
 
    Returns TRUE iff the expression was successfully pre-evaluated,
-   i.e., if INIT is now side-effect free, except for, possible, a
+   i.e., if INIT is now side-effect free, except for, possibly, a
    single call to a constructor.  */
 
 bool
@@ -3402,21 +3402,37 @@ stabilize_init (tree init, tree *initp)
   if (t == error_mark_node || processing_template_decl)
     return true;
 
-  if (TREE_CODE (t) == INIT_EXPR
-      && TREE_CODE (TREE_OPERAND (t, 1)) != TARGET_EXPR
-      && TREE_CODE (TREE_OPERAND (t, 1)) != CONSTRUCTOR
-      && TREE_CODE (TREE_OPERAND (t, 1)) != AGGR_INIT_EXPR)
-    {
-      TREE_OPERAND (t, 1) = stabilize_expr (TREE_OPERAND (t, 1), initp);
-      return true;
-    }
-
   if (TREE_CODE (t) == INIT_EXPR)
     t = TREE_OPERAND (t, 1);
   if (TREE_CODE (t) == TARGET_EXPR)
     t = TARGET_EXPR_INITIAL (t);
-  if (TREE_CODE (t) == COMPOUND_EXPR)
-    t = expr_last (t);
+
+  /* If the RHS can be stabilized without breaking copy elision, stabilize
+     it.  We specifically don't stabilize class prvalues here because that
+     would mean an extra copy, but they might be stabilized below.  */
+  if (TREE_CODE (init) == INIT_EXPR
+      && TREE_CODE (t) != CONSTRUCTOR
+      && TREE_CODE (t) != AGGR_INIT_EXPR
+      && (SCALAR_TYPE_P (TREE_TYPE (t))
+	  || lvalue_or_rvalue_with_address_p (t)))
+    {
+      TREE_OPERAND (init, 1) = stabilize_expr (t, initp);
+      return true;
+    }
+
+  if (TREE_CODE (t) == COMPOUND_EXPR
+      && TREE_CODE (init) == INIT_EXPR)
+    {
+      tree last = expr_last (t);
+      /* Handle stabilizing the EMPTY_CLASS_EXPR pattern.  */
+      if (!TREE_SIDE_EFFECTS (last))
+	{
+	  *initp = t;
+	  TREE_OPERAND (init, 1) = last;
+	  return true;
+	}
+    }
+
   if (TREE_CODE (t) == CONSTRUCTOR)
     {
       /* Aggregate initialization: stabilize each of the field
@@ -3439,11 +3455,6 @@ stabilize_init (tree init, tree *initp)
       return good;
     }
 
-  /* If the initializer is a COND_EXPR, we can't preevaluate
-     anything.  */
-  if (TREE_CODE (t) == COND_EXPR)
-    return false;
-
   if (TREE_CODE (t) == CALL_EXPR)
     {
       stabilize_call (t, initp);
diff --git a/gcc/testsuite/g++.dg/init/new34.C b/gcc/testsuite/g++.dg/init/new34.C
new file mode 100644
index 0000000..9e67eb34
--- /dev/null
+++ b/gcc/testsuite/g++.dg/init/new34.C
@@ -0,0 +1,11 @@
+// PR c++/53356
+
+struct A { A(); ~A(); };
+
+struct B {
+    operator const A () const;
+};
+
+A* cause_ICE() {
+  return new A((A(),A()));
+}
diff --git a/gcc/testsuite/g++.dg/tree-ssa/stabilize1.C b/gcc/testsuite/g++.dg/tree-ssa/stabilize1.C
new file mode 100644
index 0000000..2fe723c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tree-ssa/stabilize1.C
@@ -0,0 +1,14 @@
+// PR c++/53356
+// { dg-options "-fdump-tree-gimple" }
+// { dg-final { scan-tree-dump-not "= 0" "gimple" } }
+// { dg-final { cleanup-tree-dump "gimple" } }
+
+class A {};
+
+struct B {
+    operator const A &() const;
+};
+
+A* cause_ICE() {
+    return new A(B());
+}

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

end of thread, other threads:[~2012-05-30 21:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-30 14:45 C++ PATCH for c++/53356 (C++11 ICE with new) Jason Merrill
2012-05-30 21:59 ` Jason Merrill

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