https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96621
Bug ID: 96621 Summary: fold strlen relational expressions after nul stores Product: gcc Version: 11.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: --- strlen(s) <= N can be folded to true after nul has been stored into s[N]. $ cat t.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout t.c void f0 (char *s) { s[0] = 0; if (__builtin_strlen (s) > 0) // folded to false __builtin_abort (); } void f1 (char *s) { s[1] = 0; if (__builtin_strlen (s) > 1) // not folded but can be __builtin_abort (); } ;; Function f0 (f0, funcdef_no=0, decl_uid=1931, cgraph_uid=1, symbol_order=0) f0 (char * s) { <bb 2> [local count: 1073741824]: *s_2(D) = 0; return; } ;; Function f1 (f1, funcdef_no=1, decl_uid=1934, cgraph_uid=2, symbol_order=1) f1 (char * s) { long unsigned int _1; <bb 2> [local count: 1073741824]: MEM[(char *)s_2(D) + 1B] = 0; _1 = __builtin_strlen (s_2(D)); if (_1 > 1) goto <bb 3>; [0.00%] else goto <bb 4>; [100.00%] <bb 3> [count: 0]: __builtin_abort (); <bb 4> [local count: 1073741824]: return; }