Issue 71389
Summary Constants are not reused for their inverse (andn/orn)
Labels new issue
Assignees
Reporter Validark
    I have this code:

```zig
export fn missed_andn(v: u64) u64 {
    const mask: u64 = 0x7F7F7F7F7F7F7F7F;
 const low_7_bits = v & mask;
    const non_zero = low_7_bits + mask;
 return non_zero & ~mask;
}
```

[On x86_64 Zen 4, I get](https://zig.godbolt.org/z/vsebWje1r):

```asm
missed_andn:
 movabs  rcx, 9187201950435737471
        movabs  rax, -9187201950435737472
        and     rdi, rcx
        add     rcx, rdi
        and     rax, rcx
        ret
```

I think we could have used an `andn` instruction rather than load 2 constants.

I believe the the same principle applies on other architectures even without `andn`/`orn`, especially load-store architectures like the RISC-V SiFive U74, on which we get:

```asm
.LCPI0_0:
        .quad -9187201950435737472
missed_andn:
        lui     a1, 522232
 addiw   a1, a1, -129
        slli    a2, a1, 32
        lui     a3, %hi(.LCPI0_0)
        add     a1, a1, a2
        and     a0, a0, a1
 add     a0, a0, a1
        ld      a1, %lo(.LCPI0_0)(a3)
 and     a0, a0, a1
        ret
```

It could probably have been:

```asm
missed_andn:
        lui     a1, 522232
 addiw   a1, a1, -129
        slli    a2, a1, 32
        add     a1, a1, a2
        and     a0, a0, a1
        add     a0, a0, a1
        not a1, a1
        and     a0, a0, a1
        ret
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to