public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/56446] New: Generate one fewer relocation when calling a checked weakref function
@ 2013-02-25 19:40 thiago at kde dot org
  2013-02-25 19:48 ` [Bug tree-optimization/56446] " pinskia at gcc dot gnu.org
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: thiago at kde dot org @ 2013-02-25 19:40 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56446

             Bug #: 56446
           Summary: Generate one fewer relocation when calling a checked
                    weakref function
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: thiago@kde.org


When you have code like:

static int f() __attribute__((weakref("foo"))); 
void g() 
{ 
    int (*ptr)() = f; 
    if (ptr) 
        ptr(); 
}

which is typical for weakref functions, when compiled in PIC/PIE mode, gcc sees
through the variable and generates:

        cmpq    $0, f@GOTPCREL(%rip)
        je      .L1
        xorl    %eax, %eax
        jmp     f@PLT
.L1:
        ret

That means there will be two GOT entries for the "foo" symbol: one in the
actual GOT and one in the .plt.got (lazily initialised). Since the actual GOT
needs to have the address filled in at load time, there's no gain in lazy
initialisation -- in fact, there's a loss.

GCC could do exactly what the code is suggesting and load the actual address
onto a register and then use it. This would save one relocation, the indirect
PLT jumps and the loss in the lazy resolution.


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

* [Bug tree-optimization/56446] Generate one fewer relocation when calling a checked weakref function
  2013-02-25 19:40 [Bug c/56446] New: Generate one fewer relocation when calling a checked weakref function thiago at kde dot org
@ 2013-02-25 19:48 ` pinskia at gcc dot gnu.org
  2013-02-25 22:22 ` [Bug tree-optimization/56446] [4.6/4.7/4.8 Regression] " steven at gcc dot gnu.org
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pinskia at gcc dot gnu.org @ 2013-02-25 19:48 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56446

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c                           |tree-optimization

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2013-02-25 19:48:18 UTC ---
Basically we have to stop copy-proping for ADDR_EXPR of weak_ref/weak decls.  I
don't think we want to special case them really.


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

* [Bug tree-optimization/56446] [4.6/4.7/4.8 Regression] Generate one fewer relocation when calling a checked weakref function
  2013-02-25 19:40 [Bug c/56446] New: Generate one fewer relocation when calling a checked weakref function thiago at kde dot org
  2013-02-25 19:48 ` [Bug tree-optimization/56446] " pinskia at gcc dot gnu.org
@ 2013-02-25 22:22 ` steven at gcc dot gnu.org
  2013-02-25 22:27 ` thiago at kde dot org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: steven at gcc dot gnu.org @ 2013-02-25 22:22 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56446

Steven Bosscher <steven at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2013-02-25
            Summary|Generate one fewer          |[4.6/4.7/4.8 Regression]
                   |relocation when calling a   |Generate one fewer
                   |checked weakref function    |relocation when calling a
                   |                            |checked weakref function
     Ever Confirmed|0                           |1

--- Comment #2 from Steven Bosscher <steven at gcc dot gnu.org> 2013-02-25 22:22:11 UTC ---
Slightly modified test case to avoid the relatively new weakref attribute:

int f() __attribute__((weak));  /* weakref(("foo")) */
void g() 
{ 
    int (*ptr)() = f; 
    if (ptr) 
        ptr(); 
}

GCC 4.8 r196182 (ignoring cfi stuff) with "-O2 -fPIC":
g:
        cmpq    $0, f@GOTPCREL(%rip)
        je      .L1
        xorl    %eax, %eax
        jmp     f@PLT
.L1:
        ret

GCC 4.1.3, GCC 4.4.7 and GCC 4.6.3 produce the same code as above.


GCC 3.4.6 with "-O2 -fPIC":
g:
    movq    f@GOTPCREL(%rip), %rdx
    testq    %rdx, %rdx
    je    .L1
    xorl    %eax, %eax
    movq    %rdx, %r11
    jmp    *%r11
    .p2align 4,,7
.L1:
    rep ; ret


Arguably GCC4 produces better code, but GCC 3.4.6 re-uses the address
loaded in the register.  I think that is the right thing to do, too.

So, marking as a regression.


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

* [Bug tree-optimization/56446] [4.6/4.7/4.8 Regression] Generate one fewer relocation when calling a checked weakref function
  2013-02-25 19:40 [Bug c/56446] New: Generate one fewer relocation when calling a checked weakref function thiago at kde dot org
  2013-02-25 19:48 ` [Bug tree-optimization/56446] " pinskia at gcc dot gnu.org
  2013-02-25 22:22 ` [Bug tree-optimization/56446] [4.6/4.7/4.8 Regression] " steven at gcc dot gnu.org
