<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=http://email.email.llvm.org/c/eJy1Vk1z2zYQ_TXUBWMNTUqyfNAhjpvU0yTTidvM9OQByZWIGARYAJRE__q8BWlLqpM0Haca2QLI_Xhvd7GLwlb96saooKTWvXDUWheoEsoI1_lwpqXZJNkbXidZvlxe5mmSXifpq-H_jajllkSoSayt1nanzEZ41bSaxO3N--uz1u7Iwd66M2VQ1ohdrcpaBPLBY03QdKK1ymArHcGvVxUJa_C3FoXtTMUmC7snyDsVAhkG9xGARJItfW07XYkCGJzagoQIFktpvJYB5gLvpajUeg0cJggm1MkNJdllko8skkU6fCPN-KjtCtEZL9cgZsDNwtfwRuCzhyq8L5L51d1dk80XKsmvxIdkfp1krw9i_Y-JPfyYWBGDcJA9FV-w-JEG-ImzJP9FPLcqkourg1lNQTRdQOq9SPLrKN-w_J2n8EDO3nmFHfPnkI3A8iMLa-siOM7LAPLUw6OMYoF0Ov3w_PUjkLCPGEYA0lQH58_k-TMKlk27CXfUqjyD6B4U1BCLiAfbNG4vT-L5r3ZG3fPR1MHs1-0M4fkGr_4n8Oqf88pewCsfTfUv4fUtWmHPpkP_XeWHnxCUh-dBmb0gKPPR1MP_EpQYk4dv6eIEjl6PDVj3pH8QiIa-Zie5uD48ONmw8tAZHp8-tb1he9Vz920JfRodl1s6nG3IkJM8EaT31BS6j96XdQgt9yLMhnE8TDe2KqwOU-t4YoDlm8Va1p__-u3t79yMdiRKaYQnHhcyxI7gbUMAJj0GA1p1KcsazaO01qHrw6vn7u2DLO8F4siSlWdkjVg727AKQQW_jJGnCwgECPHs6PwjDWVa9Lc4ZMj5qfgDz7yE54IwvRRwMLAi8rWFJ7el6j-wLOf1271328iSWcl_TrvW2ZIQvmNqwmuFh1MBOMpH6oxXW3vvxd8dKIFuhfEnTZxhDXHOifMzjOrSxlHpBpYIKBJprNRKeqwEbJKRhUbmQERNaQqNplUa83YcmvfG7gbVhhrreoHVAPg4AzyVfbA8xDlMxiJPNYYoiaqLzp_I0p7KjlcIxPTklhBGXlrdk3j37tN7eHAOlQYejd2eBkZbznJM8JA9VCDrt2NKFa4KyqyV4RhtlQsdpr6jjfJDel-hUhQOxwUisJfRB9t5SjYKccdBxe3D8Bu-dXCwbLdBqureq_LU4FU3Co9WOQTBdTEhKEGPoGr_DInYSt0dV_BRbTrSPVOxEYCwTm3AXYNlGevYn4Tv3SF2gWtFed-RkNpbrliJm00Z_NcKdqNC3RVT5B0bvjG9dn0bcJV5U0uPk4ZFtMWLPM53cfvrq7NMxAtcg-vScKrw_Xhz-_rskwjSbSj46aRa5dVlfiknsgu1dStDu1Zutd1OOqdX34Gh9fbx5wzn4jOwH8OY5_lsOalXS3lB63U1Oz8vZmk1K5bFBS3O5bwsq8V8lsqJlgVpv-J7UJbB-xAVrBGIycsRqFWWZll6Hv_S2fn0osjnszJNiywtl2kxT2YpNVLpKdvhbjBxq2iy6DYeLzWKwB9eonuqjSGKgIEwqKBp9SdqmzuDxOEbiqTzuJtOIphVRPIF-naEig>53348</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Unnecessary stack usage
        </td>
    </tr>

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

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

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

<pre>
    Initially reported in rust-lang/rust#88930

I have the following simple SIMD-powered function which tests whether points are inside one of bounding boxes written in Rust (should be trivial to translate it to a different language):
```rust
pub unsafe fn foo(
    x: &[__m256i; N],
    y: &[__m256i; N],
    z: &[__m256i; N],
    bboxes: &[[__m256i; 6]],
) -> [__m256i; N] {
    let mut res = [_mm256_setzero_si256(); N];
    for bbox in bboxes {
        for i in 0..N {
            let tx = _mm256_and_si256(
                _mm256_cmpgt_epi32(x[i], bbox[0]),
                _mm256_cmpgt_epi32(bbox[1], x[i]),
            );
            let ty = _mm256_and_si256(
                _mm256_cmpgt_epi32(y[i], bbox[2]),
                _mm256_cmpgt_epi32(bbox[3], y[i]),
            );
            let t = _mm256_and_si256(tx, ty);
            let tz = _mm256_and_si256(
                _mm256_cmpgt_epi32(z[i], bbox[4]),
                _mm256_cmpgt_epi32(bbox[5], z[i]),
            );
            let t = _mm256_and_si256(t, tz);
            res[i] = _mm256_or_si256(res[i], t);
        }
    }
    res
}
```
By inspecting the [generated assembly](https://rust.godbolt.org/z/6fahjYKGP) we can see that for some reason it caches coordinates to stack and reads them from it each iteration instead of using the input pointers. The same behavior can be [observed](https://rust.godbolt.org/z/c5hGxsrvP) for a function which processes coordinate slices.  This caching looks quite redundant to me, especially considering that `noalias` is enabled (i.e. compiler should know that memory at which coordinates are stored can not change during function execution).

It looks like LLVM correctly moves coordinate loads from the inner loop using its infinite virtual registers. And it's exactly the behavior we want when there is enough physical registers. But when it's not true, it spills virtual register values to stack instead of relying on the original locations.

Looks like this issue also [affects](https://github.com/RustCrypto/hashes/issues/328) SHA-2 implementation on RISC-V targets.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1Vtty2zYQ_RrqBWMNTermBz3EcZN6mmQ6SZuZPnlAciUiBgEWACXRX5-zIG1JdZKmk1QjWwC5t3N2sYvCVv361qigpNa9cNRaF6gSygjX-XChpdkm2SteJ1m-Wl3laZLeJOmL4f-tqOWORKhJbKzWdq_MVnjVtJrEh9u3Nxet3ZODvU1nyqCsEftalbUI5IPHmqDpRGuVwVY6gl-vKhLW4G8jCtuZik0W9kCQdyoEMhzcewQkkmzla9vpShSIwakdQIhgsZTGaxlgLvBeikptNojDBMGAOrmlJLtK8hFFskiHb4QZH7VdITrj5QbADLBZ-BreCHwOUIX3RTK_vrtrsvlCJfm1eJfMb5Ls5VGs_z6xh-8TKyIJR9lz8QWLn2gAn7hI8l_Ec6siWV4fzWoKoukCUu9Fkt9E-Ybl7zyFB3L2zivsGD9TNgaWn1jYWBeD47wMQZ57eJRRLJBOp--ev34MJBxiDGMA0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