| Issue |
242379
|
| Summary |
[X86] X86CmovConversion drops a zero extension that a byte-insert pattern relies on
|
| Labels |
backend:X86,
miscompilation
|
| Assignees |
|
| Reporter |
jayfoad
|
From Claude:
`llc` miscompiles the following on x86-64 at default settings: `f` can return a value whose upper 32 bits are not zero, although the result is a `zext` of an `i32`.
```llvm
target triple = "x86_64-unknown-linux-gnu"
@g = global [4 x i8] [i8 85, i8 0, i8 0, i8 0]
declare i32 @h()
define i64 @f(i32 %c, ptr %p) {
%x = call i32 @h()
%y = load i32, ptr %p
%cond = icmp eq i32 %c, 0
%s = select i1 %cond, i32 %x, i32 %y
%l = load i8, ptr @g
%z = zext i8 %l to i32
%a = and i32 %s, -256
%o = or i32 %a, %z
%r = zext i32 %o to i64
ret i64 %r
}
```
`llc -mtriple=x86_64-linux-gnu` produces:
```asm
f:
pushq %rbp
pushq %rbx
pushq %rax
movq %rsi, %rbx
movl %edi, %ebp
callq h@PLT
testl %ebp, %ebp
je .LBB0_2
movl (%rbx), %eax
.LBB0_2:
movq g@GOTPCREL(%rip), %rcx
movb (%rcx), %al
addq $8, %rsp
popq %rbx
popq %rbp
retq
```
When `%c == 0`, nothing writes a 32-bit register between the call and the return, so bits 63:32 of `h`'s return value end up in `f`'s result. The x86-64 ABI does not require `h` to clear them for an `i32` return, so `f` must do it: per the IR, `%r` is `zext i32 %o`, so its upper 32 bits must be zero.
#### Runtime demonstration
`h.s`, an ABI-conforming `h` that returns `0xaa` with junk in the upper half of `%rax`:
```asm
.text
.globl h
.type h,@function
h:
movabsq $0xdeadbeef000000aa, %rax
retq
```
`main.c`:
```c
#include <stdio.h>
#include <stdint.h>
uint64_t f(uint32_t c, uint32_t *p);
int main(void) {
uint32_t mem = 0x12345678;
printf("c=0: %#llx (expected 0x55)\n", (unsigned long long)f(0, &mem));
printf("c=1: %#llx (expected 0x12345655)\n", (unsigned long long)f(1, &mem));
return 0;
}
```
```
$ llc -mtriple=x86_64-linux-gnu -filetype=obj f.ll -o f.o
$ clang main.c f.o h.s -o t && ./t
c=0: 0xdeadbeef00000055 (expected 0x55)
c=1: 0x12345655 (expected 0x12345655)
```
With `-x86-cmov-converter-force-mem-operand=false` the result is correct, and the code keeps `cmovnel (%rbx), %eax`.
#### Analysis
1. Instruction selection turns the `select` into `CMOV32rm`. A 32-bit `cmov` always writes its destination and therefore zeroes bits 63:32, even when the condition is false, so `X86DAGToDAGISel::isDef32` accepts it.
2. Because the `cmov` result satisfies `def32`, `(or (and def32:$dst, -256), (zextloadi8 addr))` is selected to `INSERT_SUBREG (COPY $dst), (MOV8rm addr), sub_8bit`. The byte move preserves bits 63:8 of `$dst`, so this relies on the `cmov` having cleared bits 63:32. The pattern is from #205600.
3. The `or` also satisfies `def32`, so the `zext` to `i64` is selected to `SUBREG_TO_REG`, with no instruction.
4. `X86CmovConversion` converts the `cmov` into a branch, because it has a memory operand and `x86-cmov-converter-force-mem-operand` defaults to true. On the path that skips the load, the result is the call's return value, whose upper half was never cleared.
`X86CmovConversion` does try to detect this situation. `collectCmovCandidates` skips a `cmov` group if the result is used by a `SUBREG_TO_REG` ("Check if we were relying on zero-extending behavior of the CMOV"). However, it only looks at direct users, and here the `SUBREG_TO_REG` reaches the `cmov` through the `COPY` and `INSERT_SUBREG` from step 2.
The 16-bit variant of the pattern (`and def32:$dst, -65536` with `zextloadi16`, selected to `MOV16rm`) is affected in the same way.
#### Possible fixes
- Make the check in `X86CmovConversion` follow uses through `COPY`, and through `INSERT_SUBREG` where the `cmov` result is the register being inserted into, before deciding that nothing relies on the zero extension.
- Alternatively, when converting a 32-bit `cmov`, keep the zero extension with explicit `MOV32rr`s on the paths, as the existing `FIXME` there suggests.
Reproduced with `llc` built from `main` at 5dabc01bba1d.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs