[Bug tree-optimization/78257] missing memcmp optimization with constant arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78257 --- Comment #12 from CVS Commits --- The master branch has been updated by Martin Sebor : https://gcc.gnu.org/g:d367f5fcb579d21c3093cf5c464f5787fe584a1d commit r11-2743-gd367f5fcb579d21c3093cf5c464f5787fe584a1d Author: Martin Sebor Date: Tue Aug 18 12:57:18 2020 -0600 PR middle-end/96665 - memcmp of a constant string not folded Related: PR middle-end/78257 - missing memcmp optimization with constant arrays gcc/ChangeLog: PR middle-end/96665 PR middle-end/78257 * expr.c (convert_to_bytes): Replace statically allocated buffer with a dynamically allocated one of sufficient size. gcc/testsuite/ChangeLog: PR middle-end/96665 PR middle-end/78257 * gcc.dg/memcmp-5.c: New test.
[Bug tree-optimization/78257] missing memcmp optimization with constant arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78257 --- Comment #11 from CVS Commits --- The master branch has been updated by Martin Sebor : https://gcc.gnu.org/g:bb04901d14f7749eb949092fd3dfcb6ca1958701 commit r11-2742-gbb04901d14f7749eb949092fd3dfcb6ca1958701 Author: Martin Sebor Date: Tue Aug 18 12:49:35 2020 -0600 Fix PR tree-optimization/96670 - ICE on memchr with an empty initializer. gcc/ChangeLog: PR tree-optimization/96670 PR middle-end/78257 * gimple-fold.c (gimple_fold_builtin_memchr): Call byte_representation to get it, not string_constant. gcc/testsuite/ChangeLog: PR tree-optimization/96670 * gcc.dg/memchr-2.c: New test. * gcc.dg/memcmp-6.c: New test.
[Bug tree-optimization/78257] missing memcmp optimization with constant arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78257 Martin Sebor changed: What|Removed |Added Known to work||11.0 Target Milestone|--- |11.0 Resolution|--- |FIXED Keywords||patch Assignee|unassigned at gcc dot gnu.org |msebor at gcc dot gnu.org Status|NEW |RESOLVED Known to fail||10.2.0, 7.3.0, 8.2.0, 9.2.0 --- Comment #10 from Martin Sebor --- Done for GCC 11.
[Bug tree-optimization/78257] missing memcmp optimization with constant arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78257 --- Comment #9 from CVS Commits --- The master branch has been updated by Martin Sebor : https://gcc.gnu.org/g:866626efd749ed3e2b7014e88e4340b5a4c73560 commit r11-2709-g866626efd749ed3e2b7014e88e4340b5a4c73560 Author: Martin Sebor Date: Fri Aug 14 17:11:53 2020 -0600 PR tree-optimization/78257 - missing memcmp optimization with constant arrays gcc/ChangeLog: PR middle-end/78257 * builtins.c (expand_builtin_memory_copy_args): Rename called function. (expand_builtin_stpcpy_1): Remove argument from call. (expand_builtin_memcmp): Rename called function. (inline_expand_builtin_bytecmp): Same. * expr.c (convert_to_bytes): New function. (constant_byte_string): New function (formerly string_constant). (string_constant): Call constant_byte_string. (byte_representation): New function. * expr.h (byte_representation): Declare. * fold-const-call.c (fold_const_call): Rename called function. * fold-const.c (c_getstr): Remove an argument. (getbyterep): Define a new function. * fold-const.h (c_getstr): Remove an argument. (getbyterep): Declare a new function. * gimple-fold.c (gimple_fold_builtin_memory_op): Rename callee. (gimple_fold_builtin_string_compare): Same. (gimple_fold_builtin_memchr): Same. gcc/testsuite/ChangeLog: PR middle-end/78257 * gcc.dg/memchr.c: New test. * gcc.dg/memcmp-2.c: New test. * gcc.dg/memcmp-3.c: New test. * gcc.dg/memcmp-4.c: New test.
[Bug tree-optimization/78257] missing memcmp optimization with constant arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78257 --- Comment #8 from Martin Sebor --- With r263561 brace-enclosed initializers for constant character arrays are handled by transforming them into STRING_CSTs. It should be possible to also handle wide character types (wchar_t, char16_t, char32_t). Handling arrays of arbitrary integers will probably take a different approach.
[Bug tree-optimization/78257] missing memcmp optimization with constant arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78257 Martin Liška changed: What|Removed |Added Assignee|marxin at gcc dot gnu.org |unassigned at gcc dot gnu.org --- Comment #7 from Martin Liška --- Leaving unassigned as I'm not planning to working on that.
[Bug tree-optimization/78257] missing memcmp optimization with constant arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78257 Martin Sebor changed: What|Removed |Added See Also||https://gcc.gnu.org/bugzill ||a/show_bug.cgi?id=12086 --- Comment #6 from Martin Sebor --- See also bug 12086 for a related enhancement to memcmp.
[Bug tree-optimization/78257] missing memcmp optimization with constant arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78257 --- Comment #5 from Martin Liška --- (In reply to Martin Sebor from comment #4) > Created attachment 40008 [details] > Very lightly tested patch. > > The attached (only superficially tested) patch changes fold_bultin_memcmp to > fold the comparison over the length of the arrays rather than just the first > pair of bytes. Does it look reasonable? I played a bit with the patch and it's nice that it can handle(In reply to Martin Sebor from comment #4) > Created attachment 40008 [details] > Very lightly tested patch. > > The attached (only superficially tested) patch changes fold_bultin_memcmp to > fold the comparison over the length of the arrays rather than just the first > pair of bytes. Does it look reasonable? I played a bit with the patch and it ICEs with: #include #include const int i[] = { 16843009 }; const char k[] = {1, 1, 1, 1}; int main() { unsigned test = sizeof(i) * 1; assert (__builtin_memcmp (i, k, test) == 0); return 0; } and without an optimization level, it can produce tons of assembly code: #include #include #define ctor(N) {N, N, N, N, N, N, N, N, N, N, N, N} const int i[] = ctor(123456); const int k[] = ctor(123456); int main() { assert (__builtin_memcmp (i, k, sizeof i) == 0); return 0; } folding2.c.004t.gimple: i.1_1 = &i; _2 = MEM[(const unsigned char * {ref-all})i.1_1]; k.2_3 = &k; _4 = MEM[(const unsigned char * {ref-all})k.2_3]; if (_2 == _4) goto ; else goto ; : _5 = &i + 1; _6 = *_5; _7 = &k + 1; _8 = *_7; if (_6 == _8) goto ; else goto ; : _9 = &i + 2; _10 = *_9; _11 = &k + 2; _12 = *_11; if (_10 == _12) goto ; else goto ; : _13 = &i + 3; _14 = *_13; _15 = &k + 3; _16 = *_15; if (_14 == _16) goto ; else goto ; : _17 = &i + 4; _18 = *_17; _19 = &k + 4; _20 = *_19; Martin
[Bug tree-optimization/78257] missing memcmp optimization with constant arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78257 --- Comment #4 from Martin Sebor --- Created attachment 40008 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=40008&action=edit Very lightly tested patch. The attached (only superficially tested) patch changes fold_bultin_memcmp to fold the comparison over the length of the arrays rather than just the first pair of bytes. Does it look reasonable?
[Bug tree-optimization/78257] missing memcmp optimization with constant arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78257 --- Comment #3 from Martin Liška --- 'f0' function is optimized out as of r242000
[Bug tree-optimization/78257] missing memcmp optimization with constant arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78257 Martin Liška changed: What|Removed |Added CC||marxin at gcc dot gnu.org Assignee|unassigned at gcc dot gnu.org |marxin at gcc dot gnu.org --- Comment #2 from Martin Liška --- (In reply to Richard Biener from comment #1) > Confirmed. Note handling the terminating nul is likely some off-by-one > error somewhere. Yes, I'm having patch for that, that was trivial to fix. > > Handling arrays of integers (arrays in general) is harder as we do not store > the constructors in target representation (same for strings written as > { 'a', 'b', '\0' } btw.). One can't use the host library routines to perform > the folding but has to compare two CONSTRUCTORs elementwise (they are at > least > sorted). That would be harder, I'm wondering Richi how would it be difficult to transform a ctor data to a binary blob we can use for memcmp, memchr, etc. ? Element wise comparison may be doable, however I'm wondering how usefull the job would be?
[Bug tree-optimization/78257] missing memcmp optimization with constant arrays
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78257 Richard Biener changed: What|Removed |Added Keywords||missed-optimization Status|UNCONFIRMED |NEW Last reconfirmed||2016-11-09 CC||mliska at suse dot cz Ever confirmed|0 |1 --- Comment #1 from Richard Biener --- Confirmed. Note handling the terminating nul is likely some off-by-one error somewhere. Handling arrays of integers (arrays in general) is harder as we do not store the constructors in target representation (same for strings written as { 'a', 'b', '\0' } btw.). One can't use the host library routines to perform the folding but has to compare two CONSTRUCTORs elementwise (they are at least sorted).