Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weaken xor to or under nand #58624

Open
scottmcm opened this issue Oct 26, 2022 · 4 comments
Open

Weaken xor to or under nand #58624

scottmcm opened this issue Oct 26, 2022 · 4 comments

Comments

@scottmcm
Copy link

Alive2 proof: https://alive2.llvm.org/ce/z/tMGuKE

define i1 @src(i1 %x, i1 %y) denormal-fp-math=ieee,ieee {
%start:
  %_4 = and i1 %x, %y
  %_3 = xor i1 %_4, 1
  assume i1 %_3 ; <-- because we know !(X & Y)
  %0 = xor i1 %x, %y ; <-- this X^Y
  ret i1 %0
}
=>
define i1 @tgt(i1 %x, i1 %y) denormal-fp-math=ieee,ieee {
%start:
  %_4 = and i1 %x, %y
  %_3 = xor i1 %_4, 1
  assume i1 %_3
  %0 = or i1 %x, %y ; <-- can be just X|Y
  ret i1 %0
}
Transformation seems to be correct!

It's also possible that, for normalization, this shouldn't always happen, so here's my non-minimized example: https://alive2.llvm.org/ce/z/_c36CA

In that one, replacing the xor with an or opened up a bunch of additional possibilities that ended up making a huge improvement in the generated assembly:

        test    edi, edi
        je      .LBB0_3
        test    esi, esi
        je      .LBB0_3
        cmp     edi, esi
        sete    al
        ret
 .LBB0_3:                                # %other
-       test    esi, esi
-       setne   cl
-       test    edi, edi
-       setne   al
-       xor     al, cl
-       xor     al, 1
+       or      edi, esi
+       sete    al
        ret
@RKSimon
Copy link
Collaborator

RKSimon commented Oct 26, 2022

Possibly a IR and DAG optimization candidate

@bcl5980
Copy link
Contributor

bcl5980 commented Oct 28, 2022

Maybe this pattern is more general.
https://alive2.llvm.org/ce/z/oag_NH

@RKSimon
Copy link
Collaborator

RKSimon commented Oct 28, 2022

DAG was missing this fold, but instcombine does already perform it.

What seems to be missing is any assumption cache handling inside llvm::haveNoCommonBitsSet

@RKSimon
Copy link
Collaborator

RKSimon commented Oct 29, 2022

Candidate Patch: https://reviews.llvm.org/D137021

virnarula pushed a commit to virnarula/llvm-project that referenced this issue Nov 2, 2022
@RKSimon RKSimon removed their assignment Nov 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants