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

    <tr>
        <th>Summary</th>
        <td>
            Missed Simple Loop Optimization
        </td>
    </tr>

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

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

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

<pre>
    The following is a pared down example to illustrate the problem:
```c++
int foo_optimized(int num) {
 const auto arr = new int[num];

    int ret = 0;
    for (int i = 0; i < num; ++i) {
        arr[i] = i;
        ret += arr[i];
    }
 delete[] arr;
    return ret;
}

int foo_missed(int num) {
 const auto arr = new int[num];

    int ret = 0;
    for (int i = 0; i < num; ++i) arr[i] = i;
    for (int i = 0; i < num; ++i) ret += arr[i];
    return ret;
}
```
https://godbolt.org/z/T34jensfj

`foo_optimized()` and `foo_missed()` perform the same work, yet for `foo_missed()` `clang` fails to deduce that the allocation is pointless and remove it. I was surprised that such a seemingly simple transformation was enough to defeat the optimizer.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVEtvnDAQ_jXDZZQVMY-FA4dsViu1atVDc68MHsCpsZFtsk1-fWVDHhtVqXorQgL7--b1ecbcOTloogaKAxTHhC9-NLb5zDvT9ly2SWvEY3M3EvZGKXOWekDpkOPMLQkU5qyRfvFpVoTeoFRqcd5yT-hHwtmaVtEE2Q2kR0hvoEzXtwN2CG_cldpjb8wPM3s5yScSwKqwp5cJWI2w33jYGe088sUb5NYiZEfUdEapPRSHwC6OkG3kzQQxwGjJR3r6ggekNxa3WPIFjr-3MXh2wDVPeZnH9nBroThIKI7RWF74Dk-Myg4BfOVesGB_3BaCFHlaTyGy39Is-cXq8Hmt79nyUsNJOve_CvihXv_m6u_KfijZcxuuy9H72YUeZSdgp8GI1ii_M3YAdnoCdrrL8nvSrr9_KwyU6fueBVZDmSLXAjf05TQ2aCbbGzvF2XB8Ijwb-xPYLT6SXxX4s12YGMX1EP57LpULsyZILF2YM-6jQ66U6biXRocJnY3UXpFzMR9Lk3kglH6Hn_DMHbrFzlY6Equ9W7oROTqiSepBPaKT60hbrl1IefUbLEmbZRjXBHraYj_LYHeJaDJRZzVPqLkuq3yf1qzaJ2NTdqwSXd7v-2Kf9SJrW6rqqqzStONUlV0iG5ayLC3YNWN5le53oqae5XWb8rygKsshT2niUu2UepjC-STSuYWaklV5kSjeknLxHmMsdnUAgbFwrdkm2Fy1y-AgT5V03r168dIrar5GzfH7WvgXY2b8tlYVa08Wq5p3nSL9uLS7zkzATsHb9rmarbmnzgM7xRwcsFPM8XcAAAD__-yBmuM">