public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "enkovich.gnu at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug rtl-optimization/64960] New: Inefficient address pre-computation in PIC mode
Date: Fri, 06 Feb 2015 13:56:00 -0000	[thread overview]
Message-ID: <bug-64960-4@http.gcc.gnu.org/bugzilla/> (raw)

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


             reply	other threads:[~2015-02-06 13:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-06 13:56 enkovich.gnu at gmail dot com [this message]
2021-09-27  1:55 ` [Bug target/64960] [5 Regression] " pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-64960-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).