@ 2013-02-25 22:27 ` thiago at kde dot org
  2013-02-25 22:28 ` thiago at kde dot org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: thiago at kde dot org @ 2013-02-25 22:27 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56446

--- Comment #3 from Thiago Macieira <thiago at kde dot org> 2013-02-25 22:27:14 UTC ---
This should not be done for non-PIC code. In those, it might be preferable to
make the actual call, as opposed to an indirect jump.

I also wonder what would happen for a call that resolves back into the current
module. In those cases, keeping the indirect call would be unnecessary.
However, it also seems like an edge case to me: why is the symbol weak if it's
part of the module?


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

* [Bug tree-optimization/56446] [4.6/4.7/4.8 Regression] Generate one fewer relocation when calling a checked weakref function
  2013-02-25 19:40 [Bug c/56446] New: Generate one fewer relocation when calling a checked weakref function thiago at kde dot org
                   ` (2 preceding siblings ...)
  2013-02-25 22:27 ` thiago at kde dot org
@ 2013-02-25 22:28 ` thiago at kde dot org
  2013-02-26 10:10 ` rguenth at gcc dot gnu.org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: thiago at kde dot org @ 2013-02-25 22:28 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56446

--- Comment #4 from Thiago Macieira <thiago at kde dot org> 2013-02-25 22:28:07 UTC ---
One more detail: both ICC 13 and Clang 3.0 do the same thing as GCC.


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

* [Bug tree-optimization/56446] [4.6/4.7/4.8 Regression] Generate one fewer relocation when calling a checked weakref function
  2013-02-25 19:40 [Bug c/56446] New: Generate one fewer relocation when calling a checked weakref function thiago at kde dot org
                   ` (3 preceding siblings ...)
  2013-02-25 22:28 ` thiago at kde dot org
@ 2013-02-26 10:10 ` rguenth at gcc dot gnu.org
  2013-02-26 10:11 ` rguenth at gcc dot gnu.org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2013-02-26 10:10 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56446

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> 2013-02-26 10:09:32 UTC ---
I think it's a very special case and generally indirect calls should be avoided
(they are slower, have a cost on the branch target buffer, have an extra
dependency, increase register lifetime, etc.).

If profitable for a target the target should be un-propagated/CSEd late
during RTL optimizations.

Well, just my 2 cents.


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

* [Bug tree-optimization/56446] [4.6/4.7/4.8 Regression] Generate one fewer relocation when calling a checked weakref function
  2013-02-25 19:40 [Bug c/56446] New: Generate one fewer relocation when calling a checked weakref function thiago at kde dot org
                   ` (4 preceding siblings ...)
  2013-02-26 10:10 ` rguenth at gcc dot gnu.org
@ 2013-02-26 10:11 ` rguenth at gcc dot gnu.org
  2013-02-26 10:26 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2013-02-26 10:11 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56446

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |4.6.4
           Severity|enhancement                 |normal

--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> 2013-02-26 10:11:17 UTC ---
Btw, ISTR that on ARM a call to (void *)0 is just ignored ("clever"
optimization
of that conditional call ...).

Overall it's a trade-off between the number of relocations and runtime
performance.


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

* [Bug tree-optimization/56446] [4.6/4.7/4.8 Regression] Generate one fewer relocation when calling a checked weakref function
  2013-02-25 19:40 [Bug c/56446] New: Generate one fewer relocation when calling a checked weakref function thiago at kde dot org
                   ` (5 preceding siblings ...)
  2013-02-26 10:11 ` rguenth at gcc dot gnu.org
@ 2013-02-26 10:26 ` jakub at gcc dot gnu.org
  2013-02-27  9:44 ` [Bug target/56446] " rguenth at gcc dot gnu.org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-02-26 10:26 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56446

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> 2013-02-26 10:26:11 UTC ---
Well, if you know the call will got through PLT, on many targets the PLT
contains indirect call anyway, but you generally don't know that.
foo might be hidden and defined in the same shared library or executable, at
which point this change would be code penalization rather than improvement.

Anyway, I agree with Richard here, we shouldn't avoid the propagation, but if
it is desirable on some target (but see above, I think it is really hard to
figure out whether it is desirable or not from the compiler), then it should be
done later in the RTL, because otherwise we might prevent important
optimizations.

If you know as a programmer that it is beneficial in your case, you can always
add asm ("" : "+r" (ptr)); after the int (*ptr)() = f; line.


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

* [Bug target/56446] [4.6/4.7/4.8 Regression] Generate one fewer relocation when calling a checked weakref function
  2013-02-25 19:40 [Bug c/56446] New: Generate one fewer relocation when calling a checked weakref function thiago at kde dot org
                   ` (6 preceding siblings ...)
  2013-02-26 10:26 ` jakub at gcc dot gnu.org
@ 2013-02-27  9:44 ` rguenth at gcc dot gnu.org
  2013-03-13 19:52 ` [Bug target/56446] " steven at gcc dot gnu.org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2013-02-27  9:44 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56446

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Target|                            |x86_64-*-*
           Priority|P3                          |P2
          Component|tree-optimization           |target


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

* [Bug target/56446] Generate one fewer relocation when calling a checked weakref function
  2013-02-25 19:40 [Bug c/56446] New: Generate one fewer relocation when calling a checked weakref function thiago at kde dot org
                   ` (7 preceding siblings ...)
  2013-02-27  9:44 ` [Bug target/56446] " rguenth at gcc dot gnu.org
@ 2013-03-13 19:52 ` steven at gcc dot gnu.org
  2013-04-12 15:43 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: steven at gcc dot gnu.org @ 2013-03-13 19:52 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56446

