Bug 27781 - [4.1 Regression] weak-attribute over-optimisation
Summary: [4.1 Regression] weak-attribute over-optimisation
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.1.0
: P2 normal
Target Milestone: 4.1.2
Assignee: Richard Biener
URL:
Keywords: wrong-code
: 28055 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-05-28 09:38 UTC by Jan Kiszka
Modified: 2006-06-21 15:36 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-05-28 14:00:23


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jan Kiszka 2006-05-28 09:38:59 UTC
file-a.c:
---------
void __attribute__((weak)) func(void)
{
    /* no code */
}

main()
{
    func();
}


file-b.c:
---------
void func(void)
{
    printf("func\n");
}


# gcc -c file-a.c file-b.c -O2
# gcc -o program file-a.o file-b.o

The program will not print "func", because the call to func() was optimised out during the compilation of file-a.c. This happens with -O2, but not when using -fno-unit-at-a-time. First sighted in 4.1.
Comment 1 Richard Biener 2006-05-28 12:03:59 UTC
ipa-pure-const thinks that func is const, which is wrong.  Then DCE comes along and deletes the function call.
Comment 2 Richard Biener 2006-05-28 14:00:23 UTC
I have a patch.
Comment 3 Andrew Pinski 2006-05-28 15:23:11 UTC
Actually no, you have to use -fPIC to get this not to be optimized, otherwise func will be bound locally which is not what you want.
Comment 4 Tom Rini 2006-06-01 18:36:37 UTC
(In reply to comment #3)
> Actually no, you have to use -fPIC to get this not to be optimized, otherwise
> func will be bound locally which is not what you want.

Two things.  First, that's a change in behavior from how it used to work and I don't recall seeing warnings about going-away behavior (the place this problem is actually manifesting is in code for the Linux Kernel).

Second there is some sort of bug here as if we have:
$ cat file-a.c
void __attribute__((weak)) func(void)
{
        printf("weak\n");
}

main()
{
            func();
}
$ cat file-b.c
void func(void)
{
        printf("func\n");
}
$ gcc-4.1 -c file-a.c file-b.c -O2
file-a.c: In function 'func':
file-a.c:3: warning: incompatible implicit declaration of built-in function 'printf'
file-b.c: In function 'func':
file-b.c:3: warning: incompatible implicit declaration of built-in function 'printf'
$ gcc-4.1 -o program file-a.o file-b.o
$ ./program
func
$ gcc-4.1 -o program file-a.o
$ ./program
weak
$ gcc-4.1 -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c++,java,fortran,objc,obj-c++,ada,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu --enable-libstdcxx-debug --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.1-1.4.2.0/jre --enable-mpfr --enable-checking=release x86_64-linux-gnu
Thread model: posix
gcc version 4.1.0 (Debian 4.1.0-1)
Comment 5 patchapp@dberlin.org 2006-06-15 15:30:13 UTC
Subject: Bug number PR27781

A patch for this bug has been added to the patch tracker.
The mailing list url for the patch is http://gcc.gnu.org/ml/gcc-patches/2006-06/msg00808.html
Comment 6 Richard Biener 2006-06-15 17:23:50 UTC
Subject: Bug 27781

Author: rguenth
Date: Thu Jun 15 17:23:41 2006
New Revision: 114681

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=114681
Log:
2006-06-16  Richard Guenther  <rguenther@suse.de>

	PR tree-optimization/27781
	* Makefile.in (ipa-pure-const.o): Add $(TARGET_H) dependency.
	* ipa-pure-const.c (target.h): Include.
	(analyze_function): Do not analyze functions that do not
        bind locally.

	* gcc.dg/tree-ssa/pr27781.c: New testcase.

Added:
    trunk/gcc/testsuite/gcc.dg/tree-ssa/pr27781.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/Makefile.in
    trunk/gcc/ipa-pure-const.c
    trunk/gcc/testsuite/ChangeLog

Comment 7 Richard Biener 2006-06-15 17:30:42 UTC
Fixed on the mainline.
Comment 8 Andrew Pinski 2006-06-16 17:50:29 UTC
*** Bug 28055 has been marked as a duplicate of this bug. ***
Comment 9 Richard Biener 2006-06-21 15:36:16 UTC
Fixed.
Comment 10 Richard Biener 2006-06-21 15:36:20 UTC
Subject: Bug 27781

Author: rguenth
Date: Wed Jun 21 15:36:10 2006
New Revision: 114852

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=114852
Log:
2006-06-21  Richard Guenther  <rguenther@suse.de>

	PR tree-optimization/27781
	* Makefile.in (ipa-pure-const.o): Add $(TARGET_H) dependency.
	* ipa-pure-const.c (target.h): Include.
	(analyze_function): Do not analyze functions that do not
	bind locally.

	* gcc.dg/tree-ssa/pr27781.c: New testcase.

Added:
    branches/gcc-4_1-branch/gcc/testsuite/gcc.dg/tree-ssa/pr27781.c
      - copied unchanged from r114681, trunk/gcc/testsuite/gcc.dg/tree-ssa/pr27781.c
Modified:
    branches/gcc-4_1-branch/gcc/ChangeLog
    branches/gcc-4_1-branch/gcc/Makefile.in
    branches/gcc-4_1-branch/gcc/ipa-pure-const.c
    branches/gcc-4_1-branch/gcc/testsuite/ChangeLog