From 312626229bfd4162550891bd8947b0fe310da6f5 Mon Sep 17 00:00:00 2001 From: Martin Liska Date: Tue, 30 Jul 2019 09:24:56 +0200 Subject: [PATCH] DCE: do not handle delete operators with a SSA_NAME as a size argument (PR tree-optimization/91270). gcc/ChangeLog: 2019-07-30 Martin Liska PR tree-optimization/91270 * tree-ssa-dce.c (simple_delete_operator_p): New. (propagate_necessity): Use it to filter delete operators that we want to delete. (eliminate_unnecessary_stmts): Likewise. gcc/testsuite/ChangeLog: 2019-07-30 Martin Liska PR tree-optimization/91270 * g++.dg/torture/pr91270.C: New test. --- gcc/testsuite/g++.dg/torture/pr91270.C | 10 ++++++++++ gcc/tree-ssa-dce.c | 19 ++++++++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/torture/pr91270.C diff --git a/gcc/testsuite/g++.dg/torture/pr91270.C b/gcc/testsuite/g++.dg/torture/pr91270.C new file mode 100644 index 00000000000..60d766e9e9f --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/pr91270.C @@ -0,0 +1,10 @@ +/* { dg-do compile } */ + +struct S { + ~S(); +}; +int a = 123; +void fn1() { + S *s = new S[a]; + delete[] s; +} diff --git a/gcc/tree-ssa-dce.c b/gcc/tree-ssa-dce.c index 763b76f0e53..e844824dc12 100644 --- a/gcc/tree-ssa-dce.c +++ b/gcc/tree-ssa-dce.c @@ -646,6 +646,18 @@ degenerate_phi_p (gimple *phi) return true; } +/* Return true when a GIMPLE STMT is a delete call operator that + has either one argument or second argument is an integer constant. */ + +static bool +simple_delete_operator_p (gimple *stmt) +{ + return (is_gimple_call (stmt) + && (gimple_call_operator_delete_p (as_a (stmt)) + && (gimple_call_num_args (stmt) == 1 + || TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST))); +} + /* Propagate necessity using the operands of necessary statements. Process the uses on each statement in the worklist, and add all feeding statements which contribute to the calculation of this @@ -805,9 +817,7 @@ propagate_necessity (bool aggressive) allocation function do not mark that necessary through processing the argument. */ if (gimple_call_builtin_p (stmt, BUILT_IN_FREE) - || (is_gimple_call (stmt) - && gimple_call_operator_delete_p (as_a (stmt)))) - + || simple_delete_operator_p (stmt)) { tree ptr = gimple_call_arg (stmt, 0); gimple *def_stmt; @@ -1306,8 +1316,7 @@ eliminate_unnecessary_stmts (void) (and thus is getting removed). */ if (gimple_plf (stmt, STMT_NECESSARY) && (gimple_call_builtin_p (stmt, BUILT_IN_FREE) - || (is_gimple_call (stmt) - && gimple_call_operator_delete_p (as_a (stmt))))) + || simple_delete_operator_p (stmt))) { tree ptr = gimple_call_arg (stmt, 0); if (TREE_CODE (ptr) == SSA_NAME) -- 2.22.0