On 10/1/20 5:26 AM, Richard Biener wrote:
On Wed, 30 Sep 2020, Jason Merrill wrote:

On 9/28/20 3:09 PM, Jason Merrill wrote:
On 9/28/20 3:56 AM, Richard Biener wrote:
On Fri, 25 Sep 2020, Jason Merrill wrote:

On 9/25/20 2:30 AM, Richard Biener wrote:
On Thu, 24 Sep 2020, Jason Merrill wrote:

On 9/24/20 3:43 AM, Richard Biener wrote:
On Wed, 23 Sep 2020, Jason Merrill wrote:

On 9/23/20 2:42 PM, Richard Biener wrote:
On September 23, 2020 7:53:18 PM GMT+02:00, Jason Merrill
<ja...@redhat.com>
wrote:
On 9/23/20 4:14 AM, Richard Biener wrote:
C++ operator delete, when DECL_IS_REPLACEABLE_OPERATOR_DELETE_P,
does not cause the deleted object to be escaped.? It also has no
other interesting side-effects for PTA so skip it like we do
for BUILT_IN_FREE.

Hmm, this is true of the default implementation, but since the
function

is replaceable, we don't know what a user definition might do with
the
pointer.

But can the object still be 'used' after delete? Can delete fail /
throw?

What guarantee does the predicate give us?

The deallocation function is called as part of a delete expression in
order
to
release the storage for an object, ending its lifetime (if it was not
ended
by
a destructor), so no, the object can't be used afterward.

OK, but the delete operator can access the object contents if there
wasn't a destructor ...

A deallocation function that throws has undefined behavior.

OK, so it seems the 'replaceable' operators are the global ones
(for user-defined/class-specific placement variants I see arbitrary
extra arguments that we'd possibly need to handle).

I'm happy to revert but I'd like to have a testcase that FAILs
with the patch ;)

Now, the following aborts:

struct X {
???? static struct X saved;
???? int *p;
???? X() { __builtin_memcpy (this, &saved, sizeof (X)); }
};
void operator delete (void *p)
{
???? __builtin_memcpy (&X::saved, p, sizeof (X));
}
int main()
{
???? int y = 1;
???? X *p = new X;
???? p->p = &y;
???? delete p;
???? X *q = new X;
???? *(q->p) = 2;
???? if (y != 2)
?????? __builtin_abort ();
}

and I could fix this by not making *p but what *p points to escape.
The testcase is of course maximally awkward, but hey ... ;)

Now this would all be moot if operator delete may not access
the object (or if the object contents are undefined at that point).

Oh, and the testcase segfaults when compiled with GCC 10 because
there we elide the new X / delete p pair ... which is invalid then?
Hmm, we emit

???? MEM[(struct X *)_8] ={v} {CLOBBER};
???? operator delete (_8, 8);

so the object contents are undefined _before_ calling delete
even when I do not have a DTOR?? That is, the above,
w/o -fno-lifetime-dse, makes the PTA patch OK for the testcase.

Yes, all classes have a destructor, even if it's trivial, so the
object's
lifetime definitely ends before the call to operator delete. This is
less
clear for scalar objects, but treating them similarly would be
consistent
with
other recent changes, so I think it's fine for us to assume that scalar
objects are also invalidated before the call to operator delete.  But of
course this doesn't apply to explicit calls to operator delete outside
of a
delete expression.

OK, so change the testcase main slightly to

int main()
{
??? int y = 1;
??? X *p = new X;
??? p->p = &y;
??? ::operator delete(p);
??? X *q = new X;
??? *(q->p) = 2;
??? if (y != 2)
????? __builtin_abort ();
}

in this case the lifetime of *p does not end before calling
::operator delete() and delete can stash the object contents
somewhere before ending its lifetime.? For the very same reason
we may not elide a new/delete pair like in

int main()
{
??? int *p = new int;
??? *p = 1;
??? ::operator delete (p);
}

Correct; the permission to elide new/delete pairs are for the expressions,
not
the functions.

which we before the change did not do only because calling
operator delete made p escape.? Unfortunately points-to analysis
cannot really reconstruct whether delete was called as part of
a delete expression or directly (and thus whether object lifetime
ended already), neither can DCE.? So I guess we need to mark
the operator delete call in some way to make those transforms
safe.? At least currently any operator delete call makes the
alias guarantee of a operator new call moot by forcing the object
to be aliased with all global and escaped memory ...

