https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64960
Bug ID: 64960 Summary: Inefficient address pre-computation in PIC mode Product: gcc Version: 5.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: enkovich.gnu at gmail dot com After EBX was unfixed in i386 PIC target, we may see addresses of static objects are loaded from GOT and placed to the stack for later usage. It allows to reuse PIC register for other purposes. But in cases when PIC register is still used (e.g. for calls) it may cause inefficiency in produced code. Here is an example: >cat test.c void f (int); int val1, *val2, val3; int test (int max) { int i; for (i = 0; i < max; i++) { val1 += val2[i]; f (val3); } } >gcc test.c -O2 -fPIE -S -m32 -ffixed-esi -ffixed-edi -ffixed-edx >cat test.s ... movl val1@GOT(%ebx), %eax <-- may be removed xorl %ebp, %ebp movl %eax, 4(%esp) <-- may be removed movl val2@GOT(%ebx), %eax <-- may be removed movl %eax, 8(%esp) <-- may be removed movl val3@GOT(%ebx), %eax <-- may be removed movl %eax, 12(%esp) <-- may be removed .L3: movl 8(%esp), %eax <-- equal to movl val2@GOT(%ebx), %eax subl $12, %esp movl (%eax), %ecx movl 16(%esp), %eax <-- equal to movl val1@GOT(%ebx), %eax movl (%ecx,%ebp,4), %ecx addl %ecx, (%eax) addl $1, %ebp movl 24(%esp), %eax <-- equal to movl val3@GOT(%ebx), %eax pushl (%eax) call f@PLT addl $16, %esp cmpl %ebp, 32(%esp) jne .L3 ... Also storing value on the stack doesn't benefit on static objects optimization performed by linker which transforms movl <symbol>@GOT<PIC> into lea instruction. It would be useful to avoid early address computation in case PIC register is available at address usage. Here is a code generated by GCC 4.9: xorl %ebp, %ebp .L2: movl val2@GOT(%ebx), %eax subl $12, %esp movl (%eax), %ecx movl val1@GOT(%ebx), %eax movl (%ecx,%ebp,4), %ecx addl %ecx, (%eax) addl $1, %ebp movl val3@GOT(%ebx), %eax pushl (%eax) call f@PLT addl $16, %esp cmpl 16(%esp), %ebp jne .L2 Used gcc (GCC) 5.0.0 20150205 (experimental).