https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83693
Bug ID: 83693 Summary: missing strlen optimization for array of arrays Product: gcc Version: 8.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- The strlen optimization is ineffective for constant arrays of arrays like in the case of b and c below. It looks like the string_constant() function in expr.c that strlen relies on isn't equipped handle multi-dimensional arrays. $ cat z.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout z.c const char a[4] = "012"; const char b[][4] = { "012", "234" }; const char c[][2][4] = { { "012", "234" }, { "345", "456" } }; void f0 (void) { if (__builtin_strlen (a) != 3) // folded __builtin_abort (); } void f1 (void) { if (__builtin_strlen (b[1]) != 3) // not folded __builtin_abort (); } void f2 (void) { if (__builtin_strlen (c[1][1]) != 3) // not folded __builtin_abort (); } ;; Function f0 (f0, funcdef_no=0, decl_uid=1955, cgraph_uid=0, symbol_order=3) f0 () { <bb 2> [local count: 1073741825]: return; } ;; Function f1 (f1, funcdef_no=1, decl_uid=1958, cgraph_uid=1, symbol_order=4) f1 () { long unsigned int _1; <bb 2> [local count: 1073741825]: _1 = __builtin_strlen (&b[1]); if (_1 != 3) goto <bb 3>; [0.00%] else goto <bb 4>; [99.96%] <bb 3> [count: 0]: __builtin_abort (); <bb 4> [local count: 1073312327]: return; } ;; Function f2 (f2, funcdef_no=2, decl_uid=1961, cgraph_uid=2, symbol_order=5) f2 () { long unsigned int _1; <bb 2> [local count: 1073741825]: _1 = __builtin_strlen (&c[1][1]); if (_1 != 3) goto <bb 3>; [0.00%] else goto <bb 4>; [99.96%] <bb 3> [count: 0]: __builtin_abort (); <bb 4> [local count: 1073312327]: return; }