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

    <tr>
        <th>Summary</th>
        <td>
            [ARM] `chkstk` causes erroneous code generation at -O2
        </td>
    </tr>

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

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

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

<pre>
    Given this code (https://godbolt.org/z/55PTvsse7):

```cpp
int repro(int count)
{
 volatile int *values = __builtin_alloca(
        sizeof(*values) * (count > 0 ? count : 1));
    values[0] = 0;
    for (int i = 0; i < count; ++i)
 values[i] = 1;
    return values[0];
}

int main(void)
{
 volatile int count = 1;
    int first = repro(count);
    int second = repro(count);
    return first * 10 + second;
}
```

When built on 32-bit ARM Windows with Clang at trunk, at `-O0`, the return value of `main` is `11`, which is expected.
At `-O2`, however, the return value of `main` is `0`.

This is because, while `chkstk` clobbers `CPSR`, at `-O2`, CodeGen reuses the results of a comparison before the `chkstk` call.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVMGO2zgM_Rr6QiSQ6Ti2Dz44mU1PRYvZAj0OZJuJtaNIgSQnu_36hRylgxRYbAMDkcXH9x4pi9J7dTLMLZQ7KF8yOYfJuja42ZxMoCYXWW_Hf9pP6soGw6Q8DnZkBKqnEC4eig7oAHQ42bG3OqytOwEdfgAdyvLrt6v3XAE1ESaWZyvuz3C5gOiUCej44ixQHdeDnU2IeNFBtQPR4dVqGZRmjGGg7ir1zB6heMG3t35WOijzJrW2gwSqY0b6efWD7THuPZKAmsgQvS86CMUfKBCKAz7eO8yjejS8S1wpt9wJKF8WXfERPFqHybr6GVuW-1RLsUOgHdBO3cv64FMPvvyDz3GYnXnWvEeherl3MGqdpTJA9dWq8b-b9ajpSSAGjsr5e-DR-p9tf8J5HqwZ_weYHCdO6jAXseCU_OT9cfT3Or5PbHA5QLQGC1r1KmD3-hm_KzPam8ebChPutTQnlAHjF_kOtI9r2IrVl4WJ9hgmfmob2mMELC3aClQ-vuV5Qt8mNUxxk_--8BB4XIPoukRJCTTZG1_Z_TZ7tLK-V_Ut3hDlsedBzp6TpOYIG6Z3H95j2qBt37Nbkvdf_3xNuvIXH3s78ic26Hj27JMXP-vgow2Jgz1fpFPeGuz5aB0vkGclqfU6G9tibIpGZtzmVb3ZFqIW22xqa1H2uayPo-hFL6qqbsriWOb5uBmoLvsxUy0J2oqK8lxstpvtuimkKMbNUDXiWDV1DRvBZ6n0WuvrOV7-THk_c0u5aIom07Jn7ZfhQmT4hksUiOKscW1MWvXzycNGaOWD_6AJKuhlKnWvn5eL8lzV0hB2zhq2c5pJJzbsZFDWxE6uvlA2O93-MqZUmOZ-Pdgz0CGKpb_Vxdm_eAhAh8WiBzqkGq4t_RsAAP__zJaHVQ">