Steven Bosscher <steven at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[4.6/4.7/4.8 Regression]    |Generate one fewer
                   |Generate one fewer          |relocation when calling a
                   |relocation when calling a   |checked weakref function
                   |checked weakref function    |
           Severity|normal                      |enhancement

--- Comment #8 from Steven Bosscher <steven at gcc dot gnu.org> 2013-03-13 19:51:52 UTC ---
Given comments #5 and #7, this probably should not be marked as a
regression, but rather as an enhancement.  Is it possible to tell
if the called symbol is in the same module if compiling with LTO?


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

* [Bug target/56446] Generate one fewer relocation when calling a checked weakref function
  2013-02-25 19:40 [Bug c/56446] New: Generate one fewer relocation when calling a checked weakref function thiago at kde dot org
                   ` (8 preceding siblings ...)
  2013-03-13 19:52 ` [Bug target/56446] " steven at gcc dot gnu.org
@ 2013-04-12 15:43 ` jakub at gcc dot gnu.org
  2013-05-23 13:15 ` hubicka at gcc dot gnu.org
  2014-06-12 13:53 ` rguenth at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: jakub at gcc dot gnu.org @ 2013-04-12 15:43 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56446

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|4.6.4                       |4.7.4


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

* [Bug target/56446] Generate one fewer relocation when calling a checked weakref function
  2013-02-25 19:40 [Bug c/56446] New: Generate one fewer relocation when calling a checked weakref function thiago at kde dot org
                   ` (9 preceding siblings ...)
  2013-04-12 15:43 ` jakub at gcc dot gnu.org
@ 2013-05-23 13:15 ` hubicka at gcc dot gnu.org
  2014-06-12 13:53 ` rguenth at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: hubicka at gcc dot gnu.org @ 2013-05-23 13:15 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=56446

Jan Hubicka <hubicka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hubicka at gcc dot gnu.org

--- Comment #9 from Jan Hubicka <hubicka at gcc dot gnu.org> ---
default_binds_local_p should do the job, but it doesn't follow the aliases.
With LTO the weakref target can be brought local and then the weakref can be
translated to normal static alias, but we don't do this at the moment.
I am in progress of making my mind on when this is valid.

Honza


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

* [Bug target/56446] Generate one fewer relocation when calling a checked weakref function
  2013-02-25 19:40 [Bug c/56446] New: Generate one fewer relocation when calling a checked weakref function thiago at kde dot org
                   ` (10 preceding siblings ...)
  2013-05-23 13:15 ` hubicka at gcc dot gnu.org
@ 2014-06-12 13:53 ` rguenth at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2014-06-12 13:53 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|4.7.4                       |---

--- Comment #10 from Richard Biener <rguenth at gcc dot gnu.org> ---
Unsetting target milestone of open non-regression bug from version of branch
being closed.


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

end of thread, other threads:[~2014-06-12 13:53 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-02-25 19:40 [Bug c/56446] New: Generate one fewer relocation when calling a checked weakref function thiago at kde dot org
2013-02-25 19:48 ` [Bug tree-optimization/56446] " pinskia at gcc dot gnu.org
2013-02-25 22:22 ` [Bug tree-optimization/56446] [4.6/4.7/4.8 Regression] " steven at gcc dot gnu.org
2013-02-25 22:27 ` thiago at kde dot org
2013-02-25 22:28 ` thiago at kde dot org
2013-02-26 10:10 ` rguenth at gcc dot gnu.org
2013-02-26 10:11 ` rguenth at gcc dot gnu.org
2013-02-26 10:26 ` jakub at gcc dot gnu.org
2013-02-27  9:44 ` [Bug target/56446] " rguenth at gcc dot gnu.org
2013-03-13 19:52 ` [Bug target/56446] " steven at gcc dot gnu.org
2013-04-12 15:43 ` jakub at gcc dot gnu.org
2013-05-23 13:15 ` hubicka at gcc dot gnu.org
2014-06-12 13:53 ` 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).