https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82789
Bug ID: 82789 Summary: Invalid code generated for std::array element-wise processing with -O3 Product: gcc Version: lto Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: nazarenkoal at gmail dot com Target Milestone: --- Version: $ gcc --version gcc (Ubuntu 7.2.0-8ubuntu3) 7.2.0 Not reproduced on: gcc-6 (Ubuntu 6.4.0-8ubuntu1) 6.4.0 20171010 gcc-5 (Ubuntu 5.5.0-1ubuntu1) 5.4.1 20171010 OS: $ lsb_release -a LSB Version: core-9.20160110ubuntu5-amd64:core-9.20160110ubuntu5-noarch:security-9.20160110ubuntu5-amd64:security-9.20160110ubuntu5-noarch Distributor ID: Ubuntu Description: Ubuntu 17.10 Release: 17.10 Codename: artful Commandline: gcc -DNDEBUG -O3 ./test_opt_2.cpp -o ./test_opt_2 -std=c++14 -lstdc++ Code: #include <array> #include <iostream> template <typename A> A l_and_r( A const & l, A const & r ) { A res = {0}; for ( unsigned i=0; i<l.size(); ++i ) { res[ i ] = l[ i ] & r[ i ]; } return res; } template <typename A> __attribute__ ((noinline)) A noopt_proxy( A const & a ) { return A(a); } int main () { std::array<unsigned char, 4> l = {0x01, 0x02, 0x04, 0x08 }; std::array<unsigned char, 4> r = {0x2f, 0x07, 0x0f, 0x1f }; auto ret_1 = l_and_r( noopt_proxy(l), noopt_proxy(r) ); std::cout << "l_and_r( l, r ): " << std::hex << (int)ret_1[0] << " " <<(int)ret_1[1] << " " << (int)ret_1[2] << " " << (int)ret_1[3] << std::endl; return 0; } Program output with -O0, -O1, -O2 l_and_r( l, r ): 1 2 4 8 Program output with -O3 l_and_r( l, r ): 2f 2 4 8