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

    <tr>
        <th>Summary</th>
        <td>
            `x - y * Constant` should not be folded into `x + y * -Constant` when `Constant` is already in a register
        </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>
    Possibly related to https://github.com/llvm/llvm-project/issues/71389 (although I am not familiar with LLVM internals) [Godbolt link](https://zig.godbolt.org/z/3orjcPPq9)

I have this function:

```zig
export fn columnCounts(non_newlines: u16) u64 {
    const ones: u64 = 0x1111111111111111;
    const x = pdep(non_newlines, ones);
    const y = x *% ones;
    const z = ((x ^ ones) *% 0xF);
    const pexted = pext(y, z);
    const prefix_diff = pexted -% (pexted << 4);
    return y -% pdep(prefix_diff, z) *% ones;
}
```

As you can see, I only have one constant that couldn't fit in an immediate and needs to be loaded. I.e. `ones`.

However, LLVM likes to transform the return statement into `return y +% pdep(prefix_diff, z) *% -%ones;`. This is counter-productive in this case because `ones` is already required as a constant in two other locations.

Output:

```asm
columnCounts:
        movabs rcx, 1229782938247303441
        mov     eax, edi
        pdep    rax, rax, rcx
        mov     rdx, rax
        xor     rax, rcx
 imul    rdx, rcx
        lea     rax, [rax + 4*rax]
        lea rax, [rax + 2*rax]
        pext    rcx, rdx, rax
        mov     rsi, rcx
        shl     rsi, 4
        sub     rcx, rsi
        pdep rcx, rcx, rax
        movabs  rax, -1229782938247303441
        imul rax, rcx
        add     rax, rdx
        ret
```

Expected output:

```asm
columnCounts:
        movabs  rdx, 1229782938247303441
        mov     eax, edi
        pdep    rcx, rax, rdx
        mov     rax, rcx
        xor     rcx, rdx
        imul rax, rdx
        lea     rcx, [rcx + 4*rcx]
        lea     rcx, [rcx + 2*rcx]
        pext    rsi, rax, rcx
        mov     rdi, rsi
 shl     rdi, 4
        sub     rsi, rdi
        pdep    rcx, rsi, rcx
        imul    rcx, rdx
        sub     rax, rcx
 ret
```

[Godbolt link again](https://zig.godbolt.org/z/3orjcPPq9)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVl9vo7oT_TTOy6gRMRDgIQ9Js_n9Ku3V7cPVvq4MHoK3xs7apiH99FeGwJI0aa9WG1VFYubMn-MzeJi1Yq8QVyTekHg7Y42rtFl9Y1JwZl5muean1bO2VuTyBAYlc8jBaaicO1gSrgndEbrbC1c1-bzQNaE7KV-Hx8PB6B9YOEJ3wtoGLaG7ZBGmGRCaMukq3ewreAJWg9IOSlYLKZiBo3AVfP367S8QyqFRTFpCMyDx5n-a51o6kEK9kHhLaHpZyZvYz_e9z1ybvX9D6C7U5kfx_PwzIzQjwZYE6_7_E1TsFcFVwkLZqMIJrXyoiQtZBv3fm9j3b7A9aOOgVFBo2dTqUTfKWUJTpdV3hUcpFPqCoFksfdXNMgKSbHowAEChlXWgBy9vDrcQtIurHwnfgdrO9cDxcJ2PPvYRaXYDdupgLRC6JjQ-p37n9dZ5EZoSmrZA4i9DxAEXtLvb8Q_Yel10tWHrCE1PvqC3O94GS9F-56IsRwhyePApCE3HYI8kfIToOoZB1xgFp97_TMUk5JD4ZrMk2V4d6_Ss1xZOuoGCKbCIPtATaCVPvUq0wr4Bphy4ijkodCO5IjRxUAoHQgFTIOoauWAOgSkOCpFbPzA5gtSMI5_D0xznQJZBV9kymE9L-L8-4isan7vTvxQv2OGdYcqW2tTgKhxIsI45rFH53E77mCM7hG7-Gz-exoGjZTCHf_wwCOub87PnR5g3hROv6BvsRqVgFiHHgjUWJ414FJMGGfefip-NMMiBWWC_aPMRjhq0q9CA1AXzE2cvGPi7cYfG3ZtCZuv-zcXsDc5w_tX6leUWTNH6bheUZklKszClURIGYRQt3rl3T2SdP3JxafckdtLr7cOjaG-HMXx0u7C32sA0zIgXdSOnwOvAEtkUSOKNYX6Y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