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

[InstCombine] failed to use popcount range metadata #51436

Open
rotateright opened this issue Oct 6, 2021 · 1 comment
Open

[InstCombine] failed to use popcount range metadata #51436

rotateright opened this issue Oct 6, 2021 · 1 comment
Labels
bugzilla Issues migrated from bugzilla

Comments

@rotateright
Copy link
Contributor

rotateright commented Oct 6, 2021

Bugzilla Link 52094
Version trunk
OS All
CC @RKSimon

Extended Description

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

define i32 @src(i32 %0) {
  %2 = and i32 %0, 15
  %3 = tail call i32 @llvm.ctpop.i32(i32 %2)
  %4 = and i32 %3, 5
  %5 = icmp eq i32 %4, 1
  %6 = zext i1 %5 to i32
  ret i32 %6
}

define i32 @tgt(i32 %0) {
  %2 = and i32 %0, 15
  %3 = tail call i32 @llvm.ctpop.i32(i32 %2)
  %4 = and i32 %3, 1
  ret i32 %4
}
declare i32 @llvm.ctpop.i32(i32) #2

We fail to recognize this as a parity check which could lead to far worse codegen (especially for a target that does not have a popcount instruction):

IR corresponds to source like this:

int xor4_popcount(int num) {
    int masknum = num & 0xf;
    int pop = __builtin_popcount(masknum);
    return pop == 1 || pop == 3; 
}

This example is derived from the post-commit discussion in:
https://reviews.llvm.org/D110170

@rotateright
Copy link
Contributor Author

When compiling from source currently, we do have the optimal range metadata:

%0 = tail call i32 @​llvm.ctpop.i32(i32 %and), !range !​6

!​6 = !{i32 0, i32 5}

And that is added by instcombine via foldCtpop(), so that part of the problem is working correctly already.

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

No branches or pull requests

1 participant