public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "ktkachov at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/105793] New: Missed vectorisation with conditional-select inside loop
Date: Tue, 31 May 2022 14:55:20 +0000	[thread overview]
Message-ID: <bug-105793-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 105793
           Summary: Missed vectorisation with conditional-select inside
                    loop
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ktkachov at gcc dot gnu.org
  Target Milestone: ---

The code:
#define N 1024

float f(const float in[N], unsigned int n) {
    float a = 0.0f;

    for (unsigned  i = 0; i < N; ++i) {
        float b = in[i];
        if (b < 10.f)
            a += b;
        else
            a -= b;
    }

    return a;
}

with -Ofast does not vectorise (on aarch64, for example):
f:
        movi    v0.2s, #0
        add     x1, x0, 4096
        fmov    s3, 1.0e+1
.L5:
        ldr     s1, [x0], 4
        fsub    s2, s0, s1
        fcmpe   s1, s3
        fadd    s0, s0, s1
        fcsel   s0, s0, s2, mi
        cmp     x1, x0
        bne     .L5
        ret

whereas clang can and does. Commenting out the "else a -=b;" line allows GCC to
vectorise it:
f:
        movi    v0.4s, 0
        add     x1, x0, 4096
        fmov    v3.4s, 1.0e+1
.L2:
        ldr     q2, [x0], 16
        fcmgt   v1.4s, v3.4s, v2.4s
        and     v1.16b, v1.16b, v2.16b
        fadd    v0.4s, v0.4s, v1.4s
        cmp     x1, x0
        bne     .L2
        faddp   v0.4s, v0.4s, v0.4s
        faddp   v0.4s, v0.4s, v0.4s
        ret

Examples at https://gcc.godbolt.org/z/qbn6T73qE

             reply	other threads:[~2022-05-31 14:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-31 14:55 ktkachov at gcc dot gnu.org [this message]
2022-05-31 14:59 ` [Bug tree-optimization/105793] " pinskia at gcc dot gnu.org
2022-06-01  5:59 ` crazylht at gmail dot com
2022-06-01 12:09 ` rguenth 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-105793-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).