Looks like there are some unallocated flags for CALL_EXPR we could
pick but I wonder if we can recycle protected_flag which is

???????? CALL_FROM_THUNK_P and
???????? CALL_ALLOCA_FOR_VAR_P in
???????????? CALL_EXPR

for calls to DECL_IS_OPERATOR_{NEW,DELETE}_P, thus whether
we have CALL_FROM_THUNK_P for those operators.? Guess picking
a new flag is safer.

We won't ever call those operators from a thunk, so it should be OK to
reuse
it.

But, does it seem correct that we need to distinguish
delete expressions from plain calls to operator delete?

A reason for that distinction came up in the context of omitting
new/delete
pairs: we want to consider the operator first called by the new or delete
expression, not a call from that first operator to another operator
new/delete
and exposed by inlining.

https://gcc.gnu.org/pipermail/gcc-patches/2020-April/543404.html

In this context I also wonder about non-replaceable operator delete,
specifically operator delete in classes - are there any semantic
differences between those or why did we choose to only mark
the replaceable ones?

The standard says that for omitting a 'new' allocation, the operator new
has
to be a replaceable one, but does not say the same about 'delete'; it just
says that if the allocation was omitted, the delete-expression does not
call a
deallocation function.? It may not be necessary to make this distinction
for
delete.? And this distinction could be local to the front end.

In the front end, we currently have cxx_replaceable_global_alloc_fn that
already ignores the replaceability of operator delete.? And we have
CALL_FROM_NEW_OR_DELETE_P, that would just need to move into the middle
end.
And perhaps get renamed to CALL_OMITTABLE_NEW_OR_DELETE_P, and not get set
for
calls to non-replaceable operator new.

CALL_FROM_NEW_OR_DELETE_P indeed looks like the best fit - it's
only evaluated when cxx_replaceable_global_alloc_fn matches in the C++
FE so could be made to cover only replaceable variants.

