[llvm] [X86] Invert (and X, ~(and ~Y, Z)) back into (and X, (or Y, ~Z)) (PR #109215)

Miguel Saldivar via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 19 23:05:57 PDT 2024


Saldivarcher wrote:

@RKSimon I did pretty much what you suggested and get the results we expect:
```console
$ cat to-optimize.ll 
define dso_local i64 @foo(i64 %0, i64 %1, i64 %2, i64 %3) local_unnamed_addr {
Entry:
  %4 = and i64 %2, %1
  %5 = xor i64 %4, -1
  %6 = and i64 %5, %0
  %.not = xor i64 %3, -1
  %7 = or i64 %.not, %2
  %8 = and i64 %6, %7
  ret i64 %8
}

declare void @llvm.dbg.value(metadata, metadata, metadata) #1

define dso_local <16 x i8> @fooVec(<16 x i8> %0, <16 x i8> %1, <16 x i8> %2, <16 x i8> %3) local_unnamed_addr {
Entry:
  %4 = and <16 x i8> %2, %1
  %5 = xor <16 x i8> %4, <i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1>
  %6 = and <16 x i8> %5, %0
  %.not = xor <16 x i8> %3, <i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1, i8 -1>
  %7 = or <16 x i8> %.not, %2
  %8 = and <16 x i8> %6, %7
  ret <16 x i8> %8
}
$ ~/workspace/work/llvm-project.git/build_debug/bin/llc to-optimize.ll -o out.s --x86-asm-syntax=intel
$ cat out.s
        .text
        .intel_syntax noprefix
        .file   "to-optimize.ll"
        .globl  foo                             # -- Begin function foo
        .p2align        4, 0x90
        .type   foo, at function
foo:                                    # @foo
        .cfi_startproc
# %bb.0:                                # %Entry
        mov     rax, rcx
        and     rsi, rdx
        not     rsi
        and     rsi, rdi
        not     rax
        or      rax, rdx
        and     rax, rsi
        ret
.Lfunc_end0:
        .size   foo, .Lfunc_end0-foo
        .cfi_endproc
                                        # -- End function
        .globl  fooVec                          # -- Begin function fooVec
        .p2align        4, 0x90
        .type   fooVec, at function
fooVec:                                 # @fooVec
        .cfi_startproc
# %bb.0:                                # %Entry
        andps   xmm1, xmm2
        andnps  xmm1, xmm0
        andnps  xmm2, xmm3
        andnps  xmm2, xmm1
        movaps  xmm0, xmm2
        ret
.Lfunc_end1:
        .size   fooVec, .Lfunc_end1-fooVec
        .cfi_endproc
                                        # -- End function
        .section        ".note.GNU-stack","", at progbits
$ ~/workspace/work/llvm-project.git/build_debug/bin/llc to-optimize.ll -o out.s --x86-asm-syntax=intel -mcpu=znver3
$ cat out.s
        .text
        .intel_syntax noprefix
        .file   "to-optimize.ll"
        .globl  foo                             # -- Begin function foo
        .p2align        4, 0x90
        .type   foo, at function
foo:                                    # @foo
        .cfi_startproc
# %bb.0:                                # %Entry
        and     rsi, rdx
        andn    rcx, rdx, rcx
        andn    rax, rsi, rdi
        andn    rax, rcx, rax
        ret
.Lfunc_end0:
        .size   foo, .Lfunc_end0-foo
        .cfi_endproc
                                        # -- End function
        .globl  fooVec                          # -- Begin function fooVec
        .p2align        4, 0x90
        .type   fooVec, at function
fooVec:                                 # @fooVec
        .cfi_startproc
# %bb.0:                                # %Entry
        vandps  xmm1, xmm2, xmm1
        vandnps xmm2, xmm2, xmm3
        vandnps xmm0, xmm1, xmm0
        vandnps xmm0, xmm2, xmm0
        ret
.Lfunc_end1:
        .size   fooVec, .Lfunc_end1-fooVec
        .cfi_endproc
                                        # -- End function
        .section        ".note.GNU-stack","", at progbits
```


https://github.com/llvm/llvm-project/pull/109215


More information about the llvm-commits mailing list