]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: mark TARGET_EXPRs for function arguments eliding [PR114707]
authorMarek Polacek <polacek@redhat.com>
Wed, 22 May 2024 20:28:02 +0000 (16:28 -0400)
committerMarek Polacek <polacek@redhat.com>
Tue, 28 May 2024 18:28:18 +0000 (14:28 -0400)
Coming back to our discussion in
<https://gcc.gnu.org/pipermail/gcc-patches/2024-April/649426.html>:
TARGET_EXPRs that initialize a function argument are not marked
TARGET_EXPR_ELIDING_P even though gimplify_arg drops such TARGET_EXPRs
on the floor.  To work around it, I added a pset to
replace_placeholders_for_class_temp_r, but it would be best to just rely
on TARGET_EXPR_ELIDING_P.

PR c++/114707

gcc/cp/ChangeLog:

* call.cc (convert_for_arg_passing): Call set_target_expr_eliding.
* typeck2.cc (replace_placeholders_for_class_temp_r): Don't use pset.
(digest_nsdmi_init): Call cp_walk_tree_without_duplicates instead of
cp_walk_tree.

Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/call.cc
gcc/cp/typeck2.cc

index 886760af699f1d62c2a928aa9477c0aef5085489..85536fc25ff29007def9c874aee23e91555586f5 100644 (file)
@@ -9437,6 +9437,10 @@ convert_for_arg_passing (tree type, tree val, tsubst_flags_t complain)
   if (complain & tf_warning)
     warn_for_address_of_packed_member (type, val);
 
+  /* gimplify_arg elides TARGET_EXPRs that initialize a function argument.  */
+  if (SIMPLE_TARGET_EXPR_P (val))
+    set_target_expr_eliding (val);
+
   return val;
 }
 
index 06bad4d3303f86aa2d27785abadb495dc2c7bcb3..7782f38da4384faf0e212dce8a4171b43966d738 100644 (file)
@@ -1409,16 +1409,14 @@ digest_init_flags (tree type, tree init, int flags, tsubst_flags_t complain)
    in the context of guaranteed copy elision).  */
 
 static tree
-replace_placeholders_for_class_temp_r (tree *tp, int *, void *data)
+replace_placeholders_for_class_temp_r (tree *tp, int *, void *)
 {
   tree t = *tp;
-  auto pset = static_cast<hash_set<tree> *>(data);
 
   /* We're looking for a TARGET_EXPR nested in the whole expression.  */
   if (TREE_CODE (t) == TARGET_EXPR
       /* That serves as temporary materialization, not an initializer.  */
-      && !TARGET_EXPR_ELIDING_P (t)
-      && !pset->add (t))
+      && !TARGET_EXPR_ELIDING_P (t))
     {
       tree init = TARGET_EXPR_INITIAL (t);
       while (TREE_CODE (init) == COMPOUND_EXPR)
@@ -1433,16 +1431,6 @@ replace_placeholders_for_class_temp_r (tree *tp, int *, void *data)
          gcc_checking_assert (!find_placeholders (init));
        }
     }
-  /* TARGET_EXPRs initializing function arguments are not marked as eliding,
-     even though gimplify_arg drops them on the floor.  Don't go replacing
-     placeholders in them.  */
-  else if (TREE_CODE (t) == CALL_EXPR || TREE_CODE (t) == AGGR_INIT_EXPR)
-    for (int i = 0; i < call_expr_nargs (t); ++i)
-      {
-       tree arg = get_nth_callarg (t, i);
-       if (TREE_CODE (arg) == TARGET_EXPR && !TARGET_EXPR_ELIDING_P (arg))
-         pset->add (arg);
-      }
 
   return NULL_TREE;
 }
@@ -1490,8 +1478,8 @@ digest_nsdmi_init (tree decl, tree init, tsubst_flags_t complain)
      temporary materialization does not occur when initializing an object
      from a prvalue of the same type, therefore we must not replace the
      placeholder with a temporary object so that it can be elided.  */
-  hash_set<tree> pset;
-  cp_walk_tree (&init, replace_placeholders_for_class_temp_r, &pset, nullptr);
+  cp_walk_tree_without_duplicates (&init, replace_placeholders_for_class_temp_r,
+                                  nullptr);
 
   return init;
 }