[llvm] Combine (X ^ Y) and (X == Y) where appropriate (PR #130922)

Ryan Buchner via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 12 14:37:43 PDT 2025


bababuck wrote:

I addressed the above comments, but I came across one issue in my tests:

When working with riscv64 with i32 an i32 input (a0 is i32), the following code generation occurs:
```
entry:
  %cmp.not = icmp eq i32 %x, 2048
  br i1 %cmp.not, label %if.end, label %if.then
if.then:
  %xor = xor i32 %x, 2048
  ret i32 %xor
if.end:
    tail call void @abort() #2
  unreachable

----->

# %bb.0:
   li a1, 1
   slli a1, a1, 11
   xor a0, a0, a1
   sext.w a1, a0
   beqz a1, .LBB0_2
# %bb.1: 
        ret
.LBB0_2: 
```
Because the result of the sign extension is being used for the comparison, it does not matching the XOR folding pattern.

One though is that this sign extension is not needed because we are comparing against 0, so we can fold away the sign-extension. However, I'm not sure if that is legal or not (i.e. is an LLVM i32 guaranteed to have the upper bits 0'ed when compiling to a 64 bit architecture).

Any thoughts on how to proceed? I want to make sure I am not completely off track.

https://github.com/llvm/llvm-project/pull/130922


More information about the llvm-commits mailing list