public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-2853] c++: devirtualization of array destruction [PR110057]
@ 2023-07-28 15:40 Jason Merrill
0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2023-07-28 15:40 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:a47e615fbf9c6f4b24e5032df5d720b6bf9b63b5
commit r14-2853-ga47e615fbf9c6f4b24e5032df5d720b6bf9b63b5
Author: Ng YongXiang <yongxiangng@gmail.com>
Date: Thu Jul 27 08:06:14 2023 +0800
c++: devirtualization of array destruction [PR110057]
PR c++/110057
PR ipa/83054
gcc/cp/ChangeLog:
* init.cc (build_vec_delete_1): Devirtualize array destruction.
gcc/testsuite/ChangeLog:
* g++.dg/warn/pr83054.C: Remove devirtualization warning.
* g++.dg/lto/pr89335_0.C: Likewise.
* g++.dg/tree-ssa/devirt-array-destructor-1.C: New test.
* g++.dg/tree-ssa/devirt-array-destructor-2.C: New test.
* g++.dg/warn/pr83054-2.C: New test.
Signed-off-by: Ng Yong Xiang <yongxiangng@gmail.com>
Diff:
---
gcc/cp/init.cc | 11 ++++--
gcc/testsuite/g++.dg/lto/pr89335_0.C | 2 +-
.../g++.dg/tree-ssa/devirt-array-destructor-1.C | 28 ++++++++++++++
.../g++.dg/tree-ssa/devirt-array-destructor-2.C | 29 ++++++++++++++
gcc/testsuite/g++.dg/warn/pr83054-2.C | 44 ++++++++++++++++++++++
gcc/testsuite/g++.dg/warn/pr83054.C | 2 +-
6 files changed, 110 insertions(+), 6 deletions(-)
diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
index ff5014ca576..3b9a7783391 100644
--- a/gcc/cp/init.cc
+++ b/gcc/cp/init.cc
@@ -4116,8 +4116,8 @@ build_vec_delete_1 (location_t loc, tree base, tree maxindex, tree type,
if (type_build_dtor_call (type))
{
tmp = build_delete (loc, ptype, base, sfk_complete_destructor,
- LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
- complain);
+ LOOKUP_NORMAL|LOOKUP_DESTRUCTOR|LOOKUP_NONVIRTUAL,
+ 1, complain);
if (tmp == error_mark_node)
return error_mark_node;
}
@@ -4146,9 +4146,12 @@ build_vec_delete_1 (location_t loc, tree base, tree maxindex, tree type,
if (tmp == error_mark_node)
return error_mark_node;
body = build_compound_expr (loc, body, tmp);
+ /* [expr.delete]/3: "In an array delete expression, if the dynamic type of
+ the object to be deleted is not similar to its static type, the behavior
+ is undefined." So we can set LOOKUP_NONVIRTUAL. */
tmp = build_delete (loc, ptype, tbase, sfk_complete_destructor,
- LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1,
- complain);
+ LOOKUP_NORMAL|LOOKUP_DESTRUCTOR|LOOKUP_NONVIRTUAL,
+ 1, complain);
if (tmp == error_mark_node)
return error_mark_node;
body = build_compound_expr (loc, body, tmp);
diff --git a/gcc/testsuite/g++.dg/lto/pr89335_0.C b/gcc/testsuite/g++.dg/lto/pr89335_0.C
index 95bf4b3b0cb..76382f8d742 100644
--- a/gcc/testsuite/g++.dg/lto/pr89335_0.C
+++ b/gcc/testsuite/g++.dg/lto/pr89335_0.C
@@ -9,7 +9,7 @@ public:
virtual ~Container ();
};
-class List : public Container // { dg-lto-message "final would enable devirtualization" }
+class List : public Container
{
};
diff --git a/gcc/testsuite/g++.dg/tree-ssa/devirt-array-destructor-1.C b/gcc/testsuite/g++.dg/tree-ssa/devirt-array-destructor-1.C
new file mode 100644
index 00000000000..ce8dc2a57cd
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tree-ssa/devirt-array-destructor-1.C
@@ -0,0 +1,28 @@
+// PR c++/110057
+/* { dg-do-compile } */
+/* Virtual calls should be devirtualized because we know dynamic type of object in array at compile time */
+/* { dg-options "-O3 -fdump-tree-optimized -fno-inline" } */
+
+class A
+{
+public:
+ virtual ~A()
+ {
+ }
+};
+
+class B : public A
+{
+public:
+ virtual ~B()
+ {
+ }
+};
+
+int main()
+{
+ B b[10];
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "OBJ_TYPE_REF" 0 "optimized"} } */
diff --git a/gcc/testsuite/g++.dg/tree-ssa/devirt-array-destructor-2.C b/gcc/testsuite/g++.dg/tree-ssa/devirt-array-destructor-2.C
new file mode 100644
index 00000000000..6b44dc1a4ee
--- /dev/null
+++ b/gcc/testsuite/g++.dg/tree-ssa/devirt-array-destructor-2.C
@@ -0,0 +1,29 @@
+// PR c++/110057
+/* { dg-do-compile } */
+/* Virtual calls should be devirtualized because we know dynamic type of object in array at compile time */
+/* { dg-options "-O3 -fdump-tree-optimized -fno-inline" } */
+
+class A
+{
+public:
+ virtual ~A()
+ {
+ }
+};
+
+class B : public A
+{
+public:
+ virtual ~B()
+ {
+ }
+};
+
+int main()
+{
+ B* ptr = new B[10];
+ delete[] ptr;
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "OBJ_TYPE_REF" 0 "optimized"} } */
diff --git a/gcc/testsuite/g++.dg/warn/pr83054-2.C b/gcc/testsuite/g++.dg/warn/pr83054-2.C
new file mode 100644
index 00000000000..6d8216d229f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/pr83054-2.C
@@ -0,0 +1,44 @@
+// PR ipa/83054
+// { dg-options "-O3 -Wsuggest-final-types" }
+// { dg-do compile }
+
+// A throwing dtor in C++98 mode changes the warning.
+#if __cplusplus < 201100L
+#define NOTHROW throw()
+#else
+#define NOTHROW noexcept
+#endif
+
+extern "C" int printf (const char *, ...);
+struct foo // { dg-warning "final would enable devirtualization of 1 call" }
+{
+ static int count;
+ void print (int i, int j) { printf ("foo[%d][%d] = %d\n", i, j, x); }
+ int x;
+ foo () {
+ x = count++;
+ printf("this %d = %x\n", x, (void *)this);
+ }
+ virtual ~foo () NOTHROW {
+ printf("this %d = %x\n", x, (void *)this);
+ --count;
+ }
+};
+int foo::count;
+
+
+int main ()
+{
+ foo *arr[9];
+ for (int i = 0; i < 9; ++i)
+ arr[i] = new foo();
+ if (foo::count != 9)
+ return 1;
+ for (int i = 0; i < 9; ++i)
+ arr[i]->print(i / 3, i % 3);
+ for (int i = 0; i < 9; ++i)
+ delete arr[i];
+ if (foo::count != 0)
+ return 1;
+ return 0;
+}
diff --git a/gcc/testsuite/g++.dg/warn/pr83054.C b/gcc/testsuite/g++.dg/warn/pr83054.C
index 5285f94acee..5a4a6abe248 100644
--- a/gcc/testsuite/g++.dg/warn/pr83054.C
+++ b/gcc/testsuite/g++.dg/warn/pr83054.C
@@ -10,7 +10,7 @@
#endif
extern "C" int printf (const char *, ...);
-struct foo // { dg-warning "final would enable devirtualization of 5 calls" }
+struct foo
{
static int count;
void print (int i, int j) { printf ("foo[%d][%d] = %d\n", i, j, x); }
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-07-28 15:40 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-28 15:40 [gcc r14-2853] c++: devirtualization of array destruction [PR110057] 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).