[Bug target/53617] New: Suboptimal PC-relative addressing code on x86

bugdal at aerifal dot cx gcc-bugzilla@gcc.gnu.org
Sat Jun 9 00:23:00 GMT 2012


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

             Bug #: 53617
           Summary: Suboptimal PC-relative addressing code on x86
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: target
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: bugdal@aerifal.cx


Consider a function of the form:

int *foo()
{
    static int x;
    return &x;
}

When GCC compiles this, the result is similar to the following hand-written
asm:

    call 1f
1:  pop %ecx
    add $_GLOBAL_OFFSET_TABLE_+[.-1b], %ecx
    lea x@GOTOFF(%ecx),%eax
    ret

i.e. it loads the address of the GOT in a register, then computes the
GOT-relative address of the variable. This makes sense when the GOT address
will be needed for more than one address lookup, but it's unnecessarily costly
in the case where it's only used for a single variable, in which case the
following code would be better:

    call 1f
1:  pop %eax
    add $[x-1b], %eax
    ret

The same principle applies to a great deal more code, and of course it works
when __i686.get_pc_thunk.cx or similar is being used, too. Is there any way GCC
could be enhanced to generate this superior code when possible?



More information about the Gcc-bugs mailing list