public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug lto/64827] New: LTO doesn't propogate/optimise initialised global variable value passed by reference
@ 2015-01-27 23:06 justynbutlerspam at gmail dot com
  2015-01-28 10:00 ` [Bug ipa/64827] " rguenth at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: justynbutlerspam at gmail dot com @ 2015-01-27 23:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64827

            Bug ID: 64827
           Summary: LTO doesn't propogate/optimise initialised global
                    variable value passed by reference
           Product: gcc
           Version: 4.9.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: lto
          Assignee: unassigned at gcc dot gnu.org
          Reporter: justynbutlerspam at gmail dot com

Sorry if this is already well-known or I'm misinformed, but I couldn't find any
reference to it.

Consider the following code:

#include <stdio.h>

int doublefunc (int *input) {
    return *input *2;
}

int foo = 3;

int main(void) {
    if ( doublefunc(&foo) == 2 )
        printf( "operation result was 2" );
}

Compiled with:
gcc -ffunction-sections -fdata-sections -O3 -flto -Xlinker --gc-sections -O3
-flto -o main main.c

With the doublefunc defined in the same translation unit as above, the compiler
correctly removes the printf statement as dead code (unless foo is initialised
as 1).

But if the function is put into a separate file and compiled with the same
options (including -flto), the optimisation doesn't happen. This seems to me
like an LTO failure.

I've found that in several situations the optimisation will successfully occur
even when the function is in a separate unit:
* if the global variable is initialised with const
* if the value of the variable is set inside main
* if a second (global or otherwise) variable is assigned the value of the first
variable inside main and that is passed to doublefunc

The last one makes it appear particularly strange, since using the following
(with doublefunc in a separate file) will lead the compiler to optimise
correctly:

int foo = 3;
int bar;

int main(void) {
    bar=foo;
    if ( doublefunc(&bar) == 2 )
        printf( "operation result was 2" );
}

The problem only seems to affect variables passed by reference, the
optimisation works if the variable is passed by value instead.

I've tested this with gcc 4.8.2 and also arm-none-eabi-gcc 4.9.3 on Ubuntu
14.04 64-bit.

Am I missing something fundamental or is it a bug?


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug ipa/64827] LTO doesn't propogate/optimise initialised global variable value passed by reference
  2015-01-27 23:06 [Bug lto/64827] New: LTO doesn't propogate/optimise initialised global variable value passed by reference justynbutlerspam at gmail dot com
@ 2015-01-28 10:00 ` rguenth at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: rguenth at gcc dot gnu.org @ 2015-01-28 10:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64827

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2015-01-28
                 CC|                            |hubicka at gcc dot gnu.org,
                   |                            |jamborm at gcc dot gnu.org
          Component|lto                         |ipa
     Ever confirmed|0                           |1

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
It is a non-trivial transformation ;)  First 'foo' has to be promoted to a
constant.  It's not written to - but it has its address taken, so to prove
that is non-trivial (and not done IIRC).  Then we have to apply IPA-CP.

I guess it's IPA references fault in some way.

Btw, on trunk we inline the function but fail to constant fold from the
initializer.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-01-28 10:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-27 23:06 [Bug lto/64827] New: LTO doesn't propogate/optimise initialised global variable value passed by reference justynbutlerspam at gmail dot com
2015-01-28 10:00 ` [Bug ipa/64827] " rguenth at gcc dot gnu.org

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).