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

    <tr>
        <th>Summary</th>
        <td>
            Missing optimization for return vector used inside a function.
        </td>
    </tr>

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

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

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

<pre>
    
Optimization that removes vector allocations works correctly in simple cases. When the code becomes more complex, optimization is disabled.

`
#include <vector>
#include <iostream>


int foo() {        
    std::vector<int> vec1 = {16,2,77,29};
    return vec1[3]; 
} // OK

int bar() {        
 std::vector<int> vec1 = {16,2,77,29};
    std::cout << vec1[0]<< vec1[1]; 
 return 0; 
} // OK

int baz() {        
    std::vector<int> vec1 = {16,2,77,29};
    std::cout << vec1[0]<< vec1[1]; 
    return vec1[3]; 
} // Return value of part object broken remove allocation in vector implementation

`


The baz function preserves memory allocation which can be  optimized.

https://godbolt.org/z/xz9jxr8GP

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJy0VF1v3CgU_TXXL1cZYbBj-8EP85HZlVarrFYr7TPG12MSbEaAJ5P59RWuJ5m0lVq1qoUM4sDhcDggvdeHkaiGfAP5LpFT6K2r14bOBx3-nJrYShrbvtbA1o_HoAd9kUHbEUMvAzoa7Ik8nkgF61AaY9UMe3yx7tmjss6RCuYV9YheD0dDqKQnv8L_e4oshMq2hA0pO5DHwbrYEweegW_R3q6pPbbay8ZQuwK2juWexT8XelRmaglBbD-LAfHwFaKtD47ksGBz0WPAzlrgJfAKodjg8gFbx8qHFsQaxPpKu9VjAPEQ95wiiF2ck94D33Lg26KIjQqKHYjNwuAoTG6cx0O-EZBHLNJDsUPge-B7fPzrXU0j3bfV_LqUNwZlpxAdAbG9CmOzsNue9F3qdRPse9Ivv9HIn1f_g6fw7zJGmonQdniULqBtnkgFbJx9pnFJ_E3SY7CX-M_xHmgMM_AhoHP5r6foEHbTqOapR0eeXLxAAw3Wvd7SvvRa9ajkiA3h9Rq85b4P4eijGbPwg20ba8LKugPw_QX4_nypns6u_OMfYOukrUVbiUomVKdFLipWVGmR9DXPSt6pnDVdVom0VCnP0k6lZVbIMuWtSnTNGc9ZIRhnohTFqpOUdZzaosxYw0UFGaNBarMy5jTE5RPt_UR1mqeiYomRDRk_Py6cj_SCMwqcx7fG1XHSXTMdPGTMaB_8O03QwVD9t_Zej4ePj0Bn3c1hRt8nTy3q0euWUL65u0omZ-ovjNKhn5qVsgPwfVxsqe6OzsZTBr6fJXrg-2UPp5p_CgAA__8-topF">