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

    <tr>
        <th>Summary</th>
        <td>
            malloc/memset -> calloc optimization should happen even if there are BBs between the ptr check and the memset
        </td>
    </tr>

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

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

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

<pre>
    take:
```
void *combine_alt (int s, bool c, bool *a)
{
  void *r = __builtin_malloc (s);
  if (!r)  return 0;
  if (a) *a = 1;
  __builtin_memset (r, 0, s);
  return r;
}
```

This should be optimized to:
```
void *combine_alt_tgt (int s, bool c, bool *a)
{
  void *r = __builtin_calloc (s, 1);
  if (!r)  return 0;
  if (a) *a = 1;
  return r;
}
```

Right now the check in shouldCreateCalloc is too restrictive. Instead of checking `MemsetBB != FalseBB`, it should be checking that MemsetBB post dominates the FalseBB.

Note if you remove the check for `!r`, shouldCreateCalloc is too restrictive there too. that is because MallocBB == MemsetBB check should be similarly a post dominate check.

Note I found this while fixing up GCC finally (https://gcc.gnu.org/PR83022). And I thought I would report the missed optimization back to LLVM.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVM1u4zYQfhr6MohAUVJsHXSwnboIsCmKRdGrQUkjaxqKFMiR3fTpC0pK1tlTDgsY_hE53y9NHQJdLGIlioMonjZ64t75aiQbXklvate-VaxfUWR7IffiUa4vub86akGofeOGmiyetWEQakeWIQh1hNo5A83HN6H2WqgygmwPQu4B3gE8iOwJzud6IsNkz4M2xjURK8SBbNlNXXwiVOqFKgE88uQtyM_LkWFmmiHT98U7bBwCzjp9VCbj2z3LCuuX32L79JNpIfd_9RQg9G4yLdQIbmQa6D9sgd2XQjrz5RcF1dwHdYT0F6X1hQy-06VnsO4G3CM0PTavQHZN5ehRMx4XcRSAnQOPgT01TFdM4NkGRt2C65ZRshcQj_Jl7uZwAKHSKOikTcDDIZKqIxDfhf4xxr1m-BgcXWBo3UBWM4ZZ2wqSLLr_cIzR_JubwOPgrnhnoHMeZrLUr5xf8hMRPManySKHAtTY6CkgvMxD0VL2FC19KF0YfxgKNJDR3ryB_uxi2Xkv_xk6N9kWOJ7DW08GoaN_YxjTCL8fj9CR1ca8xYZ75jHEU6lOQp0uTZNc7JQ4fxHq9Of3XSaVEqpMYG9beAbu3RR7fYbbrMvj6DzPCQ0UArbvh10zOQu1bl6BHXz79vdLsmmrrC2zUm-wSrd5nhZFsdtu-gqbbS3LJiu3hXwsCky7ba5znXY6zzCv1YYqJVUhc7lTWVbm2wRTWe7yutPY6KJopMglDppMYsx1iOI3FMKEVZrlZV5ujK7RhPn-UsriDeZVoVS8znwVhx7q6RJELg0FDj9gmNhgtVw3Qp3Wu-FBZL_B-tf65Hdtq9fjiBbwijYepaV97REOh1g83xDtnNnIfu1Zz3UhLAybyZvqp2aI-6lOGjcIdYr61o-H0bt_sGGhTrOrINRptX2t1P8BAAD__5N-yeE">