public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "pinskia at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug rtl-optimization/86352] setc/movzx introduced into loop to provide a constant 0 value for a later rep stos
Date: Sun, 29 Aug 2021 01:49:31 +0000	[thread overview]
Message-ID: <bug-86352-4-pZpsaZ2O17@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-86352-4@http.gcc.gnu.org/bugzilla/>

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
   Last reconfirmed|                            |2021-08-29
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note GCC does too good of a job and removes the zeroing of the return value as
it is not used; it actually removes the return value fully :).

Here is a new testcase which does not cause the removal of the zeroing.
    using u64 = unsigned long long;

    struct Bucket {
        u64 mLeaves[16] = {};
    };

    struct BucketMap {
        u64 acquire() noexcept {
            while (true) {
                u64 map = mData;

                u64 index = (map & 1) ? 1 : 0;
                auto mask = u64(1) << index;

                auto previous =
                    __atomic_fetch_or(&mData, mask, __ATOMIC_SEQ_CST);
                if ((previous & mask) == 0) {
                    return index;
                }
            }
        }

        __attribute__((noinline)) Bucket acquireBucket() noexcept {
            acquire();
            return Bucket();
        }

        volatile u64 mData = 1;
    };

    int main() {
        BucketMap map;
        Bucket t = map.acquireBucket();
        return t.mLeaves[0];
    }

With the trunk we get:
BucketMap::acquireBucket():
.LFB1:
        .cfi_startproc
        movq    %rdi, %r8
        movq    %rsi, %rcx
        .p2align 4,,10
        .p2align 3
.L2:
        movq    (%rsi), %rdx
        xorl    %eax, %eax
        andl    $1, %edx
        lock btsq       %rdx, (%rcx)
        setc    %al
        jc      .L2
        movq    %r8, %rdi
        movl    $16, %ecx
        rep stosq
        movq    %r8, %rax
        ret

So the setc is useless overall really.
The reason why it is still there is because it does not become useless until
after combine and the dce for RTL runs right before combine.

Trying 14, 17 -> 18:
   14: r93:DI=flags:CCC==0
      REG_DEAD flags:CCC
   17: flags:CCZ=cmp(r93:DI,0)
   18: pc={(flags:CCZ!=0)?L16:pc}
      REG_DEAD flags:CCZ
      REG_BR_PROB 955630228
Failed to match this instruction:
(parallel [
        (set (pc)
            (if_then_else (eq (reg:CCC 17 flags)
                    (const_int 0 [0]))
                (label_ref:DI 16)
                (pc)))
        (set (reg:DI 93)
            (eq:DI (reg:CCC 17 flags)
                (const_int 0 [0])))
    ])
Failed to match this instruction:
(parallel [
        (set (pc)
            (if_then_else (eq (reg:CCC 17 flags)
                    (const_int 0 [0]))
                (label_ref:DI 16)
                (pc)))
        (set (reg:DI 93)
            (eq:DI (reg:CCC 17 flags)
                (const_int 0 [0])))
    ])
Successfully matched this instruction:
(set (reg:DI 93)
    (eq:DI (reg:CCC 17 flags)
        (const_int 0 [0])))
Successfully matched this instruction:
(set (pc)
    (if_then_else (eq (reg:CCC 17 flags)
            (const_int 0 [0]))
        (label_ref:DI 16)
        (pc)))
allowing combination of insns 14, 17 and 18
original costs 4 + 4 + 12 = 20
replacement costs 4 + 12 = 16
deferring deletion of insn with uid = 14.
modifying insn i2    17: r93:DI=flags:CCC==0
deferring rescan insn with uid = 17.
modifying insn i3    18: pc={(flags:CCC==0)?L16:pc}
      REG_BR_PROB 955630228
      REG_DEAD flags:CCZ
deferring rescan insn with uid = 18.

The reason reg 93 was not REG_DEAD after if statement is because cse and/or
forwprop (and maybe even gcse) cames around and decides to that it r93 should
be reused for 0 outside of the loop.
Maybe if frowprop could do better with set/cmp/if in the first place, this
might not have happened ...
Just some good things leading to bad code and too much interactions to count
here.

Also as I said the original testcase GCC now optimizes better anyways (better
than LLVM even).

       reply	other threads:[~2021-08-29  1:49 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <bug-86352-4@http.gcc.gnu.org/bugzilla/>
2021-08-29  1:49 ` pinskia at gcc dot gnu.org [this message]
2021-08-29  1:56 ` 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-86352-4-pZpsaZ2O17@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).