https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91147
Bug ID: 91147 Summary: strlen of conditional plus index in known range not folded Product: gcc Version: 9.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 in get_range_strlen in gimple-fold.c successfully handles the case in f() below but misses the equivalent one in g(). $ cat b.c && gcc -O2 -S -Wall -Wextra -Wpedantic -fdump-tree-optimized=/dev/stdout b.c const char a3[] = "123"; const char a5[] = "12345"; void f (int i, int j) { if (j < 1) j = 1; unsigned n; if (i) n = __builtin_strlen (a3 + j); else n = __builtin_strlen (a5 + j); if (n > 4) // folded to false __builtin_abort (); } void g (int i, int j) { if (j < 1) j = 1; const char *p = i ? a3 : a5; if (__builtin_strlen (p + j) > 4) // not folded but could be __builtin_abort (); } ;; Function f (f, funcdef_no=0, decl_uid=1911, cgraph_uid=1, symbol_order=2) f (int i, int j) { <bb 2> [local count: 1073741824]: return; } ;; Function g (g, funcdef_no=1, decl_uid=1916, cgraph_uid=2, symbol_order=3) Removing basic block 3 g (int i, int j) { sizetype _1; const char * _2; long unsigned int _3; const char * iftmp.4_4; <bb 2> [local count: 1073741824]: j_6 = MAX_EXPR <j_5(D), 1>; if (i_7(D) != 0) goto <bb 4>; [50.00%] else goto <bb 3>; [50.00%] <bb 3> [local count: 536870912]: <bb 4> [local count: 1073741824]: # iftmp.4_4 = PHI <&a3(2), &a5(3)> _1 = (sizetype) j_6; _2 = iftmp.4_4 + _1; _3 = __builtin_strlen (_2); if (_3 > 4) goto <bb 5>; [0.00%] else goto <bb 6>; [100.00%] <bb 5> [count: 0]: __builtin_abort (); <bb 6> [local count: 1073741824]: return; }