Issue 80880
Summary Constant reusing fails to understand when bits are irrelevant
Labels new issue
Assignees
Reporter Validark
    In my code, I had a snippet like this:

```zig
export fn foo(x: u64) u64 {
	return ((x ^ 0x1111111111111111) * 0x1111111111111111) << 4;
}
```

Unfortunately, this gets optimized to the equivalent of this:

```zig
export fn foo(x: u64) u64 {
	return (x ^ 0x0111111111111111) * 0x1111111111111110;
}
```

```asm
foo:
        movabs  rcx, 76861433640456465
        movabs  rax, 1229782938247303440
        xor rcx, rdi
        imul    rax, rcx
        ret
```

Rather than:

```zig
export fn bar(x: u64) u64 {
	return (x ^ 0x1111111111111111) * 0x1111111111111110;
}
```

```asm
bar:
        movabs  rcx, 1229782938247303440
        lea     rax, [rcx + 1]
        xor     rax, rdi
        imul    rax, rcx
        ret
```

It would be nice if the constant reusing feature recognized that I don't actually need `0x0111111111111111`, but `0xZ111111111111111`, where `Z` is any bitstring. That means that even if I had written  `(x ^ 0xF111111111111111) * 0x1111111111111110;`, the same optimization could be applied.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to