CALL_REPLACEABLE_NEW_OR_DELETE_P () maybe, since we already use
REPLACEABLE for the fndecl flags?? OMITTABLE is too specific
for the PTA case where it really matters whether the object
lifetime ends before the delete call, not whether it can be
omitted (hmm, guess that's not 100% overlap then either...).

That seems like good overlap to me, if we agree that object lifetime ends
before any delete call from a delete-expression, whether or not the operator
delete is replaceable.

Mind doing the C++ side of things recycling protected_flag as suggested?

OK.

Find attached a patch series, your patch plus the GIMPLE side of the fix.
This shows that the C++ FE side is possibly incomplete with for
example g++.dg/pr94314-2.C now FAILing.  There we have

   A *a = new A (argc);
   delete a;

being expanded to

   <<cleanup_point <<< Unknown tree: expr_stmt
   (void) (a = TARGET_EXPR <D.2357, operator new (1)>;, try
     {
       A::A ((struct A *) D.2357, argc);
     }
   catch
     {
       operator delete (D.2357);
     }, (struct A *) D.2357;) >>>>>;
   <<cleanup_point <<< Unknown tree: expr_stmt
   if (SAVE_EXPR <a> != 0B)
     {
       try
         {
           *SAVE_EXPR <a> = {CLOBBER};
         }
       finally
         {
           operator delete ((void *) SAVE_EXPR <a>);
         }
     }
   else
     {
       <<< Unknown tree: void_cst >>>
     } >>>>>;
   return <retval> = 0;

where the operator delete call in the catch {} expression is
not marked as CALL_FROM_NEW_OR_DELETE_P, possibly because this
call is compiler generated.

So it's probably technically true that CALL_FROM_NEW_OR_DELETE_P is
false but we still expect the same guarantees to hold here, in
particular we expect to elide the new/delete pair?

That seems like an oversight in the standard. This first patch sets the flag. The second patch in the file removes consideration of whether an operator delete is replaceable, as I was discussing earlier.

commit 592714921200fa07e1a9de230522804a82bdb5c9
Author: Jason Merrill <ja...@redhat.com>
Date:   Thu Oct 1 11:21:57 2020 -0400

    c++: Set CALL_FROM_NEW_OR_DELETE_P on more calls.
    
    We were failing to set the flag on a delete call in a new expression, in a
    deleting destructor, and in a coroutine.  Fixed by setting it in the
    function that builds the call.
    
    gcc/cp/ChangeLog:
    
            * call.c (build_operator_new_call): Set CALL_FROM_NEW_OR_DELETE_P.
            (build_op_delete_call): Likewise.
            * init.c (build_new_1, build_vec_delete_1, build_delete): Not here.
            (build_delete):
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/pr94314.C: new/delete no longer omitted.

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index d67e8fe2b28..bd662518958 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -4769,7 +4769,16 @@ build_operator_new_call (tree fnname, vec<tree, va_gc> **args,
      *fn = cand->fn;
 
    /* Build the CALL_EXPR.  */
-   return build_over_call (cand, LOOKUP_NORMAL, complain);
+   tree ret = build_over_call (cand, LOOKUP_NORMAL, complain);
+
+   /* Set this flag for all callers of this function.  In addition to
+      new-expressions, this is called for allocating coroutine state; treat
+      that as an implicit new-expression.  */
+   tree call = extract_call_expr (ret);
+   if (TREE_CODE (call) == CALL_EXPR)
+     CALL_FROM_NEW_OR_DELETE_P (call) = 1;
+
+   return ret;
 }
 
 /* Build a new call to operator().  This may change ARGS.  */
@@ -6146,7 +6155,7 @@ build_new_op_1 (const op_location_t &loc, enum tree_code code, int flags,
     case VEC_NEW_EXPR:
     case VEC_DELETE_EXPR:
     case DELETE_EXPR:
-      /* Use build_op_new_call and build_op_delete_call instead.  */
+      /* Use build_operator_new_call and build_op_delete_call instead.  */
       gcc_unreachable ();
 
     case CALL_EXPR:
@@ -6983,6 +6992,7 @@ build_op_delete_call (enum tree_code code, tree addr, tree size,
       if (DECL_DELETED_FN (fn) && alloc_fn)
 	return NULL_TREE;
 
+      tree ret;
       if (placement)
 	{
 	  /* The placement args might not be suitable for overload
@@ -6995,7 +7005,7 @@ build_op_delete_call (enum tree_code code, tree addr, tree size,
 	    argarray[i] = CALL_EXPR_ARG (placement, i);
 	  if (!mark_used (fn, complain) && !(complain & tf_error))
 	    return error_mark_node;
-	  return build_cxx_call (fn, nargs, argarray, complain);
+	  ret = build_cxx_call (fn, nargs, argarray, complain);
 	}
       else
 	{
@@ -7013,7 +7023,6 @@ build_op_delete_call (enum tree_code code, tree addr, tree size,
 						  complain);
 	    }
 
-	  tree ret;
 	  releasing_vec args;
 	  args->quick_push (addr);
 	  if (destroying)
@@ -7026,8 +7035,18 @@ build_op_delete_call (enum tree_code code, tree addr, tree size,
 	      args->quick_push (al);
 	    }
 	  ret = cp_build_function_call_vec (fn, &args, complain);
-	  return ret;
 	}
+
+      /* Set this flag for all callers of this function.  In addition to
+	 delete-expressions, this is called for deallocating coroutine state;
+	 treat that as an implicit delete-expression.  This is also called for
+	 the delete if the constructor throws in a new-expression, and for a
+	 deleting destructor (which implements a delete-expression).  */
+      tree call = extract_call_expr (ret);
+      if (TREE_CODE (call) == CALL_EXPR)
+	CALL_FROM_NEW_OR_DELETE_P (call) = 1;
+
+      return ret;
     }
 
   /* [expr.new]
diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index e84e985492d..00fff3f7327 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -3433,10 +3433,6 @@ build_new_1 (vec<tree, va_gc> **placement, tree type, tree nelts,
 	}
     }
 
-  tree alloc_call_expr = extract_call_expr (alloc_call);
-  if (TREE_CODE (alloc_call_expr) == CALL_EXPR)
-    CALL_FROM_NEW_OR_DELETE_P (alloc_call_expr) = 1;
-
   if (cookie_size)
     alloc_call = maybe_wrap_new_for_constexpr (alloc_call, elt_type,
 					       cookie_size);
@@ -4145,10 +4141,6 @@ build_vec_delete_1 (location_t loc, tree base, tree maxindex, tree type,
 					      /*placement=*/NULL_TREE,
 					      /*alloc_fn=*/NULL_TREE,
 					      complain);
-
-      tree deallocate_call_expr = extract_call_expr (deallocate_expr);
-      if (TREE_CODE (deallocate_call_expr) == CALL_EXPR)
-	CALL_FROM_NEW_OR_DELETE_P (deallocate_call_expr) = 1;
     }
 
   body = loop;
@@ -5073,12 +5065,6 @@ build_delete (location_t loc, tree otype, tree addr,
 
   if (do_delete == error_mark_node)
     return error_mark_node;
-  else if (do_delete)
-    {
-      tree do_delete_call_expr = extract_call_expr (do_delete);
-      if (TREE_CODE (do_delete_call_expr) == CALL_EXPR)
-	CALL_FROM_NEW_OR_DELETE_P (do_delete_call_expr) = 1;
-    }
 
   if (do_delete && !TREE_SIDE_EFFECTS (expr))
     expr = do_delete;
diff --git a/gcc/testsuite/g++.dg/pr94314.C b/gcc/testsuite/g++.dg/pr94314.C
index 4e5ae122e9f..72467127fea 100644
--- a/gcc/testsuite/g++.dg/pr94314.C
+++ b/gcc/testsuite/g++.dg/pr94314.C
@@ -78,5 +78,5 @@ int main(){
   return 0;
 }
 
-/* { dg-final { scan-tree-dump-times "Deleting : operator delete" 1 "cddce1"} } */
+/* { dg-final { scan-tree-dump-not "Deleting : operator delete" "cddce1"} } */
 /* { dg-final { scan-tree-dump-not "Deleting : B::operator delete" "cddce1"} } */

commit 379fc0feec0b8051a8ef12d7f4d636ec0a990ec0
Author: Jason Merrill <ja...@redhat.com>
Date:   Thu Oct 1 16:39:03 2020 -0400

    tree-ssa-dce: Ignore whether an operator delete is replaceable.
    
    Now that we check CALL_FROM_NEW_OR_DELETE_P, we don't need to consider
    whether an operator delete is replaceable, that consideration only applies
    to operator new.
    
    gcc/ChangeLog:
    
            * tree.h (DECL_IS_REPLACEABLE_OPERATOR_DELETE_P): Remove.
            * gimple.c (gimple_call_replaceable_operator_delete_p):
            Rename to gimple_call_operator_delete_p.
            * gimple.h: Adjust.
            * tree-ssa-dce.c: Adjust.
            * tree-ssa-structalias.c: Adjust.

diff --git a/gcc/gimple.h b/gcc/gimple.h
index 108ae846849..3c9b9965f5a 100644
--- a/gcc/gimple.h
+++ b/gcc/gimple.h
@@ -1605,7 +1605,7 @@ extern alias_set_type gimple_get_alias_set (tree);
 extern bool gimple_ior_addresses_taken (bitmap, gimple *);
 extern bool gimple_builtin_call_types_compatible_p (const gimple *, tree);
 extern combined_fn gimple_call_combined_fn (const gimple *);
-extern bool gimple_call_replaceable_operator_delete_p (const gcall *);
+extern bool gimple_call_operator_delete_p (const gcall *);
 extern bool gimple_call_builtin_p (const gimple *);
 extern bool gimple_call_builtin_p (const gimple *, enum built_in_class);
 extern bool gimple_call_builtin_p (const gimple *, enum built_in_function);
diff --git a/gcc/tree.h b/gcc/tree.h
index f27a7399a37..c0a027a650d 100644
--- a/gcc/tree.h
+++ b/gcc/tree.h
@@ -3074,9 +3074,6 @@ set_function_decl_type (tree decl, function_decl_type t, bool set)
 #define DECL_IS_OPERATOR_DELETE_P(NODE) \
   (FUNCTION_DECL_CHECK (NODE)->function_decl.decl_type == OPERATOR_DELETE)
 
-#define DECL_IS_REPLACEABLE_OPERATOR_DELETE_P(NODE) \
-  (DECL_IS_OPERATOR_DELETE_P (NODE) && DECL_IS_REPLACEABLE_OPERATOR (NODE))
-
 #define DECL_SET_IS_OPERATOR_DELETE(NODE, VAL) \
   set_function_decl_type (FUNCTION_DECL_CHECK (NODE), OPERATOR_DELETE, VAL)
 
diff --git a/gcc/gimple.c b/gcc/gimple.c
index f07ddab7953..523d845de89 100644
--- a/gcc/gimple.c
+++ b/gcc/gimple.c
@@ -2717,12 +2717,12 @@ gimple_builtin_call_types_compatible_p (const gimple *stmt, tree fndecl)
 /* Return true when STMT is operator a replaceable delete call.  */
 
 bool
-gimple_call_replaceable_operator_delete_p (const gcall *stmt)
+gimple_call_operator_delete_p (const gcall *stmt)
 {
   tree fndecl;
 
   if ((fndecl = gimple_call_fndecl (stmt)) != NULL_TREE)
-    return DECL_IS_REPLACEABLE_OPERATOR_DELETE_P (fndecl);
+    return DECL_IS_OPERATOR_DELETE_P (fndecl);
   return false;
 }
 
diff --git a/gcc/tree-ssa-dce.c b/gcc/tree-ssa-dce.c
index c9e0c8fd116..a0466127f9c 100644
--- a/gcc/tree-ssa-dce.c
+++ b/gcc/tree-ssa-dce.c
@@ -612,7 +612,7 @@ mark_all_reaching_defs_necessary_1 (ao_ref *ref ATTRIBUTE_UNUSED,
 
       if (callee != NULL_TREE
 	  && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (callee)
-	      || DECL_IS_REPLACEABLE_OPERATOR_DELETE_P (callee))
+	      || DECL_IS_OPERATOR_DELETE_P (callee))
 	  && gimple_call_from_new_or_delete (call))
 	return false;
     }
