https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83899
Bug ID: 83899 Summary: missing strlen optimization for constant string plus offset 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: --- Thanks to r255790, GCC 8 is able to fold the conditional in the first function based on the size of the array, but it cannot perform the similar optimization for constant strings. $ cat z.c && gcc -O1 -S -fdump-tree-optimized=/dev/stdout z.c extern char a[4]; void f (int i) { const char *s = a; __SIZE_TYPE__ n = __builtin_strlen (s); if (n >= 4) // folded to false (GCC 8) __builtin_abort (); } void g (int i) { const char* const s = "123" + i; __SIZE_TYPE__ n = __builtin_strlen (s); if (n >= 4) // not folded __builtin_abort (); } ;; Function f (f, funcdef_no=0, decl_uid=1951, cgraph_uid=0, symbol_order=0) f (int i) { <bb 2> [local count: 1073741825]: return; } ;; Function g (g, funcdef_no=1, decl_uid=1956, cgraph_uid=1, symbol_order=1) g (int i) { long unsigned int n; const char * const s; sizetype _1; <bb 2> [local count: 1073741825]: _1 = (sizetype) i_2(D); s_3 = "123" + _1; n_5 = __builtin_strlen (s_3); if (n_5 > 3) goto <bb 3>; [0.00%] else goto <bb 4>; [99.96%] <bb 3> [count: 0]: __builtin_abort (); <bb 4> [local count: 1073312327]: return; }