Skip to content

Missed optimization: bitwise OR #43481

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

Closed
zamazan4ik opened this issue Nov 25, 2019 · 7 comments
Closed

Missed optimization: bitwise OR #43481

zamazan4ik opened this issue Nov 25, 2019 · 7 comments
Labels
backend:X86 bugzilla Issues migrated from bugzilla

Comments

@zamazan4ik
Copy link

zamazan4ik commented Nov 25, 2019

Bugzilla Link 44136
Resolution FIXED
Resolved on Apr 12, 2021 05:11
Version trunk
OS Linux
Blocks #49274
CC @topperc,@LebedevRI,@RKSimon,@zygoloid,@rotateright
Fixed by commit(s) 2450369, d8bc4de

Extended Description

For the following code:

bool foo(int A, int B) {
    return (A | B) == A;
}

clang(trunk) with '-O3 -march=haswell' produces:

foo(int, int): # @foo(int, int)
  or esi, edi
  cmp esi, edi
  sete al
  ret

but GCC(trunk) with '-O3 -march=haswell' produces:

foo(int, int):
  andn eax, edi, esi
  sete al
  ret

Godbolt playground: https://godbolt.org/z/MZNdWW

@LebedevRI
Copy link
Member

https://godbolt.org/z/P5-bZ5
https://rise4fun.com/Alive/3d9

Looks like we are missing an undo middle-end canonicalization too?

@RKSimon
Copy link
Collaborator

RKSimon commented Apr 9, 2021

Candidate X86 backend patch: https://reviews.llvm.org/D100177

@RKSimon
Copy link
Collaborator

RKSimon commented Apr 9, 2021

X86 Backend support:

BMI: rG245036950a7a
non-BMI: rGd8bc4de3cfe0

@RKSimon
Copy link
Collaborator

RKSimon commented Apr 9, 2021

The middle-end reverse fold would require a freeze to be valid:

https://alive2.llvm.org/ce/z/PgAUx5

define i1 @src(i8 %a, i8 %b) {
  %n = xor i8 %a, -1
  %o = and i8 %n, %b
  %c = icmp eq i8 %o, 0
  ret i1 %c
}

define i1 @tgt(i8 %a, i8 %b) {
  %aa = freeze i8 %a
  %o = or i8 %aa, %b
  %c = icmp eq i8 %o, %aa
  ret i1 %c
}

@RKSimon
Copy link
Collaborator

RKSimon commented Apr 9, 2021

Candidate middle-end patch: https://reviews.llvm.org/D100211

@RKSimon
Copy link
Collaborator

RKSimon commented Apr 12, 2021

Candidate middle-end patch: https://reviews.llvm.org/D100211

There's some concern about introducing freeze() for this middle-end fold, so this has been abandoned, we can maybe revisit this in the future.

Resolving as there's nothing else we're intending to do anytime soon.

@RKSimon
Copy link
Collaborator

RKSimon commented Nov 27, 2021

mentioned in issue #49274

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 10, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:X86 bugzilla Issues migrated from bugzilla
Projects
None yet
Development

No branches or pull requests

3 participants