https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105883
Bug ID: 105883 Summary: Memcmp folded only when size is a power of two Product: gcc Version: 13.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: denis.campredon at gmail dot com Target Milestone: --- Compiled with trunk and -O2, on x86-64, the testcase bellow shows, the calls to memcmp are folded just when the size is a power of two. Other archs seems to produce unoptimal code too. If the arrays are declared const, everything is optimized as expected. ------- template <int I, typename T> bool equals() { T a[16] = {1,2,3,4,5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; T b[16] = {1,2,3,4,5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; static_assert(I <= sizeof a); return !__builtin_memcmp(a, b, I); } template <int I, typename T> bool not_equals() { T a[16] = {1,2,3,4,5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; T b[16] = {0,2,3,4,5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; static_assert(I <= sizeof a); return !__builtin_memcmp(a, b, I); } #define TEST \ template bool equals<1, char>(); \ template bool equals<2, char>(); \ template bool equals<3, char>(); \ template bool equals<4, char>(); \ template bool equals<5, char>(); \ template bool equals<7, char>(); \ template bool equals<8, char>(); \ template bool equals<15, char>(); \ template bool equals<16, char>(); \ template bool equals<1, long long>(); \ template bool equals<2, long long>(); \ template bool equals<3, long long>(); \ template bool equals<4, long long>(); \ template bool equals<5, long long>(); \ template bool equals<7, long long>(); \ template bool equals<8, long long>(); \ template bool equals<16, long long>(); \ template bool equals<17, long long>(); \ template bool equals<31, long long>(); \ template bool equals<32, long long>(); \ template bool equals<63, long long>(); \ template bool equals<64, long long>(); \ template bool equals<127, long long>(); \ template bool equals<128, long long>(); TEST #define equals not_equals TEST