ICF: properly handle LABEL_DECLs (PR tree-opt/81696).

Message ID 7d571cd7-7e8c-f283-91b3-4062eed47cdf@suse.cz
State New
Headers show

Commit Message

Martin Liška Aug. 8, 2017, 11:11 a.m.
Hello.

As LABEL_DECL can point to another function (non-local goto), we must properly
compare them.

Patch can bootstrap on ppc64le-redhat-linux and survives regression tests.

Ready to be installed ?
Martin

gcc/ChangeLog:

2017-08-08  Martin Liska  <mliska@suse.cz>

	PR tree-opt/81696
	* ipa-icf-gimple.c (func_checker::compare_cst_or_decl): Consider
	LABEL_DECLs that can be from a different function.

gcc/testsuite/ChangeLog:

2017-08-08  Martin Liska  <mliska@suse.cz>

	PR tree-opt/81696
	* gcc.dg/ipa/pr81696.c: New test.
---
 gcc/ipa-icf-gimple.c               |  6 +++++-
 gcc/testsuite/gcc.dg/ipa/pr81696.c | 26 ++++++++++++++++++++++++++
 2 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/ipa/pr81696.c

Comments

Richard Biener Aug. 8, 2017, 11:17 a.m. | #1
On Tue, Aug 8, 2017 at 1:11 PM, Martin Liška <mliska@suse.cz> wrote:
> Hello.
>
> As LABEL_DECL can point to another function (non-local goto), we must properly
> compare them.
>
> Patch can bootstrap on ppc64le-redhat-linux and survives regression tests.
>
> Ready to be installed ?

Ok.

So with bb1 == bb2 == NULL could you equate them in case the DECL_CONTEXT
functions were merged by ICF (and the labels)?  (I guess it's not
worth the trouble)

Thanks,
Richard.

> Martin
>
> gcc/ChangeLog:
>
> 2017-08-08  Martin Liska  <mliska@suse.cz>
>
>         PR tree-opt/81696
>         * ipa-icf-gimple.c (func_checker::compare_cst_or_decl): Consider
>         LABEL_DECLs that can be from a different function.
>
> gcc/testsuite/ChangeLog:
>
> 2017-08-08  Martin Liska  <mliska@suse.cz>
>
>         PR tree-opt/81696
>         * gcc.dg/ipa/pr81696.c: New test.
> ---
>  gcc/ipa-icf-gimple.c               |  6 +++++-
>  gcc/testsuite/gcc.dg/ipa/pr81696.c | 26 ++++++++++++++++++++++++++
>  2 files changed, 31 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/gcc.dg/ipa/pr81696.c
>
>
Martin Liška Aug. 8, 2017, 11:55 a.m. | #2
On 08/08/2017 01:17 PM, Richard Biener wrote:
> On Tue, Aug 8, 2017 at 1:11 PM, Martin Liška <mliska@suse.cz> wrote:
>> Hello.
>>
>> As LABEL_DECL can point to another function (non-local goto), we must properly
>> compare them.
>>
>> Patch can bootstrap on ppc64le-redhat-linux and survives regression tests.
>>
>> Ready to be installed ?
> 
> Ok.

Thanks.

> 
> So with bb1 == bb2 == NULL could you equate them in case the DECL_CONTEXT
> functions were merged by ICF (and the labels)?  (I guess it's not
> worth the trouble)

No, that would be very rare case.

Martin

> 
> Thanks,
> Richard.
> 
>> Martin
>>
>> gcc/ChangeLog:
>>
>> 2017-08-08  Martin Liska  <mliska@suse.cz>
>>
>>         PR tree-opt/81696
>>         * ipa-icf-gimple.c (func_checker::compare_cst_or_decl): Consider
>>         LABEL_DECLs that can be from a different function.
>>
>> gcc/testsuite/ChangeLog:
>>
>> 2017-08-08  Martin Liska  <mliska@suse.cz>
>>
>>         PR tree-opt/81696
>>         * gcc.dg/ipa/pr81696.c: New test.
>> ---
>>  gcc/ipa-icf-gimple.c               |  6 +++++-
>>  gcc/testsuite/gcc.dg/ipa/pr81696.c | 26 ++++++++++++++++++++++++++
>>  2 files changed, 31 insertions(+), 1 deletion(-)
>>  create mode 100644 gcc/testsuite/gcc.dg/ipa/pr81696.c
>>
>>

Patch

diff --git a/gcc/ipa-icf-gimple.c b/gcc/ipa-icf-gimple.c
index 08dd980fdf3..f44a995f580 100644
--- a/gcc/ipa-icf-gimple.c
+++ b/gcc/ipa-icf-gimple.c
@@ -361,10 +361,14 @@  func_checker::compare_cst_or_decl (tree t1, tree t2)
       }
     case LABEL_DECL:
       {
+	if (t1 == t2)
+	  return true;
+
 	int *bb1 = m_label_bb_map.get (t1);
 	int *bb2 = m_label_bb_map.get (t2);
 
-	return return_with_debug (*bb1 == *bb2);
+	/* Labels can point to another function (non-local GOTOs).  */
+	return return_with_debug (bb1 != NULL && bb2 != NULL && *bb1 == *bb2);
       }
     case PARM_DECL:
     case RESULT_DECL:
diff --git a/gcc/testsuite/gcc.dg/ipa/pr81696.c b/gcc/testsuite/gcc.dg/ipa/pr81696.c
new file mode 100644
index 00000000000..2d3d63ff0bb
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ipa/pr81696.c
@@ -0,0 +1,26 @@ 
+/* { dg-options "-O2 -fdump-ipa-icf-details"  } */
+
+int
+main (int argc, char **argv)
+{
+  __label__ lab4, lab5, lab6;
+
+  void foo (void) { goto lab4; }
+  void foo2 (void) { goto lab4; }
+  void bar (void) { goto lab5; }
+  void baz (void) { goto lab6; }
+
+  if (argc)
+    foo ();
+  else
+    foo2 ();
+
+ lab4:;
+  bar ();
+ lab5:;
+  baz ();
+ lab6:;
+  return 0;
+}
+
+/* { dg-final { scan-ipa-dump "Equal symbols: 1" "icf"  } } */