From aa3987819a5ab392bcb125efe84008e75f99bbfc Mon Sep 17 00:00:00 2001 From: Nathan Sidwell Date: Thu, 17 Dec 2015 21:29:30 +0000 Subject: [PATCH] ipa-icf.c (sem_item_optimizer::merge): Don't pick 'main' as the source function. gcc/ * ipa-icf.c (sem_item_optimizer::merge): Don't pick 'main' as the source function. gcc/testsuite/ * gcc.dg/ipa/ipa-icf-merge-1.c: New. From-SVN: r231787 --- gcc/ChangeLog | 5 ++++ gcc/ipa-icf.c | 12 +++++++--- gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gcc.dg/ipa/ipa-icf-merge-1.c | 27 ++++++++++++++++++++++ 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/ipa/ipa-icf-merge-1.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 82a836d177c..a1f71bdfa86 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2015-12-17 Nathan Sidwell + + * ipa-icf.c (sem_item_optimizer::merge): Don't pick 'main' as the + source function. + 2015-12-17 Sebastian Pop * Makefile.in: Replace ISL with isl. diff --git a/gcc/ipa-icf.c b/gcc/ipa-icf.c index 889a07de697..4538677c860 100644 --- a/gcc/ipa-icf.c +++ b/gcc/ipa-icf.c @@ -3398,14 +3398,20 @@ sem_item_optimizer::merge_classes (unsigned int prev_class_count) if (c->members.length () == 1) continue; - gcc_assert (c->members.length ()); - sem_item *source = c->members[0]; - for (unsigned int j = 1; j < c->members.length (); j++) + if (MAIN_NAME_P (DECL_NAME (source->decl))) + /* If merge via wrappers, picking main as the target can be + problematic. */ + source = c->members[1]; + + for (unsigned int j = 0; j < c->members.length (); j++) { sem_item *alias = c->members[j]; + if (alias == source) + continue; + if (dump_file) { fprintf (dump_file, "Semantic equality hit:%s->%s\n", diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 2cbde3b8552..d5ae2990097 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2015-12-17 Nathan Sidwell + + * gcc.dg/ipa/ipa-icf-merge-1.c: New. + 2015-12-17 David Malcolm * gcc.dg/diagnostic-range-bad-return.c: New test case. diff --git a/gcc/testsuite/gcc.dg/ipa/ipa-icf-merge-1.c b/gcc/testsuite/gcc.dg/ipa/ipa-icf-merge-1.c new file mode 100644 index 00000000000..06958a4d69d --- /dev/null +++ b/gcc/testsuite/gcc.dg/ipa/ipa-icf-merge-1.c @@ -0,0 +1,27 @@ +/* { dg-do compile } */ +/* { dg-additional-options "-O2 -fdump-ipa-icf" } */ + +/* Picking 'main' as a candiate target for equivalent functios is not a + good idea. */ + +int baz (int); + +int foo () +{ + return baz (baz (0)); +} + + +int main () +{ + return baz (baz (0)); +} + +/* Notice the two functions are the same. */ +/* { dg-final { scan-ipa-dump "Semantic equality hit:foo->main" "icf" } } */ + +/* Make sure we don't tail call main. */ +/* { dg-final { scan-ipa-dump-not "= main \\(\\);" "icf" } } */ + +/* Make sure we tail call foo. */ +/* { dg-final { scan-ipa-dump "= foo \\(\\);" "icf" } } */ -- 2.30.2