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] Missed optimization: fold select if the user of its select user indicates the condition #83225

Open
XChy opened this issue Feb 28, 2024 · 1 comment · May be fixed by #83405
Open

Comments

@XChy
Copy link
Member

XChy commented Feb 28, 2024

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

Motivating example

define i32 @src(i32 %a, i1 %c1, i1 %c2){
entry:
  %s1 = select i1 %c1, i32 23, i32 45
  %s2 = select i1 %c2, i32 666, i32 %s1
  %s3 = select i1 %c1, i32 789, i32 %s2
  ret i32 %s3
}

can be folded to:

define i32 @tgt(i32 %a, i1 %c1, i1 %c2){
entry:
  %s2 = select i1 %c2, i32 666, i32 45
  %s3 = select i1 %c1, i32 789, i32 %s2
  ret i32 %s3
}

Real-world motivation

This snippet of IR is derived from libdeflate/lib/crc32.c@dispatch_crc32 (after O3 pipeline, original IR comes from llvm-opt-benchmark).
The example above is a reduced version. If you're interested in the original suboptimal IR and optimal IR, see also:https://godbolt.org/z/rjf9zWM81

Let me know if you can confirm that it's an optimization opportunity, thanks.

vfdff added a commit to vfdff/llvm-project that referenced this issue Feb 29, 2024
Fold select if the user of its select user indicates the condition

Fix llvm#83225
vfdff added a commit to vfdff/llvm-project that referenced this issue Mar 1, 2024
Fold select if the user of its select user indicates the condition

Fix llvm#83225
vfdff added a commit to vfdff/llvm-project that referenced this issue Mar 9, 2024
Fold select if the user of its select user indicates the condition

Fix llvm#83225
@vfdff
Copy link
Contributor

vfdff commented Mar 9, 2024

should also support float type, https://alive2.llvm.org/ce/z/3Uaiis

nikic added a commit to nikic/llvm-project that referenced this issue Mar 18, 2024
The select equivalence fold takes a select like "X == Y ? A : B"
and then tries to simplify A based on the known equality.

This patch also uses it for the case were we have just "C ? A : B"
by treating the condition as either "C == 1" or "C != 1".

This is intended as an alternative to llvm#83405
for fixing llvm#83225.
vfdff added a commit to vfdff/llvm-project that referenced this issue Mar 25, 2024
Fold select if the user of its select user indicates the condition

Fix llvm#83225
vfdff added a commit to vfdff/llvm-project that referenced this issue Mar 27, 2024
Fold select if the user of its select user indicates the condition

Fix llvm#83225
vfdff added a commit to vfdff/llvm-project that referenced this issue Mar 27, 2024
Fold select if the user of its select user indicates the condition

Fix llvm#83225
vfdff added a commit to vfdff/llvm-project that referenced this issue May 13, 2024
vfdff added a commit to vfdff/llvm-project that referenced this issue May 13, 2024
Fold select if the user of its select user indicates the condition

Fix llvm#83225
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants