ipa: check if cache_token != NULL before hash_set::add call

Message ID 196e54c5-4ee3-2d0b-3803-8b574eb71b99@suse.cz
State Accepted
Headers
Series ipa: check if cache_token != NULL before hash_set::add call |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Martin Liška Jan. 24, 2023, 3:15 p.m. UTC
  We should not insert an empty value to the container.

Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin

	PR ipa/108509

gcc/ChangeLog:

	* cgraphunit.cc (walk_polymorphic_call_targets): Insert
	ony non-null values.
	* ipa.cc (walk_polymorphic_call_targets): Likewise.

gcc/testsuite/ChangeLog:

	* g++.dg/ipa/pr108509.C: New test.
---
 gcc/cgraphunit.cc                   |  2 +-
 gcc/ipa.cc                          |  2 +-
 gcc/testsuite/g++.dg/ipa/pr108509.C | 22 ++++++++++++++++++++++
 3 files changed, 24 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/ipa/pr108509.C
  

Comments

Martin Jambor Feb. 1, 2023, 2:11 p.m. UTC | #1
On Tue, Jan 24 2023, Martin Liška wrote:
> We should not insert an empty value to the container.
>
> Patch can bootstrap on x86_64-linux-gnu and survives regression tests.
>
> Ready to be installed?
> Thanks,
> Martin
>
> 	PR ipa/108509
>
> gcc/ChangeLog:
>
> 	* cgraphunit.cc (walk_polymorphic_call_targets): Insert
> 	ony non-null values.
> 	* ipa.cc (walk_polymorphic_call_targets): Likewise.

This is OK.

Thanks a lot,

Martin


>
> gcc/testsuite/ChangeLog:
>
> 	* g++.dg/ipa/pr108509.C: New test.
> ---
>  gcc/cgraphunit.cc                   |  2 +-
>  gcc/ipa.cc                          |  2 +-
>  gcc/testsuite/g++.dg/ipa/pr108509.C | 22 ++++++++++++++++++++++
>  3 files changed, 24 insertions(+), 2 deletions(-)
>  create mode 100644 gcc/testsuite/g++.dg/ipa/pr108509.C
>
> diff --git a/gcc/cgraphunit.cc b/gcc/cgraphunit.cc
> index 832818d651f..a972900900b 100644
> --- a/gcc/cgraphunit.cc
> +++ b/gcc/cgraphunit.cc
> @@ -1000,7 +1000,7 @@ walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
>      = possible_polymorphic_call_targets
>  	(edge, &final, &cache_token);
>  
> -  if (!reachable_call_targets->add (cache_token))
> +  if (cache_token != NULL && !reachable_call_targets->add (cache_token))
>      {
>        if (symtab->dump_file)
>  	dump_possible_polymorphic_call_targets 
> diff --git a/gcc/ipa.cc b/gcc/ipa.cc
> index 4de605000b6..5c15b60a603 100644
> --- a/gcc/ipa.cc
> +++ b/gcc/ipa.cc
> @@ -182,7 +182,7 @@ walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
>      = possible_polymorphic_call_targets
>  	(edge, &final, &cache_token);
>  
> -  if (!reachable_call_targets->add (cache_token))
> +  if (cache_token != NULL && !reachable_call_targets->add (cache_token))
>      {
>        for (i = 0; i < targets.length (); i++)
>  	{
> diff --git a/gcc/testsuite/g++.dg/ipa/pr108509.C b/gcc/testsuite/g++.dg/ipa/pr108509.C
> new file mode 100644
> index 00000000000..2844189bbb4
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/ipa/pr108509.C
> @@ -0,0 +1,22 @@
> +// PR ipa/108509
> +// { dg-do compile }
> +// { dg-options "-O1 -fdevirtualize -fno-tree-fre" }
> +
> +struct B {
> +  virtual void deref ();
> +};
> +
> +struct RefPtr {
> +  B *p;
> +
> +  RefPtr ()
> +  {
> +    p->deref ();
> +  }
> +};
> +
> +void
> +f ()
> +{
> +  RefPtr b;
> +}
> -- 
> 2.39.0
  

Patch

diff --git a/gcc/cgraphunit.cc b/gcc/cgraphunit.cc
index 832818d651f..a972900900b 100644
--- a/gcc/cgraphunit.cc
+++ b/gcc/cgraphunit.cc
@@ -1000,7 +1000,7 @@  walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
     = possible_polymorphic_call_targets
 	(edge, &final, &cache_token);
 
-  if (!reachable_call_targets->add (cache_token))
+  if (cache_token != NULL && !reachable_call_targets->add (cache_token))
     {
       if (symtab->dump_file)
 	dump_possible_polymorphic_call_targets 
diff --git a/gcc/ipa.cc b/gcc/ipa.cc
index 4de605000b6..5c15b60a603 100644
--- a/gcc/ipa.cc
+++ b/gcc/ipa.cc
@@ -182,7 +182,7 @@  walk_polymorphic_call_targets (hash_set<void *> *reachable_call_targets,
     = possible_polymorphic_call_targets
 	(edge, &final, &cache_token);
 
-  if (!reachable_call_targets->add (cache_token))
+  if (cache_token != NULL && !reachable_call_targets->add (cache_token))
     {
       for (i = 0; i < targets.length (); i++)
 	{
diff --git a/gcc/testsuite/g++.dg/ipa/pr108509.C b/gcc/testsuite/g++.dg/ipa/pr108509.C
new file mode 100644
index 00000000000..2844189bbb4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/ipa/pr108509.C
@@ -0,0 +1,22 @@ 
+// PR ipa/108509
+// { dg-do compile }
+// { dg-options "-O1 -fdevirtualize -fno-tree-fre" }
+
+struct B {
+  virtual void deref ();
+};
+
+struct RefPtr {
+  B *p;
+
+  RefPtr ()
+  {
+    p->deref ();
+  }
+};
+
+void
+f ()
+{
+  RefPtr b;
+}