<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=http://email.email.llvm.org/c/eJylVtty4zYM_Rr5BROPI-diP_ghyTYdz6SbmWy73VdKgiQmFKmSlB3v1_eAknNr9qkzudAkCBwcHIAuXHXY3Azes43mQHum0tmgK_ZknKoCuZoGq62OWhn9kyvquHP-QNFRwTiquJ7Tn60OhJ_CxZbCUJy4PupOGVK2IkXBDb5kcdXpULqu10ZFjTjzbPElW1zdOk_8rLrecJbfkLYhBo1Puj5Qll-Q5X2zs4Jsxz5SdrHoW53lqx9inSBk-ZpOsuVv9AOHr3j23tmGVJArcoL4TRsFeO90cHZOfzO1qqLYMgXVMS6FgWmvkQfsAxsuo1wshkg6wjaQMp5VdYAXtlTrZ66OaehnjXBRghdDIwA8I1OQlhyOPB4JLLhUQ0BY71SW32I399xQCecR-MTJdCtRfAUmUR3L8DZyrwrDsqydkQQcfKm9Okxgxr8pvR0OqFexbGGLuirbeKHsZop9Q8fgLXvOllfUxtgHLIALP553mvdhbsyumzvfYOvL6eLs4mw1BtmOMVIEDtBNqQy0JLG2KOW3t6X8yvvfv39NsPVzoh2ZTqx30CBUgQRV4Xb8LpGbFqhHdkEZdwqmZVKnyPRVpS_sgsanQCI2SK3QRscDSDTgaWha6lyIBPY9SHY2yy8jWQePSX-gee8GkHokW9vIvlblJAyB0KlHpFdDX5Ft9VbIpfD7qRvJuHQVN9ANABc6ntSaDZpMVBWTX8MqjKtae6ySAYXoPE8N-VFFtRx1KjwJOzqK-JngBc2sDqJBRH4cxJVn_sljFHgCpp0yA8_pG_rRnIBxL7DGzpDUOqp0XbOMBpqAwh14QltPJQNwXRNqNgGFqF0HDUxOpBGOMsc-_XX90j2t6nuWWms7ysC7xqsOdmkIIUIQWJRgTe6QiWV0cOG8JDtimqi_1vFIJgCniuLmQRTQSYVvxHNqzdBKVUYTaaOplimhd5q7t3R39_0PGOISRqI42eKK0bwbeXTWHCYmdBiVuIchKtRMQ6gT5hGFIRwkgpVH4Xai8UjbB9R7x0YcA8Gb9hbGdhg9DgLFoMOhhCsOmAzx0LOMu3raTAHlFvbfwd-CwqHrlD-I_-gqJx2djk4oO7_G7xdKfcVH0V6GTwU6KQ-4Kge-JiHB4qOWPnrfYmCHqBuZaNDJMUHM8zTkUZtC2zQ50GA2RRjpmWKEUYbFsbYf_V9Z6waLvkztLpooUz7JE9h9Myjs1LaYBDAzWhpiYhAD1Li9bEu5RTYBY-gXTL24lDkp4kIv3uHkgetxAv7i3jtxfCjxf66Mbr893F-h-tcv43masL8wfzdrP4cyGk4z-HMTvHiFR72y5S3dozYhcg954HXxL-WRDNKzRNntIlvfZlfn2Sp_q72ypOxsof55HOACK6Dn1svK6iedDr2LCOPTg4yPd1xwxbuH7azaLKv1cq1masC09hs7WNcb13OYDd5s3r9NDfpqKOYoBD7IAzX9O8E8eZS3O79N_RmwOM_Xy8Ws3SyK1ZrV6Smvl7UqWF1eqjpXpcrzi3O1qlYzg10TNuAky_MkVnGBNRia_X8EepMv8tPTfLk4XS9W-Wq-XlcX5_UZNs8WfHm2AB0sSn15cWd-k1ziW0XAoaj39TmeqRB0g8ZKgIEw6mh484AxOxbqk29vx7E0DlaZDzLWU01nCe0mQf0XmDFrfw>52930</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Reading uninitialized memory must be poison, not undef
        </td>
    </tr>

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

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

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

<pre>
    Currently we consider loads of uninitialized memory to be undef. This is both sub-optimal and a source of miscompilations.
For example, instsimplify & newgvn convert `phi(X, undef) -> X`. This is wrong as `X` might be poison. We had the same issue with `select` but it has already been fixed.
Fixing this bug is related with uninit memory because sroa/mem2reg create phis with undef. And we need to be able to fold those away.

We have a patch for langref, sroa, mem2reg here: https://reviews.llvm.org/D104648
I have patches locally for InstSimplify & NewGVN to fix the phi issue mentioned above.

Changing the semantics of load of uninit memory breaks compatibility. Although most users won't notice, we would need to interface with the major frontends.
For clang, we would need to fix codegen of bit-fields as at the least the first field store loads uninit memory before masking it. The easy way is to just freeze the loaded value. Spill-over of poison from different fields is not an issue as if one field becomes poison is because some UB already happened in the program so we can spill over poison to neighboring fields.
Bitfields aren't very common, so this shouldn't be a major issue.

On LLVM's side, I believe the only issue is load widening. This must be either be removed at IR level, or we need to use vectors (or the byte type) for the widened type.

In summary, todo:
- [ ] Change clang's codegen of bit-field loads to do a freeze of the loaded value
- [ ] Investigate if we need new instcombine patterns to remove freezes from bitfields
- [ ] Announce breaking changes to IR semantics on the mailing list for the following LLVM version
- [ ] Change semantics: commit LangRef patch
- [ ] Change load widening to use vectors
- [ ] Commit SROA + mem2reg patches
- [ ] Commit InstSimplify patch
- [ ] Commit NewGVN patch
- [ ] Celebrate? One step closer to removing undef ðŸ¥‚

cc @aqjune @regehr @nikic @rotateright @LebedevRI
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx1Vtty2zgM_Rr5BROPLTuO_eCHJN3seCbbzqTbbl8pCZKYUqSXpOy4X78HlJzbpjO50CQIHBwcgC5cddre9t6zjeZER6bS2aAr9mScqgK5mnqrrY5aGf2LK-q4c_5E0VHBOKq4ntLfrQ6En8LFlkJfXLh91J0ypGxFioLrfcniqtOhdN1eGxU14kyz2adsdn3nPPGT6vaGs_yWtA0xaHzS9YmyfEWWj83BCrID-0jZarZvdZavf4h1gpDlG7rIFn_QDxy-4Dl6ZxtSQa7ICeI3bRTge6eDs1P6h6lVFcWWKaiOcSn0TEeNPGAf2HAZ5WLRR9IRtoGU8ayqE7ywpVo_cXVOQz9phIsSvOgbAeAZmYK05HDg8UxgwaXqA8J6p7L8Dru554ZKOI_AJ07GW4niazCJ6liGt4F7VRiWZe2MJODgSx3VaQQz_E3pHXBAexXLFraoq7KNF8pux9i3dA7esudscU1tjPuABXDhx_NB8zFMjTl0U-c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