public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "rafal at bursig dot org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/99693] -O2 not move 'if' checks on const data outside the loops
Date: Sun, 21 Mar 2021 12:28:17 +0000	[thread overview]
Message-ID: <bug-99693-4-fBivBALfj1@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-99693-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #1 from rafal at bursig dot org ---
I'm not saying that this is a regression because I see proper results on -O3
level but IMHO such results should be available in -O2 level... but to topic:

I have such code:

typedef struct Update {
    int m_update;
    //...
} Update;

extern void antagonizer( Update * );

void antagonize(Update *data, unsigned int n)
{
    while(--n)
    {
        if (data->m_update)
        {
            antagonizer( data );
        }
    }
}

then -O2 level give me such asm:

antagonize:
        sub     esi, 1
        je      .L10
        push    rbp
        mov     rbp, rdi
        push    rbx
        mov     ebx, esi
        sub     rsp, 8
        jmp     .L4
.L3:
        sub     ebx, 1
        je      .L14
.L4:
        mov     eax, DWORD PTR [rbp+0]
        test    eax, eax
        je      .L3
        mov     rdi, rbp
        call    antagonizer
        sub     ebx, 1
        jne     .L4
.L14:
        add     rsp, 8
        pop     rbx
        pop     rbp
        ret
.L10:
        ret


Which is OK because the "antagonizer( data );" call can change "Update *data"
struct and the "if (data->m_update)" can't be moved outside loop.

But when If I create local copy of "data->m_update" then this check could be
moved outside loop without problem. But below code:

typedef struct Update {
    int m_update;
    //...
} Update;

extern void antagonizer( Update * );

void antagonize(Update *data, unsigned int n)
{
    const int _update = data->m_update;

    while(--n)
    {
        if (_update)
        {
            antagonizer( data );
        }
    }
}

Do not move "if (_update)" outside the loop and asm looks like:

antagonize:
        push    r12
        push    rbp
        push    rbx
        mov     ebp, DWORD PTR [rdi]
        sub     esi, 1
        je      .L1
        mov     r12, rdi
        mov     ebx, esi
        jmp     .L4
.L3:
        sub     ebx, 1
        je      .L1
.L4:
        test    ebp, ebp      // the check 
        je      .L3           // is still in loop
        mov     rdi, r12
        call    antagonizer
        sub     ebx, 1
        jne     .L4
.L1:
        pop     rbx
        pop     rbp
        pop     r12
        ret

When I build this code with -O3 level then the check is moved out the loop
properly, but I got several others effects which I don't know if it will have
proper impact in my application (I will prefer stay with -O2). The asm for -O3
level:

antagonize:
        mov     eax, DWORD PTR [rdi]
        sub     esi, 1
        je      .L12
        push    rbp
        mov     rbp, rdi
        push    rbx
        mov     ebx, esi
        sub     rsp, 8
        test    eax, eax
        jne     .L3
.L1:
        add     rsp, 8
        pop     rbx
        pop     rbp
        ret
.L3:
        mov     rdi, rbp
        call    antagonizer
        sub     ebx, 1
        je      .L1
        mov     rdi, rbp
        call    antagonizer
        sub     ebx, 1
        jne     .L3
        jmp     .L1
.L12:
        ret

  reply	other threads:[~2021-03-21 12:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-21 12:07 [Bug c/99693] New: " rafal at bursig dot org
2021-03-21 12:28 ` rafal at bursig dot org [this message]
2021-03-21 16:41 ` [Bug c/99693] " pinskia at gcc dot gnu.org
2021-03-22  9:04 ` rguenth at gcc dot gnu.org
2021-03-22  9:23 ` rafal at bursig dot org
2021-03-22  9:43 ` rafal at bursig dot 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-99693-4-fBivBALfj1@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).