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

    <tr>
        <th>Summary</th>
        <td>
            Clang fails to remove unnecessary loop
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

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

<pre>
    The compiler seems to be pre-computing the result, but then it also doesnt remove the loop that was there to compute the value

```cpp
#include <cstdint>

template<std::size_t Index>
static void testForLoop01(int32_t* valuesNew) {
    if constexpr (Index < 8) {
 *valuesNew += *valuesNew + Index;
        testForLoop01<Index + 1>(valuesNew);
    }
}

static void testForLoop02(int32_t* valuesNew) {
    for (uint64_t x = 0; x < 8; ++x) {
        *valuesNew += *valuesNew + x;
    }
}

int a() {
    int outputValue = 0;
    for (uint64_t x = 0; x < 1024 * 1024; ++x) {
 testForLoop01<0>(&outputValue);
    }
    return outputValue;
}
```

```x86asm
a(): # @a()
        mov     eax, 1048576
.LBB0_1: # =>This Inner Loop Header: Depth=1
        add     rax, -8
 jne     .LBB0_1
        mov     eax, -134744073
 ret
```

godbolt link: https://godbolt.org/z/Tjb49E5hG
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVMGO4jgQ_RpzKYEcO4RwyIFAszPSaE-tvbaSuCDuduzIrjDMfP3KITAwvdPqRQiH8vOr96riqkLQR4tYsGXJlrtZNVDrfPHFdWj0G5ZeN2-pmNVO_SieW4TGdb026CEgdgHIQY3Qe5zHjYG0PQK1CB7DYIiJLdQDxYgFTVCZ4EA5DJbAY-dOOIKNcz1QWxF8r0KMeIzEF8YL5FSZARnfMb6ZfjN--TZ9P0WE1LYxg0JgctsEUtoSk0_3pwi73lSETG4DKSY3TG6C_okvBF-twvMNHqgi3cDJaQWEgfbOf3Ou5wkTubYkxQsxsbnICn_jdybWwFbl5TAAgD5A42wgPPcemMhH-igM8kcsE5sbCzBRMrl7F7uKu-OPn0dhcjvlECUk0YjI7-U9nGar3VSW28OHtsVnbR_c6HbQlrL0hSBa3gFnsoSre1mONkV5fnd6lPaZepw_5UZbgoqJ_H1zLIEbqB_on0h70_g_fCRcpFHX-PBHT793iF_6wkR2l_6PzYl_PNLg7b3aG_aX2-td-M8Lcs6zKnSX4FQNJuN7J4Gl_Bp56EHnTuOK1Tle4YSn-XKVXTCLb2XJX5Ibhdwx-fTc6gBfrUUP0St8wUqhj5gd9tQyuUseM1RKjau_ZJjn0_arxTF-zfKRrHki01Wa8pWcYB7pg4IcnaqdITDavkVlLVEf4ggQeyb20-7C-SMT-59M7J9f63T9tGz_mqlCqrVcVzMskmydpXmSpHzWFqrJDok6ZPlhqZa1kmnKG8Wb1UHl6TpDMdOF4EImnC95mi5TvuAyk9mSi0NzkOu8XrOUY1dpszDm1MXcMx3CgEWWi1zMTFWjCeNkFqIxlT0yIeKQ9kXEz-vhGFjKjQ4UfjGQJoPFNsLhUGkzDulp2g7WYoMhVP7HOHVngzfFb4XQ1A71onEdE_tIOi3z3rtXbIiJ_agxMLEfZf4bAAD__39bxNg">