llvm / llvm/llvm-project

[InstCombine] Masked-merge fold `(x^y)&~m ^ y` -> `(x^y)&m ^ x` introduces more undef

Open
#218,908 4 comments 0 reactions 1 assignee Claimed by @OmarAzizi View on GitHub
llvm:instcombine miscompilation:undef
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

InstCombine canonicalizes the masked-merge

```
y ^ ((x ^ y) & ~m)
```

into

```
x ^ ((x ^ y) & m)
```

his is only valid if the two mask polarities are interchangeable, which is correct for most cases. But it might be incorrect when `%x = undef` in following transform:

Source:
```llvm
define i4 @scalar(i4 %x, i4 %y, i4 %m) {
%im = xor i4 %m, -1
%n0 = xor i4 %x, %y
%n1 = and i4 %n0, %im
%r = xor i4 %n1, %y
ret i4 %r
}
```

Target:
```llvm
define i4 @scalar(i4 %x, i4 %y, i4 %m) local_unnamed_addr #0 {
%n0 = xor i4 %y, %x
%1 = and i4 %n0, %m
%r = xor i4 %1, %x
ret i4 %r
}
```

Reproduce: https://alive2.llvm.org/ce/z/XEiEYr

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.