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 misses freeze of i1 select fold which appears in X86ISelLowering #131824

Open
arsenm opened this issue Mar 18, 2025 · 5 comments
Open

Comments

@arsenm
Copy link
Contributor

arsenm commented Mar 18, 2025


define i1 @src(i8 range(i8 0, 2) noundef %i, i8 range(i8 0, 2) noundef %i1) {
bb:
  %loadedv.i.i2.i = trunc nuw i8 %i to i1
  %loadedv.i1.i.i = trunc nuw i8 %i1 to i1
  %i2 = select i1 %loadedv.i.i2.i, i1 %loadedv.i1.i.i, i1 false
  %cond.fr13 = freeze i1 %i2
  ret i1 %cond.fr13
}

define i1 @tgt(i8 range(i8 0, 2) noundef %i, i8 range(i8 0, 2) noundef %i1) {
bb:
  %i21 = and i8 %i1, %i
  %i2 = trunc nuw i8 %i21 to i1
  ret i1 %i2
}

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

I noticed this while looking at random IR samples in X86ISelLowering.cpp's output.

@nikic
Copy link
Contributor

nikic commented Mar 18, 2025

We should probably be more aggressive about pushing freeze upwards in cases where it can be done by dropping flags only (rather than inserting multiple freezes).

@BaoshanPang
Copy link
Contributor

It seems eventually the redudant instructions are eliminated:

https://alive2.llvm.org/ce/z/-erq2F

@arsenm
Copy link
Contributor Author

arsenm commented Mar 19, 2025

It seems eventually the redudant instructions are eliminated:

https://alive2.llvm.org/ce/z/-erq2F

I may have over-reduced the example. This is closer to the original pattern:


define i1 @src(ptr readonly captures(none) %arg, ptr readonly captures(none) %HasCX16.i.i.i) {
bb:
  %i = load i8, ptr %HasCX16.i.i.i, align 8, !range !0, !noundef !1
  %loadedv.i.i2.i = trunc nuw i8 %i to i1
  %Is64Bit.i.i.i = getelementptr inbounds nuw i8, ptr %arg, i64 445
  %i1 = load i8, ptr %Is64Bit.i.i.i, align 1, !range !0
  %loadedv.i1.i.i = trunc nuw i8 %i1 to i1
  %i2 = select i1 %loadedv.i.i2.i, i1 %loadedv.i1.i.i, i1 false
  %cond.fr13 = freeze i1 %i2
  ret i1 %cond.fr13
}

define i1 @tgt(ptr readonly captures(none) %arg, ptr readonly captures(none) %HasCX16.i.i.i) #0 {
bb:
  %i = load i8, ptr %HasCX16.i.i.i, align 8, !range !0, !noundef !1
  %Is64Bit.i.i.i = getelementptr inbounds nuw i8, ptr %arg, i64 445
  %i1 = load i8, ptr %Is64Bit.i.i.i, align 1, !range !0
  %i1.fr = freeze i8 %i1
  %i21 = and i8 %i1.fr, %i
  %i2 = trunc nuw i8 %i21 to i1
  ret i1 %i2
}

!0 = !{i8 0, i8 2}
!1 = !{}

opt -O3 | llc has more instructions

@BaoshanPang
Copy link
Contributor

still same:

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

@arsenm
Copy link
Contributor Author

arsenm commented Mar 19, 2025

still same:

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

On x86. aarch64 has more instructions: https://alive2.llvm.org/ce/z/aCobwf

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