Bug 101989 - Fail to optimize (a & b) | (c & ~b) to vpternlog instruction.
Summary: Fail to optimize (a & b) | (c & ~b) to vpternlog instruction.
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 12.0
: P3 enhancement
Target Milestone: 12.0
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
: 93768 (view as bug list)
Depends on:
Blocks:
 
Reported: 2021-08-20 01:46 UTC by Hongtao.liu
Modified: 2022-09-25 19:35 UTC (History)
2 users (show)

See Also:
Host:
Target: x86_64-*-* i?86-*-*
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-08-20 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hongtao.liu 2021-08-20 01:46:13 UTC
cat test.c

__m128i foo (__m128i src1, __m128i src2, __m128i src3)
{
  return (src2 & ~src1) | (src3 & src1);
}

__m128i foo1 (__m128i src1, __m128i src2, __m128i src3)
{
  return (src2 & src1) | (src3 & ~src1);
}

gcc -O2 -march=cascadelake  generates

foo(long long __vector(2), long long __vector(2), long long __vector(2)):
        vpxor   %xmm2, %xmm1, %xmm2
        vpand   %xmm0, %xmm2, %xmm2
        vpxor   %xmm1, %xmm2, %xmm0
        ret
foo1(long long __vector(2), long long __vector(2), long long __vector(2)):
        vpxor   %xmm2, %xmm1, %xmm1
        vpand   %xmm0, %xmm1, %xmm1
        vpxor   %xmm2, %xmm1, %xmm0
        ret


icx generates

foo(long long __vector(2), long long __vector(2), long long __vector(2)):                        # 
        vpternlogq      xmm0, xmm2, xmm1, 202
        ret
foo1(long long __vector(2), long long __vector(2), long long __vector(2)):                       # 
        vpternlogq      xmm0, xmm1, xmm2, 202
        ret

Guess we need a post_reload splitter to match

Failed to match this instruction:
(set (reg:V2DI 88)
    (xor:V2DI (and:V2DI (xor:V2DI (reg:V2DI 92)
                (reg/v:V2DI 87 [ src3 ]))
            (reg:V2DI 91))
        (reg/v:V2DI 87 [ src3 ])))
Failed to match this instruction:
Comment 1 Hongtao.liu 2021-08-20 01:50:09 UTC
Another testcase is from pr91796

    __m256d copysign2_pd(__m256d from, __m256d to) {
        auto a = _mm256_castpd_si256(from);
        auto avx_signbit = _mm256_castsi256_pd(_mm256_slli_epi64(_mm256_cmpeq_epi64(a, a), 63));
        return _mm256_or_pd(_mm256_and_pd(avx_signbit, from), _mm256_andnot_pd(avx_signbit, to)); // (avx_signbit & from) | (~avx_signbit & to)
    }


Failed to match this instruction:
(set (reg:V4DF 93)
    (ior:V4DF (and:V4DF (reg:V4DF 89)
            (reg:V4DF 95))
        (and:V4DF (not:V4DF (reg:V4DF 89))
            (reg:V4DF 96))))
Failed to match this instruction:
Comment 2 Drea Pinski 2021-08-20 02:00:27 UTC
Confirmed.  You might be able to handle it with a match_dup instead of needing a post_reload splitter to match the the one reg that needs to be the same.
Comment 3 GCC Commits 2021-08-24 09:45:46 UTC
The master branch has been updated by hongtao Liu <liuhongt@gcc.gnu.org>:

https://gcc.gnu.org/g:6ddb30f941a44bd528904558673ab35394565f08

commit r12-3108-g6ddb30f941a44bd528904558673ab35394565f08
Author: liuhongt <hongtao.liu@intel.com>
Date:   Fri Aug 20 15:30:40 2021 +0800

    Optimize (a & b) | (c & ~b) to vpternlog instruction.
    
    Also optimize below 3 forms to vpternlog, op1, op2, op3 are
    register_operand or unary_p as (not reg)
    
    A: (any_logic (any_logic op1 op2) op3)
    B: (any_logic (any_logic op1 op2) (any_logic op3 op4)) op3/op4 should
    be equal to op1/op2
    C: (any_logic (any_logic (any_logic:op1 op2) op3) op4) op3/op4 should
    be equal to op1/op2
    
    gcc/ChangeLog:
    
            PR target/101989
            * config/i386/i386.c (ix86_rtx_costs): Define cost for
            UNSPEC_VTERNLOG.
            * config/i386/i386.h (STRIP_UNARY): New macro.
            * config/i386/predicates.md (reg_or_notreg_operand): New
            predicate.
            * config/i386/sse.md (*<avx512>_vternlog<mode>_all): New define_insn.
            (*<avx512>_vternlog<mode>_1): New pre_reload
            define_insn_and_split.
            (*<avx512>_vternlog<mode>_2): Ditto.
            (*<avx512>_vternlog<mode>_3): Ditto.
            (any_logic1,any_logic2): New code iterator.
            (logic_op): New code attribute.
            (ternlogsuffix): Extend to VNxDF and VNxSF.
    
    gcc/testsuite/ChangeLog:
    
            PR target/101989
            * gcc.target/i386/pr101989-1.c: New test.
            * gcc.target/i386/pr101989-2.c: New test.
            * gcc.target/i386/avx512bw-shiftqihi-constant-1.c: Adjust testcase.
Comment 4 GCC Commits 2021-08-24 10:34:03 UTC
The master branch has been updated by hongtao Liu <liuhongt@gcc.gnu.org>:

https://gcc.gnu.org/g:8da9b4f73c2c878b48f45fa2ed47d8a9edd31262

commit r12-3109-g8da9b4f73c2c878b48f45fa2ed47d8a9edd31262
Author: liuhongt <hongtao.liu@intel.com>
Date:   Tue Aug 24 18:09:33 2021 +0800

    Enable avx512 embedde broadcast for vpternlog.
    
    gcc/ChangeLog:
    
            PR target/101989
            * config/i386/sse.md (<avx512>_vternlog<mode><sd_maskz_name>):
            Enable avx512 embedded broadcast.
            (*<avx512>_vternlog<mode>_all): Ditto.
            (<avx512>_vternlog<mode>_mask): Ditto.
    
    gcc/testsuite/ChangeLog:
    
            PR target/101989
            * gcc.target/i386/pr101989-broadcast-1.c: New test.
Comment 5 Hongtao.liu 2021-08-24 10:35:37 UTC
Fixed in GCC12.
Comment 6 GCC Commits 2021-08-25 01:57:16 UTC
The master branch has been updated by hongtao Liu <liuhongt@gcc.gnu.org>:

https://gcc.gnu.org/g:4f5391dde1a83086b451f7534c815ab1267bb6bc

commit r12-3133-g4f5391dde1a83086b451f7534c815ab1267bb6bc
Author: liuhongt <hongtao.liu@intel.com>
Date:   Wed Aug 25 09:45:25 2021 +0800

    Adjust testcases to avoid new failures brought by r12-3108 when compiled w -march=cascadelake.
    
    gcc/testsuite/ChangeLog:
    
            PR target/101989
            * gcc.target/i386/avx2-shiftqihi-constant-1.c: Add -mno-avx512f.
            * gcc.target/i386/sse2-shiftqihi-constant-1.c: Add -mno-avx
Comment 7 GCC Commits 2021-11-04 08:10:13 UTC
The master branch has been updated by hongtao Liu <liuhongt@gcc.gnu.org>:

https://gcc.gnu.org/g:bc9c8e5f8af08c513a4a4c329c50ba6559ff6d5c

commit r12-4882-gbc9c8e5f8af08c513a4a4c329c50ba6559ff6d5c
Author: liuhongt <hongtao.liu@intel.com>
Date:   Wed Nov 3 16:32:22 2021 +0800

    Extend vternlog define_insn_and_split to memory_operand to enable more optimziation.
    
    gcc/ChangeLog:
    
            PR target/101989
            * config/i386/predicates.md (reg_or_notreg_operand): Rename to ..
            (regmem_or_bitnot_regmem_operand): .. and extend to handle
            memory_operand.
            * config/i386/sse.md (*<avx512>_vpternlog<mode>_1): Force_reg
            the operands which are required to be register_operand.
            (*<avx512>_vpternlog<mode>_2): Ditto.
            (*<avx512>_vpternlog<mode>_3): Ditto.
            (*<avx512>_vternlog<mode>_all): Disallow embeded broadcast for
            vector HFmodes since it's not a real AVX512FP16 instruction.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.target/i386/pr101989-3.c: New test.
Comment 8 ak 2022-09-25 19:35:56 UTC
*** Bug 93768 has been marked as a duplicate of this bug. ***