@@ -877,7 +877,7 @@ propagate_necessity (bool aggressive)
 	  bool is_delete_operator
 	    = (is_gimple_call (stmt)
 	       && gimple_call_from_new_or_delete (as_a <gcall *> (stmt))
-	       && gimple_call_replaceable_operator_delete_p (as_a <gcall *> (stmt)));
+	       && gimple_call_operator_delete_p (as_a <gcall *> (stmt)));
 	  if (is_delete_operator
 	      || gimple_call_builtin_p (stmt, BUILT_IN_FREE))
 	    {
@@ -975,7 +975,7 @@ propagate_necessity (bool aggressive)
 
 	      if (callee != NULL_TREE
 		  && (DECL_IS_REPLACEABLE_OPERATOR_NEW_P (callee)
-		      || DECL_IS_REPLACEABLE_OPERATOR_DELETE_P (callee))
+		      || DECL_IS_OPERATOR_DELETE_P (callee))
 		  && gimple_call_from_new_or_delete (call))
 		continue;
 
@@ -1402,7 +1402,7 @@ eliminate_unnecessary_stmts (void)
 	      && (gimple_call_builtin_p (stmt, BUILT_IN_FREE)
 		  || (is_gimple_call (stmt)
 		      && gimple_call_from_new_or_delete (as_a <gcall *> (stmt))
-		      && gimple_call_replaceable_operator_delete_p (as_a <gcall *> (stmt)))))
+		      && gimple_call_operator_delete_p (as_a <gcall *> (stmt)))))
 	    {
 	      tree ptr = gimple_call_arg (stmt, 0);
 	      if (TREE_CODE (ptr) == SSA_NAME)
diff --git a/gcc/tree-ssa-structalias.c b/gcc/tree-ssa-structalias.c
index 69de932b14c..30a8c93b4ff 100644
--- a/gcc/tree-ssa-structalias.c
+++ b/gcc/tree-ssa-structalias.c
@@ -4862,7 +4862,7 @@ find_func_aliases_for_call (struct function *fn, gcall *t)
 	 such operator, then the effects for PTA (in particular
 	 the escaping of the pointer) can be ignored.  */
       else if (fndecl
-	       && DECL_IS_REPLACEABLE_OPERATOR_DELETE_P (fndecl)
+	       && DECL_IS_OPERATOR_DELETE_P (fndecl)
 	       && gimple_call_from_new_or_delete (t))
 	;
       else

Reply via email to