https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92501
Bug ID: 92501 Summary: strncmp with constant unterminated arrays not folded Product: gcc Version: 10.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: --- GCC 9 and later fold strncmp calls with constant strings as arguments but it doesn't optimize equivalent calls when one or both arguments is an unterminated array. The example below shows the difference. Clang folds both. $ cat z.c && gcc -O2 -c -Wall -Wextra -fdump-tree-optimized=/dev/stdout z.c const char a[] = { '1', '2', '3', '\0' }; const char b[] = { '1', '2', '3' }; void f (void) { if (__builtin_strncmp (a, "123", 3)) __builtin_abort (); } void g (void) { if (__builtin_strncmp (b, "123", 3)) __builtin_abort (); } ;; Function f (f, funcdef_no=0, decl_uid=1932, cgraph_uid=1, symbol_order=2) f () { <bb 2> [local count: 1073741824]: return; } ;; Function g (g, funcdef_no=1, decl_uid=1935, cgraph_uid=2, symbol_order=3) g () { int _1; <bb 2> [local count: 1073741824]: _1 = __builtin_strncmp (&b, "123", 3); if (_1 != 0) goto <bb 3>; [0.00%] else goto <bb 4>; [100.00%] <bb 3> [count: 0]: __builtin_abort (); <bb 4> [local count: 1073741824]: return; }