public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "nekotekina at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/103932] New: x86: strange unoptimized code generated (multiple negations of _mm_testz_si128 result)
Date: Thu, 06 Jan 2022 19:11:03 +0000	[thread overview]
Message-ID: <bug-103932-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 103932
           Summary: x86: strange unoptimized code generated (multiple
                    negations of _mm_testz_si128 result)
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nekotekina at gmail dot com
  Target Milestone: ---

GCC generates seemingly unoptimized sequence of instructions in certain cases
(can't tell exactly what triggers it, example code is below):

        xor     eax, eax
        vptest  xmm0, xmm0
        sete    al
        test    eax, eax
        sete    al
        movzx   eax, al

This should be something like this:
xor eax, eax
vptest xmm0, xmm0
setne al


https://godbolt.org/z/sTaG65Ksc
Code (-O3 -std=c++20 -march=skylake):

#include <emmintrin.h>
#include <immintrin.h>
#include <bit>
#include <cstdint>

template <typename T>
concept Vector128 = (sizeof(T) == 16);

using u64 = std::uint64_t;
using u32 = std::uint32_t;

union alignas(16) v128
{
        u64 _u64[2];

        v128() = default;

        constexpr v128(const v128&) noexcept = default;

        template <Vector128 T>
        constexpr v128(const T& rhs) noexcept
                : v128(std::bit_cast<v128>(rhs))
        {
        }

        constexpr v128& operator=(const v128&) noexcept = default;

        template <Vector128 T>
        constexpr operator T() const noexcept
        {
                return std::bit_cast<T>(*this);
        }
};

// Test if vector is zero
inline bool gv_testz(const v128& arg)
{
#if defined(__SSE4_1__)
        return _mm_testz_si128(arg, arg);
#else
        return !(arg._u64[0] | arg._u64[1]);
#endif
}

struct alignas(16) context_t
{
        v128 vec[32];
        v128 sat;
};

void test1(context_t& ctx, u32 n)
{
        const u64 bit = !gv_testz(ctx.sat);
        v128 r;
        r._u64[0] = 0;
        r._u64[1] = bit;
        ctx.vec[n] = r;
}

void test2(context_t& ctx, u32 n)
{
        ctx.vec[n]._u64[1] = !gv_testz(ctx.sat);
}

             reply	other threads:[~2022-01-06 19:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-06 19:11 nekotekina at gmail dot com [this message]
2022-01-06 21:23 ` [Bug target/103932] " pinskia at gcc dot gnu.org
2022-01-06 21:25 ` 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-103932-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).