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

    <tr>
        <th>Summary</th>
        <td>
            [missed optimization] Missed removing loop of setting zero when dereference pointer (memset)
        </td>
    </tr>

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

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

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

<pre>
    [Godbolt](https://godbolt.org/z/b57EK93e3) and [GCC Bug](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120268)
```
void f0(unsigned char *ptr, unsigned long len){
    for(unsigned long i = 0; i < len; ++i){
        ptr[i] = 0;
    }
}

void f1(unsigned char *ptr, unsigned long len){
    for(unsigned long i = 0; i < len; ++i){
 while(ptr[i])
            ptr[i]--;
    }
}

void f2(unsigned char *ptr){
    while(ptr[0])
        ptr[0]--;
}
```
When flags are -O3,
f1() compares if ptr[i] is zero before setting it to zero and f0() memset it to zero.

When flags are -O3 and -march=icelake-client, f1() doesn't call memset like f0().
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzEVEGvszYQ_DXLxQoy6xDgwCEkpYeq6qVSj5XBC7ifwZFtXtT8-sokL8nX16rHWlESedid2dllpfd6XIhqyBvIz4lcw2Rd3TiS6lc7J51Vf0bsR6s6awLkZ8ByCuHiQRwBW8B2vEOpdSNgewNsu7z44adKkACsmFwUiwlOJ9as4z8m6Pt0XNZHgm4db9oYCdj6yV5_79Yx7UcNotUKxDlDjocSsAJ-hAN_fPjxw2rFBg5YrstWkmL9JB0DPF6CAzyx572xy8gMLTFJ0QA_MsbYYN177PaMZiDOjINotr-nLUg0DLABbPR38fFEorzRkJ-fgQ8UinPUe__-FJv9P2KvkzYEWL7U3t1kb-eF7Xb_UQX-axVvgr_n5F84X_effA-Wtw7_NtHCBiNHz6QjtvtFAJ6AHzcf46T1dr5IR57p4b0V2rMbOcs6Gqwj5ikEHf0KLNg7Ekd0G52YZabZU3iD03u1X-m3uN0sXT-BOOuejPxGu95oWkLs4VOYsuQXwCKwXhrzyWD0N3rSpomqhapEJROqs2JfZEUlyiqZ6q5TZaeQDrwrhmyf9wNm5V5kKKlXMssTXSPHnOeZQCEOWKVFXsg8Kw5KDFzlVQ57TrPUJjXmY45vWaK9X6nORFVmPDGyI-O3BYC40JVtKCDGfeDqGLTr1tHDnhvtg3-lCTqYbXPM2ntSzF6CnvVNBm2X6PzP92tHs_2IlhtrL8wOzxZs5l-jr4ocDeRo6YldrF4CxTEq704BVsnqTP23raHDtHZpb2fANkp6_Owuzv5BfQBst0I8YPuo9KPGvwIAAP__K0d19A">