| Issue |
238220
|
| Summary |
[X86] Miscompile: (x & ~0xff) | zext(load i8 (x + C)) loses upper bits of x (MachineCopyPropagation / X86FixupBWInsts after VirtRegRewriter)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
jianglihui900330
|
The _expression_ `(x & ~0xff) | zext(load i8 (x + C))`, where the load address depends on the same value `x`, is compiled at -O1 and above so that the preserved upper bits of `x` are lost. The same happens for the 16-bit form `(x & ~0xffff) | zext(load i16 ...)`. -O0 is correct. This reproduces on both x86_64 and i686.
### Reproducer
`repro.ll`:
```llvm
target triple = "x86_64-unknown-linux-gnu"
; Expected: bits 8..63 of %x are preserved.
define i64 @ins8_64(i64 %x) {
%a = add i64 %x, 8
%p = inttoptr i64 %a to ptr
%b = load volatile i8, ptr %p, align 1
%z = zext i8 %b to i64
%m = and i64 %x, -256
%r = or disjoint i64 %m, %z
ret i64 %r
}
```
The `volatile` is not needed. A plain load gives the same result.
```
$ llc -O2 repro.ll -o -
ins8_64:
movzbl 8(%rdi), %eax # upper 56 bits of %x are dropped
retq
```
With -O0 the result is correct (`andq $-256, %rax; orq %rcx, %rax`).
The equivalent C is `uint64_t f(uint64_t x) { return (x & ~(uint64_t)0xff) | *(uint8_t *)(x + 8); }`. With `clang -O2` it gives the same `movzbl 8(%rdi), %eax`.
The i686 form, `uint32_t f(uint32_t x) { return (x & 0xffffff00u) | *(uint8_t *)(x + 8); }`, gives `movl 4(%esp), %eax; movzbl 8(%eax), %eax`. With `__attribute__((fastcall))` it gives `movzbl 8(%ecx), %eax`. The 16-bit variant gives `movzwl`.
### Disabling X86FixupBWInsts alone is not enough
| flags (x86_64, -O2) | output |
|---|---|
| default | `movzbl 8(%rdi), %eax` (upper bits zeroed) |
| `-fixup-byte-word-insts=false` | `movb 8(%rdi), %al` (upper bits of %rax undefined) |
| `-disable-copyprop -fixup-byte-word-insts=false` | `movq %rdi, %rax; movb 8(%rax), %al` (correct) |
The i686 fastcall variant behaves the same way. It gives `movzbl 8(%ecx), %eax`, then `movb 8(%ecx), %al`, and is correct only with both options. Executing the second form with `%eax = 0xdeadbeef` on entry returns `0xdeadbeab`.
### MIR trace (`-print-after-all`, x86_64 reproducer)
```
after Register Coalescer:
%3:gr64_with_sub_8bit = COPY $rdi
%3.sub_8bit:gr64_with_sub_8bit = MOV8rm %3:gr64_with_sub_8bit, 1, $noreg, 8, $noreg :: (volatile load (s8) from %ir.p)
$rax = COPY %3:gr64_with_sub_8bit
after Virtual Register Rewriter:
renamable $rax = COPY $rdi
renamable $al = MOV8rm killed renamable $rax, 1, $noreg, 8, $noreg, implicit-def $rax :: (volatile load (s8) from %ir.p)
RET 0, $rax
after Machine Copy Propagation Pass:
renamable $al = MOV8rm $rdi, 1, $noreg, 8, $noreg, implicit-def $rax :: (volatile load (s8) from %ir.p)
RET 0, $rax
after X86 Byte/Word Instruction Fixup:
$eax = MOVZX32rm8 $rdi, 1, $noreg, 8, $noreg, implicit-def $rax :: (volatile load (s8) from %ir.p)
RET64 $rax
```
After the coalescer, the partial def `%3.sub_8bit = MOV8rm %3, ...` is still correct. Here `%3` is both the address operand and the register whose upper part is preserved.
After VirtRegRewriter, the only operand that reads `$rax` is the explicit address operand, now `killed`, and an `implicit-def $rax` has been added. There is no implicit use of `$rax` that stands for the preserved upper bits. Two later passes then treat the upper bits as dead or undefined:
- **MachineCopyPropagation** forwards `$rdi` into the address operand. Nothing reads `$rax` any more, so it deletes `$rax = COPY $rdi`.
- **X86FixupBWInsts**, in `getSuperRegDestIfDead`: the MOV8rm/MOV16rm fallback looks only at `implicit_operands()`. It sees `implicit-def $rax` and no implicit use, and concludes that the super-register was undefined before the instruction. It ignores the explicit use of `$rax` (or, after copy propagation, the fact that the value was never placed in `$rax`). It then widens the load to `MOVZX32rm8`.
My reading is that the underlying problem is the operand representation after rewriting: the read of the preserved bits has been merged into the address operand. FixupBW and copy propagation are the two places I found that act on it. I may be wrong about which pass should own the fix.
### Versions
- Apple clang 21.0.0 (clang-2100.3.34.2), with targets `x86_64-unknown-linux-gnu` and `i686-unknown-linux-gnu`.
- rustc 1.96.1 (LLVM 22.1.2) gives the same result for the equivalent Rust code (`(x & 0xffff_ff00) | read_volatile((x + 8) as *const u8) as u32`) at `-C opt-level=1..3`. This was checked for `i686-pc-windows-gnu`.
- I have not built LLVM trunk. The `getSuperRegDestIfDead` fallback on current `main` still inspects only `implicit_operands()`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs