<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/80880>80880</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Constant reusing fails to understand when bits are irrelevant
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Validark
      </td>
    </tr>
</table>

<pre>
    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.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0VUuP5CYQ_jX4UtoWxu-DD_NIS3ONkhz2hk3ZJouhA0VP9_76CPf0ZmYfmZkoa7UaiSqK7wGFDEHPFrFn1S2r7jMZaXG-_0MaraT_lA1OnfsHC-sZRqeQiTt4gEUqkBCsPhyQwOhPCLTowIobxu8Zv_7X_PL7rOfLDJ4OzhNMFibnmGhPrLiBWJdMdGkA1txeC3QeKXoLTLQpEVj1C_BT_tWXFjJx86NIcceKOyhZcS3b3H8F7Tne3-3kPEUrCc05EU2cYEYK4A6kV_0ZFZADWhDwr6iP0qAlcNPPJH9lzt_KnL-R7ZdJGdbLTIJ1JQFP3-qOcggAfjwlRZq6rfOyKOqSl1Vd1tUPsuWWnQvRNa3oilaUTcGLsuQv80_OXyt7pV_G9BpNGp9qpbQXcY_0L-x-lbSgB1qkfYcxg_TvNubNR_K_G5NgvWLMq1IblPBMTlbd-vEETNxCzqr7b215Lv3_a80DwaOLRsGAYPWIoKftSo3OBpKWwGMM2s4woaToETyObraX27dIggdQzjLREMiRojTmDBZRAav5d-5JzRPGIdIl_vH78ccFPaaMj6zmoANIe4ZBUyCv7byD39LGK0obLhjwiDYBv_TCR6-J0AJs1b4cjf27jsYFSFIiyBWvLUeSdhbGq2DycDAa1S5TfaG6opMZ9nnDG1HmBW-ypRdtWYtpasdJ5qrKu3oQ3aQqhYrzXPI2073gouSC13lT5mWza0acxNChECrneaNYyXGV2uyMOa475-dMhxCxb3nb8szIAU3YXgshLD7CFmRCpMfD92nNhyHOgZXc6EDhnyqkyWB_943LUpuQ2mq0Cn2KqeSG3dQH6RG092jwKC1l0Zt-ITps3VbsmdjPmpY47Ea3MrFPez0NHw7e_YkjMbHfEAYm9huDvwMAAP__7Jvh4Q">