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

    <tr>
        <th>Summary</th>
        <td>
            clang++ -O2 generates incorrect code in very specific circumstances
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          hitman-codehq
      </td>
    </tr>
</table>

<pre>
    Please find attached a minimal use case that causes clang++ to produce incorrect code in a very specific circumstance.  The conditions are:

1) I have a C++ class with a couple of variables
2) The class has no constructor (important - adding a constructor resolves the issue)
3) A class instance is allocated with an overloaded operator new
4) The overloaded operator new sets the contents of the allocated memory to 0

See the file StdStringList.h for the class in question. After allocation, accessing the instances Count() method should return a value of 0.  With an optimsation setting of O1 this succeeds.  With an optimisation settings of O2 this fails and a random value is returned.  Accessing the Count() method a second time results in 0 being returned in both cases.  Here are two test runs:

With -O1:
% ./a.out 
Count: 0x6000008a4010 0 <-- First access successful
Count: 0x6000008a4010 0 <-- Second access successful

With -O2:
% ./a.out 
Count: 0x6000004ec010 15 <-- First access fails
Count: 0x6000004ec010 0 <-- Second access successful

This occurs on Mac OS M1 with Apple clang version 14.0.0 (clang-1400.0.29.102), on Ubuntu with Ubuntu clang version 14.0.0-1ubuntu1 and on Mac OS x86_64 with Apple clang version 14.0.0 (clang-1400.0.29.102).  Strangely, on Mac OS x86_64 it only occurs if I enable extra Printf() debugging in the test (see T_StringList.cpp).

I have compiled the test to assembly language with -O1 and -O2 and have annotated the files and found an instance where the edx register is not loaded correctly with -O2.  See the files T_StringList.O1.s and T_StringList.O2.s.  These were compiled on Mac OS x86_64.

My apologies that I could not make the example smaller - it really needs these very specific circumstances to recreate the bug.  But you should only need to look in T_StringList.cpp and StdStringList.h.  There is also a README.md included in the archive.

Many thanks for your help and don't hesitate to ask for more information.  It was some work to track this down and make a good test case.  :-)

[Optimiser.zip](https://github.com/llvm/llvm-project/files/9906406/Optimiser.zip)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJydVtlu4zYU_Rr7hbBAyUviBz84mRl0gAYZNFP0cUBRlMWGEj1c4rhf33NJOYmDZNA0MCJxu8u5h-eqts1x880o4RVr9dAwEYKQncIL6_Wge2FYxJqkDaETAW8YeyaNGHaT6go_FizbO9tEqZgepHVOSeyzDQ1h50G5I_N7JXWrJZPaydj7IAapCsa-dzBuh0YHbQfPhFOT-XbCP034-L-cVGv2lXXiQcHW9egS7r1nBx06TEob90Yx27IH4bSojfL5bEVnk4e0vROeDZbc-eCiDNaxSXWp-711CCewGRNNo4ddMvm8xylvzQNyDrCkvY8KZrODOTnYjub1kLPCHiaMsVIE4JhjHJgFDMaKBlN2r5wgy4M6ZDuLU6Dv7GJehewfgQU1YIBsafzsqFe9BdAoBn-J351SaWOrAdFdaO6CQ4q_ax-KjrUwH57wQbV-RuWpEgXbtkG5k3nMTKprJqRU3hNCCYoxX8-ubRwCoKQsehU62zDf2WgaYBeiSyQQJqYScRT9rxMm-6B7n8xThoEsY8ttCfsA0Uf4U41_fUKfH0lY3Fb5TCu0AfzEZObwsP3oGms5GNXA3vYskzfiF7BOvGTwp4gC0YSEEGe1onMnYzRXW0RHV4RC_U05RTxm4WBZAJzMxcG_YnXKZ3ZbPk9XS1ZMqi-isDGwPJfDmm8Zf1xx-rsUC15yhDCZX89m7It2sJ6LksHyvo3mvx2-y-m9c_osyupjUS6UJEfl8q0wU31-ffAjEX6nmlspo8NjYDdCsts7dlPma7fdky4kqSIV8kSaclHwgtPFT_OzcsExLqp1UXLSC-I5tv1ZI7iYzYzvb9mZlTEtlolyzxE8Xq5-rBb_PwrwCBcVK8ocx4jOLeuAOXM85a5baKQaSPuYesRJ9g3XPLQjqRtVx92OaAu2EuMTL7HooQ7ff7zQBLnfk_uXGI_iK22_h4Y0z-chNZAN1deIg7KIYqdyymB2AgTcSc-s3sNgQ5Kqk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