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

    <tr>
        <th>Summary</th>
        <td>
            Missed optimization: Reorder deallocations before allocations of unrelated memory
        </td>
    </tr>

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

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

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

<pre>
    Note: I am not certain which language standards allow this optimization, if any.

If we could move deallocations to occur before allocations of unrelated memory, it would reduce peak memory usage and potentially improve cache performance.

In other words, in the following example I would like to transform `bad` into `good`.

```cpp
int * bad(int * ptr) {
        auto temp = static_cast<int *>(operator new(10 * sizeof(int)));
        operator delete(ptr);
        return temp;
}

int * good(int * ptr) {
        operator delete(ptr);
        return static_cast<int *>(operator new(10 * sizeof(int)));
}
```

https://godbolt.org/z/WGq1jsdGd

With similar ability for things like `malloc` and `free`.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVE-PozYU_zTm8rQRmIHAgUN206zm0B562WNl7Ad41_hR-zFp5tNXJslOZg5VK1WyQP7z-yc_PxWjHT1iJ6rPojpmauWJQmfUizWRyWPWk7l0vxGjKA_wDGoGTwwaAyvr4TxZPYFTflzViBBZeaOCiaCcozPwZCPQwna2r4oteSG_gB1A-ctO5EeRH67f5wHOCJpWZ2CmFwSDiUBvmAhMQFqvAXocKCA87tEAqw_oFKOBGWcKl02E4bzRBTSrRlhQ_bhtwxqTV-UNLMTo2SrnLmDnJSRprfSUzoeBwqy8xvdOPRBPGOBMwcRNyQNPCAOlxNaPgH-peXEIzzcHzv7AFIGD8jGRgqjzXhlR52A9U5qORGn-TkrU-XXoZbmuWM8g5AESWDb32cJByBbE_vMd3ao16eG8gCiP6VbY6j-0iizKLzeYKH8RsqEFg2IK4PEsZFPkG2O0r0jDVULI9jbKN_6fMIMOGYVsriYezwTkNfjNxdvy_viY8J5gS_-Pgf6D4P-d9qfl-3U8JpiYlyjKg5AnIU8jmZ4c7yiMQp5ehTx9-_pn8T2ar-YR9M3yBNHO1qkAqrfO8gUGCum5-DFeC0bU-bzVeSqTVKuizoeAmKokM11p2rJVGXZF3dbV077ZN9nU9bpXtWr3Kq913xdFk8t231Z90ZembArMbCdzWRZ5Xsmy2FfNrmh12aI0GitTlb0UTznOyrqdcy9zCpLZGFfs6qasq8ypHl3cmoWUHs-wbQopU-8IXcJ86tcxiqfc2cjxjYUtO-x-tTGied8RygP8jhQMhg-v_t899mwNrvtwDZantd9pmoU8JQe336cl0HfULORp8x2FPG25_g4AAP__SCSW2w">