On Wed, Jan 08, 2014 at 12:19:02PM +0100, Richard Biener wrote:
> On Wed, 8 Jan 2014, Jakub Jelinek wrote:
> 
> > On Wed, Jan 08, 2014 at 12:15:40PM +0100, Richard Biener wrote:
> > > > I start to think this is a too complex transform for stmt folding ...
> > > 
> > > Alternatively do update_call_from_tree (gsi, 
> > > get_or_create_ssa_default_def 
> > > (cfun, create_tmp_var (TREE_TYPE (lhs)))).
> > 
> > The lhs might not be is_gimple_reg_type though.  What to do in that case?
> 
> In that case you can remove the stmt.

Ok, so like this?  Unfortunately, I haven't been able to construct a
testcase where this would be folded later than during gimplification where
lhs is obviously not a SSA_NAME and we can safely replace the call.

Bootstrapped/regtested on x86_64-linux and i686-linux.

2014-01-08  Jakub Jelinek  <ja...@redhat.com>

        PR tree-optimization/59622
        * gimple-fold.c (gimple_fold_call): Fix a typo in message.  Handle
        __cxa_pure_virtual similarly to __builtin_unreachable, but replace
        the OBJ_TYPE_REF call with the noreturn and add if needed a setter
        of the lhs SSA_NAME.  Don't devirtualize for inplace at all.

        * g++.dg/opt/pr59622-2.C: New test.
        * g++.dg/opt/pr59622-3.C: New test.
        * g++.dg/opt/pr59622-4.C: New test.

--- gcc/gimple-fold.c.jj        2014-01-08 10:23:24.536443566 +0100
+++ gcc/gimple-fold.c   2014-01-08 17:02:40.356635177 +0100
@@ -1167,7 +1167,7 @@ gimple_fold_call (gimple_stmt_iterator *
                                                  (OBJ_TYPE_REF_EXPR 
(callee)))))
            {
              fprintf (dump_file,
-                      "Type inheritnace inconsistent devirtualization of ");
+                      "Type inheritance inconsistent devirtualization of ");
              print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
              fprintf (dump_file, " to ");
              print_generic_expr (dump_file, callee, TDF_SLIM);
@@ -1177,26 +1177,46 @@ gimple_fold_call (gimple_stmt_iterator *
          gimple_call_set_fn (stmt, OBJ_TYPE_REF_EXPR (callee));
          changed = true;
        }
-      else if (flag_devirtualize && virtual_method_call_p (callee))
+      else if (flag_devirtualize && !inplace && virtual_method_call_p (callee))
        {
          bool final;
          vec <cgraph_node *>targets
            = possible_polymorphic_call_targets (callee, &final);
          if (final && targets.length () <= 1)
            {
+             tree fndecl;
              if (targets.length () == 1)
+               fndecl = targets[0]->decl;
+             else
+               fndecl = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
+
+             /* If fndecl (like __builtin_unreachable or
+                __cxa_pure_virtual) takes no arguments, doesn't have
+                return value and is noreturn, if the call doesn't have
+                lhs or lhs isn't SSA_NAME, replace the call with
+                the noreturn call, otherwise insert it before the call
+                and replace the call with setting of lhs to default def.  */
+             if (TREE_THIS_VOLATILE (fndecl)
+                 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fndecl)))
+                 && TYPE_ARG_TYPES (TREE_TYPE (fndecl)) == void_list_node)
                {
-                 gimple_call_set_fndecl (stmt, targets[0]->decl);
-                 changed = true;
-               }
-             else if (!inplace)
-               {
-                 tree fndecl = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
+                 tree lhs = gimple_call_lhs (stmt);
                  gimple new_stmt = gimple_build_call (fndecl, 0);
                  gimple_set_location (new_stmt, gimple_location (stmt));
-                 gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
+                 if (lhs && TREE_CODE (lhs) == SSA_NAME)
+                   {
+                     tree var = create_tmp_var (TREE_TYPE (lhs), NULL);
+                     tree def = get_or_create_ssa_default_def (cfun, var);
+                     gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
+                     update_call_from_tree (gsi, def);
+                   }
+                 else
+                   gsi_replace (gsi, new_stmt, true);
                  return true;
                }
+
+             gimple_call_set_fndecl (stmt, fndecl);
+             changed = true;
            }
        }
     }
--- gcc/testsuite/g++.dg/opt/pr59622-2.C.jj     2014-01-08 16:42:49.588747876 
+0100
+++ gcc/testsuite/g++.dg/opt/pr59622-2.C        2014-01-08 16:42:49.588747876 
+0100
@@ -0,0 +1,21 @@
+// PR tree-optimization/59622
+// { dg-do compile }
+// { dg-options "-O2" }
+
+namespace
+{
+  struct A
+  {
+    A () {}
+    virtual A *bar (int) = 0;
+    A *baz (int x) { return bar (x); }
+  };
+}
+
+A *a;
+
+void
+foo ()
+{
+  a->baz (0);
+}
--- gcc/testsuite/g++.dg/opt/pr59622-3.C.jj     2014-01-08 17:00:20.944359961 
+0100
+++ gcc/testsuite/g++.dg/opt/pr59622-3.C        2014-01-08 17:00:44.558244745 
+0100
@@ -0,0 +1,21 @@
+// PR tree-optimization/59622
+// { dg-do compile }
+// { dg-options "-O2" }
+
+struct C { int a; int b; };
+
+namespace
+{
+  struct A
+  {
+    virtual C foo ();
+    C bar () { return foo (); }
+  };
+}
+
+C
+baz ()
+{
+  A a;
+  return a.bar ();
+}
--- gcc/testsuite/g++.dg/opt/pr59622-4.C.jj     2014-01-08 17:00:54.332188462 
+0100
+++ gcc/testsuite/g++.dg/opt/pr59622-4.C        2014-01-08 17:01:41.113947431 
+0100
@@ -0,0 +1,23 @@
+// PR tree-optimization/59622
+// { dg-do compile }
+// { dg-options "-O2" }
+
+struct C { int a; int b; };
+
+namespace
+{
+  struct A
+  {
+    A () {}
+    virtual C bar (int) = 0;
+    C baz (int x) { return bar (x); }
+  };
+}
+
+A *a;
+
+C
+foo ()
+{
+  return a->baz (0);
+}


        Jakub

Reply via email to